In Cosahedra.cs
using UnityEngine; using System.Collections; using ProceduralToolkit; using UnityEngine.UI; using System.Collections.Generic; public class Cosahedra : MonoBehaviour { public Text NumTrisText; public float radius = 1.5f; public ArgosMeshDraft aMD = new ArgosMeshDraft(); public ArgosMeshDraft aMD_IcosaSphere = new ArgosMeshDraft(); MeshDraft mD_Tri = new MeshDraft(); int depth = 4; void Start () { aMD.Clear(); aMD.Add(MeshDraft.Icosahedron(radius)); aMD_IcosaSphere.Clear(); Create_GP(depth); aMD_IcosaSphere.FlipNormals(); GetComponent<MeshFilter>().mesh = aMD_IcosaSphere.ToMesh(); } void Create_GP(int depth) { for(int i = 0; i<20; i++) { Vector3 v0 = aMD.vertices[aMD.triangles[i*3]]; Vector3 v1 = aMD.vertices[aMD.triangles[i*3 +1]]; Vector3 v2 = aMD.vertices[aMD.triangles[i*3 + 2]]; Color col = getWhite(); subdivide(v0, v1, v2, depth, col); } }
void addHex(Vector3 v0, Vector3 v1, Vector3 v2, Color col) { mD_Tri.Clear(); mD_Tri = MeshDraft.Triangle(v0, v1, v2); mD_Tri.Paint(col); aMD_IcosaSphere.AddHex(mD_Tri); } void subdivide(Vector3 v1, Vector3 v2, Vector3 v3, int depth, Color col) { Vector3 v12, v23, v31; Vector3 v12_n, v23_n, v31_n; int i; if (depth == 0) { addHex(v1, v2, v3, col); return; } v12 = (v1 + v2) / 2.0f; v23 = (v2 + v3) / 2.0f; v31 = (v3 + v1) / 2.0f; /* extrude midpoints to lie on unit sphere */ v12_n = v12.normalized * radius; v23_n = v23.normalized * radius; v31_n = v31.normalized * radius; /* recursively subdivide new triangles */ subdivide(v1, v12_n, v31_n, depth - 1, col); subdivide(v2, v23_n, v12_n, depth - 1, col); subdivide(v3, v31_n, v23_n, depth - 1, col); subdivide(v12_n, v23_n, v31_n, depth - 1, col); }
in ArgosMeshDraft.cs
public void AddHex(MeshDraft tri)//from triangle see: http://argos.vu/wp-content/uploads/2016/04/Hex_HCs-1.png { Vector3 HC = (tri.vertices[0] + tri.vertices[1] + tri.vertices[2])/ 3f; Vector3 H0 = (tri.vertices[1] + tri.vertices[0])/2f; Vector3 H1 = (tri.vertices[1] + HC) / 2f; Vector3 H2 = (tri.vertices[2] + tri.vertices[1]) / 2f; Vector3 H3 = (tri.vertices[2] + HC) / 2f; Vector3 H4 = (tri.vertices[0] + tri.vertices[2]) / 2f; Vector3 H5 = (tri.vertices[0] + HC) / 2f; List<Vector3> vL = new List<Vector3>(8) { HC, H0, H1, H2, H3, H4, H5, H0 }; List<Vector2> uvMapL = new List<Vector2>(8) { new Vector2(0.5f, 0.5f), new Vector2(0.5f, 1f), new Vector2(1, 0.75f), new Vector2(1, 0.25f), new Vector2(0.5f, 0), new Vector2(0, 0.25f), new Vector2(0, 0.75f), new Vector2(0.5f, 1f) }; Add(HexFan(vL, uvMapL)); }
In MeshDraftPrimatives.cs
public static MeshDraft Icosahedron(float radius) { float magicAngle = Mathf.PI*26.56505f/180; float segmentAngle = Mathf.PI*72/180; float currentAngle = 0f; var upperRing = new List<Vector3>(5); for (var i = 0; i < 5; i++) { upperRing.Add(PTUtils.PointOnSphere(radius, currentAngle, magicAngle)); currentAngle -= segmentAngle; } currentAngle = segmentAngle/2; var lowerRing = new List<Vector3>(5); for (var i = 0; i < 5; i++) { lowerRing.Add(PTUtils.PointOnSphere(radius, currentAngle, -magicAngle)); currentAngle -= segmentAngle; } var draft = BaselessPyramid(new Vector3(0, -radius, 0), lowerRing); draft.Add(FlatBand(lowerRing, upperRing)); upperRing.Reverse(); draft.Add(BaselessPyramid(new Vector3(0, radius, 0), upperRing)); draft.name = "Icosahedron"; return draft; } //#endregion Platonic solids
Find the up – Relate North/South
Right = Cross(Up,In);
Rotate to proper orientation – Or do this on creation
Tile appropriately
/// <remarks> /// https://en.wikipedia.org/wiki/Triangle_fan /// </remarks> public static MeshDraft TriangleFan(List<Vector3> vertices) { var draft = new MeshDraft { vertices = vertices, triangles = new List<int>(vertices.Count - 2), normals = new List<Vector3>(vertices.Count), uv = new List<Vector2>(vertices.Count), name = "TriangleFan" }; for (int i = 1; i < vertices.Count - 1; i++) { draft.triangles.Add(0); draft.triangles.Add(i); draft.triangles.Add(i + 1); } var normal = Vector3.Cross(vertices[1] - vertices[0], vertices[2] - vertices[0]).normalized; for (int i = 0; i < vertices.Count; i++) { draft.normals.Add(normal); draft.uv.Add(new Vector2((float) i/vertices.Count, (float) i/vertices.Count)); } return draft; }