Avoid multiple runRenderLoop on reload GLBs

Hello again together,

I’m running into an issue I have been asking myself quite a while. Is there a recommend way to switch .glb-model (dispose old and load another) on runtime using buttons. My problem is I have to wait for assetsManager.onFinish and then I can start runRenderLoop that steadily moves the mesh, because the glb-mesh is not loaded and is accessed by renderLoop. I thought about a simple boolean to check if glb-mesh is ready, but is this the recommended way? I looked into Observables a bit but it seems to be for more rare occurings like playing an animation on certain trigger?

Here is my PG:

You may try to use Asset Containers | Babylon.js Documentation to add/remove models from a scene.
The logic could be something like:

  1. Load all asset containers, not adding from them to scene
  2. Then show or enable buttons
  3. Add/remove models instantly
2 Likes

I see, thanks for the idea, I will change it accordingly. I assume this solves the “loading screen”-issue?

But how do I solve the runRenderLoop issue? Do I call it outside onFinish and add an if-statement with ship.root, because I get “ship.root is undefined”-error when old ship is removed and before new is loaded?

Here is one of possible solutions - https://playground.babylonjs.com/#JA1ND3#678
Press ‘z’ to switch to another model.
Not sure that I understand your runRenderLoop issues, there are no such issues at this example.

2 Likes

If you want to wait to start a new render loop function when the new ship is ready then you should remove the old one, otherwise there will be extra code running, moving the ship multiple times per frame. There was also a runtime error that I fixed by changing the for of loop to a for in loop on line 20.

Personally I would prob create a class for the ship and call ship.update(dt) from the render loop if the ship isn’t null, for example. :slight_smile:

1 Like

Thank you I learned important stuff, so I can combine your solutions. Here is the PG, it seems to be slightly “lagging”, when I switch?

Also does it matter if engine.runRenderLoop is called more than once like on playground plus with scene.render(). Will it be overwritten or add the statement afterwards?

1 Like

I’m not noticing any lag when I run your new playground and nothing in the code jumped out that would cause it… For the other question, the existing render loop functions aren’t overwritten (that’s why the old one needs to be removed in this case).

You are right, it is only the first time I click the button. It is good to know runRenderLoop can remember added function and you can stop(remove) it.