Add SCNText to SCNScene with ARKit

元气小坏坏 提交于 2020-01-22 09:57:25

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/44733842/add-scntext-to-scnscene-with-arkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!