Custom UI shader does not update inside UI Mask

I have created a UI shader with Amplify Shader that works perfectly on its own, but when its masked, It does not update except when compiled.

Shader "HealthShader"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
       
        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _ColorMask ("Color Mask", Float) = 15

        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
        _FirstBar("FirstBar", Range( 0 , 1)) = 0.838034
        _SecondBar("SecondBar", Range( 0 , 1)) = 0.3559519
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
           
        }
       
        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp]
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]


        Pass
        {
            Name "Default"
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            #pragma multi_compile __ UNITY_UI_ALPHACLIP
           

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

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                half2 texcoord  : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };
           
            uniform fixed4 _Color;
            uniform fixed4 _TextureSampleAdd;
            uniform float4 _ClipRect;
            uniform sampler2D _MainTex;
            uniform float _FirstBar;
            uniform float _SecondBar;
           
            v2f vert( appdata_t IN  )
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.worldPosition = IN.vertex;
               
                OUT.worldPosition.xyz +=  float3( 0, 0, 0 ) ;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                OUT.texcoord = IN.texcoord;
               
                OUT.color = IN.color * _Color;
                return OUT;
            }

            fixed4 frag(v2f IN  ) : SV_Target
            {
                float4 _Color0 = float4(0.08855966,0.9264706,0.198355,0);
                float2 uv1 = IN.texcoord.xy * float2( 1,1 ) + float2( 0,0 );
                float temp_output_9_0 = ( 1.0 - floor( ( uv1.x + _FirstBar ) ) );
                float4 appendResult30 = (float4(_Color0.r , _Color0.g , _Color0.b , temp_output_9_0));
                float blendOpSrc20 = temp_output_9_0;
                float blendOpDest20 = floor( ( uv1.x + _SecondBar ) );
               
                half4 color = ( appendResult30 + ( 1.0 - ( saturate( ( 0.5 - 2.0 * ( blendOpSrc20 - 0.5 ) * ( blendOpDest20 - 0.5 ) ) )) ) );
               
                color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
               
                #ifdef UNITY_UI_ALPHACLIP
                clip (color.a - 0.001);
                #endif

                return color;
            }
        ENDCG
        }
    }
    CustomEditor "ASEMaterialInspector"
}

@PelvisParsley did you find a solution to your problem? I’m having similar problem with another shader.

Did any of you find any solution? I have just encountered this problem and the weird thing is that my shader is quite complex, putting together 3 effects and only one of these effect “branches” is not working under the mask while others do.

@Wolar Sorry for the late reply. The problem in my case was that Unity instantiates all materials under a Mask component, meaning that my reference to my custom material (and thus shader properties) was invalid.
In my case the solution was to use global shader properties to set the shader parameters runtime. (See the SetGlobal…() methods in documentation https://docs.unity3d.com/ScriptReference/Shader.html)

I have figured out the same thing just resolved it differently, I used materialForRendering from Graphic component instead of just material

https://docs.unity3d.com/ScriptReference/UI.Graphic-materialForRendering.html

2 Likes

This did the job, thank you for posting your solution!