Explain the triangular indexing algorithm for mesh in the previous article.
The first thing to know is how the vertex array is generated:
So the figure, a large rectangle, is made up of 6 vertices and two rectangles.
In general, building this rectangle requires knowing the position of each vertex and the relationship between the vertices and vertices.
Ok
Initialize vertex position private void Initvertexpos () { int currentindex = 0; for (int i = 0, i < constnumber.ylength; i++) {for (int j = 0; J < Constnumber.xlength; J + +) { vert Ices[currentindex] = new Vector3 (j, 0, I); currentindex++;}}}
This step is to say that the vertex position is arranged in a certain order into the array
So the index array is the 0,1,2,3 ... these vertex array index values.
It is known that the triangle has 3 vertices, so each of the 3 is a group, constructing a triangle.
The rectangle is produced by two triangles, which can be divided or perpendicular to the yellow line.
Well, we'll set it in a clockwise direction (note that either full clockwise or all counterclockwise)
Get 0,3,4,0,4,1,1,4,5,1,5,2
The algorithm implemented is
//Initialize triangle index private void Inittriangles () {///represents the current index value of the TRIANGL array, with each value added to the array, currentindex 1 int currentindex = 0; for (int i = 0, i < constnumber.ylength-1; i++) {for (int j = 0; J < Constnumber.xlen Gth-1; J + +) {//Draw the upper left corner triangle triangles[currentindex++] = (i + 0) * Constnumber.xlength + (j + 0); triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 0); triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 1); Draw clockwise triangle triangles[currentindex++] = (i + 0) * Constnumber.xlength + (j + 0); triangles[currentindex++] = (i + 1) * Constnumber.xlength + (j + 1); triangles[currentindex++] = (i + 0) * Constnumber.xlength + (j + 1); } } }
This is an implementation method, which is analyzed by the whole rectangle.
I also wrote another index algorithm that expresses the entire rectangle in the lower-left corner of the rectangle.
Because you know a point of a rectangle with a rectangular unit length of 1, you can know where the other points are.
OK, look at the algorithm
Initialize triangle index private void Inittriangles () { float xdelta = 1.0f/(float) (constnumber.xlength-1); float Ydelta = 1.0f/(float) (constnumber.ylength-1); int currentposnum = 0; for (int i = 0, i < constnumber.ylength; i++) {for (int j = 0; J < Constnumber.xlength; J + +) { ver Tices[currentposnum] = new Vector3 (J * Xdelta, 0, I * ydelta); currentposnum++;}}}
This kind of thing, just traverse through all the vertices (and some vertices do not need to traverse) only one layer for the OK.
However, its shortcomings are as obvious as its merits, that is, the length must be greater than the width.
In fact, it can be made up, but today there are too many things, I only record it ~ ~ ~
Unity3d-mesh algorithm for creating a triangle Index