threedog 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/core/Animator.js +268 -0
- package/core/Assets.js +256 -0
- package/core/Audio.js +267 -0
- package/core/Baked.js +72 -0
- package/core/Batching.js +247 -0
- package/core/Budget.js +221 -0
- package/core/Clock.js +94 -0
- package/core/Commits.js +77 -0
- package/core/DebugOverrides.js +143 -0
- package/core/Describe.js +145 -0
- package/core/Engine.js +184 -0
- package/core/Events.js +153 -0
- package/core/I18n.js +94 -0
- package/core/Input.js +245 -0
- package/core/Inspect.js +77 -0
- package/core/InspectScene.js +191 -0
- package/core/Jobs.js +146 -0
- package/core/Loading.js +149 -0
- package/core/MaterialInfo.js +32 -0
- package/core/Ops.js +256 -0
- package/core/Random.js +52 -0
- package/core/Rays.js +160 -0
- package/core/SaveStore.js +85 -0
- package/core/Saves.js +254 -0
- package/core/Stream.js +279 -0
- package/core/Threads.js +158 -0
- package/core/UrlState.js +94 -0
- package/core/Vars.js +35 -0
- package/core/Views.js +49 -0
- package/core/actor.js +23 -0
- package/core/assetEntry.js +89 -0
- package/core/assetLoad.js +190 -0
- package/core/assetManifest.js +35 -0
- package/core/assetReport.js +83 -0
- package/core/audioRules.js +121 -0
- package/core/audioVoice.js +225 -0
- package/core/batchGroups.js +116 -0
- package/core/canvasBox.js +63 -0
- package/core/catalog.js +169 -0
- package/core/checks.js +67 -0
- package/core/claims.js +51 -0
- package/core/dayTime.js +15 -0
- package/core/debugView.js +76 -0
- package/core/describeRender.js +45 -0
- package/core/describeText.js +143 -0
- package/core/easing.js +34 -0
- package/core/errorCodes.js +205 -0
- package/core/errorHints.js +208 -0
- package/core/errors.js +40 -0
- package/core/events.generated.js +84 -0
- package/core/ground.js +102 -0
- package/core/holds.js +106 -0
- package/core/inputContext.js +69 -0
- package/core/inputDefaults.js +22 -0
- package/core/inputPad.js +35 -0
- package/core/inputPointer.js +48 -0
- package/core/inputTouch.js +48 -0
- package/core/lifecycle.js +69 -0
- package/core/lightWatch.js +56 -0
- package/core/look/coreParts.js +112 -0
- package/core/look/layers.js +136 -0
- package/core/look/part.js +173 -0
- package/core/look/report.js +79 -0
- package/core/look/write.js +86 -0
- package/core/loop.js +200 -0
- package/core/math.js +33 -0
- package/core/modules.js +196 -0
- package/core/movement.js +19 -0
- package/core/mulberry32.js +14 -0
- package/core/opsAtomic.js +31 -0
- package/core/opsCheck.js +97 -0
- package/core/opsFail.js +20 -0
- package/core/opsGuard.js +26 -0
- package/core/opsHistory.js +27 -0
- package/core/opsRun.js +77 -0
- package/core/page.js +78 -0
- package/core/params.js +132 -0
- package/core/pureWorker.js +5 -0
- package/core/renamed.js +21 -0
- package/core/saveApply.js +128 -0
- package/core/saveFiles.js +139 -0
- package/core/saveIds.js +53 -0
- package/core/saveVersions.js +97 -0
- package/core/scatter.js +92 -0
- package/core/sceneMethods.js +100 -0
- package/core/screenToGround.js +39 -0
- package/core/services.js +73 -0
- package/core/session.js +171 -0
- package/core/shadowTypes.js +15 -0
- package/core/skip.js +104 -0
- package/core/startup.js +135 -0
- package/core/streamCells.js +24 -0
- package/core/streamSources.js +73 -0
- package/core/styleParts.js +96 -0
- package/core/targeting.js +64 -0
- package/core/thingExplain.js +68 -0
- package/core/thingOps.js +168 -0
- package/core/things.js +212 -0
- package/core/threadPure.js +120 -0
- package/core/threadServe.js +25 -0
- package/core/tier.js +11 -0
- package/core/util.js +11 -0
- package/core/validate.js +204 -0
- package/docs/AI.md +123 -0
- package/docs/API.md +83 -0
- package/docs/CODEMAP.md +347 -0
- package/docs/COMMANDS.md +108 -0
- package/docs/CONVENTIONS.md +145 -0
- package/docs/PERF.md +217 -0
- package/docs/THREE.md +86 -0
- package/docs/api/ambience.md +15 -0
- package/docs/api/assets.md +62 -0
- package/docs/api/audio.md +53 -0
- package/docs/api/budget.md +40 -0
- package/docs/api/cameraRig.md +120 -0
- package/docs/api/characters.md +144 -0
- package/docs/api/clock.md +7 -0
- package/docs/api/collision.md +39 -0
- package/docs/api/content.md +64 -0
- package/docs/api/dialogue.md +39 -0
- package/docs/api/engine.md +199 -0
- package/docs/api/entities.md +61 -0
- package/docs/api/events.md +95 -0
- package/docs/api/flow.md +27 -0
- package/docs/api/grids.md +11 -0
- package/docs/api/ground.md +16 -0
- package/docs/api/hlod.md +34 -0
- package/docs/api/hud.md +31 -0
- package/docs/api/i18n.md +10 -0
- package/docs/api/input.md +24 -0
- package/docs/api/interactions.md +31 -0
- package/docs/api/inventory.md +66 -0
- package/docs/api/jobs.md +10 -0
- package/docs/api/level.md +41 -0
- package/docs/api/lightPool.md +56 -0
- package/docs/api/music.md +34 -0
- package/docs/api/nav.md +26 -0
- package/docs/api/net.md +14 -0
- package/docs/api/openWorld.md +24 -0
- package/docs/api/particles.md +41 -0
- package/docs/api/photo.md +15 -0
- package/docs/api/post.md +10 -0
- package/docs/api/quests.md +36 -0
- package/docs/api/rays.md +26 -0
- package/docs/api/rigid.md +84 -0
- package/docs/api/saves.md +32 -0
- package/docs/api/settings.md +36 -0
- package/docs/api/sky.md +58 -0
- package/docs/api/stream.md +31 -0
- package/docs/api/style.md +134 -0
- package/docs/api/terrain.md +34 -0
- package/docs/api/timeline.md +39 -0
- package/docs/api/tools.md +57 -0
- package/docs/api/triggers.md +56 -0
- package/docs/api/vars.md +9 -0
- package/docs/api/vegetation.md +52 -0
- package/docs/api/water.md +48 -0
- package/docs/api/weather.md +29 -0
- package/docs/api/world.md +30 -0
- package/docs/recipes/01-new-game.md +148 -0
- package/docs/recipes/02-npc.md +167 -0
- package/docs/recipes/03-items-pickups.md +158 -0
- package/docs/recipes/04-quests.md +83 -0
- package/docs/recipes/05-props-bake.md +159 -0
- package/docs/recipes/06-clock-events.md +117 -0
- package/docs/recipes/07-flow-scenes-pause.md +132 -0
- package/docs/recipes/08-weather-sky.md +74 -0
- package/docs/recipes/09-style-pack.md +234 -0
- package/docs/recipes/10-blender-level.md +67 -0
- package/docs/recipes/11-vehicles.md +55 -0
- package/docs/recipes/12-navmesh.md +79 -0
- package/docs/recipes/13-animation.md +149 -0
- package/docs/recipes/14-custom-module.md +91 -0
- package/docs/recipes/15-perf.md +78 -0
- package/docs/recipes/16-dev-bridge.md +180 -0
- package/docs/recipes/18-world.md +185 -0
- package/docs/recipes/19-shooting-picking.md +76 -0
- package/docs/recipes/20-content-actions-save.md +176 -0
- package/docs/recipes/21-camera.md +153 -0
- package/docs/recipes/22-shop-gold.md +107 -0
- package/docs/recipes/23-ops.md +175 -0
- package/docs/recipes/26-ui-parts.md +138 -0
- package/docs/recipes/27-net.md +96 -0
- package/docs/recipes/28-terrain.md +80 -0
- package/docs/recipes/29-grid.md +87 -0
- package/docs/recipes/30-templates.md +131 -0
- package/docs/recipes/31-audio.md +126 -0
- package/docs/recipes/32-action-game.md +33 -0
- package/docs/recipes/33-stream-sources.md +37 -0
- package/docs/recipes/34-environment-lighting.md +36 -0
- package/docs/recipes/35-style-from-reference.md +89 -0
- package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
- package/docs/recipes/37-terrain-look-hooks.md +97 -0
- package/docs/recipes/38-environment-look-hooks.md +83 -0
- package/docs/recipes/40-scene.md +108 -0
- package/docs/recipes/41-thing-registry.md +93 -0
- package/docs/recipes/42-terrain-shape.md +64 -0
- package/docs/recipes/43-shader-patches-surface-looks.md +120 -0
- package/docs/recipes/44-water-weather-look-hooks.md +135 -0
- package/docs/recipes/45-cascaded-shadows.md +51 -0
- package/docs/recipes/46-blender-art-route.md +114 -0
- package/docs/recipes/47-island-snow-backdrop.md +44 -0
- package/docs/recipes/48-make-material.md +70 -0
- package/docs/recipes/49-game-module-contract.md +71 -0
- package/docs/recipes/50-own-renderer.md +12 -0
- package/docs/recipes/51-capability-catalog.md +63 -0
- package/docs/recipes/52-look.md +108 -0
- package/examples/action/DESIGN.md +3 -0
- package/examples/action/GAMEMAP.md +127 -0
- package/examples/action/content/actions.js +8 -0
- package/examples/action/content/audio.js +9 -0
- package/examples/action/content/index.js +10 -0
- package/examples/action/content/items.js +4 -0
- package/examples/action/content/places.js +4 -0
- package/examples/action/content/props.js +11 -0
- package/examples/action/content/templates.js +10 -0
- package/examples/action/content/views.js +6 -0
- package/examples/action/look.js +5 -0
- package/examples/action/main.js +137 -0
- package/examples/action/models.js +25 -0
- package/examples/sheepisland/GAMEMAP.md +107 -0
- package/examples/sheepisland/content/index.js +6 -0
- package/examples/sheepisland/content/props.js +19 -0
- package/examples/sheepisland/content/templates.js +5 -0
- package/examples/sheepisland/content/vegetation.js +8 -0
- package/examples/sheepisland/main.js +71 -0
- package/examples/sheepisland/params.js +25 -0
- package/examples/sheepisland/style/index.js +79 -0
- package/examples/sheepisland/style/models.js +59 -0
- package/examples/sheepisland/views.js +9 -0
- package/examples/styletest/GAMEMAP.md +135 -0
- package/examples/styletest/content/index.js +9 -0
- package/examples/styletest/content/items.js +8 -0
- package/examples/styletest/content/npcs.js +8 -0
- package/examples/styletest/content/places.js +5 -0
- package/examples/styletest/content/props.js +16 -0
- package/examples/styletest/content/templates.js +9 -0
- package/examples/styletest/content/vegetation.js +31 -0
- package/examples/styletest/look.js +11 -0
- package/examples/styletest/main.js +95 -0
- package/examples/styletest/models.js +87 -0
- package/examples/styletest/overrides.js +33 -0
- package/examples/styletest/panel.js +135 -0
- package/examples/styletest/params.js +32 -0
- package/examples/styletest/views.js +31 -0
- package/examples/world/DESIGN.md +5 -0
- package/examples/world/GAMEMAP.md +171 -0
- package/examples/world/content/actions.js +8 -0
- package/examples/world/content/audio.js +25 -0
- package/examples/world/content/bodies.js +12 -0
- package/examples/world/content/containers.js +4 -0
- package/examples/world/content/dialogues.js +33 -0
- package/examples/world/content/index.js +17 -0
- package/examples/world/content/items.js +10 -0
- package/examples/world/content/npcs.js +14 -0
- package/examples/world/content/pickups.js +11 -0
- package/examples/world/content/places.js +6 -0
- package/examples/world/content/props.js +12 -0
- package/examples/world/content/quests.js +15 -0
- package/examples/world/content/rules.js +23 -0
- package/examples/world/content/templates.js +5 -0
- package/examples/world/content/vegetation.js +32 -0
- package/examples/world/main.js +168 -0
- package/examples/world/models.js +28 -0
- package/index.d.ts +306 -0
- package/index.js +122 -0
- package/llms.txt +61 -0
- package/modules/ambience/Ambience.js +186 -0
- package/modules/camera/FirstPersonCamera.js +131 -0
- package/modules/camera/FixedCamera.js +162 -0
- package/modules/camera/FollowCamera.js +91 -0
- package/modules/camera/RtsCamera.js +132 -0
- package/modules/camera/common.js +182 -0
- package/modules/camera/index.js +11 -0
- package/modules/camera/projection.js +34 -0
- package/modules/characters/Character.js +235 -0
- package/modules/characters/NPC.js +242 -0
- package/modules/characters/assetModel.js +42 -0
- package/modules/characters/astar.js +66 -0
- package/modules/characters/behaviors.js +72 -0
- package/modules/characters/footsteps.js +13 -0
- package/modules/characters/index.js +215 -0
- package/modules/characters/moveHelpers.js +29 -0
- package/modules/characters/movers/common.js +62 -0
- package/modules/characters/movers/rapier.js +74 -0
- package/modules/characters/movers/simple.js +43 -0
- package/modules/characters/npcAvoid.js +129 -0
- package/modules/characters/npcSim.js +150 -0
- package/modules/characters/npcView.js +42 -0
- package/modules/characters/pathAsync.js +113 -0
- package/modules/characters/pathJob.js +62 -0
- package/modules/characters/pathWorker.js +7 -0
- package/modules/characters/playerStrings.js +12 -0
- package/modules/characters/proceduralRig.js +58 -0
- package/modules/characters/useModel.js +22 -0
- package/modules/characters/walkTo.js +58 -0
- package/modules/collision/Collision.js +274 -0
- package/modules/collision/grid.js +63 -0
- package/modules/content/explain.js +13 -0
- package/modules/content/ids.js +95 -0
- package/modules/content/index.js +154 -0
- package/modules/content/loader.js +190 -0
- package/modules/content/models.js +69 -0
- package/modules/content/packs.js +69 -0
- package/modules/content/props.js +89 -0
- package/modules/content/rules.js +134 -0
- package/modules/content/templates.js +189 -0
- package/modules/debug/DebugPanel.js +174 -0
- package/modules/entities/Entities.js +243 -0
- package/modules/entities/build.js +25 -0
- package/modules/entities/index.js +4 -0
- package/modules/flow/index.js +101 -0
- package/modules/gameplay/Container.js +227 -0
- package/modules/gameplay/Containers.js +89 -0
- package/modules/gameplay/Dialogue.js +219 -0
- package/modules/gameplay/Interactions.js +253 -0
- package/modules/gameplay/Inventory.js +224 -0
- package/modules/gameplay/Quests.js +205 -0
- package/modules/gameplay/boxOps.js +60 -0
- package/modules/gameplay/dialogueBox.js +48 -0
- package/modules/gameplay/interactionPrompt.js +28 -0
- package/modules/gameplay/inventoryOps.js +137 -0
- package/modules/gameplay/pickups.js +115 -0
- package/modules/gameplay/questLog.js +54 -0
- package/modules/gameplay/questSave.js +71 -0
- package/modules/grid/Grid.js +184 -0
- package/modules/grid/Layer.js +142 -0
- package/modules/grid/index.js +236 -0
- package/modules/grid/ops.js +73 -0
- package/modules/grid/preview.js +85 -0
- package/modules/grid/tiles.js +79 -0
- package/modules/hlod/index.js +167 -0
- package/modules/hud/index.js +167 -0
- package/modules/hud/parts.js +80 -0
- package/modules/level/geometry.js +46 -0
- package/modules/level/heightfield.js +160 -0
- package/modules/level/index.js +233 -0
- package/modules/level/nodes.js +84 -0
- package/modules/lights/index.js +227 -0
- package/modules/music/Music.js +294 -0
- package/modules/music/index.js +2 -0
- package/modules/music/rules.js +28 -0
- package/modules/nav/index.js +156 -0
- package/modules/nav/navPath.js +17 -0
- package/modules/net/Client.js +137 -0
- package/modules/net/Host.js +117 -0
- package/modules/net/Net.js +49 -0
- package/modules/net/index.js +77 -0
- package/modules/net/sync.js +114 -0
- package/modules/net/transports.js +118 -0
- package/modules/particles/Particles.js +179 -0
- package/modules/particles/effect.js +112 -0
- package/modules/particles/shaders.js +28 -0
- package/modules/photo/Photo.js +146 -0
- package/modules/rigid/RigidBodies.js +265 -0
- package/modules/rigid/Vehicles.js +97 -0
- package/modules/rigid/sync.js +158 -0
- package/modules/settings/Settings.js +297 -0
- package/modules/settings/apply.js +21 -0
- package/modules/settings/index.js +3 -0
- package/modules/settings/sections.js +156 -0
- package/modules/settings/strings.js +53 -0
- package/modules/settings/view.js +65 -0
- package/modules/sky/Sky.js +259 -0
- package/modules/sky/calendar.js +80 -0
- package/modules/sky/cascades.js +125 -0
- package/modules/sky/clouds.js +79 -0
- package/modules/sky/domeLayers.js +33 -0
- package/modules/sky/environment.js +141 -0
- package/modules/sky/fog.js +139 -0
- package/modules/sky/frame.js +77 -0
- package/modules/sky/layers.js +69 -0
- package/modules/sky/look.js +51 -0
- package/modules/sky/lookFog.js +80 -0
- package/modules/sky/lookShadow.js +50 -0
- package/modules/sky/lookSky.js +108 -0
- package/modules/sky/schedule.js +80 -0
- package/modules/sky/shader.js +96 -0
- package/modules/sky/shadow.js +74 -0
- package/modules/sky/skyModule.js +60 -0
- package/modules/terrain/ChunkManager.js +260 -0
- package/modules/terrain/chunkJob.js +19 -0
- package/modules/terrain/chunkLevels.js +40 -0
- package/modules/terrain/chunkQueries.js +42 -0
- package/modules/terrain/chunkWorker.js +5 -0
- package/modules/terrain/index.js +118 -0
- package/modules/terrain/lookGround.js +61 -0
- package/modules/terrain/mountTerrain.js +32 -0
- package/modules/terrain/terrainColorMap.js +41 -0
- package/modules/terrain/terrainDefaults.js +39 -0
- package/modules/terrain/terrainEdit.js +139 -0
- package/modules/terrain/terrainGen.js +155 -0
- package/modules/terrain/terrainIsland.js +131 -0
- package/modules/terrain/terrainMesh.js +92 -0
- package/modules/terrain/terrainShape.js +108 -0
- package/modules/terrain/terrainShapeMain.js +41 -0
- package/modules/terrain/terrainTexture.js +44 -0
- package/modules/timeline/Timeline.js +253 -0
- package/modules/timeline/Tween.js +114 -0
- package/modules/timeline/letterbox.js +24 -0
- package/modules/timeline/shots.js +90 -0
- package/modules/triggers/index.js +268 -0
- package/modules/vegetation/Vegetation.js +256 -0
- package/modules/vegetation/groundChunks.js +177 -0
- package/modules/vegetation/index.js +43 -0
- package/modules/vegetation/lookWind.js +37 -0
- package/modules/vegetation/modelGeometry.js +21 -0
- package/modules/water/Water.js +160 -0
- package/modules/water/look.js +80 -0
- package/modules/water/surface.js +88 -0
- package/modules/weather/Weather.js +242 -0
- package/modules/weather/look.js +75 -0
- package/modules/weather/particles.js +92 -0
- package/modules/weather/shaders.js +49 -0
- package/modules/weather/strings.js +15 -0
- package/modules/world/World.js +231 -0
- package/modules/world/cells.js +42 -0
- package/modules/world/explain.js +11 -0
- package/modules/world/index.js +71 -0
- package/modules/world/persist.js +107 -0
- package/modules/world/pos.js +20 -0
- package/modules/world/records.js +89 -0
- package/modules/world/sim.js +175 -0
- package/modules/world/simFar.js +82 -0
- package/modules/world/state.js +115 -0
- package/modules/world/thingSource.js +44 -0
- package/modules/world/views.js +182 -0
- package/package.json +66 -4
- package/presets.js +96 -0
- package/render/PostFX.js +240 -0
- package/render/detail.js +105 -0
- package/render/effects.js +144 -0
- package/render/engineUniforms.js +23 -0
- package/render/fuse.js +84 -0
- package/render/lighting.js +100 -0
- package/render/lights.js +18 -0
- package/render/lookSurface.js +161 -0
- package/render/materials.js +220 -0
- package/render/patches.js +252 -0
- package/render/postLook.js +86 -0
- package/render/surface.js +69 -0
- package/render/uniformDecl.js +29 -0
- package/types/common.d.ts +34 -0
- package/types/core.d.ts +1965 -0
- package/types/gameplay.d.ts +1822 -0
- package/types/generated/core/Audio.d.ts +200 -0
- package/types/generated/core/Batching.d.ts +47 -0
- package/types/generated/core/Budget.d.ts +90 -0
- package/types/generated/core/Commits.d.ts +31 -0
- package/types/generated/core/DebugOverrides.d.ts +52 -0
- package/types/generated/core/Events.d.ts +89 -0
- package/types/generated/core/I18n.d.ts +30 -0
- package/types/generated/core/Input.d.ts +118 -0
- package/types/generated/core/Jobs.d.ts +41 -0
- package/types/generated/core/Ops.d.ts +203 -0
- package/types/generated/core/Random.d.ts +49 -0
- package/types/generated/core/Rays.d.ts +49 -0
- package/types/generated/core/Saves.d.ts +162 -0
- package/types/generated/core/Stream.d.ts +93 -0
- package/types/generated/core/Threads.d.ts +82 -0
- package/types/generated/core/Views.d.ts +33 -0
- package/types/generated/core/audioVoice.d.ts +47 -0
- package/types/generated/core/page.d.ts +18 -0
- package/types/generated/core/session.d.ts +48 -0
- package/types/generated/modules/ambience/Ambience.d.ts +48 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
- package/types/generated/modules/flow/index.d.ts +10 -0
- package/types/generated/modules/grid/Grid.d.ts +151 -0
- package/types/generated/modules/grid/Layer.d.ts +65 -0
- package/types/generated/modules/grid/index.d.ts +86 -0
- package/types/generated/modules/hlod/index.d.ts +11 -0
- package/types/generated/modules/hud/index.d.ts +10 -0
- package/types/generated/modules/lights/index.d.ts +13 -0
- package/types/generated/modules/music/Music.d.ts +111 -0
- package/types/generated/modules/net/Net.d.ts +34 -0
- package/types/generated/modules/net/index.d.ts +53 -0
- package/types/generated/modules/net/transports.d.ts +58 -0
- package/types/generated/modules/particles/Particles.d.ts +69 -0
- package/types/generated/modules/sky/Sky.d.ts +145 -0
- package/types/generated/modules/sky/skyModule.d.ts +10 -0
- package/types/generated/modules/timeline/Timeline.d.ts +51 -0
- package/types/generated/modules/water/Water.d.ts +29 -0
- package/types/generated/modules/weather/Weather.d.ts +62 -0
- package/types/generated/modules/world/World.d.ts +242 -0
- package/types/generated/modules/world/index.d.ts +15 -0
- package/types/generated/render/PostFX.d.ts +125 -0
- package/types/generated/render/materials.d.ts +100 -0
- package/types/generated/render/patches.d.ts +41 -0
- package/types/generated/ui/Focus.d.ts +11 -0
- package/types/generated/ui/LoadingScreen.d.ts +2 -0
- package/types/generated/ui/UI.d.ts +57 -0
- package/types/nav.d.ts +52 -0
- package/types/physics.d.ts +592 -0
- package/types/style.d.ts +638 -0
- package/types/worker.d.ts +11 -0
- package/types/world.d.ts +772 -0
- package/ui/Focus.js +51 -0
- package/ui/LoadingScreen.js +42 -0
- package/ui/Parts.js +199 -0
- package/ui/UI.js +167 -0
- package/ui/defaults.js +85 -0
- package/ui/theme.js +124 -0
- package/ui/touchPart.js +49 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// Real physics (Rapier): objects that topple, roll, get pushed and float on water.
|
|
2
|
+
//
|
|
3
|
+
// Split from collision() (engine.collision, cheap walking collision covering the whole map): this is dynamic-body physics, more costly, simulated only for
|
|
4
|
+
// chunks within +-terrainRing of each physics engine.stream source (the player, a teleport destination; never a camera-only source).
|
|
5
|
+
// The player is a kinematic capsule: it pushes crates and balls but is not pushed by them.
|
|
6
|
+
//
|
|
7
|
+
// const crate = engine.rigid.addBox(mesh, { mass: 5 }); // crate
|
|
8
|
+
// const ball = engine.rigid.addBall(mesh, { radius: 0.4, restitution: 0.6 });
|
|
9
|
+
// engine.rigid.impulse(ball, [0, 3, 5]); // kick (impulse vector: Vector3 / [x, y, z] / { x, y, z })
|
|
10
|
+
// Returns an entry (handle) { body, collider, mesh (= object, position written by physics), remove() }: remove only deletes the body; the model is yours
|
|
11
|
+
// (shadow settings are untouched too)
|
|
12
|
+
// const car = engine.rigid.addVehicle(carMesh, { wheels: [...] }); // vehicle, see Vehicles.js; engine.player.enter(car) to board; car.remove()
|
|
13
|
+
// Ray: once installed, engine.raycast(origin, direction, { targets: 'all' }) also hits rigid bodies (hit carries entry, kind: 'rigid'); no separate entry
|
|
14
|
+
// point.
|
|
15
|
+
//
|
|
16
|
+
// Rapier is downloaded on demand at start() (about 1.5MB); games without physics never load it.
|
|
17
|
+
import * as THREE from 'three';
|
|
18
|
+
import { vec3, xzOf } from '../../core/math.js';
|
|
19
|
+
import { checkOptions } from '../../core/validate.js';
|
|
20
|
+
import { readParams, keysSchema } from '../../core/params.js';
|
|
21
|
+
import { cellsChanged } from '../../core/streamCells.js';
|
|
22
|
+
import { createVehicle, VEHICLE_KEYS, STRINGS } from './Vehicles.js';
|
|
23
|
+
import { syncTerrain, syncTrees, treesChanged, syncGroundTrees, syncStatics, syncLevel } from './sync.js';
|
|
24
|
+
// Params of addBox / addBall / addHull
|
|
25
|
+
const PHYSICS = { physics: true }; // stream source filter (shared, not rebuilt every step)
|
|
26
|
+
const BODY_KEYS = ['mass', 'friction', 'restitution', 'damping', 'angularDamping', 'fixed'];
|
|
27
|
+
|
|
28
|
+
export class RigidBodies {
|
|
29
|
+
/** gravity gravity = gravity; terrainRing = chunk rings around each physics stream source that get terrain collision; buoyancy = buoyancy toggle; buoyancyForce = buoyancy strength (1 = exactly floats); waterDrag = drag in water;
|
|
30
|
+
* floatLine / floatDepth: floatLine / floatDepth: buoyancy starts when the body center is below floatLine meters above the surface and peaks floatDepth
|
|
31
|
+
* meters deeper; treeColliderHeight = trunk collision cylinder height; treeRadius = trunks get cylinders only in chunks within this many meters of the player or a non-fixed body;
|
|
32
|
+
* playerCapsule { halfHeight, radius } playerCapsule { halfHeight, radius } = player capsule size (default derived from engine.movement radius / height) */
|
|
33
|
+
constructor(engine, { gravity = -9.81, terrainRing = 1, buoyancy = true, buoyancyForce = 1.6, waterDrag = 2.5, floatLine = 0.3, floatDepth = 0.8,
|
|
34
|
+
treeColliderHeight = 6, treeRadius = 16, playerCapsule } = {}) {
|
|
35
|
+
const mv = engine.movement; // default capsule uses the shared character radius / height: total height = 2 * halfHeight + 2 * radius
|
|
36
|
+
playerCapsule ??= { halfHeight: Math.max(0.05, (mv.height - 2 * mv.radius) / 2), radius: mv.radius };
|
|
37
|
+
this.engine = engine;
|
|
38
|
+
this.gravity = gravity;
|
|
39
|
+
this.terrainRing = terrainRing;
|
|
40
|
+
this.buoyancy = buoyancy; this.buoyancyForce = buoyancyForce; this.waterDrag = waterDrag; this.playerCapsule = playerCapsule;
|
|
41
|
+
this.floatLine = floatLine; this.floatDepth = floatDepth; this.treeColliderHeight = treeColliderHeight; this.treeRadius = treeRadius;
|
|
42
|
+
this._staticVersion = -1;
|
|
43
|
+
this.R = null; this.world = null;
|
|
44
|
+
this.bodies = []; // { body, mesh, offset, enabled }
|
|
45
|
+
this.terrain = new Map(); // chunk key -> { colliders: [] }
|
|
46
|
+
this.trees = new Map(); // chunk key -> { token, colliders }: trunk cylinders of the chunks near the player / bodies (see sync.js)
|
|
47
|
+
this._anchors = []; this._treeMemo = [];
|
|
48
|
+
this.groundTrees = new Map(); // 'x,z,r' -> cylinder collider: circles from ground.forEachCollider when the ground has no chunked collision
|
|
49
|
+
this.statics = new Map(); // houses/pillars from Physics.js -> collider
|
|
50
|
+
this.vehicles = [];
|
|
51
|
+
/** @internal numbers vehicles added without an id */ this._vehicleSeq = 0;
|
|
52
|
+
this.ready = false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @internal module hook */
|
|
56
|
+
async init() {
|
|
57
|
+
if (this.ready) return;
|
|
58
|
+
const mod = await import('@dimforge/rapier3d-compat');
|
|
59
|
+
const R = this.R = mod.default ?? mod;
|
|
60
|
+
await R.init();
|
|
61
|
+
this.world = new R.World({ x: 0, y: this.gravity, z: 0 });
|
|
62
|
+
this.world.timestep = 1 / 60;
|
|
63
|
+
// Player capsule (kinematic: the character controller decides position; physics only pushes other things away)
|
|
64
|
+
this.playerBody = this.world.createRigidBody(R.RigidBodyDesc.kinematicPositionBased());
|
|
65
|
+
const pc = this.playerCapsule;
|
|
66
|
+
this.playerCollider = this.world.createCollider(R.ColliderDesc.capsule(pc.halfHeight, pc.radius).setTranslation(0, pc.halfHeight + pc.radius, 0), this.playerBody);
|
|
67
|
+
// When the ground provides no chunked collision (no terrain module): use an infinite flat plane (y = 0)
|
|
68
|
+
const G = this.engine.ground;
|
|
69
|
+
if (!G.chunkCollision && !G.staticTrimeshes) this.world.createCollider(R.ColliderDesc.cuboid(5000, 0.5, 5000).setTranslation(0, -0.5, 0));
|
|
70
|
+
this.ready = true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---------- Add bodies ----------
|
|
74
|
+
_add(mesh, desc, { mass = 1, friction = 0.7, restitution = 0.1, damping = 0.05, angularDamping = 0.1, fixed = false } = {}) {
|
|
75
|
+
if (!this.ready) { console.warn('Physics is not ready: add bodies after engine.start() or in the engine:ready event'); return null; }
|
|
76
|
+
const R = this.R, p = mesh.position, q = mesh.quaternion;
|
|
77
|
+
const bd = (fixed ? R.RigidBodyDesc.fixed() : R.RigidBodyDesc.dynamic())
|
|
78
|
+
.setTranslation(p.x, p.y, p.z).setRotation({ x: q.x, y: q.y, z: q.z, w: q.w })
|
|
79
|
+
.setLinearDamping(damping).setAngularDamping(angularDamping).setCanSleep(true);
|
|
80
|
+
const body = this.world.createRigidBody(bd);
|
|
81
|
+
desc.setFriction(friction).setRestitution(restitution).setMass(mass);
|
|
82
|
+
const collider = this.world.createCollider(desc, body);
|
|
83
|
+
if (!mesh.parent) this.engine.add(mesh, { owner: 'engine:rigid' }); // shadow casting/receiving follows the model's own settings (not forced)
|
|
84
|
+
const entry = { body, collider, mesh, object: mesh, enabled: true, volume: 0, damping, angularDamping, fixed, _x: p.x, _z: p.z }; // _x / _z: where sync.js looks for trunks to give it
|
|
85
|
+
entry.remove = () => this._remove(entry);
|
|
86
|
+
this.bodies.push(entry);
|
|
87
|
+
return entry;
|
|
88
|
+
}
|
|
89
|
+
/** Box. When size is omitted it is computed from the model bounding box */
|
|
90
|
+
addBox(mesh, o = {}) {
|
|
91
|
+
checkOptions('rigid.addBox()', o, [...BODY_KEYS, 'size']);
|
|
92
|
+
const s = o.size || new THREE.Box3().setFromObject(mesh).getSize(new THREE.Vector3()).toArray();
|
|
93
|
+
const e = this._add(mesh, this.R.ColliderDesc.cuboid(s[0] / 2, s[1] / 2, s[2] / 2), o);
|
|
94
|
+
if (e) e.volume = s[0] * s[1] * s[2];
|
|
95
|
+
return e;
|
|
96
|
+
}
|
|
97
|
+
/** Ball */
|
|
98
|
+
addBall(mesh, o = {}) {
|
|
99
|
+
checkOptions('rigid.addBall()', o, [...BODY_KEYS, 'radius']);
|
|
100
|
+
const r = o.radius ?? new THREE.Box3().setFromObject(mesh).getSize(new THREE.Vector3()).x / 2;
|
|
101
|
+
const e = this._add(mesh, this.R.ColliderDesc.ball(r), o);
|
|
102
|
+
if (e) e.volume = 4.19 * r * r * r;
|
|
103
|
+
return e;
|
|
104
|
+
}
|
|
105
|
+
/** Convex hull: auto-wrapped around the model shape (irregular objects such as rocks, barrels) */
|
|
106
|
+
addHull(mesh, o = {}) {
|
|
107
|
+
checkOptions('rigid.addHull()', o, BODY_KEYS);
|
|
108
|
+
const g = mesh.geometry, pts = new Float32Array(g.attributes.position.array.length);
|
|
109
|
+
const v = new THREE.Vector3();
|
|
110
|
+
for (let i = 0; i < g.attributes.position.count; i++) {
|
|
111
|
+
v.fromBufferAttribute(g.attributes.position, i).multiply(mesh.scale); pts.set([v.x, v.y, v.z], i * 3);
|
|
112
|
+
}
|
|
113
|
+
const e = this._add(mesh, this.R.ColliderDesc.convexHull(pts), o);
|
|
114
|
+
if (e) { g.computeBoundingBox(); const s = g.boundingBox.getSize(v).multiply(mesh.scale); e.volume = s.x * s.y * s.z * 0.5; }
|
|
115
|
+
return e;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Vehicle (Rapier vehicle controller): id (stable, for saves; default vehicle1, vehicle2... in creation order), wheels, mass, engineForce, brakeForce,
|
|
119
|
+
* maxSteer, suspension, seat, camera; see Vehicles.js
|
|
120
|
+
*/
|
|
121
|
+
addVehicle(mesh, o) {
|
|
122
|
+
checkOptions('rigid.addVehicle()', o, VEHICLE_KEYS);
|
|
123
|
+
if (o.id != null && this.vehicles.some(v => v.id === o.id)) throw new Error(`rigid.addVehicle(): id '${o.id}' is already used by another vehicle; ids name the vehicle in saves, pick a unique one`);
|
|
124
|
+
if (!this.ready) return this._add(mesh); // warns "physics is not ready"
|
|
125
|
+
const { id = 'vehicle' + ++this._vehicleSeq, ...rest } = o;
|
|
126
|
+
const v = createVehicle(this, mesh, rest);
|
|
127
|
+
/** Stable id: the player's save names the vehicle it sits in by this (give one when vehicles are created in a varying order) */ v.id = id;
|
|
128
|
+
v.remove = () => this._removeVehicle(v);
|
|
129
|
+
this.vehicles.push(v);
|
|
130
|
+
return v;
|
|
131
|
+
}
|
|
132
|
+
/** Remove a vehicle (the occupant exits first): called by vehicle.remove() */
|
|
133
|
+
_removeVehicle(v) {
|
|
134
|
+
if (!this.vehicles.includes(v)) return;
|
|
135
|
+
v.driver?.exit(); v._use?.remove();
|
|
136
|
+
this.vehicles = this.vehicles.filter(x => x !== v);
|
|
137
|
+
this.world.removeVehicleController(v.controller);
|
|
138
|
+
this._remove(v);
|
|
139
|
+
}
|
|
140
|
+
/** Whether the physics ground under a position is built (used by mover: 'rapier'; always true without chunked terrain) */
|
|
141
|
+
groundReady(position) {
|
|
142
|
+
if (!this.engine.ground.chunkCollision) return true;
|
|
143
|
+
const [x, z] = xzOf(position), CH = this.engine.ground.chunkSize ?? 64;
|
|
144
|
+
return !!this.terrain.get(Math.floor(x / CH) + ',' + Math.floor(z / CH))?.ready;
|
|
145
|
+
}
|
|
146
|
+
/** Delete the body (called by entry.remove()); the model stays */
|
|
147
|
+
_remove(entry) {
|
|
148
|
+
const i = this.bodies.indexOf(entry); if (i < 0) return;
|
|
149
|
+
this.world.removeRigidBody(entry.body); this.bodies.splice(i, 1);
|
|
150
|
+
}
|
|
151
|
+
/** Push: impulse vector v (Vector3 / [x, y, z] / { x, y, z }) */
|
|
152
|
+
impulse(entry, v) { const { x, y, z } = vec3(v); entry.body.wakeUp(); entry.body.applyImpulse({ x, y, z }, true); }
|
|
153
|
+
/** Teleport to position (velocity zeroed) */
|
|
154
|
+
setPosition(entry, position) {
|
|
155
|
+
const { x, y, z } = vec3(position);
|
|
156
|
+
entry._x = x; entry._z = z;
|
|
157
|
+
entry.body.setTranslation({ x, y, z }, true); entry.body.setLinvel({ x: 0, y: 0, z: 0 }, true);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Ray (provider for engine.rays, called by engine.raycast when targets: 'all'): hits dynamic bodies only, returns RayHit (with entry, kind: 'rigid') */
|
|
161
|
+
_raycast(origin, dir, far) {
|
|
162
|
+
if (!this.ready || !this.bodies.length) return null;
|
|
163
|
+
const byHandle = new Map(this.bodies.map(b => [b.body.handle, b]));
|
|
164
|
+
const max = Number.isFinite(far) ? far : 1e6;
|
|
165
|
+
const only = c => byHandle.has(c.parent()?.handle);
|
|
166
|
+
const hit = this.world.castRayAndGetNormal(new this.R.Ray(origin, dir), max, true, undefined, undefined, undefined, this.playerBody, only);
|
|
167
|
+
if (!hit) return null;
|
|
168
|
+
const entry = byHandle.get(hit.collider.parent()?.handle);
|
|
169
|
+
if (!entry) return null;
|
|
170
|
+
const t = hit.timeOfImpact;
|
|
171
|
+
const point = new THREE.Vector3(origin.x, origin.y, origin.z).addScaledVector(dir, t);
|
|
172
|
+
return { point, distance: t, normal: new THREE.Vector3(hit.normal.x, hit.normal.y, hit.normal.z), object: entry.object, face: null, entry, kind: 'rigid' };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ---------- Every fixed step ----------
|
|
176
|
+
/** @internal module hook */
|
|
177
|
+
step(dt) {
|
|
178
|
+
if (!this.ready) return;
|
|
179
|
+
const e = this.engine, G = e.ground, CH = G.chunkSize ?? 64, chunked = !!G.chunkCollision;
|
|
180
|
+
const focus = e.player ? e.player.position : e.focus; // player-body: the player's own kinematic body follows the character, not engine.actor
|
|
181
|
+
// terrain collision around physics stream sources: sync at once when their chunks change, otherwise check every 0.5 s (tree felling etc.)
|
|
182
|
+
// compared in place against last step's cells (no array, no string key while nothing crosses a chunk border); the list is built only when syncing
|
|
183
|
+
const memo = this._srcMemo || (this._srcMemo = []);
|
|
184
|
+
const moved = cellsChanged(e.stream, memo, CH, this.terrainRing, PHYSICS), n = memo.length;
|
|
185
|
+
const treesMoved = chunked && treesChanged(this, focus, CH);
|
|
186
|
+
this._syncT = (this._syncT || 0) + dt;
|
|
187
|
+
if (moved || treesMoved || this._srcPending || this._syncT > 0.5) {
|
|
188
|
+
const src = [];
|
|
189
|
+
for (let i = 0; i < n; i += 3) src.push([memo[i], memo[i + 1], memo[i + 2]]);
|
|
190
|
+
let pending = false;
|
|
191
|
+
if (chunked) { pending = syncTerrain(this, src); pending = syncTrees(this, focus, CH) || pending; } else syncGroundTrees(this, src, CH);
|
|
192
|
+
this._srcPending = pending; this._syncT = 0; // pending: chunks still waiting for their turn, sync again next step
|
|
193
|
+
}
|
|
194
|
+
syncStatics(this);
|
|
195
|
+
syncLevel(this);
|
|
196
|
+
// player capsule follows the player (the rapier mover computes collision and writes the position itself); turned off while driving to avoid colliding with
|
|
197
|
+
// the car
|
|
198
|
+
if (e.player) this.playerBody.setNextKinematicTranslation({ x: focus.x, y: focus.y, z: focus.z });
|
|
199
|
+
const onFoot = !e.player?.vehicle;
|
|
200
|
+
if (this.playerCollider.isEnabled() !== onFoot) this.playerCollider.setEnabled(onFoot);
|
|
201
|
+
for (const v of this.vehicles) if (v.enabled) v._update(dt);
|
|
202
|
+
|
|
203
|
+
// freeze bodies without terrain collision under them (unfrozen they would fall through)
|
|
204
|
+
for (const b of this.bodies) {
|
|
205
|
+
const t = b.body.translation();
|
|
206
|
+
b._x = t.x; b._z = t.z;
|
|
207
|
+
const ok = !chunked || !!this.terrain.get(Math.floor(t.x / CH) + ',' + Math.floor(t.z / CH))?.ready;
|
|
208
|
+
if (ok !== b.enabled) { b.body.setEnabled(ok); b.enabled = ok; }
|
|
209
|
+
// buoyancy: the submerged part gets an upward force, and water adds drag
|
|
210
|
+
const water = this.buoyancy ? e.waterAt(t.x, t.z) : -Infinity;
|
|
211
|
+
if (ok && this.buoyancy && t.y < water + this.floatLine && b.body.isDynamic()) {
|
|
212
|
+
const depth = Math.min(1, (water + this.floatLine - t.y) / this.floatDepth);
|
|
213
|
+
const m = b.body.mass();
|
|
214
|
+
b.body.resetForces(true);
|
|
215
|
+
b.body.addForce({ x: 0, y: -this.gravity * m * (this.buoyancyForce * depth), z: 0 }, true);
|
|
216
|
+
b.body.setLinearDamping(this.waterDrag); b.body.setAngularDamping(this.waterDrag);
|
|
217
|
+
b.wet = true;
|
|
218
|
+
} else if (b.wet) {
|
|
219
|
+
b.body.resetForces(true); b.body.setLinearDamping(b.damping); b.body.setAngularDamping(b.angularDamping); b.wet = false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
this.world.step();
|
|
224
|
+
|
|
225
|
+
for (const b of this.bodies) {
|
|
226
|
+
if (!b.enabled || b.body.isSleeping()) continue;
|
|
227
|
+
const t = b.body.translation(), r = b.body.rotation();
|
|
228
|
+
b.mesh.position.set(t.x, t.y, t.z);
|
|
229
|
+
b.mesh.quaternion.set(r.x, r.y, r.z, r.w);
|
|
230
|
+
if (t.y < -200 && e.ground.has(t.x, t.z)) this.setPosition(b, [t.x, e.surface(t.x, t.z) + 2, t.z]); // fell through the ground
|
|
231
|
+
// fell off terrain({ shape }): one terrain:void per fall, the game decides what next
|
|
232
|
+
const under = t.y < e.ground.voidY();
|
|
233
|
+
if (under && !b._void) e.events.emit('terrain:void', { who: b, position: { x: t.x, y: t.y, z: t.z } });
|
|
234
|
+
b._void = under;
|
|
235
|
+
}
|
|
236
|
+
for (const v of this.vehicles) v._sync();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
get stats() {
|
|
240
|
+
const active = this.bodies.filter(b => b.enabled && !b.body.isSleeping()).length;
|
|
241
|
+
return { bodies: this.bodies.length, active, colliders: this.world ? this.world.colliders.len() : 0 };
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const RIGID_PARAMS = keysSchema(['gravity', 'terrainRing', 'buoyancy', 'buoyancyForce', 'waterDrag', 'floatLine', 'floatDepth', 'treeColliderHeight', 'treeRadius', 'playerCapsule']);
|
|
246
|
+
|
|
247
|
+
/** Real physics module (Rapier). Installs as engine.rigid. Requires collision() to be installed first */
|
|
248
|
+
export function rigid(opts = {}) {
|
|
249
|
+
readParams('rigid', RIGID_PARAMS, opts);
|
|
250
|
+
let r = null;
|
|
251
|
+
return {
|
|
252
|
+
name: 'rigid', params: RIGID_PARAMS, options: opts, label: 'Physics', noSkip: 'bodies rest while time jumps (stepping seconds of physics would only scatter them)', optional: ['interactions'], requires: ['collision'],
|
|
253
|
+
install(e) {
|
|
254
|
+
e.i18n.add(STRINGS); r = new RigidBodies(e, opts);
|
|
255
|
+
e.budget.metric('rigidBodies', () => r.bodies.length);
|
|
256
|
+
e.stream.readyCheck('rigid', s => !s.physics || r.ready && r.groundReady(s)); // a physics stream source waits for the collider under it
|
|
257
|
+
e.rays.addProvider((o, d, far) => r._raycast(o, d, far)); // engine.raycast(..., { targets: 'all' }) includes rigid bodies
|
|
258
|
+
return r;
|
|
259
|
+
},
|
|
260
|
+
async init() { await r.init(); },
|
|
261
|
+
counts() { return r.bodies.length; }, // engine.counts().rigid
|
|
262
|
+
fixed(dt) { if (r.ready) r.step(dt); },
|
|
263
|
+
dispose() { r.world?.free(); r.world = null; },
|
|
264
|
+
};
|
|
265
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Vehicle: Rapier ray-cast vehicle controller (DynamicRayCastVehicleController). The chassis is a dynamic body; wheels are downward rays + suspension.
|
|
2
|
+
// Convention: the front faces the model's +Z (like characters); wheel position is relative to the chassis origin.
|
|
3
|
+
//
|
|
4
|
+
// const car = engine.rigid.addVehicle(carMesh, { wheels: [
|
|
5
|
+
// { position: [-0.8, 0, 1.2], radius: 0.35, steer: true, object: wheelFL }, { position: [0.8, 0, 1.2], radius: 0.35, steer: true },
|
|
6
|
+
// { position: [-0.8, 0, -1.2], radius: 0.35, drive: true }, { position: [0.8, 0, -1.2], radius: 0.35, drive: true } ], mass: 800 });
|
|
7
|
+
// engine.player.enter(car); car.drive(throttle, steer, brake)
|
|
8
|
+
// Native objects: car.body (rigid body), car.controller (vehicle controller).
|
|
9
|
+
import * as THREE from 'three';
|
|
10
|
+
import { vec3 } from '../../core/math.js';
|
|
11
|
+
|
|
12
|
+
export const VEHICLE_KEYS = ['id', 'wheels', 'mass', 'engineForce', 'brakeForce', 'maxSteer', 'suspension', 'seat', 'camera', 'size', 'friction', 'exitOffset'];
|
|
13
|
+
export const STRINGS = { 'zh-CN': { 'vehicle.enter': '上车', 'vehicle.exit': '下车' }, en: { 'vehicle.enter': 'Get in', 'vehicle.exit': 'Get out' } };
|
|
14
|
+
|
|
15
|
+
/** Chassis size: model bounding box (wheels excluded), in local coordinates */
|
|
16
|
+
function bodyBox(mesh, wheelObjs) {
|
|
17
|
+
const box = new THREE.Box3(), q = mesh.quaternion.clone(), p = mesh.position.clone();
|
|
18
|
+
mesh.quaternion.identity(); mesh.position.set(0, 0, 0); mesh.updateMatrixWorld(true);
|
|
19
|
+
const skip = o => { for (let x = o; x; x = x.parent) if (wheelObjs.has(x)) return true; return false; };
|
|
20
|
+
mesh.traverse(o => {
|
|
21
|
+
if (!o.isMesh || skip(o)) return;
|
|
22
|
+
if (!o.geometry.boundingBox) o.geometry.computeBoundingBox();
|
|
23
|
+
box.union(o.geometry.boundingBox.clone().applyMatrix4(o.matrixWorld));
|
|
24
|
+
});
|
|
25
|
+
mesh.quaternion.copy(q); mesh.position.copy(p); mesh.updateMatrixWorld(true);
|
|
26
|
+
return box;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Create a vehicle (called by RigidBodies.addVehicle) */
|
|
30
|
+
export function createVehicle(rigid, mesh, { wheels, mass = 800, engineForce = 800, brakeForce = 20, maxSteer = 0.5, suspension = {}, seat, camera, size, friction = 1.5, exitOffset = 0.8 }) {
|
|
31
|
+
const e = rigid.engine, R = rigid.R, w = rigid.world;
|
|
32
|
+
const box = bodyBox(mesh, new Set(wheels.map(x => x.object).filter(Boolean)));
|
|
33
|
+
const s = size ? new THREE.Vector3().fromArray(size) : box.getSize(new THREE.Vector3()), c = size ? new THREE.Vector3() : box.getCenter(new THREE.Vector3());
|
|
34
|
+
const entry = rigid._add(mesh, R.ColliderDesc.cuboid(s.x / 2, s.y / 2, s.z / 2).setTranslation(c.x, c.y, c.z), { mass, friction: 0.5, damping: 0.1, angularDamping: 0.5 });
|
|
35
|
+
entry.volume = s.x * s.y * s.z;
|
|
36
|
+
const ctl = w.createVehicleController(entry.body);
|
|
37
|
+
ctl.indexUpAxis = 1; ctl.setIndexForwardAxis = 2; // up = Y, forward = Z
|
|
38
|
+
const { rest = 0.3, stiffness = 30, damping = 4 } = suspension;
|
|
39
|
+
wheels.forEach((wh, i) => {
|
|
40
|
+
const at = vec3(wh.position);
|
|
41
|
+
ctl.addWheel({ x: at.x, y: at.y, z: at.z }, { x: 0, y: -1, z: 0 }, { x: -1, y: 0, z: 0 }, rest, wh.radius);
|
|
42
|
+
ctl.setWheelSuspensionStiffness(i, stiffness);
|
|
43
|
+
ctl.setWheelSuspensionCompression(i, damping);
|
|
44
|
+
ctl.setWheelSuspensionRelaxation(i, damping);
|
|
45
|
+
ctl.setWheelFrictionSlip(i, friction * 10);
|
|
46
|
+
ctl.setWheelMaxSuspensionTravel(i, rest);
|
|
47
|
+
if (wh.object) wh._base = wh.object.position.clone();
|
|
48
|
+
});
|
|
49
|
+
const drives = wheels.filter(x => x.drive).length || wheels.length;
|
|
50
|
+
const seatAt = seat ? vec3(seat) : new THREE.Vector3(c.x, c.y, c.z);
|
|
51
|
+
let throttle = 0, steerIn = 0, brake = 0;
|
|
52
|
+
|
|
53
|
+
const v = Object.assign(entry, {
|
|
54
|
+
controller: ctl, wheels, driver: null, camera,
|
|
55
|
+
/** Control: throttle -1..1, steer -1..1 (right positive), brake 0-1 */
|
|
56
|
+
drive(t, st, b = 0) { throttle = t; steerIn = st; brake = b; if (t || b) entry.body.wakeUp(); },
|
|
57
|
+
/** Seat world position */
|
|
58
|
+
seatPosition() { return mesh.localToWorld(seatAt.clone()); },
|
|
59
|
+
/** Exit position: exitOffset meters outside the driver-side door (default 0.8) */
|
|
60
|
+
exitPosition() { return mesh.localToWorld(new THREE.Vector3(c.x + (seatAt.x >= c.x ? 1 : -1) * (s.x / 2 + exitOffset), c.y, seatAt.z)); },
|
|
61
|
+
/** Every fixed step: write controls to the wheels and advance the vehicle controller (called before the physics step) */
|
|
62
|
+
_update(dt) {
|
|
63
|
+
wheels.forEach((wh, i) => {
|
|
64
|
+
ctl.setWheelEngineForce(i, wh.drive || drives === wheels.length ? throttle * engineForce / drives : 0);
|
|
65
|
+
ctl.setWheelSteering(i, wh.steer ? -steerIn * maxSteer : 0);
|
|
66
|
+
ctl.setWheelBrake(i, brake * brakeForce);
|
|
67
|
+
});
|
|
68
|
+
ctl.updateVehicle(dt);
|
|
69
|
+
},
|
|
70
|
+
/** After the physics step: wheel models follow suspension travel, steering and rolling */
|
|
71
|
+
_sync() {
|
|
72
|
+
wheels.forEach((wh, i) => {
|
|
73
|
+
if (!wh.object) return;
|
|
74
|
+
const o = wh.object;
|
|
75
|
+
o.position.copy(wh._base); o.position.y -= (ctl.wheelSuspensionLength(i) ?? rest) - rest;
|
|
76
|
+
o.rotation.set(ctl.wheelRotation(i) ?? 0, ctl.wheelSteering(i) ?? 0, 0, 'YXZ');
|
|
77
|
+
});
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
/** Current speed (m/s, negative when reversing): chassis velocity component along the front direction (0 when asleep) */
|
|
81
|
+
const fwd = new THREE.Vector3(), q = new THREE.Quaternion();
|
|
82
|
+
Object.defineProperty(v, 'speed', { get: () => {
|
|
83
|
+
const b = entry.body; if (b.isSleeping()) return 0;
|
|
84
|
+
const l = b.linvel(); fwd.set(0, 0, 1).applyQuaternion(q.copy(b.rotation()));
|
|
85
|
+
return l.x * fwd.x + l.y * fwd.y + l.z * fwd.z;
|
|
86
|
+
} });
|
|
87
|
+
// With the interactions module installed: press the interact key near the car to get in, and again while driving to get out
|
|
88
|
+
if (e.interactions) {
|
|
89
|
+
v._use = e.interactions.add(mesh, {
|
|
90
|
+
id: v.id,
|
|
91
|
+
label: () => e.t(v.driver ? 'vehicle.exit' : 'vehicle.enter'), radius: Math.max(3, s.z / 2 + 1.5),
|
|
92
|
+
enabled: () => !!e.player && (!v.driver || v.driver === e.player),
|
|
93
|
+
onUse: () => v.driver ? v.driver.exit() : e.player.enter(v),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
return v;
|
|
97
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Colliders the rigid module keeps in sync with what the game draws: terrain chunks (trimesh + trunk cylinders), trunks on a level or code-built ground, the
|
|
2
|
+
// collision module's houses / pillars, and a level's static ground. Each function takes the RigidBodies instance as `r` and is called from its step().
|
|
3
|
+
import * as THREE from 'three';
|
|
4
|
+
|
|
5
|
+
// Chunks around physics stream sources ([cx, cz, rings] each): terrain uses triangle-mesh colliders (identical to what is drawn), vegetation uses cylinders.
|
|
6
|
+
// Data comes from the ground API ground.chunkCollision
|
|
7
|
+
// Building a trimesh costs a few ms and the broadphase pays again on the next world step; a whole new ring (9 chunks, or after a teleport) in one fixed step
|
|
8
|
+
// was the worst frame of streaming. So once playing, the chunk under the source is built at once and the others share COLLIDER_STEP_MS per step, nearest first.
|
|
9
|
+
// Returns true while chunks are still waiting, so the caller syncs again on the next step instead of after 0.5 s.
|
|
10
|
+
const CHUNKS_PER_STEP = 1;
|
|
11
|
+
export function syncTerrain(r, src) {
|
|
12
|
+
const e = r.engine, R = r.R, want = new Set(), todo = [];
|
|
13
|
+
for (const [pcx, pcz, n] of src) for (let dz = -n; dz <= n; dz++) for (let dx = -n; dx <= n; dx++) {
|
|
14
|
+
const cx = pcx + dx, cz = pcz + dz, key = cx + ',' + cz; if (want.has(key)) continue; want.add(key);
|
|
15
|
+
todo.push({ cx, cz, key, d: Math.max(Math.abs(dx), Math.abs(dz)) });
|
|
16
|
+
}
|
|
17
|
+
todo.sort((a, b) => a.d - b.d);
|
|
18
|
+
let built = 0, pending = false;
|
|
19
|
+
for (const { cx, cz, key, d } of todo) {
|
|
20
|
+
const have = r.terrain.get(key);
|
|
21
|
+
const data = e.ground.chunkCollision(cx, cz);
|
|
22
|
+
if (have?.ready && data && have.token === data.token) continue;
|
|
23
|
+
if (data && d > 0 && e.ready && built >= CHUNKS_PER_STEP) { pending = true; continue; } // keeps the old collider until its turn
|
|
24
|
+
if (have) dropTerrain(r, key);
|
|
25
|
+
if (data && d > 0) built++;
|
|
26
|
+
if (!data) { r.terrain.set(key, { ready: false, colliders: [] }); continue; }
|
|
27
|
+
// a chunk wholly in the void (terrain({ shape })) has no triangles: nothing to collide with, and Rapier rejects an empty trimesh
|
|
28
|
+
if (!data.positions.length) { r.terrain.set(key, { ready: true, token: data.token, colliders: [] }); continue; }
|
|
29
|
+
const t0 = performance.now(); // measure: the 'collider' stage of engine.budget.cost
|
|
30
|
+
const idx = new Uint32Array(data.positions.length / 3); for (let i = 0; i < idx.length; i++) idx[i] = i;
|
|
31
|
+
const ground = R.ColliderDesc.trimesh(new Float32Array(data.positions), idx).setTranslation(data.ox, 0, data.oz).setFriction(0.9);
|
|
32
|
+
const colliders = [r.world.createCollider(ground)];
|
|
33
|
+
r.terrain.set(key, { ready: true, token: data.token, colliders });
|
|
34
|
+
e.budget.cost.end('collider', t0);
|
|
35
|
+
}
|
|
36
|
+
for (const key of [...r.terrain.keys()]) if (!want.has(key)) { dropTerrain(r, key); dropTrees(r, key); }
|
|
37
|
+
return pending;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Trunk cylinders are not part of a chunk's terrain collider: only chunks near something that can hit them have them (the player and every non-fixed body,
|
|
41
|
+
// r.treeRadius), because hundreds of resident static cylinders cost world.step time while character collision never reads them (the Collision grid does).
|
|
42
|
+
// Same numeric budget as terrain (TREES_PER_STEP chunks per fixed step, never wall-clock, so replays agree), except chunks right at an anchor (URGENT):
|
|
43
|
+
// a body must never reach a trunk that has no collider yet. Dropped only beyond treeRadius + TREE_KEEP, so a body on the border does not thrash.
|
|
44
|
+
const TREES_PER_STEP = 1, URGENT = 4, TREE_KEEP = 8;
|
|
45
|
+
// r._anchors = [x, z, ...] of the player and each non-fixed body (a body's _x / _z were noted by the step loop or setPosition)
|
|
46
|
+
function anchors(r, focus) {
|
|
47
|
+
const a = r._anchors; a.length = 0;
|
|
48
|
+
a.push(focus.x, focus.z);
|
|
49
|
+
for (const b of r.bodies) if (!b.fixed) a.push(b._x, b._z);
|
|
50
|
+
return a;
|
|
51
|
+
}
|
|
52
|
+
/** Did the chunks any anchor needs change since the last call (compared in place, runs every fixed step) */
|
|
53
|
+
export function treesChanged(r, focus, CH) {
|
|
54
|
+
const a = anchors(r, focus), m = r._treeMemo, R = r.treeRadius;
|
|
55
|
+
let same = m.length === a.length * 4, k = 0;
|
|
56
|
+
for (let i = 0; i < a.length; i++, k += 4) {
|
|
57
|
+
if (span(m, k, a[i], R, CH)) same = false;
|
|
58
|
+
if (span(m, k + 2, a[i], URGENT, CH)) same = false;
|
|
59
|
+
}
|
|
60
|
+
m.length = k;
|
|
61
|
+
return !same;
|
|
62
|
+
}
|
|
63
|
+
// chunk span of [c - d, c + d] stored at m[k], m[k + 1]; true when it changed
|
|
64
|
+
function span(m, k, c, d, CH) {
|
|
65
|
+
const lo = Math.floor((c - d) / CH), hi = Math.floor((c + d) / CH);
|
|
66
|
+
if (m[k] === lo && m[k + 1] === hi) return false;
|
|
67
|
+
m[k] = lo; m[k + 1] = hi; return true;
|
|
68
|
+
}
|
|
69
|
+
/** Chunks whose Chebyshev distance to (x, z) is <= d: callback fn(cx, cz, dist) for each */
|
|
70
|
+
function nearChunks(x, z, d, CH, fn) {
|
|
71
|
+
for (let cz = Math.floor((z - d) / CH); cz <= Math.floor((z + d) / CH); cz++) for (let cx = Math.floor((x - d) / CH); cx <= Math.floor((x + d) / CH); cx++) {
|
|
72
|
+
fn(cx, cz, Math.max(Math.max(cx * CH - x, x - (cx + 1) * CH, 0), Math.max(cz * CH - z, z - (cz + 1) * CH, 0)));
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Build / drop trunk cylinders for the chunks near the anchors (only where the terrain collider is already built). Returns true while chunks are still queued */
|
|
76
|
+
export function syncTrees(r, focus, CH) {
|
|
77
|
+
const e = r.engine, R = r.R, a = anchors(r, focus), want = new Map(), keep = new Set();
|
|
78
|
+
for (let i = 0; i < a.length; i += 2) {
|
|
79
|
+
nearChunks(a[i], a[i + 1], r.treeRadius + TREE_KEEP, CH, (cx, cz, d) => {
|
|
80
|
+
const key = cx + ',' + cz; keep.add(key);
|
|
81
|
+
if (d > r.treeRadius || !r.terrain.get(key)?.ready) return;
|
|
82
|
+
const w = want.get(key);
|
|
83
|
+
if (!w) want.set(key, { cx, cz, key, d });
|
|
84
|
+
else if (d < w.d) w.d = d;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
const todo = [...want.values()].sort((p, q) => p.d - q.d);
|
|
88
|
+
let built = 0, pending = false;
|
|
89
|
+
for (const { cx, cz, key, d } of todo) {
|
|
90
|
+
const have = r.trees.get(key), data = e.ground.chunkCollision(cx, cz);
|
|
91
|
+
if (!data || have?.token === data.token) continue;
|
|
92
|
+
const urgent = d <= URGENT || !e.ready;
|
|
93
|
+
if (!urgent && built >= TREES_PER_STEP) { pending = true; continue; } // keeps the old cylinders until its turn
|
|
94
|
+
if (have) dropTrees(r, key);
|
|
95
|
+
if (!urgent) built++;
|
|
96
|
+
const colliders = [], half = r.treeColliderHeight / 2;
|
|
97
|
+
for (const k of data.circles) for (let i = 0; i < k.length; i += 3) {
|
|
98
|
+
const h = e.surface(k[i], k[i + 1]);
|
|
99
|
+
colliders.push(r.world.createCollider(R.ColliderDesc.cylinder(half, k[i + 2]).setTranslation(k[i], h + half, k[i + 1])));
|
|
100
|
+
}
|
|
101
|
+
r.trees.set(key, { token: data.token, colliders });
|
|
102
|
+
}
|
|
103
|
+
for (const key of [...r.trees.keys()]) if (!keep.has(key)) dropTrees(r, key);
|
|
104
|
+
return pending;
|
|
105
|
+
}
|
|
106
|
+
function dropTrees(r, key) {
|
|
107
|
+
const t = r.trees.get(key);
|
|
108
|
+
if (t) for (const c of t.colliders) r.world.removeCollider(c, false);
|
|
109
|
+
r.trees.delete(key);
|
|
110
|
+
}
|
|
111
|
+
// Vegetation on a level model or code-built ground has no chunked collision: its trunks reach us only as circles through
|
|
112
|
+
// ground.forEachCollider (the same source character collision and path planning read). Make them cylinders around the physics sources
|
|
113
|
+
export function syncGroundTrees(r, src, CH) {
|
|
114
|
+
const e = r.engine, G = e.ground, R = r.R, half = r.treeColliderHeight / 2, want = new Set();
|
|
115
|
+
if (G.forEachCollider) for (const [pcx, pcz, n] of src) {
|
|
116
|
+
G.forEachCollider((pcx + 0.5) * CH, (pcz + 0.5) * CH, (n + 0.5) * CH * Math.SQRT2, (x, z, rad) => {
|
|
117
|
+
const key = x + ',' + z + ',' + rad;
|
|
118
|
+
want.add(key);
|
|
119
|
+
if (r.groundTrees.has(key)) return;
|
|
120
|
+
const h = e.surface(x, z);
|
|
121
|
+
r.groundTrees.set(key, r.world.createCollider(R.ColliderDesc.cylinder(half, rad).setTranslation(x, h + half, z)));
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
for (const [key, col] of r.groundTrees) if (!want.has(key)) { r.world.removeCollider(col, false); r.groundTrees.delete(key); }
|
|
125
|
+
}
|
|
126
|
+
function dropTerrain(r, key) {
|
|
127
|
+
const t = r.terrain.get(key);
|
|
128
|
+
if (t) for (const c of t.colliders) r.world.removeCollider(c, false);
|
|
129
|
+
r.terrain.delete(key);
|
|
130
|
+
}
|
|
131
|
+
// Sync houses and pillars from the collision module into the physics world (only when they are added or removed)
|
|
132
|
+
export function syncStatics(r) {
|
|
133
|
+
const ph = r.engine.collision;
|
|
134
|
+
if (ph.version === r._staticVersion) return;
|
|
135
|
+
r._staticVersion = ph.version;
|
|
136
|
+
const R = r.R, seen = new Set();
|
|
137
|
+
ph.forEachStatic((type, o) => {
|
|
138
|
+
seen.add(o);
|
|
139
|
+
if (r.statics.has(o)) return;
|
|
140
|
+
const g = o.y ?? r.engine.surface(o.x, o.z);
|
|
141
|
+
if (type === 'box') {
|
|
142
|
+
const q = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), o.rot);
|
|
143
|
+
const d = R.ColliderDesc.cuboid(o.hw, o.h / 2, o.hd).setTranslation(o.x, g + o.h / 2, o.z).setRotation({ x: q.x, y: q.y, z: q.z, w: q.w });
|
|
144
|
+
r.statics.set(o, r.world.createCollider(d));
|
|
145
|
+
} else r.statics.set(o, r.world.createCollider(R.ColliderDesc.cylinder(o.h / 2, o.r).setTranslation(o.x, g + o.h / 2, o.z)));
|
|
146
|
+
});
|
|
147
|
+
for (const [o, col] of r.statics) if (!seen.has(o)) { r.world.removeCollider(col, false); r.statics.delete(o); }
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Ground of a level (hand-built scene): one static triangle mesh, rebuilt when the level changes
|
|
151
|
+
export function syncLevel(r) {
|
|
152
|
+
const G = r.engine.ground; if (!G.staticTrimeshes) return;
|
|
153
|
+
const v = G.staticVersion?.() ?? 0; if (v === r._levelVersion) return;
|
|
154
|
+
r._levelVersion = v;
|
|
155
|
+
for (const c of r._levelColliders || []) r.world.removeCollider(c, false);
|
|
156
|
+
const desc = t => r.R.ColliderDesc.trimesh(t.positions, t.indices).setFriction(0.9);
|
|
157
|
+
r._levelColliders = G.staticTrimeshes().map(t => r.world.createCollider(desc(t)));
|
|
158
|
+
}
|