相对于相机位置沿轴移动节点

Move nodes along axis with respect to camera position

我目前有一个附有多个子节点的根节点,我想作为一个集群一起在场景中移动。我目前通过使用左、右、上和下按钮沿 x 和 y 轴移动它,每次单击按钮时一点一点地改变 rootNode 的位置,例如向左移动:

self.newRootNode.position.x = self.newRootNode.position.x - 0.01

这样,集群总是相对于应用程序初始化时设置的坐标系移动。每次用户改变位置时,我都试图让它相对于用户的左右移动。我试过如下操作:

let nodeCam = self.sceneView.session.currentFrame!.camera
let cameraTransform = nodeCam.transform
self.newRootNode.position.x = cameraTransform.columns.3.x - 0.01

我知道这不是我想要的,我一定是缺少从相机位置到根节点位置的转换,但我不确定要遵循什么步骤。 解决这个问题的正确方法是什么?每次用户改变位置时我都需要重置跟踪吗?任何帮助将不胜感激:)

我相信您现在可以在 ARKit 1.5 中的 ARSession 上使用此功能:

func setWorldOrigin(relativeTransform: matrix_float4x4)

其中:

Changes the basis for the AR world coordinate space using the specified transform.

这是一个示例(未经测试),它可能会为您指明正确的方向:

  func session(_ session: ARSession, didUpdate frame: ARFrame) {

    guard let currentFrame = augmentedRealitySession.currentFrame?.camera else { return }
    let transform = currentFrame.transform
    augmentedRealitySession.setWorldOrigin(relativeTransform: transform )

}

当然不要忘记使用这个ARSCNDebugOptions.showWorldOrigin 为了调试和调整你的矩阵和变换等

您也可以将 delegate callback 中的代码用作 IBAction 等。

希望对您有所帮助...