/
mesh.vertexData.ts
1555 lines (1368 loc) · 81.9 KB
/
mesh.vertexData.ts
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
import { Nullable, FloatArray, IndicesArray, DeepImmutable } from "../types";
import { Matrix, Vector3, Vector2, Vector4, TmpVectors } from "../Maths/math.vector";
import { VertexBuffer } from "../Buffers/buffer";
import { _WarnImport } from '../Misc/devTools';
import { Color4, Color3 } from '../Maths/math.color';
import { Logger } from '../Misc/logger';
import { nativeOverride } from '../Misc/decorators';
declare type Geometry = import("../Meshes/geometry").Geometry;
declare type Mesh = import("../Meshes/mesh").Mesh;
import { ICreateCapsuleOptions } from "./Builders/capsuleBuilder";
declare type PolyhedronData= import("./geodesicMesh").PolyhedronData;
/**
* Define an interface for all classes that will get and set the data on vertices
*/
export interface IGetSetVerticesData {
/**
* Gets a boolean indicating if specific vertex data is present
* @param kind defines the vertex data kind to use
* @returns true is data kind is present
*/
isVerticesDataPresent(kind: string): boolean;
/**
* Gets a specific vertex data attached to this geometry. Float data is constructed if the vertex buffer data cannot be returned directly.
* @param kind defines the data kind (Position, normal, etc...)
* @param copyWhenShared defines if the returned array must be cloned upon returning it if the current geometry is shared between multiple meshes
* @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
* @returns a float array containing vertex data
*/
getVerticesData(kind: string, copyWhenShared?: boolean, forceCopy?: boolean): Nullable<FloatArray>;
/**
* Returns an array of integers or a typed array (Int32Array, Uint32Array, Uint16Array) populated with the mesh indices.
* @param copyWhenShared If true (default false) and and if the mesh geometry is shared among some other meshes, the returned array is a copy of the internal one.
* @param forceCopy defines a boolean indicating that the returned array must be cloned upon returning it
* @returns the indices array or an empty array if the mesh has no geometry
*/
getIndices(copyWhenShared?: boolean, forceCopy?: boolean): Nullable<IndicesArray>;
/**
* Set specific vertex data
* @param kind defines the data kind (Position, normal, etc...)
* @param data defines the vertex data to use
* @param updatable defines if the vertex must be flagged as updatable (false as default)
* @param stride defines the stride to use (0 by default). This value is deduced from the kind value if not specified
*/
setVerticesData(kind: string, data: FloatArray, updatable: boolean): void;
/**
* Update a specific associated vertex buffer
* @param kind defines which buffer to write to (positions, indices, normals, etc). Possible `kind` values :
* - VertexBuffer.PositionKind
* - VertexBuffer.UVKind
* - VertexBuffer.UV2Kind
* - VertexBuffer.UV3Kind
* - VertexBuffer.UV4Kind
* - VertexBuffer.UV5Kind
* - VertexBuffer.UV6Kind
* - VertexBuffer.ColorKind
* - VertexBuffer.MatricesIndicesKind
* - VertexBuffer.MatricesIndicesExtraKind
* - VertexBuffer.MatricesWeightsKind
* - VertexBuffer.MatricesWeightsExtraKind
* @param data defines the data source
* @param updateExtends defines if extends info of the mesh must be updated (can be null). This is mostly useful for "position" kind
* @param makeItUnique defines if the geometry associated with the mesh must be cloned to make the change only for this mesh (and not all meshes associated with the same geometry)
*/
updateVerticesData(kind: string, data: FloatArray, updateExtends?: boolean, makeItUnique?: boolean): void;
/**
* Creates a new index buffer
* @param indices defines the indices to store in the index buffer
* @param totalVertices defines the total number of vertices (could be null)
* @param updatable defines if the index buffer must be flagged as updatable (false by default)
*/
setIndices(indices: IndicesArray, totalVertices: Nullable<number>, updatable?: boolean): void;
}
/**
* This class contains the various kinds of data on every vertex of a mesh used in determining its shape and appearance
*/
export class VertexData {
/**
* Mesh side orientation : usually the external or front surface
*/
public static readonly FRONTSIDE = 0;
/**
* Mesh side orientation : usually the internal or back surface
*/
public static readonly BACKSIDE = 1;
/**
* Mesh side orientation : both internal and external or front and back surfaces
*/
public static readonly DOUBLESIDE = 2;
/**
* Mesh side orientation : by default, `FRONTSIDE`
*/
public static readonly DEFAULTSIDE = 0;
/**
* An array of the x, y, z position of each vertex [...., x, y, z, .....]
*/
public positions: Nullable<FloatArray>;
/**
* An array of the x, y, z normal vector of each vertex [...., x, y, z, .....]
*/
public normals: Nullable<FloatArray>;
/**
* An array of the x, y, z tangent vector of each vertex [...., x, y, z, .....]
*/
public tangents: Nullable<FloatArray>;
/**
* An array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs: Nullable<FloatArray>;
/**
* A second array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs2: Nullable<FloatArray>;
/**
* A third array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs3: Nullable<FloatArray>;
/**
* A fourth array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs4: Nullable<FloatArray>;
/**
* A fifth array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs5: Nullable<FloatArray>;
/**
* A sixth array of u,v which maps a texture image onto each vertex [...., u, v, .....]
*/
public uvs6: Nullable<FloatArray>;
/**
* An array of the r, g, b, a, color of each vertex [...., r, g, b, a, .....]
*/
public colors: Nullable<FloatArray>;
/**
* An array containing the list of indices to the array of matrices produced by bones, each vertex have up to 4 indices (8 if the matricesIndicesExtra is set).
*/
public matricesIndices: Nullable<FloatArray>;
/**
* An array containing the list of weights defining the weight of each indexed matrix in the final computation
*/
public matricesWeights: Nullable<FloatArray>;
/**
* An array extending the number of possible indices
*/
public matricesIndicesExtra: Nullable<FloatArray>;
/**
* An array extending the number of possible weights when the number of indices is extended
*/
public matricesWeightsExtra: Nullable<FloatArray>;
/**
* An array of i, j, k the three vertex indices required for each triangular facet [...., i, j, k .....]
*/
public indices: Nullable<IndicesArray>;
/**
* Uses the passed data array to set the set the values for the specified kind of data
* @param data a linear array of floating numbers
* @param kind the type of data that is being set, eg positions, colors etc
*/
public set(data: FloatArray, kind: string) {
if (!data.length) {
Logger.Warn(`Setting vertex data kind '${kind}' with an empty array`);
}
switch (kind) {
case VertexBuffer.PositionKind:
this.positions = data;
break;
case VertexBuffer.NormalKind:
this.normals = data;
break;
case VertexBuffer.TangentKind:
this.tangents = data;
break;
case VertexBuffer.UVKind:
this.uvs = data;
break;
case VertexBuffer.UV2Kind:
this.uvs2 = data;
break;
case VertexBuffer.UV3Kind:
this.uvs3 = data;
break;
case VertexBuffer.UV4Kind:
this.uvs4 = data;
break;
case VertexBuffer.UV5Kind:
this.uvs5 = data;
break;
case VertexBuffer.UV6Kind:
this.uvs6 = data;
break;
case VertexBuffer.ColorKind:
this.colors = data;
break;
case VertexBuffer.MatricesIndicesKind:
this.matricesIndices = data;
break;
case VertexBuffer.MatricesWeightsKind:
this.matricesWeights = data;
break;
case VertexBuffer.MatricesIndicesExtraKind:
this.matricesIndicesExtra = data;
break;
case VertexBuffer.MatricesWeightsExtraKind:
this.matricesWeightsExtra = data;
break;
}
}
/**
* Associates the vertexData to the passed Mesh.
* Sets it as updatable or not (default `false`)
* @param mesh the mesh the vertexData is applied to
* @param updatable when used and having the value true allows new data to update the vertexData
* @returns the VertexData
*/
public applyToMesh(mesh: Mesh, updatable?: boolean): VertexData {
this._applyTo(mesh, updatable);
return this;
}
/**
* Associates the vertexData to the passed Geometry.
* Sets it as updatable or not (default `false`)
* @param geometry the geometry the vertexData is applied to
* @param updatable when used and having the value true allows new data to update the vertexData
* @returns VertexData
*/
public applyToGeometry(geometry: Geometry, updatable?: boolean): VertexData {
this._applyTo(geometry, updatable);
return this;
}
/**
* Updates the associated mesh
* @param mesh the mesh to be updated
* @param updateExtends when true the mesh BoundingInfo will be renewed when and if position kind is updated, optional with default false
* @param makeItUnique when true, and when and if position kind is updated, a new global geometry will be created from these positions and set to the mesh, optional with default false
* @returns VertexData
*/
public updateMesh(mesh: Mesh): VertexData {
this._update(mesh);
return this;
}
/**
* Updates the associated geometry
* @param geometry the geometry to be updated
* @param updateExtends when true BoundingInfo will be renewed when and if position kind is updated, optional with default false
* @param makeItUnique when true, and when and if position kind is updated, a new global geometry will be created from these positions and set to the mesh, optional with default false
* @returns VertexData.
*/
public updateGeometry(geometry: Geometry): VertexData {
this._update(geometry);
return this;
}
private _applyTo(meshOrGeometry: IGetSetVerticesData, updatable: boolean = false): VertexData {
if (this.positions) {
meshOrGeometry.setVerticesData(VertexBuffer.PositionKind, this.positions, updatable);
}
if (this.normals) {
meshOrGeometry.setVerticesData(VertexBuffer.NormalKind, this.normals, updatable);
}
if (this.tangents) {
meshOrGeometry.setVerticesData(VertexBuffer.TangentKind, this.tangents, updatable);
}
if (this.uvs) {
meshOrGeometry.setVerticesData(VertexBuffer.UVKind, this.uvs, updatable);
}
if (this.uvs2) {
meshOrGeometry.setVerticesData(VertexBuffer.UV2Kind, this.uvs2, updatable);
}
if (this.uvs3) {
meshOrGeometry.setVerticesData(VertexBuffer.UV3Kind, this.uvs3, updatable);
}
if (this.uvs4) {
meshOrGeometry.setVerticesData(VertexBuffer.UV4Kind, this.uvs4, updatable);
}
if (this.uvs5) {
meshOrGeometry.setVerticesData(VertexBuffer.UV5Kind, this.uvs5, updatable);
}
if (this.uvs6) {
meshOrGeometry.setVerticesData(VertexBuffer.UV6Kind, this.uvs6, updatable);
}
if (this.colors) {
meshOrGeometry.setVerticesData(VertexBuffer.ColorKind, this.colors, updatable);
}
if (this.matricesIndices) {
meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updatable);
}
if (this.matricesWeights) {
meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updatable);
}
if (this.matricesIndicesExtra) {
meshOrGeometry.setVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updatable);
}
if (this.matricesWeightsExtra) {
meshOrGeometry.setVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updatable);
}
if (this.indices) {
meshOrGeometry.setIndices(this.indices, null, updatable);
} else {
meshOrGeometry.setIndices([], null);
}
return this;
}
private _update(meshOrGeometry: IGetSetVerticesData, updateExtends?: boolean, makeItUnique?: boolean): VertexData {
if (this.positions) {
meshOrGeometry.updateVerticesData(VertexBuffer.PositionKind, this.positions, updateExtends, makeItUnique);
}
if (this.normals) {
meshOrGeometry.updateVerticesData(VertexBuffer.NormalKind, this.normals, updateExtends, makeItUnique);
}
if (this.tangents) {
meshOrGeometry.updateVerticesData(VertexBuffer.TangentKind, this.tangents, updateExtends, makeItUnique);
}
if (this.uvs) {
meshOrGeometry.updateVerticesData(VertexBuffer.UVKind, this.uvs, updateExtends, makeItUnique);
}
if (this.uvs2) {
meshOrGeometry.updateVerticesData(VertexBuffer.UV2Kind, this.uvs2, updateExtends, makeItUnique);
}
if (this.uvs3) {
meshOrGeometry.updateVerticesData(VertexBuffer.UV3Kind, this.uvs3, updateExtends, makeItUnique);
}
if (this.uvs4) {
meshOrGeometry.updateVerticesData(VertexBuffer.UV4Kind, this.uvs4, updateExtends, makeItUnique);
}
if (this.uvs5) {
meshOrGeometry.updateVerticesData(VertexBuffer.UV5Kind, this.uvs5, updateExtends, makeItUnique);
}
if (this.uvs6) {
meshOrGeometry.updateVerticesData(VertexBuffer.UV6Kind, this.uvs6, updateExtends, makeItUnique);
}
if (this.colors) {
meshOrGeometry.updateVerticesData(VertexBuffer.ColorKind, this.colors, updateExtends, makeItUnique);
}
if (this.matricesIndices) {
meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesKind, this.matricesIndices, updateExtends, makeItUnique);
}
if (this.matricesWeights) {
meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsKind, this.matricesWeights, updateExtends, makeItUnique);
}
if (this.matricesIndicesExtra) {
meshOrGeometry.updateVerticesData(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra, updateExtends, makeItUnique);
}
if (this.matricesWeightsExtra) {
meshOrGeometry.updateVerticesData(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra, updateExtends, makeItUnique);
}
if (this.indices) {
meshOrGeometry.setIndices(this.indices, null);
}
return this;
}
@nativeOverride.filter((...[coordinates]: Parameters<typeof VertexData._TransformVector3Coordinates>) => !Array.isArray(coordinates))
private static _TransformVector3Coordinates(coordinates: FloatArray, transformation: DeepImmutable<Matrix>) {
const coordinate = TmpVectors.Vector3[0];
const transformedCoordinate = TmpVectors.Vector3[1];
for (let index = 0; index < coordinates.length; index += 3) {
Vector3.FromArrayToRef(coordinates, index, coordinate);
Vector3.TransformCoordinatesToRef(coordinate, transformation, transformedCoordinate);
coordinates[index] = transformedCoordinate.x;
coordinates[index + 1] = transformedCoordinate.y;
coordinates[index + 2] = transformedCoordinate.z;
}
}
@nativeOverride.filter((...[normals]: Parameters<typeof VertexData._TransformVector3Normals>) => !Array.isArray(normals))
private static _TransformVector3Normals(normals: FloatArray, transformation: DeepImmutable<Matrix>) {
const normal = TmpVectors.Vector3[0];
const transformedNormal = TmpVectors.Vector3[1];
for (let index = 0; index < normals.length; index += 3) {
Vector3.FromArrayToRef(normals, index, normal);
Vector3.TransformNormalToRef(normal, transformation, transformedNormal);
normals[index] = transformedNormal.x;
normals[index + 1] = transformedNormal.y;
normals[index + 2] = transformedNormal.z;
}
}
@nativeOverride.filter((...[normals]: Parameters<typeof VertexData._TransformVector4Normals>) => !Array.isArray(normals))
private static _TransformVector4Normals(normals: FloatArray, transformation: DeepImmutable<Matrix>) {
const normal = TmpVectors.Vector4[0];
const transformedNormal = TmpVectors.Vector4[1];
for (let index = 0; index < normals.length; index += 4) {
Vector4.FromArrayToRef(normals, index, normal);
Vector4.TransformNormalToRef(normal, transformation, transformedNormal);
normals[index] = transformedNormal.x;
normals[index + 1] = transformedNormal.y;
normals[index + 2] = transformedNormal.z;
normals[index + 3] = transformedNormal.w;
}
}
@nativeOverride.filter((...[indices]: Parameters<typeof VertexData._FlipFaces>) => !Array.isArray(indices))
private static _FlipFaces(indices: IndicesArray) {
for (let index = 0; index < indices.length; index += 3) {
const tmp = indices[index + 1];
indices[index + 1] = indices[index + 2];
indices[index + 2] = tmp;
}
}
/**
* Transforms each position and each normal of the vertexData according to the passed Matrix
* @param matrix the transforming matrix
* @returns the VertexData
*/
public transform(matrix: Matrix): VertexData {
const flip = matrix.determinant() < 0;
if (this.positions) {
VertexData._TransformVector3Coordinates(this.positions, matrix);
}
if (this.normals) {
VertexData._TransformVector3Normals(this.normals, matrix);
}
if (this.tangents) {
VertexData._TransformVector4Normals(this.tangents, matrix);
}
if (flip && this.indices) {
VertexData._FlipFaces(this.indices);
}
return this;
}
/**
* Merges the passed VertexData into the current one
* @param others the VertexData to be merged into the current one
* @param use32BitsIndices defines a boolean indicating if indices must be store in a 32 bits array
* @returns the modified VertexData
*/
public merge(others: VertexData | VertexData[], use32BitsIndices = false): VertexData {
this._validate();
others = Array.isArray(others) ? others : [others];
for (const other of others) {
other._validate();
if (!this.normals !== !other.normals ||
!this.tangents !== !other.tangents ||
!this.uvs !== !other.uvs ||
!this.uvs2 !== !other.uvs2 ||
!this.uvs3 !== !other.uvs3 ||
!this.uvs4 !== !other.uvs4 ||
!this.uvs5 !== !other.uvs5 ||
!this.uvs6 !== !other.uvs6 ||
!this.colors !== !other.colors ||
!this.matricesIndices !== !other.matricesIndices ||
!this.matricesWeights !== !other.matricesWeights ||
!this.matricesIndicesExtra !== !other.matricesIndicesExtra ||
!this.matricesWeightsExtra !== !other.matricesWeightsExtra) {
throw new Error("Cannot merge vertex data that do not have the same set of attributes");
}
}
const totalIndices = others.reduce((indexSum, vertexData) => indexSum + (vertexData.indices?.length ?? 0), this.indices?.length ?? 0);
if (totalIndices > 0) {
let indicesOffset = this.indices?.length ?? 0;
if (!this.indices) {
this.indices = new Array<number>(totalIndices);
}
if (this.indices.length !== totalIndices) {
if (Array.isArray(this.indices)) {
this.indices.length = totalIndices;
} else {
const temp = use32BitsIndices || this.indices instanceof Uint32Array ? new Uint32Array(totalIndices) : new Uint16Array(totalIndices);
temp.set(this.indices);
this.indices = temp;
}
}
let positionsOffset = this.positions ? this.positions.length / 3 : 0;
for (const other of others) {
if (other.indices) {
for (let index = 0; index < other.indices.length; index++) {
this.indices[indicesOffset + index] = other.indices[index] + positionsOffset;
}
// The call to _validate already checked for positions
positionsOffset += other.positions!.length / 3;
indicesOffset += other.indices.length;
}
}
}
this.positions = VertexData._mergeElement(this.positions, others.map((other) => other.positions));
this.normals = VertexData._mergeElement(this.normals, others.map((other) => other.normals));
this.tangents = VertexData._mergeElement(this.tangents, others.map((other) => other.tangents));
this.uvs = VertexData._mergeElement(this.uvs, others.map((other) => other.uvs));
this.uvs2 = VertexData._mergeElement(this.uvs2, others.map((other) => other.uvs2));
this.uvs3 = VertexData._mergeElement(this.uvs3, others.map((other) => other.uvs3));
this.uvs4 = VertexData._mergeElement(this.uvs4, others.map((other) => other.uvs4));
this.uvs5 = VertexData._mergeElement(this.uvs5, others.map((other) => other.uvs5));
this.uvs6 = VertexData._mergeElement(this.uvs6, others.map((other) => other.uvs6));
this.colors = VertexData._mergeElement(this.colors, others.map((other) => other.colors));
this.matricesIndices = VertexData._mergeElement(this.matricesIndices, others.map((other) => other.matricesIndices));
this.matricesWeights = VertexData._mergeElement(this.matricesWeights, others.map((other) => other.matricesWeights));
this.matricesIndicesExtra = VertexData._mergeElement(this.matricesIndicesExtra, others.map((other) => other.matricesIndicesExtra));
this.matricesWeightsExtra = VertexData._mergeElement(this.matricesWeightsExtra, others.map((other) => other.matricesWeightsExtra));
return this;
}
private static _mergeElement(source: Nullable<FloatArray>, others: readonly Nullable<FloatArray>[]): Nullable<FloatArray> {
const nonNullOthers = others.filter((other): other is FloatArray => other !== null && other !== undefined);
if (nonNullOthers.length === 0) {
return source;
}
if (!source) {
return this._mergeElement(nonNullOthers[0], nonNullOthers.slice(1));
}
const len = nonNullOthers.reduce((sumLen, elements) => sumLen + elements.length, source.length);
if (source instanceof Float32Array) {
// use non-loop method when the source is Float32Array
const ret32 = new Float32Array(len);
ret32.set(source);
let offset = source.length;
for (const other of nonNullOthers) {
ret32.set(other, offset);
offset += other.length;
}
return ret32;
} else {
// don't use concat as it is super slow, just loop for other cases
const ret = new Array<number>(len);
for (let i = 0; i < source.length; i++) {
ret[i] = source[i];
}
let offset = source.length;
for (const other of nonNullOthers) {
for (let i = 0; i < other.length; i++) {
ret[offset + i] = other[i];
}
offset += other.length;
}
return ret;
}
}
private _validate(): void {
if (!this.positions) {
throw new Error("Positions are required");
}
const getElementCount = (kind: string, values: FloatArray) => {
const stride = VertexBuffer.DeduceStride(kind);
if ((values.length % stride) !== 0) {
throw new Error("The " + kind + "s array count must be a multiple of " + stride);
}
return values.length / stride;
};
const positionsElementCount = getElementCount(VertexBuffer.PositionKind, this.positions);
const validateElementCount = (kind: string, values: FloatArray) => {
const elementCount = getElementCount(kind, values);
if (elementCount !== positionsElementCount) {
throw new Error("The " + kind + "s element count (" + elementCount + ") does not match the positions count (" + positionsElementCount + ")");
}
};
if (this.normals) { validateElementCount(VertexBuffer.NormalKind, this.normals); }
if (this.tangents) { validateElementCount(VertexBuffer.TangentKind, this.tangents); }
if (this.uvs) { validateElementCount(VertexBuffer.UVKind, this.uvs); }
if (this.uvs2) { validateElementCount(VertexBuffer.UV2Kind, this.uvs2); }
if (this.uvs3) { validateElementCount(VertexBuffer.UV3Kind, this.uvs3); }
if (this.uvs4) { validateElementCount(VertexBuffer.UV4Kind, this.uvs4); }
if (this.uvs5) { validateElementCount(VertexBuffer.UV5Kind, this.uvs5); }
if (this.uvs6) { validateElementCount(VertexBuffer.UV6Kind, this.uvs6); }
if (this.colors) { validateElementCount(VertexBuffer.ColorKind, this.colors); }
if (this.matricesIndices) { validateElementCount(VertexBuffer.MatricesIndicesKind, this.matricesIndices); }
if (this.matricesWeights) { validateElementCount(VertexBuffer.MatricesWeightsKind, this.matricesWeights); }
if (this.matricesIndicesExtra) { validateElementCount(VertexBuffer.MatricesIndicesExtraKind, this.matricesIndicesExtra); }
if (this.matricesWeightsExtra) { validateElementCount(VertexBuffer.MatricesWeightsExtraKind, this.matricesWeightsExtra); }
}
/**
* Serializes the VertexData
* @returns a serialized object
*/
public serialize(): any {
var serializationObject = this.serialize();
if (this.positions) {
serializationObject.positions = this.positions;
}
if (this.normals) {
serializationObject.normals = this.normals;
}
if (this.tangents) {
serializationObject.tangents = this.tangents;
}
if (this.uvs) {
serializationObject.uvs = this.uvs;
}
if (this.uvs2) {
serializationObject.uvs2 = this.uvs2;
}
if (this.uvs3) {
serializationObject.uvs3 = this.uvs3;
}
if (this.uvs4) {
serializationObject.uvs4 = this.uvs4;
}
if (this.uvs5) {
serializationObject.uvs5 = this.uvs5;
}
if (this.uvs6) {
serializationObject.uvs6 = this.uvs6;
}
if (this.colors) {
serializationObject.colors = this.colors;
}
if (this.matricesIndices) {
serializationObject.matricesIndices = this.matricesIndices;
serializationObject.matricesIndices._isExpanded = true;
}
if (this.matricesWeights) {
serializationObject.matricesWeights = this.matricesWeights;
}
if (this.matricesIndicesExtra) {
serializationObject.matricesIndicesExtra = this.matricesIndicesExtra;
serializationObject.matricesIndicesExtra._isExpanded = true;
}
if (this.matricesWeightsExtra) {
serializationObject.matricesWeightsExtra = this.matricesWeightsExtra;
}
serializationObject.indices = this.indices;
return serializationObject;
}
// Statics
/**
* Extracts the vertexData from a mesh
* @param mesh the mesh from which to extract the VertexData
* @param copyWhenShared defines if the VertexData must be cloned when shared between multiple meshes, optional, default false
* @param forceCopy indicating that the VertexData must be cloned, optional, default false
* @returns the object VertexData associated to the passed mesh
*/
public static ExtractFromMesh(mesh: Mesh, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
return VertexData._ExtractFrom(mesh, copyWhenShared, forceCopy);
}
/**
* Extracts the vertexData from the geometry
* @param geometry the geometry from which to extract the VertexData
* @param copyWhenShared defines if the VertexData must be cloned when the geometry is shared between multiple meshes, optional, default false
* @param forceCopy indicating that the VertexData must be cloned, optional, default false
* @returns the object VertexData associated to the passed mesh
*/
public static ExtractFromGeometry(geometry: Geometry, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
return VertexData._ExtractFrom(geometry, copyWhenShared, forceCopy);
}
private static _ExtractFrom(meshOrGeometry: IGetSetVerticesData, copyWhenShared?: boolean, forceCopy?: boolean): VertexData {
var result = new VertexData();
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.PositionKind)) {
result.positions = meshOrGeometry.getVerticesData(VertexBuffer.PositionKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.NormalKind)) {
result.normals = meshOrGeometry.getVerticesData(VertexBuffer.NormalKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.TangentKind)) {
result.tangents = meshOrGeometry.getVerticesData(VertexBuffer.TangentKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UVKind)) {
result.uvs = meshOrGeometry.getVerticesData(VertexBuffer.UVKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
result.uvs2 = meshOrGeometry.getVerticesData(VertexBuffer.UV2Kind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV3Kind)) {
result.uvs3 = meshOrGeometry.getVerticesData(VertexBuffer.UV3Kind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV4Kind)) {
result.uvs4 = meshOrGeometry.getVerticesData(VertexBuffer.UV4Kind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV5Kind)) {
result.uvs5 = meshOrGeometry.getVerticesData(VertexBuffer.UV5Kind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.UV6Kind)) {
result.uvs6 = meshOrGeometry.getVerticesData(VertexBuffer.UV6Kind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.ColorKind)) {
result.colors = meshOrGeometry.getVerticesData(VertexBuffer.ColorKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesKind)) {
result.matricesIndices = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsKind)) {
result.matricesWeights = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesIndicesExtraKind)) {
result.matricesIndicesExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesIndicesExtraKind, copyWhenShared, forceCopy);
}
if (meshOrGeometry.isVerticesDataPresent(VertexBuffer.MatricesWeightsExtraKind)) {
result.matricesWeightsExtra = meshOrGeometry.getVerticesData(VertexBuffer.MatricesWeightsExtraKind, copyWhenShared, forceCopy);
}
result.indices = meshOrGeometry.getIndices(copyWhenShared, forceCopy);
return result;
}
/**
* Creates the VertexData for a Ribbon
* @param options an object used to set the following optional parameters for the ribbon, required but can be empty
* * pathArray array of paths, each of which an array of successive Vector3
* * closeArray creates a seam between the first and the last paths of the pathArray, optional, default false
* * closePath creates a seam between the first and the last points of each path of the path array, optional, default false
* * offset a positive integer, only used when pathArray contains a single path (offset = 10 means the point 1 is joined to the point 11), default rounded half size of the pathArray length
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* * invertUV swaps in the U and V coordinates when applying a texture, optional, default false
* * uvs a linear array, of length 2 * number of vertices, of custom UV values, optional
* * colors a linear array, of length 4 * number of vertices, of custom color values, optional
* @returns the VertexData of the ribbon
*/
public static CreateRibbon(options: { pathArray: Vector3[][], closeArray?: boolean, closePath?: boolean, offset?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4, invertUV?: boolean, uvs?: Vector2[], colors?: Color4[] }): VertexData {
throw _WarnImport("ribbonBuilder");
}
/**
* Creates the VertexData for a box
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * size sets the width, height and depth of the box to the value of size, optional default 1
* * width sets the width (x direction) of the box, overwrites the width set by size, optional, default size
* * height sets the height (y direction) of the box, overwrites the height set by size, optional, default size
* * depth sets the depth (z direction) of the box, overwrites the depth set by size, optional, default size
* * faceUV an array of 6 Vector4 elements used to set different images to each box side
* * faceColors an array of 6 Color3 elements used to set different colors to each box side
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the box
*/
public static CreateBox(options: { size?: number, width?: number, height?: number, depth?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
throw _WarnImport("boxBuilder");
}
/**
* Creates the VertexData for a tiled box
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * faceTiles sets the pattern, tile size and number of tiles for a face
* * faceUV an array of 6 Vector4 elements used to set different images to each box side
* * faceColors an array of 6 Color3 elements used to set different colors to each box side
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* @returns the VertexData of the box
*/
public static CreateTiledBox(options: { pattern?: number, width?: number, height?: number, depth?: number, tileSize?: number, tileWidth?: number, tileHeight?: number, alignHorizontal?: number, alignVertical?: number, faceUV?: Vector4[], faceColors?: Color4[], sideOrientation?: number }): VertexData {
throw _WarnImport("tiledBoxBuilder");
}
/**
* Creates the VertexData for a tiled plane
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * pattern a limited pattern arrangement depending on the number
* * tileSize sets the width, height and depth of the tile to the value of size, optional default 1
* * tileWidth sets the width (x direction) of the tile, overwrites the width set by size, optional, default size
* * tileHeight sets the height (y direction) of the tile, overwrites the height set by size, optional, default size
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the tiled plane
*/
public static CreateTiledPlane(options: { pattern?: number, tileSize?: number, tileWidth?: number, tileHeight?: number, size?: number, width?: number, height?: number, alignHorizontal?: number, alignVertical?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
throw _WarnImport("tiledPlaneBuilder");
}
/**
* Creates the VertexData for an ellipsoid, defaults to a sphere
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * segments sets the number of horizontal strips optional, default 32
* * diameter sets the axes dimensions, diameterX, diameterY and diameterZ to the value of diameter, optional default 1
* * diameterX sets the diameterX (x direction) of the ellipsoid, overwrites the diameterX set by diameter, optional, default diameter
* * diameterY sets the diameterY (y direction) of the ellipsoid, overwrites the diameterY set by diameter, optional, default diameter
* * diameterZ sets the diameterZ (z direction) of the ellipsoid, overwrites the diameterZ set by diameter, optional, default diameter
* * arc a number from 0 to 1, to create an unclosed ellipsoid based on the fraction of the circumference (latitude) given by the arc value, optional, default 1
* * slice a number from 0 to 1, to create an unclosed ellipsoid based on the fraction of the height (latitude) given by the arc value, optional, default 1
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the ellipsoid
*/
public static CreateSphere(options: { segments?: number, diameter?: number, diameterX?: number, diameterY?: number, diameterZ?: number, arc?: number, slice?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
throw _WarnImport("sphereBuilder");
}
/**
* Creates the VertexData for a cylinder, cone or prism
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * height sets the height (y direction) of the cylinder, optional, default 2
* * diameterTop sets the diameter of the top of the cone, overwrites diameter, optional, default diameter
* * diameterBottom sets the diameter of the bottom of the cone, overwrites diameter, optional, default diameter
* * diameter sets the diameter of the top and bottom of the cone, optional default 1
* * tessellation the number of prism sides, 3 for a triangular prism, optional, default 24
* * subdivisions` the number of rings along the cylinder height, optional, default 1
* * arc a number from 0 to 1, to create an unclosed cylinder based on the fraction of the circumference given by the arc value, optional, default 1
* * faceColors an array of Color3 elements used to set different colors to the top, rings and bottom respectively
* * faceUV an array of Vector4 elements used to set different images to the top, rings and bottom respectively
* * hasRings when true makes each subdivision independantly treated as a face for faceUV and faceColors, optional, default false
* * enclose when true closes an open cylinder by adding extra flat faces between the height axis and vertical edges, think cut cake
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the cylinder, cone or prism
*/
public static CreateCylinder(options: { height?: number, diameterTop?: number, diameterBottom?: number, diameter?: number, tessellation?: number, subdivisions?: number, arc?: number, faceColors?: Color4[], faceUV?: Vector4[], hasRings?: boolean, enclose?: boolean, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
throw _WarnImport("cylinderBuilder");
}
/**
* Creates the VertexData for a torus
* @param options an object used to set the following optional parameters for the box, required but can be empty
* * diameter the diameter of the torus, optional default 1
* * thickness the diameter of the tube forming the torus, optional default 0.5
* * tessellation the number of prism sides, 3 for a triangular prism, optional, default 24
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the torus
*/
public static CreateTorus(options: { diameter?: number, thickness?: number, tessellation?: number, sideOrientation?: number, frontUVs?: Vector4, backUVs?: Vector4 }): VertexData {
throw _WarnImport("torusBuilder");
}
/**
* Creates the VertexData of the LineSystem
* @param options an object used to set the following optional parameters for the LineSystem, required but can be empty
* - lines an array of lines, each line being an array of successive Vector3
* - colors an array of line colors, each of the line colors being an array of successive Color4, one per line point
* @returns the VertexData of the LineSystem
*/
public static CreateLineSystem(options: { lines: Vector3[][], colors?: Nullable<Color4[][]> }): VertexData {
throw _WarnImport("linesBuilder");
}
/**
* Create the VertexData for a DashedLines
* @param options an object used to set the following optional parameters for the DashedLines, required but can be empty
* - points an array successive Vector3
* - dashSize the size of the dashes relative to the dash number, optional, default 3
* - gapSize the size of the gap between two successive dashes relative to the dash number, optional, default 1
* - dashNb the intended total number of dashes, optional, default 200
* @returns the VertexData for the DashedLines
*/
public static CreateDashedLines(options: { points: Vector3[], dashSize?: number, gapSize?: number, dashNb?: number }): VertexData {
throw _WarnImport("linesBuilder");
}
/**
* Creates the VertexData for a Ground
* @param options an object used to set the following optional parameters for the Ground, required but can be empty
* - width the width (x direction) of the ground, optional, default 1
* - height the height (z direction) of the ground, optional, default 1
* - subdivisions the number of subdivisions per side, optional, default 1
* @returns the VertexData of the Ground
*/
public static CreateGround(options: { width?: number, height?: number, subdivisions?: number, subdivisionsX?: number, subdivisionsY?: number }): VertexData {
throw _WarnImport("groundBuilder");
}
/**
* Creates the VertexData for a TiledGround by subdividing the ground into tiles
* @param options an object used to set the following optional parameters for the Ground, required but can be empty
* * xmin the ground minimum X coordinate, optional, default -1
* * zmin the ground minimum Z coordinate, optional, default -1
* * xmax the ground maximum X coordinate, optional, default 1
* * zmax the ground maximum Z coordinate, optional, default 1
* * subdivisions a javascript object {w: positive integer, h: positive integer}, `w` and `h` are the numbers of subdivisions on the ground width and height creating 'tiles', default {w: 6, h: 6}
* * precision a javascript object {w: positive integer, h: positive integer}, `w` and `h` are the numbers of subdivisions on the tile width and height, default {w: 2, h: 2}
* @returns the VertexData of the TiledGround
*/
public static CreateTiledGround(options: { xmin: number, zmin: number, xmax: number, zmax: number, subdivisions?: { w: number; h: number; }, precision?: { w: number; h: number; } }): VertexData {
throw _WarnImport("groundBuilder");
}
/**
* Creates the VertexData of the Ground designed from a heightmap
* @param options an object used to set the following parameters for the Ground, required and provided by MeshBuilder.CreateGroundFromHeightMap
* * width the width (x direction) of the ground
* * height the height (z direction) of the ground
* * subdivisions the number of subdivisions per side
* * minHeight the minimum altitude on the ground, optional, default 0
* * maxHeight the maximum altitude on the ground, optional default 1
* * colorFilter the filter to apply to the image pixel colors to compute the height, optional Color3, default (0.3, 0.59, 0.11)
* * buffer the array holding the image color data
* * bufferWidth the width of image
* * bufferHeight the height of image
* * alphaFilter Remove any data where the alpha channel is below this value, defaults 0 (all data visible)
* @returns the VertexData of the Ground designed from a heightmap
*/
public static CreateGroundFromHeightMap(options: { width: number, height: number, subdivisions: number, minHeight: number, maxHeight: number, colorFilter: Color3, buffer: Uint8Array, bufferWidth: number, bufferHeight: number, alphaFilter: number }): VertexData {
throw _WarnImport("groundBuilder");
}
/**
* Creates the VertexData for a Plane
* @param options an object used to set the following optional parameters for the plane, required but can be empty
* * size sets the width and height of the plane to the value of size, optional default 1
* * width sets the width (x direction) of the plane, overwrites the width set by size, optional, default size
* * height sets the height (y direction) of the plane, overwrites the height set by size, optional, default size
* * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
* * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
* * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
* @returns the VertexData of the box