ARKit:在屏幕上查找 SCNNode 的坐标 [英] ARKit: Finding the coordinates of a SCNNode on the screen

查看:43
本文介绍了ARKit:在屏幕上查找 SCNNode 的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Swift ARKit 设置,其中我有一个 SCNNode,其中有一个在 ARSCNView 中可见的 3D 对象.

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

我想确定这个对象在 ARSCNView 上的二维坐标.我的意思是对象被绘制到屏幕上时的 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.

推荐答案

您可以使用 SCNSceneRenderer projectPoint:point :

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

注意:从近(远)剪裁平面投影的点的 z 分量将为 0(分别为 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屋!

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