BABYLON.Animation is not synchronized with the rendering loop?

Hi all,

I noticed that if I pause the scene, for example, like this: How to pause the game correctly while in the game menu? - Questions - Babylon.js then all BABYLON.Animation that I started actually continue to play in background. When I resume the rendering of the scene, then those animations are far away already. This means that BABYLON.Animation doesn’t listen the gaming loop and probably relies on setInterval, setTimeout, which is a trouble in terms of game state managment.

Is there a way to make these animations to be synchronized with a rendering loop? Or at least magic methods like BABYLON.Animation.pauseAll(), BABYLON.Animation.resumeAll() probably exist?

Of course as the last chance I can compute all my animations on my own using:

let framesPassed = 0;
let fps = 60;
scene.onBeforeRenderObservable.add(function(e) {
    framesPassed++
    if (framesPassed >= fps * 1 && framesPassed < fps * 2) {
        // DIY
    }
    else if (framesPassed >= fps * 2 && framesPassed < fps * 3) {
        // DIY
    }
    // ....
});

But the BABYLON.Animation has a convenient mechanism of binding values to keyframes. It would be sad if I will need to create my own animation engine only because the original one is not synchronized with a rendering loop.

I don’t know if you can pause all animations at once but you can at least pause the animation using the object returned by beginDirectAnimation. See this PG: How to pause an animation with a pause button

2 Likes

pauseAll can be scene.animationsEnabled = false

1 Like

@Evgeni_Popov, in this case I will have to go through all animations across all my application and pause and unpause them. This is not a big win, comparing to remaking all animations to onBeforeRenderObservable based.

@Deltakosh, and if I will do scene.animationsEnabled = true when I want to unpause, will my animations continue from the same point where they were paused?

yes :slight_smile:

@Deltakosh,

Well, unfortunately it doesn’t work. Calculations are continuing during the pause.
Playground

Why do you say calculations are continuing? I see no code executed on my side

I guess I understand your issue: the system is catching up automatically to compensate so it is really a pause :slight_smile:

If you want to pauseAll you will have to enumerate them and call pause() :frowning:

Something like that: Pausing all animations | Babylon.js Playground (babylonjs-playground.com)

Doesn’t work for me in BabylonJS 4.1.0 and 4.2.0.
Did you implement it only in version 5.0.0+?

Nope it is there since very long time