Change color of mesh triangle based on Y position in world space

Hi,

I have a mesh that I am trying to dynamically change the colors of the triangles based on their Y position in world space. I know that I have to apply the same color to the 3 vertices that make up a triangle, so that it is one solid color.

I’ve only just started working directly with meshes and made some progress, but the next stage is causing me problems.

How can I go through the whole mesh and apply to correct color to the 3 vertices so that each triangle is one solid color? The code below seems to get ‘out of sequence’ as triangle vertices have various colors applied:

function MeshDetails(){
    meshFilter = gameObject.GetComponent(MeshFilter);
    
    mesh = meshFilter.mesh;
    vertices = mesh.vertices;
    vertexColors = mesh.colors;
    
    MeshDetailsGathered = true;
}

//-------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------

function ChangeColors()
{
	
	//Iterate through vertices
    for(var i : int = 0; i <= vertices.Length - 3; ++i) //-3 as otherwise I get out of index errors
    {

    	mesh.colors = vertexColors;
    	//If vertices count matches the index (Triangle) number
    	if(i == i){
    		
    		//Calculate Average Position for future color allocation
    		//TransformPoint transforms a point from local coordinates to world coordinates wit respect to the transforms position/rotation/scale.
    		var p1 : Vector3 = transform.TransformPoint(vertices*);*
  •  var p2 : Vector3 = transform.TransformPoint(vertices[i+1]);*
    
  •  var p3 : Vector3 = transform.TransformPoint(vertices[i+2]);*
    
  •  //Calculate Center point*
    
  •  var middlePositon = (p1 + p2 + p3) / 3;*
    
  •  //Add Position to Debug list*
    
  •  averagePositionsOfTriangles.Add(middlePositon);*
    
  •  	//Check positions and apply colors    		*
    
  •  if(middlePositon.y < level1Position){*
    
  •  		//Apply new color and count up each time to fill the vertex range which makes up the triangle*
    

_ vertexColors = level1Color;_
* vertexColors[i+1] = level1Color;*
* vertexColors[i+2] = level1Color;*
* }*

* if(middlePositon.y > level1Position && middlePositon.y < level2Position){*
* //Apply new color and count up each time to fill the vertex range which makes up the triangle*
_ vertexColors = level2Color;
* vertexColors[i+1] = level2Color;
vertexColors[i+2] = level2Color;
} *_

* if(middlePositon.y > level2Position && middlePositon.y < level3Position){*
* //Apply new color and count up each time to fill the vertex range which makes up the triangle*
_ vertexColors = level3Color;
* vertexColors[i+1] = level3Color;
vertexColors[i+2] = level3Color;
} *_

* if(middlePositon.y > level3Position && middlePositon.y < level4Position){*
* //Apply new color and count up each time to fill the vertex range which makes up the triangle*
_ vertexColors = level4Color;
* vertexColors[i+1] = level4Color;
vertexColors[i+2] = level4Color;
}*_

* if(middlePositon.y > level5Position){*
* //Apply new color and count up each time to fill the vertex range which makes up the triangle*
_ vertexColors = level5Color;
* vertexColors[i+1] = level5Color;
vertexColors[i+2] = level5Color;
}*_

* }*
}

* //Update the vertex colors*
mesh.colors = vertexColors;

//Sort array function
averagePositionsOfTriangles.Sort(ByVector2Y);

//Get Y min and max values
maxYpositionUsed = averagePositionsOfTriangles[1].y;
minYPOsitionUsed = averagePositionsOfTriangles[averagePositionsOfTriangles.Count-1].y;
}
By the way I have rebuilt the mesh so that each triangle has unique vertices for the low-poly look
Am I at least on the right track?
Any suggestions are welcome!

Your approach looks like it will work, but you have an errors line 23:

  if(i == i){

That’s always going to evaluate to ‘true’. I recommend you change your ‘for’ loop as follows:

for(var i : int = 0; i <= vertices.Length - 3; i = i + 3)

This will then count by 3s. Remove the if ‘(i == i)’.

There is also a minor hole in your ‘if’ logic in that (uncommon) situation where a value is exactly on a boundary, you won’t set the color. Add some ‘<=’ instead of ‘<’ in the statements. While not a bug, you can added some ‘else’ clauses between the ‘if’ statements to reduce extra code execution. This would also allow you to simplify the ‘if’ logic. If you have more colors than what you have here, you can simplify the code somewhat with an array.