'In arkit how to map the camera position to the screen position?

I use below code to get camera position

 guard let pointOfView = sceneView.pointOfView else {return node}
 let transform = pointOfView.transform
 let orientation = SCNVector3(-transform.m31, -transform.m32, -transform.m33)
 let location = SCNVector3(transform.m41, transform.m42, transform.m43)
 let position = orientation + location

if I assign this position to the node, the node is in the middle of the screen.

I tried to use below code to map this position to the screen position and assign this position to the button.

 func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        let screenPosition =  renderer.projectPoint(position)
        button.center.x = CGFloat(screenPosition.x)
        button.center.y = CGFloat(screenPosition.y)
}

but it does not work, I get the button position as below in my app, I assume it should be in the middle of the screen but it is on the left.

enter image description here

Is there any way to get the correct camera position on the screen?

Many thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source