Is it possible to InstantiateModelsToScene() inside web worker?

Hello guys,

I have bunch of GLBs loaded already which I need to clone at runtime for some reason. But whenever I am using InstantiateModelsToScene(), my game literally freezes for half to one second. any suggestion you have to solve this issue? I never used web worker. Just got to know about the concept today, can it help? thanks.

Unfortunately even in web worker you would experience the same as this needs to be instantiated where the engine and context is living.

Now, this might also be a bug. Could you try to repro in the playground ?

hello, here’s the report from profiler for the mentioned method…

total run time was 1400 milliseconds and this function is eating up 435 ms out of that. the function call starts from bottom and it goes up till markAllSubMeshesAsDirt().


Maybe a better strategy in your case would be to instantiate all you need at load and enable/disable on demand instead of instantiating ???

It depends if it might fit your needs.

I’ve seen a similar performance issue with the glTF loader. Can you try this?

// Block the marking of materials dirty until the scene is loaded.
const oldBlockMaterialDirtyMechanism = scene.blockMaterialDirtyMechanism;
scene.blockMaterialDirtyMechanism = true;

call InstantiateModelsToScene

// Restore the blocking of material dirty.
this._babylonScene.blockMaterialDirtyMechanism = oldBlockMaterialDirtyMechanism;

If this works, we maybe should add it to the InstantiateModelsToScene function itself.

1 Like

Hello, Thanks for responding. I have tried the suggestion but not sure if its solving the issue…
here’s the updated profile report…

there are 3-4 markAsDirty calls now. Total time was 400 ms.

1 Like