Swift SceneKit 节点滚动

Swift SceneKit node scroll

我自己实现了滚动球体的方法,但是滚动的时候感觉有点卡顿

当我使用标准滚动方法 (allowsCameraControl = true) 时,当球体急剧向一侧猛拉(如轻扫)时,球体会滚动一段时间然后停止,在我的情况下不会。我怎样才能做同样的事情?

// Set scene settings
sceneView.scene = scene

cameraOrbit = SCNNode()
cameraNode = SCNNode()
cameraNode.name = "camera"
camera = SCNCamera()

// camera stuff
camera.usesOrthographicProjection = true
camera.orthographicScale = 5
camera.zNear = 1
camera.zFar = 100


// initially position is far away as we will animate moving into the globe
cameraNode.position = SCNVector3(x: 0, y: 0, z: 50)
cameraNode.camera = camera
cameraOrbit = SCNNode()
cameraOrbit.addChildNode(cameraNode)
scene.rootNode.addChildNode(cameraNode)

// Material
let blueMaterial = SCNMaterial()
blueMaterial.diffuse.contents = UIImage(named: "earth2")
blueMaterial.shininess = 0.05
blueMaterial.multiply.contents = UIColor(displayP3Red: 0.7, green: 0.7, blue: 0.7, alpha: 1.0)

let sphere = SCNSphere(radius: 2)
sphere.segmentCount = 300
sphere.firstMaterial?.diffuse.contents = UIColor.red
earthNode = SCNNode(geometry: sphere)
earthNode.name = "sphere"
earthNode.geometry?.materials = [blueMaterial]
scene.rootNode.addChildNode(earthNode)
earthNode.rotation = SCNVector4(0, 1, 0, 0)

sceneView.allowsCameraControl = false
sceneView.backgroundColor = UIColor.clear
sceneView.cameraControlConfiguration.allowsTranslation = true
sceneView.cameraControlConfiguration.rotationSensitivity = 0.4

let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
sceneView.addGestureRecognizer(panGesture)


    @objc func handlePan(_ gestureRecognize: UIPanGestureRecognizer) {
        if gestureRecognize.numberOfTouches == 1 { //leftRightAttenuation = 5.0
            if (gestureRecognize.state == UIGestureRecognizer.State.changed) {
                let scrollWidthRatio = Float(gestureRecognize.velocity(in: gestureRecognize.view!).x) / (leftRightAttenuation * 10000) 
                let scrollHeightRatio = Float(gestureRecognize.velocity(in: gestureRecognize.view!).y) / (leftRightAttenuation * 10000)
                cameraOrbit.eulerAngles.y += Float(-2 * Double.pi) * scrollWidthRatio
                cameraOrbit.eulerAngles.x += Float(-Double.pi) * scrollHeightRatio
            }
        }
    }

带有标准滚动条的视频(allowsCameraControl = true

https://youtu.be/0BL0mY26ZkY

带自己卷轴的视频(allowsCameraControl = false)

https://youtu.be/ZwRgJMDZpmA

看看这个:

https://github.com/gadsden/SceneKit-Quaternion-Rotations

包含 3 种不同的对象旋转方法。