Unity3d-mesh algorithm for creating a triangle Index

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.