agentscape3d-engine 0.0.0-stage → 1.0.0-beta.3
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/LICENSE +21 -0
- package/README.md +19 -2
- package/cli.mjs +53 -0
- package/dist/ArticulationVerifier-BfGxXY8V.js +337 -0
- package/dist/ArticulationVerifier-BfGxXY8V.js.map +1 -0
- package/dist/AssetCompiler-BLj09iu1.js +2424 -0
- package/dist/AssetCompiler-BLj09iu1.js.map +1 -0
- package/dist/AssetModule-DME-XaFS.js +2637 -0
- package/dist/AssetModule-DME-XaFS.js.map +1 -0
- package/dist/AssetRecipe-g8dZBhgJ.js +298 -0
- package/dist/AssetRecipe-g8dZBhgJ.js.map +1 -0
- package/dist/BrowserCodeWorker.js +53856 -0
- package/dist/BrowserCodeWorker.js.map +1 -0
- package/dist/CarryCapability-CXwHU5ay.js +104 -0
- package/dist/CarryCapability-CXwHU5ay.js.map +1 -0
- package/dist/KinematicHypothesisScreen-C3TKiuYR.js +241 -0
- package/dist/KinematicHypothesisScreen-C3TKiuYR.js.map +1 -0
- package/dist/ObjectTransform-BlQUNK7c.js +61 -0
- package/dist/ObjectTransform-BlQUNK7c.js.map +1 -0
- package/dist/RapierPhysicsBackend-B2wQe_Vl.js +3259 -0
- package/dist/RapierPhysicsBackend-B2wQe_Vl.js.map +1 -0
- package/dist/RenderingSystem-WIr1ZuGu.js +1042 -0
- package/dist/RenderingSystem-WIr1ZuGu.js.map +1 -0
- package/dist/ScenePresentation-LsYiDxbm.js +152 -0
- package/dist/ScenePresentation-LsYiDxbm.js.map +1 -0
- package/dist/SceneProject-K7HH1s8o.js +2687 -0
- package/dist/SceneProject-K7HH1s8o.js.map +1 -0
- package/dist/SceneSerializer-zOzbX4jZ.js +932 -0
- package/dist/SceneSerializer-zOzbX4jZ.js.map +1 -0
- package/dist/SimulationProfile-DSYZbBLQ.js +165 -0
- package/dist/SimulationProfile-DSYZbBLQ.js.map +1 -0
- package/dist/StylePack-DcRa_kU0.js +88 -0
- package/dist/StylePack-DcRa_kU0.js.map +1 -0
- package/dist/WorldArchive-BiFGLt--.js +451 -0
- package/dist/WorldArchive-BiFGLt--.js.map +1 -0
- package/dist/WorldCommands-Debj7V4L.js +2207 -0
- package/dist/WorldCommands-Debj7V4L.js.map +1 -0
- package/dist/WorldIO-Ch8QBfKY.js +151 -0
- package/dist/WorldIO-Ch8QBfKY.js.map +1 -0
- package/dist/admission-CVEvsrdz.js +121 -0
- package/dist/admission-CVEvsrdz.js.map +1 -0
- package/dist/blankWorld-B0MW6g7q.js +75 -0
- package/dist/blankWorld-B0MW6g7q.js.map +1 -0
- package/dist/core.d.ts +68 -0
- package/dist/core.js +209 -0
- package/dist/core.js.map +1 -0
- package/dist/disposeObject3D-DIMzy0FA.js +128 -0
- package/dist/disposeObject3D-DIMzy0FA.js.map +1 -0
- package/dist/environments-HOe3sJXH.js +567 -0
- package/dist/environments-HOe3sJXH.js.map +1 -0
- package/dist/gltfIO-BzVKLyaL.js +339 -0
- package/dist/gltfIO-BzVKLyaL.js.map +1 -0
- package/dist/grandUrbanBlock-DRndRZjF.js +515 -0
- package/dist/grandUrbanBlock-DRndRZjF.js.map +1 -0
- package/dist/index.d.ts +345 -0
- package/dist/index.js +16952 -0
- package/dist/index.js.map +1 -0
- package/dist/magicCabin-CfBZjx2Q.js +11019 -0
- package/dist/magicCabin-CfBZjx2Q.js.map +1 -0
- package/dist/manifests-D0LwbJ36.js +866 -0
- package/dist/manifests-D0LwbJ36.js.map +1 -0
- package/dist/mcp.d.ts +2 -0
- package/dist/mcp.js +1 -0
- package/dist/monumentHall-awZiGtda.js +403 -0
- package/dist/monumentHall-awZiGtda.js.map +1 -0
- package/dist/node.d.ts +16 -0
- package/dist/node.js +26 -0
- package/dist/parts-DrEcfJRl.js +37 -0
- package/dist/parts-DrEcfJRl.js.map +1 -0
- package/dist/rolldown-runtime-D3KUYPsf.js +12 -0
- package/dist/ruinedCourtyard-DuF7NmUU.js +696 -0
- package/dist/ruinedCourtyard-DuF7NmUU.js.map +1 -0
- package/dist/runtime/application/AgentTools.js +28 -0
- package/dist/runtime/application/README.md +14 -0
- package/dist/runtime/application/agent/ProgressiveAgentTools.js +92 -0
- package/dist/runtime/application/agent/StudioMcpAllowlist.js +10 -0
- package/dist/runtime/application/agent/StudioProjectTools.js +18 -0
- package/dist/runtime/application/agent/TaskEvidence.js +199 -0
- package/dist/runtime/application/buildTaskObservation.js +117 -0
- package/dist/runtime/application/code-execution/AuthoringSandboxEntry.js +50 -0
- package/dist/runtime/application/code-execution/BrowserCodeExecutor.js +35 -0
- package/dist/runtime/application/code-execution/BrowserCodeWorker.js +17 -0
- package/dist/runtime/application/code-execution/RemoteAuthoringDocument.js +82 -0
- package/dist/runtime/application/createSession.js +66 -0
- package/dist/runtime/application/createWorldAuthoringContext.js +400 -0
- package/dist/runtime/application/generation/GenerationOrchestrator.js +630 -0
- package/dist/runtime/application/generation/GenerationRuntime.js +215 -0
- package/dist/runtime/application/generation/PromptHybridWorldOrchestrator.js +289 -0
- package/dist/runtime/application/native-scene/CodeProject.js +215 -0
- package/dist/runtime/application/native-scene/CodeProjectModules.js +143 -0
- package/dist/runtime/application/native-scene/ExampleWorldCatalog.js +35 -0
- package/dist/runtime/application/native-scene/NativeArticulation.js +53 -0
- package/dist/runtime/application/native-scene/NativeEditableRegistry.js +150 -0
- package/dist/runtime/application/native-scene/NativeEnvironmentGeometry.js +85 -0
- package/dist/runtime/application/native-scene/NativeLightingInspection.js +33 -0
- package/dist/runtime/application/native-scene/NativeMaterialInspection.js +52 -0
- package/dist/runtime/application/native-scene/NativeOverlayCheckpoint.js +127 -0
- package/dist/runtime/application/native-scene/NativePhysicsGeometry.js +64 -0
- package/dist/runtime/application/native-scene/NativeRenderDiscovery.js +51 -0
- package/dist/runtime/application/native-scene/NativeSceneHost.js +406 -0
- package/dist/runtime/application/native-scene/NativeSceneInspection.js +203 -0
- package/dist/runtime/application/native-scene/NativeSceneOverlay.js +116 -0
- package/dist/runtime/application/native-scene/NativeSceneQuery.js +123 -0
- package/dist/runtime/application/native-scene/NativeSceneReview.js +132 -0
- package/dist/runtime/application/native-scene/NativeThreeModule.js +16 -0
- package/dist/runtime/application/native-scene/SceneObserver.js +97 -0
- package/dist/runtime/application/native-scene/VisualCapturePolicy.js +4 -0
- package/dist/runtime/application/object-forge/SceneObjectForge.js +263 -0
- package/dist/runtime/application/programmatic-assets/ProgrammaticAssetBuilder.js +59 -0
- package/dist/runtime/application/programmatic-assets/ProgrammaticAssetContract.js +22 -0
- package/dist/runtime/application/programmatic-assets/ThreeCodeAsset.js +40 -0
- package/dist/runtime/application/scene/CapabilityResolver.js +90 -0
- package/dist/runtime/application/scene/NativeEditableContracts.js +18 -0
- package/dist/runtime/application/scene/RequestWindow.js +54 -0
- package/dist/runtime/application/scene/SceneAgentTools.js +21 -0
- package/dist/runtime/application/scene/SceneContracts.js +49 -0
- package/dist/runtime/application/scene/SceneEditService.js +154 -0
- package/dist/runtime/application/scene/SceneFacade.js +356 -0
- package/dist/runtime/application/scene/SceneInteractionAdapters.js +41 -0
- package/dist/runtime/application/scene/SceneJobController.js +320 -0
- package/dist/runtime/application/scene/SceneLifecycle.js +57 -0
- package/dist/runtime/application/scene/ScenePackage.js +173 -0
- package/dist/runtime/application/scene/ScenePersistence.js +50 -0
- package/dist/runtime/application/scene/SceneProject.js +95 -0
- package/dist/runtime/application/scene/SceneSkillNames.js +5 -0
- package/dist/runtime/application/scene/StarterScene.js +56 -0
- package/dist/runtime/application/scene/WorldArchive.js +89 -0
- package/dist/runtime/application/scene/WorldDirectory.js +20 -0
- package/dist/runtime/application/scene/WorldDocument.js +76 -0
- package/dist/runtime/application/scene/WorldIO.js +103 -0
- package/dist/runtime/application/scene/WorldStorage.js +155 -0
- package/dist/runtime/application/skills/SkillRegistry.js +193 -0
- package/dist/runtime/application/skills/packs/acceptanceSkills.js +32 -0
- package/dist/runtime/application/skills/packs/affordanceSkills.js +36 -0
- package/dist/runtime/application/skills/packs/assetSkills.js +46 -0
- package/dist/runtime/application/skills/packs/authoringSkills.js +16 -0
- package/dist/runtime/application/skills/packs/batchSkills.js +22 -0
- package/dist/runtime/application/skills/packs/codeSkills.js +106 -0
- package/dist/runtime/application/skills/packs/generationSkills.js +45 -0
- package/dist/runtime/application/skills/packs/interactionSkills.js +46 -0
- package/dist/runtime/application/skills/packs/recoverySkills.js +80 -0
- package/dist/runtime/application/skills/packs/sceneSkills.js +24 -0
- package/dist/runtime/application/skills/packs/spatialSkills.js +35 -0
- package/dist/runtime/application/skills/packs/verificationSkills.js +14 -0
- package/dist/runtime/application/skills/packs/worldSkills.js +45 -0
- package/dist/runtime/application/skills/registerCoreSkills.js +31 -0
- package/dist/runtime/application/skills/skillPrimitives.js +4 -0
- package/dist/runtime/application/tasks/CabinetCupTask.js +76 -0
- package/dist/runtime/application/tasks.jsonl +67 -0
- package/dist/runtime/application/world-authoring/AuthoringAssetProducer.js +42 -0
- package/dist/runtime/application/world-authoring/AuthoringDiff.js +149 -0
- package/dist/runtime/application/world-authoring/AuthoringDocument.js +825 -0
- package/dist/runtime/application/world-authoring/AuthoringExecutionHost.js +109 -0
- package/dist/runtime/application/world-authoring/AuthoringIdentity.js +17 -0
- package/dist/runtime/application/world-authoring/AuthoringModelResolver.js +29 -0
- package/dist/runtime/application/world-authoring/AuthoringPatch.js +118 -0
- package/dist/runtime/application/world-authoring/AuthoringPromotionController.js +334 -0
- package/dist/runtime/application/world-authoring/AuthoringPromotionSource.js +126 -0
- package/dist/runtime/application/world-authoring/AuthoringRevisionHistory.js +144 -0
- package/dist/runtime/application/world-authoring/HeadlessImageExport.js +92 -0
- package/dist/runtime/application/world-authoring/ModelRef.js +62 -0
- package/dist/runtime/application/world-authoring/SceneGLBExport.js +88 -0
- package/dist/runtime/apps/mcp-server/AUTHORING.md +71 -0
- package/dist/runtime/apps/mcp-server/AgentMcpCompat.js +68 -0
- package/dist/runtime/apps/mcp-server/AuthoringSandboxEntry.js +1 -0
- package/dist/runtime/apps/mcp-server/AuthoringSandboxWorker.mjs +30 -0
- package/dist/runtime/apps/mcp-server/BuildEnvironment.js +17 -0
- package/dist/runtime/apps/mcp-server/CodeExecutor.js +71 -0
- package/dist/runtime/apps/mcp-server/DEPLOYMENT-PC2.md +42 -0
- package/dist/runtime/apps/mcp-server/DockerBuildRunner.js +66 -0
- package/dist/runtime/apps/mcp-server/FileAssetRepository.js +159 -0
- package/dist/runtime/apps/mcp-server/HeadlessAssetIO.js +8 -0
- package/dist/runtime/apps/mcp-server/HeadlessGLTFLoader.js +47 -0
- package/dist/runtime/apps/mcp-server/HeadlessWorldHost.js +157 -0
- package/dist/runtime/apps/mcp-server/IsolatedAuthoringExecutor.js +6 -0
- package/dist/runtime/apps/mcp-server/PROGRAMMATIC-ASSETS.md +116 -0
- package/dist/runtime/apps/mcp-server/ProgrammaticAssets.js +60 -0
- package/dist/runtime/apps/mcp-server/README.md +269 -0
- package/dist/runtime/apps/mcp-server/RemoteAuthoringDocument.js +1 -0
- package/dist/runtime/apps/mcp-server/SandboxBundle.js +13 -0
- package/dist/runtime/apps/mcp-server/SessionManager.js +148 -0
- package/dist/runtime/apps/mcp-server/StudioBridgeHttp.js +75 -0
- package/dist/runtime/apps/mcp-server/StudioLiveBridge.js +246 -0
- package/dist/runtime/apps/mcp-server/StudioProjectToolContracts.js +58 -0
- package/dist/runtime/apps/mcp-server/TransferHttp.js +47 -0
- package/dist/runtime/apps/mcp-server/TransferLimits.js +6 -0
- package/dist/runtime/apps/mcp-server/TransferStore.js +134 -0
- package/dist/runtime/apps/mcp-server/TrustedAuthoringWorker.mjs +16 -0
- package/dist/runtime/apps/mcp-server/VALIDATION.md +45 -0
- package/dist/runtime/apps/mcp-server/acceptance-evidence.json +332 -0
- package/dist/runtime/apps/mcp-server/errorDiagnostics.js +32 -0
- package/dist/runtime/apps/mcp-server/examples/code-house.js +24 -0
- package/dist/runtime/apps/mcp-server/examples/procedural-assets.js +85 -0
- package/dist/runtime/apps/mcp-server/main.mjs +82 -0
- package/dist/runtime/apps/mcp-server/registerTools.js +274 -0
- package/dist/runtime/apps/mcp-server/sandbox/run.mjs +23 -0
- package/dist/runtime/apps/mcp-server/security.js +23 -0
- package/dist/runtime/apps/mcp-server/tasks.jsonl +9 -0
- package/dist/runtime/apps/server/CapabilityAdapterRegistry.js +157 -0
- package/dist/runtime/apps/server/README.md +7 -0
- package/dist/runtime/apps/server/WorldRepository.js +338 -0
- package/dist/runtime/apps/server/WorldRepositoryHttp.js +96 -0
- package/dist/runtime/apps/server/openai-compatible-agent-gateway.mjs +278 -0
- package/dist/runtime/apps/server/tasks.jsonl +10 -0
- package/dist/runtime/apps/shared/node/WorldDiskStorage.js +62 -0
- package/dist/runtime/foundation/EventBus.js +26 -0
- package/dist/runtime/foundation/JsonGateway.js +47 -0
- package/dist/runtime/foundation/PngRgba.js +99 -0
- package/dist/runtime/foundation/PolicyEngine.js +21 -0
- package/dist/runtime/foundation/README.md +7 -0
- package/dist/runtime/foundation/TraceRecorder.js +76 -0
- package/dist/runtime/foundation/errors.js +8 -0
- package/dist/runtime/foundation/simulation/SimulationProfile.js +143 -0
- package/dist/runtime/foundation/version.js +2 -0
- package/dist/runtime/modules/agent/README.md +11 -0
- package/dist/runtime/modules/agent/ToolCallingAgent.js +19 -0
- package/dist/runtime/modules/agent/bootstrapWorld.js +10 -0
- package/dist/runtime/modules/agent/buildRecoveryProposals.js +533 -0
- package/dist/runtime/modules/agent/execution/AgentExecutionRules.js +128 -0
- package/dist/runtime/modules/agent/execution/AgentRunResults.js +55 -0
- package/dist/runtime/modules/agent/execution/AgentRunSession.js +393 -0
- package/dist/runtime/modules/agent/execution/AgentToolInvoker.js +40 -0
- package/dist/runtime/modules/agent/gateway/HttpLLMGateway.js +19 -0
- package/dist/runtime/modules/agent/prompt/index.js +52 -0
- package/dist/runtime/modules/agent/tasks.jsonl +94 -0
- package/dist/runtime/modules/artifact/ArtifactContentGate.js +144 -0
- package/dist/runtime/modules/artifact/ArtifactDescriptor.js +280 -0
- package/dist/runtime/modules/artifact/ArtifactImporter.js +185 -0
- package/dist/runtime/modules/artifact/ArtifactModule.js +111 -0
- package/dist/runtime/modules/artifact/ArtifactRegistry.js +197 -0
- package/dist/runtime/modules/artifact/IncrementalSha256.js +129 -0
- package/dist/runtime/modules/artifact/MemoryArtifactByteStore.js +115 -0
- package/dist/runtime/modules/artifact/README.md +7 -0
- package/dist/runtime/modules/artifact/storage/IndexedDbArtifactStore.js +67 -0
- package/dist/runtime/modules/artifact/tasks.jsonl +31 -0
- package/dist/runtime/modules/asset/AssetModule.js +416 -0
- package/dist/runtime/modules/asset/README.md +67 -0
- package/dist/runtime/modules/asset/loading/AssetLoader.js +157 -0
- package/dist/runtime/modules/asset/loading/GltfAssetLoader.js +61 -0
- package/dist/runtime/modules/asset/loading/builtins/BuiltinAssets.js +95 -0
- package/dist/runtime/modules/asset/loading/builtins/HouseDoor.js +33 -0
- package/dist/runtime/modules/asset/loading/builtins/IndustrialCabinet.js +103 -0
- package/dist/runtime/modules/asset/model/AssetInputContracts.js +139 -0
- package/dist/runtime/modules/asset/model/AssetRef.js +12 -0
- package/dist/runtime/modules/asset/model/MechanicalAssetDraft.js +172 -0
- package/dist/runtime/modules/asset/model/ProceduralTextureContract.js +24 -0
- package/dist/runtime/modules/asset/model/admission.js +117 -0
- package/dist/runtime/modules/asset/model/errors.js +6 -0
- package/dist/runtime/modules/asset/model/parts.js +27 -0
- package/dist/runtime/modules/asset/model/resourceBudget.js +10 -0
- package/dist/runtime/modules/asset/model/schema.js +150 -0
- package/dist/runtime/modules/asset/persistence/AssetManifestStore.js +78 -0
- package/dist/runtime/modules/asset/persistence/CompiledAssetStore.js +84 -0
- package/dist/runtime/modules/asset/persistence/LocalAssetLibrary.js +86 -0
- package/dist/runtime/modules/asset/persistence/LocalAssetLibraryStore.js +81 -0
- package/dist/runtime/modules/asset/production/AssetProductionPipeline.js +272 -0
- package/dist/runtime/modules/asset/production/AssetRecipe.js +91 -0
- package/dist/runtime/modules/asset/production/ProceduralTexture.js +85 -0
- package/dist/runtime/modules/asset/production/adapters/EmbodiedGenAdapter.js +45 -0
- package/dist/runtime/modules/asset/production/adapters/EmbodiedGenBundleAdapter.js +590 -0
- package/dist/runtime/modules/asset/production/compiler/AssetCompiler.js +173 -0
- package/dist/runtime/modules/asset/production/compiler/evidence/AnimationMotionEvidence.js +124 -0
- package/dist/runtime/modules/asset/production/compiler/evidence/StaticPanelEvidence.js +56 -0
- package/dist/runtime/modules/asset/production/compiler/gltfIO.js +6 -0
- package/dist/runtime/modules/asset/production/compiler/partGeometry.js +74 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ArticulatedCollisionPass.js +63 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ArticulationCandidatePass.js +48 -0
- package/dist/runtime/modules/asset/production/compiler/passes/AuthoringIntentPass.js +16 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ColliderFallbackPass.js +30 -0
- package/dist/runtime/modules/asset/production/compiler/passes/CompileQualityPass.js +75 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ExplicitDefinitionPass.js +20 -0
- package/dist/runtime/modules/asset/production/compiler/passes/GLTFInspectPass.js +34 -0
- package/dist/runtime/modules/asset/production/compiler/passes/GeometryPass.js +32 -0
- package/dist/runtime/modules/asset/production/compiler/passes/JointFramePass.js +116 -0
- package/dist/runtime/modules/asset/production/compiler/passes/KinematicEvidencePass.js +84 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ManifestPass.js +63 -0
- package/dist/runtime/modules/asset/production/compiler/passes/NormalizeTransformPass.js +16 -0
- package/dist/runtime/modules/asset/production/compiler/passes/OptimizeGLBPass.js +23 -0
- package/dist/runtime/modules/asset/production/compiler/passes/PartColliderPass.js +50 -0
- package/dist/runtime/modules/asset/production/compiler/passes/PartGeometryEnrichmentPass.js +60 -0
- package/dist/runtime/modules/asset/production/compiler/passes/PartProposalPass.js +93 -0
- package/dist/runtime/modules/asset/production/compiler/passes/RemoteEnrichmentPass.js +32 -0
- package/dist/runtime/modules/asset/production/compiler/passes/ResourceBudgetPass.js +55 -0
- package/dist/runtime/modules/asset/production/compiler/passes/SegmentMaterializePass.js +203 -0
- package/dist/runtime/modules/asset/production/compiler/passes/SegmentationEvidencePass.js +43 -0
- package/dist/runtime/modules/asset/production/compiler/passes/SemanticHeuristicPass.js +33 -0
- package/dist/runtime/modules/asset/production/compiler/passes/StructurePass.js +60 -0
- package/dist/runtime/modules/asset/production/compiler/providers/HttpCompilerProvider.js +23 -0
- package/dist/runtime/modules/asset/production/verification/KinematicHypothesisScreen.js +198 -0
- package/dist/runtime/modules/asset/registry/AssetCatalog.js +72 -0
- package/dist/runtime/modules/asset/registry/AssetRegistry.js +51 -0
- package/dist/runtime/modules/asset/registry/manifests/index.js +173 -0
- package/dist/runtime/modules/asset/tasks.jsonl +87 -0
- package/dist/runtime/modules/generation/README.md +9 -0
- package/dist/runtime/modules/generation/connector/ConnectorArtifactClient.js +45 -0
- package/dist/runtime/modules/generation/connector/ConnectorCapabilityAdapter.js +169 -0
- package/dist/runtime/modules/generation/connector/ConnectorClient.js +192 -0
- package/dist/runtime/modules/generation/connector/ConnectorJobClient.js +167 -0
- package/dist/runtime/modules/generation/connector/ConnectorJobEventClient.js +179 -0
- package/dist/runtime/modules/generation/connector/ConnectorSession.js +202 -0
- package/dist/runtime/modules/generation/jobs/GenerationJobProjection.js +239 -0
- package/dist/runtime/modules/generation/jobs/GenerationJobReconciler.js +99 -0
- package/dist/runtime/modules/generation/jobs/GenerationJobStore.js +91 -0
- package/dist/runtime/modules/generation/jobs/GenerationJobTransportOverlay.js +50 -0
- package/dist/runtime/modules/generation/providers/ProviderRegistry.js +304 -0
- package/dist/runtime/modules/generation/tasks.jsonl +35 -0
- package/dist/runtime/modules/navigation/NavigationBackend.js +17 -0
- package/dist/runtime/modules/navigation/RecastNavigationBackend.js +263 -0
- package/dist/runtime/modules/physics/JoltPhysicsBackend.js +850 -0
- package/dist/runtime/modules/physics/PhysicsApiSurface.js +660 -0
- package/dist/runtime/modules/physics/PhysicsBackend.js +88 -0
- package/dist/runtime/modules/physics/RapierPhysicsBackend.js +414 -0
- package/dist/runtime/modules/rendering/README.md +7 -0
- package/dist/runtime/modules/rendering/RendererProbe.js +153 -0
- package/dist/runtime/modules/rendering/WebGpuPostFxPipeline.js +140 -0
- package/dist/runtime/modules/rendering/captureStill.js +54 -0
- package/dist/runtime/modules/rendering/createRenderer.js +76 -0
- package/dist/runtime/modules/rendering/disposeObject3D.js +37 -0
- package/dist/runtime/modules/rendering/loadGaussianSplatVisual.js +74 -0
- package/dist/runtime/modules/rendering/spatial/ThreeBvhRuntime.js +107 -0
- package/dist/runtime/modules/rendering/style/StylePack.js +34 -0
- package/dist/runtime/modules/rendering/style/StyleSystem.js +173 -0
- package/dist/runtime/modules/rendering/tasks.jsonl +7 -0
- package/dist/runtime/modules/world/README.md +13 -0
- package/dist/runtime/modules/world/build/WorldBuilder.js +170 -0
- package/dist/runtime/modules/world/compiler/PipelineEngine.js +32 -0
- package/dist/runtime/modules/world/compiler/WorldAdmission.js +43 -0
- package/dist/runtime/modules/world/compiler/WorldBehaviorCompiler.js +68 -0
- package/dist/runtime/modules/world/compiler/WorldCompilation.js +157 -0
- package/dist/runtime/modules/world/compiler/WorldComposer.js +201 -0
- package/dist/runtime/modules/world/compiler/WorldPhysicsAdmission.js +76 -0
- package/dist/runtime/modules/world/compiler/WorldRecompiler.js +356 -0
- package/dist/runtime/modules/world/compiler/WorldRetry.js +75 -0
- package/dist/runtime/modules/world/compiler/createWorldPipeline.js +379 -0
- package/dist/runtime/modules/world/content/blankWorld.js +35 -0
- package/dist/runtime/modules/world/content/environments.js +84 -0
- package/dist/runtime/modules/world/content/grandUrbanBlock.js +189 -0
- package/dist/runtime/modules/world/content/magic-cabin/README.md +28 -0
- package/dist/runtime/modules/world/content/magic-cabin/affordances.js +64 -0
- package/dist/runtime/modules/world/content/magic-cabin/cabinContents.js +7078 -0
- package/dist/runtime/modules/world/content/magic-cabin/lighting.js +26 -0
- package/dist/runtime/modules/world/content/magic-cabin/materials.js +20 -0
- package/dist/runtime/modules/world/content/magicCabin.js +190 -0
- package/dist/runtime/modules/world/content/monumentHall.js +154 -0
- package/dist/runtime/modules/world/content/ruinedCourtyard.js +196 -0
- package/dist/runtime/modules/world/content/suzhouGarden/README.md +141 -0
- package/dist/runtime/modules/world/content/suzhouGarden/index.js +137 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/arch.js +491 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/buildings.js +670 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/core.js +578 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/lod.js +127 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/main.js +642 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/nature.js +650 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/rockery.js +280 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/sky.js +242 -0
- package/dist/runtime/modules/world/content/suzhouGarden/src/water.js +215 -0
- package/dist/runtime/modules/world/content/woodlandWorkshop.js +192 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/README.md +36 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/arch.js +351 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/buildings.js +490 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/core.js +505 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/index.js +101 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/nature.js +435 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/sky.js +169 -0
- package/dist/runtime/modules/world/content/yuanmingyuan/water.js +245 -0
- package/dist/runtime/modules/world/errors.js +9 -0
- package/dist/runtime/modules/world/generated/GeneratedWorldCoordinates.js +50 -0
- package/dist/runtime/modules/world/generated/GeneratedWorldLoader.js +297 -0
- package/dist/runtime/modules/world/runtime/CommandHistory.js +111 -0
- package/dist/runtime/modules/world/runtime/EntityCapabilityRegistry.js +165 -0
- package/dist/runtime/modules/world/runtime/NativeNavigationGeometry.js +84 -0
- package/dist/runtime/modules/world/runtime/NativePhysicsSnapshot.js +245 -0
- package/dist/runtime/modules/world/runtime/ObjectStore.js +31 -0
- package/dist/runtime/modules/world/runtime/ObjectTransform.js +62 -0
- package/dist/runtime/modules/world/runtime/ObservedEntity.js +37 -0
- package/dist/runtime/modules/world/runtime/ProjectCapabilities.js +240 -0
- package/dist/runtime/modules/world/runtime/SceneExtensions.js +36 -0
- package/dist/runtime/modules/world/runtime/ScenePresentation.js +101 -0
- package/dist/runtime/modules/world/runtime/SceneSerializer.js +281 -0
- package/dist/runtime/modules/world/runtime/WorldAuthority.js +41 -0
- package/dist/runtime/modules/world/runtime/WorldCollisionSignature.js +14 -0
- package/dist/runtime/modules/world/runtime/WorldCommands.js +503 -0
- package/dist/runtime/modules/world/runtime/WorldObservation.js +71 -0
- package/dist/runtime/modules/world/runtime/WorldQueries.js +340 -0
- package/dist/runtime/modules/world/runtime/WorldRecovery.js +78 -0
- package/dist/runtime/modules/world/runtime/WorldRuntime.js +806 -0
- package/dist/runtime/modules/world/runtime/affordance/ObjectAffordances.js +110 -0
- package/dist/runtime/modules/world/runtime/affordance/WorldAffordances.js +130 -0
- package/dist/runtime/modules/world/runtime/behavior/BehaviorCompiler.js +79 -0
- package/dist/runtime/modules/world/runtime/behavior/ProjectStateMachine.js +195 -0
- package/dist/runtime/modules/world/runtime/behavior/RuleGraph.js +70 -0
- package/dist/runtime/modules/world/runtime/behavior/RuleRuntime.js +115 -0
- package/dist/runtime/modules/world/runtime/graph/SceneGraph.js +390 -0
- package/dist/runtime/modules/world/runtime/interaction/ArticulationRuntime.js +214 -0
- package/dist/runtime/modules/world/runtime/interaction/CarryCapability.js +65 -0
- package/dist/runtime/modules/world/runtime/interaction/CarryRuntime.js +244 -0
- package/dist/runtime/modules/world/runtime/interaction/InteractionApproach.js +147 -0
- package/dist/runtime/modules/world/runtime/interaction/InteractionContract.js +57 -0
- package/dist/runtime/modules/world/runtime/interaction/ManipulationActions.js +7 -0
- package/dist/runtime/modules/world/runtime/interaction/NativePhysicalTarget.js +31 -0
- package/dist/runtime/modules/world/runtime/interaction/RecoveryRuntime.js +123 -0
- package/dist/runtime/modules/world/runtime/interaction/SettleRuntime.js +134 -0
- package/dist/runtime/modules/world/runtime/semantic/NativePhysicsState.js +89 -0
- package/dist/runtime/modules/world/runtime/semantic/SemanticWorldView.js +129 -0
- package/dist/runtime/modules/world/runtime/simulation/SimulationClock.js +89 -0
- package/dist/runtime/modules/world/runtime/simulation/SimulationSession.js +104 -0
- package/dist/runtime/modules/world/runtime/simulation/ThermalSystem.js +269 -0
- package/dist/runtime/modules/world/runtime/simulation/WorldTimeline.js +110 -0
- package/dist/runtime/modules/world/runtime/spatial/SpatialSnapshot.js +56 -0
- package/dist/runtime/modules/world/runtime/spatial/VisualMeshBounds.js +41 -0
- package/dist/runtime/modules/world/runtime/systems/AudioSystem.js +306 -0
- package/dist/runtime/modules/world/runtime/systems/Instrument.js +39 -0
- package/dist/runtime/modules/world/runtime/systems/InteractionSystem.js +806 -0
- package/dist/runtime/modules/world/runtime/systems/LocomotionSystem.js +281 -0
- package/dist/runtime/modules/world/runtime/systems/NavigationSystem.js +561 -0
- package/dist/runtime/modules/world/runtime/systems/PhysicsSystem.js +1752 -0
- package/dist/runtime/modules/world/runtime/systems/RenderingSystem.js +527 -0
- package/dist/runtime/modules/world/runtime/systems/SpatialSystem.js +402 -0
- package/dist/runtime/modules/world/spec/WorldIR.js +165 -0
- package/dist/runtime/modules/world/spec/WorldIRToolSchema.js +86 -0
- package/dist/runtime/modules/world/spec/WorldPlannerProposal.js +51 -0
- package/dist/runtime/modules/world/spec/WorldRevision.js +231 -0
- package/dist/runtime/modules/world/spec/WorldSpec.js +86 -0
- package/dist/runtime/modules/world/tasks.jsonl +530 -0
- package/dist/runtime/modules/world/verification/ArticulationVerifier.js +254 -0
- package/dist/runtime/modules/world/verification/Finding.js +105 -0
- package/dist/runtime/modules/world/verification/InteractionEvidence.js +73 -0
- package/dist/runtime/modules/world/verification/RepairEngine.js +49 -0
- package/dist/runtime/modules/world/verification/VisualLayoutAcceptance.js +58 -0
- package/dist/runtime/modules/world/verification/WorldAcceptance.js +176 -0
- package/dist/runtime/modules/world/verification/WorldValidator.js +67 -0
- package/dist/runtime/package-lock.json +16071 -0
- package/dist/runtime/package.json +106 -0
- package/dist/runtime/public/assets/cabinet.glb +0 -0
- package/dist/runtime/public/assets/grand-urban-block/clean_asphalt_diff_1k.jpg +0 -0
- package/dist/runtime/public/assets/grand-urban-block/clean_asphalt_nor_gl_1k.jpg +0 -0
- package/dist/runtime/public/assets/grand-urban-block/concrete_pavement_diff_1k.jpg +0 -0
- package/dist/runtime/public/assets/grand-urban-block/urban_street_01_1k.hdr +0 -0
- package/dist/runtime/public/assets/monument-hall/marble_diff_1k.jpg +0 -0
- package/dist/runtime/public/assets/monument-hall/marble_nor_gl_1k.jpg +0 -0
- package/dist/runtime/public/assets/monument-hall/marble_rough_1k.jpg +0 -0
- package/dist/runtime/public/assets/monument-hall/solitude_interior_1k.hdr +0 -0
- package/dist/runtime/public/assets/ruined-courtyard/courtyard_1k.hdr +0 -0
- package/dist/runtime/public/assets/ruined-courtyard/mossy_cobblestone_diff_1k.jpg +0 -0
- package/dist/runtime/public/assets/ruined-courtyard/mossy_cobblestone_nor_gl_1k.jpg +0 -0
- package/dist/runtime/public/assets/ruined-courtyard/mossy_sandstone_diff_1k.jpg +0 -0
- package/dist/schema-DT7K9NjP.js +165 -0
- package/dist/schema-DT7K9NjP.js.map +1 -0
- package/dist/suzhouGarden-Y3hny8j6.js +6150 -0
- package/dist/suzhouGarden-Y3hny8j6.js.map +1 -0
- package/dist/version-CHc-i8ch.js +5 -0
- package/dist/version-CHc-i8ch.js.map +1 -0
- package/dist/woodlandWorkshop-DsScU-cx.js +483 -0
- package/dist/woodlandWorkshop-DsScU-cx.js.map +1 -0
- package/dist/yuanmingyuan-DsFnPzzR.js +4955 -0
- package/dist/yuanmingyuan-DsFnPzzR.js.map +1 -0
- package/package.json +75 -4
|
@@ -0,0 +1,3259 @@
|
|
|
1
|
+
import { n as orderParts, r as AgentScapeError, t as ROOT_PART } from "./parts-DrEcfJRl.js";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import RAPIER from "@dimforge/rapier3d-compat";
|
|
4
|
+
// modules/world/errors.js
|
|
5
|
+
const Errors = {
|
|
6
|
+
objectNotFound: (id) => new AgentScapeError("OBJECT_NOT_FOUND", "Object not found: " + id, { id }),
|
|
7
|
+
actionUnsupported: (id, action) => new AgentScapeError("ACTION_UNSUPPORTED", id + " does not support " + action, {
|
|
8
|
+
id,
|
|
9
|
+
action
|
|
10
|
+
}),
|
|
11
|
+
interactionUnavailable: (actorId, targetId, reason, details = {}) => new AgentScapeError("INTERACTION_UNAVAILABLE", actorId + " cannot interact with " + targetId + ": " + reason, {
|
|
12
|
+
actorId,
|
|
13
|
+
targetId,
|
|
14
|
+
reason,
|
|
15
|
+
...details
|
|
16
|
+
}),
|
|
17
|
+
carryUnavailable: (actorId, targetId, reason, details = {}) => new AgentScapeError("CARRY_UNAVAILABLE", actorId + " cannot carry " + targetId + ": " + reason, {
|
|
18
|
+
actorId,
|
|
19
|
+
targetId,
|
|
20
|
+
reason,
|
|
21
|
+
...details
|
|
22
|
+
}),
|
|
23
|
+
placeUnavailable: (actorId, targetId, reason, details = {}) => new AgentScapeError("PLACE_UNAVAILABLE", actorId + " cannot place on " + targetId + ": " + reason, {
|
|
24
|
+
actorId,
|
|
25
|
+
targetId,
|
|
26
|
+
reason,
|
|
27
|
+
...details
|
|
28
|
+
})
|
|
29
|
+
};
|
|
30
|
+
// modules/world/runtime/ObjectStore.js
|
|
31
|
+
var ObjectStore = class {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.items = /* @__PURE__ */ new Map();
|
|
34
|
+
}
|
|
35
|
+
get size() {
|
|
36
|
+
return this.items.size;
|
|
37
|
+
}
|
|
38
|
+
add(id, value) {
|
|
39
|
+
if (this.items.has(id)) throw new Error(`Duplicate object id: ${id}`);
|
|
40
|
+
this.items.set(id, value);
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
get(id) {
|
|
44
|
+
const value = this.items.get(id);
|
|
45
|
+
if (!value) throw Errors.objectNotFound(id);
|
|
46
|
+
return value;
|
|
47
|
+
}
|
|
48
|
+
has(id) {
|
|
49
|
+
return this.items.has(id);
|
|
50
|
+
}
|
|
51
|
+
delete(id) {
|
|
52
|
+
return this.items.delete(id);
|
|
53
|
+
}
|
|
54
|
+
values() {
|
|
55
|
+
return this.items.values();
|
|
56
|
+
}
|
|
57
|
+
entries() {
|
|
58
|
+
return this.items.entries();
|
|
59
|
+
}
|
|
60
|
+
list() {
|
|
61
|
+
return [...this.items.entries()];
|
|
62
|
+
}
|
|
63
|
+
/** Bounded allocation; offset traversal is O(offset + limit). Live insertion order. */
|
|
64
|
+
page({ offset = 0, limit = 100 } = {}) {
|
|
65
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || !Number.isSafeInteger(limit) || limit < 1 || limit > 1e3) throw new RangeError("Invalid object page");
|
|
66
|
+
const entries = [];
|
|
67
|
+
let index = 0;
|
|
68
|
+
for (const entry of this.items) {
|
|
69
|
+
if (index++ < offset) continue;
|
|
70
|
+
entries.push(entry);
|
|
71
|
+
if (entries.length === limit) break;
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
entries,
|
|
75
|
+
nextOffset: offset + entries.length < this.size ? offset + entries.length : null
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
clear() {
|
|
79
|
+
this.items.clear();
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
// modules/world/runtime/systems/PhysicsSystem.js
|
|
83
|
+
const array3$1 = (v) => [
|
|
84
|
+
v.x,
|
|
85
|
+
v.y,
|
|
86
|
+
v.z
|
|
87
|
+
];
|
|
88
|
+
const upright = (q) => {
|
|
89
|
+
const upX = 2 * (q.x * q.y - q.z * q.w);
|
|
90
|
+
const upY = 1 - 2 * (q.x * q.x + q.z * q.z);
|
|
91
|
+
const upZ = 2 * (q.y * q.z + q.x * q.w);
|
|
92
|
+
return upY > .999 && Math.abs(upX) < .02 && Math.abs(upZ) < .02;
|
|
93
|
+
};
|
|
94
|
+
const yaw = (q) => Math.atan2(2 * (q.w * q.y + q.x * q.z), 1 - 2 * (q.y * q.y + q.z * q.z));
|
|
95
|
+
const boxAabbHalfExtents = (half, q) => {
|
|
96
|
+
const xx = 1 - 2 * (q.y * q.y + q.z * q.z), xy = 2 * (q.x * q.y - q.z * q.w), xz = 2 * (q.x * q.z + q.y * q.w);
|
|
97
|
+
const yx = 2 * (q.x * q.y + q.z * q.w), yy = 1 - 2 * (q.x * q.x + q.z * q.z), yz = 2 * (q.y * q.z - q.x * q.w);
|
|
98
|
+
const zx = 2 * (q.x * q.z - q.y * q.w), zy = 2 * (q.y * q.z + q.x * q.w), zz = 1 - 2 * (q.x * q.x + q.y * q.y);
|
|
99
|
+
return [
|
|
100
|
+
Math.abs(xx) * half.x + Math.abs(xy) * half.y + Math.abs(xz) * half.z,
|
|
101
|
+
Math.abs(yx) * half.x + Math.abs(yy) * half.y + Math.abs(yz) * half.z,
|
|
102
|
+
Math.abs(zx) * half.x + Math.abs(zy) * half.y + Math.abs(zz) * half.z
|
|
103
|
+
];
|
|
104
|
+
};
|
|
105
|
+
const cylinderAabbHalfExtents = (radius, halfHeight, q) => {
|
|
106
|
+
return [
|
|
107
|
+
2 * (q.x * q.y - q.z * q.w),
|
|
108
|
+
1 - 2 * (q.x * q.x + q.z * q.z),
|
|
109
|
+
2 * (q.y * q.z + q.x * q.w)
|
|
110
|
+
].map((component) => radius * Math.sqrt(Math.max(0, 1 - component * component)) + halfHeight * Math.abs(component));
|
|
111
|
+
};
|
|
112
|
+
const capsuleAabbHalfExtents = (radius, halfHeight, q) => {
|
|
113
|
+
return [
|
|
114
|
+
2 * (q.x * q.y - q.z * q.w),
|
|
115
|
+
1 - 2 * (q.x * q.x + q.z * q.z),
|
|
116
|
+
2 * (q.y * q.z + q.x * q.w)
|
|
117
|
+
].map((component) => radius + halfHeight * Math.abs(component));
|
|
118
|
+
};
|
|
119
|
+
const convexAabb = (vertices, position, rotation) => {
|
|
120
|
+
const q = new THREE.Quaternion(rotation.x, rotation.y, rotation.z, rotation.w);
|
|
121
|
+
const v = new THREE.Vector3();
|
|
122
|
+
const min = new THREE.Vector3(1e400, 1e400, 1e400);
|
|
123
|
+
const max = new THREE.Vector3(-1e400, -1e400, -1e400);
|
|
124
|
+
for (let i = 0; i < vertices.length; i += 3) {
|
|
125
|
+
v.set(vertices[i], vertices[i + 1], vertices[i + 2]).applyQuaternion(q).add(position);
|
|
126
|
+
min.min(v);
|
|
127
|
+
max.max(v);
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
position: min.clone().add(max).multiplyScalar(.5).toArray(),
|
|
131
|
+
halfExtents: max.clone().sub(min).multiplyScalar(.5).toArray()
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
const navigationObstacleDescriptor = (objectId, partName, i, { shape, position, rotation }) => {
|
|
135
|
+
const id = `${objectId}:${partName}:${i}`;
|
|
136
|
+
if (shape.kind === "box") {
|
|
137
|
+
const exact = upright(rotation);
|
|
138
|
+
return {
|
|
139
|
+
id,
|
|
140
|
+
objectId,
|
|
141
|
+
part: partName,
|
|
142
|
+
collider: i,
|
|
143
|
+
shape: "box",
|
|
144
|
+
sourceShape: "box",
|
|
145
|
+
quality: exact ? "exact-yaw" : "conservative-aabb",
|
|
146
|
+
position: array3$1(position),
|
|
147
|
+
halfExtents: exact ? array3$1(shape.halfExtents) : boxAabbHalfExtents(shape.halfExtents, rotation),
|
|
148
|
+
angle: exact ? yaw(rotation) : 0
|
|
149
|
+
};
|
|
150
|
+
} else if (shape.kind === "cylinder") {
|
|
151
|
+
if (upright(rotation)) return {
|
|
152
|
+
id,
|
|
153
|
+
objectId,
|
|
154
|
+
part: partName,
|
|
155
|
+
collider: i,
|
|
156
|
+
shape: "cylinder",
|
|
157
|
+
sourceShape: "cylinder",
|
|
158
|
+
quality: "exact-upright",
|
|
159
|
+
position: [
|
|
160
|
+
position.x,
|
|
161
|
+
position.y - shape.halfHeight,
|
|
162
|
+
position.z
|
|
163
|
+
],
|
|
164
|
+
radius: shape.radius,
|
|
165
|
+
height: shape.halfHeight * 2
|
|
166
|
+
};
|
|
167
|
+
else return {
|
|
168
|
+
id,
|
|
169
|
+
objectId,
|
|
170
|
+
part: partName,
|
|
171
|
+
collider: i,
|
|
172
|
+
shape: "box",
|
|
173
|
+
sourceShape: "cylinder",
|
|
174
|
+
quality: "conservative-aabb",
|
|
175
|
+
position: array3$1(position),
|
|
176
|
+
halfExtents: cylinderAabbHalfExtents(shape.radius, shape.halfHeight, rotation),
|
|
177
|
+
angle: 0
|
|
178
|
+
};
|
|
179
|
+
} else if (shape.kind === "capsule") {
|
|
180
|
+
const extentY = shape.halfHeight + shape.radius;
|
|
181
|
+
if (upright(rotation)) return {
|
|
182
|
+
id,
|
|
183
|
+
objectId,
|
|
184
|
+
part: partName,
|
|
185
|
+
collider: i,
|
|
186
|
+
shape: "cylinder",
|
|
187
|
+
sourceShape: "capsule",
|
|
188
|
+
quality: "conservative-upright",
|
|
189
|
+
position: [
|
|
190
|
+
position.x,
|
|
191
|
+
position.y - extentY,
|
|
192
|
+
position.z
|
|
193
|
+
],
|
|
194
|
+
radius: shape.radius,
|
|
195
|
+
height: extentY * 2
|
|
196
|
+
};
|
|
197
|
+
else return {
|
|
198
|
+
id,
|
|
199
|
+
objectId,
|
|
200
|
+
part: partName,
|
|
201
|
+
collider: i,
|
|
202
|
+
shape: "box",
|
|
203
|
+
sourceShape: "capsule",
|
|
204
|
+
quality: "conservative-aabb",
|
|
205
|
+
position: array3$1(position),
|
|
206
|
+
halfExtents: capsuleAabbHalfExtents(shape.radius, shape.halfHeight, rotation),
|
|
207
|
+
angle: 0
|
|
208
|
+
};
|
|
209
|
+
} else if (shape.kind === "convexHull" && shape.vertices?.length) return {
|
|
210
|
+
id,
|
|
211
|
+
objectId,
|
|
212
|
+
part: partName,
|
|
213
|
+
collider: i,
|
|
214
|
+
shape: "box",
|
|
215
|
+
sourceShape: "convexHull",
|
|
216
|
+
quality: "conservative-aabb",
|
|
217
|
+
...convexAabb(shape.vertices, new THREE.Vector3(position.x, position.y, position.z), rotation),
|
|
218
|
+
angle: 0
|
|
219
|
+
};
|
|
220
|
+
else return null;
|
|
221
|
+
};
|
|
222
|
+
var PhysicsSystem = class {
|
|
223
|
+
constructor({ backend } = {}) {
|
|
224
|
+
if (!backend) throw new TypeError("PhysicsSystem requires a physics backend");
|
|
225
|
+
this.backend = backend;
|
|
226
|
+
this.solverEnabled = backend.hasCapability?.("rigid-body") === true;
|
|
227
|
+
this.world = null;
|
|
228
|
+
this.entries = /* @__PURE__ */ new Map();
|
|
229
|
+
this.colliderProvenance = /* @__PURE__ */ new Map();
|
|
230
|
+
this.colliderHandles = /* @__PURE__ */ new Map();
|
|
231
|
+
this.colliderRuntime = /* @__PURE__ */ new Map();
|
|
232
|
+
this.collisionPairs = /* @__PURE__ */ new Map();
|
|
233
|
+
this.collisionEvents = [];
|
|
234
|
+
this.collisionGroupBits = /* @__PURE__ */ new Map();
|
|
235
|
+
this.rootRotation = new THREE.Quaternion();
|
|
236
|
+
this.inverseRootRotation = new THREE.Quaternion();
|
|
237
|
+
this.partWorldRotation = new THREE.Quaternion();
|
|
238
|
+
this.partLocalRotation = new THREE.Quaternion();
|
|
239
|
+
this.rootWorldPosition = new THREE.Vector3();
|
|
240
|
+
this.partWorldPosition = new THREE.Vector3();
|
|
241
|
+
this.partLocalPosition = new THREE.Vector3();
|
|
242
|
+
this.characterController = null;
|
|
243
|
+
this.parentWorldPosition = new THREE.Vector3();
|
|
244
|
+
this.parentWorldRotation = new THREE.Quaternion();
|
|
245
|
+
this.inverseParentWorldRotation = new THREE.Quaternion();
|
|
246
|
+
}
|
|
247
|
+
async init(characterControllerOptions = null) {
|
|
248
|
+
await this.backend.init();
|
|
249
|
+
this.world = this.backend.createWorld();
|
|
250
|
+
this.characterControllerOptions = characterControllerOptions || this.characterControllerOptions || null;
|
|
251
|
+
if (!this.solverEnabled || !this.backend.hasCapability("character-controller")) return this;
|
|
252
|
+
this.characterController = this.backend.createCharacterController(this.world, this.characterControllerOptions || void 0);
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
setCharacterControllerOptions(options = null) {
|
|
256
|
+
this.characterControllerOptions = options && typeof options === "object" ? options : null;
|
|
257
|
+
if (!this.world || !this.solverEnabled || !this.backend.hasCapability?.("character-controller")) return this;
|
|
258
|
+
if (this.characterController && this.backend.removeCharacterController) try {
|
|
259
|
+
this.backend.removeCharacterController(this.world, this.characterController);
|
|
260
|
+
} catch {}
|
|
261
|
+
this.characterController = this.backend.createCharacterController(this.world, this.characterControllerOptions || void 0);
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
resetWorld() {
|
|
265
|
+
const previousWorld = this.world;
|
|
266
|
+
const previousController = this.characterController;
|
|
267
|
+
this.entries.clear();
|
|
268
|
+
this.colliderProvenance.clear();
|
|
269
|
+
this.colliderHandles.clear();
|
|
270
|
+
this.colliderRuntime.clear();
|
|
271
|
+
this.collisionPairs.clear();
|
|
272
|
+
this.collisionEvents.length = 0;
|
|
273
|
+
this.collisionGroupBits.clear();
|
|
274
|
+
this.characterController = null;
|
|
275
|
+
if (previousWorld && previousController) this.backend.removeCharacterController(previousWorld, previousController);
|
|
276
|
+
if (previousWorld) this.backend.dispose(previousWorld);
|
|
277
|
+
this.world = this.backend.createWorld();
|
|
278
|
+
if (this.solverEnabled && this.backend.hasCapability("character-controller")) this.characterController = this.backend.createCharacterController(this.world, this.characterControllerOptions || void 0);
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
runtimeCapabilities() {
|
|
282
|
+
const capabilities = ["transform-state", "articulation-pose"];
|
|
283
|
+
if (this.backend.hasCapability("collision") && this.backend.hasCapability("scene-query")) capabilities.push("counterfactual-query", "collision-events");
|
|
284
|
+
return capabilities;
|
|
285
|
+
}
|
|
286
|
+
hasCapability(capability) {
|
|
287
|
+
return this.backend.hasCapability(capability) || this.runtimeCapabilities().includes(capability);
|
|
288
|
+
}
|
|
289
|
+
runtimeExecutionModes() {
|
|
290
|
+
return ["render-only"];
|
|
291
|
+
}
|
|
292
|
+
supportsExecutionMode(mode) {
|
|
293
|
+
return this.backend.supportsExecutionMode(mode) || this.runtimeExecutionModes().includes(mode);
|
|
294
|
+
}
|
|
295
|
+
profile() {
|
|
296
|
+
const backendCapabilities = [...this.backend.capabilities];
|
|
297
|
+
const runtimeCapabilities = this.runtimeCapabilities();
|
|
298
|
+
const backendExecutionModes = [...this.backend.executionModes];
|
|
299
|
+
const runtimeExecutionModes = this.runtimeExecutionModes();
|
|
300
|
+
return {
|
|
301
|
+
identity: this.backend.identity,
|
|
302
|
+
backendCapabilities,
|
|
303
|
+
runtimeCapabilities,
|
|
304
|
+
capabilities: [.../* @__PURE__ */ new Set([...backendCapabilities, ...runtimeCapabilities])],
|
|
305
|
+
backendExecutionModes,
|
|
306
|
+
runtimeExecutionModes,
|
|
307
|
+
executionModes: [.../* @__PURE__ */ new Set([...backendExecutionModes, ...runtimeExecutionModes])],
|
|
308
|
+
qualities: { ...this.backend.qualities },
|
|
309
|
+
solverEnabled: this.solverEnabled
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
debugSnapshot({ nativeGeometry = true, contacts = true } = {}) {
|
|
313
|
+
const bodies = [];
|
|
314
|
+
const colliders = [];
|
|
315
|
+
const joints = [];
|
|
316
|
+
const contactPairs = [];
|
|
317
|
+
const colliderRefs = [];
|
|
318
|
+
const pushBody = (objectId, partName, body) => {
|
|
319
|
+
if (!body) return;
|
|
320
|
+
const pose = this.backend.bodyPose(body);
|
|
321
|
+
const motion = this.backend.bodyMotion(body);
|
|
322
|
+
bodies.push({
|
|
323
|
+
objectId,
|
|
324
|
+
partName,
|
|
325
|
+
bodyType: this.backend.bodyType(body),
|
|
326
|
+
position: pose?.position ? [...pose.position] : null,
|
|
327
|
+
rotation: pose?.rotation ? [...pose.rotation] : null,
|
|
328
|
+
sleeping: motion?.sleeping ?? null,
|
|
329
|
+
linearVelocity: motion?.linearVelocity ? [...motion.linearVelocity] : null,
|
|
330
|
+
angularVelocity: motion?.angularVelocity ? [...motion.angularVelocity] : null,
|
|
331
|
+
linearSpeed: Number.isFinite(motion?.linearSpeed) ? motion.linearSpeed : null,
|
|
332
|
+
angularSpeed: Number.isFinite(motion?.angularSpeed) ? motion.angularSpeed : null
|
|
333
|
+
});
|
|
334
|
+
for (const [colliderIndex, collider] of this.backend.colliders(body).entries()) {
|
|
335
|
+
const snapshot = this.backend.colliderSnapshot(collider);
|
|
336
|
+
if (!snapshot) continue;
|
|
337
|
+
const record = {
|
|
338
|
+
objectId,
|
|
339
|
+
partName,
|
|
340
|
+
colliderIndex,
|
|
341
|
+
position: [
|
|
342
|
+
snapshot.position.x,
|
|
343
|
+
snapshot.position.y,
|
|
344
|
+
snapshot.position.z
|
|
345
|
+
],
|
|
346
|
+
rotation: [
|
|
347
|
+
snapshot.rotation.x,
|
|
348
|
+
snapshot.rotation.y,
|
|
349
|
+
snapshot.rotation.z,
|
|
350
|
+
snapshot.rotation.w
|
|
351
|
+
],
|
|
352
|
+
shape: structuredClone(snapshot.shape),
|
|
353
|
+
provenance: this.provenanceOfCollider(collider)
|
|
354
|
+
};
|
|
355
|
+
colliders.push(record);
|
|
356
|
+
colliderRefs.push({
|
|
357
|
+
key: this.backend.colliderKey(collider),
|
|
358
|
+
collider,
|
|
359
|
+
record
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
for (const [objectId, entry] of this.entries) {
|
|
364
|
+
pushBody(objectId, ROOT_PART, entry.body);
|
|
365
|
+
for (const [partName, part] of entry.parts) {
|
|
366
|
+
pushBody(objectId, partName, part.body);
|
|
367
|
+
if (part.joint) {
|
|
368
|
+
const articulation = this.getArticulationState(objectId, partName);
|
|
369
|
+
const pose = this.backend.bodyPose(part.body);
|
|
370
|
+
const localAxis = part.spec?.joint?.axis ? [...part.spec.joint.axis] : null;
|
|
371
|
+
const localAnchor = part.spec?.joint?.childAnchor ? [...part.spec.joint.childAnchor] : [
|
|
372
|
+
0,
|
|
373
|
+
0,
|
|
374
|
+
0
|
|
375
|
+
];
|
|
376
|
+
let worldAxis = null, worldAnchor = null;
|
|
377
|
+
if (pose?.position && pose?.rotation) {
|
|
378
|
+
const rotation = new THREE.Quaternion(...pose.rotation);
|
|
379
|
+
worldAnchor = new THREE.Vector3(...localAnchor).applyQuaternion(rotation).add(new THREE.Vector3(...pose.position)).toArray();
|
|
380
|
+
if (localAxis) worldAxis = new THREE.Vector3(...localAxis).applyQuaternion(rotation).normalize().toArray();
|
|
381
|
+
}
|
|
382
|
+
joints.push({
|
|
383
|
+
objectId,
|
|
384
|
+
partName,
|
|
385
|
+
jointType: part.spec?.joint?.type || articulation?.jointType || null,
|
|
386
|
+
axis: localAxis,
|
|
387
|
+
limits: part.spec?.joint?.limits ? [...part.spec.joint.limits] : null,
|
|
388
|
+
parentAnchor: part.spec?.joint?.parentAnchor ? [...part.spec.joint.parentAnchor] : null,
|
|
389
|
+
childAnchor: localAnchor,
|
|
390
|
+
worldAxis,
|
|
391
|
+
worldAnchor,
|
|
392
|
+
coordinate: Number.isFinite(articulation?.coordinate) ? articulation.coordinate : null
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if (contacts && this.world && this.backend.hasCapability("collision")) {
|
|
398
|
+
const seen = /* @__PURE__ */ new Set();
|
|
399
|
+
for (const source of colliderRefs) for (const pair of this.backend.contactPairs(this.world, source.collider)) {
|
|
400
|
+
const targetKey = this.backend.colliderKey(pair.other);
|
|
401
|
+
const key = [String(source.key), String(targetKey)].sort().join(":");
|
|
402
|
+
if (seen.has(key)) continue;
|
|
403
|
+
seen.add(key);
|
|
404
|
+
const targetSnapshot = this.backend.colliderSnapshot(pair.other);
|
|
405
|
+
if (!targetSnapshot) continue;
|
|
406
|
+
const targetPosition = [
|
|
407
|
+
targetSnapshot.position.x,
|
|
408
|
+
targetSnapshot.position.y,
|
|
409
|
+
targetSnapshot.position.z
|
|
410
|
+
];
|
|
411
|
+
const anchor = source.record.position.map((value, index) => (value + targetPosition[index]) / 2);
|
|
412
|
+
contactPairs.push({
|
|
413
|
+
source: source.record.provenance || {
|
|
414
|
+
kind: "object",
|
|
415
|
+
objectId: source.record.objectId,
|
|
416
|
+
partName: source.record.partName,
|
|
417
|
+
colliderIndex: source.record.colliderIndex
|
|
418
|
+
},
|
|
419
|
+
target: this.provenanceOfCollider(pair.other) || {
|
|
420
|
+
kind: "unknown",
|
|
421
|
+
colliderIndex: null
|
|
422
|
+
},
|
|
423
|
+
sourcePosition: [...source.record.position],
|
|
424
|
+
targetPosition,
|
|
425
|
+
anchor,
|
|
426
|
+
anchorKind: "collider-midpoint",
|
|
427
|
+
normal: Array.isArray(pair.normal) ? [...pair.normal] : null,
|
|
428
|
+
minDistance: Number.isFinite(pair.minDistance) ? pair.minDistance : null,
|
|
429
|
+
totalImpulse: Number.isFinite(pair.totalImpulse) ? pair.totalImpulse : null,
|
|
430
|
+
evidenceKind: pair.evidenceKind || "contact",
|
|
431
|
+
impulseAvailable: pair.impulseAvailable === true
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
const backend = this.backend.debugSnapshot(this.world, { nativeGeometry }) || {};
|
|
436
|
+
return {
|
|
437
|
+
schemaVersion: 1,
|
|
438
|
+
source: "physics",
|
|
439
|
+
backend: this.backend.identity,
|
|
440
|
+
bodies,
|
|
441
|
+
colliders,
|
|
442
|
+
joints,
|
|
443
|
+
contacts: contactPairs,
|
|
444
|
+
nativeGeometry: backend.nativeGeometry || null,
|
|
445
|
+
nativeGeometryAvailable: backend.nativeGeometryAvailable ?? Boolean(backend.nativeGeometry),
|
|
446
|
+
metrics: {
|
|
447
|
+
...backend.metrics || {},
|
|
448
|
+
bodyCount: bodies.length,
|
|
449
|
+
colliderCount: colliders.length,
|
|
450
|
+
jointCount: joints.length,
|
|
451
|
+
contactPairCount: contactPairs.length
|
|
452
|
+
}
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
contactPartPairs(objectIds) {
|
|
456
|
+
if (!this.world || !this.backend.hasCapability("collision")) return [];
|
|
457
|
+
const allowed = new Set(objectIds);
|
|
458
|
+
const seen = /* @__PURE__ */ new Set(), result = [];
|
|
459
|
+
for (const id of allowed) {
|
|
460
|
+
const entry = this.entries.get(id);
|
|
461
|
+
if (!entry) continue;
|
|
462
|
+
for (const body of [entry.body, ...[...entry.parts.values()].map((part) => part.body)]) {
|
|
463
|
+
if (!body) continue;
|
|
464
|
+
for (const collider of this.backend.colliders(body)) {
|
|
465
|
+
const source = this.provenanceOfCollider(collider);
|
|
466
|
+
if (source?.kind !== "object") continue;
|
|
467
|
+
for (const pair of this.backend.contactPairs(this.world, collider)) {
|
|
468
|
+
const target = this.provenanceOfCollider(pair.other);
|
|
469
|
+
if (target?.kind !== "object" || source.objectId === target.objectId || !allowed.has(target.objectId)) continue;
|
|
470
|
+
const [left, right] = [source, target].sort((a, b) => a.objectId.localeCompare(b.objectId) || a.partName.localeCompare(b.partName));
|
|
471
|
+
const key = [
|
|
472
|
+
left.objectId,
|
|
473
|
+
left.partName,
|
|
474
|
+
right.objectId,
|
|
475
|
+
right.partName
|
|
476
|
+
].join("|");
|
|
477
|
+
if (seen.has(key)) continue;
|
|
478
|
+
seen.add(key);
|
|
479
|
+
result.push({
|
|
480
|
+
sourceId: left.objectId,
|
|
481
|
+
sourcePartName: left.partName,
|
|
482
|
+
targetId: right.objectId,
|
|
483
|
+
targetPartName: right.partName,
|
|
484
|
+
evidenceKind: "physics-contact"
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
contactObjectPairs(objectIds) {
|
|
493
|
+
const seen = /* @__PURE__ */ new Set(), result = [];
|
|
494
|
+
for (const pair of this.contactPartPairs(objectIds)) {
|
|
495
|
+
const key = pair.sourceId + "|" + pair.targetId;
|
|
496
|
+
if (seen.has(key)) continue;
|
|
497
|
+
seen.add(key);
|
|
498
|
+
result.push({
|
|
499
|
+
sourceId: pair.sourceId,
|
|
500
|
+
targetId: pair.targetId,
|
|
501
|
+
evidenceKind: "physics-contact"
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
return result;
|
|
505
|
+
}
|
|
506
|
+
addEnvironment(colliders = [], { id = "$environment", collision = null, sensor = false, collisionEvents = false } = {}) {
|
|
507
|
+
if (!this.solverEnabled) return null;
|
|
508
|
+
const body = this.backend.createBody(this.world, { type: "fixed" });
|
|
509
|
+
this.addColliders(body, colliders, {
|
|
510
|
+
collision,
|
|
511
|
+
sensor,
|
|
512
|
+
collisionEvents,
|
|
513
|
+
provenance: {
|
|
514
|
+
kind: "environment",
|
|
515
|
+
environmentId: id
|
|
516
|
+
}
|
|
517
|
+
});
|
|
518
|
+
return body;
|
|
519
|
+
}
|
|
520
|
+
addFloor() {
|
|
521
|
+
return this.addEnvironment([{
|
|
522
|
+
shape: "box",
|
|
523
|
+
halfExtents: [
|
|
524
|
+
5,
|
|
525
|
+
.1,
|
|
526
|
+
4
|
|
527
|
+
],
|
|
528
|
+
translation: [
|
|
529
|
+
0,
|
|
530
|
+
-.1,
|
|
531
|
+
0
|
|
532
|
+
]
|
|
533
|
+
}]);
|
|
534
|
+
}
|
|
535
|
+
setEnvironmentPose(body, pose) {
|
|
536
|
+
if (!this.solverEnabled || !body) return false;
|
|
537
|
+
this.backend.setBodyPose(body, pose);
|
|
538
|
+
return true;
|
|
539
|
+
}
|
|
540
|
+
_collisionBit(name) {
|
|
541
|
+
if (this.collisionGroupBits.has(name)) return this.collisionGroupBits.get(name);
|
|
542
|
+
if (this.collisionGroupBits.size >= 16) throw new Error("Physics collision groups exceed Rapier-compatible limit of 16");
|
|
543
|
+
const bit = 1 << this.collisionGroupBits.size;
|
|
544
|
+
this.collisionGroupBits.set(name, bit);
|
|
545
|
+
return bit;
|
|
546
|
+
}
|
|
547
|
+
_compileCollisionFilter(collision) {
|
|
548
|
+
if (!collision) return null;
|
|
549
|
+
let membership = 0, filter = 0;
|
|
550
|
+
for (const name of collision.groups || []) membership |= this._collisionBit(name);
|
|
551
|
+
for (const name of collision.collidesWith || []) filter |= this._collisionBit(name);
|
|
552
|
+
return {
|
|
553
|
+
membership: membership & 65535,
|
|
554
|
+
filter: filter & 65535
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
_collisionPairAllowed(leftRuntime, rightRuntime) {
|
|
558
|
+
const left = leftRuntime?.collisionFilter || {
|
|
559
|
+
membership: 65535,
|
|
560
|
+
filter: 65535
|
|
561
|
+
};
|
|
562
|
+
const right = rightRuntime?.collisionFilter || {
|
|
563
|
+
membership: 65535,
|
|
564
|
+
filter: 65535
|
|
565
|
+
};
|
|
566
|
+
return (left.membership & right.filter) !== 0 && (right.membership & left.filter) !== 0;
|
|
567
|
+
}
|
|
568
|
+
addColliders(body, colliders = [], { mass, friction, restitution, sensor = false, collisionEvents = false, collision = null, provenance = null } = {}) {
|
|
569
|
+
if (!this.solverEnabled || !body || !this.backend.hasCapability("collision")) return [];
|
|
570
|
+
const collisionFilter = this._compileCollisionFilter(collision);
|
|
571
|
+
const created = this.backend.createColliders(this.world, body, colliders, {
|
|
572
|
+
mass,
|
|
573
|
+
friction,
|
|
574
|
+
restitution
|
|
575
|
+
});
|
|
576
|
+
this.backend.setBodyCollisionFilter(body, collisionFilter);
|
|
577
|
+
created.forEach((collider, colliderIndex) => {
|
|
578
|
+
const key = this.backend.colliderKey(collider);
|
|
579
|
+
this.colliderHandles.set(key, collider);
|
|
580
|
+
if (provenance) this.colliderProvenance.set(key, {
|
|
581
|
+
...provenance,
|
|
582
|
+
colliderIndex
|
|
583
|
+
});
|
|
584
|
+
this.colliderRuntime.set(key, {
|
|
585
|
+
sensor: Boolean(sensor),
|
|
586
|
+
collisionEvents: Boolean(collisionEvents),
|
|
587
|
+
collisionFilter
|
|
588
|
+
});
|
|
589
|
+
});
|
|
590
|
+
if (sensor) this.backend.setBodySensor(body, true);
|
|
591
|
+
return created;
|
|
592
|
+
}
|
|
593
|
+
unregisterBodyColliders(body) {
|
|
594
|
+
if (!body) return;
|
|
595
|
+
for (const collider of this.backend.colliders(body)) {
|
|
596
|
+
const key = this.backend.colliderKey(collider);
|
|
597
|
+
this.colliderProvenance.delete(key);
|
|
598
|
+
this.colliderHandles.delete(key);
|
|
599
|
+
this.colliderRuntime.delete(key);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
provenanceOfCollider(collider) {
|
|
603
|
+
if (!collider) return null;
|
|
604
|
+
const owner = this.colliderProvenance.get(this.backend.colliderKey(collider));
|
|
605
|
+
return owner ? structuredClone(owner) : null;
|
|
606
|
+
}
|
|
607
|
+
addObject(id, manifest, object) {
|
|
608
|
+
if (!this.solverEnabled) return this.addTransformState(id, manifest, object);
|
|
609
|
+
const createdBodies = [];
|
|
610
|
+
try {
|
|
611
|
+
const worldPos = new THREE.Vector3();
|
|
612
|
+
const worldRot = new THREE.Quaternion();
|
|
613
|
+
object.getWorldPosition(worldPos);
|
|
614
|
+
object.getWorldQuaternion(worldRot);
|
|
615
|
+
const body = this.backend.createBody(this.world, {
|
|
616
|
+
type: manifest.physics?.body || "fixed",
|
|
617
|
+
position: worldPos,
|
|
618
|
+
rotation: worldRot,
|
|
619
|
+
canSleep: manifest.physics?.canSleep ?? true,
|
|
620
|
+
lockTranslation: manifest.physics?.lockTranslation,
|
|
621
|
+
lockRotation: manifest.physics?.lockRotation
|
|
622
|
+
});
|
|
623
|
+
createdBodies.push(body);
|
|
624
|
+
this.addColliders(body, manifest.physics?.colliders, {
|
|
625
|
+
...manifest.physics,
|
|
626
|
+
provenance: {
|
|
627
|
+
kind: "object",
|
|
628
|
+
objectId: id,
|
|
629
|
+
partName: ROOT_PART
|
|
630
|
+
}
|
|
631
|
+
});
|
|
632
|
+
this.backend.setBodyDynamics(body, manifest.physics || {});
|
|
633
|
+
if (manifest.physics?.ccd != null) this.backend.setBodyCcd(body, manifest.physics.ccd);
|
|
634
|
+
const entry = {
|
|
635
|
+
body,
|
|
636
|
+
root: object,
|
|
637
|
+
rootSpec: manifest.physics || {},
|
|
638
|
+
nativeNavigation: manifest.nativeNavigation === true,
|
|
639
|
+
nativeArticulation: manifest.nativeArticulation === true,
|
|
640
|
+
parts: /* @__PURE__ */ new Map(),
|
|
641
|
+
lastPosition: worldPos.clone(),
|
|
642
|
+
lastRotation: worldRot.clone()
|
|
643
|
+
};
|
|
644
|
+
const bodies = /* @__PURE__ */ new Map([[ROOT_PART, body]]);
|
|
645
|
+
for (const [partName, part] of orderParts(manifest.parts || {})) {
|
|
646
|
+
if (!part.physics || !part.joint) continue;
|
|
647
|
+
const node = object.getObjectByName(part.node);
|
|
648
|
+
if (!node) continue;
|
|
649
|
+
const parentName = part.parent || "$root";
|
|
650
|
+
const parentBody = bodies.get(parentName);
|
|
651
|
+
if (!parentBody) throw new Error(`Part parent body not available: ${partName} -> ${parentName}`);
|
|
652
|
+
const partWorld = new THREE.Vector3();
|
|
653
|
+
const partRotation = new THREE.Quaternion();
|
|
654
|
+
node.getWorldPosition(partWorld);
|
|
655
|
+
node.getWorldQuaternion(partRotation);
|
|
656
|
+
const child = this.backend.createBody(this.world, {
|
|
657
|
+
type: part.physics.body || "dynamic",
|
|
658
|
+
position: partWorld,
|
|
659
|
+
rotation: partRotation,
|
|
660
|
+
canSleep: part.physics?.canSleep ?? true,
|
|
661
|
+
lockTranslation: part.physics?.lockTranslation,
|
|
662
|
+
lockRotation: part.physics?.lockRotation
|
|
663
|
+
});
|
|
664
|
+
createdBodies.push(child);
|
|
665
|
+
this.addColliders(child, part.physics.colliders, {
|
|
666
|
+
...part.physics,
|
|
667
|
+
provenance: {
|
|
668
|
+
kind: "object",
|
|
669
|
+
objectId: id,
|
|
670
|
+
partName
|
|
671
|
+
}
|
|
672
|
+
});
|
|
673
|
+
this.backend.setBodyDynamics(child, part.physics);
|
|
674
|
+
if (part.physics?.ccd != null) this.backend.setBodyCcd(child, part.physics.ccd);
|
|
675
|
+
const joint = this.backend.createJoint(this.world, part, parentBody, child);
|
|
676
|
+
bodies.set(partName, child);
|
|
677
|
+
entry.parts.set(partName, {
|
|
678
|
+
body: child,
|
|
679
|
+
joint,
|
|
680
|
+
node,
|
|
681
|
+
spec: part,
|
|
682
|
+
parentName,
|
|
683
|
+
restLocalRotation: node.quaternion.clone(),
|
|
684
|
+
restLocalPosition: node.position.clone(),
|
|
685
|
+
lastLocalRotation: node.quaternion.clone(),
|
|
686
|
+
lastLocalPosition: node.position.clone()
|
|
687
|
+
});
|
|
688
|
+
}
|
|
689
|
+
this.entries.set(id, entry);
|
|
690
|
+
return entry;
|
|
691
|
+
} catch (error) {
|
|
692
|
+
for (const body of createdBodies.reverse()) try {
|
|
693
|
+
this.unregisterBodyColliders(body);
|
|
694
|
+
this.backend.removeBody(this.world, body);
|
|
695
|
+
} catch {}
|
|
696
|
+
throw error;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
addTransformState(id, manifest, object) {
|
|
700
|
+
object.updateMatrixWorld(true);
|
|
701
|
+
const worldPos = new THREE.Vector3();
|
|
702
|
+
const worldRot = new THREE.Quaternion();
|
|
703
|
+
object.getWorldPosition(worldPos);
|
|
704
|
+
object.getWorldQuaternion(worldRot);
|
|
705
|
+
const entry = {
|
|
706
|
+
body: null,
|
|
707
|
+
root: object,
|
|
708
|
+
rootSpec: manifest.physics || {},
|
|
709
|
+
parts: /* @__PURE__ */ new Map(),
|
|
710
|
+
lastPosition: worldPos.clone(),
|
|
711
|
+
lastRotation: worldRot.clone(),
|
|
712
|
+
held: false,
|
|
713
|
+
transformOnly: true
|
|
714
|
+
};
|
|
715
|
+
for (const [partName, part] of orderParts(manifest.parts || {})) {
|
|
716
|
+
if (!part.joint) continue;
|
|
717
|
+
const node = object.getObjectByName(part.node);
|
|
718
|
+
if (!node) continue;
|
|
719
|
+
entry.parts.set(partName, {
|
|
720
|
+
body: null,
|
|
721
|
+
joint: null,
|
|
722
|
+
node,
|
|
723
|
+
spec: part,
|
|
724
|
+
parentName: part.parent || "$root",
|
|
725
|
+
restLocalRotation: node.quaternion.clone(),
|
|
726
|
+
restLocalPosition: node.position.clone(),
|
|
727
|
+
lastLocalRotation: node.quaternion.clone(),
|
|
728
|
+
lastLocalPosition: node.position.clone()
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
this.entries.set(id, entry);
|
|
732
|
+
return entry;
|
|
733
|
+
}
|
|
734
|
+
setPosition(id, position) {
|
|
735
|
+
const entry = this.entries.get(id);
|
|
736
|
+
if (!entry) return;
|
|
737
|
+
const next = new THREE.Vector3(...position);
|
|
738
|
+
if (!entry.body) {
|
|
739
|
+
if (entry.root.parent) {
|
|
740
|
+
entry.root.parent.updateWorldMatrix(true, false);
|
|
741
|
+
entry.root.position.copy(entry.root.parent.worldToLocal(next.clone()));
|
|
742
|
+
} else entry.root.position.copy(next);
|
|
743
|
+
entry.root.updateMatrixWorld(true);
|
|
744
|
+
entry.lastPosition.copy(next);
|
|
745
|
+
return true;
|
|
746
|
+
}
|
|
747
|
+
const delta = next.clone().sub(entry.lastPosition);
|
|
748
|
+
this.backend.setBodyPose(entry.body, { position });
|
|
749
|
+
this.backend.clearBodyMotion(entry.body, {
|
|
750
|
+
linear: true,
|
|
751
|
+
angular: false,
|
|
752
|
+
wake: true
|
|
753
|
+
});
|
|
754
|
+
for (const { body } of entry.parts.values()) this.backend.translateBody(body, delta.toArray(), {
|
|
755
|
+
clearLinearVelocity: true,
|
|
756
|
+
wake: true
|
|
757
|
+
});
|
|
758
|
+
entry.lastPosition.copy(next);
|
|
759
|
+
}
|
|
760
|
+
beginTransform(id) {
|
|
761
|
+
const entry = this.entries.get(id);
|
|
762
|
+
if (!entry) return;
|
|
763
|
+
if (!entry.body) {
|
|
764
|
+
entry.transforming = true;
|
|
765
|
+
return true;
|
|
766
|
+
}
|
|
767
|
+
entry.originalType = this.backend.bodyType(entry.body);
|
|
768
|
+
this.backend.setBodyType(entry.body, "kinematic");
|
|
769
|
+
for (const part of entry.parts.values()) {
|
|
770
|
+
part.originalType = this.backend.bodyType(part.body);
|
|
771
|
+
this.backend.setBodyType(part.body, "kinematic");
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
syncTransform(id, object) {
|
|
775
|
+
const entry = this.entries.get(id);
|
|
776
|
+
if (!entry) return;
|
|
777
|
+
object.updateMatrixWorld(true);
|
|
778
|
+
const p = new THREE.Vector3();
|
|
779
|
+
const q = new THREE.Quaternion();
|
|
780
|
+
object.getWorldPosition(p);
|
|
781
|
+
object.getWorldQuaternion(q);
|
|
782
|
+
if (!entry.body) {
|
|
783
|
+
entry.lastPosition.copy(p);
|
|
784
|
+
entry.lastRotation.copy(q);
|
|
785
|
+
for (const part of entry.parts.values()) {
|
|
786
|
+
part.lastLocalPosition.copy(part.node.position);
|
|
787
|
+
part.lastLocalRotation.copy(part.node.quaternion);
|
|
788
|
+
}
|
|
789
|
+
return true;
|
|
790
|
+
}
|
|
791
|
+
this.backend.setBodyPose(entry.body, {
|
|
792
|
+
position: p,
|
|
793
|
+
rotation: q
|
|
794
|
+
});
|
|
795
|
+
entry.lastPosition.copy(p);
|
|
796
|
+
entry.lastRotation.copy(q);
|
|
797
|
+
for (const part of entry.parts.values()) {
|
|
798
|
+
const pp = new THREE.Vector3();
|
|
799
|
+
const pq = new THREE.Quaternion();
|
|
800
|
+
part.node.getWorldPosition(pp);
|
|
801
|
+
part.node.getWorldQuaternion(pq);
|
|
802
|
+
this.backend.setBodyPose(part.body, {
|
|
803
|
+
position: pp,
|
|
804
|
+
rotation: pq
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
endTransform(id) {
|
|
809
|
+
const entry = this.entries.get(id);
|
|
810
|
+
if (!entry) return;
|
|
811
|
+
if (!entry.body) {
|
|
812
|
+
delete entry.transforming;
|
|
813
|
+
return true;
|
|
814
|
+
}
|
|
815
|
+
if (entry.originalType != null) this.backend.setBodyType(entry.body, entry.originalType);
|
|
816
|
+
delete entry.originalType;
|
|
817
|
+
for (const part of entry.parts.values()) {
|
|
818
|
+
if (part.originalType != null) this.backend.setBodyType(part.body, part.originalType);
|
|
819
|
+
delete part.originalType;
|
|
820
|
+
this.backend.wakeBody(part.body);
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
removeObject(id) {
|
|
824
|
+
const entry = this.entries.get(id);
|
|
825
|
+
if (!entry) return false;
|
|
826
|
+
if (!entry.body) {
|
|
827
|
+
this.entries.delete(id);
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
for (const part of entry.parts.values()) {
|
|
831
|
+
this.unregisterBodyColliders(part.body);
|
|
832
|
+
this.backend.removeBody(this.world, part.body);
|
|
833
|
+
}
|
|
834
|
+
this.unregisterBodyColliders(entry.body);
|
|
835
|
+
this.backend.removeBody(this.world, entry.body);
|
|
836
|
+
this.entries.delete(id);
|
|
837
|
+
return true;
|
|
838
|
+
}
|
|
839
|
+
setHeld(id, held) {
|
|
840
|
+
const entry = this.entries.get(id);
|
|
841
|
+
if (!entry) return false;
|
|
842
|
+
if (!entry.body) {
|
|
843
|
+
entry.held = Boolean(held);
|
|
844
|
+
return true;
|
|
845
|
+
}
|
|
846
|
+
if (held) {
|
|
847
|
+
if (entry.heldOriginalType == null) entry.heldOriginalType = this.backend.bodyType(entry.body);
|
|
848
|
+
entry.held = true;
|
|
849
|
+
this.backend.setBodyType(entry.body, "kinematic");
|
|
850
|
+
this.backend.clearBodyMotion(entry.body, { wake: true });
|
|
851
|
+
} else {
|
|
852
|
+
entry.held = false;
|
|
853
|
+
this.backend.setBodyType(entry.body, entry.heldOriginalType ?? "dynamic");
|
|
854
|
+
this.backend.clearBodyMotion(entry.body, { wake: true });
|
|
855
|
+
delete entry.heldOriginalType;
|
|
856
|
+
}
|
|
857
|
+
return true;
|
|
858
|
+
}
|
|
859
|
+
setHeldTarget(id, target, rotation = null) {
|
|
860
|
+
const entry = this.entries.get(id);
|
|
861
|
+
const body = entry?.body;
|
|
862
|
+
if (!entry) return false;
|
|
863
|
+
if (!body) return this.setHeldPose(id, target, rotation);
|
|
864
|
+
this.backend.setBodyPose(body, {
|
|
865
|
+
position: target,
|
|
866
|
+
rotation,
|
|
867
|
+
next: true
|
|
868
|
+
});
|
|
869
|
+
return true;
|
|
870
|
+
}
|
|
871
|
+
setHeldPose(id, position, rotation = null) {
|
|
872
|
+
const entry = this.entries.get(id);
|
|
873
|
+
if (!entry) return false;
|
|
874
|
+
if (entry.body) this.backend.setBodyPose(entry.body, {
|
|
875
|
+
position,
|
|
876
|
+
rotation
|
|
877
|
+
});
|
|
878
|
+
if (entry.root.parent) {
|
|
879
|
+
entry.root.parent.updateWorldMatrix(true, false);
|
|
880
|
+
entry.root.position.copy(entry.root.parent.worldToLocal(new THREE.Vector3(...position)));
|
|
881
|
+
} else entry.root.position.fromArray(position);
|
|
882
|
+
entry.lastPosition.fromArray(position);
|
|
883
|
+
if (rotation) {
|
|
884
|
+
entry.lastRotation.fromArray(rotation);
|
|
885
|
+
if (entry.root.parent) {
|
|
886
|
+
entry.root.parent.getWorldQuaternion(this.parentWorldRotation);
|
|
887
|
+
entry.root.quaternion.copy(this.parentWorldRotation.invert()).multiply(entry.lastRotation);
|
|
888
|
+
} else entry.root.quaternion.copy(entry.lastRotation);
|
|
889
|
+
}
|
|
890
|
+
entry.root.updateMatrixWorld(true);
|
|
891
|
+
return true;
|
|
892
|
+
}
|
|
893
|
+
anchorPose(id, anchor, { next = false } = {}) {
|
|
894
|
+
const entry = this.entries.get(id);
|
|
895
|
+
const body = entry?.body;
|
|
896
|
+
if (!entry || !anchor?.translation) return null;
|
|
897
|
+
let position;
|
|
898
|
+
let rotation;
|
|
899
|
+
if (body) {
|
|
900
|
+
const pose = this.backend.bodyPose(body, { next });
|
|
901
|
+
position = new THREE.Vector3(...pose.position);
|
|
902
|
+
rotation = new THREE.Quaternion(...pose.rotation);
|
|
903
|
+
} else {
|
|
904
|
+
entry.root.updateWorldMatrix(true, false);
|
|
905
|
+
position = new THREE.Vector3();
|
|
906
|
+
rotation = new THREE.Quaternion();
|
|
907
|
+
entry.root.getWorldPosition(position);
|
|
908
|
+
entry.root.getWorldQuaternion(rotation);
|
|
909
|
+
}
|
|
910
|
+
const local = new THREE.Vector3(...anchor.translation).applyQuaternion(rotation);
|
|
911
|
+
const anchorPosition = position.clone().add(local);
|
|
912
|
+
const localRotation = new THREE.Quaternion(...anchor.rotation || [
|
|
913
|
+
0,
|
|
914
|
+
0,
|
|
915
|
+
0,
|
|
916
|
+
1
|
|
917
|
+
]);
|
|
918
|
+
const worldRotation = rotation.clone().multiply(localRotation).normalize();
|
|
919
|
+
return {
|
|
920
|
+
position: anchorPosition.toArray(),
|
|
921
|
+
rotation: worldRotation.toArray()
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
checkManifestPose(manifest, targetPosition, { excludeIds = [], quaternion = [
|
|
925
|
+
0,
|
|
926
|
+
0,
|
|
927
|
+
0,
|
|
928
|
+
1
|
|
929
|
+
] } = {}) {
|
|
930
|
+
if (!this.backend.hasCapability("collision")) return {
|
|
931
|
+
checked: false,
|
|
932
|
+
clear: false,
|
|
933
|
+
reason: "PHYSICS_CAPABILITY_UNAVAILABLE",
|
|
934
|
+
capability: "collision"
|
|
935
|
+
};
|
|
936
|
+
const colliders = manifest?.physics?.colliders || [];
|
|
937
|
+
if (!colliders.length) return {
|
|
938
|
+
checked: false,
|
|
939
|
+
clear: false,
|
|
940
|
+
reason: "ROOT_COLLIDER_UNAVAILABLE"
|
|
941
|
+
};
|
|
942
|
+
const excluded = new Set(excludeIds);
|
|
943
|
+
const blockedBy = /* @__PURE__ */ new Set();
|
|
944
|
+
const shapeFor = (spec) => {
|
|
945
|
+
return this.backend.createQueryShape(spec);
|
|
946
|
+
};
|
|
947
|
+
this.backend.syncSceneQueries(this.world);
|
|
948
|
+
for (let i = 0; i < colliders.length; i++) {
|
|
949
|
+
const spec = colliders[i], shape = shapeFor(spec);
|
|
950
|
+
if (!shape) return {
|
|
951
|
+
checked: false,
|
|
952
|
+
clear: false,
|
|
953
|
+
reason: "ROOT_COLLIDER_UNSUPPORTED",
|
|
954
|
+
collider: i,
|
|
955
|
+
shape: spec.shape || null
|
|
956
|
+
};
|
|
957
|
+
const local = spec.translation || [
|
|
958
|
+
0,
|
|
959
|
+
0,
|
|
960
|
+
0
|
|
961
|
+
];
|
|
962
|
+
const rootRotation = new THREE.Quaternion(...quaternion).normalize();
|
|
963
|
+
const position = new THREE.Vector3(...local).applyQuaternion(rootRotation).add(new THREE.Vector3(...targetPosition));
|
|
964
|
+
const rotation = rootRotation.clone().multiply(new THREE.Quaternion(...spec.rotation || [
|
|
965
|
+
0,
|
|
966
|
+
0,
|
|
967
|
+
0,
|
|
968
|
+
1
|
|
969
|
+
])).normalize();
|
|
970
|
+
try {
|
|
971
|
+
this.backend.intersectionsWithShape(this.world, position, rotation, shape, (other) => {
|
|
972
|
+
const provenance = this.provenanceOfCollider(other);
|
|
973
|
+
if (provenance?.kind === "object" && excluded.has(provenance.objectId)) return true;
|
|
974
|
+
blockedBy.add(provenance?.kind === "environment" ? `environment:${provenance.environmentId || "$environment"}` : provenance?.kind === "object" ? `object:${provenance.objectId}:${provenance.partName || "$root"}` : "$unknown");
|
|
975
|
+
return false;
|
|
976
|
+
});
|
|
977
|
+
} finally {
|
|
978
|
+
this.backend.disposeQueryShape(shape);
|
|
979
|
+
}
|
|
980
|
+
if (blockedBy.size) return {
|
|
981
|
+
checked: true,
|
|
982
|
+
clear: false,
|
|
983
|
+
blockedBy: [...blockedBy].sort(),
|
|
984
|
+
coverage: Object.keys(manifest.parts || {}).length ? "root-only" : "full-root"
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
return {
|
|
988
|
+
checked: true,
|
|
989
|
+
clear: true,
|
|
990
|
+
blockedBy: [],
|
|
991
|
+
coverage: Object.keys(manifest.parts || {}).length ? "root-only" : "full-root"
|
|
992
|
+
};
|
|
993
|
+
}
|
|
994
|
+
checkBodyPose(id, targetPosition, targetRotation = null, { excludeIds = [] } = {}) {
|
|
995
|
+
if (!this.backend.hasCapability("collision")) return {
|
|
996
|
+
clear: false,
|
|
997
|
+
code: "PHYSICS_CAPABILITY_UNAVAILABLE",
|
|
998
|
+
capability: "collision"
|
|
999
|
+
};
|
|
1000
|
+
const entry = this.entries.get(id);
|
|
1001
|
+
if (!entry || entry.parts.size) return {
|
|
1002
|
+
clear: false,
|
|
1003
|
+
code: "CARRY_BODY_UNSUPPORTED"
|
|
1004
|
+
};
|
|
1005
|
+
const bodyPose = this.backend.bodyPose(entry.body);
|
|
1006
|
+
const bodyRotation = new THREE.Quaternion(...bodyPose.rotation);
|
|
1007
|
+
const nextRotation = targetRotation ? new THREE.Quaternion(...targetRotation) : bodyRotation.clone();
|
|
1008
|
+
const targetBody = new THREE.Vector3(...targetPosition);
|
|
1009
|
+
const excluded = /* @__PURE__ */ new Set([id, ...excludeIds]);
|
|
1010
|
+
const filter = (collider) => {
|
|
1011
|
+
const parent = this.backend.colliderParent(collider);
|
|
1012
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
1013
|
+
return !owner || !excluded.has(owner.id);
|
|
1014
|
+
};
|
|
1015
|
+
this.backend.syncSceneQueries(this.world);
|
|
1016
|
+
const bodyColliders = this.backend.colliders(entry.body);
|
|
1017
|
+
for (let i = 0; i < bodyColliders.length; i++) {
|
|
1018
|
+
const collider = bodyColliders[i];
|
|
1019
|
+
const snapshot = this.backend.colliderSnapshot(collider);
|
|
1020
|
+
const spec = entry.rootSpec.colliders?.[i] || {};
|
|
1021
|
+
if (![
|
|
1022
|
+
"box",
|
|
1023
|
+
"cylinder",
|
|
1024
|
+
"capsule"
|
|
1025
|
+
].includes(spec.shape)) return {
|
|
1026
|
+
clear: false,
|
|
1027
|
+
code: "CARRY_COLLIDER_UNSUPPORTED",
|
|
1028
|
+
collider: i,
|
|
1029
|
+
shape: spec.shape || null
|
|
1030
|
+
};
|
|
1031
|
+
const targetCenter = new THREE.Vector3(...spec.translation || [
|
|
1032
|
+
0,
|
|
1033
|
+
0,
|
|
1034
|
+
0
|
|
1035
|
+
]).applyQuaternion(nextRotation).add(targetBody);
|
|
1036
|
+
let overlap = null;
|
|
1037
|
+
const colliderRotation = nextRotation.clone().multiply(new THREE.Quaternion(...spec.rotation || [
|
|
1038
|
+
0,
|
|
1039
|
+
0,
|
|
1040
|
+
0,
|
|
1041
|
+
1
|
|
1042
|
+
]));
|
|
1043
|
+
this.backend.intersectionsWithShape(this.world, targetCenter, colliderRotation, snapshot.shapeRef, (other) => {
|
|
1044
|
+
const parent = this.backend.colliderParent(other);
|
|
1045
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
1046
|
+
if (owner && excluded.has(owner.id)) return true;
|
|
1047
|
+
overlap = owner?.id || "$environment";
|
|
1048
|
+
return false;
|
|
1049
|
+
}, {
|
|
1050
|
+
excludeCollider: collider,
|
|
1051
|
+
excludeBody: entry.body,
|
|
1052
|
+
predicate: filter
|
|
1053
|
+
});
|
|
1054
|
+
if (overlap) return {
|
|
1055
|
+
clear: false,
|
|
1056
|
+
code: "CARRY_TARGET_BLOCKED",
|
|
1057
|
+
collider: i,
|
|
1058
|
+
blockedBy: [overlap]
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
return { clear: true };
|
|
1062
|
+
}
|
|
1063
|
+
checkBodyMotion(id, targetPosition, targetRotation = null, { excludeIds = [] } = {}) {
|
|
1064
|
+
if (!this.backend.hasCapability("collision")) return {
|
|
1065
|
+
clear: false,
|
|
1066
|
+
code: "PHYSICS_CAPABILITY_UNAVAILABLE",
|
|
1067
|
+
capability: "collision"
|
|
1068
|
+
};
|
|
1069
|
+
const entry = this.entries.get(id);
|
|
1070
|
+
if (!entry || entry.parts.size) return {
|
|
1071
|
+
clear: false,
|
|
1072
|
+
code: "CARRY_BODY_UNSUPPORTED"
|
|
1073
|
+
};
|
|
1074
|
+
const bodyPose = this.backend.bodyPose(entry.body);
|
|
1075
|
+
const bodyRotation = new THREE.Quaternion(...bodyPose.rotation);
|
|
1076
|
+
const nextRotation = targetRotation ? new THREE.Quaternion(...targetRotation) : bodyRotation.clone();
|
|
1077
|
+
const targetBody = new THREE.Vector3(...targetPosition);
|
|
1078
|
+
const blockedBy = /* @__PURE__ */ new Set();
|
|
1079
|
+
const excluded = /* @__PURE__ */ new Set([id, ...excludeIds]);
|
|
1080
|
+
const filter = (collider) => {
|
|
1081
|
+
const parent = this.backend.colliderParent(collider);
|
|
1082
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
1083
|
+
return !owner || !excluded.has(owner.id);
|
|
1084
|
+
};
|
|
1085
|
+
this.backend.syncSceneQueries(this.world);
|
|
1086
|
+
const bodyColliders = this.backend.colliders(entry.body);
|
|
1087
|
+
for (let i = 0; i < bodyColliders.length; i++) {
|
|
1088
|
+
const collider = bodyColliders[i];
|
|
1089
|
+
const snapshot = this.backend.colliderSnapshot(collider);
|
|
1090
|
+
const spec = entry.rootSpec.colliders?.[i] || {};
|
|
1091
|
+
if (![
|
|
1092
|
+
"box",
|
|
1093
|
+
"cylinder",
|
|
1094
|
+
"capsule"
|
|
1095
|
+
].includes(spec.shape)) return {
|
|
1096
|
+
clear: false,
|
|
1097
|
+
code: "CARRY_COLLIDER_UNSUPPORTED",
|
|
1098
|
+
collider: i,
|
|
1099
|
+
shape: spec.shape || null
|
|
1100
|
+
};
|
|
1101
|
+
const delta = new THREE.Vector3(...spec.translation || [
|
|
1102
|
+
0,
|
|
1103
|
+
0,
|
|
1104
|
+
0
|
|
1105
|
+
]).applyQuaternion(nextRotation).add(targetBody).clone().sub(new THREE.Vector3(snapshot.position.x, snapshot.position.y, snapshot.position.z));
|
|
1106
|
+
if (delta.lengthSq() > 1e-12) {
|
|
1107
|
+
const hit = this.backend.castCollider(this.world, collider, delta.toArray(), {
|
|
1108
|
+
excludeCollider: collider,
|
|
1109
|
+
excludeBody: entry.body,
|
|
1110
|
+
predicate: filter,
|
|
1111
|
+
targetDistance: 0,
|
|
1112
|
+
maxToi: 1,
|
|
1113
|
+
stopAtPenetration: false
|
|
1114
|
+
});
|
|
1115
|
+
if (hit) {
|
|
1116
|
+
const parent = this.backend.colliderParent(hit.collider);
|
|
1117
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
1118
|
+
blockedBy.add(owner?.id || "$environment");
|
|
1119
|
+
return {
|
|
1120
|
+
clear: false,
|
|
1121
|
+
code: "CARRY_SWEEP_BLOCKED",
|
|
1122
|
+
collider: i,
|
|
1123
|
+
blockedBy: [...blockedBy],
|
|
1124
|
+
toi: hit.timeOfImpact
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
return this.checkBodyPose(id, targetPosition, targetRotation, { excludeIds });
|
|
1130
|
+
}
|
|
1131
|
+
cancelCharacterMovement(id) {
|
|
1132
|
+
if (!this.backend.hasCapability("character-controller")) return false;
|
|
1133
|
+
const body = this.entries.get(id)?.body;
|
|
1134
|
+
if (!body) return false;
|
|
1135
|
+
return this.backend.cancelCharacterMovement(body);
|
|
1136
|
+
}
|
|
1137
|
+
getPosition(id) {
|
|
1138
|
+
const entry = this.entries.get(id);
|
|
1139
|
+
const pose = entry?.body ? this.backend.bodyPose(entry.body) : null;
|
|
1140
|
+
if (pose) return [...pose.position];
|
|
1141
|
+
if (!entry?.root) return null;
|
|
1142
|
+
entry.root.updateWorldMatrix(true, false);
|
|
1143
|
+
const world = new THREE.Vector3();
|
|
1144
|
+
entry.root.getWorldPosition(world);
|
|
1145
|
+
return world.toArray();
|
|
1146
|
+
}
|
|
1147
|
+
getRotation(id) {
|
|
1148
|
+
const entry = this.entries.get(id);
|
|
1149
|
+
const pose = entry?.body ? this.backend.bodyPose(entry.body) : null;
|
|
1150
|
+
if (pose) return [...pose.rotation];
|
|
1151
|
+
if (!entry?.root) return null;
|
|
1152
|
+
entry.root.updateWorldMatrix(true, false);
|
|
1153
|
+
const world = new THREE.Quaternion();
|
|
1154
|
+
entry.root.getWorldQuaternion(world);
|
|
1155
|
+
return world.toArray();
|
|
1156
|
+
}
|
|
1157
|
+
captureState() {
|
|
1158
|
+
return Array.from(this.entries.keys(), (id) => this.captureObjectState(id));
|
|
1159
|
+
}
|
|
1160
|
+
captureObjectState(id) {
|
|
1161
|
+
const entry = this.entries.get(id);
|
|
1162
|
+
if (!entry) return null;
|
|
1163
|
+
const capture = (body, node) => ({
|
|
1164
|
+
position: node.position.toArray(),
|
|
1165
|
+
rotation: node.quaternion.toArray(),
|
|
1166
|
+
...body ? {
|
|
1167
|
+
type: this.backend.bodyType(body),
|
|
1168
|
+
pose: this.backend.bodyPose(body),
|
|
1169
|
+
motion: this.backend.bodyMotion(body),
|
|
1170
|
+
...this.backend.bodyType(body) === "kinematic" ? { nextPose: this.backend.bodyPose(body, { next: true }) } : {}
|
|
1171
|
+
} : {}
|
|
1172
|
+
});
|
|
1173
|
+
return {
|
|
1174
|
+
id,
|
|
1175
|
+
root: capture(entry.body, entry.root),
|
|
1176
|
+
held: entry.held === true,
|
|
1177
|
+
heldOriginalType: entry.heldOriginalType ?? null,
|
|
1178
|
+
parts: [...entry.parts].map(([name, part]) => ({
|
|
1179
|
+
name,
|
|
1180
|
+
...capture(part.body, part.node),
|
|
1181
|
+
motorTarget: part.motorTarget ?? null
|
|
1182
|
+
}))
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
captureArticulationPose(id) {
|
|
1186
|
+
const entry = this.entries.get(id);
|
|
1187
|
+
return entry ? [...entry.parts].map(([partName, part]) => ({
|
|
1188
|
+
partName,
|
|
1189
|
+
position: part.node.position.toArray(),
|
|
1190
|
+
quaternion: part.node.quaternion.toArray(),
|
|
1191
|
+
motorTarget: part.motorTarget ?? null
|
|
1192
|
+
})) : [];
|
|
1193
|
+
}
|
|
1194
|
+
restoreArticulationPose(id, poses) {
|
|
1195
|
+
const entry = this.entries.get(id);
|
|
1196
|
+
if (!entry) throw new Error("ARTICULATION_PHYSICS_UNAVAILABLE");
|
|
1197
|
+
const checkpoint = this.captureObjectState(id);
|
|
1198
|
+
for (const pose of poses) {
|
|
1199
|
+
const part = entry.parts.get(pose.partName);
|
|
1200
|
+
if (!part) throw new Error("ARTICULATION_PART_NOT_FOUND");
|
|
1201
|
+
const limits = part.spec.joint.limits;
|
|
1202
|
+
if (pose.motorTarget != null && limits && (pose.motorTarget < limits[0] || pose.motorTarget > limits[1])) throw new Error("JOINT_LIMIT_EXCEEDED");
|
|
1203
|
+
part.node.position.fromArray(pose.position);
|
|
1204
|
+
part.node.quaternion.fromArray(pose.quaternion);
|
|
1205
|
+
}
|
|
1206
|
+
entry.root.updateMatrixWorld(true);
|
|
1207
|
+
for (const pose of poses) {
|
|
1208
|
+
const part = entry.parts.get(pose.partName), value = checkpoint.parts.find((item) => item.name === pose.partName);
|
|
1209
|
+
value.position = pose.position;
|
|
1210
|
+
value.rotation = pose.quaternion;
|
|
1211
|
+
value.motorTarget = pose.motorTarget;
|
|
1212
|
+
if (value.pose) value.pose = {
|
|
1213
|
+
position: part.node.getWorldPosition(new THREE.Vector3()).toArray(),
|
|
1214
|
+
rotation: part.node.getWorldQuaternion(new THREE.Quaternion()).toArray()
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
this.restoreObjectState(id, checkpoint);
|
|
1218
|
+
}
|
|
1219
|
+
restoreState(state) {
|
|
1220
|
+
for (const item of state) this.restoreObjectState(item.id, item);
|
|
1221
|
+
}
|
|
1222
|
+
restoreObjectState(id, item) {
|
|
1223
|
+
if (id !== item.id) throw new Error("Physics checkpoint id mismatch");
|
|
1224
|
+
const restore = (body, node, value) => {
|
|
1225
|
+
node.position.fromArray(value.position);
|
|
1226
|
+
node.quaternion.fromArray(value.rotation);
|
|
1227
|
+
node.updateMatrixWorld(true);
|
|
1228
|
+
if (!body) return;
|
|
1229
|
+
if (this.backend.bodyType(body) !== value.type) this.backend.setBodyType(body, value.type);
|
|
1230
|
+
this.backend.setBodyPose(body, {
|
|
1231
|
+
...value.pose,
|
|
1232
|
+
wake: false
|
|
1233
|
+
});
|
|
1234
|
+
if (value.nextPose) this.backend.setBodyPose(body, {
|
|
1235
|
+
...value.nextPose,
|
|
1236
|
+
next: true,
|
|
1237
|
+
wake: false
|
|
1238
|
+
});
|
|
1239
|
+
if (value.motion && value.type === "dynamic") {
|
|
1240
|
+
this.backend.setBodyMotion(body, {
|
|
1241
|
+
...value.motion,
|
|
1242
|
+
wake: false
|
|
1243
|
+
});
|
|
1244
|
+
if (value.motion.sleeping) this.backend.sleepBody(body);
|
|
1245
|
+
else this.backend.wakeBody(body);
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
const entry = this.entries.get(item.id);
|
|
1249
|
+
if (!entry) throw new Error(`Physics checkpoint object missing: ${item.id}`);
|
|
1250
|
+
entry.held = item.held;
|
|
1251
|
+
if (item.heldOriginalType == null) delete entry.heldOriginalType;
|
|
1252
|
+
else entry.heldOriginalType = item.heldOriginalType;
|
|
1253
|
+
restore(entry.body, entry.root, item.root);
|
|
1254
|
+
entry.root.getWorldPosition(entry.lastPosition);
|
|
1255
|
+
entry.root.getWorldQuaternion(entry.lastRotation);
|
|
1256
|
+
for (const value of item.parts) {
|
|
1257
|
+
const part = entry.parts.get(value.name);
|
|
1258
|
+
if (!part) throw new Error(`Physics checkpoint part missing: ${item.id}:${value.name}`);
|
|
1259
|
+
if (part.joint && part.spec.joint.type !== "fixed") this.backend.setJointTarget(part.joint, value.motorTarget, part.spec.joint.motor || {});
|
|
1260
|
+
part.motorTarget = value.motorTarget;
|
|
1261
|
+
restore(part.body, part.node, value);
|
|
1262
|
+
part.lastLocalPosition.copy(part.node.position);
|
|
1263
|
+
part.lastLocalRotation.copy(part.node.quaternion);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
getMotion(id, { partName = ROOT_PART } = {}) {
|
|
1267
|
+
const entry = this.entries.get(id);
|
|
1268
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1269
|
+
if (!entry) return null;
|
|
1270
|
+
if (!body) return {
|
|
1271
|
+
sleeping: true,
|
|
1272
|
+
linearVelocity: [
|
|
1273
|
+
0,
|
|
1274
|
+
0,
|
|
1275
|
+
0
|
|
1276
|
+
],
|
|
1277
|
+
angularVelocity: [
|
|
1278
|
+
0,
|
|
1279
|
+
0,
|
|
1280
|
+
0
|
|
1281
|
+
],
|
|
1282
|
+
linearSpeed: 0,
|
|
1283
|
+
angularSpeed: 0,
|
|
1284
|
+
source: "transform-state"
|
|
1285
|
+
};
|
|
1286
|
+
return this.backend.bodyMotion(body);
|
|
1287
|
+
}
|
|
1288
|
+
/** Solver-space bounds for registered native or managed root colliders. */
|
|
1289
|
+
manipulationBounds(id, partName = ROOT_PART) {
|
|
1290
|
+
const entry = this.entries.get(id), body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1291
|
+
if (!body) return null;
|
|
1292
|
+
const bounds = new THREE.Box3();
|
|
1293
|
+
for (const collider of this.backend.colliders(body)) {
|
|
1294
|
+
const { shape, position, rotation } = this.backend.colliderSnapshot(collider);
|
|
1295
|
+
let half, center = array3$1(position);
|
|
1296
|
+
if (shape.kind === "box") half = boxAabbHalfExtents(shape.halfExtents, rotation);
|
|
1297
|
+
else if (shape.kind === "cylinder") half = cylinderAabbHalfExtents(shape.radius, shape.halfHeight, rotation);
|
|
1298
|
+
else if (shape.kind === "capsule") half = capsuleAabbHalfExtents(shape.radius, shape.halfHeight, rotation);
|
|
1299
|
+
else if (shape.kind === "convexHull" && shape.vertices?.length) {
|
|
1300
|
+
const box = convexAabb(shape.vertices, new THREE.Vector3(...center), rotation);
|
|
1301
|
+
center = box.position;
|
|
1302
|
+
half = box.halfExtents;
|
|
1303
|
+
} else return null;
|
|
1304
|
+
if (![...center, ...half].every(Number.isFinite)) return null;
|
|
1305
|
+
bounds.expandByPoint(new THREE.Vector3(...center.map((n, i) => n - half[i])));
|
|
1306
|
+
bounds.expandByPoint(new THREE.Vector3(...center.map((n, i) => n + half[i])));
|
|
1307
|
+
}
|
|
1308
|
+
if (bounds.isEmpty()) return null;
|
|
1309
|
+
return {
|
|
1310
|
+
id,
|
|
1311
|
+
min: bounds.min.toArray(),
|
|
1312
|
+
max: bounds.max.toArray(),
|
|
1313
|
+
center: bounds.getCenter(new THREE.Vector3()).toArray(),
|
|
1314
|
+
size: bounds.getSize(new THREE.Vector3()).toArray()
|
|
1315
|
+
};
|
|
1316
|
+
}
|
|
1317
|
+
manipulationBody(id, partName = ROOT_PART, { contacts = false } = {}) {
|
|
1318
|
+
const entry = this.entries.get(id);
|
|
1319
|
+
const part = partName === "$root" ? null : entry?.parts.get(partName);
|
|
1320
|
+
const body = part ? part.body : partName === "$root" ? entry?.body : null;
|
|
1321
|
+
const spec = part ? part.spec.physics : entry?.rootSpec;
|
|
1322
|
+
const colliders = body ? this.backend.colliders(body) : [];
|
|
1323
|
+
const contactCandidates = (contacts ? colliders : []).map((collider) => {
|
|
1324
|
+
const snapshot = this.backend.colliderSnapshot(collider), shape = snapshot.shape;
|
|
1325
|
+
let vertexRadius = 0;
|
|
1326
|
+
if (shape.vertices) for (let i = 0; i < shape.vertices.length; i += 3) vertexRadius = Math.max(vertexRadius, Math.hypot(shape.vertices[i], shape.vertices[i + 1], shape.vertices[i + 2]));
|
|
1327
|
+
const radius = shape.kind === "box" ? Math.hypot(...array3$1(shape.halfExtents)) : shape.kind === "cylinder" ? Math.hypot(shape.radius, shape.halfHeight) : shape.kind === "capsule" ? shape.radius + shape.halfHeight : shape.vertices ? vertexRadius : null;
|
|
1328
|
+
return {
|
|
1329
|
+
center: array3$1(snapshot.position),
|
|
1330
|
+
radius
|
|
1331
|
+
};
|
|
1332
|
+
}).filter((candidate) => Number.isFinite(candidate.radius) && candidate.radius > 0);
|
|
1333
|
+
return {
|
|
1334
|
+
registered: Boolean(body),
|
|
1335
|
+
body: body ? this.backend.bodyType(body) : null,
|
|
1336
|
+
joint: Boolean(part?.joint),
|
|
1337
|
+
mass: spec?.mass ?? null,
|
|
1338
|
+
colliderCount: colliders.length,
|
|
1339
|
+
contactCandidates,
|
|
1340
|
+
lockTranslation: spec?.lockTranslation || [
|
|
1341
|
+
false,
|
|
1342
|
+
false,
|
|
1343
|
+
false
|
|
1344
|
+
],
|
|
1345
|
+
lockRotation: spec?.lockRotation || [
|
|
1346
|
+
false,
|
|
1347
|
+
false,
|
|
1348
|
+
false
|
|
1349
|
+
]
|
|
1350
|
+
};
|
|
1351
|
+
}
|
|
1352
|
+
setMotion(id, motion = {}, { partName = ROOT_PART } = {}) {
|
|
1353
|
+
const entry = this.entries.get(id);
|
|
1354
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1355
|
+
return body ? this.backend.setBodyMotion(body, motion) : false;
|
|
1356
|
+
}
|
|
1357
|
+
applyImpulse(id, impulse, { partName = ROOT_PART, point = null, wake = true } = {}) {
|
|
1358
|
+
const entry = this.entries.get(id);
|
|
1359
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1360
|
+
return body ? this.backend.applyImpulse(body, impulse, {
|
|
1361
|
+
point,
|
|
1362
|
+
wake
|
|
1363
|
+
}) : false;
|
|
1364
|
+
}
|
|
1365
|
+
applyForce(id, force, { partName = ROOT_PART, point = null, wake = true } = {}) {
|
|
1366
|
+
const entry = this.entries.get(id);
|
|
1367
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1368
|
+
return body ? this.backend.applyForce(body, force, {
|
|
1369
|
+
point,
|
|
1370
|
+
wake
|
|
1371
|
+
}) : false;
|
|
1372
|
+
}
|
|
1373
|
+
applyTorque(id, torque, { partName = ROOT_PART, wake = true } = {}) {
|
|
1374
|
+
const entry = this.entries.get(id);
|
|
1375
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1376
|
+
return body ? this.backend.applyTorque(body, torque, { wake }) : false;
|
|
1377
|
+
}
|
|
1378
|
+
setMaterial(id, material = {}, { partName = ROOT_PART } = {}) {
|
|
1379
|
+
const entry = this.entries.get(id);
|
|
1380
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1381
|
+
return body ? this.backend.setBodyMaterial(body, material) : false;
|
|
1382
|
+
}
|
|
1383
|
+
setDynamics(id, dynamics = {}, { partName = ROOT_PART } = {}) {
|
|
1384
|
+
const entry = this.entries.get(id);
|
|
1385
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1386
|
+
return body ? this.backend.setBodyDynamics(body, dynamics) : false;
|
|
1387
|
+
}
|
|
1388
|
+
setCcd(id, enabled, { partName = ROOT_PART } = {}) {
|
|
1389
|
+
const entry = this.entries.get(id);
|
|
1390
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1391
|
+
return body ? this.backend.setBodyCcd(body, Boolean(enabled)) : false;
|
|
1392
|
+
}
|
|
1393
|
+
sleep(id, { partName = ROOT_PART } = {}) {
|
|
1394
|
+
const entry = this.entries.get(id);
|
|
1395
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1396
|
+
return body ? this.backend.sleepBody(body) : false;
|
|
1397
|
+
}
|
|
1398
|
+
wake(id, { partName = ROOT_PART } = {}) {
|
|
1399
|
+
const entry = this.entries.get(id);
|
|
1400
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1401
|
+
return body ? this.backend.wakeBody(body) : false;
|
|
1402
|
+
}
|
|
1403
|
+
setSensor(id, enabled, { partName = ROOT_PART } = {}) {
|
|
1404
|
+
const entry = this.entries.get(id);
|
|
1405
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1406
|
+
if (!body) return false;
|
|
1407
|
+
const ok = this.backend.setBodySensor(body, Boolean(enabled));
|
|
1408
|
+
if (ok) for (const collider of this.backend.colliders(body)) {
|
|
1409
|
+
const key = this.backend.colliderKey(collider);
|
|
1410
|
+
const runtime = this.colliderRuntime.get(key) || {
|
|
1411
|
+
sensor: false,
|
|
1412
|
+
collisionEvents: false,
|
|
1413
|
+
collisionFilter: null
|
|
1414
|
+
};
|
|
1415
|
+
this.colliderRuntime.set(key, {
|
|
1416
|
+
...runtime,
|
|
1417
|
+
sensor: Boolean(enabled)
|
|
1418
|
+
});
|
|
1419
|
+
}
|
|
1420
|
+
return ok;
|
|
1421
|
+
}
|
|
1422
|
+
setCollisionFilter(id, collision, { partName = ROOT_PART } = {}) {
|
|
1423
|
+
const entry = this.entries.get(id);
|
|
1424
|
+
const body = partName === "$root" ? entry?.body : entry?.parts.get(partName)?.body;
|
|
1425
|
+
if (!body) return false;
|
|
1426
|
+
const collisionFilter = this._compileCollisionFilter(collision);
|
|
1427
|
+
const ok = this.backend.setBodyCollisionFilter(body, collisionFilter);
|
|
1428
|
+
if (ok) for (const collider of this.backend.colliders(body)) {
|
|
1429
|
+
const key = this.backend.colliderKey(collider);
|
|
1430
|
+
const runtime = this.colliderRuntime.get(key) || {
|
|
1431
|
+
sensor: false,
|
|
1432
|
+
collisionEvents: false,
|
|
1433
|
+
collisionFilter: null
|
|
1434
|
+
};
|
|
1435
|
+
this.colliderRuntime.set(key, {
|
|
1436
|
+
...runtime,
|
|
1437
|
+
collisionFilter
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
return ok;
|
|
1441
|
+
}
|
|
1442
|
+
getPartRestPose(id, partName) {
|
|
1443
|
+
const part = this.entries.get(id)?.parts.get(partName);
|
|
1444
|
+
if (!part) return null;
|
|
1445
|
+
return {
|
|
1446
|
+
position: part.restLocalPosition.toArray(),
|
|
1447
|
+
rotation: part.restLocalRotation.toArray()
|
|
1448
|
+
};
|
|
1449
|
+
}
|
|
1450
|
+
getArticulationState(id, partName, { target = null } = {}) {
|
|
1451
|
+
const entry = this.entries.get(id);
|
|
1452
|
+
const part = entry?.parts.get(partName);
|
|
1453
|
+
if (!entry || !part?.node?.parent || part.spec?.joint?.type === "fixed") return null;
|
|
1454
|
+
const parentFrame = part.parentName === "$root" ? entry.root : entry.parts.get(part.parentName)?.node;
|
|
1455
|
+
if (!parentFrame) return null;
|
|
1456
|
+
entry.root.updateWorldMatrix(true, true);
|
|
1457
|
+
const parentWorldRotation = new THREE.Quaternion();
|
|
1458
|
+
const nodeParentWorldRotation = new THREE.Quaternion();
|
|
1459
|
+
parentFrame.getWorldQuaternion(parentWorldRotation);
|
|
1460
|
+
part.node.parent.getWorldQuaternion(nodeParentWorldRotation);
|
|
1461
|
+
if (entry.nativeArticulation) return this.#nativeArticulationCoordinate(entry, part, {
|
|
1462
|
+
id,
|
|
1463
|
+
partName,
|
|
1464
|
+
target
|
|
1465
|
+
});
|
|
1466
|
+
const axis = new THREE.Vector3(...part.spec.joint.axis || [
|
|
1467
|
+
1,
|
|
1468
|
+
0,
|
|
1469
|
+
0
|
|
1470
|
+
]).normalize().applyQuaternion(parentWorldRotation).applyQuaternion(nodeParentWorldRotation.invert()).normalize();
|
|
1471
|
+
if (!Number.isFinite(axis.lengthSq()) || axis.lengthSq() < .99) return null;
|
|
1472
|
+
let coordinate;
|
|
1473
|
+
if (part.spec.joint.type === "prismatic") coordinate = part.node.position.clone().sub(part.restLocalPosition).dot(axis);
|
|
1474
|
+
else {
|
|
1475
|
+
const delta = part.node.quaternion.clone().multiply(part.restLocalRotation.clone().invert()).normalize();
|
|
1476
|
+
const angle = 2 * Math.atan2(delta.x * axis.x + delta.y * axis.y + delta.z * axis.z, delta.w);
|
|
1477
|
+
coordinate = Math.atan2(Math.sin(angle), Math.cos(angle));
|
|
1478
|
+
}
|
|
1479
|
+
if (!Number.isFinite(coordinate)) return null;
|
|
1480
|
+
const wrap = (value) => part.spec.joint.type === "revolute" ? Math.atan2(Math.sin(value), Math.cos(value)) : value;
|
|
1481
|
+
const error = Number.isFinite(target) ? Math.abs(wrap(coordinate - target)) : null;
|
|
1482
|
+
return {
|
|
1483
|
+
id,
|
|
1484
|
+
partName,
|
|
1485
|
+
jointType: part.spec.joint.type,
|
|
1486
|
+
coordinate,
|
|
1487
|
+
target: Number.isFinite(target) ? target : null,
|
|
1488
|
+
error,
|
|
1489
|
+
tolerance: part.spec.joint.type === "prismatic" ? .03 : .08,
|
|
1490
|
+
limits: part.spec.joint.limits ? [...part.spec.joint.limits] : null,
|
|
1491
|
+
localAxis: axis.toArray(),
|
|
1492
|
+
coordinateReference: "rest-zero-pose"
|
|
1493
|
+
};
|
|
1494
|
+
}
|
|
1495
|
+
/** Native links retain a direct rigid parent. Read solver poses, not a prior render frame. */
|
|
1496
|
+
#nativeArticulationCoordinate(entry, part, { id, partName, target }) {
|
|
1497
|
+
const parent = part.parentName === "$root" ? entry.body : entry.parts.get(part.parentName)?.body;
|
|
1498
|
+
const parentPose = this.backend.bodyPose(parent), childPose = this.backend.bodyPose(part.body);
|
|
1499
|
+
if (!parentPose || !childPose) return null;
|
|
1500
|
+
const inverse = new THREE.Quaternion(...parentPose.rotation).invert();
|
|
1501
|
+
const axis = new THREE.Vector3(...part.spec.joint.axis).normalize();
|
|
1502
|
+
const localPosition = new THREE.Vector3(...childPose.position).sub(new THREE.Vector3(...parentPose.position)).applyQuaternion(inverse);
|
|
1503
|
+
const localRotation = inverse.multiply(new THREE.Quaternion(...childPose.rotation));
|
|
1504
|
+
const type = part.spec.joint.type;
|
|
1505
|
+
const delta = localRotation.multiply(part.restLocalRotation.clone().invert()).normalize();
|
|
1506
|
+
const wrap = (n) => Math.atan2(Math.sin(n), Math.cos(n));
|
|
1507
|
+
const coordinate = type === "prismatic" ? localPosition.sub(part.restLocalPosition).dot(axis) : wrap(2 * Math.atan2(delta.x * axis.x + delta.y * axis.y + delta.z * axis.z, delta.w));
|
|
1508
|
+
if (!Number.isFinite(coordinate)) return null;
|
|
1509
|
+
return {
|
|
1510
|
+
id,
|
|
1511
|
+
partName,
|
|
1512
|
+
jointType: type,
|
|
1513
|
+
coordinate,
|
|
1514
|
+
target: Number.isFinite(target) ? target : null,
|
|
1515
|
+
error: Number.isFinite(target) ? Math.abs(type === "revolute" ? wrap(coordinate - target) : coordinate - target) : null,
|
|
1516
|
+
tolerance: type === "prismatic" ? .03 : .08,
|
|
1517
|
+
limits: [...part.spec.joint.limits],
|
|
1518
|
+
localAxis: axis.toArray(),
|
|
1519
|
+
coordinateReference: "rest-zero-pose"
|
|
1520
|
+
};
|
|
1521
|
+
}
|
|
1522
|
+
articulationColliderPoses(id, partName, coordinate) {
|
|
1523
|
+
if (!this.backend.hasCapability("collision")) return {
|
|
1524
|
+
checked: false,
|
|
1525
|
+
reason: "PHYSICS_CAPABILITY_UNAVAILABLE",
|
|
1526
|
+
capability: "collision",
|
|
1527
|
+
id,
|
|
1528
|
+
partName,
|
|
1529
|
+
coordinate
|
|
1530
|
+
};
|
|
1531
|
+
const entry = this.entries.get(id);
|
|
1532
|
+
const part = entry?.parts.get(partName);
|
|
1533
|
+
if (!entry || !part?.node?.parent || !Number.isFinite(coordinate)) return {
|
|
1534
|
+
checked: false,
|
|
1535
|
+
reason: "PART_POSE_UNAVAILABLE",
|
|
1536
|
+
id,
|
|
1537
|
+
partName,
|
|
1538
|
+
coordinate
|
|
1539
|
+
};
|
|
1540
|
+
const state = this.getArticulationState(id, partName);
|
|
1541
|
+
if (!state) return {
|
|
1542
|
+
checked: false,
|
|
1543
|
+
reason: "JOINT_COORDINATE_UNAVAILABLE",
|
|
1544
|
+
id,
|
|
1545
|
+
partName,
|
|
1546
|
+
coordinate
|
|
1547
|
+
};
|
|
1548
|
+
const childAnchor = new THREE.Vector3(...part.spec.joint.childAnchor || [
|
|
1549
|
+
0,
|
|
1550
|
+
0,
|
|
1551
|
+
0
|
|
1552
|
+
]);
|
|
1553
|
+
const parentRotation = new THREE.Quaternion();
|
|
1554
|
+
part.node.parent.updateWorldMatrix(true, false);
|
|
1555
|
+
if (entry.nativeArticulation) {
|
|
1556
|
+
const parent = part.parentName === "$root" ? entry.body : entry.parts.get(part.parentName)?.body;
|
|
1557
|
+
const parentPose = this.backend.bodyPose(parent);
|
|
1558
|
+
if (!parentPose) return {
|
|
1559
|
+
checked: false,
|
|
1560
|
+
reason: "JOINT_AXIS_UNAVAILABLE",
|
|
1561
|
+
id,
|
|
1562
|
+
partName,
|
|
1563
|
+
coordinate
|
|
1564
|
+
};
|
|
1565
|
+
parentRotation.fromArray(parentPose.rotation);
|
|
1566
|
+
} else part.node.parent.getWorldQuaternion(parentRotation);
|
|
1567
|
+
const worldAxis = new THREE.Vector3(...state.localAxis).applyQuaternion(parentRotation).normalize();
|
|
1568
|
+
if (!Number.isFinite(worldAxis.lengthSq()) || worldAxis.lengthSq() < .99) return {
|
|
1569
|
+
checked: false,
|
|
1570
|
+
reason: "JOINT_AXIS_UNAVAILABLE",
|
|
1571
|
+
id,
|
|
1572
|
+
partName,
|
|
1573
|
+
coordinate
|
|
1574
|
+
};
|
|
1575
|
+
const bodyPose = this.backend.bodyPose(part.body);
|
|
1576
|
+
const currentBodyPosition = new THREE.Vector3(...bodyPose.position);
|
|
1577
|
+
const currentBodyRotation = new THREE.Quaternion(...bodyPose.rotation).normalize();
|
|
1578
|
+
const delta = coordinate - state.coordinate;
|
|
1579
|
+
const bodyPosition = currentBodyPosition.clone();
|
|
1580
|
+
const bodyRotation = currentBodyRotation.clone();
|
|
1581
|
+
if (part.spec.joint.type === "prismatic") bodyPosition.addScaledVector(worldAxis, delta);
|
|
1582
|
+
else {
|
|
1583
|
+
const pivotWorld = childAnchor.clone().applyQuaternion(currentBodyRotation).add(currentBodyPosition);
|
|
1584
|
+
bodyRotation.premultiply(new THREE.Quaternion().setFromAxisAngle(worldAxis, delta)).normalize();
|
|
1585
|
+
bodyPosition.copy(pivotWorld).sub(childAnchor.clone().applyQuaternion(bodyRotation));
|
|
1586
|
+
}
|
|
1587
|
+
const inverseCurrent = currentBodyRotation.clone().invert();
|
|
1588
|
+
const colliders = [];
|
|
1589
|
+
for (const [i, collider] of this.backend.colliders(part.body).entries()) {
|
|
1590
|
+
const snapshot = this.backend.colliderSnapshot(collider);
|
|
1591
|
+
const currentColliderPosition = new THREE.Vector3(snapshot.position.x, snapshot.position.y, snapshot.position.z);
|
|
1592
|
+
const currentColliderRotation = new THREE.Quaternion(snapshot.rotation.x, snapshot.rotation.y, snapshot.rotation.z, snapshot.rotation.w).normalize();
|
|
1593
|
+
const localPosition = currentColliderPosition.clone().sub(currentBodyPosition).applyQuaternion(inverseCurrent);
|
|
1594
|
+
const localRotation = inverseCurrent.clone().multiply(currentColliderRotation).normalize();
|
|
1595
|
+
const position = localPosition.clone().applyQuaternion(bodyRotation).add(bodyPosition);
|
|
1596
|
+
const rotation = bodyRotation.clone().multiply(localRotation).normalize();
|
|
1597
|
+
colliders.push({
|
|
1598
|
+
index: i,
|
|
1599
|
+
shape: snapshot.shape,
|
|
1600
|
+
shapeRef: snapshot.shapeRef,
|
|
1601
|
+
position,
|
|
1602
|
+
rotation
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
return {
|
|
1606
|
+
checked: true,
|
|
1607
|
+
id,
|
|
1608
|
+
partName,
|
|
1609
|
+
jointType: state.jointType,
|
|
1610
|
+
currentCoordinate: state.coordinate,
|
|
1611
|
+
coordinate,
|
|
1612
|
+
parentName: part.parentName,
|
|
1613
|
+
frameAssumption: "parent-pose-at-query",
|
|
1614
|
+
colliders
|
|
1615
|
+
};
|
|
1616
|
+
}
|
|
1617
|
+
shapeBoundingRadius(shape) {
|
|
1618
|
+
if (!shape) return null;
|
|
1619
|
+
if (shape.halfExtents) {
|
|
1620
|
+
const h = shape.halfExtents;
|
|
1621
|
+
const border = Number(shape.borderRadius) || 0;
|
|
1622
|
+
return Math.hypot(h.x + border, h.y + border, h.z + border);
|
|
1623
|
+
}
|
|
1624
|
+
if (Number.isFinite(shape.halfHeight) && Number.isFinite(shape.radius)) {
|
|
1625
|
+
const border = Number(shape.borderRadius) || 0;
|
|
1626
|
+
return Math.hypot(shape.radius + border, shape.halfHeight + shape.radius + border);
|
|
1627
|
+
}
|
|
1628
|
+
if (Number.isFinite(shape.radius)) return shape.radius;
|
|
1629
|
+
if (shape.vertices?.length) {
|
|
1630
|
+
let radius = 0;
|
|
1631
|
+
for (let i = 0; i + 2 < shape.vertices.length; i += 3) radius = Math.max(radius, Math.hypot(shape.vertices[i], shape.vertices[i + 1], shape.vertices[i + 2]));
|
|
1632
|
+
return radius || null;
|
|
1633
|
+
}
|
|
1634
|
+
if (shape.a && shape.b) return Math.max(Math.hypot(shape.a.x, shape.a.y, shape.a.z), Math.hypot(shape.b.x, shape.b.y, shape.b.z));
|
|
1635
|
+
return null;
|
|
1636
|
+
}
|
|
1637
|
+
articulationCounterfactualSampleCount(id, partName, current, target, { minSamples = 5, maxSamples = 33 } = {}) {
|
|
1638
|
+
const part = this.entries.get(id)?.parts.get(partName);
|
|
1639
|
+
if (!part || !Number.isFinite(current) || !Number.isFinite(target)) return {
|
|
1640
|
+
checked: false,
|
|
1641
|
+
reason: "PART_SAMPLE_GEOMETRY_UNAVAILABLE"
|
|
1642
|
+
};
|
|
1643
|
+
const delta = Math.abs(target - current);
|
|
1644
|
+
if (delta < 1e-9) return {
|
|
1645
|
+
checked: true,
|
|
1646
|
+
count: minSamples,
|
|
1647
|
+
delta,
|
|
1648
|
+
maxTravel: 0,
|
|
1649
|
+
resolution: .08
|
|
1650
|
+
};
|
|
1651
|
+
const bodyPose = this.backend.bodyPose(part.body);
|
|
1652
|
+
const bodyPosition = new THREE.Vector3(...bodyPose.position);
|
|
1653
|
+
const inverseBody = new THREE.Quaternion(...bodyPose.rotation).normalize().clone().invert();
|
|
1654
|
+
const childAnchor = new THREE.Vector3(...part.spec.joint.childAnchor || [
|
|
1655
|
+
0,
|
|
1656
|
+
0,
|
|
1657
|
+
0
|
|
1658
|
+
]);
|
|
1659
|
+
let minRadius = 1e400, maxLever = 0, covered = 0;
|
|
1660
|
+
for (const collider of this.backend.colliders(part.body)) {
|
|
1661
|
+
const snapshot = this.backend.colliderSnapshot(collider);
|
|
1662
|
+
const radius = this.shapeBoundingRadius(snapshot.shape);
|
|
1663
|
+
if (!Number.isFinite(radius) || radius <= 0) continue;
|
|
1664
|
+
const localCenter = new THREE.Vector3(snapshot.position.x, snapshot.position.y, snapshot.position.z).sub(bodyPosition).applyQuaternion(inverseBody);
|
|
1665
|
+
minRadius = Math.min(minRadius, radius);
|
|
1666
|
+
maxLever = Math.max(maxLever, localCenter.distanceTo(childAnchor) + radius);
|
|
1667
|
+
covered += 1;
|
|
1668
|
+
}
|
|
1669
|
+
if (!covered || !Number.isFinite(minRadius)) return {
|
|
1670
|
+
checked: false,
|
|
1671
|
+
reason: "COLLIDER_EXTENT_UNAVAILABLE"
|
|
1672
|
+
};
|
|
1673
|
+
const maxTravel = part.spec.joint.type === "revolute" ? delta * maxLever : delta;
|
|
1674
|
+
const resolution = THREE.MathUtils.clamp(minRadius * .35, .02, .08);
|
|
1675
|
+
return {
|
|
1676
|
+
checked: true,
|
|
1677
|
+
count: THREE.MathUtils.clamp(Math.ceil(maxTravel / resolution) + 1, minSamples, maxSamples),
|
|
1678
|
+
delta,
|
|
1679
|
+
maxTravel,
|
|
1680
|
+
resolution,
|
|
1681
|
+
colliders: covered,
|
|
1682
|
+
minRadius,
|
|
1683
|
+
maxLever: part.spec.joint.type === "revolute" ? maxLever : null
|
|
1684
|
+
};
|
|
1685
|
+
}
|
|
1686
|
+
articulationPairCounterfactual(originalId, originalPartName, originalTarget, blockerId, blockerPartName, blockerTarget, { samples = null } = {}) {
|
|
1687
|
+
const originalState = this.getArticulationState(originalId, originalPartName, { target: originalTarget });
|
|
1688
|
+
const blockerState = this.getArticulationState(blockerId, blockerPartName, { target: blockerTarget });
|
|
1689
|
+
if (!originalState || !blockerState) return {
|
|
1690
|
+
checked: false,
|
|
1691
|
+
reason: "JOINT_COORDINATE_UNAVAILABLE"
|
|
1692
|
+
};
|
|
1693
|
+
if (!Number.isFinite(originalTarget) || !Number.isFinite(blockerTarget)) return {
|
|
1694
|
+
checked: false,
|
|
1695
|
+
reason: "TARGET_UNAVAILABLE"
|
|
1696
|
+
};
|
|
1697
|
+
const fixedSamples = Number.isFinite(samples) ? Math.max(2, Math.min(33, Math.trunc(samples))) : null;
|
|
1698
|
+
const pairedSamples = samples && typeof samples === "object" ? {
|
|
1699
|
+
original: Math.max(2, Math.min(33, Math.trunc(samples.original) || 2)),
|
|
1700
|
+
blocker: Math.max(2, Math.min(33, Math.trunc(samples.blocker) || 2))
|
|
1701
|
+
} : null;
|
|
1702
|
+
const originalSampling = fixedSamples ? {
|
|
1703
|
+
checked: true,
|
|
1704
|
+
count: fixedSamples,
|
|
1705
|
+
mode: "fixed"
|
|
1706
|
+
} : pairedSamples ? {
|
|
1707
|
+
checked: true,
|
|
1708
|
+
count: pairedSamples.original,
|
|
1709
|
+
mode: "fixed-pair"
|
|
1710
|
+
} : this.articulationCounterfactualSampleCount(originalId, originalPartName, originalState.coordinate, originalTarget);
|
|
1711
|
+
const blockerSampling = fixedSamples ? {
|
|
1712
|
+
checked: true,
|
|
1713
|
+
count: fixedSamples,
|
|
1714
|
+
mode: "fixed"
|
|
1715
|
+
} : pairedSamples ? {
|
|
1716
|
+
checked: true,
|
|
1717
|
+
count: pairedSamples.blocker,
|
|
1718
|
+
mode: "fixed-pair"
|
|
1719
|
+
} : this.articulationCounterfactualSampleCount(blockerId, blockerPartName, blockerState.coordinate, blockerTarget);
|
|
1720
|
+
if (!originalSampling.checked) return {
|
|
1721
|
+
checked: false,
|
|
1722
|
+
reason: originalSampling.reason,
|
|
1723
|
+
source: "original-sampling"
|
|
1724
|
+
};
|
|
1725
|
+
if (!blockerSampling.checked) return {
|
|
1726
|
+
checked: false,
|
|
1727
|
+
reason: blockerSampling.reason,
|
|
1728
|
+
source: "blocker-sampling"
|
|
1729
|
+
};
|
|
1730
|
+
const trajectory = (id, partName, current, target, count) => {
|
|
1731
|
+
const poses = [];
|
|
1732
|
+
for (let i = 0; i < count; i++) {
|
|
1733
|
+
const alpha = i / (count - 1);
|
|
1734
|
+
const coordinate = current + (target - current) * alpha;
|
|
1735
|
+
const pose = this.articulationColliderPoses(id, partName, coordinate);
|
|
1736
|
+
if (!pose.checked) return pose;
|
|
1737
|
+
poses.push(pose);
|
|
1738
|
+
}
|
|
1739
|
+
return {
|
|
1740
|
+
checked: true,
|
|
1741
|
+
poses
|
|
1742
|
+
};
|
|
1743
|
+
};
|
|
1744
|
+
const original = trajectory(originalId, originalPartName, originalState.coordinate, originalTarget, originalSampling.count);
|
|
1745
|
+
if (!original.checked) return {
|
|
1746
|
+
checked: false,
|
|
1747
|
+
reason: original.reason,
|
|
1748
|
+
source: "original"
|
|
1749
|
+
};
|
|
1750
|
+
const blocker = trajectory(blockerId, blockerPartName, blockerState.coordinate, blockerTarget, blockerSampling.count);
|
|
1751
|
+
if (!blocker.checked) return {
|
|
1752
|
+
checked: false,
|
|
1753
|
+
reason: blocker.reason,
|
|
1754
|
+
source: "blocker"
|
|
1755
|
+
};
|
|
1756
|
+
const pairAt = (a, b) => {
|
|
1757
|
+
let intersections = 0;
|
|
1758
|
+
for (const left of a.colliders) for (const right of b.colliders) if (this.backend.shapesIntersect(left.shapeRef, left.position.toArray(), left.rotation, right.shapeRef, right.position.toArray(), right.rotation)) intersections += 1;
|
|
1759
|
+
return intersections;
|
|
1760
|
+
};
|
|
1761
|
+
const againstPose = (originalPoses, blockerPose) => {
|
|
1762
|
+
let conflictSamples = 0, pairIntersections = 0;
|
|
1763
|
+
for (const originalPose of originalPoses) {
|
|
1764
|
+
const pairs = pairAt(originalPose, blockerPose);
|
|
1765
|
+
if (pairs) conflictSamples += 1;
|
|
1766
|
+
pairIntersections += pairs;
|
|
1767
|
+
}
|
|
1768
|
+
return {
|
|
1769
|
+
conflictSamples,
|
|
1770
|
+
pairIntersections
|
|
1771
|
+
};
|
|
1772
|
+
};
|
|
1773
|
+
const trajectoryConflict = (originalPoses, blockerPoses) => {
|
|
1774
|
+
let conflictSamplePairs = 0, pairIntersections = 0;
|
|
1775
|
+
for (const originalPose of originalPoses) for (const blockerPose of blockerPoses) {
|
|
1776
|
+
const pairs = pairAt(originalPose, blockerPose);
|
|
1777
|
+
if (pairs) conflictSamplePairs += 1;
|
|
1778
|
+
pairIntersections += pairs;
|
|
1779
|
+
}
|
|
1780
|
+
return {
|
|
1781
|
+
conflictSamplePairs,
|
|
1782
|
+
pairIntersections
|
|
1783
|
+
};
|
|
1784
|
+
};
|
|
1785
|
+
const current = againstPose(original.poses, blocker.poses[0]);
|
|
1786
|
+
const target = againstPose(original.poses, blocker.poses.at(-1));
|
|
1787
|
+
const action = trajectoryConflict(original.poses, blocker.poses);
|
|
1788
|
+
return {
|
|
1789
|
+
checked: true,
|
|
1790
|
+
geometry: this.backend.evidenceGeometry("shape-pairs"),
|
|
1791
|
+
causal: false,
|
|
1792
|
+
frameAssumption: "parent-poses-static-during-hypothesis",
|
|
1793
|
+
samples: {
|
|
1794
|
+
original: originalSampling.count,
|
|
1795
|
+
blocker: blockerSampling.count,
|
|
1796
|
+
mode: fixedSamples ? "fixed" : pairedSamples ? "fixed-pair" : "adaptive"
|
|
1797
|
+
},
|
|
1798
|
+
sampling: {
|
|
1799
|
+
original: originalSampling,
|
|
1800
|
+
blocker: blockerSampling
|
|
1801
|
+
},
|
|
1802
|
+
original: {
|
|
1803
|
+
id: originalId,
|
|
1804
|
+
partName: originalPartName,
|
|
1805
|
+
currentCoordinate: originalState.coordinate,
|
|
1806
|
+
target: originalTarget
|
|
1807
|
+
},
|
|
1808
|
+
blocker: {
|
|
1809
|
+
id: blockerId,
|
|
1810
|
+
partName: blockerPartName,
|
|
1811
|
+
currentCoordinate: blockerState.coordinate,
|
|
1812
|
+
target: blockerTarget
|
|
1813
|
+
},
|
|
1814
|
+
current,
|
|
1815
|
+
target,
|
|
1816
|
+
action,
|
|
1817
|
+
targetSweepClear: target.conflictSamples === 0,
|
|
1818
|
+
conflictReduction: Math.max(0, current.conflictSamples - target.conflictSamples)
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
articulationWorldCounterfactual(id, partName, target, { excludeObjectIds = [], excludeParts = [], samples = null } = {}) {
|
|
1822
|
+
const state = this.getArticulationState(id, partName, { target });
|
|
1823
|
+
if (!state || !Number.isFinite(target)) return {
|
|
1824
|
+
checked: false,
|
|
1825
|
+
reason: "JOINT_COORDINATE_UNAVAILABLE",
|
|
1826
|
+
id,
|
|
1827
|
+
partName
|
|
1828
|
+
};
|
|
1829
|
+
const fixedSamples = Number.isFinite(samples) ? Math.max(2, Math.min(33, Math.trunc(samples))) : null;
|
|
1830
|
+
const sampling = fixedSamples ? {
|
|
1831
|
+
checked: true,
|
|
1832
|
+
count: fixedSamples,
|
|
1833
|
+
mode: "fixed"
|
|
1834
|
+
} : this.articulationCounterfactualSampleCount(id, partName, state.coordinate, target);
|
|
1835
|
+
if (!sampling.checked) return {
|
|
1836
|
+
checked: false,
|
|
1837
|
+
reason: sampling.reason,
|
|
1838
|
+
source: "sampling",
|
|
1839
|
+
id,
|
|
1840
|
+
partName
|
|
1841
|
+
};
|
|
1842
|
+
const excluded = /* @__PURE__ */ new Set([id, ...excludeObjectIds]);
|
|
1843
|
+
const excludedParts = new Set(excludeParts.map((item) => `${item.objectId}:${item.partName || "$root"}`));
|
|
1844
|
+
const keyOf = (provenance, collider) => {
|
|
1845
|
+
const colliderKey = this.backend.colliderKey(collider);
|
|
1846
|
+
if (provenance?.kind === "environment") return `environment:${provenance.environmentId || "$environment"}:${provenance.colliderIndex ?? colliderKey}`;
|
|
1847
|
+
if (provenance?.kind === "object") return `object:${provenance.objectId}:${provenance.partName || "$root"}:${provenance.colliderIndex ?? colliderKey}`;
|
|
1848
|
+
return `unknown:${colliderKey}`;
|
|
1849
|
+
};
|
|
1850
|
+
const describe = (provenance, collider, key) => provenance?.kind === "environment" ? {
|
|
1851
|
+
key,
|
|
1852
|
+
kind: "environment",
|
|
1853
|
+
environmentId: provenance.environmentId || "$environment",
|
|
1854
|
+
colliderIndex: provenance.colliderIndex ?? null
|
|
1855
|
+
} : provenance?.kind === "object" ? {
|
|
1856
|
+
key,
|
|
1857
|
+
kind: "object",
|
|
1858
|
+
objectId: provenance.objectId,
|
|
1859
|
+
partName: provenance.partName || "$root",
|
|
1860
|
+
colliderIndex: provenance.colliderIndex ?? null
|
|
1861
|
+
} : {
|
|
1862
|
+
key,
|
|
1863
|
+
kind: "unknown",
|
|
1864
|
+
colliderHandle: this.backend.colliderKey(collider)
|
|
1865
|
+
};
|
|
1866
|
+
const poseHits = (pose) => {
|
|
1867
|
+
const hits = /* @__PURE__ */ new Map();
|
|
1868
|
+
for (const source of pose.colliders) this.backend.intersectionsWithShape(this.world, source.position.toArray(), source.rotation, source.shapeRef, (other) => {
|
|
1869
|
+
const provenance = this.provenanceOfCollider(other);
|
|
1870
|
+
if (provenance?.kind === "object" && (excluded.has(provenance.objectId) || excludedParts.has(`${provenance.objectId}:${provenance.partName || "$root"}`))) return true;
|
|
1871
|
+
const key = keyOf(provenance, other);
|
|
1872
|
+
if (!hits.has(key)) hits.set(key, describe(provenance, other, key));
|
|
1873
|
+
return true;
|
|
1874
|
+
});
|
|
1875
|
+
return hits;
|
|
1876
|
+
};
|
|
1877
|
+
this.backend.syncSceneQueries(this.world);
|
|
1878
|
+
const poses = [];
|
|
1879
|
+
for (let i = 0; i < sampling.count; i++) {
|
|
1880
|
+
const alpha = i / (sampling.count - 1);
|
|
1881
|
+
const coordinate = state.coordinate + (target - state.coordinate) * alpha;
|
|
1882
|
+
const pose = this.articulationColliderPoses(id, partName, coordinate);
|
|
1883
|
+
if (!pose.checked) return {
|
|
1884
|
+
checked: false,
|
|
1885
|
+
reason: pose.reason,
|
|
1886
|
+
source: "trajectory",
|
|
1887
|
+
id,
|
|
1888
|
+
partName
|
|
1889
|
+
};
|
|
1890
|
+
poses.push(pose);
|
|
1891
|
+
}
|
|
1892
|
+
const currentHits = poseHits(poses[0]);
|
|
1893
|
+
const targetHits = poseHits(poses.at(-1));
|
|
1894
|
+
const actionHits = /* @__PURE__ */ new Map();
|
|
1895
|
+
const sampleConflicts = [];
|
|
1896
|
+
for (let sampleIndex = 0; sampleIndex < poses.length; sampleIndex++) {
|
|
1897
|
+
const hits = poseHits(poses[sampleIndex]);
|
|
1898
|
+
if (hits.size) sampleConflicts.push({
|
|
1899
|
+
sampleIndex,
|
|
1900
|
+
coordinate: poses[sampleIndex].coordinate,
|
|
1901
|
+
blockers: [...hits.keys()]
|
|
1902
|
+
});
|
|
1903
|
+
for (const [key, value] of hits) if (!actionHits.has(key)) actionHits.set(key, value);
|
|
1904
|
+
}
|
|
1905
|
+
const introduced = (hits) => [...hits.entries()].filter(([key]) => !currentHits.has(key)).map(([, value]) => value);
|
|
1906
|
+
const current = [...currentHits.values()];
|
|
1907
|
+
const targetAll = [...targetHits.values()];
|
|
1908
|
+
const actionAll = [...actionHits.values()];
|
|
1909
|
+
const introducedTarget = introduced(targetHits);
|
|
1910
|
+
const introducedAction = introduced(actionHits);
|
|
1911
|
+
return {
|
|
1912
|
+
checked: true,
|
|
1913
|
+
geometry: this.backend.evidenceGeometry("world-shape-query"),
|
|
1914
|
+
causal: false,
|
|
1915
|
+
frameAssumption: "other-world-colliders-static-during-hypothesis",
|
|
1916
|
+
id,
|
|
1917
|
+
partName,
|
|
1918
|
+
currentCoordinate: state.coordinate,
|
|
1919
|
+
target,
|
|
1920
|
+
excludedObjectIds: [...excluded].sort(),
|
|
1921
|
+
excludedParts: [...excludedParts].sort(),
|
|
1922
|
+
samples: {
|
|
1923
|
+
count: sampling.count,
|
|
1924
|
+
mode: fixedSamples ? "fixed" : "adaptive"
|
|
1925
|
+
},
|
|
1926
|
+
sampling,
|
|
1927
|
+
current: { blockers: current },
|
|
1928
|
+
targetPose: {
|
|
1929
|
+
blockers: targetAll,
|
|
1930
|
+
introducedBlockers: introducedTarget
|
|
1931
|
+
},
|
|
1932
|
+
actionEnvelope: {
|
|
1933
|
+
blockers: actionAll,
|
|
1934
|
+
introducedBlockers: introducedAction,
|
|
1935
|
+
sampleConflicts
|
|
1936
|
+
},
|
|
1937
|
+
targetIntroducesNoCollision: introducedTarget.length === 0,
|
|
1938
|
+
actionIntroducesNoCollision: introducedAction.length === 0
|
|
1939
|
+
};
|
|
1940
|
+
}
|
|
1941
|
+
articulationPairCounterfactualConvergence(originalId, originalPartName, originalTarget, blockerId, blockerPartName, blockerTarget, { multiplier = 2 } = {}) {
|
|
1942
|
+
const base = this.articulationPairCounterfactual(originalId, originalPartName, originalTarget, blockerId, blockerPartName, blockerTarget);
|
|
1943
|
+
if (!base.checked) return {
|
|
1944
|
+
checked: false,
|
|
1945
|
+
reason: base.reason || "BASE_COUNTERFACTUAL_UNAVAILABLE",
|
|
1946
|
+
base
|
|
1947
|
+
};
|
|
1948
|
+
const denserCount = (count) => Math.min(33, Math.max(count + 2, Math.ceil(count * Math.max(1.25, multiplier))));
|
|
1949
|
+
const denseSamples = {
|
|
1950
|
+
original: denserCount(base.samples.original),
|
|
1951
|
+
blocker: denserCount(base.samples.blocker)
|
|
1952
|
+
};
|
|
1953
|
+
const dense = this.articulationPairCounterfactual(originalId, originalPartName, originalTarget, blockerId, blockerPartName, blockerTarget, { samples: denseSamples });
|
|
1954
|
+
if (!dense.checked) return {
|
|
1955
|
+
checked: false,
|
|
1956
|
+
reason: dense.reason || "DENSE_COUNTERFACTUAL_UNAVAILABLE",
|
|
1957
|
+
base,
|
|
1958
|
+
dense
|
|
1959
|
+
};
|
|
1960
|
+
const ratio = (value, total) => total > 0 ? value / total : 0;
|
|
1961
|
+
const baseRatios = {
|
|
1962
|
+
current: ratio(base.current.conflictSamples, base.samples.original),
|
|
1963
|
+
target: ratio(base.target.conflictSamples, base.samples.original),
|
|
1964
|
+
action: ratio(base.action.conflictSamplePairs, base.samples.original * base.samples.blocker)
|
|
1965
|
+
};
|
|
1966
|
+
const denseRatios = {
|
|
1967
|
+
current: ratio(dense.current.conflictSamples, dense.samples.original),
|
|
1968
|
+
target: ratio(dense.target.conflictSamples, dense.samples.original),
|
|
1969
|
+
action: ratio(dense.action.conflictSamplePairs, dense.samples.original * dense.samples.blocker)
|
|
1970
|
+
};
|
|
1971
|
+
const drift = {
|
|
1972
|
+
current: Math.abs(baseRatios.current - denseRatios.current),
|
|
1973
|
+
target: Math.abs(baseRatios.target - denseRatios.target),
|
|
1974
|
+
action: Math.abs(baseRatios.action - denseRatios.action)
|
|
1975
|
+
};
|
|
1976
|
+
const qualitative = {
|
|
1977
|
+
targetSweepClear: base.targetSweepClear === dense.targetSweepClear,
|
|
1978
|
+
clearanceGain: base.conflictReduction > 0 === dense.conflictReduction > 0
|
|
1979
|
+
};
|
|
1980
|
+
return {
|
|
1981
|
+
checked: true,
|
|
1982
|
+
causal: false,
|
|
1983
|
+
status: qualitative.targetSweepClear && qualitative.clearanceGain ? "stable" : "unstable",
|
|
1984
|
+
base,
|
|
1985
|
+
dense,
|
|
1986
|
+
qualitative,
|
|
1987
|
+
ratios: {
|
|
1988
|
+
base: baseRatios,
|
|
1989
|
+
dense: denseRatios,
|
|
1990
|
+
drift
|
|
1991
|
+
},
|
|
1992
|
+
maxRatioDrift: Math.max(drift.current, drift.target, drift.action)
|
|
1993
|
+
};
|
|
1994
|
+
}
|
|
1995
|
+
ownerOfBody(body) {
|
|
1996
|
+
const key = this.backend.bodyKey(body);
|
|
1997
|
+
for (const [id, entry] of this.entries) {
|
|
1998
|
+
if (this.backend.bodyKey(entry.body) === key) return {
|
|
1999
|
+
id,
|
|
2000
|
+
part: "$root"
|
|
2001
|
+
};
|
|
2002
|
+
for (const [part, value] of entry.parts) if (this.backend.bodyKey(value.body) === key) return {
|
|
2003
|
+
id,
|
|
2004
|
+
part
|
|
2005
|
+
};
|
|
2006
|
+
}
|
|
2007
|
+
return null;
|
|
2008
|
+
}
|
|
2009
|
+
raycast(origin, target, { excludeId = null, excludeIds = [] } = {}) {
|
|
2010
|
+
if (!this.backend.hasCapability("scene-query")) return null;
|
|
2011
|
+
const direction = [
|
|
2012
|
+
target[0] - origin[0],
|
|
2013
|
+
target[1] - origin[1],
|
|
2014
|
+
target[2] - origin[2]
|
|
2015
|
+
];
|
|
2016
|
+
const distance = Math.hypot(...direction);
|
|
2017
|
+
if (!Number.isFinite(distance) || distance < 1e-8 || !this.world) return null;
|
|
2018
|
+
const normalized = direction.map((value) => value / distance);
|
|
2019
|
+
const excluded = /* @__PURE__ */ new Set([...excludeId ? [excludeId] : [], ...excludeIds]);
|
|
2020
|
+
const filter = excluded.size ? (collider) => {
|
|
2021
|
+
const parent = this.backend.colliderParent(collider);
|
|
2022
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
2023
|
+
return !owner || !excluded.has(owner.id);
|
|
2024
|
+
} : void 0;
|
|
2025
|
+
const hit = this.backend.raycast(this.world, origin, normalized, distance, {
|
|
2026
|
+
solid: true,
|
|
2027
|
+
predicate: filter
|
|
2028
|
+
});
|
|
2029
|
+
if (!hit) return null;
|
|
2030
|
+
const body = this.backend.colliderParent(hit.collider);
|
|
2031
|
+
const owner = body ? this.ownerOfBody(body) : null;
|
|
2032
|
+
return {
|
|
2033
|
+
id: owner?.id || null,
|
|
2034
|
+
part: owner?.part || null,
|
|
2035
|
+
environment: !owner,
|
|
2036
|
+
provenance: this.provenanceOfCollider(hit.collider),
|
|
2037
|
+
distance: hit.timeOfImpact,
|
|
2038
|
+
point: [
|
|
2039
|
+
origin[0] + normalized[0] * hit.timeOfImpact,
|
|
2040
|
+
origin[1] + normalized[1] * hit.timeOfImpact,
|
|
2041
|
+
origin[2] + normalized[2] * hit.timeOfImpact
|
|
2042
|
+
]
|
|
2043
|
+
};
|
|
2044
|
+
}
|
|
2045
|
+
faceCharacter(id, direction) {
|
|
2046
|
+
const entry = this.entries.get(id);
|
|
2047
|
+
if (!entry) return false;
|
|
2048
|
+
if (Math.hypot(direction[0], direction[2]) < 1e-8) return false;
|
|
2049
|
+
const yaw = Math.atan2(-direction[0], -direction[2]);
|
|
2050
|
+
if (!entry.body) return this.setCharacterYaw(id, yaw);
|
|
2051
|
+
if (this.backend.bodyType(entry.body) !== "kinematic") return false;
|
|
2052
|
+
this.backend.setBodyPose(entry.body, {
|
|
2053
|
+
rotation: [
|
|
2054
|
+
0,
|
|
2055
|
+
Math.sin(yaw / 2),
|
|
2056
|
+
0,
|
|
2057
|
+
Math.cos(yaw / 2)
|
|
2058
|
+
],
|
|
2059
|
+
next: true
|
|
2060
|
+
});
|
|
2061
|
+
return true;
|
|
2062
|
+
}
|
|
2063
|
+
setCharacterYaw(id, yaw) {
|
|
2064
|
+
const entry = this.entries.get(id);
|
|
2065
|
+
if (!entry || !Number.isFinite(yaw)) return false;
|
|
2066
|
+
const rotation = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), yaw);
|
|
2067
|
+
if (entry.body) {
|
|
2068
|
+
if (this.backend.bodyType(entry.body) !== "kinematic") return false;
|
|
2069
|
+
this.backend.setBodyPose(entry.body, { rotation });
|
|
2070
|
+
this.backend.setBodyPose(entry.body, {
|
|
2071
|
+
rotation,
|
|
2072
|
+
next: true
|
|
2073
|
+
});
|
|
2074
|
+
}
|
|
2075
|
+
entry.root.quaternion.copy(rotation);
|
|
2076
|
+
entry.lastRotation.copy(rotation);
|
|
2077
|
+
entry.root.updateMatrixWorld(true);
|
|
2078
|
+
return true;
|
|
2079
|
+
}
|
|
2080
|
+
moveCharacter(id, desiredTranslation, { ignoreIds = [] } = {}) {
|
|
2081
|
+
if (!this.backend.hasCapability("character-controller")) return {
|
|
2082
|
+
success: false,
|
|
2083
|
+
code: "PHYSICS_CAPABILITY_UNAVAILABLE",
|
|
2084
|
+
capability: "character-controller",
|
|
2085
|
+
movement: [
|
|
2086
|
+
0,
|
|
2087
|
+
0,
|
|
2088
|
+
0
|
|
2089
|
+
],
|
|
2090
|
+
grounded: false,
|
|
2091
|
+
collisions: []
|
|
2092
|
+
};
|
|
2093
|
+
const entry = this.entries.get(id);
|
|
2094
|
+
if (!entry?.body || !this.characterController) return {
|
|
2095
|
+
success: false,
|
|
2096
|
+
code: "CHARACTER_BODY_UNAVAILABLE",
|
|
2097
|
+
movement: [
|
|
2098
|
+
0,
|
|
2099
|
+
0,
|
|
2100
|
+
0
|
|
2101
|
+
],
|
|
2102
|
+
grounded: false,
|
|
2103
|
+
collisions: []
|
|
2104
|
+
};
|
|
2105
|
+
const ignored = new Set(ignoreIds);
|
|
2106
|
+
const result = this.backend.moveCharacter(this.characterController, entry.body, desiredTranslation, { predicate: (other) => {
|
|
2107
|
+
const parent = this.backend.colliderParent(other);
|
|
2108
|
+
const owner = parent ? this.ownerOfBody(parent) : null;
|
|
2109
|
+
return !owner || !ignored.has(owner.id);
|
|
2110
|
+
} });
|
|
2111
|
+
if (!result.success) return result;
|
|
2112
|
+
return {
|
|
2113
|
+
...result,
|
|
2114
|
+
collisions: result.collisions.map(({ colliderKey, toi, normal }) => ({
|
|
2115
|
+
colliderHandle: colliderKey,
|
|
2116
|
+
toi,
|
|
2117
|
+
normal
|
|
2118
|
+
}))
|
|
2119
|
+
};
|
|
2120
|
+
}
|
|
2121
|
+
setArticulationTarget(id, partName, target) {
|
|
2122
|
+
const part = this.entries.get(id)?.parts.get(partName);
|
|
2123
|
+
if (!part || !Number.isFinite(target)) return false;
|
|
2124
|
+
if (!part.joint || !part.body) {
|
|
2125
|
+
if (!this.hasCapability("articulation-pose")) return false;
|
|
2126
|
+
const limits = part.spec.joint.limits || [];
|
|
2127
|
+
if (limits.length === 2 && (target < limits[0] || target > limits[1])) return false;
|
|
2128
|
+
const state = this.getArticulationState(id, partName);
|
|
2129
|
+
if (!state) return false;
|
|
2130
|
+
const axis = new THREE.Vector3(...state.localAxis).normalize();
|
|
2131
|
+
if (part.spec.joint.type === "prismatic") part.node.position.copy(part.restLocalPosition).addScaledVector(axis, target);
|
|
2132
|
+
else if (part.spec.joint.type === "revolute") {
|
|
2133
|
+
const rotation = new THREE.Quaternion().setFromAxisAngle(axis, target);
|
|
2134
|
+
part.node.quaternion.copy(part.restLocalRotation).premultiply(rotation).normalize();
|
|
2135
|
+
} else return false;
|
|
2136
|
+
part.node.updateMatrixWorld(true);
|
|
2137
|
+
part.lastLocalPosition.copy(part.node.position);
|
|
2138
|
+
part.lastLocalRotation.copy(part.node.quaternion);
|
|
2139
|
+
return true;
|
|
2140
|
+
}
|
|
2141
|
+
const motor = part.spec.joint.motor || {};
|
|
2142
|
+
if (!this.backend.setJointTarget(part.joint, target, motor)) return false;
|
|
2143
|
+
part.motorTarget = target;
|
|
2144
|
+
this.backend.wakeBody(part.body);
|
|
2145
|
+
return true;
|
|
2146
|
+
}
|
|
2147
|
+
holdArticulationCurrent(id, partName) {
|
|
2148
|
+
const part = this.entries.get(id)?.parts.get(partName);
|
|
2149
|
+
const state = this.getArticulationState(id, partName);
|
|
2150
|
+
if (!part || !state) return false;
|
|
2151
|
+
if (!part.joint || !part.body) return this.hasCapability("articulation-pose");
|
|
2152
|
+
const motor = part.spec.joint.motor || {};
|
|
2153
|
+
if (!this.backend.setJointTarget(part.joint, state.coordinate, motor)) return false;
|
|
2154
|
+
part.motorTarget = state.coordinate;
|
|
2155
|
+
this.backend.wakeBody(part.body);
|
|
2156
|
+
return true;
|
|
2157
|
+
}
|
|
2158
|
+
/** Read-only hypothetical collider descriptors, not a solver mutation. */
|
|
2159
|
+
nativeNavigationJointPose(id, partName, target) {
|
|
2160
|
+
const entry = this.entries.get(id);
|
|
2161
|
+
const limits = (entry?.parts.get(partName))?.spec.joint?.limits;
|
|
2162
|
+
if (!entry?.nativeArticulation || !limits || !Number.isFinite(target) || target < limits[0] || target > limits[1]) return {
|
|
2163
|
+
checked: false,
|
|
2164
|
+
reason: "NATIVE_NAVIGATION_JOINT_UNAVAILABLE"
|
|
2165
|
+
};
|
|
2166
|
+
const pose = this.articulationColliderPoses(id, partName, target);
|
|
2167
|
+
if (!pose.checked) return pose;
|
|
2168
|
+
const descriptors = pose.colliders.map((c) => navigationObstacleDescriptor(id, partName, c.index, c));
|
|
2169
|
+
if (!descriptors.length || descriptors.some((c) => !c)) return {
|
|
2170
|
+
checked: false,
|
|
2171
|
+
reason: "NATIVE_NAVIGATION_COLLIDER_UNSUPPORTED"
|
|
2172
|
+
};
|
|
2173
|
+
return {
|
|
2174
|
+
checked: true,
|
|
2175
|
+
currentCoordinate: pose.currentCoordinate,
|
|
2176
|
+
descriptors
|
|
2177
|
+
};
|
|
2178
|
+
}
|
|
2179
|
+
getNavigationObstacles() {
|
|
2180
|
+
if (!this.backend.hasCapability("collision")) return {
|
|
2181
|
+
items: [],
|
|
2182
|
+
skipped: [{
|
|
2183
|
+
reason: "physics-capability-unavailable",
|
|
2184
|
+
capability: "collision"
|
|
2185
|
+
}]
|
|
2186
|
+
};
|
|
2187
|
+
this.backend.syncSceneQueries(this.world);
|
|
2188
|
+
const items = [];
|
|
2189
|
+
const skipped = [];
|
|
2190
|
+
const addBody = (objectId, partName, body, bodyType, navigationObstacle = true) => {
|
|
2191
|
+
if (bodyType === "fixed" || navigationObstacle === false) return;
|
|
2192
|
+
for (const [i, collider] of this.backend.colliders(body).entries()) {
|
|
2193
|
+
const { shape, position, rotation } = this.backend.colliderSnapshot(collider);
|
|
2194
|
+
const descriptor = navigationObstacleDescriptor(objectId, partName, i, {
|
|
2195
|
+
shape,
|
|
2196
|
+
position,
|
|
2197
|
+
rotation
|
|
2198
|
+
});
|
|
2199
|
+
if (descriptor) items.push(descriptor);
|
|
2200
|
+
else skipped.push({
|
|
2201
|
+
id: objectId + ":" + partName + ":" + i,
|
|
2202
|
+
objectId,
|
|
2203
|
+
part: partName,
|
|
2204
|
+
collider: i,
|
|
2205
|
+
reason: "unsupported-shape",
|
|
2206
|
+
shapeType: shape.kind
|
|
2207
|
+
});
|
|
2208
|
+
}
|
|
2209
|
+
};
|
|
2210
|
+
for (const [id, entry] of this.entries) {
|
|
2211
|
+
if (!entry.held) addBody(id, "$root", entry.body, entry.rootSpec.body || "fixed", entry.rootSpec.navigationObstacle);
|
|
2212
|
+
for (const [partName, part] of entry.parts) addBody(id, partName, part.body, part.spec.physics?.body || "dynamic");
|
|
2213
|
+
}
|
|
2214
|
+
return {
|
|
2215
|
+
items,
|
|
2216
|
+
skipped
|
|
2217
|
+
};
|
|
2218
|
+
}
|
|
2219
|
+
articulationContacts(id, partName) {
|
|
2220
|
+
if (!this.backend.hasCapability("collision")) return [];
|
|
2221
|
+
const entry = this.entries.get(id);
|
|
2222
|
+
const part = entry?.parts.get(partName);
|
|
2223
|
+
const body = partName === "$root" ? entry?.body : part?.body;
|
|
2224
|
+
if (!entry || !body || !this.world) return [];
|
|
2225
|
+
const contacts = [];
|
|
2226
|
+
for (const [sourceIndex, source] of this.backend.colliders(body).entries()) {
|
|
2227
|
+
const sourceOwner = this.provenanceOfCollider(source) || {
|
|
2228
|
+
kind: "object",
|
|
2229
|
+
objectId: id,
|
|
2230
|
+
partName,
|
|
2231
|
+
colliderIndex: sourceIndex
|
|
2232
|
+
};
|
|
2233
|
+
for (const pair of this.backend.contactPairs(this.world, source)) {
|
|
2234
|
+
const target = this.provenanceOfCollider(pair.other);
|
|
2235
|
+
const external = !target || target.kind === "environment" || target.objectId !== id;
|
|
2236
|
+
contacts.push({
|
|
2237
|
+
source: sourceOwner,
|
|
2238
|
+
target: target || {
|
|
2239
|
+
kind: "unknown",
|
|
2240
|
+
colliderIndex: null
|
|
2241
|
+
},
|
|
2242
|
+
external,
|
|
2243
|
+
manifoldCount: pair.manifoldCount,
|
|
2244
|
+
contactCount: pair.contactCount,
|
|
2245
|
+
activeContactCount: pair.activeContactCount,
|
|
2246
|
+
minDistance: pair.minDistance,
|
|
2247
|
+
totalImpulse: Number.isFinite(pair.totalImpulse) ? pair.totalImpulse : null,
|
|
2248
|
+
evidenceKind: pair.evidenceKind || "contact",
|
|
2249
|
+
impulseAvailable: pair.impulseAvailable === true,
|
|
2250
|
+
normal: pair.normal
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
contacts.sort((a, b) => {
|
|
2255
|
+
const ak = `${a.target.kind}:${a.target.objectId || a.target.environmentId || ""}:${a.target.partName || ""}:${a.target.colliderIndex ?? -1}:${a.source.colliderIndex}`;
|
|
2256
|
+
const bk = `${b.target.kind}:${b.target.objectId || b.target.environmentId || ""}:${b.target.partName || ""}:${b.target.colliderIndex ?? -1}:${b.source.colliderIndex}`;
|
|
2257
|
+
return ak.localeCompare(bk);
|
|
2258
|
+
});
|
|
2259
|
+
return contacts;
|
|
2260
|
+
}
|
|
2261
|
+
articulationPenetrations(id, partName, { refresh = false } = {}) {
|
|
2262
|
+
if (!this.backend.hasCapability("collision")) return [];
|
|
2263
|
+
if (refresh) this.backend.syncSceneQueries(this.world);
|
|
2264
|
+
const entry = this.entries.get(id);
|
|
2265
|
+
const part = entry?.parts.get(partName);
|
|
2266
|
+
if (!entry || !part) return [];
|
|
2267
|
+
const owners = /* @__PURE__ */ new Map([[this.backend.bodyKey(entry.body), "$root"]]);
|
|
2268
|
+
for (const [name, value] of entry.parts) owners.set(this.backend.bodyKey(value.body), name);
|
|
2269
|
+
const hits = /* @__PURE__ */ new Map();
|
|
2270
|
+
for (const [i, source] of this.backend.colliders(part.body).entries()) for (const penetration of this.backend.penetrations(this.world, source)) {
|
|
2271
|
+
const otherBody = this.backend.colliderParent(penetration.other);
|
|
2272
|
+
if (!otherBody || this.backend.bodyKey(otherBody) === this.backend.bodyKey(part.body)) continue;
|
|
2273
|
+
const targetPart = owners.get(this.backend.bodyKey(otherBody)) || "$external";
|
|
2274
|
+
const targetColliders = this.backend.colliders(otherBody);
|
|
2275
|
+
const otherKey = this.backend.colliderKey(penetration.other);
|
|
2276
|
+
const targetIndex = targetColliders.findIndex((collider) => this.backend.colliderKey(collider) === otherKey);
|
|
2277
|
+
const key = `${partName}[${i}]->${targetPart}[${targetIndex}]`;
|
|
2278
|
+
const depth = -penetration.distance;
|
|
2279
|
+
const previous = hits.get(key);
|
|
2280
|
+
if (!previous || depth > previous.depth) hits.set(key, {
|
|
2281
|
+
key,
|
|
2282
|
+
depth,
|
|
2283
|
+
sourcePart: partName,
|
|
2284
|
+
sourceCollider: i,
|
|
2285
|
+
targetPart,
|
|
2286
|
+
targetCollider: targetIndex
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
return [...hits.values()];
|
|
2290
|
+
}
|
|
2291
|
+
dispose() {
|
|
2292
|
+
this.entries.clear();
|
|
2293
|
+
this.colliderProvenance.clear();
|
|
2294
|
+
this.colliderHandles.clear();
|
|
2295
|
+
this.colliderRuntime.clear();
|
|
2296
|
+
this.collisionPairs.clear();
|
|
2297
|
+
this.collisionEvents.length = 0;
|
|
2298
|
+
this.collisionGroupBits.clear();
|
|
2299
|
+
if (this.world && this.characterController) this.backend.removeCharacterController(this.world, this.characterController);
|
|
2300
|
+
this.characterController = null;
|
|
2301
|
+
this.backend.dispose(this.world);
|
|
2302
|
+
this.world = null;
|
|
2303
|
+
}
|
|
2304
|
+
step(dt, store) {
|
|
2305
|
+
this.backend.step(this.world, dt);
|
|
2306
|
+
this.updateCollisionEvents();
|
|
2307
|
+
return this.writeback(store);
|
|
2308
|
+
}
|
|
2309
|
+
getCollisionEvents({ clear = true } = {}) {
|
|
2310
|
+
const events = this.collisionEvents.map((event) => structuredClone(event));
|
|
2311
|
+
if (clear) this.collisionEvents.length = 0;
|
|
2312
|
+
return events;
|
|
2313
|
+
}
|
|
2314
|
+
updateCollisionEvents() {
|
|
2315
|
+
if (!this.world || !this.backend.hasCapability("collision")) return;
|
|
2316
|
+
const watched = [...this.colliderRuntime.entries()].filter(([, runtime]) => runtime.sensor || runtime.collisionEvents);
|
|
2317
|
+
if (!watched.length) {
|
|
2318
|
+
this.collisionPairs.clear();
|
|
2319
|
+
return;
|
|
2320
|
+
}
|
|
2321
|
+
this.backend.syncSceneQueries(this.world);
|
|
2322
|
+
const current = /* @__PURE__ */ new Map();
|
|
2323
|
+
const descriptor = (key) => {
|
|
2324
|
+
const p = this.colliderProvenance.get(key);
|
|
2325
|
+
if (!p) return {
|
|
2326
|
+
kind: "unknown",
|
|
2327
|
+
colliderKey: key
|
|
2328
|
+
};
|
|
2329
|
+
if (p.kind === "environment") return {
|
|
2330
|
+
kind: "environment",
|
|
2331
|
+
environmentId: p.environmentId || "$environment",
|
|
2332
|
+
colliderIndex: p.colliderIndex ?? null
|
|
2333
|
+
};
|
|
2334
|
+
return {
|
|
2335
|
+
kind: "object",
|
|
2336
|
+
objectId: p.objectId,
|
|
2337
|
+
partName: p.partName || "$root",
|
|
2338
|
+
colliderIndex: p.colliderIndex ?? null
|
|
2339
|
+
};
|
|
2340
|
+
};
|
|
2341
|
+
const addPair = (leftKey, rightKey) => {
|
|
2342
|
+
if (leftKey == null || rightKey == null || leftKey === rightKey) return;
|
|
2343
|
+
const a = leftKey < rightKey ? leftKey : rightKey, b = leftKey < rightKey ? rightKey : leftKey, key = String(a) + ":" + String(b);
|
|
2344
|
+
if (current.has(key)) return;
|
|
2345
|
+
const ar = this.colliderRuntime.get(a), br = this.colliderRuntime.get(b);
|
|
2346
|
+
if (!this._collisionPairAllowed(ar, br)) return;
|
|
2347
|
+
current.set(key, {
|
|
2348
|
+
key,
|
|
2349
|
+
sensor: Boolean(ar?.sensor || br?.sensor),
|
|
2350
|
+
a: descriptor(a),
|
|
2351
|
+
b: descriptor(b)
|
|
2352
|
+
});
|
|
2353
|
+
};
|
|
2354
|
+
for (const [sourceKey, sourceRuntime] of watched) {
|
|
2355
|
+
const source = this.colliderHandles.get(sourceKey);
|
|
2356
|
+
if (!source) continue;
|
|
2357
|
+
if (sourceRuntime.sensor) {
|
|
2358
|
+
const snapshot = this.backend.colliderSnapshot(source);
|
|
2359
|
+
if (!snapshot) continue;
|
|
2360
|
+
this.backend.intersectionsWithShape(this.world, snapshot.position, snapshot.rotation, snapshot.shapeRef, (other) => {
|
|
2361
|
+
const otherKey = this.backend.colliderKey(other);
|
|
2362
|
+
const otherBody = this.backend.colliderParent(other), sourceBody = this.backend.colliderParent(source);
|
|
2363
|
+
if (otherKey !== sourceKey && this.backend.bodyKey(otherBody) !== this.backend.bodyKey(sourceBody)) addPair(sourceKey, otherKey);
|
|
2364
|
+
return true;
|
|
2365
|
+
}, { excludeCollider: source });
|
|
2366
|
+
} else for (const pair of this.backend.contactPairs(this.world, source)) addPair(sourceKey, this.backend.colliderKey(pair.other));
|
|
2367
|
+
}
|
|
2368
|
+
for (const [key, pair] of current) if (!this.collisionPairs.has(key)) this.collisionEvents.push({
|
|
2369
|
+
type: pair.sensor ? "sensor-entered" : "collision-started",
|
|
2370
|
+
a: pair.a,
|
|
2371
|
+
b: pair.b
|
|
2372
|
+
});
|
|
2373
|
+
for (const [key, pair] of this.collisionPairs) if (!current.has(key)) this.collisionEvents.push({
|
|
2374
|
+
type: pair.sensor ? "sensor-exited" : "collision-ended",
|
|
2375
|
+
a: pair.a,
|
|
2376
|
+
b: pair.b
|
|
2377
|
+
});
|
|
2378
|
+
this.collisionPairs = current;
|
|
2379
|
+
}
|
|
2380
|
+
writeback(store) {
|
|
2381
|
+
let changed = false;
|
|
2382
|
+
if (!this.solverEnabled) {
|
|
2383
|
+
for (const [id, entry] of this.entries) {
|
|
2384
|
+
const record = store.has(id) ? store.get(id) : null;
|
|
2385
|
+
if (!record) continue;
|
|
2386
|
+
record.object.updateMatrixWorld(true);
|
|
2387
|
+
const p = new THREE.Vector3();
|
|
2388
|
+
const q = new THREE.Quaternion();
|
|
2389
|
+
record.object.getWorldPosition(p);
|
|
2390
|
+
record.object.getWorldQuaternion(q);
|
|
2391
|
+
if (p.distanceToSquared(entry.lastPosition) > 1e-10 || 1 - Math.abs(q.dot(entry.lastRotation)) > 1e-10) changed = true;
|
|
2392
|
+
entry.lastPosition.copy(p);
|
|
2393
|
+
entry.lastRotation.copy(q);
|
|
2394
|
+
for (const part of entry.parts.values()) {
|
|
2395
|
+
if (part.node.position.distanceToSquared(part.lastLocalPosition) > 1e-10 || 1 - Math.abs(part.node.quaternion.dot(part.lastLocalRotation)) > 1e-10) changed = true;
|
|
2396
|
+
part.lastLocalPosition.copy(part.node.position);
|
|
2397
|
+
part.lastLocalRotation.copy(part.node.quaternion);
|
|
2398
|
+
}
|
|
2399
|
+
}
|
|
2400
|
+
return changed;
|
|
2401
|
+
}
|
|
2402
|
+
for (const [id, entry] of this.entries) {
|
|
2403
|
+
const record = store.has(id) ? store.get(id) : null;
|
|
2404
|
+
if (!record) continue;
|
|
2405
|
+
if (this.backend.bodyType(entry.body) !== "fixed") {
|
|
2406
|
+
const pose = this.backend.bodyPose(entry.body);
|
|
2407
|
+
const p = {
|
|
2408
|
+
x: pose.position[0],
|
|
2409
|
+
y: pose.position[1],
|
|
2410
|
+
z: pose.position[2]
|
|
2411
|
+
};
|
|
2412
|
+
const q = {
|
|
2413
|
+
x: pose.rotation[0],
|
|
2414
|
+
y: pose.rotation[1],
|
|
2415
|
+
z: pose.rotation[2],
|
|
2416
|
+
w: pose.rotation[3]
|
|
2417
|
+
};
|
|
2418
|
+
const dx = p.x - entry.lastPosition.x;
|
|
2419
|
+
const dy = p.y - entry.lastPosition.y;
|
|
2420
|
+
const dz = p.z - entry.lastPosition.z;
|
|
2421
|
+
const rotationDot = Math.abs(entry.lastRotation.x * q.x + entry.lastRotation.y * q.y + entry.lastRotation.z * q.z + entry.lastRotation.w * q.w);
|
|
2422
|
+
if (dx * dx + dy * dy + dz * dz > 1e-10 || 1 - rotationDot > 1e-10) changed = true;
|
|
2423
|
+
const object = record.object;
|
|
2424
|
+
this.rootWorldPosition.set(p.x, p.y, p.z);
|
|
2425
|
+
this.rootRotation.set(q.x, q.y, q.z, q.w);
|
|
2426
|
+
if (object.parent) {
|
|
2427
|
+
object.parent.updateWorldMatrix(true, false);
|
|
2428
|
+
object.position.copy(object.parent.worldToLocal(this.rootWorldPosition));
|
|
2429
|
+
object.parent.getWorldQuaternion(this.parentWorldRotation);
|
|
2430
|
+
object.quaternion.copy(this.parentWorldRotation.invert()).multiply(this.rootRotation);
|
|
2431
|
+
} else {
|
|
2432
|
+
object.position.copy(this.rootWorldPosition);
|
|
2433
|
+
object.quaternion.copy(this.rootRotation);
|
|
2434
|
+
}
|
|
2435
|
+
object.updateMatrixWorld(true);
|
|
2436
|
+
entry.lastPosition.set(p.x, p.y, p.z);
|
|
2437
|
+
entry.lastRotation.set(q.x, q.y, q.z, q.w);
|
|
2438
|
+
}
|
|
2439
|
+
for (const part of entry.parts.values()) {
|
|
2440
|
+
const pose = this.backend.bodyPose(part.body);
|
|
2441
|
+
const p = {
|
|
2442
|
+
x: pose.position[0],
|
|
2443
|
+
y: pose.position[1],
|
|
2444
|
+
z: pose.position[2]
|
|
2445
|
+
};
|
|
2446
|
+
const q = {
|
|
2447
|
+
x: pose.rotation[0],
|
|
2448
|
+
y: pose.rotation[1],
|
|
2449
|
+
z: pose.rotation[2],
|
|
2450
|
+
w: pose.rotation[3]
|
|
2451
|
+
};
|
|
2452
|
+
this.partWorldRotation.set(q.x, q.y, q.z, q.w);
|
|
2453
|
+
this.partWorldPosition.set(p.x, p.y, p.z);
|
|
2454
|
+
const parentNode = part.node.parent;
|
|
2455
|
+
if (parentNode) {
|
|
2456
|
+
parentNode.updateWorldMatrix(true, false);
|
|
2457
|
+
parentNode.getWorldPosition(this.parentWorldPosition);
|
|
2458
|
+
parentNode.getWorldQuaternion(this.parentWorldRotation);
|
|
2459
|
+
this.inverseParentWorldRotation.copy(this.parentWorldRotation).invert();
|
|
2460
|
+
this.partLocalRotation.copy(this.inverseParentWorldRotation).multiply(this.partWorldRotation);
|
|
2461
|
+
this.partLocalPosition.copy(this.partWorldPosition).sub(this.parentWorldPosition).applyQuaternion(this.inverseParentWorldRotation);
|
|
2462
|
+
} else {
|
|
2463
|
+
this.partLocalRotation.copy(this.partWorldRotation);
|
|
2464
|
+
this.partLocalPosition.copy(this.partWorldPosition);
|
|
2465
|
+
}
|
|
2466
|
+
if (1 - Math.abs(part.lastLocalRotation.dot(this.partLocalRotation)) > 1e-10 || part.lastLocalPosition.distanceToSquared(this.partLocalPosition) > 1e-10) changed = true;
|
|
2467
|
+
part.node.quaternion.copy(this.partLocalRotation);
|
|
2468
|
+
part.node.position.copy(this.partLocalPosition);
|
|
2469
|
+
part.node.updateMatrixWorld(false);
|
|
2470
|
+
part.lastLocalRotation.copy(this.partLocalRotation);
|
|
2471
|
+
part.lastLocalPosition.copy(this.partLocalPosition);
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2474
|
+
return changed;
|
|
2475
|
+
}
|
|
2476
|
+
};
|
|
2477
|
+
// modules/physics/PhysicsBackend.js
|
|
2478
|
+
const PHYSICS_BACKEND_CAPABILITIES = Object.freeze([
|
|
2479
|
+
"rigid-body",
|
|
2480
|
+
"articulated-body",
|
|
2481
|
+
"character-controller",
|
|
2482
|
+
"collision",
|
|
2483
|
+
"joints",
|
|
2484
|
+
"scene-query"
|
|
2485
|
+
]);
|
|
2486
|
+
const missing = (identity, method) => {
|
|
2487
|
+
throw new Error(`Physics backend ${identity} ${method}() must be implemented`);
|
|
2488
|
+
};
|
|
2489
|
+
var PhysicsBackend = class {
|
|
2490
|
+
constructor(identity, capabilities = [], { executionModes = ["realtime"], qualities = {} } = {}) {
|
|
2491
|
+
if (!identity) throw new TypeError("PhysicsBackend identity is required");
|
|
2492
|
+
this.identity = identity;
|
|
2493
|
+
this.capabilities = Object.freeze([...new Set(capabilities)]);
|
|
2494
|
+
this.executionModes = Object.freeze([...new Set(executionModes)]);
|
|
2495
|
+
this.qualities = Object.freeze({
|
|
2496
|
+
realtime: qualities.realtime === true,
|
|
2497
|
+
deterministic: qualities.deterministic === true
|
|
2498
|
+
});
|
|
2499
|
+
}
|
|
2500
|
+
hasCapability(capability) {
|
|
2501
|
+
return this.capabilities.includes(capability);
|
|
2502
|
+
}
|
|
2503
|
+
supportsExecutionMode(mode) {
|
|
2504
|
+
return this.executionModes.includes(mode);
|
|
2505
|
+
}
|
|
2506
|
+
async init() {
|
|
2507
|
+
missing(this.identity, "init");
|
|
2508
|
+
}
|
|
2509
|
+
createWorld() {
|
|
2510
|
+
missing(this.identity, "createWorld");
|
|
2511
|
+
}
|
|
2512
|
+
step() {
|
|
2513
|
+
missing(this.identity, "step");
|
|
2514
|
+
}
|
|
2515
|
+
dispose() {
|
|
2516
|
+
missing(this.identity, "dispose");
|
|
2517
|
+
}
|
|
2518
|
+
createBody() {
|
|
2519
|
+
missing(this.identity, "createBody");
|
|
2520
|
+
}
|
|
2521
|
+
removeBody() {
|
|
2522
|
+
missing(this.identity, "removeBody");
|
|
2523
|
+
}
|
|
2524
|
+
createColliders() {
|
|
2525
|
+
missing(this.identity, "createColliders");
|
|
2526
|
+
}
|
|
2527
|
+
colliders() {
|
|
2528
|
+
missing(this.identity, "colliders");
|
|
2529
|
+
}
|
|
2530
|
+
bodyKey() {
|
|
2531
|
+
missing(this.identity, "bodyKey");
|
|
2532
|
+
}
|
|
2533
|
+
colliderKey() {
|
|
2534
|
+
missing(this.identity, "colliderKey");
|
|
2535
|
+
}
|
|
2536
|
+
colliderParent() {
|
|
2537
|
+
missing(this.identity, "colliderParent");
|
|
2538
|
+
}
|
|
2539
|
+
colliderSnapshot() {
|
|
2540
|
+
missing(this.identity, "colliderSnapshot");
|
|
2541
|
+
}
|
|
2542
|
+
bodyType() {
|
|
2543
|
+
missing(this.identity, "bodyType");
|
|
2544
|
+
}
|
|
2545
|
+
setBodyType() {
|
|
2546
|
+
missing(this.identity, "setBodyType");
|
|
2547
|
+
}
|
|
2548
|
+
bodyPose() {
|
|
2549
|
+
missing(this.identity, "bodyPose");
|
|
2550
|
+
}
|
|
2551
|
+
setBodyPose() {
|
|
2552
|
+
missing(this.identity, "setBodyPose");
|
|
2553
|
+
}
|
|
2554
|
+
translateBody() {
|
|
2555
|
+
missing(this.identity, "translateBody");
|
|
2556
|
+
}
|
|
2557
|
+
clearBodyMotion() {
|
|
2558
|
+
missing(this.identity, "clearBodyMotion");
|
|
2559
|
+
}
|
|
2560
|
+
bodyMotion() {
|
|
2561
|
+
missing(this.identity, "bodyMotion");
|
|
2562
|
+
}
|
|
2563
|
+
setBodyMotion() {
|
|
2564
|
+
missing(this.identity, "setBodyMotion");
|
|
2565
|
+
}
|
|
2566
|
+
applyImpulse() {
|
|
2567
|
+
missing(this.identity, "applyImpulse");
|
|
2568
|
+
}
|
|
2569
|
+
applyForce() {
|
|
2570
|
+
missing(this.identity, "applyForce");
|
|
2571
|
+
}
|
|
2572
|
+
applyTorque() {
|
|
2573
|
+
missing(this.identity, "applyTorque");
|
|
2574
|
+
}
|
|
2575
|
+
setBodyMaterial() {
|
|
2576
|
+
missing(this.identity, "setBodyMaterial");
|
|
2577
|
+
}
|
|
2578
|
+
setBodyDynamics() {
|
|
2579
|
+
missing(this.identity, "setBodyDynamics");
|
|
2580
|
+
}
|
|
2581
|
+
setBodyCcd() {
|
|
2582
|
+
missing(this.identity, "setBodyCcd");
|
|
2583
|
+
}
|
|
2584
|
+
setBodySensor() {
|
|
2585
|
+
missing(this.identity, "setBodySensor");
|
|
2586
|
+
}
|
|
2587
|
+
setBodyCollisionFilter() {
|
|
2588
|
+
missing(this.identity, "setBodyCollisionFilter");
|
|
2589
|
+
}
|
|
2590
|
+
sleepBody() {
|
|
2591
|
+
missing(this.identity, "sleepBody");
|
|
2592
|
+
}
|
|
2593
|
+
wakeBody() {
|
|
2594
|
+
missing(this.identity, "wakeBody");
|
|
2595
|
+
}
|
|
2596
|
+
createJoint() {
|
|
2597
|
+
missing(this.identity, "createJoint");
|
|
2598
|
+
}
|
|
2599
|
+
setJointTarget() {
|
|
2600
|
+
missing(this.identity, "setJointTarget");
|
|
2601
|
+
}
|
|
2602
|
+
createCharacterController() {
|
|
2603
|
+
missing(this.identity, "createCharacterController");
|
|
2604
|
+
}
|
|
2605
|
+
removeCharacterController() {
|
|
2606
|
+
missing(this.identity, "removeCharacterController");
|
|
2607
|
+
}
|
|
2608
|
+
cancelCharacterMovement() {
|
|
2609
|
+
missing(this.identity, "cancelCharacterMovement");
|
|
2610
|
+
}
|
|
2611
|
+
moveCharacter() {
|
|
2612
|
+
missing(this.identity, "moveCharacter");
|
|
2613
|
+
}
|
|
2614
|
+
syncSceneQueries() {
|
|
2615
|
+
missing(this.identity, "syncSceneQueries");
|
|
2616
|
+
}
|
|
2617
|
+
createQueryShape() {
|
|
2618
|
+
missing(this.identity, "createQueryShape");
|
|
2619
|
+
}
|
|
2620
|
+
disposeQueryShape() {
|
|
2621
|
+
missing(this.identity, "disposeQueryShape");
|
|
2622
|
+
}
|
|
2623
|
+
intersectionsWithShape() {
|
|
2624
|
+
missing(this.identity, "intersectionsWithShape");
|
|
2625
|
+
}
|
|
2626
|
+
castCollider() {
|
|
2627
|
+
missing(this.identity, "castCollider");
|
|
2628
|
+
}
|
|
2629
|
+
raycast() {
|
|
2630
|
+
missing(this.identity, "raycast");
|
|
2631
|
+
}
|
|
2632
|
+
contactPairs() {
|
|
2633
|
+
missing(this.identity, "contactPairs");
|
|
2634
|
+
}
|
|
2635
|
+
penetrations() {
|
|
2636
|
+
missing(this.identity, "penetrations");
|
|
2637
|
+
}
|
|
2638
|
+
shapesIntersect() {
|
|
2639
|
+
missing(this.identity, "shapesIntersect");
|
|
2640
|
+
}
|
|
2641
|
+
evidenceGeometry(kind) {
|
|
2642
|
+
return `${this.identity}-${kind}`;
|
|
2643
|
+
}
|
|
2644
|
+
debugSnapshot(_world, _options = {}) {
|
|
2645
|
+
return {
|
|
2646
|
+
backend: this.identity,
|
|
2647
|
+
nativeGeometry: null,
|
|
2648
|
+
metrics: {}
|
|
2649
|
+
};
|
|
2650
|
+
}
|
|
2651
|
+
};
|
|
2652
|
+
// modules/physics/RapierPhysicsBackend.js
|
|
2653
|
+
const v3 = (value = [
|
|
2654
|
+
0,
|
|
2655
|
+
0,
|
|
2656
|
+
0
|
|
2657
|
+
]) => Array.isArray(value) ? {
|
|
2658
|
+
x: value[0],
|
|
2659
|
+
y: value[1],
|
|
2660
|
+
z: value[2]
|
|
2661
|
+
} : {
|
|
2662
|
+
x: value.x,
|
|
2663
|
+
y: value.y,
|
|
2664
|
+
z: value.z
|
|
2665
|
+
};
|
|
2666
|
+
const q4 = (value = [
|
|
2667
|
+
0,
|
|
2668
|
+
0,
|
|
2669
|
+
0,
|
|
2670
|
+
1
|
|
2671
|
+
]) => Array.isArray(value) ? {
|
|
2672
|
+
x: value[0],
|
|
2673
|
+
y: value[1],
|
|
2674
|
+
z: value[2],
|
|
2675
|
+
w: value[3]
|
|
2676
|
+
} : {
|
|
2677
|
+
x: value.x,
|
|
2678
|
+
y: value.y,
|
|
2679
|
+
z: value.z,
|
|
2680
|
+
w: value.w
|
|
2681
|
+
};
|
|
2682
|
+
const array3 = (value) => [
|
|
2683
|
+
value.x,
|
|
2684
|
+
value.y,
|
|
2685
|
+
value.z
|
|
2686
|
+
];
|
|
2687
|
+
const array4 = (value) => [
|
|
2688
|
+
value.x,
|
|
2689
|
+
value.y,
|
|
2690
|
+
value.z,
|
|
2691
|
+
value.w
|
|
2692
|
+
];
|
|
2693
|
+
const createBodyDesc = (type, position, { canSleep = true, lockTranslation = null, lockRotation = null } = {}) => {
|
|
2694
|
+
const desc = type === "dynamic" ? RAPIER.RigidBodyDesc.dynamic() : type === "kinematic" ? RAPIER.RigidBodyDesc.kinematicPositionBased() : RAPIER.RigidBodyDesc.fixed();
|
|
2695
|
+
if (type === "dynamic") {
|
|
2696
|
+
desc.setCanSleep(Boolean(canSleep));
|
|
2697
|
+
if (lockTranslation) desc.restrictTranslations(!lockTranslation[0], !lockTranslation[1], !lockTranslation[2]);
|
|
2698
|
+
if (lockRotation) desc.restrictRotations(!lockRotation[0], !lockRotation[1], !lockRotation[2]);
|
|
2699
|
+
}
|
|
2700
|
+
if (position) {
|
|
2701
|
+
const p = v3(position);
|
|
2702
|
+
desc.setTranslation(p.x, p.y, p.z);
|
|
2703
|
+
}
|
|
2704
|
+
return desc;
|
|
2705
|
+
};
|
|
2706
|
+
const createColliderDesc = (spec) => {
|
|
2707
|
+
let desc;
|
|
2708
|
+
if (spec.shape === "box") desc = RAPIER.ColliderDesc.cuboid(...spec.halfExtents);
|
|
2709
|
+
else if (spec.shape === "cylinder") desc = RAPIER.ColliderDesc.cylinder(spec.halfHeight, spec.radius);
|
|
2710
|
+
else if (spec.shape === "capsule") desc = RAPIER.ColliderDesc.capsule(spec.halfHeight, spec.radius);
|
|
2711
|
+
else if (spec.shape === "convexHull") desc = RAPIER.ColliderDesc.convexHull(new Float32Array(spec.vertices));
|
|
2712
|
+
else if (spec.shape === "trimesh") desc = RAPIER.ColliderDesc.trimesh(new Float32Array(spec.vertices), new Uint32Array(spec.indices));
|
|
2713
|
+
else return null;
|
|
2714
|
+
if (!desc) throw new Error(`Rapier rejected collider shape: ${spec.shape}`);
|
|
2715
|
+
return desc;
|
|
2716
|
+
};
|
|
2717
|
+
const createJoint = (world, part, parentBody, childBody) => {
|
|
2718
|
+
const vec = (a = [
|
|
2719
|
+
0,
|
|
2720
|
+
0,
|
|
2721
|
+
0
|
|
2722
|
+
]) => ({
|
|
2723
|
+
x: a[0],
|
|
2724
|
+
y: a[1],
|
|
2725
|
+
z: a[2]
|
|
2726
|
+
});
|
|
2727
|
+
let data;
|
|
2728
|
+
if (part.joint.type === "revolute") data = RAPIER.JointData.revolute(vec(part.joint.parentAnchor), vec(part.joint.childAnchor), vec(part.joint.axis));
|
|
2729
|
+
else if (part.joint.type === "prismatic") data = RAPIER.JointData.prismatic(vec(part.joint.parentAnchor), vec(part.joint.childAnchor), vec(part.joint.axis));
|
|
2730
|
+
else if (part.joint.type === "fixed") {
|
|
2731
|
+
const parent = parentBody.rotation(), child = childBody.rotation();
|
|
2732
|
+
const inverseParent = {
|
|
2733
|
+
x: -parent.x,
|
|
2734
|
+
y: -parent.y,
|
|
2735
|
+
z: -parent.z,
|
|
2736
|
+
w: parent.w
|
|
2737
|
+
};
|
|
2738
|
+
const multiply = (a, b) => ({
|
|
2739
|
+
x: a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
|
2740
|
+
y: a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
|
2741
|
+
z: a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
|
2742
|
+
w: a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
|
|
2743
|
+
});
|
|
2744
|
+
data = RAPIER.JointData.fixed(vec(part.joint.parentAnchor), multiply(inverseParent, child), vec(part.joint.childAnchor), {
|
|
2745
|
+
x: 0,
|
|
2746
|
+
y: 0,
|
|
2747
|
+
z: 0,
|
|
2748
|
+
w: 1
|
|
2749
|
+
});
|
|
2750
|
+
} else throw new TypeError(`RapierPhysicsBackend unsupported joint type: ${part.joint.type}`);
|
|
2751
|
+
const joint = world.createImpulseJoint(data, parentBody, childBody, true);
|
|
2752
|
+
joint.setContactsEnabled(false);
|
|
2753
|
+
if (part.joint.type !== "fixed" && part.joint.limits) joint.setLimits(part.joint.limits[0], part.joint.limits[1]);
|
|
2754
|
+
return joint;
|
|
2755
|
+
};
|
|
2756
|
+
const createQueryShape = (spec) => {
|
|
2757
|
+
if (spec.shape === "box") return new RAPIER.Cuboid(...spec.halfExtents);
|
|
2758
|
+
if (spec.shape === "cylinder") return new RAPIER.Cylinder(spec.halfHeight, spec.radius);
|
|
2759
|
+
if (spec.shape === "capsule") return new RAPIER.Capsule(spec.halfHeight, spec.radius);
|
|
2760
|
+
if (spec.shape === "convexHull") return new RAPIER.ConvexPolyhedron(new Float32Array(spec.vertices));
|
|
2761
|
+
return null;
|
|
2762
|
+
};
|
|
2763
|
+
const describeShape = (shape) => {
|
|
2764
|
+
if (!shape) return { kind: "unknown" };
|
|
2765
|
+
if (shape.type === RAPIER.ShapeType.Cuboid) return {
|
|
2766
|
+
kind: "box",
|
|
2767
|
+
halfExtents: v3(shape.halfExtents),
|
|
2768
|
+
borderRadius: Number(shape.borderRadius) || 0
|
|
2769
|
+
};
|
|
2770
|
+
if (shape.type === RAPIER.ShapeType.Cylinder) return {
|
|
2771
|
+
kind: "cylinder",
|
|
2772
|
+
halfHeight: shape.halfHeight,
|
|
2773
|
+
radius: shape.radius,
|
|
2774
|
+
borderRadius: Number(shape.borderRadius) || 0
|
|
2775
|
+
};
|
|
2776
|
+
if (shape.type === RAPIER.ShapeType.Capsule) return {
|
|
2777
|
+
kind: "capsule",
|
|
2778
|
+
halfHeight: shape.halfHeight,
|
|
2779
|
+
radius: shape.radius,
|
|
2780
|
+
borderRadius: Number(shape.borderRadius) || 0
|
|
2781
|
+
};
|
|
2782
|
+
if (shape.type === RAPIER.ShapeType.ConvexPolyhedron) return {
|
|
2783
|
+
kind: "convexHull",
|
|
2784
|
+
vertices: Array.from(shape.vertices || []),
|
|
2785
|
+
borderRadius: Number(shape.borderRadius) || 0
|
|
2786
|
+
};
|
|
2787
|
+
if (shape.type === RAPIER.ShapeType.TriMesh) return {
|
|
2788
|
+
kind: "trimesh",
|
|
2789
|
+
vertices: Array.from(shape.vertices || []),
|
|
2790
|
+
indices: Array.from(shape.indices || [])
|
|
2791
|
+
};
|
|
2792
|
+
return {
|
|
2793
|
+
kind: "unknown",
|
|
2794
|
+
type: shape.type
|
|
2795
|
+
};
|
|
2796
|
+
};
|
|
2797
|
+
let rapierReady;
|
|
2798
|
+
const loadRapier = () => rapierReady ??= Promise.resolve().then(() => RAPIER.init()).catch((error) => {
|
|
2799
|
+
rapierReady = void 0;
|
|
2800
|
+
throw error;
|
|
2801
|
+
});
|
|
2802
|
+
var RapierPhysicsBackend = class extends PhysicsBackend {
|
|
2803
|
+
constructor({ gravity = {
|
|
2804
|
+
x: 0,
|
|
2805
|
+
y: -9.81,
|
|
2806
|
+
z: 0
|
|
2807
|
+
} } = {}) {
|
|
2808
|
+
super("rapier", PHYSICS_BACKEND_CAPABILITIES, {
|
|
2809
|
+
executionModes: ["realtime", "validation-only"],
|
|
2810
|
+
qualities: {
|
|
2811
|
+
realtime: true,
|
|
2812
|
+
deterministic: true
|
|
2813
|
+
}
|
|
2814
|
+
});
|
|
2815
|
+
this.gravity = gravity;
|
|
2816
|
+
}
|
|
2817
|
+
async init() {
|
|
2818
|
+
await loadRapier();
|
|
2819
|
+
return this;
|
|
2820
|
+
}
|
|
2821
|
+
createWorld() {
|
|
2822
|
+
return new RAPIER.World(this.gravity);
|
|
2823
|
+
}
|
|
2824
|
+
step(world, dt) {
|
|
2825
|
+
world.timestep = dt;
|
|
2826
|
+
world.step();
|
|
2827
|
+
world.bodies.forEach((body) => {
|
|
2828
|
+
if (body.isDynamic()) {
|
|
2829
|
+
body.resetForces(false);
|
|
2830
|
+
body.resetTorques(false);
|
|
2831
|
+
}
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
dispose(world) {
|
|
2835
|
+
world?.free?.();
|
|
2836
|
+
}
|
|
2837
|
+
debugSnapshot(world, { nativeGeometry = true } = {}) {
|
|
2838
|
+
return {
|
|
2839
|
+
backend: this.identity,
|
|
2840
|
+
nativeGeometry: nativeGeometry && world?.debugRender ? world.debugRender() : null,
|
|
2841
|
+
metrics: {}
|
|
2842
|
+
};
|
|
2843
|
+
}
|
|
2844
|
+
createBody(world, { type = "fixed", position = null, rotation = null, canSleep = true, lockTranslation = null, lockRotation = null } = {}) {
|
|
2845
|
+
const body = world.createRigidBody(createBodyDesc(type, position, {
|
|
2846
|
+
canSleep,
|
|
2847
|
+
lockTranslation,
|
|
2848
|
+
lockRotation
|
|
2849
|
+
}));
|
|
2850
|
+
if (rotation) body.setRotation(q4(rotation), true);
|
|
2851
|
+
return body;
|
|
2852
|
+
}
|
|
2853
|
+
removeBody(world, body) {
|
|
2854
|
+
if (body) world.removeRigidBody(body);
|
|
2855
|
+
}
|
|
2856
|
+
createColliders(world, body, specs = [], { mass, friction, restitution } = {}) {
|
|
2857
|
+
const colliderMass = mass != null && specs.length ? mass / specs.length : null;
|
|
2858
|
+
const created = [];
|
|
2859
|
+
for (const spec of specs) {
|
|
2860
|
+
const desc = createColliderDesc(spec);
|
|
2861
|
+
if (!desc) continue;
|
|
2862
|
+
if (spec.translation) desc.setTranslation(...spec.translation);
|
|
2863
|
+
if (spec.rotation) desc.setRotation(q4(spec.rotation));
|
|
2864
|
+
if (colliderMass != null) desc.setMass(colliderMass);
|
|
2865
|
+
if (friction != null) desc.setFriction(friction);
|
|
2866
|
+
if (restitution != null) desc.setRestitution(restitution);
|
|
2867
|
+
created.push(world.createCollider(desc, body));
|
|
2868
|
+
}
|
|
2869
|
+
return created;
|
|
2870
|
+
}
|
|
2871
|
+
colliders(body) {
|
|
2872
|
+
if (!body) return [];
|
|
2873
|
+
return Array.from({ length: body.numColliders() }, (_, index) => body.collider(index));
|
|
2874
|
+
}
|
|
2875
|
+
bodyKey(body) {
|
|
2876
|
+
return body?.handle ?? null;
|
|
2877
|
+
}
|
|
2878
|
+
colliderKey(collider) {
|
|
2879
|
+
return collider?.handle ?? null;
|
|
2880
|
+
}
|
|
2881
|
+
colliderParent(collider) {
|
|
2882
|
+
return collider?.parent?.() || null;
|
|
2883
|
+
}
|
|
2884
|
+
colliderSnapshot(collider) {
|
|
2885
|
+
if (!collider) return null;
|
|
2886
|
+
const position = collider.translation();
|
|
2887
|
+
const rotation = collider.rotation();
|
|
2888
|
+
return {
|
|
2889
|
+
ref: collider,
|
|
2890
|
+
key: this.colliderKey(collider),
|
|
2891
|
+
parent: this.colliderParent(collider),
|
|
2892
|
+
position: v3(position),
|
|
2893
|
+
rotation: q4(rotation),
|
|
2894
|
+
shape: describeShape(collider.shape),
|
|
2895
|
+
shapeRef: collider.shape
|
|
2896
|
+
};
|
|
2897
|
+
}
|
|
2898
|
+
bodyType(body) {
|
|
2899
|
+
return body?.isKinematic?.() ? "kinematic" : body?.isDynamic?.() ? "dynamic" : "fixed";
|
|
2900
|
+
}
|
|
2901
|
+
setBodyType(body, type) {
|
|
2902
|
+
if (type === "kinematic") body.setBodyType(RAPIER.RigidBodyType.KinematicPositionBased, true);
|
|
2903
|
+
else if (type === "dynamic") body.setBodyType(RAPIER.RigidBodyType.Dynamic, true);
|
|
2904
|
+
else body.setBodyType(RAPIER.RigidBodyType.Fixed, true);
|
|
2905
|
+
return true;
|
|
2906
|
+
}
|
|
2907
|
+
bodyPose(body, { next = false } = {}) {
|
|
2908
|
+
if (!body) return null;
|
|
2909
|
+
const position = next && body.nextTranslation ? body.nextTranslation() : body.translation();
|
|
2910
|
+
const rotation = next && body.nextRotation ? body.nextRotation() : body.rotation();
|
|
2911
|
+
return {
|
|
2912
|
+
position: array3(position),
|
|
2913
|
+
rotation: array4(rotation)
|
|
2914
|
+
};
|
|
2915
|
+
}
|
|
2916
|
+
setBodyPose(body, { position = null, rotation = null, next = false, wake = true } = {}) {
|
|
2917
|
+
if (!body) return false;
|
|
2918
|
+
if (next) {
|
|
2919
|
+
if (position) body.setNextKinematicTranslation(v3(position));
|
|
2920
|
+
if (rotation) body.setNextKinematicRotation(q4(rotation));
|
|
2921
|
+
} else {
|
|
2922
|
+
if (position) body.setTranslation(v3(position), wake);
|
|
2923
|
+
if (rotation) body.setRotation(q4(rotation), wake);
|
|
2924
|
+
}
|
|
2925
|
+
return true;
|
|
2926
|
+
}
|
|
2927
|
+
translateBody(body, delta, { clearLinearVelocity = true, wake = true } = {}) {
|
|
2928
|
+
const pose = this.bodyPose(body);
|
|
2929
|
+
if (!pose) return false;
|
|
2930
|
+
this.setBodyPose(body, {
|
|
2931
|
+
position: [
|
|
2932
|
+
pose.position[0] + delta[0],
|
|
2933
|
+
pose.position[1] + delta[1],
|
|
2934
|
+
pose.position[2] + delta[2]
|
|
2935
|
+
],
|
|
2936
|
+
wake
|
|
2937
|
+
});
|
|
2938
|
+
if (clearLinearVelocity) body.setLinvel?.({
|
|
2939
|
+
x: 0,
|
|
2940
|
+
y: 0,
|
|
2941
|
+
z: 0
|
|
2942
|
+
}, wake);
|
|
2943
|
+
if (wake) body.wakeUp?.();
|
|
2944
|
+
return true;
|
|
2945
|
+
}
|
|
2946
|
+
clearBodyMotion(body, { wake = false } = {}) {
|
|
2947
|
+
if (!body) return false;
|
|
2948
|
+
body.setLinvel?.({
|
|
2949
|
+
x: 0,
|
|
2950
|
+
y: 0,
|
|
2951
|
+
z: 0
|
|
2952
|
+
}, wake);
|
|
2953
|
+
body.setAngvel?.({
|
|
2954
|
+
x: 0,
|
|
2955
|
+
y: 0,
|
|
2956
|
+
z: 0
|
|
2957
|
+
}, wake);
|
|
2958
|
+
if (wake) body.wakeUp?.();
|
|
2959
|
+
return true;
|
|
2960
|
+
}
|
|
2961
|
+
bodyMotion(body) {
|
|
2962
|
+
if (!body) return null;
|
|
2963
|
+
const linear = body.linvel(), angular = body.angvel();
|
|
2964
|
+
return {
|
|
2965
|
+
sleeping: body.isSleeping(),
|
|
2966
|
+
linearVelocity: array3(linear),
|
|
2967
|
+
angularVelocity: array3(angular),
|
|
2968
|
+
linearSpeed: Math.hypot(linear.x, linear.y, linear.z),
|
|
2969
|
+
angularSpeed: Math.hypot(angular.x, angular.y, angular.z)
|
|
2970
|
+
};
|
|
2971
|
+
}
|
|
2972
|
+
setBodyMotion(body, { linearVelocity = null, angularVelocity = null, wake = true } = {}) {
|
|
2973
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
2974
|
+
if (linearVelocity) body.setLinvel(v3(linearVelocity), wake);
|
|
2975
|
+
if (angularVelocity) body.setAngvel(v3(angularVelocity), wake);
|
|
2976
|
+
return true;
|
|
2977
|
+
}
|
|
2978
|
+
applyImpulse(body, impulse, { point = null, wake = true } = {}) {
|
|
2979
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
2980
|
+
if (point) body.applyImpulseAtPoint(v3(impulse), v3(point), wake);
|
|
2981
|
+
else body.applyImpulse(v3(impulse), wake);
|
|
2982
|
+
return true;
|
|
2983
|
+
}
|
|
2984
|
+
applyForce(body, force, { point = null, wake = true } = {}) {
|
|
2985
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
2986
|
+
if (point) body.addForceAtPoint(v3(force), v3(point), wake);
|
|
2987
|
+
else body.addForce(v3(force), wake);
|
|
2988
|
+
return true;
|
|
2989
|
+
}
|
|
2990
|
+
applyTorque(body, torque, { wake = true } = {}) {
|
|
2991
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
2992
|
+
body.addTorque(v3(torque), wake);
|
|
2993
|
+
return true;
|
|
2994
|
+
}
|
|
2995
|
+
setBodyMaterial(body, { friction = null, restitution = null } = {}) {
|
|
2996
|
+
if (!body) return false;
|
|
2997
|
+
for (const collider of this.colliders(body)) {
|
|
2998
|
+
if (friction != null) collider.setFriction(friction);
|
|
2999
|
+
if (restitution != null) collider.setRestitution(restitution);
|
|
3000
|
+
}
|
|
3001
|
+
return true;
|
|
3002
|
+
}
|
|
3003
|
+
setBodyDynamics(body, { linearDamping = null, angularDamping = null, gravityScale = null, wake = true } = {}) {
|
|
3004
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
3005
|
+
if (linearDamping != null) body.setLinearDamping(linearDamping);
|
|
3006
|
+
if (angularDamping != null) body.setAngularDamping(angularDamping);
|
|
3007
|
+
if (gravityScale != null) body.setGravityScale(gravityScale, wake);
|
|
3008
|
+
return true;
|
|
3009
|
+
}
|
|
3010
|
+
setBodyCcd(body, enabled) {
|
|
3011
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
3012
|
+
body.enableCcd(Boolean(enabled));
|
|
3013
|
+
return true;
|
|
3014
|
+
}
|
|
3015
|
+
setBodySensor(body, enabled) {
|
|
3016
|
+
if (!body) return false;
|
|
3017
|
+
for (const collider of this.colliders(body)) collider.setSensor(Boolean(enabled));
|
|
3018
|
+
return true;
|
|
3019
|
+
}
|
|
3020
|
+
setBodyCollisionFilter(body, filter = null) {
|
|
3021
|
+
if (!body) return false;
|
|
3022
|
+
const groups = filter ? ((filter.membership & 65535) << 16 | filter.filter & 65535) >>> 0 : 4294967295;
|
|
3023
|
+
for (const collider of this.colliders(body)) collider.setCollisionGroups(groups);
|
|
3024
|
+
return true;
|
|
3025
|
+
}
|
|
3026
|
+
sleepBody(body) {
|
|
3027
|
+
if (!body || this.bodyType(body) !== "dynamic") return false;
|
|
3028
|
+
body.sleep();
|
|
3029
|
+
return true;
|
|
3030
|
+
}
|
|
3031
|
+
wakeBody(body) {
|
|
3032
|
+
body?.wakeUp?.();
|
|
3033
|
+
return Boolean(body);
|
|
3034
|
+
}
|
|
3035
|
+
createJoint(world, part, parentBody, childBody) {
|
|
3036
|
+
return createJoint(world, part, parentBody, childBody);
|
|
3037
|
+
}
|
|
3038
|
+
setJointTarget(joint, target, { stiffness = 40, damping = 8 } = {}) {
|
|
3039
|
+
if (!joint || joint.type?.() === RAPIER.JointType.Fixed) return false;
|
|
3040
|
+
joint.configureMotorPosition(target ?? 0, target == null ? 0 : stiffness, target == null ? 0 : damping);
|
|
3041
|
+
return true;
|
|
3042
|
+
}
|
|
3043
|
+
createCharacterController(world, { offset = .02, autostepHeight = .3, autostepMinWidth = .2, snapToGround = .3, maxSlopeClimbAngle = Math.PI / 4, minSlopeSlideAngle = Math.PI / 6 } = {}) {
|
|
3044
|
+
const controller = world.createCharacterController(offset);
|
|
3045
|
+
controller.enableAutostep(autostepHeight, autostepMinWidth, false);
|
|
3046
|
+
controller.enableSnapToGround(snapToGround);
|
|
3047
|
+
controller.setMaxSlopeClimbAngle(maxSlopeClimbAngle);
|
|
3048
|
+
controller.setMinSlopeSlideAngle(minSlopeSlideAngle);
|
|
3049
|
+
controller.setApplyImpulsesToDynamicBodies(false);
|
|
3050
|
+
return controller;
|
|
3051
|
+
}
|
|
3052
|
+
removeCharacterController(world, controller) {
|
|
3053
|
+
if (world && controller) world.removeCharacterController(controller);
|
|
3054
|
+
}
|
|
3055
|
+
cancelCharacterMovement(body) {
|
|
3056
|
+
const pose = this.bodyPose(body);
|
|
3057
|
+
if (!pose) return false;
|
|
3058
|
+
return this.setBodyPose(body, {
|
|
3059
|
+
...pose,
|
|
3060
|
+
next: true
|
|
3061
|
+
});
|
|
3062
|
+
}
|
|
3063
|
+
moveCharacter(controller, body, desiredTranslation, { predicate = null } = {}) {
|
|
3064
|
+
if (!controller || this.bodyType(body) !== "kinematic") return {
|
|
3065
|
+
success: false,
|
|
3066
|
+
code: "CHARACTER_BODY_UNAVAILABLE",
|
|
3067
|
+
movement: [
|
|
3068
|
+
0,
|
|
3069
|
+
0,
|
|
3070
|
+
0
|
|
3071
|
+
],
|
|
3072
|
+
grounded: false,
|
|
3073
|
+
collisions: []
|
|
3074
|
+
};
|
|
3075
|
+
const colliders = this.colliders(body);
|
|
3076
|
+
if (colliders.length !== 1) return {
|
|
3077
|
+
success: false,
|
|
3078
|
+
code: "CHARACTER_BODY_UNAVAILABLE",
|
|
3079
|
+
movement: [
|
|
3080
|
+
0,
|
|
3081
|
+
0,
|
|
3082
|
+
0
|
|
3083
|
+
],
|
|
3084
|
+
grounded: false,
|
|
3085
|
+
collisions: []
|
|
3086
|
+
};
|
|
3087
|
+
controller.computeColliderMovement(colliders[0], v3(desiredTranslation), void 0, void 0, predicate || void 0);
|
|
3088
|
+
const movement = controller.computedMovement();
|
|
3089
|
+
const pose = this.bodyPose(body);
|
|
3090
|
+
this.setBodyPose(body, {
|
|
3091
|
+
position: [
|
|
3092
|
+
pose.position[0] + movement.x,
|
|
3093
|
+
pose.position[1] + movement.y,
|
|
3094
|
+
pose.position[2] + movement.z
|
|
3095
|
+
],
|
|
3096
|
+
next: true
|
|
3097
|
+
});
|
|
3098
|
+
const collisions = [];
|
|
3099
|
+
for (let index = 0; index < controller.numComputedCollisions(); index++) {
|
|
3100
|
+
const hit = controller.computedCollision(index);
|
|
3101
|
+
if (!hit?.collider) continue;
|
|
3102
|
+
collisions.push({
|
|
3103
|
+
colliderKey: this.colliderKey(hit.collider),
|
|
3104
|
+
toi: hit.toi,
|
|
3105
|
+
normal: [
|
|
3106
|
+
hit.normal1.x,
|
|
3107
|
+
hit.normal1.y,
|
|
3108
|
+
hit.normal1.z
|
|
3109
|
+
]
|
|
3110
|
+
});
|
|
3111
|
+
}
|
|
3112
|
+
return {
|
|
3113
|
+
success: true,
|
|
3114
|
+
movement: array3(movement),
|
|
3115
|
+
grounded: controller.computedGrounded(),
|
|
3116
|
+
collisions
|
|
3117
|
+
};
|
|
3118
|
+
}
|
|
3119
|
+
syncSceneQueries(world) {
|
|
3120
|
+
if (!world) return;
|
|
3121
|
+
if (typeof world.updateSceneQueries === "function") world.updateSceneQueries();
|
|
3122
|
+
else world.propagateModifiedBodyPositionsToColliders?.();
|
|
3123
|
+
}
|
|
3124
|
+
createQueryShape(spec) {
|
|
3125
|
+
return createQueryShape(spec);
|
|
3126
|
+
}
|
|
3127
|
+
disposeQueryShape(shape) {
|
|
3128
|
+
shape?.free?.();
|
|
3129
|
+
}
|
|
3130
|
+
intersectionsWithShape(world, position, rotation, shape, callback, { excludeCollider = null, excludeBody = null, predicate = null } = {}) {
|
|
3131
|
+
this.syncSceneQueries(world);
|
|
3132
|
+
let active = true;
|
|
3133
|
+
world?.forEachCollider((other) => {
|
|
3134
|
+
if (!active || other.isEnabled?.() === false) return;
|
|
3135
|
+
if (excludeCollider && this.colliderKey(other) === this.colliderKey(excludeCollider)) return;
|
|
3136
|
+
const parent = this.colliderParent(other);
|
|
3137
|
+
if (excludeBody && this.bodyKey(parent) === this.bodyKey(excludeBody)) return;
|
|
3138
|
+
if (predicate && !predicate(other)) return;
|
|
3139
|
+
if (!other.intersectsShape(shape, v3(position), q4(rotation))) return;
|
|
3140
|
+
active = callback(other) !== false;
|
|
3141
|
+
});
|
|
3142
|
+
}
|
|
3143
|
+
castCollider(world, source, velocity, { excludeCollider = null, excludeBody = null, predicate = null, targetDistance = 0, maxToi = 1, stopAtPenetration = false } = {}) {
|
|
3144
|
+
this.syncSceneQueries(world);
|
|
3145
|
+
let best = null;
|
|
3146
|
+
const zero = {
|
|
3147
|
+
x: 0,
|
|
3148
|
+
y: 0,
|
|
3149
|
+
z: 0
|
|
3150
|
+
};
|
|
3151
|
+
world?.forEachCollider((other) => {
|
|
3152
|
+
if (other.isEnabled?.() === false) return;
|
|
3153
|
+
if (this.colliderKey(other) === this.colliderKey(source)) return;
|
|
3154
|
+
if (excludeCollider && this.colliderKey(other) === this.colliderKey(excludeCollider)) return;
|
|
3155
|
+
const parent = this.colliderParent(other);
|
|
3156
|
+
if (excludeBody && this.bodyKey(parent) === this.bodyKey(excludeBody)) return;
|
|
3157
|
+
if (predicate && !predicate(other)) return;
|
|
3158
|
+
const hit = source.castCollider(v3(velocity), other, zero, targetDistance, maxToi, stopAtPenetration);
|
|
3159
|
+
if (!hit) return;
|
|
3160
|
+
const colliderKey = this.colliderKey(other);
|
|
3161
|
+
if (!best || hit.time_of_impact < best.timeOfImpact) best = {
|
|
3162
|
+
colliderKey,
|
|
3163
|
+
timeOfImpact: hit.time_of_impact
|
|
3164
|
+
};
|
|
3165
|
+
});
|
|
3166
|
+
return best ? {
|
|
3167
|
+
...best,
|
|
3168
|
+
collider: world.getCollider(best.colliderKey)
|
|
3169
|
+
} : null;
|
|
3170
|
+
}
|
|
3171
|
+
raycast(world, origin, direction, maxDistance, { solid = true, predicate = null } = {}) {
|
|
3172
|
+
this.syncSceneQueries(world);
|
|
3173
|
+
const ray = new RAPIER.Ray(v3(origin), v3(direction));
|
|
3174
|
+
let best = null;
|
|
3175
|
+
world?.forEachCollider((collider) => {
|
|
3176
|
+
if (collider.isEnabled?.() === false) return;
|
|
3177
|
+
if (predicate && !predicate(collider)) return;
|
|
3178
|
+
const toi = collider.castRay(ray, maxDistance, solid);
|
|
3179
|
+
if (!Number.isFinite(toi) || toi < 0) return;
|
|
3180
|
+
const colliderKey = this.colliderKey(collider);
|
|
3181
|
+
if (!best || toi < best.timeOfImpact) best = {
|
|
3182
|
+
colliderKey,
|
|
3183
|
+
timeOfImpact: toi
|
|
3184
|
+
};
|
|
3185
|
+
});
|
|
3186
|
+
return best ? {
|
|
3187
|
+
...best,
|
|
3188
|
+
collider: world.getCollider(best.colliderKey)
|
|
3189
|
+
} : null;
|
|
3190
|
+
}
|
|
3191
|
+
contactPairs(world, source) {
|
|
3192
|
+
const pairs = [];
|
|
3193
|
+
world.contactPairsWith(source, (other) => {
|
|
3194
|
+
let manifoldCount = 0, contactCount = 0, activeContactCount = 0, minDistance = 1e400, totalImpulse = 0, normal = null;
|
|
3195
|
+
world.contactPair(source, other, (manifold, flipped) => {
|
|
3196
|
+
manifoldCount += 1;
|
|
3197
|
+
const rawNormal = manifold.normal();
|
|
3198
|
+
normal = flipped ? [
|
|
3199
|
+
-rawNormal.x,
|
|
3200
|
+
-rawNormal.y,
|
|
3201
|
+
-rawNormal.z
|
|
3202
|
+
] : [
|
|
3203
|
+
rawNormal.x,
|
|
3204
|
+
rawNormal.y,
|
|
3205
|
+
rawNormal.z
|
|
3206
|
+
];
|
|
3207
|
+
for (let index = 0; index < manifold.numContacts(); index++) {
|
|
3208
|
+
const distance = manifold.contactDist(index);
|
|
3209
|
+
const impulse = Math.abs(manifold.contactImpulse(index) || 0);
|
|
3210
|
+
contactCount += 1;
|
|
3211
|
+
if (distance <= 1e-6 || impulse > 1e-8) activeContactCount += 1;
|
|
3212
|
+
minDistance = Math.min(minDistance, distance);
|
|
3213
|
+
totalImpulse += impulse;
|
|
3214
|
+
}
|
|
3215
|
+
});
|
|
3216
|
+
if (manifoldCount && activeContactCount) pairs.push({
|
|
3217
|
+
otherKey: this.colliderKey(other),
|
|
3218
|
+
manifoldCount,
|
|
3219
|
+
contactCount,
|
|
3220
|
+
activeContactCount,
|
|
3221
|
+
minDistance: Number.isFinite(minDistance) ? minDistance : null,
|
|
3222
|
+
totalImpulse,
|
|
3223
|
+
normal,
|
|
3224
|
+
evidenceKind: "solver-contact",
|
|
3225
|
+
impulseAvailable: true
|
|
3226
|
+
});
|
|
3227
|
+
});
|
|
3228
|
+
return pairs.map((pair) => ({
|
|
3229
|
+
...pair,
|
|
3230
|
+
other: world.getCollider(pair.otherKey)
|
|
3231
|
+
}));
|
|
3232
|
+
}
|
|
3233
|
+
penetrations(world, source) {
|
|
3234
|
+
const sourceSnapshot = this.colliderSnapshot(source);
|
|
3235
|
+
const sourceBody = sourceSnapshot?.parent;
|
|
3236
|
+
if (!sourceSnapshot) return [];
|
|
3237
|
+
const hits = [];
|
|
3238
|
+
this.intersectionsWithShape(world, sourceSnapshot.position, sourceSnapshot.rotation, sourceSnapshot.shapeRef, (other) => {
|
|
3239
|
+
const otherBody = this.colliderParent(other);
|
|
3240
|
+
if (!otherBody || this.bodyKey(otherBody) === this.bodyKey(sourceBody)) return true;
|
|
3241
|
+
const contact = source.contactCollider(other, 0);
|
|
3242
|
+
if (contact && contact.distance < 0) hits.push({
|
|
3243
|
+
otherKey: this.colliderKey(other),
|
|
3244
|
+
distance: contact.distance
|
|
3245
|
+
});
|
|
3246
|
+
return true;
|
|
3247
|
+
}, { excludeCollider: source });
|
|
3248
|
+
return hits.map((hit) => ({
|
|
3249
|
+
...hit,
|
|
3250
|
+
other: world.getCollider(hit.otherKey)
|
|
3251
|
+
}));
|
|
3252
|
+
}
|
|
3253
|
+
shapesIntersect(leftShape, leftPosition, leftRotation, rightShape, rightPosition, rightRotation) {
|
|
3254
|
+
return leftShape.intersectsShape(v3(leftPosition), q4(leftRotation), rightShape, v3(rightPosition), q4(rightRotation));
|
|
3255
|
+
}
|
|
3256
|
+
};
|
|
3257
|
+
export { Errors as i, PhysicsSystem as n, ObjectStore as r, RapierPhysicsBackend as t };
|
|
3258
|
+
|
|
3259
|
+
//# sourceMappingURL=RapierPhysicsBackend-B2wQe_Vl.js.map
|