• 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
0
Question by twisted89 · Apr 23, 2013 at 02:59 PM · meshvertices

Finding the vertices of each edge on mesh

I'm fairly new to unity and game development is general so sorry in advance if this is a stupid question! Basically i'd like to know how i can get all of the vertices of a mesh along each of the sides (its a flat plane with a height map applied to it) and then stitch them to the adjacent mesh.

Comment
Add comment · Show 1
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 Loius · Apr 23, 2013 at 03:33 PM 0
Share

I can't answer, because math and me don't get along, but I'm working on a similar problem, so I can offer a starting point:

Unity keeps the edge information in mesh.triangles, which is a list of indices into mesh.vertices.

So if you have vertices 0,1,2,3 describing a square, you might have triangles be 0,1,2, 1,3,2 for the two triangles.

You'd need to Use the $$anonymous$$aths to find which pairs in the triangle list are the edges of each of your shapes, store all those edges in an ordered way, then devise an endpoint for nearby vertices of different edges to be stitched to.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by twisted89 · Apr 23, 2013 at 04:01 PM

Just ran this and I think it does the job...

 /// Builds an array of edges that connect to only one triangle.
     /// In other words, the outline of the mesh    
     public static Edge[] BuildManifoldEdges(Mesh mesh)
     {
         // Build a edge list for all unique edges in the mesh
         Edge[] edges = BuildEdges(mesh.vertexCount, mesh.triangles);
 
         // We only want edges that connect to a single triangle
         ArrayList culledEdges = new ArrayList();
         foreach (Edge edge in edges)
         {
             if (edge.faceIndex[0] == edge.faceIndex[1])
             {
                 culledEdges.Add(edge);
             }
         }
 
         return culledEdges.ToArray(typeof(Edge)) as Edge[];
     }
 
     /// Builds an array of unique edges
     /// This requires that your mesh has all vertices welded. However on import, Unity has to split
     /// vertices at uv seams and normal seams. Thus for a mesh with seams in your mesh you
     /// will get two edges adjoining one triangle.
     /// Often this is not a problem but you can fix it by welding vertices 
     /// and passing in the triangle array of the welded vertices.
     public static Edge[] BuildEdges(int vertexCount, int[] triangleArray)
     {
         int maxEdgeCount = triangleArray.Length;
         int[] firstEdge = new int[vertexCount + maxEdgeCount];
         int nextEdge = vertexCount;
         int triangleCount = triangleArray.Length / 3;
 
         for (int a = 0; a < vertexCount; a++)
             firstEdge[a] = -1;
 
         // First pass over all triangles. This finds all the edges satisfying the
         // condition that the first vertex index is less than the second vertex index
         // when the direction from the first vertex to the second vertex represents
         // a counterclockwise winding around the triangle to which the edge belongs.
         // For each edge found, the edge index is stored in a linked list of edges
         // belonging to the lower-numbered vertex index i. This allows us to quickly
         // find an edge in the second pass whose higher-numbered vertex index is i.
         Edge[] edgeArray = new Edge[maxEdgeCount];
 
         int edgeCount = 0;
         for (int a = 0; a < triangleCount; a++)
         {
             int i1 = triangleArray[a * 3 + 2];
             for (int b = 0; b < 3; b++)
             {
                 int i2 = triangleArray[a * 3 + b];
                 if (i1 < i2)
                 {
                     Edge newEdge = new Edge();
                     newEdge.vertexIndex[0] = i1;
                     newEdge.vertexIndex[1] = i2;
                     newEdge.faceIndex[0] = a;
                     newEdge.faceIndex[1] = a;
                     edgeArray[edgeCount] = newEdge;
 
                     int edgeIndex = firstEdge[i1];
                     if (edgeIndex == -1)
                     {
                         firstEdge[i1] = edgeCount;
                     }
                     else
                     {
                         while (true)
                         {
                             int index = firstEdge[nextEdge + edgeIndex];
                             if (index == -1)
                             {
                                 firstEdge[nextEdge + edgeIndex] = edgeCount;
                                 break;
                             }
 
                             edgeIndex = index;
                         }
                     }
 
                     firstEdge[nextEdge + edgeCount] = -1;
                     edgeCount++;
                 }
 
                 i1 = i2;
             }
         }
 
         // Second pass over all triangles. This finds all the edges satisfying the
         // condition that the first vertex index is greater than the second vertex index
         // when the direction from the first vertex to the second vertex represents
         // a counterclockwise winding around the triangle to which the edge belongs.
         // For each of these edges, the same edge should have already been found in
         // the first pass for a different triangle. Of course we might have edges with only one triangle
         // in that case we just add the edge here
         // So we search the list of edges
         // for the higher-numbered vertex index for the matching edge and fill in the
         // second triangle index. The maximum number of comparisons in this search for
         // any vertex is the number of edges having that vertex as an endpoint.
 
         for (int a = 0; a < triangleCount; a++)
         {
             int i1 = triangleArray[a * 3 + 2];
             for (int b = 0; b < 3; b++)
             {
                 int i2 = triangleArray[a * 3 + b];
                 if (i1 > i2)
                 {
                     bool foundEdge = false;
                     for (int edgeIndex = firstEdge[i2]; edgeIndex != -1; edgeIndex = firstEdge[nextEdge + edgeIndex])
                     {
                         Edge edge = edgeArray[edgeIndex];
                         if ((edge.vertexIndex[1] == i1) && (edge.faceIndex[0] == edge.faceIndex[1]))
                         {
                             edgeArray[edgeIndex].faceIndex[1] = a;
                             foundEdge = true;
                             break;
                         }
                     }
 
                     if (!foundEdge)
                     {
                         Edge newEdge = new Edge();
                         newEdge.vertexIndex[0] = i1;
                         newEdge.vertexIndex[1] = i2;
                         newEdge.faceIndex[0] = a;
                         newEdge.faceIndex[1] = a;
                         edgeArray[edgeCount] = newEdge;
                         edgeCount++;
                     }
                 }
 
                 i1 = i2;
             }
         }
 
         Edge[] compactedEdges = new Edge[edgeCount];
         for (int e = 0; e < edgeCount; e++)
             compactedEdges[e] = edgeArray[e];
 
         return compactedEdges;
     }
 }
 
 public class Edge
 {
     // The indiex to each vertex
     public int[] vertexIndex = new int[2];
     // The index into the face.
     // (faceindex[0] == faceindex[1] means the edge connects to only one triangle)
     public int[] faceIndex = new int[2];
 }
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 EvilTak · Oct 10, 2015 at 07:01 AM 0
Share

Doesn't work for most complex meshes, I wonder why...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Connecting flatshaded vertices 0 Answers

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

Finding Viewport Coordinates of Vertices of Meshes that are in View 1 Answer

A doubt in QA on performance on a generic mesh info? 0 Answers

can 5 verts and 4 triangles be done on a square texture? 1 Answer


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