Using multiple cameras with layer masks and an object with a refractive material causes some issues with textures. The use case I have ran into involves two cameras with different layer masks, and in one of the cameras, an object with refraction is drawn. While this does work, the layer mask for the opaqueSceneTexture (which is used for the refraction effect) is ignored, and there’s no straightforward way to reference that texture (at least last time I checked).
Refraction not respecting layerMask checks | Babylon.js Playground
Here is a demo which exhibits the issue. There is a textured plane and a refractive cube using different layer masks. The cube will appear as though the textured plane is inside of it due to opaqueSceneTexture not checking the layer mask. This effect can be observed even if scene.activeCameras is set to just [camera1] (line 16).
In the demo, the issue can be fixed by pressing the Space key, which sets forceLayerMaskCheck = true on the opaqueSceneTexture. Not sure if this results in the correct behavior in all circumstances, but it fixes this use case at least.
If the opaqueSceneTexture could be accessed via some API (e.g. know when it’s created and the ability to get a reference to it), I could have used that to modify the refraction texture’s properties. However, the only way I think this can be done currently is by the hacky .find workaround seen in the demo.
TL;DR meshes on a layer that a camera isn’t rendering will still show up in the refraction texture of other objects because refraction doesn’t seem to filter based on the camera’s layer mask.