Simulating multiple scenes

Hi everyone,

I need to simulate multiple scenes but in a single one because I need to save / load my scene (stringify, LoadSceneAsync) and these functions only use a single scene : i can’t use the multiple scenes (Babylon.js docs).

So I need to use multiple cameras, each of them showing only some meshes and hiding others…

But : I’ve read that there are only 4 layers for the cameras (Babylon.js docs) and i do not know how many “scenes” i need. So I can’t use the camera layers either.

Does anybody know how to do that ?

Thanks,

Boris

There is no limitation to layerMask (it is a 32 bits number if you us 0x00000000 format so you have as much as you want :))

Even for the cameras ?

Yup, I’m not sure where did you get that feeling but the doc does not state there is a limit

It is probably because i did not underestand the doc here :

A layerMask is a number assigned to each mesh and camera. It is used at the bit level to indicate whether lights and cameras should shine-upon or show the mesh. The default value, 0x0FFFFFFF, will cause the mesh to be illuminated and shown by any stock light and camera. To determine if a mesh is seen by a camera, a bitwise AND is performed and the result is compared to zero:

javascript

mesh.layerMask & (camera.layerMask !== 0);

The feature is used primarily when multiple cameras are active at the same time. If you wish to have a mesh that is always visible on the screen and pickable, e.g. a button, you might add a second camera and light to the scene to exclusively show and light it.

You’ll need the 2nd camera to ONLY see the button. The button should also only be visible once.

Notice that the default layerMask starts with the first 4 bits being 0, or off. If the 2nd camera and button were to both have a layerMask with one of the 4 values below, then the 2nd camera would only see the button:

  • 0x10000000
  • 0x20000000
  • 0x40000000
  • 0x80000000

I will read it again. Thanks for your answer.

1 Like

Overall the idea is that a camera will render any mesh when mesh.layerMask & (camera.layerMask !== 0) is true

so if camera.layerMask = 1 and mesh.layerMask = 1 then it will render :slight_smile:

1 Like

I’m really struggling with the masks but it’s very interesting. If I understand well, i mean there is a mistake in the docs.

in this page (Babylon.js docs) it’s written :

mesh.layerMask & (camera.layerMask !== 0);

And in this page (Babylon.js docs), it’s written :

So, for eg, if camera.layerMask = 35 and mesh.layerMask = 2 , 35 & 2 == 2 != 0 so the mesh is visible from this camera.

So i mean the correct formula is :

(mesh.layerMask & camera.layerMask) !== 0.

And if i’m right, there are only 32 combinations of masks which are excluding each others (if we are working with 32 bits).

Am i right or do i understand anything ?

Thanks for your answer.

You are totally right! The doc is wrong!
I’ll fix it!

2 Likes