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 Dimerk · Nov 20, 2014 at 02:06 PM · meshcolorvertices

Fast way to find vertex of spezific color

Hi,

i have a big mesh with colored vertices. The mesh is so big that unity split it into submeshes. Now i want to find vertices to a given color. My first try was to iterate over the whole color array of one submesh, and this for all submeshes. It works but it takes a loooooooooooooooooong to complete. Is there some sort of function which can find the vertices for a given color ?

My Code so far:

 // at start, check for filter of the parent node
 void Awake () {
     checkMeshForFilter(transform);
 }


 // just check for the mesh filter. if there is no -> check the children
 // if mesh filter is found -> check for color of the mesh
 private void checkMeshForFilter(Transform obj){
     MeshFilter filter = obj.GetComponent<MeshFilter>();
     if (filter == null)
         for (int i=0; i < obj.transform.childCount; i++)
             checkMeshForFilter(transform.GetChild(i));
     else
     {
         checkMeshForColor(new Color32(255, 0, 0, 0));
     }
 }

 // iterate over all color of the mesh and compare it with the given color
 private void checkMeshForColor(Color32 color, MeshFilter filter){
     Mesh mesh = filter.mesh;

     for (int i=0; i < mesh.colors32.Length; i++)
         if (mesh.colors32[i].Equals(color))
             Debug.Log("FOUND");
 }

Also here is my hierarchy of the mesh

 MESH (no filter)
 ->SUB_MESH_1 (with filter & mesh)
 ->SUB_MESH_2 (with filter & mesh)
 ->SUB_MESH_3 (with filter & mesh)

Best dimerk

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
4

Answer by Bunny83 · Nov 20, 2014 at 02:22 PM

There is no faster way and it shouldn't take that long. In your code above the only thing that might cause a severe delay is your Debug.Log. That is slow like hell ;)

Try this instead:

 string str = "";
 Color32[] colors = mesh.colors32;
 for (int i=0; i < colors.Length; i++)
     if (colors[i].Equals(color))
         str += i+", ";
 Debug.Log("FOUND: " + str);

If there are a lot vertices with that color this will be way much faster, even though "str +=" is also quite slow and inefficient (and a StringBuilder would be better) it s still 1000 times faster then seperate Debug.Logs.

edit
Forgot to mention that you should cache the array as you actually create two new arrays each iteration. The colors or colors32 property creates a temporary array with the color values and returns it every time you read the property. So in your original code you get a copy just to check the array length (in the for-loop-condition) and again inside the for loop body. If the array is large (which it probably is when you already have to split the mesh into submeshes) this will create a huge amount of garbage. One array with 64k elements requires around 260KB. Since you create 2 arrays for each array element it's 64k * 260KB = ~ 16.3 GB ^^

Comment
Add comment · Show 3 · 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 Dimerk · Nov 20, 2014 at 05:37 PM 0
Share

Awesome.Thank you for the solution and thank you for writing the reason what's the problem is!! Runtime now is about 1 $$anonymous$$ute. Was 5 $$anonymous$$utes or more.

avatar image Bunny83 · Nov 20, 2014 at 06:23 PM 0
Share

1 $$anonymous$$ute? That's impossible. Even when you have 10 meshes with all 64k vertices this should be a matter of seconds (or even fractions of a second). Is that your whole code?

It's also not necessary to iterate manually to all children. There is GetComponentsInChildren which you can use to get all $$anonymous$$eshFilters on that gameobject and on all child objects.

avatar image Dimerk · Nov 20, 2014 at 08:30 PM 0
Share

Yes, i had some heavy stuff running (Loading some dll and when missing some hardware it slows down the start of the whole program). When i disable this, it's instant. Thanks again :)

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

27 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 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 avatar image

Related Questions

how to remove vertices of a certain colour range from a mesh? 0 Answers

Color not appearing on created mesh? (only grey) 2 Answers

Changing Mesh Vertex Colors in editor 0 Answers

Change color of mesh triangle based on Y position in world space 1 Answer

Connecting flatshaded vertices 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