Proper way to wait for a complete render

Hi,

I’m using babylon to generate a static image and I’m running the scene.render() only once manually instead of adding it to the RenderLoop.

Which is the proper way to wait for the scene to be fully rendered?

I tried to add a .onAfterRender to the scene but it’s not working.

Thanks

The first thing to try is just putting it right after the scene.render() line, if you are really only going to run it once.

No, it doesn’t work. I’m loading a very simple scene with async and then calling the render but the scene sometimes appears complete, sometimes incomplete and sometimes empty.

Maybe the issue resides in the code I have to load the scene inside a promise.
When the SceneLoader callback happens the scene is completely loaded?

_sceneLoad:function(sceneFile){
  return new Promise(resolve=>{
     BABYLON.SceneLoader.Append("", sceneFile+".babylon", this.S, (scene)=>{
       resolve(scene)
     })
  })
}

Ah, then

const scene = new BABYLON.Scene(engine);
BABYLON.SceneLoader.Append(...);
scene.executeWhenReady( ()=> {
     scene.render();
    // do it here
});
2 Likes

I’m not sure the loaded callback is completely deterministic - there may be an asynchronous operation. Is your model multiple files? Which model format?

Are you doing a render and screenshot to see that it’s not loaded?

Yep, u got it right!
I just added the executeWhenReady inside my callback before resolving the promise and now it works like a charm. It’s not the first time I have issues with this and I always resolved using timers, but it’s a hacky way that sometimes didn’t work as expected. Thanks so much for pointing mw in the right direction. :slight_smile:

_sceneLoad:function(sceneFile){
    return new Promise(resolve=>{
      BABYLON.SceneLoader.Append("", sceneFile+".babylon", this.S, (scene)=>{
        scene.executeWhenReady(()=>{
          resolve(scene)
        })
      })
    })
  },
2 Likes