I am working on an iOS app with Swift and SceneKit. When user taps on screen, I want to render a dot in SceneKit at the location that user just tapped. How can I do that?
I am using unprojectPoint() method but my object is not rendered at expected location.
I am having a SceneView, with the camera like this:
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
And then I render the dot at tap position like this:
func handleTap(_ gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let scnView = self.view as! SCNView
// create dot
let geo = SCNSphere(radius: CGFloat(0.1))
geo.firstMaterial?.diffuse.contents = UIColor.red
let dotNode = SCNNode(geometry: geo)
let p = gestureRecognize.location(in: scnView)
let uP = scnView.unprojectPoint(SCNVector3(p.x, p.y, 5))
// HERE - how to set position for the dot ?
dotNode.position = SCNVector3(uP.x, uP.y, 5)
scnView.scene?.rootNode.addChildNode(dotNode)
}
Here is my Github repo: https://github.com/rudyhuynh/SceneKitExample