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,182 @@
|
|
|
1
|
+
// A grid over the whole sphere: one node at the middle of every cell of one level of the cube sphere (gen-core,
|
|
2
|
+
// cube-sphere.ts). Node `face · n² + y · n + x`, with `n = 2^level`, sits at the middle of cell (x, y) of that face.
|
|
3
|
+
// Neighbours are found by index arithmetic inside a face and by a seam table of the four sides of every face, built
|
|
4
|
+
// once per level; there is no table per node. Everything is arithmetic, `Math.sqrt` and what the cube sphere calls,
|
|
5
|
+
// so the same input gives the same bits in every runtime.
|
|
6
|
+
import { cubeDirectionWarped, cubeFaceOf, cubeGrid, cubeNeighbour, cubeWarp } from '../gen-core/cube-sphere.js';
|
|
7
|
+
const tables = new Map();
|
|
8
|
+
function tablesOf(level) {
|
|
9
|
+
let t = tables.get(level);
|
|
10
|
+
if (t)
|
|
11
|
+
return t;
|
|
12
|
+
const n = 2 ** level;
|
|
13
|
+
const warp = new Float64Array(n);
|
|
14
|
+
for (let i = 0; i < n; i++)
|
|
15
|
+
warp[i] = cubeWarp(cubeGrid(i + 0.5, n));
|
|
16
|
+
const seam = new Int32Array(24 * n);
|
|
17
|
+
for (let face = 0; face < 6; face++) {
|
|
18
|
+
for (let i = 0; i < n; i++) {
|
|
19
|
+
const steps = [
|
|
20
|
+
[0, i, -1, 0],
|
|
21
|
+
[n - 1, i, 1, 0],
|
|
22
|
+
[i, 0, 0, -1],
|
|
23
|
+
[i, n - 1, 0, 1],
|
|
24
|
+
];
|
|
25
|
+
for (let side = 0; side < 4; side++) {
|
|
26
|
+
const [x, y, dx, dy] = steps[side];
|
|
27
|
+
const c = cubeNeighbour(face, level, x, y, dx, dy);
|
|
28
|
+
if (!c)
|
|
29
|
+
throw new Error(`gen-plan: no cell across side ${side} of face ${face}`);
|
|
30
|
+
seam[(face * 4 + side) * n + i] = c.face * n * n + c.y * n + c.x;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
t = { warp, seam };
|
|
35
|
+
tables.set(level, t);
|
|
36
|
+
return t;
|
|
37
|
+
}
|
|
38
|
+
/** The grid of level `level` on a sphere of `radiusM`. */
|
|
39
|
+
export function sphereGrid(radiusM, level) {
|
|
40
|
+
if (!(level >= 0 && level <= 13 && Number.isInteger(level)))
|
|
41
|
+
throw new Error(`gen-plan: grid level ${level} out of 0…13`);
|
|
42
|
+
const n = 2 ** level;
|
|
43
|
+
return { radiusM, level, n, count: 6 * n * n };
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The node `x` index `i`, `y` index `j` of `face`, or across an edge of the cube when one of them is −1 or n.
|
|
47
|
+
* −1 when both are off the face (a corner of the cube: no cell lies there).
|
|
48
|
+
*/
|
|
49
|
+
function nodeOf(t, n, face, i, j) {
|
|
50
|
+
const outI = i < 0 || i >= n;
|
|
51
|
+
const outJ = j < 0 || j >= n;
|
|
52
|
+
if (!outI && !outJ)
|
|
53
|
+
return face * n * n + j * n + i;
|
|
54
|
+
if (outI && outJ)
|
|
55
|
+
return -1;
|
|
56
|
+
if (outI)
|
|
57
|
+
return t.seam[(face * 4 + (i < 0 ? 0 : 1)) * n + j];
|
|
58
|
+
return t.seam[(face * 4 + (j < 0 ? 2 : 3)) * n + i];
|
|
59
|
+
}
|
|
60
|
+
/** The node whose cell holds the direction (x, y, z), any length but not zero. */
|
|
61
|
+
export function gridNodeAt(g, x, y, z) {
|
|
62
|
+
const { face, s, t } = cubeFaceOf([x, y, z]);
|
|
63
|
+
const n = g.n;
|
|
64
|
+
const i = Math.min(n - 1, Math.max(0, Math.floor(((s + 1) / 2) * n)));
|
|
65
|
+
const j = Math.min(n - 1, Math.max(0, Math.floor(((t + 1) / 2) * n)));
|
|
66
|
+
return face * n * n + j * n + i;
|
|
67
|
+
}
|
|
68
|
+
/** Unit direction of a node's middle: `cubeDirection(face, cubeGrid(x + 0.5, n), cubeGrid(y + 0.5, n))`, bit for bit. */
|
|
69
|
+
export function gridDirection(g, node, out = [0, 0, 0]) {
|
|
70
|
+
const t = tablesOf(g.level);
|
|
71
|
+
const n = g.n;
|
|
72
|
+
const nn = n * n;
|
|
73
|
+
const face = Math.floor(node / nn);
|
|
74
|
+
const r = node - face * nn;
|
|
75
|
+
const y = Math.floor(r / n);
|
|
76
|
+
const x = r - y * n;
|
|
77
|
+
return cubeDirectionWarped(face, t.warp[x], t.warp[y], out);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 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,
|
|
81
|
+
* across an edge of the cube as `cubeNeighbour` folds it. The diagonal step over a corner of the cube does not exist
|
|
82
|
+
* and is left out, so the 24 corner nodes have 7. Writes them to `out` (at least 8 long) and returns how many.
|
|
83
|
+
*/
|
|
84
|
+
export function gridNeighbours(g, node, out) {
|
|
85
|
+
const n = g.n;
|
|
86
|
+
const nn = n * n;
|
|
87
|
+
const face = Math.floor(node / nn);
|
|
88
|
+
const r = node - face * nn;
|
|
89
|
+
const y = Math.floor(r / n);
|
|
90
|
+
const x = r - y * n;
|
|
91
|
+
let k = 0;
|
|
92
|
+
if (x > 0 && y > 0 && x < n - 1 && y < n - 1) {
|
|
93
|
+
const b = node;
|
|
94
|
+
out[0] = b - n - 1;
|
|
95
|
+
out[1] = b - n;
|
|
96
|
+
out[2] = b - n + 1;
|
|
97
|
+
out[3] = b - 1;
|
|
98
|
+
out[4] = b + 1;
|
|
99
|
+
out[5] = b + n - 1;
|
|
100
|
+
out[6] = b + n;
|
|
101
|
+
out[7] = b + n + 1;
|
|
102
|
+
return 8;
|
|
103
|
+
}
|
|
104
|
+
const t = tablesOf(g.level);
|
|
105
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
106
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
107
|
+
if (dx === 0 && dy === 0)
|
|
108
|
+
continue;
|
|
109
|
+
const q = nodeOf(t, n, face, x + dx, y + dy);
|
|
110
|
+
if (q >= 0)
|
|
111
|
+
out[k++] = q;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return k;
|
|
115
|
+
}
|
|
116
|
+
/** Area in m² of a node's cell, from the closed form of the equal-angle grid at its middle. */
|
|
117
|
+
export function gridNodeArea(g, node) {
|
|
118
|
+
const t = tablesOf(g.level);
|
|
119
|
+
const n = g.n;
|
|
120
|
+
const r = node % (n * n);
|
|
121
|
+
const y = Math.floor(r / n);
|
|
122
|
+
const x = r - y * n;
|
|
123
|
+
const a = t.warp[x];
|
|
124
|
+
const b = t.warp[y];
|
|
125
|
+
const a2 = a * a;
|
|
126
|
+
const b2 = b * b;
|
|
127
|
+
const q = 1 + a2 + b2;
|
|
128
|
+
const step = (Math.PI / 2 / n) * g.radiusM;
|
|
129
|
+
return (step * step * (1 + a2) * (1 + b2)) / (q * Math.sqrt(q));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* A field of one value per node, read at a direction (x, y, z): bilinear between the four nearest node middles in
|
|
133
|
+
* the face coordinates of the face that holds the direction. Past the last row or column of a face the nodes of the
|
|
134
|
+
* next face stand in; at a corner of the cube the missing fourth value is the mean of the other three. Equal to the
|
|
135
|
+
* field at a node middle, and continuous over the edges and corners of the cube.
|
|
136
|
+
*/
|
|
137
|
+
export function gridSample(g, field, x, y, z) {
|
|
138
|
+
const count = gridWeights(g, x, y, z, SAMPLE_NODES, SAMPLE_WEIGHTS);
|
|
139
|
+
let sum = 0;
|
|
140
|
+
for (let k = 0; k < count; k++)
|
|
141
|
+
sum += SAMPLE_WEIGHTS[k] * field[SAMPLE_NODES[k]];
|
|
142
|
+
return sum;
|
|
143
|
+
}
|
|
144
|
+
const SAMPLE_NODES = new Int32Array(4);
|
|
145
|
+
const SAMPLE_WEIGHTS = new Float64Array(4);
|
|
146
|
+
/**
|
|
147
|
+
* The nodes `gridSample` blends at a direction (x, y, z) and their weights (sum 1), in its order. Returns how many:
|
|
148
|
+
* 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
|
|
149
|
+
* `weights[k] · field[nodes[k]]` in this order is `gridSample` bit for bit.
|
|
150
|
+
*/
|
|
151
|
+
export function gridWeights(g, x, y, z, nodes, weights) {
|
|
152
|
+
const { face, s, t: tt } = cubeFaceOf([x, y, z]);
|
|
153
|
+
const n = g.n;
|
|
154
|
+
const tab = tablesOf(g.level);
|
|
155
|
+
const u = ((s + 1) / 2) * n - 0.5;
|
|
156
|
+
const v = ((tt + 1) / 2) * n - 0.5;
|
|
157
|
+
const i0 = Math.min(n - 1, Math.max(-1, Math.floor(u)));
|
|
158
|
+
const j0 = Math.min(n - 1, Math.max(-1, Math.floor(v)));
|
|
159
|
+
const fu = Math.min(1, Math.max(0, u - i0));
|
|
160
|
+
const fv = Math.min(1, Math.max(0, v - j0));
|
|
161
|
+
const ids = [
|
|
162
|
+
nodeOf(tab, n, face, i0, j0),
|
|
163
|
+
nodeOf(tab, n, face, i0 + 1, j0),
|
|
164
|
+
nodeOf(tab, n, face, i0, j0 + 1),
|
|
165
|
+
nodeOf(tab, n, face, i0 + 1, j0 + 1),
|
|
166
|
+
];
|
|
167
|
+
const w = [(1 - fu) * (1 - fv), fu * (1 - fv), (1 - fu) * fv, fu * fv];
|
|
168
|
+
// At most one of the four is missing (a corner of the cube): its weight goes in thirds to the others.
|
|
169
|
+
let missing = -1;
|
|
170
|
+
for (let k = 0; k < 4; k++)
|
|
171
|
+
if (ids[k] < 0)
|
|
172
|
+
missing = k;
|
|
173
|
+
let count = 0;
|
|
174
|
+
for (let k = 0; k < 4; k++) {
|
|
175
|
+
if (k === missing)
|
|
176
|
+
continue;
|
|
177
|
+
nodes[count] = ids[k];
|
|
178
|
+
weights[count] = missing >= 0 ? w[k] + w[missing] / 3 : w[k];
|
|
179
|
+
count++;
|
|
180
|
+
}
|
|
181
|
+
return count;
|
|
182
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { Vec3 } from '../base/math.ts';
|
|
2
|
+
import type { SphereGrid } from './grid.ts';
|
|
3
|
+
/** Four unit directions of a tetrahedron, in the axes of the grid: (1,1,1), (1,−1,−1), (−1,1,−1), (−1,−1,1) × 1/√3. */
|
|
4
|
+
export declare const HORIZON_AXES: readonly Vec3[];
|
|
5
|
+
export interface HorizonOptions {
|
|
6
|
+
/** How far along the surface the skyline is looked for, metres. */
|
|
7
|
+
reachM: number;
|
|
8
|
+
/** Samples along each direction; they lie closer near the point and farther apart toward `reachM`. */
|
|
9
|
+
steps: number;
|
|
10
|
+
/** Heights under this (metres) count as this, at the point and at the samples. Default: none. */
|
|
11
|
+
floorM?: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Sine of the skyline's elevation (never under 0) seen from the field's own height at the point (x, y, z) (any length,
|
|
15
|
+
* not zero), toward each of `HORIZON_AXES` laid into the tangent plane there. `heights` is one value per node of `g`,
|
|
16
|
+
* in units of `unitM` metres, read with `gridSample`. Writes four values to `out`. Where an axis is (nearly) the
|
|
17
|
+
* point's up, its value is 0.
|
|
18
|
+
*/
|
|
19
|
+
export declare function gridHorizon(g: SphereGrid, heights: ArrayLike<number>, unitM: number, x: number, y: number, z: number, o: HorizonOptions, out: Float32Array): void;
|
|
20
|
+
/** One byte of a skyline: `round(255 · sqrt(clamp(sine, 0, 1)))`. */
|
|
21
|
+
export declare function horizonCode(sine: number): number;
|
|
22
|
+
/** Skyline codes of a height field, computed at the grid's nodes on demand. Opaque; build it with `horizonField`. */
|
|
23
|
+
export interface HorizonField {
|
|
24
|
+
readonly grid: SphereGrid;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A field of skyline codes over `heights` (one value per node of `g`, units of `unitM` metres). Node codes are
|
|
28
|
+
* computed when first asked for and kept in a table of `slots` entries (default 65 536, five bytes each); a newer
|
|
29
|
+
* node replaces an older one in its slot. The table only saves time: results never depend on what it holds.
|
|
30
|
+
*/
|
|
31
|
+
export declare function horizonField(g: SphereGrid, heights: ArrayLike<number>, unitM: number, o: HorizonOptions & {
|
|
32
|
+
slots?: number;
|
|
33
|
+
}): HorizonField;
|
|
34
|
+
/**
|
|
35
|
+
* Four codes at the direction (x, y, z) (any length, not zero): the codes of the nodes round it, weighted as
|
|
36
|
+
* `gridSample` weights them, rounded. At a node it is that node's code.
|
|
37
|
+
*/
|
|
38
|
+
export declare function horizonCodeAt(field: HorizonField, x: number, y: number, z: number, out: Uint8Array): void;
|
|
39
|
+
/**
|
|
40
|
+
* How lit a point is by a light far away: 0 hidden by the skyline … 1 lit. `code` is four bytes 0–255 (as
|
|
41
|
+
* `horizonCodeAt` gives them), `up` and `toLight` unit vectors in the axes of the grid; `soft` (> 0) is the half-width
|
|
42
|
+
* of the edge in units of the sine. The skyline toward the light is the codes' sines blended by the squared share of
|
|
43
|
+
* each axis in the light's direction along the surface. The same formula as `HORIZON_GLSL`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function horizonLit(code: ArrayLike<number>, up: Vec3, toLight: Vec3, soft: number): number;
|
|
46
|
+
/**
|
|
47
|
+
* GLSL of `horizonLit`: `float horizonLit(vec4 code, vec3 up, vec3 toLight, float soft)`, with `code` read from a
|
|
48
|
+
* normalized u8 attribute (0–1). Needs no include and no uniform; paste it into either stage.
|
|
49
|
+
*/
|
|
50
|
+
export declare const HORIZON_GLSL: string;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
// The skyline of a height field over a sphere grid: how high the relief round a point rises, seen from the point's own
|
|
2
|
+
// height, toward four directions laid into its tangent plane. The four directions are those of a tetrahedron in the
|
|
3
|
+
// axes of the grid, projected: no spin axis is needed, a direction is lost only where the light gives it no weight,
|
|
4
|
+
// and every point (the poles of any axis included) keeps at least three of them.
|
|
5
|
+
//
|
|
6
|
+
// A skyline is kept as a code of four bytes (the square root of the sine, so low skylines get the finer steps). Codes
|
|
7
|
+
// are computed at the nodes of the grid and blended to any point with the weights `gridSample` uses, so a point gets
|
|
8
|
+
// the same code from whichever caller asks. `horizonLit` and its GLSL twin turn a code, the point's up and a direction
|
|
9
|
+
// toward a light into a factor from hidden (0) to lit (1). Arithmetic and `Math.sqrt/floor/abs/min/max/round` only.
|
|
10
|
+
import { gridDirection, gridSample, gridWeights } from './grid.js';
|
|
11
|
+
const AXIS = 0.5773502691896258;
|
|
12
|
+
/** Four unit directions of a tetrahedron, in the axes of the grid: (1,1,1), (1,−1,−1), (−1,1,−1), (−1,−1,1) × 1/√3. */
|
|
13
|
+
export const HORIZON_AXES = [
|
|
14
|
+
[AXIS, AXIS, AXIS],
|
|
15
|
+
[AXIS, -AXIS, -AXIS],
|
|
16
|
+
[-AXIS, AXIS, -AXIS],
|
|
17
|
+
[-AXIS, -AXIS, AXIS],
|
|
18
|
+
];
|
|
19
|
+
/**
|
|
20
|
+
* Sine of the skyline's elevation (never under 0) seen from the field's own height at the point (x, y, z) (any length,
|
|
21
|
+
* not zero), toward each of `HORIZON_AXES` laid into the tangent plane there. `heights` is one value per node of `g`,
|
|
22
|
+
* in units of `unitM` metres, read with `gridSample`. Writes four values to `out`. Where an axis is (nearly) the
|
|
23
|
+
* point's up, its value is 0.
|
|
24
|
+
*/
|
|
25
|
+
export function gridHorizon(g, heights, unitM, x, y, z, o, out) {
|
|
26
|
+
const R = g.radiusM;
|
|
27
|
+
const len = Math.sqrt(x * x + y * y + z * z);
|
|
28
|
+
const ux = x / len;
|
|
29
|
+
const uy = y / len;
|
|
30
|
+
const uz = z / len;
|
|
31
|
+
const floor = o.floorM;
|
|
32
|
+
const heightAt = (dx, dy, dz) => {
|
|
33
|
+
const h = gridSample(g, heights, dx, dy, dz) * unitM;
|
|
34
|
+
return floor !== undefined && h < floor ? floor : h;
|
|
35
|
+
};
|
|
36
|
+
const r0 = R + heightAt(ux, uy, uz);
|
|
37
|
+
const steps = o.steps;
|
|
38
|
+
for (let k = 0; k < 4; k++) {
|
|
39
|
+
const a = HORIZON_AXES[k];
|
|
40
|
+
const au = a[0] * ux + a[1] * uy + a[2] * uz;
|
|
41
|
+
let tx = a[0] - au * ux;
|
|
42
|
+
let ty = a[1] - au * uy;
|
|
43
|
+
let tz = a[2] - au * uz;
|
|
44
|
+
const tt = tx * tx + ty * ty + tz * tz;
|
|
45
|
+
if (tt < 1e-12) {
|
|
46
|
+
out[k] = 0;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const tl = Math.sqrt(tt);
|
|
50
|
+
tx /= tl;
|
|
51
|
+
ty /= tl;
|
|
52
|
+
tz /= tl;
|
|
53
|
+
let best = 0;
|
|
54
|
+
for (let j = 0; j < steps; j++) {
|
|
55
|
+
const tau = (j + 1) / steps;
|
|
56
|
+
const d = o.reachM * tau * (0.3 + 0.7 * tau);
|
|
57
|
+
const q = d / R;
|
|
58
|
+
let sx = ux + tx * q;
|
|
59
|
+
let sy = uy + ty * q;
|
|
60
|
+
let sz = uz + tz * q;
|
|
61
|
+
const sl = Math.sqrt(sx * sx + sy * sy + sz * sz);
|
|
62
|
+
sx /= sl;
|
|
63
|
+
sy /= sl;
|
|
64
|
+
sz /= sl;
|
|
65
|
+
const c = 1 / Math.sqrt(1 + q * q);
|
|
66
|
+
const s = q * c;
|
|
67
|
+
const r = R + heightAt(sx, sy, sz);
|
|
68
|
+
const rise = r * c - r0;
|
|
69
|
+
if (rise <= 0)
|
|
70
|
+
continue;
|
|
71
|
+
const run = r * s;
|
|
72
|
+
const sine = rise / Math.sqrt(rise * rise + run * run);
|
|
73
|
+
if (sine > best)
|
|
74
|
+
best = sine;
|
|
75
|
+
}
|
|
76
|
+
out[k] = best;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** One byte of a skyline: `round(255 · sqrt(clamp(sine, 0, 1)))`. */
|
|
80
|
+
export function horizonCode(sine) {
|
|
81
|
+
const s = sine < 0 ? 0 : sine > 1 ? 1 : sine;
|
|
82
|
+
return Math.round(255 * Math.sqrt(s));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A field of skyline codes over `heights` (one value per node of `g`, units of `unitM` metres). Node codes are
|
|
86
|
+
* computed when first asked for and kept in a table of `slots` entries (default 65 536, five bytes each); a newer
|
|
87
|
+
* node replaces an older one in its slot. The table only saves time: results never depend on what it holds.
|
|
88
|
+
*/
|
|
89
|
+
export function horizonField(g, heights, unitM, o) {
|
|
90
|
+
if (heights.length !== g.count)
|
|
91
|
+
throw new Error(`horizonField: ${heights.length} heights for ${g.count} nodes`);
|
|
92
|
+
const slots = o.slots ?? 65_536;
|
|
93
|
+
if (!(slots >= 1 && Number.isInteger(slots)))
|
|
94
|
+
throw new Error(`horizonField: slots ${slots} must be a positive integer`);
|
|
95
|
+
const options = { reachM: o.reachM, steps: o.steps };
|
|
96
|
+
if (o.floorM !== undefined)
|
|
97
|
+
options.floorM = o.floorM;
|
|
98
|
+
const state = { grid: g, heights, unitM, options, keys: new Int32Array(slots).fill(-1), codes: new Uint8Array(slots * 4) };
|
|
99
|
+
return state;
|
|
100
|
+
}
|
|
101
|
+
const NODE_DIR = [0, 0, 0];
|
|
102
|
+
const NODE_SINES = new Float32Array(4);
|
|
103
|
+
const AT_NODES = new Int32Array(4);
|
|
104
|
+
const AT_WEIGHTS = new Float64Array(4);
|
|
105
|
+
/** The four codes of one node, from the table or computed into it. Returns the slot's offset into `codes`. */
|
|
106
|
+
function nodeCodes(f, node) {
|
|
107
|
+
const slot = node % f.keys.length;
|
|
108
|
+
const off = slot * 4;
|
|
109
|
+
if (f.keys[slot] === node)
|
|
110
|
+
return off;
|
|
111
|
+
gridDirection(f.grid, node, NODE_DIR);
|
|
112
|
+
gridHorizon(f.grid, f.heights, f.unitM, NODE_DIR[0], NODE_DIR[1], NODE_DIR[2], f.options, NODE_SINES);
|
|
113
|
+
for (let k = 0; k < 4; k++)
|
|
114
|
+
f.codes[off + k] = horizonCode(NODE_SINES[k]);
|
|
115
|
+
f.keys[slot] = node;
|
|
116
|
+
return off;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Four codes at the direction (x, y, z) (any length, not zero): the codes of the nodes round it, weighted as
|
|
120
|
+
* `gridSample` weights them, rounded. At a node it is that node's code.
|
|
121
|
+
*/
|
|
122
|
+
export function horizonCodeAt(field, x, y, z, out) {
|
|
123
|
+
const f = field;
|
|
124
|
+
const count = gridWeights(f.grid, x, y, z, AT_NODES, AT_WEIGHTS);
|
|
125
|
+
let c0 = 0;
|
|
126
|
+
let c1 = 0;
|
|
127
|
+
let c2 = 0;
|
|
128
|
+
let c3 = 0;
|
|
129
|
+
for (let k = 0; k < count; k++) {
|
|
130
|
+
const off = nodeCodes(f, AT_NODES[k]);
|
|
131
|
+
const w = AT_WEIGHTS[k];
|
|
132
|
+
c0 += w * f.codes[off];
|
|
133
|
+
c1 += w * f.codes[off + 1];
|
|
134
|
+
c2 += w * f.codes[off + 2];
|
|
135
|
+
c3 += w * f.codes[off + 3];
|
|
136
|
+
}
|
|
137
|
+
out[0] = Math.min(255, Math.round(c0));
|
|
138
|
+
out[1] = Math.min(255, Math.round(c1));
|
|
139
|
+
out[2] = Math.min(255, Math.round(c2));
|
|
140
|
+
out[3] = Math.min(255, Math.round(c3));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* How lit a point is by a light far away: 0 hidden by the skyline … 1 lit. `code` is four bytes 0–255 (as
|
|
144
|
+
* `horizonCodeAt` gives them), `up` and `toLight` unit vectors in the axes of the grid; `soft` (> 0) is the half-width
|
|
145
|
+
* of the edge in units of the sine. The skyline toward the light is the codes' sines blended by the squared share of
|
|
146
|
+
* each axis in the light's direction along the surface. The same formula as `HORIZON_GLSL`.
|
|
147
|
+
*/
|
|
148
|
+
export function horizonLit(code, up, toLight, soft) {
|
|
149
|
+
const s = up[0] * toLight[0] + up[1] * toLight[1] + up[2] * toLight[2];
|
|
150
|
+
const tx = toLight[0] - up[0] * s;
|
|
151
|
+
const ty = toLight[1] - up[1] * s;
|
|
152
|
+
const tz = toLight[2] - up[2] * s;
|
|
153
|
+
let sw = 0;
|
|
154
|
+
let sum = 0;
|
|
155
|
+
for (let k = 0; k < 4; k++) {
|
|
156
|
+
const a = HORIZON_AXES[k];
|
|
157
|
+
const d = Math.max(0, tx * a[0] + ty * a[1] + tz * a[2]);
|
|
158
|
+
const w = d * d;
|
|
159
|
+
const c = code[k] / 255;
|
|
160
|
+
sw += w;
|
|
161
|
+
sum += w * c * c;
|
|
162
|
+
}
|
|
163
|
+
const skyline = sw < 1e-12 ? 0 : sum / sw;
|
|
164
|
+
const t = Math.min(1, Math.max(0, (s - skyline + soft) / (2 * soft)));
|
|
165
|
+
return t * t * (3 - 2 * t);
|
|
166
|
+
}
|
|
167
|
+
const glslAxis = (k) => {
|
|
168
|
+
const a = HORIZON_AXES[k];
|
|
169
|
+
return `vec3(${a.map((v) => v.toFixed(16)).join(', ')})`;
|
|
170
|
+
};
|
|
171
|
+
/**
|
|
172
|
+
* GLSL of `horizonLit`: `float horizonLit(vec4 code, vec3 up, vec3 toLight, float soft)`, with `code` read from a
|
|
173
|
+
* normalized u8 attribute (0–1). Needs no include and no uniform; paste it into either stage.
|
|
174
|
+
*/
|
|
175
|
+
export const HORIZON_GLSL = /* glsl */ `
|
|
176
|
+
float horizonLit(vec4 code, vec3 up, vec3 toLight, float soft) {
|
|
177
|
+
float s = dot(up, toLight);
|
|
178
|
+
vec3 t = toLight - up * s;
|
|
179
|
+
vec4 d = max(vec4(dot(t, ${glslAxis(0)}), dot(t, ${glslAxis(1)}), dot(t, ${glslAxis(2)}), dot(t, ${glslAxis(3)})), 0.0);
|
|
180
|
+
vec4 w = d * d;
|
|
181
|
+
float sw = w.x + w.y + w.z + w.w;
|
|
182
|
+
float skyline = sw < 1e-12 ? 0.0 : dot(w, code * code) / sw;
|
|
183
|
+
return smoothstep(-soft, soft, s - skyline);
|
|
184
|
+
}
|
|
185
|
+
`;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export { gridDirection, gridNeighbours, gridNodeArea, gridNodeAt, gridSample, gridWeights, sphereGrid } from './grid.ts';
|
|
2
|
+
export type { SphereGrid } from './grid.ts';
|
|
3
|
+
export { drainGrid, traceChannels } from './drain.ts';
|
|
4
|
+
export type { Channels, DrainOptions, Drainage } from './drain.ts';
|
|
5
|
+
export { fallingGrade, limitGrade, lineAt, lineLengths, smoothLine } from './lines.ts';
|
|
6
|
+
export { regionLinks, routeGrid, routeRegions, routeScratch } from './route.ts';
|
|
7
|
+
export type { RegionLinks, RouteRegions, RouteScratch, StepCost } from './route.ts';
|
|
8
|
+
export { lineTiles, tileLinesAt, tileLinesOfCell } from './line-tiles.ts';
|
|
9
|
+
export type { LineTiles, LineTilesOptions } from './line-tiles.ts';
|
|
10
|
+
export { gridSlopeAt, gridSteps, pickSites } from './sites.ts';
|
|
11
|
+
export type { PickOptions } from './sites.ts';
|
|
12
|
+
export { plotsAlong } from './plots.ts';
|
|
13
|
+
export type { Plot, PlotOptions } from './plots.ts';
|
|
14
|
+
export { HORIZON_AXES, HORIZON_GLSL, gridHorizon, horizonCode, horizonCodeAt, horizonField, horizonLit } from './horizon.ts';
|
|
15
|
+
export type { HorizonField, HorizonOptions } from './horizon.ts';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// Layer 3, gen-plan: coarse plans over a whole sphere. A grid of nodes at the middles of the cells of one level of the
|
|
2
|
+
// cube sphere, drainage of a height field over it (filled hollows, pools, flow, channels), lines over the sphere
|
|
3
|
+
// (smoothing, a profile that never rises, a limited grade, lengths and points along them), least-cost ways and regions
|
|
4
|
+
// over the grid, picks of nodes by a score (sites), plots along lines on a plane, an index from tiles to the lines that
|
|
5
|
+
// can reach them and the skyline (horizon) of a height field. Imports only `base` and `gen-core`. Pure, run anywhere.
|
|
6
|
+
export { gridDirection, gridNeighbours, gridNodeArea, gridNodeAt, gridSample, gridWeights, sphereGrid } from './grid.js';
|
|
7
|
+
export { drainGrid, traceChannels } from './drain.js';
|
|
8
|
+
export { fallingGrade, limitGrade, lineAt, lineLengths, smoothLine } from './lines.js';
|
|
9
|
+
export { regionLinks, routeGrid, routeRegions, routeScratch } from './route.js';
|
|
10
|
+
export { lineTiles, tileLinesAt, tileLinesOfCell } from './line-tiles.js';
|
|
11
|
+
export { gridSlopeAt, gridSteps, pickSites } from './sites.js';
|
|
12
|
+
export { plotsAlong } from './plots.js';
|
|
13
|
+
export { HORIZON_AXES, HORIZON_GLSL, gridHorizon, horizonCode, horizonCodeAt, horizonField, horizonLit } from './horizon.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CellId } from '../gen-core/cell.ts';
|
|
2
|
+
/** The index: tiles that hold a line, ascending, and per tile its lines, ascending. */
|
|
3
|
+
export interface LineTiles {
|
|
4
|
+
level: number;
|
|
5
|
+
/** Keys of the tiles that hold at least one line, ascending. */
|
|
6
|
+
keys: Uint32Array;
|
|
7
|
+
/** Per key + 1: its first entry in `lines`. */
|
|
8
|
+
start: Uint32Array;
|
|
9
|
+
lines: Uint32Array;
|
|
10
|
+
}
|
|
11
|
+
/** Options of `lineTiles`. */
|
|
12
|
+
export interface LineTilesOptions {
|
|
13
|
+
/** Level of the tiles. */
|
|
14
|
+
level: number;
|
|
15
|
+
/** Radius of the sphere the lines lie on, m. */
|
|
16
|
+
radiusM: number;
|
|
17
|
+
/** Per line + 1: its first point. */
|
|
18
|
+
start: ArrayLike<number>;
|
|
19
|
+
/** Unit directions, x y z per point. */
|
|
20
|
+
points: ArrayLike<number>;
|
|
21
|
+
/** Per line: how far from its segments it reaches, m. */
|
|
22
|
+
reachM: ArrayLike<number>;
|
|
23
|
+
}
|
|
24
|
+
/** Builds the index of lines `o.start` over `o.points` at tiles of `o.level`. */
|
|
25
|
+
export declare function lineTiles(o: LineTilesOptions): LineTiles;
|
|
26
|
+
/** The lines of the tile that holds the direction (x, y, z), as a range of `t.lines`; null when the tile holds none. */
|
|
27
|
+
export declare function tileLinesAt(t: LineTiles, x: number, y: number, z: number): {
|
|
28
|
+
from: number;
|
|
29
|
+
to: number;
|
|
30
|
+
} | null;
|
|
31
|
+
/**
|
|
32
|
+
* The lines that can reach a cell of the cube sphere, ascending and each once: those of the tile that holds it when it
|
|
33
|
+
* is as fine as the tiles or finer, else those of every tile under it. Clears `out` and returns it.
|
|
34
|
+
*/
|
|
35
|
+
export declare function tileLinesOfCell(t: LineTiles, cell: CellId, out: number[]): number[];
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// An index from the cells of one level of the cube sphere ("tiles") to the lines over the sphere that can reach
|
|
2
|
+
// them. A line is listed in every tile that has a point within its reach of any of its segments; more tiles than
|
|
3
|
+
// needed are allowed, fewer never. Key of a tile: `face · 4^level + y · 2^level + x`.
|
|
4
|
+
import { gridDirection, gridNeighbours, gridNodeAt, sphereGrid } from './grid.js';
|
|
5
|
+
/**
|
|
6
|
+
* Bound on the angle from the middle of a tile to any point of it, in radians, for tiles of `n` across a face. The
|
|
7
|
+
* longest half diagonal is at a corner of the cube, not in the middle of a face: there the cell is a rhombus with a
|
|
8
|
+
* 120° angle, its edges scaled by 2√2/3 against the π/2n of a face's middle, and half its long diagonal tends to
|
|
9
|
+
* 2√2/3 · cos 30° · π/2n ≈ 0.8165 · π/2n. 0.85 keeps a margin above that.
|
|
10
|
+
*/
|
|
11
|
+
const tileRadius = (n) => (0.85 * Math.PI) / (2 * n);
|
|
12
|
+
/** Builds the index of lines `o.start` over `o.points` at tiles of `o.level`. */
|
|
13
|
+
export function lineTiles(o) {
|
|
14
|
+
const g = sphereGrid(o.radiusM, o.level);
|
|
15
|
+
const lineCount = o.start.length - 1;
|
|
16
|
+
const tr = tileRadius(g.n);
|
|
17
|
+
const nb = new Int32Array(8);
|
|
18
|
+
const c = [0, 0, 0];
|
|
19
|
+
const pairs = [];
|
|
20
|
+
const seen = new Set();
|
|
21
|
+
const visited = new Set();
|
|
22
|
+
const queue = [];
|
|
23
|
+
for (let line = 0; line < lineCount; line++) {
|
|
24
|
+
seen.clear();
|
|
25
|
+
const p0 = o.start[line];
|
|
26
|
+
const p1 = o.start[line + 1];
|
|
27
|
+
const reach = o.reachM[line];
|
|
28
|
+
if (p1 <= p0)
|
|
29
|
+
continue;
|
|
30
|
+
for (let p = p0; p < Math.max(p0 + 1, p1 - 1); p++) {
|
|
31
|
+
// The segment from point p to p + 1 (a single point when the line has one).
|
|
32
|
+
const q = Math.min(p + 1, p1 - 1);
|
|
33
|
+
const ax = o.points[3 * p];
|
|
34
|
+
const ay = o.points[3 * p + 1];
|
|
35
|
+
const az = o.points[3 * p + 2];
|
|
36
|
+
const bx = o.points[3 * q];
|
|
37
|
+
const by = o.points[3 * q + 1];
|
|
38
|
+
const bz = o.points[3 * q + 2];
|
|
39
|
+
let mx = ax + bx;
|
|
40
|
+
let my = ay + by;
|
|
41
|
+
let mz = az + bz;
|
|
42
|
+
const ml = Math.sqrt(mx * mx + my * my + mz * mz);
|
|
43
|
+
mx /= ml;
|
|
44
|
+
my /= ml;
|
|
45
|
+
mz /= ml;
|
|
46
|
+
const ex = ax - bx;
|
|
47
|
+
const ey = ay - by;
|
|
48
|
+
const ez = az - bz;
|
|
49
|
+
const half = 0.5 * Math.sqrt(ex * ex + ey * ey + ez * ez);
|
|
50
|
+
// Every point within the reach of the segment lies within this angle of its middle (chord ≤ arc, with margin).
|
|
51
|
+
const limit = (reach + 2) / o.radiusM + 1.01 * half + tr;
|
|
52
|
+
const limit2 = limit * limit;
|
|
53
|
+
visited.clear();
|
|
54
|
+
queue.length = 0;
|
|
55
|
+
const first = gridNodeAt(g, mx, my, mz);
|
|
56
|
+
visited.add(first);
|
|
57
|
+
queue.push(first);
|
|
58
|
+
while (queue.length > 0) {
|
|
59
|
+
const t = queue.pop();
|
|
60
|
+
if (!seen.has(t)) {
|
|
61
|
+
seen.add(t);
|
|
62
|
+
pairs.push(t, line);
|
|
63
|
+
}
|
|
64
|
+
const k = gridNeighbours(g, t, nb);
|
|
65
|
+
for (let j = 0; j < k; j++) {
|
|
66
|
+
const u = nb[j];
|
|
67
|
+
if (visited.has(u))
|
|
68
|
+
continue;
|
|
69
|
+
visited.add(u);
|
|
70
|
+
gridDirection(g, u, c);
|
|
71
|
+
const dx = c[0] - mx;
|
|
72
|
+
const dy = c[1] - my;
|
|
73
|
+
const dz = c[2] - mz;
|
|
74
|
+
if (dx * dx + dy * dy + dz * dz <= limit2)
|
|
75
|
+
queue.push(u);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
// Grid nodes and tile keys are the same numbers: face · n² + y · n + x. The pairs were made line by line, so a sort
|
|
81
|
+
// by key that keeps the order of equal keys leaves the lines of a tile ascending.
|
|
82
|
+
const total = pairs.length / 2;
|
|
83
|
+
const tileKey = new Uint32Array(total);
|
|
84
|
+
const tileLine = new Uint32Array(total);
|
|
85
|
+
for (let i = 0; i < total; i++) {
|
|
86
|
+
tileKey[i] = pairs[2 * i];
|
|
87
|
+
tileLine[i] = pairs[2 * i + 1];
|
|
88
|
+
}
|
|
89
|
+
let lineSpan = 1;
|
|
90
|
+
while (lineSpan <= lineCount)
|
|
91
|
+
lineSpan *= 2;
|
|
92
|
+
let sorted;
|
|
93
|
+
if (g.count * lineSpan <= 2 ** 53) {
|
|
94
|
+
// Key and line packed into one exact double: a plain numeric sort.
|
|
95
|
+
const packed = new Float64Array(total);
|
|
96
|
+
for (let i = 0; i < total; i++)
|
|
97
|
+
packed[i] = tileKey[i] * lineSpan + tileLine[i];
|
|
98
|
+
packed.sort();
|
|
99
|
+
const index = new Uint32Array(total);
|
|
100
|
+
for (let i = 0; i < total; i++) {
|
|
101
|
+
const key = Math.floor(packed[i] / lineSpan);
|
|
102
|
+
tileKey[i] = key;
|
|
103
|
+
tileLine[i] = packed[i] - key * lineSpan;
|
|
104
|
+
index[i] = i;
|
|
105
|
+
}
|
|
106
|
+
sorted = index;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
const index = Array.from({ length: total }, (_, i) => i);
|
|
110
|
+
index.sort((a, b) => tileKey[a] - tileKey[b] || a - b);
|
|
111
|
+
sorted = index;
|
|
112
|
+
}
|
|
113
|
+
const keys = [];
|
|
114
|
+
const start = [];
|
|
115
|
+
const lines = new Uint32Array(total);
|
|
116
|
+
for (let i = 0; i < total; i++) {
|
|
117
|
+
const j = sorted[i];
|
|
118
|
+
const key = tileKey[j];
|
|
119
|
+
if (keys.length === 0 || keys[keys.length - 1] !== key) {
|
|
120
|
+
keys.push(key);
|
|
121
|
+
start.push(i);
|
|
122
|
+
}
|
|
123
|
+
lines[i] = tileLine[j];
|
|
124
|
+
}
|
|
125
|
+
start.push(total);
|
|
126
|
+
return { level: o.level, keys: Uint32Array.from(keys), start: Uint32Array.from(start), lines };
|
|
127
|
+
}
|
|
128
|
+
/** Index in `t.keys` of `key`, or −1. */
|
|
129
|
+
function findKey(t, key) {
|
|
130
|
+
let lo = 0;
|
|
131
|
+
let hi = t.keys.length;
|
|
132
|
+
while (lo < hi) {
|
|
133
|
+
const mid = (lo + hi) >> 1;
|
|
134
|
+
if (t.keys[mid] < key)
|
|
135
|
+
lo = mid + 1;
|
|
136
|
+
else
|
|
137
|
+
hi = mid;
|
|
138
|
+
}
|
|
139
|
+
return lo < t.keys.length && t.keys[lo] === key ? lo : -1;
|
|
140
|
+
}
|
|
141
|
+
/** The lines of the tile that holds the direction (x, y, z), as a range of `t.lines`; null when the tile holds none. */
|
|
142
|
+
export function tileLinesAt(t, x, y, z) {
|
|
143
|
+
const key = gridNodeAt(sphereGrid(1, t.level), x, y, z);
|
|
144
|
+
const k = findKey(t, key);
|
|
145
|
+
return k < 0 ? null : { from: t.start[k], to: t.start[k + 1] };
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The lines that can reach a cell of the cube sphere, ascending and each once: those of the tile that holds it when it
|
|
149
|
+
* is as fine as the tiles or finer, else those of every tile under it. Clears `out` and returns it.
|
|
150
|
+
*/
|
|
151
|
+
export function tileLinesOfCell(t, cell, out) {
|
|
152
|
+
out.length = 0;
|
|
153
|
+
const n = 2 ** t.level;
|
|
154
|
+
const base = cell.face * n * n;
|
|
155
|
+
if (cell.level >= t.level) {
|
|
156
|
+
const shift = 2 ** (cell.level - t.level);
|
|
157
|
+
const k = findKey(t, base + Math.floor(cell.y / shift) * n + Math.floor(cell.x / shift));
|
|
158
|
+
if (k >= 0)
|
|
159
|
+
for (let i = t.start[k]; i < t.start[k + 1]; i++)
|
|
160
|
+
out.push(t.lines[i]);
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
const span = 2 ** (t.level - cell.level);
|
|
164
|
+
const x0 = cell.x * span;
|
|
165
|
+
const y0 = cell.y * span;
|
|
166
|
+
for (let y = y0; y < y0 + span; y++) {
|
|
167
|
+
const row = base + y * n;
|
|
168
|
+
// The first key of this row inside the cell, then on while the keys stay inside it.
|
|
169
|
+
let lo = 0;
|
|
170
|
+
let hi = t.keys.length;
|
|
171
|
+
while (lo < hi) {
|
|
172
|
+
const mid = (lo + hi) >> 1;
|
|
173
|
+
if (t.keys[mid] < row + x0)
|
|
174
|
+
lo = mid + 1;
|
|
175
|
+
else
|
|
176
|
+
hi = mid;
|
|
177
|
+
}
|
|
178
|
+
for (let k = lo; k < t.keys.length && t.keys[k] < row + x0 + span; k++) {
|
|
179
|
+
for (let i = t.start[k]; i < t.start[k + 1]; i++)
|
|
180
|
+
out.push(t.lines[i]);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
out.sort((a, b) => a - b);
|
|
184
|
+
let w = 0;
|
|
185
|
+
for (let i = 0; i < out.length; i++)
|
|
186
|
+
if (i === 0 || out[i] !== out[w - 1])
|
|
187
|
+
out[w++] = out[i];
|
|
188
|
+
out.length = w;
|
|
189
|
+
return out;
|
|
190
|
+
}
|