Home > Net >  SCNSceneRenderer stop after 12 frame
SCNSceneRenderer stop after 12 frame

Time:03-19

i create a simple scene with a cube and a floor for study propose.

i set the delegate to self, but I can't understand why my render stop print out my message after 12 frame.. my I know why it stop? should not running forever since my scene is in view?

here my custom scene:

class GameSceneMain: SCNScene, SCNSceneRendererDelegate, SCNPhysicsContactDelegate {
 
    var camera : SCNCamera?
    var floor : SCNFloor?
    var delegate : SCNSceneRendererDelegate?
    
  
    override init() {
        super.init()
        setupScene()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupScene(){ // load sky
      
        let skybox = UIImage(named: "skybox")
        self.background.contents = skybox
        delegate = self
        addCamera()
        addFloor()
        addLight()
        addCube()
    }
    func addCamera(){
        camera = SCNCamera()
        let cameranode = SCNNode()
        cameranode.camera = camera
        cameranode.position = SCNVector3(0, 5, 10)
        cameranode.eulerAngles = SCNVector3(deg2rad(-20), deg2rad(0), 0)
        self.rootNode.addChildNode(cameranode)
    }
    
    func addFloor(){
        floor = SCNFloor()
        guard let floor = floor else {
            return
        }
        floor.reflectivity = 0
        let mat = SCNMaterial()
        mat.diffuse.contents = UIImage(named: "grass")
        mat.diffuse.wrapS = SCNWrapMode.repeat
        mat.diffuse.wrapT = SCNWrapMode.repeat
        floor.materials = [mat]
        let nodeFloor = SCNNode(geometry: floor)
        self.rootNode.addChildNode(nodeFloor)
    }
    
    func addLight(){
        let light = SCNLight()
        light.type = .ambient
        let nodeLight = SCNNode()
        nodeLight.light = light
        self.rootNode.addChildNode(nodeLight)
    }

    
    func  addCube(){
        let box = SCNBox(width: 1.5, height: 2, length: 3, chamferRadius: 0.3)
        let mat = SCNMaterial()
        mat.diffuse.contents = UIColor.blue
        box.materials = [mat]
        let node = SCNNode(geometry: box)
        node.position = SCNVector3(0, 0, 0)
        node.name = "box"
        self.rootNode.addChildNode(node)
    }
    
    var counterFrame = 0
  
    // render STOP why??
    func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
        counterFrame  = 1
        print("run \(counterFrame)")
    }

}

and another class is used for AppManager and load the scene

class AppManager:NSObject, ObservableObject  {
    var sceneView = SCNView()
    var scene : GameSceneMain?
  
    
    
    override init(){
        super.init()
        loadScene()
       
    }
    
    
    func setupView(){
        sceneView.backgroundColor = .black
        sceneView.allowsCameraControl = true
        sceneView.autoenablesDefaultLighting = true
        sceneView.showsStatistics = true
        sceneView.scene = scene
        sceneView.scene?.isPaused = false
        sceneView.delegate = scene?.delegate
       
    }
    

    func loadScene(){
        scene = GameSceneMain()
        guard let scene = scene else {
            return
        }
        sceneView.scene = scene
        
    }
  }


I notice that when the scene load the counter of the frame run until 12 than it stop.. if I move the camera in the simulator than the counter in the renderer continue. But why? Should not run forever since the scene is in view?

Thanks a lot for the help... quite new on SceneKit

CodePudding user response:

This is for performance and energy efficiency reasons. If nothing changes in the scene then rendering the same content again is wasteful.

You can have a look at the rendersContinuously property.

  • Related