Custom UI Shader works on some platforms, fails on others.

I have a simple UI shader based off of the UI Default shader that is applied to a masked RawImage to fill with color and fade to full color.

This works great on iOS/tvOS but doesn’t show on some OSX devices. There is no pink replacement when it fails, so its not something wrong with the shader missing on those OSX devices. It just doesn’t render at all. At first I thought it was the UI mask, but this shader has all the correct stencil properties for UI masks.

Unity 2019.3.14f1
I’ve included the shader in the always include shaders list in the Graphics settings.
OSX is set to auto graphics api, which I believe should pick between Metal & OpenGLCore.
On iOS/tvOS are set to use Metal.

I’m stumped trying to identify why its not working sometimes on MACs. Any help?

Shader "UI/Silhouetted" {
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _FillColor ("FillColor", Color) = (1,1,1,1)
        _FillPhase ("FillPhase", Range(0, 1)) = 0

        _StencilComp ("Stencil Comparison", Float) = 6
         _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
    }

    SubShader {
        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
        LOD 100
        Blend One OneMinusSrcAlpha
        Cull Off Lighting Off ZWrite Off

        Stencil
            {
                Ref [_Stencil]
                Comp [_StencilComp]
                Pass [_StencilOp]
                ReadMask [_StencilReadMask]
                WriteMask [_StencilWriteMask]
            }     
        ColorMask [_ColorMask]
       
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
          
            #include "UnityCG.cginc"
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            struct v2f
            {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
            sampler2D _MainTex;
            float4 _FillColor;
            float _FillPhase;
          
            v2f vert (appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
          
            fixed4 frag (v2f i) : SV_Target
            {
                // apply sihouette fill color
                float4 rawColor = tex2D(_MainTex,i.uv);
                float3 finalColor = lerp(rawColor.rgb, (_FillColor.rgb * rawColor.a), _FillPhase);
                return fixed4(finalColor, rawColor.a);
            }
            ENDCG
        }
    }
    FallBack "UI/Default"
}

Well I figured it out, it looks like some things changed in the UI shaders around 2019.3.12, here is the working version:

Shader "UI/Silhouetted"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        _FillColor ("FillColor", Color) = (1,1,1,1)
        _FillPhase ("FillPhase", Range(0, 1)) = 0

        _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
    }

    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 One 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_local _ UNITY_UI_CLIP_RECT
            #pragma multi_compile_local _ 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;
                float2 texcoord  : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            float4 _ClipRect;
            float4 _MainTex_ST;
            float4 _FillColor;
            float _FillPhase;

            v2f vert(appdata_t v)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.worldPosition = v.vertex;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);

                OUT.color = v.color * _Color;
                return OUT;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;

                #ifdef UNITY_UI_CLIP_RECT
                color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
                #endif

                #ifdef UNITY_UI_ALPHACLIP
                clip (color.a - 0.001);
                #endif

                // apply sihouette fill color
                float3 finalColor = lerp(color.rgb, (_FillColor.rgb * color.a), _FillPhase);
                return fixed4(finalColor, color.a);
            }
        ENDCG
        }
    }
    FallBack "UI/Default"
}
1 Like