【问题标题】:Swift SceneKit node scrollSwift SceneKit 节点滚动
【发布时间】:2021-10-11 02:35:19
【问题描述】:

我实现了自己的球体滚动方法,但是滚动时感觉像滞后

当我使用标准滚动方法 (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

【问题讨论】:

    标签: swift scenekit scene


    【解决方案1】:

    看看这个:

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

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

    【讨论】:

    • 只是一点提示:allowCameraControl = true 不滚动对象,它围绕对象滚动/旋转相机 - 当allowCameraControl = true 时,您可以使用几种不同的控制行为
    猜你喜欢
    • 2021-08-25
    • 2021-08-24
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2017-12-25
    • 2015-07-15
    • 2022-07-07
    • 2017-02-07
    相关资源
    最近更新 更多