You should know that any polygon in the 3D world is done by a triangle, because any irregular set of shapes can be made up of triangles. For example, the four-sided shape, either the quadrilateral or the irregular quadrilateral can be composed of two triangles. Combined with the title of this article to think about it, if you need to draw a dynamic irregular surface only need to get the dynamic two locus point can be, then combined with the picture below we carefully think about.
for the time being we ignore the z-axis (which is clearer in the plane), assuming that the z-axis coordinates are 0. Suppose that there are two locus points in the game to increase and change dynamically, and finally the two points change the trajectory of the stitching together is their generated surface. As shown, the trajectory of the first point is "3,4,5,6,7" and the trajectory of the second point is "2,1,10,9,8". The lengths of the two points are variable, provided that the number of both of them must be exactly the same. Then, as shown, we connect these points 22 together, and now we have a total of 8 triangular faces (the number of triangular polygons for the entire mesh surface can be determined based on the number of two dynamic points). Finally, we fill the 8 triangles with the same color, and we can achieve a complete stereoscopic mesh surface.
The principle is very simple, that is, I believe you can see here that everyone understands, and then we learn how to use the code to implement it. First create the Unity project, then create an empty game object, then bind the mesh filter component with the mesh renderer component to the game object.
Mesh Filter Component: Represents a mesh face, which is a polygon that we use to stitch together all the triangles.
Mesh Renderer Component: Represents a rendering of a mesh that can be set to a rendered material that includes maps and colors.
As shown, I'm talking about the more important attributes inside. Mesh renderer, the Materials drop-down list allows you to set the material for the mesh model, where we set a red material. Mesh Filter: Currently none, and it is not necessary to assign a value to it in the editor, because this grid model is generated and assigned in the code. In the following is the square just we set the red material resources, shader set the properties of the map, is currently gui/textshader. It indicates that the rendering level of this material is on the GUI, which is the first level of precedence. For example, no matter how many models are drawn in front of the grid model, it will always be displayed first. For this example, its existence is not necessary, in fact, there are many options shader, transparent, non-transparent, mirror, reflection and so on, later I will be detailed to you.
OK, now that the resource files are ready, let's learn how to draw a triangle from the simple beginning. Bind the following code to the camera object.
voidStart () {//gets the Meshfilter object, which is currently empty. Meshfilter Meshfilter = (meshfilter) gameobject.find (" Face"). Getcomponent (typeof(Meshfilter)); //get the corresponding mesh objectMesh mesh =Meshfilter.mesh; //coordinate array of triangle verticesvector3[] vertices =Newvector3[3]; //triangle Vertex ID array int[] Triangles =New int[3]; //Triangle Three fixed-point coordinates, in order to show clearly ignore z-axisvertices[0] =NewVector3 (0,0,0); vertices[1] =NewVector3 (0,1,0); vertices[2] =NewVector3 (1,0,0); //an array of triangles to draw verticestriangles[0] =0; triangles[1] =1; triangles[2] =2; Mesh.vertices=vertices; Mesh.triangles=triangles; }
There are two very important concepts in the code, namely the triangle Vertex array and the coordinate array. Let's start with the coordinate array, assuming that you need to draw a quadrilateral, the length of the triangle coordinate array should be 4, which holds the coordinates of the quadrilateral four vertices. Then is the vertex array, the four-sided shape is composed of two triangles, but a triangle is composed of 3 vertices, two triangles should be 6 vertices, no matter how many triangles their structure should be and so on.
Note 1: Here is the assignment of the vertex array of the model to the grid model, remember that when the mesh filter was created , the grid model was not assigned in the editor at the time, and actually the code went here to re-assign the Meshfilter to the mesh model. Then the triangles we draw in the code are displayed in the screen.
, the triangles have been drawn in the screen.
The figure in the array 0 1 2 represents the ID of the three vertices of the triangle. The ID corresponds to the coordinates of the vertices array index vertex in the code. That is to say (0,0,0) corresponds to Vertex 0, (0,1,0) corresponds to vertex 1, (1,0,0) corresponds to Vertex 2
Let's change the code so that we have a total of 4 triangles drawn on the screen.
//number of vertices in mesh model Private intVertices_count =6; voidStart () {//gets the Meshfilter object, which is currently empty. Meshfilter Meshfilter = (meshfilter) gameobject.find (" Face"). Getcomponent (typeof(Meshfilter)); //get the corresponding mesh objectMesh mesh =Meshfilter.mesh; //coordinate array of triangle verticesvector3[] vertices =NewVector3[vertices_count]; //get the number of triangles intTriangles_count = Vertices_count-2; //triangle Vertex ID array int[] Triangles =New int[Triangles_count *3]; //Triangle Three fixed-point coordinates, in order to show clearly ignore z-axisvertices[0] =NewVector3 (0,0,0); vertices[1] =NewVector3 (0,1,0); vertices[4] =NewVector3 (2,0,0); vertices[2] =NewVector3 (1,0,0); vertices[5] =NewVector3 (2,1,0); vertices[3] =NewVector3 (1,1,0); //an array of triangles to draw verticestriangles[0] =0; triangles[1] =1; triangles[2] =2; triangles[3] =3; triangles[4] =2; triangles[5] =1; triangles[6] =2; triangles[7] =3; triangles[8] =4; triangles[9] =3; triangles[Ten] =5; triangles[ One] =4; //Draw TrianglesMesh.vertices =vertices; Mesh.triangles=triangles;}
The number of vertices of a known model, the number of vertices minus 2 is the number of triangles, and the number of triangles multiplied by 3 is the number of triangle vertices. According to this formula, we know that the above code plots 4 triangles, the vertex coordinate array should be 6, and the vertex ID array should be 12. Multiple triangles are arranged in a vertex ID array in a special way, and you need to record them carefully before you can draw the correct triangles. As shown, because I do not have a suitable 3D coordinate point, the triangle is used to join a positive quadrilateral, the four-sided shape is composed of 6 vertices 4 small triangles, see here the clear-minded friends should understand the rule of the principle of the rules of the four-sided shape and it is exactly the same. You only need to pass in the appropriate 3D coordinate point.
Based on the above logic, we modify the algorithm. Assuming that the vertex coordinates of the triangle are any number, we need to calculate the array contents of the corresponding vertex IDs more based on the number of vertex coordinates. In the For loop, the meaning of start =0 and end =3 is to draw a vertex that is indexed to 0 from the vertex coordinate array to start drawing to a vertex with an array index of 3, which is where 3 triangles are drawn from 0 to 3.
//number of vertices in mesh model Private intVertices_count =6; voidStart () {//gets the Meshfilter object, which is currently empty. Meshfilter Meshfilter = (meshfilter) gameobject.find (" Face"). Getcomponent (typeof(Meshfilter)); //get the corresponding mesh objectMesh mesh =Meshfilter.mesh; //coordinate array of triangle verticesvector3[] vertices =NewVector3[vertices_count]; //get the number of triangles intTriangles_count = Vertices_count-2; //triangle Vertex ID array int[] Triangles =New int[Triangles_count *3]; //Triangle Three fixed-point coordinates, in order to show clearly ignore z-axisvertices[0] =NewVector3 (0,0,0); vertices[1] =NewVector3 (0,1,0); vertices[2] =NewVector3 (1,0,0); vertices[3] =NewVector3 (1,1,0); vertices[4] =NewVector3 (2,0,0); vertices[5] =NewVector3 (2,1,0); //Draw TrianglesMesh.vertices =vertices; //Start triangle vertex intStart =0; //ends the vertices of a triangle intEnd =3; for(inti = start; I < end; i++) { for(intj =0; J <3; J + +) { ifI2==0) {triangles[3* i + j] = i +J; } Else{triangles[3* i + j] = i +2-J; } }} mesh.triangles=triangles;}
As shown, a total of 3 triangles are drawn based on the above logic algorithm, and the vertex coordinate ID is from 0 to 3. Speaking of this, please think carefully about the title of this article, in fact, the two dynamic trajectory of the point is to maintain the triangles vertex coordinate array. Triangles[0],triangles[2],triangles[4] ... Represents the value of a locus point,triangles[1],triangles[3],triangles[5] ... Represents the value of another trajectory point, and eventually joins them through the algorithm above, which is the dynamic two-point trajectory to draw the surface.
Unity3d In fact very fun, easy to use although very simple, but want to go deep in fact and not so easy, today this article of thought has been written, if still not understand the friend please carefully think about the triangle and the quadrilateral but don't, wow ka ka, already not early I also have to sleep, tomorrow also have to work Gogogogogo Refueling ~ hope that we can all learn together and progress together.
- This article fixed link: http://www.xuanyusong.com/archives/780
- Reprint Please specify: Rain pine Momo May 06, 2012 Yu Yussong Momo Program Research Institute published
"Reprint" Unity3d Research Institute and the detailed description of the two trajectory points based on the dynamic