Heatmap Color Ramp on 3D model

I’m trying to generate a smooth continuous heat map color ramp of a 3D model, but I get a discrete looking shader on my model. Below is the code I used to generate the heatmap color ramp shader. In particular, I iterate through the vertices of the mesh and assign them new color values correlating to the y value so the lowest y value is given blue and the highest value is given red and the in between are cyan, green, and yellow. I want to scale the values based on height so there are more colors and the model then looks better/smoother.

I’m thinking my issue in how I am taking the Y values (the order of the vertices are stored in the mesh.vertices array) or I am not allowed to create that many colors.

Below is the model with the shader and vertex coloring method applied followed by the code.


public class TopoHeatMap : MonoBehaviour {
	public Mesh mesh;
	public Material material1;
	public Shader vertexShader; // Material with a Custom/Vertex Colored shader assigned in Inspector
	public Renderer myRenderer;
	public void renderHeatMap()
		myRenderer = GetComponent<Renderer>();
		vertexShader = Shader.Find ("Custom/Vertex Colored");

		if (myRenderer.material.shader != vertexShader) {
			vertexShader = Shader.Find ("Custom/Vertex Colored");
			myRenderer.material.shader = vertexShader;

			Mesh mesh = GetComponent<MeshFilter> ().mesh;
			Vector3[] vertices = mesh.vertices;
			Color[] colors = new Color[vertices.Length]; //stores colors for all vertices in mesh
			float partition = vertices.Length / 4;

			for (int i = 0; i < vertices.Length; i++) { //iterated through mesh vertices
				if (i < vertices.Length / 4) {
					colors  *= new Color (0f, i / (partition), 1f, 1f); //populates color array of vertices with new color*
  •  		} else if (i < vertices.Length / 2) {*

_ colors = new Color (0f, 1f, (i-partition) / (partition), 1f); //populates color array of vertices with new color_
_ } else if (i < 3 * vertices.Length / 4) {
colors = new Color ((i-2*partition)/ (partition), 1f, 0f, 1f); //populates color array of vertices with new color
* } else {_
colors _= new Color (1f, (i-3partition)/ (partition), 0f, 1f); //populates color array of vertices with new color_

* }*
* }*
* mesh.colors = colors;*
* }*
* else {*
* vertexShader = Shader.Find (“Standard”);*
* myRenderer.material.shader = vertexShader;*
* }*

* }*


Here’s what the model looks like without the heat map coloration applied.