Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by QuantumCD · Apr 28, 2013 at 05:17 PM · c#mesh

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 }
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image QuantumCD · Apr 28, 2013 at 05:57 PM 0
Share

Thanks for the example. I'll try it out and report back as soon as possible.

avatar image QuantumCD · Apr 28, 2013 at 11:29 PM 0
Share

@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.

avatar image numberkruncher · Apr 28, 2013 at 11:48 PM 0
Share

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.

avatar image numberkruncher · Apr 29, 2013 at 12:02 AM 0
Share

Done, but again, please note this has not been tested :)

avatar image QuantumCD · Apr 29, 2013 at 01:14 AM 1
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to Calculate UV Map and Tangents for Unity Mesh 1 Answer

Algorithm for automatic mesh creation 1 Answer

Using compute shader, how to take every vertex of a mesh and make a cube spawn and move to each one? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges