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,258 @@
|
|
|
1
|
+
// Mesh of the strips of a net (gen-core's `stripNet`) inside one streamed cell, at the cell's level, able to blend
|
|
2
|
+
// into the shape of the level one coarser like the surface under it.
|
|
3
|
+
//
|
|
4
|
+
// - Pieces: every station pair of a line is cut where its centre way meets an edge of the triangles the cell's level
|
|
5
|
+
// draws (`StripSurface.cuts`). A section of vertices stands at every share, shared by the pieces that meet there;
|
|
6
|
+
// a section point at across a lies at `centre + |a| · (edge − centre)` on its side, put back on the base surface.
|
|
7
|
+
// - Height of a section point with |a| ≤ 1 at level L: `max(grade + camber · up(a), heightOn(L, p) + liftM(L))`, so a
|
|
8
|
+
// vertex never lies under the ground any level draws there. A hem point hangs from its edge point by the
|
|
9
|
+
// difference of their `up`. `a_coarseShift` is the same rule at level L − 1 minus the own height, along local up.
|
|
10
|
+
// - The strip is drawn at least a given number of pixels wide: the vertex shader moves an edge vertex outward by
|
|
11
|
+
// `stripWiden` (the same rule in TS: `stripGrow`), so a far strip stays an unbroken line without a second kind of
|
|
12
|
+
// geometry.
|
|
13
|
+
//
|
|
14
|
+
// Positions are relative to `surface.origin(cell)`; the bytes depend on the net, the surface, the cell and the
|
|
15
|
+
// options only, never on other cells or the order of generation.
|
|
16
|
+
import { STRIP_STATION_FLOATS, stripUp } from '../gen-core/strip.js';
|
|
17
|
+
/** Locations a material should give the attributes. */
|
|
18
|
+
export const STRIP_ATTRIBS = { a_pos: 0, a_coarseShift: 1, a_side: 2, a_strip: 3 };
|
|
19
|
+
/**
|
|
20
|
+
* Position 3× float relative to the cell origin; `a_coarseShift`, metres along local up to the shape of the level one
|
|
21
|
+
* coarser; `a_side`, unit vector from the centre of the section to the vertex's edge (zero on the centre); `a_strip`:
|
|
22
|
+
* across a (half-widths), metres along the line, half-width of the section on the vertex's side in metres, index of
|
|
23
|
+
* the line.
|
|
24
|
+
*/
|
|
25
|
+
export const STRIP_LAYOUT = {
|
|
26
|
+
stride: 44,
|
|
27
|
+
attribs: [
|
|
28
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
29
|
+
{ name: 'a_coarseShift', size: 1, type: 'f32', offset: 12 },
|
|
30
|
+
{ name: 'a_side', size: 3, type: 'f32', offset: 16 },
|
|
31
|
+
{ name: 'a_strip', size: 4, type: 'f32', offset: 28 },
|
|
32
|
+
],
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* GLSL for a strip vertex shader: include `<header>`, then `<noise>` (for `metresPerPixelAt`), then this, after the
|
|
36
|
+
* morph code of the surface. `stripWiden` moves an edge vertex outward so the strip is at least `minPx` pixels wide;
|
|
37
|
+
* `grow` is the metres it added per side.
|
|
38
|
+
*
|
|
39
|
+
* vec3 p = a_pos + u_cellOffset;
|
|
40
|
+
* p += localUp(p) * (a_coarseShift * spherePatchMorph(p)); // or u_morphUp and cellMorph(p) on the plane
|
|
41
|
+
* float grow;
|
|
42
|
+
* p = stripWiden(p, a_side, a_strip, 1.6, grow);
|
|
43
|
+
*/
|
|
44
|
+
export const STRIP_GLSL = /* glsl */ `
|
|
45
|
+
vec3 stripWiden(vec3 p, vec3 side, vec4 strip, float minPx, out float grow) {
|
|
46
|
+
grow = max(0.0, 0.5 * minPx * metresPerPixelAt(p) - strip.z);
|
|
47
|
+
return p + side * (grow * min(abs(strip.x), 1.0));
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
/** Metres `stripWiden` adds to each side of a strip `halfWidthM` wide where a pixel covers `metresPerPixel`. */
|
|
51
|
+
export function stripGrow(halfWidthM, metresPerPixel, minPx) {
|
|
52
|
+
return Math.max(0, 0.5 * minPx * metresPerPixel - halfWidthM);
|
|
53
|
+
}
|
|
54
|
+
const FLOATS = STRIP_LAYOUT.stride / 4;
|
|
55
|
+
export function stripMesh(net, surface, cell, o) {
|
|
56
|
+
if (surface.radiusM !== net.radiusM)
|
|
57
|
+
throw new Error(`stripMesh: the surface (radius ${surface.radiusM}) and the net (radius ${net.radiusM}) differ`);
|
|
58
|
+
const R = net.radiusM;
|
|
59
|
+
const sphere = R > 0;
|
|
60
|
+
const level = cell.level;
|
|
61
|
+
const coarse = level > o.coarsestLevel;
|
|
62
|
+
const liftOwn = o.liftM(level);
|
|
63
|
+
const liftCoarse = coarse ? o.liftM(level - 1) : 0;
|
|
64
|
+
const origin = surface.origin(cell);
|
|
65
|
+
const lengthM = new Float64Array(net.lineCount);
|
|
66
|
+
const verts = [];
|
|
67
|
+
const idx = [];
|
|
68
|
+
const min = [Infinity, Infinity, Infinity];
|
|
69
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
70
|
+
/** Point at share t of the straight way between two points, put on the base surface. */
|
|
71
|
+
const way = (ax, ay, az, bx, by, bz, t, out) => {
|
|
72
|
+
const x = ax + (bx - ax) * t;
|
|
73
|
+
const y = ay + (by - ay) * t;
|
|
74
|
+
const z = az + (bz - az) * t;
|
|
75
|
+
if (sphere) {
|
|
76
|
+
const l = Math.sqrt(x * x + y * y + z * z);
|
|
77
|
+
out[0] = (x / l) * R;
|
|
78
|
+
out[1] = (y / l) * R;
|
|
79
|
+
out[2] = (z / l) * R;
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
out[0] = x;
|
|
83
|
+
out[1] = 0;
|
|
84
|
+
out[2] = z;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const C = [0, 0, 0];
|
|
88
|
+
const L = [0, 0, 0];
|
|
89
|
+
const Rt = [0, 0, 0];
|
|
90
|
+
const P = [0, 0, 0];
|
|
91
|
+
for (let li = 0; li < net.lineCount; li++) {
|
|
92
|
+
const line = net.lines[li];
|
|
93
|
+
const profile = net.profiles[line.profile];
|
|
94
|
+
const sec = profile.section;
|
|
95
|
+
const m = sec.length / 2;
|
|
96
|
+
// Index of the edge points −1 and 1 in the section.
|
|
97
|
+
let iL = -1;
|
|
98
|
+
let iR = -1;
|
|
99
|
+
for (let k = 0; k < m; k++) {
|
|
100
|
+
if (sec[k * 2] === -1 && iL < 0)
|
|
101
|
+
iL = k;
|
|
102
|
+
if (sec[k * 2] === 1)
|
|
103
|
+
iR = k;
|
|
104
|
+
}
|
|
105
|
+
const st = line.stations;
|
|
106
|
+
let prevBase = -1;
|
|
107
|
+
let prevPiece = -2;
|
|
108
|
+
let prevShare = NaN;
|
|
109
|
+
const hOwn = new Float64Array(m);
|
|
110
|
+
const hCoarse = new Float64Array(m);
|
|
111
|
+
/** One section of vertices at share t of piece k; returns the index of its first vertex. */
|
|
112
|
+
const section = (k, t) => {
|
|
113
|
+
const s0 = k * STRIP_STATION_FLOATS;
|
|
114
|
+
const s1 = s0 + STRIP_STATION_FLOATS;
|
|
115
|
+
// The stations themselves at t = 0 and t = 1.
|
|
116
|
+
const at = t === 0 ? s0 : t === 1 ? s1 : -1;
|
|
117
|
+
for (let c = 0; c < 3; c++) {
|
|
118
|
+
if (at >= 0) {
|
|
119
|
+
C[c] = st[at + c];
|
|
120
|
+
L[c] = st[at + 3 + c];
|
|
121
|
+
Rt[c] = st[at + 6 + c];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (at < 0) {
|
|
125
|
+
way(st[s0], st[s0 + 1], st[s0 + 2], st[s1], st[s1 + 1], st[s1 + 2], t, C);
|
|
126
|
+
way(st[s0 + 3], st[s0 + 4], st[s0 + 5], st[s1 + 3], st[s1 + 4], st[s1 + 5], t, L);
|
|
127
|
+
way(st[s0 + 6], st[s0 + 7], st[s0 + 8], st[s1 + 6], st[s1 + 7], st[s1 + 8], t, Rt);
|
|
128
|
+
}
|
|
129
|
+
const mixAt = (f) => (at >= 0 ? st[at + f] : st[s0 + f] + (st[s1 + f] - st[s0 + f]) * t);
|
|
130
|
+
const grade = mixAt(9);
|
|
131
|
+
const camber = mixAt(10);
|
|
132
|
+
const along = mixAt(11);
|
|
133
|
+
const base = verts.length / FLOATS;
|
|
134
|
+
// Heights: first the points on the strip, then the hem from its edge point.
|
|
135
|
+
const points = new Array(m * 3);
|
|
136
|
+
for (let k2 = 0; k2 < m; k2++) {
|
|
137
|
+
const a = sec[k2 * 2];
|
|
138
|
+
const E = a >= 0 ? Rt : L;
|
|
139
|
+
const s = Math.abs(a);
|
|
140
|
+
if (a === 0) {
|
|
141
|
+
P[0] = C[0];
|
|
142
|
+
P[1] = C[1];
|
|
143
|
+
P[2] = C[2];
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
way(C[0], C[1], C[2], E[0], E[1], E[2], s, P);
|
|
147
|
+
}
|
|
148
|
+
points[k2 * 3] = P[0];
|
|
149
|
+
points[k2 * 3 + 1] = P[1];
|
|
150
|
+
points[k2 * 3 + 2] = P[2];
|
|
151
|
+
if (Math.abs(a) <= 1) {
|
|
152
|
+
const design = grade + camber * stripUp(profile, a);
|
|
153
|
+
hOwn[k2] = Math.max(design, surface.heightOn(level, P) + liftOwn);
|
|
154
|
+
hCoarse[k2] = coarse ? Math.max(design, surface.heightOn(level - 1, P) + liftCoarse) : hOwn[k2];
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
for (let k2 = 0; k2 < m; k2++) {
|
|
158
|
+
const a = sec[k2 * 2];
|
|
159
|
+
if (Math.abs(a) <= 1)
|
|
160
|
+
continue;
|
|
161
|
+
const e = a < 0 ? iL : iR;
|
|
162
|
+
const drop = sec[k2 * 2 + 1] - sec[e * 2 + 1];
|
|
163
|
+
hOwn[k2] = hOwn[e] + drop;
|
|
164
|
+
hCoarse[k2] = hCoarse[e] + drop;
|
|
165
|
+
}
|
|
166
|
+
for (let k2 = 0; k2 < m; k2++) {
|
|
167
|
+
const a = sec[k2 * 2];
|
|
168
|
+
const E = a >= 0 ? Rt : L;
|
|
169
|
+
const px = points[k2 * 3];
|
|
170
|
+
const py = points[k2 * 3 + 1];
|
|
171
|
+
const pz = points[k2 * 3 + 2];
|
|
172
|
+
let ux = 0;
|
|
173
|
+
let uy = 1;
|
|
174
|
+
let uz = 0;
|
|
175
|
+
if (sphere) {
|
|
176
|
+
ux = px / R;
|
|
177
|
+
uy = py / R;
|
|
178
|
+
uz = pz / R;
|
|
179
|
+
}
|
|
180
|
+
const h = hOwn[k2];
|
|
181
|
+
const shift = coarse ? Math.fround(hCoarse[k2] - h) : 0;
|
|
182
|
+
const pos = sphere
|
|
183
|
+
? [Math.fround(px + ux * h - origin[0]), Math.fround(py + uy * h - origin[1]), Math.fround(pz + uz * h - origin[2])]
|
|
184
|
+
: [Math.fround(px - origin[0]), Math.fround(h - origin[1]), Math.fround(pz - origin[2])];
|
|
185
|
+
let sx = 0;
|
|
186
|
+
let sy = 0;
|
|
187
|
+
let sz = 0;
|
|
188
|
+
let hw = 0;
|
|
189
|
+
if (a !== 0) {
|
|
190
|
+
sx = E[0] - C[0];
|
|
191
|
+
sy = E[1] - C[1];
|
|
192
|
+
sz = E[2] - C[2];
|
|
193
|
+
hw = Math.sqrt(sx * sx + sy * sy + sz * sz);
|
|
194
|
+
sx /= hw;
|
|
195
|
+
sy /= hw;
|
|
196
|
+
sz /= hw;
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
// The centre: the larger of the two sides, for the widening it does not use.
|
|
200
|
+
const lx = L[0] - C[0];
|
|
201
|
+
const ly = L[1] - C[1];
|
|
202
|
+
const lz = L[2] - C[2];
|
|
203
|
+
const rx = Rt[0] - C[0];
|
|
204
|
+
const ry = Rt[1] - C[1];
|
|
205
|
+
const rz = Rt[2] - C[2];
|
|
206
|
+
hw = Math.sqrt(Math.max(lx * lx + ly * ly + lz * lz, rx * rx + ry * ry + rz * rz));
|
|
207
|
+
}
|
|
208
|
+
verts.push(pos[0], pos[1], pos[2], shift, sx, sy, sz, a, along, hw, li);
|
|
209
|
+
const up = [ux, uy, uz];
|
|
210
|
+
for (let c = 0; c < 3; c++) {
|
|
211
|
+
const p0 = pos[c];
|
|
212
|
+
const p1 = p0 + up[c] * shift;
|
|
213
|
+
min[c] = Math.min(min[c], p0 - 1e-3, p1 - 1e-3);
|
|
214
|
+
max[c] = Math.max(max[c], p0 + 1e-3, p1 + 1e-3);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return base;
|
|
218
|
+
};
|
|
219
|
+
for (let k = 0; k + 1 < line.count; k++) {
|
|
220
|
+
const s0 = k * STRIP_STATION_FLOATS;
|
|
221
|
+
const s1 = s0 + STRIP_STATION_FLOATS;
|
|
222
|
+
const shares = surface.cuts(cell, [st[s0], st[s0 + 1], st[s0 + 2]], [st[s1], st[s1 + 1], st[s1 + 2]]);
|
|
223
|
+
if (shares.length < 2)
|
|
224
|
+
continue;
|
|
225
|
+
lengthM[li] = lengthM[li] + (shares[shares.length - 1] - shares[0]) * (st[s1 + 11] - st[s0 + 11]);
|
|
226
|
+
for (let i = 0; i < shares.length; i++) {
|
|
227
|
+
const t = shares[i];
|
|
228
|
+
let base;
|
|
229
|
+
// The station between two pieces: reuse the section the previous piece ended with.
|
|
230
|
+
if (i === 0 && t === 0 && prevPiece === k - 1 && prevShare === 1)
|
|
231
|
+
base = prevBase;
|
|
232
|
+
else
|
|
233
|
+
base = section(k, t);
|
|
234
|
+
if (i > 0) {
|
|
235
|
+
const b0 = prevBase;
|
|
236
|
+
const b1 = base;
|
|
237
|
+
for (let j = 0; j + 1 < m; j++) {
|
|
238
|
+
// Left to right across, forward along: (S0[j], S0[j+1], S1[j]) winds anticlockwise seen from above.
|
|
239
|
+
idx.push(b0 + j, b0 + j + 1, b1 + j, b0 + j + 1, b1 + j + 1, b1 + j);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
prevBase = base;
|
|
243
|
+
}
|
|
244
|
+
prevPiece = k;
|
|
245
|
+
prevShare = shares[shares.length - 1];
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
if (idx.length === 0)
|
|
249
|
+
return null;
|
|
250
|
+
const count = verts.length / FLOATS;
|
|
251
|
+
const buf = new ArrayBuffer(count * STRIP_LAYOUT.stride);
|
|
252
|
+
const view = new DataView(buf);
|
|
253
|
+
for (let i = 0; i < verts.length; i++)
|
|
254
|
+
view.setFloat32(i * 4, verts[i], true);
|
|
255
|
+
const indices = count > 65535 ? Uint32Array.from(idx) : Uint16Array.from(idx);
|
|
256
|
+
const bounds = { min: [Math.fround(min[0]), Math.fround(min[1]), Math.fround(min[2])], max: [Math.fround(max[0]), Math.fround(max[1]), Math.fround(max[2])] };
|
|
257
|
+
return { mesh: { layout: STRIP_LAYOUT, vertices: new Uint8Array(buf), indices, count: idx.length }, bounds, lengthM };
|
|
258
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { SphereGrid } from './grid.ts';
|
|
2
|
+
/** What `drainGrid` returns. Arrays of one entry per node unless named otherwise. */
|
|
3
|
+
export interface Drainage {
|
|
4
|
+
/** Height after the hollows are filled: the level water stands at. */
|
|
5
|
+
filled: Float32Array;
|
|
6
|
+
/** The node this one drains to; −1 at a base node (and at the one outlet of a grid without base nodes). */
|
|
7
|
+
to: Int32Array;
|
|
8
|
+
/** All nodes, each before the node it drains to. */
|
|
9
|
+
order: Uint32Array;
|
|
10
|
+
/** Supply of the node plus the flow of every node that drains to it. */
|
|
11
|
+
flow: Float32Array;
|
|
12
|
+
/** 0, or 1 + the index of the pool the node lies in. */
|
|
13
|
+
pool: Uint16Array;
|
|
14
|
+
/** Per pool: the common filled height, the number of its nodes, and its sill (the node outside it that it spills to). */
|
|
15
|
+
pools: {
|
|
16
|
+
level: Float32Array;
|
|
17
|
+
nodes: Uint32Array;
|
|
18
|
+
outlet: Int32Array;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** Options of `drainGrid`. */
|
|
22
|
+
export interface DrainOptions {
|
|
23
|
+
/** Nodes at or under this height are base nodes: they drain nowhere. */
|
|
24
|
+
baseLevel: number;
|
|
25
|
+
/** Per node, what it adds to the flow (default 1 everywhere). Not negative. */
|
|
26
|
+
supply?: ArrayLike<number>;
|
|
27
|
+
/** A filled hollow is a pool from this many nodes on … */
|
|
28
|
+
minPoolNodes: number;
|
|
29
|
+
/** … and when its deepest node lies at least this far under the filled height. */
|
|
30
|
+
minPoolDepth: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Fills the hollows of `heights` (one per node) from the base nodes inward and drains every node.
|
|
34
|
+
*
|
|
35
|
+
* A node taken from the heap visits its unvisited neighbours in `gridNeighbours` order; each gets the larger of its
|
|
36
|
+
* own height and the filled height of the node that reached it, which is its flood parent. A node drains to the
|
|
37
|
+
* neighbour with the steepest fall of the filled height (drop over the chord between the node middles, the earlier
|
|
38
|
+
* neighbour on a tie) when one is strictly lower, else to its flood parent. Without a base node the lowest node (the
|
|
39
|
+
* lowest index on a tie) is the only outlet. Pools are numbered by their lowest node; more than 65 535 throws.
|
|
40
|
+
*/
|
|
41
|
+
export declare function drainGrid(g: SphereGrid, heights: ArrayLike<number>, o: DrainOptions): Drainage;
|
|
42
|
+
/** Channels: chains of nodes along `to`, from `traceChannels`. */
|
|
43
|
+
export interface Channels {
|
|
44
|
+
/** Per channel + 1: its first entry in `nodes`. */
|
|
45
|
+
start: Uint32Array;
|
|
46
|
+
nodes: Uint32Array;
|
|
47
|
+
/** Per channel: −1 when it ends in a base node, −2 − pool index in a pool, else the channel it runs into. */
|
|
48
|
+
into: Int32Array;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The channels of a drainage: the nodes outside base and pools with at least `minFlow`, as chains along `to`. A
|
|
52
|
+
* channel starts where no channel node drains in, or where two or more do, and ends with the first node that is a base
|
|
53
|
+
* node, a pool node or the start of another channel; that end node belongs to the chain. Channels are ordered by the
|
|
54
|
+
* place of their first node in `d.order`, so a channel comes before the ones below it.
|
|
55
|
+
*/
|
|
56
|
+
export declare function traceChannels(g: SphereGrid, d: Drainage, o: {
|
|
57
|
+
minFlow: number;
|
|
58
|
+
}): Channels;
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
// Drainage of a height field over a sphere grid: hollows are filled from the base level inward, every node gets the
|
|
2
|
+
// node it drains to, flow is gathered downstream, filled hollows that are large and deep enough become pools, and the
|
|
3
|
+
// nodes that carry enough flow are traced as channels. Exact and order-fixed: a heap ordered by (filled height, node
|
|
4
|
+
// index), neighbours in the fixed order of `gridNeighbours`, no tilt by an epsilon, so every runtime gives the same bits.
|
|
5
|
+
import { gridDirection, gridNeighbours } from './grid.js';
|
|
6
|
+
/** A binary heap of node indices ordered by (key, node). */
|
|
7
|
+
class NodeHeap {
|
|
8
|
+
items;
|
|
9
|
+
key;
|
|
10
|
+
size = 0;
|
|
11
|
+
constructor(capacity, key) {
|
|
12
|
+
this.items = new Uint32Array(capacity);
|
|
13
|
+
this.key = key;
|
|
14
|
+
}
|
|
15
|
+
less(a, b) {
|
|
16
|
+
const ka = this.key[a];
|
|
17
|
+
const kb = this.key[b];
|
|
18
|
+
return ka < kb || (ka === kb && a < b);
|
|
19
|
+
}
|
|
20
|
+
push(node) {
|
|
21
|
+
const items = this.items;
|
|
22
|
+
let i = this.size++;
|
|
23
|
+
while (i > 0) {
|
|
24
|
+
const parent = (i - 1) >> 1;
|
|
25
|
+
const p = items[parent];
|
|
26
|
+
if (!this.less(node, p))
|
|
27
|
+
break;
|
|
28
|
+
items[i] = p;
|
|
29
|
+
i = parent;
|
|
30
|
+
}
|
|
31
|
+
items[i] = node;
|
|
32
|
+
}
|
|
33
|
+
pop() {
|
|
34
|
+
const items = this.items;
|
|
35
|
+
const top = items[0];
|
|
36
|
+
const last = items[--this.size];
|
|
37
|
+
const size = this.size;
|
|
38
|
+
let i = 0;
|
|
39
|
+
for (;;) {
|
|
40
|
+
const l = 2 * i + 1;
|
|
41
|
+
if (l >= size)
|
|
42
|
+
break;
|
|
43
|
+
const r = l + 1;
|
|
44
|
+
const c = r < size && this.less(items[r], items[l]) ? r : l;
|
|
45
|
+
if (!this.less(items[c], last))
|
|
46
|
+
break;
|
|
47
|
+
items[i] = items[c];
|
|
48
|
+
i = c;
|
|
49
|
+
}
|
|
50
|
+
if (size > 0)
|
|
51
|
+
items[i] = last;
|
|
52
|
+
return top;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Fills the hollows of `heights` (one per node) from the base nodes inward and drains every node.
|
|
57
|
+
*
|
|
58
|
+
* A node taken from the heap visits its unvisited neighbours in `gridNeighbours` order; each gets the larger of its
|
|
59
|
+
* own height and the filled height of the node that reached it, which is its flood parent. A node drains to the
|
|
60
|
+
* neighbour with the steepest fall of the filled height (drop over the chord between the node middles, the earlier
|
|
61
|
+
* neighbour on a tie) when one is strictly lower, else to its flood parent. Without a base node the lowest node (the
|
|
62
|
+
* lowest index on a tie) is the only outlet. Pools are numbered by their lowest node; more than 65 535 throws.
|
|
63
|
+
*/
|
|
64
|
+
export function drainGrid(g, heights, o) {
|
|
65
|
+
const count = g.count;
|
|
66
|
+
if (heights.length !== count)
|
|
67
|
+
throw new Error(`gen-plan: ${heights.length} heights for ${count} nodes`);
|
|
68
|
+
const filled = new Float32Array(count);
|
|
69
|
+
const to = new Int32Array(count).fill(-2);
|
|
70
|
+
const order = new Uint32Array(count);
|
|
71
|
+
const heap = new NodeHeap(count, filled);
|
|
72
|
+
const nb = new Int32Array(8);
|
|
73
|
+
// The base nodes, or the lowest node.
|
|
74
|
+
let bases = 0;
|
|
75
|
+
for (let i = 0; i < count; i++) {
|
|
76
|
+
const h = Math.fround(heights[i]);
|
|
77
|
+
filled[i] = h;
|
|
78
|
+
if (h <= o.baseLevel) {
|
|
79
|
+
to[i] = -1;
|
|
80
|
+
heap.push(i);
|
|
81
|
+
bases++;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (bases === 0) {
|
|
85
|
+
let low = 0;
|
|
86
|
+
for (let i = 1; i < count; i++)
|
|
87
|
+
if (filled[i] < filled[low])
|
|
88
|
+
low = i;
|
|
89
|
+
to[low] = -1;
|
|
90
|
+
heap.push(low);
|
|
91
|
+
}
|
|
92
|
+
// The flood. `to` holds the flood parent for now; `order` is filled from the back, so it ends upstream first.
|
|
93
|
+
let taken = count;
|
|
94
|
+
while (heap.size > 0) {
|
|
95
|
+
const p = heap.pop();
|
|
96
|
+
order[--taken] = p;
|
|
97
|
+
const fp = filled[p];
|
|
98
|
+
const k = gridNeighbours(g, p, nb);
|
|
99
|
+
for (let j = 0; j < k; j++) {
|
|
100
|
+
const q = nb[j];
|
|
101
|
+
if (to[q] !== -2)
|
|
102
|
+
continue;
|
|
103
|
+
if (filled[q] < fp)
|
|
104
|
+
filled[q] = fp;
|
|
105
|
+
to[q] = p;
|
|
106
|
+
heap.push(q);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (taken !== 0)
|
|
110
|
+
throw new Error('gen-plan: the grid is not connected');
|
|
111
|
+
// The steepest strictly lower neighbour wins over the flood parent.
|
|
112
|
+
const a = [0, 0, 0];
|
|
113
|
+
const b = [0, 0, 0];
|
|
114
|
+
for (let i = 0; i < count; i++) {
|
|
115
|
+
if (to[i] === -1)
|
|
116
|
+
continue;
|
|
117
|
+
const fi = filled[i];
|
|
118
|
+
const k = gridNeighbours(g, i, nb);
|
|
119
|
+
let best = -1;
|
|
120
|
+
let bestFall = 0;
|
|
121
|
+
let haveA = false;
|
|
122
|
+
for (let j = 0; j < k; j++) {
|
|
123
|
+
const q = nb[j];
|
|
124
|
+
const drop = fi - filled[q];
|
|
125
|
+
if (!(drop > 0))
|
|
126
|
+
continue;
|
|
127
|
+
if (!haveA) {
|
|
128
|
+
gridDirection(g, i, a);
|
|
129
|
+
haveA = true;
|
|
130
|
+
}
|
|
131
|
+
gridDirection(g, q, b);
|
|
132
|
+
const dx = a[0] - b[0];
|
|
133
|
+
const dy = a[1] - b[1];
|
|
134
|
+
const dz = a[2] - b[2];
|
|
135
|
+
const fall = drop / Math.sqrt(dx * dx + dy * dy + dz * dz);
|
|
136
|
+
if (fall > bestFall) {
|
|
137
|
+
bestFall = fall;
|
|
138
|
+
best = q;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (best >= 0)
|
|
142
|
+
to[i] = best;
|
|
143
|
+
}
|
|
144
|
+
const flow = gatherFlow(count, to, order, o.supply);
|
|
145
|
+
// Pools: nodes under water joined through neighbours at the same filled height, numbered by their lowest node.
|
|
146
|
+
const pool = new Uint16Array(count);
|
|
147
|
+
const levels = [];
|
|
148
|
+
const sizes = [];
|
|
149
|
+
// A set is walked to its end before the next one starts, so one flag per node is enough.
|
|
150
|
+
const mark = new Uint8Array(count);
|
|
151
|
+
const stack = [];
|
|
152
|
+
const members = [];
|
|
153
|
+
for (let i = 0; i < count; i++) {
|
|
154
|
+
if (mark[i] !== 0 || !(filled[i] > Math.fround(heights[i])))
|
|
155
|
+
continue;
|
|
156
|
+
const level = filled[i];
|
|
157
|
+
members.length = 0;
|
|
158
|
+
stack.length = 0;
|
|
159
|
+
stack.push(i);
|
|
160
|
+
mark[i] = 1;
|
|
161
|
+
let deepest = 0;
|
|
162
|
+
while (stack.length > 0) {
|
|
163
|
+
const p = stack.pop();
|
|
164
|
+
members.push(p);
|
|
165
|
+
const depth = level - Math.fround(heights[p]);
|
|
166
|
+
if (depth > deepest)
|
|
167
|
+
deepest = depth;
|
|
168
|
+
const k = gridNeighbours(g, p, nb);
|
|
169
|
+
for (let j = 0; j < k; j++) {
|
|
170
|
+
const q = nb[j];
|
|
171
|
+
if (mark[q] !== 0 || filled[q] !== level || !(level > Math.fround(heights[q])))
|
|
172
|
+
continue;
|
|
173
|
+
mark[q] = 1;
|
|
174
|
+
stack.push(q);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (members.length < o.minPoolNodes || deepest < o.minPoolDepth)
|
|
178
|
+
continue;
|
|
179
|
+
if (levels.length >= 65535)
|
|
180
|
+
throw new Error('gen-plan: more than 65 535 pools');
|
|
181
|
+
levels.push(level);
|
|
182
|
+
sizes.push(members.length);
|
|
183
|
+
const id = levels.length;
|
|
184
|
+
for (const m of members)
|
|
185
|
+
pool[m] = id;
|
|
186
|
+
}
|
|
187
|
+
// The sill of a pool: where its first flooded node drains to (that node is the last of the pool in `order`).
|
|
188
|
+
const outlet = new Int32Array(levels.length).fill(-1);
|
|
189
|
+
for (let k = count - 1; k >= 0; k--) {
|
|
190
|
+
const i = order[k];
|
|
191
|
+
const p = pool[i];
|
|
192
|
+
if (p > 0 && outlet[p - 1] === -1)
|
|
193
|
+
outlet[p - 1] = to[i];
|
|
194
|
+
}
|
|
195
|
+
return { filled, to, order, flow, pool, pools: { level: Float32Array.from(levels), nodes: Uint32Array.from(sizes), outlet } };
|
|
196
|
+
}
|
|
197
|
+
/** Flow per node: its supply (default 1) plus the flow of every node draining to it, summed upstream first in doubles. */
|
|
198
|
+
function gatherFlow(count, to, order, supply) {
|
|
199
|
+
if (supply && supply.length !== count)
|
|
200
|
+
throw new Error(`gen-plan: ${supply.length} supply values for ${count} nodes`);
|
|
201
|
+
const sum = new Float64Array(count);
|
|
202
|
+
for (let i = 0; i < count; i++)
|
|
203
|
+
sum[i] = supply ? supply[i] : 1;
|
|
204
|
+
for (let k = 0; k < count; k++) {
|
|
205
|
+
const i = order[k];
|
|
206
|
+
const t = to[i];
|
|
207
|
+
if (t >= 0)
|
|
208
|
+
sum[t] = sum[t] + sum[i];
|
|
209
|
+
}
|
|
210
|
+
return Float32Array.from(sum);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* The channels of a drainage: the nodes outside base and pools with at least `minFlow`, as chains along `to`. A
|
|
214
|
+
* channel starts where no channel node drains in, or where two or more do, and ends with the first node that is a base
|
|
215
|
+
* node, a pool node or the start of another channel; that end node belongs to the chain. Channels are ordered by the
|
|
216
|
+
* place of their first node in `d.order`, so a channel comes before the ones below it.
|
|
217
|
+
*/
|
|
218
|
+
export function traceChannels(g, d, o) {
|
|
219
|
+
const count = g.count;
|
|
220
|
+
const { to, flow, pool, order } = d;
|
|
221
|
+
const isChannel = (i) => to[i] >= 0 && pool[i] === 0 && flow[i] >= o.minFlow;
|
|
222
|
+
// Channel nodes draining into each node, capped at 2.
|
|
223
|
+
const inflow = new Uint8Array(count);
|
|
224
|
+
for (let i = 0; i < count; i++) {
|
|
225
|
+
if (!isChannel(i))
|
|
226
|
+
continue;
|
|
227
|
+
const t = to[i];
|
|
228
|
+
if (inflow[t] < 2)
|
|
229
|
+
inflow[t] = inflow[t] + 1;
|
|
230
|
+
}
|
|
231
|
+
const startOf = new Map();
|
|
232
|
+
const firsts = [];
|
|
233
|
+
for (let k = 0; k < count; k++) {
|
|
234
|
+
const i = order[k];
|
|
235
|
+
if (!isChannel(i) || inflow[i] === 1)
|
|
236
|
+
continue;
|
|
237
|
+
startOf.set(i, firsts.length);
|
|
238
|
+
firsts.push(i);
|
|
239
|
+
}
|
|
240
|
+
const start = new Uint32Array(firsts.length + 1);
|
|
241
|
+
const into = new Int32Array(firsts.length);
|
|
242
|
+
const nodes = [];
|
|
243
|
+
for (let c = 0; c < firsts.length; c++) {
|
|
244
|
+
start[c] = nodes.length;
|
|
245
|
+
let i = firsts[c];
|
|
246
|
+
nodes.push(i);
|
|
247
|
+
for (;;) {
|
|
248
|
+
const t = to[i];
|
|
249
|
+
nodes.push(t);
|
|
250
|
+
if (to[t] === -1) {
|
|
251
|
+
into[c] = -1;
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
if (pool[t] !== 0) {
|
|
255
|
+
into[c] = -2 - (pool[t] - 1);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
const s = startOf.get(t);
|
|
259
|
+
if (s !== undefined) {
|
|
260
|
+
into[c] = s;
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
if (!isChannel(t))
|
|
264
|
+
throw new Error('gen-plan: a channel runs into a node with less flow (negative supply?)');
|
|
265
|
+
i = t;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
start[firsts.length] = nodes.length;
|
|
269
|
+
return { start, nodes: Uint32Array.from(nodes), into };
|
|
270
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Vec3 } from '../base/math.ts';
|
|
2
|
+
/** The grid of nodes at the middles of the cells of one level of the cube sphere. */
|
|
3
|
+
export interface SphereGrid {
|
|
4
|
+
readonly radiusM: number;
|
|
5
|
+
readonly level: number;
|
|
6
|
+
/** Nodes along a face side, `2^level`. */
|
|
7
|
+
readonly n: number;
|
|
8
|
+
/** All nodes, `6 n²`. */
|
|
9
|
+
readonly count: number;
|
|
10
|
+
}
|
|
11
|
+
/** The grid of level `level` on a sphere of `radiusM`. */
|
|
12
|
+
export declare function sphereGrid(radiusM: number, level: number): SphereGrid;
|
|
13
|
+
/** The node whose cell holds the direction (x, y, z), any length but not zero. */
|
|
14
|
+
export declare function gridNodeAt(g: SphereGrid, x: number, y: number, z: number): number;
|
|
15
|
+
/** Unit direction of a node's middle: `cubeDirection(face, cubeGrid(x + 0.5, n), cubeGrid(y + 0.5, n))`, bit for bit. */
|
|
16
|
+
export declare function gridDirection(g: SphereGrid, node: number, out?: Vec3): Vec3;
|
|
17
|
+
/**
|
|
18
|
+
* The neighbours of a node in the order (−1,−1) (0,−1) (1,−1) (−1,0) (1,0) (−1,1) (0,1) (1,1) of its own face axes,
|
|
19
|
+
* across an edge of the cube as `cubeNeighbour` folds it. The diagonal step over a corner of the cube does not exist
|
|
20
|
+
* and is left out, so the 24 corner nodes have 7. Writes them to `out` (at least 8 long) and returns how many.
|
|
21
|
+
*/
|
|
22
|
+
export declare function gridNeighbours(g: SphereGrid, node: number, out: Int32Array): number;
|
|
23
|
+
/** Area in m² of a node's cell, from the closed form of the equal-angle grid at its middle. */
|
|
24
|
+
export declare function gridNodeArea(g: SphereGrid, node: number): number;
|
|
25
|
+
/**
|
|
26
|
+
* A field of one value per node, read at a direction (x, y, z): bilinear between the four nearest node middles in
|
|
27
|
+
* the face coordinates of the face that holds the direction. Past the last row or column of a face the nodes of the
|
|
28
|
+
* next face stand in; at a corner of the cube the missing fourth value is the mean of the other three. Equal to the
|
|
29
|
+
* field at a node middle, and continuous over the edges and corners of the cube.
|
|
30
|
+
*/
|
|
31
|
+
export declare function gridSample(g: SphereGrid, field: ArrayLike<number>, x: number, y: number, z: number): number;
|
|
32
|
+
/**
|
|
33
|
+
* The nodes `gridSample` blends at a direction (x, y, z) and their weights (sum 1), in its order. Returns how many:
|
|
34
|
+
* 3 at a corner of the cube (the missing fourth node's weight goes in thirds to the other three), else 4. The sum of
|
|
35
|
+
* `weights[k] · field[nodes[k]]` in this order is `gridSample` bit for bit.
|
|
36
|
+
*/
|
|
37
|
+
export declare function gridWeights(g: SphereGrid, x: number, y: number, z: number, nodes: Int32Array, weights: Float64Array): number;
|