wallhammer-web 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +92 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,441 @@
|
|
|
1
|
+
// Scene environment: light, time of day, date, weather, wind and clouds. Holds two clocks (animation time and hour of day)
|
|
2
|
+
// and for every frame computes from them what the shaders need to know. The app only sets values and runs the clocks.
|
|
3
|
+
//
|
|
4
|
+
// Three lighting modes:
|
|
5
|
+
// - 'studio': the sun rotates with the camera, the scene is always lit from the left. The hour of day does not matter.
|
|
6
|
+
// - 'day': the sun follows its real path for the given place and date, light colours change with its height,
|
|
7
|
+
// the moon shines at night and the sky is blue.
|
|
8
|
+
// - 'fixed': the light stands in one world direction (`fixed.toLight`) with the colours and strengths given in
|
|
9
|
+
// `fixed`; neither the camera, the place nor the hour change it. Change `fixed` at any time.
|
|
10
|
+
//
|
|
11
|
+
// Weather (cloud cover, precipitation, snow, wetness, fog) and the date apply in both modes. Everything is a pure
|
|
12
|
+
// function of the clocks and the given numbers: the same values give the same frame. What content makes of it (foliage
|
|
13
|
+
// by season, lit openings at night, where snow settles) lives in environment slots (`EnvSlot`), not here.
|
|
14
|
+
import { Clock } from '../core/clock.js';
|
|
15
|
+
import { DEG, clamp, lerp, normalize, smoothstep } from '../base/math.js';
|
|
16
|
+
import { upFrame } from '../base/up.js';
|
|
17
|
+
import { skyDirection, sunPosition } from './sun.js';
|
|
18
|
+
/**
|
|
19
|
+
* Direction to a light that turns with the view: azimuth is measured from the "toward the viewer" direction to the left
|
|
20
|
+
* across the screen, 90° is exactly from the left, a smaller angle lights more from the front.
|
|
21
|
+
*/
|
|
22
|
+
export function studioLightDirection(heading, azimuthFromViewDeg, elevationDeg) {
|
|
23
|
+
// Screen right and the horizontal direction away from the viewer for a view with this heading.
|
|
24
|
+
const right = [Math.cos(heading), 0, -Math.sin(heading)];
|
|
25
|
+
const g = [-Math.sin(heading), 0, -Math.cos(heading)];
|
|
26
|
+
const a = azimuthFromViewDeg * DEG;
|
|
27
|
+
const e = elevationDeg * DEG;
|
|
28
|
+
const h = Math.cos(e);
|
|
29
|
+
return normalize([h * (-right[0] * Math.sin(a) - g[0] * Math.cos(a)), Math.sin(e), h * (-right[2] * Math.sin(a) - g[2] * Math.cos(a))]);
|
|
30
|
+
}
|
|
31
|
+
export const DEFAULT_STUDIO = {
|
|
32
|
+
sunElevationDeg: 44,
|
|
33
|
+
sunAzimuthFromViewDeg: 80,
|
|
34
|
+
sun: [1.4, 1.29, 0.87],
|
|
35
|
+
ambient: [0.3, 0.3, 0.43],
|
|
36
|
+
skyFill: [0.1, 0.11, 0.14],
|
|
37
|
+
bounce: [0.05, 0.04, 0.02],
|
|
38
|
+
warmTint: [1.02, 1.0, 0.93],
|
|
39
|
+
exposure: 1.0,
|
|
40
|
+
};
|
|
41
|
+
/** The 'fixed' light: the studio colours, from the south-west, 44° high. */
|
|
42
|
+
export const DEFAULT_FIXED = {
|
|
43
|
+
toLight: [-0.5, 0.7, 0.5],
|
|
44
|
+
sun: DEFAULT_STUDIO.sun,
|
|
45
|
+
ambient: DEFAULT_STUDIO.ambient,
|
|
46
|
+
skyFill: DEFAULT_STUDIO.skyFill,
|
|
47
|
+
bounce: DEFAULT_STUDIO.bounce,
|
|
48
|
+
warmTint: DEFAULT_STUDIO.warmTint,
|
|
49
|
+
exposure: DEFAULT_STUDIO.exposure,
|
|
50
|
+
backdrop: [1, 1, 1],
|
|
51
|
+
};
|
|
52
|
+
export const DEFAULT_SITE = { latitudeDeg: 50, longitudeDeg: 15, dayOfYear: 172, utcOffsetHours: 2 };
|
|
53
|
+
export const DEFAULT_DAY = {
|
|
54
|
+
sunHigh: [1.4, 1.29, 0.87],
|
|
55
|
+
sunLow: [1.5, 0.74, 0.32],
|
|
56
|
+
ambientDay: [0.3, 0.3, 0.43],
|
|
57
|
+
ambientNight: [0.15, 0.18, 0.27],
|
|
58
|
+
twilight: [0.09, 0.035, 0.06],
|
|
59
|
+
lowSunFill: 2.5,
|
|
60
|
+
lowSunExposure: 0.15,
|
|
61
|
+
skyDay: [0.1, 0.11, 0.14],
|
|
62
|
+
skyNight: [0.05, 0.065, 0.11],
|
|
63
|
+
bounce: [0.05, 0.04, 0.02],
|
|
64
|
+
tintDay: [1.02, 1.0, 0.93],
|
|
65
|
+
tintNight: [0.88, 0.95, 1.1],
|
|
66
|
+
moon: [0.17, 0.22, 0.35],
|
|
67
|
+
moonAzimuthDeg: 135,
|
|
68
|
+
moonElevationDeg: 40,
|
|
69
|
+
backdropNight: [0.2, 0.25, 0.4],
|
|
70
|
+
exposureNight: 1.45,
|
|
71
|
+
minLightElevationDeg: 3,
|
|
72
|
+
skyZenithDay: [0.25, 0.45, 0.74],
|
|
73
|
+
skyHorizonDay: [0.66, 0.77, 0.86],
|
|
74
|
+
skyZenithNight: [0.02, 0.035, 0.08],
|
|
75
|
+
skyHorizonNight: [0.08, 0.11, 0.19],
|
|
76
|
+
skyTwilight: [0.42, 0.16, 0.05],
|
|
77
|
+
sunGlowHigh: [0.2, 0.18, 0.12],
|
|
78
|
+
sunGlowLow: [0.62, 0.3, 0.08],
|
|
79
|
+
};
|
|
80
|
+
export const DEFAULT_WIND = { fromDeg: 270, speedMs: 3 };
|
|
81
|
+
export const DEFAULT_CLOUDS = { darkness: 0.3, sizeM: 320, speedFactor: 1.5 };
|
|
82
|
+
export const DEFAULT_WEATHER = { cloudCover: 0, precipitation: 0, precipitationKind: 'rain', snowCover: 0, wetness: 0, fog: 0 };
|
|
83
|
+
export const DEFAULT_SNOW_ALBEDO = [0.66, 0.65, 0.62];
|
|
84
|
+
/** Gust length in metres: the distance between waves travelling across the landscape with the wind. */
|
|
85
|
+
const GUST_LENGTH_M = 70;
|
|
86
|
+
// --- Transitions by sun height (degrees) ------------------------------------------------------------
|
|
87
|
+
/** Day and night: ambient light, sky, tint. */
|
|
88
|
+
const DAY_FROM = -6;
|
|
89
|
+
const DAY_TO = 6;
|
|
90
|
+
/** The sun gains strength from the moment it is just below the horizon. */
|
|
91
|
+
const SUN_FROM = -1.5;
|
|
92
|
+
const SUN_TO = 9;
|
|
93
|
+
/** Sun colour: from low to high. */
|
|
94
|
+
const WARM_FROM = 2;
|
|
95
|
+
const WARM_TO = 30;
|
|
96
|
+
/** The moon shines only when the sun is deep below. Between SUN_FROM and MOON_TO nothing directional shines, the light direction is swapped there. */
|
|
97
|
+
const MOON_FROM = -9;
|
|
98
|
+
const MOON_TO = -3.5;
|
|
99
|
+
/** Below this sun height ambient light starts to be added, above it the day is like noon. */
|
|
100
|
+
const LOW_SUN_FROM = 3;
|
|
101
|
+
const LOW_SUN_TO = 36;
|
|
102
|
+
/** Range of sun heights in which brightness is compensated: fully below the first value, not at all above the second. */
|
|
103
|
+
const DIM_FROM = 10;
|
|
104
|
+
const DIM_TO = 55;
|
|
105
|
+
/** Twilight width for the colour addition. */
|
|
106
|
+
const TWILIGHT_WIDTH = 6;
|
|
107
|
+
// --- Cloud cover --------------------------------------------------------------------------------------------
|
|
108
|
+
/** From what cloud cover a continuous layer starts that dims the sun everywhere, and how much of it remains when overcast. */
|
|
109
|
+
const OVERCAST_FROM = 0.55;
|
|
110
|
+
const OVERCAST_SUN = 0.2;
|
|
111
|
+
/** Cloud shadows: noise threshold at cloud cover 0 and 1. Below OVERCAST they gradually merge and vanish. */
|
|
112
|
+
const CLOUD_EDGE_CLEAR = 0.92;
|
|
113
|
+
const CLOUD_EDGE_FULL = 0.12;
|
|
114
|
+
const CLOUD_EDGE_WIDTH = 0.18;
|
|
115
|
+
/** How much sunlight is scattered into the surroundings and sky when overcast and how much it cools. */
|
|
116
|
+
const OVERCAST_AMBIENT = 0.17;
|
|
117
|
+
const OVERCAST_SKY = 0.15;
|
|
118
|
+
const OVERCAST_COOL = [0.95, 0.98, 1.02];
|
|
119
|
+
const OVERCAST_TINT = [0.97, 0.99, 1.02];
|
|
120
|
+
/** Fog colour in screen space: light grey with a hint of blue by day, dark at night. */
|
|
121
|
+
const FOG_DAY = [0.63, 0.64, 0.64];
|
|
122
|
+
const FOG_NIGHT = [0.09, 0.11, 0.16];
|
|
123
|
+
const FOG_TWILIGHT = [0.14, 0.06, 0.04];
|
|
124
|
+
// --- Sky as seen ----------------------------------------------------------------------------------------------
|
|
125
|
+
/** Overcast sky brightness as a multiple of the clear horizon's brightness. */
|
|
126
|
+
const OVERCAST_SKY_GREY = 0.92;
|
|
127
|
+
/** How far fog pulls the horizon, the zenith and the sun glow toward the fog colour at fog 1. */
|
|
128
|
+
const SKY_FOG_HORIZON = 0.85;
|
|
129
|
+
const SKY_FOG_ZENITH = 0.55;
|
|
130
|
+
const SKY_FOG_GLOW = 0.6;
|
|
131
|
+
/** How much green and blue the horizon loses with the sun just above it, and up to what sun height (degrees). */
|
|
132
|
+
const HORIZON_WARM = [0, 0.14, 0.32];
|
|
133
|
+
const HORIZON_WARM_TO = 24;
|
|
134
|
+
/** Sun heights (degrees) between which the glow around the sun fades in: it outlives the sunset a little. */
|
|
135
|
+
const GLOW_FROM = -10;
|
|
136
|
+
const GLOW_TO = -1;
|
|
137
|
+
/** Rec. 601 brightness of a colour. */
|
|
138
|
+
function luma(c) {
|
|
139
|
+
return 0.3 * c[0] + 0.59 * c[1] + 0.11 * c[2];
|
|
140
|
+
}
|
|
141
|
+
function mix3(a, b, t) {
|
|
142
|
+
return [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)];
|
|
143
|
+
}
|
|
144
|
+
function scale3(a, s) {
|
|
145
|
+
return [a[0] * s, a[1] * s, a[2] * s];
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The cloud terms `Environment.frame` derives from a cloud cover, for code that needs them per place: the noise
|
|
149
|
+
* thresholds of the shadow pattern, its darkness, the overcast share and the share of sunlight left by that overcast.
|
|
150
|
+
*/
|
|
151
|
+
export function cloudCoverTerms(cover, darkness) {
|
|
152
|
+
const overcast = smoothstep(OVERCAST_FROM, 1, cover);
|
|
153
|
+
const lo = lerp(CLOUD_EDGE_CLEAR, CLOUD_EDGE_FULL, clamp(cover / OVERCAST_FROM, 0, 1));
|
|
154
|
+
return {
|
|
155
|
+
lo,
|
|
156
|
+
hi: lo + CLOUD_EDGE_WIDTH,
|
|
157
|
+
darkness: cover > 0.01 ? darkness * (1 + 1.4 * smoothstep(0.1, OVERCAST_FROM, cover)) * (1 - overcast) : 0,
|
|
158
|
+
overcast,
|
|
159
|
+
sunShare: lerp(1, OVERCAST_SUN, overcast),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* GLSL twin of `cloudCoverTerms` for shaders that look the cover up per place (not exported from the package):
|
|
164
|
+
* `vec4 cloudCoverTerms(float cover, float darkness)` = (lo, hi, darkness, sun share).
|
|
165
|
+
*/
|
|
166
|
+
export const CLOUD_TERMS_GLSL = `
|
|
167
|
+
vec4 cloudCoverTerms(float cover, float darkness) {
|
|
168
|
+
float overcast = smoothstep(${OVERCAST_FROM.toFixed(2)}, 1.0, cover);
|
|
169
|
+
float lo = mix(${CLOUD_EDGE_CLEAR.toFixed(2)}, ${CLOUD_EDGE_FULL.toFixed(2)}, clamp(cover / ${OVERCAST_FROM.toFixed(2)}, 0.0, 1.0));
|
|
170
|
+
float d = cover > 0.01 ? darkness * (1.0 + 1.4 * smoothstep(0.1, ${OVERCAST_FROM.toFixed(2)}, cover)) * (1.0 - overcast) : 0.0;
|
|
171
|
+
return vec4(lo, lo + ${CLOUD_EDGE_WIDTH.toFixed(2)}, d, mix(1.0, ${OVERCAST_SUN.toFixed(2)}, overcast));
|
|
172
|
+
}
|
|
173
|
+
float cloudOvercast(float cover) {
|
|
174
|
+
return smoothstep(${OVERCAST_FROM.toFixed(2)}, 1.0, cover);
|
|
175
|
+
}
|
|
176
|
+
`;
|
|
177
|
+
/** A direction given with y up (x east, z south) as a world direction where up is `up`. */
|
|
178
|
+
function inUpFrame(up, d) {
|
|
179
|
+
const f = upFrame(up);
|
|
180
|
+
return [f.x[0] * d[0] + f.y[0] * d[1] + f.z[0] * d[2], f.x[1] * d[0] + f.y[1] * d[1] + f.z[1] * d[2], f.x[2] * d[0] + f.y[2] * d[1] + f.z[2] * d[2]];
|
|
181
|
+
}
|
|
182
|
+
/** `dir` lifted to at least `minDeg` above the horizon of `up`, keeping its compass direction. `e` is its height in degrees. */
|
|
183
|
+
function liftedAbove(up, dir, e, minDeg) {
|
|
184
|
+
if (e >= minDeg)
|
|
185
|
+
return dir;
|
|
186
|
+
const sinE = Math.sin(e * DEG);
|
|
187
|
+
const flat = normalize([dir[0] - up[0] * sinE, dir[1] - up[1] * sinE, dir[2] - up[2] * sinE]);
|
|
188
|
+
const c = Math.cos(minDeg * DEG);
|
|
189
|
+
const s = Math.sin(minDeg * DEG);
|
|
190
|
+
return [flat[0] * c + up[0] * s, flat[1] * c + up[1] * s, flat[2] * c + up[2] * s];
|
|
191
|
+
}
|
|
192
|
+
export class Environment {
|
|
193
|
+
mode;
|
|
194
|
+
studio;
|
|
195
|
+
/** The 'fixed' mode's light; its fields may be changed (or the object replaced) at any time. */
|
|
196
|
+
fixed;
|
|
197
|
+
site;
|
|
198
|
+
dayLight;
|
|
199
|
+
wind;
|
|
200
|
+
clouds;
|
|
201
|
+
/** Weather: changed directly, applies from the next frame. */
|
|
202
|
+
weather;
|
|
203
|
+
snowAlbedo;
|
|
204
|
+
/** Vertical extent of the fog layer; set when the scene is known. */
|
|
205
|
+
fogLayer;
|
|
206
|
+
/**
|
|
207
|
+
* In 'day' mode, for a world whose up depends on the place (default null: the sun follows `site` and up is +y).
|
|
208
|
+
* When set, the sun stands in the world direction `sunDir` whatever the site says, and everything that follows its
|
|
209
|
+
* height (light colours, exposure, sky colours, the swap to the moon) is computed for an observer whose up is `up`.
|
|
210
|
+
* Light directions of the frame are then world directions. Set it every frame from the eye's place.
|
|
211
|
+
*/
|
|
212
|
+
observer = null;
|
|
213
|
+
/** Animation time in seconds. Drives wind and clouds, later everything else that moves too. */
|
|
214
|
+
time;
|
|
215
|
+
/** Hour of day 0–24 in civil time. Speed is in hours per second. */
|
|
216
|
+
day;
|
|
217
|
+
constructor(config = {}) {
|
|
218
|
+
this.mode = config.mode ?? 'studio';
|
|
219
|
+
this.studio = { ...DEFAULT_STUDIO, ...config.studio };
|
|
220
|
+
this.fixed = { ...DEFAULT_FIXED, ...config.fixed };
|
|
221
|
+
this.site = { ...DEFAULT_SITE, ...config.site };
|
|
222
|
+
this.dayLight = { ...DEFAULT_DAY, ...config.day };
|
|
223
|
+
this.wind = { ...DEFAULT_WIND, ...config.wind };
|
|
224
|
+
this.clouds = { ...DEFAULT_CLOUDS, ...config.clouds };
|
|
225
|
+
this.weather = { ...DEFAULT_WEATHER, ...config.weather };
|
|
226
|
+
this.snowAlbedo = config.snowAlbedo ?? DEFAULT_SNOW_ALBEDO;
|
|
227
|
+
this.fogLayer = config.fogLayer ?? { bottomY: 0, topY: 1 };
|
|
228
|
+
this.time = new Clock({ playing: config.animate ?? true });
|
|
229
|
+
this.day = new Clock({ time: config.hour ?? 13, rate: config.hoursPerSecond ?? 1, wrap: 24 });
|
|
230
|
+
}
|
|
231
|
+
/** Clock for the frame loop. */
|
|
232
|
+
get clocks() {
|
|
233
|
+
return [this.time, this.day];
|
|
234
|
+
}
|
|
235
|
+
/** Switches the lighting mode. In studio and fixed the hour of day means nothing, so its run is stopped. */
|
|
236
|
+
setMode(mode) {
|
|
237
|
+
this.mode = mode;
|
|
238
|
+
if (mode !== 'day')
|
|
239
|
+
this.day.pause();
|
|
240
|
+
}
|
|
241
|
+
/** Switches to the 'fixed' mode with the light toward `toLight` (world axes) and, optionally, other numbers of it. */
|
|
242
|
+
setFixedLight(toLight, light = {}) {
|
|
243
|
+
this.fixed = { ...this.fixed, ...light, toLight: [toLight[0], toLight[1], toLight[2]] };
|
|
244
|
+
this.setMode('fixed');
|
|
245
|
+
}
|
|
246
|
+
/** Day of year (1–366). Drives the sun path; slots may read it too. */
|
|
247
|
+
get dayOfYear() {
|
|
248
|
+
return this.site.dayOfYear;
|
|
249
|
+
}
|
|
250
|
+
/** Sets the date. Optionally also the local time offset from UTC (daylight saving time). */
|
|
251
|
+
setDate(dayOfYear, utcOffsetHours) {
|
|
252
|
+
this.site = { ...this.site, dayOfYear, ...(utcOffsetHours === undefined ? {} : { utcOffsetHours }) };
|
|
253
|
+
}
|
|
254
|
+
/** Where the sun is now according to place, date and hour of day. */
|
|
255
|
+
sun() {
|
|
256
|
+
return sunPosition(this.site, this.day.time);
|
|
257
|
+
}
|
|
258
|
+
/** Environment state for one frame. Pure function of the clocks, settings and (in studio) the view heading. */
|
|
259
|
+
frame(view) {
|
|
260
|
+
const time = this.time.time;
|
|
261
|
+
const hour = this.day.time;
|
|
262
|
+
// Direction the wind blows toward: the meteorological direction is where it comes from.
|
|
263
|
+
const to = (this.wind.fromDeg + 180) * DEG;
|
|
264
|
+
const windX = Math.sin(to);
|
|
265
|
+
const windZ = -Math.cos(to);
|
|
266
|
+
const drift = this.wind.speedMs * this.clouds.speedFactor * time;
|
|
267
|
+
const w = this.weather;
|
|
268
|
+
const cc = clamp(w.cloudCover, 0, 1);
|
|
269
|
+
const overcast = smoothstep(OVERCAST_FROM, 1, cc);
|
|
270
|
+
const cloudLo = lerp(CLOUD_EDGE_CLEAR, CLOUD_EDGE_FULL, clamp(cc / OVERCAST_FROM, 0, 1));
|
|
271
|
+
const shared = {
|
|
272
|
+
time,
|
|
273
|
+
hour,
|
|
274
|
+
windX,
|
|
275
|
+
windZ,
|
|
276
|
+
windSpeedMs: this.wind.speedMs,
|
|
277
|
+
gustLengthM: GUST_LENGTH_M,
|
|
278
|
+
gustSpeed: Math.max(0.5, this.wind.speedMs),
|
|
279
|
+
cloudX: windX * drift,
|
|
280
|
+
cloudZ: windZ * drift,
|
|
281
|
+
// Without clouds there are no cloud shadows. When overcast they merge into a continuous layer and their pattern vanishes.
|
|
282
|
+
cloudDarkness: cc > 0.01 ? this.clouds.darkness * (1 + 1.4 * smoothstep(0.1, OVERCAST_FROM, cc)) * (1 - overcast) : 0,
|
|
283
|
+
cloudSizeM: this.clouds.sizeM,
|
|
284
|
+
cloudLo,
|
|
285
|
+
cloudHi: cloudLo + CLOUD_EDGE_WIDTH,
|
|
286
|
+
cloudCover: cc,
|
|
287
|
+
snowCover: clamp(w.snowCover, 0, 1),
|
|
288
|
+
wetness: clamp(w.wetness, 0, 1),
|
|
289
|
+
fog: clamp(w.fog, 0, 1),
|
|
290
|
+
precipitation: clamp(w.precipitation, 0, 1),
|
|
291
|
+
precipitationKind: w.precipitationKind === 'snow' ? 1 : 0,
|
|
292
|
+
snowAlbedo: this.snowAlbedo,
|
|
293
|
+
fogLayer: this.fogLayer,
|
|
294
|
+
dayOfYear: this.site.dayOfYear,
|
|
295
|
+
};
|
|
296
|
+
return this.applySkyFog(this.applyClouds(this.lightFrame(view.heading, shared), overcast));
|
|
297
|
+
}
|
|
298
|
+
/** Fog and haze veil the sky too: the horizon goes almost to the fog colour, the zenith less. */
|
|
299
|
+
applySkyFog(f) {
|
|
300
|
+
if (f.fog <= 0)
|
|
301
|
+
return f;
|
|
302
|
+
return {
|
|
303
|
+
...f,
|
|
304
|
+
skyHorizon: mix3(f.skyHorizon, f.fogColor, SKY_FOG_HORIZON * f.fog),
|
|
305
|
+
skyZenith: mix3(f.skyZenith, f.fogColor, SKY_FOG_ZENITH * f.fog),
|
|
306
|
+
sunGlow: scale3(f.sunGlow, 1 - SKY_FOG_GLOW * f.fog),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Continuous cloud cover: direct sun weakens, its light is scattered into the surroundings and sky, colours cool.
|
|
311
|
+
* Scene shadows fade by themselves, since only direct sun makes them.
|
|
312
|
+
*/
|
|
313
|
+
applyClouds(f, overcast) {
|
|
314
|
+
if (overcast <= 0)
|
|
315
|
+
return f;
|
|
316
|
+
const sunLum = (f.sun[0] + f.sun[1] + f.sun[2]) / 3;
|
|
317
|
+
const diffuse = sunLum * overcast;
|
|
318
|
+
return {
|
|
319
|
+
...f,
|
|
320
|
+
sun: scale3(f.sun, lerp(1, OVERCAST_SUN, overcast)),
|
|
321
|
+
ambient: [
|
|
322
|
+
f.ambient[0] + diffuse * OVERCAST_AMBIENT * OVERCAST_COOL[0],
|
|
323
|
+
f.ambient[1] + diffuse * OVERCAST_AMBIENT * OVERCAST_COOL[1],
|
|
324
|
+
f.ambient[2] + diffuse * OVERCAST_AMBIENT * OVERCAST_COOL[2],
|
|
325
|
+
],
|
|
326
|
+
skyFill: [
|
|
327
|
+
f.skyFill[0] + diffuse * OVERCAST_SKY * OVERCAST_COOL[0],
|
|
328
|
+
f.skyFill[1] + diffuse * OVERCAST_SKY * OVERCAST_COOL[1],
|
|
329
|
+
f.skyFill[2] + diffuse * OVERCAST_SKY * OVERCAST_COOL[2],
|
|
330
|
+
],
|
|
331
|
+
bounce: scale3(f.bounce, 1 - overcast),
|
|
332
|
+
warmTint: mix3(f.warmTint, OVERCAST_TINT, overcast * 0.85),
|
|
333
|
+
fogColor: scale3(f.fogColor, 1 - 0.12 * overcast),
|
|
334
|
+
// The sky turns to an even grey as bright as its horizon was; the glow around the sun fades.
|
|
335
|
+
skyHorizon: mix3(f.skyHorizon, scale3(OVERCAST_COOL, luma(f.skyHorizon) * OVERCAST_SKY_GREY), overcast),
|
|
336
|
+
skyZenith: mix3(f.skyZenith, scale3(OVERCAST_COOL, luma(f.skyHorizon) * OVERCAST_SKY_GREY * 0.86), overcast),
|
|
337
|
+
sunGlow: scale3(f.sunGlow, 1 - 0.85 * overcast),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/** Light without the influence of cloud cover: fixed, studio, or day according to the sun position. */
|
|
341
|
+
lightFrame(heading, shared) {
|
|
342
|
+
const d = this.dayLight;
|
|
343
|
+
const obs = this.mode === 'day' ? this.observer : null;
|
|
344
|
+
const moonDir = obs ? inUpFrame(obs.up, skyDirection(d.moonAzimuthDeg, d.moonElevationDeg)) : skyDirection(d.moonAzimuthDeg, d.moonElevationDeg);
|
|
345
|
+
if (this.mode === 'fixed') {
|
|
346
|
+
const x = this.fixed;
|
|
347
|
+
const toSun = normalize([x.toLight[0], x.toLight[1], x.toLight[2]]);
|
|
348
|
+
return {
|
|
349
|
+
sunDir: toSun,
|
|
350
|
+
moonDir,
|
|
351
|
+
skyZenith: d.skyZenithDay,
|
|
352
|
+
skyHorizon: d.skyHorizonDay,
|
|
353
|
+
sunGlow: d.sunGlowHigh,
|
|
354
|
+
...shared,
|
|
355
|
+
mode: 'fixed',
|
|
356
|
+
fogColor: FOG_DAY,
|
|
357
|
+
toSun,
|
|
358
|
+
sunElevationDeg: Math.asin(clamp(toSun[1], -1, 1)) / DEG,
|
|
359
|
+
sun: x.sun,
|
|
360
|
+
ambient: x.ambient,
|
|
361
|
+
skyFill: x.skyFill,
|
|
362
|
+
bounce: x.bounce,
|
|
363
|
+
warmTint: x.warmTint,
|
|
364
|
+
exposure: x.exposure,
|
|
365
|
+
backdrop: x.backdrop,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
if (this.mode === 'studio') {
|
|
369
|
+
const s = this.studio;
|
|
370
|
+
const toSun = studioLightDirection(heading, s.sunAzimuthFromViewDeg, s.sunElevationDeg);
|
|
371
|
+
return {
|
|
372
|
+
sunDir: toSun,
|
|
373
|
+
moonDir,
|
|
374
|
+
skyZenith: d.skyZenithDay,
|
|
375
|
+
skyHorizon: d.skyHorizonDay,
|
|
376
|
+
sunGlow: d.sunGlowHigh,
|
|
377
|
+
...shared,
|
|
378
|
+
mode: 'studio',
|
|
379
|
+
fogColor: FOG_DAY,
|
|
380
|
+
toSun,
|
|
381
|
+
sunElevationDeg: s.sunElevationDeg,
|
|
382
|
+
sun: s.sun,
|
|
383
|
+
ambient: s.ambient,
|
|
384
|
+
skyFill: s.skyFill,
|
|
385
|
+
bounce: s.bounce,
|
|
386
|
+
warmTint: s.warmTint,
|
|
387
|
+
exposure: s.exposure,
|
|
388
|
+
backdrop: [1, 1, 1],
|
|
389
|
+
};
|
|
390
|
+
}
|
|
391
|
+
const pos = this.sun();
|
|
392
|
+
const e = obs ? Math.asin(clamp(obs.sunDir[0] * obs.up[0] + obs.sunDir[1] * obs.up[1] + obs.sunDir[2] * obs.up[2], -1, 1)) / DEG : pos.elevationDeg;
|
|
393
|
+
const day = smoothstep(DAY_FROM, DAY_TO, e);
|
|
394
|
+
const sunUp = smoothstep(SUN_FROM, SUN_TO, e);
|
|
395
|
+
const moonUp = 1 - smoothstep(MOON_FROM, MOON_TO, e);
|
|
396
|
+
const twilight = Math.exp(-((e / TWILIGHT_WIDTH) ** 2));
|
|
397
|
+
// Between sunset and moonrise nothing directional shines, the direction is swapped invisibly.
|
|
398
|
+
const sunLit = e > (SUN_FROM + MOON_TO) / 2;
|
|
399
|
+
const toSun = obs
|
|
400
|
+
? sunLit
|
|
401
|
+
? liftedAbove(obs.up, obs.sunDir, e, d.minLightElevationDeg)
|
|
402
|
+
: moonDir
|
|
403
|
+
: sunLit
|
|
404
|
+
? skyDirection(pos.azimuthDeg, Math.max(e, d.minLightElevationDeg))
|
|
405
|
+
: skyDirection(d.moonAzimuthDeg, d.moonElevationDeg);
|
|
406
|
+
const sun = sunLit ? scale3(mix3(d.sunLow, d.sunHigh, smoothstep(WARM_FROM, WARM_TO, e)), sunUp) : scale3(d.moon, moonUp);
|
|
407
|
+
// A low sun lights the ground only at a grazing angle, so the sky light is all the more visible.
|
|
408
|
+
const fill = 1 + d.lowSunFill * day * (1 - smoothstep(LOW_SUN_FROM, LOW_SUN_TO, e));
|
|
409
|
+
const ambient = mix3(d.ambientNight, d.ambientDay, day);
|
|
410
|
+
// With the sun low, the haze at the horizon loses its blue.
|
|
411
|
+
const warm = day * (1 - smoothstep(WARM_FROM, HORIZON_WARM_TO, e));
|
|
412
|
+
return {
|
|
413
|
+
...shared,
|
|
414
|
+
mode: 'day',
|
|
415
|
+
sunElevationDeg: e,
|
|
416
|
+
fogColor: [
|
|
417
|
+
lerp(FOG_NIGHT[0], FOG_DAY[0], day) + FOG_TWILIGHT[0] * twilight,
|
|
418
|
+
lerp(FOG_NIGHT[1], FOG_DAY[1], day) + FOG_TWILIGHT[1] * twilight,
|
|
419
|
+
lerp(FOG_NIGHT[2], FOG_DAY[2], day) + FOG_TWILIGHT[2] * twilight,
|
|
420
|
+
],
|
|
421
|
+
toSun,
|
|
422
|
+
sun,
|
|
423
|
+
ambient: [ambient[0] + d.twilight[0] * twilight, ambient[1] + d.twilight[1] * twilight, ambient[2] + d.twilight[2] * twilight],
|
|
424
|
+
skyFill: mix3(d.skyNight, scale3(d.skyDay, fill), day),
|
|
425
|
+
bounce: scale3(d.bounce, sunUp),
|
|
426
|
+
warmTint: mix3(d.tintNight, d.tintDay, day),
|
|
427
|
+
exposure: lerp(d.exposureNight, 1 + d.lowSunExposure * (1 - smoothstep(DIM_FROM, DIM_TO, e)), day),
|
|
428
|
+
backdrop: mix3(d.backdropNight, [1, 1, 1], day),
|
|
429
|
+
sunDir: obs ? obs.sunDir : skyDirection(pos.azimuthDeg, e),
|
|
430
|
+
moonDir,
|
|
431
|
+
// The zenith darkens a little later than the horizon; dusk and dawn warm the horizon.
|
|
432
|
+
skyZenith: mix3(d.skyZenithNight, d.skyZenithDay, smoothstep(DAY_FROM - 4, DAY_TO, e)),
|
|
433
|
+
skyHorizon: [
|
|
434
|
+
lerp(d.skyHorizonNight[0], d.skyHorizonDay[0], day) + d.skyTwilight[0] * twilight,
|
|
435
|
+
lerp(d.skyHorizonNight[1], d.skyHorizonDay[1], day) * (1 - HORIZON_WARM[1] * warm) + d.skyTwilight[1] * twilight,
|
|
436
|
+
lerp(d.skyHorizonNight[2], d.skyHorizonDay[2], day) * (1 - HORIZON_WARM[2] * warm) + d.skyTwilight[2] * twilight,
|
|
437
|
+
],
|
|
438
|
+
sunGlow: scale3(mix3(d.sunGlowLow, d.sunGlowHigh, smoothstep(WARM_FROM, WARM_TO, e)), smoothstep(GLOW_FROM, GLOW_TO, e)),
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { EnvSlot, FrameEnvironment } from '../base/env-slot.ts';
|
|
2
|
+
/** The engine's members: light colours, time, wind, clouds, weather, fog and snow colour. */
|
|
3
|
+
export declare const ENV_CORE_SLOT: EnvSlot<null, FrameEnvironment>;
|
|
4
|
+
/** Float buffers of the two regions, as `Renderer.frameSlots` holds them. */
|
|
5
|
+
export interface FrameRegions {
|
|
6
|
+
afterLight: Float32Array;
|
|
7
|
+
afterView: Float32Array;
|
|
8
|
+
}
|
|
9
|
+
/** Result of one frame: each slot's values (by slot) and the combined shadow key. */
|
|
10
|
+
export interface FrameBlockValues {
|
|
11
|
+
values: Map<EnvSlot<any, any>, unknown>;
|
|
12
|
+
shadowKey: string;
|
|
13
|
+
}
|
|
14
|
+
/** The assembled block. Pass it as `RendererOptions.frameSlots` and to `EnvironmentLink`. */
|
|
15
|
+
export interface EnvFrameBlock {
|
|
16
|
+
/** Number of vec4 members per region. */
|
|
17
|
+
afterLight: number;
|
|
18
|
+
afterView: number;
|
|
19
|
+
/** GLSL member declarations per region. */
|
|
20
|
+
glsl: {
|
|
21
|
+
afterLight: string;
|
|
22
|
+
afterView: string;
|
|
23
|
+
};
|
|
24
|
+
/** All slots, the engine's own first. */
|
|
25
|
+
slots: readonly EnvSlot<any, any>[];
|
|
26
|
+
/** Computes every slot for one frame and writes the lanes into `out`. */
|
|
27
|
+
pack(env: FrameEnvironment, out: FrameRegions): FrameBlockValues;
|
|
28
|
+
}
|
|
29
|
+
export declare function assembleFrameBlock(slots?: readonly EnvSlot<any, any>[]): EnvFrameBlock;
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// Assembles the environment's part of the Frame block from slots: the engine's own slot (`ENV_CORE_SLOT`) first, then
|
|
2
|
+
// the registered slots in the given order. Members are sorted by their `order` within each region, so the layout does
|
|
3
|
+
// not depend on registration order. Each frame, every slot computes its values from the generic environment and
|
|
4
|
+
// writes them into its lanes.
|
|
5
|
+
const LANES = 'xyzw';
|
|
6
|
+
const REGIONS = ['afterLight', 'afterView'];
|
|
7
|
+
/** The engine's members: light colours, time, wind, clouds, weather, fog and snow colour. */
|
|
8
|
+
export const ENV_CORE_SLOT = {
|
|
9
|
+
name: 'env',
|
|
10
|
+
params: null,
|
|
11
|
+
fields: [
|
|
12
|
+
{ member: 'u_sunColor', order: 10, lanes: 'xyz', doc: '', region: 'afterLight' },
|
|
13
|
+
{ member: 'u_ambient', order: 20, lanes: 'xyz', doc: '', region: 'afterLight' },
|
|
14
|
+
{ member: 'u_skyFill', order: 30, lanes: 'xyz', doc: '', region: 'afterLight' },
|
|
15
|
+
{ member: 'u_bounce', order: 40, lanes: 'xyz', doc: 'warm bounce from the ground', region: 'afterLight' },
|
|
16
|
+
{ member: 'u_warmTint', order: 50, lanes: 'xyzw', doc: 'rgb surface colour shift, and exposure', region: 'afterLight' },
|
|
17
|
+
{ member: 'u_time', order: 10, lanes: 'xy', doc: 'x animation time in seconds, y hour of day' },
|
|
18
|
+
{ member: 'u_wind', order: 20, lanes: 'xy', doc: 'xy direction the wind blows toward (x, z)' },
|
|
19
|
+
{ member: 'u_gust', order: 30, lanes: 'xy', doc: 'x 2π / gust length, y gust speed in m/s' },
|
|
20
|
+
{ member: 'u_cloud', order: 40, lanes: 'xyzw', doc: 'xy cloud pattern offset in metres, z darkening under a cloud 0–1, w 1 / cloud size' },
|
|
21
|
+
{ member: 'u_cloudEdge', order: 50, lanes: 'xy', doc: 'x, y noise thresholds between which the cloud shadow fades in' },
|
|
22
|
+
{ member: 'u_weather', order: 60, lanes: 'xyzw', doc: 'x snow cover, y wetness, z fog, w precipitation strength (all 0–1)' },
|
|
23
|
+
{ member: 'u_fogColor', order: 70, lanes: 'xyzw', doc: 'rgb fog colour in screen space, w precipitation kind (0 rain, 1 snow)' },
|
|
24
|
+
{ member: 'u_snow', order: 80, lanes: 'xyzw', doc: 'rgb snow albedo, w cloud cover' },
|
|
25
|
+
{ member: 'u_fogShape', order: 90, lanes: 'xyz', doc: 'x fog layer floor height, y 1 / fog layer height, z day of year' },
|
|
26
|
+
],
|
|
27
|
+
compute: (env) => env,
|
|
28
|
+
lanes: (E) => [
|
|
29
|
+
...E.sun,
|
|
30
|
+
...E.ambient,
|
|
31
|
+
...E.skyFill,
|
|
32
|
+
...E.bounce,
|
|
33
|
+
...E.warmTint,
|
|
34
|
+
E.exposure,
|
|
35
|
+
E.time,
|
|
36
|
+
E.hour,
|
|
37
|
+
E.windX,
|
|
38
|
+
E.windZ,
|
|
39
|
+
(2 * Math.PI) / E.gustLengthM,
|
|
40
|
+
E.gustSpeed,
|
|
41
|
+
E.cloudX,
|
|
42
|
+
E.cloudZ,
|
|
43
|
+
E.cloudDarkness,
|
|
44
|
+
1 / E.cloudSizeM,
|
|
45
|
+
E.cloudLo,
|
|
46
|
+
E.cloudHi,
|
|
47
|
+
E.snowCover,
|
|
48
|
+
E.wetness,
|
|
49
|
+
E.fog,
|
|
50
|
+
E.precipitation,
|
|
51
|
+
...E.fogColor,
|
|
52
|
+
E.precipitationKind,
|
|
53
|
+
...E.snowAlbedo,
|
|
54
|
+
E.cloudCover,
|
|
55
|
+
E.fogLayer.bottomY,
|
|
56
|
+
1 / Math.max(1, (E.fogLayer.topY - E.fogLayer.bottomY) * 0.8),
|
|
57
|
+
E.dayOfYear,
|
|
58
|
+
],
|
|
59
|
+
};
|
|
60
|
+
export function assembleFrameBlock(slots = []) {
|
|
61
|
+
const all = [ENV_CORE_SLOT, ...slots];
|
|
62
|
+
const names = new Set();
|
|
63
|
+
for (const s of all) {
|
|
64
|
+
if (names.has(s.name))
|
|
65
|
+
throw new Error(`Two environment slots are named "${s.name}"`);
|
|
66
|
+
names.add(s.name);
|
|
67
|
+
}
|
|
68
|
+
// Members: declared by the field that gives `order`.
|
|
69
|
+
const members = new Map();
|
|
70
|
+
for (const s of all) {
|
|
71
|
+
for (const f of s.fields) {
|
|
72
|
+
if (f.order === undefined)
|
|
73
|
+
continue;
|
|
74
|
+
if (members.has(f.member))
|
|
75
|
+
throw new Error(`Frame member ${f.member} is declared twice (slot "${s.name}")`);
|
|
76
|
+
members.set(f.member, { name: f.member, region: f.region ?? 'afterView', order: f.order, docs: [], used: [false, false, false, false], index: 0 });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const sizes = { afterLight: 0, afterView: 0 };
|
|
80
|
+
const glsl = { afterLight: '', afterView: '' };
|
|
81
|
+
const sorted = [...members.values()].sort((a, b) => a.order - b.order || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
82
|
+
// Lane offsets of every slot, in field order.
|
|
83
|
+
const offsets = [];
|
|
84
|
+
for (const s of all) {
|
|
85
|
+
const list = [];
|
|
86
|
+
for (const f of s.fields) {
|
|
87
|
+
const m = members.get(f.member);
|
|
88
|
+
if (!m)
|
|
89
|
+
throw new Error(`Slot "${s.name}" writes to frame member ${f.member}, which no slot declares (give it an order)`);
|
|
90
|
+
if (f.region !== undefined && f.region !== m.region)
|
|
91
|
+
throw new Error(`Slot "${s.name}" places ${f.member} in another region`);
|
|
92
|
+
for (const ch of f.lanes) {
|
|
93
|
+
const lane = LANES.indexOf(ch);
|
|
94
|
+
if (lane < 0)
|
|
95
|
+
throw new Error(`Slot "${s.name}": unknown lane "${ch}" in ${f.member}`);
|
|
96
|
+
if (m.used[lane])
|
|
97
|
+
throw new Error(`Slot "${s.name}": lane ${f.member}.${ch} is already taken`);
|
|
98
|
+
m.used[lane] = true;
|
|
99
|
+
list.push({ region: m.region, at: lane });
|
|
100
|
+
}
|
|
101
|
+
if (f.doc)
|
|
102
|
+
m.docs.push({ lane: LANES.indexOf(f.lanes[0]), doc: f.doc });
|
|
103
|
+
}
|
|
104
|
+
offsets.push(list);
|
|
105
|
+
}
|
|
106
|
+
for (const region of REGIONS) {
|
|
107
|
+
for (const m of sorted) {
|
|
108
|
+
if (m.region !== region)
|
|
109
|
+
continue;
|
|
110
|
+
m.index = sizes[region]++;
|
|
111
|
+
const doc = [...m.docs].sort((a, b) => a.lane - b.lane).map((d) => d.doc).join(', ');
|
|
112
|
+
glsl[region] += ` ${`vec4 ${m.name};`.padEnd(18)}${doc ? ` // ${doc}` : ''}`.trimEnd() + '\n';
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Resolve lane offsets to float positions now that member indices are known.
|
|
116
|
+
for (let i = 0; i < all.length; i++) {
|
|
117
|
+
const list = offsets[i];
|
|
118
|
+
let j = 0;
|
|
119
|
+
for (const f of all[i].fields) {
|
|
120
|
+
const m = members.get(f.member);
|
|
121
|
+
for (let c = 0; c < f.lanes.length; c++)
|
|
122
|
+
list[j + c].at += m.index * 4;
|
|
123
|
+
j += f.lanes.length;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
const values = new Map();
|
|
127
|
+
return {
|
|
128
|
+
...sizes,
|
|
129
|
+
glsl,
|
|
130
|
+
slots: all,
|
|
131
|
+
pack(env, out) {
|
|
132
|
+
values.clear();
|
|
133
|
+
const keys = [];
|
|
134
|
+
for (let i = 0; i < all.length; i++) {
|
|
135
|
+
const s = all[i];
|
|
136
|
+
const v = s.compute(env, s.params);
|
|
137
|
+
values.set(s, v);
|
|
138
|
+
const list = offsets[i];
|
|
139
|
+
if (list.length > 0) {
|
|
140
|
+
const lanes = s.lanes(v);
|
|
141
|
+
if (lanes.length !== list.length)
|
|
142
|
+
throw new Error(`Slot "${s.name}" gave ${lanes.length} lanes, its fields have ${list.length}`);
|
|
143
|
+
for (let j = 0; j < list.length; j++)
|
|
144
|
+
out[list[j].region][list[j].at] = lanes[j];
|
|
145
|
+
}
|
|
146
|
+
if (s.shadowKey)
|
|
147
|
+
keys.push(s.shadowKey(v));
|
|
148
|
+
}
|
|
149
|
+
return { values, shadowKey: keys.join('|') };
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Octaves of the cloud field over the world: weight, frequency, offset and noise seed. Cloud shadows on the ground
|
|
3
|
+
* (`cloudLight`) and the clouds in the sky (`<sky>`) are both built from this table, so they agree.
|
|
4
|
+
*/
|
|
5
|
+
export declare const CLOUD_OCTAVES: readonly (readonly [weight: string, frequency: string, offset: string, seed: number])[];
|
|
6
|
+
/** The cloud field, 0–1, as a GLSL expression of `q` (world xz minus the drift, times 1 / cloud size). */
|
|
7
|
+
export declare const CLOUD_NOISE: string;
|
|
8
|
+
/**
|
|
9
|
+
* The cloud field for radial up, as a GLSL expression of a vec3 `q`: the point of the base surface, relative to the
|
|
10
|
+
* centre, minus the drift, times 1 / cloud size. The same octaves in space instead of on a plane, so the field has no
|
|
11
|
+
* seam and no pole.
|
|
12
|
+
*/
|
|
13
|
+
export declare const CLOUD_NOISE_RADIAL: string;
|
|
14
|
+
/**
|
|
15
|
+
* The shadow query include `<shadow>`: how much sun reaches a point (scene shadows from the shadow map times cloud
|
|
16
|
+
* shadows) and nothing of the light model. `cloudLight`, `sunVisibility` and `sunVisibilityBaked` (soft edge, as
|
|
17
|
+
* `<lighting>` always had them) and `sunVisibilitySharp` and `sunVisibilityBakedSharp` (one depth comparison at the
|
|
18
|
+
* centre of the shadow map texel: a hard, unsoftened edge). `<lighting>` contains the same text; both are guarded by
|
|
19
|
+
* `WH_SHADOW_QUERY`, so a material may include `<shadow>`, `<lighting>` or both in any order. `cloudField` as for
|
|
20
|
+
* `lightingGlsl`.
|
|
21
|
+
*/
|
|
22
|
+
export declare function shadowGlsl(o?: {
|
|
23
|
+
cloudField?: boolean;
|
|
24
|
+
}): string;
|
|
25
|
+
/** `<shadow>` without the cloud cover by place (see `shadowGlsl`). */
|
|
26
|
+
export declare const SHADOW_QUERY: string;
|
|
27
|
+
/**
|
|
28
|
+
* The lighting include. Without options it is `LIGHTING`. With `cloudField` (the block holds the slot
|
|
29
|
+
* `CLOUD_FIELD_SLOT`) radial up gains `cloudFieldCover` and `cloudFieldAt`, and `cloudLight` reads the cover by place.
|
|
30
|
+
* It begins with the shadow query (`shadowGlsl`, guarded so `<shadow>` may be included as well).
|
|
31
|
+
*/
|
|
32
|
+
export declare function lightingGlsl(o?: {
|
|
33
|
+
cloudField?: boolean;
|
|
34
|
+
}): string;
|
|
35
|
+
export declare const LIGHTING: string;
|