-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
MeshDraftPrimitives.cs
817 lines (738 loc) · 32.2 KB
/
MeshDraftPrimitives.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
using System;
using System.Collections.Generic;
using UnityEngine;
namespace ProceduralToolkit
{
/// <summary>
/// Helper class for procedural mesh generation
/// </summary>
public partial class MeshDraft
{
#region Platonic solids
/// <summary>
/// Constructs a tetrahedron draft
/// </summary>
public static MeshDraft Tetrahedron(float radius, bool generateUV = true)
{
const float tetrahedralAngle = -19.471220333f;
var vertex0 = new Vector3(0, radius, 0);
var vertex1 = Geometry.PointOnSphere(radius, 0, tetrahedralAngle);
var vertex2 = Geometry.PointOnSphere(radius, 120, tetrahedralAngle);
var vertex3 = Geometry.PointOnSphere(radius, 240, tetrahedralAngle);
var draft = new MeshDraft {name = "Tetrahedron"};
if (generateUV)
{
var uv0 = new Vector2(0, 0);
var uv1 = new Vector2(0.5f, 1);
var uv2 = new Vector2(1, 0);
draft.AddTriangle(vertex2, vertex0, vertex1, true, uv0, uv1, uv2)
.AddTriangle(vertex2, vertex1, vertex3, true, uv0, uv1, uv2)
.AddTriangle(vertex3, vertex0, vertex2, true, uv0, uv1, uv2)
.AddTriangle(vertex1, vertex0, vertex3, true, uv0, uv1, uv2);
}
else
{
draft.AddTriangle(vertex2, vertex0, vertex1, true)
.AddTriangle(vertex2, vertex1, vertex3, true)
.AddTriangle(vertex3, vertex0, vertex2, true)
.AddTriangle(vertex1, vertex0, vertex3, true);
}
return draft;
}
/// <summary>
/// Constructs a cube draft
/// </summary>
public static MeshDraft Cube(float side, bool generateUV = true)
{
var draft = Hexahedron(side, side, side, generateUV);
draft.name = "Cube";
return draft;
}
/// <summary>
/// Constructs a hexahedron draft
/// </summary>
public static MeshDraft Hexahedron(float width, float length, float height, bool generateUV = true)
{
return Hexahedron(Vector3.right*width, Vector3.forward*length, Vector3.up*height, generateUV);
}
/// <summary>
/// Constructs a hexahedron draft
/// </summary>
public static MeshDraft Hexahedron(Vector3 width, Vector3 length, Vector3 height, bool generateUV = true)
{
Vector3 v000 = -width/2 - length/2 - height/2;
Vector3 v001 = v000 + height;
Vector3 v010 = v000 + width;
Vector3 v011 = v000 + width + height;
Vector3 v100 = v000 + length;
Vector3 v101 = v000 + length + height;
Vector3 v110 = v000 + width + length;
Vector3 v111 = v000 + width + length + height;
var draft = new MeshDraft {name = "Hexahedron"};
if (generateUV)
{
Vector2 uv0 = new Vector2(0, 0);
Vector2 uv1 = new Vector2(0, 1);
Vector2 uv2 = new Vector2(1, 1);
Vector2 uv3 = new Vector2(1, 0);
draft.AddQuad(v100, v101, v001, v000, Vector3.left, uv0, uv1, uv2, uv3)
.AddQuad(v010, v011, v111, v110, Vector3.right, uv0, uv1, uv2, uv3)
.AddQuad(v010, v110, v100, v000, Vector3.down, uv0, uv1, uv2, uv3)
.AddQuad(v111, v011, v001, v101, Vector3.up, uv0, uv1, uv2, uv3)
.AddQuad(v000, v001, v011, v010, Vector3.back, uv0, uv1, uv2, uv3)
.AddQuad(v110, v111, v101, v100, Vector3.forward, uv0, uv1, uv2, uv3);
}
else
{
draft.AddQuad(v100, v101, v001, v000, Vector3.left)
.AddQuad(v010, v011, v111, v110, Vector3.right)
.AddQuad(v010, v110, v100, v000, Vector3.down)
.AddQuad(v111, v011, v001, v101, Vector3.up)
.AddQuad(v000, v001, v011, v010, Vector3.back)
.AddQuad(v110, v111, v101, v100, Vector3.forward);
}
return draft;
}
/// <summary>
/// Constructs a octahedron draft
/// </summary>
public static MeshDraft Octahedron(float radius, bool generateUV = true)
{
var draft = BiPyramid(radius, 4, radius*2, generateUV);
draft.name = "Octahedron";
return draft;
}
/// <summary>
/// Constructs a dodecahedron draft
/// </summary>
public static MeshDraft Dodecahedron(float radius)
{
const float magicAngle1 = 52.62263590f;
const float magicAngle2 = 10.81231754f;
const float segmentAngle = 72;
float lowerAngle = 0;
float upperAngle = segmentAngle/2;
var lowerCap = new Vector3[5];
var lowerRing = new Vector3[5];
var upperCap = new Vector3[5];
var upperRing = new Vector3[5];
for (var i = 0; i < 5; i++)
{
lowerCap[i] = Geometry.PointOnSphere(radius, lowerAngle, -magicAngle1);
lowerRing[i] = Geometry.PointOnSphere(radius, lowerAngle, -magicAngle2);
upperCap[i] = Geometry.PointOnSphere(radius, upperAngle, magicAngle1);
upperRing[i] = Geometry.PointOnSphere(radius, upperAngle, magicAngle2);
lowerAngle += segmentAngle;
upperAngle += segmentAngle;
}
var draft = new MeshDraft {name = "Dodecahedron"}
.AddTriangleFan(upperCap, Vector3.up)
.AddFlatTriangleBand(upperRing, upperCap, false)
.AddFlatTriangleBand(lowerRing, upperRing, false)
.AddFlatTriangleBand(lowerCap, lowerRing, false)
.AddTriangleFan(lowerCap, Vector3.down, true);
return draft;
}
/// <summary>
/// Constructs a icosahedron draft
/// </summary>
public static MeshDraft Icosahedron(float radius, bool generateUV = true)
{
const float magicAngle = 26.56505f;
const float segmentAngle = 72;
float lowerAngle = 0;
float upperAngle = segmentAngle/2;
var lowerRing = new Vector3[5];
var upperRing = new Vector3[5];
for (var i = 0; i < 5; i++)
{
lowerRing[i] = Geometry.PointOnSphere(radius, lowerAngle, -magicAngle);
upperRing[i] = Geometry.PointOnSphere(radius, upperAngle, magicAngle);
lowerAngle += segmentAngle;
upperAngle += segmentAngle;
}
var draft = new MeshDraft {name = "Icosahedron"}
.AddBaselessPyramid(new Vector3(0, radius, 0), upperRing, generateUV)
.AddFlatTriangleBand(lowerRing, upperRing, generateUV)
.AddBaselessPyramid(new Vector3(0, -radius, 0), lowerRing, generateUV, true);
return draft;
}
#endregion Platonic solids
/// <summary>
/// Constructs a quad draft
/// </summary>
public static MeshDraft Quad(Vector3 origin, Vector3 width, Vector3 height, bool generateUV = true)
{
var draft = new MeshDraft {name = "Quad"};
if (generateUV)
{
draft.AddQuad(origin, width, height, true, new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0));
}
else
{
draft.AddQuad(origin, width, height, true);
}
return draft;
}
/// <summary>
/// Constructs a quad draft
/// </summary>
public static MeshDraft Quad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3, bool generateUV = true)
{
var draft = new MeshDraft {name = "Quad"};
if (generateUV)
{
draft.AddQuad(vertex0, vertex1, vertex2, vertex3, true, new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0));
}
else
{
draft.AddQuad(vertex0, vertex1, vertex2, vertex3, true);
}
return draft;
}
/// <summary>
/// Constructs a plane draft
/// </summary>
public static MeshDraft Plane(float xSize = 1, float zSize = 1, int xSegments = 1, int zSegments = 1, bool generateUV = true)
{
float xStep = xSize/xSegments;
float zStep = zSize/zSegments;
var vertexCount = (xSegments + 1)*(zSegments + 1);
var draft = new MeshDraft
{
name = "Plane",
vertices = new List<Vector3>(vertexCount),
triangles = new List<int>(xSegments*zSegments*6),
normals = new List<Vector3>(vertexCount)
};
for (int z = 0; z <= zSegments; z++)
{
for (int x = 0; x <= xSegments; x++)
{
draft.vertices.Add(new Vector3(x*xStep, 0f, z*zStep));
draft.normals.Add(Vector3.up);
if (generateUV)
{
draft.uv.Add(new Vector2((float) x/xSegments, (float) z/zSegments));
}
}
}
int i = 0;
for (int z = 0; z < zSegments; z++)
{
for (int x = 0; x < xSegments; x++)
{
draft.triangles.Add(i);
draft.triangles.Add(i + xSegments + 1);
draft.triangles.Add(i + 1);
draft.triangles.Add(i + 1);
draft.triangles.Add(i + xSegments + 1);
draft.triangles.Add(i + xSegments + 2);
i++;
}
i++;
}
return draft;
}
/// <summary>
/// Constructs a pyramid draft
/// </summary>
public static MeshDraft Pyramid(float radius, int segments, float height, bool generateUV = true)
{
float segmentAngle = 360f/segments;
float currentAngle = 0;
var ring = new Vector3[segments];
for (var i = 0; i < segments; i++)
{
ring[i] = Geometry.PointOnCircle3XZ(radius, currentAngle);
currentAngle += segmentAngle;
}
var draft = new MeshDraft().AddBaselessPyramid(Vector3.up*height, ring, generateUV);
if (generateUV)
{
var fanUV = new Vector2[segments];
currentAngle = 0;
for (var i = 0; i < segments; i++)
{
Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f);
uv.x = 1 - uv.x;
fanUV[i] = uv;
currentAngle += segmentAngle;
}
draft.AddTriangleFan(ring, Vector3.down, fanUV, true);
}
else
{
draft.AddTriangleFan(ring, Vector3.down, true);
}
draft.name = "Pyramid";
return draft;
}
/// <summary>
/// Constructs a bipyramid draft
/// </summary>
public static MeshDraft BiPyramid(float radius, int segments, float height, bool generateUV = true)
{
float segmentAngle = 360f/segments;
float currentAngle = 0;
var ring = new Vector3[segments];
for (var i = 0; i < segments; i++)
{
ring[i] = Geometry.PointOnCircle3XZ(radius, currentAngle);
currentAngle += segmentAngle;
}
var draft = new MeshDraft {name = "Bipyramid"}
.AddBaselessPyramid(Vector3.up*height/2, ring, generateUV)
.AddBaselessPyramid(Vector3.down*height/2, ring, generateUV, true);
return draft;
}
/// <summary>
/// Constructs a prism draft
/// </summary>
public static MeshDraft Prism(float radius, int segments, float height, bool generateUV = true)
{
float segmentAngle = 360f/segments;
float currentAngle = 0;
Vector3 halfHeightUp = Vector3.up*height/2;
var lowerRing = new List<Vector3>(segments);
var lowerDiskUV = new List<Vector2>();
var upperRing = new List<Vector3>(segments);
var upperDiskUV = new List<Vector2>();
for (var i = 0; i < segments; i++)
{
var point = Geometry.PointOnCircle3XZ(radius, currentAngle);
lowerRing.Add(point - halfHeightUp);
upperRing.Add(point + halfHeightUp);
if (generateUV)
{
Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f);
upperDiskUV.Add(uv);
uv.x = 1 - uv.x;
lowerDiskUV.Add(uv);
}
currentAngle += segmentAngle;
}
var draft = new MeshDraft {name = "Prism"}
.AddFlatQuadBand(lowerRing, upperRing, generateUV);
if (generateUV)
{
draft.AddTriangleFan(upperRing, Vector3.up, upperDiskUV)
.AddTriangleFan(lowerRing, Vector3.down, lowerDiskUV, true);
}
else
{
draft.AddTriangleFan(upperRing, Vector3.up)
.AddTriangleFan(lowerRing, Vector3.down, true);
}
return draft;
}
/// <summary>
/// Constructs a cylinder draft
/// </summary>
public static MeshDraft Cylinder(float radius, int segments, float height, bool generateUV = true)
{
float segmentAngle = 360f/segments;
float currentAngle = 0;
Vector3 halfHeightUp = Vector3.up*height/2;
var draft = new MeshDraft {name = "Cylinder"};
var lowerRing = new List<Vector3>(segments);
var lowerDiskUV = new List<Vector2>();
var upperRing = new List<Vector3>(segments);
var upperDiskUV = new List<Vector2>();
var strip = new List<Vector3>();
var stripNormals = new List<Vector3>();
var stripUV = new List<Vector2>();
for (var i = 0; i < segments; i++)
{
AddCylinderPoints(radius, currentAngle, halfHeightUp, generateUV,
ref strip, ref stripUV, ref stripNormals, out Vector3 lowerVertex, out Vector3 upperVertex);
lowerRing.Add(lowerVertex);
upperRing.Add(upperVertex);
if (generateUV)
{
Vector2 uv = Geometry.PointOnCircle2(0.5f, currentAngle) + new Vector2(0.5f, 0.5f);
upperDiskUV.Add(uv);
uv.x = 1 - uv.x;
lowerDiskUV.Add(uv);
}
currentAngle += segmentAngle;
}
AddCylinderPoints(radius, currentAngle, halfHeightUp, generateUV,
ref strip, ref stripUV, ref stripNormals, out _, out _);
if (generateUV)
{
draft.AddTriangleFan(lowerRing, Vector3.down, lowerDiskUV, true);
draft.AddTriangleFan(upperRing, Vector3.up, upperDiskUV);
draft.AddTriangleStrip(strip, stripNormals, stripUV);
}
else
{
draft.AddTriangleFan(lowerRing, Vector3.down, true);
draft.AddTriangleFan(upperRing, Vector3.up);
draft.AddTriangleStrip(strip, stripNormals);
}
return draft;
}
private static void AddCylinderPoints(float radius, float currentAngle, Vector3 halfHeightUp, bool generateUV,
ref List<Vector3> vertices, ref List<Vector2> uv, ref List<Vector3> normals,
out Vector3 lowerVertex, out Vector3 upperVertex)
{
Vector3 normal = Geometry.PointOnCircle3XZ(1, currentAngle);
Vector3 point = normal*radius;
lowerVertex = point - halfHeightUp;
upperVertex = point + halfHeightUp;
vertices.Add(upperVertex);
normals.Add(normal);
vertices.Add(lowerVertex);
normals.Add(normal);
if (generateUV)
{
float u = 1 - currentAngle/360;
uv.Add(new Vector2(u, 1));
uv.Add(new Vector2(u, 0));
}
}
/// <summary>
/// Constructs a flat sphere draft
/// </summary>
public static MeshDraft FlatSphere(float radius, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = FlatSpheroid(radius, radius, horizontalSegments, verticalSegments, generateUV);
draft.name = "Flat sphere";
return draft;
}
/// <summary>
/// Constructs a flat spheroid draft
/// </summary>
public static MeshDraft FlatSpheroid(float radius, float height, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = FlatRevolutionSurface(Geometry.PointOnSpheroid, radius, height, horizontalSegments, verticalSegments, generateUV);
draft.name = "Flat spheroid";
return draft;
}
/// <summary>
/// Constructs a flat teardrop draft
/// </summary>
public static MeshDraft FlatTeardrop(float radius, float height, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = FlatRevolutionSurface(Geometry.PointOnTeardrop, radius, height, horizontalSegments, verticalSegments, generateUV);
draft.name = "Flat teardrop";
return draft;
}
/// <summary>
/// Constructs a flat revolution surface draft
/// </summary>
public static MeshDraft FlatRevolutionSurface(
Func<float, float, float, float, Vector3> surfaceFunction,
float radius,
float height,
int horizontalSegments,
int verticalSegments,
bool generateUV = true)
{
float horizontalSegmentAngle = 360f/horizontalSegments;
float verticalSegmentAngle = 180f/verticalSegments;
float currentVerticalAngle = -90;
int horizontalCount = horizontalSegments + 1;
var ringsVertices = new List<List<Vector3>>(verticalSegments);
List<List<Vector2>> ringsUV = null;
if (generateUV)
{
ringsUV = new List<List<Vector2>>(verticalSegments);
}
for (int y = 0; y <= verticalSegments; y++)
{
float currentHorizontalAngle = 0f;
var ringVertices = new List<Vector3>(horizontalCount);
List<Vector2> ringUV = null;
if (generateUV)
{
ringUV = new List<Vector2>(horizontalCount);
}
for (int x = 0; x <= horizontalSegments; x++)
{
var point = surfaceFunction(radius, height, currentHorizontalAngle, currentVerticalAngle);
ringVertices.Add(point);
if (generateUV)
{
ringUV.Add(new Vector2(1 - (float) x/horizontalSegments, (float) y/verticalSegments));
}
currentHorizontalAngle += horizontalSegmentAngle;
}
ringsVertices.Add(ringVertices);
if (generateUV)
{
ringsUV.Add(ringUV);
}
currentVerticalAngle += verticalSegmentAngle;
}
var draft = new MeshDraft {name = "Flat revolution surface"};
for (int y = 0; y < ringsVertices.Count - 1; y++)
{
var lowerRingVertices = ringsVertices[y];
var upperRingVertices = ringsVertices[y + 1];
if (generateUV)
{
var lowerRingUV = ringsUV[y];
var upperRingUV = ringsUV[y + 1];
for (int x = 0; x < horizontalSegments; x++)
{
Vector3 v00 = lowerRingVertices[x + 1];
Vector3 v01 = upperRingVertices[x + 1];
Vector3 v11 = upperRingVertices[x];
Vector3 v10 = lowerRingVertices[x];
Vector2 uv00 = lowerRingUV[x + 1];
Vector2 uv01 = upperRingUV[x + 1];
Vector2 uv11 = upperRingUV[x];
Vector2 uv10 = lowerRingUV[x];
draft.AddQuad(v00, v01, v11, v10, true, uv00, uv01, uv11, uv10);
}
}
else
{
for (int x = 0; x < horizontalSegments; x++)
{
Vector3 v00 = lowerRingVertices[x + 1];
Vector3 v01 = upperRingVertices[x + 1];
Vector3 v11 = upperRingVertices[x];
Vector3 v10 = lowerRingVertices[x];
draft.AddQuad(v00, v01, v11, v10, true);
}
}
}
return draft;
}
/// <summary>
/// Constructs a sphere draft
/// </summary>
public static MeshDraft Sphere(float radius, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = Spheroid(radius, radius, horizontalSegments, verticalSegments, generateUV);
draft.name = "Sphere";
return draft;
}
/// <summary>
/// Constructs a spheroid draft
/// </summary>
public static MeshDraft Spheroid(float radius, float height, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = RevolutionSurface(Geometry.PointOnSpheroid, radius, height, horizontalSegments, verticalSegments, generateUV);
draft.name = "Spheroid";
return draft;
}
/// <summary>
/// Constructs a teardrop draft
/// </summary>
public static MeshDraft Teardrop(float radius, float height, int horizontalSegments, int verticalSegments, bool generateUV = true)
{
var draft = RevolutionSurface(Geometry.PointOnTeardrop, radius, height, horizontalSegments, verticalSegments, generateUV);
draft.name = "Teardrop";
return draft;
}
/// <summary>
/// Constructs a revolution surface draft
/// </summary>
public static MeshDraft RevolutionSurface(
Func<float, float, float, float, Vector3> surfaceFunction,
float radius,
float height,
int horizontalSegments,
int verticalSegments,
bool generateUV = true)
{
var draft = new MeshDraft {name = "Revolution surface"};
float horizontalSegmentAngle = 360f/horizontalSegments;
float verticalSegmentAngle = 180f/verticalSegments;
float currentVerticalAngle = -90;
for (int y = 0; y <= verticalSegments; y++)
{
float currentHorizontalAngle = 0f;
for (int x = 0; x <= horizontalSegments; x++)
{
Vector3 point = surfaceFunction(radius, height, currentHorizontalAngle, currentVerticalAngle);
draft.vertices.Add(point);
draft.normals.Add(point.normalized);
if (generateUV)
{
draft.uv.Add(new Vector2((float) x/horizontalSegments, (float) y/verticalSegments));
}
currentHorizontalAngle -= horizontalSegmentAngle;
}
currentVerticalAngle += verticalSegmentAngle;
}
// Extra vertices due to the uvmap seam
int horizontalCount = horizontalSegments + 1;
for (int ring = 0; ring < verticalSegments; ring++)
{
for (int i = 0; i < horizontalCount - 1; i++)
{
int i0 = ring*horizontalCount + i;
int i1 = (ring + 1)*horizontalCount + i;
int i2 = ring*horizontalCount + i + 1;
int i3 = (ring + 1)*horizontalCount + i + 1;
draft.triangles.Add(i0);
draft.triangles.Add(i1);
draft.triangles.Add(i2);
draft.triangles.Add(i2);
draft.triangles.Add(i1);
draft.triangles.Add(i3);
}
}
return draft;
}
/// <summary>
/// Constructs a partial box with specified faces
/// </summary>
public static MeshDraft PartialBox(Vector3 width, Vector3 depth, Vector3 height, Directions parts, bool generateUV = true)
{
Vector3 v000 = -width/2 - depth/2 - height/2;
Vector3 v001 = v000 + height;
Vector3 v010 = v000 + width;
Vector3 v011 = v000 + width + height;
Vector3 v100 = v000 + depth;
Vector3 v101 = v000 + depth + height;
Vector3 v110 = v000 + width + depth;
Vector3 v111 = v000 + width + depth + height;
var draft = new MeshDraft {name = "Partial box"};
if (generateUV)
{
Vector2 uv0 = new Vector2(0, 0);
Vector2 uv1 = new Vector2(0, 1);
Vector2 uv2 = new Vector2(1, 1);
Vector2 uv3 = new Vector2(1, 0);
if (parts.HasFlag(Directions.Left)) draft.AddQuad(v100, v101, v001, v000, true, uv0, uv1, uv2, uv3);
if (parts.HasFlag(Directions.Right)) draft.AddQuad(v010, v011, v111, v110, true, uv0, uv1, uv2, uv3);
if (parts.HasFlag(Directions.Down)) draft.AddQuad(v010, v110, v100, v000, true, uv0, uv1, uv2, uv3);
if (parts.HasFlag(Directions.Up)) draft.AddQuad(v111, v011, v001, v101, true, uv0, uv1, uv2, uv3);
if (parts.HasFlag(Directions.Back)) draft.AddQuad(v000, v001, v011, v010, true, uv0, uv1, uv2, uv3);
if (parts.HasFlag(Directions.Forward)) draft.AddQuad(v110, v111, v101, v100, true, uv0, uv1, uv2, uv3);
}
else
{
if (parts.HasFlag(Directions.Left)) draft.AddQuad(v100, v101, v001, v000, true);
if (parts.HasFlag(Directions.Right)) draft.AddQuad(v010, v011, v111, v110, true);
if (parts.HasFlag(Directions.Down)) draft.AddQuad(v010, v110, v100, v000, true);
if (parts.HasFlag(Directions.Up)) draft.AddQuad(v111, v011, v001, v101, true);
if (parts.HasFlag(Directions.Back)) draft.AddQuad(v000, v001, v011, v010, true);
if (parts.HasFlag(Directions.Forward)) draft.AddQuad(v110, v111, v101, v100, true);
}
return draft;
}
/// <summary>
/// Constructs a capsule draft
/// </summary>
/// <param name="height">The height of the capsule</param>
/// <param name="radius">The radius of the capsule</param>
/// <param name="segments">The number of radial segments. Defaults to 32</param>
/// <param name="rings">The number of end-cap rings. Defaults to 8</param>
public static MeshDraft Capsule(float height, float radius, int segments = 32, int rings = 8)
{
float cylinderHeight = height - radius*2;
int vertexCount = 2*rings*segments + 2;
int triangleCount = 4*rings*segments;
float horizontalAngle = 360f/segments;
float verticalAngle = 90f/rings;
var vertices = new Vector3[vertexCount];
var normals = new Vector3[vertexCount];
var triangles = new int[3*triangleCount];
int vi = 2;
int ti = 0;
int topCapIndex = 0;
int bottomCapIndex = 1;
vertices[topCapIndex].Set(0, cylinderHeight/2 + radius, 0);
normals[topCapIndex].Set(0, 1, 0);
vertices[bottomCapIndex].Set(0, -cylinderHeight/2 - radius, 0);
normals[bottomCapIndex].Set(0, -1, 0);
for (int s = 0; s < segments; s++)
{
for (int r = 1; r <= rings; r++)
{
// Top cap vertex
Vector3 normal = Geometry.PointOnSphere(1, s*horizontalAngle, 90 - r*verticalAngle);
Vector3 vertex = new Vector3(
x: radius*normal.x,
y: radius*normal.y + cylinderHeight/2,
z: radius*normal.z);
vertices[vi] = vertex;
normals[vi] = normal;
vi++;
// Bottom cap vertex
vertices[vi].Set(vertex.x, -vertex.y, vertex.z);
normals[vi].Set(normal.x, -normal.y, normal.z);
vi++;
int top_s1r1 = vi - 2;
int top_s1r0 = vi - 4;
int bot_s1r1 = vi - 1;
int bot_s1r0 = vi - 3;
int top_s0r1 = top_s1r1 - 2*rings;
int top_s0r0 = top_s1r0 - 2*rings;
int bot_s0r1 = bot_s1r1 - 2*rings;
int bot_s0r0 = bot_s1r0 - 2*rings;
if (s == 0)
{
top_s0r1 += vertexCount - 2;
top_s0r0 += vertexCount - 2;
bot_s0r1 += vertexCount - 2;
bot_s0r0 += vertexCount - 2;
}
// Create cap triangles
if (r == 1)
{
triangles[3*ti + 0] = topCapIndex;
triangles[3*ti + 1] = top_s0r1;
triangles[3*ti + 2] = top_s1r1;
ti++;
triangles[3*ti + 0] = bottomCapIndex;
triangles[3*ti + 1] = bot_s1r1;
triangles[3*ti + 2] = bot_s0r1;
ti++;
}
else
{
triangles[3*ti + 0] = top_s1r0;
triangles[3*ti + 1] = top_s0r0;
triangles[3*ti + 2] = top_s1r1;
ti++;
triangles[3*ti + 0] = top_s0r0;
triangles[3*ti + 1] = top_s0r1;
triangles[3*ti + 2] = top_s1r1;
ti++;
triangles[3*ti + 0] = bot_s0r1;
triangles[3*ti + 1] = bot_s0r0;
triangles[3*ti + 2] = bot_s1r1;
ti++;
triangles[3*ti + 0] = bot_s0r0;
triangles[3*ti + 1] = bot_s1r0;
triangles[3*ti + 2] = bot_s1r1;
ti++;
}
}
// Create side triangles
int top_s1 = vi - 2;
int top_s0 = top_s1 - 2*rings;
int bot_s1 = vi - 1;
int bot_s0 = bot_s1 - 2*rings;
if (s == 0)
{
top_s0 += vertexCount - 2;
bot_s0 += vertexCount - 2;
}
triangles[3*ti + 0] = top_s0;
triangles[3*ti + 1] = bot_s1;
triangles[3*ti + 2] = top_s1;
ti++;
triangles[3*ti + 0] = bot_s0;
triangles[3*ti + 1] = bot_s1;
triangles[3*ti + 2] = top_s0;
ti++;
}
return new MeshDraft
{
name = "Capsule",
vertices = new List<Vector3>(vertices),
triangles = new List<int>(triangles),
normals = new List<Vector3>(normals)
};
}
}
}