本文介绍了使用SceneKit在ARKit中拖动SCNNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在ARKit中有一个简单的SCNNode,并且我试图将其拖动到手机上的任何地方.这是我的代码.
I have a simple SCNNode in ARKit and I am trying to drag it wherever I moved my finger on the phone. Here is my code.
@objc func pan(recognizer :UIGestureRecognizer) {
guard let currentFrame = self.sceneView.session.currentFrame else {
return
}
var translation = matrix_identity_float4x4
translation.columns.3.z = -1.5
let sceneView = recognizer.view as! ARSCNView
let touchLocation = recognizer.location(in: sceneView)
let hitTestResult = sceneView.hitTest(touchLocation, options: [:])
if !hitTestResult.isEmpty {
print("hit result")
guard let hitResult = hitTestResult.first else {
return
}
let node = hitResult.node
node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation)
}
}
问题在于拖动速度非常慢且不平滑.
The problem is that the drag is very slow and not smooth.
推荐答案
我使用PanGesture来处理翻译.除以700是为了平滑和调整运动速度,我通过反复试验达到了该值,您可能需要尝试一下
I handle translation with PanGesture like this. The division by 700 is to smooth and adjust speed of movement, I reached to that value by trial or error, you may want to experiment with it
@objc func onTranslate(_ sender: UIPanGestureRecognizer) {
let position = sender.location(in: scnView)
let state = sender.state
if (state == .failed || state == .cancelled) {
return
}
if (state == .began) {
// Check it's on a virtual object
if let objectNode = virtualObject(at: position) {
// virtualObject(at searches for root node if it's a subnode
targetNode = objectNode
latestTranslatePos = position
}
}
else if let _ = targetNode {
// Translate virtual object
let deltaX = Float(position.x - latestTranslatePos!.x)/700
let deltaY = Float(position.y - latestTranslatePos!.y)/700
targetNode!.localTranslate(by: SCNVector3Make(deltaX, 0.0, deltaY))
latestTranslatePos = position
if (state == .ended) {
targetNode = nil
}
}
}
这篇关于使用SceneKit在ARKit中拖动SCNNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!