我上了XCode 9的AR應用程序啓動器,其中錨在一個場景中創建水龍頭上打轉轉:ARKit設置ARAnchor變換基於觸摸位置
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座標應用於以米爲單位的錨點變換,也不知道它們是哪個軸適用於。
謝謝!我實際上是指在開始一個新的增強現實應用程序時得到的XCode中的框架項目,但我會看看演示應用程序。 – Elise
不客氣。它在這裏:https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip NB。從應用程序菜單中打開「調試可視化」將澄清很多。 – PDK