ARKit –获取场景中ARCamera的当前位置 [英] ARKit – Get current position of ARCamera in a scene

查看:458
本文介绍了ARKit –获取场景中ARCamera的当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在同时学习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].知道我在做什么错吗?最终的想法是,我将围绕一个不可见的球体旋转该对象,直到它在镜头前,然后对其进行动画处理,执行类似以下操作:

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和摄像机位置>. 如果您只需要一次位置,例如将一个相机移动到的节点,但是如果要获取相机位置的更新,则应该使用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屋!

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