forked from Syomus/ProceduralToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshDraft.cs
915 lines (839 loc) · 30.3 KB
/
MeshDraft.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
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace ProceduralToolkit
{
/// <summary>
/// Helper class for procedural mesh generation
/// </summary>
[Serializable]
public partial class MeshDraft
{
public string name = "";
public List<Vector3> vertices = new List<Vector3>();
public List<int> triangles = new List<int>();
public List<Vector3> normals = new List<Vector3>();
public List<Vector4> tangents = new List<Vector4>();
public List<Vector2> uv = new List<Vector2>();
public List<Vector2> uv2 = new List<Vector2>();
public List<Vector2> uv3 = new List<Vector2>();
public List<Vector2> uv4 = new List<Vector2>();
public List<Color> colors = new List<Color>();
/// <summary>
/// Shortcut for vertices.Count
/// </summary>
public int vertexCount => vertices.Count;
/// <summary>
/// Creates an empty MeshDraft
/// </summary>
public MeshDraft()
{
}
/// <summary>
/// Creates a new MeshDraft with vertex data from the <paramref name="mesh"/>>
/// </summary>
public MeshDraft(Mesh mesh)
{
if (mesh == null) throw new ArgumentNullException(nameof(mesh));
name = mesh.name;
mesh.GetVertices(vertices);
mesh.GetTriangles(triangles, 0);
mesh.GetNormals(normals);
mesh.GetTangents(tangents);
mesh.GetUVs(0, uv);
mesh.GetUVs(1, uv2);
mesh.GetUVs(2, uv3);
mesh.GetUVs(3, uv4);
mesh.GetColors(colors);
}
/// <summary>
/// Adds vertex data from the <paramref name="draft"/>
/// </summary>
public MeshDraft Add(MeshDraft draft)
{
if (draft == null) throw new ArgumentNullException(nameof(draft));
for (var i = 0; i < draft.triangles.Count; i++)
{
triangles.Add(draft.triangles[i] + vertices.Count);
}
vertices.AddRange(draft.vertices);
normals.AddRange(draft.normals);
tangents.AddRange(draft.tangents);
uv.AddRange(draft.uv);
uv2.AddRange(draft.uv2);
uv3.AddRange(draft.uv3);
uv4.AddRange(draft.uv4);
colors.AddRange(draft.colors);
return this;
}
#region AddTriangle
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, bool calculateNormal)
{
if (calculateNormal)
{
Vector3 normal = Vector3.Cross(vertex1 - vertex0, vertex2 - vertex0).normalized;
return AddTriangle(vertex0, vertex1, vertex2, normal, normal, normal);
}
return _AddTriangle(vertex0, vertex1, vertex2);
}
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 normal)
{
return AddTriangle(vertex0, vertex1, vertex2, normal, normal, normal);
}
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 normal0, Vector3 normal1, Vector3 normal2)
{
normals.Add(normal0);
normals.Add(normal1);
normals.Add(normal2);
return _AddTriangle(vertex0, vertex1, vertex2);
}
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, bool calculateNormal,
Vector2 uv0, Vector2 uv1, Vector2 uv2)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
return AddTriangle(vertex0, vertex1, vertex2, calculateNormal);
}
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 normal,
Vector2 uv0, Vector2 uv1, Vector2 uv2)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
return AddTriangle(vertex0, vertex1, vertex2, normal, normal, normal);
}
/// <summary>
/// Adds a triangle to the draft
/// </summary>
public MeshDraft AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 normal0, Vector3 normal1, Vector3 normal2,
Vector2 uv0, Vector2 uv1, Vector2 uv2)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
return AddTriangle(vertex0, vertex1, vertex2, normal0, normal1, normal2);
}
private MeshDraft _AddTriangle(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2)
{
triangles.Add(0 + vertices.Count);
triangles.Add(1 + vertices.Count);
triangles.Add(2 + vertices.Count);
vertices.Add(vertex0);
vertices.Add(vertex1);
vertices.Add(vertex2);
return this;
}
#endregion AddTriangle
#region AddQuad
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 origin, Vector3 width, Vector3 height, bool calculateNormal)
{
Vector3 vertex0 = origin;
Vector3 vertex1 = origin + height;
Vector3 vertex2 = origin + height + width;
Vector3 vertex3 = origin + width;
if (calculateNormal)
{
Vector3 normal = Vector3.Cross(height, width).normalized;
return AddQuad(vertex0, vertex1, vertex2, vertex3, normal, normal, normal, normal);
}
return _AddQuad(vertex0, vertex1, vertex2, vertex3);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3, bool calculateNormal)
{
if (calculateNormal)
{
Vector3 normal = Vector3.Cross(vertex1 - vertex0, vertex3 - vertex0).normalized;
// Fix for degenerate triangle-like quads
if (normal.sqrMagnitude < Geometry.Epsilon)
{
normal = Vector3.Cross(vertex3 - vertex2, vertex1 - vertex2).normalized;
}
return AddQuad(vertex0, vertex1, vertex2, vertex3, normal, normal, normal, normal);
}
return _AddQuad(vertex0, vertex1, vertex2, vertex3);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3, Vector3 normal)
{
return AddQuad(vertex0, vertex1, vertex2, vertex3, normal, normal, normal, normal);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3,
Vector3 normal0, Vector3 normal1, Vector3 normal2, Vector3 normal3)
{
normals.Add(normal0);
normals.Add(normal1);
normals.Add(normal2);
normals.Add(normal3);
return _AddQuad(vertex0, vertex1, vertex2, vertex3);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 origin, Vector3 width, Vector3 height, bool calculateNormal,
Vector2 uv0, Vector2 uv1, Vector2 uv2, Vector2 uv3)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
uv.Add(uv3);
return AddQuad(origin, width, height, calculateNormal);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3, bool calculateNormal,
Vector2 uv0, Vector2 uv1, Vector2 uv2, Vector2 uv3)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
uv.Add(uv3);
return AddQuad(vertex0, vertex1, vertex2, vertex3, calculateNormal);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3, Vector3 normal,
Vector2 uv0, Vector2 uv1, Vector2 uv2, Vector2 uv3)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
uv.Add(uv3);
return AddQuad(vertex0, vertex1, vertex2, vertex3, normal, normal, normal, normal);
}
/// <summary>
/// Adds a quad to the draft
/// </summary>
public MeshDraft AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3,
Vector3 normal0, Vector3 normal1, Vector3 normal2, Vector3 normal3,
Vector2 uv0, Vector2 uv1, Vector2 uv2, Vector2 uv3)
{
uv.Add(uv0);
uv.Add(uv1);
uv.Add(uv2);
uv.Add(uv3);
return AddQuad(vertex0, vertex1, vertex2, vertex3, normal0, normal1, normal2, normal3);
}
private MeshDraft _AddQuad(Vector3 vertex0, Vector3 vertex1, Vector3 vertex2, Vector3 vertex3)
{
triangles.Add(0 + vertices.Count);
triangles.Add(1 + vertices.Count);
triangles.Add(2 + vertices.Count);
triangles.Add(0 + vertices.Count);
triangles.Add(2 + vertices.Count);
triangles.Add(3 + vertices.Count);
vertices.Add(vertex0);
vertices.Add(vertex1);
vertices.Add(vertex2);
vertices.Add(vertex3);
return this;
}
#endregion AddQuad
#region AddTriangleFan
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, bool reverseTriangles = false)
{
Vector3 normal = Vector3.Cross(fan[1] - fan[0], fan[fan.Count - 1] - fan[0]).normalized;
return AddTriangleFan(fan, normal, reverseTriangles);
}
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, Vector3 normal, bool reverseTriangles = false)
{
AddTriangleFanVertices(fan, reverseTriangles);
for (int i = 0; i < fan.Count; i++)
{
normals.Add(normal);
}
return this;
}
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, IList<Vector3> normals, bool reverseTriangles = false)
{
AddTriangleFanVertices(fan, reverseTriangles);
this.normals.AddRange(normals);
return this;
}
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, IList<Vector2> uv, bool reverseTriangles = false)
{
this.uv.AddRange(uv);
return AddTriangleFan(fan, reverseTriangles);
}
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, Vector3 normal, IList<Vector2> uv, bool reverseTriangles = false)
{
this.uv.AddRange(uv);
return AddTriangleFan(fan, normal, reverseTriangles);
}
/// <summary>
/// Adds a triangle fan to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_fan
/// </remarks>
public MeshDraft AddTriangleFan(IList<Vector3> fan, IList<Vector3> normals, IList<Vector2> uv, bool reverseTriangles = false)
{
this.uv.AddRange(uv);
return AddTriangleFan(fan, normals, reverseTriangles);
}
private void AddTriangleFanVertices(IList<Vector3> fan, bool reverseTriangles)
{
int count = vertices.Count;
if (reverseTriangles)
{
for (int i = fan.Count - 1; i > 1; i--)
{
triangles.Add(0 + count);
triangles.Add(i + count);
triangles.Add(i - 1 + count);
}
}
else
{
for (int i = 1; i < fan.Count - 1; i++)
{
triangles.Add(0 + count);
triangles.Add(i + count);
triangles.Add(i + 1 + count);
}
}
vertices.AddRange(fan);
}
#endregion AddTriangleFan
#region AddTriangleStrip
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip)
{
Vector3 normal = Vector3.Cross(strip[1] - strip[0], strip[2] - strip[0]).normalized;
return AddTriangleStrip(strip, normal);
}
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip, Vector3 normal)
{
for (int i = 0, j = 1, k = 2;
i < strip.Count - 2;
i++, j += i%2*2, k += (i + 1)%2*2)
{
triangles.Add(i + vertices.Count);
triangles.Add(j + vertices.Count);
triangles.Add(k + vertices.Count);
}
vertices.AddRange(strip);
for (int i = 0; i < strip.Count; i++)
{
normals.Add(normal);
}
return this;
}
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip, IList<Vector3> normals)
{
for (int i = 0, j = 1, k = 2;
i < strip.Count - 2;
i++, j += i%2*2, k += (i + 1)%2*2)
{
triangles.Add(i + vertices.Count);
triangles.Add(j + vertices.Count);
triangles.Add(k + vertices.Count);
}
vertices.AddRange(strip);
this.normals.AddRange(normals);
return this;
}
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip, IList<Vector2> uv)
{
this.uv.AddRange(uv);
return AddTriangleStrip(strip);
}
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip, Vector3 normal, IList<Vector2> uv)
{
this.uv.AddRange(uv);
return AddTriangleStrip(strip, normal);
}
/// <summary>
/// Adds a triangle strip to the draft
/// </summary>
/// <remarks>
/// https://en.wikipedia.org/wiki/Triangle_strip
/// </remarks>
public MeshDraft AddTriangleStrip(IList<Vector3> strip, IList<Vector3> normals, IList<Vector2> uv)
{
this.uv.AddRange(uv);
return AddTriangleStrip(strip, normals);
}
#endregion AddTriangleStrip
#region AddBaselessPyramid
/// <summary>
/// Adds a baseless pyramid to the draft
/// </summary>
public MeshDraft AddBaselessPyramid(Vector3 apex, IList<Vector3> ring, bool generateUV, bool reverseTriangles = false)
{
if (generateUV)
{
var uv00 = new Vector2(0, 0);
var uvApex = new Vector2(0.5f, 1);
var uv10 = new Vector2(1, 0);
if (reverseTriangles)
{
for (int i = ring.Count - 1; i > 0; i--)
{
AddTriangle(ring[i - 1], apex, ring[i], true, uv00, uvApex, uv10);
}
AddTriangle(ring[ring.Count - 1], apex, ring[0], true, uv00, uvApex, uv10);
}
else
{
for (var i = 0; i < ring.Count - 1; i++)
{
AddTriangle(ring[i + 1], apex, ring[i], true, uv00, uvApex, uv10);
}
AddTriangle(ring[0], apex, ring[ring.Count - 1], true, uv00, uvApex, uv10);
}
}
else
{
if (reverseTriangles)
{
for (int i = ring.Count - 1; i > 0; i--)
{
AddTriangle(ring[i - 1], apex, ring[i], true);
}
AddTriangle(ring[ring.Count - 1], apex, ring[0], true);
}
else
{
for (var i = 0; i < ring.Count - 1; i++)
{
AddTriangle(ring[i + 1], apex, ring[i], true);
}
AddTriangle(ring[0], apex, ring[ring.Count - 1], true);
}
}
return this;
}
#endregion AddBaselessPyramid
#region AddFlatTriangleBand
/// <summary>
/// Adds a band made from triangles to the draft
/// </summary>
public MeshDraft AddFlatTriangleBand(IList<Vector3> lowerRing, IList<Vector3> upperRing, bool generateUV)
{
if (lowerRing.Count != upperRing.Count)
{
throw new ArgumentException("Array sizes must be equal");
}
if (lowerRing.Count < 3)
{
throw new ArgumentException("Array sizes must be greater than 2");
}
Vector2 uv00 = new Vector2(0, 0);
Vector2 uvBottomCenter = new Vector2(0.5f, 0);
Vector2 uv10 = new Vector2(1, 0);
Vector2 uv01 = new Vector2(0, 1);
Vector2 uvTopCenter = new Vector2(0.5f, 1);
Vector2 uv11 = new Vector2(1, 1);
Vector3 lower0, upper0, lower1, upper1;
for (int i = 0; i < lowerRing.Count - 1; i++)
{
lower0 = lowerRing[i];
lower1 = lowerRing[i + 1];
upper0 = upperRing[i];
upper1 = upperRing[i + 1];
if (generateUV)
{
AddTriangle(lower1, upper0, lower0, true, uv00, uvTopCenter, uv10);
AddTriangle(lower1, upper1, upper0, true, uvBottomCenter, uv01, uv11);
}
else
{
AddTriangle(lower1, upper0, lower0, true);
AddTriangle(lower1, upper1, upper0, true);
}
}
lower0 = lowerRing[lowerRing.Count - 1];
lower1 = lowerRing[0];
upper0 = upperRing[upperRing.Count - 1];
upper1 = upperRing[0];
if (generateUV)
{
AddTriangle(lower1, upper0, lower0, true, uv00, uvTopCenter, uv10);
AddTriangle(lower1, upper1, upper0, true, uvBottomCenter, uv01, uv11);
}
else
{
AddTriangle(lower1, upper0, lower0, true);
AddTriangle(lower1, upper1, upper0, true);
}
return this;
}
#endregion AddFlatTriangleBand
#region AddFlatQuadBand
/// <summary>
/// Adds a band made from quads to the draft
/// </summary>
public MeshDraft AddFlatQuadBand(IList<Vector3> lowerRing, IList<Vector3> upperRing, bool generateUV)
{
if (lowerRing.Count != upperRing.Count)
{
throw new ArgumentException("Array sizes must be equal");
}
if (lowerRing.Count < 3)
{
throw new ArgumentException("Array sizes must be greater than 2");
}
Vector2 uv00 = new Vector2(0, 0);
Vector2 uv10 = new Vector2(1, 0);
Vector2 uv01 = new Vector2(0, 1);
Vector2 uv11 = new Vector2(1, 1);
Vector3 lower0, upper0, lower1, upper1;
for (int i = 0; i < lowerRing.Count - 1; i++)
{
lower0 = lowerRing[i];
lower1 = lowerRing[i + 1];
upper0 = upperRing[i];
upper1 = upperRing[i + 1];
if (generateUV)
{
AddQuad(lower1, upper1, upper0, lower0, true, uv00, uv01, uv11, uv10);
}
else
{
AddQuad(lower1, upper1, upper0, lower0, true);
}
}
lower0 = lowerRing[lowerRing.Count - 1];
lower1 = lowerRing[0];
upper0 = upperRing[upperRing.Count - 1];
upper1 = upperRing[0];
if (generateUV)
{
AddQuad(lower1, upper1, upper0, lower0, true, uv00, uv01, uv11, uv10);
}
else
{
AddQuad(lower1, upper1, upper0, lower0, true);
}
return this;
}
#endregion AddFlatQuadBand
/// <summary>
/// Clears all vertex data and all triangle indices
/// </summary>
public void Clear()
{
vertices.Clear();
triangles.Clear();
normals.Clear();
tangents.Clear();
uv.Clear();
uv2.Clear();
uv3.Clear();
uv4.Clear();
colors.Clear();
}
/// <summary>
/// Moves draft vertices by <paramref name="vector"/>
/// </summary>
public MeshDraft Move(Vector3 vector)
{
for (int i = 0; i < vertices.Count; i++)
{
vertices[i] += vector;
}
return this;
}
/// <summary>
/// Rotates draft vertices by <paramref name="rotation"/>
/// </summary>
public MeshDraft Rotate(Quaternion rotation)
{
for (int i = 0; i < vertices.Count; i++)
{
vertices[i] = rotation*vertices[i];
normals[i] = rotation*normals[i];
}
return this;
}
/// <summary>
/// Scales draft vertices uniformly by <paramref name="scale"/>
/// </summary>
public MeshDraft Scale(float scale)
{
for (int i = 0; i < vertices.Count; i++)
{
vertices[i] *= scale;
}
return this;
}
/// <summary>
/// Scales draft vertices non-uniformly by <paramref name="scale"/>
/// </summary>
public MeshDraft Scale(Vector3 scale)
{
for (int i = 0; i < vertices.Count; i++)
{
vertices[i] = Vector3.Scale(vertices[i], scale);
normals[i] = Vector3.Scale(normals[i], scale).normalized;
}
return this;
}
/// <summary>
/// Paints draft vertices with <paramref name="color"/>
/// </summary>
public MeshDraft Paint(Color color)
{
colors.Clear();
for (int i = 0; i < vertices.Count; i++)
{
colors.Add(color);
}
return this;
}
/// <summary>
/// Flips draft faces
/// </summary>
public MeshDraft FlipFaces()
{
FlipTriangles();
FlipNormals();
return this;
}
/// <summary>
/// Reverses the winding order of draft triangles
/// </summary>
public MeshDraft FlipTriangles()
{
for (int i = 0; i < triangles.Count; i += 3)
{
var temp = triangles[i];
triangles[i] = triangles[i + 1];
triangles[i + 1] = temp;
}
return this;
}
/// <summary>
/// Reverses the direction of draft normals
/// </summary>
public MeshDraft FlipNormals()
{
for (int i = 0; i < normals.Count; i++)
{
normals[i] = -normals[i];
}
return this;
}
/// <summary>
/// Flips the UV map horizontally in the selected <paramref name="channel"/>
/// </summary>
public MeshDraft FlipUVHorizontally(int channel = 0)
{
List<Vector2> list;
switch (channel)
{
case 0:
list = uv;
break;
case 1:
list = uv2;
break;
case 2:
list = uv3;
break;
case 3:
list = uv4;
break;
default:
throw new ArgumentOutOfRangeException(nameof(channel));
}
for (var i = 0; i < list.Count; i++)
{
list[i] = new Vector2(1 - list[i].x, list[i].y);
}
return this;
}
/// <summary>
/// Flips the UV map vertically in the selected <paramref name="channel"/>
/// </summary>
public MeshDraft FlipUVVertically(int channel = 0)
{
List<Vector2> list;
switch (channel)
{
case 0:
list = uv;
break;
case 1:
list = uv2;
break;
case 2:
list = uv3;
break;
case 3:
list = uv4;
break;
default:
throw new ArgumentOutOfRangeException(nameof(channel));
}
for (var i = 0; i < list.Count; i++)
{
list[i] = new Vector2(list[i].x, 1 - list[i].y);
}
return this;
}
/// <summary>
/// Projects vertices on a sphere with the given <paramref name="radius"/> and <paramref name="center"/>, recalculates normals
/// </summary>
public MeshDraft Spherify(float radius, Vector3 center = default)
{
for (var i = 0; i < vertices.Count; i++)
{
normals[i] = (vertices[i] - center).normalized;
vertices[i] = normals[i]*radius;
}
return this;
}
/// <summary>
/// Creates a new mesh from the data in the draft
/// </summary>
/// <param name="calculateBounds"> Calculate the bounding box of the Mesh after setting the triangles. </param>
/// <param name="autoIndexFormat"> Use 16 bit or 32 bit index buffers based on vertex count. </param>
public Mesh ToMesh(bool calculateBounds = true, bool autoIndexFormat = true)
{
var mesh = new Mesh();
FillMesh(ref mesh, calculateBounds, autoIndexFormat);
return mesh;
}
/// <summary>
/// Fills the <paramref name="mesh"/> with the data in the draft
/// </summary>
/// <param name="mesh"> Resulting mesh. Cleared before use. </param>
/// <param name="calculateBounds"> Calculate the bounding box of the Mesh after setting the triangles. </param>
/// <param name="autoIndexFormat"> Use 16 bit or 32 bit index buffers based on vertex count. </param>
public void ToMesh(ref Mesh mesh, bool calculateBounds = true, bool autoIndexFormat = true)
{
if (mesh == null)
{
throw new ArgumentNullException(nameof(mesh));
}
mesh.Clear(false);
FillMesh(ref mesh, calculateBounds, autoIndexFormat);
}
private void FillMesh(ref Mesh mesh, bool calculateBounds, bool autoIndexFormat)
{
if (vertexCount > 65535)
{
if (autoIndexFormat)
{
mesh.indexFormat = IndexFormat.UInt32;
}
else
{
Debug.LogError("A mesh can't have more than 65535 vertices with 16 bit index buffer. Vertex count: " + vertexCount);
mesh.indexFormat = IndexFormat.UInt16;
}
}
else
{
if (autoIndexFormat)
{
mesh.indexFormat = IndexFormat.UInt16;
}
}
mesh.name = name;
mesh.SetVertices(vertices);
mesh.SetTriangles(triangles, 0, calculateBounds);
mesh.SetNormals(normals);
mesh.SetTangents(tangents);
mesh.SetUVs(0, uv);
mesh.SetUVs(1, uv2);
mesh.SetUVs(2, uv3);
mesh.SetUVs(3, uv4);
mesh.SetColors(colors);
}
public override string ToString()
{
return name + " (ProceduralToolkit.MeshDraft)";
}
}
}