Search Unity

How to update shader parameters in a shader with stencil (UI.Mask) ?

Discussion in 'Shaders' started by dpolyakov, May 16, 2016.

  1. dpolyakov

    dpolyakov

    Joined:
    Dec 18, 2015
    Posts:
    16
    I'm trying in a run-time update parameters for a shader in a material for an element that is a child element of a UI.Mask.

    Problem is that if I include this code in my shader:
    Code (CSharp):
    1.  
    2.         _StencilComp ("Stencil Comparison", Float) = 8
    3.         _Stencil ("Stencil ID", Float) = 0
    4.         _StencilOp ("Stencil Operation", Float) = 0
    5.         _StencilWriteMask ("Stencil Write Mask", Float) = 255
    6.         _StencilReadMask ("Stencil Read Mask", Float) = 255
    7.         _ColorMask ("Color Mask", Float) = 15
    8.  
    and in SubShader section
    Code (CSharp):
    1.  
    2.         Stencil
    3.         {
    4.             Ref [_Stencil]
    5.             Comp [_StencilComp]
    6.             Pass [_StencilOp]
    7.             ReadMask [_StencilReadMask]
    8.             WriteMask [_StencilWriteMask]
    9.         }
    10.         ColorMask [_ColorMask]
    11.  
    My code in C# script (inside Update()):
    Code (CSharp):
    1. m_mat.SetColor("_Color", m_color);
    is executed only once. All subsequent calls to .SetColor do not update the value.

    If I remove stencil code, everything works and color changing every frame, but then mask does not have any effect and a whole image is revealed. It is very similar to http://forum.unity3d.com/threads/masked-ui-elements-shader-not-updating.371542/. But I cannot change parameter globally as I have multiple instances of the same material and I need different parameters.

    Any ideas how to fix it? I looked and googled everywhere, but could not find any working solution...
     
  2. cschmidtHT

    cschmidtHT

    Joined:
    Aug 31, 2016
    Posts:
    3
    Not sure if you ever figured out the issue, but I just spent some time on the same thing and am updating the similar threads I read without an answer.

    The Mask object causes a modified material to be returned so it is no longer rendering the base material texture but a "on-the-fly" material. Assuming you are using an Image using setColor on the materialForRendering property of Image will set the correct material if a mask is involved.
     
    dieow, Zelek, Robdon and 1 other person like this.
  3. Check_2015

    Check_2015

    Joined:
    Oct 4, 2015
    Posts:
    2
    Thanks :D