Mesh Gradient C#

Good Afternoon, I am trying to make a gradient on my mesh, But i am having problems finding a way to use Gradient.Evaluate and list all the time scales.

   using UnityEngine; 
   using UnityEditor; 
   using System.Collections;

  public class Generate : MonoBehaviour
  {
  
   public Mesh s;
   public Gradient g;
  
   private Vector3[] vertices = {
       new Vector2(0,0),
       new Vector2(0,1),
       new Vector2(1,1),
       new Vector2(1,0)
   };
  
   private int[] triangles = {
       0,1,2,
       2,3,0
   };  

   void Start()
   {
       Create();
   } 
  
   public void Create()
   {
   // Generated Mesh Code //
   }

   void Update() {
        Vector3[] vertices = mesh.vertices;
        Color[] colors = new Color[vertices.Length];

    GradientColorKey[] GradientColorKey = Gradient.colorKeys;
				
    GradientAlphaKey[] GradientAlphaKey = Gradient.alphaKeys;

    g.SetKeys(GradientColorKey, GradientAlphaKey);

        int i = 0;
        while (i < vertices.Length) {
        colors *= Gradient.Evaluate(Random.Range(0f, 1f)); // Makes Random Gradients Not Linear//*

i++;
}
s.colors = colors;
}
}

It seems you’re mixing two completely seperate things here:

  • The Gradient class in Unity allows you to specify a gradient using two or more colors. This gradient can be evaluated via script at certain points. It is usually used for animations. It’s nothing more than an advanced version of Color.Lerp to support more than two colors and have the alpha value seperated so it can be animated seperately.
  • Vertex colors can be used to define the color of a triangle / mesh. Here the “color blending” is done in the shader on the GPU. Each vertex can only have a single color. The shader automatically renders your triangle as a gradient if two vertices have a differenc color.

You can’t “apply” the whole gradient to a mesh unless you use a texture. Your gradient is a 1d gradient which could be sampled into a texture of size (1, x) where x can be any integer you like.

Just to be clear: If you want to use vertex colors on a quad mesh (two triangles) you can only get a gradient between two colors from one side to the other as you can’t specify values in between without using more vertices in between.

When you use a texture the texture will be mapped onto the quad and can contain as many colors as you like.

To sample a gradient into a texture you can simply use an extension method like this:

public static class GradientToTexture
{
    public static Texture2D ToTexture(this Gradient aGradient,int aWidth)
    {
        if (aWidth < 1)
            throw new System.ArgumentException("aWidth has to be 1 or greater");
        var colors = new Color[aWidth];
        float denominator = Mathf.Max(1, aWidth - 1);
        for(int i = 0; i < aWidth; i++)
        {
            colors *= aGradient.Evaluate((float)i / denominator);*

}
var tex = new Texture2D(aWidth, 1);
tex.SetPixels(colors);
tex.Apply();
return tex;
}
}
So if you have a gradient you can simply turn it into a texture like this:
Gradient g;
Texture2D tex = g.ToTexture(128);
This will generate a texture that is 1 pixel high and 128 pixel wide. You shouldn’t use too few pixels. For example g.ToTexture(1) is a 1x1 texture so it contains only the first color. Also g.ToTexture(2) (a 2x1 texture) only contains 2 colors so any color in between can’t be represented.
To apply a texture to your quad you have to define uv coordinates for each vertex you could simply use your vertices since they are already between 0 and 1. Of course the material you use to render the mesh would need the texture we’ve just generated.
You should be more specific when asking a question. An image of your desired result would help alot. Even when it’s just drawn in MSPaint. Almost nobody understood what you actually want to do. Even i’m not sure if i get it right what you want.