- Home /
Shader Graph Gradient
I have a shader graph that has a gradient property and it wasn't too long until I figured out that you can't set gradient properties in code. So I looked for a way to take a list of colors from the user and convert it to a gradient texture, and that worked well. But I need to set the texture to the shader graph as well, so I need a way to convert the texture to a gradient so that I can sample it. Thank you!
Answer by Devster2020 · Jun 28, 2021 at 03:32 PM
Hello @Captain-Ravioli ! Why you're doing the double work? If you need to set the colors of the gradient, don't convert them to a texture.. Use them directly setting the gradient's colors in this way:
colorKey = new GradientColorKey[2];
colorKey[0].color = Color.red;
colorKey[0].time = 0.0f;
colorKey[1].color = Color.blue;
colorKey[1].time = 1.0f;
// Populate the alpha keys at relative time 0 and 1 (0 and 100%)
alphaKey = new GradientAlphaKey[2];
alphaKey[0].alpha = 1.0f;
alphaKey[0].time = 0.0f;
alphaKey[1].alpha = 0.0f;
alphaKey[1].time = 1.0f;
gradient.SetKeys(colorKey, alphaKey);
Obviously, use this pattern, but you need a for loop to set all colors in the right time.
Source: Unity Docs
Your answer
Follow this Question
Related Questions
Sync up Gradient Noise Node and Script in shader graph 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Gradient for C#? 2 Answers
Rendering full-screen gradient background without overdraw? 1 Answer