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

查看:32
本文介绍了使用 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?

推荐答案

这里至少有两个问题.

如果您将节点的位置设置为与相机的位置相匹配,您可能看不到该节点的任何内容.你想把东西放在相机前面以便它们被看到.相机总是在其本地空间的 -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.

一个简单的方法是设置一个scalepivot 在使其内容更小的节点上.

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天全站免登陆