我正在使用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 pt坐标应用于以米为单位的锚变换,也不确定它们适用于哪个轴。
发布于 2017-08-04 15:05:19
我想你指的是苹果的ARKitExample项目“在增强现实中放置虚拟对象”。
查看在移动屏幕中(已经放置的)模型时调用的方法VirtualObject.translateBasedOnScreenPos(_:instantly:infinitePlane:) --它基本上需要解决您描述的相同问题。
你会发现这反过来又叫ViewController.worldPositionFromScreenPosition(_:objectPos:infinitePlane:)。
从这种方法中提取出来的方法是:
正如您所看到的,它们考虑了可能或可能不适用于您的用例的各个方面。考虑研究和重用(部分)他们的方法。
https://stackoverflow.com/questions/45488881
复制相似问题