使用ARKit将SCNText添加到SCNScene [英] Add SCNText to SCNScene with ARKit

查看:136
本文介绍了使用ARKit将SCNText添加到SCNScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习ARKitexample和Scenekit.我阅读了一些Scenekit,发现为了添加文本,我需要使用SCNText.

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)

我只想在用户按下按钮时在SCNScene上添加一些文本(例如"hello world").

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

修改

我看到了该文本,但是由于我没有设置平面(或锚点),因此无法像看在该文本前面那样看它.我能怎么做?

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?

推荐答案

您在这里至少有两个问题.

You have at least two problems here.

如果将节点的位置设置为与摄像机的位置匹配,则可能看不到该节点的任何内容.您希望将物体放置在相机的前面,以便可以看到它们.摄像机始终沿其本地空间的-z方向看.有很多方法可以进行必要的数学运算,但是这可能很方便(在手机上进行了编码,因此在YMMV中进行了编码):

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

这应将您的物体放在相机前方半米处(或更确切地说,此时相机在哪里-它不会跟随相机).之所以有效,是因为 simdWorldFront 是向量(0,0, -1),在本地空间中是指摄影机节点指向的方向,已从本地空间转换为世界空间.

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.

SCNText的默认字体大小约为16.但这是以场景单位表示的,场景单位映射到ARKit中的仪表.同样,文本框"固定在其左下角.因此很可能看不到您的文字,因为它高16米,在您的右边.

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.

一种简单的处理方法是设置 scale 或节点上的 pivot 较小.

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

这篇关于使用ARKit将SCNText添加到SCNScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