wallhammer-web 0.0.0-stage → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -2
- package/dist/base/data.d.ts +67 -0
- package/dist/base/data.js +10 -0
- package/dist/base/destructible.d.ts +167 -0
- package/dist/base/destructible.js +47 -0
- package/dist/base/env-slot.d.ts +101 -0
- package/dist/base/env-slot.js +8 -0
- package/dist/base/frames.d.ts +138 -0
- package/dist/base/frames.js +459 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +11 -0
- package/dist/base/mat34.d.ts +46 -0
- package/dist/base/mat34.js +173 -0
- package/dist/base/material.d.ts +87 -0
- package/dist/base/material.js +6 -0
- package/dist/base/math.d.ts +27 -0
- package/dist/base/math.js +60 -0
- package/dist/base/progress.d.ts +38 -0
- package/dist/base/progress.js +81 -0
- package/dist/base/rig.d.ts +177 -0
- package/dist/base/rig.js +97 -0
- package/dist/base/trig.d.ts +29 -0
- package/dist/base/trig.js +169 -0
- package/dist/base/up.d.ts +26 -0
- package/dist/base/up.js +40 -0
- package/dist/base/view.d.ts +128 -0
- package/dist/base/view.js +136 -0
- package/dist/camera/camera.d.ts +99 -0
- package/dist/camera/camera.js +228 -0
- package/dist/camera/follow.d.ts +120 -0
- package/dist/camera/follow.js +320 -0
- package/dist/camera/free.d.ts +153 -0
- package/dist/camera/free.js +359 -0
- package/dist/camera/index.d.ts +10 -0
- package/dist/camera/index.js +6 -0
- package/dist/camera/orbit.d.ts +93 -0
- package/dist/camera/orbit.js +261 -0
- package/dist/camera/path.d.ts +48 -0
- package/dist/camera/path.js +179 -0
- package/dist/core/ballistic.d.ts +66 -0
- package/dist/core/ballistic.js +104 -0
- package/dist/core/bodies.d.ts +77 -0
- package/dist/core/bodies.js +125 -0
- package/dist/core/clock.d.ts +37 -0
- package/dist/core/clock.js +87 -0
- package/dist/core/cover-fade.d.ts +14 -0
- package/dist/core/cover-fade.js +38 -0
- package/dist/core/index.d.ts +13 -0
- package/dist/core/index.js +8 -0
- package/dist/core/kepler.d.ts +74 -0
- package/dist/core/kepler.js +108 -0
- package/dist/core/level-fade.d.ts +66 -0
- package/dist/core/level-fade.js +123 -0
- package/dist/core/palette.d.ts +15 -0
- package/dist/core/palette.js +47 -0
- package/dist/env/atmosphere.d.ts +129 -0
- package/dist/env/atmosphere.js +707 -0
- package/dist/env/cloud-field.d.ts +44 -0
- package/dist/env/cloud-field.js +111 -0
- package/dist/env/environment.d.ts +217 -0
- package/dist/env/environment.js +441 -0
- package/dist/env/frame-block.d.ts +29 -0
- package/dist/env/frame-block.js +152 -0
- package/dist/env/glsl.d.ts +35 -0
- package/dist/env/glsl.js +246 -0
- package/dist/env/index.d.ts +13 -0
- package/dist/env/index.js +9 -0
- package/dist/env/sky.d.ts +48 -0
- package/dist/env/sky.js +227 -0
- package/dist/env/sun.d.ts +31 -0
- package/dist/env/sun.js +42 -0
- package/dist/gen-arch/build.d.ts +17 -0
- package/dist/gen-arch/build.js +414 -0
- package/dist/gen-arch/damage/damaged.d.ts +44 -0
- package/dist/gen-arch/damage/damaged.js +1119 -0
- package/dist/gen-arch/damage/fall-material.d.ts +10 -0
- package/dist/gen-arch/damage/fall-material.js +47 -0
- package/dist/gen-arch/damage/fall.d.ts +78 -0
- package/dist/gen-arch/damage/fall.js +797 -0
- package/dist/gen-arch/damage/rubble.d.ts +22 -0
- package/dist/gen-arch/damage/rubble.js +188 -0
- package/dist/gen-arch/index.d.ts +25 -0
- package/dist/gen-arch/index.js +16 -0
- package/dist/gen-arch/interior/interior-plan.d.ts +114 -0
- package/dist/gen-arch/interior/interior-plan.js +615 -0
- package/dist/gen-arch/interior/interior.d.ts +97 -0
- package/dist/gen-arch/interior/interior.js +966 -0
- package/dist/gen-arch/structure/building-shader.d.ts +25 -0
- package/dist/gen-arch/structure/building-shader.js +946 -0
- package/dist/gen-arch/structure/buildings.d.ts +248 -0
- package/dist/gen-arch/structure/buildings.js +294 -0
- package/dist/gen-arch/structure/dims.d.ts +77 -0
- package/dist/gen-arch/structure/dims.js +39 -0
- package/dist/gen-arch/structure/distant.d.ts +62 -0
- package/dist/gen-arch/structure/distant.js +316 -0
- package/dist/gen-arch/structure/env-slots.d.ts +18 -0
- package/dist/gen-arch/structure/env-slots.js +47 -0
- package/dist/gen-arch/structure/facade.d.ts +193 -0
- package/dist/gen-arch/structure/facade.js +159 -0
- package/dist/gen-arch/structure/parts.d.ts +31 -0
- package/dist/gen-arch/structure/parts.js +69 -0
- package/dist/gen-arch/structure/roof.d.ts +76 -0
- package/dist/gen-arch/structure/roof.js +568 -0
- package/dist/gen-arch/structure/structure-material.d.ts +23 -0
- package/dist/gen-arch/structure/structure-material.js +55 -0
- package/dist/gen-arch/structure/writer.d.ts +94 -0
- package/dist/gen-arch/structure/writer.js +195 -0
- package/dist/gen-core/cell.d.ts +138 -0
- package/dist/gen-core/cell.js +150 -0
- package/dist/gen-core/cube-sphere.d.ts +57 -0
- package/dist/gen-core/cube-sphere.js +159 -0
- package/dist/gen-core/field.d.ts +55 -0
- package/dist/gen-core/field.js +154 -0
- package/dist/gen-core/geometry.d.ts +31 -0
- package/dist/gen-core/geometry.js +231 -0
- package/dist/gen-core/hash.d.ts +18 -0
- package/dist/gen-core/hash.js +58 -0
- package/dist/gen-core/height-field.d.ts +33 -0
- package/dist/gen-core/height-field.js +137 -0
- package/dist/gen-core/index.d.ts +22 -0
- package/dist/gen-core/index.js +17 -0
- package/dist/gen-core/instance-cells.d.ts +37 -0
- package/dist/gen-core/instance-cells.js +72 -0
- package/dist/gen-core/near-scatter.d.ts +56 -0
- package/dist/gen-core/near-scatter.js +120 -0
- package/dist/gen-core/noise.d.ts +23 -0
- package/dist/gen-core/noise.js +72 -0
- package/dist/gen-core/occupancy.d.ts +21 -0
- package/dist/gen-core/occupancy.js +104 -0
- package/dist/gen-core/sphere-patch.d.ts +138 -0
- package/dist/gen-core/sphere-patch.js +544 -0
- package/dist/gen-core/strip-surface.d.ts +34 -0
- package/dist/gen-core/strip-surface.js +246 -0
- package/dist/gen-core/strip.d.ts +102 -0
- package/dist/gen-core/strip.js +817 -0
- package/dist/gen-ground/env-slots.d.ts +48 -0
- package/dist/gen-ground/env-slots.js +78 -0
- package/dist/gen-ground/ground.d.ts +78 -0
- package/dist/gen-ground/ground.js +208 -0
- package/dist/gen-ground/index.d.ts +9 -0
- package/dist/gen-ground/index.js +7 -0
- package/dist/gen-ground/markings.d.ts +46 -0
- package/dist/gen-ground/markings.js +124 -0
- package/dist/gen-ground/materials.d.ts +18 -0
- package/dist/gen-ground/materials.js +22 -0
- package/dist/gen-ground/terrain-shader.d.ts +5 -0
- package/dist/gen-ground/terrain-shader.js +151 -0
- package/dist/gen-mesh/bare-shapes.d.ts +194 -0
- package/dist/gen-mesh/bare-shapes.js +596 -0
- package/dist/gen-mesh/bisect-surface.d.ts +47 -0
- package/dist/gen-mesh/bisect-surface.js +261 -0
- package/dist/gen-mesh/box.d.ts +18 -0
- package/dist/gen-mesh/box.js +2 -0
- package/dist/gen-mesh/cell-morph-mesh.d.ts +52 -0
- package/dist/gen-mesh/cell-morph-mesh.js +258 -0
- package/dist/gen-mesh/cell-surface-mesh.d.ts +32 -0
- package/dist/gen-mesh/cell-surface-mesh.js +140 -0
- package/dist/gen-mesh/chunk-mesh.d.ts +25 -0
- package/dist/gen-mesh/chunk-mesh.js +112 -0
- package/dist/gen-mesh/drape-mesh.d.ts +36 -0
- package/dist/gen-mesh/drape-mesh.js +98 -0
- package/dist/gen-mesh/height-field-mesh.d.ts +14 -0
- package/dist/gen-mesh/height-field-mesh.js +98 -0
- package/dist/gen-mesh/index.d.ts +26 -0
- package/dist/gen-mesh/index.js +15 -0
- package/dist/gen-mesh/line-mesh.d.ts +25 -0
- package/dist/gen-mesh/line-mesh.js +62 -0
- package/dist/gen-mesh/mesh-builder.d.ts +115 -0
- package/dist/gen-mesh/mesh-builder.js +315 -0
- package/dist/gen-mesh/packed-mesh.d.ts +53 -0
- package/dist/gen-mesh/packed-mesh.js +205 -0
- package/dist/gen-mesh/prism-mesh.d.ts +33 -0
- package/dist/gen-mesh/prism-mesh.js +176 -0
- package/dist/gen-mesh/rig-mesh.d.ts +9 -0
- package/dist/gen-mesh/rig-mesh.js +135 -0
- package/dist/gen-mesh/strip-mesh.d.ts +45 -0
- package/dist/gen-mesh/strip-mesh.js +258 -0
- package/dist/gen-plan/drain.d.ts +58 -0
- package/dist/gen-plan/drain.js +270 -0
- package/dist/gen-plan/grid.d.ts +37 -0
- package/dist/gen-plan/grid.js +182 -0
- package/dist/gen-plan/horizon.d.ts +50 -0
- package/dist/gen-plan/horizon.js +185 -0
- package/dist/gen-plan/index.d.ts +15 -0
- package/dist/gen-plan/index.js +13 -0
- package/dist/gen-plan/line-tiles.d.ts +35 -0
- package/dist/gen-plan/line-tiles.js +190 -0
- package/dist/gen-plan/lines.d.ts +31 -0
- package/dist/gen-plan/lines.js +183 -0
- package/dist/gen-plan/plots.d.ts +40 -0
- package/dist/gen-plan/plots.js +282 -0
- package/dist/gen-plan/route.d.ts +55 -0
- package/dist/gen-plan/route.js +297 -0
- package/dist/gen-plan/sites.d.ts +32 -0
- package/dist/gen-plan/sites.js +209 -0
- package/dist/gen-tex/code-stamp.d.ts +31 -0
- package/dist/gen-tex/code-stamp.js +137 -0
- package/dist/gen-tex/index.d.ts +7 -0
- package/dist/gen-tex/index.js +5 -0
- package/dist/gen-tex/light-horizon.d.ts +47 -0
- package/dist/gen-tex/light-horizon.js +180 -0
- package/dist/gen-tex/textures.d.ts +8 -0
- package/dist/gen-tex/textures.js +49 -0
- package/dist/gen-tex/value-textures.d.ts +46 -0
- package/dist/gen-tex/value-textures.js +102 -0
- package/dist/gen-vegetation/env-slots.d.ts +20 -0
- package/dist/gen-vegetation/env-slots.js +29 -0
- package/dist/gen-vegetation/index.d.ts +8 -0
- package/dist/gen-vegetation/index.js +7 -0
- package/dist/gen-vegetation/materials.d.ts +7 -0
- package/dist/gen-vegetation/materials.js +17 -0
- package/dist/gen-vegetation/planting.d.ts +170 -0
- package/dist/gen-vegetation/planting.js +363 -0
- package/dist/gen-vegetation/tree-shader.d.ts +23 -0
- package/dist/gen-vegetation/tree-shader.js +229 -0
- package/dist/gen-vegetation/trees.d.ts +59 -0
- package/dist/gen-vegetation/trees.js +329 -0
- package/dist/gpu/device.d.ts +286 -0
- package/dist/gpu/device.js +1157 -0
- package/dist/gpu/gl.d.ts +134 -0
- package/dist/gpu/gl.js +276 -0
- package/dist/gpu/index.d.ts +4 -0
- package/dist/gpu/index.js +3 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +13 -0
- package/dist/motion/graph.d.ts +244 -0
- package/dist/motion/graph.js +739 -0
- package/dist/motion/index.d.ts +8 -0
- package/dist/motion/index.js +8 -0
- package/dist/motion/pose-ops.d.ts +30 -0
- package/dist/motion/pose-ops.js +232 -0
- package/dist/motion/reach.d.ts +45 -0
- package/dist/motion/reach.js +264 -0
- package/dist/motion/skeleton.d.ts +49 -0
- package/dist/motion/skeleton.js +99 -0
- package/dist/platform/byte-store.d.ts +21 -0
- package/dist/platform/byte-store.js +137 -0
- package/dist/platform/canvas.d.ts +29 -0
- package/dist/platform/canvas.js +76 -0
- package/dist/platform/context-loss.d.ts +8 -0
- package/dist/platform/context-loss.js +17 -0
- package/dist/platform/free-input.d.ts +51 -0
- package/dist/platform/free-input.js +100 -0
- package/dist/platform/index.d.ts +18 -0
- package/dist/platform/index.js +10 -0
- package/dist/platform/input.d.ts +48 -0
- package/dist/platform/input.js +117 -0
- package/dist/platform/keys.d.ts +12 -0
- package/dist/platform/keys.js +29 -0
- package/dist/platform/loop.d.ts +41 -0
- package/dist/platform/loop.js +84 -0
- package/dist/platform/walk-input.d.ts +74 -0
- package/dist/platform/walk-input.js +226 -0
- package/dist/platform/worker.d.ts +51 -0
- package/dist/platform/worker.js +170 -0
- package/dist/render/bone-instances.d.ts +24 -0
- package/dist/render/bone-instances.js +74 -0
- package/dist/render/cells.d.ts +70 -0
- package/dist/render/cells.js +144 -0
- package/dist/render/glsl.d.ts +66 -0
- package/dist/render/glsl.js +173 -0
- package/dist/render/index.d.ts +26 -0
- package/dist/render/index.js +16 -0
- package/dist/render/instance-sets.d.ts +97 -0
- package/dist/render/instance-sets.js +241 -0
- package/dist/render/near-instances.d.ts +75 -0
- package/dist/render/near-instances.js +219 -0
- package/dist/render/particles.d.ts +63 -0
- package/dist/render/particles.js +215 -0
- package/dist/render/pieces.d.ts +57 -0
- package/dist/render/pieces.js +151 -0
- package/dist/render/point-lights.d.ts +97 -0
- package/dist/render/point-lights.js +243 -0
- package/dist/render/renderer.d.ts +447 -0
- package/dist/render/renderer.js +1100 -0
- package/dist/render/rig.d.ts +74 -0
- package/dist/render/rig.js +576 -0
- package/dist/render/shadow-areas.d.ts +49 -0
- package/dist/render/shadow-areas.js +125 -0
- package/dist/render/shadow-glsl.d.ts +12 -0
- package/dist/render/shadow-glsl.js +146 -0
- package/dist/render/transient.d.ts +61 -0
- package/dist/render/transient.js +213 -0
- package/dist/render/value-glsl.d.ts +17 -0
- package/dist/render/value-glsl.js +208 -0
- package/dist/render/value-layers.d.ts +180 -0
- package/dist/render/value-layers.js +333 -0
- package/dist/runtime/atmosphere.d.ts +37 -0
- package/dist/runtime/atmosphere.js +304 -0
- package/dist/runtime/cloud-shell.d.ts +29 -0
- package/dist/runtime/cloud-shell.js +158 -0
- package/dist/runtime/engine.d.ts +155 -0
- package/dist/runtime/engine.js +305 -0
- package/dist/runtime/environment-link.d.ts +22 -0
- package/dist/runtime/environment-link.js +49 -0
- package/dist/runtime/index.d.ts +13 -0
- package/dist/runtime/index.js +12 -0
- package/dist/runtime/precipitation-glsl.d.ts +2 -0
- package/dist/runtime/precipitation-glsl.js +70 -0
- package/dist/runtime/precipitation.d.ts +11 -0
- package/dist/runtime/precipitation.js +78 -0
- package/dist/runtime/sky.d.ts +30 -0
- package/dist/runtime/sky.js +114 -0
- package/dist/runtime/streaming.d.ts +245 -0
- package/dist/runtime/streaming.js +602 -0
- package/dist/sim/agenda.d.ts +167 -0
- package/dist/sim/agenda.js +425 -0
- package/dist/sim/bytes.d.ts +34 -0
- package/dist/sim/bytes.js +125 -0
- package/dist/sim/destruction/apply.d.ts +59 -0
- package/dist/sim/destruction/apply.js +243 -0
- package/dist/sim/destruction/cell-states.d.ts +54 -0
- package/dist/sim/destruction/cell-states.js +198 -0
- package/dist/sim/destruction/index.d.ts +11 -0
- package/dist/sim/destruction/index.js +10 -0
- package/dist/sim/destruction/int-geometry.d.ts +17 -0
- package/dist/sim/destruction/int-geometry.js +131 -0
- package/dist/sim/destruction/pick.d.ts +13 -0
- package/dist/sim/destruction/pick.js +59 -0
- package/dist/sim/destruction/rubble.d.ts +8 -0
- package/dist/sim/destruction/rubble.js +159 -0
- package/dist/sim/destruction/state.d.ts +15 -0
- package/dist/sim/destruction/state.js +83 -0
- package/dist/sim/destruction/structure.d.ts +30 -0
- package/dist/sim/destruction/structure.js +139 -0
- package/dist/sim/events.d.ts +37 -0
- package/dist/sim/events.js +106 -0
- package/dist/sim/fixed.d.ts +21 -0
- package/dist/sim/fixed.js +64 -0
- package/dist/sim/flow-probe.d.ts +58 -0
- package/dist/sim/flow-probe.js +158 -0
- package/dist/sim/flow.d.ts +257 -0
- package/dist/sim/flow.js +967 -0
- package/dist/sim/index.d.ts +28 -0
- package/dist/sim/index.js +20 -0
- package/dist/sim/interlock.d.ts +47 -0
- package/dist/sim/interlock.js +252 -0
- package/dist/sim/movers.d.ts +228 -0
- package/dist/sim/movers.js +1083 -0
- package/dist/sim/nav.d.ts +72 -0
- package/dist/sim/nav.js +292 -0
- package/dist/sim/paths.d.ts +80 -0
- package/dist/sim/paths.js +197 -0
- package/dist/sim/regions.d.ts +87 -0
- package/dist/sim/regions.js +517 -0
- package/dist/sim/simulation.d.ts +114 -0
- package/dist/sim/simulation.js +291 -0
- package/dist/sim/states.d.ts +23 -0
- package/dist/sim/states.js +54 -0
- package/dist/sim/table.d.ts +49 -0
- package/dist/sim/table.js +126 -0
- package/dist/sim/zones.d.ts +11 -0
- package/dist/sim/zones.js +196 -0
- package/dist/tools/collision-lines.d.ts +12 -0
- package/dist/tools/collision-lines.js +50 -0
- package/dist/tools/data-view-glsl.d.ts +3 -0
- package/dist/tools/data-view-glsl.js +63 -0
- package/dist/tools/data-view-layers.d.ts +20 -0
- package/dist/tools/data-view-layers.js +60 -0
- package/dist/tools/debug-view.d.ts +30 -0
- package/dist/tools/debug-view.js +4 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +7 -0
- package/dist/tools/mesh-checks.d.ts +67 -0
- package/dist/tools/mesh-checks.js +188 -0
- package/dist/tools/profile.d.ts +85 -0
- package/dist/tools/profile.js +173 -0
- package/dist/world/cell-cache.d.ts +32 -0
- package/dist/world/cell-cache.js +60 -0
- package/dist/world/cell-format.d.ts +25 -0
- package/dist/world/cell-format.js +232 -0
- package/dist/world/collide.d.ts +192 -0
- package/dist/world/collide.js +822 -0
- package/dist/world/index.d.ts +16 -0
- package/dist/world/index.js +12 -0
- package/dist/world/mesh-solids.d.ts +48 -0
- package/dist/world/mesh-solids.js +169 -0
- package/dist/world/pick.d.ts +34 -0
- package/dist/world/pick.js +177 -0
- package/dist/world/providers.d.ts +66 -0
- package/dist/world/providers.js +83 -0
- package/dist/world/sphere.d.ts +69 -0
- package/dist/world/sphere.js +271 -0
- package/dist/world/streaming.d.ts +288 -0
- package/dist/world/streaming.js +801 -0
- package/dist/world/walker.d.ts +145 -0
- package/dist/world/walker.js +502 -0
- package/docs/architecture.md +111 -0
- package/docs/cameras.md +302 -0
- package/docs/concepts.md +123 -0
- package/docs/destruction.md +127 -0
- package/docs/frames.md +199 -0
- package/docs/generators.md +575 -0
- package/docs/getting-started.md +123 -0
- package/docs/headless.md +50 -0
- package/docs/motion.md +160 -0
- package/docs/planet.md +152 -0
- package/docs/recipes.md +521 -0
- package/docs/reference.md +364 -0
- package/docs/rendering.md +823 -0
- package/docs/rigs.md +203 -0
- package/docs/simulation.md +235 -0
- package/docs/streaming.md +590 -0
- package/docs/system.md +166 -0
- package/docs/tools.md +175 -0
- package/docs/walking.md +262 -0
- package/package.json +131 -3
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Control of a stepped view (typically a camera rig) by mouse, touch and keyboard. Only events and the view, no on-screen elements:
|
|
2
|
+
// the app draws its own buttons and panels and calls the same view methods.
|
|
3
|
+
/**
|
|
4
|
+
* Attaches the controls: drag pans, wheel and +/− step the zoom, Q/E rotate by a quarter turn,
|
|
5
|
+
* arrows pan, 0 resets to home. Returns a function that detaches the controls again.
|
|
6
|
+
*/
|
|
7
|
+
export function bindCameraInput(canvas, view, options) {
|
|
8
|
+
const { onChange, onHome, draggingClass = 'dragging', keyPanPx = 80, wheelStep = 80 } = options;
|
|
9
|
+
const keyTarget = options.keyTarget ?? window;
|
|
10
|
+
const abort = new AbortController();
|
|
11
|
+
const signal = abort.signal;
|
|
12
|
+
let dragging = false;
|
|
13
|
+
let lastX = 0;
|
|
14
|
+
let lastY = 0;
|
|
15
|
+
canvas.addEventListener('pointerdown', (e) => {
|
|
16
|
+
if (options.ignorePointer?.(e))
|
|
17
|
+
return;
|
|
18
|
+
dragging = true;
|
|
19
|
+
lastX = e.clientX;
|
|
20
|
+
lastY = e.clientY;
|
|
21
|
+
canvas.setPointerCapture(e.pointerId);
|
|
22
|
+
canvas.classList.add(draggingClass);
|
|
23
|
+
}, { signal });
|
|
24
|
+
canvas.addEventListener('pointermove', (e) => {
|
|
25
|
+
if (!dragging)
|
|
26
|
+
return;
|
|
27
|
+
view.panPixels((e.clientX - lastX) * view.dpr, (e.clientY - lastY) * view.dpr);
|
|
28
|
+
lastX = e.clientX;
|
|
29
|
+
lastY = e.clientY;
|
|
30
|
+
onChange('pan');
|
|
31
|
+
}, { signal });
|
|
32
|
+
const stop = () => {
|
|
33
|
+
dragging = false;
|
|
34
|
+
canvas.classList.remove(draggingClass);
|
|
35
|
+
};
|
|
36
|
+
canvas.addEventListener('pointerup', stop, { signal });
|
|
37
|
+
canvas.addEventListener('pointercancel', stop, { signal });
|
|
38
|
+
let wheelAcc = 0;
|
|
39
|
+
canvas.addEventListener('wheel', (e) => {
|
|
40
|
+
e.preventDefault();
|
|
41
|
+
wheelAcc += e.deltaY;
|
|
42
|
+
if (Math.abs(wheelAcc) >= wheelStep) {
|
|
43
|
+
view.setZoom(view.zoom - Math.sign(wheelAcc));
|
|
44
|
+
wheelAcc = 0;
|
|
45
|
+
onChange('step');
|
|
46
|
+
}
|
|
47
|
+
}, { passive: false, signal });
|
|
48
|
+
keyTarget.addEventListener('keydown', (ev) => {
|
|
49
|
+
const e = ev;
|
|
50
|
+
if (e.ctrlKey || e.metaKey || e.altKey)
|
|
51
|
+
return;
|
|
52
|
+
// Typing into fields and adjusting sliders with arrows does not move the view.
|
|
53
|
+
const t = e.target;
|
|
54
|
+
if (t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement)
|
|
55
|
+
return;
|
|
56
|
+
const pan = keyPanPx * view.dpr;
|
|
57
|
+
if (e.key === '+' || e.key === '=')
|
|
58
|
+
view.setZoom(view.zoom + 1);
|
|
59
|
+
else if (e.key === '-' || e.key === '_')
|
|
60
|
+
view.setZoom(view.zoom - 1);
|
|
61
|
+
else if (e.key === 'q' || e.key === 'Q')
|
|
62
|
+
view.setRotation(view.rotation + 1);
|
|
63
|
+
else if (e.key === 'e' || e.key === 'E')
|
|
64
|
+
view.setRotation(view.rotation - 1);
|
|
65
|
+
else if (e.key === '0' && onHome)
|
|
66
|
+
onHome();
|
|
67
|
+
else if (e.key === 'ArrowLeft')
|
|
68
|
+
view.panPixels(pan, 0);
|
|
69
|
+
else if (e.key === 'ArrowRight')
|
|
70
|
+
view.panPixels(-pan, 0);
|
|
71
|
+
else if (e.key === 'ArrowUp')
|
|
72
|
+
view.panPixels(0, pan);
|
|
73
|
+
else if (e.key === 'ArrowDown')
|
|
74
|
+
view.panPixels(0, -pan);
|
|
75
|
+
else
|
|
76
|
+
return;
|
|
77
|
+
e.preventDefault();
|
|
78
|
+
onChange('step');
|
|
79
|
+
}, { signal });
|
|
80
|
+
return () => abort.abort();
|
|
81
|
+
}
|
|
82
|
+
/** Attaches orbit controls: drag turns and tilts, the wheel zooms. Returns a function that detaches them. */
|
|
83
|
+
export function bindOrbitInput(canvas, view, options) {
|
|
84
|
+
const { onChange, wheelFactor = 1.15, draggingClass = 'dragging' } = options;
|
|
85
|
+
const abort = new AbortController();
|
|
86
|
+
const signal = abort.signal;
|
|
87
|
+
let dragging = false;
|
|
88
|
+
let lastX = 0;
|
|
89
|
+
let lastY = 0;
|
|
90
|
+
canvas.addEventListener('pointerdown', (e) => {
|
|
91
|
+
dragging = true;
|
|
92
|
+
lastX = e.clientX;
|
|
93
|
+
lastY = e.clientY;
|
|
94
|
+
canvas.setPointerCapture(e.pointerId);
|
|
95
|
+
canvas.classList.add(draggingClass);
|
|
96
|
+
}, { signal });
|
|
97
|
+
canvas.addEventListener('pointermove', (e) => {
|
|
98
|
+
if (!dragging)
|
|
99
|
+
return;
|
|
100
|
+
view.orbitPixels((e.clientX - lastX) * view.dpr, (e.clientY - lastY) * view.dpr);
|
|
101
|
+
lastX = e.clientX;
|
|
102
|
+
lastY = e.clientY;
|
|
103
|
+
onChange();
|
|
104
|
+
}, { signal });
|
|
105
|
+
const stop = () => {
|
|
106
|
+
dragging = false;
|
|
107
|
+
canvas.classList.remove(draggingClass);
|
|
108
|
+
};
|
|
109
|
+
canvas.addEventListener('pointerup', stop, { signal });
|
|
110
|
+
canvas.addEventListener('pointercancel', stop, { signal });
|
|
111
|
+
canvas.addEventListener('wheel', (e) => {
|
|
112
|
+
e.preventDefault();
|
|
113
|
+
view.zoomBy(Math.pow(wheelFactor, e.deltaY / 100));
|
|
114
|
+
onChange();
|
|
115
|
+
}, { passive: false, signal });
|
|
116
|
+
return () => abort.abort();
|
|
117
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export interface HeldKeys {
|
|
2
|
+
/** Is the key (KeyboardEvent.code, for example 'KeyW' or 'ShiftLeft') held now? */
|
|
3
|
+
has(code: string): boolean;
|
|
4
|
+
/** Any key held now? */
|
|
5
|
+
readonly any: boolean;
|
|
6
|
+
/** Forgets every held key (a hand-over of the controls); a key still held counts again on its next key-down. */
|
|
7
|
+
clear(): void;
|
|
8
|
+
/** Detaches the listeners. */
|
|
9
|
+
dispose(): void;
|
|
10
|
+
}
|
|
11
|
+
/** Tracks held keys on the window. Typing into form fields is ignored; losing focus releases everything. */
|
|
12
|
+
export declare function trackHeldKeys(onChange?: () => void): HeldKeys;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Held keys, for continuous movement (fly or walk controls). Only raw key state: what a key does is the app's choice.
|
|
2
|
+
/** Tracks held keys on the window. Typing into form fields is ignored; losing focus releases everything. */
|
|
3
|
+
export function trackHeldKeys(onChange) {
|
|
4
|
+
const held = new Set();
|
|
5
|
+
const abort = new AbortController();
|
|
6
|
+
const signal = abort.signal;
|
|
7
|
+
const typing = (t) => t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement;
|
|
8
|
+
window.addEventListener('keydown', (e) => {
|
|
9
|
+
if (e.ctrlKey || e.metaKey || e.altKey || typing(e.target))
|
|
10
|
+
return;
|
|
11
|
+
if (!held.has(e.code)) {
|
|
12
|
+
held.add(e.code);
|
|
13
|
+
onChange?.();
|
|
14
|
+
}
|
|
15
|
+
}, { signal });
|
|
16
|
+
window.addEventListener('keyup', (e) => {
|
|
17
|
+
if (held.delete(e.code))
|
|
18
|
+
onChange?.();
|
|
19
|
+
}, { signal });
|
|
20
|
+
window.addEventListener('blur', () => held.clear(), { signal });
|
|
21
|
+
return {
|
|
22
|
+
has: (code) => held.has(code),
|
|
23
|
+
get any() {
|
|
24
|
+
return held.size > 0;
|
|
25
|
+
},
|
|
26
|
+
clear: () => held.clear(),
|
|
27
|
+
dispose: () => abort.abort(),
|
|
28
|
+
};
|
|
29
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** A clock the loop drives (see `Clock` in core). */
|
|
2
|
+
export interface LoopClock {
|
|
3
|
+
readonly animating: boolean;
|
|
4
|
+
/** Advances by real time in seconds. Returns true when the value changed. */
|
|
5
|
+
advance(dt: number): boolean;
|
|
6
|
+
onChange(listener: () => void): unknown;
|
|
7
|
+
}
|
|
8
|
+
export interface FrameLoopOptions {
|
|
9
|
+
/** Renders one frame. */
|
|
10
|
+
render: () => void;
|
|
11
|
+
/** Clocks the loop drives and uses to tell whether it should run. */
|
|
12
|
+
clocks: readonly LoopClock[];
|
|
13
|
+
/** Called after every loop frame (not after `renderNow`), e.g. for a status line. */
|
|
14
|
+
onFrame?: () => void;
|
|
15
|
+
/** Longest real-time step per frame in seconds. After returning to the tab the clocks do not jump. */
|
|
16
|
+
maxStep?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare class FrameLoop {
|
|
19
|
+
private readonly options;
|
|
20
|
+
private handle;
|
|
21
|
+
private last;
|
|
22
|
+
private dirty;
|
|
23
|
+
private started;
|
|
24
|
+
private fpsFrames;
|
|
25
|
+
private fpsSince;
|
|
26
|
+
/** Frames per second measured over the last half second, 0 when the loop is stopped. */
|
|
27
|
+
fps: number;
|
|
28
|
+
constructor(options: FrameLoopOptions);
|
|
29
|
+
/** Is continuous drawing running now? */
|
|
30
|
+
get animating(): boolean;
|
|
31
|
+
start(): void;
|
|
32
|
+
stop(): void;
|
|
33
|
+
/** Requests one frame at the browser's next tick. */
|
|
34
|
+
invalidate(): void;
|
|
35
|
+
/** Renders immediately, synchronously. Does not advance the clocks. */
|
|
36
|
+
renderNow(): void;
|
|
37
|
+
private schedule;
|
|
38
|
+
/** The browser's callback, made once, so scheduling a frame allocates nothing. */
|
|
39
|
+
private readonly onTick;
|
|
40
|
+
private frame;
|
|
41
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Frame loop. Draws continuously only while some clock is running, otherwise only on request
|
|
2
|
+
// (view change, clock setting). Reads real time only here and passes it to the clocks.
|
|
3
|
+
export class FrameLoop {
|
|
4
|
+
options;
|
|
5
|
+
handle = 0;
|
|
6
|
+
last = 0;
|
|
7
|
+
dirty = false;
|
|
8
|
+
started = false;
|
|
9
|
+
fpsFrames = 0;
|
|
10
|
+
fpsSince = 0;
|
|
11
|
+
/** Frames per second measured over the last half second, 0 when the loop is stopped. */
|
|
12
|
+
fps = 0;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.options = options;
|
|
15
|
+
for (const c of options.clocks)
|
|
16
|
+
c.onChange(() => this.invalidate());
|
|
17
|
+
}
|
|
18
|
+
/** Is continuous drawing running now? */
|
|
19
|
+
get animating() {
|
|
20
|
+
for (const c of this.options.clocks)
|
|
21
|
+
if (c.animating)
|
|
22
|
+
return true;
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
start() {
|
|
26
|
+
this.started = true;
|
|
27
|
+
this.invalidate();
|
|
28
|
+
}
|
|
29
|
+
stop() {
|
|
30
|
+
this.started = false;
|
|
31
|
+
if (this.handle)
|
|
32
|
+
cancelAnimationFrame(this.handle);
|
|
33
|
+
this.handle = 0;
|
|
34
|
+
}
|
|
35
|
+
/** Requests one frame at the browser's next tick. */
|
|
36
|
+
invalidate() {
|
|
37
|
+
this.dirty = true;
|
|
38
|
+
this.schedule();
|
|
39
|
+
}
|
|
40
|
+
/** Renders immediately, synchronously. Does not advance the clocks. */
|
|
41
|
+
renderNow() {
|
|
42
|
+
this.dirty = false;
|
|
43
|
+
this.options.render();
|
|
44
|
+
}
|
|
45
|
+
schedule() {
|
|
46
|
+
if (!this.started || this.handle)
|
|
47
|
+
return;
|
|
48
|
+
this.handle = requestAnimationFrame(this.onTick);
|
|
49
|
+
}
|
|
50
|
+
/** The browser's callback, made once, so scheduling a frame allocates nothing. */
|
|
51
|
+
onTick = (now) => this.frame(now);
|
|
52
|
+
frame(now) {
|
|
53
|
+
this.handle = 0;
|
|
54
|
+
const maxStep = this.options.maxStep ?? 0.1;
|
|
55
|
+
// After a pause the time is not counted from the last frame before it.
|
|
56
|
+
const dt = this.last > 0 ? Math.min(maxStep, (now - this.last) / 1000) : 0;
|
|
57
|
+
let moved = false;
|
|
58
|
+
for (const c of this.options.clocks)
|
|
59
|
+
moved = c.advance(dt) || moved;
|
|
60
|
+
if (moved || this.dirty) {
|
|
61
|
+
this.dirty = false;
|
|
62
|
+
this.options.render();
|
|
63
|
+
}
|
|
64
|
+
if (this.animating) {
|
|
65
|
+
this.last = now;
|
|
66
|
+
this.fpsFrames++;
|
|
67
|
+
if (this.fpsSince === 0)
|
|
68
|
+
this.fpsSince = now;
|
|
69
|
+
else if (now - this.fpsSince >= 500) {
|
|
70
|
+
this.fps = (this.fpsFrames * 1000) / (now - this.fpsSince);
|
|
71
|
+
this.fpsFrames = 0;
|
|
72
|
+
this.fpsSince = now;
|
|
73
|
+
}
|
|
74
|
+
this.schedule();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.last = 0;
|
|
78
|
+
this.fps = 0;
|
|
79
|
+
this.fpsFrames = 0;
|
|
80
|
+
this.fpsSince = 0;
|
|
81
|
+
}
|
|
82
|
+
this.options.onFrame?.();
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { Canvas } from './canvas.ts';
|
|
2
|
+
/** What the walk controls drive: a view turned by pointer pixels whose distance is changed by a factor. */
|
|
3
|
+
export interface WalkView {
|
|
4
|
+
/** Physical pixels per CSS pixel. */
|
|
5
|
+
readonly dpr: number;
|
|
6
|
+
/** Pointer movement in physical pixels: right turns the view right, down turns it down. */
|
|
7
|
+
turnPixels(dx: number, dy: number): void;
|
|
8
|
+
/** Multiplies the view's distance (above 1 is farther). */
|
|
9
|
+
zoomBy(factor: number): void;
|
|
10
|
+
}
|
|
11
|
+
/** What the controls ask for in one frame. */
|
|
12
|
+
export interface WalkInputState {
|
|
13
|
+
/** Wanted movement in the view's frame: forward and to the right, each −1 to 1. */
|
|
14
|
+
forward: number;
|
|
15
|
+
right: number;
|
|
16
|
+
run: boolean;
|
|
17
|
+
/** True once per press. */
|
|
18
|
+
jump: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Keys of each action as `KeyboardEvent.code` values; any of the listed keys counts. */
|
|
21
|
+
export interface WalkKeys {
|
|
22
|
+
forward: readonly string[];
|
|
23
|
+
back: readonly string[];
|
|
24
|
+
left: readonly string[];
|
|
25
|
+
right: readonly string[];
|
|
26
|
+
run: readonly string[];
|
|
27
|
+
jump: readonly string[];
|
|
28
|
+
}
|
|
29
|
+
export declare const DEFAULT_WALK_KEYS: WalkKeys;
|
|
30
|
+
export interface WalkInputOptions {
|
|
31
|
+
/** Called on every key change and view turn, and from `read` while the controls ask for movement: request a frame here. */
|
|
32
|
+
onChange?: () => void;
|
|
33
|
+
/** Replaces the keys of single actions; the rest keep `DEFAULT_WALK_KEYS`. */
|
|
34
|
+
keys?: Partial<WalkKeys>;
|
|
35
|
+
/** Distance factor per 100 wheel units (wheel down is farther). Default 1.15. */
|
|
36
|
+
wheelFactor?: number;
|
|
37
|
+
/** `true`: a press locks the pointer to the canvas and its movement turns the view until Escape. Default: drag turns. */
|
|
38
|
+
pointerLock?: boolean;
|
|
39
|
+
/** Class the canvas gets for the duration of a drag (for the cursor). Default 'dragging'. */
|
|
40
|
+
draggingClass?: string;
|
|
41
|
+
/** Read a gamepad with the standard layout. Default true. */
|
|
42
|
+
gamepad?: boolean;
|
|
43
|
+
/** Radial dead zone of the gamepad sticks, 0–1. Default 0.15. */
|
|
44
|
+
deadZone?: number;
|
|
45
|
+
/** CSS pixels of turn per second at the full deflection of the right stick. Default 600. */
|
|
46
|
+
stickTurnPx?: number;
|
|
47
|
+
}
|
|
48
|
+
export interface WalkInput {
|
|
49
|
+
/**
|
|
50
|
+
* Reads keys and the gamepad into `out` and passes pointer turns and wheel zoom to the view. Call once per frame.
|
|
51
|
+
* The right stick turns the view by the real time since the previous call (at most 0.1 s), or by `dtSeconds`.
|
|
52
|
+
*/
|
|
53
|
+
read(out: WalkInputState, dtSeconds?: number): WalkInputState;
|
|
54
|
+
/**
|
|
55
|
+
* `false` hands the view and the body to someone else: held keys are released, a drag ends, a locked pointer is let
|
|
56
|
+
* go, and nothing touches the view any more; `read` gives no movement. `true` takes them back.
|
|
57
|
+
*/
|
|
58
|
+
enabled: boolean;
|
|
59
|
+
/** Is the pointer locked to the canvas now? */
|
|
60
|
+
readonly pointerLocked: boolean;
|
|
61
|
+
/**
|
|
62
|
+
* Is a gamepad connected now? A gamepad sends no events for its sticks: while one is connected keep frames coming
|
|
63
|
+
* (for example `Engine.animateWhile(() => input.gamepadConnected)`), or `read` never sees it move.
|
|
64
|
+
*/
|
|
65
|
+
readonly gamepadConnected: boolean;
|
|
66
|
+
/** Detaches the listeners and releases a locked pointer. */
|
|
67
|
+
dispose(): void;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Attaches walk controls: W A S D or the arrows move, Shift runs, Space jumps, a drag (or the locked pointer) turns the
|
|
71
|
+
* view, the wheel changes its distance. A gamepad with the standard layout: left stick moves, right stick turns, A
|
|
72
|
+
* jumps, pressing the left stick or RT runs. Call `read` of the result every frame.
|
|
73
|
+
*/
|
|
74
|
+
export declare function bindWalkInput(canvas: Canvas, view: WalkView, options?: WalkInputOptions): WalkInput;
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Controls of a walking body seen through a turnable view: keys and a gamepad stick give the wanted movement in the
|
|
2
|
+
// view's frame, a drag (or the locked pointer, or the right stick) turns the view, the wheel changes the view's
|
|
3
|
+
// distance. Only events, the gamepad and the view, no on-screen elements. What the movement does is the caller's
|
|
4
|
+
// business: it reads the state once per frame and turns it into world movement with the view's heading.
|
|
5
|
+
//
|
|
6
|
+
// Touch is not built yet: a touch drag turns the view like a mouse drag, but there is no on-screen stick to walk with.
|
|
7
|
+
import { trackHeldKeys } from './keys.js';
|
|
8
|
+
export const DEFAULT_WALK_KEYS = {
|
|
9
|
+
forward: ['KeyW', 'ArrowUp'],
|
|
10
|
+
back: ['KeyS', 'ArrowDown'],
|
|
11
|
+
left: ['KeyA', 'ArrowLeft'],
|
|
12
|
+
right: ['KeyD', 'ArrowRight'],
|
|
13
|
+
run: ['ShiftLeft', 'ShiftRight'],
|
|
14
|
+
jump: ['Space'],
|
|
15
|
+
};
|
|
16
|
+
/** Standard gamepad layout: buttons and axes used here. */
|
|
17
|
+
const PAD_A = 0;
|
|
18
|
+
const PAD_RT = 7;
|
|
19
|
+
const PAD_L3 = 10;
|
|
20
|
+
const PAD_LX = 0;
|
|
21
|
+
const PAD_LY = 1;
|
|
22
|
+
const PAD_RX = 2;
|
|
23
|
+
const PAD_RY = 3;
|
|
24
|
+
/** Radial dead zone: inside it nothing, outside it the stick's deflection rescaled to start at 0. Written to `out`. */
|
|
25
|
+
function deadZoned(x, y, zone, out) {
|
|
26
|
+
const len = Math.hypot(x, y);
|
|
27
|
+
if (len <= zone || len === 0) {
|
|
28
|
+
out[0] = 0;
|
|
29
|
+
out[1] = 0;
|
|
30
|
+
return out;
|
|
31
|
+
}
|
|
32
|
+
const scaled = Math.min(1, (len - zone) / (1 - zone)) / len;
|
|
33
|
+
out[0] = x * scaled;
|
|
34
|
+
out[1] = y * scaled;
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Attaches walk controls: W A S D or the arrows move, Shift runs, Space jumps, a drag (or the locked pointer) turns the
|
|
39
|
+
* view, the wheel changes its distance. A gamepad with the standard layout: left stick moves, right stick turns, A
|
|
40
|
+
* jumps, pressing the left stick or RT runs. Call `read` of the result every frame.
|
|
41
|
+
*/
|
|
42
|
+
export function bindWalkInput(canvas, view, options = {}) {
|
|
43
|
+
const { onChange, wheelFactor = 1.15, pointerLock = false, draggingClass = 'dragging', gamepad = true, deadZone = 0.15, stickTurnPx = 600 } = options;
|
|
44
|
+
const keys = { ...DEFAULT_WALK_KEYS, ...options.keys };
|
|
45
|
+
const changed = () => onChange?.();
|
|
46
|
+
const abort = new AbortController();
|
|
47
|
+
const signal = abort.signal;
|
|
48
|
+
const held = trackHeldKeys(changed);
|
|
49
|
+
const locked = () => document.pointerLockElement === canvas;
|
|
50
|
+
const typing = (t) => t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement;
|
|
51
|
+
const owns = (code) => {
|
|
52
|
+
for (const list of [keys.forward, keys.back, keys.left, keys.right, keys.jump])
|
|
53
|
+
if (list.includes(code))
|
|
54
|
+
return true;
|
|
55
|
+
return false;
|
|
56
|
+
};
|
|
57
|
+
let enabled = true;
|
|
58
|
+
let jumpQueued = false;
|
|
59
|
+
let padJumpHeld = false;
|
|
60
|
+
let pads = 0;
|
|
61
|
+
window.addEventListener('keydown', (e) => {
|
|
62
|
+
if (!enabled || e.ctrlKey || e.metaKey || e.altKey || typing(e.target))
|
|
63
|
+
return;
|
|
64
|
+
if (keys.jump.includes(e.code) && !e.repeat)
|
|
65
|
+
jumpQueued = true;
|
|
66
|
+
// The page must not scroll under the movement keys.
|
|
67
|
+
if (owns(e.code))
|
|
68
|
+
e.preventDefault();
|
|
69
|
+
}, { signal });
|
|
70
|
+
let dragging = false;
|
|
71
|
+
let lastX = 0;
|
|
72
|
+
let lastY = 0;
|
|
73
|
+
const stopDrag = () => {
|
|
74
|
+
dragging = false;
|
|
75
|
+
canvas.classList.remove(draggingClass);
|
|
76
|
+
};
|
|
77
|
+
canvas.addEventListener('pointerdown', (e) => {
|
|
78
|
+
if (!enabled)
|
|
79
|
+
return;
|
|
80
|
+
if (pointerLock) {
|
|
81
|
+
if (!locked()) {
|
|
82
|
+
// Older browsers return nothing, newer ones a promise that rejects when the lock is refused.
|
|
83
|
+
const asked = canvas.requestPointerLock();
|
|
84
|
+
if (asked instanceof Promise)
|
|
85
|
+
asked.catch(() => { });
|
|
86
|
+
}
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
dragging = true;
|
|
90
|
+
lastX = e.clientX;
|
|
91
|
+
lastY = e.clientY;
|
|
92
|
+
canvas.setPointerCapture(e.pointerId);
|
|
93
|
+
canvas.classList.add(draggingClass);
|
|
94
|
+
}, { signal });
|
|
95
|
+
canvas.addEventListener('pointermove', (e) => {
|
|
96
|
+
if (!enabled)
|
|
97
|
+
return;
|
|
98
|
+
if (pointerLock) {
|
|
99
|
+
if (!locked())
|
|
100
|
+
return;
|
|
101
|
+
view.turnPixels(e.movementX * view.dpr, e.movementY * view.dpr);
|
|
102
|
+
changed();
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (!dragging)
|
|
106
|
+
return;
|
|
107
|
+
view.turnPixels((e.clientX - lastX) * view.dpr, (e.clientY - lastY) * view.dpr);
|
|
108
|
+
lastX = e.clientX;
|
|
109
|
+
lastY = e.clientY;
|
|
110
|
+
changed();
|
|
111
|
+
}, { signal });
|
|
112
|
+
canvas.addEventListener('pointerup', stopDrag, { signal });
|
|
113
|
+
canvas.addEventListener('pointercancel', stopDrag, { signal });
|
|
114
|
+
canvas.addEventListener('wheel', (e) => {
|
|
115
|
+
if (!enabled)
|
|
116
|
+
return;
|
|
117
|
+
e.preventDefault();
|
|
118
|
+
view.zoomBy(Math.pow(wheelFactor, e.deltaY / 100));
|
|
119
|
+
changed();
|
|
120
|
+
}, { passive: false, signal });
|
|
121
|
+
const canPad = gamepad && typeof navigator !== 'undefined' && typeof navigator.getGamepads === 'function';
|
|
122
|
+
if (canPad) {
|
|
123
|
+
window.addEventListener('gamepadconnected', () => {
|
|
124
|
+
pads++;
|
|
125
|
+
changed();
|
|
126
|
+
}, { signal });
|
|
127
|
+
window.addEventListener('gamepaddisconnected', () => (pads = Math.max(0, pads - 1)), { signal });
|
|
128
|
+
}
|
|
129
|
+
const anyHeld = (codes) => {
|
|
130
|
+
for (const c of codes)
|
|
131
|
+
if (held.has(c))
|
|
132
|
+
return true;
|
|
133
|
+
return false;
|
|
134
|
+
};
|
|
135
|
+
const stick = [0, 0];
|
|
136
|
+
let last = 0;
|
|
137
|
+
return {
|
|
138
|
+
read(out, dtSeconds) {
|
|
139
|
+
let dt = dtSeconds;
|
|
140
|
+
if (dt === undefined) {
|
|
141
|
+
const now = performance.now();
|
|
142
|
+
dt = last > 0 ? Math.min(0.1, (now - last) / 1000) : 0;
|
|
143
|
+
last = now;
|
|
144
|
+
}
|
|
145
|
+
out.forward = 0;
|
|
146
|
+
out.right = 0;
|
|
147
|
+
out.run = false;
|
|
148
|
+
out.jump = false;
|
|
149
|
+
if (!enabled) {
|
|
150
|
+
last = 0;
|
|
151
|
+
return out;
|
|
152
|
+
}
|
|
153
|
+
let forward = (anyHeld(keys.forward) ? 1 : 0) - (anyHeld(keys.back) ? 1 : 0);
|
|
154
|
+
let right = (anyHeld(keys.right) ? 1 : 0) - (anyHeld(keys.left) ? 1 : 0);
|
|
155
|
+
let run = anyHeld(keys.run);
|
|
156
|
+
let jump = jumpQueued;
|
|
157
|
+
jumpQueued = false;
|
|
158
|
+
let padActive = false;
|
|
159
|
+
if (canPad && pads > 0) {
|
|
160
|
+
// getGamepads hands out a fresh list each call in some browsers; it is read only while a pad is connected.
|
|
161
|
+
const list = navigator.getGamepads();
|
|
162
|
+
for (const pad of list) {
|
|
163
|
+
if (!pad || !pad.connected || pad.mapping !== 'standard')
|
|
164
|
+
continue;
|
|
165
|
+
const ax = pad.axes;
|
|
166
|
+
const b = pad.buttons;
|
|
167
|
+
deadZoned(ax[PAD_LX] ?? 0, ax[PAD_LY] ?? 0, deadZone, stick);
|
|
168
|
+
forward -= stick[1];
|
|
169
|
+
right += stick[0];
|
|
170
|
+
deadZoned(ax[PAD_RX] ?? 0, ax[PAD_RY] ?? 0, deadZone, stick);
|
|
171
|
+
if ((stick[0] !== 0 || stick[1] !== 0) && dt > 0) {
|
|
172
|
+
const px = stickTurnPx * dt * view.dpr;
|
|
173
|
+
view.turnPixels(stick[0] * px, stick[1] * px);
|
|
174
|
+
}
|
|
175
|
+
const a = b[PAD_A]?.pressed ?? false;
|
|
176
|
+
if (a && !padJumpHeld)
|
|
177
|
+
jump = true;
|
|
178
|
+
padJumpHeld = a;
|
|
179
|
+
if ((b[PAD_L3]?.pressed ?? false) || (b[PAD_RT]?.value ?? 0) > 0.5)
|
|
180
|
+
run = true;
|
|
181
|
+
padActive = padActive || stick[0] !== 0 || stick[1] !== 0 || forward !== 0 || right !== 0 || a;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
out.forward = Math.max(-1, Math.min(1, forward));
|
|
186
|
+
out.right = Math.max(-1, Math.min(1, right));
|
|
187
|
+
out.run = run;
|
|
188
|
+
out.jump = jump;
|
|
189
|
+
// While asked to move (or a gamepad is in use) every frame asks for the next one; at rest the real-time step starts over.
|
|
190
|
+
if (out.forward !== 0 || out.right !== 0 || jump || padActive)
|
|
191
|
+
changed();
|
|
192
|
+
else
|
|
193
|
+
last = 0;
|
|
194
|
+
return out;
|
|
195
|
+
},
|
|
196
|
+
get enabled() {
|
|
197
|
+
return enabled;
|
|
198
|
+
},
|
|
199
|
+
set enabled(on) {
|
|
200
|
+
if (on === enabled)
|
|
201
|
+
return;
|
|
202
|
+
enabled = on;
|
|
203
|
+
held.clear();
|
|
204
|
+
jumpQueued = false;
|
|
205
|
+
padJumpHeld = true; // a button held across the hand-over does not count as a new press
|
|
206
|
+
last = 0;
|
|
207
|
+
stopDrag();
|
|
208
|
+
if (!on && locked())
|
|
209
|
+
document.exitPointerLock();
|
|
210
|
+
changed();
|
|
211
|
+
},
|
|
212
|
+
get pointerLocked() {
|
|
213
|
+
return locked();
|
|
214
|
+
},
|
|
215
|
+
get gamepadConnected() {
|
|
216
|
+
return pads > 0;
|
|
217
|
+
},
|
|
218
|
+
dispose() {
|
|
219
|
+
abort.abort();
|
|
220
|
+
held.dispose();
|
|
221
|
+
stopDrag();
|
|
222
|
+
if (locked())
|
|
223
|
+
document.exitPointerLock();
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/** A job handed to the pool. */
|
|
2
|
+
export interface WorkerJob<R> {
|
|
3
|
+
readonly promise: Promise<R>;
|
|
4
|
+
/** Removes the job from the queue if it has not started; a running job finishes and its promise rejects. */
|
|
5
|
+
cancel(): void;
|
|
6
|
+
}
|
|
7
|
+
/** Threads the machine offers, as the browser reports them (at least 1). */
|
|
8
|
+
export declare function hardwareThreads(): number;
|
|
9
|
+
/** Can module workers be created here? (False in Node and in old browsers.) */
|
|
10
|
+
export declare function workersAvailable(): boolean;
|
|
11
|
+
export declare class WorkerPool {
|
|
12
|
+
readonly size: number;
|
|
13
|
+
private readonly workers;
|
|
14
|
+
private readonly idle;
|
|
15
|
+
private readonly running;
|
|
16
|
+
private readonly queue;
|
|
17
|
+
/** Per worker: copies of broadcast messages it has still to get, in the order sent. They go before the queue. */
|
|
18
|
+
private readonly direct;
|
|
19
|
+
private nextId;
|
|
20
|
+
/** Time the workers spent in jobs, from their own clock: total and count. */
|
|
21
|
+
readonly timing: {
|
|
22
|
+
ms: number;
|
|
23
|
+
jobs: number;
|
|
24
|
+
};
|
|
25
|
+
/** `url` is a module worker script that calls `serveWorker`. */
|
|
26
|
+
constructor(url: URL | string, size?: number);
|
|
27
|
+
/** Jobs waiting for a worker. */
|
|
28
|
+
get queued(): number;
|
|
29
|
+
/** Jobs running now. */
|
|
30
|
+
get busy(): number;
|
|
31
|
+
run<R>(message: unknown, transfer?: Transferable[]): WorkerJob<R>;
|
|
32
|
+
/**
|
|
33
|
+
* Posts one structured copy of `message` to every worker of the pool (no transfer list): at once to a free worker,
|
|
34
|
+
* right after its running job to a busy one, in either case before any queued job; two broadcasts reach a worker in
|
|
35
|
+
* the order sent. Resolves when every worker has answered, rejects with the first error, and with an `AbortError`
|
|
36
|
+
* after `dispose`. The replies do not count in `timing`.
|
|
37
|
+
*/
|
|
38
|
+
broadcast(message: unknown): Promise<void>;
|
|
39
|
+
dispose(): void;
|
|
40
|
+
private pump;
|
|
41
|
+
private finish;
|
|
42
|
+
}
|
|
43
|
+
/** What a worker handler returns: the reply and the buffers to transfer with it. */
|
|
44
|
+
export interface WorkerAnswer {
|
|
45
|
+
value: unknown;
|
|
46
|
+
transfer?: Transferable[];
|
|
47
|
+
}
|
|
48
|
+
/** Inside a worker script: answers every job of a `WorkerPool` with `handler`. */
|
|
49
|
+
export declare function serveWorker(handler: (message: unknown) => WorkerAnswer | Promise<WorkerAnswer>): void;
|
|
50
|
+
/** Reads a URL into bytes; rejects on an HTTP error or when the signal fires. */
|
|
51
|
+
export declare function fetchBytes(url: string, signal: AbortSignal): Promise<ArrayBuffer>;
|