-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
abstractMesh.ts
2645 lines (2317 loc) · 108 KB
/
abstractMesh.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 { Observable } from "../Misc/observable";
import type { Nullable, FloatArray, IndicesArray, DeepImmutable } from "../types";
import type { Camera } from "../Cameras/camera";
import type { Scene, IDisposable } from "../scene";
import { ScenePerformancePriority } from "../scene";
import type { Vector2 } from "../Maths/math.vector";
import { Quaternion, Matrix, Vector3, TmpVectors } from "../Maths/math.vector";
import { Engine } from "../Engines/engine";
import type { Node } from "../node";
import { VertexBuffer } from "../Buffers/buffer";
import type { IGetSetVerticesData } from "../Meshes/mesh.vertexData";
import { VertexData } from "../Meshes/mesh.vertexData";
import { TransformNode } from "../Meshes/transformNode";
import type { SubMesh } from "../Meshes/subMesh";
import { PickingInfo } from "../Collisions/pickingInfo";
import type { IntersectionInfo } from "../Collisions/intersectionInfo";
import type { ICullable } from "../Culling/boundingInfo";
import { BoundingInfo } from "../Culling/boundingInfo";
import type { Material } from "../Materials/material";
import type { MaterialDefines } from "../Materials/materialDefines";
import type { Light } from "../Lights/light";
import type { Skeleton } from "../Bones/skeleton";
import type { MorphTargetManager } from "../Morph/morphTargetManager";
import type { IBakedVertexAnimationManager } from "../BakedVertexAnimation/bakedVertexAnimationManager";
import type { IEdgesRenderer } from "../Rendering/edgesRenderer";
import type { SolidParticle } from "../Particles/solidParticle";
import { Constants } from "../Engines/constants";
import type { AbstractActionManager } from "../Actions/abstractActionManager";
import { UniformBuffer } from "../Materials/uniformBuffer";
import { _MeshCollisionData } from "../Collisions/meshCollisionData";
import { _WarnImport } from "../Misc/devTools";
import type { RawTexture } from "../Materials/Textures/rawTexture";
import { extractMinAndMax } from "../Maths/math.functions";
import { Color3, Color4 } from "../Maths/math.color";
import { Epsilon } from "../Maths/math.constants";
import type { Plane } from "../Maths/math.plane";
import { Axis } from "../Maths/math.axis";
import type { IParticleSystem } from "../Particles/IParticleSystem";
import { RegisterClass } from "../Misc/typeStore";
declare type Ray = import("../Culling/ray").Ray;
declare type Collider = import("../Collisions/collider").Collider;
declare type TrianglePickingPredicate = import("../Culling/ray").TrianglePickingPredicate;
declare type RenderingGroup = import("../Rendering/renderingGroup").RenderingGroup;
declare type IEdgesRendererOptions = import("../Rendering/edgesRenderer").IEdgesRendererOptions;
/** @internal */
// eslint-disable-next-line @typescript-eslint/naming-convention
class _FacetDataStorage {
// facetData private properties
public facetPositions: Vector3[]; // facet local positions
public facetNormals: Vector3[]; // facet local normals
public facetPartitioning: number[][]; // partitioning array of facet index arrays
public facetNb: number = 0; // facet number
public partitioningSubdivisions: number = 10; // number of subdivisions per axis in the partitioning space
public partitioningBBoxRatio: number = 1.01; // the partitioning array space is by default 1% bigger than the bounding box
public facetDataEnabled: boolean = false; // is the facet data feature enabled on this mesh ?
public facetParameters: any = {}; // keep a reference to the object parameters to avoid memory re-allocation
public bbSize: Vector3 = Vector3.Zero(); // bbox size approximated for facet data
public subDiv = {
// actual number of subdivisions per axis for ComputeNormals()
max: 1,
// eslint-disable-next-line @typescript-eslint/naming-convention
X: 1,
// eslint-disable-next-line @typescript-eslint/naming-convention
Y: 1,
// eslint-disable-next-line @typescript-eslint/naming-convention
Z: 1,
};
public facetDepthSort: boolean = false; // is the facet depth sort to be computed
public facetDepthSortEnabled: boolean = false; // is the facet depth sort initialized
public depthSortedIndices: IndicesArray; // copy of the indices array to store them once sorted
public depthSortedFacets: { ind: number; sqDistance: number }[]; // array of depth sorted facets
public facetDepthSortFunction: (f1: { ind: number; sqDistance: number }, f2: { ind: number; sqDistance: number }) => number; // facet depth sort function
public facetDepthSortFrom: Vector3; // location where to depth sort from
public facetDepthSortOrigin: Vector3; // same as facetDepthSortFrom but expressed in the mesh local space
public invertedMatrix: Matrix; // Inverted world matrix.
}
/**
* @internal
**/
// eslint-disable-next-line @typescript-eslint/naming-convention
class _InternalAbstractMeshDataInfo {
public _hasVertexAlpha = false;
public _useVertexColors = true;
public _numBoneInfluencers = 4;
public _applyFog = true;
public _receiveShadows = false;
public _facetData = new _FacetDataStorage();
public _visibility = 1.0;
public _skeleton: Nullable<Skeleton> = null;
public _layerMask: number = 0x0fffffff;
public _computeBonesUsingShaders = true;
public _isActive = false;
public _onlyForInstances = false;
public _isActiveIntermediate = false;
public _onlyForInstancesIntermediate = false;
public _actAsRegularMesh = false;
public _currentLOD: Nullable<AbstractMesh> = null;
public _currentLODIsUpToDate: boolean = false;
public _collisionRetryCount: number = 3;
public _morphTargetManager: Nullable<MorphTargetManager> = null;
public _renderingGroupId = 0;
public _bakedVertexAnimationManager: Nullable<IBakedVertexAnimationManager> = null;
public _material: Nullable<Material> = null;
public _materialForRenderPass: Array<Material | undefined>; // map a render pass id (index in the array) to a Material
public _positions: Nullable<Vector3[]> = null;
public _pointerOverDisableMeshTesting: boolean = false;
// Collisions
public _meshCollisionData = new _MeshCollisionData();
public _enableDistantPicking = false;
/** @internal
* Bounding info that is unnafected by the addition of thin instances
*/
public _rawBoundingInfo: Nullable<BoundingInfo> = null;
}
/**
* Class used to store all common mesh properties
*/
export class AbstractMesh extends TransformNode implements IDisposable, ICullable, IGetSetVerticesData {
/** No occlusion */
public static OCCLUSION_TYPE_NONE = 0;
/** Occlusion set to optimistic */
public static OCCLUSION_TYPE_OPTIMISTIC = 1;
/** Occlusion set to strict */
public static OCCLUSION_TYPE_STRICT = 2;
/** Use an accurate occlusion algorithm */
public static OCCLUSION_ALGORITHM_TYPE_ACCURATE = 0;
/** Use a conservative occlusion algorithm */
public static OCCLUSION_ALGORITHM_TYPE_CONSERVATIVE = 1;
/** Default culling strategy : this is an exclusion test and it's the more accurate.
* Test order :
* Is the bounding sphere outside the frustum ?
* If not, are the bounding box vertices outside the frustum ?
* It not, then the cullable object is in the frustum.
*/
public static readonly CULLINGSTRATEGY_STANDARD = Constants.MESHES_CULLINGSTRATEGY_STANDARD;
/** Culling strategy : Bounding Sphere Only.
* This is an exclusion test. It's faster than the standard strategy because the bounding box is not tested.
* It's also less accurate than the standard because some not visible objects can still be selected.
* Test : is the bounding sphere outside the frustum ?
* If not, then the cullable object is in the frustum.
*/
public static readonly CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY = Constants.MESHES_CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY;
/** Culling strategy : Optimistic Inclusion.
* This in an inclusion test first, then the standard exclusion test.
* This can be faster when a cullable object is expected to be almost always in the camera frustum.
* This could also be a little slower than the standard test when the tested object center is not the frustum but one of its bounding box vertex is still inside.
* Anyway, it's as accurate as the standard strategy.
* Test :
* Is the cullable object bounding sphere center in the frustum ?
* If not, apply the default culling strategy.
*/
public static readonly CULLINGSTRATEGY_OPTIMISTIC_INCLUSION = Constants.MESHES_CULLINGSTRATEGY_OPTIMISTIC_INCLUSION;
/** Culling strategy : Optimistic Inclusion then Bounding Sphere Only.
* This in an inclusion test first, then the bounding sphere only exclusion test.
* This can be the fastest test when a cullable object is expected to be almost always in the camera frustum.
* This could also be a little slower than the BoundingSphereOnly strategy when the tested object center is not in the frustum but its bounding sphere still intersects it.
* It's less accurate than the standard strategy and as accurate as the BoundingSphereOnly strategy.
* Test :
* Is the cullable object bounding sphere center in the frustum ?
* If not, apply the Bounding Sphere Only strategy. No Bounding Box is tested here.
*/
public static readonly CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY = Constants.MESHES_CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY;
/**
* No billboard
*/
public static get BILLBOARDMODE_NONE(): number {
return TransformNode.BILLBOARDMODE_NONE;
}
/** Billboard on X axis */
public static get BILLBOARDMODE_X(): number {
return TransformNode.BILLBOARDMODE_X;
}
/** Billboard on Y axis */
public static get BILLBOARDMODE_Y(): number {
return TransformNode.BILLBOARDMODE_Y;
}
/** Billboard on Z axis */
public static get BILLBOARDMODE_Z(): number {
return TransformNode.BILLBOARDMODE_Z;
}
/** Billboard on all axes */
public static get BILLBOARDMODE_ALL(): number {
return TransformNode.BILLBOARDMODE_ALL;
}
/** Billboard on using position instead of orientation */
public static get BILLBOARDMODE_USE_POSITION(): number {
return TransformNode.BILLBOARDMODE_USE_POSITION;
}
// Internal data
/** @internal */
public _internalAbstractMeshDataInfo = new _InternalAbstractMeshDataInfo();
/** @internal */
public _waitingMaterialId: Nullable<string> = null;
/**
* The culling strategy to use to check whether the mesh must be rendered or not.
* This value can be changed at any time and will be used on the next render mesh selection.
* The possible values are :
* - AbstractMesh.CULLINGSTRATEGY_STANDARD
* - AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY
* - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION
* - AbstractMesh.CULLINGSTRATEGY_OPTIMISTIC_INCLUSION_THEN_BSPHERE_ONLY
* Please read each static variable documentation to get details about the culling process.
* */
public cullingStrategy = AbstractMesh.CULLINGSTRATEGY_BOUNDINGSPHERE_ONLY;
/**
* Gets the number of facets in the mesh
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#what-is-a-mesh-facet
*/
public get facetNb(): number {
return this._internalAbstractMeshDataInfo._facetData.facetNb;
}
/**
* Gets or set the number (integer) of subdivisions per axis in the partitioning space
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#tweaking-the-partitioning
*/
public get partitioningSubdivisions(): number {
return this._internalAbstractMeshDataInfo._facetData.partitioningSubdivisions;
}
public set partitioningSubdivisions(nb: number) {
this._internalAbstractMeshDataInfo._facetData.partitioningSubdivisions = nb;
}
/**
* The ratio (float) to apply to the bounding box size to set to the partitioning space.
* Ex : 1.01 (default) the partitioning space is 1% bigger than the bounding box
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#tweaking-the-partitioning
*/
public get partitioningBBoxRatio(): number {
return this._internalAbstractMeshDataInfo._facetData.partitioningBBoxRatio;
}
public set partitioningBBoxRatio(ratio: number) {
this._internalAbstractMeshDataInfo._facetData.partitioningBBoxRatio = ratio;
}
/**
* Gets or sets a boolean indicating that the facets must be depth sorted on next call to `updateFacetData()`.
* Works only for updatable meshes.
* Doesn't work with multi-materials
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#facet-depth-sort
*/
public get mustDepthSortFacets(): boolean {
return this._internalAbstractMeshDataInfo._facetData.facetDepthSort;
}
public set mustDepthSortFacets(sort: boolean) {
this._internalAbstractMeshDataInfo._facetData.facetDepthSort = sort;
}
/**
* The location (Vector3) where the facet depth sort must be computed from.
* By default, the active camera position.
* Used only when facet depth sort is enabled
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#facet-depth-sort
*/
public get facetDepthSortFrom(): Vector3 {
return this._internalAbstractMeshDataInfo._facetData.facetDepthSortFrom;
}
public set facetDepthSortFrom(location: Vector3) {
this._internalAbstractMeshDataInfo._facetData.facetDepthSortFrom = location;
}
/** number of collision detection tries. Change this value if not all collisions are detected and handled properly */
public get collisionRetryCount(): number {
return this._internalAbstractMeshDataInfo._collisionRetryCount;
}
public set collisionRetryCount(retryCount: number) {
this._internalAbstractMeshDataInfo._collisionRetryCount = retryCount;
}
/**
* gets a boolean indicating if facetData is enabled
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/facetData#what-is-a-mesh-facet
*/
public get isFacetDataEnabled(): boolean {
return this._internalAbstractMeshDataInfo._facetData.facetDataEnabled;
}
/**
* Gets or sets the morph target manager
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/morphTargets
*/
public get morphTargetManager(): Nullable<MorphTargetManager> {
return this._internalAbstractMeshDataInfo._morphTargetManager;
}
public set morphTargetManager(value: Nullable<MorphTargetManager>) {
if (this._internalAbstractMeshDataInfo._morphTargetManager === value) {
return;
}
this._internalAbstractMeshDataInfo._morphTargetManager = value;
this._syncGeometryWithMorphTargetManager();
}
/**
* Gets or sets the baked vertex animation manager
* @see https://doc.babylonjs.com/features/featuresDeepDive/animation/baked_texture_animations
*/
public get bakedVertexAnimationManager(): Nullable<IBakedVertexAnimationManager> {
return this._internalAbstractMeshDataInfo._bakedVertexAnimationManager;
}
public set bakedVertexAnimationManager(value: Nullable<IBakedVertexAnimationManager>) {
if (this._internalAbstractMeshDataInfo._bakedVertexAnimationManager === value) {
return;
}
this._internalAbstractMeshDataInfo._bakedVertexAnimationManager = value;
this._markSubMeshesAsAttributesDirty();
}
/** @internal */
public _syncGeometryWithMorphTargetManager(): void {}
/**
* @internal
*/
public _updateNonUniformScalingState(value: boolean): boolean {
if (!super._updateNonUniformScalingState(value)) {
return false;
}
this._markSubMeshesAsMiscDirty();
return true;
}
/** @internal */
public get rawBoundingInfo(): Nullable<BoundingInfo> {
return this._internalAbstractMeshDataInfo._rawBoundingInfo;
}
public set rawBoundingInfo(boundingInfo: Nullable<BoundingInfo>) {
this._internalAbstractMeshDataInfo._rawBoundingInfo = boundingInfo;
}
// Events
/**
* An event triggered when this mesh collides with another one
*/
public onCollideObservable = new Observable<AbstractMesh>();
/** Set a function to call when this mesh collides with another one */
public set onCollide(callback: (collidedMesh?: AbstractMesh) => void) {
if (this._internalAbstractMeshDataInfo._meshCollisionData._onCollideObserver) {
this.onCollideObservable.remove(this._internalAbstractMeshDataInfo._meshCollisionData._onCollideObserver);
}
this._internalAbstractMeshDataInfo._meshCollisionData._onCollideObserver = this.onCollideObservable.add(callback);
}
/**
* An event triggered when the collision's position changes
*/
public onCollisionPositionChangeObservable = new Observable<Vector3>();
/** Set a function to call when the collision's position changes */
public set onCollisionPositionChange(callback: () => void) {
if (this._internalAbstractMeshDataInfo._meshCollisionData._onCollisionPositionChangeObserver) {
this.onCollisionPositionChangeObservable.remove(this._internalAbstractMeshDataInfo._meshCollisionData._onCollisionPositionChangeObserver);
}
this._internalAbstractMeshDataInfo._meshCollisionData._onCollisionPositionChangeObserver = this.onCollisionPositionChangeObservable.add(callback);
}
/**
* An event triggered when material is changed
*/
public onMaterialChangedObservable = new Observable<AbstractMesh>();
// Properties
/**
* Gets or sets the orientation for POV movement & rotation
*/
public definedFacingForward = true;
/** @internal */
public _occlusionQuery: Nullable<WebGLQuery | number> = null;
/** @internal */
public _renderingGroup: Nullable<RenderingGroup> = null;
/**
* Gets or sets mesh visibility between 0 and 1 (default is 1)
*/
public get visibility(): number {
return this._internalAbstractMeshDataInfo._visibility;
}
/**
* Gets or sets mesh visibility between 0 and 1 (default is 1)
*/
public set visibility(value: number) {
if (this._internalAbstractMeshDataInfo._visibility === value) {
return;
}
const oldValue = this._internalAbstractMeshDataInfo._visibility;
this._internalAbstractMeshDataInfo._visibility = value;
if ((oldValue === 1 && value !== 1) || (oldValue !== 1 && value === 1)) {
this._markSubMeshesAsDirty((defines) => {
defines.markAsMiscDirty();
defines.markAsPrePassDirty();
});
}
}
/** Gets or sets the alpha index used to sort transparent meshes
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/transparent_rendering#alpha-index
*/
public alphaIndex = Number.MAX_VALUE;
/**
* Gets or sets a boolean indicating if the mesh is visible (renderable). Default is true
*/
public isVisible = true;
/**
* Gets or sets a boolean indicating if the mesh can be picked (by scene.pick for instance or through actions). Default is true
*/
public isPickable = true;
/**
* Gets or sets a boolean indicating if the mesh can be near picked. Default is false
*/
public isNearPickable = false;
/**
* Gets or sets a boolean indicating if the mesh can be near grabbed. Default is false
*/
public isNearGrabbable = false;
/** Gets or sets a boolean indicating that bounding boxes of subMeshes must be rendered as well (false by default) */
public showSubMeshesBoundingBox = false;
/** Gets or sets a boolean indicating if the mesh must be considered as a ray blocker for lens flares (false by default)
* @see https://doc.babylonjs.com/features/featuresDeepDive/environment/lenseFlare
*/
public isBlocker = false;
/**
* Gets or sets a boolean indicating that pointer move events must be supported on this mesh (false by default)
*/
public enablePointerMoveEvents = false;
/**
* Gets or sets the property which disables the test that is checking that the mesh under the pointer is the same than the previous time we tested for it (default: false).
* Set this property to true if you want thin instances picking to be reported accurately when moving over the mesh.
* Note that setting this property to true will incur some performance penalties when dealing with pointer events for this mesh so use it sparingly.
*/
public get pointerOverDisableMeshTesting() {
return this._internalAbstractMeshDataInfo._pointerOverDisableMeshTesting;
}
public set pointerOverDisableMeshTesting(disable: boolean) {
this._internalAbstractMeshDataInfo._pointerOverDisableMeshTesting = disable;
}
/**
* Specifies the rendering group id for this mesh (0 by default)
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/advanced/transparent_rendering#rendering-groups
*/
public get renderingGroupId() {
return this._internalAbstractMeshDataInfo._renderingGroupId;
}
public set renderingGroupId(value: number) {
this._internalAbstractMeshDataInfo._renderingGroupId = value;
}
/** Gets or sets current material */
public get material(): Nullable<Material> {
return this._internalAbstractMeshDataInfo._material;
}
public set material(value: Nullable<Material>) {
if (this._internalAbstractMeshDataInfo._material === value) {
return;
}
// remove from material mesh map id needed
if (this._internalAbstractMeshDataInfo._material && this._internalAbstractMeshDataInfo._material.meshMap) {
this._internalAbstractMeshDataInfo._material.meshMap[this.uniqueId] = undefined;
}
this._internalAbstractMeshDataInfo._material = value;
if (value && value.meshMap) {
value.meshMap[this.uniqueId] = this;
}
if (this.onMaterialChangedObservable.hasObservers()) {
this.onMaterialChangedObservable.notifyObservers(this);
}
if (!this.subMeshes) {
return;
}
this.resetDrawCache();
this._unBindEffect();
}
/**
* Gets the material used to render the mesh in a specific render pass
* @param renderPassId render pass id
* @returns material used for the render pass. If no specific material is used for this render pass, undefined is returned (meaning mesh.material is used for this pass)
*/
public getMaterialForRenderPass(renderPassId: number): Material | undefined {
return this._internalAbstractMeshDataInfo._materialForRenderPass?.[renderPassId];
}
/**
* Sets the material to be used to render the mesh in a specific render pass
* @param renderPassId render pass id
* @param material material to use for this render pass. If undefined is passed, no specific material will be used for this render pass but the regular material will be used instead (mesh.material)
*/
public setMaterialForRenderPass(renderPassId: number, material?: Material): void {
this.resetDrawCache(renderPassId);
if (!this._internalAbstractMeshDataInfo._materialForRenderPass) {
this._internalAbstractMeshDataInfo._materialForRenderPass = [];
}
this._internalAbstractMeshDataInfo._materialForRenderPass[renderPassId] = material;
}
/**
* Gets or sets a boolean indicating that this mesh can receive realtime shadows
* @see https://doc.babylonjs.com/features/featuresDeepDive/lights/shadows
*/
public get receiveShadows(): boolean {
return this._internalAbstractMeshDataInfo._receiveShadows;
}
public set receiveShadows(value: boolean) {
if (this._internalAbstractMeshDataInfo._receiveShadows === value) {
return;
}
this._internalAbstractMeshDataInfo._receiveShadows = value;
this._markSubMeshesAsLightDirty();
}
/** Defines color to use when rendering outline */
public outlineColor = Color3.Red();
/** Define width to use when rendering outline */
public outlineWidth = 0.02;
/** Defines color to use when rendering overlay */
public overlayColor = Color3.Red();
/** Defines alpha to use when rendering overlay */
public overlayAlpha = 0.5;
/** Gets or sets a boolean indicating that this mesh contains vertex color data with alpha values */
public get hasVertexAlpha(): boolean {
return this._internalAbstractMeshDataInfo._hasVertexAlpha;
}
public set hasVertexAlpha(value: boolean) {
if (this._internalAbstractMeshDataInfo._hasVertexAlpha === value) {
return;
}
this._internalAbstractMeshDataInfo._hasVertexAlpha = value;
this._markSubMeshesAsAttributesDirty();
this._markSubMeshesAsMiscDirty();
}
/** Gets or sets a boolean indicating that this mesh needs to use vertex color data to render (if this kind of vertex data is available in the geometry) */
public get useVertexColors(): boolean {
return this._internalAbstractMeshDataInfo._useVertexColors;
}
public set useVertexColors(value: boolean) {
if (this._internalAbstractMeshDataInfo._useVertexColors === value) {
return;
}
this._internalAbstractMeshDataInfo._useVertexColors = value;
this._markSubMeshesAsAttributesDirty();
}
/**
* Gets or sets a boolean indicating that bone animations must be computed by the CPU (false by default)
*/
public get computeBonesUsingShaders(): boolean {
return this._internalAbstractMeshDataInfo._computeBonesUsingShaders;
}
public set computeBonesUsingShaders(value: boolean) {
if (this._internalAbstractMeshDataInfo._computeBonesUsingShaders === value) {
return;
}
this._internalAbstractMeshDataInfo._computeBonesUsingShaders = value;
this._markSubMeshesAsAttributesDirty();
}
/** Gets or sets the number of allowed bone influences per vertex (4 by default) */
public get numBoneInfluencers(): number {
return this._internalAbstractMeshDataInfo._numBoneInfluencers;
}
public set numBoneInfluencers(value: number) {
if (this._internalAbstractMeshDataInfo._numBoneInfluencers === value) {
return;
}
this._internalAbstractMeshDataInfo._numBoneInfluencers = value;
this._markSubMeshesAsAttributesDirty();
}
/** Gets or sets a boolean indicating that this mesh will allow fog to be rendered on it (true by default) */
public get applyFog(): boolean {
return this._internalAbstractMeshDataInfo._applyFog;
}
public set applyFog(value: boolean) {
if (this._internalAbstractMeshDataInfo._applyFog === value) {
return;
}
this._internalAbstractMeshDataInfo._applyFog = value;
this._markSubMeshesAsMiscDirty();
}
/** When enabled, decompose picking matrices for better precision with large values for mesh position and scling */
public get enableDistantPicking(): boolean {
return this._internalAbstractMeshDataInfo._enableDistantPicking;
}
public set enableDistantPicking(value: boolean) {
this._internalAbstractMeshDataInfo._enableDistantPicking = value;
}
/** Gets or sets a boolean indicating that internal octree (if available) can be used to boost submeshes selection (true by default) */
public useOctreeForRenderingSelection = true;
/** Gets or sets a boolean indicating that internal octree (if available) can be used to boost submeshes picking (true by default) */
public useOctreeForPicking = true;
/** Gets or sets a boolean indicating that internal octree (if available) can be used to boost submeshes collision (true by default) */
public useOctreeForCollisions = true;
/**
* Gets or sets the current layer mask (default is 0x0FFFFFFF)
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/layerMasksAndMultiCam
*/
public get layerMask(): number {
return this._internalAbstractMeshDataInfo._layerMask;
}
public set layerMask(value: number) {
if (value === this._internalAbstractMeshDataInfo._layerMask) {
return;
}
this._internalAbstractMeshDataInfo._layerMask = value;
this._resyncLightSources();
}
/**
* True if the mesh must be rendered in any case (this will shortcut the frustum clipping phase)
*/
public alwaysSelectAsActiveMesh = false;
/**
* Gets or sets a boolean indicating that the bounding info does not need to be kept in sync (for performance reason)
*/
public doNotSyncBoundingInfo = false;
/**
* Gets or sets the current action manager
* @see https://doc.babylonjs.com/features/featuresDeepDive/events/actions
*/
public actionManager: Nullable<AbstractActionManager> = null;
/**
* Gets or sets the ellipsoid used to impersonate this mesh when using collision engine (default is (0.5, 1, 0.5))
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/camera_collisions
*/
public ellipsoid = new Vector3(0.5, 1, 0.5);
/**
* Gets or sets the ellipsoid offset used to impersonate this mesh when using collision engine (default is (0, 0, 0))
* @see https://doc.babylonjs.com/features/featuresDeepDive/cameras/camera_collisions
*/
public ellipsoidOffset = new Vector3(0, 0, 0);
/**
* Gets or sets a collision mask used to mask collisions (default is -1).
* A collision between A and B will happen if A.collisionGroup & b.collisionMask !== 0
*/
public get collisionMask(): number {
return this._internalAbstractMeshDataInfo._meshCollisionData._collisionMask;
}
public set collisionMask(mask: number) {
this._internalAbstractMeshDataInfo._meshCollisionData._collisionMask = !isNaN(mask) ? mask : -1;
}
/**
* Gets or sets a collision response flag (default is true).
* when collisionResponse is false, events are still triggered but colliding entity has no response
* This helps creating trigger volume when user wants collision feedback events but not position/velocity
* to respond to the collision.
*/
public get collisionResponse(): boolean {
return this._internalAbstractMeshDataInfo._meshCollisionData._collisionResponse;
}
public set collisionResponse(response: boolean) {
this._internalAbstractMeshDataInfo._meshCollisionData._collisionResponse = response;
}
/**
* Gets or sets the current collision group mask (-1 by default).
* A collision between A and B will happen if A.collisionGroup & b.collisionMask !== 0
*/
public get collisionGroup(): number {
return this._internalAbstractMeshDataInfo._meshCollisionData._collisionGroup;
}
public set collisionGroup(mask: number) {
this._internalAbstractMeshDataInfo._meshCollisionData._collisionGroup = !isNaN(mask) ? mask : -1;
}
/**
* Gets or sets current surrounding meshes (null by default).
*
* By default collision detection is tested against every mesh in the scene.
* It is possible to set surroundingMeshes to a defined list of meshes and then only these specified
* meshes will be tested for the collision.
*
* Note: if set to an empty array no collision will happen when this mesh is moved.
*/
public get surroundingMeshes(): Nullable<AbstractMesh[]> {
return this._internalAbstractMeshDataInfo._meshCollisionData._surroundingMeshes;
}
public set surroundingMeshes(meshes: Nullable<AbstractMesh[]>) {
this._internalAbstractMeshDataInfo._meshCollisionData._surroundingMeshes = meshes;
}
// Edges
/**
* Defines edge width used when edgesRenderer is enabled
* @see https://www.babylonjs-playground.com/#10OJSG#13
*/
public edgesWidth = 1;
/**
* Defines edge color used when edgesRenderer is enabled
* @see https://www.babylonjs-playground.com/#10OJSG#13
*/
public edgesColor = new Color4(1, 0, 0, 1);
/** @internal */
public _edgesRenderer: Nullable<IEdgesRenderer> = null;
/** @internal */
public _masterMesh: Nullable<AbstractMesh> = null;
protected _boundingInfo: Nullable<BoundingInfo> = null;
protected _boundingInfoIsDirty = true;
/** @internal */
public _renderId = 0;
/**
* Gets or sets the list of subMeshes
* @see https://doc.babylonjs.com/features/featuresDeepDive/materials/using/multiMaterials
*/
public subMeshes: SubMesh[];
/** @internal */
public _intersectionsInProgress = new Array<AbstractMesh>();
/** @internal */
public _unIndexed = false;
/** @internal */
public _lightSources = new Array<Light>();
/** Gets the list of lights affecting that mesh */
public get lightSources(): Light[] {
return this._lightSources;
}
/** @internal */
public get _positions(): Nullable<Vector3[]> {
return null;
}
// Loading properties
/** @internal */
public _waitingData: {
lods: Nullable<any>;
actions: Nullable<any>;
freezeWorldMatrix: Nullable<boolean>;
} = {
lods: null,
actions: null,
freezeWorldMatrix: null,
};
/** @internal */
public _bonesTransformMatrices: Nullable<Float32Array> = null;
/** @internal */
public _transformMatrixTexture: Nullable<RawTexture> = null;
/**
* Gets or sets a skeleton to apply skinning transformations
* @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/bonesSkeletons
*/
public set skeleton(value: Nullable<Skeleton>) {
const skeleton = this._internalAbstractMeshDataInfo._skeleton;
if (skeleton && skeleton.needInitialSkinMatrix) {
skeleton._unregisterMeshWithPoseMatrix(this);
}
if (value && value.needInitialSkinMatrix) {
value._registerMeshWithPoseMatrix(this);
}
this._internalAbstractMeshDataInfo._skeleton = value;
if (!this._internalAbstractMeshDataInfo._skeleton) {
this._bonesTransformMatrices = null;
}
this._markSubMeshesAsAttributesDirty();
}
public get skeleton(): Nullable<Skeleton> {
return this._internalAbstractMeshDataInfo._skeleton;
}
/**
* An event triggered when the mesh is rebuilt.
*/
public onRebuildObservable = new Observable<AbstractMesh>();
/**
* The current mesh uniform buffer.
* @internal Internal use only.
*/
public _uniformBuffer: UniformBuffer;
// Constructor
/**
* Creates a new AbstractMesh
* @param name defines the name of the mesh
* @param scene defines the hosting scene
*/
constructor(name: string, scene: Nullable<Scene> = null) {
super(name, scene, false);
scene = this.getScene();
scene.addMesh(this);
this._resyncLightSources();
// Mesh Uniform Buffer.
this._uniformBuffer = new UniformBuffer(this.getScene().getEngine(), undefined, undefined, name, !this.getScene().getEngine().isWebGPU);
this._buildUniformLayout();
switch (scene.performancePriority) {
case ScenePerformancePriority.Aggressive:
this.doNotSyncBoundingInfo = true;
// eslint-disable-next-line no-fallthrough
case ScenePerformancePriority.Intermediate:
this.alwaysSelectAsActiveMesh = true;
this.isPickable = false;
break;
}
}
protected _buildUniformLayout(): void {
this._uniformBuffer.addUniform("world", 16);
this._uniformBuffer.addUniform("visibility", 1);
this._uniformBuffer.create();
}
/**
* Transfer the mesh values to its UBO.
* @param world The world matrix associated with the mesh
*/
public transferToEffect(world: Matrix): void {
const ubo = this._uniformBuffer;
ubo.updateMatrix("world", world);
ubo.updateFloat("visibility", this._internalAbstractMeshDataInfo._visibility);
ubo.update();
}
/**
* Gets the mesh uniform buffer.
* @returns the uniform buffer of the mesh.
*/
public getMeshUniformBuffer(): UniformBuffer {
return this._uniformBuffer;
}
/**
* Returns the string "AbstractMesh"
* @returns "AbstractMesh"
*/
public getClassName(): string {
return "AbstractMesh";
}
/**
* Gets a string representation of the current mesh
* @param fullDetails defines a boolean indicating if full details must be included
* @returns a string representation of the current mesh
*/
public toString(fullDetails?: boolean): string {
let ret = "Name: " + this.name + ", isInstance: " + (this.getClassName() !== "InstancedMesh" ? "YES" : "NO");
ret += ", # of submeshes: " + (this.subMeshes ? this.subMeshes.length : 0);
const skeleton = this._internalAbstractMeshDataInfo._skeleton;
if (skeleton) {
ret += ", skeleton: " + skeleton.name;
}
if (fullDetails) {
ret += ", billboard mode: " + ["NONE", "X", "Y", null, "Z", null, null, "ALL"][this.billboardMode];
ret += ", freeze wrld mat: " + (this._isWorldMatrixFrozen || this._waitingData.freezeWorldMatrix ? "YES" : "NO");
}
return ret;
}
/**
* @internal
*/
protected _getEffectiveParent(): Nullable<Node> {
if (this._masterMesh && this.billboardMode !== TransformNode.BILLBOARDMODE_NONE) {
return this._masterMesh;
}
return super._getEffectiveParent();
}
/**
* @internal
*/
public _getActionManagerForTrigger(trigger?: number, initialCall = true): Nullable<AbstractActionManager> {
if (this.actionManager && (initialCall || this.actionManager.isRecursive)) {
if (trigger) {
if (this.actionManager.hasSpecificTrigger(trigger)) {
return this.actionManager;
}
} else {
return this.actionManager;
}
}
if (!this.parent) {
return null;
}
return this.parent._getActionManagerForTrigger(trigger, false);
}
/**
* @internal
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public _rebuild(dispose = false): void {
this.onRebuildObservable.notifyObservers(this);
if (this._occlusionQuery !== null) {
this._occlusionQuery = null;
}
if (!this.subMeshes) {
return;
}
for (const subMesh of this.subMeshes) {
subMesh._rebuild();
}
}
/** @internal */
public _resyncLightSources(): void {
this._lightSources.length = 0;
for (const light of this.getScene().lights) {
if (!light.isEnabled()) {
continue;
}
if (light.canAffectMesh(this)) {
this._lightSources.push(light);
}
}
this._markSubMeshesAsLightDirty();
}
/**
* @internal