- Home /

# 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:

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

andrew-lukasik

### People who like this

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++;
}
}
}
```

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

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

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

**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();
}
}
```

**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();
}
}
```

### Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. **Please note, Unity Answers is now in read-only so we can prepare for the final data migration.**

For more information and updates, please read our full announcement thread in the Unity Forum.

### Follow this Question

### Related Questions

Create edges for vertices(JS) 1 Answer

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