- Home /
How does the Triangles work on Mesh?
Hello i having been searching about Triangles on mesh but i have found "0" things about them there is no tutorial for triangles for mesh in Unity, so i have some code below and if you can tell me how it works, that would be helpful.
So i have this Triangle code from here
int ti = 0;
for( int i = 0; i < segments; i++ ) {
int offset = i * vertsInShape;
for ( int l = 0; l < lines.Length; l += 2 ) {
int a = offset + lines[l] + vertsInShape;
int b = offset + lines[l];
int c = offset + lines[l+1];
int d = offset + lines[l+1] + vertsInShape;
triangleIndices[ti] = a; ti++;
triangleIndices[ti] = b; ti++;
triangleIndices[ti] = c; ti++;
triangleIndices[ti] = c; ti++;
triangleIndices[ti] = d; ti++;
triangleIndices[ti] = a; ti++;
}
}
And i am trying to change it to this, but obviously it does not work
int ti = 0;
for (int i = 0; i < segments; i++)
{
int offset = i * vertsInShape;
for (int l = 0; l < origVerts.Length; l += 6)
{
int a = offset + (stretch * l) + vertsInShape;
int b = offset + (stretch * l);
int c = offset + (stretch * l + 1);
int d = offset + (stretch * l + 1) + vertsInShape;
triangleIndices[ti] = c; ti++;
triangleIndices[ti] = b; ti++;
triangleIndices[ti] = a; ti++;
triangleIndices[ti] = a; ti++;
triangleIndices[ti] = d; ti++;
triangleIndices[ti] = c; ti++;
}
}
And this is the error i get with my code. "Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 48, VertexCount: 20 UnityEngine.Mesh:set_triangles(Int32[]) Extruder:Generate() (at Assets/Extruder.cs:73) Extruder:Start() (at Assets/Extruder.cs:23)"
But if you need the full code to know what i am doing with the code just comment below.
Answer by DiegoSLTS · Aug 25, 2016 at 01:08 AM
Well, the error is pretty clear, you've set a triangle vertex index greater than the actual vertex count for that mesh. In your case, you're setting one of the indices of some triangle to the vertex 48, but your mesh only has 20 vertices.
The problem is that you're incrementing "l" by 6, but the code inside the for only works with some index and the next. This means that you're ignoring a lot of vertices between each for iteration, and you're moving outside the valid vertices index at some point.
I'm not sure what you're trying to do, but as the error message says, you must use valid vertex indices for your triangles.
A triangle in the context of meshes is just a group of 3 vertices, but instead of having each vertex position for each triangle, a mesh has a list of vertices, and the triangles just store the index for the 3 vertices in that vertices array. This is to avoid vertex position to be replicated for each triangle that shares that vertex. Note that this is not some Unity thing, it's the most common method to represent meshes as data. I'd say it's the standard method, but I'm not familiar with all the 3D formats.
Ok... Anyway does not make much sense, but i am looking into it.
Thanks @Nose$$anonymous$$ills that did make an explanation for some things.
The first code(box) above where it says lines[](Which is an int[]) how would i be able to convert that to Vector3[] or something that can calculate all the triangles on every vertice?
Your answer
Follow this Question
Related Questions
How do i create a Mesh in the middle of a Catmull spline? 0 Answers
How do i access the MeshExtrusion.cs from Unity's PE? 1 Answer
How do i have a Vector3[] position be in another Vector3[] position (That Updates)?? 1 Answer
How do i cast mesh like a linerenderer? 0 Answers
How do i modify a mesh Filter(C#)? 2 Answers