Search Unity

Color gradients in shader graph

Discussion in 'Shaders' started by Nicksaccount, Jun 25, 2018.

  1. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
    Is there any way to use Unity's gradient editor in Shader Graph?

    I've been playing with noise and come up with a decent-enough fire effect, but I really need a 4-color gradient to complete it - one that'll follow the pattern created by my noise.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    There's no way to directly use a gradient in the shader editor, or shaders in general. There are two main solutions for getting a gradient into a shader.

    The easiest one is to use a texture with the ramp in it. There are scripts out there that let you convert a gradient into a texture, like this one.
    https://github.com/Emerix/GradientToTexture

    The other solution is to pass some number of color properties to a shader and lerp between them. I can show you some shader code for this, but you'd have to translate that into a node graph yourself.

    float a_scaled = a * 3;
    float4 gradient_2_3 = lerp(_Color2, _Color3, saturate(a_scaled - 2));
    float4 gradient_1_23 = lerp(_Color1, gradient_2_3, saturate(a_scaled - 1));
    float4 gradient = lerp(_Color0, gradient_1_23, saturate(a_scaled));



    There are also algorithmic solutions for certain colors, like approximating a black body radiation color or visible color / rainbow, but those are a bit more specialized.
     
  3. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
    Thanks for that, bglous. Possibly a silly question, but what's "a" in the above? I guess it's the given pixel's height from the bottom of the shader, but couldn't find any way to get that.
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The "a" is the input into the gradient evaluation. It's a 0.0 to 1.0 value.
     
  5. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
  6. Nicksaccount

    Nicksaccount

    Joined:
    Jan 10, 2013
    Posts:
    24
    The gradient question's also been answered in the thread linked above.
     
  7. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
  8. MMeyers23

    MMeyers23

    Joined:
    Dec 29, 2019
    Posts:
    101

    Do you know which method would be more performant if you have large number of gradients and you want to change the colors in the gradients dynamically at runtime? I would guess it is more computationally heacy to manually interpolate, but it also seems like it might not be fast to quickly rewrite and read textures on the fly?