@needle-tools/engine 3.28.7-beta → 3.28.7-beta.1

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 (849) hide show
  1. package/CHANGELOG.md +2259 -2256
  2. package/LICENSE.md +10 -10
  3. package/README.md +52 -52
  4. package/dist/needle-engine.js +9 -12
  5. package/dist/needle-engine.light.js +9 -12
  6. package/dist/needle-engine.light.min.js +8 -11
  7. package/dist/needle-engine.light.umd.cjs +8 -11
  8. package/dist/needle-engine.min.js +8 -11
  9. package/dist/needle-engine.umd.cjs +8 -11
  10. package/lib/engine/api.d.ts +52 -52
  11. package/lib/engine/api.js +51 -51
  12. package/lib/engine/assets/index.d.ts +1 -1
  13. package/lib/engine/assets/index.js +4 -4
  14. package/lib/engine/codegen/register_types.d.ts +1 -1
  15. package/lib/engine/codegen/register_types.js +439 -439
  16. package/lib/engine/debug/debug.d.ts +12 -12
  17. package/lib/engine/debug/debug.js +26 -26
  18. package/lib/engine/debug/debug_console.d.ts +2 -2
  19. package/lib/engine/debug/debug_console.js +204 -204
  20. package/lib/engine/debug/debug_overlay.d.ts +10 -10
  21. package/lib/engine/debug/debug_overlay.js +277 -277
  22. package/lib/engine/debug/index.d.ts +1 -1
  23. package/lib/engine/debug/index.js +1 -1
  24. package/lib/engine/engine_addressables.d.ts +75 -75
  25. package/lib/engine/engine_addressables.js +441 -441
  26. package/lib/engine/engine_application.d.ts +19 -19
  27. package/lib/engine/engine_application.js +45 -45
  28. package/lib/engine/engine_assetdatabase.d.ts +25 -25
  29. package/lib/engine/engine_assetdatabase.js +341 -341
  30. package/lib/engine/engine_camera.d.ts +6 -6
  31. package/lib/engine/engine_camera.js +23 -23
  32. package/lib/engine/engine_components.d.ts +17 -17
  33. package/lib/engine/engine_components.js +273 -273
  34. package/lib/engine/engine_components_internal.d.ts +11 -11
  35. package/lib/engine/engine_components_internal.js +41 -41
  36. package/lib/engine/engine_constants.d.ts +5 -5
  37. package/lib/engine/engine_constants.js +32 -32
  38. package/lib/engine/engine_context.d.ts +269 -269
  39. package/lib/engine/engine_context.js +1242 -1242
  40. package/lib/engine/engine_context_registry.d.ts +50 -50
  41. package/lib/engine/engine_context_registry.js +89 -89
  42. package/lib/engine/engine_coroutine.d.ts +4 -4
  43. package/lib/engine/engine_coroutine.js +21 -21
  44. package/lib/engine/engine_create_objects.d.ts +13 -13
  45. package/lib/engine/engine_create_objects.js +33 -33
  46. package/lib/engine/engine_default_parameters.d.ts +2 -2
  47. package/lib/engine/engine_default_parameters.js +3 -3
  48. package/lib/engine/engine_editor-sync.d.ts +17 -17
  49. package/lib/engine/engine_editor-sync.js +7 -7
  50. package/lib/engine/engine_element.d.ts +55 -55
  51. package/lib/engine/engine_element.js +559 -559
  52. package/lib/engine/engine_element_attributes.d.ts +49 -49
  53. package/lib/engine/engine_element_attributes.js +1 -1
  54. package/lib/engine/engine_element_extras.d.ts +6 -6
  55. package/lib/engine/engine_element_extras.js +13 -13
  56. package/lib/engine/engine_element_loading.d.ts +40 -40
  57. package/lib/engine/engine_element_loading.js +312 -312
  58. package/lib/engine/engine_element_overlay.d.ts +19 -19
  59. package/lib/engine/engine_element_overlay.js +143 -143
  60. package/lib/engine/engine_fileloader.d.ts +3 -3
  61. package/lib/engine/engine_fileloader.js +7 -7
  62. package/lib/engine/engine_gameobject.d.ts +39 -39
  63. package/lib/engine/engine_gameobject.js +559 -559
  64. package/lib/engine/engine_generic_utils.d.ts +1 -1
  65. package/lib/engine/engine_generic_utils.js +13 -13
  66. package/lib/engine/engine_gizmos.d.ts +26 -26
  67. package/lib/engine/engine_gizmos.js +282 -282
  68. package/lib/engine/engine_gltf.d.ts +13 -13
  69. package/lib/engine/engine_gltf.js +15 -15
  70. package/lib/engine/engine_gltf_builtin_components.d.ts +7 -7
  71. package/lib/engine/engine_gltf_builtin_components.js +298 -298
  72. package/lib/engine/engine_hot_reload.d.ts +5 -5
  73. package/lib/engine/engine_hot_reload.js +182 -182
  74. package/lib/engine/engine_input.d.ts +129 -129
  75. package/lib/engine/engine_input.js +799 -799
  76. package/lib/engine/engine_input_utils.d.ts +2 -2
  77. package/lib/engine/engine_input_utils.js +22 -22
  78. package/lib/engine/engine_instancing.d.ts +16 -16
  79. package/lib/engine/engine_instancing.js +36 -36
  80. package/lib/engine/engine_license.d.ts +4 -4
  81. package/lib/engine/engine_license.js +398 -398
  82. package/lib/engine/engine_lifecycle_api.d.ts +14 -14
  83. package/lib/engine/engine_lifecycle_api.js +24 -24
  84. package/lib/engine/engine_lifecycle_functions_internal.d.ts +6 -6
  85. package/lib/engine/engine_lifecycle_functions_internal.js +28 -28
  86. package/lib/engine/engine_lightdata.d.ts +23 -23
  87. package/lib/engine/engine_lightdata.js +86 -86
  88. package/lib/engine/engine_loaders.d.ts +7 -7
  89. package/lib/engine/engine_loaders.js +69 -69
  90. package/lib/engine/engine_mainloop_utils.d.ts +13 -13
  91. package/lib/engine/engine_mainloop_utils.js +426 -426
  92. package/lib/engine/engine_math.d.ts +43 -43
  93. package/lib/engine/engine_math.js +147 -147
  94. package/lib/engine/engine_networking.d.ts +176 -176
  95. package/lib/engine/engine_networking.js +649 -649
  96. package/lib/engine/engine_networking_auto.d.ts +24 -24
  97. package/lib/engine/engine_networking_auto.js +324 -324
  98. package/lib/engine/engine_networking_files.d.ts +23 -23
  99. package/lib/engine/engine_networking_files.js +176 -176
  100. package/lib/engine/engine_networking_files_default_components.d.ts +3 -3
  101. package/lib/engine/engine_networking_files_default_components.js +39 -39
  102. package/lib/engine/engine_networking_instantiate.d.ts +39 -39
  103. package/lib/engine/engine_networking_instantiate.js +302 -302
  104. package/lib/engine/engine_networking_peer.d.ts +15 -15
  105. package/lib/engine/engine_networking_peer.js +132 -132
  106. package/lib/engine/engine_networking_streams.d.ts +90 -90
  107. package/lib/engine/engine_networking_streams.js +428 -428
  108. package/lib/engine/engine_networking_types.d.ts +14 -14
  109. package/lib/engine/engine_networking_types.js +7 -7
  110. package/lib/engine/engine_networking_utils.d.ts +2 -2
  111. package/lib/engine/engine_networking_utils.js +20 -20
  112. package/lib/engine/engine_patcher.d.ts +10 -10
  113. package/lib/engine/engine_patcher.js +142 -142
  114. package/lib/engine/engine_physics.d.ts +115 -115
  115. package/lib/engine/engine_physics.js +228 -228
  116. package/lib/engine/engine_physics.types.d.ts +37 -37
  117. package/lib/engine/engine_physics.types.js +33 -33
  118. package/lib/engine/engine_physics_rapier.d.ts +112 -112
  119. package/lib/engine/engine_physics_rapier.js +1266 -1266
  120. package/lib/engine/engine_playerview.d.ts +26 -26
  121. package/lib/engine/engine_playerview.js +64 -64
  122. package/lib/engine/engine_scenelighting.d.ts +74 -74
  123. package/lib/engine/engine_scenelighting.js +285 -285
  124. package/lib/engine/engine_scenetools.d.ts +35 -35
  125. package/lib/engine/engine_scenetools.js +212 -212
  126. package/lib/engine/engine_serialization.d.ts +4 -4
  127. package/lib/engine/engine_serialization.js +4 -4
  128. package/lib/engine/engine_serialization_builtin_serializer.d.ts +62 -62
  129. package/lib/engine/engine_serialization_builtin_serializer.js +369 -369
  130. package/lib/engine/engine_serialization_core.d.ts +84 -84
  131. package/lib/engine/engine_serialization_core.js +576 -576
  132. package/lib/engine/engine_serialization_decorator.d.ts +15 -15
  133. package/lib/engine/engine_serialization_decorator.js +54 -54
  134. package/lib/engine/engine_setup.d.ts +1 -1
  135. package/lib/engine/engine_setup.js +2 -2
  136. package/lib/engine/engine_shaders.d.ts +31 -31
  137. package/lib/engine/engine_shaders.js +229 -229
  138. package/lib/engine/engine_shims.d.ts +3 -3
  139. package/lib/engine/engine_shims.js +22 -22
  140. package/lib/engine/engine_texture.d.ts +20 -20
  141. package/lib/engine/engine_texture.js +57 -57
  142. package/lib/engine/engine_three_utils.d.ts +51 -51
  143. package/lib/engine/engine_three_utils.js +342 -342
  144. package/lib/engine/engine_time.d.ts +19 -19
  145. package/lib/engine/engine_time.js +47 -47
  146. package/lib/engine/engine_types.d.ts +358 -358
  147. package/lib/engine/engine_types.js +72 -72
  148. package/lib/engine/engine_typestore.d.ts +16 -16
  149. package/lib/engine/engine_typestore.js +35 -35
  150. package/lib/engine/engine_util_decorator.d.ts +12 -12
  151. package/lib/engine/engine_util_decorator.js +115 -115
  152. package/lib/engine/engine_utils.d.ts +104 -104
  153. package/lib/engine/engine_utils.js +518 -518
  154. package/lib/engine/engine_utils_screenshot.d.ts +10 -10
  155. package/lib/engine/engine_utils_screenshot.js +70 -70
  156. package/lib/engine/engine_web_api.d.ts +12 -12
  157. package/lib/engine/engine_web_api.js +112 -112
  158. package/lib/engine/extensions/EXT_texture_exr.d.ts +8 -8
  159. package/lib/engine/extensions/EXT_texture_exr.js +32 -32
  160. package/lib/engine/extensions/NEEDLE_animator_controller_model.d.ts +116 -116
  161. package/lib/engine/extensions/NEEDLE_animator_controller_model.js +91 -91
  162. package/lib/engine/extensions/NEEDLE_components.d.ts +33 -33
  163. package/lib/engine/extensions/NEEDLE_components.js +206 -206
  164. package/lib/engine/extensions/NEEDLE_gameobject_data.d.ts +10 -10
  165. package/lib/engine/extensions/NEEDLE_gameobject_data.js +57 -57
  166. package/lib/engine/extensions/NEEDLE_lighting_settings.d.ts +38 -38
  167. package/lib/engine/extensions/NEEDLE_lighting_settings.js +183 -183
  168. package/lib/engine/extensions/NEEDLE_lightmaps.d.ts +18 -18
  169. package/lib/engine/extensions/NEEDLE_lightmaps.js +108 -108
  170. package/lib/engine/extensions/NEEDLE_persistent_assets.d.ts +11 -11
  171. package/lib/engine/extensions/NEEDLE_persistent_assets.js +63 -63
  172. package/lib/engine/extensions/NEEDLE_progressive.d.ts +41 -41
  173. package/lib/engine/extensions/NEEDLE_progressive.js +366 -366
  174. package/lib/engine/extensions/NEEDLE_render_objects.d.ts +13 -13
  175. package/lib/engine/extensions/NEEDLE_render_objects.js +159 -159
  176. package/lib/engine/extensions/NEEDLE_techniques_webgl.d.ts +39 -39
  177. package/lib/engine/extensions/NEEDLE_techniques_webgl.js +544 -544
  178. package/lib/engine/extensions/extension_resolver.d.ts +4 -4
  179. package/lib/engine/extensions/extension_resolver.js +1 -1
  180. package/lib/engine/extensions/extension_utils.d.ts +2 -2
  181. package/lib/engine/extensions/extension_utils.js +140 -140
  182. package/lib/engine/extensions/extensions.d.ts +21 -21
  183. package/lib/engine/extensions/extensions.js +94 -94
  184. package/lib/engine/extensions/index.d.ts +5 -5
  185. package/lib/engine/extensions/index.js +5 -5
  186. package/lib/engine/extensions/usage_tracker.d.ts +13 -13
  187. package/lib/engine/extensions/usage_tracker.js +61 -61
  188. package/lib/engine/js-extensions/Camera.d.ts +1 -1
  189. package/lib/engine/js-extensions/Camera.js +36 -36
  190. package/lib/engine/js-extensions/Layers.d.ts +3 -3
  191. package/lib/engine/js-extensions/Layers.js +19 -19
  192. package/lib/engine/js-extensions/index.d.ts +2 -2
  193. package/lib/engine/js-extensions/index.js +2 -2
  194. package/lib/engine/shaders/shaderData.d.ts +55 -55
  195. package/lib/engine/shaders/shaderData.js +58 -58
  196. package/lib/engine/tests/test_utils.d.ts +2 -2
  197. package/lib/engine/tests/test_utils.js +53 -53
  198. package/lib/engine-components/AlignmentConstraint.d.ts +10 -10
  199. package/lib/engine-components/AlignmentConstraint.js +39 -39
  200. package/lib/engine-components/Animation.d.ts +53 -53
  201. package/lib/engine-components/Animation.js +333 -333
  202. package/lib/engine-components/AnimationCurve.d.ts +16 -16
  203. package/lib/engine-components/AnimationCurve.js +97 -97
  204. package/lib/engine-components/AnimationUtils.d.ts +8 -8
  205. package/lib/engine-components/AnimationUtils.js +110 -110
  206. package/lib/engine-components/Animator.d.ts +81 -81
  207. package/lib/engine-components/Animator.js +229 -229
  208. package/lib/engine-components/AnimatorController.d.ts +57 -57
  209. package/lib/engine-components/AnimatorController.js +887 -887
  210. package/lib/engine-components/AnimatorController.js.map +1 -1
  211. package/lib/engine-components/AudioListener.d.ts +7 -7
  212. package/lib/engine-components/AudioListener.js +30 -30
  213. package/lib/engine-components/AudioSource.d.ts +61 -61
  214. package/lib/engine-components/AudioSource.js +422 -422
  215. package/lib/engine-components/AvatarLoader.d.ts +19 -19
  216. package/lib/engine-components/AvatarLoader.js +173 -173
  217. package/lib/engine-components/AxesHelper.d.ts +9 -9
  218. package/lib/engine-components/AxesHelper.js +44 -44
  219. package/lib/engine-components/BasicIKConstraint.d.ts +9 -9
  220. package/lib/engine-components/BasicIKConstraint.js +43 -43
  221. package/lib/engine-components/BoxHelperComponent.d.ts +16 -16
  222. package/lib/engine-components/BoxHelperComponent.js +89 -89
  223. package/lib/engine-components/Camera.d.ts +70 -70
  224. package/lib/engine-components/Camera.js +450 -450
  225. package/lib/engine-components/CameraUtils.d.ts +1 -1
  226. package/lib/engine-components/CameraUtils.js +77 -77
  227. package/lib/engine-components/CharacterController.d.ts +46 -46
  228. package/lib/engine-components/CharacterController.js +227 -227
  229. package/lib/engine-components/Collider.d.ts +46 -46
  230. package/lib/engine-components/Collider.js +153 -153
  231. package/lib/engine-components/Component.d.ts +228 -228
  232. package/lib/engine-components/Component.js +541 -541
  233. package/lib/engine-components/ContactShadows.d.ts +23 -23
  234. package/lib/engine-components/ContactShadows.js +233 -233
  235. package/lib/engine-components/DeleteBox.d.ts +9 -9
  236. package/lib/engine-components/DeleteBox.js +30 -30
  237. package/lib/engine-components/DeviceFlag.d.ts +12 -12
  238. package/lib/engine-components/DeviceFlag.js +43 -43
  239. package/lib/engine-components/DragControls.d.ts +51 -51
  240. package/lib/engine-components/DragControls.js +516 -516
  241. package/lib/engine-components/DropListener.d.ts +15 -15
  242. package/lib/engine-components/DropListener.js +120 -120
  243. package/lib/engine-components/Duplicatable.d.ts +16 -16
  244. package/lib/engine-components/Duplicatable.js +150 -150
  245. package/lib/engine-components/EventList.d.ts +28 -28
  246. package/lib/engine-components/EventList.js +105 -105
  247. package/lib/engine-components/EventTrigger.d.ts +12 -12
  248. package/lib/engine-components/EventTrigger.js +50 -50
  249. package/lib/engine-components/EventType.d.ts +19 -19
  250. package/lib/engine-components/EventType.js +71 -71
  251. package/lib/engine-components/FlyControls.d.ts +7 -7
  252. package/lib/engine-components/FlyControls.js +25 -25
  253. package/lib/engine-components/Fog.d.ts +20 -20
  254. package/lib/engine-components/Fog.js +60 -60
  255. package/lib/engine-components/Gizmos.d.ts +12 -12
  256. package/lib/engine-components/Gizmos.js +60 -60
  257. package/lib/engine-components/GridHelper.d.ts +12 -12
  258. package/lib/engine-components/GridHelper.js +47 -47
  259. package/lib/engine-components/GroundProjection.d.ts +21 -21
  260. package/lib/engine-components/GroundProjection.js +97 -97
  261. package/lib/engine-components/Interactable.d.ts +10 -10
  262. package/lib/engine-components/Interactable.js +11 -11
  263. package/lib/engine-components/Joints.d.ts +19 -19
  264. package/lib/engine-components/Joints.js +51 -51
  265. package/lib/engine-components/LODGroup.d.ts +30 -30
  266. package/lib/engine-components/LODGroup.js +145 -145
  267. package/lib/engine-components/Light.d.ts +75 -75
  268. package/lib/engine-components/Light.js +475 -475
  269. package/lib/engine-components/LookAtConstraint.d.ts +7 -7
  270. package/lib/engine-components/LookAtConstraint.js +17 -17
  271. package/lib/engine-components/NestedGltf.d.ts +11 -11
  272. package/lib/engine-components/NestedGltf.js +74 -74
  273. package/lib/engine-components/Networking.d.ts +11 -11
  274. package/lib/engine-components/Networking.js +70 -70
  275. package/lib/engine-components/OffsetConstraint.d.ts +14 -14
  276. package/lib/engine-components/OffsetConstraint.js +65 -65
  277. package/lib/engine-components/OrbitControls.d.ts +111 -111
  278. package/lib/engine-components/OrbitControls.js +646 -646
  279. package/lib/engine-components/ParticleSystem.d.ts +145 -145
  280. package/lib/engine-components/ParticleSystem.js +1077 -1077
  281. package/lib/engine-components/ParticleSystemModules.d.ts +489 -489
  282. package/lib/engine-components/ParticleSystemModules.js +1667 -1667
  283. package/lib/engine-components/ParticleSystemSubEmitter.d.ts +25 -25
  284. package/lib/engine-components/ParticleSystemSubEmitter.js +86 -86
  285. package/lib/engine-components/PlayerColor.d.ts +13 -13
  286. package/lib/engine-components/PlayerColor.js +83 -83
  287. package/lib/engine-components/ReflectionProbe.d.ts +22 -22
  288. package/lib/engine-components/ReflectionProbe.js +181 -181
  289. package/lib/engine-components/Renderer.d.ts +112 -112
  290. package/lib/engine-components/Renderer.js +1029 -1029
  291. package/lib/engine-components/RendererLightmap.d.ts +19 -19
  292. package/lib/engine-components/RendererLightmap.js +127 -127
  293. package/lib/engine-components/RigidBody.d.ts +120 -120
  294. package/lib/engine-components/RigidBody.js +452 -452
  295. package/lib/engine-components/SceneSwitcher.d.ts +72 -72
  296. package/lib/engine-components/SceneSwitcher.js +583 -583
  297. package/lib/engine-components/ScreenCapture.d.ts +64 -64
  298. package/lib/engine-components/ScreenCapture.js +405 -405
  299. package/lib/engine-components/ShadowCatcher.d.ts +18 -18
  300. package/lib/engine-components/ShadowCatcher.js +144 -144
  301. package/lib/engine-components/Skybox.d.ts +23 -23
  302. package/lib/engine-components/Skybox.js +287 -287
  303. package/lib/engine-components/SmoothFollow.d.ts +14 -14
  304. package/lib/engine-components/SmoothFollow.js +63 -63
  305. package/lib/engine-components/SpatialTrigger.d.ts +27 -27
  306. package/lib/engine-components/SpatialTrigger.js +144 -144
  307. package/lib/engine-components/SpectatorCamera.d.ts +45 -45
  308. package/lib/engine-components/SpectatorCamera.js +593 -593
  309. package/lib/engine-components/SpriteRenderer.d.ts +48 -48
  310. package/lib/engine-components/SpriteRenderer.js +257 -257
  311. package/lib/engine-components/SyncedCamera.d.ts +27 -27
  312. package/lib/engine-components/SyncedCamera.js +187 -187
  313. package/lib/engine-components/SyncedRoom.d.ts +24 -24
  314. package/lib/engine-components/SyncedRoom.js +162 -162
  315. package/lib/engine-components/SyncedTransform.d.ts +35 -35
  316. package/lib/engine-components/SyncedTransform.js +265 -265
  317. package/lib/engine-components/TestRunner.d.ts +13 -13
  318. package/lib/engine-components/TestRunner.js +99 -99
  319. package/lib/engine-components/TransformGizmo.d.ts +16 -16
  320. package/lib/engine-components/TransformGizmo.js +148 -148
  321. package/lib/engine-components/VideoPlayer.d.ts +86 -86
  322. package/lib/engine-components/VideoPlayer.js +792 -792
  323. package/lib/engine-components/Voip.d.ts +29 -29
  324. package/lib/engine-components/Voip.js +203 -203
  325. package/lib/engine-components/XRFlag.d.ts +33 -33
  326. package/lib/engine-components/XRFlag.js +128 -128
  327. package/lib/engine-components/api.d.ts +15 -15
  328. package/lib/engine-components/api.js +15 -15
  329. package/lib/engine-components/avatar/AvatarBlink_Simple.d.ts +10 -10
  330. package/lib/engine-components/avatar/AvatarBlink_Simple.js +75 -75
  331. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.d.ts +13 -13
  332. package/lib/engine-components/avatar/AvatarEyeLook_Rotation.js +74 -74
  333. package/lib/engine-components/avatar/Avatar_Brain_LookAt.d.ts +27 -27
  334. package/lib/engine-components/avatar/Avatar_Brain_LookAt.js +119 -119
  335. package/lib/engine-components/avatar/Avatar_MouthShapes.d.ts +13 -13
  336. package/lib/engine-components/avatar/Avatar_MouthShapes.js +78 -78
  337. package/lib/engine-components/avatar/Avatar_MustacheShake.d.ts +8 -8
  338. package/lib/engine-components/avatar/Avatar_MustacheShake.js +28 -28
  339. package/lib/engine-components/codegen/components.d.ts +216 -216
  340. package/lib/engine-components/codegen/components.js +217 -217
  341. package/lib/engine-components/debug/LogStats.d.ts +5 -5
  342. package/lib/engine-components/debug/LogStats.js +18 -18
  343. package/lib/engine-components/export/gltf/GltfExport.d.ts +25 -25
  344. package/lib/engine-components/export/gltf/GltfExport.js +215 -215
  345. package/lib/engine-components/export/index.d.ts +1 -1
  346. package/lib/engine-components/export/index.js +1 -1
  347. package/lib/engine-components/export/usdz/Extension.d.ts +10 -10
  348. package/lib/engine-components/export/usdz/Extension.js +1 -1
  349. package/lib/engine-components/export/usdz/ThreeUSDZExporter.d.ts +114 -114
  350. package/lib/engine-components/export/usdz/ThreeUSDZExporter.js +1211 -1211
  351. package/lib/engine-components/export/usdz/USDZExporter.d.ts +59 -59
  352. package/lib/engine-components/export/usdz/USDZExporter.js +450 -450
  353. package/lib/engine-components/export/usdz/extensions/Animation.d.ts +69 -69
  354. package/lib/engine-components/export/usdz/extensions/Animation.js +650 -650
  355. package/lib/engine-components/export/usdz/extensions/DocumentExtension.d.ts +5 -5
  356. package/lib/engine-components/export/usdz/extensions/DocumentExtension.js +6 -6
  357. package/lib/engine-components/export/usdz/extensions/USDZText.d.ts +55 -55
  358. package/lib/engine-components/export/usdz/extensions/USDZText.js +246 -246
  359. package/lib/engine-components/export/usdz/extensions/USDZUI.d.ts +8 -8
  360. package/lib/engine-components/export/usdz/extensions/USDZUI.js +100 -100
  361. package/lib/engine-components/export/usdz/extensions/behavior/Actions.d.ts +30 -30
  362. package/lib/engine-components/export/usdz/extensions/behavior/Actions.js +88 -88
  363. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.d.ts +9 -9
  364. package/lib/engine-components/export/usdz/extensions/behavior/AudioExtension.js +52 -52
  365. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.d.ts +22 -22
  366. package/lib/engine-components/export/usdz/extensions/behavior/Behaviour.js +134 -134
  367. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.d.ts +126 -126
  368. package/lib/engine-components/export/usdz/extensions/behavior/BehaviourComponents.js +824 -824
  369. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.d.ts +133 -133
  370. package/lib/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.js +464 -464
  371. package/lib/engine-components/export/usdz/index.d.ts +3 -3
  372. package/lib/engine-components/export/usdz/index.js +2 -2
  373. package/lib/engine-components/export/usdz/utils/animationutils.d.ts +3 -3
  374. package/lib/engine-components/export/usdz/utils/animationutils.js +85 -85
  375. package/lib/engine-components/export/usdz/utils/quicklook.d.ts +2 -2
  376. package/lib/engine-components/export/usdz/utils/quicklook.js +35 -35
  377. package/lib/engine-components/export/usdz/utils/timeutils.d.ts +1 -1
  378. package/lib/engine-components/export/usdz/utils/timeutils.js +14 -14
  379. package/lib/engine-components/js-extensions/ExtensionUtils.d.ts +6 -6
  380. package/lib/engine-components/js-extensions/ExtensionUtils.js +65 -65
  381. package/lib/engine-components/js-extensions/Object3D.d.ts +2 -2
  382. package/lib/engine-components/js-extensions/Object3D.js +140 -140
  383. package/lib/engine-components/js-extensions/RGBAColor.d.ts +14 -14
  384. package/lib/engine-components/js-extensions/RGBAColor.js +49 -49
  385. package/lib/engine-components/js-extensions/index.d.ts +3 -3
  386. package/lib/engine-components/js-extensions/index.js +3 -3
  387. package/lib/engine-components/postprocessing/Effects/Antialiasing.d.ts +13 -13
  388. package/lib/engine-components/postprocessing/Effects/Antialiasing.js +45 -45
  389. package/lib/engine-components/postprocessing/Effects/Bloom.d.ts +12 -12
  390. package/lib/engine-components/postprocessing/Effects/Bloom.js +77 -77
  391. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.d.ts +8 -8
  392. package/lib/engine-components/postprocessing/Effects/ChromaticAberration.js +38 -38
  393. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.d.ts +12 -12
  394. package/lib/engine-components/postprocessing/Effects/ColorAdjustments.js +81 -81
  395. package/lib/engine-components/postprocessing/Effects/DepthOfField.d.ts +21 -21
  396. package/lib/engine-components/postprocessing/Effects/DepthOfField.js +97 -97
  397. package/lib/engine-components/postprocessing/Effects/Pixelation.d.ts +7 -7
  398. package/lib/engine-components/postprocessing/Effects/Pixelation.js +28 -28
  399. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.d.ts +13 -13
  400. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.js +86 -86
  401. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.d.ts +24 -24
  402. package/lib/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.js +94 -94
  403. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.d.ts +13 -13
  404. package/lib/engine-components/postprocessing/Effects/TiltShiftEffect.js +62 -62
  405. package/lib/engine-components/postprocessing/Effects/Tonemapping.d.ts +16 -16
  406. package/lib/engine-components/postprocessing/Effects/Tonemapping.js +51 -51
  407. package/lib/engine-components/postprocessing/Effects/Vignette.d.ts +11 -11
  408. package/lib/engine-components/postprocessing/Effects/Vignette.js +56 -56
  409. package/lib/engine-components/postprocessing/PostProcessingEffect.d.ts +33 -33
  410. package/lib/engine-components/postprocessing/PostProcessingEffect.js +126 -126
  411. package/lib/engine-components/postprocessing/PostProcessingHandler.d.ts +22 -22
  412. package/lib/engine-components/postprocessing/PostProcessingHandler.js +201 -201
  413. package/lib/engine-components/postprocessing/Volume.d.ts +25 -25
  414. package/lib/engine-components/postprocessing/Volume.js +193 -193
  415. package/lib/engine-components/postprocessing/VolumeParameter.d.ts +22 -22
  416. package/lib/engine-components/postprocessing/VolumeParameter.js +80 -80
  417. package/lib/engine-components/postprocessing/VolumeProfile.d.ts +7 -7
  418. package/lib/engine-components/postprocessing/VolumeProfile.js +41 -41
  419. package/lib/engine-components/postprocessing/index.d.ts +4 -4
  420. package/lib/engine-components/postprocessing/index.js +4 -4
  421. package/lib/engine-components/timeline/PlayableDirector.d.ts +107 -107
  422. package/lib/engine-components/timeline/PlayableDirector.js +624 -624
  423. package/lib/engine-components/timeline/SignalAsset.d.ts +18 -18
  424. package/lib/engine-components/timeline/SignalAsset.js +124 -124
  425. package/lib/engine-components/timeline/TimelineModels.d.ts +88 -88
  426. package/lib/engine-components/timeline/TimelineModels.js +22 -22
  427. package/lib/engine-components/timeline/TimelineTracks.d.ts +90 -90
  428. package/lib/engine-components/timeline/TimelineTracks.js +825 -825
  429. package/lib/engine-components/timeline/index.d.ts +4 -4
  430. package/lib/engine-components/timeline/index.js +3 -3
  431. package/lib/engine-components/ui/BaseUIComponent.d.ts +31 -31
  432. package/lib/engine-components/ui/BaseUIComponent.js +161 -161
  433. package/lib/engine-components/ui/Button.d.ts +56 -56
  434. package/lib/engine-components/ui/Button.js +282 -282
  435. package/lib/engine-components/ui/Canvas.d.ts +67 -67
  436. package/lib/engine-components/ui/Canvas.js +382 -382
  437. package/lib/engine-components/ui/CanvasGroup.d.ts +15 -15
  438. package/lib/engine-components/ui/CanvasGroup.js +53 -53
  439. package/lib/engine-components/ui/EventSystem.d.ts +102 -102
  440. package/lib/engine-components/ui/EventSystem.js +641 -641
  441. package/lib/engine-components/ui/Graphic.d.ts +45 -45
  442. package/lib/engine-components/ui/Graphic.js +236 -236
  443. package/lib/engine-components/ui/Image.d.ts +27 -27
  444. package/lib/engine-components/ui/Image.js +107 -107
  445. package/lib/engine-components/ui/InputField.d.ts +34 -34
  446. package/lib/engine-components/ui/InputField.js +234 -234
  447. package/lib/engine-components/ui/Interfaces.d.ts +38 -38
  448. package/lib/engine-components/ui/Interfaces.js +12 -12
  449. package/lib/engine-components/ui/Layout.d.ts +72 -72
  450. package/lib/engine-components/ui/Layout.js +318 -318
  451. package/lib/engine-components/ui/Outline.d.ts +7 -7
  452. package/lib/engine-components/ui/Outline.js +20 -20
  453. package/lib/engine-components/ui/PointerEvents.d.ts +64 -64
  454. package/lib/engine-components/ui/PointerEvents.js +68 -68
  455. package/lib/engine-components/ui/RaycastUtils.d.ts +11 -11
  456. package/lib/engine-components/ui/RaycastUtils.js +67 -67
  457. package/lib/engine-components/ui/Raycaster.d.ts +18 -18
  458. package/lib/engine-components/ui/Raycaster.js +69 -69
  459. package/lib/engine-components/ui/RectTransform.d.ts +61 -61
  460. package/lib/engine-components/ui/RectTransform.js +343 -343
  461. package/lib/engine-components/ui/SpatialHtml.d.ts +6 -6
  462. package/lib/engine-components/ui/SpatialHtml.js +57 -57
  463. package/lib/engine-components/ui/Text.d.ts +74 -74
  464. package/lib/engine-components/ui/Text.js +534 -534
  465. package/lib/engine-components/ui/Utils.d.ts +23 -23
  466. package/lib/engine-components/ui/Utils.js +90 -90
  467. package/lib/engine-components/ui/index.d.ts +1 -1
  468. package/lib/engine-components/ui/index.js +1 -1
  469. package/lib/engine-components/utils/LookAt.d.ts +13 -13
  470. package/lib/engine-components/utils/LookAt.js +59 -59
  471. package/lib/engine-components/utils/OpenURL.d.ts +21 -21
  472. package/lib/engine-components/utils/OpenURL.js +124 -124
  473. package/lib/engine-components/webxr/WebARCameraBackground.d.ts +19 -19
  474. package/lib/engine-components/webxr/WebARCameraBackground.js +193 -193
  475. package/lib/engine-components/webxr/WebARSessionRoot.d.ts +38 -38
  476. package/lib/engine-components/webxr/WebARSessionRoot.js +407 -407
  477. package/lib/engine-components/webxr/WebXR.d.ts +110 -110
  478. package/lib/engine-components/webxr/WebXR.js +672 -672
  479. package/lib/engine-components/webxr/WebXRAvatar.d.ts +61 -61
  480. package/lib/engine-components/webxr/WebXRAvatar.js +289 -289
  481. package/lib/engine-components/webxr/WebXRController.d.ts +154 -154
  482. package/lib/engine-components/webxr/WebXRController.js +1028 -1028
  483. package/lib/engine-components/webxr/WebXRGrabRendering.d.ts +42 -42
  484. package/lib/engine-components/webxr/WebXRGrabRendering.js +137 -137
  485. package/lib/engine-components/webxr/WebXRImageTracking.d.ts +49 -49
  486. package/lib/engine-components/webxr/WebXRImageTracking.js +336 -336
  487. package/lib/engine-components/webxr/WebXRPlaneTracking.d.ts +49 -49
  488. package/lib/engine-components/webxr/WebXRPlaneTracking.js +372 -372
  489. package/lib/engine-components/webxr/WebXRRig.d.ts +4 -4
  490. package/lib/engine-components/webxr/WebXRRig.js +19 -19
  491. package/lib/engine-components/webxr/WebXRSync.d.ts +54 -54
  492. package/lib/engine-components/webxr/WebXRSync.js +410 -410
  493. package/lib/engine-components/webxr/index.d.ts +4 -4
  494. package/lib/engine-components/webxr/index.js +4 -4
  495. package/lib/engine-components-experimental/Presentation.d.ts +6 -6
  496. package/lib/engine-components-experimental/Presentation.js +9 -9
  497. package/lib/engine-components-experimental/api.d.ts +1 -1
  498. package/lib/engine-components-experimental/api.js +1 -1
  499. package/lib/engine-components-experimental/networking/PlayerSync.d.ts +50 -50
  500. package/lib/engine-components-experimental/networking/PlayerSync.js +200 -200
  501. package/lib/engine-schemes/api.d.ts +1 -1
  502. package/lib/engine-schemes/api.js +1 -1
  503. package/lib/engine-schemes/schemes.d.ts +7 -7
  504. package/lib/engine-schemes/schemes.js +19 -19
  505. package/lib/engine-schemes/synced-camera-model.d.ts +25 -25
  506. package/lib/engine-schemes/synced-camera-model.js +67 -67
  507. package/lib/engine-schemes/synced-transform-model.d.ts +31 -31
  508. package/lib/engine-schemes/synced-transform-model.js +66 -66
  509. package/lib/engine-schemes/transform.d.ts +12 -12
  510. package/lib/engine-schemes/transform.js +39 -39
  511. package/lib/engine-schemes/vec3.d.ts +11 -11
  512. package/lib/engine-schemes/vec3.js +29 -29
  513. package/lib/engine-schemes/vec4.d.ts +12 -12
  514. package/lib/engine-schemes/vec4.js +33 -33
  515. package/lib/engine-schemes/vr-user-state-buffer.d.ts +36 -36
  516. package/lib/engine-schemes/vr-user-state-buffer.js +103 -103
  517. package/lib/include/three/ARButton.d.ts +3 -3
  518. package/lib/include/three/ARButton.js +151 -151
  519. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.d.ts +6 -6
  520. package/lib/include/three/EXT_mesh_gpu_instancing_exporter.js +45 -45
  521. package/lib/include/three/VRButton.d.ts +5 -5
  522. package/lib/include/three/VRButton.js +118 -118
  523. package/lib/needle-engine.d.ts +6 -6
  524. package/lib/needle-engine.js +49 -49
  525. package/package.json +1 -1
  526. package/plugins/common/config.cjs +14 -14
  527. package/plugins/common/config.js +19 -19
  528. package/plugins/common/generator.js +10 -10
  529. package/plugins/common/license.cjs +30 -30
  530. package/plugins/common/version.js +11 -11
  531. package/plugins/next/license.cjs +4 -4
  532. package/plugins/next/next.js +70 -70
  533. package/plugins/types/index.d.ts +1 -1
  534. package/plugins/types/needleConfig.d.ts +21 -21
  535. package/plugins/types/userconfig.d.ts +42 -42
  536. package/plugins/vite/alias.js +70 -70
  537. package/plugins/vite/build.js +19 -19
  538. package/plugins/vite/config.js +73 -73
  539. package/plugins/vite/copyfiles.js +134 -134
  540. package/plugins/vite/defines.js +45 -45
  541. package/plugins/vite/dependency-watcher.js +224 -224
  542. package/plugins/vite/drop-client.js +76 -76
  543. package/plugins/vite/drop.js +82 -82
  544. package/plugins/vite/editor-connection.js +121 -121
  545. package/plugins/vite/facebook-instant-games.js +99 -99
  546. package/plugins/vite/gzip.js +5 -5
  547. package/plugins/vite/imports-logger.js +143 -143
  548. package/plugins/vite/index.js +81 -81
  549. package/plugins/vite/license.js +42 -42
  550. package/plugins/vite/meta.js +149 -149
  551. package/plugins/vite/peer.js +31 -31
  552. package/plugins/vite/poster-client.js +59 -59
  553. package/plugins/vite/poster.js +73 -73
  554. package/plugins/vite/reload-client.js +15 -15
  555. package/plugins/vite/reload.js +363 -363
  556. package/plugins/vite/transform-codegen.js +55 -55
  557. package/plugins/vite/vite-4.4-hack.js +31 -31
  558. package/src/engine/api.ts +54 -54
  559. package/src/engine/assets/index.ts +4 -4
  560. package/src/engine/codegen/register_types.ts +441 -441
  561. package/src/engine/debug/debug.ts +29 -29
  562. package/src/engine/debug/debug_console.ts +213 -213
  563. package/src/engine/debug/debug_overlay.ts +283 -283
  564. package/src/engine/engine.ts +13 -13
  565. package/src/engine/engine_addressables.ts +494 -494
  566. package/src/engine/engine_application.ts +53 -53
  567. package/src/engine/engine_assetdatabase.ts +383 -383
  568. package/src/engine/engine_camera.ts +32 -32
  569. package/src/engine/engine_components.ts +266 -266
  570. package/src/engine/engine_components_internal.ts +42 -42
  571. package/src/engine/engine_constants.ts +42 -42
  572. package/src/engine/engine_context.ts +1386 -1386
  573. package/src/engine/engine_context_registry.ts +103 -103
  574. package/src/engine/engine_coroutine.ts +24 -24
  575. package/src/engine/engine_create_objects.ts +39 -39
  576. package/src/engine/engine_default_parameters.ts +3 -3
  577. package/src/engine/engine_editor-sync.ts +29 -29
  578. package/src/engine/engine_element.ts +592 -592
  579. package/src/engine/engine_element_attributes.ts +61 -61
  580. package/src/engine/engine_element_extras.ts +16 -16
  581. package/src/engine/engine_element_loading.ts +341 -341
  582. package/src/engine/engine_element_overlay.ts +160 -160
  583. package/src/engine/engine_fileloader.js +8 -8
  584. package/src/engine/engine_gameobject.ts +621 -621
  585. package/src/engine/engine_generic_utils.js +13 -13
  586. package/src/engine/engine_gizmos.ts +321 -321
  587. package/src/engine/engine_gltf.ts +30 -30
  588. package/src/engine/engine_gltf_builtin_components.ts +350 -350
  589. package/src/engine/engine_hot_reload.ts +196 -196
  590. package/src/engine/engine_input.ts +879 -879
  591. package/src/engine/engine_input_utils.ts +23 -23
  592. package/src/engine/engine_instancing.ts +42 -42
  593. package/src/engine/engine_license.ts +413 -413
  594. package/src/engine/engine_lifecycle_api.ts +29 -29
  595. package/src/engine/engine_lifecycle_functions_internal.ts +36 -36
  596. package/src/engine/engine_lightdata.ts +113 -113
  597. package/src/engine/engine_loaders.ts +77 -77
  598. package/src/engine/engine_mainloop_utils.ts +431 -431
  599. package/src/engine/engine_math.ts +174 -174
  600. package/src/engine/engine_networking.ts +742 -742
  601. package/src/engine/engine_networking_auto.ts +373 -373
  602. package/src/engine/engine_networking_files.ts +206 -206
  603. package/src/engine/engine_networking_files_default_components.ts +54 -54
  604. package/src/engine/engine_networking_instantiate.ts +362 -362
  605. package/src/engine/engine_networking_peer.ts +158 -158
  606. package/src/engine/engine_networking_streams.ts +489 -489
  607. package/src/engine/engine_networking_types.ts +18 -18
  608. package/src/engine/engine_networking_utils.ts +23 -23
  609. package/src/engine/engine_networking_websocket.ts +2 -2
  610. package/src/engine/engine_patcher.ts +199 -199
  611. package/src/engine/engine_physics.ts +287 -287
  612. package/src/engine/engine_physics.types.ts +43 -43
  613. package/src/engine/engine_physics_rapier.ts +1385 -1385
  614. package/src/engine/engine_playerview.ts +79 -79
  615. package/src/engine/engine_scenelighting.ts +313 -313
  616. package/src/engine/engine_scenetools.ts +242 -242
  617. package/src/engine/engine_serialization.ts +6 -6
  618. package/src/engine/engine_serialization_builtin_serializer.ts +415 -415
  619. package/src/engine/engine_serialization_core.ts +680 -680
  620. package/src/engine/engine_serialization_decorator.ts +68 -68
  621. package/src/engine/engine_setup.ts +1 -1
  622. package/src/engine/engine_shaders.ts +242 -242
  623. package/src/engine/engine_shims.ts +28 -28
  624. package/src/engine/engine_texture.ts +70 -70
  625. package/src/engine/engine_three_utils.ts +382 -382
  626. package/src/engine/engine_time.ts +55 -55
  627. package/src/engine/engine_types.ts +489 -489
  628. package/src/engine/engine_typestore.ts +41 -41
  629. package/src/engine/engine_util_decorator.ts +134 -134
  630. package/src/engine/engine_utils.ts +605 -605
  631. package/src/engine/engine_utils_screenshot.ts +84 -84
  632. package/src/engine/engine_web_api.ts +119 -119
  633. package/src/engine/extensions/EXT_texture_exr.ts +49 -49
  634. package/src/engine/extensions/NEEDLE_animator_controller_model.ts +193 -193
  635. package/src/engine/extensions/NEEDLE_components.ts +250 -250
  636. package/src/engine/extensions/NEEDLE_gameobject_data.ts +82 -82
  637. package/src/engine/extensions/NEEDLE_lighting_settings.ts +210 -210
  638. package/src/engine/extensions/NEEDLE_lightmaps.ts +130 -130
  639. package/src/engine/extensions/NEEDLE_persistent_assets.ts +75 -75
  640. package/src/engine/extensions/NEEDLE_progressive.ts +412 -412
  641. package/src/engine/extensions/NEEDLE_render_objects.ts +209 -209
  642. package/src/engine/extensions/NEEDLE_techniques_webgl.ts +618 -618
  643. package/src/engine/extensions/extension_resolver.ts +4 -4
  644. package/src/engine/extensions/extension_utils.ts +149 -149
  645. package/src/engine/extensions/extensions.ts +118 -118
  646. package/src/engine/extensions/index.ts +4 -4
  647. package/src/engine/extensions/usage_tracker.ts +95 -95
  648. package/src/engine/js-extensions/Camera.ts +34 -34
  649. package/src/engine/js-extensions/Layers.ts +19 -19
  650. package/src/engine/js-extensions/index.ts +1 -1
  651. package/src/engine/shaders/shaderData.ts +67 -67
  652. package/src/engine/tests/test_utils.ts +63 -63
  653. package/src/engine-components/AlignmentConstraint.ts +35 -35
  654. package/src/engine-components/Animation.ts +345 -345
  655. package/src/engine-components/AnimationCurve.ts +83 -83
  656. package/src/engine-components/AnimationUtils.ts +117 -117
  657. package/src/engine-components/Animator.ts +243 -243
  658. package/src/engine-components/AnimatorController.ts +1020 -1020
  659. package/src/engine-components/AudioListener.ts +32 -32
  660. package/src/engine-components/AudioSource.ts +419 -419
  661. package/src/engine-components/AvatarLoader.ts +204 -204
  662. package/src/engine-components/AxesHelper.ts +33 -33
  663. package/src/engine-components/BasicIKConstraint.ts +53 -53
  664. package/src/engine-components/BoxCollider.ts +1 -1
  665. package/src/engine-components/BoxHelperComponent.ts +100 -100
  666. package/src/engine-components/Camera.ts +454 -454
  667. package/src/engine-components/CameraUtils.ts +89 -89
  668. package/src/engine-components/CharacterController.ts +243 -243
  669. package/src/engine-components/Collider.ts +160 -160
  670. package/src/engine-components/Component.ts +670 -670
  671. package/src/engine-components/ContactShadows.ts +265 -265
  672. package/src/engine-components/DeleteBox.ts +35 -35
  673. package/src/engine-components/DeviceFlag.ts +42 -42
  674. package/src/engine-components/DragControls.ts +574 -574
  675. package/src/engine-components/DropListener.ts +112 -112
  676. package/src/engine-components/Duplicatable.ts +146 -146
  677. package/src/engine-components/EventList.ts +125 -125
  678. package/src/engine-components/EventTrigger.ts +47 -47
  679. package/src/engine-components/EventType.ts +87 -87
  680. package/src/engine-components/FlyControls.ts +31 -31
  681. package/src/engine-components/Fog.ts +59 -59
  682. package/src/engine-components/Gizmos.ts +52 -52
  683. package/src/engine-components/GridHelper.ts +40 -40
  684. package/src/engine-components/GroundProjection.ts +97 -97
  685. package/src/engine-components/Interactable.ts +18 -18
  686. package/src/engine-components/Joints.ts +51 -51
  687. package/src/engine-components/LODGroup.ts +145 -145
  688. package/src/engine-components/Light.ts +493 -493
  689. package/src/engine-components/LookAtConstraint.ts +11 -11
  690. package/src/engine-components/NestedGltf.ts +70 -70
  691. package/src/engine-components/Networking.ts +72 -72
  692. package/src/engine-components/OffsetConstraint.ts +59 -59
  693. package/src/engine-components/OrbitControls.ts +653 -653
  694. package/src/engine-components/ParticleSystem.ts +1192 -1192
  695. package/src/engine-components/ParticleSystemModules.ts +1481 -1481
  696. package/src/engine-components/ParticleSystemSubEmitter.ts +110 -110
  697. package/src/engine-components/PlayerColor.ts +93 -93
  698. package/src/engine-components/ReflectionProbe.ts +192 -192
  699. package/src/engine-components/Renderer.ts +1125 -1125
  700. package/src/engine-components/RendererLightmap.ts +145 -145
  701. package/src/engine-components/RigidBody.ts +453 -453
  702. package/src/engine-components/SceneSwitcher.ts +594 -594
  703. package/src/engine-components/ScreenCapture.ts +437 -437
  704. package/src/engine-components/ShadowCatcher.ts +149 -149
  705. package/src/engine-components/Skybox.ts +281 -281
  706. package/src/engine-components/SmoothFollow.ts +57 -57
  707. package/src/engine-components/SpatialTrigger.ts +142 -142
  708. package/src/engine-components/SpectatorCamera.ts +675 -675
  709. package/src/engine-components/SphereCollider.ts +1 -1
  710. package/src/engine-components/SpriteRenderer.ts +244 -244
  711. package/src/engine-components/SyncedCamera.ts +208 -208
  712. package/src/engine-components/SyncedRoom.ts +166 -166
  713. package/src/engine-components/SyncedTransform.ts +336 -336
  714. package/src/engine-components/TestRunner.ts +114 -114
  715. package/src/engine-components/TransformGizmo.ts +157 -157
  716. package/src/engine-components/VideoPlayer.ts +831 -831
  717. package/src/engine-components/Voip.ts +214 -214
  718. package/src/engine-components/XRFlag.ts +138 -138
  719. package/src/engine-components/api.ts +22 -22
  720. package/src/engine-components/avatar/AvatarBlink_Simple.ts +67 -67
  721. package/src/engine-components/avatar/AvatarEyeLook_Rotation.ts +68 -68
  722. package/src/engine-components/avatar/Avatar_Brain_LookAt.ts +136 -136
  723. package/src/engine-components/avatar/Avatar_MouthShapes.ts +81 -81
  724. package/src/engine-components/avatar/Avatar_MustacheShake.ts +28 -28
  725. package/src/engine-components/codegen/components.ts +216 -216
  726. package/src/engine-components/debug/LogStats.ts +21 -21
  727. package/src/engine-components/export/gltf/GltfExport.ts +231 -231
  728. package/src/engine-components/export/usdz/Extension.ts +11 -11
  729. package/src/engine-components/export/usdz/ThreeUSDZExporter.ts +1773 -1773
  730. package/src/engine-components/export/usdz/USDZExporter.ts +477 -477
  731. package/src/engine-components/export/usdz/extensions/Animation.ts +774 -774
  732. package/src/engine-components/export/usdz/extensions/DocumentExtension.ts +9 -9
  733. package/src/engine-components/export/usdz/extensions/USDZText.ts +287 -287
  734. package/src/engine-components/export/usdz/extensions/USDZUI.ts +119 -119
  735. package/src/engine-components/export/usdz/extensions/behavior/Actions.ts +98 -98
  736. package/src/engine-components/export/usdz/extensions/behavior/AudioExtension.ts +67 -67
  737. package/src/engine-components/export/usdz/extensions/behavior/Behaviour.ts +202 -202
  738. package/src/engine-components/export/usdz/extensions/behavior/BehaviourComponents.ts +963 -963
  739. package/src/engine-components/export/usdz/extensions/behavior/BehavioursBuilder.ts +517 -517
  740. package/src/engine-components/export/usdz/index.ts +2 -2
  741. package/src/engine-components/export/usdz/utils/animationutils.ts +100 -100
  742. package/src/engine-components/export/usdz/utils/quicklook.ts +42 -42
  743. package/src/engine-components/export/usdz/utils/timeutils.ts +19 -19
  744. package/src/engine-components/js-extensions/ExtensionUtils.ts +81 -81
  745. package/src/engine-components/js-extensions/Object3D.ts +181 -181
  746. package/src/engine-components/js-extensions/RGBAColor.ts +54 -54
  747. package/src/engine-components/js-extensions/Vector.ts +16 -16
  748. package/src/engine-components/js-extensions/index.ts +2 -2
  749. package/src/engine-components/postprocessing/Effects/Antialiasing.ts +51 -51
  750. package/src/engine-components/postprocessing/Effects/Bloom.ts +76 -76
  751. package/src/engine-components/postprocessing/Effects/ChromaticAberration.ts +35 -35
  752. package/src/engine-components/postprocessing/Effects/ColorAdjustments.ts +96 -96
  753. package/src/engine-components/postprocessing/Effects/DepthOfField.ts +93 -93
  754. package/src/engine-components/postprocessing/Effects/Pixelation.ts +26 -26
  755. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusion.ts +84 -84
  756. package/src/engine-components/postprocessing/Effects/ScreenspaceAmbientOcclusionN8.ts +98 -98
  757. package/src/engine-components/postprocessing/Effects/TiltShiftEffect.ts +55 -55
  758. package/src/engine-components/postprocessing/Effects/Tonemapping.ts +54 -54
  759. package/src/engine-components/postprocessing/Effects/Vignette.ts +54 -54
  760. package/src/engine-components/postprocessing/PostProcessingEffect.ts +148 -148
  761. package/src/engine-components/postprocessing/PostProcessingHandler.ts +232 -232
  762. package/src/engine-components/postprocessing/Volume.ts +216 -216
  763. package/src/engine-components/postprocessing/VolumeParameter.ts +92 -92
  764. package/src/engine-components/postprocessing/VolumeProfile.ts +40 -40
  765. package/src/engine-components/postprocessing/index.ts +3 -3
  766. package/src/engine-components/timeline/PlayableDirector.ts +666 -666
  767. package/src/engine-components/timeline/SignalAsset.ts +138 -138
  768. package/src/engine-components/timeline/TimelineModels.ts +93 -93
  769. package/src/engine-components/timeline/TimelineTracks.ts +906 -906
  770. package/src/engine-components/timeline/index.ts +3 -3
  771. package/src/engine-components/ui/BaseUIComponent.ts +195 -195
  772. package/src/engine-components/ui/Button.ts +283 -283
  773. package/src/engine-components/ui/Canvas.ts +390 -390
  774. package/src/engine-components/ui/CanvasGroup.ts +49 -49
  775. package/src/engine-components/ui/EventSystem.ts +736 -736
  776. package/src/engine-components/ui/Graphic.ts +255 -255
  777. package/src/engine-components/ui/Image.ts +102 -102
  778. package/src/engine-components/ui/InputField.ts +290 -290
  779. package/src/engine-components/ui/Interfaces.ts +57 -57
  780. package/src/engine-components/ui/Layout.ts +322 -322
  781. package/src/engine-components/ui/Outline.ts +12 -12
  782. package/src/engine-components/ui/PointerEvents.ts +118 -118
  783. package/src/engine-components/ui/RaycastUtils.ts +68 -68
  784. package/src/engine-components/ui/Raycaster.ts +73 -73
  785. package/src/engine-components/ui/RectTransform.ts +364 -364
  786. package/src/engine-components/ui/SpatialHtml.ts +63 -63
  787. package/src/engine-components/ui/Text.ts +572 -572
  788. package/src/engine-components/ui/Utils.ts +110 -110
  789. package/src/engine-components/utils/LookAt.ts +65 -65
  790. package/src/engine-components/utils/OpenURL.ts +118 -118
  791. package/src/engine-components/webxr/WebARCameraBackground.ts +224 -224
  792. package/src/engine-components/webxr/WebARSessionRoot.ts +446 -446
  793. package/src/engine-components/webxr/WebXR.ts +761 -761
  794. package/src/engine-components/webxr/WebXRAvatar.ts +356 -356
  795. package/src/engine-components/webxr/WebXRController.ts +1168 -1168
  796. package/src/engine-components/webxr/WebXRGrabRendering.ts +150 -150
  797. package/src/engine-components/webxr/WebXRImageTracking.ts +371 -371
  798. package/src/engine-components/webxr/WebXRPlaneTracking.ts +429 -429
  799. package/src/engine-components/webxr/WebXRRig.ts +21 -21
  800. package/src/engine-components/webxr/WebXRSync.ts +463 -463
  801. package/src/engine-components/webxr/index.ts +3 -3
  802. package/src/engine-components-experimental/Presentation.ts +12 -12
  803. package/src/engine-components-experimental/networking/PlayerSync.ts +217 -217
  804. package/src/engine-schemes/COMPILE_SCHEMES.bat +3 -3
  805. package/src/engine-schemes/COMPILE_TS.bat +11 -11
  806. package/src/engine-schemes/schemes.ts +27 -27
  807. package/src/engine-schemes/synced-camera-model.ts +92 -92
  808. package/src/engine-schemes/synced-transform-model.ts +90 -90
  809. package/src/engine-schemes/syncedCamera.fbs +10 -10
  810. package/src/engine-schemes/transform.ts +50 -50
  811. package/src/engine-schemes/transforms.fbs +25 -25
  812. package/src/engine-schemes/vec.fbs +19 -19
  813. package/src/engine-schemes/vec2.ts +33 -33
  814. package/src/engine-schemes/vec3.ts +38 -38
  815. package/src/engine-schemes/vec4.ts +43 -43
  816. package/src/engine-schemes/vr-user-state-buffer.ts +138 -138
  817. package/src/engine-schemes/vrUserStateBuffer.fbs +16 -16
  818. package/src/include/draco/draco_decoder.js +34 -34
  819. package/src/include/draco/draco_wasm_wrapper.js +117 -117
  820. package/src/include/ktx2/basis_transcoder.js +21 -21
  821. package/src/include/needle/arial-msdf.json +1471 -1471
  822. package/src/include/three/ARButton.js +231 -231
  823. package/src/include/three/DragControls.js +231 -231
  824. package/src/include/three/EXT_mesh_gpu_instancing_exporter.js +66 -66
  825. package/src/include/three/VRButton.js +194 -194
  826. package/src/needle-engine.ts +55 -55
  827. package/src/engine/dist/api.js +0 -73
  828. package/src/engine/dist/api.js.meta +0 -7
  829. package/src/engine/dist/engine_networking_streams.js +0 -474
  830. package/src/engine/dist/engine_networking_streams.js.meta +0 -7
  831. package/src/engine-schemes/dist/api.js +0 -17
  832. package/src/engine-schemes/dist/api.js.meta +0 -7
  833. package/src/engine-schemes/dist/schemes.js +0 -25
  834. package/src/engine-schemes/dist/schemes.js.meta +0 -7
  835. package/src/engine-schemes/dist/synced-camera-model.js +0 -74
  836. package/src/engine-schemes/dist/synced-camera-model.js.meta +0 -7
  837. package/src/engine-schemes/dist/synced-transform-model.js +0 -73
  838. package/src/engine-schemes/dist/synced-transform-model.js.meta +0 -7
  839. package/src/engine-schemes/dist/transform.js +0 -46
  840. package/src/engine-schemes/dist/transform.js.meta +0 -7
  841. package/src/engine-schemes/dist/vec2.js +0 -32
  842. package/src/engine-schemes/dist/vec2.js.meta +0 -7
  843. package/src/engine-schemes/dist/vec3.js +0 -36
  844. package/src/engine-schemes/dist/vec3.js.meta +0 -7
  845. package/src/engine-schemes/dist/vec4.js +0 -40
  846. package/src/engine-schemes/dist/vec4.js.meta +0 -7
  847. package/src/engine-schemes/dist/vr-user-state-buffer.js +0 -110
  848. package/src/engine-schemes/dist/vr-user-state-buffer.js.meta +0 -7
  849. package/src/engine-schemes/flatc.exe +0 -0
