下面的代码将 SCNPlane 放置在触摸点上,但该平面定向(旋转)到应用程序启动时手机所在的位置。理想情况下,我希望将节点定向到物理墙或相机的当前方向。这是如何使用 ARKit 完成的?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else {return}
let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
guard let hitResult = result.last else {return}
let hitTransform = SCNMatrix4.init(hitResult.worldTransform)
let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
createPlane(position: hitVector)
}
func createPlane(position: SCNVector3) {
let background = SCNNode()
background.geometry = SCNPlane.init(width: 0.12, height: 0.10) // better set its size
background.geometry?.firstMaterial?.diffuse.contents = "odinBW2.jpeg"
background.position = position
sceneView.scene.rootNode.addChildNode(background)
}
最佳答案
如果你想将你的节点与相机的方向对齐,只需使用 SCNBillboardConstraint .
let yourNode = SCNNode()
let billboardConstraint = SCNBillboardConstraint()
billboardConstraint.freeAxes = [.X, .Y, .Z]
yourNode.constraints = [billboardConstraint]
关于ios - 如何使用 ARKit 将 SCNNode 的方向更改为相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390019/