Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Mesh IcosahedronMesh (double lengthDouble = 2d)
- {
- Mesh mesh = new Mesh();
- float length = (float)lengthDouble;
- float phiLength = (float)PhiDouble(lengthDouble);
- List<Vector3> rawVerts = new List<Vector3>();
- List<Vector3> meshVerts = new List<Vector3>();
- List<Vector3> meshNormals = new List<Vector3>();
- List<Vector2> meshUVs = new List<Vector2>();
- List<int> meshTris = new List<int>();
- rawVerts.AddRange(new Vector3[]
- {
- new Vector3(0f, phiLength, length), new Vector3(0f, phiLength, -length), new Vector3(0f, -phiLength, length), new Vector3(0f, -phiLength, -length),
- new Vector3(length, 0f, phiLength), new Vector3(length, 0f, -phiLength), new Vector3(-length, 0f, phiLength), new Vector3(-length, 0f, -phiLength),
- new Vector3(phiLength, length, 0f), new Vector3(phiLength, -length, 0f), new Vector3(-phiLength, length, 0f), new Vector3(-phiLength, -length, 0f)
- });
- meshVerts.AddRange(new Vector3[]
- {
- // North
- rawVerts[0], rawVerts[6], rawVerts[4],
- rawVerts[0], rawVerts[10], rawVerts[6],
- rawVerts[0], rawVerts[1], rawVerts[10],
- rawVerts[0], rawVerts[8], rawVerts[1],
- rawVerts[0], rawVerts[4], rawVerts[8],
- // North-Equator
- rawVerts[2], rawVerts[4], rawVerts[6],
- rawVerts[11], rawVerts[6], rawVerts[10],
- rawVerts[7], rawVerts[10], rawVerts[1],
- rawVerts[5], rawVerts[1], rawVerts[8],
- rawVerts[9], rawVerts[8], rawVerts[4],
- // South-Equator
- rawVerts[6], rawVerts[11], rawVerts[2],
- rawVerts[10], rawVerts[7], rawVerts[11],
- rawVerts[1], rawVerts[5], rawVerts[7],
- rawVerts[8], rawVerts[9], rawVerts[5],
- rawVerts[4], rawVerts[2], rawVerts[9],
- // South
- rawVerts[3], rawVerts[2], rawVerts[11],
- rawVerts[3], rawVerts[11], rawVerts[7],
- rawVerts[3], rawVerts[7], rawVerts[5],
- rawVerts[3], rawVerts[5], rawVerts[9],
- rawVerts[3], rawVerts[9], rawVerts[2]
- });
- meshTris.AddRange(new int[]
- {
- 0,1,2, 3,4,5, 6,7,8, 9,10,11, 12,13,14, // North
- 15,16,17, 18,19,20, 21,22,23, 24,25,26, 27,28,29, // North-Equator
- 30,31,32, 33,34,35, 36,37,38, 39,40,41, 42,43,44, // South-Equator
- 45,46,47, 48,49,50, 51,52,53, 54,55,56, 57,58,59 // South
- });
- // new Vector2(0.5f, 1f), new Vector2(1f, 0f), new Vector2(0f, 0f)
- meshUVs.AddRange(new Vector2[]
- {
- // North (Tri 0-4)
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
- new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
- new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
- new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
- new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
- new Vector2(.455f, .342f), new Vector2(.367f, .662f), new Vector2(.542f, .662f),
- new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
- new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
- new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
- new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
- new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
- new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
- new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
- new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
- new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
- new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
- new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
- new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f)
- });
- /*
- meshUVs.AddRange(new int[]
- {
- 0,4,6, 0,6,10, 0,10,1, 0,1,8, 0,8,4, // North
- 2,6,4, 11,10,6, 7,1,10, 5,8,1, 9,4,8, // North-Equator
- 6,2,11, 10,11,7, 1,7,5, 8,5,9, 4,9,2, // South-Equator
- 3,11,2, 3,7,11, 3,5,7, 3,9,5, 3,2,9 // South
- });
- */
- mesh.vertices = meshVerts.ToArray();
- mesh.triangles = meshTris.ToArray();
- mesh.normals = meshVerts.ToArray();
- mesh.uv = meshUVs.ToArray();
- return (mesh);
- }
- public static Mesh IcosphereMesh (int subdivisions = 1, double lengthDouble = 2d)
- {
- int triLength = (int)Mathf.Pow(2, subdivisions);
- double icoLength = lengthDouble * (double)triLength;
- double icoPhiLength = PhiDouble(lengthDouble) * (double)triLength;
- Mesh mesh = IcosahedronMesh(icoLength);
- List<Vector3> oldVerts = new List<Vector3>();
- List<int> oldTris = new List<int>();
- List<Vector3> meshVerts = new List<Vector3>();
- List<int> meshTris = new List<int>();
- meshVerts.AddRange(mesh.vertices);
- meshTris.AddRange(mesh.triangles);
- List<Vector3> meshNormals = new List<Vector3>();
- List<Vector2> meshUVs = new List<Vector2>();
- float radius = meshVerts[0].magnitude;
- int triCount;
- int triID;
- Vector3 vertA = new Vector3();
- Vector3 vertB = new Vector3();
- Vector3 vertC = new Vector3();
- Vector3 vertAB = new Vector3();
- Vector3 vertBC = new Vector3();
- Vector3 vertCA = new Vector3();
- // For each level of Subdivision...
- for (int s=1; s<=subdivisions; s++)
- {
- Debug.Log ("Subdivision #" + s + "/" + subdivisions);
- oldVerts.Clear();
- oldVerts.AddRange(meshVerts);
- oldTris.Clear();
- oldTris.AddRange(meshTris);
- meshVerts.Clear();
- meshTris.Clear();
- meshUVs.Clear();
- meshNormals.Clear();
- // For each triangle in the last dataset...
- triCount = oldTris.Count / 3;
- //Debug.Log ("Tricount: " + triCount);
- for (int t=0; t<triCount; t++)
- {
- triID = t*12;
- //Debug.Log ("Triangle #" + t + " (ID: " + triID + ")");
- // Get all Verts and Midpoints...
- vertA = oldVerts[oldTris[t*3]];
- vertB = oldVerts[oldTris[t*3+1]];
- vertC = oldVerts[oldTris[t*3+2]];
- vertAB = Calc.Midpoint(vertA, vertB).normalized * radius;
- vertBC = Calc.Midpoint(vertB, vertC).normalized * radius;
- vertCA = Calc.Midpoint(vertC, vertA).normalized * radius;
- // Create 4 new triangles from Verts and Midpoints.
- meshVerts.AddRange(new Vector3[]
- {
- vertBC, vertCA, vertAB, // Tri M
- vertA, vertAB, vertCA, // Tri A
- vertAB, vertB, vertBC, // Tri B
- vertCA, vertBC, vertC // Tri C
- });
- meshTris.AddRange(new int[]
- {
- triID, triID+1, triID+2, // Tri M
- triID+3, triID+4, triID+5, // Tri A
- triID+6, triID+7, triID+8, // Tri B
- triID+9, triID+10, triID+11 // Tri C
- });
- meshNormals.AddRange(new Vector3[]
- {
- vertBC.normalized, vertCA.normalized, vertAB.normalized, // Tri M
- vertA.normalized, vertAB.normalized, vertCA.normalized, // Tri A
- vertAB.normalized, vertB.normalized, vertBC.normalized, // Tri B
- vertCA.normalized, vertBC.normalized, vertC.normalized // Tri C
- });
- meshUVs.AddRange(new Vector2[]
- {
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri M
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri A
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri B
- new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f) // Tri C
- });
- }
- }
- mesh.vertices = meshVerts.ToArray();
- mesh.triangles = meshTris.ToArray();
- mesh.normals = meshVerts.ToArray();
- mesh.uv = meshUVs.ToArray();
- return (mesh);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement