/// <summary> /// 与SetMesh显示面相反 /// </summary> public void SetMesh2(float v2Len, float height, GameObject body, float offsetX = 0, float offsetY = 0, float angle = 0, float tileSize_x = 1, float tileSize_y = 1) { List <Vector2> v2s = new List <Vector2>(); v2s.Add(Vector2.zero); v2s.Add(Vector2.up * height); v2s.Add(Vector2.right * v2Len + Vector2.up * height); v2s.Add(Vector2.right * v2Len); List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < v2s.Count; i++) { vertices.Add(v2s[i]); } List <List <Vector2> > closelist; int[] triangles = MyTriangleSubdivision.MyTriangulatePolygon(v2s, ref vertices, ref uvs, out closelist, true); float Angle = angle * Mathf.PI / 180f; for (int i = 0; i < uvs.Count; i++) { float x = uvs[i].x; float y = uvs[i].y; Vector2 newV2 = Vector2.zero; //纹理 的缩放旋转平移 newV2.x = tileSize_x * (x * Mathf.Cos(Angle) - y * Mathf.Sin(Angle) + offsetX); newV2.y = tileSize_y * (x * Mathf.Sin(Angle) + y * Mathf.Cos(Angle) + offsetY); uvs[i] = newV2; } Mesh mesh = body.GetComponent <MeshFilter>().mesh; mesh.SetTriangles(EmtyArr, 0); mesh.SetVertices(vertices); mesh.SetUVs(0, uvs); OtherSide(triangles); mesh.SetTriangles(triangles, 0); //mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); BoxCollider colli = body.GetComponent <BoxCollider>(); if (colli != null) { colli.center = Vector3.right * v2Len / 2 + Vector3.up * height / 2; colli.size = Vector3.right * v2Len + Vector3.up * height; } }
public List <List <Vector2> > SetMesh(List <Vector2> v2s, GameObject wallbody, float offsetX = 0, float offsetY = 0, float angle = 0, float tileSize_x = 1, float tileSize_y = 1, bool useCross = false) { List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < v2s.Count; i++) { vertices.Add(v2s[i]); } List <List <Vector2> > closelist; int[] triangles = MyTriangleSubdivision.MyTriangulatePolygon(v2s, ref vertices, ref uvs, out closelist, useCross);//TriangleSubdivision.TriangulatePolygon(v2s); if (triangles == null) { Debug.LogWarning("triangles == null 不是简单多边形"); } float Angle = angle * Mathf.PI / 180f; for (int i = 0; i < uvs.Count; i++) { float x = uvs[i].x; float y = uvs[i].y; Vector2 newV2 = Vector2.zero; newV2.x = tileSize_x * (x * Mathf.Cos(Angle) - y * Mathf.Sin(Angle) + offsetX); newV2.y = tileSize_y * (x * Mathf.Sin(Angle) + y * Mathf.Cos(Angle) + offsetY); uvs[i] = newV2; } Mesh mesh = wallbody.GetComponent <MeshFilter>().mesh; mesh.SetTriangles(EmtyArr, 0); mesh.SetVertices(vertices); mesh.SetUVs(0, uvs); mesh.SetTriangles(triangles, 0); //mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); PolygonCollider2D polygon = wallbody.GetComponent <PolygonCollider2D>(); if (polygon != null) { polygon.SetPath(0, v2s.ToArray()); } return(closelist); }
public void SetMesh3D(List <Vector2> v2s, GameObject gameObject, float offsetX = 0, float offsetY = 0, float angle = 0, float tileSize_x = 1, float tileSize_y = 1, bool useCross = false, bool isFloor = true) { List <Vector3> vertices = new List <Vector3>(); List <Vector2> uvs = new List <Vector2>(); for (int i = 0; i < v2s.Count; i++) { vertices.Add(v2s[i]); } List <List <Vector2> > closelist; int[] triangles = MyTriangleSubdivision.MyTriangulatePolygon(v2s, ref vertices, ref uvs, out closelist, true);//TriangleSubdivision.TriangulatePolygon(v2s); if (triangles == null) { Debug.LogWarning("triangles == null 不是简单多边形"); } float Angle = angle * Mathf.PI / 180f; for (int i = 0; i < uvs.Count; i++) { float x = uvs[i].x; float y = uvs[i].y; Vector2 newV2 = Vector2.zero; newV2.x = tileSize_x * (x * Mathf.Cos(Angle) - y * Mathf.Sin(Angle) + offsetX); newV2.y = tileSize_y * (x * Mathf.Sin(Angle) + y * Mathf.Cos(Angle) + offsetY); uvs[i] = newV2; } if (isFloor == false) { intlist.Clear(); for (int i = 0; i < triangles.Length; i++) { intlist.Add(triangles[triangles.Length - 1 - i]); } for (int i = 0; i < intlist.Count; i++) { triangles[i] = intlist[i]; } } for (int i = 0; i < vertices.Count; i++) { vertices[i] = Vector3.right * vertices[i].x + Vector3.forward * vertices[i].y; } Mesh mesh = gameObject.GetComponent <MeshFilter>().mesh; mesh.SetTriangles(EmtyArr, 0); mesh.SetVertices(vertices); mesh.SetUVs(0, uvs); mesh.SetTriangles(triangles, 0); //mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.RecalculateBounds(); MeshCollider meshColli = gameObject.GetComponentInChildren <MeshCollider>(); if (meshColli != null) { Mesh newMesh = new Mesh(); newMesh.SetTriangles(EmtyArr, 0); newMesh.SetVertices(vertices); newMesh.SetUVs(0, uvs); newMesh.SetTriangles(triangles, 0); newMesh.RecalculateNormals(); newMesh.RecalculateBounds(); meshColli.sharedMesh = newMesh; } }