我想在 Scenekit 的 z 轴上自动移动相机的每一帧。我用 Swift 写了这段代码:
func renderer(_ renderer: SCNSceneRenderer,
updateAtTime time: TimeInterval) {
cameraNode.position.z = cameraNode.position.z + 2
}
这仅适用于 3 帧,之后相机不再移动。有人可以给我正确的代码,以便相机每帧自动移动吗?
最佳答案
SceneKit calls this method exactly once per frame, so long as the SCNView object (or other SCNSceneRenderer object) displaying the scene is not paused.
如果您的 View 中没有其他任何东西在移动,则场景“暂停”并且不会渲染。根据this question ,为了强制渲染每一帧,解决方法应该是:
sceneView.loops = true
但是,每帧都更新位置并不是最佳做法,因为 FPS 会发生变化,这会影响相机速度。但是您可以在不使用渲染功能的情况下使用 SCNAction 保持一致的相机速度:
let move = SCNAction.moveBy(x: 0, y: 0, z: 120, duration: 1.0)
let moveForever = SCNAction.repeatActionForever(move)
cameraNode.runAction(moveForever)
如果需要停止,这也将使您更容易控制相机,并且在不必要时不会强制渲染。
关于swift - 使用 Swift 在 Scenekit 中移动相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49223586/