How to load an asset in script without freezing the screen?

I tried loading a city map (less than 7MB in texture files) in runtime by pressing L. I tried loading by tag or enabling the entity. Both freeze the screen while loading for about 3 seconds. Is there a way to load an asset runtime by script without the freeze?

Here is my project.
https://playcanvas.com/editor/scene/1308171

Hi @metame,

I’ve tried your example, it seems to work quite well.

Pressing keyboard L does nothing though, any idea how to reproduce your issue?

You can press F and see the car move forward inside a tunnel and then press L which will load the city showing up at the end of the tunnel.

You will find that the animation freezes and everything freezes for a few seconds while the city is loading. Basically I will be replacing the L with another event so the city loads as the car approaches the end of the tunnel. Whether L or another event, the freeze will still happen.

Ah I see, yes, I can reproduce now.

The freeze is most likely due to materials compiling their shaders. The first time a material is being rendered in scene if it uses a shader that hasn’t being compiled, it may freeze the main thread for a while.

One way to avoid that is to have your models initially enabled on the application first frame, and immediately disable them. That should be enough to get them to compile.

1 Like

How does that look code wise? Do I enable the city entity in the beginning and disable right away in the code right after the enabling code? And then reenable again later on upon pressing L?

1 Like

You can start with the model loaded, then on the first update you check if its enabled and then just set it to disabled.
Use a flag to make sure it only happens once

1 Like