Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jordii · Dec 29, 2012 at 05:52 PM · verticesvertexconnected

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:

quad

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

Comment
Add comment · Show 6
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 Piflik · Dec 29, 2012 at 08:09 PM 1
Share

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.

avatar image Jordii · Dec 29, 2012 at 08:18 PM 0
Share

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.

avatar image pako · Dec 29, 2012 at 08:21 PM 0
Share

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

avatar image pako · Dec 29, 2012 at 08:23 PM 0
Share

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

avatar image Jordii · Dec 29, 2012 at 08:26 PM 0
Share

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

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 5 · 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 Jordii · Dec 30, 2012 at 12:26 PM 0
Share

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 see$$anonymous$$gly grouped normal. I've just opened a topic at http://forum.unity3d.com/threads/164270-Unity-s-$$anonymous$$esh.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 ins$$anonymous$$d of their individual normal?

avatar image Bunny83 · Dec 30, 2012 at 03:32 PM 1
Share

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.

avatar image Jordii · Dec 30, 2012 at 03:35 PM 0
Share

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

avatar image shanepisko · Jan 16, 2014 at 12:19 AM 0
Share

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

avatar image Bunny83 · Jan 16, 2014 at 10:56 AM 0
Share

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

avatar image
1

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

Comment
Add comment · Show 10 · 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 Jordii · Dec 29, 2012 at 08:20 PM 0
Share

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?

avatar image pako · Dec 29, 2012 at 08:36 PM 1
Share

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.

avatar image Jordii · Dec 29, 2012 at 10:36 PM 0
Share

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/nSps$$anonymous$$.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 $$anonymous$$ax however, it's not a detached vertex. How come?

avatar image pako · Dec 29, 2012 at 11:06 PM 0
Share

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.

avatar image Jordii · Dec 29, 2012 at 11:16 PM 0
Share

O$$anonymous$$ 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/nSps$$anonymous$$.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 see$$anonymous$$gly 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 :)

Show more comments

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

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

Moving vertices in shaders 1 Answer

how to Find the vertices of each edge on mesh 4 Answers

Vertex Shader problem with _WorldSpaceCameraPos 1 Answer

UnityEngine.UI.Text characters mesh 0 Answers

Setting and storing vertex locations in a shader 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