- Home /
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[i]);
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[i] = 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[i] = 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[i] = 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[i] = 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[i] = 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!
Answer by robertbu · Aug 26, 2013 at 08:02 PM
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 '
FYI: Vertex colors only work with a shader that supports vertex colors.
Great points robertbu! The counting by 3 in a for loop kept tripping me up. Very useful! The code now works, although in the editor movement is quite jerky now compared to the original script I posted, which is odd. Any ideas?
No specific ideas. Is the jerkiness due to a frame rate drop or something else?
Framerate is good +150 fps. What is weird is if I go into the scene view after allocating the colors, do a quick camera movement, and go back into the game window, the movement is fine. It also compiles and runs fine as an exe.
Also if I remove the script after the color change, the movement is normal. I think the script is still in some kind of loop, Will start debugging and mark the question as answered.
Thanks!
Found the problem. The array sort at the end is having to do a lot more work, will find another way to get the numbers I need.
Your answer
Follow this Question
Related Questions
Split single triangle mesh into 2 Equal Parts 0 Answers
how to Find the vertices of each edge on mesh 4 Answers
Ways of modificating mesh triangles 3 Answers
How can I ADD vertices and faces to a mesh? 1 Answer
Checking If A Triangle Is In A Mesh 1 Answer