- Home /
Fetching triangles from a 3D Mesh
A question derived from http://answers.unity3d.com/questions/46662/how-would-one-calculate-a-3d-mesh-volume-in-unity
As @yoyo commented there - "Get point i for triangle j with p = mesh.vertices[mesh.triangles[i + 3 * j]] -- i is 0, 1, 2 for the three corners of the triangle, and j goes from 0 to mesh.triangles.length/3 - 1."
Basicly, this is what I have made so far for triangle fetching, but it looks like the function isn't working as it should:
// function to gather all the triangles on mesh private function GatherTriangles(mesh : Mesh, triangle : int){ var p : Vector3 = Vector3.zero; for(var i = 0; i < 3; i++){ p = mesh.vertices[mesh.triangles[i + 3 * triangle]]; } return p; }
// Start function, executes gathering, counts results and stuff function Start () { var mesh : Mesh = GetComponent(MeshFilter).mesh; for(var n = 0; n < mesh.triangles.length; n++){ trianglePoints.Push(GatherTriangles(mesh, j)); j = (mesh.triangles.length / 3) - 1; } print(trianglePoints); // print #1 print(trianglePoints.length); // print #2 }
Result:
print #1: (0.0, 0.0, 0.3),(0.0, 0.0, 0.4),(0.0, 0.0, 0.4) and continues so on, no values are changed anymore.
print #2: 12060
Used on Standard Assets -> Construction Worker Mesh, has 4020 tris (4020 * 3 = 12060, so there is some kind of relation between all that hassle there).
Actually, have no idea what is causing this mess, and I have no idea what should be the format for p
.
Hope you guys can help me, thanks in advance!
GatherTriangles returns a single Vector3 -- it takes three such points to make a triangle. And when you call GatherTriangles, you keep passing in the same value of j.
Answer by Statement · Mar 17, 2011 at 12:35 AM
Given you have a mesh, you can get each individual corner of the triangles like this:
for (int i = 0; i < mesh.triangles.Length; i += 3)
{
Vector3 p1 = mesh.vertices[mesh.triangles[i + 0]];
Vector3 p2 = mesh.vertices[mesh.triangles[i + 1]];
Vector3 p3 = mesh.vertices[mesh.triangles[i + 2]];
}
See also my answer in your previous topic, it contain full source code for your problem.
The triangles property contain the indices to the vertices. So we go through each triplet of indices to look up the vertex index. We then use the vertex index to find the actual vertex.
Just to clarify what is returned from the triangles and vertices, consider this small example:
// Get the first vertex index which is the first vertex in the first triangle. int vertexIndex = mesh.triangles[0];
// Then get the vertex from a vertex index. Vector3 vertex = mesh.vertices[vertexIndex];
I have a doubt regarding the triangles of a mesh. is the order of vertices is correct because in my mesh when I try to form a triangle mesh with the first three vertices, the vertex0 and vertex2 have the same value. How can I get the triangles?
The triangles are defined in the triangle array, not the vertices array. The vertices in the vertices array can be in any order. The triangle array contains indices which define which vertices form a triangle. The index values in the triangle array are in order. So always 3 consecutive index values form one triangle. For example the triangle array could contain the values 0,5,7,3,2,4, ...
That means the first triangle is made up of the vertices at index 0, 5 and 7. The second triangle is made up of the vertices 3, 2 and 4 in that order.
Thank you @Bunny83 for the clarification. I want to know the indices of the triangles. I have 2112 vertices with 200 different vector values? how can I get the triangle indices and how can know the face of the vertex like which vertex is facing which side??
Answer by DaveA · Mar 17, 2011 at 12:25 AM
One thing off the bat is your gather func will always return just the last vertex in the given triangle
I'm aware of that, but previously I used a different approach for it, and results didn't change. I'm more interested in how to populate the correct coordinates for those triangle vertices.
Your answer
Follow this Question
Related Questions
How do i create a Mesh in the middle of a Catmull spline? 0 Answers
How does the Triangles work on Mesh? 1 Answer
Determining where a mesh triangle faces 1 Answer
Connecting flatshaded vertices 0 Answers
Triangles on my mesh don't show 1 Answer