threedog 0.7.0 → 0.8.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 +196 -1
- package/README.md +6 -9
- package/core/{Animator.js → assets/Animator.js} +28 -18
- package/core/{Assets.js → assets/Assets.js} +98 -54
- package/core/{Baked.js → assets/Baked.js} +4 -4
- package/core/assets/assetEntry.js +237 -0
- package/core/{assetHot.js → assets/assetHot.js} +7 -6
- package/core/{assetLoad.js → assets/assetLoad.js} +55 -16
- package/core/assets/assetLod.js +113 -0
- package/core/{assetManifest.js → assets/assetManifest.js} +19 -11
- package/core/{assetReport.js → assets/assetReport.js} +6 -12
- package/core/{namedModel.js → assets/namedModel.js} +17 -13
- package/core/{Audio.js → audio/Audio.js} +26 -23
- package/core/{audioRules.js → audio/audioRules.js} +8 -5
- package/core/{audioVoice.js → audio/audioVoice.js} +17 -30
- package/core/{Events.js → base/Events.js} +3 -3
- package/core/{I18n.js → base/I18n.js} +10 -10
- package/core/{Random.js → base/Random.js} +5 -4
- package/core/{easing.js → base/easing.js} +1 -1
- package/core/{errorCodes.js → base/errorCodes.js} +98 -110
- package/core/{errorHints.js → base/errorHints.js} +5 -9
- package/core/{errors.js → base/errors.js} +6 -6
- package/core/{events.generated.js → base/events.generated.js} +1 -0
- package/core/{math.js → base/math.js} +15 -4
- package/core/{params.js → base/params.js} +3 -4
- package/core/base/util.js +32 -0
- package/core/{validate.js → base/validate.js} +28 -7
- package/core/{Engine.js → engine/Engine.js} +80 -77
- package/core/{Loading.js → engine/Loading.js} +4 -2
- package/core/{copies.js → engine/copies.js} +4 -4
- package/core/{headlessLoad.js → engine/headlessLoad.js} +4 -4
- package/core/{lifecycle.js → engine/lifecycle.js} +14 -12
- package/core/{loop.js → engine/loop.js} +68 -47
- package/core/{moduleSlots.js → engine/moduleSlots.js} +6 -6
- package/core/{moduleTypes.js → engine/moduleTypes.js} +13 -14
- package/core/{modules.js → engine/modules.js} +14 -11
- package/core/{page.js → engine/page.js} +36 -23
- package/core/{plugins.js → engine/plugins.js} +6 -9
- package/core/{services.js → engine/services.js} +47 -40
- package/core/{startup.js → engine/startup.js} +26 -24
- package/core/{Input.js → input/Input.js} +30 -22
- package/core/{inputContext.js → input/inputContext.js} +14 -5
- package/core/{inputPad.js → input/inputPad.js} +1 -1
- package/core/{inputPointer.js → input/inputPointer.js} +8 -2
- package/core/{inputTouch.js → input/inputTouch.js} +1 -1
- package/core/{Budget.js → inspect/Budget.js} +21 -20
- package/core/{DebugOverrides.js → inspect/DebugOverrides.js} +30 -16
- package/core/{Describe.js → inspect/Describe.js} +26 -22
- package/core/{Inspect.js → inspect/Inspect.js} +9 -7
- package/core/{InspectMaterials.js → inspect/InspectMaterials.js} +7 -8
- package/core/{InspectPerf.js → inspect/InspectPerf.js} +19 -13
- package/core/{InspectScene.js → inspect/InspectScene.js} +8 -5
- package/core/{MaterialInfo.js → inspect/MaterialInfo.js} +3 -2
- package/core/{UrlState.js → inspect/UrlState.js} +52 -20
- package/core/{catalog.js → inspect/catalog.js} +3 -3
- package/core/{checks.js → inspect/checks.js} +7 -5
- package/core/{debugView.js → inspect/debugView.js} +4 -4
- package/core/{describeRender.js → inspect/describeRender.js} +21 -20
- package/core/{describeText.js → inspect/describeText.js} +11 -6
- package/core/inspect/geometryWatch.js +38 -0
- package/core/{lightWatch.js → inspect/lightWatch.js} +8 -8
- package/core/{pictureWriters.js → inspect/pictureWriters.js} +15 -16
- package/core/{shaderCost.js → inspect/shaderCost.js} +1 -1
- package/core/{Ops.js → ops/Ops.js} +16 -11
- package/core/{control.js → ops/control.js} +13 -12
- package/core/ops/holds.js +139 -0
- package/core/{opsAtomic.js → ops/opsAtomic.js} +2 -1
- package/core/{opsCheck.js → ops/opsCheck.js} +4 -5
- package/core/{opsGuard.js → ops/opsGuard.js} +3 -3
- package/core/{opsRun.js → ops/opsRun.js} +7 -4
- package/core/{owned.js → ops/owned.js} +14 -17
- package/core/{sharedPoint.js → ops/sharedPoint.js} +8 -8
- package/core/{stackFrames.js → ops/stackFrames.js} +2 -2
- package/core/{stacks.js → ops/stacks.js} +12 -14
- package/core/{wrap.js → ops/wrap.js} +8 -6
- package/core/{SaveStore.js → saves/SaveStore.js} +1 -1
- package/core/{Saves.js → saves/Saves.js} +58 -36
- package/core/{saveApply.js → saves/saveApply.js} +13 -9
- package/core/{saveFiles.js → saves/saveFiles.js} +17 -15
- package/core/{saveValues.js → saves/saveValues.js} +20 -3
- package/core/{saveVersions.js → saves/saveVersions.js} +4 -17
- package/core/{session.js → saves/session.js} +31 -17
- package/core/{Rays.js → scene/Rays.js} +23 -25
- package/core/{Views.js → scene/Views.js} +14 -6
- package/core/{adaptive.js → scene/adaptive.js} +15 -8
- package/core/{canvasBox.js → scene/canvasBox.js} +2 -3
- package/core/{engineCamera.js → scene/engineCamera.js} +1 -1
- package/core/{gameLights.js → scene/gameLights.js} +17 -7
- package/core/{grayMap.js → scene/grayMap.js} +8 -2
- package/core/{grid.js → scene/grid.js} +4 -4
- package/core/{gridLoad.js → scene/gridLoad.js} +1 -1
- package/core/{ground.js → scene/ground.js} +9 -9
- package/core/{movement.js → scene/movement.js} +3 -3
- package/core/{pictureSlots.js → scene/pictureSlots.js} +2 -2
- package/core/{sceneDelta.js → scene/sceneDelta.js} +2 -2
- package/core/{sceneMethods.js → scene/sceneMethods.js} +36 -13
- package/core/scene/stepPoses.js +55 -0
- package/core/{targeting.js → scene/targeting.js} +4 -5
- package/core/{thingExplain.js → scene/thingExplain.js} +6 -5
- package/core/{thingOps.js → scene/thingOps.js} +4 -3
- package/core/{things.js → scene/things.js} +18 -5
- package/core/{threePatches.js → scene/threePatches.js} +1 -1
- package/core/{view.js → scene/view.js} +17 -16
- package/core/{Batching.js → stream/Batching.js} +85 -27
- package/core/{Stream.js → stream/Stream.js} +60 -30
- package/core/{batchGroups.js → stream/batchGroups.js} +10 -9
- package/core/{chunkStream.js → stream/chunkStream.js} +40 -0
- package/core/{scatter.js → stream/scatter.js} +2 -2
- package/core/{streamSources.js → stream/streamSources.js} +12 -7
- package/core/{Commits.js → threads/Commits.js} +6 -5
- package/core/{Jobs.js → threads/Jobs.js} +12 -10
- package/core/{Threads.js → threads/Threads.js} +16 -16
- package/core/{threadPure.js → threads/threadPure.js} +7 -5
- package/core/{threadServe.js → threads/threadServe.js} +3 -3
- package/core/{Clock.js → time/Clock.js} +17 -10
- package/core/{dayTime.js → time/dayTime.js} +5 -1
- package/core/{skip.js → time/skip.js} +18 -17
- package/core/{time.js → time/time.js} +18 -12
- package/core/{uiClock.js → time/uiClock.js} +4 -4
- package/docs/AI.md +22 -19
- package/docs/API.md +24 -23
- package/docs/CODEMAP.md +76 -183
- package/docs/COMMANDS.md +66 -58
- package/docs/CONVENTIONS.md +54 -14
- package/docs/MIGRATING-0.8.md +169 -0
- package/docs/THREE.md +78 -29
- package/docs/api/assets.md +12 -13
- package/docs/api/audio.md +6 -5
- package/docs/api/budget.md +4 -4
- package/docs/api/calendar.md +10 -11
- package/docs/api/cameraRig.md +29 -29
- package/docs/api/characters.md +23 -18
- package/docs/api/clock.md +1 -1
- package/docs/api/collision.md +12 -9
- package/docs/api/content.md +6 -6
- package/docs/api/dialogue.md +3 -3
- package/docs/api/engine.md +45 -43
- package/docs/api/events.md +3 -2
- package/docs/api/flow.md +1 -1
- package/docs/api/grids.md +1 -1
- package/docs/api/ground.md +4 -5
- package/docs/api/hud.md +2 -2
- package/docs/api/i18n.md +6 -6
- package/docs/api/input.md +7 -7
- package/docs/api/interactions.md +4 -4
- package/docs/api/inventory.md +9 -9
- package/docs/api/jobs.md +5 -5
- package/docs/api/level.md +7 -7
- package/docs/api/lightPool.md +1 -6
- package/docs/api/music.md +3 -3
- package/docs/api/nav.md +10 -10
- package/docs/api/openWorld.md +1 -1
- package/docs/api/particles.md +2 -2
- package/docs/api/photo.md +4 -2
- package/docs/api/pickups.md +2 -2
- package/docs/api/post.md +20 -3
- package/docs/api/quests.md +3 -3
- package/docs/api/rays.md +5 -7
- package/docs/api/rigid.md +27 -18
- package/docs/api/saves.md +8 -7
- package/docs/api/settings.md +9 -8
- package/docs/api/sky.md +8 -7
- package/docs/api/stream.md +7 -7
- package/docs/api/swarm.md +34 -0
- package/docs/api/terrain.md +7 -8
- package/docs/api/terrainDraw.md +1 -1
- package/docs/api/timeline.md +2 -2
- package/docs/api/tools.md +25 -22
- package/docs/api/vars.md +1 -1
- package/docs/api/vegetation.md +4 -3
- package/docs/api/vegetationDraw.md +1 -1
- package/docs/api/water.md +1 -1
- package/docs/api/weather.md +9 -9
- package/docs/api/weatherDraw.md +3 -3
- package/docs/api/world.md +5 -5
- package/docs/recipes/01-new-game.md +25 -40
- package/docs/recipes/02-npc.md +19 -20
- package/docs/recipes/03-items-pickups.md +27 -43
- package/docs/recipes/04-quests.md +3 -4
- package/docs/recipes/05-props-bake.md +29 -39
- package/docs/recipes/06-clock-events.md +27 -18
- package/docs/recipes/07-flow-scenes-pause.md +12 -17
- package/docs/recipes/08-weather-sky.md +88 -34
- package/docs/recipes/09-materials-lights-models.md +22 -36
- package/docs/recipes/10-blender-level.md +6 -7
- package/docs/recipes/11-vehicles.md +30 -22
- package/docs/recipes/12-navmesh.md +9 -8
- package/docs/recipes/13-animation.md +39 -17
- package/docs/recipes/14-custom-module.md +14 -13
- package/docs/recipes/15-perf.md +49 -26
- package/docs/recipes/16-dev-bridge.md +32 -52
- package/docs/recipes/18-world.md +36 -23
- package/docs/recipes/19-shooting-picking.md +4 -2
- package/docs/recipes/20-content-actions-save.md +30 -35
- package/docs/recipes/21-camera.md +33 -5
- package/docs/recipes/22-shop-gold.md +0 -4
- package/docs/recipes/23-ops.md +10 -10
- package/docs/recipes/26-ui-parts.md +22 -12
- package/docs/recipes/27-net.md +0 -2
- package/docs/recipes/28-terrain.md +117 -21
- package/docs/recipes/29-grid.md +2 -2
- package/docs/recipes/30-templates.md +55 -19
- package/docs/recipes/31-audio.md +5 -5
- package/docs/recipes/32-action-game.md +15 -6
- package/docs/recipes/33-stream-sources.md +2 -2
- package/docs/recipes/34-environment-lighting.md +67 -19
- package/docs/recipes/35-picture-from-reference.md +10 -17
- package/docs/recipes/36-ground-plants.md +105 -0
- package/docs/recipes/40-scene.md +7 -11
- package/docs/recipes/43-shader-patches-surfaces.md +20 -41
- package/docs/recipes/44-water-weather-hooks.md +48 -43
- package/docs/recipes/45-cascaded-shadows.md +39 -17
- package/docs/recipes/46-blender-art-route.md +7 -8
- package/docs/recipes/49-game-module-contract.md +5 -5
- package/docs/recipes/50-own-renderer.md +2 -2
- package/docs/recipes/52-around-the-picture.md +54 -98
- package/docs/recipes/53-ground-up-close.md +61 -24
- package/docs/recipes/55-mods-time-wrap.md +12 -16
- package/docs/recipes/56-webgpu-tsl.md +16 -14
- package/docs/recipes/57-control.md +9 -2
- package/docs/recipes/58-input.md +2 -2
- package/docs/recipes/59-depth-post-effects.md +1 -1
- package/docs/recipes/60-judge-the-picture.md +29 -24
- package/docs/recipes/62-picture-code.md +49 -17
- package/docs/recipes/63-third-person-start.md +17 -4
- package/docs/recipes/64-particles.md +1 -1
- package/docs/recipes/65-swarm.md +140 -0
- package/examples/action/.threedog/content-index.json +1 -1
- package/examples/action/GAMEMAP.md +2 -2
- package/examples/{sheepisland → perf-island}/.threedog/content-index.json +1 -1
- package/examples/{sheepisland → perf-island}/GAMEMAP.md +12 -11
- package/examples/{sheepisland → perf-island}/assets.js +1 -1
- package/examples/{sheepisland → perf-island}/main.js +8 -8
- package/examples/{sheepisland → perf-island}/models.js +1 -1
- package/examples/{sheepisland → perf-island}/params.js +1 -1
- package/examples/{sheepisland → perf-island}/picture/index.js +1 -1
- package/examples/{sheepisland → perf-island}/picture/palette.js +2 -2
- package/examples/{sheepisland → perf-island}/views.js +1 -1
- package/examples/plain-picture/GAMEMAP.md +1 -0
- package/examples/rc2d/GAMEMAP.md +3 -2
- package/examples/rc2d/main.js +1 -3
- package/examples/styletest/.threedog/content-index.json +1 -1
- package/examples/styletest/GAMEMAP.md +3 -2
- package/examples/techniques/contact-shadows/contactShadows.js +1 -1
- package/examples/techniques/contact-shadows/main.js +1 -1
- package/examples/techniques/interior-light/main.js +1 -1
- package/examples/techniques/modular-building/README.md +7 -6
- package/examples/techniques/outlines-and-rim/picture/post.js +1 -1
- package/examples/techniques/procedural-animation/README.md +1 -1
- package/examples/webgpu-tsl/GAMEMAP.md +1 -0
- package/examples/world/.threedog/content-index.json +1 -1
- package/examples/world/GAMEMAP.md +4 -3
- package/examples/world/picture/sky.js +1 -1
- package/index.d.ts +8 -7
- package/index.js +31 -31
- package/llms.txt +9 -13
- package/modules/ambience/Ambience.js +11 -8
- package/modules/calendar/Calendar.js +19 -19
- package/modules/calendar/index.js +13 -13
- package/modules/calendar/schedule.js +11 -6
- package/modules/calendar/seasons.js +7 -7
- package/modules/camera/FirstPersonCamera.js +2 -2
- package/modules/camera/FixedCamera.js +14 -15
- package/modules/camera/FollowCamera.js +4 -4
- package/modules/camera/RtsCamera.js +11 -11
- package/modules/camera/common.js +48 -24
- package/modules/camera/contract.js +1 -0
- package/modules/camera/projection.js +2 -2
- package/modules/characters/Character.js +33 -30
- package/modules/characters/NPC.js +27 -19
- package/modules/characters/assetModel.js +14 -10
- package/modules/characters/astar.js +7 -5
- package/modules/characters/behaviors.js +4 -5
- package/modules/characters/contract.js +2 -2
- package/modules/characters/index.js +168 -214
- package/modules/characters/moveHelpers.js +14 -3
- package/modules/characters/movers/common.js +16 -14
- package/modules/characters/movers/manual.js +1 -1
- package/modules/characters/movers/rapier.js +7 -6
- package/modules/characters/movers/simple.js +3 -3
- package/modules/characters/movers/support.js +8 -8
- package/modules/characters/npcAvoid.js +4 -4
- package/modules/characters/npcSim.js +23 -17
- package/modules/characters/npcView.js +6 -5
- package/modules/characters/pathAsync.js +12 -10
- package/modules/characters/pathWorker.js +1 -1
- package/modules/characters/player.js +108 -0
- package/modules/characters/playerStrings.js +2 -2
- package/modules/characters/proceduralRig.js +2 -2
- package/modules/characters/useModel.js +3 -3
- package/modules/characters/walkTo.js +11 -17
- package/modules/collision/Collision.js +26 -20
- package/modules/collision/contract.js +15 -0
- package/modules/content/explain.js +1 -2
- package/modules/content/fields.js +24 -3
- package/modules/content/ids.js +2 -2
- package/modules/content/index.js +61 -99
- package/modules/content/loader.js +14 -17
- package/modules/content/markers.js +25 -0
- package/modules/content/models.js +10 -7
- package/modules/content/packs.js +3 -3
- package/modules/content/parts.js +2 -2
- package/modules/content/places.js +35 -0
- package/modules/content/props.js +17 -14
- package/modules/content/rules.js +25 -21
- package/modules/content/stages.js +7 -7
- package/modules/content/templates.js +20 -20
- package/modules/content/words.js +34 -0
- package/modules/debug/DebugPanel.js +10 -43
- package/modules/flow/contract.js +1 -1
- package/modules/flow/index.js +11 -7
- package/modules/gameplay/Container.js +12 -11
- package/modules/gameplay/Containers.js +14 -5
- package/modules/gameplay/Dialogue.js +13 -11
- package/modules/gameplay/Interactions.js +37 -24
- package/modules/gameplay/Inventory.js +8 -9
- package/modules/gameplay/Quests.js +16 -9
- package/modules/gameplay/boxOps.js +1 -1
- package/modules/gameplay/dialogueBubbles.js +3 -4
- package/modules/gameplay/inventoryOps.js +0 -1
- package/modules/gameplay/pickups.js +22 -23
- package/modules/gameplay/questSave.js +3 -3
- package/modules/grid/Grid.js +1 -1
- package/modules/grid/Layer.js +4 -4
- package/modules/grid/index.js +7 -8
- package/modules/grid/preview.js +7 -7
- package/modules/grid/tiles.js +6 -6
- package/modules/hlod/index.js +37 -55
- package/modules/hud/index.js +17 -11
- package/modules/hud/parts.js +4 -4
- package/modules/level/contract.js +16 -0
- package/modules/level/heightfield.js +12 -10
- package/modules/level/index.js +47 -152
- package/modules/level/load.js +79 -0
- package/modules/level/meshes.js +65 -0
- package/modules/level/nodes.js +6 -7
- package/modules/lights/index.js +28 -181
- package/modules/lights/slots.js +93 -0
- package/modules/lights/sources.js +88 -0
- package/modules/music/Music.js +30 -26
- package/modules/music/rules.js +1 -1
- package/modules/nav/contract.js +14 -0
- package/modules/nav/index.js +11 -10
- package/modules/net/Client.js +7 -7
- package/modules/net/Host.js +7 -7
- package/modules/net/Net.js +1 -1
- package/modules/net/index.js +5 -4
- package/modules/net/sync.js +2 -2
- package/modules/net/transports.js +2 -2
- package/modules/particles/Particles.js +11 -8
- package/modules/particles/effect.js +7 -5
- package/modules/particles/shaders.js +2 -1
- package/modules/photo/Photo.js +15 -17
- package/modules/rigid/RigidBodies.js +66 -43
- package/modules/rigid/Vehicles.js +2 -3
- package/modules/rigid/ragdoll.js +287 -0
- package/modules/rigid/ragdollBones.js +159 -0
- package/modules/rigid/sync.js +6 -6
- package/modules/settings/Settings.js +108 -39
- package/modules/settings/apply.js +6 -6
- package/modules/settings/sections.js +16 -14
- package/modules/settings/view.js +3 -3
- package/modules/sky/Sky.js +29 -22
- package/modules/sky/clouds.js +7 -7
- package/modules/sky/dome.js +33 -20
- package/modules/sky/domeLayers.js +9 -9
- package/modules/sky/environment.js +6 -6
- package/modules/sky/frame.js +19 -10
- package/modules/sky/season.js +7 -1
- package/modules/sky/shader.js +24 -25
- package/modules/sky/skyModule.js +14 -27
- package/modules/sky/tint.js +1 -1
- package/modules/swarm/Swarm.js +346 -0
- package/modules/swarm/draw.js +68 -0
- package/modules/swarm/grid.js +63 -0
- package/modules/terrain/ChunkManager.js +70 -52
- package/modules/terrain/chunkQueries.js +7 -24
- package/modules/terrain/chunkWorker.js +1 -1
- package/modules/terrain/draw.js +14 -11
- package/modules/terrain/index.js +28 -34
- package/modules/terrain/terrainColorMap.js +6 -3
- package/modules/terrain/terrainDefaults.js +2 -1
- package/modules/terrain/terrainEdit.js +32 -27
- package/modules/terrain/terrainGen.js +17 -12
- package/modules/terrain/terrainIsland.js +2 -2
- package/modules/terrain/terrainMesh.js +7 -6
- package/modules/terrain/terrainShape.js +8 -10
- package/modules/terrain/terrainShapeMain.js +3 -3
- package/modules/timeline/Timeline.js +27 -19
- package/modules/timeline/Tween.js +6 -6
- package/modules/timeline/shots.js +5 -7
- package/modules/triggers/index.js +9 -9
- package/modules/vegetation/Vegetation.js +30 -23
- package/modules/vegetation/densityMap.js +4 -7
- package/modules/vegetation/draw.js +23 -18
- package/modules/vegetation/groundChunks.js +11 -31
- package/modules/vegetation/index.js +8 -6
- package/modules/vegetation/modelGeometry.js +3 -6
- package/modules/vegetation/nearFade.js +1 -1
- package/modules/water/Water.js +16 -16
- package/modules/water/surface.js +3 -3
- package/modules/weather/Weather.js +41 -30
- package/modules/weather/particles.js +8 -8
- package/modules/weather/weatherDraw.js +22 -18
- package/modules/world/World.js +8 -8
- package/modules/world/explain.js +1 -1
- package/modules/world/index.js +18 -12
- package/modules/world/persist.js +73 -25
- package/modules/world/pos.js +4 -4
- package/modules/world/records.js +28 -8
- package/modules/world/sim.js +4 -4
- package/modules/world/state.js +12 -5
- package/modules/world/thingSource.js +4 -2
- package/modules/world/views.js +11 -9
- package/package.json +42 -7
- package/presets.js +1 -3
- package/render/PostFX.js +39 -27
- package/render/backend.js +26 -32
- package/render/cascades.js +17 -12
- package/render/cascades.tsl.js +5 -8
- package/render/daylight.js +20 -3
- package/render/detail.js +8 -7
- package/render/effects.js +28 -25
- package/render/fuse.js +3 -3
- package/render/fx/cloudShadow.tsl.js +3 -3
- package/render/fx/detail.tsl.js +1 -1
- package/render/fx/index.js +5 -10
- package/render/fx/library.tsl.js +11 -0
- package/render/fx/lighting.tsl.js +2 -1
- package/render/fx/nodeErrors.tsl.js +2 -2
- package/render/fx/objectOrigin.tsl.js +1 -2
- package/render/fx/patchNodes.tsl.js +5 -15
- package/render/fx/postWebGPU.tsl.js +27 -23
- package/render/fx/sky.tsl.js +15 -17
- package/render/fx/uniforms.tsl.js +4 -4
- package/render/fx/wind.tsl.js +1 -1
- package/render/fxNumbers.js +3 -0
- package/render/layers.js +1 -1
- package/render/lighting.js +5 -3
- package/render/lightmap.js +20 -20
- package/render/lights.js +6 -6
- package/render/lit.js +24 -0
- package/render/materials.js +8 -7
- package/render/mergeCommon.js +22 -0
- package/render/modeling.js +2 -2
- package/render/occlusion.js +20 -4
- package/render/ownFrame.js +4 -4
- package/render/patches.js +13 -13
- package/render/postWebGL.js +12 -10
- package/render/probes.js +16 -19
- package/render/recordSweep.js +117 -0
- package/render/shaderErrors.js +10 -9
- package/render/shadowCache.tsl.js +107 -0
- package/render/shadowTypes.js +9 -0
- package/{core → render}/shadows.js +57 -11
- package/render/slots.js +7 -7
- package/render/terms.js +22 -24
- package/render/uniformDecl.js +1 -1
- package/types/core.d.ts +200 -153
- package/types/gameplay.d.ts +163 -91
- package/types/generated/core/{Audio.d.ts → audio/Audio.d.ts} +22 -7
- package/types/generated/core/{audioVoice.d.ts → audio/audioVoice.d.ts} +2 -2
- package/types/generated/core/{Events.d.ts → base/Events.d.ts} +4 -4
- package/types/generated/core/{I18n.d.ts → base/I18n.d.ts} +7 -15
- package/types/generated/core/{Random.d.ts → base/Random.d.ts} +4 -3
- package/types/generated/core/{page.d.ts → engine/page.d.ts} +8 -9
- package/types/generated/core/{Input.d.ts → input/Input.d.ts} +12 -8
- package/types/generated/core/{Budget.d.ts → inspect/Budget.d.ts} +36 -26
- package/types/generated/core/{DebugOverrides.d.ts → inspect/DebugOverrides.d.ts} +2 -2
- package/types/generated/core/{Ops.d.ts → ops/Ops.d.ts} +4 -3
- package/types/generated/core/{control.d.ts → ops/control.d.ts} +4 -10
- package/types/generated/core/{Saves.d.ts → saves/Saves.d.ts} +37 -13
- package/types/generated/core/{session.d.ts → saves/session.d.ts} +2 -1
- package/types/generated/core/{Rays.d.ts → scene/Rays.d.ts} +8 -27
- package/types/generated/core/{Views.d.ts → scene/Views.d.ts} +1 -1
- package/types/generated/core/{Batching.d.ts → stream/Batching.d.ts} +7 -7
- package/types/generated/core/{Stream.d.ts → stream/Stream.d.ts} +18 -16
- package/types/generated/core/{Commits.d.ts → threads/Commits.d.ts} +1 -1
- package/types/generated/core/{Jobs.d.ts → threads/Jobs.d.ts} +6 -6
- package/types/generated/core/{Threads.d.ts → threads/Threads.d.ts} +4 -4
- package/types/generated/modules/calendar/Calendar.d.ts +10 -11
- package/types/generated/modules/debug/DebugPanel.d.ts +1 -9
- package/types/generated/modules/grid/Layer.d.ts +1 -1
- package/types/generated/modules/grid/index.d.ts +3 -3
- package/types/generated/modules/music/Music.d.ts +7 -5
- package/types/generated/modules/particles/Particles.d.ts +1 -1
- package/types/generated/modules/sky/Sky.d.ts +21 -10
- package/types/generated/modules/swarm/Swarm.d.ts +80 -0
- package/types/generated/modules/timeline/Timeline.d.ts +2 -1
- package/types/generated/modules/water/Water.d.ts +1 -0
- package/types/generated/modules/weather/Weather.d.ts +16 -12
- package/types/generated/modules/weather/weatherDraw.d.ts +3 -3
- package/types/generated/modules/world/World.d.ts +5 -3
- package/types/generated/render/PostFX.d.ts +17 -11
- package/types/generated/render/materials.d.ts +1 -0
- package/types/generated/render/occlusion.d.ts +9 -0
- package/types/generated/render/patches.d.ts +1 -0
- package/types/generated/ui/Focus.d.ts +1 -0
- package/types/generated/ui/UI.d.ts +1 -0
- package/types/nav.d.ts +11 -11
- package/types/physics.d.ts +99 -70
- package/types/tsl.d.ts +37 -4
- package/types/world.d.ts +38 -71
- package/ui/Focus.js +10 -9
- package/ui/Parts.js +4 -3
- package/ui/UI.js +7 -7
- package/ui/defaults.js +3 -2
- package/ui/theme.js +5 -3
- package/core/assetEntry.js +0 -109
- package/core/holds.js +0 -134
- package/core/shadowTypes.js +0 -16
- package/core/util.js +0 -17
- package/docs/MIGRATING-0.3.md +0 -248
- package/docs/MIGRATING-0.4.md +0 -320
- package/docs/MIGRATING-0.5.md +0 -68
- package/docs/MIGRATING-0.6.md +0 -126
- package/docs/PERF.md +0 -245
- package/docs/recipes/36-sky-ground-plants.md +0 -158
- package/docs/recipes/37-terrain-hooks.md +0 -115
- package/docs/recipes/38-environment-hooks.md +0 -67
- package/docs/recipes/42-terrain-shape.md +0 -64
- package/docs/recipes/47-island-snow-backdrop.md +0 -43
- package/docs/recipes/54-three-idioms.md +0 -88
- package/examples/plain-picture/FINDINGS.md +0 -53
- /package/core/{Vars.js → base/Vars.js} +0 -0
- /package/core/{mulberry32.js → base/mulberry32.js} +0 -0
- /package/core/{inputDefaults.js → input/inputDefaults.js} +0 -0
- /package/core/{textureBytes.js → inspect/textureBytes.js} +0 -0
- /package/core/{actor.js → ops/actor.js} +0 -0
- /package/core/{opsFail.js → ops/opsFail.js} +0 -0
- /package/core/{opsHistory.js → ops/opsHistory.js} +0 -0
- /package/core/{saveIds.js → saves/saveIds.js} +0 -0
- /package/core/{screenToGround.js → scene/screenToGround.js} +0 -0
- /package/core/{tier.js → scene/tier.js} +0 -0
- /package/core/{cells.js → stream/cells.js} +0 -0
- /package/core/{streamCells.js → stream/streamCells.js} +0 -0
- /package/core/{pureWorker.js → threads/pureWorker.js} +0 -0
- /package/examples/{sheepisland → perf-island}/content/index.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/props.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/templates.js +0 -0
- /package/examples/{sheepisland → perf-island}/content/vegetation.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/lights.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/materials.js +0 -0
- /package/examples/{sheepisland → perf-island}/picture/sky.js +0 -0
package/modules/hud/index.js
CHANGED
|
@@ -13,11 +13,11 @@
|
|
|
13
13
|
import * as THREE from 'three';
|
|
14
14
|
import { injectUICSS, worldToScreen } from '../../ui/UI.js';
|
|
15
15
|
|
|
16
|
-
import { readParams, keysSchema } from '../../core/params.js';
|
|
17
|
-
import { vec3 } from '../../core/math.js';
|
|
18
|
-
import { surfaceOr } from '../../core/ground.js';
|
|
16
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
17
|
+
import { vec3 } from '../../core/base/math.js';
|
|
18
|
+
import { surfaceOr } from '../../core/scene/ground.js';
|
|
19
19
|
import { CSS, defineHudParts } from './parts.js';
|
|
20
|
-
import { codedError } from '../../core/errorHints.js';
|
|
20
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
21
21
|
/** @import { ModuleDef } from '../../types/core' */
|
|
22
22
|
/** @import { Hud, HudOptions } from '../../types/gameplay' */
|
|
23
23
|
|
|
@@ -25,7 +25,7 @@ const STRINGS = {
|
|
|
25
25
|
'zh-CN': { 'module.hud': '目标指示', 'hud.distance': '{m} 米' },
|
|
26
26
|
en: { 'module.hud': 'objective markers', 'hud.distance': '{m} m' },
|
|
27
27
|
};
|
|
28
|
-
const MARKER_KEYS = ['label', 'icon', 'offscreen', 'distance', 'height'];
|
|
28
|
+
const MARKER_KEYS = ['label', 'icon', 'offscreen', 'distance', 'height'], ITEM_KEYS = ['label', 'icon', 'value', 'max', 'anchor'], BAR_KEYS = ['label', 'value', 'max', 'anchor'];
|
|
29
29
|
const _size = new THREE.Vector2();
|
|
30
30
|
const EDGE = 28; // how far the arrow sits from the screen edge (px)
|
|
31
31
|
|
|
@@ -152,28 +152,28 @@ export function hud(opts = {}) {
|
|
|
152
152
|
* status: a clock, a quest step); no value = the label alone; anchor: 'top-left' (default) | 'top' | 'top-right' |
|
|
153
153
|
* 'left' | 'right' | 'bottom-left' | 'bottom' | 'bottom-right', the screen corner / edge its column sits at
|
|
154
154
|
*/
|
|
155
|
-
counter(id, o) { item('counter', id, o); },
|
|
155
|
+
counter(id, o) { readParams('hud.counter()', keysSchema(ITEM_KEYS), o); item('counter', id, o); },
|
|
156
156
|
/**
|
|
157
157
|
* Add n (default 1; negative takes away) to a number counter and return the new value. A counter not declared yet starts at 0 with its id as the label, so
|
|
158
158
|
* declare it with counter() for a player-facing label and max
|
|
159
|
-
* @example engine.hud.counter('shells', { label: 'Shells', value: 0, max: 4 }); engine.hud.
|
|
159
|
+
* @example engine.hud.counter('shells', { label: 'Shells', value: 0, max: 4 }); engine.hud.bump('shells');
|
|
160
160
|
*/
|
|
161
|
-
|
|
161
|
+
bump(id, n = 1) {
|
|
162
162
|
const it = items.get(id), v = it?.o.value ?? 0;
|
|
163
|
-
if (typeof v !== 'number') throw codedError('hud-option', `hud.
|
|
163
|
+
if (typeof v !== 'number') throw codedError('hud-option', `hud.bump('${id}'): this counter shows text (${JSON.stringify(v)}), not a number`, TypeError);
|
|
164
164
|
item('counter', id, it ? { value: v + n } : { label: id, value: n });
|
|
165
165
|
return v + n;
|
|
166
166
|
},
|
|
167
167
|
/** Current value of a counter or bar (a number or text); undefined when there is none. This is also where an interaction's `counter` total is read */
|
|
168
168
|
value(id) { return items.get(id)?.o.value; },
|
|
169
169
|
/** Progress bar (calling again with the same id = update): { value, max = 1, label, anchor } (anchor as for counter) */
|
|
170
|
-
bar(id, o) { item('bar', id, o); },
|
|
170
|
+
bar(id, o) { readParams('hud.bar()', keysSchema(BAR_KEYS), o); item('bar', id, o); },
|
|
171
171
|
/** Remove a counter or bar */
|
|
172
172
|
remove(id) { items.get(id)?.part?.dispose(); items.delete(id); },
|
|
173
173
|
};
|
|
174
174
|
|
|
175
175
|
return {
|
|
176
|
-
name: 'hud', type: 'ui', params: HUD_PARAMS, options: opts, label: 'Objective markers', optional: ['
|
|
176
|
+
name: 'hud', type: 'ui', params: HUD_PARAMS, options: opts, label: 'Objective markers', optional: ['pickups', 'content', 'level', 'quests'], stage: 'effects', after: ['particles', 'settings'],
|
|
177
177
|
install(engine, ctx) {
|
|
178
178
|
e = engine;
|
|
179
179
|
e.i18n.add(STRINGS);
|
|
@@ -185,6 +185,12 @@ export function hud(opts = {}) {
|
|
|
185
185
|
return api;
|
|
186
186
|
},
|
|
187
187
|
frame() { for (const m of api.markers) updateMarker(m); },
|
|
188
|
+
// unloaded: nothing updates these any more, so they leave the screen too
|
|
189
|
+
dispose() {
|
|
190
|
+
for (const m of api.markers) m.remove();
|
|
191
|
+
for (const id of [...items.keys()]) api.remove(id);
|
|
192
|
+
quest.clear(); root?.layer.remove(); root = null;
|
|
193
|
+
},
|
|
188
194
|
describe() {
|
|
189
195
|
return {
|
|
190
196
|
markers: api.markers.map(m => ({ label: typeof m.o.label === 'function' ? m.o.label() : m.o.label ?? null, distance: m.dist, onScreen: m.onScreen, visible: m.visible })),
|
package/modules/hud/parts.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// colors follow ui.theme bg / fg / accent unless ui.theme hudBg / hudFg / hudAccent say otherwise; layout is plain CSS, so ui.css moves the stats column, sizes, fonts.
|
|
5
5
|
// Inner HTML: ui.templates.hudMarker / hudCounter / hudBar (data, { esc, t }) => HTML.
|
|
6
6
|
import { esc, placeAt } from '../../ui/UI.js';
|
|
7
|
-
/** @import { Engine
|
|
7
|
+
/** @import { Engine } from '../../types/core' */
|
|
8
8
|
|
|
9
9
|
export const CSS = `
|
|
10
10
|
.threedog-hud{position:fixed;inset:0;pointer-events:none;z-index:12;font:13px/1.4 var(--threedog-font);color:var(--threedog-hud-fg)}
|
|
@@ -70,8 +70,8 @@ export function defineHudParts(e, root) {
|
|
|
70
70
|
const t = tpl('hudCounter');
|
|
71
71
|
if (t) el.innerHTML = t(d, tools);
|
|
72
72
|
// value is a number (a count, with max: 2 / 5) or text (a time '08:30', a quest step): one counter for both, no second widget.
|
|
73
|
-
// No value = the label alone: a status line that has no number yet must not read "0"
|
|
74
|
-
else el.textContent = [d.icon, d.label
|
|
73
|
+
// No value = the label alone: a status line that has no number yet must not read "0". No label = the value alone: the id is the game's handle, not text for the player
|
|
74
|
+
else el.textContent = [d.icon, d.label, d.value == null ? null : d.max != null ? `${d.value} / ${d.max}` : d.value].filter(x => x != null && x !== '').join(' ');
|
|
75
75
|
},
|
|
76
76
|
dispose() { el.remove(); },
|
|
77
77
|
};
|
|
@@ -86,7 +86,7 @@ export function defineHudParts(e, root) {
|
|
|
86
86
|
place(el, d);
|
|
87
87
|
const t = tpl('hudBar');
|
|
88
88
|
if (t) { el.innerHTML = t(d, tools); return; }
|
|
89
|
-
if (label !== d.label) { label = d.label; el.innerHTML = `${d.label ? `<span>${esc(d.label)}</span>` : ''}<div class="threedog-hud-bar-track"><div class="threedog-hud-bar-fill"></div></div>`; }
|
|
89
|
+
if (label !== d.label || !el.firstChild) { label = d.label; el.innerHTML = `${d.label ? `<span>${esc(d.label)}</span>` : ''}<div class="threedog-hud-bar-track"><div class="threedog-hud-bar-fill"></div></div>`; }
|
|
90
90
|
el.style.setProperty('--threedog-hud-fill', Math.max(0, Math.min(1, (d.value ?? 0) / (d.max ?? 1))));
|
|
91
91
|
},
|
|
92
92
|
dispose() { el.remove(); },
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// What any level module must provide (fields: see gameplay/inventory.contract.js; shared check: @threedog/tools/contract).
|
|
2
|
+
// Exactly what other code reads from engine.level: marker(name) (content places, the HUD), track(handle) (a handle that goes when the level unloads),
|
|
3
|
+
// load (a level change); root / ready (hlod), markers (triggers), heightfield (vegetation's area; null when there is none).
|
|
4
|
+
// A replacement also gives the ground with engine.setGround({ height, slope, type }) and emits 'level:loaded' after each load: nav and the path caches rebuild on
|
|
5
|
+
// it (not exercised by the shared check: it needs a scene). A game with several levels registers a 'level' save with first: true, so the level is up before the player lands.
|
|
6
|
+
export default {
|
|
7
|
+
name: 'level',
|
|
8
|
+
api: ['marker', 'track', 'load'],
|
|
9
|
+
fields: ['root', 'markers', 'ready', 'heightfield'],
|
|
10
|
+
ops: [],
|
|
11
|
+
events: ['level:loaded'],
|
|
12
|
+
saves: {},
|
|
13
|
+
describe: true,
|
|
14
|
+
options: {},
|
|
15
|
+
probe: { setup: [], emits: [], state: null },
|
|
16
|
+
};
|
|
@@ -10,11 +10,11 @@
|
|
|
10
10
|
// other layers are stored only for points that have several (extra: point index -> [{ y, t }]); single-layer levels pay no extra memory or time.
|
|
11
11
|
// height(x, z, refY) with a reference height picks the highest layer not above refY + stepUp.
|
|
12
12
|
import * as THREE from 'three';
|
|
13
|
-
import { MOVEMENT_DEFAULTS } from '../../core/movement.js';
|
|
14
|
-
import { warnOnce } from '../../core/validate.js';
|
|
15
|
-
import { coded } from '../../core/errorHints.js';
|
|
13
|
+
import { MOVEMENT_DEFAULTS } from '../../core/scene/movement.js';
|
|
14
|
+
import { warnOnce } from '../../core/base/validate.js';
|
|
15
|
+
import { coded } from '../../core/base/errorHints.js';
|
|
16
16
|
|
|
17
|
-
// Step-height default comes from the shared movement params (core/movement.js); level passes engine.movement.stepHeight, don't hardcode it elsewhere
|
|
17
|
+
// Step-height default comes from the shared movement params (core/scene/movement.js); level passes engine.movement.stepHeight, don't hardcode it elsewhere
|
|
18
18
|
const STEP_UP = MOVEMENT_DEFAULTS.stepHeight, LAYER_GAP = MOVEMENT_DEFAULTS.height; // step height, min gap between layers (character height)
|
|
19
19
|
|
|
20
20
|
const _a = new THREE.Vector3(), _b = new THREE.Vector3(), _c = new THREE.Vector3();
|
|
@@ -39,10 +39,11 @@ export function buildHeightfield(meshes, { cellSize = 0.5, outside = 0, maxCells
|
|
|
39
39
|
if (nx * nz <= maxCells) break;
|
|
40
40
|
cell *= 1.5;
|
|
41
41
|
}
|
|
42
|
-
if (cell !== cellSize) warnOnce(coded('level-size', `Level too large
|
|
42
|
+
if (cell !== cellSize) warnOnce(coded('level-size', `Level too large for cellSize ${cellSize} m: the heightfield cell was enlarged to ${cell.toFixed(2)} m (coarser ground). Pass that cellSize on purpose, or split the level`));
|
|
43
43
|
const minX = bounds.min.x, minZ = bounds.min.z;
|
|
44
44
|
const H = new Float32Array(nx * nz).fill(-Infinity);
|
|
45
45
|
const T = new Uint16Array(nx * nz); // ground type index into the types array
|
|
46
|
+
const U = new Uint8Array(nx * nz); // 1: the top surface at this point faces up, so it can be stood on and survives as a layer when something is stamped above it
|
|
46
47
|
const types = ['default'], typeIndex = new Map([['default', 0]]);
|
|
47
48
|
const EPS = 1e-4;
|
|
48
49
|
const extra = new Map(); // multi-layer: point index -> layers other than the top [{ y, t }]
|
|
@@ -50,7 +51,7 @@ export function buildHeightfield(meshes, { cellSize = 0.5, outside = 0, maxCells
|
|
|
50
51
|
for (const mesh of meshes) {
|
|
51
52
|
const g = mesh.geometry, pos = g?.attributes?.position;
|
|
52
53
|
if (!pos) continue;
|
|
53
|
-
const idx = g.index, mw = mesh.matrixWorld;
|
|
54
|
+
const idx = g.index, mw = mesh.matrixWorld, mirrored = mw.determinant() < 0; // a negative scale flips the winding: its up faces read as down
|
|
54
55
|
const triCount = (idx ? idx.count : pos.count) / 3;
|
|
55
56
|
// multi-material mesh: find each triangle's material via geometry.groups
|
|
56
57
|
const groups = Array.isArray(mesh.material) && g.groups.length ? g.groups : null;
|
|
@@ -68,7 +69,7 @@ export function buildHeightfield(meshes, { cellSize = 0.5, outside = 0, maxCells
|
|
|
68
69
|
const ax = _a.x, az = _a.z, bx = _b.x, bz = _b.z, cx = _c.x, cz = _c.z;
|
|
69
70
|
const det = (bz - cz) * (ax - cx) + (cx - bx) * (az - cz);
|
|
70
71
|
if (Math.abs(det) < 1e-9) continue; // vertical faces (wall sides) are invisible from above
|
|
71
|
-
const up = (_b.z - _a.z) * (_c.x - _a.x) - (_b.x - _a.x) * (_c.z - _a.z) > 0; // only up-facing faces count as an extra layer (a box bottom is not walkable)
|
|
72
|
+
const up = (_b.z - _a.z) * (_c.x - _a.x) - (_b.x - _a.x) * (_c.z - _a.z) > 0 !== mirrored; // only up-facing faces count as an extra layer (a box bottom is not walkable)
|
|
72
73
|
const ia = Math.max(0, Math.ceil((Math.min(ax, bx, cx) - minX) / cell - 1e-6));
|
|
73
74
|
const ib = Math.min(nx - 1, Math.floor((Math.max(ax, bx, cx) - minX) / cell + 1e-6));
|
|
74
75
|
const ja = Math.max(0, Math.ceil((Math.min(az, bz, cz) - minZ) / cell - 1e-6));
|
|
@@ -85,10 +86,11 @@ export function buildHeightfield(meshes, { cellSize = 0.5, outside = 0, maxCells
|
|
|
85
86
|
if (l3 < -EPS) continue;
|
|
86
87
|
const y = l1 * _a.y + l2 * _b.y + l3 * _c.y, k = j * nx + i;
|
|
87
88
|
const top = H[k];
|
|
88
|
-
if (top === -Infinity) { H[k] = y; T[k] = curType; continue; }
|
|
89
|
+
if (top === -Infinity) { H[k] = y; T[k] = curType; U[k] = up ? 1 : 0; continue; }
|
|
89
90
|
if (y > top) {
|
|
90
|
-
|
|
91
|
-
|
|
91
|
+
// the old top becomes a lower layer when it can be stood on, whatever lands on it: a bridge's bottom face stamped before its deck must not erase the road under it
|
|
92
|
+
if (y - top > layerGap && U[k]) addLayer(k, top, T[k]);
|
|
93
|
+
H[k] = y; T[k] = curType; U[k] = up ? 1 : 0;
|
|
92
94
|
} else if (top - y > layerGap && up) addLayer(k, y, curType);
|
|
93
95
|
}
|
|
94
96
|
}
|
package/modules/level/index.js
CHANGED
|
@@ -58,201 +58,96 @@
|
|
|
58
58
|
// Event: level:loaded { root, markers, spawn }
|
|
59
59
|
// Picture: the level module draws nothing itself, the scene looks entirely like your model, so it does not read the style.
|
|
60
60
|
import * as THREE from 'three';
|
|
61
|
-
import {
|
|
62
|
-
import {
|
|
63
|
-
import {
|
|
64
|
-
import {
|
|
65
|
-
import {
|
|
66
|
-
import {
|
|
67
|
-
import { coded } from '../../core/errorHints.js';
|
|
68
|
-
import { reportError } from '../../core/errors.js';
|
|
61
|
+
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
62
|
+
import { levelNodes } from './nodes.js';
|
|
63
|
+
import { worldTrimeshes } from './geometry.js';
|
|
64
|
+
import { loadLevel, finishLoad, processScene } from './load.js';
|
|
65
|
+
import { warnOnce } from '../../core/base/validate.js';
|
|
66
|
+
import { coded } from '../../core/base/errorHints.js';
|
|
69
67
|
|
|
70
68
|
const STRINGS = {
|
|
71
69
|
'zh-CN': { 'module.level': '关卡', 'level.loadFailed': '关卡加载失败:{url}', 'level.noScene': '关卡模块需要 url 或 scene 参数' },
|
|
72
70
|
en: { 'module.level': 'level', 'level.loadFailed': 'Failed to load level: {url}', 'level.noScene': 'The level module needs a url or scene option' },
|
|
73
71
|
};
|
|
74
72
|
|
|
75
|
-
const WALK_RE = /ground|floor|road|terrain|stairs?|ramp/i;
|
|
76
|
-
const COL_RE = /^col_/i;
|
|
77
|
-
|
|
78
|
-
// a name or userData on the node itself or any ancestor counts (in glTF meshes often hang under a same-named empty)
|
|
79
|
-
function chainHas(o, root, test) {
|
|
80
|
-
for (let p = o; p; p = p.parent) { if (test(p)) return true; if (p === root) break; }
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
function chainValue(o, root, key) {
|
|
84
|
-
for (let p = o; p; p = p.parent) { if (p.userData?.[key] != null) return p.userData[key]; if (p === root) break; }
|
|
85
|
-
return undefined;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
73
|
const LEVEL_PARAMS = keysSchema(['url', 'scene', 'walkable', 'colliders', 'cellSize', 'outside', 'waterLevel', 'colliderPad', 'colliderHeight', 'sharpEdge', 'hideMarkers', 'addToScene']);
|
|
89
74
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const { url = null, scene = null, walkable = null, colliders = 'named', cellSize = 0.5, outside = 0, waterLevel = -Infinity,
|
|
93
|
-
colliderPad = 0, colliderHeight = 0, sharpEdge = 0.6, hideMarkers = true, addToScene = true } = opts;
|
|
94
|
-
let e = null, hf = null, done = false, version = 0, trimeshCache = null, pendingColliders = null;
|
|
95
|
-
let loadGen = 0; // +1 per load / unload: a file that arrives after a newer load or unload started is dropped, not processed on top of it
|
|
96
|
-
let current = typeof url === 'string' && !scene ? url : null; // current level path/asset name (for saving)
|
|
97
|
-
const walkMeshes = [], removers = [];
|
|
98
|
-
let nodeSet = null; // level nodes that become world records (nodes.js), created at install
|
|
99
|
-
|
|
75
|
+
/** The object behind engine.level; st is the module's state (level() below). load and unload go through api, so a wrapped one (engine.wrap) is the one that runs */
|
|
76
|
+
function levelApi(st) {
|
|
100
77
|
const api = {
|
|
101
78
|
root: null, markers: {}, spawn: new THREE.Vector3(), heightfield: null,
|
|
102
|
-
get ready() { return done; },
|
|
79
|
+
get ready() { return st.done; },
|
|
103
80
|
/** Register things belonging to the current level ({ remove() } handles); auto-removed on level switch / unload; returns the same handle */
|
|
104
|
-
track(h) { if (h) removers.push(h); return h; },
|
|
81
|
+
track(h) { if (h) st.removers.push(h); return h; },
|
|
105
82
|
marker: name => api.markers[name] || null,
|
|
106
83
|
markersWith: prefix => Object.values(api.markers).filter(m => m.name.startsWith(prefix)),
|
|
107
84
|
/** @internal Triangles of walkable meshes (world space), [{ positions: Float32Array, indices: Uint32Array }], cached */
|
|
108
85
|
staticTrimeshes() {
|
|
109
|
-
if (trimeshCache) return trimeshCache;
|
|
110
|
-
trimeshCache = worldTrimeshes(walkMeshes);
|
|
111
|
-
return trimeshCache;
|
|
86
|
+
if (st.trimeshCache) return st.trimeshCache;
|
|
87
|
+
st.trimeshCache = worldTrimeshes(st.walkMeshes);
|
|
88
|
+
return st.trimeshCache;
|
|
112
89
|
},
|
|
113
90
|
/** Unload the level: remove from scene, delete colliders, ground reverts to the outside height */
|
|
114
91
|
unload() {
|
|
115
|
-
for (const h of removers.splice(0)) h.remove();
|
|
116
|
-
if (api.root && addToScene) e.remove(api.root); // full removal (geometries and materials shared by assets are not disposed)
|
|
117
|
-
walkMeshes.length = 0; hf = null; trimeshCache = null; api.markers = {}; api.root = null; current = null; version++; loadGen++;
|
|
92
|
+
for (const h of st.removers.splice(0)) h.remove();
|
|
93
|
+
if (api.root && st.o.addToScene) st.e.remove(api.root); // full removal (geometries and materials shared by assets are not disposed)
|
|
94
|
+
st.walkMeshes.length = 0; st.hf = api.heightfield = null; st.pendingColliders = null; st.trimeshCache = null; api.markers = {}; api.root = null; st.current = null; st.version++; st.loadGen++;
|
|
95
|
+
st.done = true; // nothing is loading now: a file still on its way is dropped, and no finishLoad would ever come for it (load sets it back)
|
|
118
96
|
},
|
|
119
97
|
/** Switch level: unload the current one, load a new one (glTF path/asset name, or THREE object), emit level:loaded when done */
|
|
120
|
-
async load(urlOrScene) { return loadLevel(urlOrScene, false); },
|
|
98
|
+
async load(urlOrScene) { return loadLevel(st, urlOrScene, false); },
|
|
121
99
|
};
|
|
100
|
+
return api;
|
|
101
|
+
}
|
|
122
102
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// a newer load / unload started while this file loaded (two doors in a row, a save loaded mid-switch): that one owns the level now;
|
|
137
|
-
// processing this one too would put both levels in the scene under the newer level's name
|
|
138
|
-
if (gen !== loadGen || e.disposed) { e.remove(scene); return; } // e.remove releases the copy (assets.trim can free the file)
|
|
139
|
-
process(scene);
|
|
140
|
-
} catch (err) {
|
|
141
|
-
if (gen !== loadGen || e.disposed) return; // superseded: the newer load reports its own outcome
|
|
142
|
-
current = null; // the save must not name a level that never loaded
|
|
143
|
-
if (hard) throw err;
|
|
144
|
-
reportError(e, err, `level.load('${urlOrScene}')`, { soft: true }); toast(e, e.t('level.loadFailed', { url: urlOrScene }), { kind: 'error' });
|
|
145
|
-
}
|
|
146
|
-
} else process(urlOrScene);
|
|
147
|
-
loaded();
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function loaded() {
|
|
151
|
-
addColliders(); // if the collision module is installed after level, add colliders here
|
|
152
|
-
// nodes tagged template / id become world records (loading is setup, not a gameplay action) before anyone (hlod...) sees level:loaded
|
|
153
|
-
e.ops.allow(nodeSet.place);
|
|
154
|
-
done = true;
|
|
155
|
-
e.assets.trim(); // old level's models have no copies in use: return GPU memory
|
|
156
|
-
e.events.emit('level:loaded', { root: api.root, markers: api.markers, spawn: api.spawn });
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/** Process a scene: classify meshes, build the heightfield, record markers. Colliders wait for the collision module (may not be installed at install time) */
|
|
160
|
-
function process(root) {
|
|
161
|
-
api.root = root;
|
|
162
|
-
root.userData.budgetTag ??= 'level';
|
|
163
|
-
if (addToScene && !root.parent) e.add(root, { owner: 'level' });
|
|
164
|
-
root.updateMatrixWorld(true);
|
|
165
|
-
nodeSet.take(root); // record nodes leave the level root first: no ground, collision or ray registration for them, and hlod never bakes them
|
|
166
|
-
const meshes = [], cols = [], markerObjs = [];
|
|
167
|
-
root.traverse(o => {
|
|
168
|
-
if (MARKER_RE.test(o.name)) { markerObjs.push(o); return; }
|
|
169
|
-
if (!o.isMesh) return;
|
|
170
|
-
// placeholder models under a marker
|
|
171
|
-
if (o.parent && chainHas(o.parent, root, p => MARKER_RE.test(p.name))) { if (hideMarkers) o.visible = false; return; }
|
|
172
|
-
if (chainHas(o, root, p => COL_RE.test(p.name))) { cols.push(o); o.visible = false; return; }
|
|
173
|
-
meshes.push(o);
|
|
174
|
-
});
|
|
175
|
-
// walkable meshes
|
|
176
|
-
const isWalk = walkable || (m => chainHas(m, root, p => p.userData?.walkable === true || WALK_RE.test(p.name)));
|
|
177
|
-
let walk = meshes.filter(m => chainValue(m, root, 'walkable') !== false && isWalk(m));
|
|
178
|
-
if (!walkable && !walk.length) walk = meshes.filter(m => chainValue(m, root, 'walkable') !== false);
|
|
179
|
-
walkMeshes.push(...walk);
|
|
180
|
-
const walkSet = new Set(walk);
|
|
181
|
-
for (const m of meshes) {
|
|
182
|
-
if (walkSet.has(m)) continue;
|
|
183
|
-
if (chainValue(m, root, 'collider')) cols.push(m);
|
|
184
|
-
else if (colliders === 'auto') { const b = new THREE.Box3().setFromObject(m); if (b.max.y - b.min.y > 0.5) cols.push(m); }
|
|
185
|
-
}
|
|
186
|
-
// heightfield
|
|
187
|
-
const typeOf = (m, mi) => {
|
|
188
|
-
const t = chainValue(m, root, 'groundType'); if (t != null) return String(t);
|
|
189
|
-
const mat = Array.isArray(m.material) ? m.material[mi] : m.material;
|
|
190
|
-
return mat?.name || 'default';
|
|
191
|
-
};
|
|
192
|
-
const t0 = performance.now(); // measure
|
|
193
|
-
hf = api.heightfield = buildHeightfield(walk, { cellSize, outside, typeOf, sharpEdge, stepUp: e.movement.stepHeight, layerGap: e.movement.height });
|
|
194
|
-
api.buildMs = performance.now() - t0; // measure: heightfield build time (shown in the debug panel)
|
|
195
|
-
// markers
|
|
196
|
-
const markers = api.markers = {};
|
|
197
|
-
for (const o of markerObjs) {
|
|
198
|
-
const position = new THREE.Vector3(), quaternion = new THREE.Quaternion(), scale = new THREE.Vector3();
|
|
199
|
-
o.matrixWorld.decompose(position, quaternion, scale);
|
|
200
|
-
markers[o.name] = { name: o.name, position, quaternion, scale, userData: { ...o.userData }, object: o };
|
|
201
|
-
if (hideMarkers && o.isMesh) o.visible = false;
|
|
202
|
-
}
|
|
203
|
-
const sp = markers.spawn || Object.values(markers).find(m => /^spawn_/i.test(m.name));
|
|
204
|
-
api.spawn = sp ? sp.position.clone() : new THREE.Vector3(0, hf.height(0, 0), 0);
|
|
205
|
-
pendingColliders = colliders === false ? cols.filter(m => COL_RE.test(m.name) || chainValue(m, root, 'collider')) : cols;
|
|
206
|
-
// rays: all meshes; collision meshes and shown non-walkable meshes also occlude the camera, unless a node says userData.occluder = false (a glass roof, a thin railing).
|
|
207
|
-
// col_ meshes are hidden by design and still block; any other hidden mesh is not on screen, so the arm has nothing to avoid
|
|
208
|
-
const blocks = m => chainValue(m, root, 'occluder') !== false, shownHere = m => !chainHas(m, root, p => !p.visible);
|
|
209
|
-
removers.push(e.rays.addStatic(root), e.rays.addStatic([...cols.filter(blocks), ...meshes.filter(m => !walkSet.has(m) && shownHere(m) && blocks(m))], { occluder: true }));
|
|
210
|
-
trimeshCache = null; version++;
|
|
211
|
-
addColliders();
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
/** Turn collision meshes into rotated boxes in the collision module (best-fit box by the mesh's world orientation) */
|
|
215
|
-
function addColliders() {
|
|
216
|
-
const ph = e.collision;
|
|
217
|
-
if (!ph || !pendingColliders) return;
|
|
218
|
-
for (const b of colliderBoxes(pendingColliders, { colliderHeight, colliderPad })) removers.push(ph.addBox(b.at, b.opts));
|
|
219
|
-
pendingColliders = null;
|
|
220
|
-
}
|
|
103
|
+
export function level(opts = {}) {
|
|
104
|
+
readParams('level', LEVEL_PARAMS, opts);
|
|
105
|
+
const { url = null, scene = null, walkable = null, colliders = 'named', cellSize = 0.5, outside = 0, waterLevel = -Infinity,
|
|
106
|
+
colliderPad = 0, colliderHeight = 0, sharpEdge = 0.6, hideMarkers = true, addToScene = true } = opts;
|
|
107
|
+
const st = {
|
|
108
|
+
e: null, hf: null, done: false, version: 0, trimeshCache: null, pendingColliders: null,
|
|
109
|
+
o: { walkable, colliders, cellSize, outside, colliderPad, colliderHeight, sharpEdge, hideMarkers, addToScene },
|
|
110
|
+
loadGen: 0, // +1 per load / unload: a file that arrives after a newer load or unload started is dropped, not processed on top of it
|
|
111
|
+
current: typeof url === 'string' && !scene ? url : null, // current level path/asset name (for saving)
|
|
112
|
+
walkMeshes: [], removers: [],
|
|
113
|
+
nodeSet: null, // level nodes that become world records (nodes.js), created at install
|
|
114
|
+
};
|
|
115
|
+
const api = st.api = levelApi(st);
|
|
221
116
|
|
|
222
117
|
return {
|
|
223
|
-
name: 'level', type: 'world', provides: ['level'], params: LEVEL_PARAMS, options: opts, label: 'Level', stage: 'ground', after: ['terrain'], optional: ['world', 'content'],
|
|
118
|
+
name: 'level', type: 'world', provides: ['level'], params: LEVEL_PARAMS, options: opts, label: 'Level', stage: 'ground', after: ['terrain'], optional: ['world', 'content', 'collision'],
|
|
224
119
|
install(engine) {
|
|
225
|
-
e = engine;
|
|
226
|
-
nodeSet = levelNodes(e, () => api.root, () => current);
|
|
120
|
+
const e = st.e = engine;
|
|
121
|
+
st.nodeSet = levelNodes(e, () => api.root, () => st.current);
|
|
227
122
|
e.i18n.add(STRINGS);
|
|
228
123
|
// ground API: before the heightfield is built, return the outside height
|
|
229
124
|
e.setGround({
|
|
230
|
-
height: (x, z, refY) => hf ? hf.height(x, z, refY) : outside,
|
|
231
|
-
slope: (x, z) => hf ? hf.slope(x, z) : 0,
|
|
232
|
-
type: (x, z) => hf ? hf.type(x, z) : 'default',
|
|
125
|
+
height: (x, z, refY) => st.hf ? st.hf.height(x, z, refY) : outside,
|
|
126
|
+
slope: (x, z) => st.hf ? st.hf.slope(x, z) : 0,
|
|
127
|
+
type: (x, z) => st.hf ? st.hf.type(x, z) : 'default',
|
|
233
128
|
waterLevel,
|
|
234
129
|
staticTrimeshes: () => api.staticTrimeshes(),
|
|
235
|
-
staticVersion: () => version, // +1 on level change; the physics module rebuilds static colliders on it
|
|
130
|
+
staticVersion: () => st.version, // +1 on level change; the physics module rebuilds static colliders on it
|
|
236
131
|
});
|
|
237
|
-
if (scene)
|
|
132
|
+
if (scene) processScene(st, scene);
|
|
238
133
|
// on load, switch back to the saved level (code-built scenes have no name, record null, no switch)
|
|
239
134
|
// load returns a Promise: the save system waits for the new level (old level's trigger zones are already removed by unload) before reading player
|
|
240
135
|
// position etc.
|
|
241
|
-
e.saves.register('level', { first: true, save: () => current, load: d => { if (typeof d === 'string' && d !== current) return api.load(d); } });
|
|
136
|
+
e.saves.register('level', { first: true, save: () => st.current, load: d => { if (typeof d === 'string' && d !== st.current) return api.load(d); } });
|
|
242
137
|
return api;
|
|
243
138
|
},
|
|
244
139
|
critical: true, // the level file is the game's world: a failed load stops start() with the reason
|
|
245
140
|
async init(engine) {
|
|
246
|
-
if (scene) return
|
|
247
|
-
if (url) return loadLevel(url, true);
|
|
141
|
+
if (scene) return finishLoad(st);
|
|
142
|
+
if (url) return loadLevel(st, url, true);
|
|
248
143
|
warnOnce(coded('level-node', engine.t('level.noScene')));
|
|
249
|
-
|
|
144
|
+
finishLoad(st);
|
|
250
145
|
},
|
|
251
|
-
isReady() { return done; },
|
|
146
|
+
isReady() { return st.done; },
|
|
252
147
|
// which level is up and what it gave: a wrong path shows as file set but markers 0 / walk 0, a code-built scene as file null
|
|
253
148
|
describe() {
|
|
254
149
|
const s = api.spawn, r = v => Math.round(v * 10) / 10;
|
|
255
|
-
return { file: current, ready: done, markers: Object.keys(api.markers).length, walkMeshes: walkMeshes.length, removers: removers.length,
|
|
150
|
+
return { file: st.current, ready: st.done, markers: Object.keys(api.markers).length, walkMeshes: st.walkMeshes.length, removers: st.removers.length,
|
|
256
151
|
spawn: { x: r(s.x), y: r(s.y), z: r(s.z) } };
|
|
257
152
|
},
|
|
258
153
|
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Loading a level: st is the module's state (index.js), and the steps run in this order: loadLevel -> processScene -> finishLoad.
|
|
2
|
+
import * as THREE from 'three';
|
|
3
|
+
import { buildHeightfield } from './heightfield.js';
|
|
4
|
+
import { colliderBoxes } from './geometry.js';
|
|
5
|
+
import { sortNodes, groundTypeOf, readMarkers, chainHas, chainValue, COL_RE } from './meshes.js';
|
|
6
|
+
import { toast } from '../../ui/UI.js';
|
|
7
|
+
import { reportError } from '../../core/base/errors.js';
|
|
8
|
+
|
|
9
|
+
/** hard: the first load from init() (the module is critical, so a missing level file must stop start() instead of opening an empty world);
|
|
10
|
+
* a switch while playing or a save load stays soft (reported + toast) so the running game is not torn down by one bad path */
|
|
11
|
+
export async function loadLevel(st, urlOrScene, hard) {
|
|
12
|
+
const { e, api } = st;
|
|
13
|
+
api.unload(); st.done = false;
|
|
14
|
+
e.commits.invalidate(); // background results requested for the old level must not land on the new one
|
|
15
|
+
// world-data zone = level name: other levels' NPCs etc. only lose models, data stays and is rebuilt on return (code-built scenes have no name, record
|
|
16
|
+
// null, no zoning)
|
|
17
|
+
if (e.world) e.world.zone = typeof urlOrScene === 'string' ? urlOrScene : null;
|
|
18
|
+
if (typeof urlOrScene === 'string') {
|
|
19
|
+
st.current = urlOrScene; // set before processScene(): level nodes read it as their zone
|
|
20
|
+
const gen = st.loadGen;
|
|
21
|
+
try {
|
|
22
|
+
const scene = await e.assets.model(urlOrScene);
|
|
23
|
+
// a newer load / unload started while this file loaded (two doors in a row, a save loaded mid-switch): that one owns the level now;
|
|
24
|
+
// processing this one too would put both levels in the scene under the newer level's name
|
|
25
|
+
if (gen !== st.loadGen || e.disposed) { e.remove(scene); return; } // e.remove releases the copy (assets.trim can free the file)
|
|
26
|
+
processScene(st, scene);
|
|
27
|
+
} catch (err) {
|
|
28
|
+
if (gen !== st.loadGen || e.disposed) return; // superseded: the newer load reports its own outcome
|
|
29
|
+
st.current = null; // the save must not name a level that never loaded
|
|
30
|
+
if (hard) throw err;
|
|
31
|
+
reportError(e, err, `level.load('${urlOrScene}')`, { soft: true }); toast(e, e.t('level.loadFailed', { url: urlOrScene }), { kind: 'error' });
|
|
32
|
+
}
|
|
33
|
+
} else processScene(st, urlOrScene);
|
|
34
|
+
finishLoad(st);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function finishLoad(st) {
|
|
38
|
+
const { e, api } = st;
|
|
39
|
+
addColliders(st); // if the collision module is installed after level, add colliders here
|
|
40
|
+
// nodes tagged template / id become world records (loading is setup, not a gameplay action) before anyone (hlod...) sees level:loaded
|
|
41
|
+
e.ops.allow(st.nodeSet.place);
|
|
42
|
+
st.done = true;
|
|
43
|
+
e.assets.trim(); // old level's models have no copies in use: return GPU memory
|
|
44
|
+
e.events.emit('level:loaded', { root: api.root, markers: api.markers, spawn: api.spawn });
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Process a scene: classify meshes, build the heightfield, record markers. Colliders wait for the collision module (may not be installed at install time) */
|
|
48
|
+
export function processScene(st, root) {
|
|
49
|
+
const { e, api, o } = st;
|
|
50
|
+
api.root = root;
|
|
51
|
+
root.userData.budgetTag ??= 'level';
|
|
52
|
+
if (o.addToScene && !root.parent) e.add(root, { owner: 'level' });
|
|
53
|
+
root.updateMatrixWorld(true);
|
|
54
|
+
st.nodeSet.take(root); // record nodes leave the level root first: no ground, collision or ray registration for them, and hlod never bakes them
|
|
55
|
+
const { meshes, cols, markerObjs, walk, walkSet } = sortNodes(root, o);
|
|
56
|
+
st.walkMeshes.push(...walk);
|
|
57
|
+
// heightfield
|
|
58
|
+
const t0 = performance.now(); // measure
|
|
59
|
+
st.hf = api.heightfield = buildHeightfield(walk, { cellSize: o.cellSize, outside: o.outside, typeOf: groundTypeOf(root), sharpEdge: o.sharpEdge, stepUp: e.movement.stepHeight, layerGap: e.movement.height });
|
|
60
|
+
api.buildMs = performance.now() - t0; // measure: heightfield build time (shown in the debug panel)
|
|
61
|
+
const markers = api.markers = readMarkers(markerObjs, o.hideMarkers);
|
|
62
|
+
const sp = markers.spawn || Object.values(markers).find(m => /^spawn_/i.test(m.name));
|
|
63
|
+
api.spawn = sp ? sp.position.clone() : new THREE.Vector3(0, st.hf.height(0, 0), 0);
|
|
64
|
+
st.pendingColliders = o.colliders === false ? cols.filter(m => COL_RE.test(m.name) || chainValue(m, root, 'collider')) : cols;
|
|
65
|
+
// rays: all meshes; collision meshes and shown non-walkable meshes also occlude the camera, unless a node says userData.occluder = false (a glass roof, a thin railing).
|
|
66
|
+
// col_ meshes are hidden by design and still block; any other hidden mesh is not on screen, so the arm has nothing to avoid
|
|
67
|
+
const blocks = m => chainValue(m, root, 'occluder') !== false, shownHere = m => !chainHas(m, root, p => !p.visible);
|
|
68
|
+
st.removers.push(e.rays.addStatic(root), e.rays.addStatic([...cols.filter(blocks), ...meshes.filter(m => !walkSet.has(m) && shownHere(m) && blocks(m))], { occluder: true }));
|
|
69
|
+
st.trimeshCache = null; st.version++;
|
|
70
|
+
addColliders(st);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Turn collision meshes into rotated boxes in the collision module (best-fit box by the mesh's world orientation) */
|
|
74
|
+
function addColliders(st) {
|
|
75
|
+
const ph = st.e.collision;
|
|
76
|
+
if (!ph || !st.pendingColliders) return;
|
|
77
|
+
for (const b of colliderBoxes(st.pendingColliders, { colliderHeight: st.o.colliderHeight, colliderPad: st.o.colliderPad })) st.removers.push(ph.addBox(b.at, b.opts));
|
|
78
|
+
st.pendingColliders = null;
|
|
79
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Sorting a level's nodes by their names and custom properties (the naming conventions: the top of index.js). Reads the scene, builds nothing; the only write is
|
|
2
|
+
// hiding what the conventions hide.
|
|
3
|
+
import * as THREE from 'three';
|
|
4
|
+
import { MARKER_RE } from './nodes.js';
|
|
5
|
+
|
|
6
|
+
const WALK_RE = /ground|floor|road|terrain|stairs?|ramp/i;
|
|
7
|
+
export const COL_RE = /^col_/i;
|
|
8
|
+
|
|
9
|
+
// a name or userData on the node itself or any ancestor counts (in glTF meshes often hang under a same-named empty)
|
|
10
|
+
export function chainHas(o, root, test) {
|
|
11
|
+
for (let p = o; p; p = p.parent) { if (test(p)) return true; if (p === root) break; }
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
export function chainValue(o, root, key) {
|
|
15
|
+
for (let p = o; p; p = p.parent) { if (p.userData?.[key] != null) return p.userData[key]; if (p === root) break; }
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sort the nodes under root
|
|
21
|
+
* @param {any} root
|
|
22
|
+
* @param {{ walkable: any, colliders: any, hideMarkers: boolean }} o the level options
|
|
23
|
+
* @returns {{ meshes: any[], cols: any[], markerObjs: any[], walk: any[], walkSet: Set<any> }} meshes: every plain mesh; cols: the collision meshes; walk: the walkable ones
|
|
24
|
+
*/
|
|
25
|
+
export function sortNodes(root, { walkable, colliders, hideMarkers }) {
|
|
26
|
+
const meshes = [], cols = [], markerObjs = [];
|
|
27
|
+
root.traverse(o => {
|
|
28
|
+
if (MARKER_RE.test(o.name)) { markerObjs.push(o); return; }
|
|
29
|
+
if (!o.isMesh) return;
|
|
30
|
+
// placeholder models under a marker
|
|
31
|
+
if (o.parent && chainHas(o.parent, root, p => MARKER_RE.test(p.name))) { if (hideMarkers) o.visible = false; return; }
|
|
32
|
+
if (chainHas(o, root, p => COL_RE.test(p.name))) { cols.push(o); o.visible = false; return; }
|
|
33
|
+
meshes.push(o);
|
|
34
|
+
});
|
|
35
|
+
// walkable meshes
|
|
36
|
+
const isWalk = walkable || (m => chainHas(m, root, p => p.userData?.walkable === true || WALK_RE.test(p.name)));
|
|
37
|
+
let walk = meshes.filter(m => chainValue(m, root, 'walkable') !== false && isWalk(m));
|
|
38
|
+
if (!walkable && !walk.length) walk = meshes.filter(m => chainValue(m, root, 'walkable') !== false);
|
|
39
|
+
const walkSet = new Set(walk);
|
|
40
|
+
for (const m of meshes) {
|
|
41
|
+
if (walkSet.has(m)) continue;
|
|
42
|
+
if (chainValue(m, root, 'collider')) cols.push(m);
|
|
43
|
+
else if (colliders === 'auto') { const b = new THREE.Box3().setFromObject(m); if (b.max.y - b.min.y > 0.5) cols.push(m); }
|
|
44
|
+
}
|
|
45
|
+
return { meshes, cols, markerObjs, walk, walkSet };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The ground type of a mesh's material slot: custom property groundType, else the material name */
|
|
49
|
+
export const groundTypeOf = root => (m, mi) => {
|
|
50
|
+
const t = chainValue(m, root, 'groundType'); if (t != null) return String(t);
|
|
51
|
+
const mat = Array.isArray(m.material) ? m.material[mi] : m.material;
|
|
52
|
+
return mat?.name || 'default';
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** engine.level.markers from the marker nodes */
|
|
56
|
+
export function readMarkers(markerObjs, hideMarkers) {
|
|
57
|
+
const markers = {};
|
|
58
|
+
for (const o of markerObjs) {
|
|
59
|
+
const position = new THREE.Vector3(), quaternion = new THREE.Quaternion(), scale = new THREE.Vector3();
|
|
60
|
+
o.matrixWorld.decompose(position, quaternion, scale);
|
|
61
|
+
markers[o.name] = { name: o.name, position, quaternion, scale, userData: { ...o.userData }, object: o };
|
|
62
|
+
if (hideMarkers && o.isMesh) o.visible = false;
|
|
63
|
+
}
|
|
64
|
+
return markers;
|
|
65
|
+
}
|
package/modules/level/nodes.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Level nodes that become world records: nodes tagged with the custom property template or id (Blender -> glTF extras -> userData) leave the level root
|
|
2
2
|
// and are placed as authored records `<level>/<node>` once the level is processed. Created per level module by levelNodes(e, getRoot, getCurrent).
|
|
3
3
|
import * as THREE from 'three';
|
|
4
|
-
import { warnOnce } from '../../core/validate.js';
|
|
5
|
-
import { codedError } from '../../core/errorHints.js';
|
|
4
|
+
import { warnOnce, fmt } from '../../core/base/validate.js';
|
|
5
|
+
import { codedError } from '../../core/base/errorHints.js';
|
|
6
6
|
|
|
7
7
|
const STRINGS_DEV = {
|
|
8
8
|
noWorld: '[level] nodes with the custom property template or id become world records: install world() (the openWorld() preset has it); until then they stay in the level as plain scenery (ground, collision, rays like any node)',
|
|
@@ -48,14 +48,13 @@ export function levelNodes(e, getRoot, getCurrent) {
|
|
|
48
48
|
const OWN = ['template', 'id', 'name', 'hlod']; // userData keys that are not record fields (three's glTF loader keeps the node name in userData.name)
|
|
49
49
|
/** Taken nodes -> authored world records, id `<level>/<node>`: a template instance (custom property template) or the node itself (custom property id) */
|
|
50
50
|
function place() {
|
|
51
|
-
if (!taken.length) return;
|
|
52
|
-
if (!e.world) return; // takeNodes took nothing then (the nodes stayed in the level)
|
|
51
|
+
if (!taken.length) return; // also without world(): take left the nodes in the level
|
|
53
52
|
const level = levelName(), zone = e.world.zone;
|
|
54
53
|
for (const o of taken.splice(0)) {
|
|
55
54
|
const id = `${level}/${o.name}`, ud = o.userData, node = o.name, { x, y, z } = o.position;
|
|
56
55
|
const fields = Object.fromEntries(Object.entries(ud).filter(([k, v]) => !OWN.includes(k) && (v == null || typeof v !== 'object' || Array.isArray(v) || v.constructor === Object)));
|
|
57
56
|
if (ud.template != null) {
|
|
58
|
-
if (!e.content) throw codedError('level-node', STRINGS_DEV.noContent
|
|
57
|
+
if (!e.content) throw codedError('level-node', fmt(STRINGS_DEV.noContent, { id }));
|
|
59
58
|
const yaw = new THREE.Euler().setFromQuaternion(o.quaternion, 'YXZ').y, state = Object.keys(e.content.template(ud.template).state ?? {});
|
|
60
59
|
const own = Object.fromEntries(Object.entries(fields).filter(([k]) => state.includes(k)));
|
|
61
60
|
e.content.placeNode({ template: ud.template, id, x, z, floor: y, ...(yaw && { rot: yaw }), ...(o.scale.x !== 1 && { scale: o.scale.x }), ...own }, zone, { level, node });
|
|
@@ -63,7 +62,7 @@ export function levelNodes(e, getRoot, getCurrent) {
|
|
|
63
62
|
presentNodes();
|
|
64
63
|
nodes.set(id, o);
|
|
65
64
|
const rec = e.world.add({ ...fields, id, kind: 'levelNode', position: { x, y, z }, ...(zone != null && { zone }), level, node }, { initial: true });
|
|
66
|
-
if (rec && rec.kind !== 'levelNode') throw codedError('level-node', STRINGS_DEV.clash
|
|
65
|
+
if (rec && rec.kind !== 'levelNode') throw codedError('level-node', fmt(STRINGS_DEV.clash, { id, kind: rec.kind }));
|
|
67
66
|
}
|
|
68
67
|
e.world.setOrigin(id, { level, node });
|
|
69
68
|
}
|
|
@@ -74,7 +73,7 @@ export function levelNodes(e, getRoot, getCurrent) {
|
|
|
74
73
|
presented = e.world.present('levelNode', {
|
|
75
74
|
build(rec) {
|
|
76
75
|
const o = nodes.get(rec.id);
|
|
77
|
-
if (!o) throw codedError('level-node', STRINGS_DEV.notLoaded
|
|
76
|
+
if (!o) throw codedError('level-node', fmt(STRINGS_DEV.notLoaded, { id: rec.id }));
|
|
78
77
|
o.userData.unstatic = e.rays.addStatic(o);
|
|
79
78
|
return o;
|
|
80
79
|
},
|