ARKit:在屏幕上找到SCNNode的坐标

9
我有一个简单的Swift ARKit设置,其中我有一个SCNNode和一个3D对象,它在ARSCNView中可见。
我想确定此对象在ARSCNView上的2D坐标。我的意思是当对象绘制到屏幕上时,其x和y坐标。我提供了一个草图以说明我的意思: Sketch 是否有一种方法可以获取这些坐标,或者至少近似估计?我需要这个来对相机帧进行进一步处理。基本上,我对对象占据屏幕上的区域感兴趣。

你看过 ARCamera 的 viewMatrix 方法了吗?https://developer.apple.com/documentation/arkit/arcamera/2921672-viewmatrix - halileohalilei
5
这不就是 projectPoint 的作用吗? - leandrodemarco
@leandrodemarco 是的,这正是我正在寻找的。 - AscendingEagle
1个回答

10
您可以使用 SCNSceneRenderer projectPoint:point

将场景的3D世界坐标系中的一个点投影到渲染器的2D像素坐标系中。

let node:SCNNode = // Your node
let nodeWorldPosition = node.position
let nodePositionOnScreen = renderer.projectPoint(nodeWorldPosition)
let x = nodePositionOnScreen.x
let y = nodePositionOnScreen.y

注意:从近裁剪面(或远裁剪面)投影出的点将具有z分量为0(或1)。


同样的方法也可以用于ARAnchor

let anchor:ARAnchor = // ...
let anchorWorldPosition = SCNVector3(anchor.transform.columns.3)
let anchorPositionOnScreen = renderer.projectPoint(anchorWorldPosition)
let x = anchorPositionOnScreen.x
let y = anchorPositionOnScreen.y

有没有办法让它与ARImageAnchor一起工作? - Scalpweb
ARImageAnchorARAnchor 的子类,所以我认为它应该工作方式相同,不过我没有测试过。 - Axel Guilmin
成员变量“projectPoint”的引用无法在没有上下文类型的情况下解析。 - Ahmadreza
1
请注意,ARSCNView符合SCNSceneRenderer协议,因此您可以直接使用projectPoint:point:方法并将其引用到ARSCNView。对于刚接触SceneKit的人来说,这可能不是很明显。 - markckim
这行代码:let nodeWorldPosition = node.position 应该改为:let nodeWorldPosition = node.worldPosition。 - us_david

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接