Search Unity

Changing Global Shader Variables in .cginc

Discussion in 'Shaders' started by Ewanuk, Sep 17, 2017.

  1. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    If I declare a variable in a .cginc file:

    Code (CG):
    1. static float QuadradicFalloff = 1;
    Is it possible to change that value in CPU-side CG code?

    Code (CSharp):
    1. float quadradicFalloff = value;
    2. Shader.SetGlobalFloat("QuadradicFalloff", quadradicFalloff);

    This is what I'm doing now, but setting that value (using the C# code) doesn't appear to change the value.
     
  2. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    3,790
    I've done this plenty of times, though I've never marked the shader variable as Static... that may be where the issue lies. Designating an implicit value in the shader could also possibly be a no-no. Try just using
    Code (CSharp):
    1. float QuadradicFalloff;
    instead.

    Also, make sure the variable is NOT listed in the shader properties box since that will overrride any global settings you do in your code.
     
  3. Ewanuk

    Ewanuk

    Joined:
    Jul 9, 2011
    Posts:
    257
    That did the trick, thanks!

    For future reference to other readers:

    I removed the static, renamed the variables something more specific so I wouldn't get conflicting variable names, and removed the initial setters, not sure if some or all were responsible.