@babylonjs/core 5.35.0 → 5.36.0

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.
Files changed (250) hide show
  1. package/Audio/sound.d.ts +5 -0
  2. package/Audio/sound.js +7 -0
  3. package/Audio/sound.js.map +1 -1
  4. package/Debug/physicsViewer.d.ts +4 -3
  5. package/Debug/physicsViewer.js +2 -2
  6. package/Debug/physicsViewer.js.map +1 -1
  7. package/Engines/Extensions/engine.renderTarget.js +15 -15
  8. package/Engines/Extensions/engine.renderTarget.js.map +1 -1
  9. package/Engines/Extensions/engine.renderTargetCube.d.ts +1 -1
  10. package/Engines/Extensions/engine.renderTargetCube.js.map +1 -1
  11. package/Engines/Extensions/engine.views.d.ts +4 -0
  12. package/Engines/Extensions/engine.views.js +61 -42
  13. package/Engines/Extensions/engine.views.js.map +1 -1
  14. package/Engines/Native/nativeHardwareTexture.d.ts +14 -0
  15. package/Engines/Native/nativeHardwareTexture.js +24 -0
  16. package/Engines/Native/nativeHardwareTexture.js.map +1 -0
  17. package/Engines/Native/nativeInterfaces.d.ts +22 -15
  18. package/Engines/Native/nativeInterfaces.js.map +1 -1
  19. package/Engines/Native/nativePipelineContext.d.ts +252 -0
  20. package/Engines/Native/nativePipelineContext.js +502 -0
  21. package/Engines/Native/nativePipelineContext.js.map +1 -0
  22. package/Engines/Native/nativeRenderTargetWrapper.d.ts +16 -0
  23. package/Engines/Native/nativeRenderTargetWrapper.js +33 -0
  24. package/Engines/Native/nativeRenderTargetWrapper.js.map +1 -0
  25. package/Engines/WebGPU/Extensions/engine.renderTarget.js +3 -3
  26. package/Engines/WebGPU/Extensions/engine.renderTarget.js.map +1 -1
  27. package/Engines/WebGPU/Extensions/engine.renderTargetCube.js.map +1 -1
  28. package/Engines/engine.d.ts +1 -0
  29. package/Engines/engine.js +24 -18
  30. package/Engines/engine.js.map +1 -1
  31. package/Engines/nativeEngine.d.ts +18 -17
  32. package/Engines/nativeEngine.js +100 -598
  33. package/Engines/nativeEngine.js.map +1 -1
  34. package/Engines/nullEngine.d.ts +1 -1
  35. package/Engines/nullEngine.js.map +1 -1
  36. package/Engines/thinEngine.d.ts +1 -1
  37. package/Engines/thinEngine.js +41 -38
  38. package/Engines/thinEngine.js.map +1 -1
  39. package/Engines/webgpuEngine.js.map +1 -1
  40. package/Gizmos/planeRotationGizmo.js +3 -1
  41. package/Gizmos/planeRotationGizmo.js.map +1 -1
  42. package/Inputs/scene.inputManager.js +4 -4
  43. package/Inputs/scene.inputManager.js.map +1 -1
  44. package/Loading/Plugins/babylonFileLoader.js +3 -3
  45. package/Loading/Plugins/babylonFileLoader.js.map +1 -1
  46. package/Materials/Node/Blocks/Dual/textureBlock.js +1 -1
  47. package/Materials/Node/Blocks/Dual/textureBlock.js.map +1 -1
  48. package/Materials/Node/nodeMaterial.d.ts +19 -1
  49. package/Materials/Node/nodeMaterial.js +30 -0
  50. package/Materials/Node/nodeMaterial.js.map +1 -1
  51. package/Materials/Node/nodeMaterialBuildStateSharedData.d.ts +2 -8
  52. package/Materials/Node/nodeMaterialBuildStateSharedData.js.map +1 -1
  53. package/Materials/Textures/baseTexture.d.ts +2 -1
  54. package/Materials/Textures/baseTexture.js +3 -2
  55. package/Materials/Textures/baseTexture.js.map +1 -1
  56. package/Materials/Textures/internalTexture.d.ts +2 -0
  57. package/Materials/Textures/internalTexture.js +4 -0
  58. package/Materials/Textures/internalTexture.js.map +1 -1
  59. package/Materials/Textures/mirrorTexture.js +0 -2
  60. package/Materials/Textures/mirrorTexture.js.map +1 -1
  61. package/Materials/Textures/renderTargetTexture.d.ts +53 -3
  62. package/Materials/Textures/renderTargetTexture.js +25 -25
  63. package/Materials/Textures/renderTargetTexture.js.map +1 -1
  64. package/Materials/Textures/texture.d.ts +2 -0
  65. package/Materials/Textures/texture.js +35 -7
  66. package/Materials/Textures/texture.js.map +1 -1
  67. package/Materials/Textures/textureCreationOptions.d.ts +4 -1
  68. package/Materials/Textures/textureCreationOptions.js.map +1 -1
  69. package/Materials/material.js +1 -1
  70. package/Materials/material.js.map +1 -1
  71. package/Meshes/abstractMesh.d.ts +4 -4
  72. package/Meshes/abstractMesh.js +4 -4
  73. package/Meshes/abstractMesh.js.map +1 -1
  74. package/Meshes/mesh.d.ts +2 -2
  75. package/Meshes/mesh.js +1 -1
  76. package/Meshes/mesh.js.map +1 -1
  77. package/Misc/copyTextureToTexture.d.ts +9 -2
  78. package/Misc/copyTextureToTexture.js +23 -1
  79. package/Misc/copyTextureToTexture.js.map +1 -1
  80. package/Particles/IParticleSystem.d.ts +15 -1
  81. package/Particles/IParticleSystem.js.map +1 -1
  82. package/Particles/gpuParticleSystem.d.ts +24 -2
  83. package/Particles/gpuParticleSystem.js +98 -43
  84. package/Particles/gpuParticleSystem.js.map +1 -1
  85. package/Particles/particleSystem.d.ts +6 -1
  86. package/Particles/particleSystem.js +7 -2
  87. package/Particles/particleSystem.js.map +1 -1
  88. package/Physics/IPhysicsEngine.d.ts +8 -108
  89. package/Physics/IPhysicsEngine.js.map +1 -1
  90. package/Physics/Plugins/ammoJSPlugin.d.ts +1 -389
  91. package/Physics/Plugins/ammoJSPlugin.js +2 -1414
  92. package/Physics/Plugins/ammoJSPlugin.js.map +1 -1
  93. package/Physics/Plugins/cannonJSPlugin.d.ts +1 -75
  94. package/Physics/Plugins/cannonJSPlugin.js +2 -664
  95. package/Physics/Plugins/cannonJSPlugin.js.map +1 -1
  96. package/Physics/Plugins/oimoJSPlugin.d.ts +1 -59
  97. package/Physics/Plugins/oimoJSPlugin.js +2 -432
  98. package/Physics/Plugins/oimoJSPlugin.js.map +1 -1
  99. package/Physics/index.d.ts +3 -6
  100. package/Physics/index.js +3 -6
  101. package/Physics/index.js.map +1 -1
  102. package/Physics/physicsEngine.d.ts +1 -135
  103. package/Physics/physicsEngine.js +2 -221
  104. package/Physics/physicsEngine.js.map +1 -1
  105. package/Physics/physicsEngineComponent.d.ts +7 -42
  106. package/Physics/physicsEngineComponent.js +11 -71
  107. package/Physics/physicsEngineComponent.js.map +1 -1
  108. package/Physics/physicsHelper.d.ts +1 -411
  109. package/Physics/physicsHelper.js +2 -708
  110. package/Physics/physicsHelper.js.map +1 -1
  111. package/Physics/physicsImpostor.d.ts +1 -640
  112. package/Physics/physicsImpostor.js +2 -1004
  113. package/Physics/physicsImpostor.js.map +1 -1
  114. package/Physics/physicsJoint.d.ts +1 -275
  115. package/Physics/physicsJoint.js +2 -222
  116. package/Physics/physicsJoint.js.map +1 -1
  117. package/Physics/v1/IPhysicsEnginePlugin.d.ts +73 -0
  118. package/Physics/v1/IPhysicsEnginePlugin.js +2 -0
  119. package/Physics/v1/IPhysicsEnginePlugin.js.map +1 -0
  120. package/Physics/v1/Plugins/ammoJSPlugin.d.ts +401 -0
  121. package/Physics/v1/Plugins/ammoJSPlugin.js +1431 -0
  122. package/Physics/v1/Plugins/ammoJSPlugin.js.map +1 -0
  123. package/Physics/v1/Plugins/cannonJSPlugin.d.ts +87 -0
  124. package/Physics/v1/Plugins/cannonJSPlugin.js +683 -0
  125. package/Physics/v1/Plugins/cannonJSPlugin.js.map +1 -0
  126. package/Physics/{Plugins → v1/Plugins}/index.d.ts +0 -0
  127. package/Physics/{Plugins → v1/Plugins}/index.js +0 -0
  128. package/Physics/v1/Plugins/index.js.map +1 -0
  129. package/Physics/v1/Plugins/oimoJSPlugin.d.ts +71 -0
  130. package/Physics/v1/Plugins/oimoJSPlugin.js +450 -0
  131. package/Physics/v1/Plugins/oimoJSPlugin.js.map +1 -0
  132. package/Physics/v1/index.d.ts +7 -0
  133. package/Physics/v1/index.js +9 -0
  134. package/Physics/v1/index.js.map +1 -0
  135. package/Physics/v1/physicsEngine.d.ts +147 -0
  136. package/Physics/v1/physicsEngine.js +237 -0
  137. package/Physics/v1/physicsEngine.js.map +1 -0
  138. package/Physics/v1/physicsEngineComponent.d.ts +45 -0
  139. package/Physics/v1/physicsEngineComponent.js +70 -0
  140. package/Physics/v1/physicsEngineComponent.js.map +1 -0
  141. package/Physics/v1/physicsHelper.d.ts +411 -0
  142. package/Physics/v1/physicsHelper.js +709 -0
  143. package/Physics/v1/physicsHelper.js.map +1 -0
  144. package/Physics/v1/physicsImpostor.d.ts +636 -0
  145. package/Physics/v1/physicsImpostor.js +1001 -0
  146. package/Physics/v1/physicsImpostor.js.map +1 -0
  147. package/Physics/v1/physicsJoint.d.ts +275 -0
  148. package/Physics/v1/physicsJoint.js +223 -0
  149. package/Physics/v1/physicsJoint.js.map +1 -0
  150. package/Physics/v2/IPhysicsEnginePlugin.d.ts +170 -0
  151. package/Physics/v2/IPhysicsEnginePlugin.js +47 -0
  152. package/Physics/v2/IPhysicsEnginePlugin.js.map +1 -0
  153. package/Physics/v2/Plugins/index.d.ts +0 -0
  154. package/Physics/v2/Plugins/index.js +2 -0
  155. package/Physics/v2/Plugins/index.js.map +1 -0
  156. package/Physics/v2/index.d.ts +6 -0
  157. package/Physics/v2/index.js +8 -0
  158. package/Physics/v2/index.js.map +1 -0
  159. package/Physics/v2/physicsAggregate.d.ts +114 -0
  160. package/Physics/v2/physicsAggregate.js +46 -0
  161. package/Physics/v2/physicsAggregate.js.map +1 -0
  162. package/Physics/v2/physicsBody.d.ts +109 -0
  163. package/Physics/v2/physicsBody.js +158 -0
  164. package/Physics/v2/physicsBody.js.map +1 -0
  165. package/Physics/v2/physicsConstraint.d.ts +184 -0
  166. package/Physics/v2/physicsConstraint.js +257 -0
  167. package/Physics/v2/physicsConstraint.js.map +1 -0
  168. package/Physics/v2/physicsEngine.d.ts +103 -0
  169. package/Physics/v2/physicsEngine.js +146 -0
  170. package/Physics/v2/physicsEngine.js.map +1 -0
  171. package/Physics/v2/physicsEngineComponent.d.ts +31 -0
  172. package/Physics/v2/physicsEngineComponent.js +50 -0
  173. package/Physics/v2/physicsEngineComponent.js.map +1 -0
  174. package/Physics/v2/physicsMaterial.d.ts +45 -0
  175. package/Physics/v2/physicsMaterial.js +67 -0
  176. package/Physics/v2/physicsMaterial.js.map +1 -0
  177. package/Physics/v2/physicsShape.d.ts +182 -0
  178. package/Physics/v2/physicsShape.js +229 -0
  179. package/Physics/v2/physicsShape.js.map +1 -0
  180. package/PostProcesses/postProcess.d.ts +3 -1
  181. package/PostProcesses/postProcess.js +4 -1
  182. package/PostProcesses/postProcess.js.map +1 -1
  183. package/Rendering/fluidRenderer/fluidRenderer.d.ts +153 -0
  184. package/Rendering/fluidRenderer/fluidRenderer.js +410 -0
  185. package/Rendering/fluidRenderer/fluidRenderer.js.map +1 -0
  186. package/Rendering/fluidRenderer/fluidRenderingDepthTextureCopy.d.ts +13 -0
  187. package/Rendering/fluidRenderer/fluidRenderingDepthTextureCopy.js +31 -0
  188. package/Rendering/fluidRenderer/fluidRenderingDepthTextureCopy.js.map +1 -0
  189. package/Rendering/fluidRenderer/fluidRenderingObject.d.ts +81 -0
  190. package/Rendering/fluidRenderer/fluidRenderingObject.js +180 -0
  191. package/Rendering/fluidRenderer/fluidRenderingObject.js.map +1 -0
  192. package/Rendering/fluidRenderer/fluidRenderingObjectCustomParticles.d.ts +63 -0
  193. package/Rendering/fluidRenderer/fluidRenderingObjectCustomParticles.js +130 -0
  194. package/Rendering/fluidRenderer/fluidRenderingObjectCustomParticles.js.map +1 -0
  195. package/Rendering/fluidRenderer/fluidRenderingObjectParticleSystem.d.ts +64 -0
  196. package/Rendering/fluidRenderer/fluidRenderingObjectParticleSystem.js +104 -0
  197. package/Rendering/fluidRenderer/fluidRenderingObjectParticleSystem.js.map +1 -0
  198. package/Rendering/fluidRenderer/fluidRenderingTargetRenderer.d.ts +234 -0
  199. package/Rendering/fluidRenderer/fluidRenderingTargetRenderer.js +690 -0
  200. package/Rendering/fluidRenderer/fluidRenderingTargetRenderer.js.map +1 -0
  201. package/Rendering/fluidRenderer/fluidRenderingTextures.d.ts +56 -0
  202. package/Rendering/fluidRenderer/fluidRenderingTextures.js +261 -0
  203. package/Rendering/fluidRenderer/fluidRenderingTextures.js.map +1 -0
  204. package/Rendering/fluidRenderer/index.d.ts +5 -0
  205. package/Rendering/fluidRenderer/index.js +6 -0
  206. package/Rendering/fluidRenderer/index.js.map +1 -0
  207. package/Rendering/index.d.ts +1 -0
  208. package/Rendering/index.js +2 -0
  209. package/Rendering/index.js.map +1 -1
  210. package/Shaders/copyTextureToTexture.fragment.js +4 -1
  211. package/Shaders/copyTextureToTexture.fragment.js.map +1 -1
  212. package/Shaders/fluidRenderingBilateralBlur.fragment.d.ts +5 -0
  213. package/Shaders/fluidRenderingBilateralBlur.fragment.js +9 -0
  214. package/Shaders/fluidRenderingBilateralBlur.fragment.js.map +1 -0
  215. package/Shaders/fluidRenderingParticleDepth.fragment.d.ts +5 -0
  216. package/Shaders/fluidRenderingParticleDepth.fragment.js +17 -0
  217. package/Shaders/fluidRenderingParticleDepth.fragment.js.map +1 -0
  218. package/Shaders/fluidRenderingParticleDepth.vertex.d.ts +5 -0
  219. package/Shaders/fluidRenderingParticleDepth.vertex.js +13 -0
  220. package/Shaders/fluidRenderingParticleDepth.vertex.js.map +1 -0
  221. package/Shaders/fluidRenderingParticleDiffuse.fragment.d.ts +5 -0
  222. package/Shaders/fluidRenderingParticleDiffuse.fragment.js +9 -0
  223. package/Shaders/fluidRenderingParticleDiffuse.fragment.js.map +1 -0
  224. package/Shaders/fluidRenderingParticleDiffuse.vertex.d.ts +5 -0
  225. package/Shaders/fluidRenderingParticleDiffuse.vertex.js +4 -0
  226. package/Shaders/fluidRenderingParticleDiffuse.vertex.js.map +1 -0
  227. package/Shaders/fluidRenderingParticleThickness.fragment.d.ts +5 -0
  228. package/Shaders/fluidRenderingParticleThickness.fragment.js +9 -0
  229. package/Shaders/fluidRenderingParticleThickness.fragment.js.map +1 -0
  230. package/Shaders/fluidRenderingParticleThickness.vertex.d.ts +5 -0
  231. package/Shaders/fluidRenderingParticleThickness.vertex.js +9 -0
  232. package/Shaders/fluidRenderingParticleThickness.vertex.js.map +1 -0
  233. package/Shaders/fluidRenderingRender.fragment.d.ts +5 -0
  234. package/Shaders/fluidRenderingRender.fragment.js +46 -0
  235. package/Shaders/fluidRenderingRender.fragment.js.map +1 -0
  236. package/Shaders/fluidRenderingStandardBlur.fragment.d.ts +5 -0
  237. package/Shaders/fluidRenderingStandardBlur.fragment.js +9 -0
  238. package/Shaders/fluidRenderingStandardBlur.fragment.js.map +1 -0
  239. package/XR/features/WebXRControllerPhysics.d.ts +1 -1
  240. package/XR/features/WebXRControllerPhysics.js +1 -1
  241. package/XR/features/WebXRControllerPhysics.js.map +1 -1
  242. package/XR/features/WebXRHandTracking.js +1 -1
  243. package/XR/features/WebXRHandTracking.js.map +1 -1
  244. package/package.json +1 -1
  245. package/scene.js +6 -1
  246. package/scene.js.map +1 -1
  247. package/sceneComponent.d.ts +3 -0
  248. package/sceneComponent.js +3 -0
  249. package/sceneComponent.js.map +1 -1
  250. package/Physics/Plugins/index.js.map +0 -1
