Scene.onPointerObservable and multiple active cameras and layerMasks: how to get what's picked from a specific camera/layer?


I am using 2 active cameras with 2 layerMasks (0x10000000 and 0x20000000). One pair of camera/layerMask is for the 3D objects in the game world, while the other pair camera/layerMask is for the 3D objects in my menus.

My problem is that when I open my menu, the 3d objects are indeed pickable when I click on them, while when I close my menu, the 3D objects in my world are not pickable. The pointerDown event is caught but when I inspect the pickInfo, the pickedMesh is equal to null…

Is there anything to specify to onPointerObservable that we want to observe clicks on a specific layerMask/Camera?

Here how the game looks like:

I need to be able to pick up the letters by clicking on them basically.

Any thoughts?

Hey, I would first recommend to switch to babylonjs v4.0 :slight_smile:

You can decide which camera should be used for picking with:

scene.cameraToUseForPointers = xxxx
1 Like

Hello @Deltakosh :slight_smile:

Yes I switched to 4.0 just after I posted, but for some reasons, my inspector doesn’t want to change to the prettier one :confused: I will open another thread if it still doesn’t work after I reboot my machine.

Concerning the answer, that’s great :slight_smile: I was wishing it would be that simple. Can we add a line in the doc, if not mentioned already? especially in the parts where you speak about multicamera and mask layers settings? I kind of wasted one day searching how to do but I may have missed it :frowning:

Thanks again for the answer!

Oh please do. Everything that can improve the documentation is more than welcome!

1 Like


The solution works, but I want to point out that I believe it should not work.

I have one camera World.Camera for my 3D world and one camera GUI.Camera for my GUI. If we can use only one camera for picking, then if I select the camera World.Camera, I should not be able to click on my GUI buttons and vice-versa.

However, it happens that I can still click on the GUI buttons after I select World.Camera as the camera for picking. By chance, this is what I wanted; so I am happy; but it seems to me that it should be the case.

This is expected: Both cameras live in the same scene and thus can see the same objects.

If you want to filter meshes by camera you have toi use layerMask on both cameras and meshes so cameras will only see a defined set of the scene

Does it make sense?

Can we render only one camera/layerMask with the new inspector, to make sure of what objects are seen?

I need to double-check if I set it up correctly, but I am using layerMask.

You can do that:

scene.activeCameras = null;
scene.activeCamera = theCameraIWantToCheck;

yes that is true, but to be able to do that with a click in the inspector would be very useful for debugging a scene without reloading all the game :slight_smile:
I found myself struggling finding out what the different cameras were seeing and with what I could interact from which camera.

Maybe for the next update :smiley: ?