ARKit:在屏幕上查找SCNNode的坐标 [英] ARKit: Finding the coordinates of a SCNNode on the screen
问题描述
我有一个简单的Swift ARKit设置,其中有一个带有3D对象的SCNNode,该对象在ARSCNView中可见.
I have a simple Swift ARKit setup where I have a SCNNode with a 3D object that is visible in an ARSCNView.
我想确定该对象在ARSCNView上的2D坐标.我的意思是指将对象绘制到屏幕上时的x和y坐标.
I want to determine the 2D coordinates of this object on the ARSCNView. By this I mean the x- and y-coordinates the object has when it is drawn onto the screen.
我提供了一个草图来说明我的意思:
I have provided a sketch to illustrate what I mean:
有没有办法获得这些坐标,或者至少是一个近似值?我需要这样做,以便对相机框架做进一步的处理.基本上,我对对象在屏幕上占据的区域感兴趣.
Is there a way to get these coordinates, or at least an approximation? I need this in order to do some further processing with the camera frame. Basically I am interested in the area the object occupies on the screen.
推荐答案
You can use SCNSceneRenderer projectPoint:point
:
将点从场景的3D世界坐标系投影到渲染器的2D像素坐标系.
Projects a point from the 3D world coordinate system of the scene to the 2D pixel coordinate system of the renderer.
let node:SCNNode = // Your node
let nodeWorldPosition = node.position
let nodePositionOnScreen = renderer.projectPoint(nodeWorldPosition)
let x = nodePositionOnScreen.x
let y = nodePositionOnScreen.y
注意:从近(resp.far)裁剪平面投影的点的z分量将为0(resp.1).
ARAnchor
可以使用相同的方法:
The same method can be used with ARAnchor
:
let anchor:ARAnchor = // ...
let anchorWorldPosition = SCNVector3(anchor.transform.columns.3)
let anchorPositionOnScreen = renderer.projectPoint(anchorWorldPosition)
let x = anchorPositionOnScreen.x
let y = anchorPositionOnScreen.y
这篇关于ARKit:在屏幕上查找SCNNode的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!