- Home /
Selecting all Vertices of a Specific Color
I'm looking to create a seemingly simple editor script that allows me to select all the vertices of a specific color. Originally I thought it would be as simple as something like:
foreach (vertex in mesh.vertices)
{
if (vertex.color == Colors.black)
{
mesh.vertices.remove(vertex);
}
}
However, it isn't that easy, unfortunately. Thanks for any help you can offer! I would prefer C# advice, as I primarily use C#. I'm sure I could port most UnityScript though.
Answer by numberkruncher · Apr 28, 2013 at 05:56 PM
This would probably be quite easy with Linq. Warning the following has not been tested, but I think that it will work:
void RemoveVerticesByColor(Mesh mesh, Color color) {
// Get local copy of current vertex positions and color values.
Vector3[] vertices = mesh.vertices;
Color32[] colors32 = mesh.colors32;
Vector3[] normals = mesh.normals;
Vector2[] uvs = mesh.uv;
int[] triangles = mesh.triangles;
mesh.Clear();
// Extract the ones that are not `color`.
mesh.vertices = vertices.Where((vertex, index) => colors32[index] != color).ToArray();
mesh.colors32 = colors32.Where(deleteColor => deleteColor != color).ToArray();
mesh.normals = normals.Where((normal, index) => colors32[index] != color).ToArray();
mesh.uv = uvs.Where((uv, index) => colors32[index] != color).ToArray();
// Rebuild triangle data.
List<int> newTriangles = new List<int>();
for (int tri = 0; tri < triangles.Length; tri += 3) {
int v1 = triangles[tri + 0];
int v2 = triangles[tri + 1];
int v3 = triangles[tri + 2];
// Triangle must be removed if it contains at least one removed vertex.
if (colors32[v1] == color || colors32[v2] == color || colors32[v3] == color)
continue;
// Offset vertex indices.
v1 -= CountRemovedIndicesBefore(colors32, color, v1);
v2 -= CountRemovedIndicesBefore(colors32, color, v2);
v3 -= CountRemovedIndicesBefore(colors32, color, v3);
// Add vertices to triangle buffer.
newTriangles.Add(v1);
newTriangles.Add(v2);
newTriangles.Add(v3);
}
mesh.triangles = newTriangles.ToArray();
}
int CountRemovedIndicesBefore(Color32[] colors32, Color32 removedColor, int index) {
int count = 0;
for (int i = 0; i < index; ++i)
if (colors32[i].Equals(removedColor))
++count;
return count;
}
Thanks for the example. I'll try it out and report back as soon as possible.
@numberkruncher The code seems to work. I had to call mesh.Clear() before assigning, otherwise you get out of bounds exceptions. It seems to delete a logical amount of vertices, however, the mesh disappears. The GameObject still exists and all, but do I need to do something after this? I already tried recalculating the bounds and normals.
Normals are stored on a per vertex basis, so you could adjust the normals array in the same way as the vertex array. Your mesh is vanishing because the triangle references of the vertices have become incorrect. I will update the above example for you.
Done, but again, please note this has not been tested :)
I had a hunch it was something to do with the triangles, because nothing else seemed to work. ;)
Your script works pretty good, except there are a few $$anonymous$$or errors. On line 43, you should change it to: if (colors32[i].Equals(removedColor)
as the Color32 cannot be compared using ==
which it really should be...
Secondly, since you are using color twice in your LINQ code, I edited the parameter color
value to be deleteColor
ins$$anonymous$$d.
Besides that, it appears to work. Thanks!