I’m having a problem with a stencil mask sander drawing both objects in front of and behind the object I apply it to. My scene is currently set up like so:
There is simply a (blue) cube with two cylinders on top of it with a window plane in between them.
All blue/purple material are using this shader:
Shader "Custom/Past"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Base {RGB}", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader
{
Tags {"RenderType" = "Opaque" "Queue"="Geometry-2"}
LOD 200
Stencil
{
Ref 1
Comp Equal
Pass Keep
}
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
struct Input
{
float2 uv_MainTex;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
// Albedo comes from a texture tinted by color
fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = c.rgb;
// Metallic and smoothness come from slider variables
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
o.Alpha = c.a;
}
ENDCG
}
//Fallback "VertexLit"
}
And the window plane in the middle is using this shader:
Shader "Custom/PastStencilMask"
{
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry-3" }
ColorMask 0
ZWrite off
Stencil {
Ref 1
Comp Always
Pass Replace
}
LOD 200
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 = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(v2f i) : COLOR
{
return half4(1,1,1,1);
}
ENDCG
}
}
Fallback "VertexLit"
}
I want the blue/purple objects to only be visible through the window plane.
I think the half cylinder in the foreground is being rendered because the stencil value at the window plane is always 1 regardless of depth correct? If that’s the case how would I make the stencil only impact objects behind it so the cylinder in the foreground would not render? I’ve tried messing around with a depth mask but don’t really know enough about them to make them do what I want.