Stencil Buffer and occlusion

Hello everyone,
We are currently doing a VR project in Unity involving stencil buffer to manage transitions between overlapped rooms.

Like this example :

The main problem is that the window using the stencil buffer show also the room’s walls that are between the camera and the window. (example below)
The window should render walls only if the window is between camera and walls.

No problem from this point of view

Here, we see the wall (on the outside of the room) that is between the camera and the window.

Part of the Window shader : (Ref = 1, Comp = Always, Pass = Replace)

        Tags{ "Queue" = "Geometry-1" }
 
        ZWrite off
        ColorMask 0

        Stencil
        {
            Ref[_SRef]
            Comp[_SComp]
            Pass[_SOp]
        }

Room shader : (Ref = 1, Comp = Equal, Pass = Keep)

An other problem is that the window can be seen through other objects that use an other stencil buffer reference

We tried to deal with render order (render queue, ZWrite, ZTest) without any good results.

Thanks for your help !

We found a solution that work pretty well in our situation.

The two rooms are overlapped and separated in 2 parts, one on each side of the portal/window.
So we can manually set the render queue and modify stencil values of room’s parts to acheive the right occlusion order.

7828725--991452--1.PNG

Have a nice day !

7828725--991455--2.PNG

Another solution would be using an oblique projection matrix for your render texture cam with the near plane set to be the window/portal plane, that would be a more general case solution and wouldn’t require modifying your models