physicsViewer.d.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import type { Nullable } from "../types";
  2. import type { Scene } from "../scene";
  3. import type { AbstractMesh } from "../Meshes/abstractMesh";
  4. import { Mesh } from "../Meshes/mesh";
  5. import { Vector3 } from "../Maths/math.vector";
  6. import type { IPhysicsEnginePlugin as IPhysicsEnginePluginV1 } from "../Physics/v1/IPhysicsEnginePlugin";
  7. import type { IPhysicsEnginePluginV2 } from "../Physics/v2/IPhysicsEnginePlugin";
  8. import { PhysicsImpostor } from "../Physics/v1/physicsImpostor";
  9. import type { PhysicsBody } from "../Physics/v2/physicsBody";
  10. import type { PhysicsConstraint } from "../Physics/v2/physicsConstraint";
  11. /**
  12. * Used to show the physics impostor around the specific mesh
  13. */
  14. export declare class PhysicsViewer {
  15. /** @internal */
  16. protected _impostors: Array<Nullable<PhysicsImpostor>>;
  17. /** @internal */
  18. protected _meshes: Array<Nullable<AbstractMesh>>;
  19. /** @internal */
  20. protected _bodies: Array<Nullable<PhysicsBody>>;
  21. /** @internal */
  22. protected _inertiaBodies: Array<Nullable<PhysicsBody>>;
  23. /** @internal */
  24. protected _constraints: Array<Nullable<PhysicsConstraint>>;
  25. /** @internal */
  26. protected _bodyMeshes: Array<Nullable<AbstractMesh>>;
  27. /** @internal */
  28. protected _inertiaMeshes: Array<Nullable<AbstractMesh>>;
  29. /** @internal */
  30. protected _constraintMeshes: Array<Nullable<AbstractMesh>>;
  31. /** @internal */
  32. protected _scene: Nullable<Scene>;
  33. /** @internal */
  34. protected _numMeshes: number;
  35. /** @internal */
  36. protected _numBodies: number;
  37. /** @internal */
  38. protected _numInertiaBodies: number;
  39. /** @internal */
  40. protected _numConstraints: number;
  41. /** @internal */
  42. protected _physicsEnginePlugin: IPhysicsEnginePluginV1 | IPhysicsEnginePluginV2 | null;
  43. private _renderFunction;
  44. private _inertiaRenderFunction;
  45. private _constraintRenderFunction;
  46. private _utilityLayer;
  47. private _debugBoxMesh;
  48. private _debugSphereMesh;
  49. private _debugCapsuleMesh;
  50. private _debugCylinderMesh;
  51. private _debugMaterial;
  52. private _debugInertiaMaterial;
  53. private _debugMeshMeshes;
  54. private _constraintAxesSize;
  55. /**
  56. * Creates a new PhysicsViewer
  57. * @param scene defines the hosting scene
  58. */
  59. constructor(scene?: Scene);
  60. /**
  61. * Updates the debug meshes of the physics engine.
  62. *
  63. * This code is useful for synchronizing the debug meshes of the physics engine with the physics impostor and mesh.
  64. * It checks if the impostor is disposed and if the plugin version is 1, then it syncs the mesh with the impostor.
  65. * This ensures that the debug meshes are up to date with the physics engine.
  66. */
  67. protected _updateDebugMeshes(): void;
  68. /**
  69. * Updates the debug meshes of the physics engine.
  70. *
  71. * This method is useful for synchronizing the debug meshes with the physics impostors.
  72. * It iterates through the impostors and meshes, and if the plugin version is 1, it syncs the mesh with the impostor.
  73. * This ensures that the debug meshes accurately reflect the physics impostors, which is important for debugging the physics engine.
  74. */
  75. protected _updateDebugMeshesV1(): void;
  76. /**
  77. * Updates the debug meshes of the physics engine for V2 plugin.
  78. *
  79. * This method is useful for synchronizing the debug meshes of the physics engine with the current state of the bodies.
  80. * It iterates through the bodies array and updates the debug meshes with the current transform of each body.
  81. * This ensures that the debug meshes accurately reflect the current state of the physics engine.
  82. */
  83. protected _updateDebugMeshesV2(): void;
  84. protected _updateInertiaMeshes(): void;
  85. protected _updateDebugInertia(body: PhysicsBody, inertiaMesh: AbstractMesh): void;
  86. protected _updateDebugConstraints(): void;
  87. /**
  88. * Given a scaling vector, make all of its components
  89. * 1, preserving the sign
  90. * @param scaling
  91. */
  92. protected _makeScalingUnitInPlace(scaling: Vector3): void;
  93. protected _updateDebugConstraint(constraint: PhysicsConstraint, parentingMesh: AbstractMesh): void;
  94. /**
  95. * Renders a specified physic impostor
  96. * @param impostor defines the impostor to render
  97. * @param targetMesh defines the mesh represented by the impostor
  98. * @returns the new debug mesh used to render the impostor
  99. */
  100. showImpostor(impostor: PhysicsImpostor, targetMesh?: Mesh): Nullable<AbstractMesh>;
  101. /**
  102. * Shows a debug mesh for a given physics body.
  103. * @param body The physics body to show.
  104. * @returns The debug mesh, or null if the body is already shown.
  105. *
  106. * This function is useful for visualizing the physics body in the scene.
  107. * It creates a debug mesh for the given body and adds it to the scene.
  108. * It also registers a before render function to update the debug mesh position and rotation.
  109. */
  110. showBody(body: PhysicsBody): Nullable<AbstractMesh>;
  111. /**
  112. * Shows a debug box corresponding to the inertia of a given body
  113. * @param body the physics body used to get the inertia
  114. * @returns the debug mesh used to show the inertia, or null if the body is already shown
  115. */
  116. showInertia(body: PhysicsBody): Nullable<AbstractMesh>;
  117. /**
  118. * Shows a debug mesh for a given physics constraint.
  119. * @param constraint the physics constraint to show
  120. * @returns the debug mesh, or null if the constraint is already shown
  121. */
  122. showConstraint(constraint: PhysicsConstraint): Nullable<AbstractMesh>;
  123. /**
  124. * Hides an impostor from the scene.
  125. * @param impostor - The impostor to hide.
  126. *
  127. * This method is useful for hiding an impostor from the scene. It removes the
  128. * impostor from the utility layer scene, disposes the mesh, and removes the
  129. * impostor from the list of impostors. If the impostor is the last one in the
  130. * list, it also unregisters the render function.
  131. */
  132. hideImpostor(impostor: Nullable<PhysicsImpostor>): void;
  133. /**
  134. * Hides a body from the physics engine.
  135. * @param body - The body to hide.
  136. *
  137. * This function is useful for hiding a body from the physics engine.
  138. * It removes the body from the utility layer scene and disposes the mesh associated with it.
  139. * It also unregisters the render function if the number of bodies is 0.
  140. * This is useful for hiding a body from the physics engine without deleting it.
  141. */
  142. hideBody(body: Nullable<PhysicsBody>): void;
  143. /**
  144. * Hides a body's inertia from the viewer utility layer
  145. * @param body the body to hide
  146. */
  147. hideInertia(body: Nullable<PhysicsBody>): void;
  148. /**
  149. * Hide a physics constraint from the viewer utility layer
  150. * @param constraint the constraint to hide
  151. */
  152. hideConstraint(constraint: Nullable<PhysicsConstraint>): void;
  153. private _getDebugMaterial;
  154. private _getDebugInertiaMaterial;
  155. private _getDebugBoxMesh;
  156. private _getDebugSphereMesh;
  157. private _getDebugCapsuleMesh;
  158. private _getDebugCylinderMesh;
  159. private _getDebugMeshMesh;
  160. private _getDebugMesh;
  161. /**
  162. * Creates a debug mesh for a given physics body
  163. * @param body The physics body to create the debug mesh for
  164. * @returns The created debug mesh or null if the utility layer is not available
  165. *
  166. * This code is useful for creating a debug mesh for a given physics body.
  167. * It creates a Mesh object with a VertexData object containing the positions and indices
  168. * of the geometry of the body. The mesh is then assigned a debug material from the utility layer scene.
  169. * This allows for visualizing the physics body in the scene.
  170. */
  171. private _getDebugBodyMesh;
  172. private _getMeshDebugInertiaMatrixToRef;
  173. private _getDebugInertiaMesh;
  174. private _getTransformFromBodyToRef;
  175. private _getDebugConstraintMesh;
  176. /**
  177. * Clean up physics debug display
  178. */
  179. dispose(): void;
  180. }