Using Multiple Scenes
How to Use Multiple Scenes
To use multiple scenes, create them with
import { Scene } from "@babylonjs/core/scene";var scene0 = new Scene(engine);var scene1 = new Scene(engine);Then put the relevant camera, lights, and meshes inside each scene and call them in runRenderLoop.
engine.runRenderLoop(function () { scene0.render(); scene1.render();});However, this simple code has a potential problem. Remember 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:
import { Scene } from "@babylonjs/core/scene";var scene0 = new Scene(engine);var scene1 = new Scene(engine);scene1.autoClear = false;engine.runRenderLoop(function () { scene0.render(); scene1.render(); // Since scene1's autoClear is false, it will not "erase" what scene0 has rendered.});Multiple scenes on the Playground
However, there is a difference between writing multiple scenes within your own projects and trying them out in the Playground.
In your own project, it is easy enough to set up create scene functions for each scene and call them in the engine render loop. For example:
import { Scene } from "@babylonjs/core/scene";var createScene0 = function () { var scene0 = new Scene(engine); //Add camera, light and meshes for scene0 return scene0;};var createScene1 = function () { var scene1 = new Scene(engine); //Add camera, light and meshes for scene1 return scene1;};//Any other codevar scene0 = createScene0();var scene1 = createScene1();engine.runRenderLoop(function () { scene0.render(); scene1.render();});The Playground has its own engine render loop, which needs to be stopped before running your own version:
import { Scene } from "@babylonjs/core/scene";var createScene = function () { var scene = new Scene(engine); //Add camera, light and meshes for scene ////////OTHER SCENE//////////////////// var scene1 = new Scene(engine); //Add camera, light and meshes for scene ////////CONTROL ENGINE LOOP/////////// setTimeout(function () { engine.stopRenderLoop(); engine.runRenderLoop(function () { scene.render(); scene1.render(); }); }, 500); return scene;};An example of switching the currently active scene on the Playground is as follows:
Switching Scenes