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

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

问题描述

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

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.

我已经创建了 SCNText 和后续的 SCNNode,但是当我将它添加到 rootNode 时,文本似乎总是被添加到我的头顶和右侧的相机(这告诉我那是全局原点).

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).

即使我使用与立方体完全相同的位置值,SCNText 节点仍然放置在我头顶的同一位置.

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.

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

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

推荐答案

SCNGeometry 的坐标中心是它的中心点.但是当你创建一个 SCNText 时,中心点在左下角的某个地方:

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)
}

<小时>

另请注意 这个答案 解释了一些额外的陷阱:16 pts 字体大小的文本是 16 个 SceneKit 单位高.但是在 ARKit 1 SceneKit 单位 = 1 米!

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