Change vertex color of a mesh.

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.

i think there is a code by c petersen to rewrite the UVS to all the same for each vertex, and make them a function of the distance from point. otherwise, is just sent point informations to the shader and say color values +/- distance from worldspace, say a loop of 10 points that are in view in the map, hopefully less of them.

I’m not sure if this is necessary in C# (I work in JS), but getting the vertex world position, from your code would be:

float _distance1 = Distance (transform.TransformPoint(_vertexs*), pos1.position);*

Also for the vertex shader, can’t recall where I got this from but this is the simplest one I’ve found:
Shader “Vertex Color Lit” {
Properties {
_MainTex (“Base (RGB)”, 2D) = “white” {}
}
SubShader {
Pass {
Lighting On
ColorMaterial AmbientAndDiffuse
SetTexture [_MainTex] {
combine texture * primary DOUBLE
}
}
}
}