/
MechJebModuleDebugArrows.cs
770 lines (609 loc) · 25.9 KB
/
MechJebModuleDebugArrows.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
extern alias JetBrainsAnnotations;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;
namespace MuMech
{
internal class MechJebModuleDebugArrows : ComputerModule
{
[Persistent(pass = (int)Pass.GLOBAL)]
public bool displayAtCoM;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool seeThrough;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool comSphereActive;
public static DebugIcoSphere comSphere;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool colSphereActive;
public static DebugIcoSphere colSphere;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool cotSphereActive;
public static DebugIcoSphere cotSphere;
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableDouble comSphereRadius = new EditableDouble(0.09);
[Persistent(pass = (int)Pass.GLOBAL)]
public bool srfVelocityArrowActive;
public static DebugArrow srfVelocityArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool obtVelocityArrowActive;
public static DebugArrow obtVelocityArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool dotArrowActive;
public static DebugArrow dotArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool forwardArrowActive;
public static DebugArrow forwardArrow;
// Not used since I did not write the code for that one yet.
//[Persistent(pass = (int)Pass.Global)]
//public bool avgForwardArrowActive;
//public static DebugArrow avgForwardArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool requestedAttitudeArrowActive;
public static DebugArrow requestedAttitudeArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool debugArrowActive;
public static DebugArrow debugArrow;
[Persistent(pass = (int)Pass.GLOBAL)]
public bool debugArrow2Active;
public static DebugArrow debugArrow2;
public static Vector3d debugVector = Vector3d.one;
public static Vector3d debugVector2 = Vector3d.one;
[Persistent(pass = (int)Pass.GLOBAL)]
public readonly EditableDouble arrowsLength = new EditableDouble(4);
public MechJebModuleDebugArrows(MechJebCore core) : base(core)
{
Enabled = true;
}
public override void OnDestroy()
{
// Dirty cleanup. I m spending too much time on this already
if (comSphere == null)
return;
comSphere.Destroy();
comSphere = null;
colSphere.Destroy();
colSphere = null;
cotSphere.Destroy();
cotSphere = null;
srfVelocityArrow.Destroy();
srfVelocityArrow = null;
obtVelocityArrow.Destroy();
obtVelocityArrow = null;
dotArrow.Destroy();
dotArrow = null;
forwardArrow.Destroy();
forwardArrow = null;
//avgForwardArrow.Destroy();
//avgForwardArrow = null;
requestedAttitudeArrow.Destroy();
requestedAttitudeArrow = null;
debugArrow.Destroy();
debugArrow = null;
debugArrow2.Destroy();
debugArrow2 = null;
}
// TODO : I should probably use an array and an enum to lower code dup ...
public override void OnUpdate()
{
if (Vessel != FlightGlobals.ActiveVessel)
return;
if (comSphere == null)
{
comSphere = new DebugIcoSphere(XKCDColors.BloodRed, true);
colSphere = new DebugIcoSphere(XKCDColors.Teal, true);
cotSphere = new DebugIcoSphere(XKCDColors.PurplePink, true);
srfVelocityArrow = new DebugArrow(Color.green);
obtVelocityArrow = new DebugArrow(Color.red);
dotArrow = new DebugArrow(XKCDColors.PurplePink);
forwardArrow = new DebugArrow(XKCDColors.ElectricBlue);
//avgForwardArrow = new DebugArrow(Color.blue);
requestedAttitudeArrow = new DebugArrow(Color.gray);
debugArrow = new DebugArrow(XKCDColors.Fuchsia);
debugArrow2 = new DebugArrow(XKCDColors.LightBlue);
}
Vector3d frameVel =
(VesselState.orbitalVelocity - Krakensbane.GetFrameVelocity() - Vessel.orbit.GetRotFrameVel(Vessel.orbit.referenceBody).xzy) *
Time.fixedDeltaTime;
Vector3d instantCoM = VesselState.CoM + frameVel;
Vector3 arrowPos = displayAtCoM
? instantCoM
: (Vector3d)Vessel.ReferenceTransform.position;
comSphere.State(comSphereActive && Core.ShowGui);
if (comSphereActive)
{
comSphere.Set(instantCoM);
comSphere.SetRadius((float)comSphereRadius.Val);
}
colSphere.State(colSphereActive && VesselState.CoLScalar > 0 && Core.ShowGui);
if (colSphereActive)
{
colSphere.Set(VesselState.CoL + frameVel);
colSphere.SetRadius((float)comSphereRadius.Val);
}
cotSphere.State(cotSphereActive && VesselState.CoTScalar > 0 && Core.ShowGui);
if (cotSphereActive)
{
cotSphere.Set(VesselState.CoT + frameVel);
cotSphere.SetRadius((float)comSphereRadius.Val);
}
srfVelocityArrow.State(srfVelocityArrowActive && Core.ShowGui);
if (srfVelocityArrowActive)
{
srfVelocityArrow.Set(arrowPos, Vessel.srf_velocity);
srfVelocityArrow.SetLength((float)arrowsLength.Val);
srfVelocityArrow.SeeThrough(seeThrough);
}
obtVelocityArrow.State(obtVelocityArrowActive && Core.ShowGui);
if (obtVelocityArrowActive)
{
obtVelocityArrow.Set(arrowPos, Vessel.obt_velocity);
obtVelocityArrow.SetLength((float)arrowsLength.Val);
obtVelocityArrow.SeeThrough(seeThrough);
}
dotArrow.State(dotArrowActive && VesselState.thrustCurrent > 0 && Core.ShowGui);
if (dotArrowActive)
{
dotArrow.Set(VesselState.CoT + frameVel, VesselState.DoT);
dotArrow.SetLength((float)Math.Log10(VesselState.thrustCurrent + 1));
dotArrow.SeeThrough(seeThrough);
}
forwardArrow.State(forwardArrowActive && Core.ShowGui);
if (forwardArrowActive)
{
forwardArrow.Set(arrowPos, Vessel.GetTransform().up);
forwardArrow.SetLength((float)arrowsLength.Val);
forwardArrow.SeeThrough(seeThrough);
}
/*
avgForwardArrow.State(avgForwardArrowActive && core.ShowGui);
if (avgForwardArrowActive)
{
avgForwardArrow.Set(arrowPos, vesselState.forward);
avgForwardArrow.SetLength((float)arrowsLength.val);
avgForwardArrow.SeeThrough(seeThrough);
}
*/
requestedAttitudeArrow.State(requestedAttitudeArrowActive && Core.Attitude.Enabled && Core.ShowGui);
if (requestedAttitudeArrowActive && Core.Attitude.Enabled)
{
requestedAttitudeArrow.Set(arrowPos, Core.Attitude.RequestedAttitude);
requestedAttitudeArrow.SetLength((float)arrowsLength.Val);
requestedAttitudeArrow.SeeThrough(seeThrough);
}
debugArrow.State(debugArrowActive && Core.ShowGui);
if (debugArrowActive)
{
debugArrow.Set(Vessel.ReferenceTransform.position, debugVector);
debugArrow.SetLength((float)debugVector.magnitude);
debugArrow.SeeThrough(seeThrough);
}
debugArrow2.State(debugArrow2Active && Core.ShowGui);
if (debugArrow2Active)
{
//debugArrow2.Set(vessel.ReferenceTransform.position, debugVector2);
//
//debugArrow2.SetLength((float)debugVector2.magnitude);
//debugArrow2.SeeThrough(seeThrough);
Vector3d vector3d = VesselState.CoL - instantCoM + frameVel;
debugArrow2.Set(instantCoM, vector3d);
debugArrow2.SetLength((float)vector3d.magnitude);
debugArrow2.SeeThrough(seeThrough);
}
}
}
internal class DebugArrow
{
private readonly GameObject gameObject;
private readonly GameObject haft;
private GameObject cone;
private const float coneLength = 0.5f;
private float length;
private bool seeThrough;
private readonly MeshRenderer _haftMeshRenderer;
private readonly MeshRenderer _coneMeshRenderer;
public DebugArrow(Color color, bool seeThrough = false)
{
gameObject = new GameObject("DebugArrow");
gameObject.layer = 15; // Change layer. Not reentry effect that way (TODO : try 22)
haft = CreateCone(1f, 0.05f, 0.05f, 0f, 20);
haft.transform.parent = gameObject.transform;
haft.transform.localRotation = Quaternion.Euler(90, 0, 0);
haft.layer = 15;
cone = CreateCone(coneLength, 0.15f, 0f, 0f, 20);
cone.transform.parent = gameObject.transform;
cone.transform.localRotation = Quaternion.Euler(90, 0, 0);
cone.layer = 15;
SetLength(4);
_haftMeshRenderer = haft.AddComponent<MeshRenderer>();
_coneMeshRenderer = cone.AddComponent<MeshRenderer>();
_haftMeshRenderer.material.color = color;
_haftMeshRenderer.shadowCastingMode = ShadowCastingMode.Off;
_haftMeshRenderer.receiveShadows = false;
_coneMeshRenderer.material.color = color;
_coneMeshRenderer.shadowCastingMode = ShadowCastingMode.Off;
_coneMeshRenderer.receiveShadows = false;
SeeThrough(seeThrough);
}
public void Destroy()
{
if (gameObject != null)
Object.Destroy(gameObject);
}
public void SeeThrough(bool state)
{
if (seeThrough != state)
{
seeThrough = state;
_coneMeshRenderer.material.shader = state ? MechJebBundlesManager.diffuseAmbientIgnoreZ : MechJebBundlesManager.diffuseAmbient;
_haftMeshRenderer.material.shader = state ? MechJebBundlesManager.diffuseAmbientIgnoreZ : MechJebBundlesManager.diffuseAmbient;
}
}
public void SetLength(float length)
{
if (this.length == length)
return;
float conePos = length - coneLength;
if (conePos > 0)
{
this.length = length;
haft.transform.localScale = new Vector3(1f, conePos, 1f);
cone.transform.localPosition = new Vector3(0f, 0f, conePos);
cone.transform.localScale = new Vector3(1f, 1f, 1f);
}
else
{
this.length = length;
haft.transform.localScale = new Vector3(1f, 0, 1f);
cone.transform.localPosition = new Vector3(0f, 0f, 0);
cone.transform.localScale = new Vector3(length / coneLength, length / coneLength, length / coneLength);
}
}
public void Set(Vector3d position, Vector3d direction)
{
if (direction.sqrMagnitude < 0.001)
{
State(false);
return;
}
Set(position, Quaternion.LookRotation(direction));
}
public void Set(Vector3d position, Quaternion direction)
{
gameObject.transform.position = position;
gameObject.transform.rotation = direction;
}
public void State(bool state)
{
if (state != gameObject.activeSelf)
gameObject.SetActive(state);
}
// From http://wiki.unity3d.com/index.php/ProceduralPrimitives
// TODO : merge with the cylinder code on that page and create the full arrow instead of using 2 mesh.
private GameObject CreateCone(float height, float bottomRadius, float topRadius, float offset, int nbSides)
{
cone = new GameObject();
MeshFilter filter = cone.AddComponent<MeshFilter>();
Mesh mesh = filter.mesh;
mesh.Clear();
int nbVerticesCap = nbSides + 1;
#region Vertices
// bottom + top + sides
var vertices = new Vector3[nbVerticesCap + nbVerticesCap + nbSides * 2 + 2];
int vert = 0;
float _2pi = Mathf.PI * 2f;
// Bottom cap
vertices[vert++] = new Vector3(0f, offset, 0f);
while (vert <= nbSides)
{
float rad = (float)vert / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * bottomRadius, offset, Mathf.Sin(rad) * bottomRadius);
vert++;
}
// Top cap
vertices[vert++] = new Vector3(0f, offset + height, 0f);
while (vert <= nbSides * 2 + 1)
{
float rad = (float)(vert - nbSides - 1) / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, offset + height, Mathf.Sin(rad) * topRadius);
vert++;
}
// Sides
int v = 0;
while (vert <= vertices.Length - 4)
{
float rad = (float)v / nbSides * _2pi;
vertices[vert] = new Vector3(Mathf.Cos(rad) * topRadius, offset + height, Mathf.Sin(rad) * topRadius);
vertices[vert + 1] = new Vector3(Mathf.Cos(rad) * bottomRadius, offset, Mathf.Sin(rad) * bottomRadius);
vert += 2;
v++;
}
vertices[vert] = vertices[nbSides * 2 + 2];
vertices[vert + 1] = vertices[nbSides * 2 + 3];
#endregion
#region Normales
// bottom + top + sides
var normales = new Vector3[vertices.Length];
vert = 0;
// Bottom cap
while (vert <= nbSides)
{
normales[vert++] = Vector3.down;
}
// Top cap
while (vert <= nbSides * 2 + 1)
{
normales[vert++] = Vector3.up;
}
// Sides
v = 0;
while (vert <= vertices.Length - 4)
{
float rad = (float)v / nbSides * _2pi;
float cos = Mathf.Cos(rad);
float sin = Mathf.Sin(rad);
normales[vert] = new Vector3(cos, 0f, sin);
normales[vert + 1] = normales[vert];
vert += 2;
v++;
}
normales[vert] = normales[nbSides * 2 + 2];
normales[vert + 1] = normales[nbSides * 2 + 3];
#endregion
#region UVs
var uvs = new Vector2[vertices.Length];
// Bottom cap
int u = 0;
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides)
{
float rad = (float)u / nbSides * _2pi;
uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
u++;
}
// Top cap
uvs[u++] = new Vector2(0.5f, 0.5f);
while (u <= nbSides * 2 + 1)
{
float rad = (float)u / nbSides * _2pi;
uvs[u] = new Vector2(Mathf.Cos(rad) * .5f + .5f, Mathf.Sin(rad) * .5f + .5f);
u++;
}
// Sides
int u_sides = 0;
while (u <= uvs.Length - 4)
{
float t = (float)u_sides / nbSides;
uvs[u] = new Vector3(t, 1f);
uvs[u + 1] = new Vector3(t, 0f);
u += 2;
u_sides++;
}
uvs[u] = new Vector2(1f, 1f);
uvs[u + 1] = new Vector2(1f, 0f);
#endregion
#region Triangles
int nbTriangles = nbSides + nbSides + nbSides * 2;
int[] triangles = new int[nbTriangles * 3 + 3];
// Bottom cap
int tri = 0;
int i = 0;
while (tri < nbSides - 1)
{
triangles[i] = 0;
triangles[i + 1] = tri + 1;
triangles[i + 2] = tri + 2;
tri++;
i += 3;
}
triangles[i] = 0;
triangles[i + 1] = tri + 1;
triangles[i + 2] = 1;
tri++;
i += 3;
// Top cap
//tri++;
while (tri < nbSides * 2)
{
triangles[i] = tri + 2;
triangles[i + 1] = tri + 1;
triangles[i + 2] = nbVerticesCap;
tri++;
i += 3;
}
triangles[i] = nbVerticesCap + 1;
triangles[i + 1] = tri + 1;
triangles[i + 2] = nbVerticesCap;
tri++;
i += 3;
tri++;
// Sides
while (tri <= nbTriangles)
{
triangles[i] = tri + 2;
triangles[i + 1] = tri + 1;
triangles[i + 2] = tri + 0;
tri++;
i += 3;
triangles[i] = tri + 1;
triangles[i + 1] = tri + 2;
triangles[i + 2] = tri + 0;
tri++;
i += 3;
}
#endregion
mesh.vertices = vertices;
mesh.normals = normales;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateBounds();
return cone;
}
}
internal class DebugIcoSphere
{
private readonly GameObject gameObject;
private readonly MeshRenderer _meshRenderer;
private float radius;
private bool seeThrough;
public DebugIcoSphere(Color color, bool seeThrough = false)
{
gameObject = CreateIcoSphere(1);
gameObject.layer = 15; // Change layer. Not reentry effect that way (TODO : try 22)
_meshRenderer = gameObject.AddComponent<MeshRenderer>();
_meshRenderer.material.color = color;
_meshRenderer.shadowCastingMode = ShadowCastingMode.Off;
_meshRenderer.receiveShadows = false;
SetRadius(0.09f);
SeeThrough(seeThrough);
}
public void Destroy() => Object.Destroy(gameObject);
public void Set(Vector3d position) => gameObject.transform.position = position;
public void State(bool state)
{
if (state != gameObject.activeSelf)
gameObject.SetActive(state);
}
public void SetRadius(float radius)
{
if (this.radius == radius || radius <= 0)
return;
this.radius = radius;
gameObject.transform.localScale = new Vector3(radius, radius, radius);
}
public void SeeThrough(bool state)
{
if (seeThrough != state)
{
seeThrough = state;
_meshRenderer.material.shader = state ? MechJebBundlesManager.diffuseAmbientIgnoreZ : MechJebBundlesManager.diffuseAmbient;
}
}
private static GameObject CreateIcoSphere(float radius, int recursionLevel = 3)
{
var gameObject = new GameObject("DebugIcoSphere");
gameObject.layer = 15; // Change layer. Not reentry effect that way (TODO : try 22)
MeshFilter filter = gameObject.AddComponent<MeshFilter>();
Mesh mesh = filter.mesh;
mesh.Clear();
var vertList = new List<Vector3>();
var middlePointIndexCache = new Dictionary<long, int>();
// create 12 vertices of a icosahedron
float t = (1f + Mathf.Sqrt(5f)) / 2f;
vertList.Add(new Vector3(-1f, t, 0f).normalized * radius);
vertList.Add(new Vector3(1f, t, 0f).normalized * radius);
vertList.Add(new Vector3(-1f, -t, 0f).normalized * radius);
vertList.Add(new Vector3(1f, -t, 0f).normalized * radius);
vertList.Add(new Vector3(0f, -1f, t).normalized * radius);
vertList.Add(new Vector3(0f, 1f, t).normalized * radius);
vertList.Add(new Vector3(0f, -1f, -t).normalized * radius);
vertList.Add(new Vector3(0f, 1f, -t).normalized * radius);
vertList.Add(new Vector3(t, 0f, -1f).normalized * radius);
vertList.Add(new Vector3(t, 0f, 1f).normalized * radius);
vertList.Add(new Vector3(-t, 0f, -1f).normalized * radius);
vertList.Add(new Vector3(-t, 0f, 1f).normalized * radius);
// 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>();
for (int j = 0; j < faces.Count; j++)
{
TriangleIndices tri = faces[j];
// replace triangle by 4 triangles
int a = getMiddlePoint(tri.v1, tri.v2, ref vertList, ref middlePointIndexCache, radius);
int b = getMiddlePoint(tri.v2, tri.v3, ref vertList, ref middlePointIndexCache, radius);
int c = getMiddlePoint(tri.v3, tri.v1, ref vertList, ref middlePointIndexCache, radius);
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;
}
mesh.vertices = vertList.ToArray();
var triList = new List<int>();
for (int i = 0; i < faces.Count; i++)
{
triList.Add(faces[i].v1);
triList.Add(faces[i].v2);
triList.Add(faces[i].v3);
}
// The UV and normals are wrong, but it works for my needs.
mesh.triangles = triList.ToArray();
mesh.uv = new Vector2[vertList.Count];
var normales = new Vector3[vertList.Count];
for (int i = 0; i < normales.Length; i++)
normales[i] = vertList[i].normalized;
mesh.normals = normales;
mesh.RecalculateBounds();
return gameObject;
}
private struct TriangleIndices
{
public readonly int v1;
public readonly int v2;
public readonly int v3;
public TriangleIndices(int v1, int v2, int v3)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
}
// return index of point in the middle of p1 and p2
private static int getMiddlePoint(int p1, int p2, ref List<Vector3> vertices, ref Dictionary<long, int> cache, float radius)
{
// first check if we have it already
bool firstIsSmaller = p1 < p2;
long smallerIndex = firstIsSmaller ? p1 : p2;
long greaterIndex = firstIsSmaller ? p2 : p1;
long key = (smallerIndex << 32) + greaterIndex;
if (cache.TryGetValue(key, out int ret))
{
return ret;
}
// not in cache, calculate it
Vector3 point1 = vertices[p1];
Vector3 point2 = vertices[p2];
var middle = new Vector3
(
(point1.x + point2.x) / 2f,
(point1.y + point2.y) / 2f,
(point1.z + point2.z) / 2f
);
// add vertex makes sure point is on unit sphere
int i = vertices.Count;
vertices.Add(middle.normalized * radius);
// store it, return index
cache.Add(key, i);
return i;
}
}
}