Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sinithparanga · Sep 05, 2017 at 10:26 AM · mesh collidertrianglesbest practicesiterate

Looking for triangles attached to a vertice

Hey guys,

taking a mesh into consideration. If I want to see all triangles a vertice is attached to, I did the following.

 Loop start until triangle.length
     if myVertice == mesh.vertices[triangle[i]] // found the same vertice in anouther or the same triangle

This works, but I don't like the idea of itirating to all Vertices for triangle length every time. Is there another way to solve this Problem?

Thanks, Sinith

Comment
Add comment · Show 3
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 sinithparanga · Sep 05, 2017 at 07:02 PM 1
Share

Hey guys, thx for the quick answers.

I am still in the design phase and going through some ideas and tests before blueprinting everything.

Well the idea is to do a 3d pathfinding on a big sphere and I need this - find the vertices near me - feature. I want to have several objects doing this pathfinding simultaneously and they have to check on every vertices the next near vertices... So I guess this can cause some performance issues. So I could just set it up and then go for "just more memory" solution...

What I have been doing the last hour was to create a sphere from scratch. Here I have the solution to assign each vertices a latitude and a longitud. Just like earth I can calculate very easy the vertices next to the one the characters are currently on.

I just though maybe there is a neat little function that finds the vertices and triangles of near the current one.

Or maybe I just asked the question wrong.

Reading out Glurth answer now... this is a good approach as well. would definitely save up some time. I'll look into that.

Cheers and again thx for the answers! Singt

avatar image Glurth sinithparanga · Sep 05, 2017 at 07:23 PM 0
Share

converted your answer to a comment. Also upvoted Q and comment, hopefully giving you enough rep to make comments now ;)

avatar image sinithparanga Glurth · Sep 05, 2017 at 07:34 PM 0
Share

thank you.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Sep 05, 2017 at 11:04 AM

In general you want to cache the various arrays that the mesh object provides in a local variable when you need to access them more than once.

 // C#
 var verts = mesh.vertices;
 var triangles = mesh.triangles;
 for(int I = 0; I < triangles.length; I++)
 {
     if (vertex == verts[triangles[i]])
         // ...
 
 }

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 sinithparanga · Sep 05, 2017 at 11:12 AM 0
Share

This I understand. Is the only way to look for the near triangle a iteration with a for loop? Has triangles not some kind of logic that can be implemented to make this faster?

avatar image Bunny83 sinithparanga · Sep 05, 2017 at 01:57 PM 0
Share

No, there's no faster way. Triangles are just a collection of index data. If you already know the index of a particular vertex it would be faster since you don't need to compare vector3 values but only indices.

Actually in most cases when you try to find something in an array / collection there's no way around iterating through it. This is already the fastest you can get. Though a single for loop though a couple of thousand triangle indices is extremely fast anyways.

What's the exact problem you have? Do you have performance problems? Have you checked the profiler?

avatar image sgrein Bunny83 · Apr 16, 2019 at 11:02 PM 0
Share

Here is some basic implementation:

 internal static List<List<int>> neighborhood($$anonymous$$esh mesh)
     {
         int numVerts = mesh.vertices.Length;
         int numTris = mesh.triangles.Length;
         int[] localTris = mesh.triangles;
 
         /// For each mesh vertex (index) store a list of the triangle indices 
         List<List<int>> vertexToTriangleList = 
         new List<List<int>>(mesh.vertices.Length);
 
         /// Init list
         for (int i = 0; i < numVerts; i++)
         {
             vertexToTriangleList.Add(new List<int>());
         }
 
         /// Store containing triangle indices for vertices
         int stride = 3;
         for (int i = 0; i < numTris; i += stride)
         {
             vertexToTriangleList[localTris[i + 0]].Add(i);
             vertexToTriangleList[localTris[i + 1]].Add(i);
             vertexToTriangleList[localTris[i + 2]].Add(i);
         }
         return vertexToTriangleList;
     }
       

$$anonymous$$y meshes contain roughly 2,000,000 triangles - Runtime is 0.4 second. I'm not sure if my implementation is the most efficient (Intel Core i7-4700$$anonymous$$ @ 4 GHz)

avatar image
1

Answer by Glurth · Sep 05, 2017 at 06:37 PM

If this actually causes you performance problems, you can always use the solution of " use more memory".

-At startup: build an array the same size as your mesh vertex array.
-Each element of this array stores a set/list of triangles the vertex belongs to.
-Once built, you can simply index to this array (using the vertex index) to get the list of triangles the vertex belongs to, without any need to loop.

Comment
Add comment · Show 1 · 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 Glurth · Sep 05, 2017 at 08:18 PM 0
Share

Since you mentioned pathing, keep in $$anonymous$$d: multiple mesh vertices CAN potentially have the same position. In this case, they probabaly have different normals, but pathing won't care about that. Your initial loop could compare vertex positions, without worry about how long it takes.

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

70 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 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 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

Detecting a player "on the right path"? 2 Answers

Mesh Colliders, best practice for large meshes? 1 Answer

Very Confused About Convex Mesh Collider Result 0 Answers

Will mesh collider have the same tringles/veticies of the base mesh? 0 Answers

Iterate through Generic List 2 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