Hole/Window Stencil Shader Question

Hello fellow game developer collegues,

i am currently working on a project, where the player can build walls and windows. I kind of need it to be as generic as possible, so that i can easily scale/translate the windows in runtime. So i had the great idea of using the Stencil Buffer to “cut out” a hole into my wall - which workes very well, BUT (of course) also cuts out all Walls which can be seen through the new build “window”. This is kind of confusing to write, so here two screens to show what i mean:

1363851--68041--$shaderProb_01.PNG1363851--68042--$shaderProb_02.PNG

So you see, i can look through my wall and see the pink guy behind it, but it also completely “kills” the second wall in the background.

The window shader sets stencil buffer to 1

Shader "Custom/Wall/WallOccluder" {

 Category {
       Lighting Off
       Cull Back
       zwrite off
       
       SubShader {
       
       Colormask 0
       
    	Stencil {
			Ref 1
			Comp always
			Pass replace
		}
       
       Tags { 
		"Queue"="Geometry" 
		"LightMode" = "Always"
		"RenderType"="Opaque" 
		"IgnoreProjector"="True"
		}
            Pass {
            }
        }

and the wall shader is checking for stencil 1:

Shader "Custom/Wall/Wall" {
    Properties {
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB)", 2D) = "white" {}
    }
    Category {
       Lighting Off
       ZWrite on
       Cull Back
       SubShader {
       
		Stencil {
			Ref 1
			Comp notequal
			Pass Zero
		}
       
       Tags { 
		"Queue"="Geometry" 
		"LightMode" = "Always"
		"RenderType"="Opaque" 
		"IgnoreProjector"="True"
		}
            Pass {
               SetTexture [_MainTex] {
                    constantColor [_Color]
                    Combine texture * constant, texture * constant 
                 }
            }
        } 
    }
}

My question is - do you have any ideas on how to build this? I want to avoid cutting real poly-holes in my wall-mesh and i would like to avoid using a cutout texture on the wall. (I think i would try it this way, if there are no other ideas by you guys).

I really would like to know, if there is a smart way of doing this! Thanks in advance

Zeroing out the buffer on stencil fail for the walls should do the trick.

Yay, i didn’t even know i can do this - i definately should read the documentation more carefully. Now it works kind of works, Thanks a lot!

Edit:
seems like i was to fast with beeing happy, my solutions works for single window, but (of course) i cannot see through more than one window - any Ideas? I tried to mess around with “ZFail” a bit, but it seems like the “zfail” options doesn’t work, no matter what i try, results stay the same… So i think i need some more testing to find a good solution.

Sorry for double post, but it seems like “ZFail” is somehow broken or i am using it in the wrong way - i have built a test scene with just 3 planes sorted in z, 3 materials/shaders and could not see any results on Zfail, in whatever way i reordered or changed my planes/shaders. Is there any example or does anybody know how zfail should work?

Have you, by any chance, figured this out?

As far as i remember, i found a workaround which worked kind-of for the project - but i really cant’ tell you wat it was exactly. But i didn’t find the solution directly on my problem.