Stencil buffer inconsistency

Hi I’m just throwing out a wildshot question here regarding a problem I encountered.

I have created shaders using stencil buffer to mask a hole in a mesh. It works kinda in that it works at certain camera distances. Basically it goes like this:

  1. Everything works fine
  2. I move the camera back a bit and it stops working (no masking is done)
  3. I move the camera back a bit further and now it works again.

This behavior is the same both in the scene editor and the game view. The scene only contains the mask mesh and the mesh that should be effected by the mask.

Any idea in which area I should start investigating cause i’m kinda stumped here.

Cheers

Hm… I would check if the the render queue is correct (Make sure the mask is rendered first by setting its queue target to one below geometry: “Queue”=“Geometry-1”). Or are you using something like Offset in your shader?

Otherwise… Are you using forward or deferred rendering? I heard that there can be problems with deferred rendering. Maybe post the mask shader here so we can have a look. A simple mask shader would look like this:

Shader "Supyrb/Stencil/StencilMaskOne"
{
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Geometry-1" }
        ColorMask 0
        ZWrite off
       
        Stencil
        {
            Ref 1
            Comp Always
            Pass Replace
            Fail Replace
            ZFail 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
        }
    }
}
1 Like

The render queue seemed to do the trick. Big thanks Johannski!

Awesome. Glad I was able to help :slight_smile: