- Home /
Shader Graph: how to convert gradient node into a texture
Is there a way to sample a gradient node into a texture? I want to use the Unity gradient editor to create a ramp texture for my cel shader somehow, but i can't figure out how to do it properly. Anyone has any idea? Thank you
Ah I figured it out, if you haven't cracked it yet. Are you using a custom lighting set up? If so, you can take the Diffuse output node from that (assu$$anonymous$$g there is one) and using the Colorspace Conversion node, turn it from RGB to HSV. Then, a split node. Plug the R and G channels in to a Combine node, but for the B channel plug in a Sample Gradient node (where you can edit the gradient instead of using a texture). Now use another Colorspace Conversion to chang from HSV back to RGB, multiply this with the custom lighting color. Do the same for specular.
Hope this helps!
Answer by Namey5 · Aug 26, 2020 at 12:02 PM
You could render straight into a texture, but rather than doing that you could also do the same with the regular gradient editor through script;
using System.IO;
...
[SerializeField] private string m_TextureName = "Gradient";
[SerializeField] private Gradient m_Gradient = new Gradient ();
[SerializeField] private int m_Resolution = 256;
...
public void SaveGradient ()
{
Texture2D tex = new Texture2D (m_Resolution, 1);
for (int i = 0; i < m_Resolution; i++)
tex.SetPixel (i, 0, m_Gradient.Evaluate (i / (float)m_Resolution));
tex.Apply ();
byte[] data = tex.EncodeToPNG ();
File.WriteAllBytes (string.Format ("{0}/{1}.png", Application.dataPath, m_TextureName), data);
}
Something along those lines - this should save a texture called [m_TextureName].png in the assets folder.