Custom shader to create a mask problem

Hello!
I have a problem with my custom shader designed to create a mask for SpriteRenderer.

My intention is to create a material called mask and another maskable so that I can assign them and the spriteRenderer itself acts as a mask.
The truth is that my code apparently works correctly, however the problem is that it interferes with the unity mask system, so if a gameobject overlap with a spriteRenderer configured as “Visible inside mask” it will be shown even if it does not have my custom maskable material…

Any ideas on how to isolate my custom behavior from the default one so that this does not happen?

I think that with this example it will be better understood, the goomba should not be shown inside the brick since it does not have the maskable material.

This is the code of my shaders:

Mask

Shader "Custom/SpriteMask"
{
 Properties
 {
     [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
     _Color ("Tint", Color) = (1,1,1,1)
     [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
 }

 SubShader
 {
     Tags
     {
         "Queue"="Transparent"
         "IgnoreProjector"="True"
         "RenderType"="Transparent"
         "PreviewType"="Plane"
         "CanUseSpriteAtlas"="True"
     }

     Cull Off
     Lighting Off
     ZWrite Off
     Fog { Mode Off }
     Blend One OneMinusSrcAlpha

     Pass
     {
         Stencil
         {
             Ref 1
             Comp always
             Pass replace
         }

     CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma multi_compile DUMMY PIXELSNAP_ON
         #include "UnityCG.cginc"

         struct appdata_t
         {
             float4 vertex   : POSITION;
             float4 color    : COLOR;
             float2 texcoord : TEXCOORD0;
         };

         struct v2f
         {
             float4 vertex   : SV_POSITION;
             fixed4 color    : COLOR;
             half2 texcoord  : TEXCOORD0;
         };

         fixed4 _Color;

         v2f vert(appdata_t IN)
         {
             v2f OUT;
             OUT.vertex = UnityObjectToClipPos(IN.vertex);
             OUT.texcoord = IN.texcoord;
             OUT.color = IN.color * _Color;
             #ifdef PIXELSNAP_ON
             OUT.vertex = UnityPixelSnap (OUT.vertex);
             #endif

             return OUT;
         }

         sampler2D _MainTex;

         fixed4 frag(v2f IN) : SV_Target
         {
             fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
             if (c.a<0.1) discard;
             c.rgb *= c.a;
             return c;
         }
     ENDCG
     }
 }
  }

Maskeable

Shader "Custom/SpriteMaskeable"
{
Properties
{
    [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    _Color ("Tint", Color) = (1,1,1,1)
    [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}

SubShader
{
    Tags
    {
        "Queue"="Transparent+1"              
        "IgnoreProjector"="True"
        "RenderType"="Transparent"
        "PreviewType"="Plane"
        "CanUseSpriteAtlas"="True"
    }

    Cull Off
    Lighting Off
    ZWrite Off
    Fog { Mode Off }
    Blend One OneMinusSrcAlpha

    Pass
    {
        Stencil
        {
            Ref 1
            Comp Equal
        }

    CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #pragma multi_compile DUMMY PIXELSNAP_ON
        #include "UnityCG.cginc"

        struct appdata_t
        {
            float4 vertex   : POSITION;
            float4 color    : COLOR;
            float2 texcoord : TEXCOORD0;
        };

        struct v2f
        {
            float4 vertex   : SV_POSITION;
            fixed4 color    : COLOR;
            half2 texcoord  : TEXCOORD0;
        };

        fixed4 _Color;

        v2f vert(appdata_t IN)
        {
            v2f OUT;
            OUT.vertex = UnityObjectToClipPos(IN.vertex);
            OUT.texcoord = IN.texcoord;
            OUT.color = IN.color * _Color;
            #ifdef PIXELSNAP_ON
            OUT.vertex = UnityPixelSnap (OUT.vertex);
            #endif

            return OUT;
        }

        sampler2D _MainTex;

        fixed4 frag(v2f IN) : SV_Target
        {
            fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
            c.rgb *= c.a;
            return c;
        }
    ENDCG
     }
 }
}

Does anyone have any idea how to fix this? I’ve been trying to change the stencil reference values ​​but I haven’t been able to do anything… :frowning: