- Home /
Changing Mesh Vertex Colors in editor
I am trying to build an editor tool which will change the vertex colors of a chosen Mesh asset according to my calculations
I have the following code:
using UnityEditor;
using UnityEngine;
public class GenerateVertexColors : EditorWindow {
private Mesh[] meshes = new Mesh[4];
[MenuItem ("MGD SS App/GenerateVertexColors")]
public static void DoGenerateVertexColors () {
GenerateVertexColors window = (GenerateVertexColors)EditorWindow.GetWindow (typeof (GenerateVertexColors));
}
void OnGUI () {
for (int i = 0; i < meshes.Length; ++i) {
meshes[i] = EditorGUILayout.ObjectField (meshes[i], typeof (Mesh), false) as Mesh;
}
if (GUILayout.Button ("Generate")) {
//MeshFilter[] meshes = modelGameObject.GetComponentsInChildren<MeshFilter>();
float min, max = 0;
max = float.MinValue;
min = float.MaxValue;
for (int i = 0; i < meshes.Length; ++i) {
Mesh mesh = meshes[i];
if (mesh == null)
continue;
for (int j = 0 ; j < mesh.vertexCount ; ++j) {
if (mesh.vertices [j].y < min) {
min = mesh.vertices [j].y;
}
if (mesh.vertices [j].y > max) {
max = mesh.vertices [j].y;
}
}
}
for (int i = 0; i < meshes.Length; ++i) {
Mesh mesh = meshes[i];
if (mesh == null)
continue;
Color[] colors = new Color[mesh.vertexCount];
for (int j = 0 ; j < mesh.vertexCount; ++j) {
float value = (mesh.vertices[j].y - min) / (max - min) + 0.1f;
colors[j] = new Color (value, value, value, 1);
}
mesh.colors = colors;
}
}
}
}
It only works if I import the materials for the chosen .fbx and if I set them to a shader that uses vertex colors. I tried doing this in runtime, but I have meshes with 40K+ vertices, so it is kinda slow.
Please help.
Comment