Occlusion queries capture whether object in mirror is occluded, but not the original object

Here is what I have so far (many thanks to the playground examples for mirrors and occlusion queries): https://www.babylonjs-playground.com/#1YAIO7#109

The background turns green when occludee.isOccluded is true, and blue when it is false.

When occludee (the red box) is visible, the background is blue as expected:

However, isOccluded becomes true when the box in the mirror is off-screen, even when the original isn’t:

It doesn’t get occluded by the original blue box:

But does get occluded by the reflected box (note that the z position of the red box is changed here):

Effectively, everything in the original scene is ignored for the occlusion queries. If the mirror isn’t set to reflect the red box, the occlusion queries work correctly, but I would like to detect if the original or its reflection is visible on-screen. Is this doable?

Hey and welcome,

Unfortunately this is not a supported use case :frowning: You will have to not use mirrors or not use occlusion

You can try a trick:


I have created a second “red” box at the same location of the existing red box, but disabling color writing and depth writing: it is there only for the occlusion query with the blue box.

I have disabled color writing and depth writing to speed things up, the regular red box, when drawn, will update the color/depth buffer accordingly.

Screen goes green when only the box in the mirror is occluded, blue when only the box outside of the mirror is occluded, cyan when both boxes are occluded.

Note that if a box is not rendered at all (because culled early), the occluded test will be false! The occlusion test is performed only for meshes that are rendered.

1 Like

Thanks for the trick. Do you know of any way to detect whether the real blue box occludes the mirrored red box, or when the reflection of the red box is still “on-screen” but not visible on the mirror:


For some context, I’m trying to build a puzzle game where a major component is what objects are visible to the player at any given time. Some levels would involve mirrors.

The other approach I’ve seen for something like this would involve a second copy of the scene where each object has a color corresponding to an id. This would then be rendered to a texture. Mirrors would need some special handling with this option, but the trickiest part would likely be iterating through the few million pixels of the output texture each frame.

Yes, rendering each object with a different id (color) in a texture is a way to detect if an object is visible or not. I think using the stencil buffer maybe could help, but the main problem of both technics would be to read back the results, as you said.