Writing the shader myself I stumbled across this Question so many times because google image search shows it every time.
I might be 4 years to late but for everyone else, I've written a shader that does exactly what's on the picture:
Shader for the Wall (ShaderOne)
Shader "Custom/ShaderOne"
{
SubShader {
Tags { "RenderType"="Opaque" "Queue"="Geometry"}
Pass {
Stencil {
Ref 2
Comp always
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0.5,0.5,0.5,1);
}
ENDCG
}
}
}
Shader for the Object(s) (ShaderTwo)
Shader "Custom/ShaderTwo"
{
SubShader {
Tags {"Queue" = "Transparent" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Pass {
Stencil {
Ref 2
Comp equal
Pass IncrWrap
ZFail keep
}
ZTest less
Cull Front
ZWrite OFF
CGPROGRAM
#pragma vertex vert
#pragma fragment frag alpha
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(0,0,1,0.0);
}
ENDCG
}
Pass {
Stencil {
Ref 2
Comp equal
Pass keep
ZFail keep
}
ZTest less
Cull Back
ZWrite OFF
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata {
float4 vertex : POSITION;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : SV_Target {
return half4(1,0,0,0.5);
}
ENDCG
}
}
}