Untiy Drawing Grid Mesh

Source: Internet
Author: User

About the drawing grid, rain pine predecessors have explained very in place, here I just porter, really feel myself to describe the words will not have a clear description of the rain, the article gradually, step by step Guide readers to understand the Unirty drawing mechanism, really is no better than this, the following is the original article:

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.

1 usingUnityengine;2 usingSystem.Collections.Generic;3 usingSystem;4  5  Public classTest:monobehaviour {6  7     voidStart ()8     {9         //gets the Meshfilter object, which is currently empty. TenMeshfilter Meshfilter = (meshfilter) gameobject.find (" Face"). Getcomponent (typeof(Meshfilter)); One         //get the corresponding mesh object AMesh mesh =Meshfilter.mesh; -   -         //coordinate array of triangle vertices thevector3[] vertices =Newvector3[3]; -         //triangle Vertex ID array -         int[] Triangles =New int[3]; -   +         //Triangle Three fixed-point coordinates, in order to show clearly ignore z-axis -vertices[0] =NewVector3 (0,0,0); +vertices[1] =NewVector3 (0,1,0); Avertices[2] =NewVector3 (1,0,0); at   -         //an array of triangles to draw vertices -triangles[0] =0; -triangles[1] =1; -triangles[2] =2; -   in         //Note 1 -Mesh.vertices =vertices; to   +Mesh.triangles =triangles; -   the     } *   $}

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.

Let's change the code so that we have a total of 4 triangles drawn on the screen.

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.

1 usingUnityengine;2 usingSystem.Collections.Generic;3 usingSystem;4  5  Public classTest:monobehaviour {6  7     //number of vertices in mesh model8     Private intVertices_count =6;9  Ten     voidStart () One     { A         //gets the Meshfilter object, which is currently empty.  -Meshfilter Meshfilter = (meshfilter) gameobject.find (" Face"). Getcomponent (typeof(Meshfilter)); -         //get the corresponding mesh object theMesh mesh =Meshfilter.mesh; -   -         //coordinate array of triangle vertices -vector3[] vertices =NewVector3[vertices_count]; +   -         //get the number of triangles +         intTriangles_count = Vertices_count-2; A   at         //triangle Vertex ID array -         int[] Triangles =New int[Triangles_count *3]; -   -         //Triangle Three fixed-point coordinates, in order to show clearly ignore z-axis -vertices[0] =NewVector3 (0,0,0); -vertices[1] =NewVector3 (0,1,0); invertices[2] =NewVector3 (1,0,0); -vertices[3] =NewVector3 (1,1,0); tovertices[4] =NewVector3 (2,0,0); +vertices[5] =NewVector3 (2,1,0); -   the          //Draw Triangles *Mesh.vertices =vertices; $  Panax Notoginseng         //Start triangle vertex -         intStart =0; the   +         //ends the vertices of a triangle A         intEnd =3; the   +          for(inti = start; I <end; i++) -         { $              for(intj =0; J <3; J + +) $             { -                 if(i%2==0) -                 { thetriangles[3*i + j] = i +J; -}ElseWuyi                 { thetriangles[3*i + j] = i +2-J; -                 } Wu   -             } About         } $   -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.

The above content refers to the article of the predecessor of the Rain Pine, in which the beginning of the removal of some introductory content, in addition, for easy to understand the use of the picture is different.

For example, to draw 4 triangles, there will be 4+2= 6 key points, each triangle has three vertices. Then need to 3*4=12 a triangle vertex, but only 6 points, in fact, some points need to be used 2 times, some points were used three times;

Three points to determine a face. These three dots are connected in a clockwise direction, such as:

In the case of the comparison rules are only for the sake of understanding, the truth is understood.

Look at the predecessors of the blog, I feel the algorithm is how great, not to defy. Only the final script of the double-layer for loop content let me understand for a long time, only to understand that the algorithm is very subtle. Do not know whether the predecessor of the original, haha ~ ~ ~.

About the internal if else algorithm for the FOR loop I really admire it. It's still in the cult of the flower 、、、、、

Welcome you and I study together, progress together, ~ ~ Feel big, should sleep, tomorrow also to work ~ ~

Untiy Plot Mesh mesh

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.