Mesh
2018-10-30 本文已影响0人
叫我颜先生
简介
所有图形都是三角形组成的,绘制包括三部分
- vertices:图形的所有顶点坐标
- triangles:图形的绘制顺序,选三个顶点组成一个三角形,顺序必须顺时针,逆时针会绘制在背面
- uv:材质的贴图要和图形对应,uv左下角(0,0),右上角(1,1),就是将每一个顶点坐标对应到贴图上,可先将顶点坐标移至正右上角区域,然后进行比例缩放到1
实例
-
三角形 正方形
//3 Vector3[] v_3 = new Vector3[] { new Vector3(1, 1, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0) }; int[] t_3 = new int[] { 0,1,2 }; Vector2[] uv_3 = new Vector2[] { new Vector2(1,1), new Vector2(0,0), new Vector2(0,1), }; //4 Vector3[] v_4 = new Vector3[] { new Vector3(1, 1, 0), new Vector3(1, 0, 0), new Vector3(0, 0, 0), new Vector3(0, 1, 0) }; int[] t_4 = new int[] { 0,2,3, 1,2,3 }; Vector2[] uv_4 = new Vector2[] { new Vector2(1,1), new Vector2(1,0), new Vector2(0,0), new Vector2(0,1), }; void Start() { Creater("obj3", v_3, t_3, uv_3, Mat.Green); Creater("obj4", v_4, t_4, uv_4, Mat.Red); } void Creater(string name, Vector3[] ver, int[] tri, Vector2[] uv, Mat mat) { GameObject obj = new GameObject(); obj.name = name; obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; obj.transform.localRotation = Quaternion.identity; obj.AddComponent<MeshFilter>(); Mesh mesh = obj.GetComponent<MeshFilter>().mesh; mesh.Clear(); mesh.vertices = ver; mesh.triangles = tri; obj.AddComponent<MeshRenderer>(); obj.GetComponent<MeshRenderer>().material = mats[(int)mat]; mesh.uv = uv; }
-
圆形,由圆心与圆上点组成三角形
void CreateCircle(Vector2 center, float r, int num) { GameObject obj = new GameObject(); obj.name = "circle"; obj.transform.localPosition = CreatePos * CreateNum; obj.transform.localScale = Vector3.one; obj.transform.localRotation = Quaternion.identity; CreateNum += 1; obj.AddComponent<MeshFilter>(); Mesh mesh = obj.GetComponent<MeshFilter>().mesh; mesh.Clear(); Vector3[] ver = new Vector3[num + 1]; int[] tri = new int[3 * num]; Vector2[] uv = new Vector2[num + 1]; float deltaAngle = 360 / num; ver[num] = (Vector3)center; for (int i = 0; i < num; i++) { float angle = (90 - i * deltaAngle) * Mathf.Deg2Rad; ver[i] = new Vector3(r * Mathf.Cos(angle) + center.x, r * Mathf.Sin(angle) + center.y, 0); tri[i * 3] = i; if (i == num - 1) tri[i * 3 + 1] = 0; else tri[i * 3 + 1] = i + 1; tri[i * 3 + 2] = num; } mesh.vertices = ver; mesh.triangles = tri; obj.AddComponent<MeshRenderer>(); obj.GetComponent<MeshRenderer>().material = mats[1]; for (int i = 0; i < ver.Length; i++) { uv[i] = new Vector2(ver[i].x - center.x + r, ver[i].y - center.y + r)/2/r; } mesh.uv = uv; }
-
电风扇型
void CreateCircle1(Vector2 center, float r, int num) { GameObject obj = new GameObject(); obj.name = "circle1"; obj.transform.localPosition = CreatePos * CreateNum; obj.transform.localScale = Vector3.one; obj.transform.localRotation = Quaternion.identity; CreateNum += 1; obj.AddComponent<MeshFilter>(); Mesh mesh = obj.GetComponent<MeshFilter>().mesh; mesh.Clear(); Vector3[] ver = new Vector3[num + 1]; int[] tri = new int[3 * num]; Vector2[] uv = new Vector2[num + 1]; float deltaAngle = 360 / num; ver[num] = (Vector3)center; for (int i = 0; i < num; i++) { float angle = (90 - i * deltaAngle) * Mathf.Deg2Rad; ver[i] = new Vector3(r * Mathf.Cos(angle) + center.x, r * Mathf.Sin(angle) + center.y, 0); if (i % 2 == 1) { tri[i * 3] = num; tri[i * 3 + 1] = num; tri[i * 3 + 2] = num; } else { tri[i * 3] = i; if (i == num - 1) tri[i * 3 + 1] = 0; else tri[i * 3 + 1] = i + 1; tri[i * 3 + 2] = num; } } mesh.vertices = ver; mesh.triangles = tri; obj.AddComponent<MeshRenderer>(); obj.GetComponent<MeshRenderer>().material = mats[1]; for (int i = 0; i < ver.Length; i++) { uv[i] = new Vector2(ver[i].x - center.x + r, ver[i].y - center.y + r) / 2 / r; } mesh.uv = uv; }
-
圆环,有一个个小梯形组成,每个梯形又可以由两个三角形组成
void CreateCircle2(Vector2 center, float r1, float r2, int num) { GameObject obj = new GameObject(); obj.name = "circle2"; obj.transform.localPosition = CreatePos * CreateNum; obj.transform.localScale = Vector3.one; obj.transform.localRotation = Quaternion.identity; CreateNum += 1; obj.AddComponent<MeshFilter>(); Mesh mesh = obj.GetComponent<MeshFilter>().mesh; mesh.Clear(); Vector3[] ver = new Vector3[num * 2]; int[] tri = new int[3 * num * 2]; Vector2[] uv = new Vector2[num * 2]; float deltaAngle = 360 / num; for (int i = 0; i < num; i++) { float angle = (90 - i * deltaAngle) * Mathf.Deg2Rad; ver[i] = new Vector3(r1 * Mathf.Cos(angle) + center.x, r1 * Mathf.Sin(angle) + center.y, 0); ver[i + num] = new Vector3(r2 * Mathf.Cos(angle) + center.x, r2 * Mathf.Sin(angle) + center.y, 0); //连接处 if (i == num - 1) { tri[i * 3 * 2] = i; tri[i * 3 * 2 + 1] = 0; tri[i * 3 * 2 + 2] = num; tri[i * 3 * 2 + 3] = i; tri[i * 3 * 2 + 4] = num; tri[i * 3 * 2 + 5] = i + num; } else { tri[i * 3 * 2] = i; tri[i * 3 * 2 + 1] = i + 1; tri[i * 3 * 2 + 2] = i + 1 + num; tri[i * 3 * 2 + 3] = i; tri[i * 3 * 2 + 4] = i + 1 + num; tri[i * 3 * 2 + 5] = i + num; } } mesh.vertices = ver; mesh.triangles = tri; obj.AddComponent<MeshRenderer>(); obj.GetComponent<MeshRenderer>().material = mats[1]; for (int i = 0; i < ver.Length; i++) { uv[i] = new Vector2(ver[i].x - center.x + r1, ver[i].y - center.y + r1) / 2 / r1; } mesh.uv = uv; }
-
圆弧形
void Creater(float r1, float r2, float angle) { GameObject obj = new GameObject(); obj.transform.parent = gameObject.transform; obj.transform.localPosition = Vector3.zero; obj.transform.localScale = Vector3.one; obj.transform.localRotation = Quaternion.identity; obj.AddComponent<MeshFilter>(); Mesh mesh = obj.GetComponent<MeshFilter>().mesh; mesh.Clear(); //默认一圈分割成60份,6度一份 float deltaAngle = 6; int num = Mathf.CeilToInt(angle * 2 / 2 / deltaAngle) + 1; //5 Vector3[] ver = new Vector3[num * 2]; //10 int[] tri = new int[(num - 1) * 2 * 3]; for (int i = 0; i < num; i++) { float rad = (i * deltaAngle - angle / 2) * Mathf.Deg2Rad; ver[i] = new Vector3(r1 * Mathf.Cos(rad), 0, r1 * Mathf.Sin(rad)); ver[i + num] = new Vector3(r2 * Mathf.Cos(rad), 0, r2 * Mathf.Sin(rad)); if (i == num - 1) { continue; } else { tri[i * 3 * 2] = i + 1 + num; tri[i * 3 * 2 + 1] = i + 1; tri[i * 3 * 2 + 2] = i; tri[i * 3 * 2 + 3] = i + num; tri[i * 3 * 2 + 4] = i + 1 + num; tri[i * 3 * 2 + 5] = i; } } mesh.vertices = ver; mesh.triangles = tri; Material material = new Material(Shader.Find("Diffuse")); material.SetColor("_Color", Color.white); obj.AddComponent<MeshRenderer>(); obj.GetComponent<MeshRenderer>().material = material; obj.AddComponent<MeshCollider>(); }