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

Resolved Shader-Properties not animating when using stencil mask

Discussion in 'Shaders' started by Noblauch, Aug 11, 2020.

  1. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    270
    Hi, I recently made a shader with shader graph and stumbled across the problem, that it can not be used as a UI component, since the Unity Mask isn't working anymore.

    I thought I fixed the problem by adding Stencil properties and stuff to the raw shader code as you can see in my post linked above. However, I just found out, that the shader properties won't animate anymore when masked, and I'm completely lost how this can be?

    Take a look:

    As you can see the shader seems to be frozen in time, when I turn on the Mask component in the parent object.

    Here is the shader code:
    Code (CSharp):
    1. Shader "2DGlow"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset]_MainTex("MainTex", 2D) = "white" {}
    6.         _GlowIntensity("Glow Intensity", Range(0, 1)) = 0
    7.         [NonModifiableTextureData][NoScaleOffset]_SampleTexture2D_D69F3F9D_Texture_1("Texture2D", 2D) = "white" {}
    8.  
    9.         // Added to make it work with Unity Masks:
    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.         _ColorMask ("Color Mask", Float) = 15
    16.         // end
    17.     }
    18.     SubShader
    19.     {
    20.         Tags
    21.         {
    22.             "RenderPipeline"="UniversalPipeline"
    23.             "RenderType"="Transparent"
    24.             "Queue"="Transparent+0"
    25.         }
    26.    
    27.         // Added to make it work with Unity Masks:
    28.         Stencil
    29.         {
    30.             Ref [_Stencil]
    31.             Comp [_StencilComp]
    32.             Pass [_StencilOp]
    33.             ReadMask [_StencilReadMask]
    34.             WriteMask [_StencilWriteMask]
    35.         }
    36.    
    37.         ColorMask [_ColorMask]
    38.         // end
    39.    
    40.         Pass
    41.         {
    42.             // Render State
    43.             Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
    44.             Cull Off
    45.             ZTest LEqual
    46.             ZWrite Off
    47.             // ColorMask: <None>
    48.    
    49.             HLSLPROGRAM
    50.             #pragma vertex vert
    51.             #pragma fragment frag
    52.    
    53.             // --------------------------------------------------
    54.             // Pass
    55.    
    56.             // Pragmas
    57.             #pragma prefer_hlslcc gles
    58.             #pragma exclude_renderers d3d11_9x
    59.             #pragma target 2.0
    60.    
    61.             // Keywords
    62.             #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
    63.             // GraphKeywords: <None>
    64.        
    65.             // Defines
    66.             #define _SURFACE_TYPE_TRANSPARENT 1
    67.             #define ATTRIBUTES_NEED_NORMAL
    68.             #define ATTRIBUTES_NEED_TANGENT
    69.             #define ATTRIBUTES_NEED_TEXCOORD0
    70.             #define ATTRIBUTES_NEED_COLOR
    71.             #define VARYINGS_NEED_TEXCOORD0
    72.             #define VARYINGS_NEED_COLOR
    73.             #define SHADERPASS_SPRITEUNLIT
    74.    
    75.             // Includes
    76.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
    77.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    78.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    79.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderGraphFunctions.hlsl"
    80.    
    81.             // --------------------------------------------------
    82.             // Graph
    83.    
    84.             // Graph Properties
    85.             CBUFFER_START(UnityPerMaterial)
    86.             float _GlowIntensity;
    87.             CBUFFER_END
    88.             TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex); float4 _MainTex_TexelSize;
    89.             SAMPLER(_SampleTexture2D_E430CDDC_Sampler_3_Linear_Repeat);
    90.             TEXTURE2D(_SampleTexture2D_D69F3F9D_Texture_1); SAMPLER(sampler_SampleTexture2D_D69F3F9D_Texture_1); float4 _SampleTexture2D_D69F3F9D_Texture_1_TexelSize;
    91.             SAMPLER(_SampleTexture2D_D69F3F9D_Sampler_3_Linear_Repeat);
    92.    
    93.             // Graph Functions
    94.        
    95.             void Unity_Remap_float(float In, float2 InMinMax, float2 OutMinMax, out float Out)
    96.             {
    97.                 Out = OutMinMax.x + (In - InMinMax.x) * (OutMinMax.y - OutMinMax.x) / (InMinMax.y - InMinMax.x);
    98.             }
    99.        
    100.             void Unity_Multiply_float(float A, float B, out float Out)
    101.             {
    102.                 Out = A * B;
    103.             }
    104.        
    105.             void Unity_Combine_float(float R, float G, float B, float A, out float4 RGBA, out float3 RGB, out float2 RG)
    106.             {
    107.                 RGBA = float4(R, G, B, A);
    108.                 RGB = float3(R, G, B);
    109.                 RG = float2(R, G);
    110.             }
    111.        
    112.             void Unity_Add_float4(float4 A, float4 B, out float4 Out)
    113.             {
    114.                 Out = A + B;
    115.             }
    116.    
    117.             // Graph Vertex
    118.             // GraphVertex: <None>
    119.        
    120.             // Graph Pixel
    121.             struct SurfaceDescriptionInputs
    122.             {
    123.                 float4 uv0;
    124.             };
    125.        
    126.             struct SurfaceDescription
    127.             {
    128.                 float4 Color;
    129.             };
    130.        
    131.             SurfaceDescription SurfaceDescriptionFunction(SurfaceDescriptionInputs IN)
    132.             {
    133.                 SurfaceDescription surface = (SurfaceDescription)0;
    134.                 float4 _SampleTexture2D_E430CDDC_RGBA_0 = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv0.xy);
    135.                 float _SampleTexture2D_E430CDDC_R_4 = _SampleTexture2D_E430CDDC_RGBA_0.r;
    136.                 float _SampleTexture2D_E430CDDC_G_5 = _SampleTexture2D_E430CDDC_RGBA_0.g;
    137.                 float _SampleTexture2D_E430CDDC_B_6 = _SampleTexture2D_E430CDDC_RGBA_0.b;
    138.                 float _SampleTexture2D_E430CDDC_A_7 = _SampleTexture2D_E430CDDC_RGBA_0.a;
    139.                 float _Property_1A4E7584_Out_0 = _GlowIntensity;
    140.                 float _Remap_C6250819_Out_3;
    141.                 Unity_Remap_float(_Property_1A4E7584_Out_0, float2 (0, 1), float2 (1, 2), _Remap_C6250819_Out_3);
    142.                 float _Multiply_E38790AD_Out_2;
    143.                 Unity_Multiply_float(_SampleTexture2D_E430CDDC_R_4, _Remap_C6250819_Out_3, _Multiply_E38790AD_Out_2);
    144.                 float _Multiply_C56E1A7_Out_2;
    145.                 Unity_Multiply_float(_SampleTexture2D_E430CDDC_G_5, _Remap_C6250819_Out_3, _Multiply_C56E1A7_Out_2);
    146.                 float _Multiply_EBC221C4_Out_2;
    147.                 Unity_Multiply_float(_SampleTexture2D_E430CDDC_B_6, _Remap_C6250819_Out_3, _Multiply_EBC221C4_Out_2);
    148.                 float4 _Combine_38C0EFDE_RGBA_4;
    149.                 float3 _Combine_38C0EFDE_RGB_5;
    150.                 float2 _Combine_38C0EFDE_RG_6;
    151.                 Unity_Combine_float(_Multiply_E38790AD_Out_2, _Multiply_C56E1A7_Out_2, _Multiply_EBC221C4_Out_2, _SampleTexture2D_E430CDDC_A_7, _Combine_38C0EFDE_RGBA_4, _Combine_38C0EFDE_RGB_5, _Combine_38C0EFDE_RG_6);
    152.                 float4 _SampleTexture2D_D69F3F9D_RGBA_0 = SAMPLE_TEXTURE2D(_SampleTexture2D_D69F3F9D_Texture_1, sampler_SampleTexture2D_D69F3F9D_Texture_1, IN.uv0.xy);
    153.                 float _SampleTexture2D_D69F3F9D_R_4 = _SampleTexture2D_D69F3F9D_RGBA_0.r;
    154.                 float _SampleTexture2D_D69F3F9D_G_5 = _SampleTexture2D_D69F3F9D_RGBA_0.g;
    155.                 float _SampleTexture2D_D69F3F9D_B_6 = _SampleTexture2D_D69F3F9D_RGBA_0.b;
    156.                 float _SampleTexture2D_D69F3F9D_A_7 = _SampleTexture2D_D69F3F9D_RGBA_0.a;
    157.                 float _Multiply_DE6F6CD6_Out_2;
    158.                 Unity_Multiply_float(_SampleTexture2D_D69F3F9D_R_4, _Property_1A4E7584_Out_0, _Multiply_DE6F6CD6_Out_2);
    159.                 float _Multiply_C07DAB61_Out_2;
    160.                 Unity_Multiply_float(_SampleTexture2D_D69F3F9D_G_5, _Property_1A4E7584_Out_0, _Multiply_C07DAB61_Out_2);
    161.                 float _Multiply_83A1ADD1_Out_2;
    162.                 Unity_Multiply_float(_SampleTexture2D_D69F3F9D_B_6, _Property_1A4E7584_Out_0, _Multiply_83A1ADD1_Out_2);
    163.                 float _Multiply_DE613504_Out_2;
    164.                 Unity_Multiply_float(_SampleTexture2D_E430CDDC_A_7, _Property_1A4E7584_Out_0, _Multiply_DE613504_Out_2);
    165.                 float4 _Combine_83894105_RGBA_4;
    166.                 float3 _Combine_83894105_RGB_5;
    167.                 float2 _Combine_83894105_RG_6;
    168.                 Unity_Combine_float(_Multiply_DE6F6CD6_Out_2, _Multiply_C07DAB61_Out_2, _Multiply_83A1ADD1_Out_2, _Multiply_DE613504_Out_2, _Combine_83894105_RGBA_4, _Combine_83894105_RGB_5, _Combine_83894105_RG_6);
    169.                 float4 _Add_EAB34B46_Out_2;
    170.                 Unity_Add_float4(_Combine_38C0EFDE_RGBA_4, _Combine_83894105_RGBA_4, _Add_EAB34B46_Out_2);
    171.                 surface.Color = _Add_EAB34B46_Out_2;
    172.                 return surface;
    173.             }
    174.    
    175.             // --------------------------------------------------
    176.             // Structs and Packing
    177.    
    178.             // Generated Type: Attributes
    179.             struct Attributes
    180.             {
    181.                 float3 positionOS : POSITION;
    182.                 float3 normalOS : NORMAL;
    183.                 float4 tangentOS : TANGENT;
    184.                 float4 uv0 : TEXCOORD0;
    185.                 float4 color : COLOR;
    186.                 #if UNITY_ANY_INSTANCING_ENABLED
    187.                 uint instanceID : INSTANCEID_SEMANTIC;
    188.                 #endif
    189.             };
    190.    
    191.             // Generated Type: Varyings
    192.             struct Varyings
    193.             {
    194.                 float4 positionCS : SV_POSITION;
    195.                 float4 texCoord0;
    196.                 float4 color;
    197.                 #if UNITY_ANY_INSTANCING_ENABLED
    198.                 uint instanceID : CUSTOM_INSTANCE_ID;
    199.                 #endif
    200.                 #if (defined(UNITY_STEREO_INSTANCING_ENABLED))
    201.                 uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex;
    202.                 #endif
    203.                 #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
    204.                 uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
    205.                 #endif
    206.                 #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE)
    207.                 FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC;
    208.                 #endif
    209.             };
    210.        
    211.             // Generated Type: PackedVaryings
    212.             struct PackedVaryings
    213.             {
    214.                 float4 positionCS : SV_POSITION;
    215.                 #if UNITY_ANY_INSTANCING_ENABLED
    216.                 uint instanceID : CUSTOM_INSTANCE_ID;
    217.                 #endif
    218.                 float4 interp00 : TEXCOORD0;
    219.                 float4 interp01 : TEXCOORD1;
    220.                 #if (defined(UNITY_STEREO_INSTANCING_ENABLED))
    221.                 uint stereoTargetEyeIndexAsRTArrayIdx : SV_RenderTargetArrayIndex;
    222.                 #endif
    223.                 #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
    224.                 uint stereoTargetEyeIndexAsBlendIdx0 : BLENDINDICES0;
    225.                 #endif
    226.                 #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE)
    227.                 FRONT_FACE_TYPE cullFace : FRONT_FACE_SEMANTIC;
    228.                 #endif
    229.             };
    230.        
    231.             // Packed Type: Varyings
    232.             PackedVaryings PackVaryings(Varyings input)
    233.             {
    234.                 PackedVaryings output = (PackedVaryings)0;
    235.                 output.positionCS = input.positionCS;
    236.                 output.interp00.xyzw = input.texCoord0;
    237.                 output.interp01.xyzw = input.color;
    238.                 #if UNITY_ANY_INSTANCING_ENABLED
    239.                 output.instanceID = input.instanceID;
    240.                 #endif
    241.                 #if (defined(UNITY_STEREO_INSTANCING_ENABLED))
    242.                 output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx;
    243.                 #endif
    244.                 #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
    245.                 output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
    246.                 #endif
    247.                 #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE)
    248.                 output.cullFace = input.cullFace;
    249.                 #endif
    250.                 return output;
    251.             }
    252.        
    253.             // Unpacked Type: Varyings
    254.             Varyings UnpackVaryings(PackedVaryings input)
    255.             {
    256.                 Varyings output = (Varyings)0;
    257.                 output.positionCS = input.positionCS;
    258.                 output.texCoord0 = input.interp00.xyzw;
    259.                 output.color = input.interp01.xyzw;
    260.                 #if UNITY_ANY_INSTANCING_ENABLED
    261.                 output.instanceID = input.instanceID;
    262.                 #endif
    263.                 #if (defined(UNITY_STEREO_INSTANCING_ENABLED))
    264.                 output.stereoTargetEyeIndexAsRTArrayIdx = input.stereoTargetEyeIndexAsRTArrayIdx;
    265.                 #endif
    266.                 #if (defined(UNITY_STEREO_MULTIVIEW_ENABLED)) || (defined(UNITY_STEREO_INSTANCING_ENABLED) && (defined(SHADER_API_GLES3) || defined(SHADER_API_GLCORE)))
    267.                 output.stereoTargetEyeIndexAsBlendIdx0 = input.stereoTargetEyeIndexAsBlendIdx0;
    268.                 #endif
    269.                 #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE)
    270.                 output.cullFace = input.cullFace;
    271.                 #endif
    272.                 return output;
    273.             }
    274.    
    275.             // --------------------------------------------------
    276.             // Build Graph Inputs
    277.    
    278.             SurfaceDescriptionInputs BuildSurfaceDescriptionInputs(Varyings input)
    279.             {
    280.                 SurfaceDescriptionInputs output;
    281.                 ZERO_INITIALIZE(SurfaceDescriptionInputs, output);
    282.        
    283.        
    284.        
    285.        
    286.        
    287.                 output.uv0 =                         input.texCoord0;
    288.             #if defined(SHADER_STAGE_FRAGMENT) && defined(VARYINGS_NEED_CULLFACE)
    289.             #define BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN output.FaceSign =                    IS_FRONT_VFACE(input.cullFace, true, false);
    290.             #else
    291.             #define BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN
    292.             #endif
    293.             #undef BUILD_SURFACE_DESCRIPTION_INPUTS_OUTPUT_FACESIGN
    294.        
    295.                 return output;
    296.             }
    297.        
    298.    
    299.             // --------------------------------------------------
    300.             // Main
    301.    
    302.             #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl"
    303.             #include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/SpriteUnlitPass.hlsl"
    304.    
    305.             ENDHLSL
    306.         }
    307.    
    308.     }
    309.     FallBack "Hidden/Shader Graph/FallbackError"
    310. }
    311.  
    And this is how I animate it:
    Code (CSharp):
    1.         public static void Glow(this Image img, float duration = 2f, float intensity = .5f, Ease ease = Ease.OutQuad,
    2.             int loops = 2, LoopType loopType = LoopType.Yoyo)
    3.         {
    4.             Material originalMaterial = img.material;
    5.             img.material = GlowMaterial;
    6.        
    7.             int glowIntensityProperty = Shader.PropertyToID("_GlowIntensity");
    8.  
    9.             duration /= 2;
    10.             float tmp = 0;
    11.                 DOTween.To(() => tmp, x => tmp = x, intensity, duration)
    12.                 .SetEase(ease)
    13.                 .SetLoops(loops, loopType)
    14.                 .OnUpdate(() => { img.material.SetFloat(glowIntensityProperty, tmp); })
    15.                 .OnComplete(() =>
    16.                 {
    17.                     img.material = originalMaterial;
    18.                 });
    19.         }
    I guess I need a real pro here, any yep – I know, shader graph is creating a lot of unnecessary things, but I'm not enough into shader programming to remove stuff and really know what I'm doing :(

    I hope there is a soul out there that can help! :s
     
    Last edited: Aug 11, 2020
    jonny4th, azevedco and manjeetku8 like this.
  2. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    270
    First findings:
    if I observe the img.material for the animated float by calling
    img.materialForRendering.GetFloat("_GlowIntensity")
    , I can see that the float is properly animating, even though the rendered object is not (as seen in the video).

    However if I do the same with img.materialForRendering I can see the exact behaviour as on screen. The value is frozen whenever I activate the Mask component!
    But how can this be? I don't understand :(
     
    azevedco and manjeetku8 like this.
  3. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    270
    Solve:
    I ended up setting the float on the materialForRendering even though the docs states that it's readonly..

    So my shader animation script now does this:
    img.materialForRendering.SetFloat(glowIntensityProperty, tmp);
     
    azevedco and Bela_ITK like this.
  4. SpaceKryptonite

    SpaceKryptonite

    Joined:
    May 4, 2015
    Posts:
    4
    THANK YOU! I needed this so badly so now I can officially animate shaders while having it crop during scrollview!
     
  5. manjeetku8

    manjeetku8

    Joined:
    May 22, 2016
    Posts:
    4
    Thank you so much.
     
  6. azevedco

    azevedco

    Joined:
    Mar 2, 2014
    Posts:
    34
    Cheers! Worked for me. Gave my shader an Alpha Threshold, doesn't really update outside of playmode, so had to do what NoBlauch mentioned in their last message there to see the Alpha Clipping update in play mode.
    Saying that as a heads up to others to not be immediately discouraged if it's not working right away, test in play mode!