Search Unity

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

Discussion in 'General Graphics' started by DustinREDGames, Jun 6, 2020.

  1. DustinREDGames

    DustinREDGames

    Joined:
    Dec 4, 2018
    Posts:
    2
    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?

    Code (CSharp):
    1. Shader "UI/Silhouetted" {
    2.     Properties {
    3.         _MainTex ("Texture", 2D) = "white" {}
    4.         _FillColor ("FillColor", Color) = (1,1,1,1)
    5.         _FillPhase ("FillPhase", Range(0, 1)) = 0
    6.  
    7.         _StencilComp ("Stencil Comparison", Float) = 6
    8.          _Stencil ("Stencil ID", Float) = 0
    9.          _StencilOp ("Stencil Operation", Float) = 0
    10.          _StencilWriteMask ("Stencil Write Mask", Float) = 255
    11.          _StencilReadMask ("Stencil Read Mask", Float) = 255
    12.          _ColorMask ("Color Mask", Float) = 15
    13.     }
    14.  
    15.     SubShader {
    16.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }
    17.         LOD 100
    18.         Blend One OneMinusSrcAlpha
    19.         Cull Off Lighting Off ZWrite Off
    20.  
    21.         Stencil
    22.             {
    23.                 Ref [_Stencil]
    24.                 Comp [_StencilComp]
    25.                 Pass [_StencilOp]
    26.                 ReadMask [_StencilReadMask]
    27.                 WriteMask [_StencilWriteMask]
    28.             }    
    29.         ColorMask [_ColorMask]
    30.        
    31.         Pass {
    32.             CGPROGRAM
    33.             #pragma vertex vert
    34.             #pragma fragment frag
    35.          
    36.             #include "UnityCG.cginc"
    37.             struct appdata
    38.             {
    39.                 float4 vertex : POSITION;
    40.                 float2 uv : TEXCOORD0;
    41.             };
    42.             struct v2f
    43.             {
    44.                 float4 pos : SV_POSITION;
    45.                 float2 uv : TEXCOORD0;
    46.             };
    47.             sampler2D _MainTex;
    48.             float4 _FillColor;
    49.             float _FillPhase;
    50.          
    51.             v2f vert (appdata v)
    52.             {
    53.                 v2f o;
    54.                 o.pos = UnityObjectToClipPos(v.vertex);
    55.                 o.uv = v.uv;
    56.                 return o;
    57.             }
    58.          
    59.             fixed4 frag (v2f i) : SV_Target
    60.             {
    61.                 // apply sihouette fill color
    62.                 float4 rawColor = tex2D(_MainTex,i.uv);
    63.                 float3 finalColor = lerp(rawColor.rgb, (_FillColor.rgb * rawColor.a), _FillPhase);
    64.                 return fixed4(finalColor, rawColor.a);
    65.             }
    66.             ENDCG
    67.         }
    68.     }
    69.     FallBack "UI/Default"
    70. }
     
  2. DustinREDGames

    DustinREDGames

    Joined:
    Dec 4, 2018
    Posts:
    2
    Well I figured it out, it looks like some things changed in the UI shaders around 2019.3.12, here is the working version:

    Code (CSharp):
    1. Shader "UI/Silhouetted"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _FillColor ("FillColor", Color) = (1,1,1,1)
    8.         _FillPhase ("FillPhase", Range(0, 1)) = 0
    9.  
    10.         _StencilComp ("Stencil Comparison", Float) = 8
    11.         _Stencil ("Stencil ID", Float) = 0
    12.         _StencilOp ("Stencil Operation", Float) = 0
    13.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    14.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    15.  
    16.         _ColorMask ("Color Mask", Float) = 15
    17.  
    18.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    19.     }
    20.  
    21.     SubShader
    22.     {
    23.         Tags
    24.         {
    25.             "Queue"="Transparent"
    26.             "IgnoreProjector"="True"
    27.             "RenderType"="Transparent"
    28.             "PreviewType"="Plane"
    29.             "CanUseSpriteAtlas"="True"
    30.         }
    31.  
    32.         Stencil
    33.         {
    34.             Ref [_Stencil]
    35.             Comp [_StencilComp]
    36.             Pass [_StencilOp]
    37.             ReadMask [_StencilReadMask]
    38.             WriteMask [_StencilWriteMask]
    39.         }
    40.  
    41.         Cull Off
    42.         Lighting Off
    43.         ZWrite Off
    44.         ZTest [unity_GUIZTestMode]
    45.         Blend One OneMinusSrcAlpha
    46.         ColorMask [_ColorMask]
    47.  
    48.         Pass
    49.         {
    50.             Name "Default"
    51.         CGPROGRAM
    52.             #pragma vertex vert
    53.             #pragma fragment frag
    54.             #pragma target 2.0
    55.  
    56.             #include "UnityCG.cginc"
    57.             #include "UnityUI.cginc"
    58.  
    59.             #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
    60.             #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
    61.  
    62.             struct appdata_t
    63.             {
    64.                 float4 vertex   : POSITION;
    65.                 float4 color    : COLOR;
    66.                 float2 texcoord : TEXCOORD0;
    67.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    68.             };
    69.  
    70.             struct v2f
    71.             {
    72.                 float4 vertex   : SV_POSITION;
    73.                 fixed4 color    : COLOR;
    74.                 float2 texcoord  : TEXCOORD0;
    75.                 float4 worldPosition : TEXCOORD1;
    76.                 UNITY_VERTEX_OUTPUT_STEREO
    77.             };
    78.  
    79.             sampler2D _MainTex;
    80.             fixed4 _Color;
    81.             fixed4 _TextureSampleAdd;
    82.             float4 _ClipRect;
    83.             float4 _MainTex_ST;
    84.             float4 _FillColor;
    85.             float _FillPhase;
    86.  
    87.             v2f vert(appdata_t v)
    88.             {
    89.                 v2f OUT;
    90.                 UNITY_SETUP_INSTANCE_ID(v);
    91.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    92.                 OUT.worldPosition = v.vertex;
    93.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
    94.  
    95.                 OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    96.  
    97.                 OUT.color = v.color * _Color;
    98.                 return OUT;
    99.             }
    100.  
    101.             fixed4 frag(v2f IN) : SV_Target
    102.             {
    103.                 half4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;
    104.  
    105.                 #ifdef UNITY_UI_CLIP_RECT
    106.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    107.                 #endif
    108.  
    109.                 #ifdef UNITY_UI_ALPHACLIP
    110.                 clip (color.a - 0.001);
    111.                 #endif
    112.  
    113.                 // apply sihouette fill color
    114.                 float3 finalColor = lerp(color.rgb, (_FillColor.rgb * color.a), _FillPhase);
    115.                 return fixed4(finalColor, color.a);
    116.             }
    117.         ENDCG
    118.         }
    119.     }
    120.     FallBack "UI/Default"
    121. }
     
    MoonLabsStudios likes this.