• 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
Question by CHRISYS · Oct 31, 2013 at 06:42 AM · meshverticesedgetriangleconnected

how to Find the vertices of each edge on mesh

hi guys i am trying to get all of the mesh edges(sides- the light blue lines that appear when you select a mesh, the wire frame) each edge is a pair of vertices that form that edge(line, side) and are conected, i saw this:

http://answers.unity3d.com/questions/443633/finding-the-vertices-of-each-edge-on-mesh.html

and try to use the sample code in the link that its from unity procedural example, i try it with unity default plane and cube it works fine but it only retruns the edges on the borders the horizontal and vertical external lines of a plane but no the diagonal or internal lines, like this:

alt text

it only return me the red lines but not the green ones, some maybe plz can help me with this

sin título.jpg (11.2 kB)
Comment
IgorAherne
andrew-lukasik

People who like this

2 Show 2
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 LijuDeveloper · Oct 31, 2013 at 10:00 AM 0
Share

This code for finding all vertices

 using UnityEngine;
 using System.Collections;

 public class Example2 : MonoBehaviour 
 {
 
 
 
     void Start () 
     {
         
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         float [][] verticesArray;// = new float [vertices.Length][3];
         verticesArray = new float [vertices.Length][];
         
         int vertexCount = mesh.vertexCount;
         
         int i = 0;
         while (i < vertices.Length)
         {
             verticesArray[i] = new float [3];
             
             
             verticesArray[i][0] = vertices[i].x;// X vertex
             verticesArray[i][1] = vertices[i].y;// Y vertex
             verticesArray[i][2] = vertices[i].z;// Z vertex
             
             
             
             Debug.Log (" verticesArray["+i+"] X ="+verticesArray[i][0] +" verticesArray["+i+"] Y ="+ verticesArray[i][1]+" verticesArray["+i+"] Z ="+verticesArray[i][2]+" vertexCount = " +vertexCount );
           i++;
         }
     
     }
 
 
 }
avatar image ArkaneX LijuDeveloper · Oct 31, 2013 at 10:18 AM 0
Share

OP was asking for edge vertices, not for triangle vertices.

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by reefwirrax · Oct 31, 2013 at 10:57 AM

this is a method that takes about 20 lines but it's very simple... STEP 1 Get the array of mesh.triangles, STEP 2run through the entire list in a loop, and return 3 sets of edges, made of 2 vertices, for each triangle and put them in a new array of called mesh.edges, preferably, keep it orderly so that the point with the smallest X, Y, Z is the 1st points of the pair in the list for each edge, that means you have to compare XY and Z for each edge prior to storing it in the array. the array of mesh.edges is going to be the same style the mesh.triangles, except it references the points in the mesh.vertex array in pairs.

if you're not worried about having duplicate edges, they need to compare them and order them, just convert the mesh triangles into a new array of edges with 6 vertices per triangle.

by this time you have about 15 lines. mostly the compare XYZ logic in the loop. The rest is 5 lines.

step 3 after that you can delete duplicates because they are ordered so that the 1st point is always the lowest XYZ, so you can easily compare the 1st and 2nd points to all the other ones in the array. there should be a lot of duplicates because triangles share edges.

It's a good learning project I have done stuff seminar. use debug.drawline if you want, draw the edges using one of the points and the Vector3 the 2nd one.

remember that some models don't share vertices among triangles and 3 times more vertices in the array, duplicates. It doesn't really matter but it's good to know, please vote at my answer if it's useful my feedback is very low, my last profile was the thousand! I can't give people,karma.

Comment
Lahzar
IgorAherne
$$anonymous$$
Hellojeska

People who like this

4 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 reefwirrax · Oct 31, 2013 at 08:02 AM 0
Share

he answer is waiting approval by a moderator. OMG

avatar image Pthorndycraft · Jan 02, 2015 at 04:37 PM 0
Share

I love this answer! Seems so simple now you have described the theory, the edges of a mesh are ones which are not shared by other tris... Simple but effective. Thank you so much for this answer, this will help me out greatly... Now to work out how to repeat a texture along a line...

avatar image IgorAherne · Aug 29, 2015 at 09:47 AM 0
Share

thank you!

avatar image

Answer by markus-weiss · Dec 27, 2018 at 03:38 PM

Yes this question is from 2013, but here is a very short solution: Copy all vertices of ur Mesh to a List. Now remove all duplicate Vector3

The RemoveFunction is from here: https://answers.unity.com/questions/154923/how-do-i-scan-a-list-and-remove-all-duplicates.html

 using System.Linq;
 public class Vertices : MonoBehaviour {
 
     void Start()
     {
         mesh = GetComponent<MeshFilter>().mesh;
         vertices = mesh.vertices;
  
         List<Vector3> VertexList = new List<Vector3>();
         foreach(Vector3 vertex in vertices)
         {
             VertexList.Add(vertex);
 
         }
         VertexList = VertexList.Distinct().ToList();
 
     }
 }
 


Comment
WaqasHaiderDev
Rakun1

People who like this

0 Show 0 · 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

Answer by wheezy602 · Oct 31, 2013 at 10:57 AM

why not just texture the objects? it would make it a lot more graphically sound, would it not?

Comment

People who like this

0 Show 0 · 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

Answer by ArkaneX · Oct 31, 2013 at 11:33 AM

Firstly you need to create a list of edges (edge could be a struct). Then you have to iterate through mesh.triangles, taking 3 elements (vertices) at a time, and then check if any pair of these vertices should be added to your list.

Sample code below, but please note that it's just a very basic solution, not optimized at all. Its performance degrades significantly with triangles count increase.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class EdgesTest : MonoBehaviour
 {
     public struct Edge
     {
         public Vector3 v1;
         public Vector3 v2;
 
         public Edge(Vector3 v1, Vector3 v2)
         {
             if (v1.x < v2.x || (v1.x == v2.x && (v1.y < v2.y || (v1.y == v2.y && v1.z <= v2.z))))
             {
                 this.v1 = v1;
                 this.v2 = v2;
             }
             else
             {
                 this.v1 = v2;
                 this.v2 = v1;
             }
         }
     }
 
     void Start()
     {
         var mesh = this.GetComponent<MeshFilter>().mesh;
 
         var edges = GetMeshEdges(mesh);
         //for (int i = 0; i < edges.Length; i++)
         //{
         //    print(i + ": " + edges[i].v1 + ", " + edges[i].v2);
         //}
     }
 
     private Edge[] GetMeshEdges(Mesh mesh)
     {
         HashSet<Edge> edges = new HashSet<Edge>();
 
         for (int i = 0; i < mesh.triangles.Length; i += 3)
         {
             var v1 = mesh.vertices[mesh.triangles[i]];
             var v2 = mesh.vertices[mesh.triangles[i + 1]];
             var v3 = mesh.vertices[mesh.triangles[i + 2]];
             edges.Add(new Edge(v1, v2));
             edges.Add(new Edge(v1, v3));
             edges.Add(new Edge(v2, v3));
         }
 
         return edges.ToArray();
     }
 }
Comment

People who like this

0 Show 0 · 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

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

21 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

Related Questions

Split single triangle mesh into 2 Equal Parts 0 Answers

How can I ADD vertices and faces to a mesh? 1 Answer

Checking If A Triangle Is In A Mesh 1 Answer

Procedural Cylinder Generation 2 Answers

Find int in triangle index from vertices edge list 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