TMP_MeshInfo.cs 24.8 KB
Newer Older
BlackAngle233's avatar
212    
BlackAngle233 committed
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
using UnityEngine;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;


namespace TMPro
{
    public enum VertexSortingOrder { Normal, Reverse };

    /// <summary>
    /// Structure which contains the vertex attributes (geometry) of the text object.
    /// </summary>
    public struct TMP_MeshInfo
    {
        private static readonly Color32 s_DefaultColor = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
        private static readonly Vector3 s_DefaultNormal = new Vector3(0.0f, 0.0f, -1f);
        private static readonly Vector4 s_DefaultTangent = new Vector4(-1f, 0.0f, 0.0f, 1f);
        private static readonly Bounds s_DefaultBounds = new Bounds();

        public Mesh mesh;
        public int vertexCount;

        public Vector3[] vertices;
        public Vector3[] normals;
        public Vector4[] tangents;

        public Vector2[] uvs0;
        public Vector2[] uvs2;
        //public Vector2[] uvs4;
        public Color32[] colors32;
        public int[] triangles;


        /// <summary>
        /// Function to pre-allocate vertex attributes for a mesh of size X.
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="size"></param>
        public TMP_MeshInfo(Mesh mesh, int size)
        {
            // Reference to the TMP Text Component.
            //this.textComponent = null;

            // Clear existing mesh data
            if (mesh == null)
                mesh = new Mesh();
            else
                mesh.Clear();

            this.mesh = mesh;

            // Limit the mesh to less than 65535 vertices which is the limit for Unity's Mesh.
            size = Mathf.Min(size, 16383);

            int sizeX4 = size * 4;
            int sizeX6 = size * 6;

            this.vertexCount = 0;

            this.vertices = new Vector3[sizeX4];
            this.uvs0 = new Vector2[sizeX4];
            this.uvs2 = new Vector2[sizeX4];
            //this.uvs4 = new Vector2[sizeX4]; // SDF scale data
            this.colors32 = new Color32[sizeX4];

            this.normals = new Vector3[sizeX4];
            this.tangents = new Vector4[sizeX4];

            this.triangles = new int[sizeX6];

            int index_X6 = 0;
            int index_X4 = 0;
            while (index_X4 / 4 < size)
            {
                for (int i = 0; i < 4; i++)
                {
                    this.vertices[index_X4 + i] = Vector3.zero;
                    this.uvs0[index_X4 + i] = Vector2.zero;
                    this.uvs2[index_X4 + i] = Vector2.zero;
                    //this.uvs4[index_X4 + i] = Vector2.zero;
                    this.colors32[index_X4 + i] = s_DefaultColor;
                    this.normals[index_X4 + i] = s_DefaultNormal;
                    this.tangents[index_X4 + i] = s_DefaultTangent;
                }

                this.triangles[index_X6 + 0] = index_X4 + 0;
                this.triangles[index_X6 + 1] = index_X4 + 1;
                this.triangles[index_X6 + 2] = index_X4 + 2;
                this.triangles[index_X6 + 3] = index_X4 + 2;
                this.triangles[index_X6 + 4] = index_X4 + 3;
                this.triangles[index_X6 + 5] = index_X4 + 0;

                index_X4 += 4;
                index_X6 += 6;
            }

            // Pre-assign base vertex attributes.
            this.mesh.vertices = this.vertices;
            this.mesh.normals = this.normals;
            this.mesh.tangents = this.tangents;
            this.mesh.triangles = this.triangles;
            this.mesh.bounds = s_DefaultBounds;
        }


        /// <summary>
        /// Function to pre-allocate vertex attributes for a mesh of size X.
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="size"></param>
        /// <param name="isVolumetric"></param>
        public TMP_MeshInfo(Mesh mesh, int size, bool isVolumetric)
        {
            // Reference to the TMP Text Component.
            //this.textComponent = null;

            // Clear existing mesh data
            if (mesh == null)
                mesh = new Mesh();
            else
                mesh.Clear();

            this.mesh = mesh;

            int s0 = !isVolumetric ? 4 : 8;
            int s1 = !isVolumetric ? 6 : 36;

            // Limit the mesh to less than 65535 vertices which is the limit for Unity's Mesh.
            size = Mathf.Min(size, 65532 / s0);

            int size_x_s0 = size * s0;
            int size_x_s1 = size * s1;

            this.vertexCount = 0;

            this.vertices = new Vector3[size_x_s0];
            this.uvs0 = new Vector2[size_x_s0];
            this.uvs2 = new Vector2[size_x_s0];
            //this.uvs4 = new Vector2[sizeX8]; // SDF scale data
            this.colors32 = new Color32[size_x_s0];

            this.normals = new Vector3[size_x_s0];
            this.tangents = new Vector4[size_x_s0];

            this.triangles = new int[size_x_s1];

            int index_x_s0 = 0;
            int index_x_s1 = 0;
            while (index_x_s0 / s0 < size)
            {
                for (int i = 0; i < s0; i++)
                {
                    this.vertices[index_x_s0 + i] = Vector3.zero;
                    this.uvs0[index_x_s0 + i] = Vector2.zero;
                    this.uvs2[index_x_s0 + i] = Vector2.zero;
                    //this.uvs4[index_X4 + i] = Vector2.zero;
                    this.colors32[index_x_s0 + i] = s_DefaultColor;
                    this.normals[index_x_s0 + i] = s_DefaultNormal;
                    this.tangents[index_x_s0 + i] = s_DefaultTangent;
                }

                // Front Face
                this.triangles[index_x_s1 + 0] = index_x_s0 + 0;
                this.triangles[index_x_s1 + 1] = index_x_s0 + 1;
                this.triangles[index_x_s1 + 2] = index_x_s0 + 2;
                this.triangles[index_x_s1 + 3] = index_x_s0 + 2;
                this.triangles[index_x_s1 + 4] = index_x_s0 + 3;
                this.triangles[index_x_s1 + 5] = index_x_s0 + 0;

                if (isVolumetric)
                {
                    // Left Face
                    this.triangles[index_x_s1 + 6] = index_x_s0 + 4;
                    this.triangles[index_x_s1 + 7] = index_x_s0 + 5;
                    this.triangles[index_x_s1 + 8] = index_x_s0 + 1;
                    this.triangles[index_x_s1 + 9] = index_x_s0 + 1;
                    this.triangles[index_x_s1 + 10] = index_x_s0 + 0;
                    this.triangles[index_x_s1 + 11] = index_x_s0 + 4;

                    // Right Face
                    this.triangles[index_x_s1 + 12] = index_x_s0 + 3;
                    this.triangles[index_x_s1 + 13] = index_x_s0 + 2;
                    this.triangles[index_x_s1 + 14] = index_x_s0 + 6;
                    this.triangles[index_x_s1 + 15] = index_x_s0 + 6;
                    this.triangles[index_x_s1 + 16] = index_x_s0 + 7;
                    this.triangles[index_x_s1 + 17] = index_x_s0 + 3;

                    // Top Face
                    this.triangles[index_x_s1 + 18] = index_x_s0 + 1;
                    this.triangles[index_x_s1 + 19] = index_x_s0 + 5;
                    this.triangles[index_x_s1 + 20] = index_x_s0 + 6;
                    this.triangles[index_x_s1 + 21] = index_x_s0 + 6;
                    this.triangles[index_x_s1 + 22] = index_x_s0 + 2;
                    this.triangles[index_x_s1 + 23] = index_x_s0 + 1;

                    // Bottom Face
                    this.triangles[index_x_s1 + 24] = index_x_s0 + 4;
                    this.triangles[index_x_s1 + 25] = index_x_s0 + 0;
                    this.triangles[index_x_s1 + 26] = index_x_s0 + 3;
                    this.triangles[index_x_s1 + 27] = index_x_s0 + 3;
                    this.triangles[index_x_s1 + 28] = index_x_s0 + 7;
                    this.triangles[index_x_s1 + 29] = index_x_s0 + 4;

                    // Back Face
                    this.triangles[index_x_s1 + 30] = index_x_s0 + 7;
                    this.triangles[index_x_s1 + 31] = index_x_s0 + 6;
                    this.triangles[index_x_s1 + 32] = index_x_s0 + 5;
                    this.triangles[index_x_s1 + 33] = index_x_s0 + 5;
                    this.triangles[index_x_s1 + 34] = index_x_s0 + 4;
                    this.triangles[index_x_s1 + 35] = index_x_s0 + 7;
                }

                index_x_s0 += s0;
                index_x_s1 += s1;
            }

            // Pre-assign base vertex attributes.
            this.mesh.vertices = this.vertices;
            this.mesh.normals = this.normals;
            this.mesh.tangents = this.tangents;
            this.mesh.triangles = this.triangles;
            this.mesh.bounds = s_DefaultBounds;
        }


