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,159 @@
|
|
|
1
|
+
// A cube blown up into a sphere: the geometry behind a sphere made of six quadtrees. Every face of the cube carries a
|
|
2
|
+
// square grid; a grid point is pushed out along its direction from the centre until it lies on the sphere. Before
|
|
3
|
+
// that the grid is warped by the tangent of an angle, so grid lines are equal angles apart and the cells of one level
|
|
4
|
+
// differ in edge length by at most about a third (a plain cube grid gives a factor of three).
|
|
5
|
+
//
|
|
6
|
+
// Faces: 0 +x, 1 −x, 2 +y, 3 −y, 4 +z, 5 −z. Each has two grid axes u and v with u × v = the outward axis, so a cell's
|
|
7
|
+
// `x` index runs along u and its `y` index along v, and a triangle (u, v) winds anticlockwise seen from outside.
|
|
8
|
+
//
|
|
9
|
+
// Everything is a pure function built from arithmetic, `Math.sqrt` and the reproducible trigonometry of base, and a
|
|
10
|
+
// direction is always normalised from its world components in the order x, y, z. A point on an edge shared by two
|
|
11
|
+
// faces therefore gets the same bits from either face: content sampled at it has no seam.
|
|
12
|
+
import { detAtan, detSinCos } from '../base/trig.js';
|
|
13
|
+
/** Grid axes `u`, `v` and the outward axis `n` of each face of the cube. */
|
|
14
|
+
export const CUBE_FACE_AXES = [
|
|
15
|
+
{ u: [0, 1, 0], v: [0, 0, 1], n: [1, 0, 0] },
|
|
16
|
+
{ u: [0, 0, 1], v: [0, 1, 0], n: [-1, 0, 0] },
|
|
17
|
+
{ u: [0, 0, 1], v: [1, 0, 0], n: [0, 1, 0] },
|
|
18
|
+
{ u: [1, 0, 0], v: [0, 0, 1], n: [0, -1, 0] },
|
|
19
|
+
{ u: [1, 0, 0], v: [0, 1, 0], n: [0, 0, 1] },
|
|
20
|
+
{ u: [0, 1, 0], v: [1, 0, 0], n: [0, 0, -1] },
|
|
21
|
+
];
|
|
22
|
+
const QUARTER_PI = 0.7853981633974483;
|
|
23
|
+
const scratch = [0, 0];
|
|
24
|
+
/**
|
|
25
|
+
* Face coordinate (−1 … 1, equal angles) of grid line `g` out of `count` intervals across the face. Line `count − g`
|
|
26
|
+
* gives exactly the negated value, and a line of a finer grid that coincides with this one gives the same bits.
|
|
27
|
+
*/
|
|
28
|
+
export function cubeGrid(g, count) {
|
|
29
|
+
return (2 * g - count) / count;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The warp: face coordinate (−1 … 1, proportional to the angle from the face's middle) → position on the face of the
|
|
33
|
+
* cube (−1 … 1), `tan(s · π/4)`. Odd, and exactly ±1 at ±1. Defined a little beyond the face as well.
|
|
34
|
+
*/
|
|
35
|
+
export function cubeWarp(s) {
|
|
36
|
+
const a = Math.abs(s);
|
|
37
|
+
if (a === 1 || a === 0)
|
|
38
|
+
return s;
|
|
39
|
+
const [sin, cos] = detSinCos(a * QUARTER_PI, scratch);
|
|
40
|
+
let t = sin / cos;
|
|
41
|
+
if (a < 1 && t > 1)
|
|
42
|
+
t = 1;
|
|
43
|
+
return s < 0 ? -t : t;
|
|
44
|
+
}
|
|
45
|
+
/** Inverse of `cubeWarp`. */
|
|
46
|
+
export function cubeUnwarp(t) {
|
|
47
|
+
return detAtan(t) / QUARTER_PI;
|
|
48
|
+
}
|
|
49
|
+
/** Unit direction from the centre to the point of `face` at warped coordinates (`cubeWarp` of the face coordinates). */
|
|
50
|
+
export function cubeDirectionWarped(face, wu, wv, out = [0, 0, 0]) {
|
|
51
|
+
const a = CUBE_FACE_AXES[face];
|
|
52
|
+
// Per component exactly one term is not zero, so the sums are exact and the same from every face.
|
|
53
|
+
const x = a.n[0] + a.u[0] * wu + a.v[0] * wv;
|
|
54
|
+
const y = a.n[1] + a.u[1] * wu + a.v[1] * wv;
|
|
55
|
+
const z = a.n[2] + a.u[2] * wu + a.v[2] * wv;
|
|
56
|
+
const l = Math.sqrt(x * x + y * y + z * z);
|
|
57
|
+
out[0] = x / l;
|
|
58
|
+
out[1] = y / l;
|
|
59
|
+
out[2] = z / l;
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
/** Unit direction from the centre to the point of `face` at face coordinates `s`, `t` (−1 … 1). */
|
|
63
|
+
export function cubeDirection(face, s, t, out = [0, 0, 0]) {
|
|
64
|
+
return cubeDirectionWarped(face, cubeWarp(s), cubeWarp(t), out);
|
|
65
|
+
}
|
|
66
|
+
/** The face a direction points at (the first of x, y, z among equals) and its face coordinates there (−1 … 1). */
|
|
67
|
+
export function cubeFaceOf(dir) {
|
|
68
|
+
const ax = Math.abs(dir[0]);
|
|
69
|
+
const ay = Math.abs(dir[1]);
|
|
70
|
+
const az = Math.abs(dir[2]);
|
|
71
|
+
const face = ax >= ay && ax >= az ? (dir[0] >= 0 ? 0 : 1) : ay >= az ? (dir[1] >= 0 ? 2 : 3) : dir[2] >= 0 ? 4 : 5;
|
|
72
|
+
const [s, t] = cubeFaceCoords(face, dir);
|
|
73
|
+
return { face, s: Math.max(-1, Math.min(1, s)), t: Math.max(-1, Math.min(1, t)) };
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Face coordinates of a direction on a given face, not clamped: beyond ±1 the direction points past the face's edge.
|
|
77
|
+
* Only directions on the face's side of the centre have coordinates; for the others the result is [NaN, NaN].
|
|
78
|
+
*/
|
|
79
|
+
export function cubeFaceCoords(face, dir) {
|
|
80
|
+
const a = CUBE_FACE_AXES[face];
|
|
81
|
+
const along = dir[0] * a.n[0] + dir[1] * a.n[1] + dir[2] * a.n[2];
|
|
82
|
+
if (!(along > 0))
|
|
83
|
+
return [NaN, NaN];
|
|
84
|
+
const pu = (dir[0] * a.u[0] + dir[1] * a.u[1] + dir[2] * a.u[2]) / along;
|
|
85
|
+
const pv = (dir[0] * a.v[0] + dir[1] * a.v[1] + dir[2] * a.v[2]) / along;
|
|
86
|
+
return [cubeUnwarp(pu), cubeUnwarp(pv)];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* The cell `dx`, `dy` cells away from cell (`x`, `y`) of `face` at `level`, across an edge of the face if need be.
|
|
90
|
+
* Null when the step leaves the face over a corner of the cube: three faces meet there and no cell lies diagonally.
|
|
91
|
+
*
|
|
92
|
+
* Exact integer geometry: the cell's middle is taken on the cube in half-cell units, the part that sticks out past
|
|
93
|
+
* the face's edge is folded round the edge onto the next face, and that face's indices are read back.
|
|
94
|
+
*/
|
|
95
|
+
export function cubeNeighbour(face, level, x, y, dx, dy) {
|
|
96
|
+
const n = 2 ** level;
|
|
97
|
+
const nx = x + dx;
|
|
98
|
+
const ny = y + dy;
|
|
99
|
+
const outX = nx < 0 || nx >= n;
|
|
100
|
+
const outY = ny < 0 || ny >= n;
|
|
101
|
+
if (!outX && !outY)
|
|
102
|
+
return { face, x: nx, y: ny };
|
|
103
|
+
if (outX && outY)
|
|
104
|
+
return null;
|
|
105
|
+
// Half-cell units: the face spans −n … n along u and v and lies at n along its outward axis.
|
|
106
|
+
let cu = 2 * nx + 1 - n;
|
|
107
|
+
let cv = 2 * ny + 1 - n;
|
|
108
|
+
let cn;
|
|
109
|
+
if (outX) {
|
|
110
|
+
cn = 2 * n - Math.abs(cu);
|
|
111
|
+
cu = cu < 0 ? -n : n;
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
cn = 2 * n - Math.abs(cv);
|
|
115
|
+
cv = cv < 0 ? -n : n;
|
|
116
|
+
}
|
|
117
|
+
// Farther than one whole face away: not a neighbour in any sense.
|
|
118
|
+
if (cn <= -n)
|
|
119
|
+
return null;
|
|
120
|
+
const a = CUBE_FACE_AXES[face];
|
|
121
|
+
const px = a.n[0] * cn + a.u[0] * cu + a.v[0] * cv;
|
|
122
|
+
const py = a.n[1] * cn + a.u[1] * cu + a.v[1] * cv;
|
|
123
|
+
const pz = a.n[2] * cn + a.u[2] * cu + a.v[2] * cv;
|
|
124
|
+
for (let f = 0; f < 6; f++) {
|
|
125
|
+
const b = CUBE_FACE_AXES[f];
|
|
126
|
+
if (f === face || px * b.n[0] + py * b.n[1] + pz * b.n[2] !== n)
|
|
127
|
+
continue;
|
|
128
|
+
return {
|
|
129
|
+
face: f,
|
|
130
|
+
x: (px * b.u[0] + py * b.u[1] + pz * b.u[2] + n - 1) / 2,
|
|
131
|
+
y: (px * b.v[0] + py * b.v[1] + pz * b.v[2] + n - 1) / 2,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Two unit directions across `dir` that depend on `dir` alone (not on a face or a grid): `t1` is square to `dir` and
|
|
138
|
+
* to the world axis `dir` leans on least, `t2 = dir × t1`, so `t1 × t2 = dir`. Differences taken along them give the
|
|
139
|
+
* same result wherever the point is reached from.
|
|
140
|
+
*/
|
|
141
|
+
export function directionTangents(dir) {
|
|
142
|
+
const ax = Math.abs(dir[0]);
|
|
143
|
+
const ay = Math.abs(dir[1]);
|
|
144
|
+
const az = Math.abs(dir[2]);
|
|
145
|
+
let t1;
|
|
146
|
+
// axis × dir for the axis with the smallest component.
|
|
147
|
+
if (ax <= ay && ax <= az)
|
|
148
|
+
t1 = [0, -dir[2], dir[1]];
|
|
149
|
+
else if (ay <= az)
|
|
150
|
+
t1 = [dir[2], 0, -dir[0]];
|
|
151
|
+
else
|
|
152
|
+
t1 = [-dir[1], dir[0], 0];
|
|
153
|
+
const l = Math.sqrt(t1[0] * t1[0] + t1[1] * t1[1] + t1[2] * t1[2]);
|
|
154
|
+
t1[0] /= l;
|
|
155
|
+
t1[1] /= l;
|
|
156
|
+
t1[2] /= l;
|
|
157
|
+
const t2 = [dir[1] * t1[2] - dir[2] * t1[1], dir[2] * t1[0] - dir[0] * t1[2], dir[0] * t1[1] - dir[1] * t1[0]];
|
|
158
|
+
return { t1, t2 };
|
|
159
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Ring } from './geometry.ts';
|
|
2
|
+
/** A set of segments with a distance query. Each segment may have a width (half the width is subtracted from the distance). */
|
|
3
|
+
export declare class SegmentField {
|
|
4
|
+
readonly sizeX: number;
|
|
5
|
+
readonly sizeZ: number;
|
|
6
|
+
/** Edge of a square bin in metres. */
|
|
7
|
+
readonly bucketM: number;
|
|
8
|
+
private readonly cols;
|
|
9
|
+
private readonly rows;
|
|
10
|
+
private readonly buckets;
|
|
11
|
+
/** Five numbers per segment: ax, az, bx, bz, half width. */
|
|
12
|
+
private readonly segs;
|
|
13
|
+
/** Number of the line (polygon, path) the segment belongs to. */
|
|
14
|
+
private readonly owner;
|
|
15
|
+
constructor(sizeX: number, sizeZ: number, bucketM?: number);
|
|
16
|
+
get count(): number;
|
|
17
|
+
private col;
|
|
18
|
+
private row;
|
|
19
|
+
add(ax: number, az: number, bx: number, bz: number, halfWidth?: number, owner?: number): void;
|
|
20
|
+
/** Polyline as a flat array x, z. */
|
|
21
|
+
addPolyline(points: ArrayLike<number>, halfWidth?: number, owner?: number, closed?: boolean): void;
|
|
22
|
+
/**
|
|
23
|
+
* Distance to the nearest segment reduced by its half width, at most `maxDistance`.
|
|
24
|
+
* `skipOwner` omits the segments of one line.
|
|
25
|
+
*/
|
|
26
|
+
distance(x: number, z: number, maxDistance: number, skipOwner?: number): number;
|
|
27
|
+
}
|
|
28
|
+
/** Signed distance field to the outlines of a set of polygons (each a ring set with holes). */
|
|
29
|
+
export declare class OutlineField {
|
|
30
|
+
readonly sizeX: number;
|
|
31
|
+
readonly sizeZ: number;
|
|
32
|
+
private readonly segments;
|
|
33
|
+
private readonly polygons;
|
|
34
|
+
private readonly bucketM;
|
|
35
|
+
private readonly cols;
|
|
36
|
+
private readonly rows;
|
|
37
|
+
/** Polygons whose bounding rectangle reaches into the bin. */
|
|
38
|
+
private readonly inBucket;
|
|
39
|
+
/** `polygons[i]` are the rings of one polygon: outer boundary and possible holes. */
|
|
40
|
+
constructor(polygons: Ring[][], sizeX: number, sizeZ: number, bucketM?: number);
|
|
41
|
+
/** Does a point lie inside some polygon? */
|
|
42
|
+
inside(x: number, z: number): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Distance from a point to the nearest outline in metres, at most `maxDistance`. Negative inside a polygon.
|
|
45
|
+
* Computed exactly to the polygon edges, not from the raster.
|
|
46
|
+
*/
|
|
47
|
+
distance(x: number, z: number, maxDistance?: number): number;
|
|
48
|
+
/** The field written out to a raster with step `step` (value at the cell centre), for textures and debugging. */
|
|
49
|
+
rasterize(step: number, maxDistance?: number): {
|
|
50
|
+
data: Float32Array;
|
|
51
|
+
cols: number;
|
|
52
|
+
rows: number;
|
|
53
|
+
step: number;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Distance fields in the x-z plane: how far the nearest polygon outline or line edge is from a given point.
|
|
2
|
+
// Segments lie in a coarse grid of bins and the query computes the exact distance to the nearest ones,
|
|
3
|
+
// so the result is not burdened by the raster resolution. Placement rules query this.
|
|
4
|
+
import { distToSegment, pointInRings } from './geometry.js';
|
|
5
|
+
/** A set of segments with a distance query. Each segment may have a width (half the width is subtracted from the distance). */
|
|
6
|
+
export class SegmentField {
|
|
7
|
+
sizeX;
|
|
8
|
+
sizeZ;
|
|
9
|
+
/** Edge of a square bin in metres. */
|
|
10
|
+
bucketM;
|
|
11
|
+
cols;
|
|
12
|
+
rows;
|
|
13
|
+
buckets;
|
|
14
|
+
/** Five numbers per segment: ax, az, bx, bz, half width. */
|
|
15
|
+
segs = [];
|
|
16
|
+
/** Number of the line (polygon, path) the segment belongs to. */
|
|
17
|
+
owner = [];
|
|
18
|
+
constructor(sizeX, sizeZ, bucketM = 8) {
|
|
19
|
+
this.sizeX = sizeX;
|
|
20
|
+
this.sizeZ = sizeZ;
|
|
21
|
+
this.bucketM = bucketM;
|
|
22
|
+
this.cols = Math.max(1, Math.ceil(sizeX / bucketM));
|
|
23
|
+
this.rows = Math.max(1, Math.ceil(sizeZ / bucketM));
|
|
24
|
+
this.buckets = Array.from({ length: this.cols * this.rows }, () => []);
|
|
25
|
+
}
|
|
26
|
+
get count() {
|
|
27
|
+
return this.owner.length;
|
|
28
|
+
}
|
|
29
|
+
col(x) {
|
|
30
|
+
return Math.min(this.cols - 1, Math.max(0, Math.floor(x / this.bucketM)));
|
|
31
|
+
}
|
|
32
|
+
row(z) {
|
|
33
|
+
return Math.min(this.rows - 1, Math.max(0, Math.floor(z / this.bucketM)));
|
|
34
|
+
}
|
|
35
|
+
add(ax, az, bx, bz, halfWidth = 0, owner = 0) {
|
|
36
|
+
const id = this.owner.length;
|
|
37
|
+
this.segs.push(ax, az, bx, bz, halfWidth);
|
|
38
|
+
this.owner.push(owner);
|
|
39
|
+
const c0 = this.col(Math.min(ax, bx) - halfWidth);
|
|
40
|
+
const c1 = this.col(Math.max(ax, bx) + halfWidth);
|
|
41
|
+
const r0 = this.row(Math.min(az, bz) - halfWidth);
|
|
42
|
+
const r1 = this.row(Math.max(az, bz) + halfWidth);
|
|
43
|
+
for (let r = r0; r <= r1; r++)
|
|
44
|
+
for (let c = c0; c <= c1; c++)
|
|
45
|
+
this.buckets[r * this.cols + c].push(id);
|
|
46
|
+
}
|
|
47
|
+
/** Polyline as a flat array x, z. */
|
|
48
|
+
addPolyline(points, halfWidth = 0, owner = 0, closed = false) {
|
|
49
|
+
const n = points.length / 2;
|
|
50
|
+
const last = closed ? n : n - 1;
|
|
51
|
+
for (let i = 0; i < last; i++) {
|
|
52
|
+
const j = (i + 1) % n;
|
|
53
|
+
this.add(points[i * 2], points[i * 2 + 1], points[j * 2], points[j * 2 + 1], halfWidth, owner);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Distance to the nearest segment reduced by its half width, at most `maxDistance`.
|
|
58
|
+
* `skipOwner` omits the segments of one line.
|
|
59
|
+
*/
|
|
60
|
+
distance(x, z, maxDistance, skipOwner = -1) {
|
|
61
|
+
let best = maxDistance;
|
|
62
|
+
const c0 = this.col(x - maxDistance);
|
|
63
|
+
const c1 = this.col(x + maxDistance);
|
|
64
|
+
const r0 = this.row(z - maxDistance);
|
|
65
|
+
const r1 = this.row(z + maxDistance);
|
|
66
|
+
const s = this.segs;
|
|
67
|
+
for (let r = r0; r <= r1; r++) {
|
|
68
|
+
for (let c = c0; c <= c1; c++) {
|
|
69
|
+
for (const id of this.buckets[r * this.cols + c]) {
|
|
70
|
+
if (this.owner[id] === skipOwner)
|
|
71
|
+
continue;
|
|
72
|
+
const o = id * 5;
|
|
73
|
+
const d = distToSegment(x, z, s[o], s[o + 1], s[o + 2], s[o + 3]) - s[o + 4];
|
|
74
|
+
if (d < best)
|
|
75
|
+
best = d;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return best;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** How far the outline field searches at most. Beyond that no rule needs the distance. */
|
|
83
|
+
const OUTLINE_SEARCH_M = 24;
|
|
84
|
+
/** Signed distance field to the outlines of a set of polygons (each a ring set with holes). */
|
|
85
|
+
export class OutlineField {
|
|
86
|
+
sizeX;
|
|
87
|
+
sizeZ;
|
|
88
|
+
segments;
|
|
89
|
+
polygons;
|
|
90
|
+
bucketM;
|
|
91
|
+
cols;
|
|
92
|
+
rows;
|
|
93
|
+
/** Polygons whose bounding rectangle reaches into the bin. */
|
|
94
|
+
inBucket;
|
|
95
|
+
/** `polygons[i]` are the rings of one polygon: outer boundary and possible holes. */
|
|
96
|
+
constructor(polygons, sizeX, sizeZ, bucketM = 8) {
|
|
97
|
+
this.sizeX = sizeX;
|
|
98
|
+
this.sizeZ = sizeZ;
|
|
99
|
+
this.polygons = polygons;
|
|
100
|
+
this.bucketM = bucketM;
|
|
101
|
+
this.cols = Math.max(1, Math.ceil(sizeX / bucketM));
|
|
102
|
+
this.rows = Math.max(1, Math.ceil(sizeZ / bucketM));
|
|
103
|
+
this.inBucket = Array.from({ length: this.cols * this.rows }, () => []);
|
|
104
|
+
this.segments = new SegmentField(sizeX, sizeZ, bucketM);
|
|
105
|
+
const clampCol = (x) => Math.min(this.cols - 1, Math.max(0, Math.floor(x / bucketM)));
|
|
106
|
+
const clampRow = (z) => Math.min(this.rows - 1, Math.max(0, Math.floor(z / bucketM)));
|
|
107
|
+
polygons.forEach((rings, i) => {
|
|
108
|
+
let minX = Infinity;
|
|
109
|
+
let maxX = -Infinity;
|
|
110
|
+
let minZ = Infinity;
|
|
111
|
+
let maxZ = -Infinity;
|
|
112
|
+
for (const ring of rings) {
|
|
113
|
+
this.segments.addPolyline(ring, 0, i, true);
|
|
114
|
+
for (let k = 0; k < ring.length; k += 2) {
|
|
115
|
+
minX = Math.min(minX, ring[k]);
|
|
116
|
+
maxX = Math.max(maxX, ring[k]);
|
|
117
|
+
minZ = Math.min(minZ, ring[k + 1]);
|
|
118
|
+
maxZ = Math.max(maxZ, ring[k + 1]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (let r = clampRow(minZ); r <= clampRow(maxZ); r++)
|
|
122
|
+
for (let c = clampCol(minX); c <= clampCol(maxX); c++)
|
|
123
|
+
this.inBucket[r * this.cols + c].push(i);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/** Does a point lie inside some polygon? */
|
|
127
|
+
inside(x, z) {
|
|
128
|
+
if (x < 0 || z < 0 || x >= this.sizeX || z >= this.sizeZ)
|
|
129
|
+
return false;
|
|
130
|
+
const b = Math.floor(z / this.bucketM) * this.cols + Math.floor(x / this.bucketM);
|
|
131
|
+
for (const i of this.inBucket[b] ?? [])
|
|
132
|
+
if (pointInRings(x, z, this.polygons[i]))
|
|
133
|
+
return true;
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Distance from a point to the nearest outline in metres, at most `maxDistance`. Negative inside a polygon.
|
|
138
|
+
* Computed exactly to the polygon edges, not from the raster.
|
|
139
|
+
*/
|
|
140
|
+
distance(x, z, maxDistance = OUTLINE_SEARCH_M) {
|
|
141
|
+
const d = this.segments.distance(x, z, maxDistance);
|
|
142
|
+
return this.inside(x, z) ? -d : d;
|
|
143
|
+
}
|
|
144
|
+
/** The field written out to a raster with step `step` (value at the cell centre), for textures and debugging. */
|
|
145
|
+
rasterize(step, maxDistance = OUTLINE_SEARCH_M) {
|
|
146
|
+
const cols = Math.ceil(this.sizeX / step);
|
|
147
|
+
const rows = Math.ceil(this.sizeZ / step);
|
|
148
|
+
const data = new Float32Array(cols * rows);
|
|
149
|
+
for (let j = 0; j < rows; j++)
|
|
150
|
+
for (let i = 0; i < cols; i++)
|
|
151
|
+
data[j * cols + i] = this.distance((i + 0.5) * step, (j + 0.5) * step, maxDistance);
|
|
152
|
+
return { data, cols, rows, step };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type Ring = Float32Array;
|
|
2
|
+
export declare function ringSignedArea(r: ArrayLike<number>): number;
|
|
3
|
+
/** Area of a ring set: outer boundaries minus holes. */
|
|
4
|
+
export declare function ringsArea(rings: Ring[]): number;
|
|
5
|
+
export declare function pointInRings(x: number, z: number, rings: Ring[]): boolean;
|
|
6
|
+
/** Distance from a point to a segment. */
|
|
7
|
+
export declare function distToSegment(px: number, pz: number, ax: number, az: number, bx: number, bz: number): number;
|
|
8
|
+
/** Rotated bounding rectangle: centre, unit direction of the longer axis and half extents. */
|
|
9
|
+
export interface OrientedBox {
|
|
10
|
+
cx: number;
|
|
11
|
+
cz: number;
|
|
12
|
+
/** Unit direction of the longer axis. */
|
|
13
|
+
ux: number;
|
|
14
|
+
uz: number;
|
|
15
|
+
halfL: number;
|
|
16
|
+
halfW: number;
|
|
17
|
+
}
|
|
18
|
+
/** Minimum-area bounding rectangle. One of its sides always lies on an edge of the convex hull. */
|
|
19
|
+
export declare function orientedBox(r: ArrayLike<number>): OrientedBox;
|
|
20
|
+
/**
|
|
21
|
+
* One ring round an open line: `points` are x, z pairs of at least two points, the ring lies `halfWidth` on both sides
|
|
22
|
+
* of it with butt ends and mitred joins, wound like an outer boundary (positive area). Null when the line has fewer
|
|
23
|
+
* than two points, a segment is shorter than `halfWidth`, or a mitre would be longer than 3 × `halfWidth` (the limit
|
|
24
|
+
* of `insetRing`).
|
|
25
|
+
*/
|
|
26
|
+
export declare function lineRing(points: ArrayLike<number>, halfWidth: number): Ring | null;
|
|
27
|
+
/**
|
|
28
|
+
* Offsets a ring by d metres into the solid (outer boundary inward, hole away from the hole).
|
|
29
|
+
* Returns null when the offset shape would flip, in which case the simpler shape without offset should be used.
|
|
30
|
+
*/
|
|
31
|
+
export declare function insetRing(r: Ring, d: number): Ring | null;
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
// Planar polygon geometry. A ring is a flat array x0, z0, x1, z1, … without repeating the first point.
|
|
2
|
+
// An outer boundary has positive area according to `ringSignedArea`, holes negative.
|
|
3
|
+
export function ringSignedArea(r) {
|
|
4
|
+
let s = 0;
|
|
5
|
+
const n = r.length / 2;
|
|
6
|
+
for (let i = 0, j = n - 1; i < n; j = i++)
|
|
7
|
+
s += r[j * 2] * r[i * 2 + 1] - r[i * 2] * r[j * 2 + 1];
|
|
8
|
+
return s / 2;
|
|
9
|
+
}
|
|
10
|
+
/** Area of a ring set: outer boundaries minus holes. */
|
|
11
|
+
export function ringsArea(rings) {
|
|
12
|
+
let s = 0;
|
|
13
|
+
for (const r of rings)
|
|
14
|
+
s += ringSignedArea(r);
|
|
15
|
+
return Math.abs(s);
|
|
16
|
+
}
|
|
17
|
+
export function pointInRings(x, z, rings) {
|
|
18
|
+
let inside = false;
|
|
19
|
+
for (const r of rings) {
|
|
20
|
+
const n = r.length / 2;
|
|
21
|
+
for (let i = 0, j = n - 1; i < n; j = i++) {
|
|
22
|
+
const xi = r[i * 2];
|
|
23
|
+
const zi = r[i * 2 + 1];
|
|
24
|
+
const xj = r[j * 2];
|
|
25
|
+
const zj = r[j * 2 + 1];
|
|
26
|
+
if (zi > z !== zj > z && x < ((xj - xi) * (z - zi)) / (zj - zi) + xi)
|
|
27
|
+
inside = !inside;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return inside;
|
|
31
|
+
}
|
|
32
|
+
/** Distance from a point to a segment. */
|
|
33
|
+
export function distToSegment(px, pz, ax, az, bx, bz) {
|
|
34
|
+
const dx = bx - ax;
|
|
35
|
+
const dz = bz - az;
|
|
36
|
+
const l2 = dx * dx + dz * dz;
|
|
37
|
+
let t = l2 > 0 ? ((px - ax) * dx + (pz - az) * dz) / l2 : 0;
|
|
38
|
+
t = t < 0 ? 0 : t > 1 ? 1 : t;
|
|
39
|
+
return Math.hypot(px - ax - t * dx, pz - az - t * dz);
|
|
40
|
+
}
|
|
41
|
+
function convexHull(r) {
|
|
42
|
+
const n = r.length / 2;
|
|
43
|
+
const order = Array.from({ length: n }, (_, i) => i).sort((a, b) => r[a * 2] - r[b * 2] || r[a * 2 + 1] - r[b * 2 + 1]);
|
|
44
|
+
const crossAt = (o, a, b) => (r[a * 2] - r[o * 2]) * (r[b * 2 + 1] - r[o * 2 + 1]) - (r[a * 2 + 1] - r[o * 2 + 1]) * (r[b * 2] - r[o * 2]);
|
|
45
|
+
const hull = [];
|
|
46
|
+
for (const i of order) {
|
|
47
|
+
while (hull.length >= 2 && crossAt(hull[hull.length - 2], hull[hull.length - 1], i) <= 0)
|
|
48
|
+
hull.pop();
|
|
49
|
+
hull.push(i);
|
|
50
|
+
}
|
|
51
|
+
const lower = hull.length + 1;
|
|
52
|
+
for (let k = order.length - 2; k >= 0; k--) {
|
|
53
|
+
const i = order[k];
|
|
54
|
+
while (hull.length >= lower && crossAt(hull[hull.length - 2], hull[hull.length - 1], i) <= 0)
|
|
55
|
+
hull.pop();
|
|
56
|
+
hull.push(i);
|
|
57
|
+
}
|
|
58
|
+
hull.pop();
|
|
59
|
+
return hull;
|
|
60
|
+
}
|
|
61
|
+
/** Minimum-area bounding rectangle. One of its sides always lies on an edge of the convex hull. */
|
|
62
|
+
export function orientedBox(r) {
|
|
63
|
+
const hull = convexHull(r);
|
|
64
|
+
let best = null;
|
|
65
|
+
let bestArea = Infinity;
|
|
66
|
+
for (let k = 0; k < hull.length; k++) {
|
|
67
|
+
const a = hull[k];
|
|
68
|
+
const b = hull[(k + 1) % hull.length];
|
|
69
|
+
let ex = r[b * 2] - r[a * 2];
|
|
70
|
+
let ez = r[b * 2 + 1] - r[a * 2 + 1];
|
|
71
|
+
const el = Math.hypot(ex, ez);
|
|
72
|
+
if (el < 1e-6)
|
|
73
|
+
continue;
|
|
74
|
+
ex /= el;
|
|
75
|
+
ez /= el;
|
|
76
|
+
let minU = Infinity;
|
|
77
|
+
let maxU = -Infinity;
|
|
78
|
+
let minV = Infinity;
|
|
79
|
+
let maxV = -Infinity;
|
|
80
|
+
for (const i of hull) {
|
|
81
|
+
const u = r[i * 2] * ex + r[i * 2 + 1] * ez;
|
|
82
|
+
const v = -r[i * 2] * ez + r[i * 2 + 1] * ex;
|
|
83
|
+
if (u < minU)
|
|
84
|
+
minU = u;
|
|
85
|
+
if (u > maxU)
|
|
86
|
+
maxU = u;
|
|
87
|
+
if (v < minV)
|
|
88
|
+
minV = v;
|
|
89
|
+
if (v > maxV)
|
|
90
|
+
maxV = v;
|
|
91
|
+
}
|
|
92
|
+
const area = (maxU - minU) * (maxV - minV);
|
|
93
|
+
if (area < bestArea) {
|
|
94
|
+
bestArea = area;
|
|
95
|
+
const cu = (minU + maxU) / 2;
|
|
96
|
+
const cv = (minV + maxV) / 2;
|
|
97
|
+
const hu = (maxU - minU) / 2;
|
|
98
|
+
const hv = (maxV - minV) / 2;
|
|
99
|
+
const cx = cu * ex - cv * ez;
|
|
100
|
+
const cz = cu * ez + cv * ex;
|
|
101
|
+
best = hu >= hv ? { cx, cz, ux: ex, uz: ez, halfL: hu, halfW: hv } : { cx, cz, ux: -ez, uz: ex, halfL: hv, halfW: hu };
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (!best)
|
|
105
|
+
throw new Error('The ring has no area');
|
|
106
|
+
return best;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* One ring round an open line: `points` are x, z pairs of at least two points, the ring lies `halfWidth` on both sides
|
|
110
|
+
* of it with butt ends and mitred joins, wound like an outer boundary (positive area). Null when the line has fewer
|
|
111
|
+
* than two points, a segment is shorter than `halfWidth`, or a mitre would be longer than 3 × `halfWidth` (the limit
|
|
112
|
+
* of `insetRing`).
|
|
113
|
+
*/
|
|
114
|
+
export function lineRing(points, halfWidth) {
|
|
115
|
+
const n = Math.floor(points.length / 2);
|
|
116
|
+
if (n < 2 || !(halfWidth > 0))
|
|
117
|
+
return null;
|
|
118
|
+
const MITRE_LIMIT = 3;
|
|
119
|
+
// Unit direction of every segment.
|
|
120
|
+
const dir = new Float64Array(2 * (n - 1));
|
|
121
|
+
for (let i = 0; i + 1 < n; i++) {
|
|
122
|
+
const dx = points[2 * i + 2] - points[2 * i];
|
|
123
|
+
const dz = points[2 * i + 3] - points[2 * i + 1];
|
|
124
|
+
const l = Math.hypot(dx, dz);
|
|
125
|
+
if (!(l >= halfWidth))
|
|
126
|
+
return null;
|
|
127
|
+
dir[2 * i] = dx / l;
|
|
128
|
+
dir[2 * i + 1] = dz / l;
|
|
129
|
+
}
|
|
130
|
+
const left = new Float64Array(2 * n);
|
|
131
|
+
for (let i = 0; i < n; i++) {
|
|
132
|
+
// Left normals of the segment before and after the point (the same one at an end).
|
|
133
|
+
const a = Math.max(0, i - 1);
|
|
134
|
+
const b = Math.min(n - 2, i);
|
|
135
|
+
const n0x = -dir[2 * a + 1];
|
|
136
|
+
const n0z = dir[2 * a];
|
|
137
|
+
const n1x = -dir[2 * b + 1];
|
|
138
|
+
const n1z = dir[2 * b];
|
|
139
|
+
const k = 1 + n0x * n1x + n0z * n1z;
|
|
140
|
+
if (k < 1e-6)
|
|
141
|
+
return null;
|
|
142
|
+
const mx = (n0x + n1x) / k;
|
|
143
|
+
const mz = (n0z + n1z) / k;
|
|
144
|
+
if (Math.hypot(mx, mz) > MITRE_LIMIT)
|
|
145
|
+
return null;
|
|
146
|
+
left[2 * i] = mx * halfWidth;
|
|
147
|
+
left[2 * i + 1] = mz * halfWidth;
|
|
148
|
+
}
|
|
149
|
+
// Left side forward, right side back.
|
|
150
|
+
const out = new Float32Array(4 * n);
|
|
151
|
+
for (let i = 0; i < n; i++) {
|
|
152
|
+
out[2 * i] = points[2 * i] + left[2 * i];
|
|
153
|
+
out[2 * i + 1] = points[2 * i + 1] + left[2 * i + 1];
|
|
154
|
+
const j = n - 1 - i;
|
|
155
|
+
out[2 * (n + i)] = points[2 * j] - left[2 * j];
|
|
156
|
+
out[2 * (n + i) + 1] = points[2 * j + 1] - left[2 * j + 1];
|
|
157
|
+
}
|
|
158
|
+
if (ringSignedArea(out) < 0) {
|
|
159
|
+
for (let i = 0, j = 2 * n - 1; i < j; i++, j--) {
|
|
160
|
+
const x = out[2 * i];
|
|
161
|
+
const z = out[2 * i + 1];
|
|
162
|
+
out[2 * i] = out[2 * j];
|
|
163
|
+
out[2 * i + 1] = out[2 * j + 1];
|
|
164
|
+
out[2 * j] = x;
|
|
165
|
+
out[2 * j + 1] = z;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Offsets a ring by d metres into the solid (outer boundary inward, hole away from the hole).
|
|
172
|
+
* Returns null when the offset shape would flip, in which case the simpler shape without offset should be used.
|
|
173
|
+
*/
|
|
174
|
+
export function insetRing(r, d) {
|
|
175
|
+
const n = r.length / 2;
|
|
176
|
+
const out = new Float32Array(r.length);
|
|
177
|
+
/** Longest allowed miter in multiples of d, sharp spikes are cut off. */
|
|
178
|
+
const MITRE_LIMIT = 3;
|
|
179
|
+
for (let i = 0; i < n; i++) {
|
|
180
|
+
const p = (i + n - 1) % n;
|
|
181
|
+
const q = (i + 1) % n;
|
|
182
|
+
let ax = r[i * 2] - r[p * 2];
|
|
183
|
+
let az = r[i * 2 + 1] - r[p * 2 + 1];
|
|
184
|
+
let bx = r[q * 2] - r[i * 2];
|
|
185
|
+
let bz = r[q * 2 + 1] - r[i * 2 + 1];
|
|
186
|
+
const al = Math.hypot(ax, az) || 1;
|
|
187
|
+
const bl = Math.hypot(bx, bz) || 1;
|
|
188
|
+
ax /= al;
|
|
189
|
+
az /= al;
|
|
190
|
+
bx /= bl;
|
|
191
|
+
bz /= bl;
|
|
192
|
+
// The left normal of an edge points inward for a positively oriented outer boundary.
|
|
193
|
+
const n0x = -az;
|
|
194
|
+
const n0z = ax;
|
|
195
|
+
const n1x = -bz;
|
|
196
|
+
const n1z = bx;
|
|
197
|
+
const k = 1 + n0x * n1x + n0z * n1z;
|
|
198
|
+
let mx;
|
|
199
|
+
let mz;
|
|
200
|
+
if (k < 1e-3) {
|
|
201
|
+
mx = n0x;
|
|
202
|
+
mz = n0z;
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
mx = (n0x + n1x) / k;
|
|
206
|
+
mz = (n0z + n1z) / k;
|
|
207
|
+
const ml = Math.hypot(mx, mz);
|
|
208
|
+
if (ml > MITRE_LIMIT) {
|
|
209
|
+
mx *= MITRE_LIMIT / ml;
|
|
210
|
+
mz *= MITRE_LIMIT / ml;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
out[i * 2] = r[i * 2] + mx * d;
|
|
214
|
+
out[i * 2 + 1] = r[i * 2 + 1] + mz * d;
|
|
215
|
+
}
|
|
216
|
+
// Check: no edge may flip and the area must not collapse.
|
|
217
|
+
for (let i = 0; i < n; i++) {
|
|
218
|
+
const q = (i + 1) % n;
|
|
219
|
+
const ex = r[q * 2] - r[i * 2];
|
|
220
|
+
const ez = r[q * 2 + 1] - r[i * 2 + 1];
|
|
221
|
+
const fx = out[q * 2] - out[i * 2];
|
|
222
|
+
const fz = out[q * 2 + 1] - out[i * 2 + 1];
|
|
223
|
+
if (ex * fx + ez * fz <= 0 && Math.hypot(ex, ez) > 2 * d)
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
const a0 = ringSignedArea(r);
|
|
227
|
+
const a1 = ringSignedArea(out);
|
|
228
|
+
if (a0 > 0 ? a1 <= 0.2 * a0 : a1 >= 0)
|
|
229
|
+
return null;
|
|
230
|
+
return out;
|
|
231
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** FNV-1a over the characters of a string, the result is an unsigned 32-bit number. */
|
|
2
|
+
export declare function hashString(s: string): number;
|
|
3
|
+
/** Full avalanche mixing of 32 bits (finalizer from MurmurHash3). */
|
|
4
|
+
export declare function mix32(h: number): number;
|
|
5
|
+
/** Combines a seed with another integer. */
|
|
6
|
+
export declare function hashInt(seed: number, k: number): number;
|
|
7
|
+
/** Combines a seed with any number of integers. The order of keys matters. */
|
|
8
|
+
export declare function hashInts(seed: number, ...keys: number[]): number;
|
|
9
|
+
/** Seed of one subsystem's stream. */
|
|
10
|
+
export declare function streamSeed(worldSeed: string, subsystem: string): number;
|
|
11
|
+
/** Number in the range 0 to 1 (excluding 1) from a hash. */
|
|
12
|
+
export declare function unit(h: number): number;
|
|
13
|
+
/** Number 0 to 1 for a given seed and keys. */
|
|
14
|
+
export declare function rand(seed: number, ...keys: number[]): number;
|
|
15
|
+
/** Number in the range lo to hi. */
|
|
16
|
+
export declare function randRange(lo: number, hi: number, seed: number, ...keys: number[]): number;
|
|
17
|
+
/** Choice among options with weights according to a number 0–1. */
|
|
18
|
+
export declare function pickWeighted<T>(options: readonly (readonly [T, number])[], r: number): T;
|