Unity3d Research Institute and graphic details based on two dynamic trajectory points (20)

Source: Internet
Author: User
If you need to repost the original article, please note: Repost from Yusong Momo program Research Institute link address: unity3d Research Institute and detailed description of Drawing Based on two dynamic trajectory points (20)

You should know that any surface in the 3D world is drawn by a triangle, because any irregular set of images can be made up of triangles. For example, a quadrilateral, whether positive or irregular, can be spliced by two triangles. Let's take a closer look at the title of this article. If you need to draw a dynamic and rule-free surface, you only need to get two dynamic track points. Then, let's take a closer look at the following picture.


(Click an image to view the large image)

For the moment, we ignore the Z axis (which makes it clearer in the plane), assuming that the Z axis coordinates are all 0. Assume that there are two track points dynamically increasing and changing in the game, and finally splicing the tracks of these two points is what they generate. As shown in, the trajectory of the first vertex is "3, 4, 5, 6, 7". the trajectory of the second vertex is "2, 1, 10, 9, 8 ". The length of the two points is variable, provided that the number of the two points must be exactly the same. Next, as shown in, we connect these points to each other. At present, a total of eight triangle surfaces are formed (the number of triangles on the entire mesh surface can be determined based on the number of two dynamic points ). Finally, we can fill these eight triangles with the same color to implement a complete three-dimensional mesh surface.

 

(Click an image to view the large image)

The principle is very simple. I believe everyone can understand it here, and then we will learn how to use code to implement it. Create a unity project, create an empty game object, and bind the Mesh Filter component to the game object with the mesh Renderer component.

Mesh Filter component: it indicates the mesh surface, which is generated by splicing all triangles using code.

Mesh Renderer component: it indicates the rendering of the mesh. You can set a rendering material, including textures and colors.

As shown in, let me talk about the important attributes. In mesh Renderer, you can set the mesh model material in the materials drop-down list. At this time, we have set a red material. Mesh Filter: It is currently none, and no need to assign values to it in the editor, because this mesh model is generated and assigned values in the code. In the following section, we set the red material resource. The texture attribute is set in the shader, which is currently GUI/textshader. It indicates that the rendering level of this material is on the GUI, that is, the highest priority. For example, no matter how many models are drawn before the grid model, it will always be displayed at the beginning. In this example, its existence is not necessary. In fact, there are many options for Shader, such as transparency, non-transparency, mirroring, and reflection. I will give you a detailed explanation later.

 

OK. Now all the resource files are ready. Next we will learn how to draw a triangle from the simple start .. Bind the following code to the camera object.

