/// <summary> /// Creates a 20 sided polygon /// </summary> /// <returns>Mesh that is the Icosahedron</returns> public static Mesh CreateIcosahedron() { Icosahedron lShape = new Icosahedron(); Mesh lMesh = new Mesh(); lMesh.hideFlags = HideFlags.HideAndDontSave; lMesh.vertices = lShape.Vertices; lMesh.triangles = lShape.Triangles; lMesh.RecalculateNormals(); return lMesh; }
static void get_triangulation(int num, Icosahedron ico) { Dictionary<Vector3, int> vertDict = new Dictionary<Vector3, int>(); // dict lookup to speed up vertex indexing float[,] subdivision = getSubMatrix(num + 2); // vertex subdivision matrix calculation Vector3 p1, p2, p3; int index = 0; int vertIndex; int len = subdivision.GetLength(0); int triNum = (num + 1) * (num + 1) * 20; // number of triangle faces vertices = new Vector3[triNum / 2 + 2]; // allocate verticies, triangles, etc... triangleIndices = new int[triNum * 3]; triangles = new int[triNum, 3]; Vector3[] tempVerts = new Vector3[len]; // temporary structures for subdividing each Icosahedron face int[] tempIndices = new int[len]; int[,] triIndices = triangulate(num); // precalculate generic subdivided triangle indices int triLength = triIndices.GetLength(0); for (int i = 0; i < 20; i++) // calculate subdivided vertices and triangles for each face { p1 = ico.Vertices[ico.Triangles[i * 3]]; // get 3 original vertex locations for each face p2 = ico.Vertices[ico.Triangles[i * 3 + 1]]; p3 = ico.Vertices[ico.Triangles[i * 3 + 2]]; for (int j = 0; j < len; j++) // calculate new subdivided vertex locations { tempVerts[j].x = subdivision[j, 0] * p1.x + subdivision[j, 1] * p2.x + subdivision[j, 2] * p3.x; tempVerts[j].y = subdivision[j, 0] * p1.y + subdivision[j, 1] * p2.y + subdivision[j, 2] * p3.y; tempVerts[j].z = subdivision[j, 0] * p1.z + subdivision[j, 1] * p2.z + subdivision[j, 2] * p3.z; tempVerts[j].Normalize(); if (!vertDict.TryGetValue(tempVerts[j], out vertIndex)) // quick lookup to avoid vertex duplication { vertDict[tempVerts[j]] = index; // if vertex not in dict, add it to dictionary and final array vertIndex = index; vertices[index] = tempVerts[j]; index += 1; } tempIndices[j] = vertIndex; // assemble vertex indices for triangle assignment } for (int j = 0; j < triLength; j++) // map precalculated subdivided triangle indices to vertex indices { triangles[triLength * i + j, 0] = tempIndices[triIndices[j, 0]]; triangles[triLength * i + j, 1] = tempIndices[triIndices[j, 1]]; triangles[triLength * i + j, 2] = tempIndices[triIndices[j, 2]]; triangleIndices[3 * triLength * i + 3 * j] = tempIndices[triIndices[j, 0]]; triangleIndices[3 * triLength * i + 3 * j + 1] = tempIndices[triIndices[j, 1]]; triangleIndices[3 * triLength * i + 3 * j + 2] = tempIndices[triIndices[j, 2]]; } } }