Advertisement
Sunspider

Icosahedron and Icosphere

Mar 4th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.52 KB | None | 0 0
  1. public static Mesh IcosahedronMesh (double lengthDouble = 2d)
  2. {
  3.     Mesh mesh = new Mesh();
  4.  
  5.     float length = (float)lengthDouble;
  6.     float phiLength = (float)PhiDouble(lengthDouble);
  7.  
  8.     List<Vector3> rawVerts = new List<Vector3>();
  9.     List<Vector3> meshVerts = new List<Vector3>();
  10.     List<Vector3> meshNormals = new List<Vector3>();
  11.     List<Vector2> meshUVs = new List<Vector2>();
  12.     List<int> meshTris = new List<int>();
  13.  
  14.     rawVerts.AddRange(new Vector3[]
  15.     {
  16.         new Vector3(0f, phiLength, length), new Vector3(0f, phiLength, -length), new Vector3(0f, -phiLength, length), new Vector3(0f, -phiLength, -length),
  17.         new Vector3(length, 0f, phiLength), new Vector3(length, 0f, -phiLength), new Vector3(-length, 0f, phiLength), new Vector3(-length, 0f, -phiLength),
  18.         new Vector3(phiLength, length, 0f), new Vector3(phiLength, -length, 0f), new Vector3(-phiLength, length, 0f), new Vector3(-phiLength, -length, 0f)
  19.     });
  20.  
  21.     meshVerts.AddRange(new Vector3[]
  22.     {
  23.         // North
  24.         rawVerts[0], rawVerts[6], rawVerts[4],
  25.         rawVerts[0], rawVerts[10], rawVerts[6],
  26.         rawVerts[0], rawVerts[1], rawVerts[10],
  27.         rawVerts[0], rawVerts[8], rawVerts[1],
  28.         rawVerts[0], rawVerts[4], rawVerts[8],
  29.  
  30.         // North-Equator
  31.         rawVerts[2], rawVerts[4], rawVerts[6],
  32.         rawVerts[11], rawVerts[6], rawVerts[10],
  33.         rawVerts[7], rawVerts[10], rawVerts[1],
  34.         rawVerts[5], rawVerts[1], rawVerts[8],
  35.         rawVerts[9], rawVerts[8], rawVerts[4],
  36.  
  37.         // South-Equator
  38.         rawVerts[6], rawVerts[11], rawVerts[2],
  39.         rawVerts[10], rawVerts[7], rawVerts[11],
  40.         rawVerts[1], rawVerts[5], rawVerts[7],
  41.         rawVerts[8], rawVerts[9], rawVerts[5],
  42.         rawVerts[4], rawVerts[2], rawVerts[9],
  43.  
  44.         // South
  45.         rawVerts[3], rawVerts[2], rawVerts[11],
  46.         rawVerts[3], rawVerts[11], rawVerts[7],
  47.         rawVerts[3], rawVerts[7], rawVerts[5],
  48.         rawVerts[3], rawVerts[5], rawVerts[9],
  49.         rawVerts[3], rawVerts[9], rawVerts[2]
  50.     });
  51.  
  52.     meshTris.AddRange(new int[]
  53.     {
  54.         0,1,2, 3,4,5, 6,7,8, 9,10,11, 12,13,14, // North
  55.         15,16,17, 18,19,20, 21,22,23, 24,25,26, 27,28,29, // North-Equator
  56.         30,31,32, 33,34,35, 36,37,38, 39,40,41, 42,43,44, // South-Equator
  57.         45,46,47, 48,49,50, 51,52,53, 54,55,56, 57,58,59 // South
  58.     });
  59.  
  60.     // new Vector2(0.5f, 1f), new Vector2(1f, 0f), new Vector2(0f, 0f)
  61.     meshUVs.AddRange(new Vector2[]
  62.     {
  63.         // North (Tri 0-4)
  64.         new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
  65.         new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
  66.         new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
  67.         new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
  68.         new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
  69.  
  70.         new Vector2(.455f, .342f), new Vector2(.367f, .662f), new Vector2(.542f, .662f),
  71.         new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
  72.         new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
  73.         new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
  74.         new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
  75.  
  76.         new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
  77.         new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
  78.         new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
  79.         new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
  80.         new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f),
  81.  
  82.         new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f),
  83.         new Vector2(.273f, .992f), new Vector2(.360f, .671f), new Vector2(.185f, .671f),
  84.         new Vector2(.091f, .992f), new Vector2(.178f, .671f), new Vector2(.003f, .671f),
  85.         new Vector2(.818f, .992f), new Vector2(.906f, .671f), new Vector2(.731f, .671f),
  86.         new Vector2(.636f, .992f), new Vector2(.724f, .671f), new Vector2(.549f, .671f)
  87.     });
  88.  
  89.     /*
  90.     meshUVs.AddRange(new int[]
  91.     {
  92.         0,4,6, 0,6,10, 0,10,1, 0,1,8, 0,8,4, // North
  93.         2,6,4, 11,10,6, 7,1,10, 5,8,1, 9,4,8, // North-Equator
  94.         6,2,11, 10,11,7, 1,7,5, 8,5,9, 4,9,2, // South-Equator
  95.         3,11,2, 3,7,11, 3,5,7, 3,9,5, 3,2,9 // South
  96.     });
  97.     */
  98.  
  99.     mesh.vertices = meshVerts.ToArray();
  100.     mesh.triangles = meshTris.ToArray();
  101.     mesh.normals = meshVerts.ToArray();
  102.     mesh.uv = meshUVs.ToArray();
  103.  
  104.     return (mesh);
  105. }
  106.  
  107. public static Mesh IcosphereMesh (int subdivisions = 1, double lengthDouble = 2d)
  108. {
  109.     int triLength = (int)Mathf.Pow(2, subdivisions);
  110.  
  111.     double icoLength = lengthDouble * (double)triLength;
  112.     double icoPhiLength = PhiDouble(lengthDouble) * (double)triLength;
  113.  
  114.     Mesh mesh = IcosahedronMesh(icoLength);
  115.  
  116.     List<Vector3> oldVerts = new List<Vector3>();
  117.     List<int> oldTris = new List<int>();
  118.  
  119.     List<Vector3> meshVerts = new List<Vector3>();
  120.     List<int> meshTris = new List<int>();
  121.     meshVerts.AddRange(mesh.vertices);
  122.     meshTris.AddRange(mesh.triangles);
  123.     List<Vector3> meshNormals = new List<Vector3>();
  124.     List<Vector2> meshUVs = new List<Vector2>();
  125.  
  126.     float radius = meshVerts[0].magnitude;
  127.  
  128.     int triCount;
  129.     int triID;
  130.  
  131.     Vector3 vertA = new Vector3();
  132.     Vector3 vertB = new Vector3();
  133.     Vector3 vertC = new Vector3();
  134.     Vector3 vertAB = new Vector3();
  135.     Vector3 vertBC = new Vector3();
  136.     Vector3 vertCA = new Vector3();
  137.  
  138.     // For each level of Subdivision...
  139.     for (int s=1; s<=subdivisions; s++)
  140.     {
  141.         Debug.Log ("Subdivision #" + s + "/" + subdivisions);
  142.         oldVerts.Clear();
  143.         oldVerts.AddRange(meshVerts);
  144.         oldTris.Clear();
  145.         oldTris.AddRange(meshTris);
  146.         meshVerts.Clear();
  147.         meshTris.Clear();
  148.         meshUVs.Clear();
  149.         meshNormals.Clear();
  150.  
  151.         // For each triangle in the last dataset...
  152.         triCount = oldTris.Count / 3;
  153.         //Debug.Log ("Tricount: " + triCount);
  154.         for (int t=0; t<triCount; t++)
  155.         {
  156.             triID = t*12;
  157.             //Debug.Log ("Triangle #" + t + " (ID: " + triID + ")");
  158.  
  159.             // Get all Verts and Midpoints...
  160.             vertA = oldVerts[oldTris[t*3]];
  161.             vertB = oldVerts[oldTris[t*3+1]];
  162.             vertC = oldVerts[oldTris[t*3+2]];
  163.             vertAB = Calc.Midpoint(vertA, vertB).normalized * radius;
  164.             vertBC = Calc.Midpoint(vertB, vertC).normalized * radius;
  165.             vertCA = Calc.Midpoint(vertC, vertA).normalized * radius;
  166.  
  167.             // Create 4 new triangles from Verts and Midpoints.
  168.             meshVerts.AddRange(new Vector3[]
  169.             {
  170.                 vertBC, vertCA, vertAB, // Tri M
  171.                 vertA, vertAB, vertCA, // Tri A
  172.                 vertAB, vertB, vertBC, // Tri B
  173.                 vertCA, vertBC, vertC // Tri C
  174.             });
  175.  
  176.             meshTris.AddRange(new int[]
  177.             {
  178.                 triID, triID+1, triID+2, // Tri M
  179.                 triID+3, triID+4, triID+5, // Tri A
  180.                 triID+6, triID+7, triID+8, // Tri B
  181.                 triID+9, triID+10, triID+11 // Tri C
  182.             });
  183.  
  184.             meshNormals.AddRange(new Vector3[]
  185.             {
  186.                 vertBC.normalized, vertCA.normalized, vertAB.normalized, // Tri M
  187.                 vertA.normalized, vertAB.normalized, vertCA.normalized, // Tri A
  188.                 vertAB.normalized, vertB.normalized, vertBC.normalized, // Tri B
  189.                 vertCA.normalized, vertBC.normalized, vertC.normalized // Tri C
  190.             });
  191.  
  192.             meshUVs.AddRange(new Vector2[]
  193.             {
  194.                 new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri M
  195.                 new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri A
  196.                 new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f), // Tri B
  197.                 new Vector2(.455f, .992f), new Vector2(.542f, .671f), new Vector2(.367f, .671f) // Tri C
  198.             });
  199.         }
  200.     }
  201.  
  202.     mesh.vertices = meshVerts.ToArray();
  203.     mesh.triangles = meshTris.ToArray();
  204.     mesh.normals = meshVerts.ToArray();
  205.     mesh.uv = meshUVs.ToArray();
  206.  
  207.     return (mesh);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement