wallhammer-web 0.0.0-stage → 0.4.1
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 +92 -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,205 @@
|
|
|
1
|
+
// Packed positions: a mesh whose positions are 16-bit steps from the origin of the cell it belongs to, and the
|
|
2
|
+
// material rewrite that reads them. A cell of a streamed world is small against the world, so 16 bits per axis give
|
|
3
|
+
// centimetres where a float position spends 32 bits on the distance from the world origin. A mesh without indices
|
|
4
|
+
// also gets an index: equal vertices are stored once.
|
|
5
|
+
//
|
|
6
|
+
// The horizontal step halves with every quadtree level (`PACK_STEPS_PER_EDGE` steps per cell edge, so a position may
|
|
7
|
+
// reach two edges past the cell on either side); the vertical step and the height origin are the same on every
|
|
8
|
+
// level. The vertex shader gets them as `u_pack` (see `packUniform`) and takes the level and the cell origin from
|
|
9
|
+
// what the streamed world sets per cell (`u_cellLevel`, `u_cellOffset`).
|
|
10
|
+
/** Name of the packed position attribute: four int16, x y z in steps and one spare. */
|
|
11
|
+
export const PACKED_POSITION = 'a_packed';
|
|
12
|
+
/** Horizontal position steps per cell edge. */
|
|
13
|
+
export const PACK_STEPS_PER_EDGE = 16384;
|
|
14
|
+
const POSITION = 'a_pos';
|
|
15
|
+
const I16_MIN = -32768;
|
|
16
|
+
const I16_MAX = 32767;
|
|
17
|
+
/** The frame of a cell: `edgeM` is its edge, `origin` its corner, `heightOrigin` and `stepY` are the world's. */
|
|
18
|
+
export function packFrame(edgeM, origin, heightOrigin, stepY) {
|
|
19
|
+
return { stepXZ: edgeM / PACK_STEPS_PER_EDGE, stepY, origin: [origin[0], origin[1] + heightOrigin, origin[2]] };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Value of the uniform `u_pack` for materials made by `packedMaterial`: `rootEdgeM` is the edge of a level 0 cell,
|
|
23
|
+
* `stepY` and `heightOrigin` as given to `packFrame`.
|
|
24
|
+
*/
|
|
25
|
+
export function packUniform(rootEdgeM, stepY, heightOrigin) {
|
|
26
|
+
return { vec4: [rootEdgeM / PACK_STEPS_PER_EDGE, stepY, heightOrigin, 0] };
|
|
27
|
+
}
|
|
28
|
+
/** The layout `packMesh` gives a mesh of `layout`: the float position becomes `PACKED_POSITION`, the rest moves up. */
|
|
29
|
+
export function packedLayout(layout) {
|
|
30
|
+
const pos = positionOf(layout);
|
|
31
|
+
const attribs = layout.attribs.map((a) => {
|
|
32
|
+
if (a === pos)
|
|
33
|
+
return { name: PACKED_POSITION, size: 4, type: 'i16', offset: a.offset };
|
|
34
|
+
return a.offset > pos.offset ? { ...a, offset: a.offset - 4 } : a;
|
|
35
|
+
});
|
|
36
|
+
return { stride: layout.stride - 4, attribs };
|
|
37
|
+
}
|
|
38
|
+
function positionOf(layout) {
|
|
39
|
+
const pos = layout.attribs.find((a) => a.name === POSITION);
|
|
40
|
+
if (!pos || pos.type !== 'f32' || pos.size !== 3)
|
|
41
|
+
throw new Error(`packMesh: the layout needs "${POSITION}" as three floats`);
|
|
42
|
+
return pos;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Packs a mesh: positions as 16-bit steps of `frame`, every other attribute byte for byte. A mesh without indices is
|
|
46
|
+
* indexed (equal packed vertices once, in the order of their first use); a mesh with indices keeps its vertices and
|
|
47
|
+
* their order. Indices are 16-bit when the vertices allow it. The result depends only on the input bytes and the
|
|
48
|
+
* frame, so one range packed alone gives the bytes it has inside the whole mesh.
|
|
49
|
+
*/
|
|
50
|
+
export function packMesh(mesh, frame, options = {}) {
|
|
51
|
+
const layout = mesh.layout;
|
|
52
|
+
const pos = positionOf(layout);
|
|
53
|
+
const inStride = layout.stride;
|
|
54
|
+
const outStride = inStride - 4;
|
|
55
|
+
const src = new Uint8Array(mesh.vertices.buffer, mesh.vertices.byteOffset, mesh.vertices.byteLength);
|
|
56
|
+
const inCount = Math.floor(src.length / inStride);
|
|
57
|
+
const srcView = new DataView(src.buffer, src.byteOffset, src.byteLength);
|
|
58
|
+
const out = new Uint8Array(inCount * outStride);
|
|
59
|
+
const outView = new DataView(out.buffer);
|
|
60
|
+
const head = pos.offset;
|
|
61
|
+
const tail = inStride - head - 12;
|
|
62
|
+
let clamped = 0;
|
|
63
|
+
const q = (v, origin, step) => {
|
|
64
|
+
const s = Math.round((v - origin) / step);
|
|
65
|
+
if (s < I16_MIN || s > I16_MAX) {
|
|
66
|
+
clamped++;
|
|
67
|
+
return s < I16_MIN ? I16_MIN : I16_MAX;
|
|
68
|
+
}
|
|
69
|
+
return s;
|
|
70
|
+
};
|
|
71
|
+
/** Writes input vertex `v` packed as output vertex `at`. */
|
|
72
|
+
const write = (v, at) => {
|
|
73
|
+
const i = v * inStride;
|
|
74
|
+
const o = at * outStride;
|
|
75
|
+
if (head > 0)
|
|
76
|
+
out.set(src.subarray(i, i + head), o);
|
|
77
|
+
outView.setInt16(o + head, q(srcView.getFloat32(i + head, true), frame.origin[0], frame.stepXZ), true);
|
|
78
|
+
outView.setInt16(o + head + 2, q(srcView.getFloat32(i + head + 4, true), frame.origin[1], frame.stepY), true);
|
|
79
|
+
outView.setInt16(o + head + 4, q(srcView.getFloat32(i + head + 8, true), frame.origin[2], frame.stepXZ), true);
|
|
80
|
+
outView.setInt16(o + head + 6, 0, true);
|
|
81
|
+
if (tail > 0)
|
|
82
|
+
out.set(src.subarray(i + head + 12, i + inStride), o + head + 8);
|
|
83
|
+
};
|
|
84
|
+
const bounds = options.spans ? Array.from(options.spans) : [0, inCount];
|
|
85
|
+
const outSpans = new Uint32Array(bounds.length);
|
|
86
|
+
const packedLayoutOut = packedLayout(layout);
|
|
87
|
+
if (mesh.indices) {
|
|
88
|
+
for (let v = 0; v < inCount; v++)
|
|
89
|
+
write(v, v);
|
|
90
|
+
outSpans.set(bounds);
|
|
91
|
+
const indices = inCount <= 65536 && !(mesh.indices instanceof Uint16Array) ? Uint16Array.from(mesh.indices) : mesh.indices;
|
|
92
|
+
return { mesh: { layout: packedLayoutOut, vertices: out, indices, count: mesh.count, ...(mesh.primitive ? { primitive: mesh.primitive } : {}) }, spans: outSpans, clamped };
|
|
93
|
+
}
|
|
94
|
+
// Without indices: every vertex is packed at the end of the output, then looked up among the vertices its range
|
|
95
|
+
// already has (an open hash table over the packed bytes); a new one stays, a known one is dropped again.
|
|
96
|
+
const index = new Uint32Array(inCount);
|
|
97
|
+
let size = 16;
|
|
98
|
+
while (size < inCount * 2)
|
|
99
|
+
size *= 2;
|
|
100
|
+
const table = new Int32Array(size).fill(-1);
|
|
101
|
+
const mask = size - 1;
|
|
102
|
+
let outCount = 0;
|
|
103
|
+
let spanStart = 0;
|
|
104
|
+
const same = (a, b) => {
|
|
105
|
+
const oa = a * outStride;
|
|
106
|
+
const ob = b * outStride;
|
|
107
|
+
for (let k = 0; k < outStride; k++)
|
|
108
|
+
if (out[oa + k] !== out[ob + k])
|
|
109
|
+
return false;
|
|
110
|
+
return true;
|
|
111
|
+
};
|
|
112
|
+
for (let s = 0; s + 1 < bounds.length; s++) {
|
|
113
|
+
outSpans[s] = outCount;
|
|
114
|
+
spanStart = outCount;
|
|
115
|
+
const end = Math.min(inCount, bounds[s + 1]);
|
|
116
|
+
for (let v = Math.max(0, bounds[s]); v < end; v++) {
|
|
117
|
+
write(v, outCount);
|
|
118
|
+
// FNV-1a over the packed vertex.
|
|
119
|
+
let h = 2166136261;
|
|
120
|
+
const o = outCount * outStride;
|
|
121
|
+
for (let k = 0; k < outStride; k++)
|
|
122
|
+
h = Math.imul(h ^ out[o + k], 16777619);
|
|
123
|
+
let slot = (h >>> 0) & mask;
|
|
124
|
+
let found = -1;
|
|
125
|
+
for (;;) {
|
|
126
|
+
const t = table[slot];
|
|
127
|
+
if (t < 0)
|
|
128
|
+
break;
|
|
129
|
+
// Entries of earlier ranges stay in the table and are passed over.
|
|
130
|
+
if (t >= spanStart && same(t, outCount)) {
|
|
131
|
+
found = t;
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
slot = (slot + 1) & mask;
|
|
135
|
+
}
|
|
136
|
+
if (found < 0) {
|
|
137
|
+
table[slot] = outCount;
|
|
138
|
+
found = outCount++;
|
|
139
|
+
}
|
|
140
|
+
index[v] = found;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
outSpans[bounds.length - 1] = outCount;
|
|
144
|
+
const vertices = out.slice(0, outCount * outStride);
|
|
145
|
+
// The index covers the vertices inside the spans, in order; a mesh without spans is all of it.
|
|
146
|
+
const first = Math.max(0, bounds[0]);
|
|
147
|
+
const last = Math.min(inCount, bounds[bounds.length - 1]);
|
|
148
|
+
const used = index.subarray(first, last);
|
|
149
|
+
const indices = outCount <= 65536 ? Uint16Array.from(used) : Uint32Array.from(used);
|
|
150
|
+
return { mesh: { layout: packedLayoutOut, vertices, indices, count: indices.length, ...(mesh.primitive ? { primitive: mesh.primitive } : {}) }, spans: outSpans, clamped };
|
|
151
|
+
}
|
|
152
|
+
const POSITION_INPUT = `in vec3 ${POSITION};\n`;
|
|
153
|
+
const OFFSET_UNIFORM = 'uniform vec3 u_cellOffset;';
|
|
154
|
+
const LEVEL_UNIFORM = 'uniform float u_cellLevel;';
|
|
155
|
+
/** The vertex shader of one pass, reading the position from the packed attribute. */
|
|
156
|
+
function packedVs(vs, label) {
|
|
157
|
+
if (!vs.includes(POSITION_INPUT))
|
|
158
|
+
throw new Error(`packedMaterial: ${label} has no line "${POSITION_INPUT.trim()}"`);
|
|
159
|
+
// A shader that adds the cell offset itself keeps doing so; any other gets it added here.
|
|
160
|
+
const own = vs.includes(OFFSET_UNIFORM);
|
|
161
|
+
const lines = [
|
|
162
|
+
`in vec4 ${PACKED_POSITION};`,
|
|
163
|
+
'uniform vec4 u_pack;',
|
|
164
|
+
...(own ? [] : [OFFSET_UNIFORM]),
|
|
165
|
+
...(vs.includes(LEVEL_UNIFORM) ? [] : [LEVEL_UNIFORM]),
|
|
166
|
+
'vec3 whUnpackPosition() {',
|
|
167
|
+
' // A power of two: the division is exact.',
|
|
168
|
+
' float s = u_pack.x / float(1 << int(u_cellLevel));',
|
|
169
|
+
` vec3 p = vec3(${PACKED_POSITION}.x * s, ${PACKED_POSITION}.y * u_pack.y + u_pack.z, ${PACKED_POSITION}.z * s);`,
|
|
170
|
+
own ? ' return p;' : ' return p + u_cellOffset;',
|
|
171
|
+
'}',
|
|
172
|
+
`#define ${POSITION} whUnpackPosition()`,
|
|
173
|
+
'',
|
|
174
|
+
];
|
|
175
|
+
return vs.replace(POSITION_INPUT, lines.join('\n'));
|
|
176
|
+
}
|
|
177
|
+
const PLAIN_DEPTH_VS = `#include <header>
|
|
178
|
+
uniform mat4 u_lightRender;
|
|
179
|
+
in vec3 ${POSITION};
|
|
180
|
+
void main() {
|
|
181
|
+
gl_Position = u_lightRender * vec4(${POSITION}, 1.0);
|
|
182
|
+
}`;
|
|
183
|
+
const PLAIN_DEPTH_FS = `#version 300 es
|
|
184
|
+
precision highp float;
|
|
185
|
+
void main() {}`;
|
|
186
|
+
/**
|
|
187
|
+
* The material for meshes made by `packMesh`: every vertex shader reads `PACKED_POSITION` where it read `a_pos`
|
|
188
|
+
* (a line `in vec3 a_pos;` of its own is required), a `'depth'` pass becomes a depth program that does the same.
|
|
189
|
+
* Uniforms: `u_pack` (`packUniform`), and per cell `u_cellLevel` and `u_cellOffset` as the streamed world sets them.
|
|
190
|
+
* A shader that declares `u_cellOffset` itself is taken to add it itself: its `a_pos` stays relative to the cell.
|
|
191
|
+
*/
|
|
192
|
+
export function packedMaterial(desc) {
|
|
193
|
+
const passes = {};
|
|
194
|
+
for (const name of Object.keys(desc.passes)) {
|
|
195
|
+
const p = desc.passes[name];
|
|
196
|
+
if (p === 'depth')
|
|
197
|
+
passes[name] = { vs: packedVs(PLAIN_DEPTH_VS, `${desc.name} (${name})`), fs: PLAIN_DEPTH_FS };
|
|
198
|
+
else
|
|
199
|
+
passes[name] = { vs: packedVs(p.vs, `${desc.name} (${name})`), fs: p.fs };
|
|
200
|
+
}
|
|
201
|
+
const attribs = {};
|
|
202
|
+
for (const [name, loc] of Object.entries(desc.attribs))
|
|
203
|
+
attribs[name === POSITION ? PACKED_POSITION : name] = loc;
|
|
204
|
+
return { ...desc, name: `${desc.name}+packed`, attribs, passes };
|
|
205
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
import type { Vec3 } from '../base/math.ts';
|
|
3
|
+
/** Position 3× float (`a_pos`) and the table row as a float (`a_ref`). */
|
|
4
|
+
export declare const PRISM_LAYOUT: VertexLayout;
|
|
5
|
+
/** Attribute locations a material should give the prisms. */
|
|
6
|
+
export declare const PRISM_ATTRIBS: {
|
|
7
|
+
readonly a_pos: 0;
|
|
8
|
+
readonly a_ref: 1;
|
|
9
|
+
};
|
|
10
|
+
/** One prism: an outline (x0, z0, x1, z1, …, not closed, either winding), its bottom and top, and its table row. */
|
|
11
|
+
export interface PrismShape {
|
|
12
|
+
ring: ArrayLike<number>;
|
|
13
|
+
minY: number;
|
|
14
|
+
maxY: number;
|
|
15
|
+
/** Row of the table the prism takes its value from, 0 and up. */
|
|
16
|
+
ref: number;
|
|
17
|
+
}
|
|
18
|
+
export interface PrismMeshOptions {
|
|
19
|
+
/** Moves the walls outward by this many metres, so the shell stands clear of what it wraps. Default 0. */
|
|
20
|
+
growM?: number;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Triangles of a simple polygon by ear clipping: vertex numbers of the ring, three per triangle, each triangle with
|
|
24
|
+
* a positive area in the x–z plane. Points on a straight line are dropped. A ring that crosses itself is closed with
|
|
25
|
+
* a fan, so the result always covers something.
|
|
26
|
+
*/
|
|
27
|
+
export declare function triangulateRing(ring: ArrayLike<number>): number[];
|
|
28
|
+
/** One mesh of all the prisms (walls and lids, faces outward) and the box that holds it. */
|
|
29
|
+
export declare function prismMesh(shapes: readonly PrismShape[], o?: PrismMeshOptions): {
|
|
30
|
+
mesh: MeshData;
|
|
31
|
+
min: Vec3;
|
|
32
|
+
max: Vec3;
|
|
33
|
+
};
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
// Upright prisms over outlines, each carrying the number of a table row: shells that a layer colours by a value
|
|
2
|
+
// looked up per row. An outline is a simple polygon in the x–z plane (holes are not cut; the lid covers them).
|
|
3
|
+
// Walls and lid share vertices and carry no normals; the shader takes the face direction from screen derivatives.
|
|
4
|
+
/** Position 3× float (`a_pos`) and the table row as a float (`a_ref`). */
|
|
5
|
+
export const PRISM_LAYOUT = {
|
|
6
|
+
stride: 16,
|
|
7
|
+
attribs: [
|
|
8
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
9
|
+
{ name: 'a_ref', size: 1, type: 'f32', offset: 12 },
|
|
10
|
+
],
|
|
11
|
+
};
|
|
12
|
+
/** Attribute locations a material should give the prisms. */
|
|
13
|
+
export const PRISM_ATTRIBS = { a_pos: 0, a_ref: 1 };
|
|
14
|
+
/** Twice the signed area of a ring in the x–z plane. */
|
|
15
|
+
function area2(ring) {
|
|
16
|
+
const n = ring.length >> 1;
|
|
17
|
+
let a = 0;
|
|
18
|
+
for (let i = 0; i < n; i++) {
|
|
19
|
+
const j = (i + 1) % n;
|
|
20
|
+
a += ring[i * 2] * ring[j * 2 + 1] - ring[j * 2] * ring[i * 2 + 1];
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Triangles of a simple polygon by ear clipping: vertex numbers of the ring, three per triangle, each triangle with
|
|
26
|
+
* a positive area in the x–z plane. Points on a straight line are dropped. A ring that crosses itself is closed with
|
|
27
|
+
* a fan, so the result always covers something.
|
|
28
|
+
*/
|
|
29
|
+
export function triangulateRing(ring) {
|
|
30
|
+
const n = ring.length >> 1;
|
|
31
|
+
if (n < 3)
|
|
32
|
+
return [];
|
|
33
|
+
const forward = area2(ring) > 0;
|
|
34
|
+
const idx = [];
|
|
35
|
+
for (let i = 0; i < n; i++)
|
|
36
|
+
idx.push(forward ? i : n - 1 - i);
|
|
37
|
+
const x = (i) => ring[i * 2];
|
|
38
|
+
const z = (i) => ring[i * 2 + 1];
|
|
39
|
+
const cross = (a, b, c) => (x(b) - x(a)) * (z(c) - z(a)) - (z(b) - z(a)) * (x(c) - x(a));
|
|
40
|
+
const out = [];
|
|
41
|
+
let i = 0;
|
|
42
|
+
// Vertices tried in a row without finding an ear; a full round means the ring is not simple.
|
|
43
|
+
let tried = 0;
|
|
44
|
+
while (idx.length > 3 && tried < idx.length) {
|
|
45
|
+
const m = idx.length;
|
|
46
|
+
const at = i % m;
|
|
47
|
+
const a = idx[(at + m - 1) % m];
|
|
48
|
+
const b = idx[at];
|
|
49
|
+
const c = idx[(at + 1) % m];
|
|
50
|
+
const turn = cross(a, b, c);
|
|
51
|
+
let ear = turn > 1e-12;
|
|
52
|
+
if (ear) {
|
|
53
|
+
for (const p of idx) {
|
|
54
|
+
if (p === a || p === b || p === c)
|
|
55
|
+
continue;
|
|
56
|
+
// Strictly inside or on an edge of the candidate triangle: not an ear.
|
|
57
|
+
if (cross(a, b, p) >= 0 && cross(b, c, p) >= 0 && cross(c, a, p) >= 0) {
|
|
58
|
+
ear = false;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (ear || Math.abs(turn) <= 1e-12) {
|
|
64
|
+
if (ear)
|
|
65
|
+
out.push(a, b, c);
|
|
66
|
+
idx.splice(at, 1);
|
|
67
|
+
tried = 0;
|
|
68
|
+
i = at;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
i = at + 1;
|
|
72
|
+
tried++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (idx.length === 3) {
|
|
76
|
+
if (cross(idx[0], idx[1], idx[2]) > 1e-12)
|
|
77
|
+
out.push(idx[0], idx[1], idx[2]);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
for (let k = 1; k + 1 < idx.length; k++)
|
|
81
|
+
out.push(idx[0], idx[k], idx[k + 1]);
|
|
82
|
+
}
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
/** One mesh of all the prisms (walls and lids, faces outward) and the box that holds it. */
|
|
86
|
+
export function prismMesh(shapes, o = {}) {
|
|
87
|
+
const grow = o.growM ?? 0;
|
|
88
|
+
let vertexCount = 0;
|
|
89
|
+
for (const s of shapes)
|
|
90
|
+
vertexCount += (s.ring.length >> 1) * 2;
|
|
91
|
+
const v = new Float32Array(vertexCount * 4);
|
|
92
|
+
const idx = [];
|
|
93
|
+
const min = [Infinity, Infinity, Infinity];
|
|
94
|
+
const max = [-Infinity, -Infinity, -Infinity];
|
|
95
|
+
let base = 0;
|
|
96
|
+
for (const s of shapes) {
|
|
97
|
+
const n = s.ring.length >> 1;
|
|
98
|
+
if (n < 3)
|
|
99
|
+
continue;
|
|
100
|
+
// Walk the ring so that its area is positive: the outside is then to the right of every edge.
|
|
101
|
+
const forward = area2(s.ring) > 0;
|
|
102
|
+
const px = new Float64Array(n);
|
|
103
|
+
const pz = new Float64Array(n);
|
|
104
|
+
for (let i = 0; i < n; i++) {
|
|
105
|
+
const k = forward ? i : n - 1 - i;
|
|
106
|
+
px[i] = s.ring[k * 2];
|
|
107
|
+
pz[i] = s.ring[k * 2 + 1];
|
|
108
|
+
}
|
|
109
|
+
const flat = new Float32Array(n * 2);
|
|
110
|
+
for (let i = 0; i < n; i++) {
|
|
111
|
+
let x = px[i];
|
|
112
|
+
let z = pz[i];
|
|
113
|
+
if (grow !== 0) {
|
|
114
|
+
// Outward normals of the two edges at the vertex; their mitre keeps both walls `grow` away from the outline.
|
|
115
|
+
const p = (i + n - 1) % n;
|
|
116
|
+
const q = (i + 1) % n;
|
|
117
|
+
let ax = x - px[p];
|
|
118
|
+
let az = z - pz[p];
|
|
119
|
+
let bx = px[q] - x;
|
|
120
|
+
let bz = pz[q] - z;
|
|
121
|
+
const al = Math.hypot(ax, az) || 1;
|
|
122
|
+
const bl = Math.hypot(bx, bz) || 1;
|
|
123
|
+
ax /= al;
|
|
124
|
+
az /= al;
|
|
125
|
+
bx /= bl;
|
|
126
|
+
bz /= bl;
|
|
127
|
+
const mx = az + bz;
|
|
128
|
+
const mz = -ax - bx;
|
|
129
|
+
// 1 + cos of the turn; a near reversal would send the mitre far out, so it is capped.
|
|
130
|
+
const k = Math.max(0.4, 1 + (az * bz + ax * bx));
|
|
131
|
+
x += (mx / k) * grow;
|
|
132
|
+
z += (mz / k) * grow;
|
|
133
|
+
}
|
|
134
|
+
flat[i * 2] = x;
|
|
135
|
+
flat[i * 2 + 1] = z;
|
|
136
|
+
for (let t = 0; t < 2; t++) {
|
|
137
|
+
const w = (base + i * 2 + t) * 4;
|
|
138
|
+
v[w] = x;
|
|
139
|
+
v[w + 1] = t === 0 ? s.minY : s.maxY;
|
|
140
|
+
v[w + 2] = z;
|
|
141
|
+
v[w + 3] = s.ref;
|
|
142
|
+
}
|
|
143
|
+
if (x < min[0])
|
|
144
|
+
min[0] = x;
|
|
145
|
+
if (x > max[0])
|
|
146
|
+
max[0] = x;
|
|
147
|
+
if (z < min[2])
|
|
148
|
+
min[2] = z;
|
|
149
|
+
if (z > max[2])
|
|
150
|
+
max[2] = z;
|
|
151
|
+
}
|
|
152
|
+
if (s.minY < min[1])
|
|
153
|
+
min[1] = s.minY;
|
|
154
|
+
if (s.maxY > max[1])
|
|
155
|
+
max[1] = s.maxY;
|
|
156
|
+
for (let i = 0; i < n; i++) {
|
|
157
|
+
const j = (i + 1) % n;
|
|
158
|
+
const a = base + i * 2;
|
|
159
|
+
const b = base + j * 2;
|
|
160
|
+
// Bottom a, bottom b, top b, top a; the face looks to the right of the edge.
|
|
161
|
+
idx.push(a, b + 1, b, a, a + 1, b + 1);
|
|
162
|
+
}
|
|
163
|
+
// The lid looks up: a triangle with a positive area in x–z is turned over.
|
|
164
|
+
const lid = triangulateRing(flat);
|
|
165
|
+
for (let k = 0; k < lid.length; k += 3)
|
|
166
|
+
idx.push(base + lid[k] * 2 + 1, base + lid[k + 2] * 2 + 1, base + lid[k + 1] * 2 + 1);
|
|
167
|
+
base += n * 2;
|
|
168
|
+
}
|
|
169
|
+
if (base === 0) {
|
|
170
|
+
min.fill(0);
|
|
171
|
+
max.fill(0);
|
|
172
|
+
}
|
|
173
|
+
const vertices = base * 4 === v.length ? v : v.slice(0, base * 4);
|
|
174
|
+
const indices = base > 65535 ? Uint32Array.from(idx) : Uint16Array.from(idx);
|
|
175
|
+
return { mesh: { layout: PRISM_LAYOUT, vertices, indices, count: idx.length }, min, max };
|
|
176
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MeshData } from '../base/data.ts';
|
|
2
|
+
import type { RigDesc } from '../base/rig.ts';
|
|
3
|
+
/** The rig's parts as one mesh in `RIG_VERTEX_LAYOUT`. `far` leaves out the parts marked `nearOnly`. */
|
|
4
|
+
export declare function rigMesh(desc: RigDesc, far?: boolean): MeshData;
|
|
5
|
+
/**
|
|
6
|
+
* Contact shadow: one quad per segment in `RIG_VERTEX_LAYOUT`. Position holds the corner (±1, 0, ±1), the pivot holds
|
|
7
|
+
* the half width, half length and forward offset, the motion's third value the segment.
|
|
8
|
+
*/
|
|
9
|
+
export declare function rigShadowMesh(desc: RigDesc): MeshData;
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Meshes of a rig: every part's shape placed in segment space, with its pivot, motion, colour slot and finish written
|
|
2
|
+
// into each vertex, so one draw call poses all parts in the vertex shader. Two levels of detail (the far one leaves
|
|
3
|
+
// out `nearOnly` parts) and a flat contact shadow quad per segment.
|
|
4
|
+
import { RIG_BEND_AXIS, RIG_FINISH, RIG_MOTION, RIG_VERTEX_FLOATS, RIG_VERTEX_LAYOUT } from '../base/rig.js';
|
|
5
|
+
class Builder {
|
|
6
|
+
v = [];
|
|
7
|
+
idx = [];
|
|
8
|
+
head = [];
|
|
9
|
+
tail = [];
|
|
10
|
+
/** Per-part attributes shared by every vertex of the part. */
|
|
11
|
+
part(pivot, motion, amount, segment, slot, color, finish, phase, variant) {
|
|
12
|
+
this.head = [pivot[0], pivot[1], pivot[2], motion, amount, segment, slot];
|
|
13
|
+
this.tail = [color[0], color[1], color[2], finish, phase, variant];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A flat polygon (convex, counter-clockwise seen from outside). The rig's local axes (x right, y up, z forward) are
|
|
17
|
+
* left-handed once placed in the world, so the triangles are written in the reverse order to come out
|
|
18
|
+
* counter-clockwise there; the normal is taken from the listed order and points outward.
|
|
19
|
+
*/
|
|
20
|
+
face(p) {
|
|
21
|
+
const [a, b, c] = p;
|
|
22
|
+
const e1 = [b[0] - a[0], b[1] - a[1], b[2] - a[2]];
|
|
23
|
+
const e2 = [c[0] - a[0], c[1] - a[1], c[2] - a[2]];
|
|
24
|
+
let nx = e1[1] * e2[2] - e1[2] * e2[1];
|
|
25
|
+
let ny = e1[2] * e2[0] - e1[0] * e2[2];
|
|
26
|
+
let nz = e1[0] * e2[1] - e1[1] * e2[0];
|
|
27
|
+
const l = Math.hypot(nx, ny, nz) || 1;
|
|
28
|
+
nx /= l;
|
|
29
|
+
ny /= l;
|
|
30
|
+
nz /= l;
|
|
31
|
+
const base = this.v.length / RIG_VERTEX_FLOATS;
|
|
32
|
+
for (const q of p)
|
|
33
|
+
this.v.push(q[0], q[1], q[2], nx, ny, nz, ...this.head, ...this.tail);
|
|
34
|
+
for (let i = 1; i + 1 < p.length; i++)
|
|
35
|
+
this.idx.push(base, base + i + 1, base + i);
|
|
36
|
+
}
|
|
37
|
+
mesh() {
|
|
38
|
+
return { layout: RIG_VERTEX_LAYOUT, vertices: new Float32Array(this.v), indices: new Uint32Array(this.idx), count: this.idx.length };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function box(b, c, s) {
|
|
42
|
+
const [x0, x1] = [c[0] - s[0] / 2, c[0] + s[0] / 2];
|
|
43
|
+
const [y0, y1] = [c[1] - s[1] / 2, c[1] + s[1] / 2];
|
|
44
|
+
const [z0, z1] = [c[2] - s[2] / 2, c[2] + s[2] / 2];
|
|
45
|
+
b.face([[x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1]]);
|
|
46
|
+
b.face([[x1, y0, z0], [x0, y0, z0], [x0, y1, z0], [x1, y1, z0]]);
|
|
47
|
+
b.face([[x1, y0, z1], [x1, y0, z0], [x1, y1, z0], [x1, y1, z1]]);
|
|
48
|
+
b.face([[x0, y0, z0], [x0, y0, z1], [x0, y1, z1], [x0, y1, z0]]);
|
|
49
|
+
b.face([[x0, y1, z1], [x1, y1, z1], [x1, y1, z0], [x0, y1, z0]]);
|
|
50
|
+
b.face([[x0, y0, z0], [x1, y0, z0], [x1, y0, z1], [x0, y0, z1]]);
|
|
51
|
+
}
|
|
52
|
+
function frustum(b, at, r0, r1, h, sides, depth, cap) {
|
|
53
|
+
const ring = (r, y) => Array.from({ length: sides }, (_, i) => {
|
|
54
|
+
const a = ((i + 0.5) / sides) * Math.PI * 2;
|
|
55
|
+
return [at[0] + Math.cos(a) * r, at[1] + y, at[2] + Math.sin(a) * r * depth];
|
|
56
|
+
});
|
|
57
|
+
const lo = ring(r0, 0);
|
|
58
|
+
const hi = ring(r1, h);
|
|
59
|
+
for (let i = 0; i < sides; i++) {
|
|
60
|
+
const j = (i + 1) % sides;
|
|
61
|
+
b.face([lo[j], lo[i], hi[i], hi[j]]);
|
|
62
|
+
}
|
|
63
|
+
if (cap && r1 > 0)
|
|
64
|
+
b.face([...hi].reverse());
|
|
65
|
+
if (cap && r0 > 0)
|
|
66
|
+
b.face(lo);
|
|
67
|
+
}
|
|
68
|
+
function roller(b, c, r, w, sides) {
|
|
69
|
+
const ring = (x) => Array.from({ length: sides }, (_, i) => {
|
|
70
|
+
const a = ((i + 0.5) / sides) * Math.PI * 2;
|
|
71
|
+
return [x, c[1] + Math.cos(a) * r, c[2] + Math.sin(a) * r];
|
|
72
|
+
});
|
|
73
|
+
const l = ring(c[0] - w / 2);
|
|
74
|
+
const h = ring(c[0] + w / 2);
|
|
75
|
+
for (let i = 0; i < sides; i++) {
|
|
76
|
+
const j = (i + 1) % sides;
|
|
77
|
+
b.face([l[i], l[j], h[j], h[i]]);
|
|
78
|
+
}
|
|
79
|
+
b.face([...h]);
|
|
80
|
+
b.face([...l].reverse());
|
|
81
|
+
}
|
|
82
|
+
function rod(b, at, dir, len, t) {
|
|
83
|
+
const dl = Math.hypot(dir[0], dir[1], dir[2]) || 1;
|
|
84
|
+
const d = [dir[0] / dl, dir[1] / dl, dir[2] / dl];
|
|
85
|
+
// Two axes across the rod.
|
|
86
|
+
const up = Math.abs(d[1]) > 0.9 ? [1, 0, 0] : [0, 1, 0];
|
|
87
|
+
const ux = d[1] * up[2] - d[2] * up[1], uy = d[2] * up[0] - d[0] * up[2], uz = d[0] * up[1] - d[1] * up[0];
|
|
88
|
+
const ul = Math.hypot(ux, uy, uz);
|
|
89
|
+
const u = [(ux / ul) * t / 2, (uy / ul) * t / 2, (uz / ul) * t / 2];
|
|
90
|
+
const v = [(d[1] * u[2] - d[2] * u[1]), (d[2] * u[0] - d[0] * u[2]), (d[0] * u[1] - d[1] * u[0])];
|
|
91
|
+
const corner = (s, i) => {
|
|
92
|
+
const su = i === 0 || i === 3 ? -1 : 1;
|
|
93
|
+
const sv = i < 2 ? -1 : 1;
|
|
94
|
+
return [at[0] + d[0] * s + u[0] * su + v[0] * sv, at[1] + d[1] * s + u[1] * su + v[1] * sv, at[2] + d[2] * s + u[2] * su + v[2] * sv];
|
|
95
|
+
};
|
|
96
|
+
for (let i = 0; i < 4; i++) {
|
|
97
|
+
const j = (i + 1) % 4;
|
|
98
|
+
b.face([corner(0, i), corner(0, j), corner(len, j), corner(len, i)]);
|
|
99
|
+
}
|
|
100
|
+
b.face([corner(len, 0), corner(len, 1), corner(len, 2), corner(len, 3)]);
|
|
101
|
+
}
|
|
102
|
+
function addPart(b, p) {
|
|
103
|
+
// A link of a chain carries its axis in the kind: 7 + 0, 1 or 2.
|
|
104
|
+
const motion = p.motion === 'bend' ? RIG_MOTION.bend + RIG_BEND_AXIS[p.axis ?? 'y'] : RIG_MOTION[p.motion ?? 'fixed'];
|
|
105
|
+
b.part(p.pivot ?? p.at, motion, p.amount ?? 0, p.segment ?? 0, p.slot ?? -1, p.tint ?? [1, 1, 1], RIG_FINISH[p.finish ?? 'matte'], p.phase ?? 0, p.variant ?? -1);
|
|
106
|
+
const s = p.shape;
|
|
107
|
+
if (s.kind === 'box')
|
|
108
|
+
box(b, p.at, s.size);
|
|
109
|
+
else if (s.kind === 'frustum')
|
|
110
|
+
frustum(b, p.at, s.r0, s.r1, s.height, s.sides, s.depth ?? 1, s.cap ?? false);
|
|
111
|
+
else if (s.kind === 'roller')
|
|
112
|
+
roller(b, p.at, s.radius, s.width, s.sides);
|
|
113
|
+
else
|
|
114
|
+
rod(b, p.at, s.dir, s.length, s.thickness);
|
|
115
|
+
}
|
|
116
|
+
/** The rig's parts as one mesh in `RIG_VERTEX_LAYOUT`. `far` leaves out the parts marked `nearOnly`. */
|
|
117
|
+
export function rigMesh(desc, far = false) {
|
|
118
|
+
const b = new Builder();
|
|
119
|
+
for (const p of desc.parts)
|
|
120
|
+
if (!far || !p.nearOnly)
|
|
121
|
+
addPart(b, p);
|
|
122
|
+
return b.mesh();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Contact shadow: one quad per segment in `RIG_VERTEX_LAYOUT`. Position holds the corner (±1, 0, ±1), the pivot holds
|
|
126
|
+
* the half width, half length and forward offset, the motion's third value the segment.
|
|
127
|
+
*/
|
|
128
|
+
export function rigShadowMesh(desc) {
|
|
129
|
+
const b = new Builder();
|
|
130
|
+
desc.segments.forEach((s, i) => {
|
|
131
|
+
b.part(s.shadow, 0, 0, i, -1, [0, 0, 0], 0, 0, -1);
|
|
132
|
+
b.face([[-1, 0, 1], [1, 0, 1], [1, 0, -1], [-1, 0, -1]]);
|
|
133
|
+
});
|
|
134
|
+
return b.mesh();
|
|
135
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { MeshData, VertexLayout } from '../base/data.ts';
|
|
2
|
+
import type { CellId, LocalBounds } from '../gen-core/cell.ts';
|
|
3
|
+
import type { StripNet } from '../gen-core/strip.ts';
|
|
4
|
+
import type { StripSurface } from '../gen-core/strip-surface.ts';
|
|
5
|
+
/** Locations a material should give the attributes. */
|
|
6
|
+
export declare const STRIP_ATTRIBS: {
|
|
7
|
+
readonly a_pos: 0;
|
|
8
|
+
readonly a_coarseShift: 1;
|
|
9
|
+
readonly a_side: 2;
|
|
10
|
+
readonly a_strip: 3;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Position 3× float relative to the cell origin; `a_coarseShift`, metres along local up to the shape of the level one
|
|
14
|
+
* coarser; `a_side`, unit vector from the centre of the section to the vertex's edge (zero on the centre); `a_strip`:
|
|
15
|
+
* across a (half-widths), metres along the line, half-width of the section on the vertex's side in metres, index of
|
|
16
|
+
* the line.
|
|
17
|
+
*/
|
|
18
|
+
export declare const STRIP_LAYOUT: VertexLayout;
|
|
19
|
+
/**
|
|
20
|
+
* GLSL for a strip vertex shader: include `<header>`, then `<noise>` (for `metresPerPixelAt`), then this, after the
|
|
21
|
+
* morph code of the surface. `stripWiden` moves an edge vertex outward so the strip is at least `minPx` pixels wide;
|
|
22
|
+
* `grow` is the metres it added per side.
|
|
23
|
+
*
|
|
24
|
+
* vec3 p = a_pos + u_cellOffset;
|
|
25
|
+
* p += localUp(p) * (a_coarseShift * spherePatchMorph(p)); // or u_morphUp and cellMorph(p) on the plane
|
|
26
|
+
* float grow;
|
|
27
|
+
* p = stripWiden(p, a_side, a_strip, 1.6, grow);
|
|
28
|
+
*/
|
|
29
|
+
export declare const STRIP_GLSL = "\nvec3 stripWiden(vec3 p, vec3 side, vec4 strip, float minPx, out float grow) {\n grow = max(0.0, 0.5 * minPx * metresPerPixelAt(p) - strip.z);\n return p + side * (grow * min(abs(strip.x), 1.0));\n}\n";
|
|
30
|
+
/** Metres `stripWiden` adds to each side of a strip `halfWidthM` wide where a pixel covers `metresPerPixel`. */
|
|
31
|
+
export declare function stripGrow(halfWidthM: number, metresPerPixel: number, minPx: number): number;
|
|
32
|
+
export interface StripMeshOptions {
|
|
33
|
+
/** The coarsest level that draws the strips: it has nothing to blend into (`a_coarseShift` 0). */
|
|
34
|
+
coarsestLevel: number;
|
|
35
|
+
/** Metres a level lifts the strip above its own triangles at least. */
|
|
36
|
+
liftM: (level: number) => number;
|
|
37
|
+
}
|
|
38
|
+
export interface StripMesh {
|
|
39
|
+
mesh: MeshData;
|
|
40
|
+
/** Extent relative to `surface.origin(cell)`, at any blend factor, without the shader's widening. */
|
|
41
|
+
bounds: LocalBounds;
|
|
42
|
+
/** Per line: metres of its centre line this cell holds. */
|
|
43
|
+
lengthM: Float64Array;
|
|
44
|
+
}
|
|
45
|
+
export declare function stripMesh(net: StripNet, surface: StripSurface, cell: CellId, o: StripMeshOptions): StripMesh | null;
|