I want to read all the polygons from a mesh. I have created a custom class representing a polygon which stores a list of the vertices as well as the normal. I then want to create a list of these by reading the mesh. My current code is like this:
Vector3[] vertices = level.vertices;
Vector3[] normals = level.normals;
int[] triangles = level.triangles;
int normalIndex = 0;
for (int i = 0; i < triangles.Length; i += 3)
{
Vector3[] polyVerts = new Vector3[3];
polyVerts[0] = vertices[triangles[i+0]];
polyVerts[1] = vertices[triangles[i+1]];
polyVerts[2] = vertices[triangles[i+2]];
navMesh.Add(new NavMeshPolygon(polyVerts, normals[normalIndex++]));
}
Debug.Log(normals.Length + " | " + normalIndex);
However I seem to misunderstand how the normals are stored since the Debug.Log() at the end returns different values for the amount of normals in the mesh and the normals used when I create my polygons. Anyone who can explain how this works (and also give me a heads up if the other code seems correct)?
I just realised that vertices have normals so there are as many normals as there are vertices. However are the normals and vertices “synced” in the list so that for example normals[0] should be used with vertices[0] and so on? Also, if there are 3 vertices for a polygon and thus 3 normals, how do I calculate the “final” normal for the polygon?
I want to know the direction the polygon is facing.
However now I also have another problem. Lets say I have a list of all the polygons (a code representation) where each polygon stores its vertices and normal. How would I go about determining if 2 of those polygons “share” an edge (in other words if they are next to eachother).
What do you mean by “unique per triangle”? Each polygon stores its own vertices, however several polygons can have the same vertices (different copy but same values). The only way I can think of is to loop through each polygon, checking wether every 2 vertices match every pair of vertices in the other polygons. This would result in some nasty nested loops (3 or 4) if I’m not completely mistaken?
By unique I mean are vertices shared or not. For example a quad could be made from 4 vertices where 2 are shared between 2 triangles, or 6 unique vertices where none are shared.
foreach (Polygon polygon in navMesh.Polygons)
{
for (int i = 0; i < polygon.Indices.Length; i++)
{
int indexOne, indexTwo;
if (i == polygon.Indices.Length - 1)
{
indexOne = polygon.Indices[polygon.Indices.Length - 1];
indexTwo = polygon.Indices[0];
}
else
{
indexOne = polygon.Indices[i];
indexTwo = polygon.Indices[i + 1];
}
for (int j = 0; j < navMesh.Polygons.Count; j++)
{
if (navMesh.Polygons[j] == polygon) continue;
for (int k = 0; k < navMesh.Polygons[j].Indices.Length; k++)
{
if (k == navMesh.Polygons[j].Indices.Length - 1)
{
if ((navMesh.Polygons[j].Indices[navMesh.Polygons[j].Indices.Length - 1] == indexOne navMesh.Polygons[j].Indices[0] == indexTwo) ||
(navMesh.Polygons[j].Indices[navMesh.Polygons[j].Indices.Length - 1] == indexTwo navMesh.Polygons[j].Indices[0] == indexOne))
{
navMesh.Graph.AddEdge(polygon.Node, navMesh.Polygons[j].Node);
}
}
else
{
if ((navMesh.Polygons[j].Indices[k] == indexOne navMesh.Polygons[j].Indices[k + 1] == indexTwo) ||
(navMesh.Polygons[j].Indices[k] == indexTwo navMesh.Polygons[j].Indices[k + 1] == indexOne))
{
navMesh.Graph.AddEdge(polygon.Node, navMesh.Polygons[j].Node);
break;
}
}
}
}
}
}
I essentially loop through every polygon, checking every edge against every edge of every other polygon. All polygons are triangles and thus have 3 vertices/indices.
However, lines isnt generated between every pair of polygons.
Okay so I managed to solve it. Instead of comparing the indices I compare the vertices. I assume the indices isnt ordered in such a way that checking like this is gonna work.