ARKit - 获取 ARCamera 在场景中的当前位置 [英] ARKit – Get current position of ARCamera in a scene
问题描述
我正在同时学习 ARKit 和 Scenekit,这有点挑战.
I'm in the process of learning both ARKit and Scenekit concurrently, and it's been a bit of a challenge.
创建 ARWorldTrackingSessionConfiguration 会话后,我想知道是否有人知道在场景会话中获取用户相机"位置的方法.这个想法是我想朝着用户的当前位置设置一个对象的动画.
With a ARWorldTrackingSessionConfiguration session created, I was wondering if anyone knew of a way to get the position of the user's 'camera' in the scene session. The idea is I want to animate an object towards the user's current position.
let reaperScene = SCNScene(named: "reaper.dae")!
let reaperNode = reaperScene.rootNode.childNode(withName: "reaper", recursively: true)!
reaperNode.position = SCNVector3Make(0, 0, -1)
let scene = SCNScene()
scene.rootNode.addChildNode(reaperNode)
// some unknown amount of time later
let currentCameraPosition = sceneView.pointOfView?.position
let moveAction = SCNAction.move(to: currentCameraPosition!, duration: 1.0)
reaperNode.runAction(moveAction)
然而,似乎currentCameraPosition 总是[0,0,0],即使我正在移动相机.知道我做错了什么吗?最终的想法是我将围绕一个不可见的球体旋转对象,直到它位于相机前面,然后将其设置为动画,执行类似以下操作:旋转 SCNCamera 节点,看着一个假想球体周围的物体(这样用户就会看到物体朝着他们移动)
However, it seems that currentCameraPosition is always [0,0,0], even though I am moving the camera around. Any idea what I'm doing wrong? Eventually the idea is I would rotate the object around an invisible sphere until it is in front of the camera and then animate it in, doing something similar to this: Rotate SCNCamera node looking at an object around an imaginary sphere (that way the user sees the object animate towards them)
感谢您的帮助.
推荐答案
将自己设置为 ARSession.delegate
.然后你可以实现 session(_:didUpdate:)
,它会为你会话中处理的每个帧提供一个 ARFrame
.该框架有一个 camera
属性,用于保存有关相机变换、旋转和位置的信息.
Set yourself as the ARSession.delegate
. Than you can implement session(_:didUpdate:)
which will give you an ARFrame
for every frame processed in your session. The frame has an camera
property that holds information on the cameras transform, rotation and position.
func session(_ session: ARSession, didUpdate frame: ARFrame) {
// Do something with the new transform
let currentTransform = frame.camera.transform
doSomething(with: currentTransform)
}
<小时>
正如 rickster 指出的那样,您始终可以通过以下方式获取当前的 ARFrame
和相机位置它通过调用 session.currentFrame
.如果您只需要一次位置,这很有用,例如移动摄像机所在的节点,但如果您想获取摄像机位置的更新,则应使用 delegate
方法.
As rickster pointed out you always can get the current ARFrame
and the camera position through it by calling session.currentFrame
.
This is useful if you need the position just once, eg to move a node where the camera has been but you should use the delegate
method if you want to get updates on the camera's position.
这篇关于ARKit - 获取 ARCamera 在场景中的当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!