在轴上移动 x 米的虚拟对象。方舟 Swift

Move virtual object with x meters on axis. ARKIT Swift

我想在场景中设置一个参考点,并将对象放置在距参考点 5​​ 米的位置(示例中的 X 轴)。基本上,我想在没有点击手势的情况下移动一个对象,只需要使用前面多少米,对,我的对象应该被放置。你能帮帮我吗?

当您第一次 运行 一个 ARSession 时,worldOrigin 设置为 SCNVector3(0,0,0)

这样你就可以据此确定一个初始参考点。

例如,将对象放置在 X 轴右侧 1 米处,Y 轴上 0 米处,距离相机 -1.5 处,可以这样做:

 let nodeToAdd = SCNNode()
 let nodeGeometry = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)
 nodeGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
 nodeToAdd.geometry = nodeGeometry
 nodeToAdd.position = SCNVector3(1, 0, -1.5)
 augmentedRealityView.scene.rootNode.addChildNode(nodeToAdd)

然后您可以通过多种方式移动 SCNNode;一个非常简单的方法是使用 SCNAction 即:

A simple, reusable animation that changes attributes of any node you attach it to.

移动SCNNode的动作如下:

因此,在我们的例子中,让我们使用 class 函数:

 move(to: SCNVector3, duration: TimeInterval)

这只是将我们的节点移动到一个新位置:

let moveAction = SCNAction.move(to: SCNVector3(1.5, 0, -1.5), duration: 5)
nodeToAdd.runAction(moveAction)

希望这能为您指明正确的方向。