- Home /
Question by
Woj_Gabel_FertileSky · May 25, 2012 at 04:34 PM ·
colorlerpverticesvalues
Lerp color of verts with position bigger than 1
Hi On this page (link) there is a script to lerp color of verts in a object. How can i change the t value of lerp to be something other than a value between 0 and 1?
For example, if position.y of vert0 = 0.0 and vert3 = 1.0 than all verts between them are going to be changed gradient-like. But if my verts are going to be between -30 and -126 than all of them will be lerped to the second color! How can i clamp all position.y of verts between those high points to be between 0 and 1 and still be accurately divided to preserve smooth gradient?
code:
function Start () {
objects = new GameObject.FindGameObjectsWithTag("gSides");
bottomColor.a = 0.0;
for (var x:int = 0 ; x<objects.Length ; x++) {
var mesh : Mesh = objects[x].GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = mesh.vertices;
var colors : Color[] = new Color[vertices.Length];
var tChange : float;
for (var i = 0; i < vertices.Length;i++){
var worldPosition : Vector3 = objects[x].transform.TransformPoint( vertices[i]);
if(worldPosition.y <startChange) {
colors[i] = Color.Lerp(myColor, bottomColor, vertices[i].y-2);
}
}
mesh.colors = colors;
}
}
Comment
Best Answer
Answer by whydoidoit · May 25, 2012 at 05:33 PM
First you need to get the range - do this before your current for loop:
var minY=1000000.0;
var maxY=-10000000.0;
for (var i = 0; i < vertices.Length;i++) {
minY = Mathf.Min(vertices[i].y, minY);
maxY = Mathf.Max(vertices[i].y, maxY);
}
Then instead of:
colors[i] = Color.Lerp(myColor, bottomColor, vertices[i].y-2);
Do
colors[i] = Color.Lerp(myColor, bottomColor, (vertices[i].y - minY) / (maxY-minY));