Search code examples
swiftuiscenekit

The renderer function in SCNSceneRendererDelegate is only called by input, not automatically


I am aware that this question has been discussed many times before. Nevertheless, it is unclear to me how the whole thing has to be implemented in SwiftUI.

Currently:

After an input (for example moving the camera) the renderer function is called correctly.

Goal:

The renderer function should also be called without needing input.

Setup:

struct ContentView: View {
    @ObservedObject var gameData : GameViewModel
    
    var body: some View {
        SceneView(
            scene: gameData.scene,
            pointOfView: gameData.camera,
            options: [
                .allowsCameraControl
            ],
            delegate: gameData
        )
    }
}


class GameViewModel: NSObject, ObservableObject { ... }


extension GameViewModel: SCNSceneRendererDelegate {
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) { ... }
}

Solution

  • This is controlled by the rendersContinuously property on SCNView.

    When working with SwiftUI, there's the rendersContinuously option to use with SceneView .init(scene:pointOfView:options:preferredFramesPerSecond:antialiasingMode:delegate:technique:)