-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
babylon.mesh.ts
3089 lines (2665 loc) · 143 KB
/
babylon.mesh.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
module BABYLON {
export class _InstancesBatch {
public mustReturn = false;
public visibleInstances = new Array<Array<InstancedMesh>>();
public renderSelf = new Array<boolean>();
}
export class Mesh extends AbstractMesh implements IGetSetVerticesData {
// Consts
public static _FRONTSIDE: number = 0;
public static _BACKSIDE: number = 1;
public static _DOUBLESIDE: number = 2;
public static _DEFAULTSIDE: number = 0;
public static _NO_CAP = 0;
public static _CAP_START = 1;
public static _CAP_END = 2;
public static _CAP_ALL = 3;
/**
* Mesh side orientation : usually the external or front surface
*/
public static get FRONTSIDE(): number {
return Mesh._FRONTSIDE;
}
/**
* Mesh side orientation : usually the internal or back surface
*/
public static get BACKSIDE(): number {
return Mesh._BACKSIDE;
}
/**
* Mesh side orientation : both internal and external or front and back surfaces
*/
public static get DOUBLESIDE(): number {
return Mesh._DOUBLESIDE;
}
/**
* Mesh side orientation : by default, `FRONTSIDE`
*/
public static get DEFAULTSIDE(): number {
return Mesh._DEFAULTSIDE;
}
/**
* Mesh cap setting : no cap
*/
public static get NO_CAP(): number {
return Mesh._NO_CAP;
}
/**
* Mesh cap setting : one cap at the beginning of the mesh
*/
public static get CAP_START(): number {
return Mesh._CAP_START;
}
/**
* Mesh cap setting : one cap at the end of the mesh
*/
public static get CAP_END(): number {
return Mesh._CAP_END;
}
/**
* Mesh cap setting : two caps, one at the beginning and one at the end of the mesh
*/
public static get CAP_ALL(): number {
return Mesh._CAP_ALL;
}
// Events
/**
* An event triggered before rendering the mesh
* @type {BABYLON.Observable}
*/
public onBeforeRenderObservable = new Observable<Mesh>();
/**
* An event triggered after rendering the mesh
* @type {BABYLON.Observable}
*/
public onAfterRenderObservable = new Observable<Mesh>();
/**
* An event triggered before drawing the mesh
* @type {BABYLON.Observable}
*/
public onBeforeDrawObservable = new Observable<Mesh>();
private _onBeforeDrawObserver: Observer<Mesh>;
public set onBeforeDraw(callback: () => void) {
if (this._onBeforeDrawObserver) {
this.onBeforeDrawObservable.remove(this._onBeforeDrawObserver);
}
this._onBeforeDrawObserver = this.onBeforeDrawObservable.add(callback);
}
// Members
public delayLoadState = Engine.DELAYLOADSTATE_NONE;
public instances = new Array<InstancedMesh>();
public delayLoadingFile: string;
public _binaryInfo: any;
private _LODLevels = new Array<Internals.MeshLODLevel>();
public onLODLevelSelection: (distance: number, mesh: Mesh, selectedLevel: Mesh) => void;
// Morph
private _morphTargetManager: MorphTargetManager;
public get morphTargetManager(): MorphTargetManager {
return this._morphTargetManager;
}
public set morphTargetManager(value: MorphTargetManager) {
if (this._morphTargetManager === value) {
return;
}
this._morphTargetManager = value;
this._syncGeometryWithMorphTargetManager();
}
// Private
public _geometry: Geometry;
public _delayInfo; //ANY
public _delayLoadingFunction: (any: any, mesh: Mesh) => void;
public _visibleInstances: any = {};
private _renderIdForInstances = new Array<number>();
private _batchCache = new _InstancesBatch();
private _instancesBufferSize = 32 * 16 * 4; // let's start with a maximum of 32 instances
private _instancesBuffer: Buffer;
private _instancesData: Float32Array;
private _overridenInstanceCount: number;
public _shouldGenerateFlatShading: boolean;
private _preActivateId: number;
private _sideOrientation: number = Mesh._DEFAULTSIDE;
private _areNormalsFrozen: boolean = false; // Will be used by ribbons mainly
private _sourcePositions: Float32Array; // Will be used to save original positions when using software skinning
private _sourceNormals: Float32Array; // Will be used to save original normals when using software skinning
// Will be used to save a source mesh reference, If any
private _source: BABYLON.Mesh = null;
public get source(): BABYLON.Mesh {
return this._source;
}
/**
* @constructor
* @param {string} name The value used by scene.getMeshByName() to do a lookup.
* @param {Scene} scene The scene to add this mesh to.
* @param {Node} parent The parent of this mesh, if it has one
* @param {Mesh} source An optional Mesh from which geometry is shared, cloned.
* @param {boolean} doNotCloneChildren When cloning, skip cloning child meshes of source, default False.
* When false, achieved by calling a clone(), also passing False.
* This will make creation of children, recursive.
* @param {boolean} clonePhysicsImpostor When cloning, include cloning mesh physics impostor, default True.
*/
constructor(name: string, scene: Scene, parent: Node = null, source?: Mesh, doNotCloneChildren?: boolean, clonePhysicsImpostor: boolean = true) {
super(name, scene);
if (source) {
// Source mesh
this._source = source;
// Geometry
if (source._geometry) {
source._geometry.applyToMesh(this);
}
// Deep copy
Tools.DeepCopy(source, this, ["name", "material", "skeleton", "instances", "parent", "uniqueId"], ["_poseMatrix"]);
// Parent
this.parent = source.parent;
// Pivot
this.setPivotMatrix(source.getPivotMatrix());
this.id = name + "." + source.id;
// Material
this.material = source.material;
var index: number;
if (!doNotCloneChildren) {
// Children
for (index = 0; index < scene.meshes.length; index++) {
var mesh = scene.meshes[index];
if (mesh.parent === source) {
// doNotCloneChildren is always going to be False
var newChild = mesh.clone(name + "." + mesh.name, this, doNotCloneChildren);
}
}
}
// Physics clone
var physicsEngine = this.getScene().getPhysicsEngine();
if (clonePhysicsImpostor && physicsEngine) {
var impostor = physicsEngine.getImpostorForPhysicsObject(source);
if (impostor) {
this.physicsImpostor = impostor.clone(this);
}
}
// Particles
for (index = 0; index < scene.particleSystems.length; index++) {
var system = scene.particleSystems[index];
if (system.emitter === source) {
system.clone(system.name, this);
}
}
this.computeWorldMatrix(true);
}
// Parent
if (parent !== null) {
this.parent = parent;
}
}
// Methods
/**
* Returns the string "Mesh".
*/
public getClassName(): string {
return "Mesh";
}
/**
* Returns a string.
* @param {boolean} fullDetails - support for multiple levels of logging within scene loading
*/
public toString(fullDetails?: boolean): string {
var ret = super.toString(fullDetails);
ret += ", n vertices: " + this.getTotalVertices();
ret += ", parent: " + (this._waitingParentId ? this._waitingParentId : (this.parent ? this.parent.name : "NONE"));
if (this.animations) {
for (var i = 0; i < this.animations.length; i++) {
ret += ", animation[0]: " + this.animations[i].toString(fullDetails);
}
}
if (fullDetails) {
ret += ", flat shading: " + (this._geometry ? (this.getVerticesData(VertexBuffer.PositionKind).length / 3 === this.getIndices().length ? "YES" : "NO") : "UNKNOWN");
}
return ret;
}
/**
* True if the mesh has some Levels Of Details (LOD).
* Returns a boolean.
*/
public get hasLODLevels(): boolean {
return this._LODLevels.length > 0;
}
private _sortLODLevels(): void {
this._LODLevels.sort((a, b) => {
if (a.distance < b.distance) {
return 1;
}
if (a.distance > b.distance) {
return -1;
}
return 0;
});
}
/**
* Add a mesh as LOD level triggered at the given distance.
* tuto : http://doc.babylonjs.com/tutorials/How_to_use_LOD
* @param {number} distance The distance from the center of the object to show this level
* @param {Mesh} mesh The mesh to be added as LOD level
* @return {Mesh} This mesh (for chaining)
*/
public addLODLevel(distance: number, mesh: Mesh): Mesh {
if (mesh && mesh._masterMesh) {
Tools.Warn("You cannot use a mesh as LOD level twice");
return this;
}
var level = new Internals.MeshLODLevel(distance, mesh);
this._LODLevels.push(level);
if (mesh) {
mesh._masterMesh = this;
}
this._sortLODLevels();
return this;
}
/**
* Returns the LOD level mesh at the passed distance or null if not found.
* It is related to the method `addLODLevel(distance, mesh)`.
* tuto : http://doc.babylonjs.com/tutorials/How_to_use_LOD
* Returns an object Mesh or `null`.
*/
public getLODLevelAtDistance(distance: number): Mesh {
for (var index = 0; index < this._LODLevels.length; index++) {
var level = this._LODLevels[index];
if (level.distance === distance) {
return level.mesh;
}
}
return null;
}
/**
* Remove a mesh from the LOD array
* tuto : http://doc.babylonjs.com/tutorials/How_to_use_LOD
* @param {Mesh} mesh The mesh to be removed.
* @return {Mesh} This mesh (for chaining)
*/
public removeLODLevel(mesh: Mesh): Mesh {
for (var index = 0; index < this._LODLevels.length; index++) {
if (this._LODLevels[index].mesh === mesh) {
this._LODLevels.splice(index, 1);
if (mesh) {
mesh._masterMesh = null;
}
}
}
this._sortLODLevels();
return this;
}
/**
* Returns the registered LOD mesh distant from the parameter `camera` position if any, else returns the current mesh.
* tuto : http://doc.babylonjs.com/tutorials/How_to_use_LOD
*/
public getLOD(camera: Camera, boundingSphere?: BoundingSphere): AbstractMesh {
if (!this._LODLevels || this._LODLevels.length === 0) {
return this;
}
var distanceToCamera = (boundingSphere ? boundingSphere : this.getBoundingInfo().boundingSphere).centerWorld.subtract(camera.globalPosition).length();
if (this._LODLevels[this._LODLevels.length - 1].distance > distanceToCamera) {
if (this.onLODLevelSelection) {
this.onLODLevelSelection(distanceToCamera, this, this._LODLevels[this._LODLevels.length - 1].mesh);
}
return this;
}
for (var index = 0; index < this._LODLevels.length; index++) {
var level = this._LODLevels[index];
if (level.distance < distanceToCamera) {
if (level.mesh) {
level.mesh._preActivate();
level.mesh._updateSubMeshesBoundingInfo(this.worldMatrixFromCache);
}
if (this.onLODLevelSelection) {
this.onLODLevelSelection(distanceToCamera, this, level.mesh);
}
return level.mesh;
}
}
if (this.onLODLevelSelection) {
this.onLODLevelSelection(distanceToCamera, this, this);
}
return this;
}
/**
* Returns the mesh internal Geometry object.
*/
public get geometry(): Geometry {
return this._geometry;
}
/**
* Returns a positive integer : the total number of vertices within the mesh geometry or zero if the mesh has no geometry.
*/
public getTotalVertices(): number {
if (!this._geometry) {
return 0;
}
return this._geometry.getTotalVertices();
}
/**
* Returns an array of integers or floats, or a Float32Array, depending on the requested `kind` (positions, indices, normals, etc).
* If `copywhenShared` is true (default false) and if the mesh geometry is shared among some other meshes, the returned array is a copy of the internal one.
* You can force the copy with forceCopy === true
* Returns null if the mesh has no geometry or no vertex buffer.
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*/
public getVerticesData(kind: string, copyWhenShared?: boolean, forceCopy?: boolean): number[] | Float32Array {
if (!this._geometry) {
return null;
}
return this._geometry.getVerticesData(kind, copyWhenShared, forceCopy);
}
/**
* Returns the mesh VertexBuffer object from the requested `kind` : positions, indices, normals, etc.
* Returns `undefined` if the mesh has no geometry.
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*/
public getVertexBuffer(kind): VertexBuffer {
if (!this._geometry) {
return undefined;
}
return this._geometry.getVertexBuffer(kind);
}
/**
* Returns a boolean depending on the existence of the Vertex Data for the requested `kind`.
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*/
public isVerticesDataPresent(kind: string): boolean {
if (!this._geometry) {
if (this._delayInfo) {
return this._delayInfo.indexOf(kind) !== -1;
}
return false;
}
return this._geometry.isVerticesDataPresent(kind);
}
/**
* Returns a string : the list of existing `kinds` of Vertex Data for this mesh.
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*/
public getVerticesDataKinds(): string[] {
if (!this._geometry) {
var result = [];
if (this._delayInfo) {
this._delayInfo.forEach(function (kind, index, array) {
result.push(kind);
});
}
return result;
}
return this._geometry.getVerticesDataKinds();
}
/**
* Returns a positive integer : the total number of indices in this mesh geometry.
* Returns zero if the mesh has no geometry.
*/
public getTotalIndices(): number {
if (!this._geometry) {
return 0;
}
return this._geometry.getTotalIndices();
}
/**
* Returns an array of integers or a typed array (Int32Array, Uint32Array, Uint16Array) populated with the mesh indices.
* If the parameter `copyWhenShared` is 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.
* Returns an empty array if the mesh has no geometry.
*/
public getIndices(copyWhenShared?: boolean): IndicesArray {
if (!this._geometry) {
return [];
}
return this._geometry.getIndices(copyWhenShared);
}
public get isBlocked(): boolean {
return this._masterMesh !== null && this._masterMesh !== undefined;
}
/**
* Boolean : true once the mesh is ready after all the delayed process (loading, etc) are complete.
*/
public isReady(): boolean {
if (this.delayLoadState === Engine.DELAYLOADSTATE_LOADING) {
return false;
}
return super.isReady();
}
/**
* Boolean : true if the mesh has been disposed.
*/
public isDisposed(): boolean {
return this._isDisposed;
}
public get sideOrientation(): number {
return this._sideOrientation;
}
/**
* Sets the mesh side orientation : BABYLON.Mesh.FRONTSIDE, BABYLON.Mesh.BACKSIDE, BABYLON.Mesh.DOUBLESIDE or BABYLON.Mesh.DEFAULTSIDE
* tuto : http://doc.babylonjs.com/tutorials/Discover_Basic_Elements#side-orientation
*/
public set sideOrientation(sideO: number) {
this._sideOrientation = sideO;
}
/**
* Boolean : true if the normals aren't to be recomputed on next mesh `positions` array update.
* This property is pertinent only for updatable parametric shapes.
*/
public get areNormalsFrozen(): boolean {
return this._areNormalsFrozen;
}
/**
* This function affects parametric shapes on vertex position update only : ribbons, tubes, etc.
* It has no effect at all on other shapes.
* It prevents the mesh normals from being recomputed on next `positions` array update.
* Returns the Mesh.
*/
public freezeNormals(): Mesh {
this._areNormalsFrozen = true;
return this;
}
/**
* This function affects parametric shapes on vertex position update only : ribbons, tubes, etc.
* It has no effect at all on other shapes.
* It reactivates the mesh normals computation if it was previously frozen.
* Returns the Mesh.
*/
public unfreezeNormals(): Mesh {
this._areNormalsFrozen = false;
return this;
}
/**
* Overrides instance count. Only applicable when custom instanced InterleavedVertexBuffer are used rather than InstancedMeshs
*/
public set overridenInstanceCount(count: number) {
this._overridenInstanceCount = count;
}
// Methods
public _preActivate(): Mesh {
var sceneRenderId = this.getScene().getRenderId();
if (this._preActivateId === sceneRenderId) {
return this;
}
this._preActivateId = sceneRenderId;
this._visibleInstances = null;
return this;
}
public _preActivateForIntermediateRendering(renderId: number): Mesh {
if (this._visibleInstances) {
this._visibleInstances.intermediateDefaultRenderId = renderId;
}
return this;
}
public _registerInstanceForRenderId(instance: InstancedMesh, renderId: number): Mesh {
if (!this._visibleInstances) {
this._visibleInstances = {};
this._visibleInstances.defaultRenderId = renderId;
this._visibleInstances.selfDefaultRenderId = this._renderId;
}
if (!this._visibleInstances[renderId]) {
this._visibleInstances[renderId] = new Array<InstancedMesh>();
}
this._visibleInstances[renderId].push(instance);
return this;
}
/**
* This method recomputes and sets a new BoundingInfo to the mesh unless it is locked.
* This means the mesh underlying bounding box and sphere are recomputed.
* Returns the Mesh.
*/
public refreshBoundingInfo(): Mesh {
if (this._boundingInfo.isLocked) {
return;
}
var data = this.getVerticesData(VertexBuffer.PositionKind);
if (data) {
var extend = Tools.ExtractMinAndMax(data, 0, this.getTotalVertices());
this._boundingInfo = new BoundingInfo(extend.minimum, extend.maximum);
}
if (this.subMeshes) {
for (var index = 0; index < this.subMeshes.length; index++) {
this.subMeshes[index].refreshBoundingInfo();
}
}
this._updateBoundingInfo();
return this;
}
public _createGlobalSubMesh(): SubMesh {
var totalVertices = this.getTotalVertices();
if (!totalVertices || !this.getIndices()) {
return null;
}
this.releaseSubMeshes();
return new SubMesh(0, 0, totalVertices, 0, this.getTotalIndices(), this);
}
public subdivide(count: number): void {
if (count < 1) {
return;
}
var totalIndices = this.getTotalIndices();
var subdivisionSize = (totalIndices / count) | 0;
var offset = 0;
// Ensure that subdivisionSize is a multiple of 3
while (subdivisionSize % 3 !== 0) {
subdivisionSize++;
}
this.releaseSubMeshes();
for (var index = 0; index < count; index++) {
if (offset >= totalIndices) {
break;
}
SubMesh.CreateFromIndices(0, offset, Math.min(subdivisionSize, totalIndices - offset), this);
offset += subdivisionSize;
}
this.synchronizeInstances();
}
/**
* Sets the vertex data of the mesh geometry for the requested `kind`.
* If the mesh has no geometry, a new Geometry object is set to the mesh and then passed this vertex data.
* The `data` are either a numeric array either a Float32Array.
* The parameter `updatable` is passed as is to the underlying Geometry object constructor (if initianilly none) or updater.
* The parameter `stride` is an optional positive integer, it is usually automatically deducted from the `kind` (3 for positions or normals, 2 for UV, etc).
* Note that a new underlying VertexBuffer object is created each call.
* If the `kind` is the `PositionKind`, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
*
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*
* Returns the Mesh.
*/
public setVerticesData(kind: string, data: number[] | Float32Array, updatable?: boolean, stride?: number): Mesh {
if (!this._geometry) {
var vertexData = new VertexData();
vertexData.set(data, kind);
var scene = this.getScene();
new Geometry(Geometry.RandomId(), scene, vertexData, updatable, this);
}
else {
this._geometry.setVerticesData(kind, data, updatable, stride);
}
return this;
}
public markVerticesDataAsUpdatable(kind: string, updatable = true) {
if (this.getVertexBuffer(kind).isUpdatable() === updatable) {
return;
}
this.setVerticesData(kind, this.getVerticesData(kind), updatable);
}
/**
* Sets the mesh VertexBuffer.
* Returns the Mesh.
*/
public setVerticesBuffer(buffer: VertexBuffer): Mesh {
if (!this._geometry) {
var scene = this.getScene();
new Geometry(Geometry.RandomId(), scene).applyToMesh(this);
}
this._geometry.setVerticesBuffer(buffer);
return this;
}
/**
* Updates the existing vertex data of the mesh geometry for the requested `kind`.
* If the mesh has no geometry, it is simply returned as it is.
* The `data` are either a numeric array either a Float32Array.
* No new underlying VertexBuffer object is created.
* If the `kind` is the `PositionKind` and if `updateExtends` is true, the mesh BoundingInfo is renewed, so the bounding box and sphere, and the mesh World Matrix is recomputed.
* If the parameter `makeItUnique` is true, a new global geometry is created from this positions and is set to the mesh.
*
* Possible `kind` values :
* - BABYLON.VertexBuffer.PositionKind
* - BABYLON.VertexBuffer.UVKind
* - BABYLON.VertexBuffer.UV2Kind
* - BABYLON.VertexBuffer.UV3Kind
* - BABYLON.VertexBuffer.UV4Kind
* - BABYLON.VertexBuffer.UV5Kind
* - BABYLON.VertexBuffer.UV6Kind
* - BABYLON.VertexBuffer.ColorKind
* - BABYLON.VertexBuffer.MatricesIndicesKind
* - BABYLON.VertexBuffer.MatricesIndicesExtraKind
* - BABYLON.VertexBuffer.MatricesWeightsKind
* - BABYLON.VertexBuffer.MatricesWeightsExtraKind
*
* Returns the Mesh.
*/
public updateVerticesData(kind: string, data: number[] | Float32Array, updateExtends?: boolean, makeItUnique?: boolean): Mesh {
if (!this._geometry) {
return;
}
if (!makeItUnique) {
this._geometry.updateVerticesData(kind, data, updateExtends);
}
else {
this.makeGeometryUnique();
this.updateVerticesData(kind, data, updateExtends, false);
}
return this;
}
/**
* This method updates the vertex positions of an updatable mesh according to the `positionFunction` returned values.
* tuto : http://doc.babylonjs.com/tutorials/How_to_dynamically_morph_a_mesh#other-shapes-updatemeshpositions
* The parameter `positionFunction` is a simple JS function what is passed the mesh `positions` array. It doesn't need to return anything.
* The parameter `computeNormals` is a boolean (default true) to enable/disable the mesh normal recomputation after the vertex position update.
* Returns the Mesh.
*/
public updateMeshPositions(positionFunction, computeNormals: boolean = true): Mesh {
var positions = this.getVerticesData(VertexBuffer.PositionKind);
positionFunction(positions);
this.updateVerticesData(VertexBuffer.PositionKind, positions, false, false);
if (computeNormals) {
var indices = this.getIndices();
var normals = this.getVerticesData(VertexBuffer.NormalKind);
VertexData.ComputeNormals(positions, indices, normals);
this.updateVerticesData(VertexBuffer.NormalKind, normals, false, false);
}
return this;
}
/**
* Creates a un-shared specific occurence of the geometry for the mesh.
* Returns the Mesh.
*/
public makeGeometryUnique(): Mesh {
if (!this._geometry) {
return;
}
var oldGeometry = this._geometry;
var geometry = this._geometry.copy(Geometry.RandomId());
oldGeometry.releaseForMesh(this, true);
geometry.applyToMesh(this);
return this;
}
/**
* Sets the mesh indices.
* Expects an array populated with integers or a typed array (Int32Array, Uint32Array, Uint16Array).
* If the mesh has no geometry, a new Geometry object is created and set to the mesh.
* This method creates a new index buffer each call.
* Returns the Mesh.
*/
public setIndices(indices: IndicesArray, totalVertices?: number): Mesh {
if (!this._geometry) {
var vertexData = new VertexData();
vertexData.indices = indices;
var scene = this.getScene();
new Geometry(Geometry.RandomId(), scene, vertexData, false, this);
}
else {
this._geometry.setIndices(indices, totalVertices);
}
return this;
}
/**
* Invert the geometry to move from a right handed system to a left handed one.
* Returns the Mesh.
*/
public toLeftHanded(): Mesh {
if (!this._geometry) {
return;
}
this._geometry.toLeftHanded();
return this;
}
public _bind(subMesh: SubMesh, effect: Effect, fillMode: number): Mesh {
var engine = this.getScene().getEngine();
// Wireframe
var indexToBind;
if (this._unIndexed) {
indexToBind = null;
} else {
switch (fillMode) {
case Material.PointFillMode:
indexToBind = null;
break;
case Material.WireFrameFillMode:
indexToBind = subMesh.getLinesIndexBuffer(this.getIndices(), engine);
break;
default:
case Material.TriangleFillMode:
indexToBind = this._unIndexed ? null : this._geometry.getIndexBuffer();
break;
}
}
// VBOs
this._geometry._bind(effect, indexToBind);
return this;
}
public _draw(subMesh: SubMesh, fillMode: number, instancesCount?: number): Mesh {
if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
return this;
}
this.onBeforeDrawObservable.notifyObservers(this);
var engine = this.getScene().getEngine();
// Draw order
switch (fillMode) {
case Material.PointFillMode:
engine.drawPointClouds(subMesh.verticesStart, subMesh.verticesCount, instancesCount);
break;
case Material.WireFrameFillMode:
if (this._unIndexed) {
engine.drawUnIndexed(false, subMesh.verticesStart, subMesh.verticesCount, instancesCount);
} else {
engine.draw(false, 0, instancesCount > 0 ? subMesh.linesIndexCount / 2 : subMesh.linesIndexCount, instancesCount);
}
break;
default:
if (this._unIndexed) {
engine.drawUnIndexed(true, subMesh.verticesStart, subMesh.verticesCount, instancesCount);
} else {
engine.draw(true, subMesh.indexStart, subMesh.indexCount, instancesCount);
}
}
return this;
}
/**
* Registers for this mesh a javascript function called just before the rendering process.
* This function is passed the current mesh.
* Return the Mesh.
*/
public registerBeforeRender(func: (mesh: AbstractMesh) => void): Mesh {
this.onBeforeRenderObservable.add(func);
return this;
}
/**
* Disposes a previously registered javascript function called before the rendering.
* This function is passed the current mesh.
* Returns the Mesh.
*/
public unregisterBeforeRender(func: (mesh: AbstractMesh) => void): Mesh {
this.onBeforeRenderObservable.removeCallback(func);
return this;
}
/**
* Registers for this mesh a javascript function called just after the rendering is complete.
* This function is passed the current mesh.
* Returns the Mesh.
*/
public registerAfterRender(func: (mesh: AbstractMesh) => void): Mesh {
this.onAfterRenderObservable.add(func);
return this;
}
/**
* Disposes a previously registered javascript function called after the rendering.
* This function is passed the current mesh.
* Return the Mesh.
*/
public unregisterAfterRender(func: (mesh: AbstractMesh) => void): Mesh {
this.onAfterRenderObservable.removeCallback(func);
return this;
}
public _getInstancesRenderList(subMeshId: number): _InstancesBatch {
var scene = this.getScene();
this._batchCache.mustReturn = false;
this._batchCache.renderSelf[subMeshId] = this.isEnabled() && this.isVisible;
this._batchCache.visibleInstances[subMeshId] = null;
if (this._visibleInstances) {
var currentRenderId = scene.getRenderId();
var defaultRenderId = (scene._isInIntermediateRendering() ? this._visibleInstances.intermediateDefaultRenderId : this._visibleInstances.defaultRenderId);
this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[currentRenderId];
var selfRenderId = this._renderId;
if (!this._batchCache.visibleInstances[subMeshId] && defaultRenderId) {
this._batchCache.visibleInstances[subMeshId] = this._visibleInstances[defaultRenderId];
currentRenderId = Math.max(defaultRenderId, currentRenderId);
selfRenderId = Math.max(this._visibleInstances.selfDefaultRenderId, currentRenderId);
}
if (this._batchCache.visibleInstances[subMeshId] && this._batchCache.visibleInstances[subMeshId].length) {
if (this._renderIdForInstances[subMeshId] === currentRenderId) {
this._batchCache.mustReturn = true;
return this._batchCache;
}
if (currentRenderId !== selfRenderId) {
this._batchCache.renderSelf[subMeshId] = false;
}
}
this._renderIdForInstances[subMeshId] = currentRenderId;
}
return this._batchCache;
}
public _renderWithInstances(subMesh: SubMesh, fillMode: number, batch: _InstancesBatch, effect: Effect, engine: Engine): Mesh {
var visibleInstances = batch.visibleInstances[subMesh._id];
var matricesCount = visibleInstances.length + 1;
var bufferSize = matricesCount * 16 * 4;
var currentInstancesBufferSize = this._instancesBufferSize;
var instancesBuffer = this._instancesBuffer;
while (this._instancesBufferSize < bufferSize) {
this._instancesBufferSize *= 2;