使用ARKit / Scenekit,我希望将映射到平面节点的2D图像添加到场景中,但我希望此节点不断以用户/摄像机为中心。
这是我在初始0,0,0位置添加节点的功能。
let plane = SCNPlane(width: 20, height: 20)
let material = SCNMaterial()
material.isDoubleSided = true
material.diffuse.contents = UIImage(named: "CompassRose.png")
plane.materials = [material]
NodeCompass = SCNNode(geometry: plane)
NodeCompass.position = SCNVector3Make(0,0,0)
skScene.rootNode.addChildNode(NodeCompass)
诀窍是在用户位置更改时修改此节点的位置。这是怎么做到的?
答案 0 :(得分:3)
要使节点始终位于视图中心,您必须计算世界位置的中心位置。
extension ARSCNView {
// create real world position of the point
func realWorldPosition(for point: CGPoint) -> SCNVector3? {
let result = self.hitTest(point, types: [.featurePoint])
guard let hitResult = result.last else { return nil }
let hitTransform = SCNMatrix4(hitResult.worldTransform)
// m4x -> position ;; 1: x, 2: y, 3: z
let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
return hitVector
}
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
DispatchQueue.main.async {
let center = self.view.center
guard let realWorldPosition = self.sceneView.realWorldPosition(for: center) else { return }
self.NodeCompass.position = realWorldPosition
}
}