Unity3d Mesh (vii) Draw box

Source: Internet
Author: User
Tags draw box

Objective:

From now on, finally feel into a little bit of a point! Dynamic creation of three-dimensional three-dimensional model mesh! still starts with a simple: cuboid.

First, the basic idea

Since the box mesh was created, the previous study found that two data had to be, namely: Vertex data: vertices and indexed triangles (that is, the index order of three points responsible for managing each triangle): triangles. Box: Generally know: the length and width of the high, that today we are from the length and width of the parameters to get vertices and triangles.

Ii. framework of basic procedures

Create an empty gameobject that hangs in the script.

The basic framework can be achieved by the basic idea, then the function function can be realized;

1 usingUnityengine;2 3[Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))]4  Public classCube_mesh:monobehaviour5 {6      Public floatLength =5;//The length of the box7      Public floatWidth =6;//width of Box8      Public floatHeigth =7;//the height of the box9     PrivateMeshfilter Meshfilter;Ten  One     voidStart () A     { -Meshfilter = getcomponent<meshfilter>(); -Meshfilter.mesh =Createmesh (Length, Width, heigth); the     } -  -Mesh Createmesh (floatLengthfloatWidthfloatheigth) -     { +         //vertices (vertex, required): -         //......... +  A         //triangles (index triangle, required): at         //......... -  -         //UV: -         //......... -  -         //load properties and mesh inMesh mesh =NewMesh (); -         //......... to         returnmesh; +     } -}

Third, the implementation of the drawing function and the entire program code

1 usingUnityengine;2 3[Requirecomponent (typeof(Meshrenderer),typeof(Meshfilter))]4  Public classCube_mesh:monobehaviour5 {6      Public floatLength =5;//The length of the box7      Public floatWidth =6;//width of Box8      Public floatHeigth =7;//the height of the box9     PrivateMeshfilter Meshfilter;Ten  One     voidStart () A     { -Meshfilter = getcomponent<meshfilter>(); -Meshfilter.mesh =Createmesh (Length, Width, heigth); the     } -  -Mesh Createmesh (floatLengthfloatWidthfloatheigth) -     { +  -         //vertices (vertex, required): +         intVertices_count =4*6;//Number of vertices (4 points per polygon, six faces) Avector3[] vertices =NewVector3[vertices_count]; atvertices[0] =NewVector3 (0,0,0);//The point in the lower left corner of the front -vertices[1] =NewVector3 (0, Heigth,0);//The point in the upper-left corner of the front -vertices[2] =NewVector3 (length,0,0);//The point in the lower right corner of the front -vertices[3] =NewVector3 (length, Heigth,0);//The point in the upper-right corner of the front -  -vertices[4] =NewVector3 (length,0, width);//The point in the lower right corner of the back invertices[5] =NewVector3 (length, heigth, width);//The point in the upper right corner of the back -vertices[6] =NewVector3 (0,0, width);//The point in the lower left corner of the back tovertices[7] =NewVector3 (0, heigth, width);//The point in the upper left corner of the back +  -vertices[8] = vertices[6];//left thevertices[9] = vertices[7]; *vertices[Ten] = vertices[0]; $vertices[ One] = vertices[1];Panax Notoginseng  -vertices[ A] = vertices[2];//Right thevertices[ -] = vertices[3]; +vertices[ -] = vertices[4]; Avertices[ the] = vertices[5]; the  +vertices[ -] = vertices[1];//on -vertices[ -] = vertices[7]; $vertices[ -] = vertices[3]; $vertices[ +] = vertices[5]; -  -vertices[ -] = vertices[2];//under thevertices[ +] = vertices[4]; -vertices[ A] = vertices[0];Wuyivertices[ at] = vertices[6]; the  -  Wu         //triangles (index triangle, required): -         intNumber of Split triangles =6*2; About         intTriangles_cout = number of Split triangles *3;//Index point number of index triangle $         int[] Triangles =New int[Triangles_cout];//Indexed triangle Array -          for(intI=0, vi=0;i< triangles_cout;i+=6, vi+=4) -         { -Triangles[i] =VI; Atriangles[i+1] = vi+1; +triangles[i+2] = vi+2; the  -triangles[i+3] = vi+3; $triangles[i+4] = vi+2; thetriangles[i+5] = vi+1; the  the         } the  -         //UV: in         //......... the  the         //load properties and mesh AboutMesh mesh =NewMesh (); theMesh.vertices =vertices; theMesh.triangles =triangles; the         returnmesh; +     } -}

Four

V. Other relevant notes

1, redundant vertex coordinates

The cube consists of 6 polygons, each of which consists of 2 triangles, so a total of 36 triangle vertex indexes are required. But the cube has only 8 vertices, why do I need 24 vertex coordinate data?

The answer is: Unity3d's mesh.triangles is a triangular index array, which not only relies on this index value to index triangle vertex coordinates, but also index texture coordinates, index normal vectors. That is, each vertex of a cube participates in 3 planes, and the normal vectors of the 3 planes are different, and the vertices need to be indexed to different normal vectors when rendering the 3 planes. And because vertex coordinates and normal vectors are obtained by the same index value triangles[index], for example, the vertices that are indexed in vertices according to vertices[0],vertices[10],vertices[22] are (0,0,0), However, the normal vector values indexed in normals are different. This determines that a vertex in the cube requires 3 copies of the storage. (If you need to create other models, you need to determine the redundancy of vertex coordinates according to the actual situation.) In essence, the redundancy of vertex coordinates is convenient for the access of normal coordinates and texture coordinates. )

2. Rendering of triangles

Guideline: The triangle has two sides, the front is visible, the back is not visible . The rendering order of triangles and the front normals of the triangles are left-handed spirals .

This determines that if we need to render the following square face, we need to make sure that the front normals of the two small triangles that make up the square are pointing to the outside of the screen .

The order of vertices in the program is, triangle 1:0--->1--->2, triangle 2:3--->2--->1.

"Welcome to Reprint"

Reprint please indicate source: le study

Unity3d Mesh (vii) Draw box

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.