Transparent Walls

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)

Hi , I’m here to help you :slight_smile:
before answering I recommended Instead of changing Stencil from shader change It from Inspector.because It may confuse you
https://github.com/supyrb/ConfigurableShaders/blob/master/Assets/Shaders/ConfigurableStencil.shader

It’s because walls have same stencil comparison , so sphere will cut both walls!


but Unfortunately this work just on one side of the wall!

to fixing this you can send a ray to sphere Before the rays reach the player It will hit the wall , so you can set target’s Stencil with Comp Not Equal