Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add SCNText to SCNScene with ARKit

Tags:

scenekit

arkit

I just started studying for ARKitexample and Scenekit. I read a few Scenekit and found out that in order to add text, I need to use SCNText.

I try to write like this but it doesn't show.

    guard let pointOfView = sceneView.pointOfView else { return }

    let text = SCNText(string: "Hello", extrusionDepth: 4)
    let textNode = SCNNode(geometry: text)
    textNode.geometry = text
    textNode.position = SCNVector3Make(pointOfView.position.x, pointOfView.position.y, pointOfView.position.z)
    sceneView.scene.rootNode.addChildNode(textNode)

I just want to add some text (like "hello world") on SCNScene when user press button.

Edit

I saw that text but since I haven't set up plane (or anchor), I can't look at that as if I am in front of that text. How can I do?

like image 770
Khant Thu Linn Avatar asked Jun 24 '17 07:06

Khant Thu Linn


1 Answers

You have at least two problems here.


If you set a node's position to match that of the camera, you probably won't see any of that node's content. You want to position things in front of the camera for them to be seen. A camera always looks in the -z direction of its local space. There's a ton of ways to do the requisite math, but here's one that might be handy (coded on phone, so YMMV):

textNode.simdPosition = pointOfView.simdPosition + pointOfView.simdWorldFront * 0.5

This should put your object half a meter in front of the camera (or rather, where the camera is at that moment — it won't follow the camera). It works because simdWorldFront is the vector (0,0,-1), which in local space means the direction the camera node points, converted from local space to world space.


The default font size for SCNText is something like 16. But that's in scene units, and scene units map to meters in ARKit. Also, the "text box" is anchored at its lower left. So quite likely your text isn't visible because it's sixteen meters tall and off to your right.

An easy way to handle this is by setting a scale or pivot on the node that makes its contents much smaller.

like image 103
rickster Avatar answered Nov 10 '22 15:11

rickster