Unity3d Creating mesh Dynamically (I.)

Source: Internet
Author: User

1.Mesh, Meshfilter, meshrenderer relationship Finishing

Create a cube in Unity3d, where inspector can see the Meshfilter, Meshrenderer components.

The meshfilter contains a public member of the Mesh.

Data for three-dimensional models are stored in mesh: vertices (vertex data array vector3[]), triangles (triangle Vertex index array, int[]), normals (normal vector array, vector3[]), UV (array of texture coordinates, Vector2[]).

2. Create a cube using mesh

Create a script Dyn3d.cs, become a component of main camera, click to run to see the dynamically generated cube

1 usingUnityengine;2 usingSystem.Collections;3 4  Public classDyn3d:monobehaviour {5 6     //Use this for initialization7     voidStart () {8     9 Createcube ();Ten     } One      A     //Update is called once per frame -     voidUpdate () { -  the     } -  -     voidCreatecube () -     { +  -Gameobject obj=NewGameobject ("Cube"); +Meshfilter Mf=obj. Addcomponent<meshfilter>(); AMeshrenderer Mr=obj. Addcomponent<meshrenderer>(); at          -          -Vector3[] Vertices=Newvector3[ -]; -         int[] triangles=New int[ $]; -  -         //forward invertices[0]. Set (0.5f,-0.5f,0.5f); -vertices[1]. Set (-0.5f,-0.5f,0.5f); tovertices[2]. Set (0.5f,0.5f,0.5f); +vertices[3]. Set (-0.5f,0.5f,0.5f); -         // Back thevertices[4]. Set (vertices[2].x,vertices[2].y,-0.5f); *vertices[5]. Set (vertices[3].x,vertices[3].y,-0.5f); $vertices[6]. Set (vertices[0].x,vertices[0].y,-0.5f);Panax Notoginsengvertices[7]. Set (vertices[1].x,vertices[1].y,-0.5f); -         // up thevertices[8]=vertices[2]; +vertices[9]=vertices[3]; Avertices[Ten]=vertices[4]; thevertices[ One]=vertices[5]; +         // Down -vertices[ A]. Set (vertices[Ten].x,-0.5f, vertices[Ten].z); $vertices[ -]. Set (vertices[ One].x,-0.5f, vertices[ One].z); $vertices[ -]. Set (vertices[8].x,-0.5f, vertices[8].z); -vertices[ the]. Set (vertices[9].x,-0.5f, vertices[9].z); -         // Right thevertices[ -]=vertices[6]; -vertices[ -]=vertices[0];Wuyivertices[ -]=vertices[4]; thevertices[ +]=vertices[2]; -         // Left Wuvertices[ -]. Set (-0.5f, vertices[ -].y,vertices[ -].z); -vertices[ +]. Set (-0.5f, vertices[ +].y,vertices[ +].z); Aboutvertices[ A]. Set (-0.5f, vertices[ -].y,vertices[ -].z); $vertices[ at]. Set (-0.5f, vertices[ -].y,vertices[ -].z); -          -         intCurrentcount=0; -          for(intI=0;i< -; i=i+4) A         { +triangles[currentcount++]=i; thetriangles[currentcount++]=i+3; -triangles[currentcount++]=i+1; $              thetriangles[currentcount++]=i; thetriangles[currentcount++]=i+2; thetriangles[currentcount++]=i+3; the              -         } in  themf.mesh.vertices=vertices; themf.mesh.triangles=triangles; About          the     } the}

Here the vertices array is defined to store vertex coordinate information, defining the triangles array storage triangle Vertex index. However, this mesh does not contain normals and UV information, so the displayed cube is a texture-free map and does not respond to ambient lighting.

3. Vertex coordinates for redundancy

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, according to TRIANGLES[0],TRIANGLES[14],TRIANGLES[17] The vertices that are indexed in vertices are (0.5,-0.5,0.5), but the normal vector values that are 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. )

4. 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 .

My vertex order in the program is, triangle 1:0->3->1, triangle 2:0->2->3.

1 intCurrentcount=0;2  for(intI=0;i< -; i=i+4)3 {4         //Triangle 15triangles[currentcount++]=i;6triangles[currentcount++]=i+3;7triangles[currentcount++]=i+1;8     //Triangle 29triangles[currentcount++]=i;Tentriangles[currentcount++]=i+2; Onetriangles[currentcount++]=i+3; A}

This code guarantees the 6 faces of the cube, and the 12 triangles are correctly rendered (front normal facing outwards).

Unity3d Creating mesh Dynamically (I.)

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.