wallhammer-web 0.0.0-stage → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +88 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
// Skeletons, poses and bone masks as plain data. A skeleton is a list of bones, parents before children, each hung
|
|
2
|
+
// off its parent at a fixed joint. A pose holds per bone a local rotation (unit quaternion [x, y, z, w]) and an
|
|
3
|
+
// offset added to the joint; the rest pose is the identity rotation and a zero offset. A mask holds a weight 0–1 per
|
|
4
|
+
// bone. Nothing here knows what a skeleton stands for: the game names its bones and writes its own motions.
|
|
5
|
+
/** Bones in parent-before-child order, as typed arrays. Built once; shared by every figure of that build. */
|
|
6
|
+
export class Skeleton {
|
|
7
|
+
count;
|
|
8
|
+
names;
|
|
9
|
+
/** Parent of every bone, −1 for roots. */
|
|
10
|
+
parent;
|
|
11
|
+
/** Joint of every bone in its parent's frame, 3 numbers per bone. */
|
|
12
|
+
joint;
|
|
13
|
+
byName = new Map();
|
|
14
|
+
constructor(bones) {
|
|
15
|
+
const n = bones.length;
|
|
16
|
+
if (n > 32767)
|
|
17
|
+
throw new Error('Skeleton: too many bones');
|
|
18
|
+
this.count = n;
|
|
19
|
+
this.names = bones.map((b) => b.name);
|
|
20
|
+
this.parent = new Int16Array(n);
|
|
21
|
+
this.joint = new Float32Array(n * 3);
|
|
22
|
+
bones.forEach((b, i) => {
|
|
23
|
+
if (b.parent >= i || b.parent < -1)
|
|
24
|
+
throw new Error(`Skeleton: bone ${b.name} must come after its parent`);
|
|
25
|
+
if (this.byName.has(b.name))
|
|
26
|
+
throw new Error(`Skeleton: two bones named ${b.name}`);
|
|
27
|
+
this.byName.set(b.name, i);
|
|
28
|
+
this.parent[i] = b.parent;
|
|
29
|
+
this.joint[i * 3] = b.joint[0];
|
|
30
|
+
this.joint[i * 3 + 1] = b.joint[1];
|
|
31
|
+
this.joint[i * 3 + 2] = b.joint[2];
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/** Index of a bone by name; throws when there is none. */
|
|
35
|
+
index(name) {
|
|
36
|
+
const i = this.byName.get(name);
|
|
37
|
+
if (i === undefined)
|
|
38
|
+
throw new Error(`Skeleton: no bone ${name}`);
|
|
39
|
+
return i;
|
|
40
|
+
}
|
|
41
|
+
/** Index of a bone by name, or −1. */
|
|
42
|
+
find(name) {
|
|
43
|
+
return this.byName.get(name) ?? -1;
|
|
44
|
+
}
|
|
45
|
+
/** True when `bone` is `ancestor` or lies below it. */
|
|
46
|
+
isBelow(bone, ancestor) {
|
|
47
|
+
for (let b = bone; b >= 0; b = this.parent[b])
|
|
48
|
+
if (b === ancestor)
|
|
49
|
+
return true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** Local pose: per bone a rotation (quaternion x, y, z, w) and an offset added to the joint. Plain typed arrays. */
|
|
54
|
+
export class Pose {
|
|
55
|
+
skeleton;
|
|
56
|
+
/** 4 numbers per bone, [x, y, z, w]. */
|
|
57
|
+
rot;
|
|
58
|
+
/** 3 numbers per bone, added to the bone's joint. */
|
|
59
|
+
offset;
|
|
60
|
+
constructor(skeleton) {
|
|
61
|
+
this.skeleton = skeleton;
|
|
62
|
+
this.rot = new Float32Array(skeleton.count * 4);
|
|
63
|
+
this.offset = new Float32Array(skeleton.count * 3);
|
|
64
|
+
this.reset();
|
|
65
|
+
}
|
|
66
|
+
/** The rest pose: identity rotations, zero offsets. */
|
|
67
|
+
reset() {
|
|
68
|
+
const r = this.rot;
|
|
69
|
+
for (let i = 0; i < r.length; i += 4) {
|
|
70
|
+
r[i] = 0;
|
|
71
|
+
r[i + 1] = 0;
|
|
72
|
+
r[i + 2] = 0;
|
|
73
|
+
r[i + 3] = 1;
|
|
74
|
+
}
|
|
75
|
+
this.offset.fill(0);
|
|
76
|
+
}
|
|
77
|
+
copy(from) {
|
|
78
|
+
this.rot.set(from.rot);
|
|
79
|
+
this.offset.set(from.offset);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** A mask with `weight` on the named bones (and by default everything below them), 0 elsewhere. For setup. */
|
|
83
|
+
export function boneMask(skeleton, names, o = {}) {
|
|
84
|
+
const m = new Float32Array(skeleton.count);
|
|
85
|
+
if (o.from)
|
|
86
|
+
m.set(o.from);
|
|
87
|
+
const w = o.weight ?? 1;
|
|
88
|
+
const deep = o.descendants ?? true;
|
|
89
|
+
const picked = names.map((n) => skeleton.index(n));
|
|
90
|
+
for (let b = 0; b < skeleton.count; b++) {
|
|
91
|
+
for (const p of picked) {
|
|
92
|
+
if (b === p || (deep && skeleton.isBelow(b, p))) {
|
|
93
|
+
m[b] = w;
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return m;
|
|
99
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Byte arrays by key. Reads of a missing key give `null`; writes that fail are dropped. */
|
|
2
|
+
export interface ByteStore {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
5
|
+
put(key: string, bytes: Uint8Array): Promise<void>;
|
|
6
|
+
/** Deletes every key that does not start with `prefix` (what an older version of the content left behind). */
|
|
7
|
+
keepOnly(prefix: string): Promise<void>;
|
|
8
|
+
/** Deletes everything. */
|
|
9
|
+
clear(): Promise<void>;
|
|
10
|
+
/** Keys and bytes held, counted by reading every entry. */
|
|
11
|
+
usage(): Promise<{
|
|
12
|
+
keys: number;
|
|
13
|
+
bytes: number;
|
|
14
|
+
}>;
|
|
15
|
+
close(): void;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Opens (or creates) the store called `name`. Resolves to `null` where the browser has no storage for the page or
|
|
19
|
+
* refuses it; the caller then simply works without a store.
|
|
20
|
+
*/
|
|
21
|
+
export declare function openByteStore(name: string): Promise<ByteStore | null>;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// A store of byte arrays by name that outlives the page, kept in the browser's own storage (IndexedDB). It holds
|
|
2
|
+
// what is expensive to make and cheap to keep: for example finished cells of a streamed world, so the second visit
|
|
3
|
+
// does not build them again. Everything here may fail (private windows, a full disk, a browser without storage);
|
|
4
|
+
// a store that cannot be opened is `null` and every read of a store that broke later answers "not there".
|
|
5
|
+
const TABLE = 'bytes';
|
|
6
|
+
/** The longest an open may take, ms. */
|
|
7
|
+
const OPEN_WAIT_MS = 3000;
|
|
8
|
+
function done(request) {
|
|
9
|
+
return new Promise((resolve, reject) => {
|
|
10
|
+
request.onsuccess = () => resolve(request.result);
|
|
11
|
+
request.onerror = () => reject(request.error ?? new Error('storage request failed'));
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
function finished(tx) {
|
|
15
|
+
return new Promise((resolve, reject) => {
|
|
16
|
+
tx.oncomplete = () => resolve();
|
|
17
|
+
tx.onerror = () => reject(tx.error ?? new Error('storage transaction failed'));
|
|
18
|
+
tx.onabort = () => reject(tx.error ?? new Error('storage transaction aborted'));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Opens (or creates) the store called `name`. Resolves to `null` where the browser has no storage for the page or
|
|
23
|
+
* refuses it; the caller then simply works without a store.
|
|
24
|
+
*/
|
|
25
|
+
export async function openByteStore(name) {
|
|
26
|
+
if (typeof indexedDB === 'undefined')
|
|
27
|
+
return null;
|
|
28
|
+
let db;
|
|
29
|
+
try {
|
|
30
|
+
const open = indexedDB.open(name, 1);
|
|
31
|
+
open.onupgradeneeded = () => {
|
|
32
|
+
if (!open.result.objectStoreNames.contains(TABLE))
|
|
33
|
+
open.result.createObjectStore(TABLE);
|
|
34
|
+
};
|
|
35
|
+
// A browser may leave the request without an answer (blocked by another tab, a storage that hangs). Callers wait
|
|
36
|
+
// for the store before every read, so an open that takes too long counts as "no storage".
|
|
37
|
+
const opened = await Promise.race([
|
|
38
|
+
done(open),
|
|
39
|
+
new Promise((resolve) => {
|
|
40
|
+
open.onblocked = () => resolve(null);
|
|
41
|
+
setTimeout(() => resolve(null), OPEN_WAIT_MS);
|
|
42
|
+
}),
|
|
43
|
+
]);
|
|
44
|
+
if (!opened) {
|
|
45
|
+
// Should it open after all, it is closed again at once.
|
|
46
|
+
open.onsuccess = () => open.result.close();
|
|
47
|
+
open.onerror = () => { };
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
db = opened;
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
// Another tab asking for a newer layout must not be blocked by this one.
|
|
56
|
+
db.onversionchange = () => db.close();
|
|
57
|
+
const table = (mode) => {
|
|
58
|
+
const tx = db.transaction(TABLE, mode);
|
|
59
|
+
return { tx, store: tx.objectStore(TABLE) };
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
name,
|
|
63
|
+
async get(key) {
|
|
64
|
+
try {
|
|
65
|
+
const v = (await done(table('readonly').store.get(key)));
|
|
66
|
+
if (!v)
|
|
67
|
+
return null;
|
|
68
|
+
return v instanceof Uint8Array ? v : new Uint8Array(v);
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
async put(key, bytes) {
|
|
75
|
+
try {
|
|
76
|
+
const { tx, store } = table('readwrite');
|
|
77
|
+
// An exact copy of the bytes: a view may sit inside a larger buffer.
|
|
78
|
+
store.put(bytes.slice().buffer, key);
|
|
79
|
+
await finished(tx);
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// A full or failing store only means the entry is built again next time.
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
async keepOnly(prefix) {
|
|
86
|
+
try {
|
|
87
|
+
const { tx, store } = table('readwrite');
|
|
88
|
+
const keys = (await done(store.getAllKeys()));
|
|
89
|
+
for (const k of keys)
|
|
90
|
+
if (typeof k !== 'string' || !k.startsWith(prefix))
|
|
91
|
+
store.delete(k);
|
|
92
|
+
await finished(tx);
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
// As for `put`.
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
async clear() {
|
|
99
|
+
try {
|
|
100
|
+
const { tx, store } = table('readwrite');
|
|
101
|
+
store.clear();
|
|
102
|
+
await finished(tx);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// As for `put`.
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
async usage() {
|
|
109
|
+
try {
|
|
110
|
+
const { store } = table('readonly');
|
|
111
|
+
let keys = 0;
|
|
112
|
+
let bytes = 0;
|
|
113
|
+
await new Promise((resolve, reject) => {
|
|
114
|
+
const cursor = store.openCursor();
|
|
115
|
+
cursor.onerror = () => reject(cursor.error ?? new Error('storage cursor failed'));
|
|
116
|
+
cursor.onsuccess = () => {
|
|
117
|
+
const c = cursor.result;
|
|
118
|
+
if (!c) {
|
|
119
|
+
resolve();
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
keys++;
|
|
123
|
+
bytes += c.value.byteLength ?? 0;
|
|
124
|
+
c.continue();
|
|
125
|
+
};
|
|
126
|
+
});
|
|
127
|
+
return { keys, bytes };
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
return { keys: 0, bytes: 0 };
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
close() {
|
|
134
|
+
db.close();
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The drawing surface as modules above platform see it (they must not name browser DOM types). */
|
|
2
|
+
export type Canvas = HTMLCanvasElement;
|
|
3
|
+
/** Whatever follows the drawing-buffer size, typically a target rig. */
|
|
4
|
+
export interface CanvasSizeTarget {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
/** Physical pixels per CSS pixel. */
|
|
8
|
+
dpr: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Pixels of the drawing buffer per CSS pixel: the browser's `devicePixelRatio`, at most `maxPixelRatio` when that is
|
|
12
|
+
* given. Below the device's ratio the canvas is drawn at fewer pixels and the browser scales it up to its CSS size.
|
|
13
|
+
*/
|
|
14
|
+
export declare function canvasPixelRatio(maxPixelRatio?: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Sets the canvas and target size in physical pixels. Returns true when the size changed. `target.dpr` becomes the
|
|
17
|
+
* device's pixel ratio, or `pixelRatio` when given (the ratio the size was computed with).
|
|
18
|
+
*/
|
|
19
|
+
export declare function resizeCanvas(canvas: HTMLCanvasElement, target: CanvasSizeTarget, width: number, height: number, pixelRatio?: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Syncs the size right now according to the page layout. Useful before a frame outside the browser's timer.
|
|
22
|
+
* `maxPixelRatio` caps the pixels per CSS pixel (`canvasPixelRatio`); default: no cap.
|
|
23
|
+
*/
|
|
24
|
+
export declare function syncCanvasSize(canvas: HTMLCanvasElement, target: CanvasSizeTarget, maxPixelRatio?: number): boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Watches the canvas size and keeps it in physical pixels. `onResize` is called only on a real change.
|
|
27
|
+
* `maxPixelRatio` caps the pixels per CSS pixel (`canvasPixelRatio`); default: no cap, the device's own pixels.
|
|
28
|
+
*/
|
|
29
|
+
export declare function observeCanvasSize(canvas: HTMLCanvasElement, target: CanvasSizeTarget, onResize?: () => void, maxPixelRatio?: number): ResizeObserver;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
// Canvas size in physical pixels. Taken directly from the browser (devicePixelContentBoxSize),
|
|
2
|
+
// otherwise the image gets resampled and moire appears on fine patterns.
|
|
3
|
+
/**
|
|
4
|
+
* Pixels of the drawing buffer per CSS pixel: the browser's `devicePixelRatio`, at most `maxPixelRatio` when that is
|
|
5
|
+
* given. Below the device's ratio the canvas is drawn at fewer pixels and the browser scales it up to its CSS size.
|
|
6
|
+
*/
|
|
7
|
+
export function canvasPixelRatio(maxPixelRatio) {
|
|
8
|
+
const dpr = window.devicePixelRatio || 1;
|
|
9
|
+
return maxPixelRatio !== undefined && maxPixelRatio > 0 ? Math.min(dpr, maxPixelRatio) : dpr;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Sets the canvas and target size in physical pixels. Returns true when the size changed. `target.dpr` becomes the
|
|
13
|
+
* device's pixel ratio, or `pixelRatio` when given (the ratio the size was computed with).
|
|
14
|
+
*/
|
|
15
|
+
export function resizeCanvas(canvas, target, width, height, pixelRatio) {
|
|
16
|
+
const w = Math.max(1, Math.round(width));
|
|
17
|
+
const h = Math.max(1, Math.round(height));
|
|
18
|
+
const changed = canvas.width !== w || canvas.height !== h;
|
|
19
|
+
if (changed) {
|
|
20
|
+
canvas.width = w;
|
|
21
|
+
canvas.height = h;
|
|
22
|
+
}
|
|
23
|
+
target.width = w;
|
|
24
|
+
target.height = h;
|
|
25
|
+
target.dpr = pixelRatio ?? (window.devicePixelRatio || 1);
|
|
26
|
+
return changed;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Syncs the size right now according to the page layout. Useful before a frame outside the browser's timer.
|
|
30
|
+
* `maxPixelRatio` caps the pixels per CSS pixel (`canvasPixelRatio`); default: no cap.
|
|
31
|
+
*/
|
|
32
|
+
export function syncCanvasSize(canvas, target, maxPixelRatio) {
|
|
33
|
+
const r = canvas.getBoundingClientRect();
|
|
34
|
+
if (maxPixelRatio === undefined) {
|
|
35
|
+
const dpr = window.devicePixelRatio || 1;
|
|
36
|
+
return resizeCanvas(canvas, target, r.width * dpr, r.height * dpr);
|
|
37
|
+
}
|
|
38
|
+
const ratio = canvasPixelRatio(maxPixelRatio);
|
|
39
|
+
return resizeCanvas(canvas, target, r.width * ratio, r.height * ratio, ratio);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Watches the canvas size and keeps it in physical pixels. `onResize` is called only on a real change.
|
|
43
|
+
* `maxPixelRatio` caps the pixels per CSS pixel (`canvasPixelRatio`); default: no cap, the device's own pixels.
|
|
44
|
+
*/
|
|
45
|
+
export function observeCanvasSize(canvas, target, onResize, maxPixelRatio) {
|
|
46
|
+
const observer = new ResizeObserver((entries) => {
|
|
47
|
+
const entry = entries[0];
|
|
48
|
+
if (!entry)
|
|
49
|
+
return;
|
|
50
|
+
const box = entry.devicePixelContentBoxSize?.[0];
|
|
51
|
+
const dpr = window.devicePixelRatio || 1;
|
|
52
|
+
let changed;
|
|
53
|
+
if (maxPixelRatio === undefined) {
|
|
54
|
+
changed = box
|
|
55
|
+
? resizeCanvas(canvas, target, box.inlineSize, box.blockSize)
|
|
56
|
+
: resizeCanvas(canvas, target, entry.contentRect.width * dpr, entry.contentRect.height * dpr);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
// Capped: the device pixel box scaled by the share of the device's pixels that is kept.
|
|
60
|
+
const ratio = canvasPixelRatio(maxPixelRatio);
|
|
61
|
+
changed = box
|
|
62
|
+
? resizeCanvas(canvas, target, (box.inlineSize * ratio) / dpr, (box.blockSize * ratio) / dpr, ratio)
|
|
63
|
+
: resizeCanvas(canvas, target, entry.contentRect.width * ratio, entry.contentRect.height * ratio, ratio);
|
|
64
|
+
}
|
|
65
|
+
if (changed)
|
|
66
|
+
onResize?.();
|
|
67
|
+
});
|
|
68
|
+
try {
|
|
69
|
+
observer.observe(canvas, { box: 'device-pixel-content-box' });
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
observer.observe(canvas);
|
|
73
|
+
}
|
|
74
|
+
syncCanvasSize(canvas, target, maxPixelRatio);
|
|
75
|
+
return observer;
|
|
76
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export interface ContextLossHandlers {
|
|
2
|
+
/** The context is gone: nothing can be drawn or uploaded until `restored`. */
|
|
3
|
+
lost?: () => void;
|
|
4
|
+
/** A fresh context is there: every object of the old one is void and has to be made again. */
|
|
5
|
+
restored: () => void;
|
|
6
|
+
}
|
|
7
|
+
/** Calls the handlers when the canvas loses its context and when it has a new one. Returns a function that stops. */
|
|
8
|
+
export declare function watchContextLoss(canvas: HTMLCanvasElement, handlers: ContextLossHandlers): () => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Loss of the graphics context: the browser may take the context of a canvas away at any time (the driver restarted,
|
|
2
|
+
// too many contexts, the tab slept on a phone) and hand a fresh, empty one back later. This watches the two events.
|
|
3
|
+
// The browser only restores a context when the "lost" event was answered with `preventDefault`, which is done here.
|
|
4
|
+
/** Calls the handlers when the canvas loses its context and when it has a new one. Returns a function that stops. */
|
|
5
|
+
export function watchContextLoss(canvas, handlers) {
|
|
6
|
+
const onLost = (e) => {
|
|
7
|
+
e.preventDefault();
|
|
8
|
+
handlers.lost?.();
|
|
9
|
+
};
|
|
10
|
+
const onRestored = () => handlers.restored();
|
|
11
|
+
canvas.addEventListener('webglcontextlost', onLost);
|
|
12
|
+
canvas.addEventListener('webglcontextrestored', onRestored);
|
|
13
|
+
return () => {
|
|
14
|
+
canvas.removeEventListener('webglcontextlost', onLost);
|
|
15
|
+
canvas.removeEventListener('webglcontextrestored', onRestored);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** What the free controls drive: a view that is turned by pointer pixels and moved along its own axes. */
|
|
2
|
+
export interface FreeControl {
|
|
3
|
+
/** Physical pixels per CSS pixel. */
|
|
4
|
+
readonly dpr: number;
|
|
5
|
+
/** Pointer movement in physical pixels: right turns the view right, down turns it down. */
|
|
6
|
+
turnPixels(dx: number, dy: number): void;
|
|
7
|
+
/** Multiplies the speed (above 1 is faster). */
|
|
8
|
+
scaleSpeed(factor: number): void;
|
|
9
|
+
/** Moves for `dtSeconds`; the inputs are −1…1 (forward, right, up). Returns whether the view is still moving. */
|
|
10
|
+
move(forward: number, right: number, up: number, dtSeconds: number, boost: boolean): boolean;
|
|
11
|
+
}
|
|
12
|
+
/** Keys of each movement as `KeyboardEvent.code` values; any of the listed keys counts. */
|
|
13
|
+
export interface FreeKeys {
|
|
14
|
+
forward: readonly string[];
|
|
15
|
+
back: readonly string[];
|
|
16
|
+
left: readonly string[];
|
|
17
|
+
right: readonly string[];
|
|
18
|
+
up: readonly string[];
|
|
19
|
+
down: readonly string[];
|
|
20
|
+
boost: readonly string[];
|
|
21
|
+
}
|
|
22
|
+
export declare const DEFAULT_FREE_KEYS: FreeKeys;
|
|
23
|
+
export interface FreeInputOptions {
|
|
24
|
+
/** Called after every view change and while a movement key is held or the view still moves: request a frame here. */
|
|
25
|
+
onChange: () => void;
|
|
26
|
+
/** Replaces the keys of single movements; the rest keep `DEFAULT_FREE_KEYS`. */
|
|
27
|
+
keys?: Partial<FreeKeys>;
|
|
28
|
+
/** Speed factor per 100 wheel units (wheel up is faster). */
|
|
29
|
+
wheelFactor?: number;
|
|
30
|
+
/** `true`: a press locks the pointer to the canvas and its movement turns the view until Escape. Default: drag turns. */
|
|
31
|
+
pointerLock?: boolean;
|
|
32
|
+
/** `true`: a drag pulls the image along with the pointer instead of turning the view toward it. */
|
|
33
|
+
grab?: boolean;
|
|
34
|
+
/** Class the canvas gets for the duration of a drag (for the cursor). */
|
|
35
|
+
draggingClass?: string;
|
|
36
|
+
}
|
|
37
|
+
export interface FreeInput {
|
|
38
|
+
/**
|
|
39
|
+
* Applies the held keys to the view once per frame, before rendering. Without an argument the step is the real time
|
|
40
|
+
* since the previous call (at most 0.1 s; the first call after a rest steps by 0). Returns whether a movement key is
|
|
41
|
+
* held or the view still moves.
|
|
42
|
+
*/
|
|
43
|
+
step(dtSeconds?: number): boolean;
|
|
44
|
+
/** Detaches the listeners and releases a locked pointer. */
|
|
45
|
+
dispose(): void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Attaches free controls: W A S D or the arrows move, E / Space and Q / C move up and down, Shift boosts, a drag (or
|
|
49
|
+
* the locked pointer) turns, the wheel changes the speed. Call `step` of the result every frame.
|
|
50
|
+
*/
|
|
51
|
+
export declare function bindFreeInput(canvas: HTMLCanvasElement, view: FreeControl, options: FreeInputOptions): FreeInput;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Controls of a freely moving view by keyboard and pointer: held keys move it, dragging (or the locked pointer) turns
|
|
2
|
+
// it, the wheel changes its speed. Only events and the view, no on-screen elements; what the view does with a turn or
|
|
3
|
+
// a move is the rig's business (see `FreeRig` in camera).
|
|
4
|
+
import { trackHeldKeys } from './keys.js';
|
|
5
|
+
export const DEFAULT_FREE_KEYS = {
|
|
6
|
+
forward: ['KeyW', 'ArrowUp'],
|
|
7
|
+
back: ['KeyS', 'ArrowDown'],
|
|
8
|
+
left: ['KeyA', 'ArrowLeft'],
|
|
9
|
+
right: ['KeyD', 'ArrowRight'],
|
|
10
|
+
up: ['KeyE', 'Space'],
|
|
11
|
+
down: ['KeyQ', 'KeyC'],
|
|
12
|
+
boost: ['ShiftLeft', 'ShiftRight'],
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Attaches free controls: W A S D or the arrows move, E / Space and Q / C move up and down, Shift boosts, a drag (or
|
|
16
|
+
* the locked pointer) turns, the wheel changes the speed. Call `step` of the result every frame.
|
|
17
|
+
*/
|
|
18
|
+
export function bindFreeInput(canvas, view, options) {
|
|
19
|
+
const { onChange, wheelFactor = 1.15, pointerLock = false, draggingClass = 'dragging' } = options;
|
|
20
|
+
const keys = { ...DEFAULT_FREE_KEYS, ...options.keys };
|
|
21
|
+
const sign = options.grab ? -1 : 1;
|
|
22
|
+
const abort = new AbortController();
|
|
23
|
+
const signal = abort.signal;
|
|
24
|
+
const held = trackHeldKeys(onChange);
|
|
25
|
+
const locked = () => document.pointerLockElement === canvas;
|
|
26
|
+
let dragging = false;
|
|
27
|
+
let lastX = 0;
|
|
28
|
+
let lastY = 0;
|
|
29
|
+
canvas.addEventListener('pointerdown', (e) => {
|
|
30
|
+
if (pointerLock) {
|
|
31
|
+
if (!locked()) {
|
|
32
|
+
// Older browsers return nothing, newer ones a promise that rejects when the lock is refused.
|
|
33
|
+
const asked = canvas.requestPointerLock();
|
|
34
|
+
if (asked instanceof Promise)
|
|
35
|
+
asked.catch(() => { });
|
|
36
|
+
}
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
dragging = true;
|
|
40
|
+
lastX = e.clientX;
|
|
41
|
+
lastY = e.clientY;
|
|
42
|
+
canvas.setPointerCapture(e.pointerId);
|
|
43
|
+
canvas.classList.add(draggingClass);
|
|
44
|
+
}, { signal });
|
|
45
|
+
canvas.addEventListener('pointermove', (e) => {
|
|
46
|
+
if (pointerLock) {
|
|
47
|
+
if (!locked())
|
|
48
|
+
return;
|
|
49
|
+
view.turnPixels(e.movementX * view.dpr, e.movementY * view.dpr);
|
|
50
|
+
onChange();
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
if (!dragging)
|
|
54
|
+
return;
|
|
55
|
+
view.turnPixels((e.clientX - lastX) * view.dpr * sign, (e.clientY - lastY) * view.dpr * sign);
|
|
56
|
+
lastX = e.clientX;
|
|
57
|
+
lastY = e.clientY;
|
|
58
|
+
onChange();
|
|
59
|
+
}, { signal });
|
|
60
|
+
const stop = () => {
|
|
61
|
+
dragging = false;
|
|
62
|
+
canvas.classList.remove(draggingClass);
|
|
63
|
+
};
|
|
64
|
+
canvas.addEventListener('pointerup', stop, { signal });
|
|
65
|
+
canvas.addEventListener('pointercancel', stop, { signal });
|
|
66
|
+
canvas.addEventListener('wheel', (e) => {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
view.scaleSpeed(Math.pow(wheelFactor, -e.deltaY / 100));
|
|
69
|
+
onChange();
|
|
70
|
+
}, { passive: false, signal });
|
|
71
|
+
const axis = (plus, minus) => (plus.some((c) => held.has(c)) ? 1 : 0) - (minus.some((c) => held.has(c)) ? 1 : 0);
|
|
72
|
+
let last = 0;
|
|
73
|
+
return {
|
|
74
|
+
step(dtSeconds) {
|
|
75
|
+
let dt = dtSeconds;
|
|
76
|
+
if (dt === undefined) {
|
|
77
|
+
const now = performance.now();
|
|
78
|
+
dt = last > 0 ? Math.min(0.1, (now - last) / 1000) : 0;
|
|
79
|
+
last = now;
|
|
80
|
+
}
|
|
81
|
+
const forward = axis(keys.forward, keys.back);
|
|
82
|
+
const right = axis(keys.right, keys.left);
|
|
83
|
+
const up = axis(keys.up, keys.down);
|
|
84
|
+
const moving = view.move(forward, right, up, dt, keys.boost.some((c) => held.has(c)));
|
|
85
|
+
const active = moving || forward !== 0 || right !== 0 || up !== 0;
|
|
86
|
+
// While active every frame asks for the next one; at rest the real-time step starts over.
|
|
87
|
+
if (active)
|
|
88
|
+
onChange();
|
|
89
|
+
else
|
|
90
|
+
last = 0;
|
|
91
|
+
return active;
|
|
92
|
+
},
|
|
93
|
+
dispose() {
|
|
94
|
+
abort.abort();
|
|
95
|
+
held.dispose();
|
|
96
|
+
if (locked())
|
|
97
|
+
document.exitPointerLock();
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { canvasPixelRatio, observeCanvasSize, resizeCanvas, syncCanvasSize } from './canvas.ts';
|
|
2
|
+
export type { Canvas, CanvasSizeTarget } from './canvas.ts';
|
|
3
|
+
export { bindCameraInput, bindOrbitInput } from './input.ts';
|
|
4
|
+
export type { CameraInputOptions, OrbitControl, OrbitInputOptions, ViewChangeKind, ViewControl } from './input.ts';
|
|
5
|
+
export { FrameLoop } from './loop.ts';
|
|
6
|
+
export type { FrameLoopOptions, LoopClock } from './loop.ts';
|
|
7
|
+
export { WorkerPool, fetchBytes, hardwareThreads, serveWorker, workersAvailable } from './worker.ts';
|
|
8
|
+
export type { WorkerAnswer, WorkerJob } from './worker.ts';
|
|
9
|
+
export { trackHeldKeys } from './keys.ts';
|
|
10
|
+
export type { HeldKeys } from './keys.ts';
|
|
11
|
+
export { DEFAULT_FREE_KEYS, bindFreeInput } from './free-input.ts';
|
|
12
|
+
export type { FreeControl, FreeInput, FreeInputOptions, FreeKeys } from './free-input.ts';
|
|
13
|
+
export { openByteStore } from './byte-store.ts';
|
|
14
|
+
export type { ByteStore } from './byte-store.ts';
|
|
15
|
+
export { watchContextLoss } from './context-loss.ts';
|
|
16
|
+
export type { ContextLossHandlers } from './context-loss.ts';
|
|
17
|
+
export { DEFAULT_WALK_KEYS, bindWalkInput } from './walk-input.ts';
|
|
18
|
+
export type { WalkInput, WalkInputOptions, WalkInputState, WalkKeys, WalkView } from './walk-input.ts';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Layer 1, platform: the only place that touches the browser. Canvas size, raw input, the frame loop and real time.
|
|
2
|
+
export { canvasPixelRatio, observeCanvasSize, resizeCanvas, syncCanvasSize } from './canvas.js';
|
|
3
|
+
export { bindCameraInput, bindOrbitInput } from './input.js';
|
|
4
|
+
export { FrameLoop } from './loop.js';
|
|
5
|
+
export { WorkerPool, fetchBytes, hardwareThreads, serveWorker, workersAvailable } from './worker.js';
|
|
6
|
+
export { trackHeldKeys } from './keys.js';
|
|
7
|
+
export { DEFAULT_FREE_KEYS, bindFreeInput } from './free-input.js';
|
|
8
|
+
export { openByteStore } from './byte-store.js';
|
|
9
|
+
export { watchContextLoss } from './context-loss.js';
|
|
10
|
+
export { DEFAULT_WALK_KEYS, bindWalkInput } from './walk-input.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** What the controls drive: a view with stepped zoom and rotation that can be panned by screen pixels. */
|
|
2
|
+
export interface ViewControl {
|
|
3
|
+
/** Physical pixels per CSS pixel. */
|
|
4
|
+
readonly dpr: number;
|
|
5
|
+
readonly zoom: number;
|
|
6
|
+
readonly rotation: number;
|
|
7
|
+
panPixels(dx: number, dy: number): void;
|
|
8
|
+
setZoom(level: number): void;
|
|
9
|
+
setRotation(step: number): void;
|
|
10
|
+
}
|
|
11
|
+
/** Kind of view change: smooth movement (drag), or a jump (rotation, zoom, arrow step, reset). */
|
|
12
|
+
export type ViewChangeKind = 'pan' | 'step';
|
|
13
|
+
export interface CameraInputOptions {
|
|
14
|
+
/** Called after every view change. During 'pan' it fires often, so it is enough to request another frame. */
|
|
15
|
+
onChange: (kind: ViewChangeKind) => void;
|
|
16
|
+
/** Key 0: return to the default view. The app sets the view, `onChange` is called afterwards. */
|
|
17
|
+
onHome?: () => void;
|
|
18
|
+
/** Class the canvas gets for the duration of a drag (for the cursor). */
|
|
19
|
+
draggingClass?: string;
|
|
20
|
+
/** Arrow-key pan step in CSS pixels. */
|
|
21
|
+
keyPanPx?: number;
|
|
22
|
+
/** How many wheel units make one zoom level. Fine touchpad movements are accumulated. */
|
|
23
|
+
wheelStep?: number;
|
|
24
|
+
/** Where key listeners are attached, the default is the window. */
|
|
25
|
+
keyTarget?: Window | HTMLElement;
|
|
26
|
+
/** A press that does not start a drag (e.g. Alt+click, which the app uses for something else). */
|
|
27
|
+
ignorePointer?: (e: PointerEvent) => boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Attaches the controls: drag pans, wheel and +/− step the zoom, Q/E rotate by a quarter turn,
|
|
31
|
+
* arrows pan, 0 resets to home. Returns a function that detaches the controls again.
|
|
32
|
+
*/
|
|
33
|
+
export declare function bindCameraInput(canvas: HTMLCanvasElement, view: ViewControl, options: CameraInputOptions): () => void;
|
|
34
|
+
/** What orbit controls drive: a view turned by dragging and zoomed by a factor. */
|
|
35
|
+
export interface OrbitControl {
|
|
36
|
+
readonly dpr: number;
|
|
37
|
+
orbitPixels(dx: number, dy: number): void;
|
|
38
|
+
zoomBy(factor: number): void;
|
|
39
|
+
}
|
|
40
|
+
export interface OrbitInputOptions {
|
|
41
|
+
/** Called after every view change. */
|
|
42
|
+
onChange: () => void;
|
|
43
|
+
/** Distance factor per 100 wheel units. */
|
|
44
|
+
wheelFactor?: number;
|
|
45
|
+
draggingClass?: string;
|
|
46
|
+
}
|
|
47
|
+
/** Attaches orbit controls: drag turns and tilts, the wheel zooms. Returns a function that detaches them. */
|
|
48
|
+
export declare function bindOrbitInput(canvas: HTMLCanvasElement, view: OrbitControl, options: OrbitInputOptions): () => void;
|