Reputation: 147
With ARKit/Scenekit, I am looking to add a 2D image mapped onto a plane node into a scene, but I want this node to continually center on the user/camera.
Here is my function for adding the node at the initial 0,0,0 location.
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)
The trick is to modify the position of this node when the users location changes. How is this done?
Upvotes: 2
Views: 1516
Reputation: 6459
To keep your node always in the center of view, you have to calculate the center position of world position.
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
}
}
Upvotes: 4