wallhammer-web 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +88 -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,29 @@
|
|
|
1
|
+
import type { Vec3 } from '../base/math.ts';
|
|
2
|
+
import type { DrawContext, Layer, Renderer } from '../render/index.ts';
|
|
3
|
+
import type { EnvironmentLink } from './environment-link.ts';
|
|
4
|
+
export declare const CLOUD_SHELL_VS = "#include <header>\nuniform vec4 u_shellCenter; // xyz centre in render space, w radius of the shell\nin vec3 a_pos; // unit direction from the centre\nout vec3 v_world;\nvoid main() {\n vec3 p = u_shellCenter.xyz + a_pos * u_shellCenter.w;\n v_world = p;\n gl_Position = u_viewProj * vec4(p, 1.0);\n}";
|
|
5
|
+
export declare const CLOUD_SHELL_FS: string;
|
|
6
|
+
export declare class CloudShellLayer implements Layer {
|
|
7
|
+
/** Name in profiles (`Layer.name`). */
|
|
8
|
+
readonly name: string;
|
|
9
|
+
/** Centre of the base sphere, render space. */
|
|
10
|
+
center: Vec3;
|
|
11
|
+
/** Radius of the sphere the field lies on (the shadows' sphere). */
|
|
12
|
+
baseRadiusM: number;
|
|
13
|
+
/** Height of the shell above the base sphere. */
|
|
14
|
+
heightM: number;
|
|
15
|
+
/** 3 × 3, row-major: render directions → the frame the field is fixed in. Identity by default. */
|
|
16
|
+
toField: Float32Array;
|
|
17
|
+
/** Unit direction to the light, render space. */
|
|
18
|
+
light: Vec3;
|
|
19
|
+
/** 0 = nothing drawn. */
|
|
20
|
+
opacity: number;
|
|
21
|
+
private readonly program;
|
|
22
|
+
private readonly mesh;
|
|
23
|
+
private readonly renderer;
|
|
24
|
+
constructor(renderer: Renderer, link: EnvironmentLink, o?: {
|
|
25
|
+
segments?: number;
|
|
26
|
+
});
|
|
27
|
+
draw(ctx: DrawContext): void;
|
|
28
|
+
dispose(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// The clouds of a cloud cover by place (`cloudFieldSlot`) seen from outside or from below: a sphere shell above the
|
|
2
|
+
// base sphere, drawn from the same field as the cloud shadows. A fragment looks the field up where the light's ray
|
|
3
|
+
// through it meets the base sphere, so a cloud stands over its own shadow. One draw in the `color` pass, both sides,
|
|
4
|
+
// with the depth test and without depth writes: peaks and the body hide it in every depth mode.
|
|
5
|
+
import { CLOUD_FIELD_SLOT, CLOUD_OCTAVES } from '../env/index.js';
|
|
6
|
+
const POSITION_LAYOUT = { stride: 12, attribs: [{ name: 'a_pos', size: 3, type: 'f32', offset: 0 }] };
|
|
7
|
+
const ATTRIBS = { a_pos: 0 };
|
|
8
|
+
export const CLOUD_SHELL_VS = /* glsl */ `#include <header>
|
|
9
|
+
uniform vec4 u_shellCenter; // xyz centre in render space, w radius of the shell
|
|
10
|
+
in vec3 a_pos; // unit direction from the centre
|
|
11
|
+
out vec3 v_world;
|
|
12
|
+
void main() {
|
|
13
|
+
vec3 p = u_shellCenter.xyz + a_pos * u_shellCenter.w;
|
|
14
|
+
v_world = p;
|
|
15
|
+
gl_Position = u_viewProj * vec4(p, 1.0);
|
|
16
|
+
}`;
|
|
17
|
+
/** The cloud pattern with octaves under about two pixels replaced by their mean, as a GLSL expression of `q` and `fw`. */
|
|
18
|
+
const CLOUD_NOISE_AA = CLOUD_OCTAVES.map(([w, f, o, seed], i) => `${w} * mix(0.5, vnoise3(${i === 0 ? 'q' : `q * ${f} + ${o}`}, ${seed}u), 1.0 - smoothstep(0.3, 0.7, fw * ${f}))`).join(' + ');
|
|
19
|
+
export const CLOUD_SHELL_FS = /* glsl */ `#include <header>
|
|
20
|
+
#include <noise>
|
|
21
|
+
#include <lighting>
|
|
22
|
+
#include <atmosphere>
|
|
23
|
+
uniform vec4 u_shellCenter; // xyz centre in render space, w radius of the shell
|
|
24
|
+
uniform vec4 u_shellBase; // x radius of the base sphere, y opacity
|
|
25
|
+
uniform vec3 u_shellEye; // the eye in render space
|
|
26
|
+
uniform vec3 u_shellLight; // unit direction to the light, render space
|
|
27
|
+
uniform vec3 u_toField0; // rows of render → field directions
|
|
28
|
+
uniform vec3 u_toField1;
|
|
29
|
+
uniform vec3 u_toField2;
|
|
30
|
+
in vec3 v_world;
|
|
31
|
+
out vec4 o_color;
|
|
32
|
+
vec3 toField(vec3 d) { return normalize(vec3(dot(u_toField0, d), dot(u_toField1, d), dot(u_toField2, d))); }
|
|
33
|
+
void main() {
|
|
34
|
+
#ifdef WH_LOCAL_UP
|
|
35
|
+
vec3 rel = v_world - u_shellCenter.xyz;
|
|
36
|
+
float rb = u_shellBase.x;
|
|
37
|
+
vec3 L = u_shellLight;
|
|
38
|
+
// Where the ray from the fragment away from the light meets the base sphere; the radial point where it misses.
|
|
39
|
+
float b = dot(rel, L);
|
|
40
|
+
float disc = b * b - (dot(rel, rel) - rb * rb);
|
|
41
|
+
vec3 hit = disc > 0.0 ? rel - L * (b - sqrt(disc)) : normalize(rel) * rb;
|
|
42
|
+
if (disc > 0.0 && b - sqrt(disc) < 0.0) hit = normalize(rel) * rb;
|
|
43
|
+
vec3 upHere = normalize(rel);
|
|
44
|
+
vec3 up = toField(normalize(hit));
|
|
45
|
+
float c = cloudFieldCover(up, rb);
|
|
46
|
+
vec3 q = (up * rb - vec3(u_cloud.x, 0.0, u_cloud.y)) * u_cloud.w;
|
|
47
|
+
float fw = metresPerPixelAt(v_world) * u_cloud.w;
|
|
48
|
+
float n = ${CLOUD_NOISE_AA};
|
|
49
|
+
vec2 s = cloudFieldShade(c, n);
|
|
50
|
+
float a = s.y * u_shellBase.y;
|
|
51
|
+
if (a <= 0.003) discard;
|
|
52
|
+
// Lit by the sun of the place (the frame's sun over its overcast dimming) on the day side, darker where thick and
|
|
53
|
+
// from below.
|
|
54
|
+
float mu = dot(upHere, L);
|
|
55
|
+
float day = smoothstep(-0.12, 0.2, mu);
|
|
56
|
+
float below = step(length(u_shellEye - u_shellCenter.xyz), u_shellCenter.w);
|
|
57
|
+
float shade = mix(1.0, 0.62, c) * mix(1.0, 0.7, below * c);
|
|
58
|
+
vec3 sun = u_sunColor.rgb / max(u_cloudFieldLight.y, 1e-3);
|
|
59
|
+
vec3 col = 0.92 * (sun * (0.55 + 0.45 * max(mu, 0.0)) * day * shade + (u_ambient.rgb + u_skyFill.rgb) * mix(0.15, 1.0, day));
|
|
60
|
+
o_color = vec4(atmosphereApply(toDisplay(col), v_world), a);
|
|
61
|
+
#else
|
|
62
|
+
discard;
|
|
63
|
+
#endif
|
|
64
|
+
}`;
|
|
65
|
+
/** A cube sphere: `segments` quads along each face edge, unit radius. */
|
|
66
|
+
function cubeSphere(segments) {
|
|
67
|
+
const n = Math.max(1, Math.floor(segments));
|
|
68
|
+
const side = n + 1;
|
|
69
|
+
const vertices = new Float32Array(6 * side * side * 3);
|
|
70
|
+
const indices = new Uint32Array(6 * n * n * 6);
|
|
71
|
+
const faces = [
|
|
72
|
+
[[1, 0, 0], [0, 0, -1], [0, 1, 0]],
|
|
73
|
+
[[-1, 0, 0], [0, 0, 1], [0, 1, 0]],
|
|
74
|
+
[[0, 1, 0], [1, 0, 0], [0, 0, -1]],
|
|
75
|
+
[[0, -1, 0], [1, 0, 0], [0, 0, 1]],
|
|
76
|
+
[[0, 0, 1], [1, 0, 0], [0, 1, 0]],
|
|
77
|
+
[[0, 0, -1], [-1, 0, 0], [0, 1, 0]],
|
|
78
|
+
];
|
|
79
|
+
let v = 0;
|
|
80
|
+
let k = 0;
|
|
81
|
+
faces.forEach(([nrm, u, w], f) => {
|
|
82
|
+
for (let j = 0; j <= n; j++) {
|
|
83
|
+
for (let i = 0; i <= n; i++) {
|
|
84
|
+
// Equal angles along the face edge keep the quads near the same size.
|
|
85
|
+
const a = Math.tan(((i / n) * 2 - 1) * (Math.PI / 4));
|
|
86
|
+
const b = Math.tan(((j / n) * 2 - 1) * (Math.PI / 4));
|
|
87
|
+
const x = nrm[0] + u[0] * a + w[0] * b;
|
|
88
|
+
const y = nrm[1] + u[1] * a + w[1] * b;
|
|
89
|
+
const z = nrm[2] + u[2] * a + w[2] * b;
|
|
90
|
+
const len = Math.hypot(x, y, z);
|
|
91
|
+
vertices[v++] = x / len;
|
|
92
|
+
vertices[v++] = y / len;
|
|
93
|
+
vertices[v++] = z / len;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const base = f * side * side;
|
|
97
|
+
for (let j = 0; j < n; j++) {
|
|
98
|
+
for (let i = 0; i < n; i++) {
|
|
99
|
+
const p = base + j * side + i;
|
|
100
|
+
indices[k++] = p;
|
|
101
|
+
indices[k++] = p + 1;
|
|
102
|
+
indices[k++] = p + side + 1;
|
|
103
|
+
indices[k++] = p;
|
|
104
|
+
indices[k++] = p + side + 1;
|
|
105
|
+
indices[k++] = p + side;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return { layout: POSITION_LAYOUT, vertices, indices, count: indices.length };
|
|
110
|
+
}
|
|
111
|
+
export class CloudShellLayer {
|
|
112
|
+
/** Name in profiles (`Layer.name`). */
|
|
113
|
+
name = 'cloud-shell';
|
|
114
|
+
/** Centre of the base sphere, render space. */
|
|
115
|
+
center = [0, 0, 0];
|
|
116
|
+
/** Radius of the sphere the field lies on (the shadows' sphere). */
|
|
117
|
+
baseRadiusM = 1;
|
|
118
|
+
/** Height of the shell above the base sphere. */
|
|
119
|
+
heightM = 3000;
|
|
120
|
+
/** 3 × 3, row-major: render directions → the frame the field is fixed in. Identity by default. */
|
|
121
|
+
toField = new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
|
|
122
|
+
/** Unit direction to the light, render space. */
|
|
123
|
+
light = [0, 1, 0];
|
|
124
|
+
/** 0 = nothing drawn. */
|
|
125
|
+
opacity = 1;
|
|
126
|
+
program;
|
|
127
|
+
mesh;
|
|
128
|
+
renderer;
|
|
129
|
+
constructor(renderer, link, o = {}) {
|
|
130
|
+
if (!link.block.slots.some((s) => s.name === CLOUD_FIELD_SLOT)) {
|
|
131
|
+
throw new Error(`CloudShellLayer needs the environment slot "${CLOUD_FIELD_SLOT}" (cloudFieldSlot) in the frame block`);
|
|
132
|
+
}
|
|
133
|
+
this.renderer = renderer;
|
|
134
|
+
this.program = renderer.device.program('cloud shell', renderer.resolve(CLOUD_SHELL_VS), renderer.resolve(CLOUD_SHELL_FS), ATTRIBS);
|
|
135
|
+
this.mesh = renderer.device.createMesh(cubeSphere(o.segments ?? 48), ATTRIBS);
|
|
136
|
+
}
|
|
137
|
+
draw(ctx) {
|
|
138
|
+
if (ctx.pass !== 'color' || this.opacity <= 0)
|
|
139
|
+
return;
|
|
140
|
+
const device = ctx.device;
|
|
141
|
+
const p = this.program;
|
|
142
|
+
const t = this.toField;
|
|
143
|
+
device.setState({ depthTest: true, depthWrite: false, cullBack: false, blend: { src: 'src-alpha', dst: 'one-minus-src-alpha' } });
|
|
144
|
+
device.use(p);
|
|
145
|
+
p.set('u_shellCenter', { vec4: [this.center[0], this.center[1], this.center[2], this.baseRadiusM + this.heightM] });
|
|
146
|
+
p.set('u_shellBase', { vec4: [this.baseRadiusM, this.opacity, 0, 0] });
|
|
147
|
+
p.vec3v('u_shellEye', ctx.view.cornerRays()[0].origin);
|
|
148
|
+
p.vec3v('u_shellLight', this.light);
|
|
149
|
+
p.set('u_toField0', { vec3: [t[0], t[1], t[2]] });
|
|
150
|
+
p.set('u_toField1', { vec3: [t[3], t[4], t[5]] });
|
|
151
|
+
p.set('u_toField2', { vec3: [t[6], t[7], t[8]] });
|
|
152
|
+
device.draw(this.mesh);
|
|
153
|
+
ctx.count(this.mesh.triangles);
|
|
154
|
+
}
|
|
155
|
+
dispose() {
|
|
156
|
+
this.renderer.device.deleteMesh(this.mesh);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { RGB } from '../base/math.ts';
|
|
2
|
+
import type { View } from '../base/view.ts';
|
|
3
|
+
import type { EnvSlot } from '../base/env-slot.ts';
|
|
4
|
+
import { FrameLoop } from '../platform/index.ts';
|
|
5
|
+
import type { Canvas, CanvasSizeTarget } from '../platform/index.ts';
|
|
6
|
+
import { Device } from '../gpu/index.ts';
|
|
7
|
+
import { Renderer } from '../render/index.ts';
|
|
8
|
+
import type { Layer, RendererOptions } from '../render/index.ts';
|
|
9
|
+
import { Environment } from '../env/index.ts';
|
|
10
|
+
import type { AtmosphereParams, EnvFrameBlock, EnvironmentConfig, SkyParams } from '../env/index.ts';
|
|
11
|
+
import { Simulation } from '../sim/index.ts';
|
|
12
|
+
import type { SimData, SimulationOptions, System } from '../sim/index.ts';
|
|
13
|
+
import { EnvironmentLink } from './environment-link.ts';
|
|
14
|
+
import { SkyLayer } from './sky.ts';
|
|
15
|
+
import { AtmosphereLayer } from './atmosphere.ts';
|
|
16
|
+
export interface HeadlessEngineOptions {
|
|
17
|
+
/** Simulation settings, or a simulation built beforehand. */
|
|
18
|
+
sim?: SimulationOptions | Simulation;
|
|
19
|
+
/** Environment settings, or an environment built beforehand. */
|
|
20
|
+
environment?: EnvironmentConfig | Environment;
|
|
21
|
+
}
|
|
22
|
+
/** A view the engine can size to the canvas: any rig. */
|
|
23
|
+
export type EngineView = View & CanvasSizeTarget;
|
|
24
|
+
export interface EngineOptions<V extends EngineView> extends HeadlessEngineOptions {
|
|
25
|
+
canvas: Canvas;
|
|
26
|
+
view: V;
|
|
27
|
+
/** The project's palette (`definePalette`). */
|
|
28
|
+
palette: {
|
|
29
|
+
data(): Float32Array;
|
|
30
|
+
};
|
|
31
|
+
/** Background in full daylight; the environment darkens it at night. */
|
|
32
|
+
background: RGB;
|
|
33
|
+
/** Environment slots of content and app; they shape the Frame block, so they are fixed at creation. */
|
|
34
|
+
slots?: readonly EnvSlot<any, any>[];
|
|
35
|
+
/** Renderer settings besides palette and frame slots (among them the depth mode, `depth`). */
|
|
36
|
+
renderer?: Omit<RendererOptions, 'palette' | 'frameSlots'>;
|
|
37
|
+
/** Drag orbits and the wheel zooms (the view must be an orbit rig). */
|
|
38
|
+
orbitControls?: boolean;
|
|
39
|
+
/** Adds the screen-space rain and snow layer; it stays the last layer drawn. */
|
|
40
|
+
precipitation?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Adds the sky (perspective views only): the sky slot in the Frame block and the sky layer, drawn first. `true` or
|
|
43
|
+
* the sky's params. Materials get the haze on distant geometry by calling `aerialPerspective` from `<sky>`.
|
|
44
|
+
*/
|
|
45
|
+
sky?: boolean | Partial<SkyParams>;
|
|
46
|
+
/**
|
|
47
|
+
* Adds air as a shell around a sphere (perspective views only; default off): the atmosphere slot in the Frame block
|
|
48
|
+
* and the atmosphere layer, drawn first. `true` for the default air, or its params (for a sphere of another size
|
|
49
|
+
* spread `atmosphereFor(radiusM, heightM)` into them). The sphere is the renderer's `radialUp` sphere; without it
|
|
50
|
+
* the ground is the plane `groundY`. Materials get the air in front of them by calling `atmosphereApply` and their
|
|
51
|
+
* light from `atmosphereLight`, both from `<atmosphere>`. Use it in the place of `sky`, not together with it.
|
|
52
|
+
*/
|
|
53
|
+
atmosphere?: boolean | Partial<AtmosphereParams>;
|
|
54
|
+
/**
|
|
55
|
+
* Upper limit of the canvas's pixels per CSS pixel. Default: none, the device's own pixel ratio. Below it the image
|
|
56
|
+
* is drawn at fewer pixels and the browser scales it up to the canvas's CSS size; the view's `dpr` is the ratio in
|
|
57
|
+
* effect. The samples per pixel are the renderer's option `samples`.
|
|
58
|
+
*/
|
|
59
|
+
maxPixelRatio?: number;
|
|
60
|
+
/** On a canvas resize, render at once instead of at the next browser frame (no stretched frame in between). */
|
|
61
|
+
renderOnResize?: boolean;
|
|
62
|
+
/** Called after every loop frame, e.g. for a status line. */
|
|
63
|
+
onFrame?: () => void;
|
|
64
|
+
/**
|
|
65
|
+
* Survive a loss of the graphics context (default off). The device then keeps what every upload was made from
|
|
66
|
+
* (`Device.retainUploads`: references to the uploaded arrays stay alive with their GPU objects) and, when the
|
|
67
|
+
* browser hands a fresh context back, makes every program, buffer, mesh and texture again in place and draws a
|
|
68
|
+
* frame. Nothing above the device has to take part.
|
|
69
|
+
*/
|
|
70
|
+
contextRecovery?: boolean;
|
|
71
|
+
}
|
|
72
|
+
export declare class HeadlessEngine {
|
|
73
|
+
readonly sim: Simulation;
|
|
74
|
+
readonly env: Environment;
|
|
75
|
+
private readonly inputs;
|
|
76
|
+
private readonly extractors;
|
|
77
|
+
/** Conditions that keep the frame loop running (see `animateWhile`). */
|
|
78
|
+
protected readonly keepAlive: (() => boolean)[];
|
|
79
|
+
protected disposed: boolean;
|
|
80
|
+
/** Real seconds of the frame being made (0 on a frame after a rest); read it in the input and extraction hooks. */
|
|
81
|
+
frameSeconds: number;
|
|
82
|
+
constructor(o?: HeadlessEngineOptions);
|
|
83
|
+
/** Runs at the start of every frame; turn input into commands here (`command`). */
|
|
84
|
+
onInput(fn: () => void): void;
|
|
85
|
+
/** Runs after the ticks of every frame, before rendering: read interpolated transforms (`alpha`) into layer data. */
|
|
86
|
+
onExtract(fn: (alpha: number) => void): void;
|
|
87
|
+
/**
|
|
88
|
+
* Keeps the frame loop drawing continuously while `fn` returns true (something the simulation does not own moves:
|
|
89
|
+
* a walking body, a following view). When it turns true call `invalidate` once so the loop wakes up. Returns a
|
|
90
|
+
* function that removes the condition.
|
|
91
|
+
*/
|
|
92
|
+
animateWhile(fn: () => boolean): () => void;
|
|
93
|
+
/** Registers a simulation system after the ones already registered. */
|
|
94
|
+
system(name: string, run: System): void;
|
|
95
|
+
/** Queues a command for the next tick. */
|
|
96
|
+
command(kind: string, data?: SimData): void;
|
|
97
|
+
/** Input, ticks and clocks of one frame of `realSeconds`; returns whether anything moved. */
|
|
98
|
+
protected advance(realSeconds: number): boolean;
|
|
99
|
+
/** Runs the extraction hooks with the simulation's current alpha. */
|
|
100
|
+
extract(): void;
|
|
101
|
+
/** One whole frame without waiting for the browser (headless: no image). Returns the ticks run. */
|
|
102
|
+
frame(realSeconds: number): number;
|
|
103
|
+
/** Releases what the engine owns; the object is unusable afterwards. */
|
|
104
|
+
dispose(): void;
|
|
105
|
+
}
|
|
106
|
+
export declare class Engine<V extends EngineView = EngineView> extends HeadlessEngine {
|
|
107
|
+
readonly canvas: Canvas;
|
|
108
|
+
readonly device: Device;
|
|
109
|
+
readonly block: EnvFrameBlock;
|
|
110
|
+
readonly renderer: Renderer;
|
|
111
|
+
readonly link: EnvironmentLink;
|
|
112
|
+
private _view;
|
|
113
|
+
readonly loop: FrameLoop;
|
|
114
|
+
private readonly undo;
|
|
115
|
+
private readonly precipitation;
|
|
116
|
+
/** The sky layer (and through it the sky slot), or null without `sky`. */
|
|
117
|
+
readonly sky: SkyLayer | null;
|
|
118
|
+
/** The atmosphere layer (and through it the atmosphere slot), or null without `atmosphere`. */
|
|
119
|
+
readonly atmosphere: AtmosphereLayer | null;
|
|
120
|
+
/** The cap of the canvas's pixel ratio (`EngineOptions.maxPixelRatio`), undefined for none. */
|
|
121
|
+
readonly maxPixelRatio: number | undefined;
|
|
122
|
+
constructor(o: EngineOptions<V>);
|
|
123
|
+
/** The view the renderer draws through. */
|
|
124
|
+
get view(): V;
|
|
125
|
+
/**
|
|
126
|
+
* Draws through another view from the next frame on (for example a following view while a body walks, and the
|
|
127
|
+
* orbit view again afterwards). The new view takes the canvas size at once. `orbitControls` act only while the
|
|
128
|
+
* view they were made for is the current one; other controls are the caller's to switch (`WalkInput.enabled`).
|
|
129
|
+
*/
|
|
130
|
+
setView(view: V): void;
|
|
131
|
+
/** Adds a layer; layers draw in the order they are added, the precipitation layer stays last. */
|
|
132
|
+
add(layer: Layer): void;
|
|
133
|
+
/**
|
|
134
|
+
* Draws the sky behind the scene (`SkyLayer.behindScene`): only where no layer drawn before it has drawn, instead
|
|
135
|
+
* of across the whole screen before everything. Moves the sky layer to just before `layer`, which should be the
|
|
136
|
+
* first layer that blends or does not write depth (water, glass, particles); null puts it after every layer added
|
|
137
|
+
* so far (before the precipitation). The image stays the same when every layer before it is opaque. Layers added
|
|
138
|
+
* afterwards are drawn after the sky. Does nothing without `sky`.
|
|
139
|
+
*/
|
|
140
|
+
moveSkyBefore(layer: Layer | null): void;
|
|
141
|
+
/** Starts the frame loop: continuous while something moves, otherwise only on `invalidate`. */
|
|
142
|
+
start(): void;
|
|
143
|
+
stop(): void;
|
|
144
|
+
/** Asks for a frame at the browser's next tick. */
|
|
145
|
+
invalidate(): void;
|
|
146
|
+
/** Renders right now with the current state (no ticks, no clock movement). */
|
|
147
|
+
renderNow(): void;
|
|
148
|
+
/** One whole frame without waiting for the browser, including the image. */
|
|
149
|
+
frame(realSeconds: number): number;
|
|
150
|
+
dispose(): void;
|
|
151
|
+
}
|
|
152
|
+
/** Creates the engine for a canvas: device, renderer, environment, view, simulation and the frame loop. */
|
|
153
|
+
export declare function createEngine<V extends EngineView>(options: EngineOptions<V>): Engine<V>;
|
|
154
|
+
/** Creates the same engine without canvas and GPU: simulation and environment only, stepped by `frame`. */
|
|
155
|
+
export declare function createHeadlessEngine(options?: HeadlessEngineOptions): HeadlessEngine;
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
// The runtime mediator (Wallhammer Zero `eng_runtime`): one object that owns the init order, the frame and the
|
|
2
|
+
// shutdown. No container and no plugins: what the engine is made of is spelled out below, in order.
|
|
3
|
+
//
|
|
4
|
+
// Init: simulation → environment → canvas and device → frame block from slots → renderer → environment link
|
|
5
|
+
// → view size → input → frame loop
|
|
6
|
+
// A frame: input hooks (they queue commands) → whole fixed ticks of the simulation → environment clocks
|
|
7
|
+
// → extraction hooks (interpolated transforms into layer data) → environment frame → render
|
|
8
|
+
// Dispose: the same in reverse.
|
|
9
|
+
//
|
|
10
|
+
// `HeadlessEngine` is the same object without canvas and GPU: it steps the simulation and the environment clocks,
|
|
11
|
+
// for tests and tools in plain Node. The simulation never reads anything the image side owns.
|
|
12
|
+
import { FrameLoop, bindOrbitInput, observeCanvasSize, syncCanvasSize, watchContextLoss } from '../platform/index.js';
|
|
13
|
+
import { Device, createContext } from '../gpu/index.js';
|
|
14
|
+
import { Renderer } from '../render/index.js';
|
|
15
|
+
import { Environment, assembleFrameBlock, atmosphereSlot, skySlot } from '../env/index.js';
|
|
16
|
+
import { Simulation } from '../sim/index.js';
|
|
17
|
+
import { EnvironmentLink } from './environment-link.js';
|
|
18
|
+
import { PrecipitationLayer } from './precipitation.js';
|
|
19
|
+
import { SkyLayer } from './sky.js';
|
|
20
|
+
import { AtmosphereLayer } from './atmosphere.js';
|
|
21
|
+
export class HeadlessEngine {
|
|
22
|
+
sim;
|
|
23
|
+
env;
|
|
24
|
+
inputs = [];
|
|
25
|
+
extractors = [];
|
|
26
|
+
/** Conditions that keep the frame loop running (see `animateWhile`). */
|
|
27
|
+
keepAlive = [];
|
|
28
|
+
disposed = false;
|
|
29
|
+
/** Real seconds of the frame being made (0 on a frame after a rest); read it in the input and extraction hooks. */
|
|
30
|
+
frameSeconds = 0;
|
|
31
|
+
constructor(o = {}) {
|
|
32
|
+
this.sim = o.sim instanceof Simulation ? o.sim : new Simulation(o.sim);
|
|
33
|
+
this.env = o.environment instanceof Environment ? o.environment : new Environment(o.environment);
|
|
34
|
+
}
|
|
35
|
+
/** Runs at the start of every frame; turn input into commands here (`command`). */
|
|
36
|
+
onInput(fn) {
|
|
37
|
+
this.inputs.push(fn);
|
|
38
|
+
}
|
|
39
|
+
/** Runs after the ticks of every frame, before rendering: read interpolated transforms (`alpha`) into layer data. */
|
|
40
|
+
onExtract(fn) {
|
|
41
|
+
this.extractors.push(fn);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Keeps the frame loop drawing continuously while `fn` returns true (something the simulation does not own moves:
|
|
45
|
+
* a walking body, a following view). When it turns true call `invalidate` once so the loop wakes up. Returns a
|
|
46
|
+
* function that removes the condition.
|
|
47
|
+
*/
|
|
48
|
+
animateWhile(fn) {
|
|
49
|
+
this.keepAlive.push(fn);
|
|
50
|
+
return () => {
|
|
51
|
+
const i = this.keepAlive.indexOf(fn);
|
|
52
|
+
if (i >= 0)
|
|
53
|
+
this.keepAlive.splice(i, 1);
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Registers a simulation system after the ones already registered. */
|
|
57
|
+
system(name, run) {
|
|
58
|
+
this.sim.addSystem(name, run);
|
|
59
|
+
}
|
|
60
|
+
/** Queues a command for the next tick. */
|
|
61
|
+
command(kind, data = {}) {
|
|
62
|
+
this.sim.command(kind, data);
|
|
63
|
+
}
|
|
64
|
+
/** Input, ticks and clocks of one frame of `realSeconds`; returns whether anything moved. */
|
|
65
|
+
advance(realSeconds) {
|
|
66
|
+
this.frameSeconds = realSeconds;
|
|
67
|
+
for (const fn of this.inputs)
|
|
68
|
+
fn();
|
|
69
|
+
const ticks = this.sim.advance(realSeconds);
|
|
70
|
+
let moved = ticks > 0 || (this.sim.animating && realSeconds > 0);
|
|
71
|
+
for (const c of this.env.clocks)
|
|
72
|
+
moved = c.advance(realSeconds) || moved;
|
|
73
|
+
return moved;
|
|
74
|
+
}
|
|
75
|
+
/** Runs the extraction hooks with the simulation's current alpha. */
|
|
76
|
+
extract() {
|
|
77
|
+
const alpha = this.sim.alpha;
|
|
78
|
+
for (const fn of this.extractors)
|
|
79
|
+
fn(alpha);
|
|
80
|
+
}
|
|
81
|
+
/** One whole frame without waiting for the browser (headless: no image). Returns the ticks run. */
|
|
82
|
+
frame(realSeconds) {
|
|
83
|
+
const before = this.sim.tick;
|
|
84
|
+
this.advance(realSeconds);
|
|
85
|
+
this.extract();
|
|
86
|
+
return this.sim.tick - before;
|
|
87
|
+
}
|
|
88
|
+
/** Releases what the engine owns; the object is unusable afterwards. */
|
|
89
|
+
dispose() {
|
|
90
|
+
this.disposed = true;
|
|
91
|
+
this.inputs.length = 0;
|
|
92
|
+
this.extractors.length = 0;
|
|
93
|
+
this.keepAlive.length = 0;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export class Engine extends HeadlessEngine {
|
|
97
|
+
canvas;
|
|
98
|
+
device;
|
|
99
|
+
block;
|
|
100
|
+
renderer;
|
|
101
|
+
link;
|
|
102
|
+
_view;
|
|
103
|
+
loop;
|
|
104
|
+
undo = [];
|
|
105
|
+
precipitation;
|
|
106
|
+
/** The sky layer (and through it the sky slot), or null without `sky`. */
|
|
107
|
+
sky;
|
|
108
|
+
/** The atmosphere layer (and through it the atmosphere slot), or null without `atmosphere`. */
|
|
109
|
+
atmosphere;
|
|
110
|
+
/** The cap of the canvas's pixel ratio (`EngineOptions.maxPixelRatio`), undefined for none. */
|
|
111
|
+
maxPixelRatio;
|
|
112
|
+
constructor(o) {
|
|
113
|
+
super(o);
|
|
114
|
+
this.canvas = o.canvas;
|
|
115
|
+
this._view = o.view;
|
|
116
|
+
// The depth mode is the renderer's option; the context has to know it, as a canvas for the reversed or the
|
|
117
|
+
// logarithmic mode is created without its own depth buffer.
|
|
118
|
+
this.device = new Device(createContext(o.canvas, o.renderer?.depth ?? 'standard', o.renderer?.samples));
|
|
119
|
+
this.maxPixelRatio = o.maxPixelRatio;
|
|
120
|
+
if (o.contextRecovery) {
|
|
121
|
+
// Before the first upload: the device keeps only what is uploaded from now on.
|
|
122
|
+
this.device.retainUploads = true;
|
|
123
|
+
this.undo.push(watchContextLoss(o.canvas, {
|
|
124
|
+
restored: () => {
|
|
125
|
+
this.device.contextRestored();
|
|
126
|
+
this.loop.invalidate();
|
|
127
|
+
},
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
const sky = o.sky ? skySlot(o.sky === true ? {} : o.sky) : null;
|
|
131
|
+
const air = o.atmosphere ? atmosphereSlot(o.atmosphere === true ? {} : o.atmosphere) : null;
|
|
132
|
+
this.block = air ? assembleFrameBlock([...(o.slots ?? []), ...(sky ? [sky] : []), air]) : assembleFrameBlock(sky ? [...(o.slots ?? []), sky] : (o.slots ?? []));
|
|
133
|
+
this.renderer = new Renderer(this.device, { ...o.renderer, palette: o.palette.data(), frameSlots: this.block });
|
|
134
|
+
this.link = new EnvironmentLink(this.renderer, this.env, { background: o.background, block: this.block });
|
|
135
|
+
this.sky = sky ? new SkyLayer(this.renderer, sky) : null;
|
|
136
|
+
if (this.sky)
|
|
137
|
+
this.renderer.add(this.sky);
|
|
138
|
+
this.atmosphere = air ? new AtmosphereLayer(this.renderer, air) : null;
|
|
139
|
+
if (this.atmosphere)
|
|
140
|
+
this.renderer.add(this.atmosphere);
|
|
141
|
+
this.precipitation = o.precipitation ? new PrecipitationLayer(this.renderer, this.link) : null;
|
|
142
|
+
if (this.precipitation)
|
|
143
|
+
this.renderer.add(this.precipitation);
|
|
144
|
+
// The loop drives one clock: this engine's frame. It is animating while the simulation or an environment clock runs.
|
|
145
|
+
const engine = this;
|
|
146
|
+
const frameClock = {
|
|
147
|
+
get animating() {
|
|
148
|
+
if (engine.sim.animating)
|
|
149
|
+
return true;
|
|
150
|
+
for (const c of engine.env.clocks)
|
|
151
|
+
if (c.animating)
|
|
152
|
+
return true;
|
|
153
|
+
for (const fn of engine.keepAlive)
|
|
154
|
+
if (fn())
|
|
155
|
+
return true;
|
|
156
|
+
return false;
|
|
157
|
+
},
|
|
158
|
+
advance: (dt) => this.advance(dt),
|
|
159
|
+
onChange: (listener) => {
|
|
160
|
+
this.undo.push(this.sim.onChange(listener));
|
|
161
|
+
for (const c of this.env.clocks)
|
|
162
|
+
this.undo.push(c.onChange(listener));
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
this.loop = new FrameLoop({
|
|
166
|
+
render: () => {
|
|
167
|
+
this.extract();
|
|
168
|
+
this.renderer.render(this.view);
|
|
169
|
+
},
|
|
170
|
+
clocks: [frameClock],
|
|
171
|
+
...(o.onFrame ? { onFrame: o.onFrame } : {}),
|
|
172
|
+
});
|
|
173
|
+
// The canvas size goes to whichever view is current (`setView`), not only to the first one.
|
|
174
|
+
const sizeTarget = {
|
|
175
|
+
get width() {
|
|
176
|
+
return engine._view.width;
|
|
177
|
+
},
|
|
178
|
+
set width(w) {
|
|
179
|
+
engine._view.width = w;
|
|
180
|
+
},
|
|
181
|
+
get height() {
|
|
182
|
+
return engine._view.height;
|
|
183
|
+
},
|
|
184
|
+
set height(h) {
|
|
185
|
+
engine._view.height = h;
|
|
186
|
+
},
|
|
187
|
+
get dpr() {
|
|
188
|
+
return engine._view.dpr;
|
|
189
|
+
},
|
|
190
|
+
set dpr(d) {
|
|
191
|
+
engine._view.dpr = d;
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
const observer = observeCanvasSize(o.canvas, sizeTarget, () => (o.renderOnResize ? this.loop.renderNow() : this.loop.invalidate()), o.maxPixelRatio);
|
|
195
|
+
this.undo.push(() => observer.disconnect());
|
|
196
|
+
if (o.orbitControls) {
|
|
197
|
+
if (!('orbitPixels' in o.view))
|
|
198
|
+
throw new Error('Engine: orbitControls needs an orbit view');
|
|
199
|
+
// The controls act only while the view they were made for is the current one (see `setView`).
|
|
200
|
+
const orbit = o.view;
|
|
201
|
+
const gated = {
|
|
202
|
+
get dpr() {
|
|
203
|
+
return orbit.dpr;
|
|
204
|
+
},
|
|
205
|
+
orbitPixels: (dx, dy) => {
|
|
206
|
+
if (engine._view === orbit)
|
|
207
|
+
orbit.orbitPixels(dx, dy);
|
|
208
|
+
},
|
|
209
|
+
zoomBy: (f) => {
|
|
210
|
+
if (engine._view === orbit)
|
|
211
|
+
orbit.zoomBy(f);
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
this.undo.push(bindOrbitInput(o.canvas, gated, { onChange: () => this.loop.invalidate() }));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** The view the renderer draws through. */
|
|
218
|
+
get view() {
|
|
219
|
+
return this._view;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Draws through another view from the next frame on (for example a following view while a body walks, and the
|
|
223
|
+
* orbit view again afterwards). The new view takes the canvas size at once. `orbitControls` act only while the
|
|
224
|
+
* view they were made for is the current one; other controls are the caller's to switch (`WalkInput.enabled`).
|
|
225
|
+
*/
|
|
226
|
+
setView(view) {
|
|
227
|
+
if (view === this._view)
|
|
228
|
+
return;
|
|
229
|
+
view.width = this._view.width;
|
|
230
|
+
view.height = this._view.height;
|
|
231
|
+
view.dpr = this._view.dpr;
|
|
232
|
+
this._view = view;
|
|
233
|
+
this.loop.invalidate();
|
|
234
|
+
}
|
|
235
|
+
/** Adds a layer; layers draw in the order they are added, the precipitation layer stays last. */
|
|
236
|
+
add(layer) {
|
|
237
|
+
this.renderer.add(layer);
|
|
238
|
+
if (this.precipitation) {
|
|
239
|
+
this.renderer.remove(this.precipitation);
|
|
240
|
+
this.renderer.add(this.precipitation);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Draws the sky behind the scene (`SkyLayer.behindScene`): only where no layer drawn before it has drawn, instead
|
|
245
|
+
* of across the whole screen before everything. Moves the sky layer to just before `layer`, which should be the
|
|
246
|
+
* first layer that blends or does not write depth (water, glass, particles); null puts it after every layer added
|
|
247
|
+
* so far (before the precipitation). The image stays the same when every layer before it is opaque. Layers added
|
|
248
|
+
* afterwards are drawn after the sky. Does nothing without `sky`.
|
|
249
|
+
*/
|
|
250
|
+
moveSkyBefore(layer) {
|
|
251
|
+
const sky = this.sky;
|
|
252
|
+
if (!sky)
|
|
253
|
+
return;
|
|
254
|
+
sky.behindScene = true;
|
|
255
|
+
this.renderer.remove(sky);
|
|
256
|
+
if (layer)
|
|
257
|
+
this.renderer.addBefore(sky, layer);
|
|
258
|
+
else if (this.precipitation)
|
|
259
|
+
this.renderer.addBefore(sky, this.precipitation);
|
|
260
|
+
else
|
|
261
|
+
this.renderer.add(sky);
|
|
262
|
+
}
|
|
263
|
+
/** Starts the frame loop: continuous while something moves, otherwise only on `invalidate`. */
|
|
264
|
+
start() {
|
|
265
|
+
this.loop.start();
|
|
266
|
+
}
|
|
267
|
+
stop() {
|
|
268
|
+
this.loop.stop();
|
|
269
|
+
}
|
|
270
|
+
/** Asks for a frame at the browser's next tick. */
|
|
271
|
+
invalidate() {
|
|
272
|
+
this.loop.invalidate();
|
|
273
|
+
}
|
|
274
|
+
/** Renders right now with the current state (no ticks, no clock movement). */
|
|
275
|
+
renderNow() {
|
|
276
|
+
syncCanvasSize(this.canvas, this.view, this.maxPixelRatio);
|
|
277
|
+
this.extract();
|
|
278
|
+
this.renderer.render(this.view);
|
|
279
|
+
}
|
|
280
|
+
/** One whole frame without waiting for the browser, including the image. */
|
|
281
|
+
frame(realSeconds) {
|
|
282
|
+
const ticks = super.frame(realSeconds);
|
|
283
|
+
this.renderer.render(this.view);
|
|
284
|
+
return ticks;
|
|
285
|
+
}
|
|
286
|
+
dispose() {
|
|
287
|
+
if (this.disposed)
|
|
288
|
+
return;
|
|
289
|
+
this.loop.stop();
|
|
290
|
+
while (this.undo.length > 0)
|
|
291
|
+
this.undo.pop()();
|
|
292
|
+
for (const layer of [...this.renderer.layers].reverse())
|
|
293
|
+
this.renderer.remove(layer);
|
|
294
|
+
this.renderer.beforeFrame.length = 0;
|
|
295
|
+
super.dispose();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/** Creates the engine for a canvas: device, renderer, environment, view, simulation and the frame loop. */
|
|
299
|
+
export function createEngine(options) {
|
|
300
|
+
return new Engine(options);
|
|
301
|
+
}
|
|
302
|
+
/** Creates the same engine without canvas and GPU: simulation and environment only, stepped by `frame`. */
|
|
303
|
+
export function createHeadlessEngine(options = {}) {
|
|
304
|
+
return new HeadlessEngine(options);
|
|
305
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RGB } from '../base/math.ts';
|
|
2
|
+
import type { EnvSlot, FrameEnvironment } from '../base/env-slot.ts';
|
|
3
|
+
import type { Renderer } from '../render/index.ts';
|
|
4
|
+
import type { EnvFrameBlock, Environment } from '../env/index.ts';
|
|
5
|
+
export interface EnvironmentLinkOptions {
|
|
6
|
+
/** Background colour in full daylight; the environment darkens it at night. */
|
|
7
|
+
background: RGB;
|
|
8
|
+
/** The assembled Frame block; the renderer must have been created with it as `frameSlots`. */
|
|
9
|
+
block: EnvFrameBlock;
|
|
10
|
+
}
|
|
11
|
+
export declare class EnvironmentLink {
|
|
12
|
+
readonly env: Environment;
|
|
13
|
+
readonly block: EnvFrameBlock;
|
|
14
|
+
/** Environment of the last frame. */
|
|
15
|
+
frame: FrameEnvironment | null;
|
|
16
|
+
background: RGB;
|
|
17
|
+
private slotValues;
|
|
18
|
+
constructor(renderer: Renderer, env: Environment, options: EnvironmentLinkOptions);
|
|
19
|
+
/** Values a slot computed for the last frame, or undefined before the first frame. */
|
|
20
|
+
value<V>(slot: EnvSlot<any, V>): V | undefined;
|
|
21
|
+
private update;
|
|
22
|
+
}
|