Why stencil does not always work?

Hello everyone! I wrote a really simple stencil shaders:

(1)
Properties {
 _Color ("Color", Color) = (1,1,1,1)
 _MainTex ("Albedo(RGB)", 2D) = "white" {}
 _Glossiness ("Smoothness", Range(0,1)) = 0.5
 _Metallic ("Metallic", Range(0,1)) = 0.0
 }
 SubShader {
 Tags { "RenderType"="Opaque" }
 LOD 200

 Stencil {
 Ref 2
 Comp notEqual
 //Pass zero
 }
 
 CGPROGRAM
 //PhysicallybasedStandardlightingmodel,andenableshadowsonalllight types
 #pragma surface surf Standard fullforwardshadows

 //Useshadermodel3.0target,togetnicerlooking lighting
 #pragma target 3.0

 sampler2D _MainTex;

 struct Input {
 float2 uv_MainTex;
 };

 half _Glossiness;
 half _Metallic;
 fixed4 _Color;

 void surf (Input IN, inout SurfaceOutputStandard o) {
 //Albedocomesfromatexturetintedby color
 fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
 o.Albedo = c.rgb;
 //Metallicandsmoothnesscomefromslider variables
 o.Metallic = _Metallic;
 o.Smoothness = _Glossiness;
 o.Alpha = c.a;
 }
 ENDCG
 }
 FallBack "Diffuse"

And I wrote this shader:

(2)
SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        Pass {
            Stencil {
                Ref 2
                Comp always
                Pass replace
                //ZFail decrWrap
            }
       
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : POSITION;// SV_POOOO
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(1,0,0,1);
            }
            ENDCG
        }
    }

These shaders work as I had planned. Now I have decided to make a special shader, it’s this:

(3)
Properties
 {
 _MainTex ("Base", 2D) = "" {}
     …
 }
 
 CGINCLUDE
 #include "UnityCG.cginc"

 struct appdata_t {
     …
 };

 struct v2f
 {
     …
 };
 
 sampler2D _MainTex;
 
 v2f vert(appdata_t v)
 {
     v2f o;
     …
    return outC;
 }
 ENDCG 
 
 Subshader
 {
     Tags {
        "Queue"="Transparent" 
        "IgnoreProjector"="True" 
        "RenderType"="Opaque" 
      }

 Blend SrcAlpha OneMinusSrcAlpha
 GrabPass { 
 Name "BASE"
    Tags { "LightMode" = "Always" }
 }
 Pass
 {
     Stencil {
        Ref 2
        Comp always
        Pass replace
     }

 Cull off///Front//Off 
 Lighting Off 
 //ZTest Less
 //Cull off//front //back
 ZWrite off//ON//Off show backward wall
 Fog { Mode off }


 CGPROGRAM
 //#pragma target 3.0
 #pragma multi_compile_particles
 #pragma fragmentoption ARB_precision_hint_fastest 


 #pragma vertex vert
 #pragma fragment frag
 ENDCG
 }
 }
 Fallback off

This shader (3) should work with a shader (1) as well as shader (2) works with it.
But it does not work. Why???

The marking of the stencil buffer (by shader 2 or 3) should happen before the testing (by shader 1.) Since the Queue of shader 3 is transparent, the stencil buffer is only marked after shader 1 is drawn.

Render order is important to keep in mind when using the stencil buffer. I would recommend not to rely on the Queue values in the shaders, since these are often not applied correctly. It’s more safe to set them directly on the materials from a script.

Thank you so much!)