        /// <summary>
        /// Function to resized the content of MeshData and re-assign normals, tangents and triangles.
        /// </summary>
        /// <param name="meshData"></param>
        /// <param name="size"></param>
        public void ResizeMeshInfo(int size)
        {
            // Limit the mesh to less than 65535 vertices which is the limit for Unity's Mesh.
            size = Mathf.Min(size, 16383);

            int size_X4 = size * 4;
            int size_X6 = size * 6;

            int previousSize = this.vertices.Length / 4;

            Array.Resize(ref this.vertices, size_X4);
            Array.Resize(ref this.normals, size_X4);
            Array.Resize(ref this.tangents, size_X4);

            Array.Resize(ref this.uvs0, size_X4);
            Array.Resize(ref this.uvs2, size_X4);
            //Array.Resize(ref this.uvs4, size_X4);

            Array.Resize(ref this.colors32, size_X4);

            Array.Resize(ref this.triangles, size_X6);


            // Re-assign Normals, Tangents and Triangles
            if (size <= previousSize)
            {
                this.mesh.triangles = this.triangles;
                this.mesh.vertices = this.vertices;
                this.mesh.normals = this.normals;
                this.mesh.tangents = this.tangents;

                return;
            }

            for (int i = previousSize; i < size; i++)
            {
                int index_X4 = i * 4;
                int index_X6 = i * 6;

                this.normals[0 + index_X4] = s_DefaultNormal;
                this.normals[1 + index_X4] = s_DefaultNormal;
                this.normals[2 + index_X4] = s_DefaultNormal;
                this.normals[3 + index_X4] = s_DefaultNormal;

                this.tangents[0 + index_X4] = s_DefaultTangent;
                this.tangents[1 + index_X4] = s_DefaultTangent;
                this.tangents[2 + index_X4] = s_DefaultTangent;
                this.tangents[3 + index_X4] = s_DefaultTangent;

                // Setup Triangles
                this.triangles[0 + index_X6] = 0 + index_X4;
                this.triangles[1 + index_X6] = 1 + index_X4;
                this.triangles[2 + index_X6] = 2 + index_X4;
                this.triangles[3 + index_X6] = 2 + index_X4;
                this.triangles[4 + index_X6] = 3 + index_X4;
                this.triangles[5 + index_X6] = 0 + index_X4;
            }

            this.mesh.vertices = this.vertices;
            this.mesh.normals = this.normals;
            this.mesh.tangents = this.tangents;
            this.mesh.triangles = this.triangles;
        }