@@ -1,1385 +1,1385 @@
1
- import { BufferAttribute, BufferGeometry, LineBasicMaterial, LineSegments, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from 'three'
2
- import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js'
3
- import { CircularBuffer, getParam } from "./engine_utils.js"
4
- import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPositionXYZ, setWorldQuaternionXYZW } from "./engine_three_utils.js"
5
- import type {
6
- IPhysicsEngine,
7
- IComponent,
8
- ICollider,
9
- IRigidbody,
10
- Vec3,
11
- IGameObject,
12
- Vec2,
13
- IContext,
14
- ISphereCollider,
15
- IBoxCollider,
16
- } from './engine_types.js';
17
- import { ContactPoint, Collision } from './engine_types.js';
18
- import { foreachComponent } from './engine_gameobject.js';
19
-
20
- import { ActiveCollisionTypes, ActiveEvents, CoefficientCombineRule, Ball, Collider, ColliderDesc, EventQueue, JointData, QueryFilterFlags, RigidBody, RigidBodyType, ShapeColliderTOI, World, Ray, ShapeType, Cuboid } from '@dimforge/rapier3d-compat';
21
- import { CollisionDetectionMode, type PhysicsMaterial, PhysicsMaterialCombine } from '../engine/engine_physics.types.js';
22
- import { Gizmos } from './engine_gizmos.js';
23
- import { Mathf } from './engine_math.js';
24
- import { SphereOverlapResult } from './engine_types.js';
25
- import { ContextEvent, ContextRegistry } from './engine_context_registry.js';
26
- import { isDevEnvironment } from './debug/debug.js';
27
-
28
- const debugPhysics = getParam("debugphysics");
29
- const debugColliderPlacement = getParam("debugcolliderplacement");
30
- const debugCollisions = getParam("debugcollisions");
31
- const showColliders = getParam("showcolliders");
32
-
33
-
34
- /** on physics body and references the needle component */
35
- const $componentKey = Symbol("needle component");
36
- /** on needle component and references physics body */
37
- const $bodyKey = Symbol("physics body");
38
- const $colliderRigidbody = Symbol("rigidbody");
39
-
40
-
41
- let RAPIER: undefined | any = undefined;
42
- declare const NEEDLE_USE_RAPIER: boolean;
43
- globalThis["NEEDLE_USE_RAPIER"] = globalThis["NEEDLE_USE_RAPIER"] !== undefined ? globalThis["NEEDLE_USE_RAPIER"] : true;
44
- if (debugPhysics)
45
- console.log("Use Rapier", NEEDLE_USE_RAPIER, globalThis["NEEDLE_USE_RAPIER"])
46
-
47
- if (NEEDLE_USE_RAPIER) {
48
- ContextRegistry.registerCallback(ContextEvent.ContextCreationStart, evt => {
49
- if (debugPhysics)
50
- console.log("Register rapier physics backend")
51
- evt.context.physics.engine = new RapierPhysics();
52
- // We want the physics engine to be initialized on start so when components start to enable and modify values they don't have delays
53
- // TODO: should the promise be returned here to make the engine creation wait?
54
- if (NEEDLE_USE_RAPIER) {
55
- evt.context.physics.engine.initialize(evt.context);
56
- }
57
- });
58
- }
59
-
60
-
61
- declare type PhysicsBody = {
62
- translation(): { x: number, y: number, z: number }
63
- rotation(): { x: number, y: number, z: number, w: number }
64
- }
65
-
66
- export class RapierPhysics implements IPhysicsEngine {
67
-
68
- /** Enable to draw collider shapes */
69
- debugRenderColliders: boolean = false;
70
-
71
- removeBody(obj: IComponent) {
72
- if (!obj) return;
73
- this.validate();
74
- const body = obj[$bodyKey];
75
- obj[$bodyKey] = null;
76
- if (body && this.world) {
77
- const index = this.objects.findIndex(o => o === obj);
78
- if (index >= 0) {
79
- const rapierBody = this.bodies[index];
80
- // Remove references
81
- this.bodies.splice(index, 1);
82
- this.objects.splice(index, 1);
83
-
84
-
85
- // Remove the collider from the physics world
86
- if (rapierBody instanceof Collider) {
87
- const rapierCollider = rapierBody as Collider;
88
- this.world?.removeCollider(rapierCollider, true);
89
-
90
- // also remove the rigidbody if it doesnt have colliders anymore
91
- const rapierRigidbody: RigidBody | null = rapierCollider.parent();
92
- if (rapierRigidbody && rapierRigidbody.numColliders() <= 0) {
93
- const rigidbody = rapierRigidbody[$componentKey] as IRigidbody;
94
- if (rigidbody) {
95
- // If the collider was attached to a rigidbody and this rigidbody now has no colliders anymore we should ignore it - because the Rigidbody component will delete itself
96
- }
97
- else {
98
- // But if there is no explicit rigidbody needle component then the colliders did create it implictly and thus we need to remove it here:
99
- this.world?.removeRigidBody(rapierRigidbody);
100
- }
101
- }
102
- }
103
- // Remove the rigidbody from the physics world
104
- else if (rapierBody instanceof RigidBody) {
105
- if (rapierBody.numColliders() <= 0) {
106
- this.world?.removeRigidBody(rapierBody);
107
- }
108
- else {
109
- if (isDevEnvironment()) {
110
- if (!rapierBody["did_log_removing"]) {
111
- setTimeout(() => {
112
- if (rapierBody.numColliders() > 0) {
113
- rapierBody["did_log_removing"] = true;
114
- console.warn("RapierPhysics: removing rigidbody with colliders from the physics world is not possible right now, please remove the colliders first");
115
- }
116
- }, 1);
117
-
118
- }
119
- }
120
- }
121
- }
122
- }
123
- }
124
- }
125
-
126
- updateBody(comp: ICollider | IRigidbody, translation: boolean, rotation: boolean) {
127
- this.validate();
128
- if (!this.enabled) return;
129
- if (comp.destroyed || !comp.gameObject) return;
130
- if (!translation && !rotation) return;
131
-
132
- if ((comp as ICollider).isCollider === true) {
133
- // const collider = comp as ICollider;
134
- console.warn("TODO: implement updating collider position");
135
- }
136
- else {
137
- const rigidbody = comp as IRigidbody;
138
- const body = rigidbody[$bodyKey];
139
- if (body) {
140
- this.syncPhysicsBody(rigidbody.gameObject, body, translation, rotation);
141
- }
142
- }
143
- }
144
-
145
- updateProperties(obj: IRigidbody | ICollider) {
146
- this.validate();
147
-
148
- if ((obj as ICollider).isCollider) {
149
- const col = obj as ICollider;
150
- const body = col[$bodyKey];
151
- if (body) {
152
- this.internalUpdateColliderProperties(col, body);
153
- if(col.sharedMaterial)
154
- this.updatePhysicsMaterial(col);
155
- }
156
- }
157
- else {
158
- const rb = obj as IRigidbody;
159
- const physicsBody = this.internal_getRigidbody(rb);
160
- if (physicsBody) {
161
- this.internalUpdateRigidbodyProperties(rb, physicsBody);
162
- }
163
- }
164
- }
165
- addForce(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
166
- this.validate();
167
- const body = this.internal_getRigidbody(rigidbody);
168
- body?.addForce(force, wakeup)
169
- }
170
- addImpulse(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
171
- this.validate();
172
- const body = this.internal_getRigidbody(rigidbody);
173
- body?.applyImpulse(force, wakeup)
174
- }
175
- getLinearVelocity(comp: IRigidbody | ICollider): Vec3 | null {
176
- this.validate();
177
- const body = this.internal_getRigidbody(comp);
178
- if (body) {
179
- const vel = body.linvel();
180
- return vel;
181
- }
182
- return null;
183
- }
184
- getAngularVelocity(rb: IRigidbody): Vec3 | null {
185
- this.validate();
186
- const body = this.internal_getRigidbody(rb);
187
- if (body) {
188
- const vel = body.angvel();
189
- return vel;
190
- }
191
- return null;
192
- }
193
- resetForces(rb: IRigidbody, wakeup: boolean) {
194
- this.validate();
195
- const body = this.internal_getRigidbody(rb);
196
- body?.resetForces(wakeup);
197
- }
198
- resetTorques(rb: IRigidbody, wakeup: boolean) {
199
- this.validate();
200
- const body = this.internal_getRigidbody(rb);
201
- body?.resetTorques(wakeup);
202
- }
203
- applyImpulse(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
204
- this.validate();
205
- const body = this.internal_getRigidbody(rb);
206
- body?.applyImpulse(vec, wakeup);
207
- }
208
-
209
- wakeup(rb: IRigidbody) {
210
- this.validate();
211
- const body = this.internal_getRigidbody(rb);
212
- body?.wakeUp();
213
- }
214
- setAngularVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
215
- this.validate();
216
- const body = this.internal_getRigidbody(rb);
217
- body?.setAngvel(vec, wakeup);
218
- }
219
- setLinearVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
220
- this.validate();
221
- const body = this.internal_getRigidbody(rb);
222
- body?.setLinvel(vec, wakeup);
223
- }
224
-
225
- private context?: IContext;
226
- private _initializePromise?: Promise<boolean>;
227
- private _isInitialized: boolean = false;
228
-
229
- async initialize(context: IContext) {
230
- this.context = context;
231
- if (!this._initializePromise)
232
- this._initializePromise = this.internalInitialization();
233
- return this._initializePromise;
234
- }
235
-
236
- private async internalInitialization() {
237
- if (debugPhysics) console.log("Initialize rapier physics engine");
238
- // NEEDLE_PHYSICS_INIT_START
239
- // use .env file with VITE_NEEDLE_USE_RAPIER=false to treeshake rapier
240
- // @ts-ignore
241
- if ("env" in import.meta && import.meta.env.VITE_NEEDLE_USE_RAPIER === "false") {
242
- if (debugPhysics) console.log("Rapier disabled");
243
- return false;
244
- }
245
- // Can be transformed during build time to disable rapier
246
- if (!NEEDLE_USE_RAPIER) return false;
247
- if (this._hasCreatedWorld) {
248
- console.error("Invalid call to create physics world: world is already created");
249
- return true;
250
- }
251
- this._hasCreatedWorld = true;
252
- if (RAPIER === undefined) {
253
- if (debugPhysics) console.log("Import Rapier");
254
- const _rapier = await import("@dimforge/rapier3d-compat");
255
- if (debugPhysics) console.log("Init Rapier");
256
- await _rapier.init();
257
- // only assign after all loads are done to avoid a race condition
258
- // where RAPIER is already set and then used while actually still waiting for initialization.
259
- RAPIER = _rapier;
260
- }
261
- if (debugPhysics) console.log("Physics engine initialized, creating world...");
262
- this._world = new World(this._gravity);
263
- this.enabled = true;
264
- this._isInitialized = true;
265
- if (debugPhysics) console.log("Physics world created");
266
- return true;
267
- // NEEDLE_PHYSICS_INIT_END
268
- }
269
-
270
- /** Check is the physics engine has been initialized and the call can be made */
271
- private validate() {
272
- if (!this._isInitialized) {
273
- if (debugPhysics) {
274
- this["_lastWarnTime"] = this["_lastWarnTime"] ?? 0;
275
- if (Date.now() - this["_lastWarnTime"] > 1000) {
276
- this["_lastWarnTime"] = Date.now();
277
- console.warn("Physics engine is not initialized");
278
- }
279
- }
280
- }
281
- }
282
-
283
-
284
- private rapierRay = new Ray({ x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 1 });
285
- private raycastVectorsBuffer = new CircularBuffer(() => new Vector3(), 10);
286
- public raycast(origin?: Vec2 | Vec3, direction?: Vec3, maxDistance?: number, solid?: boolean)
287
- : null | { point: Vector3, collider: ICollider } {
288
-
289
- if (maxDistance === undefined) maxDistance = Infinity;
290
- if (solid === undefined) solid = true;
291
-
292
- const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
293
- if (!ray) return null;
294
-
295
- const hit = this.world?.castRay(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
296
- // ignore objects in the IgnoreRaycast=2 layer
297
- return !c[$componentKey]?.gameObject.layers.isEnabled(2);
298
- });
299
- if (hit) {
300
- const point = ray.pointAt(hit.toi);
301
- const vec = this.raycastVectorsBuffer.get();
302
- vec.set(point.x, point.y, point.z);
303
- return { point: vec, collider: hit.collider[$componentKey] };
304
- }
305
-
306
- return null;
307
- }
308
-
309
- public raycastAndGetNormal(origin?: Vec2 | Vec3, direction?: Vec3, maxDistance?: number, solid?: boolean)
310
- : null | { point: Vector3, normal: Vector3, collider: ICollider } {
311
-
312
- if (maxDistance === undefined) maxDistance = Infinity;
313
- if (solid === undefined) solid = true;
314
-
315
- const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
316
- if (!ray) return null;
317
-
318
- const hit = this.world?.castRayAndGetNormal(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
319
- // ignore objects in the IgnoreRaycast=2 layer
320
- return !c[$componentKey]?.gameObject.layers.isEnabled(2);
321
- });
322
- if (hit) {
323
- const point = ray.pointAt(hit.toi);
324
- const normal = hit.normal;
325
- const vec = this.raycastVectorsBuffer.get();
326
- const nor = this.raycastVectorsBuffer.get();
327
- vec.set(point.x, point.y, point.z);
328
- nor.set(normal.x, normal.y, normal.z);
329
- return { point: vec, normal: nor, collider: hit.collider[$componentKey] };
330
- }
331
- return null;
332
- }
333
-
334
- private getPhysicsRay(ray: Ray, origin?: Vec2 | Vec3, direction?: Vec3): Ray | null {
335
- const cam = this.context?.mainCamera;
336
- if (origin === undefined) {
337
- const pos = this.context?.input.getPointerPosition(0);
338
- if (pos) origin = pos;
339
- else return null;
340
- }
341
- // if we get origin in 2d space we need to project it to 3d space
342
- if (origin["z"] === undefined) {
343
- if (!cam) {
344
- console.error("Can not perform raycast from 2d point - no main camera found");
345
- return null;
346
- }
347
- const vec3 = this.raycastVectorsBuffer.get();
348
- vec3.x = origin.x;
349
- vec3.y = origin.y;
350
- vec3.z = 0;
351
- // if the origin is in screen space we need to convert it to raycaster space
352
- if (vec3.x > 1 || vec3.y > 1 || vec3.y < -1 || vec3.x < -1) {
353
- if (debugPhysics) console.warn("Converting screenspace to raycast space", vec3)
354
- this.context?.input.convertScreenspaceToRaycastSpace(vec3);
355
- }
356
- vec3.unproject(cam);
357
- origin = vec3;
358
- }
359
-
360
- const o = origin as Vec3;
361
-
362
- ray.origin.x = o.x;
363
- ray.origin.y = o.y;
364
- ray.origin.z = o.z;
365
- const vec = this.raycastVectorsBuffer.get();
366
- if (direction)
367
- vec.set(direction.x, direction.y, direction.z);
368
- else {
369
- if (!cam) {
370
- console.error("Can not perform raycast - no camera found");
371
- return null;
372
- }
373
- vec.set(ray.origin.x, ray.origin.y, ray.origin.z);
374
- const camPosition = getWorldPosition(cam);
375
- vec.sub(camPosition);
376
- }
377
- // we need to normalize the ray because our input is a max travel length and the direction may be not normalized
378
- vec.normalize();
379
- ray.dir.x = vec.x;
380
- ray.dir.y = vec.y;
381
- ray.dir.z = vec.z;
382
- // Gizmos.DrawRay(ray.origin, ray.dir, 0xff0000, Infinity);
383
- return ray;
384
- }
385
-
386
-
387
- private rapierSphere: Ball | null = null;
388
- private rapierColliderArray: Array<SphereOverlapResult> = [];
389
- private readonly rapierIdentityRotation = { x: 0, y: 0, z: 0, w: 1 };
390
- private readonly rapierForwardVector = { x: 0, y: 0, z: 1 };
391
- /** Precice sphere overlap detection using rapier against colliders
392
- * @param point center of the sphere in worldspace
393
- * @param radius radius of the sphere
394
- * @returns array of colliders that overlap with the sphere. Note: they currently only contain the collider and the gameobject
395
- */
396
- public sphereOverlap(point: Vector3, radius: number): Array<SphereOverlapResult> {
397
- this.rapierColliderArray.length = 0;
398
- if (!this.world) return this.rapierColliderArray;
399
- if (!this.rapierSphere)
400
- this.rapierSphere = new Ball(radius);
401
- this.rapierSphere.radius = radius;
402
-
403
- this.world.intersectionsWithShape(point, this.rapierIdentityRotation, this.rapierSphere, col => {
404
- const collider = col[$componentKey] as ICollider
405
- // if (collider.gameObject.layers.isEnabled(2)) return true;
406
- const intersection = new SphereOverlapResult(collider.gameObject, collider);
407
- this.rapierColliderArray.push(intersection);
408
- return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
409
- }, QueryFilterFlags.EXCLUDE_SENSORS, undefined, undefined, undefined,
410
- col => {
411
- const collider = col[$componentKey] as ICollider
412
- return collider.gameObject.layers.isEnabled(2) == false
413
- }
414
- );
415
- return this.rapierColliderArray;
416
-
417
-
418
- // TODO: this only returns one hit
419
- // let filterGroups = 0xffffffff;
420
- // filterGroups &= ~(1 << 2);
421
- // const hit: ShapeColliderTOI | null = this.world.castShape(point,
422
- // this.rapierIdentityRotation,
423
- // this.rapierForwardVector,
424
- // this.rapierSphere,
425
- // 0,
426
- // QueryFilterFlags.EXCLUDE_SENSORS,
427
- // // filterGroups,
428
- // );
429
- // // console.log(hit);
430
- // if (hit) {
431
- // const collider = hit.collider[$componentKey] as ICollider
432
- // const intersection = new SphereOverlapResult(collider.gameObject);
433
- // this.rapierColliderArray.push(intersection);
434
- // // const localpt = hit.witness2;
435
- // // // const normal = hit.normal2;
436
- // // const hitPoint = new Vector3(localpt.x, localpt.y, localpt.z);
437
- // // // collider.gameObject.localToWorld(hitPoint);
438
- // // // const normalPt = new Vector3(normal.x, normal.y, normal.z);
439
- // // // const mat = new Matrix4().setPosition(point).scale(new Vector3(radius, radius, radius));
440
- // // // hitPoint.applyMatrix4(mat);
441
- // // console.log(hit.witness2)
442
- // // // hitPoint.add(point);
443
- // // const dist = hitPoint.distanceTo(point);
444
- // }
445
-
446
- // return this.rapierColliderArray;
447
- }
448
-
449
-
450
-
451
-
452
- // physics simulation
453
-
454
- enabled: boolean = false;
455
- /** Get access to the rapier world */
456
- public get world(): World | undefined { return this._world };
457
-
458
- private _tempPosition: Vector3 = new Vector3();
459
- private _tempQuaternion: Quaternion = new Quaternion();
460
- private _tempScale: Vector3 = new Vector3();
461
- private _tempMatrix: Matrix4 = new Matrix4();
462
-
463
- private static _didLoadPhysicsEngine: boolean = false;
464
-
465
- private _isUpdatingPhysicsWorld: boolean = false;
466
- get isUpdating(): boolean { return this._isUpdatingPhysicsWorld; }
467
-
468
-
469
- private _world?: World;
470
- private _hasCreatedWorld: boolean = false;
471
- private eventQueue?: EventQueue;
472
- private collisionHandler?: PhysicsCollisionHandler;
473
-
474
-
475
- private objects: IComponent[] = [];
476
- private bodies: PhysicsBody[] = [];
477
-
478
- private _meshCache: Map<string, Float32Array> = new Map<string, Float32Array>();
479
-
480
- private _gravity = { x: 0.0, y: -9.81, z: 0.0 };
481
-
482
- get gravity() {
483
- return this.world?.gravity ?? this._gravity;
484
- }
485
-
486
- set gravity(value: Vec3) {
487
- if (this.world) {
488
- this.world.gravity = value;
489
- }
490
- else {
491
- this._gravity = value;
492
- }
493
- }
494
-
495
- clearCaches() {
496
- this._meshCache.clear();
497
- if (this.eventQueue?.raw)
498
- this.eventQueue?.free();
499
- if (this.world?.bodies)
500
- this.world?.free();
501
- }
502
-
503
- async addBoxCollider(collider: ICollider, size: Vector3) {
504
- if (!this._isInitialized)
505
- await this.initialize(collider.context);
506
- if (!collider.activeAndEnabled) return;
507
-
508
- if (!this.enabled) {
509
- if (debugPhysics) console.warn("Physics are disabled");
510
- return;
511
- }
512
- const obj = collider.gameObject;
513
- const scale = getWorldScale(obj, this._tempPosition).multiply(size);
514
- scale.multiplyScalar(0.5);
515
-
516
- // prevent negative scale
517
- if (scale.x < 0)
518
- scale.x = Math.abs(scale.x);
519
- if (scale.y < 0)
520
- scale.y = Math.abs(scale.y);
521
- if (scale.z < 0)
522
- scale.z = Math.abs(scale.z);
523
-
524
- // prevent zero scale - seems normals are flipped otherwise
525
- if (scale.x == 0) scale.x = 0.0000001;
526
- if (scale.y == 0) scale.y = 0.0000001;
527
- if (scale.z == 0) scale.z = 0.0000001;
528
-
529
- const desc = ColliderDesc.cuboid(scale.x, scale.y, scale.z);
530
- // const objectLayerMask = collider.gameObject.layers.mask;
531
- // const mask = objectLayerMask & ~2;
532
- // TODO: https://rapier.rs/docs/user_guides/javascript/colliders/#collision-groups-and-solver-groups
533
- // desc.setCollisionGroups(objectLayerMask);
534
- this.createCollider(collider, desc);
535
- }
536
-
537
- async addSphereCollider(collider: ICollider) {
538
- if (!this._isInitialized)
539
- await this.initialize(collider.context);
540
- if (!collider.activeAndEnabled) return;
541
- if (!this.enabled) {
542
- if (debugPhysics) console.warn("Physics are disabled");
543
- return;
544
- }
545
- const desc = ColliderDesc.ball(.5);
546
- this.createCollider(collider, desc);
547
- this.updateProperties(collider);
548
- }
549
-
550
- async addCapsuleCollider(collider: ICollider, height: number, radius: number) {
551
- if (!this._isInitialized)
552
- await this.initialize(collider.context);
553
- if (!collider.activeAndEnabled) return;
554
- if (!this.enabled) {
555
- if (debugPhysics) console.warn("Physics are disabled");
556
- return;
557
- }
558
- const obj = collider.gameObject;
559
- const scale = getWorldScale(obj, this._tempPosition);
560
- // Prevent negative scales
561
- scale.x = Math.abs(scale.x);
562
- scale.y = Math.abs(scale.y);
563
- const finalRadius = radius * scale.x;
564
- // half height = distance between capsule origin and top sphere origin (not the top end of the capsule)
565
- height = Math.max(height, finalRadius * 2);
566
- const hh = Mathf.clamp((height * .5 * scale.y) - (radius * scale.x), 0, Number.MAX_SAFE_INTEGER);
567
- const desc = ColliderDesc.capsule(hh, finalRadius);
568
- this.createCollider(collider, desc);
569
- }
570
-
571
- async addMeshCollider(collider: ICollider, mesh: Mesh, convex: boolean, scale: Vector3) {
572
- if (!this._isInitialized)
573
- await this.initialize(collider.context);
574
- if (!collider.activeAndEnabled) return;
575
- if (!this.enabled) {
576
- if (debugPhysics) console.warn("Physics are disabled");
577
- return;
578
- }
579
- let geo = mesh.geometry;
580
- if (!geo) {
581
- if (debugPhysics) console.warn("Missing mesh geometry", mesh.name);
582
- return;
583
- }
584
-
585
- // check if mesh is indexed, if not generate indices
586
- if (!geo.index?.array?.length) {
587
- console.warn(`Your MeshCollider is missing vertices or indices in the assined mesh \"${mesh.name}\". Consider providing an indexed geometry.`);
588
- geo = BufferGeometryUtils.mergeVertices(geo);
589
- }
590
-
591
- let positions = geo.getAttribute("position").array as Float32Array;
592
- const indices = geo.index?.array as Uint32Array;
593
-
594
- // scaling seems not supported yet https://github.com/dimforge/rapier/issues/243
595
- if (Math.abs(scale.x - 1) > 0.0001 || Math.abs(scale.y - 1) > 0.0001 || Math.abs(scale.z - 1) > 0.0001) {
596
- const key = geo.uuid + "_" + scale.x + "_" + scale.y + "_" + scale.z + "_" + convex;
597
- if (this._meshCache.has(key)) {
598
- if (debugPhysics) console.warn("Use cached mesh collider")
599
- positions = this._meshCache.get(key)!;
600
- }
601
- else {
602
- console.warn(`Your MeshCollider \"${collider.name}\" is scaled (${scale.x}, ${scale.y}, ${scale.z})\nthis is not optimal for performance since this isn't supported by the Rapier physics engine yet. Consider applying the scale to the collider mesh`);
603
- // showBalloonWarning("Your model is using scaled mesh colliders which is not optimal for performance: " + mesh.name + ", consider using unscaled objects");
604
- const scaledPositions = new Float32Array(positions.length);
605
- for (let i = 0; i < positions.length; i += 3) {
606
- scaledPositions[i] = positions[i] * scale.x;
607
- scaledPositions[i + 1] = positions[i + 1] * scale.y;
608
- scaledPositions[i + 2] = positions[i + 2] * scale.z;
609
- }
610
- positions = scaledPositions;
611
- this._meshCache.set(key, scaledPositions);
612
- }
613
- }
614
- const desc = convex ? ColliderDesc.convexHull(positions) : ColliderDesc.trimesh(positions, indices);
615
- if (desc) {
616
- this.createCollider(collider, desc);
617
- // col.setMassProperties(1, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0, w: 1 });
618
- // rb?.setTranslation({ x: 0, y: 2, z: 0 });
619
- // col.setTranslationWrtParent(new Vector3(0,2,0));
620
-
621
- }
622
- }
623
-
624
- updatePhysicsMaterial(col: ICollider) {
625
- if (!col) return;
626
- const physicsMaterial = col.sharedMaterial;
627
- const rapier_collider = col[$bodyKey] as Collider;
628
- if (!rapier_collider) return;
629
-
630
- if (physicsMaterial) {
631
- if (physicsMaterial.bounciness !== undefined)
632
- rapier_collider.setRestitution(physicsMaterial.bounciness);
633
-
634
- if (physicsMaterial.bounceCombine !== undefined) {
635
- switch (physicsMaterial.bounceCombine) {
636
- case PhysicsMaterialCombine.Average:
637
- rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Average);
638
- break;
639
- case PhysicsMaterialCombine.Maximum:
640
- rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Max);
641
- break;
642
- case PhysicsMaterialCombine.Minimum:
643
- rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Min);
644
- break;
645
- case PhysicsMaterialCombine.Multiply:
646
- rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
647
- break;
648
- }
649
- }
650
-
651
- if (physicsMaterial.dynamicFriction !== undefined)
652
- rapier_collider.setFriction(physicsMaterial.dynamicFriction);
653
-
654
- if (physicsMaterial.frictionCombine !== undefined) {
655
- switch (physicsMaterial.frictionCombine) {
656
- case PhysicsMaterialCombine.Average:
657
- rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Average);
658
- break;
659
- case PhysicsMaterialCombine.Maximum:
660
- rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Max);
661
- break;
662
- case PhysicsMaterialCombine.Minimum:
663
- rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Min);
664
- break;
665
- case PhysicsMaterialCombine.Multiply:
666
- rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Multiply);
667
- break;
668
- }
669
- }
670
- }
671
- }
672
-
673
- /** Get the rapier body for a Needle component */
674
- getBody(obj: ICollider | IRigidbody): null | any {
675
- if (!obj) return null;
676
- const body = obj[$bodyKey];
677
- return body;
678
- }
679
-
680
- /** Get the Needle Engine component for a rapier object */
681
- getComponent(rapierObject: object): IComponent | null {
682
- if (!rapierObject) return null;
683
- const component = rapierObject[$componentKey];
684
- return component;
685
- }
686
-
687
- private createCollider(collider: ICollider, desc: ColliderDesc) {
688
- if (!this.world) throw new Error("Physics world not initialized");
689
- const matrix = this._tempMatrix;
690
- let rigidBody: RigidBody | undefined = undefined;
691
- if (!collider.attachedRigidbody) {
692
- if(debugPhysics) console.log("Create collider without rigidbody", collider.name);
693
- matrix.makeRotationFromQuaternion(getWorldQuaternion(collider.gameObject));
694
- matrix.setPosition(getWorldPosition(collider.gameObject));
695
- }
696
- else {
697
- rigidBody = this.getRigidbody(collider, this._tempMatrix);
698
- }
699
-
700
- matrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
701
- this.tryApplyCenter(collider, this._tempPosition);
702
- desc.setTranslation(this._tempPosition.x, this._tempPosition.y, this._tempPosition.z);
703
- desc.setRotation(this._tempQuaternion);
704
- desc.setSensor(collider.isTrigger);
705
-
706
- // TODO: we might want to update this if the material changes
707
- const physicsMaterial = collider.sharedMaterial;
708
- if (physicsMaterial) {
709
-
710
- if (physicsMaterial.bounciness !== undefined)
711
- desc.setRestitution(physicsMaterial.bounciness);
712
-
713
- if (physicsMaterial.bounceCombine !== undefined) {
714
- switch (physicsMaterial.bounceCombine) {
715
- case PhysicsMaterialCombine.Average:
716
- desc.setRestitutionCombineRule(CoefficientCombineRule.Average);
717
- break;
718
- case PhysicsMaterialCombine.Maximum:
719
- desc.setRestitutionCombineRule(CoefficientCombineRule.Max);
720
- break;
721
- case PhysicsMaterialCombine.Minimum:
722
- desc.setRestitutionCombineRule(CoefficientCombineRule.Min);
723
- break;
724
- case PhysicsMaterialCombine.Multiply:
725
- desc.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
726
- break;
727
- }
728
- }
729
-
730
- if (physicsMaterial.dynamicFriction !== undefined)
731
- desc.setFriction(physicsMaterial.dynamicFriction);
732
-
733
- if (physicsMaterial.frictionCombine !== undefined) {
734
- switch (physicsMaterial.frictionCombine) {
735
- case PhysicsMaterialCombine.Average:
736
- desc.setFrictionCombineRule(CoefficientCombineRule.Average);
737
- break;
738
- case PhysicsMaterialCombine.Maximum:
739
- desc.setFrictionCombineRule(CoefficientCombineRule.Max);
740
- break;
741
- case PhysicsMaterialCombine.Minimum:
742
- desc.setFrictionCombineRule(CoefficientCombineRule.Min);
743
- break;
744
- case PhysicsMaterialCombine.Multiply:
745
- desc.setFrictionCombineRule(CoefficientCombineRule.Multiply);
746
- break;
747
- }
748
- }
749
- }
750
-
751
- // if we want to use explicit mass properties, we need to set the collider density to 0
752
- // otherwise rapier will compute the mass properties based on the collider shape and density
753
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
754
- if (collider.attachedRigidbody?.autoMass === false) {
755
- desc.setDensity(.000001);
756
- desc.setMass(.000001);
757
- }
758
-
759
- try {
760
- const col = this.world.createCollider(desc, rigidBody);
761
- col[$componentKey] = collider;
762
- collider[$bodyKey] = col;
763
- col.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
764
- // We want to receive collisitons between two triggers too
765
- col.setActiveCollisionTypes(ActiveCollisionTypes.ALL);
766
-
767
- // const objectLayerMask = collider.gameObject.layers.mask;
768
- // const mask = objectLayerMask & ~2;
769
- // col.setCollisionGroups(objectLayerMask);
770
- this.objects.push(collider);
771
- this.bodies.push(col);
772
- return col;
773
- }
774
- catch (e) {
775
- console.error("Error creating collider \"" + collider.name + "\"\nError:", e);
776
- return null;
777
- }
778
- }
779
-
780
- private getRigidbody(collider: ICollider, _matrix: Matrix4): RigidBody {
781
-
782
- if (!this.world) throw new Error("Physics world not initialized");
783
- let rigidBody: RigidBody | null = null;
784
-
785
- if (collider.attachedRigidbody) {
786
- const rb = collider.attachedRigidbody;
787
- rigidBody = rb[$bodyKey];
788
- if (!rigidBody) {
789
- const kinematic = rb.isKinematic && !debugColliderPlacement;
790
- if (debugPhysics)
791
- console.log("Create rigidbody", kinematic);
792
- const rigidBodyDesc = kinematic ? RAPIER.RigidBodyDesc.kinematicPositionBased() : RAPIER.RigidBodyDesc.dynamic();
793
- const pos = getWorldPosition(collider.attachedRigidbody.gameObject);
794
- rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
795
- rigidBodyDesc.setRotation(getWorldQuaternion(collider.attachedRigidbody.gameObject));
796
- rigidBody = this.world.createRigidBody(rigidBodyDesc);
797
- this.bodies.push(rigidBody);
798
- this.objects.push(rb);
799
- }
800
- rigidBody[$componentKey] = rb;
801
- rb[$bodyKey] = rigidBody;
802
- this.internalUpdateRigidbodyProperties(rb, rigidBody);
803
- this.getRigidbodyRelativeMatrix(collider.gameObject, rb.gameObject, _matrix);
804
- collider[$colliderRigidbody] = rigidBody;
805
- }
806
- else {
807
- const rigidBodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased();
808
- const pos = getWorldPosition(collider.gameObject);
809
- rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
810
- rigidBodyDesc.setRotation(getWorldQuaternion(collider.gameObject));
811
- rigidBody = this.world.createRigidBody(rigidBodyDesc);
812
- _matrix.identity();
813
- rigidBody[$componentKey] = null;
814
- }
815
-
816
-
817
- return rigidBody;
818
- }
819
-
820
- private internal_getRigidbody(rb: IRigidbody | ICollider): RigidBody | null {
821
- if ((rb as ICollider).isCollider === true) return rb[$colliderRigidbody] as RigidBody;
822
- return rb[$bodyKey] as RigidBody;
823
- }
824
-
825
- private internalUpdateColliderProperties(col: ICollider, collider: Collider) {
826
- const shape = collider.shape;
827
- let sizeHasChanged = false;
828
- switch (shape.type) {
829
- // Sphere Collider
830
- case ShapeType.Ball:
831
- {
832
- const ball = shape as Ball;
833
- const sc = col as ISphereCollider;
834
- const obj = col.gameObject;
835
- const scale = getWorldScale(obj, this._tempPosition);
836
- const radius = Math.abs(sc.radius * scale.x);
837
- sizeHasChanged = ball.radius !== radius;
838
- ball.radius = radius;
839
- if (sizeHasChanged) {
840
- collider.setShape(ball);
841
- }
842
- break;
843
- }
844
- case ShapeType.Cuboid:
845
- const cuboid = shape as Cuboid;
846
- const sc = col as IBoxCollider;
847
- const newX = sc.size.x * 0.5;
848
- const newY = sc.size.y * 0.5;
849
- const newZ = sc.size.z * 0.5;
850
- sizeHasChanged = cuboid.halfExtents.x !== newX || cuboid.halfExtents.y !== newY || cuboid.halfExtents.z !== newZ;
851
- cuboid.halfExtents.x = newX;
852
- cuboid.halfExtents.y = newY;
853
- cuboid.halfExtents.z = newZ;
854
- if (sizeHasChanged) {
855
- collider.setShape(cuboid);
856
- }
857
- break;
858
- }
859
-
860
- if (sizeHasChanged) {
861
- const rb = col.attachedRigidbody;
862
- if (rb?.autoMass) {
863
- const ph = this.getBody(rb) as RigidBody
864
- ph?.recomputeMassPropertiesFromColliders();
865
- }
866
- }
867
- }
868
-
869
- private internalUpdateRigidbodyProperties(rb: IRigidbody, rigidbody: RigidBody) {
870
- // continuous collision detection
871
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#continuous-collision-detection
872
- rigidbody.enableCcd(rb.collisionDetectionMode !== CollisionDetectionMode.Discrete);
873
- rigidbody.setLinearDamping(rb.drag);
874
- rigidbody.setAngularDamping(rb.angularDrag);
875
- rigidbody.setGravityScale(rb.useGravity ? rb.gravityScale : 0, true);
876
-
877
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#dominance
878
- if (rb.dominanceGroup <= 127 && rb.dominanceGroup >= -127)
879
- rigidbody.setDominanceGroup(Math.floor(rb.dominanceGroup))
880
- else rigidbody.setDominanceGroup(0);
881
-
882
- if (rb.autoMass) {
883
- rigidbody.setAdditionalMass(0, false);
884
- for (let i = 0; i < rigidbody.numColliders(); i++) {
885
- const col = rigidbody.collider(i);
886
- col.setDensity(1);
887
- }
888
- rigidbody.recomputeMassPropertiesFromColliders();
889
- }
890
- else {
891
- rigidbody.setAdditionalMass(rb.mass, false);
892
- for (let i = 0; i < rigidbody.numColliders(); i++) {
893
- const col = rigidbody.collider(i);
894
- col.setDensity(0.0000001);
895
- }
896
- rigidbody.recomputeMassPropertiesFromColliders();
897
- }
898
-
899
- // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
900
- // rigidbody.setAdditionalMass(rb.mass, true);
901
- // for (let i = 0; i < rigidbody.numColliders(); i++) {
902
- // const collider = rigidbody.collider(i);
903
- // if (collider) {
904
- // collider.setMass(rb.mass);
905
- // // const density = rb.mass / collider.shape.computeMassProperties().mass;
906
- // }
907
- // }
908
-
909
- // lock rotations
910
- rigidbody.setEnabledRotations(!rb.lockRotationX, !rb.lockRotationY, !rb.lockRotationZ, true);
911
- rigidbody.setEnabledTranslations(!rb.lockPositionX, !rb.lockPositionY, !rb.lockPositionZ, true);
912
-
913
- if (rb.isKinematic) {
914
- rigidbody.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased);
915
- }
916
- else {
917
- rigidbody.setBodyType(RAPIER.RigidBodyType.Dynamic);
918
- }
919
- }
920
-
921
- // private _lastStepTime: number | undefined = 0;
922
- private lines?: LineSegments;
923
-
924
- public step(dt?: number) {
925
- if (!this.world) return;
926
- if (!this.enabled) return;
927
- this._isUpdatingPhysicsWorld = true;
928
- if (!this.eventQueue) {
929
- this.eventQueue = new EventQueue(false);
930
- }
931
- if (dt) {
932
- // if we make to sudden changes to the timestep the physics can get unstable
933
- // https://rapier.rs/docs/user_guides/javascript/integration_parameters/#dt
934
- this.world.timestep = Mathf.lerp(this.world.timestep, dt, 0.8);
935
- }
936
- this.world.step(this.eventQueue);
937
- this._isUpdatingPhysicsWorld = false;
938
- this.updateDebugRendering(this.world);
939
- }
940
-
941
- private updateDebugRendering(world: World) {
942
- if (debugPhysics || debugColliderPlacement || showColliders || this.debugRenderColliders === true) {
943
- if (!this.lines) {
944
- const material = new LineBasicMaterial({
945
- color: 0x227700,
946
- fog: false,
947
- // vertexColors: THREE.VertexColors
948
- });
949
- const geometry = new BufferGeometry();
950
- this.lines = new LineSegments(geometry, material);
951
- this.lines.layers.disableAll();
952
- this.lines.layers.enable(2);
953
- }
954
- if (this.lines.parent !== this.context?.scene)
955
- this.context?.scene.add(this.lines);
956
- const buffers = world.debugRender();
957
- this.lines.geometry.setAttribute('position', new BufferAttribute(buffers.vertices, 3));
958
- this.lines.geometry.setAttribute('color', new BufferAttribute(buffers.colors, 4));
959
- }
960
- else {
961
- if (this.lines) {
962
- this.context?.scene.remove(this.lines);
963
- }
964
- }
965
- }
966
-
967
- public postStep() {
968
- if (!this.world) return;
969
- if (!this.enabled) return;
970
- this._isUpdatingPhysicsWorld = true;
971
- this.syncObjects();
972
- this._isUpdatingPhysicsWorld = false;
973
-
974
- if (this.eventQueue && !this.collisionHandler) {
975
- this.collisionHandler = new PhysicsCollisionHandler(this.world, this.eventQueue);
976
- }
977
- if (this.collisionHandler) {
978
- this.collisionHandler.handleCollisionEvents();
979
- this.collisionHandler.update();
980
- }
981
- }
982
-
983
- /** sync rendered objects with physics world (except for colliders without rigidbody) */
984
- private syncObjects() {
985
- if (debugColliderPlacement) return;
986
- for (let i = 0; i < this.bodies.length; i++) {
987
- const obj = this.objects[i];
988
- const body = this.bodies[i] as Collider;
989
-
990
- // if the collider is not attached to a rigidbody
991
- // it means that its kinematic so we need to update its position
992
- const col = (obj as ICollider);
993
- if (col?.isCollider === true && !col.attachedRigidbody) {
994
- const rigidbody = body.parent();
995
- if (rigidbody)
996
- this.syncPhysicsBody(obj.gameObject, rigidbody, true, true);
997
- else
998
- this.syncPhysicsBody(obj.gameObject, body, true, true);
999
- continue;
1000
- }
1001
-
1002
-
1003
- // sync
1004
- const pos = body.translation();
1005
- const rot = body.rotation();
1006
- if (Number.isNaN(pos.x) || Number.isNaN(rot.x)) {
1007
- if (!col["__COLLIDER_NAN"] && isDevEnvironment()) {
1008
- console.warn("Collider has NaN values", col.name, col.gameObject, body);
1009
- col["__COLLIDER_NAN"] = true;
1010
- }
1011
- continue;
1012
- }
1013
-
1014
- // make sure to keep the collider offset
1015
- const center = obj["center"] as Vector3;
1016
- if (center && center.isVector3) {
1017
- this._tempQuaternion.set(rot.x, rot.y, rot.z, rot.w);
1018
- const offset = this._tempPosition.copy(center).applyQuaternion(this._tempQuaternion);
1019
- // const scale = getWorldScale(obj.gameObject);
1020
- // offset.multiply(scale);
1021
- pos.x -= offset.x;
1022
- pos.y -= offset.y;
1023
- pos.z -= offset.z;
1024
- }
1025
- setWorldPositionXYZ(obj.gameObject, pos.x, pos.y, pos.z);
1026
- setWorldQuaternionXYZW(obj.gameObject, rot.x, rot.y, rot.z, rot.w);
1027
- }
1028
- }
1029
-
1030
- private syncPhysicsBody(obj: Object3D, body: RigidBody | Collider, translation: boolean, rotation: boolean) {
1031
-
1032
- // const bodyType = body.bodyType();
1033
- // const previous = physicsBody.translation();
1034
- // const vel = physicsBody.linvel();
1035
-
1036
- if (body instanceof RigidBody) {
1037
- const worldPosition = getWorldPosition(obj, this._tempPosition);
1038
- const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
1039
- const type = body.bodyType();
1040
- switch (type) {
1041
- case RigidBodyType.Fixed:
1042
- case RigidBodyType.KinematicPositionBased:
1043
- case RigidBodyType.KinematicVelocityBased:
1044
- if (translation)
1045
- body.setNextKinematicTranslation(worldPosition);
1046
- if (rotation)
1047
- body.setNextKinematicRotation(worldQuaternion);
1048
- break;
1049
- default:
1050
- if (translation)
1051
- body.setTranslation(worldPosition, false);
1052
- if (rotation)
1053
- body.setRotation(worldQuaternion, false);
1054
- break;
1055
-
1056
- }
1057
- body.wakeUp();
1058
- }
1059
- else if (body instanceof Collider) {
1060
- const worldPosition = getWorldPosition(obj, this._tempPosition);
1061
- const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
1062
- const collider = body[$componentKey] as ICollider;
1063
- this.tryApplyCenter(collider, worldPosition);
1064
- if (translation)
1065
- body.setTranslation(worldPosition);
1066
- if (rotation)
1067
- body.setRotation(worldQuaternion);
1068
-
1069
- }
1070
-
1071
- // physicsBody.setBodyType(RAPIER.RigidBodyType.Fixed);
1072
- // physicsBody.setLinvel(vel, false);
1073
-
1074
- // update velocity
1075
- // const pos = physicsBody.translation();
1076
- // pos.x -= previous.x;
1077
- // pos.y -= previous.y;
1078
- // pos.z -= previous.z;
1079
- // // threhold
1080
- // const t = 1;
1081
- // const canUpdateVelocity = Math.abs(pos.x) < t && Math.abs(pos.y) < t && Math.abs(pos.z) < t;
1082
- // if (canUpdateVelocity) {
1083
- // const damping = 1 + this.context.time.deltaTime;
1084
- // vel.x *= damping;
1085
- // vel.y *= damping;
1086
- // vel.z *= damping;
1087
- // vel.x += pos.x;
1088
- // vel.y += pos.y;
1089
- // vel.z += pos.z;
1090
- // console.log(vel);
1091
- // physicsBody.setLinvel(vel, true);
1092
- // }
1093
- // else if(debugPhysics) console.warn("Movement exceeded threshold, not updating velocity", pos);
1094
-
1095
- // body.setBodyType(bodyType);
1096
- }
1097
-
1098
- private readonly _tempCenterPos: Vector3 = new Vector3();
1099
- private readonly _tempCenterVec:Vector3 = new Vector3();
1100
- private readonly _tempCenterQuaternion:Quaternion = new Quaternion();
1101
- private tryApplyCenter(collider: ICollider, targetVector: Vector3) {
1102
- const center = collider.center;
1103
- if (center && collider.gameObject) {
1104
- if (center.x !== 0 || center.y !== 0 || center.z !== 0) {
1105
- // TODO: fix export of center in editor integrations so we dont have to flip here
1106
- this._tempCenterPos.x = -center.x;
1107
- this._tempCenterPos.y = center.y;
1108
- this._tempCenterPos.z = center.z;
1109
- getWorldScale(collider.gameObject, this._tempCenterVec);
1110
- this._tempCenterPos.multiply(this._tempCenterVec);
1111
- const rot = getWorldQuaternion(collider.gameObject, this._tempCenterQuaternion);
1112
- this._tempCenterPos.applyQuaternion(rot);
1113
- targetVector.x += this._tempCenterPos.x;
1114
- targetVector.y += this._tempCenterPos.y;
1115
- targetVector.z += this._tempCenterPos.z;
1116
- }
1117
- }
1118
- }
1119
-
1120
- private static _matricesBuffer: Matrix4[] = [];
1121
- private getRigidbodyRelativeMatrix(comp: Object3D, rigidbody: Object3D, mat: Matrix4, matrices?: Matrix4[]): Matrix4 {
1122
- // collect all matrices to the rigidbody and then build the rigidbody relative matrix
1123
- if (matrices === undefined) {
1124
- matrices = RapierPhysics._matricesBuffer;
1125
- matrices.length = 0;
1126
- }
1127
- if (comp === rigidbody) {
1128
- const scale = getWorldScale(comp, this._tempPosition);
1129
- mat.makeScale(scale.x, scale.y, scale.z);
1130
- for (let i = matrices.length - 1; i >= 0; i--) {
1131
- mat.multiply(matrices[i]);
1132
- }
1133
- return mat;
1134
- }
1135
- matrices.push(comp.matrix);
1136
- if (comp.parent) {
1137
- this.getRigidbodyRelativeMatrix(comp.parent, rigidbody, mat, matrices);
1138
- }
1139
- return mat;
1140
- }
1141
-
1142
- private static centerConnectionPos = { x: 0, y: 0, z: 0 };
1143
- private static centerConnectionRot = { x: 0, y: 0, z: 0, w: 1 };
1144
-
1145
-
1146
-
1147
- addFixedJoint(body1: IRigidbody, body2: IRigidbody) {
1148
- if (!this.world) {
1149
- console.error("Physics world not initialized");
1150
- return;
1151
- }
1152
- const b1 = body1[$bodyKey] as RigidBody;
1153
- const b2 = body2[$bodyKey] as RigidBody;
1154
-
1155
- this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
1156
- this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
1157
-
1158
- const params = JointData.fixed(
1159
- RapierPhysics.centerConnectionPos, RapierPhysics.centerConnectionRot,
1160
- this._tempPosition, this._tempQuaternion,
1161
- );
1162
- const joint = this.world.createImpulseJoint(params, b1, b2, true);
1163
- if (debugPhysics)
1164
- console.log("ADD FIXED JOINT", joint)
1165
- }
1166
-
1167
-
1168
- /** The joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc. They are characterized by one local anchor as well as one local axis on each rigid-body. */
1169
- addHingeJoint(body1: IRigidbody, body2: IRigidbody, anchor: { x: number, y: number, z: number }, axis: { x: number, y: number, z: number }) {
1170
- if (!this.world) {
1171
- console.error("Physics world not initialized");
1172
- return;
1173
- }
1174
- const b1 = body1[$bodyKey] as RigidBody;
1175
- const b2 = body2[$bodyKey] as RigidBody;
1176
-
1177
- this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
1178
- this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
1179
-
1180
- const params = RAPIER.JointData.revolute(anchor, this._tempPosition, axis);
1181
- const joint = this.world.createImpulseJoint(params, b1, b2, true);
1182
- if (debugPhysics)
1183
- console.log("ADD HINGE JOINT", joint)
1184
- }
1185
-
1186
-
1187
- private calculateJointRelativeMatrices(body1: IGameObject, body2: IGameObject, mat: Matrix4) {
1188
- body1.updateWorldMatrix(true, false);
1189
- body2.updateWorldMatrix(true, false);
1190
- const world1 = body1.matrixWorld;
1191
- const world2 = body2.matrixWorld;
1192
- // set scale to 1
1193
- world1.elements[0] = 1;
1194
- world1.elements[5] = 1;
1195
- world1.elements[10] = 1;
1196
- world2.elements[0] = 1;
1197
- world2.elements[5] = 1;
1198
- world2.elements[10] = 1;
1199
- mat.copy(world2).premultiply(world1.invert()).invert();
1200
- }
1201
- }
1202
-
1203
-
1204
-
1205
- /** responsible of processing collision events for the component system */
1206
- class PhysicsCollisionHandler {
1207
-
1208
- readonly world: World;
1209
- readonly eventQueue: EventQueue;
1210
-
1211
- constructor(world: World, eventQueue: EventQueue) {
1212
- this.world = world;
1213
- this.eventQueue = eventQueue;
1214
- }
1215
-
1216
- private activeCollisions: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
1217
- private activeCollisionsStay: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
1218
- private activeTriggers: Array<{ collider: ICollider, component: IComponent, otherCollider: ICollider }> = [];
1219
-
1220
- handleCollisionEvents() {
1221
- if (!this.eventQueue) return;
1222
- if (!this.world) return;
1223
- this.eventQueue.drainCollisionEvents((handle1, handle2, started) => {
1224
- const col1 = this.world!.getCollider(handle1);
1225
- const col2 = this.world!.getCollider(handle2);
1226
- const colliderComponent1 = col1[$componentKey];
1227
- const colliderComponent2 = col2[$componentKey];
1228
- if (debugCollisions)
1229
- console.log("EVT", colliderComponent1.name, colliderComponent2.name, started, col1, col2);
1230
- if (colliderComponent1 && colliderComponent2) {
1231
- if (started) {
1232
- this.onCollisionStarted(colliderComponent1, col1, colliderComponent2, col2);
1233
- this.onCollisionStarted(colliderComponent2, col2, colliderComponent1, col1);
1234
- }
1235
- else {
1236
- this.onCollisionEnded(colliderComponent1, colliderComponent2);
1237
- this.onCollisionEnded(colliderComponent2, colliderComponent1);
1238
- }
1239
- }
1240
- });
1241
- }
1242
-
1243
- update() {
1244
- this.onHandleCollisionStay();
1245
- }
1246
-
1247
- private onCollisionStarted(self: ICollider, selfBody: Collider, other: ICollider, otherBody: Collider) {
1248
- let collision: Collision | null = null;
1249
-
1250
- // if one is a trigger we dont get collisions but want to raise the trigger events
1251
- if (self.isTrigger || other.isTrigger) {
1252
- foreachComponent(self.gameObject, (c: IComponent) => {
1253
- if (c.onTriggerEnter && !c.destroyed) {
1254
- c.onTriggerEnter(other);
1255
- }
1256
- this.activeTriggers.push({ collider: self, component: c, otherCollider: other });
1257
- });
1258
- }
1259
- else {
1260
- const object = self.gameObject;
1261
- // TODO: we dont respect the flip value here!
1262
- this.world.contactPair(selfBody, otherBody, (manifold, _flipped) => {
1263
- foreachComponent(object, (c: IComponent) => {
1264
- if (c.destroyed) return;
1265
- const hasDeclaredEventMethod = c.onCollisionEnter || c.onCollisionStay || c.onCollisionExit;
1266
- if (hasDeclaredEventMethod || debugCollisions) {
1267
- if (!collision) {
1268
- const contacts: Array<ContactPoint> = [];
1269
- const normal = manifold.normal();
1270
- for (let i = 0; i < manifold.numSolverContacts(); i++) {
1271
- // solver points are in world space
1272
- // https://rapier.rs/docs/user_guides/javascript/advanced_collision_detection_js#the-contact-graph
1273
- const pt = manifold.solverContactPoint(i);
1274
- const impulse = manifold.contactImpulse(i);
1275
- if (pt) {
1276
- const dist = manifold.contactDist(i);
1277
- const friction = manifold.solverContactFriction(i);
1278
- const tangentVelocity = manifold.solverContactTangentVelocity(i);
1279
- const contact = new ContactPoint(pt, dist, normal, impulse, friction, tangentVelocity);
1280
- contacts.push(contact);
1281
- if (debugCollisions) {
1282
- Gizmos.DrawDirection(pt, normal, 0xff0000, 3, true);
1283
- }
1284
- }
1285
- }
1286
- collision = new Collision(object, other, contacts);
1287
- }
1288
-
1289
- // we only need to keep track if any event exists
1290
- if (hasDeclaredEventMethod) {
1291
- const info = { collider: self, component: c, collision };
1292
-
1293
- this.activeCollisions.push(info);
1294
- if (c.onCollisionStay) {
1295
- this.activeCollisionsStay.push(info);
1296
- }
1297
-
1298
- c.onCollisionEnter?.call(c, collision);
1299
- }
1300
-
1301
- }
1302
- });
1303
- });
1304
- }
1305
- }
1306
-
1307
- private onHandleCollisionStay() {
1308
- for (const active of this.activeCollisionsStay) {
1309
- const c = active.component;
1310
- if (c.destroyed) continue;
1311
- if (c.activeAndEnabled && c.onCollisionStay) {
1312
- if(active.collision.collider.destroyed) continue;
1313
- const arg = active.collision;
1314
- c.onCollisionStay(arg);
1315
- }
1316
- }
1317
- for (const active of this.activeTriggers) {
1318
- const c = active.component;
1319
- if (c.destroyed) continue;
1320
- if (c.activeAndEnabled && c.onTriggerStay) {
1321
- const arg = active.otherCollider;
1322
- if(arg.destroyed) continue;
1323
- c.onTriggerStay(arg);
1324
- }
1325
- }
1326
- }
1327
-
1328
- private onCollisionEnded(self: ICollider, other: ICollider) {
1329
- if (self.destroyed || other.destroyed) return;
1330
- for (let i = 0; i < this.activeCollisions.length; i++) {
1331
- const active = this.activeCollisions[i];
1332
- const collider = active.collider;
1333
- if (collider.destroyed || active.collision.collider.destroyed) {
1334
- this.activeCollisions.splice(i, 1);
1335
- i--;
1336
- continue;
1337
- }
1338
- if (collider === self && active.collision.collider === other) {
1339
- const c = active.component;
1340
- this.activeCollisions.splice(i, 1);
1341
- i--;
1342
- if (c.activeAndEnabled && c.onCollisionExit) {
1343
- const collision = active.collision;
1344
- c.onCollisionExit(collision);
1345
- }
1346
- }
1347
- }
1348
- for (let i = 0; i < this.activeCollisionsStay.length; i++) {
1349
- const active = this.activeCollisionsStay[i];
1350
- const collider = active.collider;
1351
- if (collider.destroyed || active.collision.collider.destroyed) {
1352
- this.activeCollisionsStay.splice(i, 1);
1353
- i--;
1354
- continue;
1355
- }
1356
- if (collider === self && active.collision.collider === other) {
1357
- const c = active.component;
1358
- this.activeCollisionsStay.splice(i, 1);
1359
- i--;
1360
- if (c.activeAndEnabled && c.onCollisionExit) {
1361
- const collision = active.collision;
1362
- c.onCollisionExit(collision);
1363
- }
1364
- }
1365
- }
1366
- for (let i = 0; i < this.activeTriggers.length; i++) {
1367
- const active = this.activeTriggers[i];
1368
- const collider = active.collider;
1369
- if (collider.destroyed || active.otherCollider.destroyed) {
1370
- this.activeTriggers.splice(i, 1);
1371
- i--;
1372
- continue;
1373
- }
1374
- if (collider === self && active.otherCollider === other) {
1375
- const c = active.component;
1376
- this.activeTriggers.splice(i, 1);
1377
- i--;
1378
- if (c.activeAndEnabled && c.onTriggerExit) {
1379
- const collision = active.otherCollider;
1380
- c.onTriggerExit(collision);
1381
- }
1382
- }
1383
- }
1384
- }
1385
- }
1
+ import { BufferAttribute, BufferGeometry, LineBasicMaterial, LineSegments, Matrix4, Mesh, Object3D, Quaternion, Vector3 } from 'three'
2
+ import * as BufferGeometryUtils from 'three/examples/jsm/utils/BufferGeometryUtils.js'
3
+ import { CircularBuffer, getParam } from "./engine_utils.js"
4
+ import { getWorldPosition, getWorldQuaternion, getWorldScale, setWorldPositionXYZ, setWorldQuaternionXYZW } from "./engine_three_utils.js"
5
+ import type {
6
+ IPhysicsEngine,
7
+ IComponent,
8
+ ICollider,
9
+ IRigidbody,
10
+ Vec3,
11
+ IGameObject,
12
+ Vec2,
13
+ IContext,
14
+ ISphereCollider,
15
+ IBoxCollider,
16
+ } from './engine_types.js';
17
+ import { ContactPoint, Collision } from './engine_types.js';
18
+ import { foreachComponent } from './engine_gameobject.js';
19
+
20
+ import { ActiveCollisionTypes, ActiveEvents, CoefficientCombineRule, Ball, Collider, ColliderDesc, EventQueue, JointData, QueryFilterFlags, RigidBody, RigidBodyType, ShapeColliderTOI, World, Ray, ShapeType, Cuboid } from '@dimforge/rapier3d-compat';
21
+ import { CollisionDetectionMode, type PhysicsMaterial, PhysicsMaterialCombine } from '../engine/engine_physics.types.js';
22
+ import { Gizmos } from './engine_gizmos.js';
23
+ import { Mathf } from './engine_math.js';
24
+ import { SphereOverlapResult } from './engine_types.js';
25
+ import { ContextEvent, ContextRegistry } from './engine_context_registry.js';
26
+ import { isDevEnvironment } from './debug/debug.js';
27
+
28
+ const debugPhysics = getParam("debugphysics");
29
+ const debugColliderPlacement = getParam("debugcolliderplacement");
30
+ const debugCollisions = getParam("debugcollisions");
31
+ const showColliders = getParam("showcolliders");
32
+
33
+
34
+ /** on physics body and references the needle component */
35
+ const $componentKey = Symbol("needle component");
36
+ /** on needle component and references physics body */
37
+ const $bodyKey = Symbol("physics body");
38
+ const $colliderRigidbody = Symbol("rigidbody");
39
+
40
+
41
+ let RAPIER: undefined | any = undefined;
42
+ declare const NEEDLE_USE_RAPIER: boolean;
43
+ globalThis["NEEDLE_USE_RAPIER"] = globalThis["NEEDLE_USE_RAPIER"] !== undefined ? globalThis["NEEDLE_USE_RAPIER"] : true;
44
+ if (debugPhysics)
45
+ console.log("Use Rapier", NEEDLE_USE_RAPIER, globalThis["NEEDLE_USE_RAPIER"])
46
+
47
+ if (NEEDLE_USE_RAPIER) {
48
+ ContextRegistry.registerCallback(ContextEvent.ContextCreationStart, evt => {
49
+ if (debugPhysics)
50
+ console.log("Register rapier physics backend")
51
+ evt.context.physics.engine = new RapierPhysics();
52
+ // We want the physics engine to be initialized on start so when components start to enable and modify values they don't have delays
53
+ // TODO: should the promise be returned here to make the engine creation wait?
54
+ if (NEEDLE_USE_RAPIER) {
55
+ evt.context.physics.engine.initialize(evt.context);
56
+ }
57
+ });
58
+ }
59
+
60
+
61
+ declare type PhysicsBody = {
62
+ translation(): { x: number, y: number, z: number }
63
+ rotation(): { x: number, y: number, z: number, w: number }
64
+ }
65
+
66
+ export class RapierPhysics implements IPhysicsEngine {
67
+
68
+ /** Enable to draw collider shapes */
69
+ debugRenderColliders: boolean = false;
70
+
71
+ removeBody(obj: IComponent) {
72
+ if (!obj) return;
73
+ this.validate();
74
+ const body = obj[$bodyKey];
75
+ obj[$bodyKey] = null;
76
+ if (body && this.world) {
77
+ const index = this.objects.findIndex(o => o === obj);
78
+ if (index >= 0) {
79
+ const rapierBody = this.bodies[index];
80
+ // Remove references
81
+ this.bodies.splice(index, 1);
82
+ this.objects.splice(index, 1);
83
+
84
+
85
+ // Remove the collider from the physics world
86
+ if (rapierBody instanceof Collider) {
87
+ const rapierCollider = rapierBody as Collider;
88
+ this.world?.removeCollider(rapierCollider, true);
89
+
90
+ // also remove the rigidbody if it doesnt have colliders anymore
91
+ const rapierRigidbody: RigidBody | null = rapierCollider.parent();
92
+ if (rapierRigidbody && rapierRigidbody.numColliders() <= 0) {
93
+ const rigidbody = rapierRigidbody[$componentKey] as IRigidbody;
94
+ if (rigidbody) {
95
+ // If the collider was attached to a rigidbody and this rigidbody now has no colliders anymore we should ignore it - because the Rigidbody component will delete itself
96
+ }
97
+ else {
98
+ // But if there is no explicit rigidbody needle component then the colliders did create it implictly and thus we need to remove it here:
99
+ this.world?.removeRigidBody(rapierRigidbody);
100
+ }
101
+ }
102
+ }
103
+ // Remove the rigidbody from the physics world
104
+ else if (rapierBody instanceof RigidBody) {
105
+ if (rapierBody.numColliders() <= 0) {
106
+ this.world?.removeRigidBody(rapierBody);
107
+ }
108
+ else {
109
+ if (isDevEnvironment()) {
110
+ if (!rapierBody["did_log_removing"]) {
111
+ setTimeout(() => {
112
+ if (rapierBody.numColliders() > 0) {
113
+ rapierBody["did_log_removing"] = true;
114
+ console.warn("RapierPhysics: removing rigidbody with colliders from the physics world is not possible right now, please remove the colliders first");
115
+ }
116
+ }, 1);
117
+
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+
126
+ updateBody(comp: ICollider | IRigidbody, translation: boolean, rotation: boolean) {
127
+ this.validate();
128
+ if (!this.enabled) return;
129
+ if (comp.destroyed || !comp.gameObject) return;
130
+ if (!translation && !rotation) return;
131
+
132
+ if ((comp as ICollider).isCollider === true) {
133
+ // const collider = comp as ICollider;
134
+ console.warn("TODO: implement updating collider position");
135
+ }
136
+ else {
137
+ const rigidbody = comp as IRigidbody;
138
+ const body = rigidbody[$bodyKey];
139
+ if (body) {
140
+ this.syncPhysicsBody(rigidbody.gameObject, body, translation, rotation);
141
+ }
142
+ }
143
+ }
144
+
145
+ updateProperties(obj: IRigidbody | ICollider) {
146
+ this.validate();
147
+
148
+ if ((obj as ICollider).isCollider) {
149
+ const col = obj as ICollider;
150
+ const body = col[$bodyKey];
151
+ if (body) {
152
+ this.internalUpdateColliderProperties(col, body);
153
+ if(col.sharedMaterial)
154
+ this.updatePhysicsMaterial(col);
155
+ }
156
+ }
157
+ else {
158
+ const rb = obj as IRigidbody;
159
+ const physicsBody = this.internal_getRigidbody(rb);
160
+ if (physicsBody) {
161
+ this.internalUpdateRigidbodyProperties(rb, physicsBody);
162
+ }
163
+ }
164
+ }
165
+ addForce(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
166
+ this.validate();
167
+ const body = this.internal_getRigidbody(rigidbody);
168
+ body?.addForce(force, wakeup)
169
+ }
170
+ addImpulse(rigidbody: IRigidbody, force: Vec3, wakeup: boolean) {
171
+ this.validate();
172
+ const body = this.internal_getRigidbody(rigidbody);
173
+ body?.applyImpulse(force, wakeup)
174
+ }
175
+ getLinearVelocity(comp: IRigidbody | ICollider): Vec3 | null {
176
+ this.validate();
177
+ const body = this.internal_getRigidbody(comp);
178
+ if (body) {
179
+ const vel = body.linvel();
180
+ return vel;
181
+ }
182
+ return null;
183
+ }
184
+ getAngularVelocity(rb: IRigidbody): Vec3 | null {
185
+ this.validate();
186
+ const body = this.internal_getRigidbody(rb);
187
+ if (body) {
188
+ const vel = body.angvel();
189
+ return vel;
190
+ }
191
+ return null;
192
+ }
193
+ resetForces(rb: IRigidbody, wakeup: boolean) {
194
+ this.validate();
195
+ const body = this.internal_getRigidbody(rb);
196
+ body?.resetForces(wakeup);
197
+ }
198
+ resetTorques(rb: IRigidbody, wakeup: boolean) {
199
+ this.validate();
200
+ const body = this.internal_getRigidbody(rb);
201
+ body?.resetTorques(wakeup);
202
+ }
203
+ applyImpulse(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
204
+ this.validate();
205
+ const body = this.internal_getRigidbody(rb);
206
+ body?.applyImpulse(vec, wakeup);
207
+ }
208
+
209
+ wakeup(rb: IRigidbody) {
210
+ this.validate();
211
+ const body = this.internal_getRigidbody(rb);
212
+ body?.wakeUp();
213
+ }
214
+ setAngularVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
215
+ this.validate();
216
+ const body = this.internal_getRigidbody(rb);
217
+ body?.setAngvel(vec, wakeup);
218
+ }
219
+ setLinearVelocity(rb: IRigidbody, vec: Vec3, wakeup: boolean) {
220
+ this.validate();
221
+ const body = this.internal_getRigidbody(rb);
222
+ body?.setLinvel(vec, wakeup);
223
+ }
224
+
225
+ private context?: IContext;
226
+ private _initializePromise?: Promise<boolean>;
227
+ private _isInitialized: boolean = false;
228
+
229
+ async initialize(context: IContext) {
230
+ this.context = context;
231
+ if (!this._initializePromise)
232
+ this._initializePromise = this.internalInitialization();
233
+ return this._initializePromise;
234
+ }
235
+
236
+ private async internalInitialization() {
237
+ if (debugPhysics) console.log("Initialize rapier physics engine");
238
+ // NEEDLE_PHYSICS_INIT_START
239
+ // use .env file with VITE_NEEDLE_USE_RAPIER=false to treeshake rapier
240
+ // @ts-ignore
241
+ if ("env" in import.meta && import.meta.env.VITE_NEEDLE_USE_RAPIER === "false") {
242
+ if (debugPhysics) console.log("Rapier disabled");
243
+ return false;
244
+ }
245
+ // Can be transformed during build time to disable rapier
246
+ if (!NEEDLE_USE_RAPIER) return false;
247
+ if (this._hasCreatedWorld) {
248
+ console.error("Invalid call to create physics world: world is already created");
249
+ return true;
250
+ }
251
+ this._hasCreatedWorld = true;
252
+ if (RAPIER === undefined) {
253
+ if (debugPhysics) console.log("Import Rapier");
254
+ const _rapier = await import("@dimforge/rapier3d-compat");
255
+ if (debugPhysics) console.log("Init Rapier");
256
+ await _rapier.init();
257
+ // only assign after all loads are done to avoid a race condition
258
+ // where RAPIER is already set and then used while actually still waiting for initialization.
259
+ RAPIER = _rapier;
260
+ }
261
+ if (debugPhysics) console.log("Physics engine initialized, creating world...");
262
+ this._world = new World(this._gravity);
263
+ this.enabled = true;
264
+ this._isInitialized = true;
265
+ if (debugPhysics) console.log("Physics world created");
266
+ return true;
267
+ // NEEDLE_PHYSICS_INIT_END
268
+ }
269
+
270
+ /** Check is the physics engine has been initialized and the call can be made */
271
+ private validate() {
272
+ if (!this._isInitialized) {
273
+ if (debugPhysics) {
274
+ this["_lastWarnTime"] = this["_lastWarnTime"] ?? 0;
275
+ if (Date.now() - this["_lastWarnTime"] > 1000) {
276
+ this["_lastWarnTime"] = Date.now();
277
+ console.warn("Physics engine is not initialized");
278
+ }
279
+ }
280
+ }
281
+ }
282
+
283
+
284
+ private rapierRay = new Ray({ x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 1 });
285
+ private raycastVectorsBuffer = new CircularBuffer(() => new Vector3(), 10);
286
+ public raycast(origin?: Vec2 | Vec3, direction?: Vec3, maxDistance?: number, solid?: boolean)
287
+ : null | { point: Vector3, collider: ICollider } {
288
+
289
+ if (maxDistance === undefined) maxDistance = Infinity;
290
+ if (solid === undefined) solid = true;
291
+
292
+ const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
293
+ if (!ray) return null;
294
+
295
+ const hit = this.world?.castRay(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
296
+ // ignore objects in the IgnoreRaycast=2 layer
297
+ return !c[$componentKey]?.gameObject.layers.isEnabled(2);
298
+ });
299
+ if (hit) {
300
+ const point = ray.pointAt(hit.toi);
301
+ const vec = this.raycastVectorsBuffer.get();
302
+ vec.set(point.x, point.y, point.z);
303
+ return { point: vec, collider: hit.collider[$componentKey] };
304
+ }
305
+
306
+ return null;
307
+ }
308
+
309
+ public raycastAndGetNormal(origin?: Vec2 | Vec3, direction?: Vec3, maxDistance?: number, solid?: boolean)
310
+ : null | { point: Vector3, normal: Vector3, collider: ICollider } {
311
+
312
+ if (maxDistance === undefined) maxDistance = Infinity;
313
+ if (solid === undefined) solid = true;
314
+
315
+ const ray = this.getPhysicsRay(this.rapierRay, origin, direction);
316
+ if (!ray) return null;
317
+
318
+ const hit = this.world?.castRayAndGetNormal(ray, maxDistance, solid, undefined, undefined, undefined, undefined, (c) => {
319
+ // ignore objects in the IgnoreRaycast=2 layer
320
+ return !c[$componentKey]?.gameObject.layers.isEnabled(2);
321
+ });
322
+ if (hit) {
323
+ const point = ray.pointAt(hit.toi);
324
+ const normal = hit.normal;
325
+ const vec = this.raycastVectorsBuffer.get();
326
+ const nor = this.raycastVectorsBuffer.get();
327
+ vec.set(point.x, point.y, point.z);
328
+ nor.set(normal.x, normal.y, normal.z);
329
+ return { point: vec, normal: nor, collider: hit.collider[$componentKey] };
330
+ }
331
+ return null;
332
+ }
333
+
334
+ private getPhysicsRay(ray: Ray, origin?: Vec2 | Vec3, direction?: Vec3): Ray | null {
335
+ const cam = this.context?.mainCamera;
336
+ if (origin === undefined) {
337
+ const pos = this.context?.input.getPointerPosition(0);
338
+ if (pos) origin = pos;
339
+ else return null;
340
+ }
341
+ // if we get origin in 2d space we need to project it to 3d space
342
+ if (origin["z"] === undefined) {
343
+ if (!cam) {
344
+ console.error("Can not perform raycast from 2d point - no main camera found");
345
+ return null;
346
+ }
347
+ const vec3 = this.raycastVectorsBuffer.get();
348
+ vec3.x = origin.x;
349
+ vec3.y = origin.y;
350
+ vec3.z = 0;
351
+ // if the origin is in screen space we need to convert it to raycaster space
352
+ if (vec3.x > 1 || vec3.y > 1 || vec3.y < -1 || vec3.x < -1) {
353
+ if (debugPhysics) console.warn("Converting screenspace to raycast space", vec3)
354
+ this.context?.input.convertScreenspaceToRaycastSpace(vec3);
355
+ }
356
+ vec3.unproject(cam);
357
+ origin = vec3;
358
+ }
359
+
360
+ const o = origin as Vec3;
361
+
362
+ ray.origin.x = o.x;
363
+ ray.origin.y = o.y;
364
+ ray.origin.z = o.z;
365
+ const vec = this.raycastVectorsBuffer.get();
366
+ if (direction)
367
+ vec.set(direction.x, direction.y, direction.z);
368
+ else {
369
+ if (!cam) {
370
+ console.error("Can not perform raycast - no camera found");
371
+ return null;
372
+ }
373
+ vec.set(ray.origin.x, ray.origin.y, ray.origin.z);
374
+ const camPosition = getWorldPosition(cam);
375
+ vec.sub(camPosition);
376
+ }
377
+ // we need to normalize the ray because our input is a max travel length and the direction may be not normalized
378
+ vec.normalize();
379
+ ray.dir.x = vec.x;
380
+ ray.dir.y = vec.y;
381
+ ray.dir.z = vec.z;
382
+ // Gizmos.DrawRay(ray.origin, ray.dir, 0xff0000, Infinity);
383
+ return ray;
384
+ }
385
+
386
+
387
+ private rapierSphere: Ball | null = null;
388
+ private rapierColliderArray: Array<SphereOverlapResult> = [];
389
+ private readonly rapierIdentityRotation = { x: 0, y: 0, z: 0, w: 1 };
390
+ private readonly rapierForwardVector = { x: 0, y: 0, z: 1 };
391
+ /** Precice sphere overlap detection using rapier against colliders
392
+ * @param point center of the sphere in worldspace
393
+ * @param radius radius of the sphere
394
+ * @returns array of colliders that overlap with the sphere. Note: they currently only contain the collider and the gameobject
395
+ */
396
+ public sphereOverlap(point: Vector3, radius: number): Array<SphereOverlapResult> {
397
+ this.rapierColliderArray.length = 0;
398
+ if (!this.world) return this.rapierColliderArray;
399
+ if (!this.rapierSphere)
400
+ this.rapierSphere = new Ball(radius);
401
+ this.rapierSphere.radius = radius;
402
+
403
+ this.world.intersectionsWithShape(point, this.rapierIdentityRotation, this.rapierSphere, col => {
404
+ const collider = col[$componentKey] as ICollider
405
+ // if (collider.gameObject.layers.isEnabled(2)) return true;
406
+ const intersection = new SphereOverlapResult(collider.gameObject, collider);
407
+ this.rapierColliderArray.push(intersection);
408
+ return true; // Return `false` instead if we want to stop searching for other colliders that contain this point.
409
+ }, QueryFilterFlags.EXCLUDE_SENSORS, undefined, undefined, undefined,
410
+ col => {
411
+ const collider = col[$componentKey] as ICollider
412
+ return collider.gameObject.layers.isEnabled(2) == false
413
+ }
414
+ );
415
+ return this.rapierColliderArray;
416
+
417
+
418
+ // TODO: this only returns one hit
419
+ // let filterGroups = 0xffffffff;
420
+ // filterGroups &= ~(1 << 2);
421
+ // const hit: ShapeColliderTOI | null = this.world.castShape(point,
422
+ // this.rapierIdentityRotation,
423
+ // this.rapierForwardVector,
424
+ // this.rapierSphere,
425
+ // 0,
426
+ // QueryFilterFlags.EXCLUDE_SENSORS,
427
+ // // filterGroups,
428
+ // );
429
+ // // console.log(hit);
430
+ // if (hit) {
431
+ // const collider = hit.collider[$componentKey] as ICollider
432
+ // const intersection = new SphereOverlapResult(collider.gameObject);
433
+ // this.rapierColliderArray.push(intersection);
434
+ // // const localpt = hit.witness2;
435
+ // // // const normal = hit.normal2;
436
+ // // const hitPoint = new Vector3(localpt.x, localpt.y, localpt.z);
437
+ // // // collider.gameObject.localToWorld(hitPoint);
438
+ // // // const normalPt = new Vector3(normal.x, normal.y, normal.z);
439
+ // // // const mat = new Matrix4().setPosition(point).scale(new Vector3(radius, radius, radius));
440
+ // // // hitPoint.applyMatrix4(mat);
441
+ // // console.log(hit.witness2)
442
+ // // // hitPoint.add(point);
443
+ // // const dist = hitPoint.distanceTo(point);
444
+ // }
445
+
446
+ // return this.rapierColliderArray;
447
+ }
448
+
449
+
450
+
451
+
452
+ // physics simulation
453
+
454
+ enabled: boolean = false;
455
+ /** Get access to the rapier world */
456
+ public get world(): World | undefined { return this._world };
457
+
458
+ private _tempPosition: Vector3 = new Vector3();
459
+ private _tempQuaternion: Quaternion = new Quaternion();
460
+ private _tempScale: Vector3 = new Vector3();
461
+ private _tempMatrix: Matrix4 = new Matrix4();
462
+
463
+ private static _didLoadPhysicsEngine: boolean = false;
464
+
465
+ private _isUpdatingPhysicsWorld: boolean = false;
466
+ get isUpdating(): boolean { return this._isUpdatingPhysicsWorld; }
467
+
468
+
469
+ private _world?: World;
470
+ private _hasCreatedWorld: boolean = false;
471
+ private eventQueue?: EventQueue;
472
+ private collisionHandler?: PhysicsCollisionHandler;
473
+
474
+
475
+ private objects: IComponent[] = [];
476
+ private bodies: PhysicsBody[] = [];
477
+
478
+ private _meshCache: Map<string, Float32Array> = new Map<string, Float32Array>();
479
+
480
+ private _gravity = { x: 0.0, y: -9.81, z: 0.0 };
481
+
482
+ get gravity() {
483
+ return this.world?.gravity ?? this._gravity;
484
+ }
485
+
486
+ set gravity(value: Vec3) {
487
+ if (this.world) {
488
+ this.world.gravity = value;
489
+ }
490
+ else {
491
+ this._gravity = value;
492
+ }
493
+ }
494
+
495
+ clearCaches() {
496
+ this._meshCache.clear();
497
+ if (this.eventQueue?.raw)
498
+ this.eventQueue?.free();
499
+ if (this.world?.bodies)
500
+ this.world?.free();
501
+ }
502
+
503
+ async addBoxCollider(collider: ICollider, size: Vector3) {
504
+ if (!this._isInitialized)
505
+ await this.initialize(collider.context);
506
+ if (!collider.activeAndEnabled) return;
507
+
508
+ if (!this.enabled) {
509
+ if (debugPhysics) console.warn("Physics are disabled");
510
+ return;
511
+ }
512
+ const obj = collider.gameObject;
513
+ const scale = getWorldScale(obj, this._tempPosition).multiply(size);
514
+ scale.multiplyScalar(0.5);
515
+
516
+ // prevent negative scale
517
+ if (scale.x < 0)
518
+ scale.x = Math.abs(scale.x);
519
+ if (scale.y < 0)
520
+ scale.y = Math.abs(scale.y);
521
+ if (scale.z < 0)
522
+ scale.z = Math.abs(scale.z);
523
+
524
+ // prevent zero scale - seems normals are flipped otherwise
525
+ if (scale.x == 0) scale.x = 0.0000001;
526
+ if (scale.y == 0) scale.y = 0.0000001;
527
+ if (scale.z == 0) scale.z = 0.0000001;
528
+
529
+ const desc = ColliderDesc.cuboid(scale.x, scale.y, scale.z);
530
+ // const objectLayerMask = collider.gameObject.layers.mask;
531
+ // const mask = objectLayerMask & ~2;
532
+ // TODO: https://rapier.rs/docs/user_guides/javascript/colliders/#collision-groups-and-solver-groups
533
+ // desc.setCollisionGroups(objectLayerMask);
534
+ this.createCollider(collider, desc);
535
+ }
536
+
537
+ async addSphereCollider(collider: ICollider) {
538
+ if (!this._isInitialized)
539
+ await this.initialize(collider.context);
540
+ if (!collider.activeAndEnabled) return;
541
+ if (!this.enabled) {
542
+ if (debugPhysics) console.warn("Physics are disabled");
543
+ return;
544
+ }
545
+ const desc = ColliderDesc.ball(.5);
546
+ this.createCollider(collider, desc);
547
+ this.updateProperties(collider);
548
+ }
549
+
550
+ async addCapsuleCollider(collider: ICollider, height: number, radius: number) {
551
+ if (!this._isInitialized)
552
+ await this.initialize(collider.context);
553
+ if (!collider.activeAndEnabled) return;
554
+ if (!this.enabled) {
555
+ if (debugPhysics) console.warn("Physics are disabled");
556
+ return;
557
+ }
558
+ const obj = collider.gameObject;
559
+ const scale = getWorldScale(obj, this._tempPosition);
560
+ // Prevent negative scales
561
+ scale.x = Math.abs(scale.x);
562
+ scale.y = Math.abs(scale.y);
563
+ const finalRadius = radius * scale.x;
564
+ // half height = distance between capsule origin and top sphere origin (not the top end of the capsule)
565
+ height = Math.max(height, finalRadius * 2);
566
+ const hh = Mathf.clamp((height * .5 * scale.y) - (radius * scale.x), 0, Number.MAX_SAFE_INTEGER);
567
+ const desc = ColliderDesc.capsule(hh, finalRadius);
568
+ this.createCollider(collider, desc);
569
+ }
570
+
571
+ async addMeshCollider(collider: ICollider, mesh: Mesh, convex: boolean, scale: Vector3) {
572
+ if (!this._isInitialized)
573
+ await this.initialize(collider.context);
574
+ if (!collider.activeAndEnabled) return;
575
+ if (!this.enabled) {
576
+ if (debugPhysics) console.warn("Physics are disabled");
577
+ return;
578
+ }
579
+ let geo = mesh.geometry;
580
+ if (!geo) {
581
+ if (debugPhysics) console.warn("Missing mesh geometry", mesh.name);
582
+ return;
583
+ }
584
+
585
+ // check if mesh is indexed, if not generate indices
586
+ if (!geo.index?.array?.length) {
587
+ console.warn(`Your MeshCollider is missing vertices or indices in the assined mesh \"${mesh.name}\". Consider providing an indexed geometry.`);
588
+ geo = BufferGeometryUtils.mergeVertices(geo);
589
+ }
590
+
591
+ let positions = geo.getAttribute("position").array as Float32Array;
592
+ const indices = geo.index?.array as Uint32Array;
593
+
594
+ // scaling seems not supported yet https://github.com/dimforge/rapier/issues/243
595
+ if (Math.abs(scale.x - 1) > 0.0001 || Math.abs(scale.y - 1) > 0.0001 || Math.abs(scale.z - 1) > 0.0001) {
596
+ const key = geo.uuid + "_" + scale.x + "_" + scale.y + "_" + scale.z + "_" + convex;
597
+ if (this._meshCache.has(key)) {
598
+ if (debugPhysics) console.warn("Use cached mesh collider")
599
+ positions = this._meshCache.get(key)!;
600
+ }
601
+ else {
602
+ console.warn(`Your MeshCollider \"${collider.name}\" is scaled (${scale.x}, ${scale.y}, ${scale.z})\nthis is not optimal for performance since this isn't supported by the Rapier physics engine yet. Consider applying the scale to the collider mesh`);
603
+ // showBalloonWarning("Your model is using scaled mesh colliders which is not optimal for performance: " + mesh.name + ", consider using unscaled objects");
604
+ const scaledPositions = new Float32Array(positions.length);
605
+ for (let i = 0; i < positions.length; i += 3) {
606
+ scaledPositions[i] = positions[i] * scale.x;
607
+ scaledPositions[i + 1] = positions[i + 1] * scale.y;
608
+ scaledPositions[i + 2] = positions[i + 2] * scale.z;
609
+ }
610
+ positions = scaledPositions;
611
+ this._meshCache.set(key, scaledPositions);
612
+ }
613
+ }
614
+ const desc = convex ? ColliderDesc.convexHull(positions) : ColliderDesc.trimesh(positions, indices);
615
+ if (desc) {
616
+ this.createCollider(collider, desc);
617
+ // col.setMassProperties(1, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0 }, { x: 0, y: 0, z: 0, w: 1 });
618
+ // rb?.setTranslation({ x: 0, y: 2, z: 0 });
619
+ // col.setTranslationWrtParent(new Vector3(0,2,0));
620
+
621
+ }
622
+ }
623
+
624
+ updatePhysicsMaterial(col: ICollider) {
625
+ if (!col) return;
626
+ const physicsMaterial = col.sharedMaterial;
627
+ const rapier_collider = col[$bodyKey] as Collider;
628
+ if (!rapier_collider) return;
629
+
630
+ if (physicsMaterial) {
631
+ if (physicsMaterial.bounciness !== undefined)
632
+ rapier_collider.setRestitution(physicsMaterial.bounciness);
633
+
634
+ if (physicsMaterial.bounceCombine !== undefined) {
635
+ switch (physicsMaterial.bounceCombine) {
636
+ case PhysicsMaterialCombine.Average:
637
+ rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Average);
638
+ break;
639
+ case PhysicsMaterialCombine.Maximum:
640
+ rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Max);
641
+ break;
642
+ case PhysicsMaterialCombine.Minimum:
643
+ rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Min);
644
+ break;
645
+ case PhysicsMaterialCombine.Multiply:
646
+ rapier_collider.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
647
+ break;
648
+ }
649
+ }
650
+
651
+ if (physicsMaterial.dynamicFriction !== undefined)
652
+ rapier_collider.setFriction(physicsMaterial.dynamicFriction);
653
+
654
+ if (physicsMaterial.frictionCombine !== undefined) {
655
+ switch (physicsMaterial.frictionCombine) {
656
+ case PhysicsMaterialCombine.Average:
657
+ rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Average);
658
+ break;
659
+ case PhysicsMaterialCombine.Maximum:
660
+ rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Max);
661
+ break;
662
+ case PhysicsMaterialCombine.Minimum:
663
+ rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Min);
664
+ break;
665
+ case PhysicsMaterialCombine.Multiply:
666
+ rapier_collider.setFrictionCombineRule(CoefficientCombineRule.Multiply);
667
+ break;
668
+ }
669
+ }
670
+ }
671
+ }
672
+
673
+ /** Get the rapier body for a Needle component */
674
+ getBody(obj: ICollider | IRigidbody): null | any {
675
+ if (!obj) return null;
676
+ const body = obj[$bodyKey];
677
+ return body;
678
+ }
679
+
680
+ /** Get the Needle Engine component for a rapier object */
681
+ getComponent(rapierObject: object): IComponent | null {
682
+ if (!rapierObject) return null;
683
+ const component = rapierObject[$componentKey];
684
+ return component;
685
+ }
686
+
687
+ private createCollider(collider: ICollider, desc: ColliderDesc) {
688
+ if (!this.world) throw new Error("Physics world not initialized");
689
+ const matrix = this._tempMatrix;
690
+ let rigidBody: RigidBody | undefined = undefined;
691
+ if (!collider.attachedRigidbody) {
692
+ if(debugPhysics) console.log("Create collider without rigidbody", collider.name);
693
+ matrix.makeRotationFromQuaternion(getWorldQuaternion(collider.gameObject));
694
+ matrix.setPosition(getWorldPosition(collider.gameObject));
695
+ }
696
+ else {
697
+ rigidBody = this.getRigidbody(collider, this._tempMatrix);
698
+ }
699
+
700
+ matrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
701
+ this.tryApplyCenter(collider, this._tempPosition);
702
+ desc.setTranslation(this._tempPosition.x, this._tempPosition.y, this._tempPosition.z);
703
+ desc.setRotation(this._tempQuaternion);
704
+ desc.setSensor(collider.isTrigger);
705
+
706
+ // TODO: we might want to update this if the material changes
707
+ const physicsMaterial = collider.sharedMaterial;
708
+ if (physicsMaterial) {
709
+
710
+ if (physicsMaterial.bounciness !== undefined)
711
+ desc.setRestitution(physicsMaterial.bounciness);
712
+
713
+ if (physicsMaterial.bounceCombine !== undefined) {
714
+ switch (physicsMaterial.bounceCombine) {
715
+ case PhysicsMaterialCombine.Average:
716
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Average);
717
+ break;
718
+ case PhysicsMaterialCombine.Maximum:
719
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Max);
720
+ break;
721
+ case PhysicsMaterialCombine.Minimum:
722
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Min);
723
+ break;
724
+ case PhysicsMaterialCombine.Multiply:
725
+ desc.setRestitutionCombineRule(CoefficientCombineRule.Multiply);
726
+ break;
727
+ }
728
+ }
729
+
730
+ if (physicsMaterial.dynamicFriction !== undefined)
731
+ desc.setFriction(physicsMaterial.dynamicFriction);
732
+
733
+ if (physicsMaterial.frictionCombine !== undefined) {
734
+ switch (physicsMaterial.frictionCombine) {
735
+ case PhysicsMaterialCombine.Average:
736
+ desc.setFrictionCombineRule(CoefficientCombineRule.Average);
737
+ break;
738
+ case PhysicsMaterialCombine.Maximum:
739
+ desc.setFrictionCombineRule(CoefficientCombineRule.Max);
740
+ break;
741
+ case PhysicsMaterialCombine.Minimum:
742
+ desc.setFrictionCombineRule(CoefficientCombineRule.Min);
743
+ break;
744
+ case PhysicsMaterialCombine.Multiply:
745
+ desc.setFrictionCombineRule(CoefficientCombineRule.Multiply);
746
+ break;
747
+ }
748
+ }
749
+ }
750
+
751
+ // if we want to use explicit mass properties, we need to set the collider density to 0
752
+ // otherwise rapier will compute the mass properties based on the collider shape and density
753
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
754
+ if (collider.attachedRigidbody?.autoMass === false) {
755
+ desc.setDensity(.000001);
756
+ desc.setMass(.000001);
757
+ }
758
+
759
+ try {
760
+ const col = this.world.createCollider(desc, rigidBody);
761
+ col[$componentKey] = collider;
762
+ collider[$bodyKey] = col;
763
+ col.setActiveEvents(ActiveEvents.COLLISION_EVENTS);
764
+ // We want to receive collisitons between two triggers too
765
+ col.setActiveCollisionTypes(ActiveCollisionTypes.ALL);
766
+
767
+ // const objectLayerMask = collider.gameObject.layers.mask;
768
+ // const mask = objectLayerMask & ~2;
769
+ // col.setCollisionGroups(objectLayerMask);
770
+ this.objects.push(collider);
771
+ this.bodies.push(col);
772
+ return col;
773
+ }
774
+ catch (e) {
775
+ console.error("Error creating collider \"" + collider.name + "\"\nError:", e);
776
+ return null;
777
+ }
778
+ }
779
+
780
+ private getRigidbody(collider: ICollider, _matrix: Matrix4): RigidBody {
781
+
782
+ if (!this.world) throw new Error("Physics world not initialized");
783
+ let rigidBody: RigidBody | null = null;
784
+
785
+ if (collider.attachedRigidbody) {
786
+ const rb = collider.attachedRigidbody;
787
+ rigidBody = rb[$bodyKey];
788
+ if (!rigidBody) {
789
+ const kinematic = rb.isKinematic && !debugColliderPlacement;
790
+ if (debugPhysics)
791
+ console.log("Create rigidbody", kinematic);
792
+ const rigidBodyDesc = kinematic ? RAPIER.RigidBodyDesc.kinematicPositionBased() : RAPIER.RigidBodyDesc.dynamic();
793
+ const pos = getWorldPosition(collider.attachedRigidbody.gameObject);
794
+ rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
795
+ rigidBodyDesc.setRotation(getWorldQuaternion(collider.attachedRigidbody.gameObject));
796
+ rigidBody = this.world.createRigidBody(rigidBodyDesc);
797
+ this.bodies.push(rigidBody);
798
+ this.objects.push(rb);
799
+ }
800
+ rigidBody[$componentKey] = rb;
801
+ rb[$bodyKey] = rigidBody;
802
+ this.internalUpdateRigidbodyProperties(rb, rigidBody);
803
+ this.getRigidbodyRelativeMatrix(collider.gameObject, rb.gameObject, _matrix);
804
+ collider[$colliderRigidbody] = rigidBody;
805
+ }
806
+ else {
807
+ const rigidBodyDesc = RAPIER.RigidBodyDesc.kinematicPositionBased();
808
+ const pos = getWorldPosition(collider.gameObject);
809
+ rigidBodyDesc.setTranslation(pos.x, pos.y, pos.z);
810
+ rigidBodyDesc.setRotation(getWorldQuaternion(collider.gameObject));
811
+ rigidBody = this.world.createRigidBody(rigidBodyDesc);
812
+ _matrix.identity();
813
+ rigidBody[$componentKey] = null;
814
+ }
815
+
816
+
817
+ return rigidBody;
818
+ }
819
+
820
+ private internal_getRigidbody(rb: IRigidbody | ICollider): RigidBody | null {
821
+ if ((rb as ICollider).isCollider === true) return rb[$colliderRigidbody] as RigidBody;
822
+ return rb[$bodyKey] as RigidBody;
823
+ }
824
+
825
+ private internalUpdateColliderProperties(col: ICollider, collider: Collider) {
826
+ const shape = collider.shape;
827
+ let sizeHasChanged = false;
828
+ switch (shape.type) {
829
+ // Sphere Collider
830
+ case ShapeType.Ball:
831
+ {
832
+ const ball = shape as Ball;
833
+ const sc = col as ISphereCollider;
834
+ const obj = col.gameObject;
835
+ const scale = getWorldScale(obj, this._tempPosition);
836
+ const radius = Math.abs(sc.radius * scale.x);
837
+ sizeHasChanged = ball.radius !== radius;
838
+ ball.radius = radius;
839
+ if (sizeHasChanged) {
840
+ collider.setShape(ball);
841
+ }
842
+ break;
843
+ }
844
+ case ShapeType.Cuboid:
845
+ const cuboid = shape as Cuboid;
846
+ const sc = col as IBoxCollider;
847
+ const newX = sc.size.x * 0.5;
848
+ const newY = sc.size.y * 0.5;
849
+ const newZ = sc.size.z * 0.5;
850
+ sizeHasChanged = cuboid.halfExtents.x !== newX || cuboid.halfExtents.y !== newY || cuboid.halfExtents.z !== newZ;
851
+ cuboid.halfExtents.x = newX;
852
+ cuboid.halfExtents.y = newY;
853
+ cuboid.halfExtents.z = newZ;
854
+ if (sizeHasChanged) {
855
+ collider.setShape(cuboid);
856
+ }
857
+ break;
858
+ }
859
+
860
+ if (sizeHasChanged) {
861
+ const rb = col.attachedRigidbody;
862
+ if (rb?.autoMass) {
863
+ const ph = this.getBody(rb) as RigidBody
864
+ ph?.recomputeMassPropertiesFromColliders();
865
+ }
866
+ }
867
+ }
868
+
869
+ private internalUpdateRigidbodyProperties(rb: IRigidbody, rigidbody: RigidBody) {
870
+ // continuous collision detection
871
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#continuous-collision-detection
872
+ rigidbody.enableCcd(rb.collisionDetectionMode !== CollisionDetectionMode.Discrete);
873
+ rigidbody.setLinearDamping(rb.drag);
874
+ rigidbody.setAngularDamping(rb.angularDrag);
875
+ rigidbody.setGravityScale(rb.useGravity ? rb.gravityScale : 0, true);
876
+
877
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#dominance
878
+ if (rb.dominanceGroup <= 127 && rb.dominanceGroup >= -127)
879
+ rigidbody.setDominanceGroup(Math.floor(rb.dominanceGroup))
880
+ else rigidbody.setDominanceGroup(0);
881
+
882
+ if (rb.autoMass) {
883
+ rigidbody.setAdditionalMass(0, false);
884
+ for (let i = 0; i < rigidbody.numColliders(); i++) {
885
+ const col = rigidbody.collider(i);
886
+ col.setDensity(1);
887
+ }
888
+ rigidbody.recomputeMassPropertiesFromColliders();
889
+ }
890
+ else {
891
+ rigidbody.setAdditionalMass(rb.mass, false);
892
+ for (let i = 0; i < rigidbody.numColliders(); i++) {
893
+ const col = rigidbody.collider(i);
894
+ col.setDensity(0.0000001);
895
+ }
896
+ rigidbody.recomputeMassPropertiesFromColliders();
897
+ }
898
+
899
+ // https://rapier.rs/docs/user_guides/javascript/rigid_bodies#mass-properties
900
+ // rigidbody.setAdditionalMass(rb.mass, true);
901
+ // for (let i = 0; i < rigidbody.numColliders(); i++) {
902
+ // const collider = rigidbody.collider(i);
903
+ // if (collider) {
904
+ // collider.setMass(rb.mass);
905
+ // // const density = rb.mass / collider.shape.computeMassProperties().mass;
906
+ // }
907
+ // }
908
+
909
+ // lock rotations
910
+ rigidbody.setEnabledRotations(!rb.lockRotationX, !rb.lockRotationY, !rb.lockRotationZ, true);
911
+ rigidbody.setEnabledTranslations(!rb.lockPositionX, !rb.lockPositionY, !rb.lockPositionZ, true);
912
+
913
+ if (rb.isKinematic) {
914
+ rigidbody.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased);
915
+ }
916
+ else {
917
+ rigidbody.setBodyType(RAPIER.RigidBodyType.Dynamic);
918
+ }
919
+ }
920
+
921
+ // private _lastStepTime: number | undefined = 0;
922
+ private lines?: LineSegments;
923
+
924
+ public step(dt?: number) {
925
+ if (!this.world) return;
926
+ if (!this.enabled) return;
927
+ this._isUpdatingPhysicsWorld = true;
928
+ if (!this.eventQueue) {
929
+ this.eventQueue = new EventQueue(false);
930
+ }
931
+ if (dt) {
932
+ // if we make to sudden changes to the timestep the physics can get unstable
933
+ // https://rapier.rs/docs/user_guides/javascript/integration_parameters/#dt
934
+ this.world.timestep = Mathf.lerp(this.world.timestep, dt, 0.8);
935
+ }
936
+ this.world.step(this.eventQueue);
937
+ this._isUpdatingPhysicsWorld = false;
938
+ this.updateDebugRendering(this.world);
939
+ }
940
+
941
+ private updateDebugRendering(world: World) {
942
+ if (debugPhysics || debugColliderPlacement || showColliders || this.debugRenderColliders === true) {
943
+ if (!this.lines) {
944
+ const material = new LineBasicMaterial({
945
+ color: 0x227700,
946
+ fog: false,
947
+ // vertexColors: THREE.VertexColors
948
+ });
949
+ const geometry = new BufferGeometry();
950
+ this.lines = new LineSegments(geometry, material);
951
+ this.lines.layers.disableAll();
952
+ this.lines.layers.enable(2);
953
+ }
954
+ if (this.lines.parent !== this.context?.scene)
955
+ this.context?.scene.add(this.lines);
956
+ const buffers = world.debugRender();
957
+ this.lines.geometry.setAttribute('position', new BufferAttribute(buffers.vertices, 3));
958
+ this.lines.geometry.setAttribute('color', new BufferAttribute(buffers.colors, 4));
959
+ }
960
+ else {
961
+ if (this.lines) {
962
+ this.context?.scene.remove(this.lines);
963
+ }
964
+ }
965
+ }
966
+
967
+ public postStep() {
968
+ if (!this.world) return;
969
+ if (!this.enabled) return;
970
+ this._isUpdatingPhysicsWorld = true;
971
+ this.syncObjects();
972
+ this._isUpdatingPhysicsWorld = false;
973
+
974
+ if (this.eventQueue && !this.collisionHandler) {
975
+ this.collisionHandler = new PhysicsCollisionHandler(this.world, this.eventQueue);
976
+ }
977
+ if (this.collisionHandler) {
978
+ this.collisionHandler.handleCollisionEvents();
979
+ this.collisionHandler.update();
980
+ }
981
+ }
982
+
983
+ /** sync rendered objects with physics world (except for colliders without rigidbody) */
984
+ private syncObjects() {
985
+ if (debugColliderPlacement) return;
986
+ for (let i = 0; i < this.bodies.length; i++) {
987
+ const obj = this.objects[i];
988
+ const body = this.bodies[i] as Collider;
989
+
990
+ // if the collider is not attached to a rigidbody
991
+ // it means that its kinematic so we need to update its position
992
+ const col = (obj as ICollider);
993
+ if (col?.isCollider === true && !col.attachedRigidbody) {
994
+ const rigidbody = body.parent();
995
+ if (rigidbody)
996
+ this.syncPhysicsBody(obj.gameObject, rigidbody, true, true);
997
+ else
998
+ this.syncPhysicsBody(obj.gameObject, body, true, true);
999
+ continue;
1000
+ }
1001
+
1002
+
1003
+ // sync
1004
+ const pos = body.translation();
1005
+ const rot = body.rotation();
1006
+ if (Number.isNaN(pos.x) || Number.isNaN(rot.x)) {
1007
+ if (!col["__COLLIDER_NAN"] && isDevEnvironment()) {
1008
+ console.warn("Collider has NaN values", col.name, col.gameObject, body);
1009
+ col["__COLLIDER_NAN"] = true;
1010
+ }
1011
+ continue;
1012
+ }
1013
+
1014
+ // make sure to keep the collider offset
1015
+ const center = obj["center"] as Vector3;
1016
+ if (center && center.isVector3) {
1017
+ this._tempQuaternion.set(rot.x, rot.y, rot.z, rot.w);
1018
+ const offset = this._tempPosition.copy(center).applyQuaternion(this._tempQuaternion);
1019
+ // const scale = getWorldScale(obj.gameObject);
1020
+ // offset.multiply(scale);
1021
+ pos.x -= offset.x;
1022
+ pos.y -= offset.y;
1023
+ pos.z -= offset.z;
1024
+ }
1025
+ setWorldPositionXYZ(obj.gameObject, pos.x, pos.y, pos.z);
1026
+ setWorldQuaternionXYZW(obj.gameObject, rot.x, rot.y, rot.z, rot.w);
1027
+ }
1028
+ }
1029
+
1030
+ private syncPhysicsBody(obj: Object3D, body: RigidBody | Collider, translation: boolean, rotation: boolean) {
1031
+
1032
+ // const bodyType = body.bodyType();
1033
+ // const previous = physicsBody.translation();
1034
+ // const vel = physicsBody.linvel();
1035
+
1036
+ if (body instanceof RigidBody) {
1037
+ const worldPosition = getWorldPosition(obj, this._tempPosition);
1038
+ const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
1039
+ const type = body.bodyType();
1040
+ switch (type) {
1041
+ case RigidBodyType.Fixed:
1042
+ case RigidBodyType.KinematicPositionBased:
1043
+ case RigidBodyType.KinematicVelocityBased:
1044
+ if (translation)
1045
+ body.setNextKinematicTranslation(worldPosition);
1046
+ if (rotation)
1047
+ body.setNextKinematicRotation(worldQuaternion);
1048
+ break;
1049
+ default:
1050
+ if (translation)
1051
+ body.setTranslation(worldPosition, false);
1052
+ if (rotation)
1053
+ body.setRotation(worldQuaternion, false);
1054
+ break;
1055
+
1056
+ }
1057
+ body.wakeUp();
1058
+ }
1059
+ else if (body instanceof Collider) {
1060
+ const worldPosition = getWorldPosition(obj, this._tempPosition);
1061
+ const worldQuaternion = getWorldQuaternion(obj, this._tempQuaternion);
1062
+ const collider = body[$componentKey] as ICollider;
1063
+ this.tryApplyCenter(collider, worldPosition);
1064
+ if (translation)
1065
+ body.setTranslation(worldPosition);
1066
+ if (rotation)
1067
+ body.setRotation(worldQuaternion);
1068
+
1069
+ }
1070
+
1071
+ // physicsBody.setBodyType(RAPIER.RigidBodyType.Fixed);
1072
+ // physicsBody.setLinvel(vel, false);
1073
+
1074
+ // update velocity
1075
+ // const pos = physicsBody.translation();
1076
+ // pos.x -= previous.x;
1077
+ // pos.y -= previous.y;
1078
+ // pos.z -= previous.z;
1079
+ // // threhold
1080
+ // const t = 1;
1081
+ // const canUpdateVelocity = Math.abs(pos.x) < t && Math.abs(pos.y) < t && Math.abs(pos.z) < t;
1082
+ // if (canUpdateVelocity) {
1083
+ // const damping = 1 + this.context.time.deltaTime;
1084
+ // vel.x *= damping;
1085
+ // vel.y *= damping;
1086
+ // vel.z *= damping;
1087
+ // vel.x += pos.x;
1088
+ // vel.y += pos.y;
1089
+ // vel.z += pos.z;
1090
+ // console.log(vel);
1091
+ // physicsBody.setLinvel(vel, true);
1092
+ // }
1093
+ // else if(debugPhysics) console.warn("Movement exceeded threshold, not updating velocity", pos);
1094
+
1095
+ // body.setBodyType(bodyType);
1096
+ }
1097
+
1098
+ private readonly _tempCenterPos: Vector3 = new Vector3();
1099
+ private readonly _tempCenterVec:Vector3 = new Vector3();
1100
+ private readonly _tempCenterQuaternion:Quaternion = new Quaternion();
1101
+ private tryApplyCenter(collider: ICollider, targetVector: Vector3) {
1102
+ const center = collider.center;
1103
+ if (center && collider.gameObject) {
1104
+ if (center.x !== 0 || center.y !== 0 || center.z !== 0) {
1105
+ // TODO: fix export of center in editor integrations so we dont have to flip here
1106
+ this._tempCenterPos.x = -center.x;
1107
+ this._tempCenterPos.y = center.y;
1108
+ this._tempCenterPos.z = center.z;
1109
+ getWorldScale(collider.gameObject, this._tempCenterVec);
1110
+ this._tempCenterPos.multiply(this._tempCenterVec);
1111
+ const rot = getWorldQuaternion(collider.gameObject, this._tempCenterQuaternion);
1112
+ this._tempCenterPos.applyQuaternion(rot);
1113
+ targetVector.x += this._tempCenterPos.x;
1114
+ targetVector.y += this._tempCenterPos.y;
1115
+ targetVector.z += this._tempCenterPos.z;
1116
+ }
1117
+ }
1118
+ }
1119
+
1120
+ private static _matricesBuffer: Matrix4[] = [];
1121
+ private getRigidbodyRelativeMatrix(comp: Object3D, rigidbody: Object3D, mat: Matrix4, matrices?: Matrix4[]): Matrix4 {
1122
+ // collect all matrices to the rigidbody and then build the rigidbody relative matrix
1123
+ if (matrices === undefined) {
1124
+ matrices = RapierPhysics._matricesBuffer;
1125
+ matrices.length = 0;
1126
+ }
1127
+ if (comp === rigidbody) {
1128
+ const scale = getWorldScale(comp, this._tempPosition);
1129
+ mat.makeScale(scale.x, scale.y, scale.z);
1130
+ for (let i = matrices.length - 1; i >= 0; i--) {
1131
+ mat.multiply(matrices[i]);
1132
+ }
1133
+ return mat;
1134
+ }
1135
+ matrices.push(comp.matrix);
1136
+ if (comp.parent) {
1137
+ this.getRigidbodyRelativeMatrix(comp.parent, rigidbody, mat, matrices);
1138
+ }
1139
+ return mat;
1140
+ }
1141
+
1142
+ private static centerConnectionPos = { x: 0, y: 0, z: 0 };
1143
+ private static centerConnectionRot = { x: 0, y: 0, z: 0, w: 1 };
1144
+
1145
+
1146
+
1147
+ addFixedJoint(body1: IRigidbody, body2: IRigidbody) {
1148
+ if (!this.world) {
1149
+ console.error("Physics world not initialized");
1150
+ return;
1151
+ }
1152
+ const b1 = body1[$bodyKey] as RigidBody;
1153
+ const b2 = body2[$bodyKey] as RigidBody;
1154
+
1155
+ this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
1156
+ this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
1157
+
1158
+ const params = JointData.fixed(
1159
+ RapierPhysics.centerConnectionPos, RapierPhysics.centerConnectionRot,
1160
+ this._tempPosition, this._tempQuaternion,
1161
+ );
1162
+ const joint = this.world.createImpulseJoint(params, b1, b2, true);
1163
+ if (debugPhysics)
1164
+ console.log("ADD FIXED JOINT", joint)
1165
+ }
1166
+
1167
+
1168
+ /** The joint prevents any relative movement between two rigid-bodies, except for relative rotations along one axis. This is typically used to simulate wheels, fans, etc. They are characterized by one local anchor as well as one local axis on each rigid-body. */
1169
+ addHingeJoint(body1: IRigidbody, body2: IRigidbody, anchor: { x: number, y: number, z: number }, axis: { x: number, y: number, z: number }) {
1170
+ if (!this.world) {
1171
+ console.error("Physics world not initialized");
1172
+ return;
1173
+ }
1174
+ const b1 = body1[$bodyKey] as RigidBody;
1175
+ const b2 = body2[$bodyKey] as RigidBody;
1176
+
1177
+ this.calculateJointRelativeMatrices(body1.gameObject, body2.gameObject, this._tempMatrix);
1178
+ this._tempMatrix.decompose(this._tempPosition, this._tempQuaternion, this._tempScale);
1179
+
1180
+ const params = RAPIER.JointData.revolute(anchor, this._tempPosition, axis);
1181
+ const joint = this.world.createImpulseJoint(params, b1, b2, true);
1182
+ if (debugPhysics)
1183
+ console.log("ADD HINGE JOINT", joint)
1184
+ }
1185
+
1186
+
1187
+ private calculateJointRelativeMatrices(body1: IGameObject, body2: IGameObject, mat: Matrix4) {
1188
+ body1.updateWorldMatrix(true, false);
1189
+ body2.updateWorldMatrix(true, false);
1190
+ const world1 = body1.matrixWorld;
1191
+ const world2 = body2.matrixWorld;
1192
+ // set scale to 1
1193
+ world1.elements[0] = 1;
1194
+ world1.elements[5] = 1;
1195
+ world1.elements[10] = 1;
1196
+ world2.elements[0] = 1;
1197
+ world2.elements[5] = 1;
1198
+ world2.elements[10] = 1;
1199
+ mat.copy(world2).premultiply(world1.invert()).invert();
1200
+ }
1201
+ }
1202
+
1203
+
1204
+
1205
+ /** responsible of processing collision events for the component system */
1206
+ class PhysicsCollisionHandler {
1207
+
1208
+ readonly world: World;
1209
+ readonly eventQueue: EventQueue;
1210
+
1211
+ constructor(world: World, eventQueue: EventQueue) {
1212
+ this.world = world;
1213
+ this.eventQueue = eventQueue;
1214
+ }
1215
+
1216
+ private activeCollisions: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
1217
+ private activeCollisionsStay: Array<{ collider: ICollider, component: IComponent, collision: Collision }> = [];
1218
+ private activeTriggers: Array<{ collider: ICollider, component: IComponent, otherCollider: ICollider }> = [];
1219
+
1220
+ handleCollisionEvents() {
1221
+ if (!this.eventQueue) return;
1222
+ if (!this.world) return;
1223
+ this.eventQueue.drainCollisionEvents((handle1, handle2, started) => {
1224
+ const col1 = this.world!.getCollider(handle1);
1225
+ const col2 = this.world!.getCollider(handle2);
1226
+ const colliderComponent1 = col1[$componentKey];
1227
+ const colliderComponent2 = col2[$componentKey];
1228
+ if (debugCollisions)
1229
+ console.log("EVT", colliderComponent1.name, colliderComponent2.name, started, col1, col2);
1230
+ if (colliderComponent1 && colliderComponent2) {
1231
+ if (started) {
1232
+ this.onCollisionStarted(colliderComponent1, col1, colliderComponent2, col2);
1233
+ this.onCollisionStarted(colliderComponent2, col2, colliderComponent1, col1);
1234
+ }
1235
+ else {
1236
+ this.onCollisionEnded(colliderComponent1, colliderComponent2);
1237
+ this.onCollisionEnded(colliderComponent2, colliderComponent1);
1238
+ }
1239
+ }
1240
+ });
1241
+ }
1242
+
1243
+ update() {
1244
+ this.onHandleCollisionStay();
1245
+ }
1246
+
1247
+ private onCollisionStarted(self: ICollider, selfBody: Collider, other: ICollider, otherBody: Collider) {
1248
+ let collision: Collision | null = null;
1249
+
1250
+ // if one is a trigger we dont get collisions but want to raise the trigger events
1251
+ if (self.isTrigger || other.isTrigger) {
1252
+ foreachComponent(self.gameObject, (c: IComponent) => {
1253
+ if (c.onTriggerEnter && !c.destroyed) {
1254
+ c.onTriggerEnter(other);
1255
+ }
1256
+ this.activeTriggers.push({ collider: self, component: c, otherCollider: other });
1257
+ });
1258
+ }
1259
+ else {
1260
+ const object = self.gameObject;
1261
+ // TODO: we dont respect the flip value here!
1262
+ this.world.contactPair(selfBody, otherBody, (manifold, _flipped) => {
1263
+ foreachComponent(object, (c: IComponent) => {
1264
+ if (c.destroyed) return;
1265
+ const hasDeclaredEventMethod = c.onCollisionEnter || c.onCollisionStay || c.onCollisionExit;
1266
+ if (hasDeclaredEventMethod || debugCollisions) {
1267
+ if (!collision) {
1268
+ const contacts: Array<ContactPoint> = [];
1269
+ const normal = manifold.normal();
1270
+ for (let i = 0; i < manifold.numSolverContacts(); i++) {
1271
+ // solver points are in world space
1272
+ // https://rapier.rs/docs/user_guides/javascript/advanced_collision_detection_js#the-contact-graph
1273
+ const pt = manifold.solverContactPoint(i);
1274
+ const impulse = manifold.contactImpulse(i);
1275
+ if (pt) {
1276
+ const dist = manifold.contactDist(i);
1277
+ const friction = manifold.solverContactFriction(i);
1278
+ const tangentVelocity = manifold.solverContactTangentVelocity(i);
1279
+ const contact = new ContactPoint(pt, dist, normal, impulse, friction, tangentVelocity);
1280
+ contacts.push(contact);
1281
+ if (debugCollisions) {
1282
+ Gizmos.DrawDirection(pt, normal, 0xff0000, 3, true);
1283
+ }
1284
+ }
1285
+ }
1286
+ collision = new Collision(object, other, contacts);
1287
+ }
1288
+
1289
+ // we only need to keep track if any event exists
1290
+ if (hasDeclaredEventMethod) {
1291
+ const info = { collider: self, component: c, collision };
1292
+
1293
+ this.activeCollisions.push(info);
1294
+ if (c.onCollisionStay) {
1295
+ this.activeCollisionsStay.push(info);
1296
+ }
1297
+
1298
+ c.onCollisionEnter?.call(c, collision);
1299
+ }
1300
+
1301
+ }
1302
+ });
1303
+ });
1304
+ }
1305
+ }
1306
+
1307
+ private onHandleCollisionStay() {
1308
+ for (const active of this.activeCollisionsStay) {
1309
+ const c = active.component;
1310
+ if (c.destroyed) continue;
1311
+ if (c.activeAndEnabled && c.onCollisionStay) {
1312
+ if(active.collision.collider.destroyed) continue;
1313
+ const arg = active.collision;
1314
+ c.onCollisionStay(arg);
1315
+ }
1316
+ }
1317
+ for (const active of this.activeTriggers) {
1318
+ const c = active.component;
1319
+ if (c.destroyed) continue;
1320
+ if (c.activeAndEnabled && c.onTriggerStay) {
1321
+ const arg = active.otherCollider;
1322
+ if(arg.destroyed) continue;
1323
+ c.onTriggerStay(arg);
1324
+ }
1325
+ }
1326
+ }
1327
+
1328
+ private onCollisionEnded(self: ICollider, other: ICollider) {
1329
+ if (self.destroyed || other.destroyed) return;
1330
+ for (let i = 0; i < this.activeCollisions.length; i++) {
1331
+ const active = this.activeCollisions[i];
1332
+ const collider = active.collider;
1333
+ if (collider.destroyed || active.collision.collider.destroyed) {
1334
+ this.activeCollisions.splice(i, 1);
1335
+ i--;
1336
+ continue;
1337
+ }
1338
+ if (collider === self && active.collision.collider === other) {
1339
+ const c = active.component;
1340
+ this.activeCollisions.splice(i, 1);
1341
+ i--;
1342
+ if (c.activeAndEnabled && c.onCollisionExit) {
1343
+ const collision = active.collision;
1344
+ c.onCollisionExit(collision);
1345
+ }
1346
+ }
1347
+ }
1348
+ for (let i = 0; i < this.activeCollisionsStay.length; i++) {
1349
+ const active = this.activeCollisionsStay[i];
1350
+ const collider = active.collider;
1351
+ if (collider.destroyed || active.collision.collider.destroyed) {
1352
+ this.activeCollisionsStay.splice(i, 1);
1353
+ i--;
1354
+ continue;
1355
+ }
1356
+ if (collider === self && active.collision.collider === other) {
1357
+ const c = active.component;
1358
+ this.activeCollisionsStay.splice(i, 1);
1359
+ i--;
1360
+ if (c.activeAndEnabled && c.onCollisionExit) {
1361
+ const collision = active.collision;
1362
+ c.onCollisionExit(collision);
1363
+ }
1364
+ }
1365
+ }
1366
+ for (let i = 0; i < this.activeTriggers.length; i++) {
1367
+ const active = this.activeTriggers[i];
1368
+ const collider = active.collider;
1369
+ if (collider.destroyed || active.otherCollider.destroyed) {
1370
+ this.activeTriggers.splice(i, 1);
1371
+ i--;
1372
+ continue;
1373
+ }
1374
+ if (collider === self && active.otherCollider === other) {
1375
+ const c = active.component;
1376
+ this.activeTriggers.splice(i, 1);
1377
+ i--;
1378
+ if (c.activeAndEnabled && c.onTriggerExit) {
1379
+ const collision = active.otherCollider;
1380
+ c.onTriggerExit(collision);
1381
+ }
1382
+ }
1383
+ }
1384
+ }
1385
+ }