Hey,
I’m developing a project where users can build a room and be able to put a hole on a wall.
I’m using stencil buffer shaders to do it :
- one used for the plane making a hole in the wall :
Shader "Custom/Stencil/Mask OneZLess"
{
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry-1" }
ColorMask 0
ZWrite off
Stencil
{
Ref 1
Comp always
Pass replace
}
Pass
{
Cull Back
ZTest Less
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 = mul(UNITY_MATRIX_MVP, v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
return half4(1,1,0,1);
}
ENDCG
}
}
}
- one for the wall that needs to be cut by the plane :
Shader "Custom/Stencil/Diffuse NotEqualOne"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
LOD 200
Stencil
{
Ref 1
Comp notequal
Pass keep
}
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
fixed4 _Color;
struct Input
{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
o.Alpha = c.a;
}
ENDCG
}
Fallback "VertexLit"
}
The walls are like this :
- The red wall uses the wall shader with Ref = 1
- The 2 blue planes use the mask shader with Ref = 1
- The red green uses the wall shader with Ref = 2
- The 2 yellow planes use the mask shader with Ref = 2
In game it give me this :
Here, no problem, I can see through both holes.
Same thing if I look from the other side, no problem.
The problem is when I’m looking through one hole and there’s another behind it. In this case the second hole doesn’t render properly, as you can see :
Has anyone ever tried to do something like this ?