threedog 0.8.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +223 -4
- package/README.md +6 -6
- package/core/assets/Animator.js +2 -0
- package/core/assets/Assets.js +44 -13
- package/core/assets/assetEntry.js +87 -22
- package/core/assets/assetHot.js +2 -1
- package/core/assets/assetReport.js +1 -2
- package/core/audio/Audio.js +24 -5
- package/core/base/I18n.js +6 -3
- package/core/base/Random.js +3 -1
- package/core/base/Vars.js +14 -4
- package/core/base/errorCodes.js +54 -16
- package/core/base/errorHints.js +12 -1
- package/core/base/errors.js +6 -4
- package/core/base/events.generated.js +10 -0
- package/core/base/follows.js +26 -0
- package/core/base/math.js +15 -8
- package/core/base/params.js +2 -2
- package/core/{saves → base}/saveValues.js +24 -6
- package/core/base/sections.js +97 -0
- package/core/base/spelling.js +32 -0
- package/core/base/validate.js +8 -29
- package/core/engine/Engine.js +61 -18
- package/core/engine/Loading.js +30 -3
- package/core/engine/copies.js +1 -1
- package/core/engine/headlessLoad.js +30 -13
- package/core/engine/lifecycle.js +30 -25
- package/core/engine/loop.js +22 -4
- package/core/engine/moduleSlots.js +91 -15
- package/core/engine/moduleTypes.js +13 -0
- package/core/engine/modules.js +248 -62
- package/core/engine/page.js +25 -8
- package/core/engine/plugins.js +15 -3
- package/core/engine/services.js +3 -0
- package/core/engine/startup.js +46 -36
- package/core/input/Input.js +39 -19
- package/core/input/inputKeyboard.js +39 -0
- package/core/input/inputPad.js +23 -2
- package/core/input/inputPointer.js +3 -1
- package/core/input/inputSeat.js +66 -0
- package/core/input/inputTouch.js +23 -6
- package/core/inspect/DebugOverrides.js +13 -2
- package/core/inspect/Describe.js +10 -6
- package/core/inspect/Inspect.js +31 -4
- package/core/inspect/UrlState.js +14 -10
- package/core/inspect/catalog.js +12 -5
- package/core/inspect/describeRender.js +3 -2
- package/core/inspect/describeText.js +6 -2
- package/core/inspect/metadata.generated.js +4140 -0
- package/core/inspect/pictureWriters.js +117 -33
- package/core/inspect/toolDoor.js +21 -0
- package/core/inspect/valueHistory.js +35 -0
- package/core/ops/Ops.js +32 -7
- package/core/ops/control.js +48 -23
- package/core/ops/holds.js +4 -3
- package/core/ops/opsAtomic.js +18 -6
- package/core/ops/opsCheck.js +7 -5
- package/core/ops/opsFail.js +5 -7
- package/core/ops/opsRun.js +2 -2
- package/core/ops/owned.js +20 -3
- package/core/ops/sharedPoint.js +7 -10
- package/core/ops/stacks.js +22 -1
- package/core/saves/SaveStore.js +52 -26
- package/core/saves/Saves.js +122 -27
- package/core/saves/saveApply.js +159 -61
- package/core/saves/saveFiles.js +30 -25
- package/core/saves/saveVersions.js +20 -6
- package/core/saves/session.js +76 -33
- package/core/saves/stateReplace.js +184 -0
- package/core/scene/Rays.js +21 -5
- package/core/scene/Views.js +6 -6
- package/core/scene/adaptive.js +13 -10
- package/core/scene/canvasBox.js +2 -0
- package/core/scene/compositeGround.js +72 -0
- package/core/scene/floor.js +17 -0
- package/core/scene/grid.js +2 -0
- package/core/scene/ground.contract.js +14 -0
- package/core/scene/ground.js +52 -17
- package/core/scene/pixel.js +154 -0
- package/core/scene/pixelRatio.js +167 -0
- package/core/scene/sceneDelta.js +3 -3
- package/core/scene/sceneMethods.js +11 -6
- package/core/scene/stepPoses.js +6 -1
- package/core/scene/thingExplain.js +1 -1
- package/core/scene/thingOps.js +22 -14
- package/core/scene/things.js +1 -1
- package/core/scene/tier.js +10 -0
- package/core/scene/view.js +126 -9
- package/core/stream/Batching.js +0 -1
- package/core/stream/Stream.js +24 -12
- package/core/stream/batchGroups.js +1 -0
- package/core/stream/scatter.js +5 -2
- package/core/stream/streamSources.js +2 -1
- package/core/threads/Commits.js +12 -7
- package/core/threads/Jobs.js +15 -5
- package/core/threads/Threads.js +5 -2
- package/core/time/Clock.js +3 -1
- package/core/time/skip.js +10 -4
- package/core/time/time.js +16 -10
- package/docs/AI.md +89 -72
- package/docs/API.md +18 -14
- package/docs/CODEMAP.md +68 -40
- package/docs/COMMANDS.md +22 -23
- package/docs/CONVENTIONS.md +120 -17
- package/docs/MIGRATING-0.8.md +2 -0
- package/docs/MIGRATING-1.0.md +101 -0
- package/docs/THREE.md +65 -12
- package/docs/api/cameraRig.md +3 -0
- package/docs/api/characters.md +13 -7
- package/docs/api/collider3d.md +9 -0
- package/docs/api/content.md +16 -15
- package/docs/api/dialogue.md +2 -1
- package/docs/api/engine.md +18 -16
- package/docs/api/events.md +14 -4
- package/docs/api/ground.md +2 -2
- package/docs/api/i18n.md +1 -1
- package/docs/api/input.md +2 -1
- package/docs/api/interactions.md +1 -1
- package/docs/api/inventory.md +1 -1
- package/docs/api/level.md +3 -2
- package/docs/api/nav.md +4 -2
- package/docs/api/particles.md +8 -5
- package/docs/api/pickups.md +1 -1
- package/docs/api/post.md +3 -4
- package/docs/api/quests.md +4 -2
- package/docs/api/rays.md +2 -0
- package/docs/api/rigid.md +3 -2
- package/docs/api/saves.md +8 -7
- package/docs/api/stream.md +4 -5
- package/docs/api/terrain.md +3 -4
- package/docs/api/terrainDraw.md +1 -1
- package/docs/api/timeline.md +2 -2
- package/docs/api/tools.md +3 -1
- package/docs/api/triggers.md +2 -0
- package/docs/api/vegetationDraw.md +1 -1
- package/docs/api/water.md +6 -2
- package/docs/api/weather.md +19 -4
- package/docs/api/weatherDraw.md +1 -1
- package/docs/api/world.md +1 -1
- package/docs/recipes/01-new-game.md +3 -3
- package/docs/recipes/02-npc.md +3 -4
- package/docs/recipes/03-items-pickups.md +11 -11
- package/docs/recipes/04-quests.md +2 -0
- package/docs/recipes/05-props-bake.md +9 -9
- package/docs/recipes/06-clock-events.md +6 -6
- package/docs/recipes/07-flow-scenes-pause.md +18 -15
- package/docs/recipes/08-weather-sky.md +5 -5
- package/docs/recipes/09-materials-lights-models.md +3 -3
- package/docs/recipes/10-blender-level.md +16 -4
- package/docs/recipes/11-vehicles.md +18 -6
- package/docs/recipes/12-navmesh.md +52 -8
- package/docs/recipes/13-animation.md +34 -8
- package/docs/recipes/14-custom-module.md +20 -8
- package/docs/recipes/15-perf.md +4 -4
- package/docs/recipes/16-dev-bridge.md +9 -8
- package/docs/recipes/18-world.md +13 -5
- package/docs/recipes/19-shooting-picking.md +3 -2
- package/docs/recipes/20-content-actions-save.md +26 -6
- package/docs/recipes/21-camera.md +17 -23
- package/docs/recipes/22-shop-gold.md +21 -12
- package/docs/recipes/23-ops.md +19 -15
- package/docs/recipes/26-ui-parts.md +3 -2
- package/docs/recipes/27-net.md +3 -3
- package/docs/recipes/28-terrain.md +3 -3
- package/docs/recipes/29-grid.md +1 -1
- package/docs/recipes/30-templates.md +10 -13
- package/docs/recipes/31-audio.md +2 -2
- package/docs/recipes/32-action-game.md +6 -5
- package/docs/recipes/33-stream-sources.md +4 -1
- package/docs/recipes/34-environment-lighting.md +7 -8
- package/docs/recipes/35-picture-from-reference.md +13 -9
- package/docs/recipes/36-ground-plants.md +3 -2
- package/docs/recipes/40-scene.md +11 -10
- package/docs/recipes/43-shader-patches-surfaces.md +7 -7
- package/docs/recipes/44-water-weather-hooks.md +9 -8
- package/docs/recipes/45-cascaded-shadows.md +3 -4
- package/docs/recipes/46-blender-art-route.md +3 -3
- package/docs/recipes/49-game-module-contract.md +2 -1
- package/docs/recipes/50-own-renderer.md +1 -0
- package/docs/recipes/51-capability-catalog.md +14 -8
- package/docs/recipes/52-around-the-picture.md +4 -4
- package/docs/recipes/55-mods-time-wrap.md +5 -4
- package/docs/recipes/56-webgpu-tsl.md +12 -1
- package/docs/recipes/57-control.md +30 -10
- package/docs/recipes/58-input.md +13 -0
- package/docs/recipes/60-judge-the-picture.md +1 -1
- package/docs/recipes/62-picture-code.md +106 -48
- package/docs/recipes/63-third-person-start.md +2 -1
- package/docs/recipes/64-particles.md +53 -4
- package/docs/recipes/65-swarm.md +2 -2
- package/docs/recipes/66-common-tasks.md +313 -0
- package/examples/action/.threedog/content-index.json +1 -1
- package/examples/action/GAMEMAP.md +3 -3
- package/examples/perf-island/.threedog/content-index.json +1 -1
- package/examples/perf-island/GAMEMAP.md +3 -3
- package/examples/perf-island/main.js +2 -2
- package/examples/plain-picture/GAMEMAP.md +3 -1
- package/examples/rc2d/GAMEMAP.md +1 -1
- package/examples/rc2d/rc2d.js +1 -1
- package/examples/styletest/.threedog/content-index.json +1 -1
- package/examples/styletest/GAMEMAP.md +3 -3
- package/examples/webgpu-tsl/GAMEMAP.md +1 -1
- package/examples/world/.threedog/content-index.json +1 -1
- package/examples/world/GAMEMAP.md +5 -4
- package/examples/world/main.js +3 -3
- package/index.d.ts +8 -5
- package/index.js +22 -1
- package/llms.txt +2 -1
- package/modules/ambience/Ambience.js +5 -3
- package/modules/calendar/index.js +7 -5
- package/modules/camera/FirstPersonCamera.js +3 -3
- package/modules/camera/FixedCamera.js +34 -20
- package/modules/camera/FollowCamera.js +20 -11
- package/modules/camera/RtsCamera.js +1 -0
- package/modules/camera/common.js +33 -22
- package/modules/camera/pixelSnap.js +62 -0
- package/modules/characters/Character.js +136 -43
- package/modules/characters/NPC.js +60 -28
- package/modules/characters/assetModel.js +2 -2
- package/modules/characters/astar.js +47 -28
- package/modules/characters/character.contract.js +21 -0
- package/modules/characters/contract.js +17 -3
- package/modules/characters/index.js +94 -34
- package/modules/characters/moveHelpers.js +80 -15
- package/modules/characters/mover.contract.js +14 -0
- package/modules/characters/movers/common.js +6 -4
- package/modules/characters/movers/rapier.js +12 -5
- package/modules/characters/movers/simple.js +25 -12
- package/modules/characters/movers/support.js +18 -7
- package/modules/characters/npcAvoid.js +15 -12
- package/modules/characters/npcSim.js +75 -25
- package/modules/characters/npcView.js +13 -6
- package/modules/characters/pathAsync.js +74 -24
- package/modules/characters/pathJob.js +38 -21
- package/modules/characters/player.js +59 -47
- package/modules/characters/seat.js +56 -0
- package/modules/characters/vehicle.contract.js +8 -5
- package/modules/characters/walkTo.js +31 -18
- package/modules/collision/Collision.js +44 -26
- package/modules/collision/contract.js +2 -2
- package/modules/collision/grid.js +6 -1
- package/modules/content/explain.js +1 -1
- package/modules/content/fields.js +3 -3
- package/modules/content/ids.js +3 -1
- package/modules/content/index.js +83 -27
- package/modules/content/loader.js +110 -130
- package/modules/content/markers.js +1 -1
- package/modules/content/models.js +48 -14
- package/modules/content/packs.js +2 -1
- package/modules/content/places.js +5 -4
- package/modules/content/props.js +8 -6
- package/modules/content/rules.js +29 -47
- package/modules/content/templates.js +66 -35
- package/modules/content/words.js +19 -6
- package/modules/debug/DebugPanel.js +4 -2
- package/modules/flow/contract.js +1 -0
- package/modules/flow/index.js +24 -14
- package/modules/gameplay/Container.js +5 -2
- package/modules/gameplay/Containers.js +9 -1
- package/modules/gameplay/Dialogue.js +82 -25
- package/modules/gameplay/Interactions.js +61 -32
- package/modules/gameplay/Inventory.js +56 -13
- package/modules/gameplay/Quests.js +133 -24
- package/modules/gameplay/boxOps.js +7 -0
- package/modules/gameplay/dialogueBubbles.js +14 -9
- package/modules/gameplay/inventoryOps.js +11 -7
- package/modules/gameplay/pickups.js +43 -16
- package/modules/gameplay/questLog.js +4 -2
- package/modules/gameplay/questSave.js +6 -1
- package/modules/gameplay/quests.contract.js +1 -1
- package/modules/grid/Grid.js +1 -1
- package/modules/grid/Layer.js +8 -4
- package/modules/grid/index.js +21 -15
- package/modules/grid/ops.js +43 -46
- package/modules/grid/tiles.js +1 -1
- package/modules/hlod/index.js +6 -3
- package/modules/hud/index.js +6 -4
- package/modules/hud/parts.js +4 -4
- package/modules/level/contract.js +2 -2
- package/modules/level/heightfield.js +17 -21
- package/modules/level/index.js +83 -26
- package/modules/level/load.js +165 -48
- package/modules/level/nodes.js +34 -16
- package/modules/lights/index.js +6 -3
- package/modules/lights/sources.js +3 -1
- package/modules/music/Music.js +4 -2
- package/modules/nav/contract.js +7 -5
- package/modules/nav/index.js +174 -44
- package/modules/nav/navPath.js +45 -10
- package/modules/net/index.js +1 -1
- package/modules/particles/Particles.js +85 -22
- package/modules/particles/effect.js +80 -28
- package/modules/particles/shaders.js +17 -0
- package/modules/photo/Photo.js +12 -10
- package/modules/rigid/RigidBodies.js +200 -57
- package/modules/rigid/Vehicles.js +15 -5
- package/modules/rigid/collider3d.contract.js +26 -0
- package/modules/rigid/ragdoll.js +55 -44
- package/modules/rigid/ragdollBones.js +83 -33
- package/modules/rigid/refresh.js +10 -0
- package/modules/rigid/sync.js +46 -15
- package/modules/settings/Settings.js +84 -37
- package/modules/settings/apply.js +15 -8
- package/modules/settings/sections.js +7 -4
- package/modules/settings/strings.js +2 -2
- package/modules/sky/skyModule.js +3 -1
- package/modules/swarm/Swarm.js +16 -6
- package/modules/terrain/ChunkManager.js +10 -16
- package/modules/terrain/index.js +17 -11
- package/modules/terrain/terrainEdit.js +13 -2
- package/modules/terrain/terrainGen.js +5 -3
- package/modules/terrain/terrainShape.js +1 -1
- package/modules/timeline/Timeline.js +58 -26
- package/modules/timeline/Tween.js +19 -13
- package/modules/timeline/shots.js +19 -10
- package/modules/triggers/index.js +87 -20
- package/modules/vegetation/Vegetation.js +4 -3
- package/modules/vegetation/draw.js +16 -0
- package/modules/vegetation/groundChunks.js +10 -7
- package/modules/vegetation/index.js +30 -7
- package/modules/water/Water.js +6 -2
- package/modules/weather/Weather.js +16 -15
- package/modules/weather/weatherDraw.js +7 -6
- package/modules/world/World.js +20 -7
- package/modules/world/explain.js +1 -1
- package/modules/world/index.js +13 -6
- package/modules/world/persist.js +23 -8
- package/modules/world/pos.js +4 -4
- package/modules/world/records.js +13 -7
- package/modules/world/sim.js +74 -23
- package/modules/world/simFar.js +2 -2
- package/modules/world/state.js +31 -6
- package/modules/world/thingSource.js +16 -3
- package/modules/world/views.js +19 -8
- package/package.json +7 -1
- package/render/PostFX.js +47 -29
- package/render/backend.js +5 -1
- package/render/fx/particles.tsl.js +8 -6
- package/render/fx/postWebGPU.tsl.js +3 -3
- package/render/layers.js +1 -1
- package/render/lightmap.js +23 -3
- package/render/occlusion.js +43 -12
- package/render/patches.js +2 -2
- package/render/postWebGL.js +4 -3
- package/render/probes.js +25 -0
- package/render/shadows.js +2 -2
- package/types/core.d.ts +142 -55
- package/types/gameplay.d.ts +92 -40
- package/types/generated/core/base/I18n.d.ts +2 -2
- package/types/generated/core/input/Input.d.ts +16 -2
- package/types/generated/core/input/inputSeat.d.ts +26 -0
- package/types/generated/core/inspect/DebugOverrides.d.ts +15 -1
- package/types/generated/core/ops/Ops.d.ts +2 -1
- package/types/generated/core/ops/control.d.ts +12 -2
- package/types/generated/core/saves/Saves.d.ts +26 -6
- package/types/generated/core/saves/session.d.ts +1 -1
- package/types/generated/core/scene/Views.d.ts +1 -1
- package/types/generated/core/stream/Stream.d.ts +6 -4
- package/types/generated/modules/grid/Grid.d.ts +1 -0
- package/types/generated/modules/particles/Particles.d.ts +4 -3
- package/types/generated/modules/timeline/Timeline.d.ts +5 -4
- package/types/generated/modules/weather/Weather.d.ts +1 -7
- package/types/generated/modules/world/World.d.ts +6 -4
- package/types/generated/render/PostFX.d.ts +6 -14
- package/types/nav.d.ts +36 -4
- package/types/physics.d.ts +55 -7
- package/types/pixel.d.ts +75 -0
- package/types/world.d.ts +14 -8
- package/ui/Parts.js +9 -2
- package/ui/theme.js +2 -2
- package/core/scene/engineCamera.js +0 -32
- package/modules/camera/projection.js +0 -21
package/modules/level/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Level module: load a hand-built scene (glTF/GLB from Blender, or THREE objects assembled in code) and make it playable:
|
|
2
|
-
// - ground: walkable meshes become ground height (provides engine.surface/slope/groundType;
|
|
2
|
+
// - ground: walkable meshes become ground height (provides engine.surface/slope/groundType; holds the exclusive 'ground' slot, as terrain does: installing both is refused at use())
|
|
3
3
|
// - collision: meshes named col_* (or userData.collider) become walls the character cannot cross; col_ meshes themselves are hidden
|
|
4
4
|
// - markers: objects named spawn, spawn_*, npc_*, trigger_*, marker_* (usually Blender empties) have their positions recorded
|
|
5
5
|
//
|
|
@@ -40,10 +40,12 @@
|
|
|
40
40
|
// markers { name: { name, position, quaternion, scale, userData, object } } (world space; position is a Vector3, object is the original)
|
|
41
41
|
// spawn spawn position (spawn, else the first spawn_*, else the origin)
|
|
42
42
|
// marker(name) / markersWith(prefix) get one / get a group (e.g. markersWith('npc_'))
|
|
43
|
-
// ready
|
|
43
|
+
// ready the level's state is committed and nothing is loading (boolean). After unload() it is true with no level, since nothing is loading: root says whether a level stands
|
|
44
|
+
// playable every module and required nearby build settled after the commit too (boolean)
|
|
44
45
|
// heightfield heightfield data (for debugging)
|
|
45
46
|
// staticTrimeshes() walkable-mesh triangles (world space), for building Rapier static colliders
|
|
46
|
-
// load(path/asset name or THREE object) switch level
|
|
47
|
+
// load(path/asset name or THREE object) switch level: the new one is built first and replaces the old one only on success; a failure keeps the old level
|
|
48
|
+
// and rejects. Returns a Promise
|
|
47
49
|
// unload() unload (also removes everything registered via track)
|
|
48
50
|
// track(handle) register things that belong to the current level and need no keeping (handles with remove(): trigger zones...); auto-removed on level switch /
|
|
49
51
|
// unload.
|
|
@@ -53,30 +55,35 @@
|
|
|
53
55
|
// cameraOcclusion).
|
|
54
56
|
// Save: the current level (path or asset name) goes into the save; on load, switch back first and wait for it before loading the rest (save key level comes
|
|
55
57
|
// first, load returns a Promise);
|
|
56
|
-
//
|
|
57
|
-
// and use level({ url: name }).
|
|
58
|
-
//
|
|
58
|
+
// a scene built in code has no file a stored save could name: it saves a token only this page can read back ({ scene: n }), so a rollback and a new game return to it; a token from another page
|
|
59
|
+
// stays on the level that stands. To load across pages, register the builder in the assets manifest and use level({ url: name }).
|
|
60
|
+
// Events: level:loading { source }, then level:failed { source, error } or level:loaded { root, markers, spawn } (state committed), then level:playable { root }.
|
|
61
|
+
// level:unloaded { source } says a level left: on unload(), and on a switch just before the new one commits. What was built from the level goes on it
|
|
59
62
|
// Picture: the level module draws nothing itself, the scene looks entirely like your model, so it does not read the style.
|
|
60
63
|
import * as THREE from 'three';
|
|
61
64
|
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
62
65
|
import { levelNodes } from './nodes.js';
|
|
63
66
|
import { worldTrimeshes } from './geometry.js';
|
|
64
|
-
import {
|
|
67
|
+
import { follows } from '../../core/base/follows.js';
|
|
68
|
+
import { loadLevel, dropLoad, dropAsks, finishLoad, readScene, adoptScene, addColliders, levelSection } from './load.js';
|
|
65
69
|
import { warnOnce } from '../../core/base/validate.js';
|
|
66
70
|
import { coded } from '../../core/base/errorHints.js';
|
|
71
|
+
import { checkSaveFile } from '../../core/base/saveValues.js';
|
|
72
|
+
import { whenTurnFree } from '../../core/saves/stateReplace.js';
|
|
67
73
|
|
|
68
74
|
const STRINGS = {
|
|
69
75
|
'zh-CN': { 'module.level': '关卡', 'level.loadFailed': '关卡加载失败:{url}', 'level.noScene': '关卡模块需要 url 或 scene 参数' },
|
|
70
76
|
en: { 'module.level': 'level', 'level.loadFailed': 'Failed to load level: {url}', 'level.noScene': 'The level module needs a url or scene option' },
|
|
71
77
|
};
|
|
72
78
|
|
|
73
|
-
const LEVEL_PARAMS = keysSchema(['url', 'scene', 'walkable', 'colliders', 'cellSize', 'outside', 'waterLevel', 'colliderPad', 'colliderHeight', 'sharpEdge', 'hideMarkers', 'addToScene']);
|
|
79
|
+
const LEVEL_PARAMS = keysSchema(['url', 'scene', 'walkable', 'colliders', 'cellSize', 'outside', 'waterLevel', 'colliderPad', 'colliderHeight', 'sharpEdge', 'hideMarkers', 'addToScene', 'part']);
|
|
74
80
|
|
|
75
81
|
/** 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
82
|
function levelApi(st) {
|
|
77
83
|
const api = {
|
|
78
84
|
root: null, markers: {}, spawn: new THREE.Vector3(), heightfield: null,
|
|
79
85
|
get ready() { return st.done; },
|
|
86
|
+
get playable() { return st.done && st.playable; },
|
|
80
87
|
/** Register things belonging to the current level ({ remove() } handles); auto-removed on level switch / unload; returns the same handle */
|
|
81
88
|
track(h) { if (h) st.removers.push(h); return h; },
|
|
82
89
|
marker: name => api.markers[name] || null,
|
|
@@ -89,55 +96,103 @@ function levelApi(st) {
|
|
|
89
96
|
},
|
|
90
97
|
/** Unload the level: remove from scene, delete colliders, ground reverts to the outside height */
|
|
91
98
|
unload() {
|
|
92
|
-
|
|
99
|
+
const source = api.root ? st.current : undefined; // undefined: no level stood here, nobody needs telling
|
|
100
|
+
for (const h of [...st.removers.splice(0), ...st.walls.splice(0)]) h.remove();
|
|
93
101
|
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.
|
|
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)
|
|
102
|
+
st.walkMeshes.length = 0; st.hf = api.heightfield = null; st.colliders = null; st.wallsIn.reset(); st.trimeshCache = null; api.markers = {}; api.root = null; st.current = null; st.version++; dropAsks(st); st.playable = false;
|
|
103
|
+
st.done = true; st.playable = false; // nothing is loading now: a file still on its way is dropped, and no finishLoad would ever come for it (load sets it back)
|
|
104
|
+
if (source !== undefined) {
|
|
105
|
+
// a level that left takes its zone and spawn with it: the next scene's nodes must not be placed in a zone that is gone
|
|
106
|
+
api.spawn = new THREE.Vector3();
|
|
107
|
+
if (st.e.world) st.e.world.zone = null;
|
|
108
|
+
st.e.events.emit('level:unloaded', { source });
|
|
109
|
+
}
|
|
96
110
|
},
|
|
97
|
-
/** Switch level:
|
|
98
|
-
async load(urlOrScene) { return loadLevel(st, urlOrScene, false); },
|
|
111
|
+
/** Switch level (glTF path/asset name, or THREE object): the new one is built first, a failure keeps the old one and rejects (load.js). During a save load it waits for the load to end, so the level a load answered true for is the one that stands */
|
|
112
|
+
async load(urlOrScene, { turn } = {}) { return whenTurnFree(st.e, () => loadLevel(st, urlOrScene, false), turn); },
|
|
99
113
|
};
|
|
100
114
|
return api;
|
|
101
115
|
}
|
|
102
116
|
|
|
117
|
+
const OPENING = '(opening scene)'; // no file or asset can be named this
|
|
118
|
+
|
|
119
|
+
/** What the save holds for a level built in code. Weak: a token in a stored save must not keep every level this page ever built in memory */
|
|
120
|
+
function sceneToken(st, root) {
|
|
121
|
+
const ids = st.sceneIds, id = ids.get(root) ?? ids.set(root, st.scenes.push(new WeakRef(root)) - 1).get(root);
|
|
122
|
+
return { scene: id };
|
|
123
|
+
}
|
|
124
|
+
|
|
103
125
|
export function level(opts = {}) {
|
|
104
126
|
readParams('level', LEVEL_PARAMS, opts);
|
|
105
127
|
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;
|
|
128
|
+
colliderPad = 0, colliderHeight = 0, sharpEdge = 0.6, hideMarkers = true, addToScene = true, part = false } = opts;
|
|
107
129
|
const st = {
|
|
108
|
-
e: null, hf: null,
|
|
130
|
+
e: null, hf: null, playable: false,
|
|
131
|
+
_done: false, idle: [], // done: nothing is loading. idle: who waits for that (init); a setter, because a load ends in many ways: committed, failed, cancelled, overtaken, unloaded
|
|
132
|
+
get done() { return this._done; },
|
|
133
|
+
set done(v) { this._done = v; if (v) for (const go of this.idle.splice(0)) go(); },
|
|
134
|
+
version: 0, trimeshCache: null,
|
|
135
|
+
colliders: null, walls: [], wallsIn: follows(() => st.e.collision), // the level's collision meshes, the handles of their boxes, the module those boxes are in (load.js addColliders)
|
|
109
136
|
o: { walkable, colliders, cellSize, outside, colliderPad, colliderHeight, sharpEdge, hideMarkers, addToScene },
|
|
110
|
-
|
|
137
|
+
asks: [], // the loads under way, newest last: only the newest may commit, so a file that arrives after a newer load, a save or an unload is dropped
|
|
111
138
|
current: typeof url === 'string' && !scene ? url : null, // current level path/asset name (for saving)
|
|
112
139
|
walkMeshes: [], removers: [],
|
|
140
|
+
scenes: [], sceneIds: new WeakMap(), // levels built in code, by token (sceneToken)
|
|
113
141
|
nodeSet: null, // level nodes that become world records (nodes.js), created at install
|
|
142
|
+
taken: [], // record nodes of the adopted scene, placed by finishLoad
|
|
114
143
|
};
|
|
115
144
|
const api = st.api = levelApi(st);
|
|
145
|
+
st.section = levelSection(st);
|
|
116
146
|
|
|
117
147
|
return {
|
|
118
|
-
name: 'level', type: 'world', provides: ['level'], params: LEVEL_PARAMS, options: opts, label: 'Level', stage: 'ground',
|
|
119
|
-
|
|
148
|
+
name: 'level', skipKind: 'none', type: 'world', provides: part ? ['level'] : ['level', 'ground'], params: LEVEL_PARAMS, options: opts, label: 'Level', stage: 'ground', optional: ['world', 'content', 'collision'],
|
|
149
|
+
content: { level: st.section },
|
|
150
|
+
install(engine, ctx) {
|
|
151
|
+
ctx.feeds('collision', () => addColliders(st));
|
|
120
152
|
const e = st.e = engine;
|
|
121
|
-
st.nodeSet = levelNodes(e, () => api.root, () => st.current);
|
|
153
|
+
st.nodeSet = levelNodes(e, ctx, () => api.root, () => st.current);
|
|
122
154
|
e.i18n.add(STRINGS);
|
|
123
155
|
// ground API: before the heightfield is built, return the outside height
|
|
124
|
-
|
|
156
|
+
const ground = {
|
|
125
157
|
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',
|
|
158
|
+
slope: (x, z, refY) => st.hf ? st.hf.slope(x, z, refY) : 0,
|
|
159
|
+
type: (x, z, refY) => st.hf ? st.hf.type(x, z, refY) : 'default',
|
|
128
160
|
waterLevel,
|
|
129
161
|
staticTrimeshes: () => api.staticTrimeshes(),
|
|
130
162
|
staticVersion: () => st.version, // +1 on level change; the physics module rebuilds static colliders on it
|
|
131
|
-
}
|
|
132
|
-
if (
|
|
163
|
+
};
|
|
164
|
+
if (part) api.ground = ground; else ctx.ground.provide(ground); // part: compositeGround holds the slot and reads level.ground
|
|
165
|
+
// the level is the module's: scene, walls, rays and level.track handles leave with it (unuse, replaces, a failed init). Before the part: handles go newest first, so the
|
|
166
|
+
// part still reads the level as it leaves
|
|
167
|
+
ctx.track({ remove: () => api.unload() });
|
|
133
168
|
// on load, switch back to the saved level (code-built scenes have no name, record null, no switch)
|
|
134
169
|
// 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
|
|
135
170
|
// position etc.
|
|
136
|
-
|
|
171
|
+
// a save that names this level, or none, stays here: a load on its way would land on top of what the save put back
|
|
172
|
+
// The level a holder stood on comes back through the same part when a module takes over the key (Saves.register): a swap keeps the game where it was, and a pack's older
|
|
173
|
+
// level must not take it back (the section's once). A newcomer that says where to open keeps that promise; the same definition put back after a failed swap has none to make
|
|
174
|
+
// the opening scene has no name either, so it saves as OPENING: a new game goes back to it, and a save from before this mark takes it from the new-game state
|
|
175
|
+
const opens = !st.installed && (scene || url);
|
|
176
|
+
let installing = st.installed = true;
|
|
177
|
+
st.kept = false;
|
|
178
|
+
ctx.saves.register('level', { notAtomic: 'its load fetches and builds a level: not plain data, and not something a failed op can wait for', first: true, save: () => st.current ?? (!api.root ? null : api.root === scene ? OPENING : sceneToken(st, api.root)), load: (d, turn) => {
|
|
179
|
+
if (installing) { if (opens || typeof d !== 'string' || d === OPENING) return; st.kept = true; }
|
|
180
|
+
else if (d === OPENING) return api.root === scene ? dropLoad(st) : api.load(scene, { turn });
|
|
181
|
+
else if (d?.scene != null) { const root = st.scenes[d.scene]?.deref(); return !root || root === api.root ? dropLoad(st) : api.load(root, { turn }); }
|
|
182
|
+
else if (typeof d !== 'string' || d === st.current) return dropLoad(st);
|
|
183
|
+
checkSaveFile(e.assets, 'level', 'the level file', d);
|
|
184
|
+
return api.load(d, { turn });
|
|
185
|
+
} });
|
|
186
|
+
installing = false;
|
|
187
|
+
if (scene) adoptScene(st, readScene(st, scene));
|
|
137
188
|
return api;
|
|
138
189
|
},
|
|
139
190
|
critical: true, // the level file is the game's world: a failed load stops start() with the reason
|
|
140
191
|
async init(engine) {
|
|
192
|
+
// the options say where to open; what the game asked for before init (a pack's level, level.load) comes first and stays, and the url file is not fetched. Asking here
|
|
193
|
+
// would be the newer load and overtake it. If that load ends with no level (refused, failed), the options are the level after all
|
|
194
|
+
if (st.asks.length) await new Promise(res => st.idle.push(res));
|
|
195
|
+
if (api.root && api.root !== scene) return;
|
|
141
196
|
if (scene) return finishLoad(st);
|
|
142
197
|
if (url) return loadLevel(st, url, true);
|
|
143
198
|
warnOnce(coded('level-node', engine.t('level.noScene')));
|
|
@@ -147,8 +202,10 @@ export function level(opts = {}) {
|
|
|
147
202
|
// 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
|
|
148
203
|
describe() {
|
|
149
204
|
const s = api.spawn, r = v => Math.round(v * 10) / 10;
|
|
150
|
-
return { file: st.current, ready: st.done, markers: Object.keys(api.markers).length, walkMeshes: st.walkMeshes.length, removers: st.removers.length,
|
|
205
|
+
return { file: st.current, ready: st.done, playable: api.playable, markers: Object.keys(api.markers).length, walkMeshes: st.walkMeshes.length, removers: st.removers.length + st.walls.length,
|
|
151
206
|
spawn: { x: r(s.x), y: r(s.y), z: r(s.z) } };
|
|
152
207
|
},
|
|
153
208
|
};
|
|
154
209
|
}
|
|
210
|
+
|
|
211
|
+
export const handRolled = 'none: loading a map by hand is one model load that looks like any other';
|
package/modules/level/load.js
CHANGED
|
@@ -1,79 +1,196 @@
|
|
|
1
|
-
// Loading a level
|
|
1
|
+
// Loading a level is the module's content section (core/base/sections.js): prepare builds the new level beside the old one, commit swaps it in.
|
|
2
|
+
// st is the module's state (index.js). Nothing live changes before commit, so a level that cannot load leaves the old one playable:
|
|
3
|
+
// a save transaction rolls back around it, and the error reaches whoever asked.
|
|
4
|
+
// The moments: level:loading (asked for), then level:failed, or level:loaded (state committed: ground, markers, records) and later
|
|
5
|
+
// level:playable (every module and required nearby build settled). A load overtaken by a newer one, a save or an unload ends silently: the newest stands and reports. Only the level gives way: the rest of its pack goes in, on the level that won.
|
|
2
6
|
import * as THREE from 'three';
|
|
3
7
|
import { buildHeightfield } from './heightfield.js';
|
|
4
8
|
import { colliderBoxes } from './geometry.js';
|
|
5
9
|
import { sortNodes, groundTypeOf, readMarkers, chainHas, chainValue, COL_RE } from './meshes.js';
|
|
6
10
|
import { toast } from '../../ui/UI.js';
|
|
7
|
-
import {
|
|
11
|
+
import { runSections, DROPPED } from '../../core/base/sections.js';
|
|
12
|
+
import { whenSettled } from '../../core/engine/headlessLoad.js';
|
|
13
|
+
import { codedError, coded } from '../../core/base/errorHints.js';
|
|
14
|
+
import { warnOnce } from '../../core/base/validate.js';
|
|
8
15
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
16
|
+
const isScene = v => !!v?.isObject3D;
|
|
17
|
+
const nameOf = source => typeof source === 'string' ? source : null; // code-built scenes have no name: saves record null, no zoning
|
|
18
|
+
|
|
19
|
+
/** The level section: a file or asset name, or a THREE object, becomes the level; plain { markers } data is the tools' file for a code-built level and loads nothing */
|
|
20
|
+
export const levelSection = st => ({
|
|
21
|
+
desc: 'level markers (spawn point, NPCs, trigger zones) of a code-built level: for the tools (content checks, GAMEMAP); load() reads no markers from it. A file or asset name here switches level, as engine.level.load does',
|
|
22
|
+
validate(data) {
|
|
23
|
+
if (typeof data === 'string' || isScene(data) || (data && typeof data === 'object' && !Array.isArray(data))) return;
|
|
24
|
+
throw codedError('content-misuse', `level: expected a file or asset name, a THREE object, or { markers }, got ${JSON.stringify(data)}`);
|
|
25
|
+
},
|
|
26
|
+
prepare: (data, c) => typeof data === 'string' || isScene(data) ? prepareLevel(st, data, c) : null,
|
|
27
|
+
// an overtaken load's pack goes in on the level that won, so it waits to see which. Not for a pack queued behind this one: that one waits for this
|
|
28
|
+
waitFor: p => p && !isNewest(st, p) && st.asks.length && !st.asks.at(-1).behind ? new Promise(res => st.idle.push(res)) : undefined,
|
|
29
|
+
commit: (p, c) => p ? commitLevel(st, p, c) : undefined,
|
|
30
|
+
cancel: p => { if (p) letGo(st, p.ask ?? p); },
|
|
31
|
+
undo: p => p ? backToLevel(st) : undefined,
|
|
32
|
+
get once() { return st.kept; }, // the level came back through the save part: the pack's is older than where the game stood
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* The undo of a level swap: the old root left the scene and its memory may be gone, so the old level loads again, by the same road as any level (a failure there is told
|
|
37
|
+
* as a level failure, and reaches whoever asked for the pack: sections.js). Records of its nodes are still in the world. A pack with no level before it ends with no level
|
|
38
|
+
*/
|
|
39
|
+
function backToLevel(st) {
|
|
40
|
+
const { e, api } = st, root = api.root, source = st.current ?? root, zone = e.world?.zone;
|
|
41
|
+
return () => {
|
|
42
|
+
if (api.root === root) return;
|
|
43
|
+
api.unload();
|
|
44
|
+
if (e.world) e.world.zone = zone;
|
|
45
|
+
if (source) return loadLevel(st, source, false);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** hard: the first load from init() stops start() on its own (the module is critical), so no toast over the loading screen.
|
|
50
|
+
* async: a scene with nothing to wait for commits on this tick, and any failure still arrives as a rejection */
|
|
51
|
+
export async function loadLevel(st, source, hard) {
|
|
52
|
+
await runSections([{ s: st.section, data: source }], { hard });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The load is over: a failure is told once, here, and passed on. It ends every load before it too: they were already overtaken */
|
|
56
|
+
function failLevel(st, source, err, { hard }) {
|
|
57
|
+
const { e } = st;
|
|
58
|
+
dropAsks(st); st.done = true; // nothing is loading any more
|
|
59
|
+
if (!st.api.root) st.current = null; // a first load that failed: the save must not name a level that never loaded
|
|
60
|
+
e.events.emit('level:failed', { source: nameOf(source), error: err });
|
|
61
|
+
if (!hard) toast(e, e.t('level.loadFailed', { url: nameOf(source) ?? 'scene' }), { kind: 'error' });
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Is this load the last one asked for? Only that one may commit */
|
|
66
|
+
const isNewest = (st, ask) => st.asks.at(-1) === ask && !st.e.disposed;
|
|
67
|
+
|
|
68
|
+
/** A load that goes no further. e.remove releases a file, so assets.trim can free it; a code-built scene stays the caller's, whole */
|
|
69
|
+
function letGo(st, ask) {
|
|
70
|
+
const i = st.asks.indexOf(ask), { root, source, taken = [] } = ask;
|
|
71
|
+
if (i >= 0) st.asks.splice(i, 1);
|
|
72
|
+
ask.leave.abort(); // a file still on its way: this load stops waiting for it, and with no other waiter the download ends
|
|
73
|
+
if (!st.asks.length) st.done = true;
|
|
74
|
+
if (!root) return;
|
|
75
|
+
ask.root = null;
|
|
76
|
+
if (typeof source === 'string') st.e.remove(root); else st.nodeSet.giveBack(taken);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A save that stays on this level: what is on its way would land on top of it. The first load is the level itself, so it goes on */
|
|
80
|
+
export function dropLoad(st) { if (st.api.root) { dropAsks(st); st.done = true; } }
|
|
81
|
+
|
|
82
|
+
/** No load under way counts any more: each stops waiting for its file (its prepare then ends as an overtaken load does) */
|
|
83
|
+
export function dropAsks(st) { for (const ask of st.asks.splice(0)) ask.leave.abort(); }
|
|
84
|
+
|
|
85
|
+
/** Everything that can fail, away from the live game. Returns what commitLevel needs, or for a file a Promise of it */
|
|
86
|
+
function prepareLevel(st, source, c) {
|
|
87
|
+
const { e } = st, ask = { source, behind: !!c.after, leave: new AbortController() }; // grows into the prepared level; behind: a content load queued after an earlier one; leave: stop waiting for the file
|
|
88
|
+
st.asks.push(ask);
|
|
89
|
+
st.done = false;
|
|
90
|
+
e.events.emit('level:loading', { source: nameOf(source) });
|
|
91
|
+
const build = root => {
|
|
92
|
+
Object.assign(ask, readScene(st, root));
|
|
93
|
+
st.nodeSet.check(ask.taken, nameOf(source), root);
|
|
94
|
+
return ask;
|
|
95
|
+
};
|
|
96
|
+
const fail = err => {
|
|
97
|
+
const newest = isNewest(st, ask);
|
|
98
|
+
letGo(st, ask);
|
|
99
|
+
return newest ? failLevel(st, source, err, c) : ask;
|
|
100
|
+
};
|
|
101
|
+
if (isScene(source)) { ask.root = source; try { return build(source); } catch (err) { return fail(err); } }
|
|
102
|
+
return Object.assign(new Promise(res => res(e.assets.wholeModel(source, ask.leave.signal))).then(root => {
|
|
103
|
+
ask.root = root;
|
|
104
|
+
// a newer load / unload / save came while this file loaded (two doors in a row, a save loaded mid-switch): that one owns the level now
|
|
105
|
+
if (!isNewest(st, ask)) { letGo(st, ask); return ask; }
|
|
106
|
+
return build(root);
|
|
107
|
+
}).catch(fail), { ask }); // cancel may come while the file is on its way
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** The swap, or giving way to a newer load: the newest level wins, the pack's other sections do not depend on that. Prepare checked what it can, so a throw here is a bug: told like any failure */
|
|
111
|
+
function commitLevel(st, p, c) {
|
|
12
112
|
const { e, api } = st;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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);
|
|
113
|
+
if (!isNewest(st, p)) { letGo(st, p); return DROPPED; }
|
|
114
|
+
try {
|
|
115
|
+
api.unload();
|
|
116
|
+
st.done = false;
|
|
117
|
+
e.commits.invalidate(); // background results requested for the old level must not land on the new one
|
|
118
|
+
// world-data zone = level name: other levels' NPCs etc. only lose models, data stays and is rebuilt on return
|
|
119
|
+
if (e.world) e.world.zone = nameOf(p.source);
|
|
120
|
+
st.current = nameOf(p.source); // before the records are placed: level nodes read it as their zone
|
|
121
|
+
adoptScene(st, p);
|
|
122
|
+
finishLoad(st);
|
|
123
|
+
} catch (err) { failLevel(st, p.source, err, c); }
|
|
35
124
|
}
|
|
36
125
|
|
|
37
126
|
export function finishLoad(st) {
|
|
38
127
|
const { e, api } = st;
|
|
39
128
|
addColliders(st); // if the collision module is installed after level, add colliders here
|
|
40
129
|
// 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);
|
|
130
|
+
e.ops.allow(() => st.nodeSet.place(st.taken.splice(0)));
|
|
42
131
|
st.done = true;
|
|
43
132
|
e.assets.trim(); // old level's models have no copies in use: return GPU memory
|
|
44
133
|
e.events.emit('level:loaded', { root: api.root, markers: api.markers, spawn: api.spawn });
|
|
134
|
+
// playable comes later than committed: nav, nearby models and physics start building on level:loaded. This level leaving ends the wait, a load that may not come to anything does not
|
|
135
|
+
const version = st.version;
|
|
136
|
+
st.playable = false;
|
|
137
|
+
whenSettled(e, () => version === st.version).then(ok => {
|
|
138
|
+
if (!ok) return;
|
|
139
|
+
st.playable = true;
|
|
140
|
+
e.events.emit('level:playable', { root: api.root });
|
|
141
|
+
}, err => warnOnce(coded('module-not-ready', `level:playable did not come: ${err.message}`)));
|
|
45
142
|
}
|
|
46
143
|
|
|
47
|
-
/**
|
|
48
|
-
export function
|
|
49
|
-
const { e,
|
|
50
|
-
api.root = root;
|
|
144
|
+
/** Read a scene without touching the game: classify meshes, build the heightfield, read markers */
|
|
145
|
+
export function readScene(st, root) {
|
|
146
|
+
const { e, o } = st;
|
|
51
147
|
root.userData.budgetTag ??= 'level';
|
|
52
|
-
if (o.addToScene && !root.parent) e.add(root, { owner: 'level' });
|
|
53
148
|
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
|
|
149
|
+
const taken = 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
150
|
const { meshes, cols, markerObjs, walk, walkSet } = sortNodes(root, o);
|
|
56
|
-
st.walkMeshes.push(...walk);
|
|
57
|
-
// heightfield
|
|
58
151
|
const t0 = performance.now(); // measure
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const markers =
|
|
152
|
+
const hf = buildHeightfield(walk, { cellSize: o.cellSize, outside: o.outside, typeOf: groundTypeOf(root), sharpEdge: o.sharpEdge, stepUp: e.movement.stepHeight, layerGap: e.movement.height });
|
|
153
|
+
const buildMs = performance.now() - t0; // measure: heightfield build time (shown in the debug panel)
|
|
154
|
+
const markers = readMarkers(markerObjs, o.hideMarkers);
|
|
62
155
|
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
156
|
// 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
157
|
// 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
158
|
const blocks = m => chainValue(m, root, 'occluder') !== false, shownHere = m => !chainHas(m, root, p => !p.visible);
|
|
68
|
-
|
|
159
|
+
return {
|
|
160
|
+
root, taken, walk, hf, buildMs, markers,
|
|
161
|
+
spawn: sp ? sp.position.clone() : new THREE.Vector3(0, hf.height(0, 0), 0),
|
|
162
|
+
colliders: o.colliders === false ? cols.filter(m => COL_RE.test(m.name) || chainValue(m, root, 'collider')) : cols,
|
|
163
|
+
occluders: [...cols.filter(blocks), ...meshes.filter(m => !walkSet.has(m) && shownHere(m) && blocks(m))],
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A read scene becomes the level. Colliders wait for the collision module (may not be installed at install time) */
|
|
168
|
+
export function adoptScene(st, p) {
|
|
169
|
+
const { e, api, o } = st;
|
|
170
|
+
api.root = p.root;
|
|
171
|
+
if (o.addToScene && !p.root.parent) e.add(p.root, { owner: 'level' });
|
|
172
|
+
st.taken = p.taken;
|
|
173
|
+
st.walkMeshes.push(...p.walk);
|
|
174
|
+
st.hf = api.heightfield = p.hf; api.buildMs = p.buildMs;
|
|
175
|
+
api.markers = p.markers; api.spawn = p.spawn;
|
|
176
|
+
st.colliders = p.colliders;
|
|
177
|
+
st.removers.push(e.rays.addStatic(p.root), e.rays.addStatic(p.occluders, { occluder: true }));
|
|
69
178
|
st.trimeshCache = null; st.version++;
|
|
70
179
|
addColliders(st);
|
|
71
180
|
}
|
|
72
181
|
|
|
73
|
-
/**
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
182
|
+
/**
|
|
183
|
+
* Turn collision meshes into rotated boxes in the collision module (best-fit box by the mesh's world orientation). A level load and a change of collision module ask (ctx.feeds):
|
|
184
|
+
* it acts only when the module holding them is not the one installed now, so one that came late, came back or replaced the old one gets the walls, and the old handles, gone
|
|
185
|
+
* with their module, are dropped. All or none: a module that refuses a box keeps none of them, and its use() fails on that error
|
|
186
|
+
*/
|
|
187
|
+
export function addColliders(st) {
|
|
188
|
+
if (!st.colliders) return;
|
|
189
|
+
st.wallsIn.sync(() => {
|
|
190
|
+
const ph = st.e.collision, fresh = [];
|
|
191
|
+
if (ph) try {
|
|
192
|
+
for (const b of colliderBoxes(st.colliders, { colliderHeight: st.o.colliderHeight, colliderPad: st.o.colliderPad })) fresh.push(ph.addBox(b.at, b.opts));
|
|
193
|
+
} catch (err) { for (const h of fresh) h.remove?.(); throw err; }
|
|
194
|
+
st.walls.length = 0; st.walls.push(...fresh);
|
|
195
|
+
});
|
|
79
196
|
}
|
package/modules/level/nodes.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
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
|
-
// and are placed as authored records `<level>/<node>` once the level is processed. Created per level module by levelNodes(e, getRoot, getCurrent).
|
|
2
|
+
// and are placed as authored records `<level>/<node>` once the level is processed. Created per level module by levelNodes(e, ctx, getRoot, getCurrent).
|
|
3
3
|
import * as THREE from 'three';
|
|
4
4
|
import { warnOnce, fmt } from '../../core/base/validate.js';
|
|
5
5
|
import { codedError } from '../../core/base/errorHints.js';
|
|
@@ -13,16 +13,17 @@ const STRINGS_DEV = {
|
|
|
13
13
|
|
|
14
14
|
export const MARKER_RE = /^(spawn|spawn_.*|npc_.*|trigger_.*|marker_.*)$/i;
|
|
15
15
|
|
|
16
|
-
/** @param {any} e engine @param {() => any} getRoot current level root @param {() => string | null} getCurrent current level path / name */
|
|
17
|
-
export function levelNodes(e, getRoot, getCurrent) {
|
|
18
|
-
const
|
|
16
|
+
/** @param {any} e engine @param {any} ctx the level module's ctx @param {() => any} getRoot current level root @param {() => string | null} getCurrent current level path / name */
|
|
17
|
+
export function levelNodes(e, ctx, getRoot, getCurrent) {
|
|
18
|
+
const nodes = new Map(); // record id -> node object (levelNode records)
|
|
19
|
+
const homes = new WeakMap(); // taken node -> the parent it left
|
|
19
20
|
let presented = null;
|
|
20
21
|
/**
|
|
21
22
|
* Nodes tagged with the custom property template or id (Blender custom property -> glTF extras -> userData): taken out of the level root, world transform
|
|
22
|
-
* kept
|
|
23
|
+
* kept. Returns them: the level being prepared holds its own list, the live one is untouched
|
|
23
24
|
*/
|
|
24
25
|
function take(root) {
|
|
25
|
-
taken
|
|
26
|
+
const taken = [];
|
|
26
27
|
const walk = o => {
|
|
27
28
|
for (const c of [...o.children]) {
|
|
28
29
|
if ((c.userData?.template == null && c.userData?.id == null) || MARKER_RE.test(c.name)) { walk(c); continue; }
|
|
@@ -31,26 +32,43 @@ export function levelNodes(e, getRoot, getCurrent) {
|
|
|
31
32
|
if (!e.world) { warnOnce(STRINGS_DEV.noWorld); continue; }
|
|
32
33
|
const p = new THREE.Vector3(), q = new THREE.Quaternion(), s = new THREE.Vector3();
|
|
33
34
|
c.matrixWorld.decompose(p, q, s);
|
|
35
|
+
homes.set(c, c.parent);
|
|
34
36
|
c.removeFromParent(); c.position.copy(p); c.quaternion.copy(q); c.scale.copy(s);
|
|
35
37
|
c.userData.hlod = false; // its model comes and goes with the record: never part of a merged far proxy
|
|
36
38
|
taken.push(c);
|
|
37
39
|
}
|
|
38
40
|
};
|
|
39
41
|
walk(root);
|
|
42
|
+
return taken;
|
|
40
43
|
}
|
|
44
|
+
/** Undo take, for a code-built scene that will not become the level: it is the caller's, and may be loaded again */
|
|
45
|
+
const giveBack = taken => { for (const o of taken) homes.get(o).attach(o); };
|
|
41
46
|
/** The name in node ids `<level>/<node>`: the level file name without folders and extension; a code-built scene uses its root name */
|
|
42
|
-
const levelName = () => {
|
|
43
|
-
|
|
44
|
-
if (!current) return getRoot()?.name || 'level';
|
|
47
|
+
const levelName = (current, root) => {
|
|
48
|
+
if (!current) return root?.name || 'level';
|
|
45
49
|
const f = String(current), base = f.slice(Math.max(f.lastIndexOf('/'), f.lastIndexOf('\\')) + 1), dot = base.lastIndexOf('.');
|
|
46
50
|
return dot > 0 ? base.slice(0, dot) : base;
|
|
47
51
|
};
|
|
48
52
|
const OWN = ['template', 'id', 'name', 'hlod']; // userData keys that are not record fields (three's glTF loader keeps the node name in userData.name)
|
|
53
|
+
/** What place would refuse, asked before the commit so a level that cannot place its records never replaces the old one */
|
|
54
|
+
function check(taken, current, root) {
|
|
55
|
+
const level = levelName(current, root);
|
|
56
|
+
for (const o of taken) {
|
|
57
|
+
const id = `${level}/${o.name}`, tpl = o.userData.template;
|
|
58
|
+
if (tpl != null) {
|
|
59
|
+
if (!e.content) throw codedError('level-node', fmt(STRINGS_DEV.noContent, { id }));
|
|
60
|
+
e.content.template(tpl); // throws, naming the templates there are
|
|
61
|
+
} else {
|
|
62
|
+
const kind = e.world.get(id)?.kind;
|
|
63
|
+
if (kind && kind !== 'levelNode') throw codedError('level-node', fmt(STRINGS_DEV.clash, { id, kind }));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
49
67
|
/** Taken nodes -> authored world records, id `<level>/<node>`: a template instance (custom property template) or the node itself (custom property id) */
|
|
50
|
-
function place() {
|
|
68
|
+
function place(taken) {
|
|
51
69
|
if (!taken.length) return; // also without world(): take left the nodes in the level
|
|
52
|
-
const level = levelName(), zone = e.world.zone;
|
|
53
|
-
for (const o of taken
|
|
70
|
+
const level = levelName(getCurrent(), getRoot()), zone = e.world.zone;
|
|
71
|
+
for (const o of taken) {
|
|
54
72
|
const id = `${level}/${o.name}`, ud = o.userData, node = o.name, { x, y, z } = o.position;
|
|
55
73
|
const fields = Object.fromEntries(Object.entries(ud).filter(([k, v]) => !OWN.includes(k) && (v == null || typeof v !== 'object' || Array.isArray(v) || v.constructor === Object)));
|
|
56
74
|
if (ud.template != null) {
|
|
@@ -70,15 +88,15 @@ export function levelNodes(e, getRoot, getCurrent) {
|
|
|
70
88
|
/** How a level node record looks: the node itself (detached while far or on another level, rebuilt from the same object) */
|
|
71
89
|
function presentNodes() {
|
|
72
90
|
if (presented) return;
|
|
73
|
-
presented = e.world.present('levelNode', {
|
|
91
|
+
presented = ctx.track(e.world.present('levelNode', {
|
|
74
92
|
build(rec) {
|
|
75
93
|
const o = nodes.get(rec.id);
|
|
76
94
|
if (!o) throw codedError('level-node', fmt(STRINGS_DEV.notLoaded, { id: rec.id }));
|
|
77
95
|
o.userData.unstatic = e.rays.addStatic(o);
|
|
78
96
|
return o;
|
|
79
97
|
},
|
|
80
|
-
dispose(rec, o) { o.userData.unstatic?.remove();
|
|
81
|
-
});
|
|
98
|
+
dispose(rec, o) { o.userData.unstatic?.remove(); o.removeFromParent(); },
|
|
99
|
+
}));
|
|
82
100
|
}
|
|
83
|
-
return { take, place };
|
|
101
|
+
return { take, giveBack, check, place };
|
|
84
102
|
}
|
package/modules/lights/index.js
CHANGED
|
@@ -23,7 +23,7 @@ import { warnOnce, fmt } from '../../core/base/validate.js';
|
|
|
23
23
|
import { readParams, keysSchema } from '../../core/base/params.js';
|
|
24
24
|
import { coded } from '../../core/base/errorHints.js';
|
|
25
25
|
import { daylightOf } from '../../render/daylight.js';
|
|
26
|
-
import { addSource } from './sources.js';
|
|
26
|
+
import { addSource, restoreGlow } from './sources.js';
|
|
27
27
|
import { makeGroup, assign, update } from './slots.js';
|
|
28
28
|
/** @import { ModuleDef } from '../../types/core' */
|
|
29
29
|
/** @import { LightPool, LightPoolOptions } from '../../types/world' */
|
|
@@ -71,7 +71,7 @@ export function lightPool(opts = {}) {
|
|
|
71
71
|
};
|
|
72
72
|
|
|
73
73
|
return {
|
|
74
|
-
name: 'lightPool', type: 'picture', params: LIGHT_POOL_PARAMS, options: opts, label: 'Light pool', stage: 'late',
|
|
74
|
+
name: 'lightPool', skipKind: 'none', type: 'picture', params: LIGHT_POOL_PARAMS, options: opts, label: 'Light pool', stage: 'late',
|
|
75
75
|
install(engine) { engine.i18n.add({ 'zh-CN': { 'module.lightPool': '光源池' }, en: { 'module.lightPool': 'light pool' } });
|
|
76
76
|
const e = pool.e = engine;
|
|
77
77
|
const k = Math.max(0, Math.min(shadows, point));
|
|
@@ -93,10 +93,13 @@ export function lightPool(opts = {}) {
|
|
|
93
93
|
if ((pool.timer += dt) >= every) { pool.timer = 0; assign(pool); }
|
|
94
94
|
update(pool, dt);
|
|
95
95
|
},
|
|
96
|
-
|
|
96
|
+
// newest first: sources sharing one material each saved the value they found, so the oldest restore comes last and leaves the true original (M12)
|
|
97
|
+
dispose() { offCut?.remove(); for (let i = pool.sources.length - 1; i >= 0; i--) restoreGlow(pool.sources[i]); for (const L of api.lights) { L.removeFromParent(); L.target?.removeFromParent(); L.dispose(); } },
|
|
97
98
|
describe() {
|
|
98
99
|
const u = api.stats;
|
|
99
100
|
return { lights: api.lights.length, lit: u.lit, sources: u.sources, shadows: Math.max(0, Math.min(shadows, point)) };
|
|
100
101
|
},
|
|
101
102
|
};
|
|
102
103
|
}
|
|
104
|
+
|
|
105
|
+
export const handRolled = 'none: lights in the scene are plain three.js, used on purpose';
|
|
@@ -27,6 +27,8 @@ function glowOf(g) {
|
|
|
27
27
|
// 1 for a source that ignores the time of day, else the night factor (0 by day, 1 at night) for its dusk
|
|
28
28
|
const nightK = (pool, src) => src.dusk === null ? 1 : nightOf(pool.daylight, src.dusk);
|
|
29
29
|
export const litOf = (pool, src) => src.intensity * nightK(pool, src);
|
|
30
|
+
/** Put a source's stand-in back as it was added */
|
|
31
|
+
export function restoreGlow(src) { const g = src.glow; if (g) { g.m[g.key] = g.base; g.k = -1; } }
|
|
30
32
|
export function setGlow(pool, src, k) {
|
|
31
33
|
const g = src.glow;
|
|
32
34
|
if (!g) return;
|
|
@@ -66,7 +68,7 @@ function handleOf(pool, src) {
|
|
|
66
68
|
src.removed = true;
|
|
67
69
|
const i = pool.sources.indexOf(src); if (i >= 0) pool.sources.splice(i, 1);
|
|
68
70
|
if (src.slot) src.slot.want = 0;
|
|
69
|
-
|
|
71
|
+
restoreGlow(src);
|
|
70
72
|
pool.timer = Infinity;
|
|
71
73
|
},
|
|
72
74
|
};
|
package/modules/music/Music.js
CHANGED
|
@@ -300,11 +300,11 @@ export function music(opts = {}) {
|
|
|
300
300
|
readParams('music', MUSIC_PARAMS, opts);
|
|
301
301
|
let m = null;
|
|
302
302
|
return {
|
|
303
|
-
name: 'music', type: 'picture', params: MUSIC_PARAMS, options: opts, label: 'Music', optional: ['dialogue'], stage: 'audio',
|
|
303
|
+
name: 'music', skipKind: 'none', type: 'picture', params: MUSIC_PARAMS, options: opts, label: 'Music', optional: ['dialogue'], stage: 'audio',
|
|
304
304
|
install(e, ctx) { e.i18n.add({ 'zh-CN': { 'module.music': '音乐' }, en: { 'module.music': 'music' } });
|
|
305
305
|
m = new Music(e, opts, ctx.track);
|
|
306
306
|
ctx.track(e.events.on('session:end', () => m.stop({ fade: 0 }))); // a playthrough ends: its music stops at once
|
|
307
|
-
|
|
307
|
+
ctx.saves.register('music', { notAtomic: 'sound, which an atomic op leaves alone: side effects go in after', save: () => ({ override: m._override ?? null, intensity: m.intensity }), load: d => m._load(d) });
|
|
308
308
|
return m;
|
|
309
309
|
},
|
|
310
310
|
frame(dt) { m.frame(dt); },
|
|
@@ -313,3 +313,5 @@ export function music(opts = {}) {
|
|
|
313
313
|
describe() { return { ...m.state, ready: m.ready, ruled: m.ruled, ducked: !!m._duck, tracks: m.tracks.size }; },
|
|
314
314
|
};
|
|
315
315
|
}
|
|
316
|
+
|
|
317
|
+
export const handRolled = 'none: a song by hand is the audio hint already';
|