[SOLVED] Possibility to pause rendering without removing render loops

Hi,

Was wondering if its possible to fully stop rendering loop if nothing is changing on the screen.
Checked code for the Engine and found this function, that stop rendering if focus is lost.

public _renderLoop(): void {
        if (!this._contextWasLost) {
            var shouldRender = true;
            if (!this.renderEvenInBackground && this._windowIsBackground) {
                shouldRender = false;
            }

            if (shouldRender) {
                // Start new frame
                this.beginFrame();

                for (var index = 0; index < this._activeRenderLoops.length; index++) {
                    var renderFunction = this._activeRenderLoops[index];

                    renderFunction();
                }

                // Present
                this.endFrame();
            }
        }

        if (this._activeRenderLoops.length > 0) {
           this._frameHandler = this._queueNewFrame(this._bindedRenderFunction, this.getHostWindow());
        } else {
            this._renderingQueueLaunched = false;
        }
    }

What about having possibility to manully stop it ? like this

public _renderLoop(): void {
        if (!this._contextWasLost) {
            var shouldRender = true;
            if (!this.renderEvenInBackground && this._windowIsBackground) {
                shouldRender = false;
            }

            if (shouldRender && !this.paused) {
                // Start new frame
                this.beginFrame();

                for (var index = 0; index < this._activeRenderLoops.length; index++) {
                    var renderFunction = this._activeRenderLoops[index];

                    renderFunction();
                }

                // Present
                this.endFrame();
            }
        }

        if (this._activeRenderLoops.length > 0) {
           this._frameHandler = this._queueNewFrame(this._bindedRenderFunction, this.getHostWindow());
        } else {
            this._renderingQueueLaunched = false;
        }
    }

Also should this part still be triggered even if render is blocked ?

 if (this._activeRenderLoops.length > 0) {
           this._frameHandler = this._queueNewFrame(this._bindedRenderFunction, this.getHostWindow());
        } else {
            this._renderingQueueLaunched = false;
        }

:slightly_smiling_face:

The best option is to just call stopRenderLoop and then call runRenderLoop when needed or other idea: inside your render loop, you can have a boolean to decide if you want to call scene.render

If you want to control it manually, you can use:

        engine.runRenderLoop(function () { 
                if(!scene.paused){
                    scene.render();
                }
        });

for you own render loop and set scene.paused when you want.

Or

scene.freezeActiveMeshes();

according to docs

BUT. I think you can’t control it in automatic way

And you have to know by youself that’s nothing changing in your core right now

Thank you for the input, I went for not calling scene.render() if game is pasued, as stopRenderLoop / runRenderLoop is heavy operation, if done too ofen

Hey, @Hersir Where did you read that runRenderLoop and stopRenderLoop are heavy?

I am using a boolean variable, but would gladly stop the whole render loop if it is not that heavy.

 setPointerRenderLoops() {
    this._pointerRenderLoopEnabled = false;
    this._pointerRenderLoop = () => {
      if(this._pointerRenderLoopEnabled)
        this._scene.render();
    };

    this._scene.getEngine().runRenderLoop(this._pointerRenderLoop);
 
    this._scene.onPointerObservable.add((eventData, eventState) => {
      if (eventData.type == BABYLON.PointerEventTypes.POINTERDOWN) {
        this._pointerRenderLoopEnabled = true;
      } else if (eventData.type == BABYLON.PointerEventTypes.POINTERWHEEL) {
        this._scene.render();
      } else if (eventData.type == BABYLON.PointerEventTypes.POINTERUP) {
        this._pointerRenderLoopEnabled = false;
      }
    });
  }

The scene knows all about objects, camera, etc., so it would be great if it could decide on its own if a redraw is necessary or not.

Sure, some applications need a constantly running “game/event loop”, but for some applications a render-on-demand mode which is handled by the scene would be very helpful and save a lot of laptop battery, IMHO.

1 Like