How to define unique layerMasks in BABYLON?

Hello all,

I am new to BABYLON (and these type of tools actually), and I’m trying to create some simple geometry visualizer but I’m coming up with an issue on how to define new layerMasks. It’s rather a theoretical question, and that’s why I’m not posting code, but if someone thinks it can help, I’ll try providing a simple mockup. I’ll give some introduction before jumping into the actual question. This introduction is not necessary to answer the question, and it is just to provide some extra information in case someone is interested on it, you can jump directly to the question if you prefer so.

INTRODUCTION:
My wish is to create different viewports to visualize a main scenario from different perspectives. To give an example, imagine a boat sailing in the sea, and I want two viewports (left and right in the screen, at some point I might wish for 6 viewports) with two different cameras pointing at the same objective (the boat). For doing so, I have created different viewports with different cameras, and it works fine.

I also want to add some compass in a corner of each viewport that shows the main axes of the boat, and how they turn as the boat turns. My approach for this has been:

  • I created a new viewport for the compass area ;
  • I created a compass (some orthogonal axes);
  • I created a new secondary camera, that points at the compass;
  • I update the secondary camera, so that it follows the movement of the main camera (the one that points at the boat);
  • I created a new layerMask and bounded the compass, and the secondary camera to this layerMask, so that the secondary camera can only see the compass and the main camera is not able to see the compass.

Up to here everything works as expected. My issue comes when I generate several viewports for different perspectives (once again, imagine left and right division of the screen), because apparently the camera in layer “A” can see the objects in layer “B”, and my secondary camera in the right of the screen sees both its compass and the one from the left screen. (I know it might seem some code could help here, but this was just some introduction in case someone wanted more background, it’s not really necessary to solve the question).

QUESTION: I have seen (Use Layermasks and Multi-Camera Textures - Babylon.js Documentation) that layers can be defined as image, and also as integers (I can’t find now the playground where I saw so, I’m sorry). But I don’t really know how to create new independent (each one is isolated from the rest) layerMasks. If I want to create “n” layerMasks, could I just define:
layerMask_1 = 1;
layerMask_2 = 2;
layerMask_3 = 3;
.
.
layerMask_n = n;

Or there is a specific nomenclature that they have to follow so each one is independent of the rest?

Sorry for the long post, and thank you

Hi @grolivas
Welcome!

LayerMasks are simple bitmasks, :slight_smile:
2 4 8 16, etc …

so 1 2 3 in your example could cause problems,
https://playground.babylonjs.com/#QFRJ7K#6
try changing those layerMask_ONE and layerMask_TWO values to 2 & 3, it’ll break,
2 4 8, etc will always work.