【Unity】mesh绘制简单图形
应用
使用mesh绘制一个彩虹色三角形、四边形、多四边形瓦片
控制mesh移动,实现游戏的对象的身体关节的动作
步骤
mesh(网格类)
Mesh mesh = new Mesh();
-
vertices:顶点数组,包括mesh中的所有顶点,如三角形三个顶点
//三角形三个顶点 Vector3[] vertices = new Vector3[3]; vertices[0] = new Vector3(0,0); vertices[1] = new Vector3(0,100); vertices[2] = new Vector3(100,100); //四边形4个顶点 Vector3[] vertices = new Vector3[4]; vertices[0] = new Vector3(0, 0); vertices[1] = new Vector3(0, 100); vertices[2] = new Vector3(100, 100); vertices[3] = new Vector3(100, 0);
-
triangles:三角形数组,如何构建每个三角形和三角形的个数
//顺时针的一个三角形 int[] triangles = new int[3]; triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; // 1 ——- 2 // | / // 0 / //两个顺时针三角形 int[] triangles = new int[3*2]; triangles[0] = 0; triangles[1] = 1; triangles[2] = 2; triangles[3] = 0; triangles[4] = 2; triangles[5] = 3; // 1 ——- 2 // | / // 0 / // 1 ——- 2 // | / | // 0 / — 3
-
uv:uv数组,确定每个顶点的uv坐标
每个uv包含一个纹理位置,将纹理位置和顶点位置匹配
//三角形 Vector2[] uv = new Vector2[3]; uv[0] = new Vector2(0,0); uv[1] = new Vector2(0,1); uv[2] = new Vector2(1,1); //四边形 Vector2[] uv = new Vector2[4]; uv[0] = new Vector2(0, 0); uv[1] = new Vector2(0, 1); uv[2] = new Vector2(1, 1); uv[3] = new Vector2(1, 0);
mesh.vertices = vertices; mesh.uv = uv; mesh.triangles = triangles;
mesh Filter
Mesh Filter是用了一个从资源里取到的mesh资源,然后通过Mesh Render,渲染到屏幕中
GetComponent<MeshFilter>().mesh = mesh;
mesh Render(将网格信息渲染出来)
- 需要材料