@onerjs/core 8.47.9 → 8.48.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,13 +2,16 @@ import { type Nullable } from "../types.js";
2
2
  import { type Scene } from "../scene.js";
3
3
  import { type AbstractMesh } from "../Meshes/abstractMesh.js";
4
4
  import { Mesh } from "../Meshes/mesh.js";
5
- import { Vector3 } from "../Maths/math.vector.js";
5
+ import { Matrix, Vector3 } from "../Maths/math.vector.js";
6
+ import { type Material } from "../Materials/material.js";
7
+ import { StandardMaterial } from "../Materials/standardMaterial.js";
6
8
  import { type IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePlugin.js";
7
- import { type IPhysicsEnginePluginV2 } from "../Physics/v2/IPhysicsEnginePlugin.js";
9
+ import { type IPhysicsEnginePluginV2, type PhysicsMassProperties } from "../Physics/v2/IPhysicsEnginePlugin.js";
8
10
  import { PhysicsImpostor } from "../Physics/v1/physicsImpostor.js";
9
11
  import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer.js";
10
12
  import { type PhysicsBody } from "../Physics/v2/physicsBody.js";
11
13
  import { type PhysicsConstraint } from "../Physics/v2/physicsConstraint.js";
14
+ import { TransformNode } from "../Meshes/transformNode.js";
12
15
  import "../Physics/joinedPhysicsEngineComponent.js";
13
16
  /**
14
17
  * Used to show the physics impostor around the specific mesh
@@ -29,7 +32,7 @@ export declare class PhysicsViewer {
29
32
  /** @internal */
30
33
  protected _inertiaMeshes: Array<Nullable<AbstractMesh>>;
31
34
  /** @internal */
32
- protected _constraintMeshes: Array<Nullable<Array<AbstractMesh>>>;
35
+ protected _constraintMeshes: Array<Nullable<Array<TransformNode>>>;
33
36
  /** @internal */
34
37
  protected _scene: Nullable<Scene>;
35
38
  /** @internal */
@@ -42,25 +45,20 @@ export declare class PhysicsViewer {
42
45
  protected _numConstraints: number;
43
46
  /** @internal */
44
47
  protected _physicsEnginePlugin: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null;
45
- private _renderFunction;
46
- private _inertiaRenderFunction;
47
- private _constraintRenderFunction;
48
- private _utilityLayer;
49
- private _ownUtilityLayer;
50
- private _debugBoxMesh;
51
- private _debugSphereMesh;
52
- private _debugCapsuleMesh;
53
- private _debugCylinderMesh;
54
- private _debugMaterial;
55
- private _debugInertiaMaterial;
56
- private _debugMeshMeshes;
57
- private _constraintAxesSize;
58
- private _constraintAngularSize;
59
- private _localMatrixCache;
60
- private _tempVectorCache;
61
- private _inertiaMatrixCache;
62
- private _transformMatrixCache;
63
- private _finalMatrixCache;
48
+ protected _renderFunction: () => void;
49
+ protected _inertiaRenderFunction: () => void;
50
+ protected _constraintRenderFunction: () => void;
51
+ protected _utilityLayer: Nullable<UtilityLayerRenderer>;
52
+ protected _ownUtilityLayer: boolean;
53
+ protected _debugBoxMesh: Mesh;
54
+ protected _debugSphereMesh: Mesh;
55
+ protected _debugCapsuleMesh: Mesh;
56
+ protected _debugCylinderMesh: Mesh;
57
+ protected _debugMaterial: StandardMaterial;
58
+ protected _debugInertiaMaterial: StandardMaterial;
59
+ protected _debugMeshMeshes: Mesh[];
60
+ protected _constraintAxesSize: number;
61
+ protected _constraintAngularSize: number;
64
62
  /**
65
63
  * Creates a new PhysicsViewer
66
64
  * @param scene defines the hosting scene
@@ -101,7 +99,7 @@ export declare class PhysicsViewer {
101
99
  * @param scaling
102
100
  */
103
101
  protected _makeScalingUnitInPlace(scaling: Vector3): void;
104
- protected _updateDebugConstraint(constraint: PhysicsConstraint, parentingMesh: AbstractMesh): void;
102
+ protected _updateDebugConstraint(constraint: PhysicsConstraint, parentingMesh: TransformNode): void;
105
103
  /**
106
104
  * Renders a specified physic impostor
107
105
  * @param impostor defines the impostor to render
@@ -130,7 +128,7 @@ export declare class PhysicsViewer {
130
128
  * @param constraint the physics constraint to show
131
129
  * @returns the debug mesh, or null if the constraint is already shown
132
130
  */
133
- showConstraint(constraint: PhysicsConstraint): Nullable<AbstractMesh>;
131
+ showConstraint(constraint: PhysicsConstraint): Nullable<TransformNode>;
134
132
  /**
135
133
  * Hides an impostor from the scene.
136
134
  * @param impostor - The impostor to hide.
@@ -163,15 +161,15 @@ export declare class PhysicsViewer {
163
161
  * @param constraint the constraint to hide
164
162
  */
165
163
  hideConstraint(constraint: Nullable<PhysicsConstraint>): void;
166
- private _getDebugMaterial;
167
- private _getDebugInertiaMaterial;
168
- private _getDebugAxisColoredMaterial;
169
- private _getDebugBoxMesh;
170
- private _getDebugSphereMesh;
171
- private _getDebugCapsuleMesh;
172
- private _getDebugCylinderMesh;
173
- private _getDebugMeshMesh;
174
- private _getDebugMesh;
164
+ protected _getDebugMaterial(scene: Scene): Material;
165
+ protected _getDebugInertiaMaterial(scene: Scene): Material;
166
+ protected _getDebugAxisColoredMaterial(axisNumber: number, scene: Scene): Material;
167
+ protected _getDebugBoxMesh(scene: Scene): AbstractMesh;
168
+ protected _getDebugSphereMesh(scene: Scene): AbstractMesh;
169
+ protected _getDebugCapsuleMesh(scene: Scene): AbstractMesh;
170
+ protected _getDebugCylinderMesh(scene: Scene): AbstractMesh;
171
+ protected _getDebugMeshMesh(mesh: Mesh, scene: Scene): AbstractMesh;
172
+ protected _getDebugMesh(impostor: PhysicsImpostor, targetMesh?: Mesh): Nullable<AbstractMesh>;
175
173
  /**
176
174
  * Creates a debug mesh for a given physics body
177
175
  * @param body The physics body to create the debug mesh for
@@ -182,13 +180,13 @@ export declare class PhysicsViewer {
182
180
  * of the geometry of the body. The mesh is then assigned a debug material from the utility layer scene.
183
181
  * This allows for visualizing the physics body in the scene.
184
182
  */
185
- private _getDebugBodyMesh;
186
- private _getMeshDebugInertiaMatrixToRef;
187
- private _getDebugInertiaMesh;
188
- private _getTransformFromBodyToRef;
189
- private _createAngularConstraintMesh;
190
- private _createCage;
191
- private _getDebugConstraintMesh;
183
+ protected _getDebugBodyMesh(body: PhysicsBody): Nullable<AbstractMesh>;
184
+ protected _getMeshDebugInertiaMatrixToRef(massProps: PhysicsMassProperties, matrix: Matrix): Matrix;
185
+ protected _getDebugInertiaMesh(body: PhysicsBody): Nullable<AbstractMesh>;
186
+ protected _getTransformFromBodyToRef(body: PhysicsBody, matrix: Matrix, instanceIndex?: number): Matrix;
187
+ protected _createAngularConstraintMesh(minLimit: number, maxLimit: number, axisNumber: number, parent: TransformNode, scene: Scene): TransformNode;
188
+ protected _createCage(parent: TransformNode, scene: Scene): AbstractMesh;
189
+ protected _getDebugConstraintMesh(constraint: PhysicsConstraint): Nullable<Array<TransformNode>>;
192
190
  /**
193
191
  * Clean up physics debug display
194
192
  */
@@ -55,11 +55,6 @@ export class PhysicsViewer {
55
55
  this._debugMeshMeshes = new Array();
56
56
  this._constraintAxesSize = 0.4;
57
57
  this._constraintAngularSize = 0.4;
58
- this._localMatrixCache = Matrix.Identity();
59
- this._tempVectorCache = Vector3.Zero();
60
- this._inertiaMatrixCache = Matrix.Identity();
61
- this._transformMatrixCache = Matrix.Identity();
62
- this._finalMatrixCache = Matrix.Identity();
63
58
  this._scene = scene || EngineStore.LastCreatedScene;
64
59
  if (!this._scene) {
65
60
  return;
@@ -161,30 +156,33 @@ export class PhysicsViewer {
161
156
  }
162
157
  }
163
158
  _updateDebugInertia(body, inertiaMesh) {
159
+ const inertiaMatrixRef = Matrix.Identity();
160
+ const transformMatrixRef = Matrix.Identity();
161
+ const finalMatrixRef = Matrix.Identity();
164
162
  if (body._pluginDataInstances.length) {
165
163
  const inertiaAsMesh = inertiaMesh;
166
164
  const inertiaMeshMatrixData = inertiaAsMesh._thinInstanceDataStorage.matrixData;
167
165
  const bodyTransformMatrixData = body.transformNode._thinInstanceDataStorage.matrixData;
168
166
  for (let i = 0; i < body._pluginDataInstances.length; i++) {
169
167
  const props = body.getMassProperties(i);
170
- this._getMeshDebugInertiaMatrixToRef(props, this._inertiaMatrixCache);
171
- Matrix.FromArrayToRef(bodyTransformMatrixData, i * 16, this._transformMatrixCache);
172
- this._inertiaMatrixCache.multiplyToRef(this._transformMatrixCache, this._finalMatrixCache);
173
- this._finalMatrixCache.copyToArray(inertiaMeshMatrixData, i * 16);
168
+ this._getMeshDebugInertiaMatrixToRef(props, inertiaMatrixRef);
169
+ Matrix.FromArrayToRef(bodyTransformMatrixData, i * 16, transformMatrixRef);
170
+ inertiaMatrixRef.multiplyToRef(transformMatrixRef, finalMatrixRef);
171
+ finalMatrixRef.copyToArray(inertiaMeshMatrixData, i * 16);
174
172
  }
175
173
  inertiaAsMesh.thinInstanceBufferUpdated("matrix");
176
174
  }
177
175
  else {
178
176
  const props = body.getMassProperties();
179
- this._getMeshDebugInertiaMatrixToRef(props, this._inertiaMatrixCache);
180
- body.transformNode.rotationQuaternion?.toRotationMatrix(this._transformMatrixCache);
181
- this._transformMatrixCache.setTranslation(body.transformNode.position);
177
+ this._getMeshDebugInertiaMatrixToRef(props, inertiaMatrixRef);
178
+ body.transformNode.rotationQuaternion?.toRotationMatrix(transformMatrixRef);
179
+ transformMatrixRef.setTranslation(body.transformNode.position);
182
180
  if (body.transformNode.parent) {
183
181
  const parentTransform = body.transformNode.parent.computeWorldMatrix(true);
184
- this._transformMatrixCache.multiplyToRef(parentTransform, this._transformMatrixCache);
182
+ transformMatrixRef.multiplyToRef(parentTransform, transformMatrixRef);
185
183
  }
186
- this._inertiaMatrixCache.multiplyToRef(this._transformMatrixCache, this._inertiaMatrixCache);
187
- this._inertiaMatrixCache.decomposeToTransformNode(inertiaMesh);
184
+ inertiaMatrixRef.multiplyToRef(transformMatrixRef, inertiaMatrixRef);
185
+ inertiaMatrixRef.decomposeToTransformNode(inertiaMesh);
188
186
  }
189
187
  }
190
188
  _updateDebugConstraints() {
@@ -221,18 +219,11 @@ export class PhysicsViewer {
221
219
  if (!pivotA || !pivotB || !axisA || !axisB || !perpAxisA || !perpAxisB) {
222
220
  return;
223
221
  }
224
- const childNodes = parentingMesh.getChildMeshes(false);
225
- for (const parentConstraintMesh of childNodes) {
226
- if (!(parentConstraintMesh instanceof TransformNode)) {
227
- continue;
228
- }
229
- // Get the direct children of parentOfPair
230
- const directChildren = parentConstraintMesh.getChildren((node) => node instanceof TransformNode, true);
231
- const parentCoordSystemNode = directChildren.find((child) => child.name === "parentCoordSystem");
232
- const childCoordSystemNode = directChildren.find((child) => child.name === "childCoordSystem");
233
- if (!parentCoordSystemNode || !childCoordSystemNode) {
234
- continue;
235
- }
222
+ const descendants = parentingMesh.getDescendants(true);
223
+ for (const parentConstraintMesh of descendants) {
224
+ // Get the parent transform
225
+ const parentCoordSystemNode = parentConstraintMesh.getDescendants(true)[0];
226
+ const childCoordSystemNode = parentConstraintMesh.getDescendants(true)[1];
236
227
  const { parentBody, parentBodyIndex } = parentCoordSystemNode.metadata;
237
228
  const { childBody, childBodyIndex } = childCoordSystemNode.metadata;
238
229
  const parentTransform = this._getTransformFromBodyToRef(parentBody, TmpVectors.Matrix[0], parentBodyIndex);
@@ -242,56 +233,13 @@ export class PhysicsViewer {
242
233
  childTransform.decomposeToTransformNode(childCoordSystemNode);
243
234
  this._makeScalingUnitInPlace(childCoordSystemNode.scaling);
244
235
  // Create a transform node and set its matrix
245
- const parentTransformNodeChildren = parentCoordSystemNode.getChildren((node) => node instanceof TransformNode, true);
246
- const parentTransformNode = parentTransformNodeChildren[0];
236
+ const parentTransformNode = parentCoordSystemNode.getDescendants(true)[0];
247
237
  parentTransformNode.position.copyFrom(pivotA);
248
- const childTransformNodeChildren = childCoordSystemNode.getChildren((node) => node instanceof TransformNode, true);
249
- const childTransformNode = childTransformNodeChildren[0];
238
+ const childTransformNode = childCoordSystemNode.getDescendants(true)[0];
250
239
  childTransformNode.position.copyFrom(pivotB);
251
240
  // Get the transform to align the XYZ axes to the constraint axes
252
241
  Quaternion.FromRotationMatrixToRef(Matrix.FromXYZAxesToRef(axisA, perpAxisA, Vector3.CrossToRef(axisA, perpAxisA, TmpVectors.Vector3[0]), TmpVectors.Matrix[0]), parentTransformNode.rotationQuaternion);
253
242
  Quaternion.FromRotationMatrixToRef(Matrix.FromXYZAxesToRef(axisB, perpAxisB, Vector3.CrossToRef(axisB, perpAxisB, TmpVectors.Vector3[1]), TmpVectors.Matrix[1]), childTransformNode.rotationQuaternion);
254
- // Sync angular constraint meshes with child body transform
255
- const allDescendants = parentConstraintMesh.getDescendants(true);
256
- for (const descendant of allDescendants) {
257
- if (descendant.metadata?.childBody) {
258
- const { childBody, axisNumber } = descendant.metadata;
259
- if (childBody.transformNode) {
260
- const childWorldMatrix = childBody.transformNode.getWorldMatrix();
261
- const childWorldMatrixInverse = TmpVectors.Matrix[2];
262
- childWorldMatrix.invertToRef(childWorldMatrixInverse);
263
- childWorldMatrixInverse.multiplyToRef(childTransform, this._localMatrixCache);
264
- const position = TmpVectors.Vector3[0];
265
- const rotation = TmpVectors.Quaternion[0];
266
- const scaling = TmpVectors.Vector3[1];
267
- this._localMatrixCache.decompose(scaling, rotation, position);
268
- position.addToRef(pivotB, this._tempVectorCache);
269
- descendant.position.copyFrom(this._tempVectorCache);
270
- const worldRotation = TmpVectors.Quaternion[1];
271
- childBody.transformNode.getWorldMatrix().getRotationQuaternionToRef(worldRotation);
272
- worldRotation.multiplyToRef(descendant.rotationQuaternion, TmpVectors.Quaternion[2]);
273
- TmpVectors.Quaternion[2].copyFrom(descendant.rotationQuaternion);
274
- const parentScaling = childBody.transformNode.absoluteScaling;
275
- switch (axisNumber) {
276
- case 0:
277
- descendant.scaling.x = 1 / parentScaling.x;
278
- descendant.scaling.y = 1 / parentScaling.z;
279
- descendant.scaling.z = 1 / parentScaling.y;
280
- break;
281
- case 1:
282
- descendant.scaling.x = 1 / parentScaling.z;
283
- descendant.scaling.y = 1 / parentScaling.y;
284
- descendant.scaling.z = 1 / parentScaling.x;
285
- break;
286
- case 2:
287
- descendant.scaling.x = 1 / parentScaling.x;
288
- descendant.scaling.y = 1 / parentScaling.z;
289
- descendant.scaling.z = 1 / parentScaling.y;
290
- break;
291
- }
292
- }
293
- }
294
- }
295
243
  }
296
244
  }
297
245
  /**
@@ -533,7 +481,6 @@ export class PhysicsViewer {
533
481
  return;
534
482
  }
535
483
  let removed = false;
536
- const utilityLayerScene = this._utilityLayer.utilityLayerScene;
537
484
  for (let i = 0; i < this._numConstraints; i++) {
538
485
  if (this._constraints[i] === constraint) {
539
486
  const meshes = this._constraintMeshes[i];
@@ -541,7 +488,6 @@ export class PhysicsViewer {
541
488
  continue;
542
489
  }
543
490
  for (const mesh of meshes) {
544
- utilityLayerScene.removeMesh(mesh);
545
491
  mesh.dispose();
546
492
  }
547
493
  this._constraints.splice(i, 1);
@@ -809,25 +755,40 @@ export class PhysicsViewer {
809
755
  return matrix.copyFrom(tnode.getWorldMatrix());
810
756
  }
811
757
  }
812
- _createAngularConstraintMesh(minLimit, maxLimit, axisNumber, childBody, childBodyIndex, scene) {
758
+ _createAngularConstraintMesh(minLimit, maxLimit, axisNumber, parent, scene) {
813
759
  const arcAngle = (maxLimit - minLimit) / (Math.PI * 2);
814
760
  const mesh = MeshBuilder.CreateCylinder("ConstraintCylinder", { height: 0.0001, diameter: 3 * this._constraintAngularSize, arc: arcAngle }, scene);
761
+ const root = new TransformNode("angular_root", scene);
762
+ root.metadata = { bodyNode: parent };
815
763
  mesh.material = this._getDebugAxisColoredMaterial(axisNumber, scene);
816
- mesh.metadata = { childBody, childBodyIndex, axisNumber, minLimit };
817
- mesh.rotationQuaternion = Quaternion.Identity();
764
+ parent.getWorldMatrix().decomposeToTransformNode(root);
765
+ mesh.parent = root;
766
+ const parentScaling = parent.absoluteScaling;
818
767
  switch (axisNumber) {
819
768
  case 0:
820
769
  mesh.rotation.z = Math.PI * 0.5;
821
770
  mesh.rotation.x = -minLimit + Math.PI * 0.5;
771
+ // scaling on y,z
772
+ mesh.scaling.x = 1 / parentScaling.x;
773
+ mesh.scaling.y = 1 / parentScaling.z;
774
+ mesh.scaling.z = 1 / parentScaling.y;
822
775
  break;
823
776
  case 1:
824
777
  mesh.rotation.y = Math.PI * 1.5 + minLimit;
778
+ // flip x,z
779
+ mesh.scaling.x = 1 / parentScaling.z;
780
+ mesh.scaling.y = 1 / parentScaling.y;
781
+ mesh.scaling.z = 1 / parentScaling.x;
825
782
  break;
826
783
  case 2:
827
784
  mesh.rotation.x = Math.PI * 0.5;
785
+ // flip z,y
786
+ mesh.scaling.x = 1 / parentScaling.x;
787
+ mesh.scaling.y = 1 / parentScaling.z;
788
+ mesh.scaling.z = 1 / parentScaling.y;
828
789
  break;
829
790
  }
830
- return mesh;
791
+ return root;
831
792
  }
832
793
  _createCage(parent, scene) {
833
794
  const cage = MeshBuilder.CreateBox("cage", { size: 1 }, scene);
@@ -864,9 +825,6 @@ export class PhysicsViewer {
864
825
  // Create a mesh to keep the pair of constraint axes
865
826
  const parentOfPair = new TransformNode("parentOfPair", utilityLayerScene);
866
827
  parentOfPair.parent = parentingMesh;
867
- // Create a container for angular constraint meshes within this pair
868
- const angularConstraintContainer = new TransformNode("angularConstraintContainer", utilityLayerScene);
869
- angularConstraintContainer.parent = parentOfPair;
870
828
  const { parentBody, parentBodyIndex, childBody, childBodyIndex } = bodyPairInfo;
871
829
  // Get the parent transform
872
830
  const parentTransform = this._getTransformFromBodyToRef(parentBody, TmpVectors.Matrix[0], parentBodyIndex);
@@ -900,6 +858,7 @@ export class PhysicsViewer {
900
858
  // Create axes for the constraint
901
859
  const parentAxes = new AxesViewer(utilityLayerScene, this._constraintAxesSize);
902
860
  parentAxes.xAxis.parent = parentTransformNode;
861
+ // parentAxes.xAxis.isPi
903
862
  parentAxes.yAxis.parent = parentTransformNode;
904
863
  parentAxes.zAxis.parent = parentTransformNode;
905
864
  const childAxes = new AxesViewer(utilityLayerScene, this._constraintAxesSize);
@@ -957,8 +916,7 @@ export class PhysicsViewer {
957
916
  maxLimit = engine.getAxisMaxLimit(constraint, axis);
958
917
  }
959
918
  if (axisMode != 2 /* PhysicsConstraintAxisLimitMode.LOCKED */ && constraint.options.pivotB) {
960
- const mesh = this._createAngularConstraintMesh(minLimit, maxLimit, axisIndex, childBody, childBodyIndex, utilityLayerScene);
961
- mesh.parent = angularConstraintContainer;
919
+ const mesh = this._createAngularConstraintMesh(minLimit, maxLimit, axisIndex, childBody.transformNode, utilityLayerScene);
962
920
  mesh.position.copyFrom(constraint.options.pivotB);
963
921
  parentedConstraintMeshes.push(mesh);
964
922
  }