public static Mesh CreateMesh(Vector2[] poly, float extrusion) { // convert polygon to triangles Triangulator triangulator = new Triangulator(poly); int[] tris = triangulator.Triangulate(); Mesh m = new Mesh(); Vector3[] vertices = new Vector3[poly.Length * 2]; for (int i = 0; i < poly.Length; i++) { vertices[i].x = poly[i].x; vertices[i].y = 0.0f; vertices[i].z = poly[i].y; // front vertex vertices[i + poly.Length].x = poly[i].x; vertices[i + poly.Length].y = extrusion; vertices[i + poly.Length].z = poly[i].y; // back vertex } int[] triangles = new int[tris.Length * 2 + poly.Length * 6]; int count_tris = 0; for (int i = 0; i < tris.Length; i += 3) { triangles[i] = tris[i]; triangles[i + 1] = tris[i + 2]; triangles[i + 2] = tris[i + 1]; } // front vertices count_tris += tris.Length; for (int i = 0; i < tris.Length; i += 3) { triangles[count_tris + i] = tris[i + 1] + poly.Length; triangles[count_tris + i + 1] = tris[i + 2] + poly.Length; triangles[count_tris + i + 2] = tris[i] + poly.Length; } //texture coordinate Vector2[] uvs = new Vector2[vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } m.vertices = vertices; m.triangles = triangles; m.uv = uvs; m = Triangulator.SideExtrusion(m); m.RecalculateNormals(); m.RecalculateBounds(); return(m); }
public static Mesh CreateMesh(Vector2[] poly, float extrusion) { Array.Resize <Vector2>(ref poly, poly.Length - 1); // convert polygon to triangles Triangulator triangulator = new Triangulator(poly, false); //Triangulator triangulator = new Triangulator(poly); int[] tris = triangulator.Triangulate(); Mesh m = new Mesh(); Vector3[] vertices = new Vector3[poly.Length * 2]; for (int i = 0; i < poly.Length; i++) { vertices[i].x = poly[i].x; vertices[i].y = 0.0f; vertices[i].z = poly[i].y; // front vertex vertices[i + poly.Length].x = poly[i].x; vertices[i + poly.Length].y = extrusion; vertices[i + poly.Length].z = poly[i].y; // back vertex } int[] triangles = new int[tris.Length]; int count_tris = 0; // If we want to render the underneath //for (int i = 0; i < tris.Length; i += 3) //{ // triangles[i] = tris[i]; // triangles[i + 1] = tris[i + 2]; // triangles[i + 2] = tris[i + 1]; //} // front vertices //count_tris += tris.Length; for (int i = 0; i < tris.Length; i += 3) { triangles[count_tris + i] = tris[i + 1] + poly.Length; triangles[count_tris + i + 1] = tris[i + 2] + poly.Length; triangles[count_tris + i + 2] = tris[i] + poly.Length; } //texture coordinate Vector2[] uvs = new Vector2[vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } m.vertices = vertices; m.triangles = triangles; m.uv = uvs; m = Triangulator.SideExtrusion(m, IsClockwise(poly)); m.RecalculateNormals(); m.RecalculateBounds(); return(m); }
public static Mesh CreateMesh3D(Vector2[] poly, float extrusion) { // convert polygon to triangles Triangulator triangulator = new Triangulator(poly); int[] traingulatedTris = triangulator.Triangulate(); Mesh m = new Mesh(); Vector3[] vertices; if (extrusion == 0f) { vertices = new Vector3[poly.Length]; } else { vertices = new Vector3[poly.Length * 2]; } for (int i = 0; i < poly.Length; i++) { vertices [i].x = poly [i].x; vertices [i].y = poly [i].y; vertices [i].z = -extrusion; // front vertex if (extrusion != 0f) { vertices [i + poly.Length].x = poly [i].x; vertices [i + poly.Length].y = poly [i].y; vertices [i + poly.Length].z = extrusion; // back vertex } } int[] triangles; if (extrusion == 0f) { triangles = new int[traingulatedTris.Length]; } else { triangles = new int[traingulatedTris.Length * 2 + poly.Length * 6]; } int count_tris = 0; for (int i = 0; i < traingulatedTris.Length; i += 3) { triangles [i] = traingulatedTris [i]; triangles [i + 1] = traingulatedTris [i + 1]; triangles [i + 2] = traingulatedTris [i + 2]; } // front vertices if (extrusion != 0f) { count_tris += traingulatedTris.Length; for (int i = 0; i < traingulatedTris.Length; i += 3) { triangles [count_tris + i] = traingulatedTris [i + 2] + poly.Length; triangles [count_tris + i + 1] = traingulatedTris [i + 1] + poly.Length; triangles [count_tris + i + 2] = traingulatedTris [i] + poly.Length; } // back vertices } //texture coordinate Vector2[] uvs = new Vector2[vertices.Length]; for (int i = 0, c = uvs.Length; i < c; ++i) { uvs[i].x = vertices[i].x; uvs[i].y = vertices[i].y; } m.vertices = vertices; m.triangles = triangles; m.uv = uvs; if (extrusion != 0f) { m = Triangulator.SideExtrusion(m); } m.RecalculateNormals(); m.RecalculateBounds(); m.Optimize(); return(m); }