        /// <summary>
        /// Function to resized the content of MeshData and re-assign normals, tangents and triangles.
        /// </summary>
        /// <param name="size"></param>
        /// <param name="isVolumetric"></param>
        public void ResizeMeshInfo(int size, bool isVolumetric)
        {
            int s0 = !isVolumetric ? 4 : 8;
            int s1 = !isVolumetric ? 6 : 36;

            // Limit the mesh to less than 65535 vertices which is the limit for Unity's Mesh.
            size = Mathf.Min(size, 65532 / s0);

            int size_X4 = size * s0;
            int size_X6 = size * s1;

            int previousSize = this.vertices.Length / s0;

            Array.Resize(ref this.vertices, size_X4);
            Array.Resize(ref this.normals, size_X4);
            Array.Resize(ref this.tangents, size_X4);

            Array.Resize(ref this.uvs0, size_X4);
            Array.Resize(ref this.uvs2, size_X4);
            //Array.Resize(ref this.uvs4, size_X4);

            Array.Resize(ref this.colors32, size_X4);

            Array.Resize(ref this.triangles, size_X6);


            // Re-assign Normals, Tangents and Triangles
            if (size <= previousSize)
            {
                this.mesh.triangles = this.triangles;
                this.mesh.vertices = this.vertices;
                this.mesh.normals = this.normals;
                this.mesh.tangents = this.tangents;

                return;
            }

            for (int i = previousSize; i < size; i++)
            {
                int index_X4 = i * s0;
                int index_X6 = i * s1;

                this.normals[0 + index_X4] = s_DefaultNormal;
                this.normals[1 + index_X4] = s_DefaultNormal;
                this.normals[2 + index_X4] = s_DefaultNormal;
                this.normals[3 + index_X4] = s_DefaultNormal;

                this.tangents[0 + index_X4] = s_DefaultTangent;
                this.tangents[1 + index_X4] = s_DefaultTangent;
                this.tangents[2 + index_X4] = s_DefaultTangent;
                this.tangents[3 + index_X4] = s_DefaultTangent;

                if (isVolumetric)
                {
                    this.normals[4 + index_X4] = s_DefaultNormal;
                    this.normals[5 + index_X4] = s_DefaultNormal;
                    this.normals[6 + index_X4] = s_DefaultNormal;
                    this.normals[7 + index_X4] = s_DefaultNormal;

                    this.tangents[4 + index_X4] = s_DefaultTangent;
                    this.tangents[5 + index_X4] = s_DefaultTangent;
                    this.tangents[6 + index_X4] = s_DefaultTangent;
                    this.tangents[7 + index_X4] = s_DefaultTangent;
                }

                // Setup Triangles
                this.triangles[0 + index_X6] = 0 + index_X4;
                this.triangles[1 + index_X6] = 1 + index_X4;
                this.triangles[2 + index_X6] = 2 + index_X4;
                this.triangles[3 + index_X6] = 2 + index_X4;
                this.triangles[4 + index_X6] = 3 + index_X4;
                this.triangles[5 + index_X6] = 0 + index_X4;

                if (isVolumetric)
                {
                    // Left Face
                    this.triangles[index_X6 + 6] = index_X4 + 4;
                    this.triangles[index_X6 + 7] = index_X4 + 5;
                    this.triangles[index_X6 + 8] = index_X4 + 1;
                    this.triangles[index_X6 + 9] = index_X4 + 1;
                    this.triangles[index_X6 + 10] = index_X4 + 0;
                    this.triangles[index_X6 + 11] = index_X4 + 4;

                    // Right Face
                    this.triangles[index_X6 + 12] = index_X4 + 3;
                    this.triangles[index_X6 + 13] = index_X4 + 2;
                    this.triangles[index_X6 + 14] = index_X4 + 6;
                    this.triangles[index_X6 + 15] = index_X4 + 6;
                    this.triangles[index_X6 + 16] = index_X4 + 7;
                    this.triangles[index_X6 + 17] = index_X4 + 3;

                    // Top Face
                    this.triangles[index_X6 + 18] = index_X4 + 1;
                    this.triangles[index_X6 + 19] = index_X4 + 5;
                    this.triangles[index_X6 + 20] = index_X4 + 6;
                    this.triangles[index_X6 + 21] = index_X4 + 6;
                    this.triangles[index_X6 + 22] = index_X4 + 2;
                    this.triangles[index_X6 + 23] = index_X4 + 1;

                    // Bottom Face
                    this.triangles[index_X6 + 24] = index_X4 + 4;
                    this.triangles[index_X6 + 25] = index_X4 + 0;
                    this.triangles[index_X6 + 26] = index_X4 + 3;
                    this.triangles[index_X6 + 27] = index_X4 + 3;
                    this.triangles[index_X6 + 28] = index_X4 + 7;
                    this.triangles[index_X6 + 29] = index_X4 + 4;

                    // Back Face
                    this.triangles[index_X6 + 30] = index_X4 + 7;
                    this.triangles[index_X6 + 31] = index_X4 + 6;
                    this.triangles[index_X6 + 32] = index_X4 + 5;
                    this.triangles[index_X6 + 33] = index_X4 + 5;
                    this.triangles[index_X6 + 34] = index_X4 + 4;
                    this.triangles[index_X6 + 35] = index_X4 + 7;
                }
            }

            this.mesh.vertices = this.vertices;
            this.mesh.normals = this.normals;
            this.mesh.tangents = this.tangents;
            this.mesh.triangles = this.triangles;
        }


