Unity Mesh 网格绘制多边形
利用 Mesh 网格在理论上可以绘制出各种形状,而原理就是所有图形都是由三角形构成的
一、绘制三角形
1 // 绘制的材质 2 public Material material; 3 4 void DrawTriangle() 5 { 6 // 添加 MeshFilter 和 MeshRenderer 组件 7 gameObject.AddComponent<MeshFilter>(); 8 gameObject.AddComponent<MeshRenderer>(); 9 10 // 绘制图案的材质 11 gameObject.GetComponent<MeshRenderer>().material = material; 12 13 Mesh mesh = GetComponent<MeshFilter>().mesh; 14 mesh.Clear(); 15 16 // 设置顶点 17 mesh.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0) }; 18 19 // 设置三角形顶点顺序,顺时针设置 20 mesh.triangles = new int[] { 0, 1, 2 }; 21 }
-
添加组件:MeshFilter 和 MeshRenderer 组件是绘制 Mesh 网格必须的
-
指定顶点:比如三角形,需要指定三个顶点
-
指定顶点顺序:triangles 数组的大小必须是3的倍数,顶点的顺序必须是顺时针(逆时针只能在反面才能看到)
绘制完成的图形:
二、绘制正方形
1 // 绘制的材质 2 public Material material; 3 4 void Start() 5 { 6 DrawSquare(); 7 } 8 9 void DrawSquare() 10 { 11 // 添加 MeshFilter 和 MeshRenderer 组件 12 gameObject.AddComponent<MeshFilter>(); 13 gameObject.AddComponent<MeshRenderer>(); 14 15 // 绘制图案的材质 16 gameObject.GetComponent<MeshRenderer>().material = material; 17 18 Mesh mesh = GetComponent<MeshFilter>().mesh; 19 mesh.Clear(); 20 21 // 设置顶点 22 mesh.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 0, 0) }; 23 mesh.triangles = new int[] { 0, 1, 2, 0, 2, 3 }; 24 }
绘制两个三角形就可以组成正方形,所以只要在三角形的基础上再加一个点(1,0)就可以了
绘制完成的图形:
三、绘制圆形
1 // 绘制的材质 2 public Material material; 3 4 void Start() 5 { 6 DrawCircle(3, 50, Vector3.zero); 7 } 8 9 /// <summary> 10 /// 画圆 11 /// </summary> 12 /// <param name="radius"> 圆的半径 </param> 13 /// <param name="segments"> 圆的分割数 </param> 14 /// <param name="centerCircle"> 圆心的位置 </param> 15 void DrawCircle(float radius, int segments, Vector3 centerCircle) 16 { 17 gameObject.AddComponent<MeshFilter>(); 18 gameObject.AddComponent<MeshRenderer>(); 19 gameObject.GetComponent<MeshRenderer>().material = material; 20 21 // 计算所需顶点 22 Vector3[] vertices = new Vector3[segments + 1]; 23 vertices[0] = centerCircle; 24 float deltaAngle = Mathf.Deg2Rad * 360f / segments; 25 float currentAngle = 0; 26 for (int i = 1; i < vertices.Length; i++) 27 { 28 float cosA = Mathf.Cos(currentAngle); 29 float sinA = Mathf.Sin(currentAngle); 30 vertices[i] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0); 31 currentAngle += deltaAngle; 32 } 33 34 // 绘制所需三角形 35 int[] triangles = new int[segments * 3]; 36 for (int i = 0, j = 1; i < segments * 3 - 3; i += 3, j++) 37 { 38 triangles[i] = 0; 39 triangles[i + 1] = j + 1; 40 triangles[i + 2] = j; 41 } 42 triangles[segments * 3 - 3] = 0; 43 triangles[segments * 3 - 2] = 1; 44 triangles[segments * 3 - 1] = segments; 45 46 Mesh mesh = GetComponent<MeshFilter>().mesh; 47 mesh.Clear(); 48 49 mesh.vertices = vertices; 50 mesh.triangles = triangles; 51 }
绘制圆形的函数需要的参数:半径、圆心坐标以及分割的个数(即组成的三角形个数)
在相同大小的情况下,分割数越多,所绘制的圆越圆滑
如下图,分别是分割数 10 和分割数 50 绘制出来的圆形
四、绘制圆环
1 // 绘制的材质 2 public Material material; 3 4 void Start() 5 { 6 DrawRing(3, 2, 50, Vector3.zero); 7 } 8 9 /// <summary> 10 /// 画圆环 11 /// </summary> 12 /// <param name="radius"> 圆半径 </param> 13 /// <param name="innerRadius"> 内圆半径 </param> 14 /// <param name="segments"> 圆的分个数 </param> 15 /// <param name="centerCircle"> 圆心坐标 </param> 16 void DrawRing(float radius, float innerRadius, int segments, Vector3 centerCircle) 17 { 18 gameObject.AddComponent<MeshFilter>(); 19 gameObject.AddComponent<MeshRenderer>(); 20 gameObject.GetComponent<MeshRenderer>().material = material; 21 22 // 所需顶点 23 Vector3[] vertices = new Vector3[segments * 2]; 24 float deltaAngle = Mathf.Deg2Rad * 360f / segments; 25 float currentAngle = 0; 26 for (int i = 0; i < vertices.Length; i += 2) 27 { 28 float cosA = Mathf.Cos(currentAngle); 29 float sinA = Mathf.Sin(currentAngle); 30 vertices[i] = new Vector3(cosA * innerRadius + centerCircle.x, sinA * innerRadius + centerCircle.y, 0); 31 vertices[i + 1] = new Vector3(cosA * radius + centerCircle.x, sinA * radius + centerCircle.y, 0); 32 currentAngle += deltaAngle; 33 } 34 35 // 所需三角形 36 int[] triangles = new int[segments * 6]; 37 for (int i = 0, j = 0; i < segments * 6; i += 6, j += 2) 38 { 39 triangles[i] = j; 40 triangles[i + 1] = (j + 3) % vertices.Length; 41 triangles[i + 2] = (j + 1) % vertices.Length; 42 43 triangles[i + 3] = j; 44 triangles[i + 4] = (j + 2) % vertices.Length; 45 triangles[i + 5] = (j + 3) % vertices.Length; 46 } 47 48 Mesh mesh = GetComponent<MeshFilter>().mesh; 49 mesh.Clear(); 50 51 mesh.vertices = vertices; 52 mesh.triangles = triangles; 53 }
圆环也是在圆形的基础上,由三角形构成梯形,再由梯形按分割数组成圆环
而这里比圆形多了一个参数:内圆半径,通过两个圆的大小去指定圆环的大小
同样,在相同大小的情况下,分割数越多,所绘制的圆环越圆滑
如下图,分别是分割数 10 和分割数 50 绘制出来的圆环
而绘制其他多边形也是同样的原理:
先确定多边形的各个顶点,然后按照顶点的顺序绘制所需要三角形,最后由绘制的三角形构成所要绘制的多边形
*** | 以上内容仅为学习参考、学习笔记使用 | ***