2nd Scene (UI) Disables Lighting

Hi!

I’m having a very strange issue. I am attempting to get a UI proof of concept working where there is the main scene (the game world) and the UI scene as as as a second scene. I saw this technique on the docs. For my game purpose there will be multiple UIs / menus so swapping and overlaying scenes is important. For this specific first test scene, it is a button panel at the bottom of the screen.

After some testing, I was able to get both scenes to render at the same time. However, the rendering of the second scene disables the lighting on the first scene. The first scene and second scene both have their own camera and lighting.

Note I deleted some launcher / initialization code from the example code below so that helpers didnt have to look through 200 lines of code. basically, my game initialization class loads a series of JS scripts that do various things. Before this happens, a blank scene is made. Then after the scripts are loaded, the 2nd scene, a UI scene is made. The UI scene simply adds one picture to the screen. It does this correctly, but when this 2nd scene / UI scene is active, we see the first scene lighting disappear. Disabling the 2nd scene then reenables lighting Note I do in fact have a camera and lighting for the first scene but I deleted that example code from below for tidiness. I know its working because only when the 2nd scene is enabled does the first scene have any issues.

At this link we see the example of the issue. The UI scene correctly shows an example panel, but the model on the main scene / 3d world is totally black. Turning off Scene 2 initialization and rendering fixes this.

https://imgur.com/a/9PENxf7

Any help would be appreciated. Thanks so much!

class GameInitialization {
  constructor(engineInstance) {
    window.baseMenuUI = null;
    this.scene = new BABYLON.Scene(this.engine);
  }

  /**
   * Initializes the game by loading all necessary scripts and setting up the scene.
   */
  initialize() {

    this.loadScripts(scriptsToLoad, () => {

       this.scene2 = new BaseGameUI(this.engine);
         this.scene2.autoClear = false;
        this.scene2.initUI();

      var camera = new BABYLON.FreeCamera(
        "camera",
        new BABYLON.Vector3(0, 0, 0),
        this.scene2
      );
      camera.setTarget(BABYLON.Vector3.Zero());
      const light = new BABYLON.HemisphericLight(
        "dayLight",
        new BABYLON.Vector3(0, 1, 0),
        this.scene2
      );
      light.intensity = 1;
      // Set up resize handling
      this.setupResizeHandler();

      // Start the render loop
      this.engine.runRenderLoop(() => {
        this.scene.render();
        // this.scene2.render();
        //this.baseMenuUI.render();
      });
      this.buildScene();
    });
  }


If you can reproduce it in the Playground, it will be easier to help you or to see what is happening.

1 Like

Thank you! I will do that tomorrow!

1 Like

Hi!

So I just looked at Babylon playground, and I don’t see a way to even add a second scene. The entire code body by default returns one scene which presumably is rendered in an unobservable / uneditable loop.

I did see an article from August 2024 that says multiple file support will be added for playground in the future. However, considering my issue is the ability to add a second scene for UI (so that I can have multiple UI scenes such as a player level menu, a settings menu, a map selector and so on), I don’t see how I can effectively / at all load two scenes into playground to show the issue.

Is anyone perhaps willing to simply create two scenes in Babylon with 1 scene having a single model (can be anything) and a second scene hosting the 2d UI system, and then the 2 scenes both rendered?

@Dad72
*EDIT I switched from point light to hemispheric light (1 from above and 1 from left and right) and now the issue no longer appears. I’m testing if I can reproduce this, because it is certainly an odd behavior for the light on scene 1 to be either broken or not broken by a separate second scene depending on what light is implemented.

You may refer to the docs - Babylon.js docs
and PG example with multiple scenes - Babylon.js Playground
As for me, I would use the second camera for the UI, not the second scene.

Thank you that is super helpful! Bookmarked. Also it seems my issue is resolved, so I’m going to close this ticket

@labris could you elaborate on a second camera for the UI? If I have multiple UIs in a complex / fully featured game, isnt simply swapping which UI scene is rendered more effective / efficient than swapping cameras? How would I even change which controls / UI images are loaded as part of a singular UI Object without manually deleting and adding them one by one?

1 Like