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
package/dist/base/rig.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// An instanced rig as plain data: rigid parts with pivots and a motion each, in one or two segments that follow the
|
|
2
|
+
// same path. The pose is not stored anywhere: a vertex shader computes it from the instance record (position,
|
|
3
|
+
// heading, distance travelled, speed, turn rate, seed, state) with closed-form functions. Types and layouts only;
|
|
4
|
+
// gen-mesh builds the part meshes, the render module draws them.
|
|
5
|
+
export const RIG_MOTION = { fixed: 0, spin: 1, 'spin-steer': 2, limb: 3, swing: 4, look: 5, wobble: 6, bend: 7 };
|
|
6
|
+
/** Index of a `bend` axis; the mesh writes the motion kind `RIG_MOTION.bend + RIG_BEND_AXIS[axis]`. */
|
|
7
|
+
export const RIG_BEND_AXIS = { x: 0, y: 1, z: 2 };
|
|
8
|
+
export const RIG_FINISH = { matte: 0, glass: 1, 'lamp-front': 2, 'lamp-rear': 3, glow: 4, tiles: 5 };
|
|
9
|
+
/**
|
|
10
|
+
* True when a description needs the extended pose (a 'free' gait or any `bend` part); such kinds are drawn by a
|
|
11
|
+
* second program, every other kind by the same program as before these options existed.
|
|
12
|
+
*/
|
|
13
|
+
export function rigExtended(desc) {
|
|
14
|
+
return desc.gait === 'free' || desc.parts.some((p) => p.motion === 'bend');
|
|
15
|
+
}
|
|
16
|
+
/** Vertex of a rig mesh: position, normal, pivot, motion (kind, amount, segment, colour slot), colour, extra (phase, variant, near only). */
|
|
17
|
+
export const RIG_VERTEX_LAYOUT = {
|
|
18
|
+
stride: 76,
|
|
19
|
+
attribs: [
|
|
20
|
+
{ name: 'a_pos', size: 3, type: 'f32', offset: 0 },
|
|
21
|
+
{ name: 'a_normal', size: 3, type: 'f32', offset: 12 },
|
|
22
|
+
{ name: 'a_pivot', size: 3, type: 'f32', offset: 24 },
|
|
23
|
+
{ name: 'a_motion', size: 4, type: 'f32', offset: 36 },
|
|
24
|
+
{ name: 'a_color', size: 4, type: 'f32', offset: 52 },
|
|
25
|
+
{ name: 'a_extra', size: 2, type: 'f32', offset: 68 },
|
|
26
|
+
],
|
|
27
|
+
};
|
|
28
|
+
export const RIG_VERTEX_FLOATS = 19;
|
|
29
|
+
/**
|
|
30
|
+
* One rig instance, `RIG_INSTANCE_FLOATS` floats:
|
|
31
|
+
* - 0–3: leading segment anchor x, y, z, slope (rise per metre forward)
|
|
32
|
+
* - 4–7: leading direction x, z, trailing direction x, z
|
|
33
|
+
* - 8–11: trailing segment anchor x, y, z, slope
|
|
34
|
+
* - 12–15: distance travelled (m), speed (m/s), turn rate (rad/s, positive right), seed 0–1
|
|
35
|
+
* - 16–19: state (0 moving, 1 standing, 2 parked; kept for the owner, no shader of the engine reads it), height scale,
|
|
36
|
+
* width scale, variant bits
|
|
37
|
+
* - 20–23: four colours, each 0xRRGGBB as a float
|
|
38
|
+
*/
|
|
39
|
+
export const RIG_INSTANCE_FLOATS = 24;
|
|
40
|
+
export const RIG_INSTANCE_LAYOUT = {
|
|
41
|
+
stride: RIG_INSTANCE_FLOATS * 4,
|
|
42
|
+
attribs: [
|
|
43
|
+
{ name: 'i_p0', size: 4, type: 'f32', offset: 0 },
|
|
44
|
+
{ name: 'i_dir', size: 4, type: 'f32', offset: 16 },
|
|
45
|
+
{ name: 'i_p1', size: 4, type: 'f32', offset: 32 },
|
|
46
|
+
{ name: 'i_move', size: 4, type: 'f32', offset: 48 },
|
|
47
|
+
{ name: 'i_look', size: 4, type: 'f32', offset: 64 },
|
|
48
|
+
{ name: 'i_colors', size: 4, type: 'f32', offset: 80 },
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
export const RIG_ATTRIBS = {
|
|
52
|
+
a_pos: 0,
|
|
53
|
+
a_normal: 1,
|
|
54
|
+
a_pivot: 2,
|
|
55
|
+
a_motion: 3,
|
|
56
|
+
a_color: 4,
|
|
57
|
+
a_extra: 5,
|
|
58
|
+
i_p0: 6,
|
|
59
|
+
i_dir: 7,
|
|
60
|
+
i_p1: 8,
|
|
61
|
+
i_move: 9,
|
|
62
|
+
i_look: 10,
|
|
63
|
+
i_colors: 11,
|
|
64
|
+
};
|
|
65
|
+
/** Packs an RGB colour (0–1 per channel) into one float for the instance record. */
|
|
66
|
+
export function packRgb(r, g, b) {
|
|
67
|
+
const q = (v) => Math.max(0, Math.min(255, Math.round(v * 255)));
|
|
68
|
+
return q(r) * 65536 + q(g) * 256 + q(b);
|
|
69
|
+
}
|
|
70
|
+
/** Writes instance `index` into a record buffer of `RIG_INSTANCE_FLOATS` floats per instance. */
|
|
71
|
+
export function writeRigInstance(out, index, r) {
|
|
72
|
+
const o = index * RIG_INSTANCE_FLOATS;
|
|
73
|
+
out[o] = r.x;
|
|
74
|
+
out[o + 1] = r.y;
|
|
75
|
+
out[o + 2] = r.z;
|
|
76
|
+
out[o + 3] = r.slope;
|
|
77
|
+
out[o + 4] = r.dirX;
|
|
78
|
+
out[o + 5] = r.dirZ;
|
|
79
|
+
out[o + 6] = r.dirX1;
|
|
80
|
+
out[o + 7] = r.dirZ1;
|
|
81
|
+
out[o + 8] = r.x1;
|
|
82
|
+
out[o + 9] = r.y1;
|
|
83
|
+
out[o + 10] = r.z1;
|
|
84
|
+
out[o + 11] = r.slope1;
|
|
85
|
+
out[o + 12] = r.distance;
|
|
86
|
+
out[o + 13] = r.speed;
|
|
87
|
+
out[o + 14] = r.turnRate;
|
|
88
|
+
out[o + 15] = r.seed;
|
|
89
|
+
out[o + 16] = r.state;
|
|
90
|
+
out[o + 17] = r.heightScale;
|
|
91
|
+
out[o + 18] = r.widthScale;
|
|
92
|
+
out[o + 19] = r.variants;
|
|
93
|
+
out[o + 20] = r.colors[0];
|
|
94
|
+
out[o + 21] = r.colors[1];
|
|
95
|
+
out[o + 22] = r.colors[2];
|
|
96
|
+
out[o + 23] = r.colors[3];
|
|
97
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Radians in one turn (2π rounded to a double). */
|
|
2
|
+
export declare const RAD_PER_TURN = 6.283185307179586;
|
|
3
|
+
/**
|
|
4
|
+
* Sine and cosine of an angle in turns, as `[sin, cos]` (written to `out` when given). The reduction to one turn is
|
|
5
|
+
* exact, so the result stays accurate for any number of whole turns.
|
|
6
|
+
*/
|
|
7
|
+
export declare function detSinCosTurns(turns: number, out?: [number, number]): [number, number];
|
|
8
|
+
/** Sine of an angle in turns. */
|
|
9
|
+
export declare function detSinTurns(turns: number): number;
|
|
10
|
+
/** Cosine of an angle in turns. */
|
|
11
|
+
export declare function detCosTurns(turns: number): number;
|
|
12
|
+
/**
|
|
13
|
+
* Sine and cosine of an angle in radians, as `[sin, cos]`. The absolute error stays near 1e-16 up to about a million
|
|
14
|
+
* radians (the reduced angle is good to about 1e-20, so a result close to zero keeps fewer digits as the angle
|
|
15
|
+
* grows); larger angles are still reproducible but lose accuracy, keep those in turns.
|
|
16
|
+
*/
|
|
17
|
+
export declare function detSinCos(radians: number, out?: [number, number]): [number, number];
|
|
18
|
+
/** Sine of an angle in radians. */
|
|
19
|
+
export declare function detSin(radians: number): number;
|
|
20
|
+
/** Cosine of an angle in radians. */
|
|
21
|
+
export declare function detCos(radians: number): number;
|
|
22
|
+
/** Arc tangent in radians, −π/2 … π/2. */
|
|
23
|
+
export declare function detAtan(v: number): number;
|
|
24
|
+
/** Angle of the vector (x, y) in radians, −π … π. Both arguments must be finite; (0, 0) gives 0. */
|
|
25
|
+
export declare function detAtan2(y: number, x: number): number;
|
|
26
|
+
/** Arc sine in radians, −π/2 … π/2. The argument is clamped to −1 … 1. */
|
|
27
|
+
export declare function detAsin(v: number): number;
|
|
28
|
+
/** Arc cosine in radians, 0 … π. The argument is clamped to −1 … 1. */
|
|
29
|
+
export declare function detAcos(v: number): number;
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// Reproducible trigonometry. `Math.sin` and its relatives are not defined exactly by IEEE 754 or by the language, so
|
|
2
|
+
// two machines (or Node and a browser) may return results that differ in the last bit. Everything here is built only
|
|
3
|
+
// from + − × ÷, `Math.sqrt`, `Math.floor`, `Math.round` and `Math.abs`, which are exact, in a fixed order of
|
|
4
|
+
// operations, so the same input gives the same 64 bits everywhere. The accuracy is that of the usual library kernels
|
|
5
|
+
// (about one unit in the last place); the point is that the error is the same on every machine.
|
|
6
|
+
//
|
|
7
|
+
// Angles are best kept in turns (1 turn = 360°): the fractional part of a turn count is taken without any rounding,
|
|
8
|
+
// so an angle that grows with time (`phase + t / period`) keeps its precision however many turns have passed.
|
|
9
|
+
/** Radians in one turn (2π rounded to a double). */
|
|
10
|
+
export const RAD_PER_TURN = 6.283185307179586;
|
|
11
|
+
const TURN_PER_RAD = 0.15915494309189535;
|
|
12
|
+
const HALF_PI = 1.5707963267948966;
|
|
13
|
+
const PI = 3.141592653589793;
|
|
14
|
+
/** 2 / π, and π / 2 split into a 33-bit head (so `k * PIO2_HI` is exact for |k| < 2^20) and its remainder. */
|
|
15
|
+
const TWO_OVER_PI = 0.6366197723675814;
|
|
16
|
+
const PIO2_HI = 1.5707963267341256;
|
|
17
|
+
const PIO2_LO = 6.077100506506192e-11;
|
|
18
|
+
/** Largest |radians| reduced with the split π / 2; beyond it the angle is reduced through turns (less accurate). */
|
|
19
|
+
const RAD_REDUCE_MAX = 1.5e6;
|
|
20
|
+
// Polynomial kernels on [−π/4, π/4] (minimax coefficients of the classic sine and cosine kernels).
|
|
21
|
+
const S1 = -1.66666666666666324348e-1;
|
|
22
|
+
const S2 = 8.33333333332248946124e-3;
|
|
23
|
+
const S3 = -1.98412698298579493134e-4;
|
|
24
|
+
const S4 = 2.75573137070700676789e-6;
|
|
25
|
+
const S5 = -2.50507602534068634195e-8;
|
|
26
|
+
const S6 = 1.58969099521155010221e-10;
|
|
27
|
+
const C1 = 4.16666666666666019037e-2;
|
|
28
|
+
const C2 = -1.38888888888741095749e-3;
|
|
29
|
+
const C3 = 2.48015872894767294178e-5;
|
|
30
|
+
const C4 = -2.75573143513906633035e-7;
|
|
31
|
+
const C5 = 2.08757232129817482790e-9;
|
|
32
|
+
const C6 = -1.13596475577881948265e-11;
|
|
33
|
+
function kernelSin(x) {
|
|
34
|
+
const z = x * x;
|
|
35
|
+
const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
|
|
36
|
+
return x + z * x * (S1 + z * r);
|
|
37
|
+
}
|
|
38
|
+
function kernelCos(x) {
|
|
39
|
+
const z = x * x;
|
|
40
|
+
const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
|
|
41
|
+
return 1 - (0.5 * z - z * r);
|
|
42
|
+
}
|
|
43
|
+
/** Writes sine and cosine of the kernel argument `x` (|x| ≤ π/4) moved by `quarter` quarter turns. */
|
|
44
|
+
function fromQuarter(quarter, x, out) {
|
|
45
|
+
const s = kernelSin(x);
|
|
46
|
+
const c = kernelCos(x);
|
|
47
|
+
switch (quarter & 3) {
|
|
48
|
+
case 0:
|
|
49
|
+
out[0] = s;
|
|
50
|
+
out[1] = c;
|
|
51
|
+
break;
|
|
52
|
+
case 1:
|
|
53
|
+
out[0] = c;
|
|
54
|
+
out[1] = -s;
|
|
55
|
+
break;
|
|
56
|
+
case 2:
|
|
57
|
+
out[0] = -s;
|
|
58
|
+
out[1] = -c;
|
|
59
|
+
break;
|
|
60
|
+
default:
|
|
61
|
+
out[0] = -c;
|
|
62
|
+
out[1] = s;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Sine and cosine of an angle in turns, as `[sin, cos]` (written to `out` when given). The reduction to one turn is
|
|
68
|
+
* exact, so the result stays accurate for any number of whole turns.
|
|
69
|
+
*/
|
|
70
|
+
export function detSinCosTurns(turns, out = [0, 0]) {
|
|
71
|
+
const f = turns - Math.floor(turns); // 0 … 1, no rounding
|
|
72
|
+
const quarter = Math.round(f * 4); // 0 … 4
|
|
73
|
+
return fromQuarter(quarter, (f - quarter * 0.25) * RAD_PER_TURN, out);
|
|
74
|
+
}
|
|
75
|
+
/** Sine of an angle in turns. */
|
|
76
|
+
export function detSinTurns(turns) {
|
|
77
|
+
return detSinCosTurns(turns)[0];
|
|
78
|
+
}
|
|
79
|
+
/** Cosine of an angle in turns. */
|
|
80
|
+
export function detCosTurns(turns) {
|
|
81
|
+
return detSinCosTurns(turns)[1];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Sine and cosine of an angle in radians, as `[sin, cos]`. The absolute error stays near 1e-16 up to about a million
|
|
85
|
+
* radians (the reduced angle is good to about 1e-20, so a result close to zero keeps fewer digits as the angle
|
|
86
|
+
* grows); larger angles are still reproducible but lose accuracy, keep those in turns.
|
|
87
|
+
*/
|
|
88
|
+
export function detSinCos(radians, out = [0, 0]) {
|
|
89
|
+
if (!(Math.abs(radians) < RAD_REDUCE_MAX))
|
|
90
|
+
return detSinCosTurns(radians * TURN_PER_RAD, out);
|
|
91
|
+
const quarter = Math.round(radians * TWO_OVER_PI);
|
|
92
|
+
return fromQuarter(quarter, radians - quarter * PIO2_HI - quarter * PIO2_LO, out);
|
|
93
|
+
}
|
|
94
|
+
/** Sine of an angle in radians. */
|
|
95
|
+
export function detSin(radians) {
|
|
96
|
+
return detSinCos(radians)[0];
|
|
97
|
+
}
|
|
98
|
+
/** Cosine of an angle in radians. */
|
|
99
|
+
export function detCos(radians) {
|
|
100
|
+
return detSinCos(radians)[1];
|
|
101
|
+
}
|
|
102
|
+
// Arc tangent: the argument is folded onto one of four reference points, the rest is an odd polynomial.
|
|
103
|
+
const ATAN_HI = [4.63647609000806093515e-1, 7.85398163397448278999e-1, 9.82793723247329054082e-1, 1.570796326794896558];
|
|
104
|
+
const ATAN_LO = [2.26987774529616870924e-17, 3.06161699786838301793e-17, 1.39033110312309984516e-17, 6.12323399573676603587e-17];
|
|
105
|
+
const T0 = 3.33333333333329318027e-1;
|
|
106
|
+
const T1 = -1.99999999998764832476e-1;
|
|
107
|
+
const T2 = 1.42857142725034663711e-1;
|
|
108
|
+
const T3 = -1.11111104054623557880e-1;
|
|
109
|
+
const T4 = 9.09088713343650656196e-2;
|
|
110
|
+
const T5 = -7.69187620504482999495e-2;
|
|
111
|
+
const T6 = 6.66107313738753120669e-2;
|
|
112
|
+
const T7 = -5.83357013379057348645e-2;
|
|
113
|
+
const T8 = 4.97687799461593236017e-2;
|
|
114
|
+
const T9 = -3.65315727442169155270e-2;
|
|
115
|
+
const T10 = 1.62858201153657823623e-2;
|
|
116
|
+
/** Arc tangent in radians, −π/2 … π/2. */
|
|
117
|
+
export function detAtan(v) {
|
|
118
|
+
if (v !== v)
|
|
119
|
+
return v;
|
|
120
|
+
const neg = v < 0;
|
|
121
|
+
let x = neg ? -v : v;
|
|
122
|
+
if (x > 1e18)
|
|
123
|
+
return neg ? -HALF_PI : HALF_PI;
|
|
124
|
+
let id = -1;
|
|
125
|
+
if (x >= 0.4375) {
|
|
126
|
+
if (x < 1.1875) {
|
|
127
|
+
if (x < 0.6875) {
|
|
128
|
+
id = 0;
|
|
129
|
+
x = (2 * x - 1) / (2 + x);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
id = 1;
|
|
133
|
+
x = (x - 1) / (x + 1);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else if (x < 2.4375) {
|
|
137
|
+
id = 2;
|
|
138
|
+
x = (x - 1.5) / (1 + 1.5 * x);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
id = 3;
|
|
142
|
+
x = -1 / x;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const z = x * x;
|
|
146
|
+
const w = z * z;
|
|
147
|
+
const s1 = z * (T0 + w * (T2 + w * (T4 + w * (T6 + w * (T8 + w * T10)))));
|
|
148
|
+
const s2 = w * (T1 + w * (T3 + w * (T5 + w * (T7 + w * T9))));
|
|
149
|
+
const r = id < 0 ? x - x * (s1 + s2) : ATAN_HI[id] - (x * (s1 + s2) - ATAN_LO[id] - x);
|
|
150
|
+
return neg ? -r : r;
|
|
151
|
+
}
|
|
152
|
+
/** Angle of the vector (x, y) in radians, −π … π. Both arguments must be finite; (0, 0) gives 0. */
|
|
153
|
+
export function detAtan2(y, x) {
|
|
154
|
+
if (x > 0)
|
|
155
|
+
return detAtan(y / x);
|
|
156
|
+
if (x < 0)
|
|
157
|
+
return y >= 0 ? detAtan(y / x) + PI : detAtan(y / x) - PI;
|
|
158
|
+
return y > 0 ? HALF_PI : y < 0 ? -HALF_PI : 0;
|
|
159
|
+
}
|
|
160
|
+
/** Arc sine in radians, −π/2 … π/2. The argument is clamped to −1 … 1. */
|
|
161
|
+
export function detAsin(v) {
|
|
162
|
+
const x = v < -1 ? -1 : v > 1 ? 1 : v;
|
|
163
|
+
return detAtan2(x, Math.sqrt((1 - x) * (1 + x)));
|
|
164
|
+
}
|
|
165
|
+
/** Arc cosine in radians, 0 … π. The argument is clamped to −1 … 1. */
|
|
166
|
+
export function detAcos(v) {
|
|
167
|
+
const x = v < -1 ? -1 : v > 1 ? 1 : v;
|
|
168
|
+
return detAtan2(Math.sqrt((1 - x) * (1 + x)), x);
|
|
169
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Vec3 } from './math.ts';
|
|
2
|
+
/** Up points away from `center` (world metres, double); `radiusM` is the radius of the base surface around it. */
|
|
3
|
+
export interface RadialUp {
|
|
4
|
+
readonly center: readonly [number, number, number];
|
|
5
|
+
readonly radiusM: number;
|
|
6
|
+
}
|
|
7
|
+
/** Three unit axes of a local frame, as world directions: `y` is up, `x` and `z` span the horizon. */
|
|
8
|
+
export interface UpFrame {
|
|
9
|
+
x: Vec3;
|
|
10
|
+
y: Vec3;
|
|
11
|
+
z: Vec3;
|
|
12
|
+
}
|
|
13
|
+
/** Unit direction from `center` to `p` (+y when the two coincide). */
|
|
14
|
+
export declare function radialUpAt(center: readonly [number, number, number], p: readonly [number, number, number]): Vec3;
|
|
15
|
+
/** Height of `p` above the base surface of a radial up. */
|
|
16
|
+
export declare function radialHeight(up: RadialUp, p: readonly [number, number, number]): number;
|
|
17
|
+
/**
|
|
18
|
+
* The local frame for a unit up direction: the world axes turned by the shortest rotation that takes +y to `up`, so
|
|
19
|
+
* for up = +y it is the world's own axes. Exactly the shader's `localFrame`. Opposite to +y (within 1e-5 of the
|
|
20
|
+
* cosine) the shortest rotation is not defined and the frame is the world turned half round about x.
|
|
21
|
+
*
|
|
22
|
+
* A heading stored as two numbers (x, z), as instance records do, means `x · frame.x + z · frame.z` here.
|
|
23
|
+
*/
|
|
24
|
+
export declare function upFrame(up: readonly [number, number, number]): UpFrame;
|
|
25
|
+
/** A world direction as the (x, z) pair of a local frame (its part along up is dropped): the inverse of the note above. */
|
|
26
|
+
export declare function toFramePlane(frame: UpFrame, dir: readonly [number, number, number]): [number, number];
|
package/dist/base/up.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// "Up" that depends on the place. A flat world has one up, +y. A world wrapped round a point has an up that points
|
|
2
|
+
// away from that point (`RadialUp`); the renderer then hands the point to every shader, which asks `localUp(p)` and
|
|
3
|
+
// `localFrame(p)` instead of assuming +y. The functions here are the CPU side of the same definitions, so a position
|
|
4
|
+
// or a heading computed on the CPU and one computed in a shader agree.
|
|
5
|
+
/** Unit direction from `center` to `p` (+y when the two coincide). */
|
|
6
|
+
export function radialUpAt(center, p) {
|
|
7
|
+
const x = p[0] - center[0];
|
|
8
|
+
const y = p[1] - center[1];
|
|
9
|
+
const z = p[2] - center[2];
|
|
10
|
+
const l = Math.sqrt(x * x + y * y + z * z);
|
|
11
|
+
return l > 0 ? [x / l, y / l, z / l] : [0, 1, 0];
|
|
12
|
+
}
|
|
13
|
+
/** Height of `p` above the base surface of a radial up. */
|
|
14
|
+
export function radialHeight(up, p) {
|
|
15
|
+
const x = p[0] - up.center[0];
|
|
16
|
+
const y = p[1] - up.center[1];
|
|
17
|
+
const z = p[2] - up.center[2];
|
|
18
|
+
return Math.sqrt(x * x + y * y + z * z) - up.radiusM;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The local frame for a unit up direction: the world axes turned by the shortest rotation that takes +y to `up`, so
|
|
22
|
+
* for up = +y it is the world's own axes. Exactly the shader's `localFrame`. Opposite to +y (within 1e-5 of the
|
|
23
|
+
* cosine) the shortest rotation is not defined and the frame is the world turned half round about x.
|
|
24
|
+
*
|
|
25
|
+
* A heading stored as two numbers (x, z), as instance records do, means `x · frame.x + z · frame.z` here.
|
|
26
|
+
*/
|
|
27
|
+
export function upFrame(up) {
|
|
28
|
+
const [ux, uy, uz] = up;
|
|
29
|
+
if (uy < -0.99999)
|
|
30
|
+
return { x: [1, 0, 0], y: [0, -1, 0], z: [0, 0, -1] };
|
|
31
|
+
// k = 1 / (1 + uy). Below the horizon it is taken as (1 − uy) / (ux² + uz²), the same number for a unit vector:
|
|
32
|
+
// 1 + uy loses its digits near −y (an up that went through 32-bit floats would shear the frame by a percent).
|
|
33
|
+
const h = ux * ux + uz * uz;
|
|
34
|
+
const k = uy > 0 || h === 0 ? 1 / (1 + uy) : (1 - uy) / h;
|
|
35
|
+
return { x: [1 - ux * ux * k, -ux, -ux * uz * k], y: [ux, uy, uz], z: [-ux * uz * k, -uz, 1 - uz * uz * k] };
|
|
36
|
+
}
|
|
37
|
+
/** A world direction as the (x, z) pair of a local frame (its part along up is dropped): the inverse of the note above. */
|
|
38
|
+
export function toFramePlane(frame, dir) {
|
|
39
|
+
return [frame.x[0] * dir[0] + frame.x[1] * dir[1] + frame.x[2] * dir[2], frame.z[0] * dir[0] + frame.z[1] * dir[1] + frame.z[2] * dir[2]];
|
|
40
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { OrthoFrame, Vec3 } from './math.ts';
|
|
2
|
+
/** A ray in world space. */
|
|
3
|
+
export interface Ray {
|
|
4
|
+
origin: Vec3;
|
|
5
|
+
dir: Vec3;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Box-shaped visible volume: a rectangle in the plane spanned by `axes.right` and `axes.up`, unbounded along `axes.forward`.
|
|
9
|
+
* `cx`, `cy` are the rectangle centre measured along `right` and `up` from the world origin. This is what an orthographic
|
|
10
|
+
* view or a directional light sees.
|
|
11
|
+
*/
|
|
12
|
+
export interface CullBox {
|
|
13
|
+
kind: 'box';
|
|
14
|
+
axes: OrthoFrame;
|
|
15
|
+
cx: number;
|
|
16
|
+
cy: number;
|
|
17
|
+
halfW: number;
|
|
18
|
+
halfH: number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Convex volume bounded by planes, what a perspective view sees. A point p is inside when
|
|
22
|
+
* `n·p + d >= 0` for every plane `[nx, ny, nz, d]` (normals point inward, unit length).
|
|
23
|
+
*/
|
|
24
|
+
export interface CullFrustum {
|
|
25
|
+
kind: 'frustum';
|
|
26
|
+
planes: readonly (readonly [number, number, number, number])[];
|
|
27
|
+
}
|
|
28
|
+
/** Volume used to select what is worth drawing. */
|
|
29
|
+
export type CullVolume = CullBox | CullFrustum;
|
|
30
|
+
export interface View {
|
|
31
|
+
/** Drawing surface in physical pixels. */
|
|
32
|
+
readonly width: number;
|
|
33
|
+
readonly height: number;
|
|
34
|
+
/** Physical pixels per CSS pixel. */
|
|
35
|
+
readonly dpr: number;
|
|
36
|
+
/** Rotation of the view around the vertical axis in radians; 0 looks toward −z. */
|
|
37
|
+
readonly heading: number;
|
|
38
|
+
/** World size of one screen pixel at the point the view is centred on, in metres. */
|
|
39
|
+
readonly metresPerPixel: number;
|
|
40
|
+
/**
|
|
41
|
+
* How the pixel size grows with depth: at a point p the pixel measures
|
|
42
|
+
* `metresPerPixel + pixelGrowth * (forward·p − refDepth)` metres. Both are 0 for an orthographic view.
|
|
43
|
+
*/
|
|
44
|
+
readonly pixelGrowth: number;
|
|
45
|
+
readonly refDepth: number;
|
|
46
|
+
/**
|
|
47
|
+
* Unit axes of the view: screen right, screen up and the viewing direction. With `out` a rig may write into it (its
|
|
48
|
+
* three arrays) and return it instead of allocating; always use the returned frame, a rig may ignore `out`.
|
|
49
|
+
*/
|
|
50
|
+
axes(out?: OrthoFrame): OrthoFrame;
|
|
51
|
+
/**
|
|
52
|
+
* World → clip matrix with standard depth (clip z from −w at the near plane to +w at the far plane). The depth range
|
|
53
|
+
* covers a sphere of `depthHalfRange` around `sceneCenter`. In the reversed and logarithmic depth modes the renderer
|
|
54
|
+
* does not use this matrix for a perspective view: it builds its own from `axes`, `pixelGrowth`, `width`, `height`
|
|
55
|
+
* and the eye (the origin of `cornerRays`), see `depthViewProj`. A rig therefore needs no code per depth mode.
|
|
56
|
+
* With `out` (16 floats) a rig may write the matrix into it and return it; always use the returned array.
|
|
57
|
+
*/
|
|
58
|
+
viewProj(sceneCenter: Vec3, depthHalfRange: number, out?: Float32Array): Float32Array;
|
|
59
|
+
/**
|
|
60
|
+
* Rays through the four image corners (left-bottom, left-top, right-bottom, right-top). Used to fit the shadow map.
|
|
61
|
+
* For an orthographic view they are lines (the view sees both ways); with `pixelGrowth > 0` they start at the eye.
|
|
62
|
+
* With `out` (four rays, each with its own `origin` and `dir` arrays, see `newCornerRays`) a rig may write into it
|
|
63
|
+
* and return it instead of allocating; always use the returned rays.
|
|
64
|
+
*/
|
|
65
|
+
cornerRays(out?: Ray[]): Ray[];
|
|
66
|
+
/** What the view can see, for culling. */
|
|
67
|
+
cullVolume(): CullVolume;
|
|
68
|
+
/** Changes whenever the image of a static scene would change (position, orientation, zoom, size). */
|
|
69
|
+
key(): string;
|
|
70
|
+
}
|
|
71
|
+
/** A frame of three zero vectors to hand to `View.axes(out)` again and again. */
|
|
72
|
+
export declare function newOrthoFrame(): OrthoFrame;
|
|
73
|
+
/** Four rays with arrays of their own, to hand to `View.cornerRays(out)` again and again. */
|
|
74
|
+
export declare function newCornerRays(): Ray[];
|
|
75
|
+
/** An axis-aligned box. */
|
|
76
|
+
export interface Box3 {
|
|
77
|
+
min: Vec3;
|
|
78
|
+
max: Vec3;
|
|
79
|
+
}
|
|
80
|
+
/** Does an axis-aligned box reach into the volume? */
|
|
81
|
+
export declare function boxInVolume(b: Box3, v: CullVolume): boolean;
|
|
82
|
+
/** Distance from a point to an axis-aligned box (0 inside). */
|
|
83
|
+
export declare function distanceToBox(p: Vec3, b: Box3): number;
|
|
84
|
+
/**
|
|
85
|
+
* How the main passes (`color`, `data`, `overlay`) store depth. The shadow pass always uses standard depth.
|
|
86
|
+
* - `'standard'`: the usual depth buffer of the drawing surface. A perspective view is limited to a far : near ratio of
|
|
87
|
+
* a few thousand.
|
|
88
|
+
* - `'reversed'`: clip z runs from 1 at the near plane to 0 at infinity, in a 32-bit float depth buffer. Precision is
|
|
89
|
+
* then about the same relative to the distance at every distance, so one view holds a metre and a thousand
|
|
90
|
+
* kilometres. Needs the `EXT_clip_control` extension.
|
|
91
|
+
* - `'logarithmic'`: the fragment shader writes the logarithm of the distance as depth. Works everywhere, costs the
|
|
92
|
+
* early depth test. The fallback when `'reversed'` is asked for and the extension is missing.
|
|
93
|
+
*/
|
|
94
|
+
export type DepthMode = 'standard' | 'reversed' | 'logarithmic';
|
|
95
|
+
/** The projection of the main passes for one frame. */
|
|
96
|
+
export interface DepthProjection {
|
|
97
|
+
/** World → clip matrix, column-major. */
|
|
98
|
+
viewProj: Float32Array;
|
|
99
|
+
/**
|
|
100
|
+
* Logarithmic mode with a perspective view: `1 / log2(1 + far)`, so that `log2(1 + distance) * logScale` is the
|
|
101
|
+
* depth 0–1 the fragment shader writes. 0 in every other case (the depth of the matrix is used as it is).
|
|
102
|
+
*/
|
|
103
|
+
logScale: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* World → clip matrix of a view for a depth mode. `'standard'` returns the view's own matrix untouched.
|
|
107
|
+
*
|
|
108
|
+
* A perspective view (`pixelGrowth > 0`) in the other modes gets a matrix built here from the view's axes, pixel
|
|
109
|
+
* growth, size and eye, with the near plane at `nearM` and no far plane. Reversed: clip z = `nearM`, so depth is
|
|
110
|
+
* `nearM / distance`. Logarithmic: clip z only clips at the near plane; the scale of the logarithm comes from the far
|
|
111
|
+
* end of the sphere of `depthHalfRange` around `sceneCenter`, and what lies beyond it is drawn at depth 1.
|
|
112
|
+
*
|
|
113
|
+
* An orthographic view has linear depth, which needs no help: reversed mode flips its depth to 1 near, 0 far, and
|
|
114
|
+
* logarithmic mode leaves it as it is.
|
|
115
|
+
*/
|
|
116
|
+
export declare function depthViewProj(view: View, mode: DepthMode, sceneCenter: Vec3, depthHalfRange: number, nearM: number, out?: DepthProjection): DepthProjection;
|
|
117
|
+
/** Which axes a render origin follows: the two horizontal ones (its height stays 0), or all three. */
|
|
118
|
+
export type OriginAxes = 'horizontal' | 'all';
|
|
119
|
+
/**
|
|
120
|
+
* Render origin for a focus point (typically the eye or what it looks at): the grid point at or below the focus on
|
|
121
|
+
* every followed axis. Positions are kept in 64-bit numbers on the CPU and only their difference to this origin is
|
|
122
|
+
* handed to the GPU as 32-bit floats (`toRenderSpace`), so the numbers there stay below `gridM` near the focus
|
|
123
|
+
* wherever it is in the world. Because the origin depends only on the focus, the same place gives the same image
|
|
124
|
+
* however the focus got there; a coarse grid keeps patterns that repeat with the grid fixed to the world.
|
|
125
|
+
*/
|
|
126
|
+
export declare function snapRenderOrigin(focus: readonly [number, number, number], gridM: number, axes?: OriginAxes): Vec3;
|
|
127
|
+
/** A world position relative to a render origin. Subtract here, in 64 bits, never on the GPU. */
|
|
128
|
+
export declare function toRenderSpace(p: readonly [number, number, number], origin: readonly [number, number, number]): Vec3;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// What a renderer needs to know about a view, independent of the camera rig that produced it.
|
|
2
|
+
// Rigs (module camera) implement `View`, the renderer (module render) and the environment (module env) read it.
|
|
3
|
+
// Besides the types, two small pure tests on axis-aligned boxes that every culling layer shares, the projection of a
|
|
4
|
+
// view in each depth mode (`depthViewProj`) and the render origin that follows a focus point (`snapRenderOrigin`).
|
|
5
|
+
/** A frame of three zero vectors to hand to `View.axes(out)` again and again. */
|
|
6
|
+
export function newOrthoFrame() {
|
|
7
|
+
return { right: [0, 0, 0], up: [0, 1, 0], forward: [0, 0, -1] };
|
|
8
|
+
}
|
|
9
|
+
/** Four rays with arrays of their own, to hand to `View.cornerRays(out)` again and again. */
|
|
10
|
+
export function newCornerRays() {
|
|
11
|
+
const rays = [];
|
|
12
|
+
for (let i = 0; i < 4; i++)
|
|
13
|
+
rays.push({ origin: [0, 0, 0], dir: [0, 0, -1] });
|
|
14
|
+
return rays;
|
|
15
|
+
}
|
|
16
|
+
/** Does an axis-aligned box reach into the volume? */
|
|
17
|
+
export function boxInVolume(b, v) {
|
|
18
|
+
if (v.kind === 'frustum') {
|
|
19
|
+
for (const [nx, ny, nz, d] of v.planes) {
|
|
20
|
+
// The box corner farthest along the plane normal.
|
|
21
|
+
const px = nx >= 0 ? b.max[0] : b.min[0];
|
|
22
|
+
const py = ny >= 0 ? b.max[1] : b.min[1];
|
|
23
|
+
const pz = nz >= 0 ? b.max[2] : b.min[2];
|
|
24
|
+
if (nx * px + ny * py + nz * pz + d < 0)
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
const { right, up } = v.axes;
|
|
30
|
+
const cx = (b.min[0] + b.max[0]) / 2;
|
|
31
|
+
const cy = (b.min[1] + b.max[1]) / 2;
|
|
32
|
+
const cz = (b.min[2] + b.max[2]) / 2;
|
|
33
|
+
const hx = (b.max[0] - b.min[0]) / 2;
|
|
34
|
+
const hy = (b.max[1] - b.min[1]) / 2;
|
|
35
|
+
const hz = (b.max[2] - b.min[2]) / 2;
|
|
36
|
+
const rx = right[0] * cx + right[1] * cy + right[2] * cz - v.cx;
|
|
37
|
+
const rr = Math.abs(right[0]) * hx + Math.abs(right[1]) * hy + Math.abs(right[2]) * hz;
|
|
38
|
+
if (Math.abs(rx) > v.halfW + rr)
|
|
39
|
+
return false;
|
|
40
|
+
const uy = up[0] * cx + up[1] * cy + up[2] * cz - v.cy;
|
|
41
|
+
const ur = Math.abs(up[0]) * hx + Math.abs(up[1]) * hy + Math.abs(up[2]) * hz;
|
|
42
|
+
return Math.abs(uy) <= v.halfH + ur;
|
|
43
|
+
}
|
|
44
|
+
/** Distance from a point to an axis-aligned box (0 inside). */
|
|
45
|
+
export function distanceToBox(p, b) {
|
|
46
|
+
let s = 0;
|
|
47
|
+
for (let i = 0; i < 3; i++) {
|
|
48
|
+
const d = p[i] < b.min[i] ? b.min[i] - p[i] : p[i] > b.max[i] ? p[i] - b.max[i] : 0;
|
|
49
|
+
s += d * d;
|
|
50
|
+
}
|
|
51
|
+
return Math.sqrt(s);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* World → clip matrix of a view for a depth mode. `'standard'` returns the view's own matrix untouched.
|
|
55
|
+
*
|
|
56
|
+
* A perspective view (`pixelGrowth > 0`) in the other modes gets a matrix built here from the view's axes, pixel
|
|
57
|
+
* growth, size and eye, with the near plane at `nearM` and no far plane. Reversed: clip z = `nearM`, so depth is
|
|
58
|
+
* `nearM / distance`. Logarithmic: clip z only clips at the near plane; the scale of the logarithm comes from the far
|
|
59
|
+
* end of the sphere of `depthHalfRange` around `sceneCenter`, and what lies beyond it is drawn at depth 1.
|
|
60
|
+
*
|
|
61
|
+
* An orthographic view has linear depth, which needs no help: reversed mode flips its depth to 1 near, 0 far, and
|
|
62
|
+
* logarithmic mode leaves it as it is.
|
|
63
|
+
*/
|
|
64
|
+
export function depthViewProj(view, mode, sceneCenter, depthHalfRange, nearM, out) {
|
|
65
|
+
// With `out` its `viewProj` (16 floats) is written in place and `out` is returned: nothing is allocated by this
|
|
66
|
+
// function (the view may still allocate when its methods ignore their `out`).
|
|
67
|
+
const res = out ?? { viewProj: new Float32Array(16), logScale: 0 };
|
|
68
|
+
res.logScale = 0;
|
|
69
|
+
if (mode === 'standard') {
|
|
70
|
+
const vp = view.viewProj(sceneCenter, depthHalfRange, res.viewProj);
|
|
71
|
+
if (vp !== res.viewProj)
|
|
72
|
+
res.viewProj.set(vp);
|
|
73
|
+
return res;
|
|
74
|
+
}
|
|
75
|
+
if (!(view.pixelGrowth > 0)) {
|
|
76
|
+
const m = res.viewProj;
|
|
77
|
+
const vp = view.viewProj(sceneCenter, depthHalfRange, m);
|
|
78
|
+
if (vp !== m)
|
|
79
|
+
m.set(vp);
|
|
80
|
+
// z' = (w − z) / 2 on every column: −w…+w becomes 1…0 (w is 1 for a matrix without perspective).
|
|
81
|
+
if (mode === 'reversed')
|
|
82
|
+
for (let c = 0; c < 16; c += 4)
|
|
83
|
+
m[c + 2] = (m[c + 3] - m[c + 2]) / 2;
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
const { right: r, up: u, forward: w } = view.axes(scratchFrame);
|
|
87
|
+
const eye = view.cornerRays(scratchRays)[0].origin;
|
|
88
|
+
// pixelGrowth = 2 tan(fovY / 2) / height.
|
|
89
|
+
const fy = 2 / (view.pixelGrowth * view.height);
|
|
90
|
+
const fx = (fy * view.height) / view.width;
|
|
91
|
+
const re = r[0] * eye[0] + r[1] * eye[1] + r[2] * eye[2];
|
|
92
|
+
const ue = u[0] * eye[0] + u[1] * eye[1] + u[2] * eye[2];
|
|
93
|
+
const we = w[0] * eye[0] + w[1] * eye[1] + w[2] * eye[2];
|
|
94
|
+
const m = res.viewProj;
|
|
95
|
+
m.fill(0);
|
|
96
|
+
for (let i = 0; i < 3; i++) {
|
|
97
|
+
m[i * 4 + 0] = fx * r[i];
|
|
98
|
+
m[i * 4 + 1] = fy * u[i];
|
|
99
|
+
m[i * 4 + 3] = w[i];
|
|
100
|
+
}
|
|
101
|
+
m[12] = -fx * re;
|
|
102
|
+
m[13] = -fy * ue;
|
|
103
|
+
m[15] = -we;
|
|
104
|
+
if (mode === 'reversed') {
|
|
105
|
+
// Clip z is the constant `nearM`: after the division by w (the distance along the view) depth is nearM / distance.
|
|
106
|
+
m[14] = nearM;
|
|
107
|
+
return res;
|
|
108
|
+
}
|
|
109
|
+
// Clip z only clips here, the fragment shader writes the depth: z = distance − 2 nearM puts the near plane at nearM
|
|
110
|
+
// and leaves no far plane (a far plane a million times the near one would not survive 32-bit floats in the matrix).
|
|
111
|
+
for (let i = 0; i < 3; i++)
|
|
112
|
+
m[i * 4 + 2] = w[i];
|
|
113
|
+
m[14] = -we - 2 * nearM;
|
|
114
|
+
// The far end of the scene sets only the scale of the logarithm; whatever lies beyond it is drawn at depth 1.
|
|
115
|
+
const dc = w[0] * sceneCenter[0] + w[1] * sceneCenter[1] + w[2] * sceneCenter[2] - we;
|
|
116
|
+
const far = Math.max(dc + depthHalfRange, nearM * 2, 1);
|
|
117
|
+
res.logScale = 1 / Math.log2(1 + far);
|
|
118
|
+
return res;
|
|
119
|
+
}
|
|
120
|
+
/** Scratch for `depthViewProj` (single-threaded: read right after the call). */
|
|
121
|
+
const scratchFrame = newOrthoFrame();
|
|
122
|
+
const scratchRays = newCornerRays();
|
|
123
|
+
/**
|
|
124
|
+
* Render origin for a focus point (typically the eye or what it looks at): the grid point at or below the focus on
|
|
125
|
+
* every followed axis. Positions are kept in 64-bit numbers on the CPU and only their difference to this origin is
|
|
126
|
+
* handed to the GPU as 32-bit floats (`toRenderSpace`), so the numbers there stay below `gridM` near the focus
|
|
127
|
+
* wherever it is in the world. Because the origin depends only on the focus, the same place gives the same image
|
|
128
|
+
* however the focus got there; a coarse grid keeps patterns that repeat with the grid fixed to the world.
|
|
129
|
+
*/
|
|
130
|
+
export function snapRenderOrigin(focus, gridM, axes = 'horizontal') {
|
|
131
|
+
return [Math.floor(focus[0] / gridM) * gridM, axes === 'all' ? Math.floor(focus[1] / gridM) * gridM : 0, Math.floor(focus[2] / gridM) * gridM];
|
|
132
|
+
}
|
|
133
|
+
/** A world position relative to a render origin. Subtract here, in 64 bits, never on the GPU. */
|
|
134
|
+
export function toRenderSpace(p, origin) {
|
|
135
|
+
return [p[0] - origin[0], p[1] - origin[1], p[2] - origin[2]];
|
|
136
|
+
}
|