- Home /

# Is there an easy way to find connected vertices in Mesh.vertices?

As of now, Mesh.vertices seems to return 3 vertices for each triangle. However, uin the case of a quad I expected it to be just four, like this:

But on point 0 and 2 in the image, there are two vertices, each with their own normal. I expected these to be connected.

**QUESTION:** How do I find these "connected" vertices from Mesh.vertices? So in my example, how do I know which two vertices are at point 2.

I of course, can iterate and make groups of vertices which share the exact same position, but I thought maybe Unity had a simpler solution for this.

Thanks in advance :)

There are no quads, only triangles. Ever.

Vertex 0 and 2 are 2 vertices each if the two triangles either have two different smoothing groups, material ids or are part of different UV shells.

Piflik I know there are only triangles. I'm just wondering why the parts where triangles "share" a vertex, like point 0 and 2, they have TWO vertices. And how I can easily find which ones are "connected" if you will.

It is so that they can have different normals, in order for the edges to look sharper.

If you use Mesh.triangles as per my answer below, you'll be able to see exactly with what vertices each triangle is formed.

no pako, I need WHICH vertices are on the same position/connected.

**Answer** by Bunny83
·
Dec 30, 2012 at 12:21 PM

Usually if a vertex can be shared most modelling tools *will* share it. So in case of a simple quad you should have 4 vertices and 6 triangle indices (2 * 3 == 2 triangles). Two of the vertices will be shared. However in most cases when it comes to more complex models vertices need to be split because they aren't equal in each detail.

A vertex is made up by:

the position

the vertex normal vector

texture coodinates (UV)

vertex color

vertex tangent

If any of those things have to be different for an adjacent triangle, the vertices can't be shared since they are different. For example a cube has 24 vertices instead of 8 because each "face" / quad will need different UVs and much more important a unique normal. At each corner of the cube you will have 3 vertices at the same position but with 3 different normals so they can't be shared since they are not the same.

If you just want to find vertices which shared the same position you have to iterate through the vertices array, there's no way around that since the vertices are not connected in any way.

Wow this was what I was looking for. I did not know this. However, I want to build a displacement script which moves vertices along their seemingly grouped normal. I've just opened a topic at http://forum.unity3d.com/threads/164270-Unity-s-Mesh.vertices-doesn-t-seem-to-connect-all-vertices.-Why with some examples.

See how 0, 7 and 15 are disconnected? How do I move them along a grouped normal instead of their individual normal?

Well, like SpookyCat said you probably have different UV (texture) coordinates so the vertices need to be split.

Finding all vertices that are at the same position isn't that difficult. However if you need to do this alot and if you have a many vertices you might create a "connection map". Just an array that holds information about the connected vertices.

This is a helper class to find which vertices belong to each other:

```
public class VertexConnection
{
public List<int> connections = new List<int>();
}
```

Now you need to fill the array and create one instance of this class for each vertex

```
Vector3[] vertices = mesh.vertices;
VertexConnection[] connections = new VertexConnection[vertices.Length];
for(int i = 0; i < vertices.Length; i++)
{
var P1 = vertices[i];
var VC1 = connections[i];
for(int n = i+1; n < vertices.Length; n++)
{
if (P1 == vertices[n])
{
var VC2 = connections[n];
if (VC2 == null)
VC2 = connections[n] = new VertexConnection();
if (VC1 == null)
VC1 = connections[i] = new VertexConnection();
VC1.connections.Add(n);
VC2.connections.Add(i);
}
}
}
```

Now you have the connections array which only contains a VertexConnection instance when there are connected vertices otherwise it contains null.

If you want to move an vertex just check the connections array, if it contains an instance, process the vertices in the array as well.

Thanks Bunny83. That was kind of what I already started to build, but was hoping I maybe overlooked something Unity already had internally. Thanks so much for the explanation because the UV thing was new to me. Accepted it as the answer to my question :)

so how would i access only one of the shared vertices, not all of them?

@shanepisko: I don't get your question. This sounds like "how can I send an email to one person only and not the whole world" or "how can I find my friend with blond hair in the phone book".

Think about what you actually want to do. Your current "question" is way to unspecific. Also you might want to post an actual question rather than a comment to this one since your question seems to be different.

**Answer** by pako
·
Dec 29, 2012 at 07:55 PM

You want the triangles for the mesh:

http://docs.unity3d.com/Documentation/ScriptReference/Mesh-triangles.html

If I recall correctly it has a list of triangles, which indexes of the three vertices a triangle consists of. In Unity, each triangle has unique vertices I think. So in my image above I have 6 vertices, NOT 4. How can I find the overlapping ones on 0 and 2?

The triangle does not have unique vertices, its vertices may be used in other triangles. In you example your have 2 triangles:

[0 ,1 , 2] and [0, 2, 3]

i.e. 4 vertices, with 2 vertices common. Your image has only 4 vertices, not 6.

Hmmm you seem to be correct pako. However, it doesn't seem to work as I expected, which led me to believe triangles wasn't the solution at first. When I move vertices[1] I get this: http://i.imgur.com/nSpsK.jpg

However, when I move vertices[0] I get this: http://i.imgur.com/IUzGe.jpg

0 doesn't seem to be connected in triangles to any other triangle. In 3ds Max however, it's not a detached vertex. How come?

Hey Jordii,

I had a look at the 2 images, and tried to figure out exactly what you mean, but I'm not completely sure what you mean. Could you be a bit more specific to what it is that you want to do, and the relevance of these images. I'm trying to help, but it seems a bit difficult for me "to connect the dots" and follow what you want to do.

OK I'll be clear on what I want to do. I am building a displacement map script in Unity, seeing that that isn't available. I have a texture, which is the Diffuse here. White shall be displaced 1f along the vertex normal. Black will not be displaced. I had it working, but in some cases, vertices seem to not be shared along triangles, like in my example is the case with vertex 0 (http://i.imgur.com/IUzGe.jpg) So this vertex [0] and [1] example does NOT look at the diffuse. I displace them by code.

In this example I grabbed vertices[1] and displaced it (the correct version, http://i.imgur.com/nSpsK.jpg). As you can see its connected to several triangles. But when I displace vertices[0] along its normal, it lifts only one triangle vertex, not the other seemingly connected ones.

In this example it's a 20 triangles based geosphere. I've printed the triangles + corresponding vertex indices to: http://nopaste.info/acc95e582f.html

As you can see vertex 0 is only used once, tho in the picture (http://i.imgur.com/IUzGe.jpg) you can see it seems to be shared among other triangles. Why is this?

Also want to really thank you for your time pako, it is very much appreciated :)

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.