public MeshGeometry3D Create(int recursionLevel) { this.geometry = new MeshGeometry3D (); this.middlePointIndexCache = new Dictionary<long, int> (); this.index = 0; // create 12 vertices of a icosahedron float t = (float)((1.0f + Math.Sqrt (5.0)) / 2.0); addVertex (new Vector3 (-1, t, 0f)); addVertex (new Vector3 (1, t, 0f)); addVertex (new Vector3 (-1, -t, 0f)); addVertex (new Vector3 (1, -t, 0f)); addVertex (new Vector3 (0, -1, t)); addVertex (new Vector3 (0, 1, t)); addVertex (new Vector3 (0, -1, -t)); addVertex (new Vector3 (0, 1, -t)); addVertex (new Vector3 (t, 0, -1)); addVertex (new Vector3 (t, 0, 1)); addVertex (new Vector3 (-t, 0, -1)); addVertex (new Vector3 (-t, 0, 1)); // create 20 triangles of the icosahedron var faces = new List<TriangleIndices> (); // 5 faces around point 0 faces.Add (new TriangleIndices (0, 11, 5)); faces.Add (new TriangleIndices (0, 5, 1)); faces.Add (new TriangleIndices (0, 1, 7)); faces.Add (new TriangleIndices (0, 7, 10)); faces.Add (new TriangleIndices (0, 10, 11)); // 5 adjacent faces faces.Add (new TriangleIndices (1, 5, 9)); faces.Add (new TriangleIndices (5, 11, 4)); faces.Add (new TriangleIndices (11, 10, 2)); faces.Add (new TriangleIndices (10, 7, 6)); faces.Add (new TriangleIndices (7, 1, 8)); // 5 faces around point 3 faces.Add (new TriangleIndices (3, 9, 4)); faces.Add (new TriangleIndices (3, 4, 2)); faces.Add (new TriangleIndices (3, 2, 6)); faces.Add (new TriangleIndices (3, 6, 8)); faces.Add (new TriangleIndices (3, 8, 9)); // 5 adjacent faces faces.Add (new TriangleIndices (4, 9, 5)); faces.Add (new TriangleIndices (2, 4, 11)); faces.Add (new TriangleIndices (6, 2, 10)); faces.Add (new TriangleIndices (8, 6, 7)); faces.Add (new TriangleIndices (9, 8, 1)); // refine triangles for (int i = 0; i < recursionLevel; i++) { var faces2 = new List<TriangleIndices> (); foreach (var tri in faces) { // replace triangle by 4 triangles int a = getMiddlePoint (tri.v1, tri.v2); int b = getMiddlePoint (tri.v2, tri.v3); int c = getMiddlePoint (tri.v3, tri.v1); faces2.Add (new TriangleIndices (tri.v1, a, c)); faces2.Add (new TriangleIndices (tri.v2, b, a)); faces2.Add (new TriangleIndices (tri.v3, c, b)); faces2.Add (new TriangleIndices (a, b, c)); } faces = faces2; } this.geometry.Faces = faces; // done, now add triangles to mesh foreach (var tri in faces) { this.geometry.MeshIndicies.Add (tri.v1); this.geometry.MeshIndicies.Add (tri.v2); this.geometry.MeshIndicies.Add (tri.v3); } return this.geometry; }
public MeshGeometry3D Create(int recursionLevel) { this.geometry = new MeshGeometry3D(); this.middlePointIndexCache = new Dictionary <long, int> (); this.index = 0; // create 12 vertices of a icosahedron float t = (float)((1.0f + Math.Sqrt(5.0)) / 2.0); addVertex(new Vector3(-1, t, 0f)); addVertex(new Vector3(1, t, 0f)); addVertex(new Vector3(-1, -t, 0f)); addVertex(new Vector3(1, -t, 0f)); addVertex(new Vector3(0, -1, t)); addVertex(new Vector3(0, 1, t)); addVertex(new Vector3(0, -1, -t)); addVertex(new Vector3(0, 1, -t)); addVertex(new Vector3(t, 0, -1)); addVertex(new Vector3(t, 0, 1)); addVertex(new Vector3(-t, 0, -1)); addVertex(new Vector3(-t, 0, 1)); // create 20 triangles of the icosahedron var faces = new List <TriangleIndices> (); // 5 faces around point 0 faces.Add(new TriangleIndices(0, 11, 5)); faces.Add(new TriangleIndices(0, 5, 1)); faces.Add(new TriangleIndices(0, 1, 7)); faces.Add(new TriangleIndices(0, 7, 10)); faces.Add(new TriangleIndices(0, 10, 11)); // 5 adjacent faces faces.Add(new TriangleIndices(1, 5, 9)); faces.Add(new TriangleIndices(5, 11, 4)); faces.Add(new TriangleIndices(11, 10, 2)); faces.Add(new TriangleIndices(10, 7, 6)); faces.Add(new TriangleIndices(7, 1, 8)); // 5 faces around point 3 faces.Add(new TriangleIndices(3, 9, 4)); faces.Add(new TriangleIndices(3, 4, 2)); faces.Add(new TriangleIndices(3, 2, 6)); faces.Add(new TriangleIndices(3, 6, 8)); faces.Add(new TriangleIndices(3, 8, 9)); // 5 adjacent faces faces.Add(new TriangleIndices(4, 9, 5)); faces.Add(new TriangleIndices(2, 4, 11)); faces.Add(new TriangleIndices(6, 2, 10)); faces.Add(new TriangleIndices(8, 6, 7)); faces.Add(new TriangleIndices(9, 8, 1)); // refine triangles for (int i = 0; i < recursionLevel; i++) { var faces2 = new List <TriangleIndices> (); foreach (var tri in faces) { // replace triangle by 4 triangles int a = getMiddlePoint(tri.v1, tri.v2); int b = getMiddlePoint(tri.v2, tri.v3); int c = getMiddlePoint(tri.v3, tri.v1); faces2.Add(new TriangleIndices(tri.v1, a, c)); faces2.Add(new TriangleIndices(tri.v2, b, a)); faces2.Add(new TriangleIndices(tri.v3, c, b)); faces2.Add(new TriangleIndices(a, b, c)); } faces = faces2; } this.geometry.Faces = faces; // done, now add triangles to mesh foreach (var tri in faces) { this.geometry.MeshIndicies.Add(tri.v1); this.geometry.MeshIndicies.Add(tri.v2); this.geometry.MeshIndicies.Add(tri.v3); } return(this.geometry); }