Problems with silhouette overlay shader

Hello, everyone. I’m trying to make a shader to use when characters are behind walls, to draw a transparent silhouette (blending a solid color with the wall).
The problem that I have is that my characters are modelled with more than 1 mesh, so even if I make the shader to avoid rendering, and thus blending, hidden parts, when it renders another mesh from the same character, the blending inevitably occurs, as you can see in the picture attached. The hierarchy of my model also looks like in the other picture.

2343462--158527--hierarchy.png 2343462--158533--sil1.png

Now, to the core of the problem. I thought I could write the stencil buffer, and then the other meshes of the same character would ignore the parts already drawn, but that doesn’t work if there are other characters overlapping that character, they would not render the silhouette, resulting in fake visual depth order, like this:

2343462--158535--sil3.png

This is the shader to get this result:

SubShader {
            Tags { "Queue" = "Transparent+100" "IgnoreProjector"="True" "RenderType"="Transparent" }
            LOD 100
            ColorMask RGB
            Lighting Off
      
            Pass {
               //the first time the silhouette is rendered, writes 10 to the stencil. Other passes won't render, because stencil doesn't pass (Comp NotEqual)
                Stencil {
                    Ref 10
                    Comp NotEqual
                    Pass Replace
                }
          
                ZTest Greater  //to draw only behind walls
                ZWrite Off
                Blend SrcAlpha OneMinusSrcAlpha
              
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"
              
                uniform float4 _ColorOverlay;
               
                struct vertexOutput { float4 pos: POSITION; };
              
                vertexOutput vert ( appdata_base v ) {  
                    vertexOutput output;
                    output.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                    return output;
                }
                float4 frag ( vertexOutput input ) : COLOR {
                    float4 lColor     = _ColorOverlay;                  
                    lColor.a = 0.5;                  
                    return lColor;
                }
                ENDCG
            }
}

Then what I tried was to “reset” the stencil buffer after rendering the mesh, with an extra pass like this:

Pass {
                ZTest Always
                Colormask 0
                ZWrite Off
              
                Stencil {
                    Ref 10
                    Comp Equal
                    Pass Zero
                }
            }

…but then, what I got is the first result I told. That would be with the 2 characters:

2343462--158534--sil2.png

So… what can I do to get the characters with all its meshes in a flat color, and in the correct depth order?

Any help would be appreciated, shader gurus! Thanks for your time!

bump!

Hi,

I don’t know if this is possible because of how the render pipeline works.
The first object drawn is the wall as it is opaque. Afterwards the transparent objects are drawn in back to front order so the first object drawn is the furthest away which operates first on the stencil buffer. So the pixel of the green object is already blended with the wall before the red object is drawn. Deleting the green pixel also results in deleting the wall pixel.

The best thing I think is to use opaque silhouettes.
The other possibility would be to change the render order of the transparent objects so that the stencil buffer can be used.
e.g. use one shader with Transparent+10 and one with Transparent+100.
But this is static and I don’t know if you can change the render order on the fly. Possibly with Material.renderQueue.

Cheers Mark