我有一个带有居中物体的简单场景,相机位于中心周围的虚拟球体上(灵感来自Rotate SCNCamera node looking at an object around an imaginary sphere)。
现在我希望能够用双指平移手势移动对象。只要我不先旋转相机,我的代码就可以正常工作。如果我首先将相机旋转180°(以便相机从后面看物体),那么移动的方向是错误的(这对我来说很有意义)。其他相机位置会变得更糟。但是,我无法找到如何将识别器的x / y平移转换或投影到与摄像机平行的平面。物体应该在手指下移动..
这是我场景的设置:
let scene = SCNScene()
//add camera
let camera = SCNCamera()
camera.usesOrthographicProjection = true
camera.orthographicScale = 30
camera.zNear = 1.0
camera.zFar = 1000
cameraNode = SCNNode()
cameraNode.camera = camera
cameraNode.position = SCNVector3(x: 0.0, y: 0.0, z: 50.0)
cameraOrbit = SCNNode()
cameraOrbit.addChildNode(cameraNode)
scene.rootNode.addChildNode(cameraOrbit)
boxNode.geometry = SCNBox(width: 10, height: 40, length: 10, chamferRadius: 0.2)
scene.rootNode.addChildNode(boxNode)
let panMoveGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(panMoveGesture))
panMoveGestureRecognizer.minimumNumberOfTouches = 2
sceneView.addGestureRecognizer(panMoveGestureRecognizer)
手势识别器代码:
func panMoveGesture(recognizer: UIPanGestureRecognizer) {
//move
let translation = recognizer.translation(in: self.view.superview)
var newXMovement = -Float(translation.x) * 0.1
var newYMovement = Float(translation.y) * 0.1
switch recognizer.state {
case .began:
currentXMovement = cameraOrbit.position.x
currentYMovement = cameraOrbit.position.y
case .changed:
newXMovement += currentXMovement
newYMovement += currentYMovement
cameraOrbit.position.x = newXMovement
cameraOrbit.position.y = newYMovement
case .ended:
currentXMovement = newXMovement
currentYMovement = newYMovement
default: break
}
}
请帮助; - )