@@ -1,1415 +1,3 @@
1
- import { Quaternion, Vector3, Matrix } from "../../Maths/math.vector.js";
2
- import { Logger } from "../../Misc/logger.js";
3
- import { PhysicsImpostor } from "../../Physics/physicsImpostor.js";
4
- import { PhysicsJoint } from "../../Physics/physicsJoint.js";
5
- import { VertexBuffer } from "../../Buffers/buffer.js";
6
- import { VertexData } from "../../Meshes/mesh.vertexData.js";
7
- import { ExtrudeShape } from "../../Meshes/Builders/shapeBuilder.js";
8
- import { CreateLines } from "../../Meshes/Builders/linesBuilder.js";
9
- import { PhysicsRaycastResult } from "../physicsRaycastResult.js";
10
- import { Scalar } from "../../Maths/math.scalar.js";
11
- import { Epsilon } from "../../Maths/math.constants.js";
12
- /**
13
- * AmmoJS Physics plugin
14
- * @see https://doc.babylonjs.com/features/featuresDeepDive/physics/usingPhysicsEngine
15
- * @see https://github.com/kripken/ammo.js/
16
- */
17
- export class AmmoJSPlugin {
18
- /**
19
- * Initializes the ammoJS plugin
20
- * @param _useDeltaForWorldStep if the time between frames should be used when calculating physics steps (Default: true)
21
- * @param ammoInjection can be used to inject your own ammo reference
22
- * @param overlappingPairCache can be used to specify your own overlapping pair cache
23
- */
24
- constructor(_useDeltaForWorldStep = true, ammoInjection = Ammo, overlappingPairCache = null) {
25
- this._useDeltaForWorldStep = _useDeltaForWorldStep;
26
- /**
27
- * Reference to the Ammo library
28
- */
29
- this.bjsAMMO = {};
30
- /**
31
- * Name of the plugin
32
- */
33
- this.name = "AmmoJSPlugin";
34
- this._timeStep = 1 / 60;
35
- this._fixedTimeStep = 1 / 60;
36
- this._maxSteps = 5;
37
- this._tmpQuaternion = new Quaternion();
38
- this._tmpContactCallbackResult = false;
39
- this._tmpContactPoint = new Vector3();
40
- this._tmpContactNormal = new Vector3();
41
- this._tmpVec3 = new Vector3();
42
- this._tmpMatrix = new Matrix();
43
- if (typeof ammoInjection === "function") {
44
- Logger.Error("AmmoJS is not ready. Please make sure you await Ammo() before using the plugin.");
45
- return;
46
- }
47
- else {
48
- this.bjsAMMO = ammoInjection;
49
- }
50
- if (!this.isSupported()) {
51
- Logger.Error("AmmoJS is not available. Please make sure you included the js file.");
52
- return;
53
- }
54
- // Initialize the physics world
55
- this._collisionConfiguration = new this.bjsAMMO.btSoftBodyRigidBodyCollisionConfiguration();
56
- this._dispatcher = new this.bjsAMMO.btCollisionDispatcher(this._collisionConfiguration);
57
- this._overlappingPairCache = overlappingPairCache || new this.bjsAMMO.btDbvtBroadphase();
58
- this._solver = new this.bjsAMMO.btSequentialImpulseConstraintSolver();
59
- this._softBodySolver = new this.bjsAMMO.btDefaultSoftBodySolver();
60
- this.world = new this.bjsAMMO.btSoftRigidDynamicsWorld(this._dispatcher, this._overlappingPairCache, this._solver, this._collisionConfiguration, this._softBodySolver);
61
- this._tmpAmmoConcreteContactResultCallback = new this.bjsAMMO.ConcreteContactResultCallback();
62
- this._tmpAmmoConcreteContactResultCallback.addSingleResult = (contactPoint) => {
63
- contactPoint = this.bjsAMMO.wrapPointer(contactPoint, this.bjsAMMO.btManifoldPoint);
64
- const worldPoint = contactPoint.getPositionWorldOnA();
65
- const worldNormal = contactPoint.m_normalWorldOnB;
66
- this._tmpContactPoint.x = worldPoint.x();
67
- this._tmpContactPoint.y = worldPoint.y();
68
- this._tmpContactPoint.z = worldPoint.z();
69
- this._tmpContactNormal.x = worldNormal.x();
70
- this._tmpContactNormal.y = worldNormal.y();
71
- this._tmpContactNormal.z = worldNormal.z();
72
- this._tmpContactImpulse = contactPoint.getAppliedImpulse();
73
- this._tmpContactDistance = contactPoint.getDistance();
74
- this._tmpContactCallbackResult = true;
75
- };
76
- this._raycastResult = new PhysicsRaycastResult();
77
- // Create temp ammo variables
78
- this._tmpAmmoTransform = new this.bjsAMMO.btTransform();
79
- this._tmpAmmoTransform.setIdentity();
80
- this._tmpAmmoQuaternion = new this.bjsAMMO.btQuaternion(0, 0, 0, 1);
81
- this._tmpAmmoVectorA = new this.bjsAMMO.btVector3(0, 0, 0);
82
- this._tmpAmmoVectorB = new this.bjsAMMO.btVector3(0, 0, 0);
83
- this._tmpAmmoVectorC = new this.bjsAMMO.btVector3(0, 0, 0);
84
- this._tmpAmmoVectorD = new this.bjsAMMO.btVector3(0, 0, 0);
85
- }
86
- /**
87
- * Sets the gravity of the physics world (m/(s^2))
88
- * @param gravity Gravity to set
89
- */
90
- setGravity(gravity) {
91
- this._tmpAmmoVectorA.setValue(gravity.x, gravity.y, gravity.z);
92
- this.world.setGravity(this._tmpAmmoVectorA);
93
- this.world.getWorldInfo().set_m_gravity(this._tmpAmmoVectorA);
94
- }
95
- /**
96
- * Amount of time to step forward on each frame (only used if useDeltaForWorldStep is false in the constructor)
97
- * @param timeStep timestep to use in seconds
98
- */
99
- setTimeStep(timeStep) {
100
- this._timeStep = timeStep;
101
- }
102
- /**
103
- * Increment to step forward in the physics engine (If timeStep is set to 1/60 and fixedTimeStep is set to 1/120 the physics engine should run 2 steps per frame) (Default: 1/60)
104
- * @param fixedTimeStep fixedTimeStep to use in seconds
105
- */
106
- setFixedTimeStep(fixedTimeStep) {
107
- this._fixedTimeStep = fixedTimeStep;
108
- }
109
- /**
110
- * Sets the maximum number of steps by the physics engine per frame (Default: 5)
111
- * @param maxSteps the maximum number of steps by the physics engine per frame
112
- */
113
- setMaxSteps(maxSteps) {
114
- this._maxSteps = maxSteps;
115
- }
116
- /**
117
- * Gets the current timestep (only used if useDeltaForWorldStep is false in the constructor)
118
- * @returns the current timestep in seconds
119
- */
120
- getTimeStep() {
121
- return this._timeStep;
122
- }
123
- // Ammo's contactTest and contactPairTest take a callback that runs synchronously, wrap them so that they are easier to consume
124
- _isImpostorInContact(impostor) {
125
- this._tmpContactCallbackResult = false;
126
- this.world.contactTest(impostor.physicsBody, this._tmpAmmoConcreteContactResultCallback);
127
- return this._tmpContactCallbackResult;
128
- }
129
- // Ammo's collision events have some weird quirks
130
- // contactPairTest fires too many events as it fires events even when objects are close together but contactTest does not
131
- // so only fire event if both contactTest and contactPairTest have a hit
132
- _isImpostorPairInContact(impostorA, impostorB) {
133
- this._tmpContactCallbackResult = false;
134
- this.world.contactPairTest(impostorA.physicsBody, impostorB.physicsBody, this._tmpAmmoConcreteContactResultCallback);
135
- return this._tmpContactCallbackResult;
136
- }
137
- // Ammo's behavior when maxSteps > 0 does not behave as described in docs
138
- // @see http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
139
- //
140
- // When maxSteps is 0 do the entire simulation in one step
141
- // When maxSteps is > 0, run up to maxStep times, if on the last step the (remaining step - fixedTimeStep) is < fixedTimeStep, the remainder will be used for the step. (eg. if remainder is 1.001 and fixedTimeStep is 1 the last step will be 1.001, if instead it did 2 steps (1, 0.001) issues occuered when having a tiny step in ammo)
142
- // Note: To get deterministic physics, timeStep would always need to be divisible by fixedTimeStep
143
- _stepSimulation(timeStep = 1 / 60, maxSteps = 10, fixedTimeStep = 1 / 60) {
144
- if (maxSteps == 0) {
145
- this.world.stepSimulation(timeStep, 0);
146
- }
147
- else {
148
- while (maxSteps > 0 && timeStep > 0) {
149
- if (timeStep - fixedTimeStep < fixedTimeStep) {
150
- this.world.stepSimulation(timeStep, 0);
151
- timeStep = 0;
152
- }
153
- else {
154
- timeStep -= fixedTimeStep;
155
- this.world.stepSimulation(fixedTimeStep, 0);
156
- }
157
- maxSteps--;
158
- }
159
- }
160
- }
161
- /**
162
- * Moves the physics simulation forward delta seconds and updates the given physics imposters
163
- * Prior to the step the imposters physics location is set to the position of the babylon meshes
164
- * After the step the babylon meshes are set to the position of the physics imposters
165
- * @param delta amount of time to step forward
166
- * @param impostors array of imposters to update before/after the step
167
- */
168
- executeStep(delta, impostors) {
169
- for (const impostor of impostors) {
170
- // Update physics world objects to match babylon world
171
- if (!impostor.soft) {
172
- impostor.beforeStep();
173
- }
174
- }
175
- this._stepSimulation(this._useDeltaForWorldStep ? delta : this._timeStep, this._maxSteps, this._fixedTimeStep);
176
- for (const mainImpostor of impostors) {
177
- // After physics update make babylon world objects match physics world objects
178
- if (mainImpostor.soft) {
179
- this._afterSoftStep(mainImpostor);
180
- }
181
- else {
182
- mainImpostor.afterStep();
183
- }
184
- // Handle collision event
185
- if (mainImpostor._onPhysicsCollideCallbacks.length > 0) {
186
- if (this._isImpostorInContact(mainImpostor)) {
187
- for (const collideCallback of mainImpostor._onPhysicsCollideCallbacks) {
188
- for (const otherImpostor of collideCallback.otherImpostors) {
189
- if (mainImpostor.physicsBody.isActive() || otherImpostor.physicsBody.isActive()) {
190
- if (this._isImpostorPairInContact(mainImpostor, otherImpostor)) {
191
- mainImpostor.onCollide({
192
- body: otherImpostor.physicsBody,
193
- point: this._tmpContactPoint,
194
- distance: this._tmpContactDistance,
195
- impulse: this._tmpContactImpulse,
196
- normal: this._tmpContactNormal,
197
- });
198
- otherImpostor.onCollide({
199
- body: mainImpostor.physicsBody,
200
- point: this._tmpContactPoint,
201
- distance: this._tmpContactDistance,
202
- impulse: this._tmpContactImpulse,
203
- normal: this._tmpContactNormal,
204
- });
205
- }
206
- }
207
- }
208
- }
209
- }
210
- }
211
- }
212
- }
213
- /**
214
- * Update babylon mesh to match physics world object
215
- * @param impostor imposter to match
216
- */
217
- _afterSoftStep(impostor) {
218
- if (impostor.type === PhysicsImpostor.RopeImpostor) {
219
- this._ropeStep(impostor);
220
- }
221
- else {
222
- this._softbodyOrClothStep(impostor);
223
- }
224
- }
225
- /**
226
- * Update babylon mesh vertices vertices to match physics world softbody or cloth
227
- * @param impostor imposter to match
228
- */
229
- _ropeStep(impostor) {
230
- const bodyVertices = impostor.physicsBody.get_m_nodes();
231
- const nbVertices = bodyVertices.size();
232
- let node;
233
- let nodePositions;
234
- let x, y, z;
235
- const path = new Array();
236
- for (let n = 0; n < nbVertices; n++) {
237
- node = bodyVertices.at(n);
238
- nodePositions = node.get_m_x();
239
- x = nodePositions.x();
240
- y = nodePositions.y();
241
- z = nodePositions.z();
242
- path.push(new Vector3(x, y, z));
243
- }
244
- const object = impostor.object;
245
- const shape = impostor.getParam("shape");
246
- if (impostor._isFromLine) {
247
- impostor.object = CreateLines("lines", { points: path, instance: object });
248
- }
249
- else {
250
- impostor.object = ExtrudeShape("ext", { shape: shape, path: path, instance: object });
251
- }
252
- }
253
- /**
254
- * Update babylon mesh vertices vertices to match physics world softbody or cloth
255
- * @param impostor imposter to match
256
- */
257
- _softbodyOrClothStep(impostor) {
258
- const normalDirection = impostor.type === PhysicsImpostor.ClothImpostor ? 1 : -1;
259
- const object = impostor.object;
260
- let vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
261
- if (!vertexPositions) {
262
- vertexPositions = [];
263
- }
264
- let vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
265
- if (!vertexNormals) {
266
- vertexNormals = [];
267
- }
268
- const nbVertices = vertexPositions.length / 3;
269
- const bodyVertices = impostor.physicsBody.get_m_nodes();
270
- let node;
271
- let nodePositions;
272
- let x, y, z;
273
- let nx, ny, nz;
274
- for (let n = 0; n < nbVertices; n++) {
275
- node = bodyVertices.at(n);
276
- nodePositions = node.get_m_x();
277
- x = nodePositions.x();
278
- y = nodePositions.y();
279
- z = nodePositions.z() * normalDirection;
280
- const nodeNormals = node.get_m_n();
281
- nx = nodeNormals.x();
282
- ny = nodeNormals.y();
283
- nz = nodeNormals.z() * normalDirection;
284
- vertexPositions[3 * n] = x;
285
- vertexPositions[3 * n + 1] = y;
286
- vertexPositions[3 * n + 2] = z;
287
- vertexNormals[3 * n] = nx;
288
- vertexNormals[3 * n + 1] = ny;
289
- vertexNormals[3 * n + 2] = nz;
290
- }
291
- const vertex_data = new VertexData();
292
- vertex_data.positions = vertexPositions;
293
- vertex_data.normals = vertexNormals;
294
- vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
295
- vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
296
- if (object && object.getIndices) {
297
- vertex_data.indices = object.getIndices();
298
- }
299
- vertex_data.applyToMesh(object);
300
- }
301
- /**
302
- * Applies an impulse on the imposter
303
- * @param impostor imposter to apply impulse to
304
- * @param force amount of force to be applied to the imposter
305
- * @param contactPoint the location to apply the impulse on the imposter
306
- */
307
- applyImpulse(impostor, force, contactPoint) {
308
- if (!impostor.soft) {
309
- impostor.physicsBody.activate();
310
- const worldPoint = this._tmpAmmoVectorA;
311
- const impulse = this._tmpAmmoVectorB;
312
- // Convert contactPoint relative to center of mass
313
- if (impostor.object && impostor.object.getWorldMatrix) {
314
- contactPoint.subtractInPlace(impostor.object.getWorldMatrix().getTranslation());
315
- }
316
- worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
317
- impulse.setValue(force.x, force.y, force.z);
318
- impostor.physicsBody.applyImpulse(impulse, worldPoint);
319
- }
320
- else {
321
- Logger.Warn("Cannot be applied to a soft body");
322
- }
323
- }
324
- /**
325
- * Applies a force on the imposter
326
- * @param impostor imposter to apply force
327
- * @param force amount of force to be applied to the imposter
328
- * @param contactPoint the location to apply the force on the imposter
329
- */
330
- applyForce(impostor, force, contactPoint) {
331
- if (!impostor.soft) {
332
- impostor.physicsBody.activate();
333
- const worldPoint = this._tmpAmmoVectorA;
334
- const impulse = this._tmpAmmoVectorB;
335
- // Convert contactPoint relative to center of mass
336
- if (impostor.object && impostor.object.getWorldMatrix) {
337
- const localTranslation = impostor.object.getWorldMatrix().getTranslation();
338
- worldPoint.setValue(contactPoint.x - localTranslation.x, contactPoint.y - localTranslation.y, contactPoint.z - localTranslation.z);
339
- }
340
- else {
341
- worldPoint.setValue(contactPoint.x, contactPoint.y, contactPoint.z);
342
- }
343
- impulse.setValue(force.x, force.y, force.z);
344
- impostor.physicsBody.applyForce(impulse, worldPoint);
345
- }
346
- else {
347
- Logger.Warn("Cannot be applied to a soft body");
348
- }
349
- }
350
- /**
351
- * Creates a physics body using the plugin
352
- * @param impostor the imposter to create the physics body on
353
- */
354
- generatePhysicsBody(impostor) {
355
- // Note: this method will not be called on child imposotrs for compound impostors
356
- impostor._pluginData.toDispose = [];
357
- //parent-child relationship
358
- if (impostor.parent) {
359
- if (impostor.physicsBody) {
360
- this.removePhysicsBody(impostor);
361
- impostor.forceUpdate();
362
- }
363
- return;
364
- }
365
- if (impostor.isBodyInitRequired()) {
366
- const colShape = this._createShape(impostor);
367
- const mass = impostor.getParam("mass");
368
- impostor._pluginData.mass = mass;
369
- if (impostor.soft) {
370
- colShape.get_m_cfg().set_collisions(0x11);
371
- colShape.get_m_cfg().set_kDP(impostor.getParam("damping"));
372
- this.bjsAMMO.castObject(colShape, this.bjsAMMO.btCollisionObject).getCollisionShape().setMargin(impostor.getParam("margin"));
373
- colShape.setActivationState(AmmoJSPlugin._DISABLE_DEACTIVATION_FLAG);
374
- this.world.addSoftBody(colShape, 1, -1);
375
- impostor.physicsBody = colShape;
376
- impostor._pluginData.toDispose.push(colShape);
377
- this.setBodyPressure(impostor, 0);
378
- if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
379
- this.setBodyPressure(impostor, impostor.getParam("pressure"));
380
- }
381
- this.setBodyStiffness(impostor, impostor.getParam("stiffness"));
382
- this.setBodyVelocityIterations(impostor, impostor.getParam("velocityIterations"));
383
- this.setBodyPositionIterations(impostor, impostor.getParam("positionIterations"));
384
- }
385
- else {
386
- const localInertia = new this.bjsAMMO.btVector3(0, 0, 0);
387
- const startTransform = new this.bjsAMMO.btTransform();
388
- impostor.object.computeWorldMatrix(true);
389
- startTransform.setIdentity();
390
- if (mass !== 0) {
391
- colShape.calculateLocalInertia(mass, localInertia);
392
- }
393
- this._tmpAmmoVectorA.setValue(impostor.object.position.x, impostor.object.position.y, impostor.object.position.z);
394
- this._tmpAmmoQuaternion.setValue(impostor.object.rotationQuaternion.x, impostor.object.rotationQuaternion.y, impostor.object.rotationQuaternion.z, impostor.object.rotationQuaternion.w);
395
- startTransform.setOrigin(this._tmpAmmoVectorA);
396
- startTransform.setRotation(this._tmpAmmoQuaternion);
397
- const myMotionState = new this.bjsAMMO.btDefaultMotionState(startTransform);
398
- const rbInfo = new this.bjsAMMO.btRigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
399
- const body = new this.bjsAMMO.btRigidBody(rbInfo);
400
- // Make objects kinematic if it's mass is 0
401
- if (mass === 0) {
402
- body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin._KINEMATIC_FLAG);
403
- body.setActivationState(AmmoJSPlugin._DISABLE_DEACTIVATION_FLAG);
404
- }
405
- // Disable collision if NoImpostor, but keep collision if shape is btCompoundShape
406
- if (impostor.type == PhysicsImpostor.NoImpostor && !colShape.getChildShape) {
407
- body.setCollisionFlags(body.getCollisionFlags() | AmmoJSPlugin._DISABLE_COLLISION_FLAG);
408
- }
409
- // compute delta position: compensate the difference between shape center and mesh origin
410
- if (impostor.type !== PhysicsImpostor.MeshImpostor && impostor.type !== PhysicsImpostor.NoImpostor) {
411
- const boundingInfo = impostor.object.getBoundingInfo();
412
- this._tmpVec3.copyFrom(impostor.object.getAbsolutePosition());
413
- this._tmpVec3.subtractInPlace(boundingInfo.boundingBox.centerWorld);
414
- this._tmpVec3.x /= impostor.object.scaling.x;
415
- this._tmpVec3.y /= impostor.object.scaling.y;
416
- this._tmpVec3.z /= impostor.object.scaling.z;
417
- impostor.setDeltaPosition(this._tmpVec3);
418
- }
419
- const group = impostor.getParam("group");
420
- const mask = impostor.getParam("mask");
421
- if (group && mask) {
422
- this.world.addRigidBody(body, group, mask);
423
- }
424
- else {
425
- this.world.addRigidBody(body);
426
- }
427
- impostor.physicsBody = body;
428
- impostor._pluginData.toDispose = impostor._pluginData.toDispose.concat([body, rbInfo, myMotionState, startTransform, localInertia, colShape]);
429
- }
430
- this.setBodyRestitution(impostor, impostor.getParam("restitution"));
431
- this.setBodyFriction(impostor, impostor.getParam("friction"));
432
- }
433
- }
434
- /**
435
- * Removes the physics body from the imposter and disposes of the body's memory
436
- * @param impostor imposter to remove the physics body from
437
- */
438
- removePhysicsBody(impostor) {
439
- if (this.world) {
440
- if (impostor.soft) {
441
- this.world.removeSoftBody(impostor.physicsBody);
442
- }
443
- else {
444
- this.world.removeRigidBody(impostor.physicsBody);
445
- }
446
- if (impostor._pluginData) {
447
- impostor._pluginData.toDispose.forEach((d) => {
448
- this.bjsAMMO.destroy(d);
449
- });
450
- impostor._pluginData.toDispose = [];
451
- }
452
- }
453
- }
454
- /**
455
- * Generates a joint
456
- * @param impostorJoint the imposter joint to create the joint with
457
- */
458
- generateJoint(impostorJoint) {
459
- const mainBody = impostorJoint.mainImpostor.physicsBody;
460
- const connectedBody = impostorJoint.connectedImpostor.physicsBody;
461
- if (!mainBody || !connectedBody) {
462
- return;
463
- }
464
- const jointData = impostorJoint.joint.jointData;
465
- if (!jointData.mainPivot) {
466
- jointData.mainPivot = new Vector3(0, 0, 0);
467
- }
468
- if (!jointData.connectedPivot) {
469
- jointData.connectedPivot = new Vector3(0, 0, 0);
470
- }
471
- let joint;
472
- switch (impostorJoint.joint.type) {
473
- case PhysicsJoint.DistanceJoint: {
474
- const distance = jointData.maxDistance;
475
- if (distance) {
476
- jointData.mainPivot = new Vector3(0, -distance / 2, 0);
477
- jointData.connectedPivot = new Vector3(0, distance / 2, 0);
478
- }
479
- joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
480
- break;
481
- }
482
- case PhysicsJoint.HingeJoint: {
483
- if (!jointData.mainAxis) {
484
- jointData.mainAxis = new Vector3(0, 0, 0);
485
- }
486
- if (!jointData.connectedAxis) {
487
- jointData.connectedAxis = new Vector3(0, 0, 0);
488
- }
489
- const mainAxis = new this.bjsAMMO.btVector3(jointData.mainAxis.x, jointData.mainAxis.y, jointData.mainAxis.z);
490
- const connectedAxis = new this.bjsAMMO.btVector3(jointData.connectedAxis.x, jointData.connectedAxis.y, jointData.connectedAxis.z);
491
- joint = new this.bjsAMMO.btHingeConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z), mainAxis, connectedAxis);
492
- break;
493
- }
494
- case PhysicsJoint.BallAndSocketJoint:
495
- joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
496
- break;
497
- default:
498
- Logger.Warn("JointType not currently supported by the Ammo plugin, falling back to PhysicsJoint.BallAndSocketJoint");
499
- joint = new this.bjsAMMO.btPoint2PointConstraint(mainBody, connectedBody, new this.bjsAMMO.btVector3(jointData.mainPivot.x, jointData.mainPivot.y, jointData.mainPivot.z), new this.bjsAMMO.btVector3(jointData.connectedPivot.x, jointData.connectedPivot.y, jointData.connectedPivot.z));
500
- break;
501
- }
502
- this.world.addConstraint(joint, !impostorJoint.joint.jointData.collision);
503
- impostorJoint.joint.physicsJoint = joint;
504
- }
505
- /**
506
- * Removes a joint
507
- * @param impostorJoint the imposter joint to remove the joint from
508
- */
509
- removeJoint(impostorJoint) {
510
- if (this.world) {
511
- this.world.removeConstraint(impostorJoint.joint.physicsJoint);
512
- }
513
- }
514
- // adds all verticies (including child verticies) to the triangle mesh
515
- _addMeshVerts(btTriangleMesh, topLevelObject, object) {
516
- let triangleCount = 0;
517
- if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
518
- let indices = object.getIndices();
519
- if (!indices) {
520
- indices = [];
521
- }
522
- let vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
523
- if (!vertexPositions) {
524
- vertexPositions = [];
525
- }
526
- let localMatrix;
527
- if (topLevelObject && topLevelObject !== object) {
528
- // top level matrix used for shape transform doesn't take scale into account.
529
- // Moreover, every children vertex position must be in that space.
530
- // So, each vertex position here is transform by (mesh world matrix * toplevelMatrix -1)
531
- let topLevelQuaternion;
532
- if (topLevelObject.rotationQuaternion) {
533
- topLevelQuaternion = topLevelObject.rotationQuaternion;
534
- }
535
- else if (topLevelObject.rotation) {
536
- topLevelQuaternion = Quaternion.FromEulerAngles(topLevelObject.rotation.x, topLevelObject.rotation.y, topLevelObject.rotation.z);
537
- }
538
- else {
539
- topLevelQuaternion = Quaternion.Identity();
540
- }
541
- const topLevelMatrix = Matrix.Compose(Vector3.One(), topLevelQuaternion, topLevelObject.position);
542
- topLevelMatrix.invertToRef(this._tmpMatrix);
543
- const wm = object.computeWorldMatrix(false);
544
- localMatrix = wm.multiply(this._tmpMatrix);
545
- }
546
- else {
547
- // current top level is same as object level -> only use local scaling
548
- Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
549
- localMatrix = this._tmpMatrix;
550
- }
551
- const faceCount = indices.length / 3;
552
- for (let i = 0; i < faceCount; i++) {
553
- const triPoints = [];
554
- for (let point = 0; point < 3; point++) {
555
- let v = new Vector3(vertexPositions[indices[i * 3 + point] * 3 + 0], vertexPositions[indices[i * 3 + point] * 3 + 1], vertexPositions[indices[i * 3 + point] * 3 + 2]);
556
- v = Vector3.TransformCoordinates(v, localMatrix);
557
- let vec;
558
- if (point == 0) {
559
- vec = this._tmpAmmoVectorA;
560
- }
561
- else if (point == 1) {
562
- vec = this._tmpAmmoVectorB;
563
- }
564
- else {
565
- vec = this._tmpAmmoVectorC;
566
- }
567
- vec.setValue(v.x, v.y, v.z);
568
- triPoints.push(vec);
569
- }
570
- btTriangleMesh.addTriangle(triPoints[0], triPoints[1], triPoints[2]);
571
- triangleCount++;
572
- }
573
- object.getChildMeshes().forEach((m) => {
574
- triangleCount += this._addMeshVerts(btTriangleMesh, topLevelObject, m);
575
- });
576
- }
577
- return triangleCount;
578
- }
579
- /**
580
- * Initialise the soft body vertices to match its object's (mesh) vertices
581
- * Softbody vertices (nodes) are in world space and to match this
582
- * The object's position and rotation is set to zero and so its vertices are also then set in world space
583
- * @param impostor to create the softbody for
584
- */
585
- _softVertexData(impostor) {
586
- const object = impostor.object;
587
- if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
588
- let indices = object.getIndices();
589
- if (!indices) {
590
- indices = [];
591
- }
592
- let vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
593
- if (!vertexPositions) {
594
- vertexPositions = [];
595
- }
596
- let vertexNormals = object.getVerticesData(VertexBuffer.NormalKind);
597
- if (!vertexNormals) {
598
- vertexNormals = [];
599
- }
600
- object.computeWorldMatrix(false);
601
- const newPoints = [];
602
- const newNorms = [];
603
- for (let i = 0; i < vertexPositions.length; i += 3) {
604
- let v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
605
- let n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
606
- v = Vector3.TransformCoordinates(v, object.getWorldMatrix());
607
- n = Vector3.TransformNormal(n, object.getWorldMatrix());
608
- newPoints.push(v.x, v.y, v.z);
609
- newNorms.push(n.x, n.y, n.z);
610
- }
611
- const vertex_data = new VertexData();
612
- vertex_data.positions = newPoints;
613
- vertex_data.normals = newNorms;
614
- vertex_data.uvs = object.getVerticesData(VertexBuffer.UVKind);
615
- vertex_data.colors = object.getVerticesData(VertexBuffer.ColorKind);
616
- if (object && object.getIndices) {
617
- vertex_data.indices = object.getIndices();
618
- }
619
- vertex_data.applyToMesh(object);
620
- object.position = Vector3.Zero();
621
- object.rotationQuaternion = null;
622
- object.rotation = Vector3.Zero();
623
- object.computeWorldMatrix(true);
624
- return vertex_data;
625
- }
626
- return VertexData.ExtractFromMesh(object);
627
- }
628
- /**
629
- * Create an impostor's soft body
630
- * @param impostor to create the softbody for
631
- */
632
- _createSoftbody(impostor) {
633
- const object = impostor.object;
634
- if (object && object.getIndices) {
635
- let indices = object.getIndices();
636
- if (!indices) {
637
- indices = [];
638
- }
639
- const vertex_data = this._softVertexData(impostor);
640
- const vertexPositions = vertex_data.positions;
641
- const vertexNormals = vertex_data.normals;
642
- if (vertexPositions === null || vertexNormals === null) {
643
- return new this.bjsAMMO.btCompoundShape();
644
- }
645
- else {
646
- const triPoints = [];
647
- const triNorms = [];
648
- for (let i = 0; i < vertexPositions.length; i += 3) {
649
- const v = new Vector3(vertexPositions[i], vertexPositions[i + 1], vertexPositions[i + 2]);
650
- const n = new Vector3(vertexNormals[i], vertexNormals[i + 1], vertexNormals[i + 2]);
651
- triPoints.push(v.x, v.y, -v.z);
652
- triNorms.push(n.x, n.y, -n.z);
653
- }
654
- const softBody = new this.bjsAMMO.btSoftBodyHelpers().CreateFromTriMesh(this.world.getWorldInfo(), triPoints, object.getIndices(), indices.length / 3, true);
655
- const nbVertices = vertexPositions.length / 3;
656
- const bodyVertices = softBody.get_m_nodes();
657
- let node;
658
- let nodeNormals;
659
- for (let i = 0; i < nbVertices; i++) {
660
- node = bodyVertices.at(i);
661
- nodeNormals = node.get_m_n();
662
- nodeNormals.setX(triNorms[3 * i]);
663
- nodeNormals.setY(triNorms[3 * i + 1]);
664
- nodeNormals.setZ(triNorms[3 * i + 2]);
665
- }
666
- return softBody;
667
- }
668
- }
669
- }
670
- /**
671
- * Create cloth for an impostor
672
- * @param impostor to create the softbody for
673
- */
674
- _createCloth(impostor) {
675
- const object = impostor.object;
676
- if (object && object.getIndices) {
677
- let indices = object.getIndices();
678
- if (!indices) {
679
- indices = [];
680
- }
681
- const vertex_data = this._softVertexData(impostor);
682
- const vertexPositions = vertex_data.positions;
683
- const vertexNormals = vertex_data.normals;
684
- if (vertexPositions === null || vertexNormals === null) {
685
- return new this.bjsAMMO.btCompoundShape();
686
- }
687
- else {
688
- const len = vertexPositions.length;
689
- const segments = Math.sqrt(len / 3);
690
- impostor.segments = segments;
691
- const segs = segments - 1;
692
- this._tmpAmmoVectorA.setValue(vertexPositions[0], vertexPositions[1], vertexPositions[2]);
693
- this._tmpAmmoVectorB.setValue(vertexPositions[3 * segs], vertexPositions[3 * segs + 1], vertexPositions[3 * segs + 2]);
694
- this._tmpAmmoVectorD.setValue(vertexPositions[len - 3], vertexPositions[len - 2], vertexPositions[len - 1]);
695
- this._tmpAmmoVectorC.setValue(vertexPositions[len - 3 - 3 * segs], vertexPositions[len - 2 - 3 * segs], vertexPositions[len - 1 - 3 * segs]);
696
- const clothBody = new this.bjsAMMO.btSoftBodyHelpers().CreatePatch(this.world.getWorldInfo(), this._tmpAmmoVectorA, this._tmpAmmoVectorB, this._tmpAmmoVectorC, this._tmpAmmoVectorD, segments, segments, impostor.getParam("fixedPoints"), true);
697
- return clothBody;
698
- }
699
- }
700
- }
701
- /**
702
- * Create rope for an impostor
703
- * @param impostor to create the softbody for
704
- */
705
- _createRope(impostor) {
706
- let len;
707
- let segments;
708
- const vertex_data = this._softVertexData(impostor);
709
- const vertexPositions = vertex_data.positions;
710
- const vertexNormals = vertex_data.normals;
711
- if (vertexPositions === null || vertexNormals === null) {
712
- return new this.bjsAMMO.btCompoundShape();
713
- }
714
- //force the mesh to be updatable
715
- vertex_data.applyToMesh(impostor.object, true);
716
- impostor._isFromLine = true;
717
- // If in lines mesh all normals will be zero
718
- const vertexSquared = vertexNormals.map((x) => x * x);
719
- const reducer = (accumulator, currentValue) => accumulator + currentValue;
720
- const reduced = vertexSquared.reduce(reducer);
721
- if (reduced === 0) {
722
- // line mesh
723
- len = vertexPositions.length;
724
- segments = len / 3 - 1;
725
- this._tmpAmmoVectorA.setValue(vertexPositions[0], vertexPositions[1], vertexPositions[2]);
726
- this._tmpAmmoVectorB.setValue(vertexPositions[len - 3], vertexPositions[len - 2], vertexPositions[len - 1]);
727
- }
728
- else {
729
- //extruded mesh
730
- impostor._isFromLine = false;
731
- const pathVectors = impostor.getParam("path");
732
- const shape = impostor.getParam("shape");
733
- if (shape === null) {
734
- Logger.Warn("No shape available for extruded mesh");
735
- return new this.bjsAMMO.btCompoundShape();
736
- }
737
- len = pathVectors.length;
738
- segments = len - 1;
739
- this._tmpAmmoVectorA.setValue(pathVectors[0].x, pathVectors[0].y, pathVectors[0].z);
740
- this._tmpAmmoVectorB.setValue(pathVectors[len - 1].x, pathVectors[len - 1].y, pathVectors[len - 1].z);
741
- }
742
- impostor.segments = segments;
743
- let fixedPoints = impostor.getParam("fixedPoints");
744
- fixedPoints = fixedPoints > 3 ? 3 : fixedPoints;
745
- const ropeBody = new this.bjsAMMO.btSoftBodyHelpers().CreateRope(this.world.getWorldInfo(), this._tmpAmmoVectorA, this._tmpAmmoVectorB, segments - 1, fixedPoints);
746
- ropeBody.get_m_cfg().set_collisions(0x11);
747
- return ropeBody;
748
- }
749
- /**
750
- * Create a custom physics impostor shape using the plugin's onCreateCustomShape handler
751
- * @param impostor to create the custom physics shape for
752
- */
753
- _createCustom(impostor) {
754
- let returnValue = null;
755
- if (this.onCreateCustomShape) {
756
- returnValue = this.onCreateCustomShape(impostor);
757
- }
758
- if (returnValue == null) {
759
- returnValue = new this.bjsAMMO.btCompoundShape();
760
- }
761
- return returnValue;
762
- }
763
- // adds all verticies (including child verticies) to the convex hull shape
764
- _addHullVerts(btConvexHullShape, topLevelObject, object) {
765
- let triangleCount = 0;
766
- if (object && object.getIndices && object.getWorldMatrix && object.getChildMeshes) {
767
- let indices = object.getIndices();
768
- if (!indices) {
769
- indices = [];
770
- }
771
- let vertexPositions = object.getVerticesData(VertexBuffer.PositionKind);
772
- if (!vertexPositions) {
773
- vertexPositions = [];
774
- }
775
- object.computeWorldMatrix(false);
776
- const faceCount = indices.length / 3;
777
- for (let i = 0; i < faceCount; i++) {
778
- const triPoints = [];
779
- for (let point = 0; point < 3; point++) {
780
- let v = new Vector3(vertexPositions[indices[i * 3 + point] * 3 + 0], vertexPositions[indices[i * 3 + point] * 3 + 1], vertexPositions[indices[i * 3 + point] * 3 + 2]);
781
- // Adjust for initial scaling
782
- Matrix.ScalingToRef(object.scaling.x, object.scaling.y, object.scaling.z, this._tmpMatrix);
783
- v = Vector3.TransformCoordinates(v, this._tmpMatrix);
784
- let vec;
785
- if (point == 0) {
786
- vec = this._tmpAmmoVectorA;
787
- }
788
- else if (point == 1) {
789
- vec = this._tmpAmmoVectorB;
790
- }
791
- else {
792
- vec = this._tmpAmmoVectorC;
793
- }
794
- vec.setValue(v.x, v.y, v.z);
795
- triPoints.push(vec);
796
- }
797
- btConvexHullShape.addPoint(triPoints[0], true);
798
- btConvexHullShape.addPoint(triPoints[1], true);
799
- btConvexHullShape.addPoint(triPoints[2], true);
800
- triangleCount++;
801
- }
802
- object.getChildMeshes().forEach((m) => {
803
- triangleCount += this._addHullVerts(btConvexHullShape, topLevelObject, m);
804
- });
805
- }
806
- return triangleCount;
807
- }
808
- _createShape(impostor, ignoreChildren = false) {
809
- const object = impostor.object;
810
- let returnValue;
811
- const impostorExtents = impostor.getObjectExtents();
812
- if (!ignoreChildren) {
813
- const meshChildren = impostor.object.getChildMeshes ? impostor.object.getChildMeshes(true) : [];
814
- returnValue = new this.bjsAMMO.btCompoundShape();
815
- // Add shape of all children to the compound shape
816
- let childrenAdded = 0;
817
- meshChildren.forEach((childMesh) => {
818
- const childImpostor = childMesh.getPhysicsImpostor();
819
- if (childImpostor) {
820
- if (childImpostor.type == PhysicsImpostor.MeshImpostor) {
821
- throw "A child MeshImpostor is not supported. Only primitive impostors are supported as children (eg. box or sphere)";
822
- }
823
- const shape = this._createShape(childImpostor);
824
- // Position needs to be scaled based on parent's scaling
825
- const parentMat = childMesh.parent.getWorldMatrix().clone();
826
- const s = new Vector3();
827
- parentMat.decompose(s);
828
- this._tmpAmmoTransform.getOrigin().setValue(childMesh.position.x * s.x, childMesh.position.y * s.y, childMesh.position.z * s.z);
829
- this._tmpAmmoQuaternion.setValue(childMesh.rotationQuaternion.x, childMesh.rotationQuaternion.y, childMesh.rotationQuaternion.z, childMesh.rotationQuaternion.w);
830
- this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
831
- returnValue.addChildShape(this._tmpAmmoTransform, shape);
832
- childImpostor.dispose();
833
- childrenAdded++;
834
- }
835
- });
836
- if (childrenAdded > 0) {
837
- // Add parents shape as a child if present
838
- if (impostor.type != PhysicsImpostor.NoImpostor) {
839
- const shape = this._createShape(impostor, true);
840
- if (shape) {
841
- this._tmpAmmoTransform.getOrigin().setValue(0, 0, 0);
842
- this._tmpAmmoQuaternion.setValue(0, 0, 0, 1);
843
- this._tmpAmmoTransform.setRotation(this._tmpAmmoQuaternion);
844
- returnValue.addChildShape(this._tmpAmmoTransform, shape);
845
- }
846
- }
847
- return returnValue;
848
- }
849
- else {
850
- // If no children with impostors create the actual shape below instead
851
- this.bjsAMMO.destroy(returnValue);
852
- returnValue = null;
853
- }
854
- }
855
- switch (impostor.type) {
856
- case PhysicsImpostor.SphereImpostor:
857
- // Is there a better way to compare floats number? With an epsilon or with a Math function
858
- if (Scalar.WithinEpsilon(impostorExtents.x, impostorExtents.y, 0.0001) && Scalar.WithinEpsilon(impostorExtents.x, impostorExtents.z, 0.0001)) {
859
- returnValue = new this.bjsAMMO.btSphereShape(impostorExtents.x / 2);
860
- }
861
- else {
862
- // create a btMultiSphereShape because it's not possible to set a local scaling on a btSphereShape
863
- const positions = [new this.bjsAMMO.btVector3(0, 0, 0)];
864
- const radii = [1];
865
- returnValue = new this.bjsAMMO.btMultiSphereShape(positions, radii, 1);
866
- returnValue.setLocalScaling(new this.bjsAMMO.btVector3(impostorExtents.x / 2, impostorExtents.y / 2, impostorExtents.z / 2));
867
- }
868
- break;
869
- case PhysicsImpostor.CapsuleImpostor:
870
- {
871
- // https://pybullet.org/Bullet/BulletFull/classbtCapsuleShape.html#details
872
- // Height is just the height between the center of each 'sphere' of the capsule caps
873
- const capRadius = impostorExtents.x / 2;
874
- returnValue = new this.bjsAMMO.btCapsuleShape(capRadius, impostorExtents.y - capRadius * 2);
875
- }
876
- break;
877
- case PhysicsImpostor.CylinderImpostor:
878
- this._tmpAmmoVectorA.setValue(impostorExtents.x / 2, impostorExtents.y / 2, impostorExtents.z / 2);
879
- returnValue = new this.bjsAMMO.btCylinderShape(this._tmpAmmoVectorA);
880
- break;
881
- case PhysicsImpostor.PlaneImpostor:
882
- case PhysicsImpostor.BoxImpostor:
883
- this._tmpAmmoVectorA.setValue(impostorExtents.x / 2, impostorExtents.y / 2, impostorExtents.z / 2);
884
- returnValue = new this.bjsAMMO.btBoxShape(this._tmpAmmoVectorA);
885
- break;
886
- case PhysicsImpostor.MeshImpostor: {
887
- if (impostor.getParam("mass") == 0) {
888
- // Only create btBvhTriangleMeshShape if the impostor is static
889
- // See https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=7283
890
- if (this.onCreateCustomMeshImpostor) {
891
- returnValue = this.onCreateCustomMeshImpostor(impostor);
892
- }
893
- else {
894
- const triMesh = new this.bjsAMMO.btTriangleMesh();
895
- impostor._pluginData.toDispose.push(triMesh);
896
- const triangleCount = this._addMeshVerts(triMesh, object, object);
897
- if (triangleCount == 0) {
898
- returnValue = new this.bjsAMMO.btCompoundShape();
899
- }
900
- else {
901
- returnValue = new this.bjsAMMO.btBvhTriangleMeshShape(triMesh);
902
- }
903
- }
904
- break;
905
- }
906
- }
907
- // Otherwise create convexHullImpostor
908
- // eslint-disable-next-line no-fallthrough
909
- case PhysicsImpostor.ConvexHullImpostor: {
910
- if (this.onCreateCustomConvexHullImpostor) {
911
- returnValue = this.onCreateCustomConvexHullImpostor(impostor);
912
- }
913
- else {
914
- const convexHull = new this.bjsAMMO.btConvexHullShape();
915
- const triangleCount = this._addHullVerts(convexHull, object, object);
916
- if (triangleCount == 0) {
917
- // Cleanup Unused Convex Hull Shape
918
- impostor._pluginData.toDispose.push(convexHull);
919
- returnValue = new this.bjsAMMO.btCompoundShape();
920
- }
921
- else {
922
- returnValue = convexHull;
923
- }
924
- }
925
- break;
926
- }
927
- case PhysicsImpostor.NoImpostor:
928
- // Fill with sphere but collision is disabled on the rigid body in generatePhysicsBody, using an empty shape caused unexpected movement with joints
929
- returnValue = new this.bjsAMMO.btSphereShape(impostorExtents.x / 2);
930
- break;
931
- case PhysicsImpostor.CustomImpostor:
932
- // Only usable when the plugin's onCreateCustomShape is set
933
- returnValue = this._createCustom(impostor);
934
- break;
935
- case PhysicsImpostor.SoftbodyImpostor:
936
- // Only usable with a mesh that has sufficient and shared vertices
937
- returnValue = this._createSoftbody(impostor);
938
- break;
939
- case PhysicsImpostor.ClothImpostor:
940
- // Only usable with a ground mesh that has sufficient and shared vertices
941
- returnValue = this._createCloth(impostor);
942
- break;
943
- case PhysicsImpostor.RopeImpostor:
944
- // Only usable with a line mesh or an extruded mesh that is updatable
945
- returnValue = this._createRope(impostor);
946
- break;
947
- default:
948
- Logger.Warn("The impostor type is not currently supported by the ammo plugin.");
949
- break;
950
- }
951
- return returnValue;
952
- }
953
- /**
954
- * Sets the mesh body position/rotation from the babylon impostor
955
- * @param impostor imposter containing the physics body and babylon object
956
- */
957
- setTransformationFromPhysicsBody(impostor) {
958
- impostor.physicsBody.getMotionState().getWorldTransform(this._tmpAmmoTransform);
959
- impostor.object.position.set(this._tmpAmmoTransform.getOrigin().x(), this._tmpAmmoTransform.getOrigin().y(), this._tmpAmmoTransform.getOrigin().z());
960
- if (!impostor.object.rotationQuaternion) {
961
- if (impostor.object.rotation) {
962
- this._tmpQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
963
- this._tmpQuaternion.toEulerAnglesToRef(impostor.object.rotation);
964
- }
965
- }
966
- else {
967
- impostor.object.rotationQuaternion.set(this._tmpAmmoTransform.getRotation().x(), this._tmpAmmoTransform.getRotation().y(), this._tmpAmmoTransform.getRotation().z(), this._tmpAmmoTransform.getRotation().w());
968
- }
969
- }
970
- /**
971
- * Sets the babylon object's position/rotation from the physics body's position/rotation
972
- * @param impostor imposter containing the physics body and babylon object
973
- * @param newPosition new position
974
- * @param newRotation new rotation
975
- */
976
- setPhysicsBodyTransformation(impostor, newPosition, newRotation) {
977
- const trans = impostor.physicsBody.getWorldTransform();
978
- // If rotation/position has changed update and activate rigged body
979
- if (Math.abs(trans.getOrigin().x() - newPosition.x) > Epsilon ||
980
- Math.abs(trans.getOrigin().y() - newPosition.y) > Epsilon ||
981
- Math.abs(trans.getOrigin().z() - newPosition.z) > Epsilon ||
982
- Math.abs(trans.getRotation().x() - newRotation.x) > Epsilon ||
983
- Math.abs(trans.getRotation().y() - newRotation.y) > Epsilon ||
984
- Math.abs(trans.getRotation().z() - newRotation.z) > Epsilon ||
985
- Math.abs(trans.getRotation().w() - newRotation.w) > Epsilon) {
986
- this._tmpAmmoVectorA.setValue(newPosition.x, newPosition.y, newPosition.z);
987
- trans.setOrigin(this._tmpAmmoVectorA);
988
- this._tmpAmmoQuaternion.setValue(newRotation.x, newRotation.y, newRotation.z, newRotation.w);
989
- trans.setRotation(this._tmpAmmoQuaternion);
990
- impostor.physicsBody.setWorldTransform(trans);
991
- if (impostor.mass == 0) {
992
- // Kinematic objects must be updated using motion state
993
- const motionState = impostor.physicsBody.getMotionState();
994
- if (motionState) {
995
- motionState.setWorldTransform(trans);
996
- }
997
- }
998
- else {
999
- impostor.physicsBody.activate();
1000
- }
1001
- }
1002
- }
1003
- /**
1004
- * If this plugin is supported
1005
- * @returns true if its supported
1006
- */
1007
- isSupported() {
1008
- return this.bjsAMMO !== undefined;
1009
- }
1010
- /**
1011
- * Sets the linear velocity of the physics body
1012
- * @param impostor imposter to set the velocity on
1013
- * @param velocity velocity to set
1014
- */
1015
- setLinearVelocity(impostor, velocity) {
1016
- this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
1017
- if (impostor.soft) {
1018
- impostor.physicsBody.linearVelocity(this._tmpAmmoVectorA);
1019
- }
1020
- else {
1021
- impostor.physicsBody.setLinearVelocity(this._tmpAmmoVectorA);
1022
- }
1023
- }
1024
- /**
1025
- * Sets the angular velocity of the physics body
1026
- * @param impostor imposter to set the velocity on
1027
- * @param velocity velocity to set
1028
- */
1029
- setAngularVelocity(impostor, velocity) {
1030
- this._tmpAmmoVectorA.setValue(velocity.x, velocity.y, velocity.z);
1031
- if (impostor.soft) {
1032
- impostor.physicsBody.angularVelocity(this._tmpAmmoVectorA);
1033
- }
1034
- else {
1035
- impostor.physicsBody.setAngularVelocity(this._tmpAmmoVectorA);
1036
- }
1037
- }
1038
- /**
1039
- * gets the linear velocity
1040
- * @param impostor imposter to get linear velocity from
1041
- * @returns linear velocity
1042
- */
1043
- getLinearVelocity(impostor) {
1044
- let v;
1045
- if (impostor.soft) {
1046
- v = impostor.physicsBody.linearVelocity();
1047
- }
1048
- else {
1049
- v = impostor.physicsBody.getLinearVelocity();
1050
- }
1051
- if (!v) {
1052
- return null;
1053
- }
1054
- const result = new Vector3(v.x(), v.y(), v.z());
1055
- this.bjsAMMO.destroy(v);
1056
- return result;
1057
- }
1058
- /**
1059
- * gets the angular velocity
1060
- * @param impostor imposter to get angular velocity from
1061
- * @returns angular velocity
1062
- */
1063
- getAngularVelocity(impostor) {
1064
- let v;
1065
- if (impostor.soft) {
1066
- v = impostor.physicsBody.angularVelocity();
1067
- }
1068
- else {
1069
- v = impostor.physicsBody.getAngularVelocity();
1070
- }
1071
- if (!v) {
1072
- return null;
1073
- }
1074
- const result = new Vector3(v.x(), v.y(), v.z());
1075
- this.bjsAMMO.destroy(v);
1076
- return result;
1077
- }
1078
- /**
1079
- * Sets the mass of physics body
1080
- * @param impostor imposter to set the mass on
1081
- * @param mass mass to set
1082
- */
1083
- setBodyMass(impostor, mass) {
1084
- if (impostor.soft) {
1085
- impostor.physicsBody.setTotalMass(mass, false);
1086
- }
1087
- else {
1088
- impostor.physicsBody.setMassProps(mass);
1089
- }
1090
- impostor._pluginData.mass = mass;
1091
- }
1092
- /**
1093
- * Gets the mass of the physics body
1094
- * @param impostor imposter to get the mass from
1095
- * @returns mass
1096
- */
1097
- getBodyMass(impostor) {
1098
- return impostor._pluginData.mass || 0;
1099
- }
1100
- /**
1101
- * Gets friction of the impostor
1102
- * @param impostor impostor to get friction from
1103
- * @returns friction value
1104
- */
1105
- getBodyFriction(impostor) {
1106
- return impostor._pluginData.friction || 0;
1107
- }
1108
- /**
1109
- * Sets friction of the impostor
1110
- * @param impostor impostor to set friction on
1111
- * @param friction friction value
1112
- */
1113
- setBodyFriction(impostor, friction) {
1114
- if (impostor.soft) {
1115
- impostor.physicsBody.get_m_cfg().set_kDF(friction);
1116
- }
1117
- else {
1118
- impostor.physicsBody.setFriction(friction);
1119
- }
1120
- impostor._pluginData.friction = friction;
1121
- }
1122
- /**
1123
- * Gets restitution of the impostor
1124
- * @param impostor impostor to get restitution from
1125
- * @returns restitution value
1126
- */
1127
- getBodyRestitution(impostor) {
1128
- return impostor._pluginData.restitution || 0;
1129
- }
1130
- /**
1131
- * Sets restitution of the impostor
1132
- * @param impostor impostor to set resitution on
1133
- * @param restitution resitution value
1134
- */
1135
- setBodyRestitution(impostor, restitution) {
1136
- impostor.physicsBody.setRestitution(restitution);
1137
- impostor._pluginData.restitution = restitution;
1138
- }
1139
- /**
1140
- * Gets pressure inside the impostor
1141
- * @param impostor impostor to get pressure from
1142
- * @returns pressure value
1143
- */
1144
- getBodyPressure(impostor) {
1145
- if (!impostor.soft) {
1146
- Logger.Warn("Pressure is not a property of a rigid body");
1147
- return 0;
1148
- }
1149
- return impostor._pluginData.pressure || 0;
1150
- }
1151
- /**
1152
- * Sets pressure inside a soft body impostor
1153
- * Cloth and rope must remain 0 pressure
1154
- * @param impostor impostor to set pressure on
1155
- * @param pressure pressure value
1156
- */
1157
- setBodyPressure(impostor, pressure) {
1158
- if (impostor.soft) {
1159
- if (impostor.type === PhysicsImpostor.SoftbodyImpostor) {
1160
- impostor.physicsBody.get_m_cfg().set_kPR(pressure);
1161
- impostor._pluginData.pressure = pressure;
1162
- }
1163
- else {
1164
- impostor.physicsBody.get_m_cfg().set_kPR(0);
1165
- impostor._pluginData.pressure = 0;
1166
- }
1167
- }
1168
- else {
1169
- Logger.Warn("Pressure can only be applied to a softbody");
1170
- }
1171
- }
1172
- /**
1173
- * Gets stiffness of the impostor
1174
- * @param impostor impostor to get stiffness from
1175
- * @returns pressure value
1176
- */
1177
- getBodyStiffness(impostor) {
1178
- if (!impostor.soft) {
1179
- Logger.Warn("Stiffness is not a property of a rigid body");
1180
- return 0;
1181
- }
1182
- return impostor._pluginData.stiffness || 0;
1183
- }
1184
- /**
1185
- * Sets stiffness of the impostor
1186
- * @param impostor impostor to set stiffness on
1187
- * @param stiffness stiffness value from 0 to 1
1188
- */
1189
- setBodyStiffness(impostor, stiffness) {
1190
- if (impostor.soft) {
1191
- stiffness = stiffness < 0 ? 0 : stiffness;
1192
- stiffness = stiffness > 1 ? 1 : stiffness;
1193
- impostor.physicsBody.get_m_materials().at(0).set_m_kLST(stiffness);
1194
- impostor._pluginData.stiffness = stiffness;
1195
- }
1196
- else {
1197
- Logger.Warn("Stiffness cannot be applied to a rigid body");
1198
- }
1199
- }
1200
- /**
1201
- * Gets velocityIterations of the impostor
1202
- * @param impostor impostor to get velocity iterations from
1203
- * @returns velocityIterations value
1204
- */
1205
- getBodyVelocityIterations(impostor) {
1206
- if (!impostor.soft) {
1207
- Logger.Warn("Velocity iterations is not a property of a rigid body");
1208
- return 0;
1209
- }
1210
- return impostor._pluginData.velocityIterations || 0;
1211
- }
1212
- /**
1213
- * Sets velocityIterations of the impostor
1214
- * @param impostor impostor to set velocity iterations on
1215
- * @param velocityIterations velocityIterations value
1216
- */
1217
- setBodyVelocityIterations(impostor, velocityIterations) {
1218
- if (impostor.soft) {
1219
- velocityIterations = velocityIterations < 0 ? 0 : velocityIterations;
1220
- impostor.physicsBody.get_m_cfg().set_viterations(velocityIterations);
1221
- impostor._pluginData.velocityIterations = velocityIterations;
1222
- }
1223
- else {
1224
- Logger.Warn("Velocity iterations cannot be applied to a rigid body");
1225
- }
1226
- }
1227
- /**
1228
- * Gets positionIterations of the impostor
1229
- * @param impostor impostor to get position iterations from
1230
- * @returns positionIterations value
1231
- */
1232
- getBodyPositionIterations(impostor) {
1233
- if (!impostor.soft) {
1234
- Logger.Warn("Position iterations is not a property of a rigid body");
1235
- return 0;
1236
- }
1237
- return impostor._pluginData.positionIterations || 0;
1238
- }
1239
- /**
1240
- * Sets positionIterations of the impostor
1241
- * @param impostor impostor to set position on
1242
- * @param positionIterations positionIterations value
1243
- */
1244
- setBodyPositionIterations(impostor, positionIterations) {
1245
- if (impostor.soft) {
1246
- positionIterations = positionIterations < 0 ? 0 : positionIterations;
1247
- impostor.physicsBody.get_m_cfg().set_piterations(positionIterations);
1248
- impostor._pluginData.positionIterations = positionIterations;
1249
- }
1250
- else {
1251
- Logger.Warn("Position iterations cannot be applied to a rigid body");
1252
- }
1253
- }
1254
- /**
1255
- * Append an anchor to a cloth object
1256
- * @param impostor is the cloth impostor to add anchor to
1257
- * @param otherImpostor is the rigid impostor to anchor to
1258
- * @param width ratio across width from 0 to 1
1259
- * @param height ratio up height from 0 to 1
1260
- * @param influence the elasticity between cloth impostor and anchor from 0, very stretchy to 1, little stretch
1261
- * @param noCollisionBetweenLinkedBodies when true collisions between soft impostor and anchor are ignored; default false
1262
- */
1263
- appendAnchor(impostor, otherImpostor, width, height, influence = 1, noCollisionBetweenLinkedBodies = false) {
1264
- const segs = impostor.segments;
1265
- const nbAcross = Math.round((segs - 1) * width);
1266
- const nbUp = Math.round((segs - 1) * height);
1267
- const nbDown = segs - 1 - nbUp;
1268
- const node = nbAcross + segs * nbDown;
1269
- impostor.physicsBody.appendAnchor(node, otherImpostor.physicsBody, noCollisionBetweenLinkedBodies, influence);
1270
- }
1271
- /**
1272
- * Append an hook to a rope object
1273
- * @param impostor is the rope impostor to add hook to
1274
- * @param otherImpostor is the rigid impostor to hook to
1275
- * @param length ratio along the rope from 0 to 1
1276
- * @param influence the elasticity between soft impostor and anchor from 0, very stretchy to 1, little stretch
1277
- * @param noCollisionBetweenLinkedBodies when true collisions between soft impostor and anchor are ignored; default false
1278
- */
1279
- appendHook(impostor, otherImpostor, length, influence = 1, noCollisionBetweenLinkedBodies = false) {
1280
- const node = Math.round(impostor.segments * length);
1281
- impostor.physicsBody.appendAnchor(node, otherImpostor.physicsBody, noCollisionBetweenLinkedBodies, influence);
1282
- }
1283
- /**
1284
- * Sleeps the physics body and stops it from being active
1285
- * @param impostor impostor to sleep
1286
- */
1287
- sleepBody(impostor) {
1288
- impostor.physicsBody.forceActivationState(0);
1289
- }
1290
- /**
1291
- * Activates the physics body
1292
- * @param impostor impostor to activate
1293
- */
1294
- wakeUpBody(impostor) {
1295
- impostor.physicsBody.activate();
1296
- }
1297
- /**
1298
- * Updates the distance parameters of the joint
1299
- */
1300
- updateDistanceJoint() {
1301
- Logger.Warn("updateDistanceJoint is not currently supported by the Ammo physics plugin");
1302
- }
1303
- /**
1304
- * Sets a motor on the joint
1305
- * @param joint joint to set motor on
1306
- * @param speed speed of the motor
1307
- * @param maxForce maximum force of the motor
1308
- */
1309
- setMotor(joint, speed, maxForce) {
1310
- joint.physicsJoint.enableAngularMotor(true, speed, maxForce);
1311
- }
1312
- /**
1313
- * Sets the motors limit
1314
- */
1315
- setLimit() {
1316
- Logger.Warn("setLimit is not currently supported by the Ammo physics plugin");
1317
- }
1318
- /**
1319
- * Syncs the position and rotation of a mesh with the impostor
1320
- * @param mesh mesh to sync
1321
- * @param impostor impostor to update the mesh with
1322
- */
1323
- syncMeshWithImpostor(mesh, impostor) {
1324
- const body = impostor.physicsBody;
1325
- body.getMotionState().getWorldTransform(this._tmpAmmoTransform);
1326
- mesh.position.x = this._tmpAmmoTransform.getOrigin().x();
1327
- mesh.position.y = this._tmpAmmoTransform.getOrigin().y();
1328
- mesh.position.z = this._tmpAmmoTransform.getOrigin().z();
1329
- if (mesh.rotationQuaternion) {
1330
- mesh.rotationQuaternion.x = this._tmpAmmoTransform.getRotation().x();
1331
- mesh.rotationQuaternion.y = this._tmpAmmoTransform.getRotation().y();
1332
- mesh.rotationQuaternion.z = this._tmpAmmoTransform.getRotation().z();
1333
- mesh.rotationQuaternion.w = this._tmpAmmoTransform.getRotation().w();
1334
- }
1335
- }
1336
- /**
1337
- * Gets the radius of the impostor
1338
- * @param impostor impostor to get radius from
1339
- * @returns the radius
1340
- */
1341
- getRadius(impostor) {
1342
- const extents = impostor.getObjectExtents();
1343
- return extents.x / 2;
1344
- }
1345
- /**
1346
- * Gets the box size of the impostor
1347
- * @param impostor impostor to get box size from
1348
- * @param result the resulting box size
1349
- */
1350
- getBoxSizeToRef(impostor, result) {
1351
- const extents = impostor.getObjectExtents();
1352
- result.x = extents.x;
1353
- result.y = extents.y;
1354
- result.z = extents.z;
1355
- }
1356
- /**
1357
- * Disposes of the impostor
1358
- */
1359
- dispose() {
1360
- // Dispose of world
1361
- this.bjsAMMO.destroy(this.world);
1362
- this.bjsAMMO.destroy(this._solver);
1363
- this.bjsAMMO.destroy(this._overlappingPairCache);
1364
- this.bjsAMMO.destroy(this._dispatcher);
1365
- this.bjsAMMO.destroy(this._collisionConfiguration);
1366
- // Dispose of temp variables
1367
- this.bjsAMMO.destroy(this._tmpAmmoVectorA);
1368
- this.bjsAMMO.destroy(this._tmpAmmoVectorB);
1369
- this.bjsAMMO.destroy(this._tmpAmmoVectorC);
1370
- this.bjsAMMO.destroy(this._tmpAmmoTransform);
1371
- this.bjsAMMO.destroy(this._tmpAmmoQuaternion);
1372
- this.bjsAMMO.destroy(this._tmpAmmoConcreteContactResultCallback);
1373
- this.world = null;
1374
- }
1375
- /**
1376
- * Does a raycast in the physics world
1377
- * @param from where should the ray start?
1378
- * @param to where should the ray end?
1379
- * @returns PhysicsRaycastResult
1380
- */
1381
- raycast(from, to) {
1382
- this._tmpAmmoVectorRCA = new this.bjsAMMO.btVector3(from.x, from.y, from.z);
1383
- this._tmpAmmoVectorRCB = new this.bjsAMMO.btVector3(to.x, to.y, to.z);
1384
- const rayCallback = new this.bjsAMMO.ClosestRayResultCallback(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB);
1385
- this.world.rayTest(this._tmpAmmoVectorRCA, this._tmpAmmoVectorRCB, rayCallback);
1386
- this._raycastResult.reset(from, to);
1387
- if (rayCallback.hasHit()) {
1388
- // TODO: do we want/need the body? If so, set all the data
1389
- /*
1390
- var rigidBody = this.bjsAMMO.btRigidBody.prototype.upcast(
1391
- rayCallback.get_m_collisionObject()
1392
- );
1393
- var body = {};
1394
- */
1395
- this._raycastResult.setHitData({
1396
- x: rayCallback.get_m_hitNormalWorld().x(),
1397
- y: rayCallback.get_m_hitNormalWorld().y(),
1398
- z: rayCallback.get_m_hitNormalWorld().z(),
1399
- }, {
1400
- x: rayCallback.get_m_hitPointWorld().x(),
1401
- y: rayCallback.get_m_hitPointWorld().y(),
1402
- z: rayCallback.get_m_hitPointWorld().z(),
1403
- });
1404
- this._raycastResult.calculateHitDistance();
1405
- }
1406
- this.bjsAMMO.destroy(rayCallback);
1407
- this.bjsAMMO.destroy(this._tmpAmmoVectorRCA);
1408
- this.bjsAMMO.destroy(this._tmpAmmoVectorRCB);
1409
- return this._raycastResult;
1410
- }
1411
- }
1412
- AmmoJSPlugin._DISABLE_COLLISION_FLAG = 4;
1413
- AmmoJSPlugin._KINEMATIC_FLAG = 2;
1414
- AmmoJSPlugin._DISABLE_DEACTIVATION_FLAG = 4;
1
+ // ES 6 Compatibility
2
+ export * from "../v1/Plugins/ammoJSPlugin.js";
1415
3
  //# sourceMappingURL=ammoJSPlugin.js.map