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