我正在玩XCode 9上的AR入门应用程序,其中在点击场景时创建锚点:
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)
}
}
无论我在哪里点击屏幕,这总是导致锚点在屏幕中间创建,这是有道理的,因为我们获取当前相机变换并仅在z轴上应用平移。
我希望锚点实际上放置在我用手指点击的位置。我可以使用touches.first?.location(in: sceneView)
获取点击的位置,即从屏幕左上角开始的点距离,但我不确定如何将这些2D点坐标应用于以米为单位的锚点变换,也不知道它们适用于哪个轴。