/
MeshGenerator.cs
1387 lines (1212 loc) · 51.4 KB
/
MeshGenerator.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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Spine Runtimes License Agreement
* Last updated January 1, 2020. Replaces all prior versions.
*
* Copyright (c) 2013-2020, Esoteric Software LLC
*
* Integration of the Spine Runtimes into software or otherwise creating
* derivative works of the Spine Runtimes is permitted under the terms and
* conditions of Section 2 of the Spine Editor License Agreement:
* http://esotericsoftware.com/spine-editor-license
*
* Otherwise, it is permitted to integrate the Spine Runtimes into software
* or otherwise create derivative works of the Spine Runtimes (collectively,
* "Products"), provided that each user of the Products must obtain their own
* Spine Editor license and redistribution of the Products in any form must
* include this license and copyright notice.
*
* THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
* BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#if UNITY_2019_3_OR_NEWER
#define MESH_SET_TRIANGLES_PROVIDES_LENGTH_PARAM
#endif
// Not for optimization. Do not disable.
#define SPINE_TRIANGLECHECK // Avoid calling SetTriangles at the cost of checking for mesh differences (vertex counts, memberwise attachment list compare) every frame.
//#define SPINE_DEBUG
using UnityEngine;
using System;
using System.Collections.Generic;
namespace Spine.Unity {
public delegate void MeshGeneratorDelegate (MeshGeneratorBuffers buffers);
public struct MeshGeneratorBuffers {
/// <summary>The vertex count that will actually be used for the mesh. The Lengths of the buffer arrays may be larger than this number.</summary>
public int vertexCount;
/// <summary> Vertex positions. To be used for UnityEngine.Mesh.vertices.</summary>
public Vector3[] vertexBuffer;
/// <summary> Vertex UVs. To be used for UnityEngine.Mesh.uvs.</summary>
public Vector2[] uvBuffer;
/// <summary> Vertex colors. To be used for UnityEngine.Mesh.colors32.</summary>
public Color32[] colorBuffer;
/// <summary> The Spine rendering component's MeshGenerator. </summary>
public MeshGenerator meshGenerator;
}
/// <summary>Holds several methods to prepare and generate a UnityEngine mesh based on a skeleton. Contains buffers needed to perform the operation, and serializes settings for mesh generation.</summary>
[System.Serializable]
public class MeshGenerator {
public Settings settings = Settings.Default;
[System.Serializable]
public struct Settings {
public bool useClipping;
[Space]
[Range(-0.1f, 0f)] public float zSpacing;
[Space]
[Header("Vertex Data")]
public bool pmaVertexColors;
public bool tintBlack;
[Tooltip("Enable when using Additive blend mode at SkeletonGraphic under a CanvasGroup. " +
"When enabled, Additive alpha value is stored at uv2.g instead of color.a to capture CanvasGroup modifying color.a.")]
public bool canvasGroupTintBlack;
public bool calculateTangents;
public bool addNormals;
public bool immutableTriangles;
static public Settings Default {
get {
return new Settings {
pmaVertexColors = true,
zSpacing = 0f,
useClipping = true,
tintBlack = false,
calculateTangents = false,
//renderMeshes = true,
addNormals = false,
immutableTriangles = false
};
}
}
}
const float BoundsMinDefault = float.PositiveInfinity;
const float BoundsMaxDefault = float.NegativeInfinity;
[NonSerialized] readonly ExposedList<Vector3> vertexBuffer = new ExposedList<Vector3>(4);
[NonSerialized] readonly ExposedList<Vector2> uvBuffer = new ExposedList<Vector2>(4);
[NonSerialized] readonly ExposedList<Color32> colorBuffer = new ExposedList<Color32>(4);
[NonSerialized] readonly ExposedList<ExposedList<int>> submeshes = new ExposedList<ExposedList<int>> { new ExposedList<int>(6) }; // start with 1 submesh.
[NonSerialized] Vector2 meshBoundsMin, meshBoundsMax;
[NonSerialized] float meshBoundsThickness;
[NonSerialized] int submeshIndex = 0;
[NonSerialized] SkeletonClipping clipper = new SkeletonClipping();
[NonSerialized] float[] tempVerts = new float[8];
[NonSerialized] int[] regionTriangles = { 0, 1, 2, 2, 3, 0 };
#region Optional Buffers
// These optional buffers are lazy-instantiated when the feature is used.
[NonSerialized] Vector3[] normals;
[NonSerialized] Vector4[] tangents;
[NonSerialized] Vector2[] tempTanBuffer;
[NonSerialized] ExposedList<Vector2> uv2;
[NonSerialized] ExposedList<Vector2> uv3;
#endregion
public int VertexCount { get { return vertexBuffer.Count; } }
/// <summary>A set of mesh arrays whose values are modifiable by the user. Modify these values before they are passed to the UnityEngine mesh object in order to see the effect.</summary>
public MeshGeneratorBuffers Buffers {
get {
return new MeshGeneratorBuffers {
vertexCount = this.VertexCount,
vertexBuffer = this.vertexBuffer.Items,
uvBuffer = this.uvBuffer.Items,
colorBuffer = this.colorBuffer.Items,
meshGenerator = this
};
}
}
public MeshGenerator () {
submeshes.TrimExcess();
}
#region Step 1 : Generate Instructions
/// <summary>
/// A specialized variant of <see cref="GenerateSkeletonRendererInstruction"/>.
/// Generates renderer instructions using a single submesh, using only a single material and texture.
/// </summary>
/// <param name="instructionOutput">The resulting instructions.</param>
/// <param name="skeleton">The skeleton to generate renderer instructions for.</param>
/// <param name="material">Material to be set at the renderer instruction. When null, the last attachment
/// in the draw order list is assigned as the instruction's material.</param>
public static void GenerateSingleSubmeshInstruction (SkeletonRendererInstruction instructionOutput, Skeleton skeleton, Material material) {
ExposedList<Slot> drawOrder = skeleton.drawOrder;
int drawOrderCount = drawOrder.Count;
// Clear last state of attachments and submeshes
instructionOutput.Clear(); // submeshInstructions.Clear(); attachments.Clear();
var workingSubmeshInstructions = instructionOutput.submeshInstructions;
#if SPINE_TRIANGLECHECK
instructionOutput.attachments.Resize(drawOrderCount);
var workingAttachmentsItems = instructionOutput.attachments.Items;
int totalRawVertexCount = 0;
#endif
var current = new SubmeshInstruction {
skeleton = skeleton,
preActiveClippingSlotSource = -1,
startSlot = 0,
#if SPINE_TRIANGLECHECK
rawFirstVertexIndex = 0,
#endif
material = material,
forceSeparate = false,
endSlot = drawOrderCount
};
#if SPINE_TRIANGLECHECK
object rendererObject = null;
bool skeletonHasClipping = false;
var drawOrderItems = drawOrder.Items;
for (int i = 0; i < drawOrderCount; i++) {
Slot slot = drawOrderItems[i];
if (!slot.bone.active) {
workingAttachmentsItems[i] = null;
continue;
}
Attachment attachment = slot.attachment;
workingAttachmentsItems[i] = attachment;
int attachmentTriangleCount;
int attachmentVertexCount;
var regionAttachment = attachment as RegionAttachment;
if (regionAttachment != null) {
rendererObject = regionAttachment.RendererObject;
attachmentVertexCount = 4;
attachmentTriangleCount = 6;
} else {
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
rendererObject = meshAttachment.RendererObject;
attachmentVertexCount = meshAttachment.worldVerticesLength >> 1;
attachmentTriangleCount = meshAttachment.triangles.Length;
} else {
var clippingAttachment = attachment as ClippingAttachment;
if (clippingAttachment != null) {
current.hasClipping = true;
skeletonHasClipping = true;
}
attachmentVertexCount = 0;
attachmentTriangleCount = 0;
}
}
current.rawTriangleCount += attachmentTriangleCount;
current.rawVertexCount += attachmentVertexCount;
totalRawVertexCount += attachmentVertexCount;
}
#if !SPINE_TK2D
if (material == null && rendererObject != null)
current.material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
#else
if (material == null && rendererObject != null)
current.material = (rendererObject is Material) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
instructionOutput.hasActiveClipping = skeletonHasClipping;
instructionOutput.rawVertexCount = totalRawVertexCount;
#endif
if (totalRawVertexCount > 0) {
workingSubmeshInstructions.Resize(1);
workingSubmeshInstructions.Items[0] = current;
}
else {
workingSubmeshInstructions.Resize(0);
}
}
public static bool RequiresMultipleSubmeshesByDrawOrder (Skeleton skeleton) {
#if SPINE_TK2D
return false;
#endif
ExposedList<Slot> drawOrder = skeleton.drawOrder;
int drawOrderCount = drawOrder.Count;
var drawOrderItems = drawOrder.Items;
Material lastRendererMaterial = null;
for (int i = 0; i < drawOrderCount; i++) {
Slot slot = drawOrderItems[i];
if (!slot.bone.active) continue;
Attachment attachment = slot.attachment;
var rendererAttachment = attachment as IHasRendererObject;
if (rendererAttachment != null) {
AtlasRegion atlasRegion = (AtlasRegion)rendererAttachment.RendererObject;
Material material = (Material)atlasRegion.page.rendererObject;
if (lastRendererMaterial != material) {
if (lastRendererMaterial != null)
return true;
else
lastRendererMaterial = material;
}
}
}
return false;
}
public static void GenerateSkeletonRendererInstruction (SkeletonRendererInstruction instructionOutput, Skeleton skeleton, Dictionary<Slot, Material> customSlotMaterials, List<Slot> separatorSlots, bool generateMeshOverride, bool immutableTriangles = false) {
// if (skeleton == null) throw new ArgumentNullException("skeleton");
// if (instructionOutput == null) throw new ArgumentNullException("instructionOutput");
ExposedList<Slot> drawOrder = skeleton.drawOrder;
int drawOrderCount = drawOrder.Count;
// Clear last state of attachments and submeshes
instructionOutput.Clear(); // submeshInstructions.Clear(); attachments.Clear();
var workingSubmeshInstructions = instructionOutput.submeshInstructions;
#if SPINE_TRIANGLECHECK
instructionOutput.attachments.Resize(drawOrderCount);
var workingAttachmentsItems = instructionOutput.attachments.Items;
int totalRawVertexCount = 0;
bool skeletonHasClipping = false;
#endif
var current = new SubmeshInstruction {
skeleton = skeleton,
preActiveClippingSlotSource = -1
};
#if !SPINE_TK2D
bool isCustomSlotMaterialsPopulated = customSlotMaterials != null && customSlotMaterials.Count > 0;
#endif
int separatorCount = separatorSlots == null ? 0 : separatorSlots.Count;
bool hasSeparators = separatorCount > 0;
int clippingAttachmentSource = -1;
int lastPreActiveClipping = -1; // The index of the last slot that had an active ClippingAttachment.
SlotData clippingEndSlot = null;
int submeshIndex = 0;
var drawOrderItems = drawOrder.Items;
for (int i = 0; i < drawOrderCount; i++) {
Slot slot = drawOrderItems[i];
if (!slot.bone.active) {
workingAttachmentsItems[i] = null;
continue;
}
Attachment attachment = slot.attachment;
#if SPINE_TRIANGLECHECK
workingAttachmentsItems[i] = attachment;
int attachmentVertexCount = 0, attachmentTriangleCount = 0;
#endif
object rendererObject = null; // An AtlasRegion in plain Spine-Unity. Spine-TK2D hooks into TK2D's system. eventual source of Material object.
bool noRender = false; // Using this allows empty slots as separators, and keeps separated parts more stable despite slots being reordered
var regionAttachment = attachment as RegionAttachment;
if (regionAttachment != null) {
rendererObject = regionAttachment.RendererObject;
#if SPINE_TRIANGLECHECK
attachmentVertexCount = 4;
attachmentTriangleCount = 6;
#endif
} else {
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
rendererObject = meshAttachment.RendererObject;
#if SPINE_TRIANGLECHECK
attachmentVertexCount = meshAttachment.worldVerticesLength >> 1;
attachmentTriangleCount = meshAttachment.triangles.Length;
#endif
} else {
#if SPINE_TRIANGLECHECK
var clippingAttachment = attachment as ClippingAttachment;
if (clippingAttachment != null) {
clippingEndSlot = clippingAttachment.endSlot;
clippingAttachmentSource = i;
current.hasClipping = true;
skeletonHasClipping = true;
}
#endif
noRender = true;
}
}
// Create a new SubmeshInstruction when material changes. (or when forced to separate by a submeshSeparator)
// Slot with a separator/new material will become the starting slot of the next new instruction.
if (hasSeparators) { //current.forceSeparate = hasSeparators && separatorSlots.Contains(slot);
current.forceSeparate = false;
for (int s = 0; s < separatorCount; s++) {
if (Slot.ReferenceEquals(slot, separatorSlots[s])) {
current.forceSeparate = true;
break;
}
}
}
if (noRender) {
if (current.forceSeparate && generateMeshOverride) { // && current.rawVertexCount > 0) {
{ // Add
current.endSlot = i;
current.preActiveClippingSlotSource = lastPreActiveClipping;
workingSubmeshInstructions.Resize(submeshIndex + 1);
workingSubmeshInstructions.Items[submeshIndex] = current;
submeshIndex++;
}
current.startSlot = i;
lastPreActiveClipping = clippingAttachmentSource;
#if SPINE_TRIANGLECHECK
current.rawTriangleCount = 0;
current.rawVertexCount = 0;
current.rawFirstVertexIndex = totalRawVertexCount;
current.hasClipping = clippingAttachmentSource >= 0;
#endif
}
} else {
#if !SPINE_TK2D
Material material;
if (isCustomSlotMaterialsPopulated) {
if (!customSlotMaterials.TryGetValue(slot, out material))
material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
} else {
material = (Material)((AtlasRegion)rendererObject).page.rendererObject;
}
#else
Material material = (rendererObject is Material) ? (Material)rendererObject : (Material)((AtlasRegion)rendererObject).page.rendererObject;
#endif
if (current.forceSeparate || (current.rawVertexCount > 0 && !System.Object.ReferenceEquals(current.material, material))) { // Material changed. Add the previous submesh.
{ // Add
current.endSlot = i;
current.preActiveClippingSlotSource = lastPreActiveClipping;
workingSubmeshInstructions.Resize(submeshIndex + 1);
workingSubmeshInstructions.Items[submeshIndex] = current;
submeshIndex++;
}
current.startSlot = i;
lastPreActiveClipping = clippingAttachmentSource;
#if SPINE_TRIANGLECHECK
current.rawTriangleCount = 0;
current.rawVertexCount = 0;
current.rawFirstVertexIndex = totalRawVertexCount;
current.hasClipping = clippingAttachmentSource >= 0;
#endif
}
// Update state for the next Attachment.
current.material = material;
#if SPINE_TRIANGLECHECK
current.rawTriangleCount += attachmentTriangleCount;
current.rawVertexCount += attachmentVertexCount;
current.rawFirstVertexIndex = totalRawVertexCount;
totalRawVertexCount += attachmentVertexCount;
#endif
}
if (clippingEndSlot != null && slot.data == clippingEndSlot && i != clippingAttachmentSource) {
clippingEndSlot = null;
clippingAttachmentSource = -1;
}
}
if (current.rawVertexCount > 0) {
{ // Add last or only submesh.
current.endSlot = drawOrderCount;
current.preActiveClippingSlotSource = lastPreActiveClipping;
current.forceSeparate = false;
workingSubmeshInstructions.Resize(submeshIndex + 1);
workingSubmeshInstructions.Items[submeshIndex] = current;
//submeshIndex++;
}
}
#if SPINE_TRIANGLECHECK
instructionOutput.hasActiveClipping = skeletonHasClipping;
instructionOutput.rawVertexCount = totalRawVertexCount;
#endif
instructionOutput.immutableTriangles = immutableTriangles;
}
public static void TryReplaceMaterials (ExposedList<SubmeshInstruction> workingSubmeshInstructions, Dictionary<Material, Material> customMaterialOverride) {
// Material overrides are done here so they can be applied per submesh instead of per slot
// but they will still be passed through the GenerateMeshOverride delegate,
// and will still go through the normal material match check step in STEP 3.
var wsii = workingSubmeshInstructions.Items;
for (int i = 0; i < workingSubmeshInstructions.Count; i++) {
var m = wsii[i].material;
Material mo;
if (customMaterialOverride.TryGetValue(m, out mo))
wsii[i].material = mo;
}
}
#endregion
#region Step 2 : Populate vertex data and triangle index buffers.
public void Begin () {
vertexBuffer.Clear(false);
colorBuffer.Clear(false);
uvBuffer.Clear(false);
clipper.ClipEnd();
{
meshBoundsMin.x = BoundsMinDefault;
meshBoundsMin.y = BoundsMinDefault;
meshBoundsMax.x = BoundsMaxDefault;
meshBoundsMax.y = BoundsMaxDefault;
meshBoundsThickness = 0f;
}
submeshIndex = 0;
submeshes.Count = 1;
//submeshes.Items[0].Clear(false);
}
public void AddSubmesh (SubmeshInstruction instruction, bool updateTriangles = true) {
var settings = this.settings;
int newSubmeshCount = submeshIndex + 1;
if (submeshes.Items.Length < newSubmeshCount)
submeshes.Resize(newSubmeshCount);
submeshes.Count = newSubmeshCount;
var submesh = submeshes.Items[submeshIndex];
if (submesh == null)
submeshes.Items[submeshIndex] = submesh = new ExposedList<int>();
submesh.Clear(false);
var skeleton = instruction.skeleton;
var drawOrderItems = skeleton.drawOrder.Items;
Color32 color = default(Color32);
float skeletonA = skeleton.a, skeletonR = skeleton.r, skeletonG = skeleton.g, skeletonB = skeleton.b;
Vector2 meshBoundsMin = this.meshBoundsMin, meshBoundsMax = this.meshBoundsMax;
// Settings
float zSpacing = settings.zSpacing;
bool pmaVertexColors = settings.pmaVertexColors;
bool tintBlack = settings.tintBlack;
#if SPINE_TRIANGLECHECK
bool useClipping = settings.useClipping && instruction.hasClipping;
#else
bool useClipping = settings.useClipping;
#endif
bool canvasGroupTintBlack = settings.tintBlack && settings.canvasGroupTintBlack;
if (useClipping) {
if (instruction.preActiveClippingSlotSource >= 0) {
var slot = drawOrderItems[instruction.preActiveClippingSlotSource];
clipper.ClipStart(slot, slot.attachment as ClippingAttachment);
}
}
for (int slotIndex = instruction.startSlot; slotIndex < instruction.endSlot; slotIndex++) {
var slot = drawOrderItems[slotIndex];
if (!slot.bone.active) {
clipper.ClipEnd(slot);
continue;
}
var attachment = slot.attachment;
float z = zSpacing * slotIndex;
var workingVerts = this.tempVerts;
float[] uvs;
int[] attachmentTriangleIndices;
int attachmentVertexCount;
int attachmentIndexCount;
Color c = default(Color);
// Identify and prepare values.
var region = attachment as RegionAttachment;
if (region != null) {
region.ComputeWorldVertices(slot.bone, workingVerts, 0);
uvs = region.uvs;
attachmentTriangleIndices = regionTriangles;
c.r = region.r; c.g = region.g; c.b = region.b; c.a = region.a;
attachmentVertexCount = 4;
attachmentIndexCount = 6;
} else {
var mesh = attachment as MeshAttachment;
if (mesh != null) {
int meshVerticesLength = mesh.worldVerticesLength;
if (workingVerts.Length < meshVerticesLength) {
workingVerts = new float[meshVerticesLength];
this.tempVerts = workingVerts;
}
mesh.ComputeWorldVertices(slot, 0, meshVerticesLength, workingVerts, 0); //meshAttachment.ComputeWorldVertices(slot, tempVerts);
uvs = mesh.uvs;
attachmentTriangleIndices = mesh.triangles;
c.r = mesh.r; c.g = mesh.g; c.b = mesh.b; c.a = mesh.a;
attachmentVertexCount = meshVerticesLength >> 1; // meshVertexCount / 2;
attachmentIndexCount = mesh.triangles.Length;
} else {
if (useClipping) {
var clippingAttachment = attachment as ClippingAttachment;
if (clippingAttachment != null) {
clipper.ClipStart(slot, clippingAttachment);
continue;
}
}
// If not any renderable attachment.
clipper.ClipEnd(slot);
continue;
}
}
float tintBlackAlpha = 1.0f;
if (pmaVertexColors) {
color.a = (byte)(skeletonA * slot.a * c.a * 255);
color.r = (byte)(skeletonR * slot.r * c.r * color.a);
color.g = (byte)(skeletonG * slot.g * c.g * color.a);
color.b = (byte)(skeletonB * slot.b * c.b * color.a);
if (slot.data.blendMode == BlendMode.Additive) {
if (canvasGroupTintBlack)
tintBlackAlpha = 0;
else
color.a = 0;
}
} else {
color.a = (byte)(skeletonA * slot.a * c.a * 255);
color.r = (byte)(skeletonR * slot.r * c.r * 255);
color.g = (byte)(skeletonG * slot.g * c.g * 255);
color.b = (byte)(skeletonB * slot.b * c.b * 255);
}
if (useClipping && clipper.IsClipping) {
clipper.ClipTriangles(workingVerts, attachmentVertexCount << 1, attachmentTriangleIndices, attachmentIndexCount, uvs);
workingVerts = clipper.clippedVertices.Items;
attachmentVertexCount = clipper.clippedVertices.Count >> 1;
attachmentTriangleIndices = clipper.clippedTriangles.Items;
attachmentIndexCount = clipper.clippedTriangles.Count;
uvs = clipper.clippedUVs.Items;
}
// Actually add slot/attachment data into buffers.
if (attachmentVertexCount != 0 && attachmentIndexCount != 0) {
if (tintBlack) {
float r2 = slot.r2;
float g2 = slot.g2;
float b2 = slot.b2;
if (pmaVertexColors) {
float alpha = skeletonA * slot.a * c.a;
r2 *= alpha;
g2 *= alpha;
b2 *= alpha;
}
AddAttachmentTintBlack(r2, g2, b2, tintBlackAlpha, attachmentVertexCount);
}
//AddAttachment(workingVerts, uvs, color, attachmentTriangleIndices, attachmentVertexCount, attachmentIndexCount, ref meshBoundsMin, ref meshBoundsMax, z);
int ovc = vertexBuffer.Count;
// Add data to vertex buffers
{
int newVertexCount = ovc + attachmentVertexCount;
int oldArraySize = vertexBuffer.Items.Length;
if (newVertexCount > oldArraySize) {
int newArraySize = (int)(oldArraySize * 1.3f);
if (newArraySize < newVertexCount) newArraySize = newVertexCount;
Array.Resize(ref vertexBuffer.Items, newArraySize);
Array.Resize(ref uvBuffer.Items, newArraySize);
Array.Resize(ref colorBuffer.Items, newArraySize);
}
vertexBuffer.Count = uvBuffer.Count = colorBuffer.Count = newVertexCount;
}
var vbi = vertexBuffer.Items;
var ubi = uvBuffer.Items;
var cbi = colorBuffer.Items;
if (ovc == 0) {
for (int i = 0; i < attachmentVertexCount; i++) {
int vi = ovc + i;
int i2 = i << 1; // i * 2
float x = workingVerts[i2];
float y = workingVerts[i2 + 1];
vbi[vi].x = x;
vbi[vi].y = y;
vbi[vi].z = z;
ubi[vi].x = uvs[i2];
ubi[vi].y = uvs[i2 + 1];
cbi[vi] = color;
// Calculate bounds.
if (x < meshBoundsMin.x) meshBoundsMin.x = x;
if (x > meshBoundsMax.x) meshBoundsMax.x = x;
if (y < meshBoundsMin.y) meshBoundsMin.y = y;
if (y > meshBoundsMax.y) meshBoundsMax.y = y;
}
} else {
for (int i = 0; i < attachmentVertexCount; i++) {
int vi = ovc + i;
int i2 = i << 1; // i * 2
float x = workingVerts[i2];
float y = workingVerts[i2 + 1];
vbi[vi].x = x;
vbi[vi].y = y;
vbi[vi].z = z;
ubi[vi].x = uvs[i2];
ubi[vi].y = uvs[i2 + 1];
cbi[vi] = color;
// Calculate bounds.
if (x < meshBoundsMin.x) meshBoundsMin.x = x;
else if (x > meshBoundsMax.x) meshBoundsMax.x = x;
if (y < meshBoundsMin.y) meshBoundsMin.y = y;
else if (y > meshBoundsMax.y) meshBoundsMax.y = y;
}
}
// Add data to triangle buffer
if (updateTriangles) {
int oldTriangleCount = submesh.Count;
{ //submesh.Resize(oldTriangleCount + attachmentIndexCount);
int newTriangleCount = oldTriangleCount + attachmentIndexCount;
if (newTriangleCount > submesh.Items.Length) Array.Resize(ref submesh.Items, newTriangleCount);
submesh.Count = newTriangleCount;
}
var submeshItems = submesh.Items;
for (int i = 0; i < attachmentIndexCount; i++)
submeshItems[oldTriangleCount + i] = attachmentTriangleIndices[i] + ovc;
}
}
clipper.ClipEnd(slot);
}
clipper.ClipEnd();
this.meshBoundsMin = meshBoundsMin;
this.meshBoundsMax = meshBoundsMax;
meshBoundsThickness = instruction.endSlot * zSpacing;
// Trim or zero submesh triangles.
var currentSubmeshItems = submesh.Items;
for (int i = submesh.Count, n = currentSubmeshItems.Length; i < n; i++)
currentSubmeshItems[i] = 0;
submeshIndex++; // Next AddSubmesh will use a new submeshIndex value.
}
public void BuildMesh (SkeletonRendererInstruction instruction, bool updateTriangles) {
var wsii = instruction.submeshInstructions.Items;
for (int i = 0, n = instruction.submeshInstructions.Count; i < n; i++)
this.AddSubmesh(wsii[i], updateTriangles);
}
// Use this faster method when no clipping is involved.
public void BuildMeshWithArrays (SkeletonRendererInstruction instruction, bool updateTriangles) {
var settings = this.settings;
bool canvasGroupTintBlack = settings.tintBlack && settings.canvasGroupTintBlack;
int totalVertexCount = instruction.rawVertexCount;
// Add data to vertex buffers
{
if (totalVertexCount > vertexBuffer.Items.Length) { // Manual ExposedList.Resize()
Array.Resize(ref vertexBuffer.Items, totalVertexCount);
Array.Resize(ref uvBuffer.Items, totalVertexCount);
Array.Resize(ref colorBuffer.Items, totalVertexCount);
}
vertexBuffer.Count = uvBuffer.Count = colorBuffer.Count = totalVertexCount;
}
// Populate Verts
Color32 color = default(Color32);
int vertexIndex = 0;
var tempVerts = this.tempVerts;
Vector2 bmin = this.meshBoundsMin;
Vector2 bmax = this.meshBoundsMax;
var vbi = vertexBuffer.Items;
var ubi = uvBuffer.Items;
var cbi = colorBuffer.Items;
int lastSlotIndex = 0;
// drawOrder[endSlot] is excluded
for (int si = 0, n = instruction.submeshInstructions.Count; si < n; si++) {
var submesh = instruction.submeshInstructions.Items[si];
var skeleton = submesh.skeleton;
var drawOrderItems = skeleton.drawOrder.Items;
float a = skeleton.a, r = skeleton.r, g = skeleton.g, b = skeleton.b;
int endSlot = submesh.endSlot;
int startSlot = submesh.startSlot;
lastSlotIndex = endSlot;
if (settings.tintBlack) {
Vector2 rg, b2;
int vi = vertexIndex;
b2.y = 1f;
{
if (uv2 == null) {
uv2 = new ExposedList<Vector2>();
uv3 = new ExposedList<Vector2>();
}
if (totalVertexCount > uv2.Items.Length) { // Manual ExposedList.Resize()
Array.Resize(ref uv2.Items, totalVertexCount);
Array.Resize(ref uv3.Items, totalVertexCount);
}
uv2.Count = uv3.Count = totalVertexCount;
}
var uv2i = uv2.Items;
var uv3i = uv3.Items;
for (int slotIndex = startSlot; slotIndex < endSlot; slotIndex++) {
var slot = drawOrderItems[slotIndex];
if (!slot.bone.active) continue;
var attachment = slot.attachment;
rg.x = slot.r2; //r
rg.y = slot.g2; //g
b2.x = slot.b2; //b
b2.y = 1.0f;
var regionAttachment = attachment as RegionAttachment;
if (regionAttachment != null) {
if (settings.pmaVertexColors) {
float alpha = a * slot.a * regionAttachment.a;
rg.x *= alpha;
rg.y *= alpha;
b2.x *= alpha;
b2.y = slot.data.blendMode == BlendMode.Additive ? 0 : alpha;
}
uv2i[vi] = rg; uv2i[vi + 1] = rg; uv2i[vi + 2] = rg; uv2i[vi + 3] = rg;
uv3i[vi] = b2; uv3i[vi + 1] = b2; uv3i[vi + 2] = b2; uv3i[vi + 3] = b2;
vi += 4;
} else { //} if (settings.renderMeshes) {
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
if (settings.pmaVertexColors) {
float alpha = a * slot.a * meshAttachment.a;
rg.x *= alpha;
rg.y *= alpha;
b2.x *= alpha;
b2.y = slot.data.blendMode == BlendMode.Additive ? 0 : alpha;
}
int meshVertexCount = meshAttachment.worldVerticesLength;
for (int iii = 0; iii < meshVertexCount; iii += 2) {
uv2i[vi] = rg;
uv3i[vi] = b2;
vi++;
}
}
}
}
}
for (int slotIndex = startSlot; slotIndex < endSlot; slotIndex++) {
var slot = drawOrderItems[slotIndex];
if (!slot.bone.active) continue;
var attachment = slot.attachment;
float z = slotIndex * settings.zSpacing;
var regionAttachment = attachment as RegionAttachment;
if (regionAttachment != null) {
regionAttachment.ComputeWorldVertices(slot.bone, tempVerts, 0);
float x1 = tempVerts[RegionAttachment.BLX], y1 = tempVerts[RegionAttachment.BLY];
float x2 = tempVerts[RegionAttachment.ULX], y2 = tempVerts[RegionAttachment.ULY];
float x3 = tempVerts[RegionAttachment.URX], y3 = tempVerts[RegionAttachment.URY];
float x4 = tempVerts[RegionAttachment.BRX], y4 = tempVerts[RegionAttachment.BRY];
vbi[vertexIndex].x = x1; vbi[vertexIndex].y = y1; vbi[vertexIndex].z = z;
vbi[vertexIndex + 1].x = x4; vbi[vertexIndex + 1].y = y4; vbi[vertexIndex + 1].z = z;
vbi[vertexIndex + 2].x = x2; vbi[vertexIndex + 2].y = y2; vbi[vertexIndex + 2].z = z;
vbi[vertexIndex + 3].x = x3; vbi[vertexIndex + 3].y = y3; vbi[vertexIndex + 3].z = z;
if (settings.pmaVertexColors) {
color.a = (byte)(a * slot.a * regionAttachment.a * 255);
color.r = (byte)(r * slot.r * regionAttachment.r * color.a);
color.g = (byte)(g * slot.g * regionAttachment.g * color.a);
color.b = (byte)(b * slot.b * regionAttachment.b * color.a);
if (slot.data.blendMode == BlendMode.Additive && !canvasGroupTintBlack) color.a = 0;
} else {
color.a = (byte)(a * slot.a * regionAttachment.a * 255);
color.r = (byte)(r * slot.r * regionAttachment.r * 255);
color.g = (byte)(g * slot.g * regionAttachment.g * 255);
color.b = (byte)(b * slot.b * regionAttachment.b * 255);
}
cbi[vertexIndex] = color; cbi[vertexIndex + 1] = color; cbi[vertexIndex + 2] = color; cbi[vertexIndex + 3] = color;
float[] regionUVs = regionAttachment.uvs;
ubi[vertexIndex].x = regionUVs[RegionAttachment.BLX]; ubi[vertexIndex].y = regionUVs[RegionAttachment.BLY];
ubi[vertexIndex + 1].x = regionUVs[RegionAttachment.BRX]; ubi[vertexIndex + 1].y = regionUVs[RegionAttachment.BRY];
ubi[vertexIndex + 2].x = regionUVs[RegionAttachment.ULX]; ubi[vertexIndex + 2].y = regionUVs[RegionAttachment.ULY];
ubi[vertexIndex + 3].x = regionUVs[RegionAttachment.URX]; ubi[vertexIndex + 3].y = regionUVs[RegionAttachment.URY];
if (x1 < bmin.x) bmin.x = x1; // Potential first attachment bounds initialization. Initial min should not block initial max. Same for Y below.
if (x1 > bmax.x) bmax.x = x1;
if (x2 < bmin.x) bmin.x = x2;
else if (x2 > bmax.x) bmax.x = x2;
if (x3 < bmin.x) bmin.x = x3;
else if (x3 > bmax.x) bmax.x = x3;
if (x4 < bmin.x) bmin.x = x4;
else if (x4 > bmax.x) bmax.x = x4;
if (y1 < bmin.y) bmin.y = y1;
if (y1 > bmax.y) bmax.y = y1;
if (y2 < bmin.y) bmin.y = y2;
else if (y2 > bmax.y) bmax.y = y2;
if (y3 < bmin.y) bmin.y = y3;
else if (y3 > bmax.y) bmax.y = y3;
if (y4 < bmin.y) bmin.y = y4;
else if (y4 > bmax.y) bmax.y = y4;
vertexIndex += 4;
} else { //if (settings.renderMeshes) {
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
int meshVertexCount = meshAttachment.worldVerticesLength;
if (tempVerts.Length < meshVertexCount) this.tempVerts = tempVerts = new float[meshVertexCount];
meshAttachment.ComputeWorldVertices(slot, tempVerts);
if (settings.pmaVertexColors) {
color.a = (byte)(a * slot.a * meshAttachment.a * 255);
color.r = (byte)(r * slot.r * meshAttachment.r * color.a);
color.g = (byte)(g * slot.g * meshAttachment.g * color.a);
color.b = (byte)(b * slot.b * meshAttachment.b * color.a);
if (slot.data.blendMode == BlendMode.Additive && !canvasGroupTintBlack) color.a = 0;
} else {
color.a = (byte)(a * slot.a * meshAttachment.a * 255);
color.r = (byte)(r * slot.r * meshAttachment.r * 255);
color.g = (byte)(g * slot.g * meshAttachment.g * 255);
color.b = (byte)(b * slot.b * meshAttachment.b * 255);
}
float[] attachmentUVs = meshAttachment.uvs;
// Potential first attachment bounds initialization. See conditions in RegionAttachment logic.
if (vertexIndex == 0) {
// Initial min should not block initial max.
// vi == vertexIndex does not always mean the bounds are fresh. It could be a submesh. Do not nuke old values by omitting the check.
// Should know that this is the first attachment in the submesh. slotIndex == startSlot could be an empty slot.
float fx = tempVerts[0], fy = tempVerts[1];
if (fx < bmin.x) bmin.x = fx;
if (fx > bmax.x) bmax.x = fx;
if (fy < bmin.y) bmin.y = fy;
if (fy > bmax.y) bmax.y = fy;
}
for (int iii = 0; iii < meshVertexCount; iii += 2) {
float x = tempVerts[iii], y = tempVerts[iii + 1];
vbi[vertexIndex].x = x; vbi[vertexIndex].y = y; vbi[vertexIndex].z = z;
cbi[vertexIndex] = color; ubi[vertexIndex].x = attachmentUVs[iii]; ubi[vertexIndex].y = attachmentUVs[iii + 1];
if (x < bmin.x) bmin.x = x;
else if (x > bmax.x) bmax.x = x;
if (y < bmin.y) bmin.y = y;
else if (y > bmax.y) bmax.y = y;
vertexIndex++;
}
}
}
}
}
this.meshBoundsMin = bmin;
this.meshBoundsMax = bmax;
this.meshBoundsThickness = lastSlotIndex * settings.zSpacing;
int submeshInstructionCount = instruction.submeshInstructions.Count;
submeshes.Count = submeshInstructionCount;
// Add triangles
if (updateTriangles) {
// Match submesh buffers count with submeshInstruction count.
if (this.submeshes.Items.Length < submeshInstructionCount) {
this.submeshes.Resize(submeshInstructionCount);
for (int i = 0, n = submeshInstructionCount; i < n; i++) {
var submeshBuffer = this.submeshes.Items[i];
if (submeshBuffer == null)
this.submeshes.Items[i] = new ExposedList<int>();
else
submeshBuffer.Clear(false);
}
}
var submeshInstructionsItems = instruction.submeshInstructions.Items; // This relies on the resize above.
// Fill the buffers.
int attachmentFirstVertex = 0;
for (int smbi = 0; smbi < submeshInstructionCount; smbi++) {
var submeshInstruction = submeshInstructionsItems[smbi];
var currentSubmeshBuffer = this.submeshes.Items[smbi];
{ //submesh.Resize(submesh.rawTriangleCount);
int newTriangleCount = submeshInstruction.rawTriangleCount;
if (newTriangleCount > currentSubmeshBuffer.Items.Length)
Array.Resize(ref currentSubmeshBuffer.Items, newTriangleCount);
else if (newTriangleCount < currentSubmeshBuffer.Items.Length) {
// Zero the extra.
var sbi = currentSubmeshBuffer.Items;
for (int ei = newTriangleCount, nn = sbi.Length; ei < nn; ei++)
sbi[ei] = 0;
}
currentSubmeshBuffer.Count = newTriangleCount;
}
var tris = currentSubmeshBuffer.Items;
int triangleIndex = 0;
var skeleton = submeshInstruction.skeleton;
var drawOrderItems = skeleton.drawOrder.Items;
for (int slotIndex = submeshInstruction.startSlot, endSlot = submeshInstruction.endSlot; slotIndex < endSlot; slotIndex++) {
var slot = drawOrderItems[slotIndex];
if (!slot.bone.active) continue;
var attachment = drawOrderItems[slotIndex].attachment;
if (attachment is RegionAttachment) {
tris[triangleIndex] = attachmentFirstVertex;
tris[triangleIndex + 1] = attachmentFirstVertex + 2;
tris[triangleIndex + 2] = attachmentFirstVertex + 1;
tris[triangleIndex + 3] = attachmentFirstVertex + 2;
tris[triangleIndex + 4] = attachmentFirstVertex + 3;
tris[triangleIndex + 5] = attachmentFirstVertex + 1;
triangleIndex += 6;
attachmentFirstVertex += 4;
continue;
}
var meshAttachment = attachment as MeshAttachment;
if (meshAttachment != null) {
int[] attachmentTriangles = meshAttachment.triangles;
for (int ii = 0, nn = attachmentTriangles.Length; ii < nn; ii++, triangleIndex++)
tris[triangleIndex] = attachmentFirstVertex + attachmentTriangles[ii];
attachmentFirstVertex += meshAttachment.worldVerticesLength >> 1; // length/2;
}
}
}
}
}