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
package/docs/recipes.md
ADDED
|
@@ -0,0 +1,521 @@
|
|
|
1
|
+
# Recipes
|
|
2
|
+
|
|
3
|
+
Short, type-checked samples for common extensions. Each says where the code belongs. After any of them, run the checks in [Tools](tools.md).
|
|
4
|
+
|
|
5
|
+
## Add a layer
|
|
6
|
+
|
|
7
|
+
Most layers are a `MeshLayer` or an `InstancedLayer` ([Rendering](rendering.md#layers)). Write your own `Layer` when the draw needs logic: several meshes, a uniform per draw, a pass-specific mesh.
|
|
8
|
+
|
|
9
|
+
Where: the app or example that needs it. A generic layer may go into `render` (no use-case words).
|
|
10
|
+
|
|
11
|
+
<!-- check: wrap -->
|
|
12
|
+
```ts
|
|
13
|
+
import type { MeshData } from 'wallhammer-web/base';
|
|
14
|
+
import type { Mesh } from 'wallhammer-web/gpu';
|
|
15
|
+
import { SHADOW_MAP_UNIT } from 'wallhammer-web/render';
|
|
16
|
+
import type { DrawContext, Layer, Material, Renderer } from 'wallhammer-web/render';
|
|
17
|
+
declare const renderer: Renderer;
|
|
18
|
+
declare const material: Material;
|
|
19
|
+
declare const shapes: MeshData[];
|
|
20
|
+
|
|
21
|
+
/** Draws each mesh with its own brightness. */
|
|
22
|
+
class StripLayer implements Layer {
|
|
23
|
+
readonly meshes: Mesh[];
|
|
24
|
+
readonly material: Material;
|
|
25
|
+
constructor(r: Renderer, m: Material, data: readonly MeshData[]) {
|
|
26
|
+
this.material = m;
|
|
27
|
+
this.meshes = data.map((d) => r.createMesh(d, m));
|
|
28
|
+
}
|
|
29
|
+
draw(ctx: DrawContext): void {
|
|
30
|
+
const p = this.material.program(ctx.pass);
|
|
31
|
+
if (!p) return;
|
|
32
|
+
const state = this.material.state[ctx.pass];
|
|
33
|
+
if (state) ctx.device.setState(state);
|
|
34
|
+
ctx.device.use(p);
|
|
35
|
+
// MeshLayer does the same two lines: depth programs need the light matrix, others the shadow map unit.
|
|
36
|
+
if (ctx.pass === 'shadow') p.mat4('u_lightRender', ctx.lightRender);
|
|
37
|
+
else p.int('u_shadowMap', SHADOW_MAP_UNIT);
|
|
38
|
+
this.meshes.forEach((mesh, i) => {
|
|
39
|
+
p.float('u_brightness', 0.5 + 0.1 * i);
|
|
40
|
+
ctx.device.draw(mesh);
|
|
41
|
+
ctx.count(mesh.triangles);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
renderer.add(new StripLayer(renderer, material, shapes));
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Checklist:
|
|
49
|
+
|
|
50
|
+
- Return early for passes you do not draw in.
|
|
51
|
+
- If the layer culls, implement `cull` and remember it runs only when the shadow map is refitted.
|
|
52
|
+
- Call `engine.invalidate()` after changing what the layer draws.
|
|
53
|
+
|
|
54
|
+
## Add a material
|
|
55
|
+
|
|
56
|
+
A material is a `MaterialDesc`. Instanced or displaced geometry needs its own shadow program, because `'depth'` only transforms `a_pos`.
|
|
57
|
+
|
|
58
|
+
Where: next to the generator whose layout it draws (content module or app).
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
import type { MaterialDesc, VertexLayout } from 'wallhammer-web/base';
|
|
62
|
+
import { DEPTH_FS } from 'wallhammer-web/render';
|
|
63
|
+
|
|
64
|
+
/** One instance: base x, y, z and a height. */
|
|
65
|
+
export const POST_INSTANCE: VertexLayout = { stride: 16, attribs: [{ name: 'i_base', size: 4, type: 'f32', offset: 0 }] };
|
|
66
|
+
export const POST_ATTRIBS = { a_pos: 0, a_normal: 1, i_base: 2 } as const;
|
|
67
|
+
|
|
68
|
+
const PLACE = `vec3 placed(vec3 p) { return i_base.xyz + vec3(p.x, p.y * i_base.w, p.z); }`;
|
|
69
|
+
|
|
70
|
+
export const POST_MATERIAL: MaterialDesc = {
|
|
71
|
+
name: 'post',
|
|
72
|
+
attribs: POST_ATTRIBS,
|
|
73
|
+
passes: {
|
|
74
|
+
shadow: {
|
|
75
|
+
vs: `#include <header>
|
|
76
|
+
uniform mat4 u_lightRender;
|
|
77
|
+
in vec3 a_pos;
|
|
78
|
+
in vec4 i_base;
|
|
79
|
+
${PLACE}
|
|
80
|
+
void main() { gl_Position = u_lightRender * vec4(placed(a_pos), 1.0); }`,
|
|
81
|
+
fs: DEPTH_FS,
|
|
82
|
+
},
|
|
83
|
+
color: {
|
|
84
|
+
vs: `#include <header>
|
|
85
|
+
in vec3 a_pos;
|
|
86
|
+
in vec3 a_normal;
|
|
87
|
+
in vec4 i_base;
|
|
88
|
+
out vec3 v_world;
|
|
89
|
+
out vec3 v_normal;
|
|
90
|
+
${PLACE}
|
|
91
|
+
void main() {
|
|
92
|
+
v_world = placed(a_pos);
|
|
93
|
+
v_normal = a_normal;
|
|
94
|
+
gl_Position = u_viewProj * vec4(v_world, 1.0);
|
|
95
|
+
}`,
|
|
96
|
+
fs: `#include <header>
|
|
97
|
+
#include <noise>
|
|
98
|
+
#include <lighting>
|
|
99
|
+
uniform vec3 u_albedo;
|
|
100
|
+
in vec3 v_world;
|
|
101
|
+
in vec3 v_normal;
|
|
102
|
+
out vec4 o_color;
|
|
103
|
+
void main() {
|
|
104
|
+
vec3 n = normalize(v_normal);
|
|
105
|
+
vec3 c = toDisplay(u_albedo * lightOn(n, sunVisibility(v_world, n, 1.0), 1.0));
|
|
106
|
+
o_color = vec4(applyFog(c, v_world), 1.0);
|
|
107
|
+
}`,
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Draw it with an `InstancedLayer` whose `bounds` is `{ position: 0, radius: 0.3, height: 3 }` for this layout. GLSL errors appear only in the browser; the type check does not see them.
|
|
114
|
+
|
|
115
|
+
## Draw moving copies with instance sets
|
|
116
|
+
|
|
117
|
+
For things the app moves itself (parts posed by its own code, props that spin or swing), use an `InstanceSet` and an
|
|
118
|
+
`InstanceSetLayer` ([Rendering](rendering.md#instance-sets)). A set is one mesh and up to `capacity` records; each
|
|
119
|
+
record is a 3 × 4 matrix (the layout of a `Mat34`, [Concepts](concepts.md#3--4-matrices)) and four free numbers. One
|
|
120
|
+
instanced draw per visible set, shadows included, no culling.
|
|
121
|
+
|
|
122
|
+
Where: the app or example. The material is the app's; it only adds the instance attributes.
|
|
123
|
+
|
|
124
|
+
1. Make the set: `instanceSet(mesh, capacity, { name?, castShadow?, visible? })`.
|
|
125
|
+
2. Give the material the attributes: `INSTANCE_SET_GLSL` in the vertex shaders of every pass it draws (the shadow pass
|
|
126
|
+
too, `'depth'` does not know instances) and `instanceSetAttribs(first)` in its `attribs`, `first` being the count of
|
|
127
|
+
the mesh's own attributes.
|
|
128
|
+
3. Add the set to an `InstanceSetLayer` and the layer to the renderer.
|
|
129
|
+
4. Each frame (or tick) write the records with `writeInstance(set, i, m, a, b, c, d)` (or append with
|
|
130
|
+
`pushInstance`, which sets `dirty` and grows `count`), set `count`, and set `dirty = true`. The layer uploads at the
|
|
131
|
+
start of the next frame; nothing is allocated.
|
|
132
|
+
|
|
133
|
+
<!-- check: wrap -->
|
|
134
|
+
```ts
|
|
135
|
+
import { mat34FromQuat, quatAxisTurns } from 'wallhammer-web/base';
|
|
136
|
+
import type { MaterialDesc, MeshData } from 'wallhammer-web/base';
|
|
137
|
+
import { DEPTH_FS, INSTANCE_SET_GLSL, InstanceSetLayer, instanceSet, instanceSetAttribs, writeInstance } from 'wallhammer-web/render';
|
|
138
|
+
import type { Renderer } from 'wallhammer-web/render';
|
|
139
|
+
declare const renderer: Renderer;
|
|
140
|
+
declare const shape: MeshData; // a_pos, a_normal
|
|
141
|
+
|
|
142
|
+
const SPIN_MATERIAL: MaterialDesc = {
|
|
143
|
+
name: 'spin',
|
|
144
|
+
attribs: { a_pos: 0, a_normal: 1, ...instanceSetAttribs(2) },
|
|
145
|
+
passes: {
|
|
146
|
+
shadow: {
|
|
147
|
+
vs: `#include <header>
|
|
148
|
+
uniform mat4 u_lightRender;
|
|
149
|
+
in vec3 a_pos;
|
|
150
|
+
${INSTANCE_SET_GLSL}
|
|
151
|
+
void main() { gl_Position = u_lightRender * vec4(instancePoint(a_pos), 1.0); }`,
|
|
152
|
+
fs: DEPTH_FS,
|
|
153
|
+
},
|
|
154
|
+
color: {
|
|
155
|
+
vs: `#include <header>
|
|
156
|
+
in vec3 a_pos;
|
|
157
|
+
in vec3 a_normal;
|
|
158
|
+
${INSTANCE_SET_GLSL}
|
|
159
|
+
out vec3 v_world;
|
|
160
|
+
out vec3 v_normal;
|
|
161
|
+
out vec3 v_tint;
|
|
162
|
+
void main() {
|
|
163
|
+
v_world = instancePoint(a_pos);
|
|
164
|
+
v_normal = instanceNormal(a_normal);
|
|
165
|
+
v_tint = i_extra.rgb;
|
|
166
|
+
gl_Position = u_viewProj * vec4(v_world, 1.0);
|
|
167
|
+
}`,
|
|
168
|
+
fs: `#include <header>
|
|
169
|
+
#include <noise>
|
|
170
|
+
#include <lighting>
|
|
171
|
+
in vec3 v_world;
|
|
172
|
+
in vec3 v_normal;
|
|
173
|
+
in vec3 v_tint;
|
|
174
|
+
out vec4 o_color;
|
|
175
|
+
void main() {
|
|
176
|
+
vec3 n = normalize(v_normal);
|
|
177
|
+
o_color = vec4(applyFog(toDisplay(v_tint * lightOn(n, sunVisibility(v_world, n, 1.0), 1.0)), v_world), 1.0);
|
|
178
|
+
}`,
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const set = instanceSet(shape, 8, { name: 'spinners' });
|
|
184
|
+
const layer = new InstanceSetLayer(renderer, { material: renderer.material(SPIN_MATERIAL) });
|
|
185
|
+
layer.add(set);
|
|
186
|
+
renderer.add(layer);
|
|
187
|
+
|
|
188
|
+
const m = new Float32Array(12);
|
|
189
|
+
/** Call once per tick with the simulation time in seconds. */
|
|
190
|
+
function pose(seconds: number): void {
|
|
191
|
+
for (let i = 0; i < set.capacity; i++) {
|
|
192
|
+
mat34FromQuat(quatAxisTurns([0, 1, 0], seconds * 0.25 + i / set.capacity), m, i * 3, 0, 0);
|
|
193
|
+
writeInstance(set, i, m, 0.8, 0.6 + 0.05 * i, 0.4);
|
|
194
|
+
}
|
|
195
|
+
set.count = set.capacity;
|
|
196
|
+
set.dirty = true;
|
|
197
|
+
}
|
|
198
|
+
pose(0);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Checklist:
|
|
202
|
+
|
|
203
|
+
- Keep sets few: each visible set is a draw call per pass.
|
|
204
|
+
- A set may be added to several layers (another material for an outline); the records are uploaded once.
|
|
205
|
+
- `castShadow: false` keeps a set out of the shadow pass; a layer without a shadow pass never redraws shadows.
|
|
206
|
+
- The layer does not cull. For many still copies spread over a large area use `InstancedLayer` or `InstanceCellLayer`.
|
|
207
|
+
|
|
208
|
+
## Add an environment slot
|
|
209
|
+
|
|
210
|
+
Where: the content module or app that uses the value. Slots are fixed when the engine is created.
|
|
211
|
+
|
|
212
|
+
```ts
|
|
213
|
+
import type { EnvSlot } from 'wallhammer-web/base';
|
|
214
|
+
import type { Engine } from 'wallhammer-web/runtime';
|
|
215
|
+
|
|
216
|
+
export interface HazeParams {
|
|
217
|
+
strength: number;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** `u_haze.x`: haze strength that grows with fog and at low sun. */
|
|
221
|
+
export function hazeSlot(params: Partial<HazeParams> = {}): EnvSlot<HazeParams, number> {
|
|
222
|
+
return {
|
|
223
|
+
name: 'haze',
|
|
224
|
+
params: { strength: 1, ...params },
|
|
225
|
+
fields: [{ member: 'u_haze', order: 110, lanes: 'x', doc: 'x haze 0–1' }],
|
|
226
|
+
compute: (env, p) => p.strength * Math.min(1, env.fog + Math.max(0, 10 - env.sunElevationDeg) / 30),
|
|
227
|
+
lanes: (v) => [v],
|
|
228
|
+
glsl: `float hazeAmount() { return u_haze.x; }`,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function readHaze(engine: Engine, slot: EnvSlot<HazeParams, number>): number {
|
|
233
|
+
return engine.link.value(slot) ?? 0;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Register it with `createEngine({ slots: [hazeSlot()] })`. Shaders then read `u_haze` directly, or `#include <haze>` for `hazeAmount()`.
|
|
238
|
+
|
|
239
|
+
- `order` places the member within its region. The engine's own members use 10 to 90; the minimal example's glow slot uses 100. Equal orders are sorted by member name. A member declared twice, or a lane filled twice, throws.
|
|
240
|
+
- To fill a free lane of an engine member instead, set `member: 'u_wind', lanes: 'zw'` and leave out `order`.
|
|
241
|
+
- If the value changes shadows, add `shadowKey`.
|
|
242
|
+
|
|
243
|
+
## Add a generator
|
|
244
|
+
|
|
245
|
+
Where: `gen-mesh` or `gen-tex` if it is generic and names no use case; `gen-arch`, `gen-ground` or `gen-vegetation` if it is content; the app otherwise.
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
import type { InstanceData, VertexLayout } from 'wallhammer-web/base';
|
|
249
|
+
import { hashInts, unit } from 'wallhammer-web/gen-core';
|
|
250
|
+
import type { HeightField } from 'wallhammer-web/gen-core';
|
|
251
|
+
|
|
252
|
+
export const MARKER_LAYOUT: VertexLayout = { stride: 16, attribs: [{ name: 'i_base', size: 4, type: 'f32', offset: 0 }] };
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* One marker per grid cell where the hash allows it. Keys are the cell indices (input), never a running counter,
|
|
256
|
+
* so a cell's marker does not depend on its neighbours or on iteration order.
|
|
257
|
+
*/
|
|
258
|
+
export function scatterMarkers(field: HeightField, seed: number, gridM: number, share: number): InstanceData {
|
|
259
|
+
const out: number[] = [];
|
|
260
|
+
const cols = Math.floor(((field.cols - 1) * field.step) / gridM);
|
|
261
|
+
const rows = Math.floor(((field.rows - 1) * field.step) / gridM);
|
|
262
|
+
for (let j = 0; j < rows; j++) {
|
|
263
|
+
for (let i = 0; i < cols; i++) {
|
|
264
|
+
if (unit(hashInts(seed, i, j, 0)) > share) continue;
|
|
265
|
+
const x = (i + 0.2 + 0.6 * unit(hashInts(seed, i, j, 1))) * gridM;
|
|
266
|
+
const z = (j + 0.2 + 0.6 * unit(hashInts(seed, i, j, 2))) * gridM;
|
|
267
|
+
out.push(x, field.heightAt(x, z), z, 1 + 2 * unit(hashInts(seed, i, j, 3)));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return { layout: MARKER_LAYOUT, data: Float32Array.from(out) };
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Then pin its output with a golden recipe. In `tools/golden/engine/golden.ts`, import it by relative path and add an entry to `RECIPES` (the file has a local `hash` helper):
|
|
275
|
+
|
|
276
|
+
<!-- check: skip -->
|
|
277
|
+
```ts
|
|
278
|
+
'gen-mesh scatterMarkers': () => hash(scatterMarkers(ground, 7, 8, 0.4).data),
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Run the golden test, read the `MISSING` line, accept as described in [Tools](tools.md#add-an-entry).
|
|
282
|
+
|
|
283
|
+
## Add a destructible structure
|
|
284
|
+
|
|
285
|
+
A `StructureDesc` can come from `gen-arch` or be written by hand. Lengths are integer centimetres; parts and bonds must be sorted.
|
|
286
|
+
|
|
287
|
+
```ts
|
|
288
|
+
import { PART_FLAG, PART_ROLE } from 'wallhammer-web/base';
|
|
289
|
+
import type { StructureDesc } from 'wallhammer-web/base';
|
|
290
|
+
import { applyEvent, destructionEvent, intactState, stateHash } from 'wallhammer-web/sim/destruction';
|
|
291
|
+
|
|
292
|
+
/** An axis-aligned box piece in centimetres, corners counter-clockwise. */
|
|
293
|
+
const box = (x0: number, z0: number, x1: number, z1: number, y0: number, y1: number) => ({ xz: Int32Array.from([x0, z0, x1, z0, x1, z1, x0, z1]), y0, y1 });
|
|
294
|
+
|
|
295
|
+
const pier: StructureDesc = {
|
|
296
|
+
id: 1,
|
|
297
|
+
keepOut: false,
|
|
298
|
+
groundY: 0,
|
|
299
|
+
cx: 50,
|
|
300
|
+
cz: 50,
|
|
301
|
+
outline: Int32Array.from([0, 0, 100, 0, 100, 100, 0, 100]),
|
|
302
|
+
materials: [{ crack: 200, breach: 500, gone: 900, density: 2000 }],
|
|
303
|
+
parts: [
|
|
304
|
+
{ key: 1, role: PART_ROLE.foundation, material: 0, mass: 2000, piece: box(0, 0, 100, 100, -50, 0), flags: PART_FLAG.anchor | PART_FLAG.indestructible, maxSpan: 255 },
|
|
305
|
+
{ key: 2, role: PART_ROLE.bearing, material: 0, mass: 1200, piece: box(0, 0, 100, 30, 0, 300), flags: 0, maxSpan: 255 },
|
|
306
|
+
],
|
|
307
|
+
bonds: [{ a: 1, b: 2, bearing: true, x: 50, y: 0, z: 15, area: 3000 }],
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
const ev = destructionEvent({ position: [0.5, 1.5, 0.15], radius: 1, energy: 1000, seed: 3, tick: 1 });
|
|
311
|
+
const res = applyEvent(pier, intactState(pier.id), ev);
|
|
312
|
+
console.log(res.changes.map((c) => `${c.part}: ${c.from} → ${c.to}`), stateHash(res.state));
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
To draw a hand-written structure you need your own mesh; `gen-arch`'s damage drawing works only for structures it generated. Test the description in Node first ([Headless use](headless.md)).
|
|
316
|
+
|
|
317
|
+
## Add a streamed cell provider
|
|
318
|
+
|
|
319
|
+
A provider delivers `CellContent` for a cell id. Write one when cells come from somewhere new: a server, an IndexedDB cache, a different file layout. Where: the app or example.
|
|
320
|
+
|
|
321
|
+
```ts
|
|
322
|
+
import type { CellId } from 'wallhammer-web/gen-core';
|
|
323
|
+
import { cellKey } from 'wallhammer-web/gen-core';
|
|
324
|
+
import { decodeCell } from 'wallhammer-web/world';
|
|
325
|
+
import type { CellProvider, CellResult } from 'wallhammer-web/world';
|
|
326
|
+
|
|
327
|
+
/** Reads cells from an HTTP endpoint that serves WHC1 bytes under `<base>?cell=<key>`, caching them in memory. */
|
|
328
|
+
export class EndpointCellProvider implements CellProvider {
|
|
329
|
+
readonly name = 'endpoint';
|
|
330
|
+
readonly blocking = false;
|
|
331
|
+
private readonly cache = new Map<string, Uint8Array>();
|
|
332
|
+
private readonly base: string;
|
|
333
|
+
constructor(base: string) {
|
|
334
|
+
this.base = base;
|
|
335
|
+
}
|
|
336
|
+
async request(cell: CellId, signal: AbortSignal): Promise<CellResult> {
|
|
337
|
+
const key = cellKey(cell);
|
|
338
|
+
const hit = this.cache.get(key);
|
|
339
|
+
if (hit) return { content: decodeCell(hit), bytes: hit, origin: 'memory' };
|
|
340
|
+
const res = await fetch(`${this.base}?cell=${encodeURIComponent(key)}`, { signal });
|
|
341
|
+
if (!res.ok) throw new Error(`cell ${key}: HTTP ${res.status}`);
|
|
342
|
+
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
343
|
+
this.cache.set(key, bytes);
|
|
344
|
+
return { content: decodeCell(bytes), bytes, origin: 'file' };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
```
|
|
348
|
+
|
|
349
|
+
Rules:
|
|
350
|
+
|
|
351
|
+
- `request` must reject with an error named `AbortError` when the signal fires first. `fetch` does that for you.
|
|
352
|
+
- Set `blocking: true` only if `request` does its work synchronously; the streamer then calls it within a frame budget.
|
|
353
|
+
- Content must be byte-identical to what the generator would produce, or seams and level changes break. Compare with `GeneratorCellProvider.bytes(cell)` in a Node script, as `examples/endless/scripts/verify.ts` does.
|
|
354
|
+
|
|
355
|
+
`fetch` in a provider is browser code. Keep such a provider in the app, or in `platform` if it becomes generic.
|
|
356
|
+
|
|
357
|
+
## Add an animated rig
|
|
358
|
+
|
|
359
|
+
Where: the app (the campus app keeps its rigs in one folder of its own). A rig is plain data; the layer animates it.
|
|
360
|
+
|
|
361
|
+
```ts
|
|
362
|
+
import type { RigDesc, RigPart } from 'wallhammer-web/base';
|
|
363
|
+
|
|
364
|
+
/** A two-wheeled handcart pushed by nobody: a box on an axle, wheels that roll, a handle that sways a little. */
|
|
365
|
+
export function handcartRig(): RigDesc {
|
|
366
|
+
const wood: readonly [number, number, number] = [0.52, 0.38, 0.24];
|
|
367
|
+
const parts: RigPart[] = [
|
|
368
|
+
{ shape: { kind: 'box', size: [0.9, 0.35, 1.3] }, at: [0, 0.62, 0], slot: 0, tint: [1, 1, 1] },
|
|
369
|
+
{ shape: { kind: 'roller', radius: 0.32, width: 0.08, sides: 10 }, at: [-0.52, 0.32, 0], motion: 'spin', amount: 0.32, tint: [0.2, 0.2, 0.2] },
|
|
370
|
+
{ shape: { kind: 'roller', radius: 0.32, width: 0.08, sides: 10 }, at: [0.52, 0.32, 0], motion: 'spin', amount: 0.32, tint: [0.2, 0.2, 0.2] },
|
|
371
|
+
{ shape: { kind: 'rod', dir: [0, 0.35, -0.94], length: 1.1, thickness: 0.05 }, at: [0, 0.75, -0.6], motion: 'wobble', amount: 0.03, tint: wood, nearOnly: true },
|
|
372
|
+
];
|
|
373
|
+
return { name: 'handcart', parts, segments: [{ shadow: [0.5, 0.75, 0] }], height: 1.1, radius: 1.4, bob: 0.01 };
|
|
374
|
+
}
|
|
375
|
+
```
|
|
376
|
+
|
|
377
|
+
1. Local axes: x right, y up, z forward, origin on the ground under the anchor (where the mover's position is).
|
|
378
|
+
2. Give every wheel `spin` with its radius as `amount`, so it rolls without slipping at any speed.
|
|
379
|
+
3. Draw it with `RigLayer.addKind(rig, capacity)` and records from `extractPose` ([Rigs](rigs.md#drawing-instances)).
|
|
380
|
+
4. Pin it with a golden recipe that hashes `rigMesh(desc, false)` and `rigMesh(desc, true)` from `gen-mesh`, as the `gen-actor rigs` entry does.
|
|
381
|
+
|
|
382
|
+
## Drive entities with agendas
|
|
383
|
+
|
|
384
|
+
Where: the app. The engine routes, moves and times; the app says what places are and what happens there. This one gives people a ride: they walk to a stop, wait there until a vehicle of a replayed timetable dwells, get carried to the next place and walk from there.
|
|
385
|
+
|
|
386
|
+
<!-- check: wrap -->
|
|
387
|
+
```ts
|
|
388
|
+
import { Agendas, Movers, NavGraph, Simulation } from 'wallhammer-web/sim';
|
|
389
|
+
import type { AgendaPlace } from 'wallhammer-web/sim';
|
|
390
|
+
declare const nav: NavGraph;
|
|
391
|
+
declare const places: AgendaPlace[];
|
|
392
|
+
/** Is a vehicle dwelling at this place at agenda time `t` (seconds)? From the app's timetable. */
|
|
393
|
+
declare const dwelling: (place: number, t: number) => boolean;
|
|
394
|
+
|
|
395
|
+
const hz = 10;
|
|
396
|
+
const MODE = { walk: 0, board: 1, alight: 2 } as const;
|
|
397
|
+
const sim = new Simulation({ seed: 'rides', hz });
|
|
398
|
+
const people = new Movers('people', { hz, collision: 'agents', width: 0.5 });
|
|
399
|
+
let WAITING = -1;
|
|
400
|
+
let RIDING = -1;
|
|
401
|
+
const agendas = new Agendas('people.agendas', {
|
|
402
|
+
movers: people,
|
|
403
|
+
nav,
|
|
404
|
+
places,
|
|
405
|
+
origin: 7 * 3600,
|
|
406
|
+
spawn: () => ({ cruise: 1.3, gap: 0.8 }),
|
|
407
|
+
// Somebody whose agenda starts with getting off is on board, not standing at the stop.
|
|
408
|
+
onAppear: (e, a) => (a.activity(e).mode === MODE.alight ? RIDING : undefined),
|
|
409
|
+
onArrive: (e, a) => (a.activity(e).mode === MODE.board ? WAITING : undefined),
|
|
410
|
+
});
|
|
411
|
+
WAITING = agendas.states.define('waiting', {
|
|
412
|
+
update: (e, a) => {
|
|
413
|
+
if (!dwelling(a.activity(e).place, a.time)) return;
|
|
414
|
+
a.hide(e);
|
|
415
|
+
return a.arriveNext(e); // carried to the next activity: its `onArrive` runs (or `stay`)
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
RIDING = agendas.states.define('riding', {
|
|
419
|
+
enter: (e, a) => a.hide(e),
|
|
420
|
+
update: (e, a) => {
|
|
421
|
+
const act = a.activity(e);
|
|
422
|
+
if (a.now < act.at || !dwelling(act.place, a.time)) return;
|
|
423
|
+
const p = a.places[act.place]!;
|
|
424
|
+
a.show(e, p.x ?? 0, p.z ?? 0);
|
|
425
|
+
return a.leave(e); // walk on to the next activity
|
|
426
|
+
},
|
|
427
|
+
});
|
|
428
|
+
sim.addState(people);
|
|
429
|
+
sim.addState(agendas);
|
|
430
|
+
sim.addSystem('people', people.system);
|
|
431
|
+
sim.addSystem('agendas', agendas.system);
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
- Add every entity (`agendas.add`) before the first tick; a snapshot holds the table, not the activity lists.
|
|
435
|
+
- Keep app state that matters for the outcome in the agenda table (`setData`, `setTimer`) or recompute it from the tick; anything else breaks `seek` and snapshots.
|
|
436
|
+
- Places marked `inside` cost nothing while people stay there. Thousands of entities with a few hundred out of doors run at about a millisecond per tick.
|
|
437
|
+
- Listen to `agenda.unreachable` (a place off the graph's largest component) and count it.
|
|
438
|
+
|
|
439
|
+
## Feed agendas from external data
|
|
440
|
+
|
|
441
|
+
Where: a preparation script in the app writes a file; the app reads it. The engine never parses files. A format that keeps the external model out of the app: places as kinds and points in local metres, people as activities in seconds of the day.
|
|
442
|
+
|
|
443
|
+
<!-- check: skip -->
|
|
444
|
+
```ts
|
|
445
|
+
{
|
|
446
|
+
"version": 1,
|
|
447
|
+
"source": "what produced it",
|
|
448
|
+
"notes": ["what was assumed, one line each"],
|
|
449
|
+
"places": [{ "kind": "entrance", "x": 412.5, "z": 88.0 }, { "kind": "stop", "x": 520.1, "z": 131.4, "stop": "Name" }],
|
|
450
|
+
"people": [{ "activities": [
|
|
451
|
+
{ "place": 1, "at": 27000, "stay": 0, "mode": "alight" },
|
|
452
|
+
{ "place": 0, "at": 27090, "stay": 30600 },
|
|
453
|
+
{ "place": 1, "at": 57700, "stay": 0, "mode": "board" }
|
|
454
|
+
] }]
|
|
455
|
+
}
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
The app maps it onto the engine:
|
|
459
|
+
|
|
460
|
+
<!-- check: wrap -->
|
|
461
|
+
```ts
|
|
462
|
+
import type { AgendaEntity, AgendaPlace } from 'wallhammer-web/sim';
|
|
463
|
+
declare const file: { places: { kind: string; x: number; z: number }[]; people: { activities: { place: number; at: number; stay: number; mode?: string }[] }[] };
|
|
464
|
+
/** Nearest node of the walking graph to a point (the app's own lookup). */
|
|
465
|
+
declare const nearestNode: (x: number, z: number) => number;
|
|
466
|
+
|
|
467
|
+
const MODES: Record<string, number> = { walk: 0, board: 1, alight: 2 };
|
|
468
|
+
const places: AgendaPlace[] = file.places.map((p) => ({ node: nearestNode(p.x, p.z), x: p.x, z: p.z, inside: p.kind === 'entrance' }));
|
|
469
|
+
const entities: AgendaEntity[] = file.people.map((person, i) => ({
|
|
470
|
+
tag: i,
|
|
471
|
+
activities: person.activities.map((a) => ({ place: a.place, at: a.at, stay: a.stay, mode: MODES[a.mode ?? 'walk'] ?? 0 })),
|
|
472
|
+
}));
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
- Snap points to the graph in the app (doors to path ends at houses, parking to bays, the border to graph nodes near the edge) and say how far each moved.
|
|
476
|
+
- Write down in `notes` what the source does not have (stay lengths, the way back, which vehicle someone rides) and what was assumed instead. The campus app has such a preparation script for its synthetic population.
|
|
477
|
+
- Keep a synthetic fallback for when the file is missing, so the app runs without the external model.
|
|
478
|
+
|
|
479
|
+
## Report loading progress
|
|
480
|
+
|
|
481
|
+
A `Progress` is a list of named stages with weights. Whoever does the work advances its stage; whoever shows it
|
|
482
|
+
subscribes. It has no DOM and no clock, so a worker job, an upload or a shader compile can feed it, and the page turns
|
|
483
|
+
the report into a bar.
|
|
484
|
+
|
|
485
|
+
Where: the app. The engine only provides the report; the stages, their labels (in the app's language) and the screen
|
|
486
|
+
belong to the app.
|
|
487
|
+
|
|
488
|
+
<!-- check: wrap -->
|
|
489
|
+
```ts
|
|
490
|
+
import { Progress } from 'wallhammer-web/base';
|
|
491
|
+
import type { Device } from 'wallhammer-web/gpu';
|
|
492
|
+
declare const device: Device;
|
|
493
|
+
declare const pieces: Promise<unknown>[];
|
|
494
|
+
declare function showBar(fraction: number, label: string): void;
|
|
495
|
+
|
|
496
|
+
const progress = new Progress([
|
|
497
|
+
{ key: 'data', label: 'Loading data', weight: 1 },
|
|
498
|
+
{ key: 'pieces', label: 'Building pieces', weight: 6 },
|
|
499
|
+
{ key: 'shaders', label: 'Compiling shaders', weight: 1 },
|
|
500
|
+
]);
|
|
501
|
+
const stop = progress.subscribe((r) => showBar(r.fraction, r.of > 1 ? `${r.label} (${r.units}/${r.of})` : r.label));
|
|
502
|
+
|
|
503
|
+
// ... fetch the data, then:
|
|
504
|
+
progress.complete('data');
|
|
505
|
+
// Worker jobs: one unit each, advanced as each settles.
|
|
506
|
+
await progress.track('pieces', pieces);
|
|
507
|
+
// Programs compile in parallel in the driver; poll between frames instead of blocking on the status.
|
|
508
|
+
const total = device.compiling();
|
|
509
|
+
progress.total('shaders', total);
|
|
510
|
+
for (let left = total, done = 0; left > 0; left = device.compiling()) {
|
|
511
|
+
progress.advance('shaders', total - left - done);
|
|
512
|
+
done = total - left;
|
|
513
|
+
await new Promise((r) => requestAnimationFrame(r));
|
|
514
|
+
}
|
|
515
|
+
progress.complete('shaders');
|
|
516
|
+
stop();
|
|
517
|
+
```
|
|
518
|
+
|
|
519
|
+
- Keep a step that runs on the main thread shorter than a frame budget: write it as a generator that `yield`s between
|
|
520
|
+
pieces (as `plantTreesSteps` does) and let the app run it in slices. The result must not depend on where it pauses.
|
|
521
|
+
- Weights are relative; set them from measured times, not from the number of units.
|