Using unityengine; using system. Collections. Generic; using system; public class test: monobehaviour {void start () {// get the meshfilter object, which is empty currently. Meshfilter = (meshfilter) gameobject. find ("face "). getcomponent (typeof (meshfilter); // obtain the mesh object mesh = meshfilter. mesh; // coordinate array of triangle vertices vector3 [] vertices = new vector3 [3]; // array of triangle vertex IDs int [] triangles = new int [3]; // coordinate of the triangle at three fixed points. To clearly ignore the Z axis vertices [0] = new vector3 (, 0); vertices [1] = new vector3 (, 0 ); vertices [2] = new vector3 (, 0); // array of triangle-drawn vertex triangles [0] = 0; triangles [1] = 1; triangles [2] = 2; // comment 1mesh. vertices = vertices; mesh. triangles = triangles ;}}


Note 1: Assign the vertex array and coordinate array of the model to the mesh model.Mesh Filter, the mesh model was not assigned a value in the editor at that time. In fact, when the Code comes here, it will re-assign a value to the mesh model meshfilter, the triangle we draw in the code will be displayed on the screen. There are two very important concepts in the Code: triangle vertex array and coordinate array. Let's talk about the coordinate array first. Suppose we need to draw a quadrilateral. At this time, the length of the triangle coordinate array should be 4, which stores the coordinates of the four vertices of the Quadrilateral. Then there is the vertex array. The quadrilateral is composed of two triangles. However, a triangle is composed of three vertices, and the two triangles should be composed of six vertices, no matter how many triangles they are structured, and so on.

The triangle has been drawn on the screen. The array 0 1 2 in the figure indicates the IDs of the three vertices of the triangle. This ID corresponds to the vertices array index vertex coordinates in the code.

 

 

Next, let's modify the code to draw a total of four triangles on the screen.

Using unityengine; using system. collections. generic; using system; public class test: monobehaviour {// Number of mesh model vertices private int vertices_count = 6; void start () {// get the meshfilter object, which is currently empty. Meshfilter = (meshfilter) gameobject. find ("face "). getcomponent (typeof (meshfilter); // obtain the mesh object mesh = meshfilter. mesh; // coordinate array of triangle vertices vector3 [] vertices = new vector3 [vertices_count]; // obtain the number of triangles int triangles_count = vertices_count-2; // array of triangle vertex IDs int [] triangles = new int [triangles_count * 3]; // coordinate of Three triangle points, to clearly ignore the Z axis vertices [0] = new vector3 (, 0); vertices [1] = new vector3 (, 0 ); vertices [2] = new vector3 (, 0); vertices [3] = new vector3 (, 0); vertices [4] = new vector3 (, 0 ); vertices [5] = new vector3 (, 0); // array of triangle-drawn vertex triangles [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] = 5; triangles [10] = 4; triangles [11] = 3; // draw a triangle mesh. vertices = vertices; mesh. triangles = triangles ;}}

 

Number of vertices of a known model. The number of vertices minus 2 indicates the number of triangles. The number of triangles multiplied by 3 indicates the number of triangle vertices. According to this formula, the above Code draws four triangles, and the vertex coordinate array should be 6, and the vertex ID array should be 12. The arrangement of multiple triangles in the vertex ID array is special. You need to record them carefully or you will not be able to draw the correct triangle. As shown in,Since there is no proper 3D coordinate point on our side, we use a positive triangle to concatenate a positive quadrilateral, which consists of six vertices and four small triangles, if you see a clear idea, you should understand that the principle of creating a rule-less quadrilateral is exactly the same as that of it. You only need to input a proper 3D coordinate point.

 

 

Based on the above logic, let's modify the algorithm. Assuming that the vertex coordinates of a triangle are any number, we need to calculate the array content of the corresponding vertex id based on the number of vertex coordinates. In a for loop, start = 0 and end = 3 indicate that the vertex whose index is 3 is drawn from the vertex whose index is 0 in the vertex coordinate array, that is to say, three triangles are drawn from 0 to 3.


Using unityengine; using system. collections. generic; using system; public class test: monobehaviour {// Number of mesh model vertices private int vertices_count = 6; void start () {// get the meshfilter object, which is currently empty. Meshfilter = (meshfilter) gameobject. find ("face "). getcomponent (typeof (meshfilter); // obtain the mesh object mesh = meshfilter. mesh; // coordinate array of triangle vertices vector3 [] vertices = new vector3 [vertices_count]; // obtain the number of triangles int triangles_count = vertices_count-2; // array of triangle vertex IDs int [] triangles = new int [triangles_count * 3]; // coordinate of Three triangle points, to clearly ignore the Z axis vertices [0] = new vector3 (, 0); vertices [1] = new vector3 (, 0 ); vertices [2] = new vector3 (, 0); vertices [3] = new vector3 (, 0); vertices [4] = new vector3 (, 0 ); vertices [5] = new vector3 (2, 1, 0); // draw a triangle mesh. vertices = vertices; // start vertex int start = 0; // The vertex int end of the end triangle = 3; for (INT I = start; I <end; I ++) {for (Int J = 0; j <3; j ++) {if (I % 2 = 0) {triangles [3 * I + J] = I + J;} else {triangles [3 * I + J] = I + 2-j ;}} mesh. triangles = triangles ;}}

As shown in, three triangles are drawn based on the preceding logic algorithm, and the vertex coordinate IDs are from 0 to 3. Here, let's take a closer look at the title of this article. In fact, the points of the two dynamic tracks are maintaining the triangles vertex coordinate array. Triangles [0],Triangles [2],Triangles [4]... Indicates the value of a track point,Triangles [1],Triangles [3],Triangles [5]... It indicates the value of another trajectory point, and finally connects the triangle surface through the above algorithm, it is a dynamic two-point trajectory drawing surface.

 

Unity3d is actually very fun. Although it is easy to get started, it is not so easy to get started with it. Today, the idea of this article has been written, if you still can't understand it, please try to figure out the difference between the triangle and the Quadrilateral. Wow, it's not too early. I have to go to bed. I have to go to work tomorrow, gogogogogogo. Come on ~ We hope that we can all learn and make progress together.


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.