- Home /

# Mesh triangles and vertices, searching for adjacent triangle

I made a 3D model using 3ds Max. It has 160 vertices, 316 triangles and 4 smoothing groups. I set it as a mesh collider for game object in Unity. When I'm spherecasting against this object and hitting an edge I want to know the angle between 2 triangles adjacent to this edge. I know if the edge was hit by using my own function that calculates barycentric coordinates (built-in property doesn't work for spherecast for some reason). By using "triangleIndex" hit property and barycentric coordinates, I'm getting the vertex indices of the edge we hit. The only thing to do is to find 2 triangles that has these 2 vertices in common, right? The first triangle is already found and its index written in "triangleIndex" property. My problem is that I just can't find the second one. Analysing triangles[] and vertices[] arrays in debug mode I found out that mesh has 328 vertices and 948 triangles! That's way much more then model actually has. I heard it can be because of smoothing groups, but model without smoothing groups shows the same numbers. Also I searched how many triangles are connected with, for example, the first vertex. It turned out there are only 2 triangles! How can it be? My model is closed and it has no holes. So the question is how the hell these triangles and vertices are numbered and how to find adjacent triangle knowing 2 vertices. Or how to make mesh collider with correct structure.

**Answer** by Bunny83
·
Aug 06, 2018 at 06:05 PM

Well you have several seperate issues here. First of all don't trust any vertex / triangle information you get from modelling tools. They work with logical vertices to make editing easy.

The next thing is vertices don't need to be shared. In fact if the same vertex that is used by two trianges but has any vertex attributes that differ (position, normal, uv, color, tangent, ....), the vertex has to be duplicated. You can't have two or more normals, positions, colors, ... for the same vertex. So If a vertex is actually shared between two triangles, you can detect them through the triangles indices. However if the vertices are splitted they are essentially two seperate vertices. If you need to find adjacent triangles you first have to find other vertices with the same position. Once you have all vertices for a certain position you can check the triangles array where those are used.

For example an ordinary cube mesh has 8 "logical" vertices but actually requires 24 vertices. This has several reasons. The most important is that each "face" has a different surface normal. Since a cube face is flat, all 4 corners of a face need to have the same normal. Since always 3 "faces" (quads) meet at each of the 8 corners each corner need to be splitted into 3 seperate vertices (3 * 8 == 24). The same number can be aquired by having seperate 4 vertices per face. Since a cube has 6 faces 4*6==24. Apart from the normals you often need to split vertices when you want to unwrap the mesh (create UV coordinates). In theory a sphere mesh could use fully shared vertices since at each point 4 quads meet and have the same normal. However when you want any meaningful UV mapping you need at least 1 UV "seam". At those seems you will have duplicated vertices.

There has been several other questions that revolve around shared vertices. The code in my answers might be useful. One here, another one here, and another one

Thank you for your answer! I got the vertices duplication thing. But why triangles have to be duplicated too? And back to my problem. The only way to find the angle between triangles is to search over the whole array of vertices for ones with the same position, and then search through the whole array of triangles? Can "==" operator be used then? Is it correct to compare float values?

About your last question, that depends entirely on the mesh. The == operator could be used if the positions of the two vertices are exactly the same. However the slightest difference and it won't work. It's common to compare them approximately to a certain threshold.. Just subtract the two positions from each other. Take the squared magnitude of the resulting vector. If it's close to 0 (or equal) they could be considered the same.

Triangles aren't really duplicated. Your mesh contains exactly 316 triangles. $$anonymous$$eep in mind that the triangles array contains **vertex indices**. You need 3 to define one triangle. 3*316 == 948 indices.

What exact angle do you want to know? When you have smooth normals (no hard edges) using barycentric interpolation should give you the proper normal at any given point.

$$anonymous$$eep in mind that when you use a mesh as collider for a dynamic object (rigidbody) the collider need to be marked as convex. However this could mean that the actual mesh will look completely different since PhysX will calculate the convex hull of your mesh. In the past the convex hull was limited to 255 polygons. I'm not sure if this still applies as the documentation doesn't mention that limit anywhere. Note that static colliders don't need to be convex and can have more that 255 polys.

I'm using it for my custom character controller. It follows the rotation of the surface when moving. But it follows even when I don't need this, for example stairs (or any sharp corner of floor). So I want to make condition when character should follow rotation and when it doesn't. If hit the edge > check angle between faces making this edge > if angle is greater than certain value then do not rotate.

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

### Follow this Question

### Related Questions

Quick way to get touching triangles? 0 Answers

How to find connected mesh triangles? 2 Answers

Problem creating submeshes based on raycast. 0 Answers

Does mesh.triangles also return a copy of the triangle array? 1 Answer

Setting triangles failing 0 Answers