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.

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