-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicosahedron.cs
638 lines (493 loc) · 18.2 KB
/
icosahedron.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Icosahedron : MonoBehaviour
{
#region Variables
[Header("Scene Refrences")]
[SerializeField] private GameObject[] hexagonPrefabs;
[SerializeField] private GameObject pentagonPrefab;
//Hidden
private int resolution = 2; //Number of subdivisions for the sphere
private GameObject sphereMesh;
private IcosahedronGenerator icosahedron;
private List<Hexagon> sphereHexes;
private Mesh mesh;
private float hexScale1 = 17f; //Change if hexagon scale is not right
private float hexScale2 = 8.5f; //Change if hexagon scale is not right
private float hexScale3 = 4.3f; //Change if hexagon scale is not right
private float hexScale4 = 2.4f; //Change if hexagon scale is not right
private float hexScale5 = 1f; //Change if hexagon scale is not right
[HideInInspector] public bool check = false;
#endregion
#region Unity Functions
private void Awake()
{
// GenerateMesh();
}
#endregion
#region Functions
//If you would like to use this script to generate modular icosahedron
//mesh you will just need to add mesh renderer and filter to sphere
//mesh object and modify code a bit
public IEnumerator GenerateMeshSub(int subdivisions, float scale)
{
check = false;
icosahedron = new IcosahedronGenerator();
icosahedron.Initialize();
StartCoroutine(icosahedron.Subdivide(subdivisions));
yield return new WaitUntil(() => icosahedron.check);
icosahedron.MapVertFace();
icosahedron.SortPolygons();
icosahedron.CreateTiles();
PrepareMesh();
StartCoroutine(CreateHexagons(subdivisions, scale));
icosahedron.CleanUp();
CleanUp2();
check = true;
}
private IEnumerator CreateHexagons(int res, float scale = 1)
{
GameObject go2 = new GameObject("Sphere");
go2.transform.SetParent(transform, true);
//Create hexagon for each entry in list
foreach (var i in sphereHexes)
{
//check if is hexagon
if (i.verts[5] != Vector3.zero)
{
//Instantiate
GameObject go = Instantiate(hexagonPrefabs[Random.Range(0,hexagonPrefabs.Length)]);
go.name = "Hex";
switch (res)
{
case 1:
go.transform.localScale = new Vector3(hexScale1, hexScale1, hexScale1);
break;
case 2:
go.transform.localScale = new Vector3(hexScale2, hexScale2, hexScale2);
break;
case 3:
go.transform.localScale = new Vector3(hexScale3, hexScale3, hexScale3);
break;
case 4:
go.transform.localScale = new Vector3(hexScale4, hexScale4, hexScale4);
break;
case 5:
go.transform.localScale = new Vector3(hexScale5, hexScale5, hexScale5);
break;
}
//Calculate middle of the hexagon
go.transform.position = Vector3.Lerp(i.verts[0], i.verts[4], .5f);
//Calculate rotation of the hexagon
Vector3 vecup = i.verts[4] - i.verts[0];
Vector3 vec = Vector3.zero - go.transform.position;
go.transform.rotation = Quaternion.LookRotation(vec, vecup);
//Set parent to keep hierarchy clean
go.transform.SetParent(go2.transform, true);
yield return null;
}
else if(pentagonPrefab != null)
{
//Instantiate
GameObject go = Instantiate(pentagonPrefab);
go.name = "Hex";
switch (res)
{
case 1:
go.transform.localScale = new Vector3(hexScale1, hexScale1, hexScale1);
break;
case 2:
go.transform.localScale = new Vector3(hexScale2, hexScale2, hexScale2);
break;
case 3:
go.transform.localScale = new Vector3(hexScale3, hexScale3, hexScale3);
break;
case 4:
go.transform.localScale = new Vector3(hexScale4, hexScale4, hexScale4);
break;
case 5:
go.transform.localScale = new Vector3(hexScale5, hexScale5, hexScale5);
break;
}
//Calculate middle of the hexagon
go.transform.position = Vector3.Lerp(i.verts[0], i.verts[4], .5f);
//Calculate rotation of the hexagon
Vector3 vecup = i.verts[4] - i.verts[0];
Vector3 vec = Vector3.zero - go.transform.position;
go.transform.rotation = Quaternion.LookRotation(vec, vecup);
//Set parent to keep hierarchy clean
go.transform.SetParent(go2.transform, true);
yield return null;
}
}
go2.transform.localScale = new Vector3(scale, scale, scale);
}
private void PrepareMesh()
{
sphereHexes = icosahedron.hexes;
this.sphereMesh = new GameObject("Sphere Mesh");
this.sphereMesh.transform.parent = this.transform;
mesh = new Mesh();
int vertexCount = icosahedron.Polygons.Count * 3;
int[] indices = new int[vertexCount];
Vector3[] vertices = new Vector3[vertexCount];
Vector3[] normals = new Vector3[vertexCount];
for (int i = 0; i < icosahedron.Polygons.Count; i++)
{
var poly = icosahedron.Polygons[i];
indices[i * 3 + 0] = i * 3 + 0;
indices[i * 3 + 1] = i * 3 + 1;
indices[i * 3 + 2] = i * 3 + 2;
vertices[i * 3 + 0] = icosahedron.Vertices[poly.vertices[0]];
vertices[i * 3 + 1] = icosahedron.Vertices[poly.vertices[1]];
vertices[i * 3 + 2] = icosahedron.Vertices[poly.vertices[2]];
normals[i * 3 + 0] = icosahedron.Vertices[poly.vertices[0]];
normals[i * 3 + 1] = icosahedron.Vertices[poly.vertices[1]];
normals[i * 3 + 2] = icosahedron.Vertices[poly.vertices[2]];
}
mesh.vertices = vertices;
mesh.normals = normals;
mesh.SetTriangles(indices, 0);
}
private void CleanUp2()
{
Destroy(sphereMesh);
mesh = null;
sphereMesh = null;
icosahedron= null;
}
#endregion
}
///GENERATOR
#region Icosahendron Generator
public class IcosahedronGenerator
{
//Verices and polygons
private List<Polygon> polygons;
private List<Vector3> vertices;
//List of vertices after creating hexagon tiles
public List<Vector3> newVerts;
//List of created hexagons
public List<Hexagon> hexes;
//List of VMaps for mesh
public List<VMap> vMaps;
//Get set refrences
public List<Polygon> Polygons { get => polygons; private set => polygons = value; }
public List<Vector3> Vertices { get => vertices; set => vertices = value; }
public bool check = false;
//Init
public void Initialize()
{
check = false;
polygons = new List<Polygon>();
vertices = new List<Vector3>();
// Icosahedron has 12 vertices
float t = (1.0f + Mathf.Sqrt(5.0f)) / 2.0f;
vertices.Add(new Vector3(-1, t, 0).normalized);
vertices.Add(new Vector3(1, t, 0).normalized);
vertices.Add(new Vector3(-1, -t, 0).normalized);
vertices.Add(new Vector3(1, -t, 0).normalized);
vertices.Add(new Vector3(0, -1, t).normalized);
vertices.Add(new Vector3(0, 1, t).normalized);
vertices.Add(new Vector3(0, -1, -t).normalized);
vertices.Add(new Vector3(0, 1, -t).normalized);
vertices.Add(new Vector3(t, 0, -1).normalized);
vertices.Add(new Vector3(t, 0, 1).normalized);
vertices.Add(new Vector3(-t, 0, -1).normalized);
vertices.Add(new Vector3(-t, 0, 1).normalized);
// Create 20 faces
polygons.Add(new Polygon(0, 11, 5));
polygons.Add(new Polygon(0, 5, 1));
polygons.Add(new Polygon(0, 1, 7));
polygons.Add(new Polygon(0, 7, 10));
polygons.Add(new Polygon(0, 10, 11));
polygons.Add(new Polygon(1, 5, 9));
polygons.Add(new Polygon(5, 11, 4));
polygons.Add(new Polygon(11, 10, 2));
polygons.Add(new Polygon(10, 7, 6));
polygons.Add(new Polygon(7, 1, 8));
polygons.Add(new Polygon(3, 9, 4));
polygons.Add(new Polygon(3, 4, 2));
polygons.Add(new Polygon(3, 2, 6));
polygons.Add(new Polygon(3, 6, 8));
polygons.Add(new Polygon(3, 8, 9));
polygons.Add(new Polygon(4, 9, 5));
polygons.Add(new Polygon(2, 4, 11));
polygons.Add(new Polygon(6, 2, 10));
polygons.Add(new Polygon(8, 6, 7));
polygons.Add(new Polygon(9, 8, 1));
}
//Used to subdivide mesh
public IEnumerator Subdivide(int recursions)
{
check = false;
var midPointCache = new Dictionary<int, int>();
for (int i = 0; i < recursions; i++)
{
var newPolys = new List<Polygon>();
foreach (var poly in polygons)
{
int a = poly.vertices[0];
int b = poly.vertices[1];
int c = poly.vertices[2];
// Use GetMidPointIndex to either create a new vertex between two old vertices
int ab = GetMidPointIndex(midPointCache, a, b);
int bc = GetMidPointIndex(midPointCache, b, c);
int ca = GetMidPointIndex(midPointCache, c, a);
// Create the four new polygons using our original
newPolys.Add(new Polygon(a, ab, ca));
newPolys.Add(new Polygon(b, bc, ab));
newPolys.Add(new Polygon(c, ca, bc));
newPolys.Add(new Polygon(ab, bc, ca));
}
// Replace all our old polygons with the new set of subdivided ones.
polygons = newPolys;
yield return null;
}
check = true;
}
//Map Vert Faces
public void MapVertFace()
{
Polygon f;
int pos;
vMaps = new List<VMap>(vertices.Count);
foreach (var face in polygons)
{
for (var i = 0; i < 3; i++)
{
//Clone face since we want to modify it
f = face.Clone();
//Get where in the array the point exists in the face
pos = f.vertices.IndexOf(face.vertices[i]);
//If vert is not first move it to first place
if (pos != 0)
f.MoveToStart(pos);
bool con = false;
//Check if there is entry in list for this vertex
foreach (var item in vMaps)
{
if (item.vert == face.vertices[i])
{
//If so add it
item.faces.Add(f);
con = true;
break;
}
}
//If not create one
if (!con)
{
VMap map = new VMap(face.vertices[i], new List<Polygon>(5));
map.faces.Add(f);
vMaps.Add(map);
}
}
}
}
public void SortPolygons()
{
VMap list; //Unsorted list
VMap oList; //Sorted list
int a; // Vertex index looking for
int b; // Vertex index looking for
int i; // Loop Index
Polygon face; // Reference polygon
// Store old VMaps in placeholder and clear it
List<VMap> _vmaps = new List<VMap>(vMaps.Count);
foreach (var item in vMaps)
{
_vmaps.Add(item);
}
vMaps.Clear();
for (int m = 0; m < _vmaps.Count; m++)
{
//Copy list
list = _vmaps[m];
oList = new VMap(list.vert, new List<Polygon>(5));
oList.faces.Add(list.faces[0]);
//Sort
a = list.faces[0].vertices[0];
b = list.faces[0].vertices[1];
i = 0;
while (list.faces.Count > 0 && i < list.faces.Count)
{
face = list.faces[i];
if (face.vertices.IndexOf(a) != -1 && face.vertices.IndexOf(b) != -1)
{
list.faces.RemoveAt(i);
oList.faces.Add(face);
a = face.vertices[0];
b = face.vertices[1];
i = 0;
continue;
}
i++;
}
vMaps.Add(oList);
}
}
//Create Hex tiles
public void CreateTiles()
{
//Centroid of polygon
float[] fcPnt = new float[]
{
0, 0, 0
};
//Our map
Map cache = new Map(vMaps.Count * 6);
//List for newly created vertices
newVerts = new List<Vector3>(vertices.Count);
int vCnt = 0;
int ip;
//Key for mapping
string key = "";
//List used to store vertices in hexagons
hexes = new List<Hexagon>(vMaps.Count);
foreach (var vmap in vMaps)
{
for (int i = 0; i < vmap.faces.Count - 1; i++)
{
//Get centroid
fcPnt = vmap.faces[i].Centroid(Vertices);
//Create key
key = fcPnt[0] + "_" + fcPnt[1] + "_" + fcPnt[2];
//If map has key get ip and add to hex
if (cache.Has(key))
{
ip = cache.Get(key);
hexes[vMaps.IndexOf(vmap)].verts[i] = new Vector3(fcPnt[0], fcPnt[1], fcPnt[2]);
}
//Else add new key increment ip and create new hex
else
{
ip = vCnt++;
cache.Set(key, ip);
Vector3 vec = new Vector3(fcPnt[0], fcPnt[1], fcPnt[2]);
newVerts.Add(vec);
Hexagon hex = new Hexagon();
hexes.Add(hex);
hexes[vMaps.IndexOf(vmap)].verts[i] = new Vector3(fcPnt[0], fcPnt[1], fcPnt[2]);
}
}
}
}
//Destroy mesh and everything unnecessary
public void CleanUp()
{
polygons = null;
vertices = null;
newVerts = null;
hexes = null;
vMaps = null;
}
public int GetMidPointIndex(Dictionary<int, int> cache, int indexA, int indexB)
{
int smallerIndex = Mathf.Min(indexA, indexB);
int greaterIndex = Mathf.Max(indexA, indexB);
int key = (smallerIndex << 16) + greaterIndex;
// If a midpoint is already defined, just return it.
int ret;
if (cache.TryGetValue(key, out ret))
return ret;
// If we're here, it's because a midpoint for these two vertices hasn't been created yet.
Vector3 p1 = vertices[indexA];
Vector3 p2 = vertices[indexB];
Vector3 middle = Vector3.Lerp(p1, p2, 0.5f).normalized;
ret = vertices.Count;
vertices.Add(middle);
cache.Add(key, ret);
return ret;
}
}
#endregion
///UTILITY STRUCTS
#region Utility Structs
//Used to create Vertex Maps
public class VMap
{
public readonly int vert;
public List<Polygon> faces;
public VMap(int v, List<Polygon> f)
{
vert = v;
faces = f;
}
}
//Used to map tiles
public class Map
{
public List<string> keys = new List<string>();
public Map(int l)
{
keys = new List<string>(l);
}
public void Set(string s, int i)
{
keys.Insert(i, s);
}
public int Get(string s)
{
return keys.IndexOf(s);
}
public bool Has(string s)
{
if (keys.Contains(s)) return true;
else
return false;
}
}
//Used to handle hexagons after making tiles
public class Hexagon
{
public Vector3[] verts = new Vector3[6];
}
//Polygon class used to handle polygons
public class Polygon
{
//Vertices of the polygon
public readonly List<int> vertices;
public Polygon(int a, int b, int c)
{
vertices = new List<int>() { a, b, c };
}
//Clone
public Polygon Clone()
{
var f = new Polygon(vertices[0], vertices[1], vertices[2]);
return f;
}
//Move indexed vertex to start of the array
public void MoveToStart(int pos)
{
var tmp = this.vertices[pos];
if (pos == 1) { this.vertices[1] = this.vertices[2]; this.vertices[2] = this.vertices[0]; this.vertices[0] = tmp; }
else if (pos == 2) { this.vertices[2] = this.vertices[1]; this.vertices[1] = this.vertices[0]; this.vertices[0] = tmp; }
}
//Get centroid point of polygon
public float[] Centroid(List<Vector3> verts)
{
float[] a = null;
if (a != null)
a[0] = a[1] = a[2] = 0;
else
a = new float[]
{
0, 0, 0
};
for (var i = 0; i < vertices.Count; i++)
{
var v = verts[vertices[i]];
a[0] += v[0];
a[1] += v[1];
a[2] += v[2];
}
a[0] = a[0] / 3;
a[1] = a[1] / 3;
a[2] = a[2] / 3;
return a;
}
}
#endregion