Multi_scene nullengine

I need to create several scenes on the server (with NullEngine), for each player. Accordingly, scenes will be added in the process. But I don’t understand how to use it correctly:
engine.runRenderLoop(function () {
scene0.render();
scene1.render();
});
Those. I don’t understand how exactly this works. From the documentation and examples, I assume that I need to put an array of scenes in the runRenderLoop function call like this:
engine.runRenderLoop(function () {
arrayscene.forEach(s => {
s.render()
})
})
at the same time add new scenes to the arrayscene or delete unnecessary ones. I could experiment, but I don’t know how to check that render works correctly.
I also don’t understand whether it is necessary to use .autoClear = false for each scene. Please consult me someone who understands the issue.

First regarding the render loop - you could have an external array like you suggested, or you could have a different render loop for each scene. so you could do that as well:

engine.runRenderLoop(function () {
scene0.render();
});
engine.runRenderLoop(function () {
scene1.render();
});

the engine will add those to the active render loop array and will execute them when rendering. This might be simpler for you to handle.

Regarding clear - that depends on how you use the scene(s). If you can reproduce this or share a bit more code as to how your scenes look like it will be simpler to answer. The general answer is - you should set whatever fits your use case, but this is not really a good answer :slight_smile:

Unfortunately, I cannot provide an example, since I initially had one common scene, so the question of multi-scenes did not arise, but now it was necessary to create separate scenes for each player and delete them when the player is no longer active (not immediately, but if he will not connect to the server for a month so that they do not take up memory).
So I decided to find out how to do it correctly before I started doing it.
And I don’t understand how it works, so I can’t figure it out myself :frowning:
From this example from the documentation Babylon.js Playground
It seems to me that “function () {scene1.render()}” runs constantly and iterating through the array of scenes in it would be the right solution to process only the current list of scenes, did I understand correctly?
And do I need to use “scene.autoClear = false” for all scenes or vice versa on the server do I not need to use it for any?

By default scene.autoClear = true. It means that the canvas where the scene in drawn is cleared every frame. Babylon.js automatically clears the color, depth, and stencil buffers before rendering the scene. Here is the simplest example of what will happen if the scene with clearColor is not cleared - Babylon.js Playground (try to move camera and see what will happen).

If your scene is set up in such a way that the viewport is always 100% filled with opaque geometry (if you’re always inside a skybox, for instance), you can disable the default scene clearing behavior with scene.autoClear = false.
Example - https://playground.babylonjs.com/#4MZQU7#1

Relating to multi-scene rendering it means that each scene.render call will try to clear what has been rendered before, and to avoid one scene erasing what another has rendered, you need to set scene.autoClear = false on all the scenes rendered on “top” of others:

var scene0 = new BABYLON.Scene(engine);
var scene1 = new BABYLON.Scene(engine);
scene1.autoClear = false;

In this example from the Docs - Babylon.js Playground - we have 3 scenes. 2 of them are toggling between active/inactive scene and GUI scene is always on top.
If we don’t disable scene clearing for the GUI scene, we’ll not be able to see any other scene except it. Example - https://playground.babylonjs.com/#P3E9YP#50.
If we don’t clear scenes between switching (secondScene.autoClear = false) the situation will look like here - https://playground.babylonjs.com/#P3E9YP#51 - after the first switching the first scene is not cleared, and the clearColor of the second scene is not visible.

Hope this explanation helps :slight_smile:

Meanwhile, I am curious to ask what is the use case for the rendering of multiple scenes for different users? Do you really need to render all players’ scenes at once one over another?

1 Like

That’s the thing, I don’t know. The issue is not with the browser, but with the server with nullEngine. On my server I need multiple scenes for the navigation plugin, for each scene and in some cases for raycasting so that the data on the client cannot be tampered with.
There’s probably no need for separate scenes for everything else, because everything else could be solved with simple objects and without Babylon.
This is why there are so many seemingly stupid questions, because I don’t understand how this would work in NullEngine and whether there is even a need for it.