我在ARKit中做了一个小型演示。我正面临如何通过滑动进入3D对象的问题。
例如,我有一个家庭3D对象,它可以与Tap Gesture,Rotation Gesture配合使用,我想通过将一个人滑动到家庭3D对象中的另一个房间来进入家庭。
它旋转了整个3D对象节点,我无法滑动并进入家庭。
这是我用于基于Y轴旋转的代码,
@objc private func viewRotated(_ gesture: UIRotationGestureRecognizer) {
let location = gesture.location(in: sceneView)
guard let node = node(at: location) else { return }
switch gesture.state {
case .began:
originalRotation = node.eulerAngles
case .changed:
guard var originalRotation = originalRotation else { return }
originalRotation.y -= Float(gesture.rotation)
node.eulerAngles = originalRotation
default:
originalRotation = nil
}
}
要使用UIPanGestureRecognizer全方位旋转,这是我添加的代码,
@objc func viewPanned(gestureRecognize: UIPanGestureRecognizer){
let translation = gestureRecognize.translation(in: gestureRecognize.view!)
let x = Float(translation.x)
let y = Float(-translation.y)
let anglePan = sqrt(pow(x,2)+pow(y,2))*(Float)(M_PI)/180.0
var rotationVector = SCNVector4()
rotationVector.x = -y
rotationVector.y = x
rotationVector.z = 0
rotationVector.w = anglePan
homeNode?.rotation = rotationVector
if(gestureRecognize.state == UIGestureRecognizerState.ended) {
//
let currentPivot = homeNode?.pivot
let changePivot = SCNMatrix4Invert( (homeNode?.transform)!)
homeNode?.pivot = SCNMatrix4Mult(changePivot, currentPivot!)
homeNode?.transform = SCNMatrix4Identity
}
}
也许还有另一种方法可以做到,有人可以建议一种方法吗?谢谢。