- Home /
Vertex colour based on distance from transform center
Hey, I'm trying to set colours for a mesh, based on the distance each vertex is from the center of the transform bounds.
I have a VertexColors Component that I'm using for it, and the result I am getting. It seems to be calculating it wrong. Highest points should be grey, others should be green. Any help is appreciated.
using UnityEngine;
using System.Collections;
public class VertexColours : MonoBehaviour
{
public Color GrassColour = new Color(68f, 174f, 117f);
public Color WaterColour = new Color(25f, 59f, 141f);
public Color DirtColour = new Color(94f, 81f, 8f);
public Color RockColour = new Color(165f, 165f, 165f);
public Color SnowColor = new Color(255f, 255f, 255f);
public void UpdateColors()
{
Mesh mesh = this.GetComponent<MeshFilter>().mesh;
Color[] colors = new Color[mesh.vertices.Length];
float[] distances = new float[mesh.vertices.Length];
for (int i = 0; i < mesh.vertices.Length; i++)
{
distances[i] = Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices[i]));
}
float max = Mathf.Max(distances);
float min = Mathf.Min(distances);
for (int i = 0; i < mesh.vertices.Length; i++)
{
colors[i] = this.VertexColor(distances[i], (max + min) / 2f);
}
mesh.colors = colors;
}
protected Color VertexColor(float distance, float mid)
{
if (distance > mid)
{
return this.RockColour;
}
return this.GrassColour;
}
}
Answer by ncallaway · Jan 12, 2014 at 03:12 AM
I suspect the problem only occurs if the object is not positioned at Vector3.zero.
The distance comparison on line 20 below compares mesh.vertices[i] which is in local space, to renderer.bounds.center which is in global space.
distances[i] = Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices[i]));
If you change that line to the following, it should work.
distances[i] = Vector3.Distance(this.renderer.bounds.center - transform.position, mesh.vertices[i]);
The major change that I've made is that we subtract transform.position from renderer.bounds.center to convert this from world space to local space. That way the distance comparison is between vectors in the same (local) space.
(There are also some other minor differences between the two lines that don't actually make an impact. Mathf.Abs() is unnecessary, as Vector3.Distance() will always be positive (or 0). You also have access to this.renderer directly in a MonoBehavior, and you don't need to go through this.transform in order to get the renderer).
Thanks @ncallaway - For this test, I've positioned the object at Vector3.zero world position. But I've also adjusted the code to compensate for local/world space positioning.. Unfortunately this has no effect. The result is very much the same. New image attached:
That's interesting. I'm running that code in a unity project and it seems to be working with a capsule. (I'm using a lerp rather than a binary setting on the capsule, but that should be the only difference).
Any chance you can share your geometry or project so I can see if I get the same errors?