ARKit: Finding the coordinates of a SCNNode on the screen

℡╲_俬逩灬. 提交于 2020-06-27 08:17:17

问题


I have a simple Swift ARKit setup where I have a SCNNode with a 3D object that is visible in an ARSCNView.

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: Sketch

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.


回答1:


You can use SCNSceneRenderer projectPoint:point :

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

Note : A point projected from the near (resp. far) clip plane will have a z component of 0 (resp. 1).


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


来源:https://stackoverflow.com/questions/47992268/arkit-finding-the-coordinates-of-a-scnnode-on-the-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!