I'm playing around with the AR starter app on XCode 9 where anchors are created in a scene on tap:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let sceneView = self.view as? ARSKView else {
return
}
// Create anchor using the camera’s current position
if let currentFrame = sceneView.session.currentFrame {
// Create a transform with a translation of 0.2 meters in front
// of the camera
var translation = matrix_identity_float4x4
translation.columns.3.z = -0.2
let transform = simd_mul(currentFrame.camera.transform, translation)
// Add a new anchor to the session
let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
}
}
This always results in the anchor being created in the middle of the screen regardless of where I tap, which makes sense, as we're getting the current camera transform and applying only a translation in the z axis to it.
I would like the anchor instead to be placed where I actually tapped with my finger. I can get the location of the tap using touches.first?.location(in: sceneView), i.e., just the distance from the top left corner of the screen in points, but I'm unsure how to apply these 2D pt coordinates to the anchor transform in meters, nor which axis they apply to.