Working with on-demand rendering loop

I’m not sure to get what is needed. What if a mesh moves? Do you want to track every vector3 changes?

What is the list of things to track?

Scene.isReady can track a lot (like loading or shader being ready)

This seems to be a complete solution.

If meshes are moving within the engine - then yes, but if meshes are moving in the application code, then the developer should track these changes themselves.

If I change some properties of a mesh, for example, the material, and the visual part doesn’t change immediately, but only after some internal processes in the engine occur, then it would be good to have an event for this.

For example, I’ve now encountered a situation where a mesh is rendering incorrectly because its shader hasn’t compiled yet. It’s good that I understand that in this case, I need to compile the shader and call forceCompilation with a callback. But what if there’s some other change where I won’t know after what I need to trigger a redraw.

Or another example. Camera movement with the mouse. Currently, I’m doing a redraw when scene.activeCamera.hasMoved. If there was a flag indicating that the scene is currently in dynamic state, then we could just check that, which would significantly simplify the final code of applications using Babylon.

I propose creating a scene property hasChanged, which would be set to true after every change that leads to a change in the picture on the screen. And it would be set to false at the beginning of scene.render().

Then the render loop would look like this:

engine.runRenderLoop(() => { if (scene.hasChanged) scene.render(); });

And, for example, changing the position of a mesh would look like this:

mesh.position = new Vector3(1,2,3); scene.hasChanged = true;

And every shader compilation would also end with the expression:

scene.hasChanged = true;

I honestly do not believe we need more than what we have already.

In a user controlled renderloop, I can see 2 things that will mess up with when the scene is ready:

  • Parallel shader compilation: This one needs simply to be turned off
  • Loading resources: This is accounted by scene.isReady

With that, you can then theorically call scene.render() when you want to see a change if scene.isReady().

Having a property that the user is supposed to set on the scene will be wrong as this is user land. If you want to track when to render, you can create your own global var, you don’t need the scene for that

2 Likes

I have created a working solution with this exact approach here but it seems it didn’t get @andrii’s attention:

1 Like

I think the problem is much deeper now. For example, even the camera will not work or track mouse movement without the call to draw. So we need to write a lot of hacks to make it work. For example, we need to call render for each mouse event… so there is no way to make an efficient solution for mobile platforms where render will be called only when needed, and mobile devices now will drain the battery like hell rendering almost all the time.

The hack will be there anyway right? But as user code you know what to track in your app so it is easier and cleaner for you to have a flag and render when you need to

1 Like

What would be great is to update the documentation to explain how rendering works in relation to the classes and typical Scene approaches. For example some nice diagrams and references to methods/examples. Include the Scene built-in checks, any internal/exposed flags etc.

1 Like

I do agree. We have to provide a cleaner view BUT as we plan to move anytime soon to a Render Graph model this can be done after because in a Render Graph world we can provide a tool like NME to see the render process :slight_smile:

2 Likes