threedog 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/README.md +39 -2
- package/core/Animator.js +268 -0
- package/core/Assets.js +256 -0
- package/core/Audio.js +267 -0
- package/core/Baked.js +72 -0
- package/core/Batching.js +247 -0
- package/core/Budget.js +221 -0
- package/core/Clock.js +94 -0
- package/core/Commits.js +77 -0
- package/core/DebugOverrides.js +143 -0
- package/core/Describe.js +145 -0
- package/core/Engine.js +184 -0
- package/core/Events.js +153 -0
- package/core/I18n.js +94 -0
- package/core/Input.js +245 -0
- package/core/Inspect.js +77 -0
- package/core/InspectScene.js +191 -0
- package/core/Jobs.js +146 -0
- package/core/Loading.js +149 -0
- package/core/MaterialInfo.js +32 -0
- package/core/Ops.js +256 -0
- package/core/Random.js +52 -0
- package/core/Rays.js +160 -0
- package/core/SaveStore.js +85 -0
- package/core/Saves.js +254 -0
- package/core/Stream.js +279 -0
- package/core/Threads.js +158 -0
- package/core/UrlState.js +94 -0
- package/core/Vars.js +35 -0
- package/core/Views.js +49 -0
- package/core/actor.js +23 -0
- package/core/assetEntry.js +89 -0
- package/core/assetLoad.js +190 -0
- package/core/assetManifest.js +35 -0
- package/core/assetReport.js +83 -0
- package/core/audioRules.js +121 -0
- package/core/audioVoice.js +225 -0
- package/core/batchGroups.js +116 -0
- package/core/canvasBox.js +63 -0
- package/core/catalog.js +169 -0
- package/core/checks.js +67 -0
- package/core/claims.js +51 -0
- package/core/dayTime.js +15 -0
- package/core/debugView.js +76 -0
- package/core/describeRender.js +45 -0
- package/core/describeText.js +143 -0
- package/core/easing.js +34 -0
- package/core/errorCodes.js +205 -0
- package/core/errorHints.js +208 -0
- package/core/errors.js +40 -0
- package/core/events.generated.js +84 -0
- package/core/ground.js +102 -0
- package/core/holds.js +106 -0
- package/core/inputContext.js +69 -0
- package/core/inputDefaults.js +22 -0
- package/core/inputPad.js +35 -0
- package/core/inputPointer.js +48 -0
- package/core/inputTouch.js +48 -0
- package/core/lifecycle.js +69 -0
- package/core/lightWatch.js +56 -0
- package/core/look/coreParts.js +112 -0
- package/core/look/layers.js +136 -0
- package/core/look/part.js +173 -0
- package/core/look/report.js +79 -0
- package/core/look/write.js +86 -0
- package/core/loop.js +200 -0
- package/core/math.js +33 -0
- package/core/modules.js +196 -0
- package/core/movement.js +19 -0
- package/core/mulberry32.js +14 -0
- package/core/opsAtomic.js +31 -0
- package/core/opsCheck.js +97 -0
- package/core/opsFail.js +20 -0
- package/core/opsGuard.js +26 -0
- package/core/opsHistory.js +27 -0
- package/core/opsRun.js +77 -0
- package/core/page.js +78 -0
- package/core/params.js +132 -0
- package/core/pureWorker.js +5 -0
- package/core/renamed.js +21 -0
- package/core/saveApply.js +128 -0
- package/core/saveFiles.js +139 -0
- package/core/saveIds.js +53 -0
- package/core/saveVersions.js +97 -0
- package/core/scatter.js +92 -0
- package/core/sceneMethods.js +100 -0
- package/core/screenToGround.js +39 -0
- package/core/services.js +73 -0
- package/core/session.js +171 -0
- package/core/shadowTypes.js +15 -0
- package/core/skip.js +104 -0
- package/core/startup.js +135 -0
- package/core/streamCells.js +24 -0
- package/core/streamSources.js +73 -0
- package/core/styleParts.js +96 -0
- package/core/targeting.js +64 -0
- package/core/thingExplain.js +68 -0
- package/core/thingOps.js +168 -0
- package/core/things.js +212 -0
- package/core/threadPure.js +120 -0
- package/core/threadServe.js +25 -0
- package/core/tier.js +11 -0
- package/core/util.js +11 -0
- package/core/validate.js +204 -0
- package/docs/AI.md +123 -0
- package/docs/API.md +83 -0
- package/docs/CODEMAP.md +347 -0
- package/docs/COMMANDS.md +108 -0
- package/docs/CONVENTIONS.md +145 -0
- package/docs/PERF.md +217 -0
- package/docs/THREE.md +86 -0
- package/docs/api/ambience.md +15 -0
- package/docs/api/assets.md +62 -0
- package/docs/api/audio.md +53 -0
- package/docs/api/budget.md +40 -0
- package/docs/api/cameraRig.md +120 -0
- package/docs/api/characters.md +144 -0
- package/docs/api/clock.md +7 -0
- package/docs/api/collision.md +39 -0
- package/docs/api/content.md +64 -0
- package/docs/api/dialogue.md +39 -0
- package/docs/api/engine.md +199 -0
- package/docs/api/entities.md +61 -0
- package/docs/api/events.md +95 -0
- package/docs/api/flow.md +27 -0
- package/docs/api/grids.md +11 -0
- package/docs/api/ground.md +16 -0
- package/docs/api/hlod.md +34 -0
- package/docs/api/hud.md +31 -0
- package/docs/api/i18n.md +10 -0
- package/docs/api/input.md +24 -0
- package/docs/api/interactions.md +31 -0
- package/docs/api/inventory.md +66 -0
- package/docs/api/jobs.md +10 -0
- package/docs/api/level.md +41 -0
- package/docs/api/lightPool.md +56 -0
- package/docs/api/music.md +34 -0
- package/docs/api/nav.md +26 -0
- package/docs/api/net.md +14 -0
- package/docs/api/openWorld.md +24 -0
- package/docs/api/particles.md +41 -0
- package/docs/api/photo.md +15 -0
- package/docs/api/post.md +10 -0
- package/docs/api/quests.md +36 -0
- package/docs/api/rays.md +26 -0
- package/docs/api/rigid.md +84 -0
- package/docs/api/saves.md +32 -0
- package/docs/api/settings.md +36 -0
- package/docs/api/sky.md +58 -0
- package/docs/api/stream.md +31 -0
- package/docs/api/style.md +134 -0
- package/docs/api/terrain.md +34 -0
- package/docs/api/timeline.md +39 -0
- package/docs/api/tools.md +57 -0
- package/docs/api/triggers.md +56 -0
- package/docs/api/vars.md +9 -0
- package/docs/api/vegetation.md +52 -0
- package/docs/api/water.md +48 -0
- package/docs/api/weather.md +29 -0
- package/docs/api/world.md +30 -0
- package/docs/recipes/01-new-game.md +148 -0
- package/docs/recipes/02-npc.md +167 -0
- package/docs/recipes/03-items-pickups.md +158 -0
- package/docs/recipes/04-quests.md +83 -0
- package/docs/recipes/05-props-bake.md +159 -0
- package/docs/recipes/06-clock-events.md +117 -0
- package/docs/recipes/07-flow-scenes-pause.md +132 -0
- package/docs/recipes/08-weather-sky.md +74 -0
- package/docs/recipes/09-style-pack.md +234 -0
- package/docs/recipes/10-blender-level.md +67 -0
- package/docs/recipes/11-vehicles.md +55 -0
- package/docs/recipes/12-navmesh.md +79 -0
- package/docs/recipes/13-animation.md +149 -0
- package/docs/recipes/14-custom-module.md +91 -0
- package/docs/recipes/15-perf.md +78 -0
- package/docs/recipes/16-dev-bridge.md +180 -0
- package/docs/recipes/18-world.md +185 -0
- package/docs/recipes/19-shooting-picking.md +76 -0
- package/docs/recipes/20-content-actions-save.md +176 -0
- package/docs/recipes/21-camera.md +153 -0
- package/docs/recipes/22-shop-gold.md +107 -0
- package/docs/recipes/23-ops.md +175 -0
- package/docs/recipes/26-ui-parts.md +138 -0
- package/docs/recipes/27-net.md +96 -0
- package/docs/recipes/28-terrain.md +80 -0
- package/docs/recipes/29-grid.md +87 -0
- package/docs/recipes/30-templates.md +131 -0
- package/docs/recipes/31-audio.md +126 -0
- package/docs/recipes/32-action-game.md +33 -0
- package/docs/recipes/33-stream-sources.md +37 -0
- package/docs/recipes/34-environment-lighting.md +36 -0
- package/docs/recipes/35-style-from-reference.md +89 -0
- package/docs/recipes/36-sky-ground-plant-looks.md +116 -0
- package/docs/recipes/37-terrain-look-hooks.md +97 -0
- package/docs/recipes/38-environment-look-hooks.md +83 -0
- package/docs/recipes/40-scene.md +108 -0
- package/docs/recipes/41-thing-registry.md +93 -0
- package/docs/recipes/42-terrain-shape.md +64 -0
- package/docs/recipes/43-shader-patches-surface-looks.md +120 -0
- package/docs/recipes/44-water-weather-look-hooks.md +135 -0
- package/docs/recipes/45-cascaded-shadows.md +51 -0
- package/docs/recipes/46-blender-art-route.md +114 -0
- package/docs/recipes/47-island-snow-backdrop.md +44 -0
- package/docs/recipes/48-make-material.md +70 -0
- package/docs/recipes/49-game-module-contract.md +71 -0
- package/docs/recipes/50-own-renderer.md +12 -0
- package/docs/recipes/51-capability-catalog.md +63 -0
- package/docs/recipes/52-look.md +108 -0
- package/examples/action/DESIGN.md +3 -0
- package/examples/action/GAMEMAP.md +127 -0
- package/examples/action/content/actions.js +8 -0
- package/examples/action/content/audio.js +9 -0
- package/examples/action/content/index.js +10 -0
- package/examples/action/content/items.js +4 -0
- package/examples/action/content/places.js +4 -0
- package/examples/action/content/props.js +11 -0
- package/examples/action/content/templates.js +10 -0
- package/examples/action/content/views.js +6 -0
- package/examples/action/look.js +5 -0
- package/examples/action/main.js +137 -0
- package/examples/action/models.js +25 -0
- package/examples/sheepisland/GAMEMAP.md +107 -0
- package/examples/sheepisland/content/index.js +6 -0
- package/examples/sheepisland/content/props.js +19 -0
- package/examples/sheepisland/content/templates.js +5 -0
- package/examples/sheepisland/content/vegetation.js +8 -0
- package/examples/sheepisland/main.js +71 -0
- package/examples/sheepisland/params.js +25 -0
- package/examples/sheepisland/style/index.js +79 -0
- package/examples/sheepisland/style/models.js +59 -0
- package/examples/sheepisland/views.js +9 -0
- package/examples/styletest/GAMEMAP.md +135 -0
- package/examples/styletest/content/index.js +9 -0
- package/examples/styletest/content/items.js +8 -0
- package/examples/styletest/content/npcs.js +8 -0
- package/examples/styletest/content/places.js +5 -0
- package/examples/styletest/content/props.js +16 -0
- package/examples/styletest/content/templates.js +9 -0
- package/examples/styletest/content/vegetation.js +31 -0
- package/examples/styletest/look.js +11 -0
- package/examples/styletest/main.js +95 -0
- package/examples/styletest/models.js +87 -0
- package/examples/styletest/overrides.js +33 -0
- package/examples/styletest/panel.js +135 -0
- package/examples/styletest/params.js +32 -0
- package/examples/styletest/views.js +31 -0
- package/examples/world/DESIGN.md +5 -0
- package/examples/world/GAMEMAP.md +171 -0
- package/examples/world/content/actions.js +8 -0
- package/examples/world/content/audio.js +25 -0
- package/examples/world/content/bodies.js +12 -0
- package/examples/world/content/containers.js +4 -0
- package/examples/world/content/dialogues.js +33 -0
- package/examples/world/content/index.js +17 -0
- package/examples/world/content/items.js +10 -0
- package/examples/world/content/npcs.js +14 -0
- package/examples/world/content/pickups.js +11 -0
- package/examples/world/content/places.js +6 -0
- package/examples/world/content/props.js +12 -0
- package/examples/world/content/quests.js +15 -0
- package/examples/world/content/rules.js +23 -0
- package/examples/world/content/templates.js +5 -0
- package/examples/world/content/vegetation.js +32 -0
- package/examples/world/main.js +168 -0
- package/examples/world/models.js +28 -0
- package/index.d.ts +306 -0
- package/index.js +122 -0
- package/llms.txt +61 -0
- package/modules/ambience/Ambience.js +186 -0
- package/modules/camera/FirstPersonCamera.js +131 -0
- package/modules/camera/FixedCamera.js +162 -0
- package/modules/camera/FollowCamera.js +91 -0
- package/modules/camera/RtsCamera.js +132 -0
- package/modules/camera/common.js +182 -0
- package/modules/camera/index.js +11 -0
- package/modules/camera/projection.js +34 -0
- package/modules/characters/Character.js +235 -0
- package/modules/characters/NPC.js +242 -0
- package/modules/characters/assetModel.js +42 -0
- package/modules/characters/astar.js +66 -0
- package/modules/characters/behaviors.js +72 -0
- package/modules/characters/footsteps.js +13 -0
- package/modules/characters/index.js +215 -0
- package/modules/characters/moveHelpers.js +29 -0
- package/modules/characters/movers/common.js +62 -0
- package/modules/characters/movers/rapier.js +74 -0
- package/modules/characters/movers/simple.js +43 -0
- package/modules/characters/npcAvoid.js +129 -0
- package/modules/characters/npcSim.js +150 -0
- package/modules/characters/npcView.js +42 -0
- package/modules/characters/pathAsync.js +113 -0
- package/modules/characters/pathJob.js +62 -0
- package/modules/characters/pathWorker.js +7 -0
- package/modules/characters/playerStrings.js +12 -0
- package/modules/characters/proceduralRig.js +58 -0
- package/modules/characters/useModel.js +22 -0
- package/modules/characters/walkTo.js +58 -0
- package/modules/collision/Collision.js +274 -0
- package/modules/collision/grid.js +63 -0
- package/modules/content/explain.js +13 -0
- package/modules/content/ids.js +95 -0
- package/modules/content/index.js +154 -0
- package/modules/content/loader.js +190 -0
- package/modules/content/models.js +69 -0
- package/modules/content/packs.js +69 -0
- package/modules/content/props.js +89 -0
- package/modules/content/rules.js +134 -0
- package/modules/content/templates.js +189 -0
- package/modules/debug/DebugPanel.js +174 -0
- package/modules/entities/Entities.js +243 -0
- package/modules/entities/build.js +25 -0
- package/modules/entities/index.js +4 -0
- package/modules/flow/index.js +101 -0
- package/modules/gameplay/Container.js +227 -0
- package/modules/gameplay/Containers.js +89 -0
- package/modules/gameplay/Dialogue.js +219 -0
- package/modules/gameplay/Interactions.js +253 -0
- package/modules/gameplay/Inventory.js +224 -0
- package/modules/gameplay/Quests.js +205 -0
- package/modules/gameplay/boxOps.js +60 -0
- package/modules/gameplay/dialogueBox.js +48 -0
- package/modules/gameplay/interactionPrompt.js +28 -0
- package/modules/gameplay/inventoryOps.js +137 -0
- package/modules/gameplay/pickups.js +115 -0
- package/modules/gameplay/questLog.js +54 -0
- package/modules/gameplay/questSave.js +71 -0
- package/modules/grid/Grid.js +184 -0
- package/modules/grid/Layer.js +142 -0
- package/modules/grid/index.js +236 -0
- package/modules/grid/ops.js +73 -0
- package/modules/grid/preview.js +85 -0
- package/modules/grid/tiles.js +79 -0
- package/modules/hlod/index.js +167 -0
- package/modules/hud/index.js +167 -0
- package/modules/hud/parts.js +80 -0
- package/modules/level/geometry.js +46 -0
- package/modules/level/heightfield.js +160 -0
- package/modules/level/index.js +233 -0
- package/modules/level/nodes.js +84 -0
- package/modules/lights/index.js +227 -0
- package/modules/music/Music.js +294 -0
- package/modules/music/index.js +2 -0
- package/modules/music/rules.js +28 -0
- package/modules/nav/index.js +156 -0
- package/modules/nav/navPath.js +17 -0
- package/modules/net/Client.js +137 -0
- package/modules/net/Host.js +117 -0
- package/modules/net/Net.js +49 -0
- package/modules/net/index.js +77 -0
- package/modules/net/sync.js +114 -0
- package/modules/net/transports.js +118 -0
- package/modules/particles/Particles.js +179 -0
- package/modules/particles/effect.js +112 -0
- package/modules/particles/shaders.js +28 -0
- package/modules/photo/Photo.js +146 -0
- package/modules/rigid/RigidBodies.js +265 -0
- package/modules/rigid/Vehicles.js +97 -0
- package/modules/rigid/sync.js +158 -0
- package/modules/settings/Settings.js +297 -0
- package/modules/settings/apply.js +21 -0
- package/modules/settings/index.js +3 -0
- package/modules/settings/sections.js +156 -0
- package/modules/settings/strings.js +53 -0
- package/modules/settings/view.js +65 -0
- package/modules/sky/Sky.js +259 -0
- package/modules/sky/calendar.js +80 -0
- package/modules/sky/cascades.js +125 -0
- package/modules/sky/clouds.js +79 -0
- package/modules/sky/domeLayers.js +33 -0
- package/modules/sky/environment.js +141 -0
- package/modules/sky/fog.js +139 -0
- package/modules/sky/frame.js +77 -0
- package/modules/sky/layers.js +69 -0
- package/modules/sky/look.js +51 -0
- package/modules/sky/lookFog.js +80 -0
- package/modules/sky/lookShadow.js +50 -0
- package/modules/sky/lookSky.js +108 -0
- package/modules/sky/schedule.js +80 -0
- package/modules/sky/shader.js +96 -0
- package/modules/sky/shadow.js +74 -0
- package/modules/sky/skyModule.js +60 -0
- package/modules/terrain/ChunkManager.js +260 -0
- package/modules/terrain/chunkJob.js +19 -0
- package/modules/terrain/chunkLevels.js +40 -0
- package/modules/terrain/chunkQueries.js +42 -0
- package/modules/terrain/chunkWorker.js +5 -0
- package/modules/terrain/index.js +118 -0
- package/modules/terrain/lookGround.js +61 -0
- package/modules/terrain/mountTerrain.js +32 -0
- package/modules/terrain/terrainColorMap.js +41 -0
- package/modules/terrain/terrainDefaults.js +39 -0
- package/modules/terrain/terrainEdit.js +139 -0
- package/modules/terrain/terrainGen.js +155 -0
- package/modules/terrain/terrainIsland.js +131 -0
- package/modules/terrain/terrainMesh.js +92 -0
- package/modules/terrain/terrainShape.js +108 -0
- package/modules/terrain/terrainShapeMain.js +41 -0
- package/modules/terrain/terrainTexture.js +44 -0
- package/modules/timeline/Timeline.js +253 -0
- package/modules/timeline/Tween.js +114 -0
- package/modules/timeline/letterbox.js +24 -0
- package/modules/timeline/shots.js +90 -0
- package/modules/triggers/index.js +268 -0
- package/modules/vegetation/Vegetation.js +256 -0
- package/modules/vegetation/groundChunks.js +177 -0
- package/modules/vegetation/index.js +43 -0
- package/modules/vegetation/lookWind.js +37 -0
- package/modules/vegetation/modelGeometry.js +21 -0
- package/modules/water/Water.js +160 -0
- package/modules/water/look.js +80 -0
- package/modules/water/surface.js +88 -0
- package/modules/weather/Weather.js +242 -0
- package/modules/weather/look.js +75 -0
- package/modules/weather/particles.js +92 -0
- package/modules/weather/shaders.js +49 -0
- package/modules/weather/strings.js +15 -0
- package/modules/world/World.js +231 -0
- package/modules/world/cells.js +42 -0
- package/modules/world/explain.js +11 -0
- package/modules/world/index.js +71 -0
- package/modules/world/persist.js +107 -0
- package/modules/world/pos.js +20 -0
- package/modules/world/records.js +89 -0
- package/modules/world/sim.js +175 -0
- package/modules/world/simFar.js +82 -0
- package/modules/world/state.js +115 -0
- package/modules/world/thingSource.js +44 -0
- package/modules/world/views.js +182 -0
- package/package.json +66 -4
- package/presets.js +96 -0
- package/render/PostFX.js +240 -0
- package/render/detail.js +105 -0
- package/render/effects.js +144 -0
- package/render/engineUniforms.js +23 -0
- package/render/fuse.js +84 -0
- package/render/lighting.js +100 -0
- package/render/lights.js +18 -0
- package/render/lookSurface.js +161 -0
- package/render/materials.js +220 -0
- package/render/patches.js +252 -0
- package/render/postLook.js +86 -0
- package/render/surface.js +69 -0
- package/render/uniformDecl.js +29 -0
- package/types/common.d.ts +34 -0
- package/types/core.d.ts +1965 -0
- package/types/gameplay.d.ts +1822 -0
- package/types/generated/core/Audio.d.ts +200 -0
- package/types/generated/core/Batching.d.ts +47 -0
- package/types/generated/core/Budget.d.ts +90 -0
- package/types/generated/core/Commits.d.ts +31 -0
- package/types/generated/core/DebugOverrides.d.ts +52 -0
- package/types/generated/core/Events.d.ts +89 -0
- package/types/generated/core/I18n.d.ts +30 -0
- package/types/generated/core/Input.d.ts +118 -0
- package/types/generated/core/Jobs.d.ts +41 -0
- package/types/generated/core/Ops.d.ts +203 -0
- package/types/generated/core/Random.d.ts +49 -0
- package/types/generated/core/Rays.d.ts +49 -0
- package/types/generated/core/Saves.d.ts +162 -0
- package/types/generated/core/Stream.d.ts +93 -0
- package/types/generated/core/Threads.d.ts +82 -0
- package/types/generated/core/Views.d.ts +33 -0
- package/types/generated/core/audioVoice.d.ts +47 -0
- package/types/generated/core/page.d.ts +18 -0
- package/types/generated/core/session.d.ts +48 -0
- package/types/generated/modules/ambience/Ambience.d.ts +48 -0
- package/types/generated/modules/debug/DebugPanel.d.ts +24 -0
- package/types/generated/modules/flow/index.d.ts +10 -0
- package/types/generated/modules/grid/Grid.d.ts +151 -0
- package/types/generated/modules/grid/Layer.d.ts +65 -0
- package/types/generated/modules/grid/index.d.ts +86 -0
- package/types/generated/modules/hlod/index.d.ts +11 -0
- package/types/generated/modules/hud/index.d.ts +10 -0
- package/types/generated/modules/lights/index.d.ts +13 -0
- package/types/generated/modules/music/Music.d.ts +111 -0
- package/types/generated/modules/net/Net.d.ts +34 -0
- package/types/generated/modules/net/index.d.ts +53 -0
- package/types/generated/modules/net/transports.d.ts +58 -0
- package/types/generated/modules/particles/Particles.d.ts +69 -0
- package/types/generated/modules/sky/Sky.d.ts +145 -0
- package/types/generated/modules/sky/skyModule.d.ts +10 -0
- package/types/generated/modules/timeline/Timeline.d.ts +51 -0
- package/types/generated/modules/water/Water.d.ts +29 -0
- package/types/generated/modules/weather/Weather.d.ts +62 -0
- package/types/generated/modules/world/World.d.ts +242 -0
- package/types/generated/modules/world/index.d.ts +15 -0
- package/types/generated/render/PostFX.d.ts +125 -0
- package/types/generated/render/materials.d.ts +100 -0
- package/types/generated/render/patches.d.ts +41 -0
- package/types/generated/ui/Focus.d.ts +11 -0
- package/types/generated/ui/LoadingScreen.d.ts +2 -0
- package/types/generated/ui/UI.d.ts +57 -0
- package/types/nav.d.ts +52 -0
- package/types/physics.d.ts +592 -0
- package/types/style.d.ts +638 -0
- package/types/worker.d.ts +11 -0
- package/types/world.d.ts +772 -0
- package/ui/Focus.js +51 -0
- package/ui/LoadingScreen.js +42 -0
- package/ui/Parts.js +199 -0
- package/ui/UI.js +167 -0
- package/ui/defaults.js +85 -0
- package/ui/theme.js +124 -0
- package/ui/touchPart.js +49 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Sky environment lighting part (mixed into Sky.prototype): scene.environment from a procedural sky or an HDR / panorama asset.
|
|
2
|
+
//
|
|
3
|
+
// What three 0.187 does with scene.environment on a MeshLambertMaterial (read from the source, not assumed):
|
|
4
|
+
// - WebGLPrograms.getParameters hands scene.environment to Standard, Lambert and Phong materials (usePMREM is true for Lambert without its own envMap).
|
|
5
|
+
// - lights_fragment_maps adds getIBLIrradiance(normal) = PI * (the PMREM's blurriest mip in the world normal's direction) * envMapIntensity
|
|
6
|
+
// to the diffuse light, so a Lambert surface gets soft, direction-dependent ambient light that is coloured by the environment.
|
|
7
|
+
// - There is no specular term for Lambert with a PMREM map: the cube reflection mix in envmap_fragment only runs for ENVMAP_TYPE_CUBE.
|
|
8
|
+
// - envMapIntensity comes from scene.environmentIntensity and the lookup direction is turned by scene.environmentRotation.
|
|
9
|
+
// - Adding or removing scene.environment changes the program key of every Lambert material: one recompile at the switch, none after.
|
|
10
|
+
// - The hemisphere light adds on top of it. A style that turns the environment on usually lowers its ambient column of sky.keyframes.
|
|
11
|
+
// The engine sets no environment by default, so the plain low-poly Lambert look is unchanged until a style or the game asks for one.
|
|
12
|
+
import * as THREE from 'three';
|
|
13
|
+
import { warnOnce } from '../../core/validate.js';
|
|
14
|
+
import { defineLookPart, REPLACE } from '../../core/look/part.js';
|
|
15
|
+
|
|
16
|
+
const KEYS = ['source', 'intensity', 'rotation', 'interval'];
|
|
17
|
+
const DEFAULT_INTERVAL = 1; // seconds between regenerations of a procedural environment (only when the sky actually changed)
|
|
18
|
+
const STRINGS = {
|
|
19
|
+
noSource: "look.env.replace(source): source is missing. Use 'sky' (built from the procedural sky) or the name of an HDR / panorama asset, " +
|
|
20
|
+
"or look.env.replace(false) to turn the environment lighting off",
|
|
21
|
+
badSource: "look.env.replace(source): source must be 'sky', an asset name / path (.hdr, .jpg, .ktx2...) or null; got {got}",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The environment settings to start with: the style's sky.environment overridden by sky({ environment }). On a low-tier device a procedural
|
|
26
|
+
* environment regenerates 4 times less often, the style's environment.low overrides that, and low: false turns it off there. Returns null when off
|
|
27
|
+
*/
|
|
28
|
+
export function environmentFor(tier, styleEnv, optEnv) {
|
|
29
|
+
const { low, ...env } = { ...styleEnv, ...optEnv };
|
|
30
|
+
if (!env.source) return null;
|
|
31
|
+
if (tier !== 'low') return env;
|
|
32
|
+
return low === false ? null : { ...env, interval: (env.interval ?? DEFAULT_INTERVAL) * 4, ...low };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Numbers that decide how the procedural sky looks right now: when they stay put the environment is not rebuilt
|
|
36
|
+
function skyKey(u) {
|
|
37
|
+
const f = x => Math.round(x * 400);
|
|
38
|
+
return [...u.uTop.value.toArray(), ...u.uHorizon.value.toArray(), ...u.uSunDir.value.toArray(), ...u.uSunColor.value.toArray(), u.uNight.value, u.uCloud.value].map(f).join(',');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* look.env: environment lighting as a look part. set({ intensity, rotation, interval }) tunes it, replace(source) swaps the map ('sky', an asset
|
|
43
|
+
* name, or false = off), replace(null) gives it back to the style. The style layer is style.sky.environment merged with sky({ environment }).
|
|
44
|
+
* Why the asset stays a name: loading belongs to engine.assets; the part only says which map to use.
|
|
45
|
+
* @param {any} sky @param {object | null} env environmentFor(...) at start
|
|
46
|
+
*/
|
|
47
|
+
export function defineEnvLook(sky, env) {
|
|
48
|
+
const e = sky.engine, scene = e.scene;
|
|
49
|
+
let src = null, vals = { intensity: 1, rotation: 0, interval: DEFAULT_INTERVAL };
|
|
50
|
+
const sync = () => (src ? sky._envApply({ source: src, ...vals }) : sky._envOff());
|
|
51
|
+
const { source, low, ...tune } = env ?? {};
|
|
52
|
+
defineLookPart(e, {
|
|
53
|
+
id: 'env', group: 'atmosphere', module: 'sky', object: () => scene.environment, docs: ['recipes/52-look.md'],
|
|
54
|
+
params: {
|
|
55
|
+
intensity: { type: 'number', default: 1, min: 0, neutral: true, cost: 'uniform', desc: 'scene.environmentIntensity' },
|
|
56
|
+
rotation: { type: 'number', default: 0, neutral: true, cost: 'uniform', desc: 'turns the map around Y (radians)' },
|
|
57
|
+
interval: { type: 'number', default: DEFAULT_INTERVAL, min: 0, neutral: true, cost: 'gpu', desc: "seconds between rebuilds of a 'sky' map (only when the sky changed)" },
|
|
58
|
+
},
|
|
59
|
+
style: () => ({ ...tune, ...(source ? { [REPLACE]: source } : {}) }),
|
|
60
|
+
read: () => (src ? { intensity: scene.environmentIntensity, rotation: scene.environmentRotation.y } : {}),
|
|
61
|
+
apply(v) { vals = v; sync(); },
|
|
62
|
+
replace: { accepts: "'sky' (built from the procedural sky) | an asset name or path (.hdr, .jpg, .ktx2) | false (off)", loses: ['nothing: intensity and rotation still apply to your map'] },
|
|
63
|
+
applyReplace(impl) {
|
|
64
|
+
if (impl != null && impl !== false && typeof impl !== 'string') throw new Error(STRINGS.badSource.replace('{got}', String(impl)));
|
|
65
|
+
src = impl || null; sync();
|
|
66
|
+
},
|
|
67
|
+
noPatch: 'environment lighting has no GLSL hook: it is a texture; swap it with look.env.replace(...) or tune it with look.env.set({ intensity })',
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export const environmentMethods = {
|
|
72
|
+
// (the environment is changed through engine.look.env.replace(source) / .set({ intensity, rotation, interval }))
|
|
73
|
+
// Apply settings to the scene (look.env's apply): a changed source rebuilds or reloads the map, the same source only retunes it
|
|
74
|
+
_envApply(o) {
|
|
75
|
+
const cur = this._env;
|
|
76
|
+
const next = { source: cur?.source, intensity: cur?.intensity ?? 1, rotation: cur?.rotation ?? 0, interval: cur?.interval ?? DEFAULT_INTERVAL };
|
|
77
|
+
for (const k of KEYS) if (o[k] !== undefined) next[k] = o[k];
|
|
78
|
+
if (!next.source) throw new Error(STRINGS.noSource);
|
|
79
|
+
if (typeof next.source !== 'string') throw new Error(STRINGS.badSource.replace('{got}', String(next.source)));
|
|
80
|
+
const scene = this.engine.scene, changed = next.source !== cur?.source;
|
|
81
|
+
scene.environmentIntensity = next.intensity;
|
|
82
|
+
scene.environmentRotation.set(0, next.rotation, 0);
|
|
83
|
+
this._env = { ...next, key: null, since: 0, builds: cur?.builds ?? 0, token: (cur?.token ?? 0) + (changed ? 1 : 0), target: cur?.target ?? null, deferred: !this._envFramed };
|
|
84
|
+
if (changed && next.source !== 'sky') this._envFromAsset(next.source, this._env.token);
|
|
85
|
+
else if (changed && this._envFramed) this._envBuild();
|
|
86
|
+
return this;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
// Snapshot behind sky.environment (builds = times a procedural environment was generated); null when off
|
|
90
|
+
_envInfo() {
|
|
91
|
+
const e = this._env;
|
|
92
|
+
return e ? { source: e.source, intensity: e.intensity, rotation: e.rotation, interval: e.interval, builds: e.builds } : null;
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
// Each frame: rebuild a procedural environment when its interval has passed and the sky moved (the first build waits for the first frame's colors)
|
|
96
|
+
_envFrame(dt) {
|
|
97
|
+
this._envFramed = true;
|
|
98
|
+
const e = this._env;
|
|
99
|
+
if (!e || e.source !== 'sky') return;
|
|
100
|
+
e.since += dt;
|
|
101
|
+
if (!e.deferred && e.since < e.interval) return;
|
|
102
|
+
e.since = 0; e.deferred = false;
|
|
103
|
+
if (skyKey(this.uniforms) !== e.key) this._envBuild();
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
// Render the dome alone into a cube and prefilter it (PMREM). The dome mesh follows the camera, so a copy sharing its material is rendered
|
|
107
|
+
_envBuild() {
|
|
108
|
+
const e = this._env, r = this.engine.renderer;
|
|
109
|
+
this._pmrem ??= new THREE.PMREMGenerator(r);
|
|
110
|
+
if (!this._envScene) { this._envScene = new THREE.Scene(); this._envScene.add(new THREE.Mesh(this.dome.geometry, this.dome.material)); }
|
|
111
|
+
const mesh = this._envScene.children[0];
|
|
112
|
+
mesh.onBeforeRender = (rr, sc, cam) => { mesh.scale.setScalar(cam.far * 0.9); mesh.updateMatrixWorld(); };
|
|
113
|
+
mesh.frustumCulled = false; mesh.renderOrder = -1000;
|
|
114
|
+
this._envSet(this._pmrem.fromScene(this._envScene, 0, 0.1, 100, { size: 256 }));
|
|
115
|
+
e.builds++; e.key = skyKey(this.uniforms);
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
// Panorama asset (.hdr goes through HDRLoader in the asset loader). A newer look.env.replace call makes the token stale and the late result is dropped
|
|
119
|
+
_envFromAsset(name, token) {
|
|
120
|
+
this.engine.assets.texture(name).then(t => {
|
|
121
|
+
if (this._env?.token !== token || this.engine.disposed) return;
|
|
122
|
+
t.mapping = THREE.EquirectangularReflectionMapping;
|
|
123
|
+
this._pmrem ??= new THREE.PMREMGenerator(this.engine.renderer);
|
|
124
|
+
this._envSet(this._pmrem.fromEquirectangular(t));
|
|
125
|
+
}).catch(err => warnOnce(err.message));
|
|
126
|
+
},
|
|
127
|
+
|
|
128
|
+
_envSet(target) {
|
|
129
|
+
this._env.target?.dispose();
|
|
130
|
+
this._env.target = target;
|
|
131
|
+
this.engine.scene.environment = target.texture;
|
|
132
|
+
},
|
|
133
|
+
|
|
134
|
+
_envOff() {
|
|
135
|
+
if (!this._env && !this._pmrem) return;
|
|
136
|
+
this._env?.target?.dispose();
|
|
137
|
+
if (this.engine.scene.environment === this._env?.target?.texture) this.engine.scene.environment = null;
|
|
138
|
+
this._env = null;
|
|
139
|
+
this._pmrem?.dispose(); this._pmrem = null; this._envScene = null;
|
|
140
|
+
},
|
|
141
|
+
};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// Sky fog (helper of Sky.js): style.sky.fog / sky({ fog }) picks the fog model and color; the sky drives it every frame.
|
|
2
|
+
// kind 'linear' (default, three.js Fog: near = 0.3 x far) | 'exp' | 'exp2' (three.js FogExp2) | 'height' (thick low, thin high)
|
|
3
|
+
// color 'horizon' (default: the dome's horizon color, so distant ground melts into the sky) | a color | (sky) => color
|
|
4
|
+
// density per meter for exp / exp2 / height (default: derived from engine.fogFar, so the fog reaches the same distance as linear)
|
|
5
|
+
// height / falloff 'height' only: the fog's floor (world y) and how fast it thins above it (per meter)
|
|
6
|
+
// glsl replace the fog math in every material: `vec4 threedogFog(vec3 color, float depth, vec3 worldPos)` returns
|
|
7
|
+
// (fog rgb, amount 0..1). In scope: fogColor, cameraPosition, fogNear / fogFar (linear) or fogDensity (the other kinds),
|
|
8
|
+
// THREEDOG_FOG_HEIGHT / THREEDOG_FOG_FALLOFF. No custom uniforms: three copies fog uniforms into each material when it
|
|
9
|
+
// compiles, so a live value would need every material patched; write constants in the GLSL instead.
|
|
10
|
+
// How: 'linear' / 'exp2' without glsl are three's own fog, untouched (the default draws exactly what it always drew). 'exp', 'height'
|
|
11
|
+
// and glsl replace three's fog_* shader chunks (ShaderChunk is shared by every material, including other packages' and the game's own),
|
|
12
|
+
// once at install: three caches programs by material settings, not by source, so the chunks cannot change mid-play; density and color
|
|
13
|
+
// stay live. The replacement keeps three's code for materials that compile under the other fog object. The chunks are shared by the
|
|
14
|
+
// page, so two engines may use the same custom setting (reference counted) but a different one throws; the originals return when the
|
|
15
|
+
// last engine using them is disposed.
|
|
16
|
+
// sky({ fog: true | 'manual' | false }) (look.js) says who owns scene.fog: the sky (default), the game (the sky never writes it), or nobody (null).
|
|
17
|
+
import * as THREE from 'three';
|
|
18
|
+
import { checkOptions, closest } from '../../core/validate.js';
|
|
19
|
+
|
|
20
|
+
const KINDS = ['linear', 'exp', 'exp2', 'height'];
|
|
21
|
+
export const FOG_KINDS = KINDS;
|
|
22
|
+
const KEYS = ['kind', 'color', 'density', 'height', 'falloff', 'glsl', 'near', 'far', 'enabled']; // near / far / enabled: look.fog (lookFog.js)
|
|
23
|
+
const NAMES = ['fog_pars_vertex', 'fog_vertex', 'fog_pars_fragment', 'fog_fragment'];
|
|
24
|
+
const ORIGINAL = Object.fromEntries(NAMES.map(n => [n, THREE.ShaderChunk[n]]));
|
|
25
|
+
// fog amount reached at the far distance with the derived density: exp(-3.9) = 2% (exp, height), exp(-1.98^2) = 2% (exp2)
|
|
26
|
+
const REACH = { exp: 3.9, exp2: 1.98, height: 3.9 };
|
|
27
|
+
const flt = v => Number.isInteger(v) ? v.toFixed(1) : String(v);
|
|
28
|
+
|
|
29
|
+
const FN = {
|
|
30
|
+
exp: 'vec4 threedogFog(vec3 color, float depth, vec3 worldPos){ return vec4(color, 1.0 - exp(-fogDensity * depth)); }',
|
|
31
|
+
// density decays as exp(-falloff * (y - floor)); this is its integral along the view ray. Heights are clamped at the floor (full
|
|
32
|
+
// density below it), which also keeps the exponentials finite for any camera height
|
|
33
|
+
height: `vec4 threedogFog(vec3 color, float depth, vec3 worldPos){
|
|
34
|
+
float b = THREEDOG_FOG_FALLOFF, hc = max(cameraPosition.y - THREEDOG_FOG_HEIGHT, 0.0), hp = max(worldPos.y - THREEDOG_FOG_HEIGHT, 0.0);
|
|
35
|
+
float dist = length(worldPos - cameraPosition), dh = hp - hc;
|
|
36
|
+
float k = abs(dh * b) < 1e-4 ? exp(-b * hc) : (exp(-b * hc) - exp(-b * hp)) / (b * dh);
|
|
37
|
+
return vec4(color, 1.0 - exp(-fogDensity * dist * k));
|
|
38
|
+
}`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Check style.sky.fog (+ sky({ fog }) on top) and return the resolved settings; throws with the fix on a bad kind or glsl */
|
|
42
|
+
export function resolveFog(spec) {
|
|
43
|
+
const s = spec || {};
|
|
44
|
+
checkOptions('style.sky.fog', s, KEYS);
|
|
45
|
+
const kind = s.kind ?? 'linear';
|
|
46
|
+
if (!KINDS.includes(kind)) throw new Error(`style.sky.fog.kind '${kind}' is not a fog kind; use ${KINDS.map(k => `'${k}'`).join(' | ')}` + (closest(kind, KINDS) ? ` (did you mean '${closest(kind, KINDS)}'?)` : ''));
|
|
47
|
+
if (s.glsl != null && (typeof s.glsl !== 'string' || !/vec4\s+threedogFog\s*\(/.test(s.glsl)))
|
|
48
|
+
throw new Error('style.sky.fog.glsl must be GLSL that defines `vec4 threedogFog(vec3 color, float depth, vec3 worldPos)` returning (fog rgb, amount 0..1); see modules/sky/fog.js');
|
|
49
|
+
const color = s.color ?? 'horizon';
|
|
50
|
+
return { kind, color: color === 'horizon' || typeof color === 'function' ? color : new THREE.Color(color), density: s.density ?? null,
|
|
51
|
+
height: s.height ?? 0, falloff: s.falloff ?? 0.05, glsl: s.glsl ?? FN[kind] ?? null, near: s.near ?? null, far: s.far ?? null, enabled: s.enabled !== false };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The fog object for the kind: density-based kinds use FogExp2 (its fogDensity uniform is live), linear uses Fog */
|
|
55
|
+
export function makeFog(f, far) {
|
|
56
|
+
return f.kind === 'linear' ? new THREE.Fog(0xcccccc, far * 0.3, far) : new THREE.FogExp2(0xcccccc, REACH[f.kind] / far);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// The chunks are one global per page, so only one custom fog setting can own them at a time. Engines with the same setting share
|
|
60
|
+
// them (count); a different setting throws; the originals come back when the last owner is disposed. Plain linear / exp2 never claim.
|
|
61
|
+
let held = null; // { key, count }
|
|
62
|
+
|
|
63
|
+
/** Install the fog chunks for f (a plain linear / exp2 touches nothing); returns the function that releases them (restores at the last owner) */
|
|
64
|
+
export function applyFogChunks(f) {
|
|
65
|
+
const C = THREE.ShaderChunk;
|
|
66
|
+
if (!f?.glsl) return () => {};
|
|
67
|
+
// everything the chunks bake in: kind picks the branch, height / falloff are constants, glsl is the math
|
|
68
|
+
const key = JSON.stringify([f.kind, f.height, f.falloff, f.glsl]);
|
|
69
|
+
if (held && held.key !== key)
|
|
70
|
+
throw new Error(`sky fog: another engine on this page already installed a different custom fog (kind '${held.kind}'); three.js shares the fog shader chunks across the page, so only one custom fog setting can exist at a time. `
|
|
71
|
+
+ `Use the same style.sky.fog (kind, height, falloff, glsl) in both engines, give only one engine a custom fog (the other keeps 'linear' / 'exp2'), or dispose the first engine before creating this one.`);
|
|
72
|
+
if (held) { held.count++; return release(held); }
|
|
73
|
+
held = { key, kind: f.kind, count: 1 };
|
|
74
|
+
// the material compiles under FOG_EXP2 when the fog object is FogExp2, so that branch holds the replacement for density kinds;
|
|
75
|
+
// a custom glsl for 'linear' takes the other branch
|
|
76
|
+
const dense = f.kind !== 'linear', own = `vec4 threedogFogOut = threedogFog(fogColor, vFogDepth, vFogWorld);
|
|
77
|
+
gl_FragColor.rgb = mix(gl_FragColor.rgb, threedogFogOut.rgb, clamp(threedogFogOut.a, 0.0, 1.0));`;
|
|
78
|
+
const orig = ORIGINAL.fog_fragment.replace('#ifdef USE_FOG', '').replace(/#endif\s*$/, '');
|
|
79
|
+
C.fog_pars_vertex = '#ifdef USE_FOG\n\tvarying float vFogDepth;\n\tvarying vec3 vFogWorld;\n#endif\n';
|
|
80
|
+
// world position from the view-space one: the view matrix is a pure rotation + translation, so its transpose undoes the rotation
|
|
81
|
+
// (cheaper than another matrix and right for instanced and skinned meshes too, which mvPosition already includes)
|
|
82
|
+
C.fog_vertex = '#ifdef USE_FOG\n\tvFogDepth = - mvPosition.z;\n\tvFogWorld = ( vec4( mvPosition.xyz, 0.0 ) * viewMatrix ).xyz + cameraPosition;\n#endif\n';
|
|
83
|
+
C.fog_pars_fragment = `#ifdef USE_FOG
|
|
84
|
+
uniform vec3 fogColor;
|
|
85
|
+
varying float vFogDepth;
|
|
86
|
+
varying vec3 vFogWorld;
|
|
87
|
+
const float THREEDOG_FOG_HEIGHT = ${flt(f.height)};
|
|
88
|
+
const float THREEDOG_FOG_FALLOFF = ${flt(f.falloff)};
|
|
89
|
+
#ifdef FOG_EXP2
|
|
90
|
+
uniform float fogDensity;
|
|
91
|
+
${dense ? f.glsl : ''}
|
|
92
|
+
#else
|
|
93
|
+
uniform float fogNear;
|
|
94
|
+
uniform float fogFar;
|
|
95
|
+
${dense ? '' : f.glsl}
|
|
96
|
+
#endif
|
|
97
|
+
#endif
|
|
98
|
+
`;
|
|
99
|
+
C.fog_fragment = `#ifdef USE_FOG
|
|
100
|
+
#ifdef FOG_EXP2
|
|
101
|
+
${dense ? own : orig}
|
|
102
|
+
#else
|
|
103
|
+
${dense ? orig : own}
|
|
104
|
+
#endif
|
|
105
|
+
#endif
|
|
106
|
+
`;
|
|
107
|
+
return release(held);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// One release per owner; the originals return only when the count reaches 0
|
|
111
|
+
function release(h) {
|
|
112
|
+
let done = false;
|
|
113
|
+
return () => {
|
|
114
|
+
if (done) return; done = true;
|
|
115
|
+
if (--h.count > 0) return;
|
|
116
|
+
for (const n of NAMES) THREE.ShaderChunk[n] = ORIGINAL[n];
|
|
117
|
+
if (held === h) held = null;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Per frame: fog color and distance / density from the sky's horizon and the modifier layers (fog multiplies the distance) */
|
|
121
|
+
export function updateFog(sky, horizon, M) {
|
|
122
|
+
// only when the sky drives fog (sky({ fog: true })) and scene.fog is still a fog object: the game may have nulled or replaced it
|
|
123
|
+
const fog = sky.engine.scene.fog, f = sky._fog;
|
|
124
|
+
if (sky._drive.fog !== true || sky._fogHeld || !fog?.color) return; // _fogHeld: look.fog.replace(...) gave the fog to the game
|
|
125
|
+
// look.fog: far / near set explicitly are the base distance (weather still scales it); enabled false pushes the fog out of reach without a recompile
|
|
126
|
+
const far = f.enabled ? (f.far ?? sky.engine.fogFar) * M.fog : 1e9;
|
|
127
|
+
if (f.color === 'horizon') fog.color.copy(horizon);
|
|
128
|
+
else if (typeof f.color === 'function') fog.color.set(f.color(sky));
|
|
129
|
+
else fog.color.copy(f.color);
|
|
130
|
+
// near / far are written for every kind: engine.setFogFar and the dev guard read them
|
|
131
|
+
fog.far = far; fog.near = f.enabled && f.near != null ? f.near * M.fog : far * 0.3;
|
|
132
|
+
if (f.kind !== 'linear') fog.density = !f.enabled ? 0 : f.density != null ? f.density / M.fog : REACH[f.kind] / far;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Dome addition when the fog color is not the horizon: the dome's lower band fades to the fog color so the horizon line does not show */
|
|
136
|
+
export function domeFog(f) {
|
|
137
|
+
if (f.color === 'horizon') return { decl: '', body: '' };
|
|
138
|
+
return { decl: 'uniform vec3 uFogColor;', body: '\n col = mix(col, uFogColor, 1.0 - smoothstep(-0.02, 0.15, d.y));' };
|
|
139
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// Sky per-frame work (helper of Sky.js): dome colors, sun / moon light, ambient, fog and the shadow center from the time of day and
|
|
2
|
+
// the modifier layers; plus the dev guard that warns when a game edits what this overwrites every frame.
|
|
3
|
+
import { ownedProps } from '../../core/validate.js';
|
|
4
|
+
import { applySeason } from './calendar.js';
|
|
5
|
+
import { applyCover } from './layers.js';
|
|
6
|
+
import { updateFog } from './fog.js';
|
|
7
|
+
import { cascadeFrame } from './cascades.js';
|
|
8
|
+
|
|
9
|
+
// Developer message: how to fix when a per-frame recomputed property (ownedProps) is edited directly
|
|
10
|
+
const STRINGS = {
|
|
11
|
+
sun: "use engine.sky.modify('layerName', { sun })",
|
|
12
|
+
sunColor: "edit style pack sky.keyframes or use engine.sky.modify('layerName', { tint, tintAmount })",
|
|
13
|
+
sunPos: 'sun position is driven by sky.timeOfDay',
|
|
14
|
+
hemi: "use engine.sky.modify('layerName', { ambient })",
|
|
15
|
+
fog: "use look.fog.set({ color, density, far }), engine.sky.modify('layerName', { fog }) or engine.setFogFar(); to own scene.fog, create the sky with sky({ fog: 'manual' }) (or false)",
|
|
16
|
+
modifiers: "sky.modifiers is the composite of all layers; use engine.sky.modify('layerName', { sun, ambient, fog, ... })",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/** Watchers of the properties sky rewrites every frame (validate.js ownedProps) */
|
|
20
|
+
export function watchOwned(sky) {
|
|
21
|
+
const scene = sky.engine.scene, fog = () => scene.fog;
|
|
22
|
+
// only what the sky writes: a light in 'manual' or false mode (look.js) is the game's to edit
|
|
23
|
+
const D = sky._drive;
|
|
24
|
+
return [
|
|
25
|
+
D.sun === true && ownedProps('sky.sun.intensity', () => sky.sun.intensity, STRINGS.sun),
|
|
26
|
+
D.sun === true && ownedProps('sky.sun.color', () => sky.sun.color.toArray(), STRINGS.sunColor),
|
|
27
|
+
D.sun !== false && ownedProps('sky.sun.position', () => sky.sun.position.toArray(), STRINGS.sunPos),
|
|
28
|
+
D.hemi === true && ownedProps('sky.hemi', () => [sky.hemi.intensity, ...sky.hemi.color.toArray(), ...sky.hemi.groundColor.toArray()], STRINGS.hemi),
|
|
29
|
+
// fog: the object itself, color, near/far ratio (engine.setFogFar keeps the ratio, so it is not a violation)
|
|
30
|
+
D.fog === true && ownedProps('scene.fog', () => sky._fogHeld ? [null] : [fog(), ...(fog()?.color?.toArray() ?? []), fog()?.near / fog()?.far], STRINGS.fog),
|
|
31
|
+
ownedProps('sky.modifiers', () => { const M = sky.modifiers; return [M.sun, M.ambient, M.fog, M.tintAmount, M.horizonTintAmount, M.flash, ...M.tint.toArray()]; }, STRINGS.modifiers),
|
|
32
|
+
].filter(Boolean);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Recompute dome, lights, fog and shadow placement for this frame */
|
|
36
|
+
export function updateSky(sky, focus) {
|
|
37
|
+
applySeason(sky, sky.engine.uniforms);
|
|
38
|
+
const t = sky.timeOfDay, kf = sky.kf;
|
|
39
|
+
let i = 0; while (i < kf.length - 2 && t > kf[i + 1][0]) i++;
|
|
40
|
+
const a = kf[i], b = kf[i + 1], f = (t - a[0]) / (b[0] - a[0]);
|
|
41
|
+
const u = sky.uniforms, scene = sky.engine.scene;
|
|
42
|
+
u.uTop.value.copy(a[1]).lerp(b[1], f);
|
|
43
|
+
u.uHorizon.value.copy(a[2]).lerp(b[2], f);
|
|
44
|
+
for (const o of sky._owned) o.check();
|
|
45
|
+
sky._combine();
|
|
46
|
+
applyCover(sky, sky.engine.uniforms); // after the season: snow / ash cover lies over the season tint
|
|
47
|
+
const M = sky.modifiers;
|
|
48
|
+
if (M.tintAmount) u.uTop.value.lerp(M.tint, M.tintAmount);
|
|
49
|
+
if (M.horizonTintAmount) u.uHorizon.value.lerp(M.tint, M.horizonTintAmount);
|
|
50
|
+
if (M.flash) { u.uTop.value.addScalar(M.flash * 0.4); u.uHorizon.value.addScalar(M.flash * 0.3); }
|
|
51
|
+
const bd = sky._backdrop; // style.sky.backdrop (look.js): a flat background in place of the dome, fog follows it
|
|
52
|
+
if (bd) { if (bd.fn) bd.color.set(bd.fn(sky)); sky.dome.visible = false; scene.background = bd.color; }
|
|
53
|
+
updateFog(sky, bd ? bd.color : u.uHorizon.value, M);
|
|
54
|
+
u.uSunColor.value.copy(a[3]).lerp(b[3], f); // the dome's sun disk follows the keyframes whatever drives the light
|
|
55
|
+
if (sky._drive.sun === true) sky.sun.color.copy(u.uSunColor.value);
|
|
56
|
+
const sunI = a[4] + (b[4] - a[4]) * f, hemiI = a[5] + (b[5] - a[5]) * f;
|
|
57
|
+
|
|
58
|
+
const ang = (t - 0.25) * Math.PI * 2, elev = Math.sin(ang);
|
|
59
|
+
sky.sunDir.set(Math.cos(ang) * sky.orbit.sway, elev, sky.orbit.tilt).normalize();
|
|
60
|
+
u.uSunDir.value.copy(sky.sunDir);
|
|
61
|
+
u.uNight.value = Math.min(1, Math.max(0, -elev * 4));
|
|
62
|
+
const lightDir = elev >= 0 ? sky.sunDir : sky._lightDir.copy(sky.sunDir).negate().setZ(sky.orbit.tilt).normalize();
|
|
63
|
+
if (sky._drive.sun === true) sky.sun.intensity = sunI * Math.min(1, Math.abs(elev) * 7) * M.sun; // fade out near the horizon so day/night switching does not pop
|
|
64
|
+
if (sky._drive.hemi === true) {
|
|
65
|
+
sky.hemi.intensity = hemiI * M.ambient + M.flash * 3;
|
|
66
|
+
sky.hemi.color.copy(u.uTop.value).lerp(u.uHorizon.value, 0.5);
|
|
67
|
+
sky.hemi.groundColor.copy(sky.groundTint).multiplyScalar(0.4 + 0.6 * Math.min(1, hemiI));
|
|
68
|
+
}
|
|
69
|
+
sky.engine.look.lights.applyTop(); // a game look.lights.set({ sun: { intensity } }) wins over the keyframes (look/part.js)
|
|
70
|
+
const ex = sky.engine.exposureDriver;
|
|
71
|
+
if (ex) sky.engine.renderer.toneMappingExposure = ex(sky); // style.exposure keyframes / function (engine.look.tone.set)
|
|
72
|
+
|
|
73
|
+
if (sky.skybox) sky.engine.scene.backgroundIntensity = Math.max(0.05, hemiI * M.ambient / 1.2);
|
|
74
|
+
if (sky.csm) cascadeFrame(sky, lightDir); // cascades place their own lights from the camera (cascades.js)
|
|
75
|
+
else if (sky._drive.sun !== false) sky._placeShadow(focus, lightDir);
|
|
76
|
+
for (const o of sky._owned) o.mark();
|
|
77
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Sky modifier layers (mixed into Sky.prototype): weather and games each write their own layer with modify(); every frame the sky
|
|
2
|
+
// composites them into sky.modifiers and applies the result on top of its own colors and light. A layer may also lay a cover color
|
|
3
|
+
// (snow, ash, frost) over the foliage / ground season tint (applyCover).
|
|
4
|
+
import * as THREE from 'three';
|
|
5
|
+
import { checkOptions } from '../../core/validate.js';
|
|
6
|
+
|
|
7
|
+
const LAYER_KEYS = ['sun', 'ambient', 'fog', 'tint', 'tintAmount', 'horizonTintAmount', 'flash', 'foliageCover', 'groundCover', 'coverAmount'];
|
|
8
|
+
/** A layer that changes nothing (also the starting value of sky.modifiers) */
|
|
9
|
+
export const blankLayer = () => ({ sun: 1, ambient: 1, fog: 1, tint: new THREE.Color(0x888888), tintAmount: 0, horizonTintAmount: 0, flash: 0 });
|
|
10
|
+
|
|
11
|
+
export const layerMethods = {
|
|
12
|
+
/** Write a modifier layer (only the given fields); null removes it. See the JSDoc on Sky.modify */
|
|
13
|
+
modify(owner, values) {
|
|
14
|
+
if (values == null) { this._layers.delete(owner); this._checked.delete(owner); return this; }
|
|
15
|
+
// a per-frame writer (weather) reuses one object: its keys are checked once, not every frame
|
|
16
|
+
if (this._checked.get(owner) !== values) { checkOptions(`sky.modify('${owner}')`, values, LAYER_KEYS); this._checked.set(owner, values); }
|
|
17
|
+
let L = this._layers.get(owner);
|
|
18
|
+
if (!L) this._layers.set(owner, L = blankLayer());
|
|
19
|
+
for (const k in values) {
|
|
20
|
+
if (k === 'tint') L.tint.set(values.tint);
|
|
21
|
+
else if (k === 'foliageCover' || k === 'groundCover') L[k] = values[k] == null ? null : (L[k] ?? new THREE.Color()).set(values[k]);
|
|
22
|
+
else if (k in L || k === 'coverAmount') L[k] = values[k];
|
|
23
|
+
}
|
|
24
|
+
return this;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
// composite all layers -> modifiers: sun / ambient / fog multiply; tint blends weighted by tintAmount; flash takes the max
|
|
28
|
+
_combine() {
|
|
29
|
+
const M = this.modifiers;
|
|
30
|
+
M.sun = M.ambient = M.fog = 1; M.flash = 0;
|
|
31
|
+
let r = 0, g = 0, b = 0, w = 0, keep = 1, keepH = 1;
|
|
32
|
+
for (const L of this._layers.values()) {
|
|
33
|
+
M.sun *= L.sun; M.ambient *= L.ambient; M.fog *= L.fog; M.flash = Math.max(M.flash, L.flash);
|
|
34
|
+
const a = Math.max(L.tintAmount, L.horizonTintAmount);
|
|
35
|
+
r += L.tint.r * a; g += L.tint.g * a; b += L.tint.b * a; w += a;
|
|
36
|
+
keep *= 1 - L.tintAmount; keepH *= 1 - L.horizonTintAmount;
|
|
37
|
+
}
|
|
38
|
+
if (w > 0) M.tint.setRGB(r / w, g / w, b / w);
|
|
39
|
+
M.tintAmount = 1 - keep; M.horizonTintAmount = 1 - keepH;
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const tmp = new THREE.Color(), acc = new THREE.Color();
|
|
44
|
+
/**
|
|
45
|
+
* Lay the layers' cover colors over the season tint uniforms (after applySeason), reusing the season patch instead of a second
|
|
46
|
+
* shader pass. Nothing is touched while no layer covers, so the default look and a game that writes the tint uniforms itself keep
|
|
47
|
+
* working. The patch draws mix(color, tint, m) (multiply when m is 0); a cover of amount a over mix m1 is again one mix:
|
|
48
|
+
* m = 1 - (1 - m1)(1 - a), tint = (T1 m1 (1 - a) + cover a) / m. A season in 'multiply' mode (m1 = 0) gives way to the cover.
|
|
49
|
+
*/
|
|
50
|
+
export function applyCover(sky, U) {
|
|
51
|
+
let on = false;
|
|
52
|
+
// no calendar seasons: nothing rewrites the tint every frame, so start from neutral (and return to it once the cover is gone)
|
|
53
|
+
const calendar = sky.seasonStyle && sky.seasons.length;
|
|
54
|
+
for (const [part, T, mix] of [['foliageCover', U.uFoliageTint, U.uFoliageMix], ['groundCover', U.uGroundTint, U.uGroundMix]]) {
|
|
55
|
+
let a = 0, keep = 1; acc.setRGB(0, 0, 0);
|
|
56
|
+
for (const L of sky._layers.values()) {
|
|
57
|
+
const c = L[part], k = c ? Math.min(1, Math.max(0, L.coverAmount ?? 0)) : 0;
|
|
58
|
+
if (k > 0) { acc.add(tmp.copy(c).multiplyScalar(k)); a += k; keep *= 1 - k; }
|
|
59
|
+
}
|
|
60
|
+
if (!calendar && (a > 0 || sky._covered)) { T.value.setRGB(1, 1, 1); mix.value = 0; }
|
|
61
|
+
if (a <= 0) continue;
|
|
62
|
+
on = true;
|
|
63
|
+
const amt = 1 - keep, m1 = mix.value, m = 1 - (1 - m1) * (1 - amt);
|
|
64
|
+
acc.multiplyScalar(amt / a); // the layers' weighted cover color, times the combined amount
|
|
65
|
+
T.value.multiplyScalar(m1 * (1 - amt)).add(acc).multiplyScalar(1 / m);
|
|
66
|
+
mix.value = m;
|
|
67
|
+
}
|
|
68
|
+
sky._covered = on;
|
|
69
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Sky look setup (helper of Sky.js): which engine lights the sky drives, the fog model, extra dome layers.
|
|
2
|
+
// Light modes, sky() options only (whether the engine's two lights exist is how the game lights its world, not a swappable look):
|
|
3
|
+
// sky({ sun: true }) default: the sky drives engine.lights.sun (color, intensity, direction, shadow box) from the keyframes
|
|
4
|
+
// sky({ sun: 'manual' }) the sky still aims it along the time of day and moves the shadow box; color and intensity are the game's
|
|
5
|
+
// sky({ sun: false }) hidden (visible = false, no shadows) and never touched: light the world with your own three.js lights
|
|
6
|
+
// sky({ hemisphere: true | 'manual' | false }) the same for engine.lights.hemi (ambient): 'manual' leaves its colors and intensity alone
|
|
7
|
+
// sky({ fog: true | 'manual' | false }) scene.fog: true (default) the sky creates it and writes color and distance every frame |
|
|
8
|
+
// 'manual' created once, then never written (the game sets color / near / far / its own object) |
|
|
9
|
+
// false scene.fog = null and never touched. An object here is style.sky.fog settings (= true)
|
|
10
|
+
// Hidden lights are left in the scene (visible = false): three skips them, and toggling visible later recompiles like adding a light would.
|
|
11
|
+
import * as THREE from 'three';
|
|
12
|
+
import { resolveFog, domeFog } from './fog.js';
|
|
13
|
+
import { resolveDomeLayers } from './domeLayers.js';
|
|
14
|
+
|
|
15
|
+
const MODES = [true, false, 'manual'];
|
|
16
|
+
const modeOf = (name, v = true) => {
|
|
17
|
+
if (!MODES.includes(v)) throw new Error(`sky({ ${name} }) must be true (the sky drives it), 'manual' (the game sets its color and intensity) or false (hidden, light the world yourself); got ${JSON.stringify(v)}`);
|
|
18
|
+
return v;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Resolve light modes, fog and dome layers for a new Sky; returns the extra dome uniforms and GLSL ({ uniforms, decl, body }) */
|
|
22
|
+
export function prepareLook(sky, st, over) {
|
|
23
|
+
/** @internal which engine lights the sky writes each frame: true / 'manual' / false */
|
|
24
|
+
// fog is also the look override (an object of style.sky.fog settings, which means true); a boolean or 'manual' picks the mode and keeps the style's settings
|
|
25
|
+
const fogObj = over.fog != null && typeof over.fog === 'object';
|
|
26
|
+
sky._drive = { sun: modeOf('sun', over.sun), hemi: modeOf('hemisphere', over.hemisphere), fog: fogObj ? true : modeOf('fog', over.fog) };
|
|
27
|
+
const { sun, hemi } = sky.engine.lights;
|
|
28
|
+
if (sky._drive.sun === false) { sun.visible = false; sun.castShadow = false; }
|
|
29
|
+
if (sky._drive.hemi === false) hemi.visible = false;
|
|
30
|
+
/** @internal resolved fog settings (fog.js) */
|
|
31
|
+
sky._fogSpec = fogObj || over.fog == null ? st.fog : sky.engine.style.sky?.fog; // look.fog's style layer (lookFog.js)
|
|
32
|
+
sky._fog = resolveFog(sky._fogSpec);
|
|
33
|
+
sky._backdrop = resolveBackdrop(st);
|
|
34
|
+
const f = domeFog(sky._fog), l = resolveDomeLayers(st.layers);
|
|
35
|
+
const uniforms = { ...l.uniforms };
|
|
36
|
+
// the fog object is created after the dome, and its color is rewritten each frame: read it at draw time
|
|
37
|
+
if (f.decl) uniforms.uFogColor = { get value() { return sky.engine.scene.fog?.color ?? sky.uniforms.uHorizon.value; } }; // no fog object (fog: false): fade to the horizon
|
|
38
|
+
return { uniforms, decl: [f.decl, l.decl].filter(Boolean).join('\n'), body: f.body + l.body };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// style.sky.backdrop: one flat color (or (sky) => color each frame) instead of the dome -- no gradient, horizon, sun / moon disk,
|
|
42
|
+
// stars or clouds. The color goes straight to scene.background (no tone mapping), so what is written is what shows; the keyframes
|
|
43
|
+
// still light the world, and fog in its default 'horizon' color follows the backdrop so far things fade into it.
|
|
44
|
+
export function resolveBackdrop(st) {
|
|
45
|
+
const v = st.backdrop;
|
|
46
|
+
if (v == null) return null;
|
|
47
|
+
const color = typeof v === 'number' || typeof v === 'string' || v.isColor;
|
|
48
|
+
if (!color && typeof v !== 'function') throw new Error(`style.sky.backdrop must be a color (0xefebe4, '#efebe4') or (sky) => color; got ${JSON.stringify(v)}`);
|
|
49
|
+
if (st.skybox) throw new Error('style.sky: backdrop and skybox both replace the dome; keep one (backdrop for a flat color, skybox for a panorama)');
|
|
50
|
+
return { fn: color ? null : v, color: new THREE.Color(color ? v : 0x000000) };
|
|
51
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// look.fog: the sky's fog as a look part. set({ kind, color, density, near, far, height, falloff, enabled }), patch('fog', glsl), replace('manual' | THREE.Fog).
|
|
2
|
+
// The sky still writes color and distance every frame (updateFog in fog.js) from the resolved values, so a game set() simply changes what it writes.
|
|
3
|
+
// What cannot change after the first frame: kind 'exp' / 'height', height, falloff and the 'fog' hook replace three's shared fog_* shader chunks, and
|
|
4
|
+
// three compiles each material once (fog.js explains). Those are accepted before the first frame (style, sky({ fog }), or right after createEngine) and
|
|
5
|
+
// throw afterwards, naming the way out. kind linear <-> exp2, color, density, near, far, enabled work live. The dome's horizon band (a fog-colored fade
|
|
6
|
+
// so the horizon line hides) is part of the dome shader and exists only when the start color was not 'horizon'.
|
|
7
|
+
import { defineLookPart, REPLACE } from '../../core/look/part.js';
|
|
8
|
+
import { resolveFog, makeFog, applyFogChunks, FOG_KINDS } from './fog.js';
|
|
9
|
+
|
|
10
|
+
const LATE = "look.fog: {what} changes three.js's shared fog shader chunks, which are compiled once per material, so it can only be set before the first frame " +
|
|
11
|
+
"(style.sky.fog, sky({ fog }), or right after createEngine). Live now: color, density, near, far, enabled, and kind 'linear' <-> 'exp2'";
|
|
12
|
+
const chunkKey = f => (f.glsl ? JSON.stringify([f.kind, f.height, f.falloff, f.glsl]) : '');
|
|
13
|
+
|
|
14
|
+
/** @param {any} sky @param {object} spec the style.sky.fog settings in use (look.js keeps them in sky._fogSpec) */
|
|
15
|
+
export function defineFogLook(sky, spec) {
|
|
16
|
+
const e = sky.engine, scene = e.scene;
|
|
17
|
+
const rendered = () => (e.renderer?.info?.render?.frame ?? 0) > 0;
|
|
18
|
+
let part;
|
|
19
|
+
sky._fogHeld = false;
|
|
20
|
+
sky._fogKey = chunkKey(sky._fog);
|
|
21
|
+
const late = (key, what) => v => (rendered() && v !== part?.value(key) && (sky._fogKey || (key === 'kind' && (v === 'exp' || v === 'height'))) ? LATE.replace('{what}', what) : null);
|
|
22
|
+
|
|
23
|
+
const rebuild = () => {
|
|
24
|
+
const v = part.values(), glsl = part.value('hook:fog');
|
|
25
|
+
const f = resolveFog({ kind: v.kind, color: v.color, density: v.density ?? undefined, height: v.height, falloff: v.falloff, near: v.near ?? undefined,
|
|
26
|
+
far: v.far ?? undefined, enabled: v.enabled, glsl: typeof glsl === 'string' ? glsl : undefined });
|
|
27
|
+
const key = chunkKey(f);
|
|
28
|
+
if (key !== sky._fogKey) {
|
|
29
|
+
if (rendered()) throw new Error(LATE.replace('{what}', 'this fog change'));
|
|
30
|
+
sky._fogOff();
|
|
31
|
+
try { sky._fogOff = applyFogChunks(f); } catch (err) { sky._fogOff = applyFogChunks(sky._fog); throw err; }
|
|
32
|
+
sky._fogKey = key;
|
|
33
|
+
}
|
|
34
|
+
sky._fog = f;
|
|
35
|
+
// linear is three's Fog, every other kind FogExp2: swap the object (materials recompile for it) when the kind crossed that line
|
|
36
|
+
const cur = scene.fog;
|
|
37
|
+
if (sky._drive.fog === true && !sky._fogHeld && cur && (f.kind === 'linear' ? !cur.isFog : !cur.isFogExp2)) {
|
|
38
|
+
scene.fog = makeFog(f, f.far ?? e.fogFar); scene.fog.color.copy(cur.color);
|
|
39
|
+
for (const o of sky._owned ?? []) o.mark();
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
part = defineLookPart(e, {
|
|
44
|
+
id: 'fog', group: 'atmosphere', module: 'sky', object: () => scene.fog, applyAtStart: false, docs: ['recipes/52-look.md', 'recipes/38-sky.md'],
|
|
45
|
+
params: {
|
|
46
|
+
enabled: { type: 'bool', default: true, cost: 'uniform', desc: 'fog on / off without a recompile',
|
|
47
|
+
why: 'distant things melt into the sky and the edge of a streamed world is hidden', off: 'look.fog.set({ enabled: false }), or sky({ fog: false })' },
|
|
48
|
+
kind: { type: 'string', default: 'linear', values: FOG_KINDS, cost: 'shader', desc: "'linear' | 'exp' | 'exp2' | 'height'", neutral: true,
|
|
49
|
+
check: late('kind', "kind 'exp' / 'height'") },
|
|
50
|
+
color: { type: 'any', default: 'horizon', cost: 'uniform', desc: "'horizon' (the dome's horizon color, written every frame) | a color | (sky) => color", neutral: true },
|
|
51
|
+
density: { type: 'any', default: null, cost: 'uniform', neutral: true, desc: 'per meter for exp / exp2 / height; null = derived so the fog reaches the same distance as linear' },
|
|
52
|
+
far: { type: 'any', default: null, cost: 'uniform', desc: 'fog distance in meters (weather scales it); null = engine.fogFar (createEngine, terrain view range)',
|
|
53
|
+
why: 'fog reaches full strength at engine.fogFar (600 by default), starting at 0.3 x that', off: 'look.fog.set({ enabled: false })' },
|
|
54
|
+
near: { type: 'any', default: null, cost: 'uniform', neutral: true, desc: 'linear only: where the fog starts; null = 0.3 x far' },
|
|
55
|
+
height: { type: 'number', default: 0, neutral: true, cost: 'shader', desc: "kind 'height': the fog's floor (world y)", check: late('height', 'height') },
|
|
56
|
+
falloff: { type: 'number', default: 0.05, min: 0, neutral: true, cost: 'shader', desc: "kind 'height': how fast it thins above the floor (per meter)", check: late('falloff', 'falloff') },
|
|
57
|
+
},
|
|
58
|
+
style: () => {
|
|
59
|
+
const s = spec ?? {};
|
|
60
|
+
return { ...Object.fromEntries(['enabled', 'kind', 'color', 'density', 'far', 'near', 'height', 'falloff'].filter(k => s[k] != null).map(k => [k, s[k]])),
|
|
61
|
+
...(s.glsl ? { 'hook:fog': s.glsl } : {}) };
|
|
62
|
+
},
|
|
63
|
+
apply() { rebuild(); },
|
|
64
|
+
hooks: { fog: { signature: 'vec4 threedogFog(vec3 color, float depth, vec3 worldPos)', scope: ['fogColor', 'cameraPosition', 'fogNear / fogFar (linear)', 'fogDensity (other kinds)', 'THREEDOG_FOG_HEIGHT', 'THREEDOG_FOG_FALLOFF'],
|
|
65
|
+
example: 'vec4 threedogFog(vec3 color, float depth, vec3 worldPos){ return vec4(color, 1.0 - exp(-fogDensity * depth)); }' } },
|
|
66
|
+
applyHook(name, glsl) {
|
|
67
|
+
rebuild();
|
|
68
|
+
},
|
|
69
|
+
// wrong hook GLSL or replace value stops here, before any layer is written
|
|
70
|
+
validate: (k, v) => k === 'hook:fog' ? (typeof v === 'string' && /vec4\s+threedogFog\s*\(/.test(v) ? null : 'needs GLSL that defines `vec4 threedogFog(vec3 color, float depth, vec3 worldPos)` returning (fog rgb, amount 0..1)')
|
|
71
|
+
: k === REPLACE && v !== 'manual' && !v?.isFog && !v?.isFogExp2 ? "is 'manual' or a THREE.Fog / THREE.FogExp2; null gives the fog back to the sky" : null,
|
|
72
|
+
replace: { accepts: "'manual' (the sky stops writing scene.fog) | a THREE.Fog / FogExp2 (becomes scene.fog)", loses: ['weather fog distance (sky.modify fog)', 'the sky-colored horizon fade'] },
|
|
73
|
+
applyReplace(impl) {
|
|
74
|
+
sky._fogHeld = impl != null;
|
|
75
|
+
if (impl?.isFog || impl?.isFogExp2) scene.fog = impl;
|
|
76
|
+
else if (impl == null && sky._drive.fog === true) scene.fog = makeFog(sky._fog, sky._fog.far ?? e.fogFar);
|
|
77
|
+
for (const o of sky._owned ?? []) o.mark(); // the dev guard re-reads the fog it watches
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// look.shadow: the sun's shadow as a look part (set only: the csm patch is internal, and a game that wants its own shadows lights with its own lights).
|
|
2
|
+
// Why a part: the type / cascades / bias a game set were invisible to describe(); now every key says who set it.
|
|
3
|
+
// the work itself is Sky._applyShadow (shadow.js).
|
|
4
|
+
import { defineLookPart } from '../../core/look/part.js';
|
|
5
|
+
import { SHADOW_TYPES } from '../../core/shadowTypes.js';
|
|
6
|
+
|
|
7
|
+
const SPLITS = ['practical', 'uniform', 'logarithmic'];
|
|
8
|
+
const KEYS = ['bias', 'normalBias', 'updateEvery', 'snap', 'ahead', 'type', 'radius', 'blurSamples', 'cascades', 'maxFar', 'fade', 'lightMargin', 'split'];
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {any} sky @param {object} sh shadowFor(...) at start: becomes the style layer (style.sky.shadow, tier-adjusted, merged with sky({ shadow }))
|
|
12
|
+
*/
|
|
13
|
+
export function defineShadowLook(sky, sh) {
|
|
14
|
+
const e = sky.engine, sun = sky.sun;
|
|
15
|
+
const cost = 'shadow pass';
|
|
16
|
+
defineLookPart(e, {
|
|
17
|
+
id: 'shadow', group: 'atmosphere', module: 'sky', object: () => sun.shadow, docs: ['recipes/52-look.md', 'recipes/45-cascaded-shadows.md'],
|
|
18
|
+
params: {
|
|
19
|
+
enabled: { type: 'bool', default: e.shadows, cost, desc: 'master shadow switch (engine.setShadows)',
|
|
20
|
+
why: 'the sun casts shadows (createEngine({ shadows }) is the start value)', off: 'look.shadow.set({ enabled: false }), or createEngine({ shadows: false })' },
|
|
21
|
+
type: { type: 'string', default: e.shadowType, values: Object.keys(SHADOW_TYPES), neutral: true, cost, desc: "'basic' hard edge | 'pcf' | 'vsm' softest; switching recompiles materials, do it rarely" },
|
|
22
|
+
radius: { type: 'number', default: 1, min: 0, neutral: true, cost: 'uniform', desc: 'edge softness (pcf, vsm)' },
|
|
23
|
+
blurSamples: { type: 'int', default: 8, min: 1, neutral: true, cost, desc: 'blur quality (vsm only)' },
|
|
24
|
+
bias: { type: 'number', default: -0.0006, neutral: true, cost: 'uniform', desc: 'depth bias against acne' },
|
|
25
|
+
normalBias: { type: 'number', default: 0.04, neutral: true, cost: 'uniform', desc: 'normal offset against acne (meters)' },
|
|
26
|
+
mapSize: { type: 'int', default: 2048, min: 64, neutral: true, cost: 'gpu', desc: 'shadow map pixels (the nearest cascade with cascades)' },
|
|
27
|
+
updateEvery: { type: 'int', default: 1, min: 1, neutral: true, cost, desc: 'update the shadow map every N frames' },
|
|
28
|
+
snap: { type: 'any', default: 0.5, neutral: true, cost: 'uniform', desc: "shadow center snap grid in meters, or 'texel'" },
|
|
29
|
+
ahead: { type: 'number', default: 0, neutral: true, cost: 'uniform', desc: 'meters the shadow center is shifted in front of the view' },
|
|
30
|
+
cascades: { type: 'int', default: 1, neutral: true, cost, desc: '1 = one shadow map; 2-4 = cascaded shadows (recipes/45)',
|
|
31
|
+
check: v => (v < 1 || v > 4 ? `must be a whole number 1 (one shadow map, the default) to 4; got ${v}` : v > 1 && sky._drive.sun === false ? "cannot be above 1: cascades split the engine sun's shadow, and sky({ sun: false }) has no engine sun" : null) },
|
|
32
|
+
maxFar: { type: 'number', default: 120, min: 0.001, neutral: true, cost: 'uniform', desc: 'meters from the camera that cascades cover' },
|
|
33
|
+
fade: { type: 'bool', default: true, neutral: true, cost: 'uniform', desc: 'cascades blend into each other' },
|
|
34
|
+
lightMargin: { type: 'number', default: 100, neutral: true, cost: 'uniform', desc: 'meters the cascade lights sit behind their frustum' },
|
|
35
|
+
split: { type: 'any', default: 'practical', neutral: true, cost: 'uniform', desc: "'practical' | 'uniform' | 'logarithmic' | (count, near, far, out) => void",
|
|
36
|
+
check: v => (typeof v === 'function' || SPLITS.includes(v) ? null : `must be ${SPLITS.map(s => `'${s}'`).join(' | ')} or a function (count, near, far, out) => void; got ${JSON.stringify(v)}`) },
|
|
37
|
+
},
|
|
38
|
+
style: () => ({ ...Object.fromEntries(KEYS.filter(k => sh[k] !== undefined).map(k => [k, sh[k]])), ...(sh.mapSize !== undefined ? { mapSize: sh.mapSize } : {}) }),
|
|
39
|
+
read: () => ({ enabled: e.shadows, type: e.shadowType, radius: sun.shadow.radius, mapSize: sun.shadow.mapSize.x, bias: sun.shadow.bias, normalBias: sun.shadow.normalBias }),
|
|
40
|
+
apply(v, changed) {
|
|
41
|
+
const o = {};
|
|
42
|
+
for (const k of changed) if (KEYS.includes(k)) o[k] = v[k];
|
|
43
|
+
if (changed.has('enabled') && v.enabled !== e.shadows) e.setShadows(v.enabled);
|
|
44
|
+
if (changed.has('mapSize')) sky.setShadowMapSize(v.mapSize);
|
|
45
|
+
if (Object.keys(o).length) sky._applyShadow(o);
|
|
46
|
+
},
|
|
47
|
+
noPatch: "the cascade patch ('csm') is internal; change the shadows with look.shadow.set({ type, radius, cascades, ... })",
|
|
48
|
+
noReplace: 'to own the shadows, light the world with your own three.js lights: sky({ sun: false }) or createEngine({ lights: false })',
|
|
49
|
+
});
|
|
50
|
+
}
|