I am trying to color the vertices of a mesh based on each vertex’s distance from some nodes. Ultimately I want to use this to color the ground to visually message the player about the fertility of the ground.
I made a test with a cube in the middle and 2 nodes that had different colors associated with them.
Mesh _mesh;
Vector3[] _vertexs;
public Color _color1;
public Color _color2;
public Transform pos1;
public Transform pos2;
// Use this for initialization
void ChangeColors(){
Color[] _color = new Color[_vertexs.Length];
for (int i = 0; i < _vertexs.Length; i++){
float _distance1 = Distance (_vertexs*, pos1.position);*
float _distance2 = Distance (_vertexs*, pos2.position);
float _ratio1 = _distance1 / (_distance1+_distance2);
float _ratio2= _distance2 / (_distance1+distance2);
_color = _color1 * _ratio1 + _color2ratio2;
* }*
* mesh.colors = color;*
* }*
* void Start () {*
* _mesh = GetComponent ().mesh;
vertexs = mesh.vertices;*
* ChangeColors ();*
* }*
* float Distance(Vector3 start, Vector3 end){
return Vector3.Distance (start, end);*
* }*
* void ChangeToRed(){*
* for (int i = 0; i < mesh.colors.Length; i++) {
mesh.colors *= Color.red; *
}*
* }*
The interpolation bit works (I used to have it spit out the numbers to the log.) But the colors don’t take. If I go through and print out the colors array it’s all just white.
I have tried finding shaders that use vertex color but this doesn’t seem to be working. (I have no experience with shaders, but I think I may have to learn a bit to solve this problem.)
To reiterate, I’m trying to find a way to assign a color to each vertex of a mesh.
Thanks.