Unity3d Mesh (iv) drawing polygons

Source: Internet
Author: User

Objective:

The same as the previous basic introduction, so the relevant knowledge point does not do baggage, only on the case, knowledge as a test for their own records, and then to review.

A description of some of the knowledge points is recorded and supplemented:

1.

In general, the idea of drawing a plane is very simple, is to split the required plane into a few triangles and then to draw it, the main idea is to split the triangle. If a plane has 7 vertices, we have them numbered 0 to 6, divided by:

That is: If you use N to represent the number of vertices, then in the same plane, the number of triangles that can be divided is: n-2;

2.

Here we choose to apply the mesh filter component to draw, the mesh property in the mesh filter component is the object of our main operation, here we use:

Mesh.vertices arrays and Mesh.triangles arrays, the first is an array of Vector3, and the second is an array of int.

Where Mesh.vertices stores the vertex information of the plane, corresponding to the coordinates of the six points of the 0 to 6th numbers.

The Mesh.triangles stores the vertex order of the triangle when the plane is drawn, corresponding to the following:

061 651 521 542 432 (clockwise)

Each of the three groups represents a triangle, but everyone here to pay attention, that is, the final plot of the small triangle is a one-way map, is seen on the other side is not visible, so, in order to ensure that all the small triangle toward one to, To adjust the Mesh.triangles array, adjust the result as follows:

016 156 125 245 234 (counterclockwise)

is to ensure that the vertices of the small triangle are clockwise or counterclockwise reading ~ Everyone think it is understood ~

Therefore: The basic algorithm idea is:

Entry parameters: vector3[] vertices, storing the plane fixed-point information, the vertices need to be stored sequentially

Algorithm idea: Iterate from the end of the array to the middle, generating an array of triangles vertex IDs (the part of the comments in the following code)

Step:

1, create a empty gameobject;

2. Add a script to this game object;

The algorithm implementation code is as follows:

usingUnityengine;usingSystem.Collections; [Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))] Public classquad:monobehaviour{/*creat a triangle by using Mesh 2016/11/21 ———— Carl*/    voidStart () {Creatpolygon (); }       Private voidCreatpolygon () {/*1. Vertices, triangles, normals, UV coordinates, absolutely necessary parts only vertices and triangles. If the light in the scene is not required in the model, no normals are required. If the model does not require a material to be pasted, then no UV*/vector3[] vertices=        {         NewVector3 (2f,0,0),         NewVector3 (4f,0,0),         NewVector3 (6f,0,0),         NewVector3 (10f,0,0),         NewVector3 (10f, 20f,0),         NewVector3 (6f,10f,0),         NewVector3 (4f, 4f,0)                }; Vector3[] Normals={vector3.up, vector3.up, Vector3.up, Vector3.up, Vecto        R3.up, Vector3.up, vector3.up}; Vector2[] UV={Vector2.zero,-Vector2.left, Vector2.one, Vector2.right, Vector2.zero,-vector2.left, Vector2.one}; /*2. Triangle, vertex index: The triangle is determined by 3 integers, and each integer is the index of the vertex of the corner. The order of the vertices of each triangle is usually from the bottom up, either clockwise or counterclockwise, usually depending on which direction we look at the triangle. Typically, a "counterclockwise" face is blocked when the mesh is rendered. We want to ensure that the clockwise face is consistent with the primary of the normal*/        int[] Indices =New int[ the]; indices[0] =0; indices[1] =6; indices[2] =1; indices[3] =6; indices[4] =2; indices[5] =1; indices[6] =6; indices[7] =5; indices[8] =2; indices[9] =5; indices[Ten] =4; indices[ One] =2; indices[ A] =4; indices[ -] =3; indices[ -] =2; //int numberoftriangles = vertices. Length-2;//the number of triangles equals the number of vertices minus 2.//int[] Indices = new int[numberoftriangles * 3];//triangles array size is equal to the number of triangles multiply by 3 this is//int f = 0, B = vertices. Length-1;//F record the first half of the Traverse position, B records the second half of the Traverse position is 0-7//for (int i = 1; I <= numberoftriangles; i++)//assigns a value to three elements in the triangles array each time//{ //numberoftriangles Times//if (i% 2 = = 1)//    {        //indices[3 * i-3] = f++; //indices[3 * I-2] = f; //indices[3 * I-1] = b;//forward assignment, for I=1 assignment: 0,1,2//    }        //Else//    {        //indices[3 * I-1] = b--; //indices[3 * I-2] = b; //indices[3 * i-3] = f;//inverse assignment, for i=2 assignment: 1,5,6//    }Mesh Mesh=NewMesh (); Mesh.vertices=vertices; Mesh.normals=normals; Mesh.uv=UV; Mesh.triangles=indices; Meshfilter Meshfilter= This.gameobject.getcomponent<meshfilter>(); Meshfilter.mesh=mesh; }    }

"Welcome to Reprint"

Reprint please indicate source: le study

Unity3d Mesh (iv) drawing polygons

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.