Hi all,
I wanted to create a shader that makes walls seethrough if the player is standing behind them. My current solution adds a sphere with a shader around the player. This sphere shader sets the stencil buffer to 1.
My wall shader just has a stenciltest which stops drawing if the stencil buffer is 1.
This works. However, also walls behind the player will be made transparent. So I tried to add a ZTest to the sphere shader: Just set the StencilBuffer if ZTest is GEqual. Sadly if I do this no wall ever will be transparent. I am quite sure the reason is: Sphere_Shader is running at Geometry-1. So there is no Geometry to loose ZTest against. If I change it to Geometry+10 the StencilBuffer may be set correctly, but the wall is already drawn everywhere.
My idea to get a working solution was: Set the Stencil Buffer in the Sphere Shader at Geometry+10. Create the wall before but with ColorMask 0. Have a second pass for the Wall at Geometry+11 writing only the Colors AND checking the (now correctly set) Stencil Buffer and only draw if buffer != 1.
Sadly I cannot get this to work as CGPROGRAM is creating errors if I put it into a Pass {}. And as I am very new to shaders I hope you could support me somehow: Is my idea an approach that could work? If not, which approach could work? If yes, what do I have to change?
My current shaders are mostly identical to the first two shaders presented in this post: unity - How can I create a "see behind walls" effect? - Game Development Stack Exchange (Lets implement: Wall, Mask)

