如何使用平移手势识别器在ARSCNView中移动节点?

时间:2017-07-16 23:02:35

标签: ios scenekit arkit

我在viewDidLoad

中有以下代码
let scene = SCNScene(named: "funModel")!
myNode = scene.rootNode.childNode(withName: "blah")!
myNode.position = SCNVector3Make(0, 0, -1)
sceneView.scene = scene
let pan = UIPanGestureRecognizer(target: self, action: #selector(panGesture(recognizer:)))
view.addGestureRecognizer(pan)

然后来自another SO answerpanGesture中的以下代码,但它无效。该节点似乎只是0,0,0。我只是希望能够在屏幕上拖动节点。

let projectedOrigin = sceneView.projectPoint(SCNVector3Zero)
let vp = recognizer.location(in: sceneView)
let vpWithZ = SCNVector3(x: Float(vp.x), y: Float(vp.y), z: Float(projectedOrigin.z))
let worldPoint = sceneView.unprojectPoint(vpWithZ)
myNode.position = worldPoint

1 个答案:

答案 0 :(得分:1)

首先需要从UIPanGestureRecognizer位置(UIPanGestureRecognizer功能中的)获得最高点,例如:

let point = pan.location(in: scene)
let result = scene.hitTest(point, types: .featurePoint)
let resultPoint = result.first

现在你的新世界点是:

let vpWithZ = SCNVector3(x: Float(resultPoint.worldTransform.columns.3.x), y: Float(resultPoint.worldTransform.columns.3.y), z: Float(projectedOrigin.z))

希望它有所帮助。