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,596 @@
|
|
|
1
|
+
// Bare shapes for `MeshBuilder<V>`: plain geometry that asks the caller for the payload of every vertex. Nothing here
|
|
2
|
+
// carries a look: no colour, no seams, no bevels, no irregularity. A caller adds all of that through the payload
|
|
3
|
+
// callback (which gets the geometric facts of the vertex) and, for irregular shapes, through the `warp` hook that may
|
|
4
|
+
// move grid points before the normals are taken.
|
|
5
|
+
//
|
|
6
|
+
// gridSurface a parametric grid of points, stitched into quads (the base of the others)
|
|
7
|
+
// revolve a surface of revolution about +y from a profile of [radius, y] points
|
|
8
|
+
// sweep a section pulled along a path, with rotation-minimising frames
|
|
9
|
+
// extrude an outline in the x–y plane pushed along +z, with optional caps
|
|
10
|
+
// polygon a flat simple polygon (convex: a fan from the first corner; otherwise ear clipping)
|
|
11
|
+
// box, cylinder special cases of extrude and revolve
|
|
12
|
+
//
|
|
13
|
+
// Winding: counter-clockwise seen from outside, so a closed shape has a positive volume. Normals are smooth (shared
|
|
14
|
+
// vertices) or flat (every face its own vertices), by the `smooth` option of each shape.
|
|
15
|
+
import { faceNormal } from './mesh-builder.js';
|
|
16
|
+
import { triangulateRing } from './prism-mesh.js';
|
|
17
|
+
const TAU = Math.PI * 2;
|
|
18
|
+
class Facts {
|
|
19
|
+
p = [0, 0, 0];
|
|
20
|
+
n = [0, 0, 0];
|
|
21
|
+
part = 'side';
|
|
22
|
+
i = 0;
|
|
23
|
+
j = 0;
|
|
24
|
+
u = 0;
|
|
25
|
+
v = 0;
|
|
26
|
+
face = -1;
|
|
27
|
+
corner = -1;
|
|
28
|
+
centre = false;
|
|
29
|
+
set(p, n, part, i, j, u, v, face, corner, centre = false) {
|
|
30
|
+
this.p[0] = p[0];
|
|
31
|
+
this.p[1] = p[1];
|
|
32
|
+
this.p[2] = p[2];
|
|
33
|
+
this.n[0] = n[0];
|
|
34
|
+
this.n[1] = n[1];
|
|
35
|
+
this.n[2] = n[2];
|
|
36
|
+
this.part = part;
|
|
37
|
+
this.i = i;
|
|
38
|
+
this.j = j;
|
|
39
|
+
this.u = u;
|
|
40
|
+
this.v = v;
|
|
41
|
+
this.face = face;
|
|
42
|
+
this.corner = corner;
|
|
43
|
+
this.centre = centre;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
|
|
48
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
49
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
50
|
+
function unit(a) {
|
|
51
|
+
const l = Math.sqrt(dot(a, a));
|
|
52
|
+
if (l > 0) {
|
|
53
|
+
a[0] /= l;
|
|
54
|
+
a[1] /= l;
|
|
55
|
+
a[2] /= l;
|
|
56
|
+
}
|
|
57
|
+
return a;
|
|
58
|
+
}
|
|
59
|
+
/** Shares 0–1 by cumulative distance between consecutive points (`closed` adds the closing edge; n + 1 values then). */
|
|
60
|
+
function arcShares(pts, dims, closed) {
|
|
61
|
+
const out = [0];
|
|
62
|
+
const n = pts.length;
|
|
63
|
+
const edges = closed ? n : n - 1;
|
|
64
|
+
for (let k = 0; k < edges; k++) {
|
|
65
|
+
const a = pts[k], b = pts[(k + 1) % n];
|
|
66
|
+
let d = 0;
|
|
67
|
+
for (let c = 0; c < dims; c++)
|
|
68
|
+
d += (b[c] - a[c]) ** 2;
|
|
69
|
+
out.push(out[k] + Math.sqrt(d));
|
|
70
|
+
}
|
|
71
|
+
const total = out[out.length - 1];
|
|
72
|
+
return out.map((x, k) => (total > 0 ? x / total : edges > 0 ? k / edges : 0));
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A flat simple polygon (corners counter-clockwise seen from the front) with one normal (Newell's). A convex polygon
|
|
76
|
+
* is a fan from its first corner; a concave one is ear-clipped (corners on a straight line may be left out of the
|
|
77
|
+
* triangles). Returns the index of the first vertex written.
|
|
78
|
+
*/
|
|
79
|
+
export function polygon(b, points, payload, o = {}) {
|
|
80
|
+
const s = new Facts();
|
|
81
|
+
return writePolygon(b, points, payload, s, o.part ?? 'side', o.face ?? 0, o.fan ?? false, 0, null);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Writes one flat polygon. `iBase` and `rowJ` let shapes report their own grid indices for the corners (`rowJ` null:
|
|
85
|
+
* `i` = corner, `j` = 0).
|
|
86
|
+
*/
|
|
87
|
+
function writePolygon(b, pts, payload, s, part, face, fan, iBase, rowJ) {
|
|
88
|
+
const n = pts.length;
|
|
89
|
+
const first = b.vertexCount;
|
|
90
|
+
if (n < 3)
|
|
91
|
+
return first;
|
|
92
|
+
const nrm = faceNormal(pts);
|
|
93
|
+
const shares = arcShares(pts, 3, true);
|
|
94
|
+
if (fan) {
|
|
95
|
+
const c = [0, 0, 0];
|
|
96
|
+
for (const p of pts)
|
|
97
|
+
for (let k = 0; k < 3; k++)
|
|
98
|
+
c[k] = c[k] + p[k] / n;
|
|
99
|
+
const ic = b.vertex(c, nrm, payload(s.set(c, nrm, part, iBase, rowJ ?? 0, 0, 0, face, -1, true)));
|
|
100
|
+
const rim = [];
|
|
101
|
+
for (let k = 0; k < n; k++)
|
|
102
|
+
rim.push(b.vertex(pts[k], nrm, payload(s.set(pts[k], nrm, part, iBase + k, rowJ ?? 0, shares[k], 1, face, k))));
|
|
103
|
+
for (let k = 0; k < n; k++)
|
|
104
|
+
b.tri(ic, rim[k], rim[(k + 1) % n]);
|
|
105
|
+
return first;
|
|
106
|
+
}
|
|
107
|
+
for (let k = 0; k < n; k++)
|
|
108
|
+
b.vertex(pts[k], nrm, payload(s.set(pts[k], nrm, part, iBase + k, rowJ ?? 0, shares[k], 1, face, k)));
|
|
109
|
+
// Project onto the plane: e1 × e2 = normal, so the polygon is counter-clockwise in (e1, e2).
|
|
110
|
+
const ref = Math.abs(nrm[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
111
|
+
const e1 = unit(cross(ref, nrm));
|
|
112
|
+
const e2 = cross(nrm, e1);
|
|
113
|
+
const flat = [];
|
|
114
|
+
for (const p of pts)
|
|
115
|
+
flat.push(dot(p, e1), dot(p, e2));
|
|
116
|
+
let convex = true;
|
|
117
|
+
for (let k = 0; k < n && convex; k++) {
|
|
118
|
+
const a = k * 2, c = ((k + 1) % n) * 2, d = ((k + 2) % n) * 2;
|
|
119
|
+
const t = (flat[c] - flat[a]) * (flat[d + 1] - flat[a + 1]) - (flat[c + 1] - flat[a + 1]) * (flat[d] - flat[a]);
|
|
120
|
+
if (t < -1e-12)
|
|
121
|
+
convex = false;
|
|
122
|
+
}
|
|
123
|
+
if (convex) {
|
|
124
|
+
for (let k = 1; k + 1 < n; k++)
|
|
125
|
+
b.tri(first, first + k, first + k + 1);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// Ear clipping in (e1, e2): its triangles have a positive area there, i.e. they wind counter-clockwise about the normal.
|
|
129
|
+
const t = triangulateRing(flat);
|
|
130
|
+
for (let k = 0; k < t.length; k += 3)
|
|
131
|
+
b.tri(first + t[k], first + t[k + 1], first + t[k + 2]);
|
|
132
|
+
}
|
|
133
|
+
return first;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* A grid of `cols` × `rows` points `at(i, j)` stitched into quads (i, j) (i+1, j) (i+1, j+1) (i, j+1), which face
|
|
137
|
+
* the side d/di × d/dj points to. Smooth normals come from central differences of neighbouring points (wrapping
|
|
138
|
+
* where the grid wraps). A cell whose two points on one edge coincide (an apex) becomes a triangle. With 'along'
|
|
139
|
+
* every column strip has its own vertices, normals across from the strip's edge and along from central differences
|
|
140
|
+
* (face and corner −1). Returns the number of flat faces written (0 unless flat).
|
|
141
|
+
*/
|
|
142
|
+
export function gridSurface(b, cols, rows, at, payload, o = {}) {
|
|
143
|
+
if (cols < 2 && !o.wrapI)
|
|
144
|
+
return 0;
|
|
145
|
+
if (rows < 2 && !o.wrapJ)
|
|
146
|
+
return 0;
|
|
147
|
+
const P = [];
|
|
148
|
+
for (let j = 0; j < rows; j++)
|
|
149
|
+
for (let i = 0; i < cols; i++)
|
|
150
|
+
P.push(at(i, j, [0, 0, 0]));
|
|
151
|
+
const s = new Facts();
|
|
152
|
+
const wi = !!o.wrapI, wj = !!o.wrapJ;
|
|
153
|
+
const cellsI = wi ? cols : cols - 1;
|
|
154
|
+
const cellsJ = wj ? rows : rows - 1;
|
|
155
|
+
const U = o.u ?? Array.from({ length: cols + 1 }, (_, i) => (wi ? i / cols : i / Math.max(1, cols - 1)));
|
|
156
|
+
const Vs = o.v ?? Array.from({ length: rows + 1 }, (_, j) => (wj ? j / rows : j / Math.max(1, rows - 1)));
|
|
157
|
+
const part = o.part ?? 'side';
|
|
158
|
+
const flip = !!o.flip;
|
|
159
|
+
const get = (i, j) => {
|
|
160
|
+
i = wi ? ((i % cols) + cols) % cols : Math.max(0, Math.min(cols - 1, i));
|
|
161
|
+
j = wj ? ((j % rows) + rows) % rows : Math.max(0, Math.min(rows - 1, j));
|
|
162
|
+
return P[j * cols + i];
|
|
163
|
+
};
|
|
164
|
+
const same = (a, c) => Math.abs(a[0] - c[0]) + Math.abs(a[1] - c[1]) + Math.abs(a[2] - c[2]) < 1e-9;
|
|
165
|
+
if (o.smooth === false) {
|
|
166
|
+
let face = o.faceBase ?? 0;
|
|
167
|
+
const corner = [[0, 0], [1, 0], [1, 1], [0, 1]];
|
|
168
|
+
for (let j = 0; j < cellsJ; j++) {
|
|
169
|
+
for (let i = 0; i < cellsI; i++) {
|
|
170
|
+
const pts = [];
|
|
171
|
+
const ids = [];
|
|
172
|
+
for (const [di, dj] of flip ? [corner[0], corner[3], corner[2], corner[1]] : corner) {
|
|
173
|
+
const p = get(i + di, j + dj);
|
|
174
|
+
if (pts.length && same(pts[pts.length - 1], p))
|
|
175
|
+
continue;
|
|
176
|
+
if (pts.length === 3 && same(pts[0], p))
|
|
177
|
+
continue;
|
|
178
|
+
pts.push(p);
|
|
179
|
+
ids.push([i + di, j + dj]);
|
|
180
|
+
}
|
|
181
|
+
if (pts.length > 1 && same(pts[0], pts[pts.length - 1]))
|
|
182
|
+
(pts.pop(), ids.pop());
|
|
183
|
+
if (pts.length < 3)
|
|
184
|
+
continue;
|
|
185
|
+
const nrm = faceNormal(pts);
|
|
186
|
+
const firstV = b.vertexCount;
|
|
187
|
+
pts.forEach((p, k) => {
|
|
188
|
+
const [ii, jj] = ids[k];
|
|
189
|
+
b.vertex(p, nrm, payload(s.set(p, nrm, part, ii, jj, U[ii], Vs[jj], face, k)));
|
|
190
|
+
});
|
|
191
|
+
for (let k = 1; k + 1 < pts.length; k++)
|
|
192
|
+
b.tri(firstV, firstV + k, firstV + k + 1);
|
|
193
|
+
face++;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return face - (o.faceBase ?? 0);
|
|
197
|
+
}
|
|
198
|
+
if (o.smooth === 'along') {
|
|
199
|
+
const vr = wj ? rows + 1 : rows;
|
|
200
|
+
const nrm = [0, 0, 0];
|
|
201
|
+
for (let i = 0; i < cellsI; i++) {
|
|
202
|
+
const firstV = b.vertexCount;
|
|
203
|
+
for (let j = 0; j < vr; j++) {
|
|
204
|
+
const p0 = get(i, j), p1 = get(i + 1, j);
|
|
205
|
+
let across = sub(p1, p0);
|
|
206
|
+
// At an apex the strip's edge collapses: take it from the neighbouring row.
|
|
207
|
+
if (dot(across, across) < 1e-24)
|
|
208
|
+
across = sub(get(i + 1, j + 1), get(i, j + 1));
|
|
209
|
+
if (dot(across, across) < 1e-24)
|
|
210
|
+
across = sub(get(i + 1, j - 1), get(i, j - 1));
|
|
211
|
+
const ahead = sub(get(i, j + 1), get(i, j - 1));
|
|
212
|
+
const ahead1 = sub(get(i + 1, j + 1), get(i + 1, j - 1));
|
|
213
|
+
const c = cross(across, [ahead[0] + ahead1[0], ahead[1] + ahead1[1], ahead[2] + ahead1[2]]);
|
|
214
|
+
nrm[0] = flip ? -c[0] : c[0];
|
|
215
|
+
nrm[1] = flip ? -c[1] : c[1];
|
|
216
|
+
nrm[2] = flip ? -c[2] : c[2];
|
|
217
|
+
unit(nrm);
|
|
218
|
+
b.vertex(p0, nrm, payload(s.set(p0, nrm, part, i, j, U[i], Vs[j], -1, -1)));
|
|
219
|
+
b.vertex(p1, nrm, payload(s.set(p1, nrm, part, i + 1, j, U[i + 1], Vs[j], -1, -1)));
|
|
220
|
+
}
|
|
221
|
+
for (let j = 0; j < cellsJ; j++) {
|
|
222
|
+
const a = firstV + j * 2, c = a + 1, d = a + 3, e = a + 2;
|
|
223
|
+
if (flip)
|
|
224
|
+
b.quad(a, e, d, c);
|
|
225
|
+
else
|
|
226
|
+
b.quad(a, c, d, e);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return 0;
|
|
230
|
+
}
|
|
231
|
+
// Smooth: one vertex per grid point (plus the seam column / row again when wrapping, for u / v continuity).
|
|
232
|
+
const vc = wi ? cols + 1 : cols;
|
|
233
|
+
const vr = wj ? rows + 1 : rows;
|
|
234
|
+
const first = b.vertexCount;
|
|
235
|
+
const nrm = [0, 0, 0];
|
|
236
|
+
for (let j = 0; j < vr; j++) {
|
|
237
|
+
for (let i = 0; i < vc; i++) {
|
|
238
|
+
const p = get(i, j);
|
|
239
|
+
if (o.normalAt)
|
|
240
|
+
o.normalAt(i % cols, j % rows, nrm);
|
|
241
|
+
else {
|
|
242
|
+
const di = sub(get(i + 1, j), get(i - 1, j));
|
|
243
|
+
const dj = sub(get(i, j + 1), get(i, j - 1));
|
|
244
|
+
const c = cross(di, dj);
|
|
245
|
+
nrm[0] = c[0];
|
|
246
|
+
nrm[1] = c[1];
|
|
247
|
+
nrm[2] = c[2];
|
|
248
|
+
if (dot(nrm, nrm) < 1e-24) {
|
|
249
|
+
// A collapsed row (an apex): the mean of the neighbouring cells' normals along j.
|
|
250
|
+
const k = cross(sub(get(i + 1, j + 1), get(i - 1, j + 1)), sub(get(i, j + 1), p));
|
|
251
|
+
const l = cross(sub(get(i + 1, j - 1), get(i - 1, j - 1)), sub(p, get(i, j - 1)));
|
|
252
|
+
nrm[0] = k[0] + l[0];
|
|
253
|
+
nrm[1] = k[1] + l[1];
|
|
254
|
+
nrm[2] = k[2] + l[2];
|
|
255
|
+
}
|
|
256
|
+
if (flip)
|
|
257
|
+
(nrm[0] = -nrm[0], nrm[1] = -nrm[1], nrm[2] = -nrm[2]);
|
|
258
|
+
}
|
|
259
|
+
unit(nrm);
|
|
260
|
+
b.vertex(p, nrm, payload(s.set(p, nrm, part, i, j, U[i], Vs[j], -1, -1)));
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
for (let j = 0; j < cellsJ; j++) {
|
|
264
|
+
for (let i = 0; i < cellsI; i++) {
|
|
265
|
+
const a = first + j * vc + i, c = a + 1, d = a + vc + 1, e = a + vc;
|
|
266
|
+
const pa = get(i, j), pc = get(i + 1, j), pd = get(i + 1, j + 1), pe = get(i, j + 1);
|
|
267
|
+
const q = flip ? [a, e, d, c] : [a, c, d, e];
|
|
268
|
+
const qp = flip ? [pa, pe, pd, pc] : [pa, pc, pd, pe];
|
|
269
|
+
// Drop a collapsed edge: the cell is a triangle.
|
|
270
|
+
let k = 0;
|
|
271
|
+
for (; k < 4; k++)
|
|
272
|
+
if (same(qp[k], qp[(k + 1) % 4]))
|
|
273
|
+
break;
|
|
274
|
+
if (k === 4)
|
|
275
|
+
b.quad(q[0], q[1], q[2], q[3]);
|
|
276
|
+
else {
|
|
277
|
+
// Keep the first vertex of the collapsed edge (the apex vertex of this column), drop the second.
|
|
278
|
+
const r = [q[k], q[(k + 2) % 4], q[(k + 3) % 4]];
|
|
279
|
+
const rp = [qp[k], qp[(k + 2) % 4], qp[(k + 3) % 4]];
|
|
280
|
+
if (!same(rp[0], rp[1]) && !same(rp[1], rp[2]) && !same(rp[2], rp[0]))
|
|
281
|
+
b.tri(r[0], r[1], r[2]);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
return 0;
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* A surface of revolution about +y from a profile of [radius, y] points. Faces point to the right of the profile's
|
|
289
|
+
* direction in the r–y plane: a profile from bottom to top faces outward. A point of radius 0 is an apex (the cells
|
|
290
|
+
* next to it are triangles). Side vertices report `i` around (0…segments), `j` the profile point, `u` the angle
|
|
291
|
+
* share, `v` the arc-length share along the profile. Returns the vertex index of the first vertex written.
|
|
292
|
+
*/
|
|
293
|
+
export function revolve(b, profile, payload, o = {}) {
|
|
294
|
+
const first = b.vertexCount;
|
|
295
|
+
const np = profile.length;
|
|
296
|
+
if (np < 2)
|
|
297
|
+
return first;
|
|
298
|
+
const seg = Math.max(1, o.segments ?? 24);
|
|
299
|
+
const from = o.from ?? 0;
|
|
300
|
+
const to = o.to ?? TAU;
|
|
301
|
+
const full = Math.abs(Math.abs(to - from) - TAU) < 1e-9;
|
|
302
|
+
const angle = (i) => from + ((to - from) * i) / seg;
|
|
303
|
+
// Outward profile normals in the r–y plane from the neighbours (one-sided at the ends).
|
|
304
|
+
const pn = [];
|
|
305
|
+
for (let k = 0; k < np; k++) {
|
|
306
|
+
const a = profile[Math.max(0, k - 1)], c = profile[Math.min(np - 1, k + 1)];
|
|
307
|
+
const dr = c[0] - a[0], dy = c[1] - a[1];
|
|
308
|
+
const l = Math.hypot(dr, dy) || 1;
|
|
309
|
+
pn.push([dy / l, -dr / l]);
|
|
310
|
+
}
|
|
311
|
+
const vShares = arcShares(profile, 2, false);
|
|
312
|
+
const uShares = Array.from({ length: seg + 1 }, (_, i) => i / seg);
|
|
313
|
+
const at = (i, j, out) => {
|
|
314
|
+
const t = angle(i);
|
|
315
|
+
const [r, y] = profile[j];
|
|
316
|
+
out[0] = r * Math.sin(t);
|
|
317
|
+
out[1] = y;
|
|
318
|
+
out[2] = r * Math.cos(t);
|
|
319
|
+
if (o.warp)
|
|
320
|
+
o.warp(out, i, j);
|
|
321
|
+
return out;
|
|
322
|
+
};
|
|
323
|
+
const analytic = (i, j, out) => {
|
|
324
|
+
const t = angle(i);
|
|
325
|
+
out[0] = pn[j][0] * Math.sin(t);
|
|
326
|
+
out[1] = pn[j][1];
|
|
327
|
+
out[2] = pn[j][0] * Math.cos(t);
|
|
328
|
+
return out;
|
|
329
|
+
};
|
|
330
|
+
// A full turn wraps around (the seam vertices written twice, u 0 and 1); a partial range has seg + 1 columns.
|
|
331
|
+
let faces = gridSurface(b, full ? seg : seg + 1, np, at, payload, {
|
|
332
|
+
smooth: o.smooth ?? true,
|
|
333
|
+
wrapI: full,
|
|
334
|
+
u: uShares,
|
|
335
|
+
v: vShares,
|
|
336
|
+
...(o.warp ? {} : { normalAt: analytic }),
|
|
337
|
+
});
|
|
338
|
+
const s = new Facts();
|
|
339
|
+
const fan = o.capFan ?? true;
|
|
340
|
+
const cap = (j, part) => {
|
|
341
|
+
const [r, y] = profile[j];
|
|
342
|
+
if (r <= 1e-12)
|
|
343
|
+
return;
|
|
344
|
+
// The disc faces away from the profile's neighbouring point along y.
|
|
345
|
+
const other = profile[j === 0 ? 1 : np - 2];
|
|
346
|
+
const down = other[1] > y || (other[1] === y && j === 0);
|
|
347
|
+
const pts = [];
|
|
348
|
+
const count = full ? seg : seg + 1;
|
|
349
|
+
for (let i = 0; i < count; i++)
|
|
350
|
+
pts.push(at(i, j, [0, 0, 0]));
|
|
351
|
+
if (!full)
|
|
352
|
+
pts.push([0, y, 0]);
|
|
353
|
+
const ordered = faceNormal(pts)[1] * (down ? -1 : 1) >= 0 ? pts : [...pts].reverse();
|
|
354
|
+
writePolygon(b, ordered, payload, s, part, faces++, fan, 0, j);
|
|
355
|
+
};
|
|
356
|
+
if (o.capStart)
|
|
357
|
+
cap(0, 'start');
|
|
358
|
+
if (o.capEnd)
|
|
359
|
+
cap(np - 1, 'end');
|
|
360
|
+
if (!full && o.sides) {
|
|
361
|
+
for (const i of [0, seg]) {
|
|
362
|
+
const pts = [];
|
|
363
|
+
for (let j = 0; j < np; j++)
|
|
364
|
+
pts.push(at(i, j, [0, 0, 0]));
|
|
365
|
+
const yTop = profile[np - 1][1], yBottom = profile[0][1];
|
|
366
|
+
if (profile[np - 1][0] > 1e-12)
|
|
367
|
+
pts.push([0, yTop, 0]);
|
|
368
|
+
if (profile[0][0] > 1e-12)
|
|
369
|
+
pts.push([0, yBottom, 0]);
|
|
370
|
+
// The meridian at `from` faces −θ, the one at `to` faces +θ: order the polygon to match.
|
|
371
|
+
const nrm = faceNormal(pts);
|
|
372
|
+
const t = angle(i);
|
|
373
|
+
const want = i === 0 ? [-Math.cos(t), 0, Math.sin(t)] : [Math.cos(t), 0, -Math.sin(t)];
|
|
374
|
+
writePolygon(b, dot(nrm, want) * (to > from ? 1 : -1) >= 0 ? pts : [...pts].reverse(), payload, s, 'cut', faces++, false, 0, i === 0 ? 0 : 1);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return first;
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Rotation-minimising frames along a polyline (parallel transport): the first `u` is the direction across the
|
|
381
|
+
* tangent closest to perpendicular to `up` (u = up × t), then each next `u` is the previous one with its tangent part
|
|
382
|
+
* removed, so the frame does not twist. With `closed` the path's ends are neighbours for the tangents.
|
|
383
|
+
*/
|
|
384
|
+
export function pathFrames(path, up = [0, 1, 0], closed = false) {
|
|
385
|
+
const out = [];
|
|
386
|
+
const n = path.length;
|
|
387
|
+
let prev = null;
|
|
388
|
+
const across = (t) => {
|
|
389
|
+
let ref = up;
|
|
390
|
+
const ul = Math.sqrt(dot(up, up)) || 1;
|
|
391
|
+
if (Math.abs(dot(t, up) / ul) > 0.97)
|
|
392
|
+
ref = Math.abs(t[0]) < 0.9 ? [1, 0, 0] : [0, 0, 1];
|
|
393
|
+
return unit(cross(ref, t));
|
|
394
|
+
};
|
|
395
|
+
for (let k = 0; k < n; k++) {
|
|
396
|
+
const a = closed ? path[(k - 1 + n) % n] : path[Math.max(0, k - 1)];
|
|
397
|
+
const c = closed ? path[(k + 1) % n] : path[Math.min(n - 1, k + 1)];
|
|
398
|
+
const t = unit(sub(c, a));
|
|
399
|
+
let u;
|
|
400
|
+
if (!prev)
|
|
401
|
+
u = across(t);
|
|
402
|
+
else {
|
|
403
|
+
const d = dot(prev, t);
|
|
404
|
+
u = [prev[0] - t[0] * d, prev[1] - t[1] * d, prev[2] - t[2] * d];
|
|
405
|
+
if (dot(u, u) < 1e-12)
|
|
406
|
+
u = across(t);
|
|
407
|
+
unit(u);
|
|
408
|
+
}
|
|
409
|
+
out.push({ t, u, v: cross(t, u) });
|
|
410
|
+
prev = u;
|
|
411
|
+
}
|
|
412
|
+
return out;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* A smooth curve through `points` (centripetal Catmull–Rom): `steps` samples per segment, the given points kept. With
|
|
416
|
+
* `closed` the curve returns to its start (the first point is not repeated at the end).
|
|
417
|
+
*/
|
|
418
|
+
export function smoothPath(points, steps, closed = false) {
|
|
419
|
+
const n = points.length;
|
|
420
|
+
const out = [];
|
|
421
|
+
if (n < 2 || steps <= 1) {
|
|
422
|
+
for (const p of points)
|
|
423
|
+
out.push([p[0], p[1], p[2]]);
|
|
424
|
+
return out;
|
|
425
|
+
}
|
|
426
|
+
const P = (k) => {
|
|
427
|
+
if (closed)
|
|
428
|
+
return points[((k % n) + n) % n];
|
|
429
|
+
if (k < 0) {
|
|
430
|
+
const a = points[0], b = points[1];
|
|
431
|
+
return [2 * a[0] - b[0], 2 * a[1] - b[1], 2 * a[2] - b[2]];
|
|
432
|
+
}
|
|
433
|
+
if (k >= n) {
|
|
434
|
+
const a = points[n - 1], b = points[n - 2];
|
|
435
|
+
return [2 * a[0] - b[0], 2 * a[1] - b[1], 2 * a[2] - b[2]];
|
|
436
|
+
}
|
|
437
|
+
return points[k];
|
|
438
|
+
};
|
|
439
|
+
const segs = closed ? n : n - 1;
|
|
440
|
+
for (let k = 0; k < segs; k++) {
|
|
441
|
+
const p0 = P(k - 1), p1 = P(k), p2 = P(k + 1), p3 = P(k + 2);
|
|
442
|
+
const knot = (a, c) => Math.max(1e-9, Math.sqrt(Math.sqrt(dot(sub(c, a), sub(c, a)))));
|
|
443
|
+
const t0 = 0, t1 = t0 + knot(p0, p1), t2 = t1 + knot(p1, p2), t3 = t2 + knot(p2, p3);
|
|
444
|
+
for (let s = 0; s < steps; s++) {
|
|
445
|
+
const t = t1 + ((t2 - t1) * s) / steps;
|
|
446
|
+
const q = [0, 0, 0];
|
|
447
|
+
for (let c = 0; c < 3; c++) {
|
|
448
|
+
const a1 = ((t1 - t) * p0[c] + (t - t0) * p1[c]) / (t1 - t0);
|
|
449
|
+
const a2 = ((t2 - t) * p1[c] + (t - t1) * p2[c]) / (t2 - t1);
|
|
450
|
+
const a3 = ((t3 - t) * p2[c] + (t - t2) * p3[c]) / (t3 - t2);
|
|
451
|
+
const b1 = ((t2 - t) * a1 + (t - t0) * a2) / (t2 - t0);
|
|
452
|
+
const b2 = ((t3 - t) * a2 + (t - t1) * a3) / (t3 - t1);
|
|
453
|
+
q[c] = ((t2 - t) * b1 + (t - t1) * b2) / (t2 - t1);
|
|
454
|
+
}
|
|
455
|
+
out.push(q);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (!closed) {
|
|
459
|
+
const l = points[n - 1];
|
|
460
|
+
out.push([l[0], l[1], l[2]]);
|
|
461
|
+
}
|
|
462
|
+
return out;
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Pulls a section ([x, y] in the frame u–v across the path) along a path of 3D points with rotation-minimising
|
|
466
|
+
* frames. A closed section given counter-clockwise in u–v faces outward; a clockwise one is turned around, so the
|
|
467
|
+
* faces always point away from the path. Side vertices report `i` the section point, `j` the path point, `u` the
|
|
468
|
+
* share along the section, `v` the share of the path length. Returns the index of the first vertex written.
|
|
469
|
+
*/
|
|
470
|
+
export function sweep(b, path, section, payload, o = {}) {
|
|
471
|
+
const first = b.vertexCount;
|
|
472
|
+
const pts = o.subdivide && o.subdivide > 1 ? smoothPath(path, o.subdivide, !!o.loop) : path.map((p) => [p[0], p[1], p[2]]);
|
|
473
|
+
const np = section.length;
|
|
474
|
+
if (pts.length < 2 || np < 2)
|
|
475
|
+
return first;
|
|
476
|
+
const closed = o.closed ?? true;
|
|
477
|
+
const loop = !!o.loop;
|
|
478
|
+
const frames = pathFrames(pts, o.up ?? [0, 1, 0], loop);
|
|
479
|
+
const along = arcShares(pts, 3, loop);
|
|
480
|
+
let area = 0;
|
|
481
|
+
for (let k = 0; k < np; k++) {
|
|
482
|
+
const a = section[k], c = section[(k + 1) % np];
|
|
483
|
+
area += a[0] * c[1] - c[0] * a[1];
|
|
484
|
+
}
|
|
485
|
+
const flip = closed && area < 0;
|
|
486
|
+
const at = (i, j, out) => {
|
|
487
|
+
const f = along[j];
|
|
488
|
+
const sc = o.scale ? o.scale(f) : 1;
|
|
489
|
+
const tw = o.twist ? o.twist(f) : 0;
|
|
490
|
+
const c = Math.cos(tw), s = Math.sin(tw);
|
|
491
|
+
const [x, y] = section[i];
|
|
492
|
+
const rx = (c * x - s * y) * sc, ry = (s * x + c * y) * sc;
|
|
493
|
+
const { u, v } = frames[j];
|
|
494
|
+
const p = pts[j];
|
|
495
|
+
for (let k = 0; k < 3; k++)
|
|
496
|
+
out[k] = p[k] + u[k] * rx + v[k] * ry;
|
|
497
|
+
if (o.warp)
|
|
498
|
+
o.warp(out, i, j);
|
|
499
|
+
return out;
|
|
500
|
+
};
|
|
501
|
+
let faces = gridSurface(b, np, pts.length, at, payload, {
|
|
502
|
+
smooth: o.smooth ?? true,
|
|
503
|
+
wrapI: closed,
|
|
504
|
+
wrapJ: loop,
|
|
505
|
+
flip,
|
|
506
|
+
u: arcShares(section, 2, closed),
|
|
507
|
+
v: along,
|
|
508
|
+
});
|
|
509
|
+
if (closed && !loop && o.caps) {
|
|
510
|
+
const s = new Facts();
|
|
511
|
+
for (const [j, part] of [[0, 'start'], [pts.length - 1, 'end']]) {
|
|
512
|
+
const ring = [];
|
|
513
|
+
for (let i = 0; i < np; i++)
|
|
514
|
+
ring.push(at(i, j, [0, 0, 0]));
|
|
515
|
+
// The start cap faces back along the path (−t), the end cap forward.
|
|
516
|
+
const want = frames[j].t;
|
|
517
|
+
const nrm = faceNormal(ring);
|
|
518
|
+
const forward = dot(nrm, want) >= 0;
|
|
519
|
+
const ordered = forward === (part === 'end') ? ring : ring.reverse();
|
|
520
|
+
writePolygon(b, ordered, payload, s, part, faces++, !!o.capFan, 0, j);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
return first;
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* An outline ([x, y] points, counter-clockwise seen from +z; a clockwise one is turned around) pushed from z = z0 to
|
|
527
|
+
* z = z1. Each side face is a flat quad with corners (p, z0) (q, z0) (q, z1) (p, z1) for the edge p → q, reported as
|
|
528
|
+
* `corner` 0–3, with `i` the outline point, `j` 0 at z0 and 1 at z1, `u` the share along the outline and `v` 0 or 1.
|
|
529
|
+
* To extrude upward from a ground outline, push a rotation (+z onto +y) first. Returns the first vertex index.
|
|
530
|
+
*/
|
|
531
|
+
export function extrude(b, outline, z0, z1, payload, o = {}) {
|
|
532
|
+
const first = b.vertexCount;
|
|
533
|
+
let pts = outline;
|
|
534
|
+
if (pts.length < 3)
|
|
535
|
+
return first;
|
|
536
|
+
let area = 0;
|
|
537
|
+
for (let k = 0; k < pts.length; k++) {
|
|
538
|
+
const a = pts[k], c = pts[(k + 1) % pts.length];
|
|
539
|
+
area += a[0] * c[1] - c[0] * a[1];
|
|
540
|
+
}
|
|
541
|
+
if (area < 0)
|
|
542
|
+
pts = [...pts].reverse();
|
|
543
|
+
const lo = Math.min(z0, z1), hi = Math.max(z0, z1);
|
|
544
|
+
const n = pts.length;
|
|
545
|
+
const s = new Facts();
|
|
546
|
+
let faces = 0;
|
|
547
|
+
const shares = arcShares(pts, 2, true);
|
|
548
|
+
if (o.smooth) {
|
|
549
|
+
faces = gridSurface(b, n, 2, (i, j, out) => {
|
|
550
|
+
const p = pts[i];
|
|
551
|
+
out[0] = p[0];
|
|
552
|
+
out[1] = p[1];
|
|
553
|
+
out[2] = j === 0 ? lo : hi;
|
|
554
|
+
return out;
|
|
555
|
+
}, payload, { wrapI: true, u: shares, v: [0, 1] });
|
|
556
|
+
}
|
|
557
|
+
else {
|
|
558
|
+
for (let k = 0; k < n; k++) {
|
|
559
|
+
const p = pts[k], q = pts[(k + 1) % n];
|
|
560
|
+
const quad = [[p[0], p[1], lo], [q[0], q[1], lo], [q[0], q[1], hi], [p[0], p[1], hi]];
|
|
561
|
+
const nrm = faceNormal(quad);
|
|
562
|
+
const firstV = b.vertexCount;
|
|
563
|
+
const corners = [[k, 0], [k + 1, 0], [k + 1, 1], [k, 1]];
|
|
564
|
+
quad.forEach((c, m) => {
|
|
565
|
+
const [i, j] = corners[m];
|
|
566
|
+
b.vertex(c, nrm, payload(s.set(c, nrm, 'side', i, j, shares[i], j, faces, m)));
|
|
567
|
+
});
|
|
568
|
+
b.quad(firstV, firstV + 1, firstV + 2, firstV + 3);
|
|
569
|
+
faces++;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
const fan = !!o.capFan;
|
|
573
|
+
if (o.capEnd ?? true)
|
|
574
|
+
writePolygon(b, pts.map((p) => [p[0], p[1], hi]), payload, s, 'end', faces++, fan, 0, 1);
|
|
575
|
+
if (o.capStart ?? true)
|
|
576
|
+
writePolygon(b, pts.map((p) => [p[0], p[1], lo]).reverse(), payload, s, 'start', faces++, fan, 0, 0);
|
|
577
|
+
return first;
|
|
578
|
+
}
|
|
579
|
+
// --- Special cases ----------------------------------------------------------------------------------------------------
|
|
580
|
+
/**
|
|
581
|
+
* An axis-aligned box from `min` to `max`: an extrusion of the x–y rectangle along z, six flat faces (sides first,
|
|
582
|
+
* then +z, then −z). Returns the first vertex index.
|
|
583
|
+
*/
|
|
584
|
+
export function box(b, min, max, payload) {
|
|
585
|
+
const x0 = min[0], y0 = min[1], x1 = max[0], y1 = max[1];
|
|
586
|
+
return extrude(b, [[x0, y0], [x1, y0], [x1, y1], [x0, y1]], min[2], max[2], payload);
|
|
587
|
+
}
|
|
588
|
+
/** A cylinder (or cone frustum) about +y from y0 to y1: a revolution of a two-point profile. */
|
|
589
|
+
export function cylinder(b, radius, y0, y1, payload, o = {}) {
|
|
590
|
+
return revolve(b, [[radius, y0], [o.radiusTop ?? radius, y1]], payload, {
|
|
591
|
+
segments: o.segments ?? 24,
|
|
592
|
+
smooth: o.smooth ?? true,
|
|
593
|
+
capStart: o.capStart ?? true,
|
|
594
|
+
capEnd: o.capEnd ?? true,
|
|
595
|
+
});
|
|
596
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
/** Attribute locations: those of the height field mesh, with the packed position in place of `a_pos`. */
|
|
3
|
+
export declare const BISECT_SURFACE_ATTRIBS: {
|
|
4
|
+
readonly a_packed: 0;
|
|
5
|
+
readonly a_normal: 1;
|
|
6
|
+
};
|
|
7
|
+
/** Packed position 4× int16; normal 3× int16 (not normalized, ×32767) and the kind (0 surface, 1 cut) as the fourth. */
|
|
8
|
+
export declare const BISECT_SURFACE_LAYOUT: VertexLayout;
|
|
9
|
+
export interface BisectSurfaceOptions {
|
|
10
|
+
/** Quads along one side of the square: a power of two from 2 to 256. */
|
|
11
|
+
quads: number;
|
|
12
|
+
/** Edge of the square in metres. */
|
|
13
|
+
edgeM: number;
|
|
14
|
+
/** Largest height difference in metres between the mesh and the grid. 0 drops only grid points that lie exactly on the mesh without them. */
|
|
15
|
+
toleranceM: number;
|
|
16
|
+
/** Metres per vertical step and the height of step 0 (as `packFrame`). */
|
|
17
|
+
stepY: number;
|
|
18
|
+
heightOrigin: number;
|
|
19
|
+
/** How far the skirt hangs below the surface, in metres. 0 = no skirt. */
|
|
20
|
+
skirtM: number;
|
|
21
|
+
/**
|
|
22
|
+
* Per side (low x, high x, low z, high z): `null` = an ordinary skirt; a height in metres = this side is a cut
|
|
23
|
+
* through the ground, its strip reaches down to that height.
|
|
24
|
+
*/
|
|
25
|
+
cuts?: readonly (number | null)[];
|
|
26
|
+
/**
|
|
27
|
+
* The part of the square that exists, in metres from its corner: `[minX, minZ, maxX, maxZ]`. Vertices outside are
|
|
28
|
+
* moved onto its border, triangles left without area are dropped. The heights of grid points outside should be
|
|
29
|
+
* those of the border point they move to.
|
|
30
|
+
*/
|
|
31
|
+
clip?: readonly [number, number, number, number];
|
|
32
|
+
}
|
|
33
|
+
export interface BisectSurface {
|
|
34
|
+
mesh: MeshData;
|
|
35
|
+
/** Triangles of the surface (without the skirt), and how many the full grid would have. */
|
|
36
|
+
triangles: number;
|
|
37
|
+
gridTriangles: number;
|
|
38
|
+
/** Largest and smallest height among the surface vertices, in metres. */
|
|
39
|
+
minY: number;
|
|
40
|
+
maxY: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Mesh of a square height grid within `toleranceM`. `heights` holds `(quads + 3)²` samples, row by row along x, with
|
|
44
|
+
* one extra sample all round: sample `(i + 1, j + 1)` is the grid point `(i, j)`; the margin is used for normals only,
|
|
45
|
+
* so squares that share an edge and sample the same ground agree on the normals there.
|
|
46
|
+
*/
|
|
47
|
+
export declare function bisectSurface(heights: Float32Array, o: BisectSurfaceOptions): BisectSurface;
|