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,58 @@
|
|
|
1
|
+
// Stateless randomness. Every number is a pure function of its key: world seed → subsystem → cell → index.
|
|
2
|
+
// Subsystems have their own streams, so changing one subsystem does not move another, and an object has a seed from its own ID.
|
|
3
|
+
/** FNV-1a over the characters of a string, the result is an unsigned 32-bit number. */
|
|
4
|
+
export function hashString(s) {
|
|
5
|
+
let h = 0x811c9dc5;
|
|
6
|
+
for (let i = 0; i < s.length; i++) {
|
|
7
|
+
h ^= s.charCodeAt(i);
|
|
8
|
+
h = Math.imul(h, 0x01000193);
|
|
9
|
+
}
|
|
10
|
+
return h >>> 0;
|
|
11
|
+
}
|
|
12
|
+
/** Full avalanche mixing of 32 bits (finalizer from MurmurHash3). */
|
|
13
|
+
export function mix32(h) {
|
|
14
|
+
h ^= h >>> 16;
|
|
15
|
+
h = Math.imul(h, 0x85ebca6b);
|
|
16
|
+
h ^= h >>> 13;
|
|
17
|
+
h = Math.imul(h, 0xc2b2ae35);
|
|
18
|
+
h ^= h >>> 16;
|
|
19
|
+
return h >>> 0;
|
|
20
|
+
}
|
|
21
|
+
/** Combines a seed with another integer. */
|
|
22
|
+
export function hashInt(seed, k) {
|
|
23
|
+
return mix32((seed ^ Math.imul(k | 0, 0x9e3779b1)) + 0x7f4a7c15);
|
|
24
|
+
}
|
|
25
|
+
/** Combines a seed with any number of integers. The order of keys matters. */
|
|
26
|
+
export function hashInts(seed, ...keys) {
|
|
27
|
+
let h = seed >>> 0;
|
|
28
|
+
for (const k of keys)
|
|
29
|
+
h = hashInt(h, k);
|
|
30
|
+
return h;
|
|
31
|
+
}
|
|
32
|
+
/** Seed of one subsystem's stream. */
|
|
33
|
+
export function streamSeed(worldSeed, subsystem) {
|
|
34
|
+
return hashInt(hashString(worldSeed), hashString(subsystem));
|
|
35
|
+
}
|
|
36
|
+
/** Number in the range 0 to 1 (excluding 1) from a hash. */
|
|
37
|
+
export function unit(h) {
|
|
38
|
+
return (h >>> 0) / 4294967296;
|
|
39
|
+
}
|
|
40
|
+
/** Number 0 to 1 for a given seed and keys. */
|
|
41
|
+
export function rand(seed, ...keys) {
|
|
42
|
+
return unit(hashInts(seed, ...keys));
|
|
43
|
+
}
|
|
44
|
+
/** Number in the range lo to hi. */
|
|
45
|
+
export function randRange(lo, hi, seed, ...keys) {
|
|
46
|
+
return lo + (hi - lo) * rand(seed, ...keys);
|
|
47
|
+
}
|
|
48
|
+
/** Choice among options with weights according to a number 0–1. */
|
|
49
|
+
export function pickWeighted(options, r) {
|
|
50
|
+
const total = options.reduce((s, o) => s + o[1], 0);
|
|
51
|
+
let acc = 0;
|
|
52
|
+
for (const [v, w] of options) {
|
|
53
|
+
acc += w / total;
|
|
54
|
+
if (r < acc)
|
|
55
|
+
return v;
|
|
56
|
+
}
|
|
57
|
+
return options[options.length - 1][0];
|
|
58
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare class HeightField {
|
|
2
|
+
/** Vertex spacing in metres. */
|
|
3
|
+
readonly step: number;
|
|
4
|
+
readonly cols: number;
|
|
5
|
+
readonly rows: number;
|
|
6
|
+
/** Heights in metres above sea level, row by row from the north. */
|
|
7
|
+
readonly heights: Float32Array;
|
|
8
|
+
readonly minY: number;
|
|
9
|
+
readonly maxY: number;
|
|
10
|
+
constructor(step: number, cols: number, rows: number, heights: Float32Array);
|
|
11
|
+
get sizeX(): number;
|
|
12
|
+
get sizeZ(): number;
|
|
13
|
+
/**
|
|
14
|
+
* Thins a denser grid to every n-th vertex. The height of a new vertex is a weighted average of its 3 × 3 neighbourhood,
|
|
15
|
+
* so the thinning does not pick random spikes.
|
|
16
|
+
*/
|
|
17
|
+
static decimate(step: number, cols: number, rows: number, src: ArrayLike<number>, scale: number, offset: number, n: number): HeightField;
|
|
18
|
+
/**
|
|
19
|
+
* The surface that a grid thinned to every n-th vertex would draw, evaluated at every vertex of the full grid: at a
|
|
20
|
+
* kept vertex the value itself, between kept vertices the value of the coarse triangle there (a coarse quad is split
|
|
21
|
+
* by the same diagonal as in `heightAt`). This is the shape of a level of detail n times coarser, sampled where the
|
|
22
|
+
* finer level has its vertices; a blend between two levels needs it. `cols − 1` and `rows − 1` must be multiples of
|
|
23
|
+
* n. Works for any per-vertex value (a height, one component of a normal, one colour channel).
|
|
24
|
+
*
|
|
25
|
+
* On a line of the coarse grid the value depends only on the two kept vertices of that line, in one fixed formula,
|
|
26
|
+
* so two grids that share such a line (neighbouring cells) get identical numbers along it.
|
|
27
|
+
*/
|
|
28
|
+
static coarseSurface(cols: number, rows: number, src: ArrayLike<number>, n: number): Float64Array;
|
|
29
|
+
/** Height at a point. Computed per triangle exactly as the mesh draws them, so objects sit precisely on the surface. */
|
|
30
|
+
heightAt(x: number, z: number): number;
|
|
31
|
+
/** Normal of grid vertex (i, j), same as in `heightFieldMesh`: from the difference of neighbouring heights. */
|
|
32
|
+
vertexNormal(i: number, j: number): [number, number, number];
|
|
33
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// Height field: a regular grid of heights with a bilinear height query and grid normals.
|
|
2
|
+
export class HeightField {
|
|
3
|
+
/** Vertex spacing in metres. */
|
|
4
|
+
step;
|
|
5
|
+
cols;
|
|
6
|
+
rows;
|
|
7
|
+
/** Heights in metres above sea level, row by row from the north. */
|
|
8
|
+
heights;
|
|
9
|
+
minY;
|
|
10
|
+
maxY;
|
|
11
|
+
constructor(step, cols, rows, heights) {
|
|
12
|
+
this.step = step;
|
|
13
|
+
this.cols = cols;
|
|
14
|
+
this.rows = rows;
|
|
15
|
+
this.heights = heights;
|
|
16
|
+
let lo = Infinity;
|
|
17
|
+
let hi = -Infinity;
|
|
18
|
+
for (const h of heights) {
|
|
19
|
+
if (h < lo)
|
|
20
|
+
lo = h;
|
|
21
|
+
if (h > hi)
|
|
22
|
+
hi = h;
|
|
23
|
+
}
|
|
24
|
+
this.minY = lo;
|
|
25
|
+
this.maxY = hi;
|
|
26
|
+
}
|
|
27
|
+
get sizeX() {
|
|
28
|
+
return (this.cols - 1) * this.step;
|
|
29
|
+
}
|
|
30
|
+
get sizeZ() {
|
|
31
|
+
return (this.rows - 1) * this.step;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Thins a denser grid to every n-th vertex. The height of a new vertex is a weighted average of its 3 × 3 neighbourhood,
|
|
35
|
+
* so the thinning does not pick random spikes.
|
|
36
|
+
*/
|
|
37
|
+
static decimate(step, cols, rows, src, scale, offset, n) {
|
|
38
|
+
const c2 = Math.floor((cols - 1) / n) + 1;
|
|
39
|
+
const r2 = Math.floor((rows - 1) / n) + 1;
|
|
40
|
+
const out = new Float32Array(c2 * r2);
|
|
41
|
+
for (let j = 0; j < r2; j++) {
|
|
42
|
+
for (let i = 0; i < c2; i++) {
|
|
43
|
+
let sum = 0;
|
|
44
|
+
let wsum = 0;
|
|
45
|
+
for (let dj = -1; dj <= 1; dj++) {
|
|
46
|
+
const jj = j * n + dj;
|
|
47
|
+
if (jj < 0 || jj >= rows)
|
|
48
|
+
continue;
|
|
49
|
+
for (let di = -1; di <= 1; di++) {
|
|
50
|
+
const ii = i * n + di;
|
|
51
|
+
if (ii < 0 || ii >= cols)
|
|
52
|
+
continue;
|
|
53
|
+
const w = (di === 0 ? 2 : 1) * (dj === 0 ? 2 : 1);
|
|
54
|
+
sum += src[jj * cols + ii] * w;
|
|
55
|
+
wsum += w;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
out[j * c2 + i] = n === 1 ? src[j * cols + i] * scale + offset : (sum / wsum) * scale + offset;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return new HeightField(step * n, c2, r2, out);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The surface that a grid thinned to every n-th vertex would draw, evaluated at every vertex of the full grid: at a
|
|
65
|
+
* kept vertex the value itself, between kept vertices the value of the coarse triangle there (a coarse quad is split
|
|
66
|
+
* by the same diagonal as in `heightAt`). This is the shape of a level of detail n times coarser, sampled where the
|
|
67
|
+
* finer level has its vertices; a blend between two levels needs it. `cols − 1` and `rows − 1` must be multiples of
|
|
68
|
+
* n. Works for any per-vertex value (a height, one component of a normal, one colour channel).
|
|
69
|
+
*
|
|
70
|
+
* On a line of the coarse grid the value depends only on the two kept vertices of that line, in one fixed formula,
|
|
71
|
+
* so two grids that share such a line (neighbouring cells) get identical numbers along it.
|
|
72
|
+
*/
|
|
73
|
+
static coarseSurface(cols, rows, src, n) {
|
|
74
|
+
if (n < 1 || (cols - 1) % n !== 0 || (rows - 1) % n !== 0)
|
|
75
|
+
throw new Error(`coarseSurface: a ${cols} × ${rows} grid cannot be thinned by ${n}`);
|
|
76
|
+
const out = new Float64Array(cols * rows);
|
|
77
|
+
const lastI = Math.max(0, cols - 1 - n);
|
|
78
|
+
const lastJ = Math.max(0, rows - 1 - n);
|
|
79
|
+
for (let j = 0; j < rows; j++) {
|
|
80
|
+
const j0 = Math.min(j - (j % n), lastJ);
|
|
81
|
+
const fv = (j - j0) / n;
|
|
82
|
+
const onJ = j % n === 0;
|
|
83
|
+
for (let i = 0; i < cols; i++) {
|
|
84
|
+
const i0 = Math.min(i - (i % n), lastI);
|
|
85
|
+
const fu = (i - i0) / n;
|
|
86
|
+
const onI = i % n === 0;
|
|
87
|
+
let v;
|
|
88
|
+
if (onI && onJ)
|
|
89
|
+
v = src[j * cols + i];
|
|
90
|
+
else if (onI) {
|
|
91
|
+
const p = src[j0 * cols + i];
|
|
92
|
+
v = p + (src[(j0 + n) * cols + i] - p) * fv;
|
|
93
|
+
}
|
|
94
|
+
else if (onJ) {
|
|
95
|
+
const p = src[j * cols + i0];
|
|
96
|
+
v = p + (src[j * cols + i0 + n] - p) * fu;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
const a = src[j0 * cols + i0];
|
|
100
|
+
const b = src[j0 * cols + i0 + n];
|
|
101
|
+
const c = src[(j0 + n) * cols + i0];
|
|
102
|
+
const d = src[(j0 + n) * cols + i0 + n];
|
|
103
|
+
v = fu + fv <= 1 ? a + (b - a) * fu + (c - a) * fv : d + (c - d) * (1 - fu) + (b - d) * (1 - fv);
|
|
104
|
+
}
|
|
105
|
+
out[j * cols + i] = v;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
/** Height at a point. Computed per triangle exactly as the mesh draws them, so objects sit precisely on the surface. */
|
|
111
|
+
heightAt(x, z) {
|
|
112
|
+
const gx = Math.min(Math.max(x / this.step, 0), this.cols - 1.0001);
|
|
113
|
+
const gz = Math.min(Math.max(z / this.step, 0), this.rows - 1.0001);
|
|
114
|
+
const i = Math.floor(gx);
|
|
115
|
+
const j = Math.floor(gz);
|
|
116
|
+
const fx = gx - i;
|
|
117
|
+
const fz = gz - j;
|
|
118
|
+
const k = j * this.cols + i;
|
|
119
|
+
const a = this.heights[k];
|
|
120
|
+
const b = this.heights[k + 1];
|
|
121
|
+
const c = this.heights[k + this.cols];
|
|
122
|
+
const d = this.heights[k + this.cols + 1];
|
|
123
|
+
// The cell is split by the b–c diagonal.
|
|
124
|
+
if (fx + fz <= 1)
|
|
125
|
+
return a + (b - a) * fx + (c - a) * fz;
|
|
126
|
+
return d + (c - d) * (1 - fx) + (b - d) * (1 - fz);
|
|
127
|
+
}
|
|
128
|
+
/** Normal of grid vertex (i, j), same as in `heightFieldMesh`: from the difference of neighbouring heights. */
|
|
129
|
+
vertexNormal(i, j) {
|
|
130
|
+
const { cols, rows, step, heights } = this;
|
|
131
|
+
const h = (ii, jj) => heights[Math.min(rows - 1, Math.max(0, jj)) * cols + Math.min(cols - 1, Math.max(0, ii))];
|
|
132
|
+
const dx = (h(i + 1, j) - h(i - 1, j)) / (2 * step);
|
|
133
|
+
const dz = (h(i, j + 1) - h(i, j - 1)) / (2 * step);
|
|
134
|
+
const l = Math.hypot(dx, 1, dz);
|
|
135
|
+
return [-dx / l, 1 / l, -dz / l];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export { hashInt, hashInts, hashString, mix32, pickWeighted, rand, randRange, streamSeed, unit } from './hash.ts';
|
|
2
|
+
export { valueNoise2 } from './noise.ts';
|
|
3
|
+
export { fractalNoise3, valueNoise3 } from './noise.ts';
|
|
4
|
+
export type { FractalNoise3 } from './noise.ts';
|
|
5
|
+
export { CUBE_FACE_AXES, cubeDirection, cubeDirectionWarped, cubeFaceCoords, cubeFaceOf, cubeGrid, cubeNeighbour, cubeUnwarp, cubeWarp, directionTangents } from './cube-sphere.ts';
|
|
6
|
+
export { SPHERE_PATCH_ATTRIBS, SPHERE_PATCH_COVER_ATTRIBS, SPHERE_PATCH_COVER_EXTRA_ATTRIBS, SPHERE_PATCH_COVER_EXTRA_LAYOUT, SPHERE_PATCH_COVER_LAYOUT, SPHERE_PATCH_EXTRA_ATTRIBS, SPHERE_PATCH_EXTRA_LAYOUT, SPHERE_PATCH_GLSL, SPHERE_PATCH_LAYOUT, spherePatchMesh, spherePatchRadius } from './sphere-patch.ts';
|
|
7
|
+
export type { SpherePatch, SpherePatchOptions } from './sphere-patch.ts';
|
|
8
|
+
export { distToSegment, insetRing, lineRing, orientedBox, pointInRings, ringSignedArea, ringsArea } from './geometry.ts';
|
|
9
|
+
export type { OrientedBox, Ring } from './geometry.ts';
|
|
10
|
+
export { HeightField } from './height-field.ts';
|
|
11
|
+
export { OutlineField, SegmentField } from './field.ts';
|
|
12
|
+
export { Occupancy } from './occupancy.ts';
|
|
13
|
+
export { FACE_FLAT, FLAT_ROOT_EDGE_M, FlatWorld, LEVEL, cellBits, cellContentBytes, cellFromBits, cellId, cellKey, compareCells, parseCellKey, sameCell, shapeHash } from './cell.ts';
|
|
14
|
+
export type { CellContent, CellId, CellInstances, CellMesh, LocalBounds, WorldPos, WorldShape } from './cell.ts';
|
|
15
|
+
export { DEFAULT_INSTANCE_TIERS, splitInstanceCells } from './instance-cells.ts';
|
|
16
|
+
export type { InstanceCell, InstanceCellOptions, InstanceTier } from './instance-cells.ts';
|
|
17
|
+
export { STRIP_STATION_FLOATS, stripCarve, stripLength, stripNearest, stripNet, stripTouches, stripUp } from './strip.ts';
|
|
18
|
+
export type { StripHit, StripInput, StripLine, StripNet, StripNetLine, StripNode, StripProfile } from './strip.ts';
|
|
19
|
+
export { flatStripSurface, sphereStripSurface } from './strip-surface.ts';
|
|
20
|
+
export type { StripSurface } from './strip-surface.ts';
|
|
21
|
+
export { NEAR_FADE_GLSL, NEAR_FLOATS, NEAR_LAYOUT, nearCount, nearKeep, nearLeaveM, nearScatter } from './near-scatter.ts';
|
|
22
|
+
export type { NearFade, NearRecords, NearScatterOptions } from './near-scatter.ts';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Layer 2, gen-core: stateless randomness with named streams, value noise, planar polygon geometry and the spatial
|
|
2
|
+
// structures generators query (height field, distance fields, occupancy grid), and the split of instance streams into
|
|
3
|
+
// thinned cells. Pure, run anywhere.
|
|
4
|
+
export { hashInt, hashInts, hashString, mix32, pickWeighted, rand, randRange, streamSeed, unit } from './hash.js';
|
|
5
|
+
export { valueNoise2 } from './noise.js';
|
|
6
|
+
export { fractalNoise3, valueNoise3 } from './noise.js';
|
|
7
|
+
export { CUBE_FACE_AXES, cubeDirection, cubeDirectionWarped, cubeFaceCoords, cubeFaceOf, cubeGrid, cubeNeighbour, cubeUnwarp, cubeWarp, directionTangents } from './cube-sphere.js';
|
|
8
|
+
export { SPHERE_PATCH_ATTRIBS, SPHERE_PATCH_COVER_ATTRIBS, SPHERE_PATCH_COVER_EXTRA_ATTRIBS, SPHERE_PATCH_COVER_EXTRA_LAYOUT, SPHERE_PATCH_COVER_LAYOUT, SPHERE_PATCH_EXTRA_ATTRIBS, SPHERE_PATCH_EXTRA_LAYOUT, SPHERE_PATCH_GLSL, SPHERE_PATCH_LAYOUT, spherePatchMesh, spherePatchRadius } from './sphere-patch.js';
|
|
9
|
+
export { distToSegment, insetRing, lineRing, orientedBox, pointInRings, ringSignedArea, ringsArea } from './geometry.js';
|
|
10
|
+
export { HeightField } from './height-field.js';
|
|
11
|
+
export { OutlineField, SegmentField } from './field.js';
|
|
12
|
+
export { Occupancy } from './occupancy.js';
|
|
13
|
+
export { FACE_FLAT, FLAT_ROOT_EDGE_M, FlatWorld, LEVEL, cellBits, cellContentBytes, cellFromBits, cellId, cellKey, compareCells, parseCellKey, sameCell, shapeHash } from './cell.js';
|
|
14
|
+
export { DEFAULT_INSTANCE_TIERS, splitInstanceCells } from './instance-cells.js';
|
|
15
|
+
export { STRIP_STATION_FLOATS, stripCarve, stripLength, stripNearest, stripNet, stripTouches, stripUp } from './strip.js';
|
|
16
|
+
export { flatStripSurface, sphereStripSurface } from './strip-surface.js';
|
|
17
|
+
export { NEAR_FADE_GLSL, NEAR_FLOATS, NEAR_LAYOUT, nearCount, nearKeep, nearLeaveM, nearScatter } from './near-scatter.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { InstanceBounds } from '../base/data.ts';
|
|
2
|
+
import type { Vec3 } from '../base/math.ts';
|
|
3
|
+
/** One thinning tier: keep one instance in `keep` (chosen by hash), scale its radius by `widen`. */
|
|
4
|
+
export interface InstanceTier {
|
|
5
|
+
keep: number;
|
|
6
|
+
widen: number;
|
|
7
|
+
}
|
|
8
|
+
/** Tier 0 is every instance unchanged; the next ones keep one in 4 and one in 12. */
|
|
9
|
+
export declare const DEFAULT_INSTANCE_TIERS: readonly InstanceTier[];
|
|
10
|
+
/** One square cell: its box (holding every tier, widened) and the instance data per tier. */
|
|
11
|
+
export interface InstanceCell {
|
|
12
|
+
/** Column and row: the cell covers x in [i·size, (i+1)·size), z likewise with j. */
|
|
13
|
+
i: number;
|
|
14
|
+
j: number;
|
|
15
|
+
min: Vec3;
|
|
16
|
+
max: Vec3;
|
|
17
|
+
/** Instance data per tier, in the stream's layout. An empty array when a tier keeps nothing in this cell. */
|
|
18
|
+
tiers: Float32Array[];
|
|
19
|
+
}
|
|
20
|
+
export interface InstanceCellOptions {
|
|
21
|
+
/** Floats per instance. */
|
|
22
|
+
stride: number;
|
|
23
|
+
/** Where position, radius and height sit inside an instance. */
|
|
24
|
+
bounds: InstanceBounds;
|
|
25
|
+
/** Cell edge in metres. */
|
|
26
|
+
cellM: number;
|
|
27
|
+
/** Thinning tiers, tier 0 first. Default `DEFAULT_INSTANCE_TIERS`. A tier keeps a subset of the tier before it when each `keep` divides the next. */
|
|
28
|
+
tiers?: readonly InstanceTier[];
|
|
29
|
+
/** Seed of the thinning choice. Default 0. */
|
|
30
|
+
seed?: number;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Sorts instances into cells by their base point (x, z), cells ordered by row then column. Within a cell and tier the
|
|
34
|
+
* instances keep their input order. An instance is kept in tier t when `hash(seed, index) % keep === 0`, so the
|
|
35
|
+
* choice depends only on its index in the input, not on the cell size.
|
|
36
|
+
*/
|
|
37
|
+
export declare function splitInstanceCells(instances: Float32Array, o: InstanceCellOptions): InstanceCell[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Splits a stream of instances into square cells on the ground plane, each with thinned copies for distance.
|
|
2
|
+
// A cell is culled as one box and picks its own level of detail; far away it draws a thinned tier: one instance in
|
|
3
|
+
// `keep`, its radius widened by `widen`, so a dense group keeps roughly its covered area with a fraction of the draws.
|
|
4
|
+
// Pure: the same instances, cell size, tiers and seed give the same bytes. Runs in workers.
|
|
5
|
+
import { hashInt } from './hash.js';
|
|
6
|
+
/** Tier 0 is every instance unchanged; the next ones keep one in 4 and one in 12. */
|
|
7
|
+
export const DEFAULT_INSTANCE_TIERS = [
|
|
8
|
+
{ keep: 1, widen: 1 },
|
|
9
|
+
{ keep: 4, widen: 1.7 },
|
|
10
|
+
{ keep: 12, widen: 2.6 },
|
|
11
|
+
];
|
|
12
|
+
/**
|
|
13
|
+
* Sorts instances into cells by their base point (x, z), cells ordered by row then column. Within a cell and tier the
|
|
14
|
+
* instances keep their input order. An instance is kept in tier t when `hash(seed, index) % keep === 0`, so the
|
|
15
|
+
* choice depends only on its index in the input, not on the cell size.
|
|
16
|
+
*/
|
|
17
|
+
export function splitInstanceCells(instances, o) {
|
|
18
|
+
const { stride, bounds, cellM } = o;
|
|
19
|
+
const tiers = o.tiers ?? DEFAULT_INSTANCE_TIERS;
|
|
20
|
+
const seed = o.seed ?? 0;
|
|
21
|
+
const P = bounds.position;
|
|
22
|
+
const R = bounds.radius;
|
|
23
|
+
const H = bounds.height;
|
|
24
|
+
const n = Math.floor(instances.length / stride);
|
|
25
|
+
const widest = tiers.reduce((w, t) => Math.max(w, t.widen), 1);
|
|
26
|
+
// Cell of each instance, and the instances of each cell in input order.
|
|
27
|
+
const byCell = new Map();
|
|
28
|
+
for (let k = 0; k < n; k++) {
|
|
29
|
+
const i = Math.floor(instances[k * stride + P] / cellM);
|
|
30
|
+
const j = Math.floor(instances[k * stride + P + 2] / cellM);
|
|
31
|
+
const key = `${i},${j}`;
|
|
32
|
+
let c = byCell.get(key);
|
|
33
|
+
if (!c)
|
|
34
|
+
byCell.set(key, (c = { i, j, members: [] }));
|
|
35
|
+
c.members.push(k);
|
|
36
|
+
}
|
|
37
|
+
const cells = [...byCell.values()].sort((a, b) => a.j - b.j || a.i - b.i);
|
|
38
|
+
return cells.map(({ i, j, members }) => {
|
|
39
|
+
const min = [Infinity, Infinity, Infinity];
|
|
40
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
41
|
+
const lists = tiers.map(() => []);
|
|
42
|
+
for (const k of members) {
|
|
43
|
+
const at = k * stride;
|
|
44
|
+
const x = instances[at + P];
|
|
45
|
+
const y = instances[at + P + 1];
|
|
46
|
+
const z = instances[at + P + 2];
|
|
47
|
+
const r = instances[at + R] * widest;
|
|
48
|
+
min[0] = Math.min(min[0], x - r);
|
|
49
|
+
min[1] = Math.min(min[1], y);
|
|
50
|
+
min[2] = Math.min(min[2], z - r);
|
|
51
|
+
max[0] = Math.max(max[0], x + r);
|
|
52
|
+
max[1] = Math.max(max[1], y + instances[at + H]);
|
|
53
|
+
max[2] = Math.max(max[2], z + r);
|
|
54
|
+
const h = hashInt(seed, k);
|
|
55
|
+
tiers.forEach((t, ti) => {
|
|
56
|
+
if (h % t.keep === 0)
|
|
57
|
+
lists[ti].push(k);
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const data = lists.map((list, ti) => {
|
|
61
|
+
const out = new Float32Array(list.length * stride);
|
|
62
|
+
const widen = tiers[ti].widen;
|
|
63
|
+
list.forEach((k, m) => {
|
|
64
|
+
out.set(instances.subarray(k * stride, (k + 1) * stride), m * stride);
|
|
65
|
+
if (widen !== 1)
|
|
66
|
+
out[m * stride + R] = out[m * stride + R] * widen;
|
|
67
|
+
});
|
|
68
|
+
return out;
|
|
69
|
+
});
|
|
70
|
+
return { i, j, min, max, tiers: data };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { VertexLayout } from '../base/data.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Floats of one record: x y z relative to the anchor of its set (m), order 0–1 | width m, height m, turn about up
|
|
4
|
+
* (rad), bend 0–1 | r g b, variant.
|
|
5
|
+
*/
|
|
6
|
+
export declare const NEAR_FLOATS = 12;
|
|
7
|
+
/** Instance layout of a record: `i_base` (xyz, order), `i_shape` (width, height, turn, bend), `i_color` (rgb, variant). */
|
|
8
|
+
export declare const NEAR_LAYOUT: VertexLayout;
|
|
9
|
+
export interface NearScatterOptions {
|
|
10
|
+
seed: number;
|
|
11
|
+
/** Integers naming the square (for example space, column, row); part of every hash, their order matters. */
|
|
12
|
+
key: readonly number[];
|
|
13
|
+
/** Per kind: places tried, rounded up to a square number g², one in each cell of a g × g grid over the square. */
|
|
14
|
+
candidates: readonly number[];
|
|
15
|
+
/** Bit k: make kind k. Default all. */
|
|
16
|
+
mask?: number;
|
|
17
|
+
/** Share of the candidates kept at (u, v) ∈ [0, 1)² of the square, 0–1. */
|
|
18
|
+
densityAt(kind: number, u: number, v: number): number;
|
|
19
|
+
/**
|
|
20
|
+
* Only for a kept candidate: writes its record at `out[at …]` except the order slot (`at + 3`), which the tool owns.
|
|
21
|
+
* `roll(q)` is the candidate's own hash number 0–1 for any integer `q`.
|
|
22
|
+
*/
|
|
23
|
+
fill(kind: number, u: number, v: number, roll: (q: number) => number, out: Float32Array, at: number): void;
|
|
24
|
+
}
|
|
25
|
+
/** Records of one square: `start` has kinds + 1 entries, in records; a kind outside the mask is empty. */
|
|
26
|
+
export interface NearRecords {
|
|
27
|
+
data: Float32Array;
|
|
28
|
+
start: Uint32Array;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The records of one square. Candidate `c` of kind `k` lies in grid cell `(c % g, floor(c / g))`, jittered by
|
|
32
|
+
* `unit(hashInts(seed, ...key, k, c, 1 | 2))`; it is kept when `unit(hashInts(seed, ...key, k, c, 3)) < densityAt`;
|
|
33
|
+
* its order is `unit(hashInts(seed, ...key, k, c, 4))`, and `roll(q)` is `unit(hashInts(seed, ...key, k, c, 16 + q))`.
|
|
34
|
+
* Within a kind the records are sorted by order, highest first, ties by candidate ascending.
|
|
35
|
+
*/
|
|
36
|
+
export declare function nearScatter(o: NearScatterOptions): NearRecords;
|
|
37
|
+
/**
|
|
38
|
+
* Fade of a kind by distance: nothing beyond `reachM`; the instances leave one by one over the outer `band` share of
|
|
39
|
+
* the reach (lowest order first), each shrinking to nothing over `softM`.
|
|
40
|
+
*/
|
|
41
|
+
export interface NearFade {
|
|
42
|
+
reachM: number;
|
|
43
|
+
band: number;
|
|
44
|
+
softM: number;
|
|
45
|
+
}
|
|
46
|
+
/** Distance at which an instance of this order has left: `gain · reachM · (1 − band · (1 − order))`. */
|
|
47
|
+
export declare function nearLeaveM(f: NearFade, gain: number, order: number): number;
|
|
48
|
+
/** Scale 0–1 of an instance of this order at `distM`: `smoothstep(0, 1, (leave − dist) / softM)`. */
|
|
49
|
+
export declare function nearKeep(f: NearFade, gain: number, order: number, distM: number): number;
|
|
50
|
+
/**
|
|
51
|
+
* How many leading records of `[from, to)` (indices of records in `data`, sorted by order, highest first) can have a
|
|
52
|
+
* scale above 0 at `distM` or farther: a binary search over the order.
|
|
53
|
+
*/
|
|
54
|
+
export declare function nearCount(data: Float32Array, from: number, to: number, f: NearFade, gain: number, distM: number): number;
|
|
55
|
+
/** GLSL of the fade, `float nearKeep(vec3 fade, float order, float dist)` with `fade` = (gain · reach, band, soft). Any stage. */
|
|
56
|
+
export declare const NEAR_FADE_GLSL = "\nfloat nearKeep(vec3 fade, float order, float dist) {\n float leave = fade.x * (1.0 - fade.y * (1.0 - order));\n return smoothstep(0.0, 1.0, (leave - dist) / fade.z);\n}\n";
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// Small instances near the viewer: records made per square of a fixed fine grid, and the fade that lets them leave one
|
|
2
|
+
// by one with distance.
|
|
3
|
+
//
|
|
4
|
+
// A square is named by integers (its `key`); every number of a record is a hash of the seed, that key, the kind and
|
|
5
|
+
// the candidate's index, so a square gives the same bytes whatever else is made, in any order and on any thread.
|
|
6
|
+
// Candidates of a kind sit one in each cell of a g × g grid over the square, jittered inside their cell; a density
|
|
7
|
+
// callback keeps a share of them, so a higher density only adds instances. Each record carries an order 0–1: within
|
|
8
|
+
// a kind the records are sorted by it, highest first, and the fade by distance removes the lowest first. What can
|
|
9
|
+
// still be seen from a distance is then a prefix of the records (`nearCount`), and a drawing layer copies only that.
|
|
10
|
+
//
|
|
11
|
+
// Pure: arithmetic, Math.sqrt / floor / ceil / min / max and the hashes. The fade exists here and, with the same
|
|
12
|
+
// formula, as GLSL (`NEAR_FADE_GLSL`).
|
|
13
|
+
import { hashInt, hashInts, unit } from './hash.js';
|
|
14
|
+
/**
|
|
15
|
+
* Floats of one record: x y z relative to the anchor of its set (m), order 0–1 | width m, height m, turn about up
|
|
16
|
+
* (rad), bend 0–1 | r g b, variant.
|
|
17
|
+
*/
|
|
18
|
+
export const NEAR_FLOATS = 12;
|
|
19
|
+
/** Instance layout of a record: `i_base` (xyz, order), `i_shape` (width, height, turn, bend), `i_color` (rgb, variant). */
|
|
20
|
+
export const NEAR_LAYOUT = {
|
|
21
|
+
stride: 48,
|
|
22
|
+
attribs: [
|
|
23
|
+
{ name: 'i_base', size: 4, type: 'f32', offset: 0 },
|
|
24
|
+
{ name: 'i_shape', size: 4, type: 'f32', offset: 16 },
|
|
25
|
+
{ name: 'i_color', size: 4, type: 'f32', offset: 32 },
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The records of one square. Candidate `c` of kind `k` lies in grid cell `(c % g, floor(c / g))`, jittered by
|
|
30
|
+
* `unit(hashInts(seed, ...key, k, c, 1 | 2))`; it is kept when `unit(hashInts(seed, ...key, k, c, 3)) < densityAt`;
|
|
31
|
+
* its order is `unit(hashInts(seed, ...key, k, c, 4))`, and `roll(q)` is `unit(hashInts(seed, ...key, k, c, 16 + q))`.
|
|
32
|
+
* Within a kind the records are sorted by order, highest first, ties by candidate ascending.
|
|
33
|
+
*/
|
|
34
|
+
export function nearScatter(o) {
|
|
35
|
+
const kinds = o.candidates.length;
|
|
36
|
+
const mask = o.mask ?? -1;
|
|
37
|
+
const keyed = hashInts(o.seed, ...o.key);
|
|
38
|
+
const parts = [];
|
|
39
|
+
let total = 0;
|
|
40
|
+
let h = 0;
|
|
41
|
+
const roll = (q) => unit(hashInt(h, 16 + q));
|
|
42
|
+
for (let k = 0; k < kinds; k++) {
|
|
43
|
+
if (((mask >>> k) & 1) === 0) {
|
|
44
|
+
parts.push({ data: new Float32Array(0), count: 0 });
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const g = Math.ceil(Math.sqrt(Math.max(0, o.candidates[k])));
|
|
48
|
+
const n = g * g;
|
|
49
|
+
const hk = hashInt(keyed, k);
|
|
50
|
+
const kept = [];
|
|
51
|
+
const orders = [];
|
|
52
|
+
const tmp = new Float32Array(n * NEAR_FLOATS);
|
|
53
|
+
let count = 0;
|
|
54
|
+
for (let c = 0; c < n; c++) {
|
|
55
|
+
const hc = hashInt(hk, c);
|
|
56
|
+
const u = ((c % g) + unit(hashInt(hc, 1))) / g;
|
|
57
|
+
const v = (Math.floor(c / g) + unit(hashInt(hc, 2))) / g;
|
|
58
|
+
if (!(unit(hashInt(hc, 3)) < o.densityAt(k, u, v)))
|
|
59
|
+
continue;
|
|
60
|
+
h = hc;
|
|
61
|
+
const at = count * NEAR_FLOATS;
|
|
62
|
+
o.fill(k, u, v, roll, tmp, at);
|
|
63
|
+
const order = unit(hashInt(hc, 4));
|
|
64
|
+
tmp[at + 3] = order;
|
|
65
|
+
kept.push(c);
|
|
66
|
+
orders.push(order);
|
|
67
|
+
count++;
|
|
68
|
+
}
|
|
69
|
+
// Highest order first, ties by candidate ascending (the candidates are already ascending).
|
|
70
|
+
const idx = Array.from({ length: count }, (_, i) => i);
|
|
71
|
+
idx.sort((a, b) => orders[b] - orders[a] || kept[a] - kept[b]);
|
|
72
|
+
const data = new Float32Array(count * NEAR_FLOATS);
|
|
73
|
+
for (let i = 0; i < count; i++)
|
|
74
|
+
data.set(tmp.subarray(idx[i] * NEAR_FLOATS, (idx[i] + 1) * NEAR_FLOATS), i * NEAR_FLOATS);
|
|
75
|
+
parts.push({ data, count });
|
|
76
|
+
total += count;
|
|
77
|
+
}
|
|
78
|
+
const data = new Float32Array(total * NEAR_FLOATS);
|
|
79
|
+
const start = new Uint32Array(kinds + 1);
|
|
80
|
+
let at = 0;
|
|
81
|
+
for (let k = 0; k < kinds; k++) {
|
|
82
|
+
start[k] = at;
|
|
83
|
+
data.set(parts[k].data, at * NEAR_FLOATS);
|
|
84
|
+
at += parts[k].count;
|
|
85
|
+
}
|
|
86
|
+
start[kinds] = at;
|
|
87
|
+
return { data, start };
|
|
88
|
+
}
|
|
89
|
+
/** Distance at which an instance of this order has left: `gain · reachM · (1 − band · (1 − order))`. */
|
|
90
|
+
export function nearLeaveM(f, gain, order) {
|
|
91
|
+
return gain * f.reachM * (1 - f.band * (1 - order));
|
|
92
|
+
}
|
|
93
|
+
/** Scale 0–1 of an instance of this order at `distM`: `smoothstep(0, 1, (leave − dist) / softM)`. */
|
|
94
|
+
export function nearKeep(f, gain, order, distM) {
|
|
95
|
+
const t = Math.min(1, Math.max(0, (nearLeaveM(f, gain, order) - distM) / f.softM));
|
|
96
|
+
return t * t * (3 - 2 * t);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* How many leading records of `[from, to)` (indices of records in `data`, sorted by order, highest first) can have a
|
|
100
|
+
* scale above 0 at `distM` or farther: a binary search over the order.
|
|
101
|
+
*/
|
|
102
|
+
export function nearCount(data, from, to, f, gain, distM) {
|
|
103
|
+
let lo = from;
|
|
104
|
+
let hi = to;
|
|
105
|
+
while (lo < hi) {
|
|
106
|
+
const mid = (lo + hi) >>> 1;
|
|
107
|
+
if (nearLeaveM(f, gain, data[mid * NEAR_FLOATS + 3]) > distM)
|
|
108
|
+
lo = mid + 1;
|
|
109
|
+
else
|
|
110
|
+
hi = mid;
|
|
111
|
+
}
|
|
112
|
+
return lo - from;
|
|
113
|
+
}
|
|
114
|
+
/** GLSL of the fade, `float nearKeep(vec3 fade, float order, float dist)` with `fade` = (gain · reach, band, soft). Any stage. */
|
|
115
|
+
export const NEAR_FADE_GLSL = /* glsl */ `
|
|
116
|
+
float nearKeep(vec3 fade, float order, float dist) {
|
|
117
|
+
float leave = fade.x * (1.0 - fade.y * (1.0 - order));
|
|
118
|
+
return smoothstep(0.0, 1.0, (leave - dist) / fade.z);
|
|
119
|
+
}
|
|
120
|
+
`;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Noise 0–1 at a point. `cellM` is the blob size in metres. */
|
|
2
|
+
export declare function valueNoise2(x: number, z: number, cellM: number, seed: number): number;
|
|
3
|
+
/**
|
|
4
|
+
* Noise 0–1 at a point in space. `cellM` is the blob size in metres. Sampled at points of a curved surface it has no
|
|
5
|
+
* seam and no pole: the value depends only on the point, not on how the surface is laid out. Built from the integer
|
|
6
|
+
* hash, `Math.floor` and arithmetic, so the same input gives the same bits everywhere.
|
|
7
|
+
*/
|
|
8
|
+
export declare function valueNoise3(x: number, y: number, z: number, cellM: number, seed: number): number;
|
|
9
|
+
export interface FractalNoise3 {
|
|
10
|
+
/** Blob size of the first octave in metres. */
|
|
11
|
+
cellM: number;
|
|
12
|
+
/** Number of octaves; each is `lacunarity` times finer and `gain` times weaker than the one before. */
|
|
13
|
+
octaves: number;
|
|
14
|
+
/** Default 2. */
|
|
15
|
+
lacunarity?: number;
|
|
16
|
+
/** Default 0.5. */
|
|
17
|
+
gain?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Sum of octaves of `valueNoise3`, 0–1 (the weights are normalised). Every octave has its own seed and an offset, so
|
|
21
|
+
* the lattices do not line up.
|
|
22
|
+
*/
|
|
23
|
+
export declare function fractalNoise3(x: number, y: number, z: number, o: FractalNoise3, seed: number): number;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Smooth value noise on the CPU, from the same integer hash as the rest of the generators.
|
|
2
|
+
// Used for properties that should change smoothly across groups (hue, variant), not item by item.
|
|
3
|
+
import { hashInt, hashInts, unit } from './hash.js';
|
|
4
|
+
/** Noise 0–1 at a point. `cellM` is the blob size in metres. */
|
|
5
|
+
export function valueNoise2(x, z, cellM, seed) {
|
|
6
|
+
const gx = x / cellM;
|
|
7
|
+
const gz = z / cellM;
|
|
8
|
+
const i = Math.floor(gx);
|
|
9
|
+
const j = Math.floor(gz);
|
|
10
|
+
let tx = gx - i;
|
|
11
|
+
let tz = gz - j;
|
|
12
|
+
tx = tx * tx * (3 - 2 * tx);
|
|
13
|
+
tz = tz * tz * (3 - 2 * tz);
|
|
14
|
+
const a = unit(hashInts(seed, i, j));
|
|
15
|
+
const b = unit(hashInts(seed, i + 1, j));
|
|
16
|
+
const c = unit(hashInts(seed, i, j + 1));
|
|
17
|
+
const d = unit(hashInts(seed, i + 1, j + 1));
|
|
18
|
+
return a + (b - a) * tx + (c - a) * tz + (a - b - c + d) * tx * tz;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Noise 0–1 at a point in space. `cellM` is the blob size in metres. Sampled at points of a curved surface it has no
|
|
22
|
+
* seam and no pole: the value depends only on the point, not on how the surface is laid out. Built from the integer
|
|
23
|
+
* hash, `Math.floor` and arithmetic, so the same input gives the same bits everywhere.
|
|
24
|
+
*/
|
|
25
|
+
export function valueNoise3(x, y, z, cellM, seed) {
|
|
26
|
+
const gx = x / cellM;
|
|
27
|
+
const gy = y / cellM;
|
|
28
|
+
const gz = z / cellM;
|
|
29
|
+
const i = Math.floor(gx);
|
|
30
|
+
const j = Math.floor(gy);
|
|
31
|
+
const k = Math.floor(gz);
|
|
32
|
+
let tx = gx - i;
|
|
33
|
+
let ty = gy - j;
|
|
34
|
+
let tz = gz - k;
|
|
35
|
+
tx = tx * tx * (3 - 2 * tx);
|
|
36
|
+
ty = ty * ty * (3 - 2 * ty);
|
|
37
|
+
tz = tz * tz * (3 - 2 * tz);
|
|
38
|
+
const a = unit(hashInts(seed, i, j, k));
|
|
39
|
+
const b = unit(hashInts(seed, i + 1, j, k));
|
|
40
|
+
const c = unit(hashInts(seed, i, j + 1, k));
|
|
41
|
+
const d = unit(hashInts(seed, i + 1, j + 1, k));
|
|
42
|
+
const e = unit(hashInts(seed, i, j, k + 1));
|
|
43
|
+
const f = unit(hashInts(seed, i + 1, j, k + 1));
|
|
44
|
+
const g = unit(hashInts(seed, i, j + 1, k + 1));
|
|
45
|
+
const h = unit(hashInts(seed, i + 1, j + 1, k + 1));
|
|
46
|
+
const ab = a + (b - a) * tx;
|
|
47
|
+
const cd = c + (d - c) * tx;
|
|
48
|
+
const ef = e + (f - e) * tx;
|
|
49
|
+
const gh = g + (h - g) * tx;
|
|
50
|
+
const lo = ab + (cd - ab) * ty;
|
|
51
|
+
const hi = ef + (gh - ef) * ty;
|
|
52
|
+
return lo + (hi - lo) * tz;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Sum of octaves of `valueNoise3`, 0–1 (the weights are normalised). Every octave has its own seed and an offset, so
|
|
56
|
+
* the lattices do not line up.
|
|
57
|
+
*/
|
|
58
|
+
export function fractalNoise3(x, y, z, o, seed) {
|
|
59
|
+
const lacunarity = o.lacunarity ?? 2;
|
|
60
|
+
const gain = o.gain ?? 0.5;
|
|
61
|
+
let cell = o.cellM;
|
|
62
|
+
let weight = 1;
|
|
63
|
+
let sum = 0;
|
|
64
|
+
let total = 0;
|
|
65
|
+
for (let k = 0; k < o.octaves; k++) {
|
|
66
|
+
sum += weight * valueNoise3(x + k * 131.7, y + k * 71.3, z + k * 197.1, cell, hashInt(seed, k));
|
|
67
|
+
total += weight;
|
|
68
|
+
cell /= lacunarity;
|
|
69
|
+
weight *= gain;
|
|
70
|
+
}
|
|
71
|
+
return total > 0 ? sum / total : 0;
|
|
72
|
+
}
|