How to detect when scene is ready and all shaders have finished compiling

we’re wanting to build a lifecycle hook that fires when everything is ready to be displayed. So basically something that fires after all meshes are added to the scene and all shaders are compiled.

We have a situation where we preloaded a bunch of models via assetManager into basically an array.
we then loop through those models and configure some material parameters. These have to be done after the assetManager finishes and cannot be done per task unforutnately.

so we loop through our array of pre-loaded meshes and set some params.

eg:,mesh.material.albedoTexture= …etc
then we add the mesh to the scene
scene.addMesh(mesh);

if the render loop is running you can visually see the meshes resolve one by one as the shaders finish compiling… those assets seem to be appearing only after the shader has compiled. , we want to only start the render loop after everything is ready to be viewed. Whats the best way to achieve this?

It seems that shaderCompile time is the way so i thought about counting the number of shadercompiles against a total number of expected shader compiles but I cant seem to figure out how to get the total number of shaders expected to compile? Is there a flag that is set that indicates that a shader needs to be re-complied that I can track? (you could force all shaders to recompile but that would not be ideal for performance)

I tried using other hooks such as onNewGeometryAddedObservable and onMeshImportedObservable and comparing this against a total but that also seems unreliable.

Our fallback plan is to just have a magic delay number we set after the assetManager finishes loading all assets but this isnt as nice. :slight_smile:

Any thoughts?

I think whenReadyAsync would work for this, to know when everything in the scene is ready to be used.

await scene.whenReadyAsync();

Or you could use executeWhenReady if you want a callback instead.

scene.executeWhenReady(() => {
    // ready!
});
5 Likes

thank you sir! that was the ticket… I actually came across this method before but for some reason it wasnt working for me earlier when i had a slightly different approach but now it works! what the heck. Thank you again!

1 Like