        /// <summary>
        /// Function to clear the vertices while preserving the Triangles, Normals and Tangents.
        /// </summary>
        public void Clear()
        {
            if (this.vertices == null) return;

            Array.Clear(this.vertices, 0, this.vertices.Length);
            this.vertexCount = 0;

            if (this.mesh != null)
                this.mesh.vertices = this.vertices;
        }


        /// <summary>
        /// Function to clear the vertices while preserving the Triangles, Normals and Tangents.
        /// </summary>
        public void Clear(bool uploadChanges)
        {
            if (this.vertices == null) return;

            Array.Clear(this.vertices, 0, this.vertices.Length);
            this.vertexCount = 0;

            if (uploadChanges && this.mesh != null)
                this.mesh.vertices = this.vertices;

            if (this.mesh != null)
                this.mesh.bounds = s_DefaultBounds;
        }


        /// <summary>
        /// Function to clear the vertices while preserving the Triangles, Normals and Tangents.
        /// </summary>
        public void ClearUnusedVertices()
        {
            int length = vertices.Length - vertexCount;

            if (length > 0)
                Array.Clear(vertices, vertexCount, length);
        }


        /// <summary>
        /// Function used to mark unused vertices as degenerate.
        /// </summary>
        /// <param name="startIndex"></param>
        public void ClearUnusedVertices(int startIndex)
        {
            int length = this.vertices.Length - startIndex;

            if (length > 0)
                Array.Clear(this.vertices, startIndex, length);
        }


        /// <summary>
        /// Function used to mark unused vertices as degenerate an upload resulting data to the mesh.
        /// </summary>
        /// <param name="startIndex"></param>
        public void ClearUnusedVertices(int startIndex, bool updateMesh)
        {
            int length = this.vertices.Length - startIndex;

            if (length > 0)
                Array.Clear(this.vertices, startIndex, length);

            if (updateMesh && mesh != null)
                this.mesh.vertices = this.vertices;
        }


        public void SortGeometry (VertexSortingOrder order)
        {
            switch (order)
            {
                case VertexSortingOrder.Normal:
                    // Do nothing 
                    break;
                case VertexSortingOrder.Reverse:
                    int size = vertexCount / 4;
                    for (int i = 0; i < size; i++)
                    {
                        int src = i * 4;
                        int dst = (size - i - 1) * 4;

                        if (src < dst)
                            SwapVertexData(src, dst);

                    }
                    break;
                //case VertexSortingOrder.Depth:
                //    break;

            }
        }


        /// <summary>
        /// Function to rearrange the quads of the text object to change their rendering order.
        /// </summary>
        /// <param name="sortingOrder"></param>
        public void SortGeometry(IList<int> sortingOrder)
        {
            // Make sure the sorting order array is not larger than the vertices array.
            int indexCount = sortingOrder.Count;

            if (indexCount * 4 > vertices.Length) return;

            int src_index;

            for (int dst_index = 0; dst_index < indexCount; dst_index++)
            {
                src_index = sortingOrder[dst_index];

                while (src_index < dst_index)
                {
                    src_index = sortingOrder[src_index];
                }

                // Swap items
                if (src_index != dst_index)
                    SwapVertexData(src_index * 4, dst_index * 4);

                //Debug.Log("Swap element [" + dst_index + "] with [" + src_index + "]. Vertex[" + dst_index + "] is " + vertices[dst_index * 4].z);
            }
        }


