Heatmap Color Ramp on 3D model
I'm trying to generate a smooth continuous heat map color ramp of a 3D model, but I get a discrete looking shader on my model. Below is the code I used to generate the heatmap color ramp shader. In particular, I iterate through the vertices of the mesh and assign them new color values correlating to the y value so the lowest y value is given blue and the highest value is given red and the in between are cyan, green, and yellow. I want to scale the values based on height so there are more colors and the model then looks better/smoother.
I'm thinking my issue in how I am taking the Y values (the order of the vertices are stored in the mesh.vertices array) or I am not allowed to create that many colors.
Below is the model with the shader and vertex coloring method applied followed by the code.
public class TopoHeatMap : MonoBehaviour {
public Mesh mesh;
public Material material1;
public Shader vertexShader; // Material with a Custom/Vertex Colored shader assigned in Inspector
public Renderer myRenderer;
public void renderHeatMap()
{
myRenderer = GetComponent<Renderer>();
vertexShader = Shader.Find ("Custom/Vertex Colored");
if (myRenderer.material.shader != vertexShader) {
vertexShader = Shader.Find ("Custom/Vertex Colored");
myRenderer.material.shader = vertexShader;
Mesh mesh = GetComponent<MeshFilter> ().mesh;
Vector3[] vertices = mesh.vertices;
Color[] colors = new Color[vertices.Length]; //stores colors for all vertices in mesh
float partition = vertices.Length / 4;
for (int i = 0; i < vertices.Length; i++) { //iterated through mesh vertices
if (i < vertices.Length / 4) {
colors [i] = new Color (0f, i / (partition), 1f, 1f); //populates color array of vertices with new color
} else if (i < vertices.Length / 2) {
colors [i] = new Color (0f, 1f, (i-partition) / (partition), 1f); //populates color array of vertices with new color
} else if (i < 3 * vertices.Length / 4) {
colors [i] = new Color ((i-2*partition)/ (partition), 1f, 0f, 1f); //populates color array of vertices with new color
} else {
colors [i] = new Color (1f, (i-3*partition)/ (partition), 0f, 1f); //populates color array of vertices with new color
}
}
mesh.colors = colors;
}
else {
vertexShader = Shader.Find ("Standard");
myRenderer.material.shader = vertexShader;
}
}
}
Answer by AlecKWhite · Mar 21, 2017 at 06:56 PM
Here's what the model looks like without the heat map coloration applied.
Your answer
Follow this Question
Related Questions
Mesh missing in prefab 0 Answers
Vertex Colors are lost after build Android 1 Answer
[Mesh] Unity 4 to Unity 5 Mesh Export 0 Answers
trying to skew preafabs 0 Answers
URGENT How to get triangles of mesh in world space? 0 Answers