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.
[20543-screenshot.454.png*_|20543]
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 _= Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices*));*_
}
float max = Mathf.Max(distances);
float min = Mathf.Min(distances);
for (int i = 0; i < mesh.vertices.Length; i++)
{
colors = this.VertexColor(distances*, (max + min) / 2f);*
}
mesh.colors = colors;
}
protected Color VertexColor(float distance, float mid)
{
if (distance > mid)
{
return this.RockColour;
}
return this.GrassColour;
}
}
_*