        /// <summary>
        /// Method to swap the vertex attributes between src and dst quads.
        /// </summary>
        /// <param name="src">Index of the first vertex attribute of the source character / quad.</param>
        /// <param name="dst">Index of the first vertex attribute of the destination character / quad.</param>
        public void SwapVertexData(int src, int dst)
        {
            int src_Index = src; //  * 4;
            int dst_Index = dst; // * 4;

            // Swap vertices
            Vector3 vertex;
            vertex = vertices[dst_Index + 0];
            vertices[dst_Index + 0] = vertices[src_Index + 0];
            vertices[src_Index + 0] = vertex;

            vertex = vertices[dst_Index + 1];
            vertices[dst_Index + 1] = vertices[src_Index + 1];
            vertices[src_Index + 1] = vertex;

            vertex = vertices[dst_Index + 2];
            vertices[dst_Index + 2] = vertices[src_Index + 2];
            vertices[src_Index + 2] = vertex;

            vertex = vertices[dst_Index + 3];
            vertices[dst_Index + 3] = vertices[src_Index + 3];
            vertices[src_Index + 3] = vertex;


            //Swap UVs0
            Vector2 uvs;
            uvs = uvs0[dst_Index + 0];
            uvs0[dst_Index + 0] = uvs0[src_Index + 0];
            uvs0[src_Index + 0] = uvs;

            uvs = uvs0[dst_Index + 1];
            uvs0[dst_Index + 1] = uvs0[src_Index + 1];
            uvs0[src_Index + 1] = uvs;

            uvs = uvs0[dst_Index + 2];
            uvs0[dst_Index + 2] = uvs0[src_Index + 2];
            uvs0[src_Index + 2] = uvs;

            uvs = uvs0[dst_Index + 3];
            uvs0[dst_Index + 3] = uvs0[src_Index + 3];
            uvs0[src_Index + 3] = uvs;

            // Swap UVs2
            uvs = uvs2[dst_Index + 0];
            uvs2[dst_Index + 0] = uvs2[src_Index + 0];
            uvs2[src_Index + 0] = uvs;

            uvs = uvs2[dst_Index + 1];
            uvs2[dst_Index + 1] = uvs2[src_Index + 1];
            uvs2[src_Index + 1] = uvs;

            uvs = uvs2[dst_Index + 2];
            uvs2[dst_Index + 2] = uvs2[src_Index + 2];
            uvs2[src_Index + 2] = uvs;

            uvs = uvs2[dst_Index + 3];
            uvs2[dst_Index + 3] = uvs2[src_Index + 3];
            uvs2[src_Index + 3] = uvs;

            // Vertex Colors
            Color32 color;
            color = colors32[dst_Index + 0];
            colors32[dst_Index + 0] = colors32[src_Index + 0];
            colors32[src_Index + 0] = color;

            color = colors32[dst_Index + 1];
            colors32[dst_Index + 1] = colors32[src_Index + 1];
            colors32[src_Index + 1] = color;

            color = colors32[dst_Index + 2];
            colors32[dst_Index + 2] = colors32[src_Index + 2];
            colors32[src_Index + 2] = color;

            color = colors32[dst_Index + 3];
            colors32[dst_Index + 3] = colors32[src_Index + 3];
            colors32[src_Index + 3] = color;
        }


        //int Partition (int start, int end)
        //{
        //    float pivot = vertices[end].z;

        //    int partitionIndex = start;
        //    for (int i = start; i < end; i++)
        //    {
        //        if (vertices[i].z <= pivot)
        //        {
        //            Swap(vertices[i], vertices[partitionIndex]);
        //            partitionIndex += 1;
        //        }
        //    }
        //    Swap(vertices[partitionIndex], vertices[end]);
        //    return partitionIndex;
        //}


        //void Swap(Vector3 a, Vector3 b)
        //{
        //    Vector3 temp = a;
        //    a = b;
        //    b = a;
        //}

    }
}