Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Custom UI shader does not update inside UI Mask

Discussion in 'Shaders' started by PelvisParsley, Apr 1, 2018.

  1. PelvisParsley

    PelvisParsley

    Joined:
    Aug 9, 2016
    Posts:
    89
    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.

    Code (CSharp):
    1. Shader "HealthShader"
    2. {
    3.     Properties
    4.     {
    5.         [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.        
    8.         _StencilComp ("Stencil Comparison", Float) = 8
    9.         _Stencil ("Stencil ID", Float) = 0
    10.         _StencilOp ("Stencil Operation", Float) = 0
    11.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    12.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    13.  
    14.         _ColorMask ("Color Mask", Float) = 15
    15.  
    16.         [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    17.         _FirstBar("FirstBar", Range( 0 , 1)) = 0.838034
    18.         _SecondBar("SecondBar", Range( 0 , 1)) = 0.3559519
    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.        
    33.         Stencil
    34.         {
    35.             Ref [_Stencil]
    36.             Comp [_StencilComp]
    37.             Pass [_StencilOp]
    38.             ReadMask [_StencilReadMask]
    39.             WriteMask [_StencilWriteMask]
    40.         }
    41.  
    42.         Cull Off
    43.         Lighting Off
    44.         ZWrite Off
    45.         ZTest [unity_GUIZTestMode]
    46.         Blend SrcAlpha OneMinusSrcAlpha
    47.         ColorMask [_ColorMask]
    48.  
    49.  
    50.         Pass
    51.         {
    52.             Name "Default"
    53.         CGPROGRAM
    54.             #pragma vertex vert
    55.             #pragma fragment frag
    56.             #pragma target 2.0
    57.  
    58.             #include "UnityCG.cginc"
    59.             #include "UnityUI.cginc"
    60.  
    61.             #pragma multi_compile __ UNITY_UI_ALPHACLIP
    62.            
    63.  
    64.            
    65.             struct appdata_t
    66.             {
    67.                 float4 vertex   : POSITION;
    68.                 float4 color    : COLOR;
    69.                 float2 texcoord : TEXCOORD0;
    70.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    71.             };
    72.  
    73.             struct v2f
    74.             {
    75.                 float4 vertex   : SV_POSITION;
    76.                 fixed4 color    : COLOR;
    77.                 half2 texcoord  : TEXCOORD0;
    78.                 float4 worldPosition : TEXCOORD1;
    79.                 UNITY_VERTEX_OUTPUT_STEREO
    80.             };
    81.            
    82.             uniform fixed4 _Color;
    83.             uniform fixed4 _TextureSampleAdd;
    84.             uniform float4 _ClipRect;
    85.             uniform sampler2D _MainTex;
    86.             uniform float _FirstBar;
    87.             uniform float _SecondBar;
    88.            
    89.             v2f vert( appdata_t IN  )
    90.             {
    91.                 v2f OUT;
    92.                 UNITY_SETUP_INSTANCE_ID(v);
    93.                 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
    94.                 OUT.worldPosition = IN.vertex;
    95.                
    96.                 OUT.worldPosition.xyz +=  float3( 0, 0, 0 ) ;
    97.                 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
    98.  
    99.                 OUT.texcoord = IN.texcoord;
    100.                
    101.                 OUT.color = IN.color * _Color;
    102.                 return OUT;
    103.             }
    104.  
    105.             fixed4 frag(v2f IN  ) : SV_Target
    106.             {
    107.                 float4 _Color0 = float4(0.08855966,0.9264706,0.198355,0);
    108.                 float2 uv1 = IN.texcoord.xy * float2( 1,1 ) + float2( 0,0 );
    109.                 float temp_output_9_0 = ( 1.0 - floor( ( uv1.x + _FirstBar ) ) );
    110.                 float4 appendResult30 = (float4(_Color0.r , _Color0.g , _Color0.b , temp_output_9_0));
    111.                 float blendOpSrc20 = temp_output_9_0;
    112.                 float blendOpDest20 = floor( ( uv1.x + _SecondBar ) );
    113.                
    114.                 half4 color = ( appendResult30 + ( 1.0 - ( saturate( ( 0.5 - 2.0 * ( blendOpSrc20 - 0.5 ) * ( blendOpDest20 - 0.5 ) ) )) ) );
    115.                
    116.                 color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
    117.                
    118.                 #ifdef UNITY_UI_ALPHACLIP
    119.                 clip (color.a - 0.001);
    120.                 #endif
    121.  
    122.                 return color;
    123.             }
    124.         ENDCG
    125.         }
    126.     }
    127.     CustomEditor "ASEMaterialInspector"
    128. }
     
  2. andersemil

    andersemil

    Joined:
    Feb 2, 2015
    Posts:
    111
    @PelvisParsley did you find a solution to your problem? I'm having similar problem with another shader.
     
  3. Wolar

    Wolar

    Joined:
    Sep 25, 2014
    Posts:
    38
    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.
     
  4. andersemil

    andersemil

    Joined:
    Feb 2, 2015
    Posts:
    111
    @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)
     
  5. Wolar

    Wolar

    Joined:
    Sep 25, 2014
    Posts:
    38
    JonasMumm and andersemil like this.
  6. JonasMumm

    JonasMumm

    Joined:
    Feb 22, 2019
    Posts:
    23