Unity3D之Mesh(七)绘制长方体

前言:数组

从如今开始,终于感受进入一点点正题了!动态建立三维立体模型mesh!依然从简单入手:长方体。框架


 1、基本思路函数

因为是建立长方体mesh,由以前的研究得知,两个数据必需要有,即:顶点的数据:vertices与索引的三角形(即负责管理每一个三角形的三点的索引顺序):triangles。长方体:通常会得知:长宽高;即今天咱们由长宽高为参数获得vertices与triangles。学习


2、基本程序框架ui

建立一个empty的gameobject,挂在脚本。spa

由基本思路可得基本框架,以后,实现函数功能便可;code

 1 using UnityEngine;
 2 
 3 [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
 4 public class cube_mesh : MonoBehaviour
 5 {
 6     public float Length = 5;              //长方体的长
 7     public float Width = 6;               //长方体的宽
 8     public float Heigth = 7;              //长方体的高
 9     private MeshFilter meshFilter;
10 
11     void Start()
12     {
13         meshFilter = GetComponent<MeshFilter>();
14         meshFilter.mesh = CreateMesh(Length, Width, Heigth);
15     }
16 
17     Mesh CreateMesh(float length, float width, float heigth)
18     {
19         //vertices(顶点、必须):
20         //.........
21 
22         //triangles(索引三角形、必须):
23         //.........
24 
25         //uv:
26         //.........
27 
28         //负载属性与mesh
29         Mesh mesh = new Mesh();
30         //.........
31         return mesh;
32     }
33 }


 

3、绘制函数的实现以及整个程序代码orm

 1 using UnityEngine;
 2 
 3 [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter))]
 4 public class cube_mesh : MonoBehaviour
 5 {
 6     public float Length = 5;              //长方体的长
 7     public float Width = 6;               //长方体的宽
 8     public float Heigth = 7;              //长方体的高
 9     private MeshFilter meshFilter;
10 
11     void Start()
12     {
13         meshFilter = GetComponent<MeshFilter>();
14         meshFilter.mesh = CreateMesh(Length, Width, Heigth);
15     }
16 
17     Mesh CreateMesh(float length, float width, float heigth)
18     {
19 
20         //vertices(顶点、必须):
21         int vertices_count = 4*6;                                 //顶点数(每一个面4个点,六个面)
22         Vector3[] vertices = new Vector3[vertices_count];
23         vertices[0] = new Vector3(0, 0, 0);                     //前面的左下角的点
24         vertices[1] = new Vector3(0, heigth, 0);                //前面的左上角的点
25         vertices[2] = new Vector3(length, 0, 0);                //前面的右下角的点
26         vertices[3] = new Vector3(length, heigth, 0);           //前面的右上角的点
27 
28         vertices[4] = new Vector3(length, 0, width);           //后面的右下角的点
29         vertices[5] = new Vector3(length, heigth, width);      //后面的右上角的点
30         vertices[6] = new Vector3(0, 0, width);                //后面的左下角的点
31         vertices[7] = new Vector3(0, heigth, width);           //后面的左上角的点
32 
33         vertices[8] = vertices[6];                              //
34         vertices[9] = vertices[7];
35         vertices[10] = vertices[0];
36         vertices[11] = vertices[1];
37 
38         vertices[12] = vertices[2];                              //
39         vertices[13] = vertices[3];
40         vertices[14] = vertices[4];
41         vertices[15] = vertices[5];
42 
43         vertices[16] = vertices[1];                              //
44         vertices[17] = vertices[7];
45         vertices[18] = vertices[3];
46         vertices[19] = vertices[5];
47 
48         vertices[20] = vertices[2];                              //
49         vertices[21] = vertices[4];
50         vertices[22] = vertices[0];
51         vertices[23] = vertices[6];
52 
53 
54         //triangles(索引三角形、必须):
55         int 分割三角形数 = 6 * 2;
56         int triangles_cout = 分割三角形数 * 3;                  //索引三角形的索引点个数
57         int[] triangles = new int [triangles_cout];            //索引三角形数组
58         for(int i=0,vi=0;i< triangles_cout;i+=6,vi+=4)
59         {
60             triangles[i] = vi;
61             triangles[i+1] = vi+1;
62             triangles[i+2] = vi+2;
63 
64             triangles[i+3] = vi+3;
65             triangles[i+4] = vi+2;
66             triangles[i+5] = vi+1;
67 
68         }
69 
70         //uv:
71         //.........
72 
73         //负载属性与mesh
74         Mesh mesh = new Mesh();
75         mesh.vertices = vertices;
76         mesh.triangles = triangles;
77         return mesh;
78     }
79 }

4、效果图blog


5、其余相关的说明索引

一、冗余的顶点坐标

正方体6个面,每一个面由2个三角形组成,因此共须要36个三角形顶点索引。可是正方体只有8个顶点,为何须要24个顶点坐标数据呢?

答案是:Unity3D的Mesh.triangles是三角形索引数组,不只依靠这个索引值索引三角形顶点坐标,并且索引纹理坐标,索引法线向量。即正方体的每一个顶点都参与了3个平面,而这3个平面的法线向量是不一样的,该顶点在渲染这3个平面的时候须要索引到不一样的法线向量。而因为顶点坐标和法线向量是由同一个索引值triangles[Index]取得的,例如,根据vertices[0],vertices[10],vertices[22]在vertices中索引到的顶点都为(0,0,0),可是在normals中索引到的法向量值各不相同。这就决定了在正方体中一个顶点,须要有3份存储。(若是你须要建立其它模型,须要根据实际状况决定顶点坐标的冗余度。实质上顶点坐标的冗余正是方便了法线坐标、纹理坐标的存取。)

二、三角形的渲染

准则:三角形有两面,正面可见,背面不可见。三角形的渲染顺序与三角形的正面法线呈左手螺旋定则

这就决定了,若是咱们须要渲染以下一个正方形面,那么就须要保证组成这个正方形的两个小三角形的正面法线都是指向屏幕外的。

程序中的顶点顺序为,三角形1: 0--->1--->2,三角形2:3--->2--->1 。


【欢迎转载】

 转载请代表出处: 乐学习

相关文章
相关标签/搜索