ARKit:如何在中断后重置世界方向

dmr*_*r07 6 ios swift arkit

我试图让ARWorldTracking会话在会话中断后重新定向北.我已经阅读了几次文档,但我发现它令人困惑.

目前的行为:

当我锁定设备并重新打开应用程序,触发时sessionWasInterrupted,SCNNodes在指南针上逆时针旋转大约90度左右.

当您使用与会话的当前配置不同类型的配置调用run(_:options :)方法时,会话始终重置跟踪

我解释说,当我生成一组与之不同的新配置时,viewWillAppear会话将"重置".我没有得到实际发生的事情,但中断后的方向是关闭的.( removeExistingAnchors什么都不做)

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARWorldTrackingSessionConfiguration()
    configuration.planeDetection = .horizontal
    configuration.worldAlignment = .gravityAndHeading   
    sceneView.session.run(configuration)
}

func sessionWasInterrupted(_ session: ARSession) {
    let configuration = ARWorldTrackingSessionConfiguration()
    configuration.planeDetection = .horizontal
    configuration.worldAlignment = .gravityAndHeading
    self.sceneView.session.run(configuration, options: [ARSession.RunOptions.removeExistingAnchors, ARSession.RunOptions.resetTracking])
}
Run Code Online (Sandbox Code Playgroud)

期望的行为:

当应用程序检测到会话中断时,我希望它将自己重新定位回真北.

小智 8

这个问题也杀了我 - 你帮我解决了一半的解决方案 - 添加“重置跟踪/删除现有锚点”标志对我来说是神奇的关键 - 我认为另一半是这篇文章的指导,你必须暂停会话并从场景中删除所有节点,然后重新定位它们。这两件事的结合使我在会话中断后将指南针重置回正北。

func resetARSession() {
    // Called by sessionInterruptionDidEnd

    sceneView.session.pause()
    sceneView.scene.rootNode.enumerateChildNodes { (node, stop) in
        node.removeFromParentNode() }
    setupARSession()
    setupSceneView()
}

func setupARSession() {

    let configuration = ARWorldTrackingConfiguration()
    configuration.worldAlignment = .gravityAndHeading
    sceneView.session.run(configuration, options: [ARSession.RunOptions.resetTracking, ARSession.RunOptions.removeExistingAnchors])

}
Run Code Online (Sandbox Code Playgroud)