So I have a script set up to generate a 16X16 grid of vertices. I want to loop through them and set up the triangles between the vertices to make it into a solid mesh plane. I am having trouble wrapping my head around the numbers I need to use for the triangles, I keep ending up with really weird results. What sort of equation would I use so that I can loop through and set up the triangle array?
Assuming widthResolution and lengthResolution are the number of verts:
if you WANT a 2x2 grid of quads, that means 3x3 verts, with 2x2x6 tri indices.
This also assumes that your verts are indexed like
6 7 8
3 4 5
0 1 2
int[] tris = new int[( widthResolution - 1 ) * ( lengthResolution - 1 ) * 6];
for ( int y = 0; y < lengthResolution - 1; y++ ) {
for ( int x = 0; x < widthResolution - 1; x++ ) {
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 0] = y * widthResolution + x;
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 1] = ( y + 1 ) * widthResolution + x;
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 2] = y * widthResolution + x + 1;
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 3] = y * widthResolution + x + 1;
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 4] = ( y + 1 ) * widthResolution + x;
tris[( ( y * ( widthResolution - 1 ) ) + x ) * 6 + 5] = ( y + 1 ) * widthResolution + x + 1;
}
}
Thank you, that helped a lot!