ARKit:将SCNText放置在相机前面的特定位置 [英] ARKit: Placing an SCNText at a particular point in front of the camera

查看:69
本文介绍了ARKit:将SCNText放置在相机前面的特定位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法在指向照相机的表面上放置了一个立方体(SCNNode),但是我发现很难完成将文本放置在同一位置的简单(?)任务.

我已经创建了SCNText和后续的SCNNode,但是,当我将其添加到rootNode时,该文本似乎总是添加到我的头顶上方,并在右侧的摄像头旁边(这告诉我这是全局原点)./p>

即使我使用与多维数据集完全相同的位置值,SCNText节点仍会放在同一位置的头顶上方.

很抱歉,如果这是一个基本问题,我以前从未在SceneKit中工作过.

解决方案

SCNGeometry的坐标中心是其中心点.但是,当您创建SCNText时,中心点位于左下角的某个地方:

您需要先将文本居中.这可以通过选中包含文本的节点的边界框并设置枢轴转换以将文本中心更改为其实际中心来实现:

func center(node: SCNNode) {
    let (min, max) = node.boundingBox

    let dx = min.x + 0.5 * (max.x - min.x)
    let dy = min.y + 0.5 * (max.y - min.y)
    let dz = min.z + 0.5 * (max.z - min.z)
    node.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
}


还请注意此答案,其中解释了一些其他陷阱: 字体大小为16磅的文本的高度为16个SceneKit单位.但是在ARKit中1个SceneKit单位= 1米!

I've managed to get a cube (SCNNode) placed on a surface where the camera is pointed, however I am finding it very difficult to do the simple (?) task of also placing text in the same position.

I've created the SCNText and subsequent SCNNode, however when I add it to the rootNode the text always seems to be added above my head and off the camera to the right (which tells me thats the global origin point).

Even when I use the exact same values of position I used for the the cube, the SCNText node still gets placed above my head in the same spot.

Apologies if this is a basic question, I've never worked in SceneKit before.

解决方案

The coordinate center for an SCNGeometry is its center point. But when you are creating a SCNText the center point is somewhere in the bottom left corner:

You need to center the text first. This can be done by checking the bounding box of the node containing your text and setting a pivot transform to change the texts center to its actual center:

func center(node: SCNNode) {
    let (min, max) = node.boundingBox

    let dx = min.x + 0.5 * (max.x - min.x)
    let dy = min.y + 0.5 * (max.y - min.y)
    let dz = min.z + 0.5 * (max.z - min.z)
    node.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
}


Edit:

Also note this answer that explains some additional pitfalls: A text with 16 pts font size is 16 SceneKit units tall. But in ARKit 1 SceneKit units = 1 meter!

这篇关于ARKit:将SCNText放置在相机前面的特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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