diff --git a/README.md b/README.md index ff70a75..10fa440 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,14 @@ and [apirrone/mjlab_microduck](https://github.com/apirrone/mjlab_microduck). In roller mode the legs-only actions (kicks, sit) are disabled and their hints fade out; play the ball by driving into it. +### URL parameters + +- `?boot=1`: skip the welcome modal and land straight on the BIOS console, + which then shows the real loading progress live (honest loader) before + the normal entrance plays. Boot failures (missing asset, policy fetch + error...) freeze the console on a `SYSTEM HALTED` screen with the error + detail - handy for debugging. + The ball is local-only: it lives in your tab's physics and is not shared with the multiplayer ghosts. A square arena (3 x 3 m) fences the play area so neither the ball nor the duck can wander off. diff --git a/assets/microduck-wordmark.svg b/assets/microduck-wordmark.svg new file mode 100644 index 0000000..213ee79 --- /dev/null +++ b/assets/microduck-wordmark.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/assets/stickers/bang-red.webp b/assets/stickers/bang-red.webp new file mode 100644 index 0000000..3c08e7f --- /dev/null +++ b/assets/stickers/bang-red.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9ad4baa5430230a2d5e982c54dac279a86f479ffca3763932b943075a3d9a122 +size 44386 diff --git a/assets/stickers/boom.webp b/assets/stickers/boom.webp new file mode 100644 index 0000000..4571822 --- /dev/null +++ b/assets/stickers/boom.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:81083452beb521d1e304540534d20525c19f46102c4ef63e951f685cab61bfcc +size 44314 diff --git a/assets/stickers/burst-yellow.webp b/assets/stickers/burst-yellow.webp new file mode 100644 index 0000000..66096aa --- /dev/null +++ b/assets/stickers/burst-yellow.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b08865ed6ad01b10d54e0fb7ef0783c9d709fbe532cf99621ce789f12d213bfd +size 20162 diff --git a/assets/stickers/quack-quack.webp b/assets/stickers/quack-quack.webp new file mode 100644 index 0000000..a4e23bf --- /dev/null +++ b/assets/stickers/quack-quack.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3608b49bda042808ca69c75bddfed14226867bab741f18b186059ecd3e5e0b08 +size 32972 diff --git a/assets/stickers/quack.webp b/assets/stickers/quack.webp new file mode 100644 index 0000000..64c0b62 --- /dev/null +++ b/assets/stickers/quack.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:289e830372b197078e2d3f9e0ab1d8ae7d941895e73e1037d6b5e7871f1fe64f +size 39484 diff --git a/assets/stickers/shooting-star.webp b/assets/stickers/shooting-star.webp new file mode 100644 index 0000000..f9d2856 --- /dev/null +++ b/assets/stickers/shooting-star.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f31230606d12fc64af7c4d6a39efb339e36ae81eb79caa297ec4e0ebc7c073c5 +size 26220 diff --git a/assets/stickers/star-yellow.webp b/assets/stickers/star-yellow.webp new file mode 100644 index 0000000..99f9840 --- /dev/null +++ b/assets/stickers/star-yellow.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:93ee62a3e5e697c4dc7af15a00a24919c81c940f56c53cc730caf9eae110cf77 +size 10466 diff --git a/assets/stickers/wave.webp b/assets/stickers/wave.webp new file mode 100644 index 0000000..31c61a5 --- /dev/null +++ b/assets/stickers/wave.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:eb115e0f06dcc7d6e86b63052bb52e5d2568186138cff09d801106cc5ab6d607 +size 41602 diff --git a/assets/stickers/ziouuu.webp b/assets/stickers/ziouuu.webp new file mode 100644 index 0000000..e22f029 --- /dev/null +++ b/assets/stickers/ziouuu.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:737f020f74e1cbe3b0b0d9e58de38cf5c5ea0be8b5ee872313aad7cb0673b441 +size 35972 diff --git a/ball-actor.js b/ball-actor.js new file mode 100644 index 0000000..a5cc7e2 --- /dev/null +++ b/ball-actor.js @@ -0,0 +1,102 @@ +// Ball actor: physics stays in rl.js, this owns the mesh + wireframe FX. +// +// visual: +// hidden parked, mesh off +// live following qpos, optional appear scan playing +// ghost physics already parked; mesh frozen at last pose while the +// reverse scan peels it away +// +// spawn while live/ghost queues a pop: despawn finishes, then the caller +// places physics and appear()s. + +// Appear/disappear speed. The shared wireframe FX timeline lasts +// fx.TOTAL_S (0.9 s, tuned for the duck's ceremony); the ball plays the +// same timeline time-scaled to these snappier durations. +const BALL_APPEAR_S = 0.2; // spawn pop-in (scan up) +const BALL_DISAPPEAR_S = 0.15; // despawn/park peel-away (reverse scan) + +export function createBallActor({ THREE, scene, camera, renderer, fxModule, mesh, group }) { + const fx = fxModule.createWireframeFx(); + fx.init({ THREE, scene, root: group, camera, renderer, hidden: false }); + + let visual = "hidden"; // hidden | live | ghost + let ghost = null; + let pendingSpawn = false; + let fxPrev = null; + + function appear() { + visual = "live"; + ghost = null; + mesh.visible = true; + fx.start(); + fxPrev = performance.now(); + } + + function despawn({ cancelQueued = false, parkPhysics }) { + if (cancelQueued) pendingSpawn = false; + if (visual === "hidden") return; + if (visual === "live") { + ghost = { + pos: mesh.position.clone(), + quat: mesh.quaternion.clone(), + }; + visual = "ghost"; + } + parkPhysics(); + fx.startReverse(); + fxPrev = performance.now(); + } + + function queueRespawn() { + pendingSpawn = true; + } + + function poseFromQpos(qpos, adr) { + mesh.position.set(qpos[adr], qpos[adr + 1], qpos[adr + 2]); + mesh.quaternion.set(qpos[adr + 4], qpos[adr + 5], qpos[adr + 6], qpos[adr + 3]); + } + + function sync(qpos, adr, physicsLive) { + if (visual === "ghost" && ghost) { + mesh.visible = true; + mesh.position.copy(ghost.pos); + mesh.quaternion.copy(ghost.quat); + } else if (physicsLive) { + mesh.visible = true; + poseFromQpos(qpos, adr); + } else { + mesh.visible = false; + } + } + + function drive(onQueuedSpawn) { + if (fx.playing) { + const now = performance.now(); + if (fxPrev === null) fxPrev = now; + const dt = Math.min((now - fxPrev) / 1000, 0.25); + fxPrev = now; + // Time-scale the FX so the ball pops in/out at its own speed. + const speed = fx.TOTAL_S / (fx.reversing ? BALL_DISAPPEAR_S : BALL_APPEAR_S); + fx.update(dt * speed); + } + if (visual === "ghost" && fx.isDone()) { + mesh.visible = false; + visual = "hidden"; + ghost = null; + fx.restore(); + const queued = pendingSpawn; + pendingSpawn = false; + if (queued) onQueuedSpawn(); + } + } + + return { + appear, + despawn, + queueRespawn, + poseFromQpos, + sync, + drive, + get visual() { return visual; }, + }; +} diff --git a/ceremony.js b/ceremony.js new file mode 100644 index 0000000..b4c789c --- /dev/null +++ b/ceremony.js @@ -0,0 +1,171 @@ +// Cutscene layer: page-load entrance and every later respawn. +// +// Physics (resetSim) snaps the duck to the stand keyframe; this module +// owns what the player sees around that snap. Two mutually exclusive +// timelines share the duck's wireframe FX: +// +// entrance (once) grid + walls draw in, then the duck scans up +// respawn (every kill / Space / loco switch) +// 0.00 duck clipped away, camera glides home from the kill view +// 0.80 camera almost settled -> duck scan-up cues +// 0.90 camera lands +// 1.70 scan done, inputs unlock +// +// rl.js injects the world (rig, grid, camera reset) and the lock/flash +// callbacks; this file never touches MuJoCo. + +export const CAM_RESET_S = 0.9; +export const RESPAWN_SCAN_AT = 0.8; // cue scan-up at 80% of the camera glide + +const ENTRANCE_GRID_S = 1.1; +const ENTRANCE_WALL_DELAY_S = 0.35; +const ENTRANCE_WALL_S = 0.9; +const ENTRANCE_FX_START_S = 0.6; + +const clamp01 = (x) => Math.min(Math.max(x, 0), 1); + +export function createCeremony({ + THREE, scene, camera, renderer, fx, + getRig, grid, wallMats, + syncRig, startCameraReset, + setLocked, flashReset, +}) { + const ENTRANCE_TOTAL_S = Math.max( + ENTRANCE_GRID_S, + ENTRANCE_WALL_DELAY_S + ENTRANCE_WALL_S, + ENTRANCE_FX_START_S + fx.TOTAL_S, + ); + + let fxRig = null; + let fxPrev = null; + let entranceT0 = null; + let entranceFxCued = false; + let entranceDone = false; + let respawn = null; // { t0, scanCued } | null + let entranceFinishedResolve; + const entranceFinished = new Promise((r) => { entranceFinishedResolve = r; }); + + function bind() { + const rig = getRig(); + if (fxRig === rig) return; + fx.dispose(); + fx.init({ THREE, scene, rig, camera, renderer }); + rig.placer.traverse((o) => { + if (o.isMesh && !o.userData.meshName) o.userData.fxOverlay = true; + }); + fxRig = rig; + } + + function fxStart() { + bind(); + fx.start(); + fxPrev = performance.now(); + } + function fxDrive() { + if (fxPrev === null || fx.isDone()) return; + const now = performance.now(); + const dt = Math.min((now - fxPrev) / 1000, 0.25); + fxPrev = now; + fx.update(dt); + } + function fxForceFinish() { + if (!fx.isDone()) fx.update(1e3); + } + + function startEntrance() { + if (entranceT0 !== null || entranceDone) return; + entranceT0 = performance.now(); + entranceFxCued = false; + grid.material.uniforms.uReveal.value = 0; + for (const m of wallMats) m.uniforms.uReveal.value = 0; + } + + function driveEntrance() { + if (entranceT0 === null) return; + const t = (performance.now() - entranceT0) / 1000; + grid.material.uniforms.uReveal.value = clamp01(t / ENTRANCE_GRID_S); + const wallR = clamp01((t - ENTRANCE_WALL_DELAY_S) / ENTRANCE_WALL_S); + for (const m of wallMats) m.uniforms.uReveal.value = wallR; + if (t >= ENTRANCE_FX_START_S && !entranceFxCued) { + entranceFxCued = true; + fxStart(); + } + fxDrive(); + if (t >= ENTRANCE_TOTAL_S) { + if (!fx.isDone() && t < ENTRANCE_TOTAL_S + 0.5) return; + fxForceFinish(); + entranceT0 = null; + entranceDone = true; + grid.material.uniforms.uReveal.value = 1; + for (const m of wallMats) m.uniforms.uReveal.value = 1; + setLocked(false); + entranceFinishedResolve(); + } + } + + function playRespawn() { + if (!entranceDone) return; + setLocked(true); + flashReset(); + bind(); + startCameraReset(); + syncRig(); + fx.setProgress(0); + respawn = { t0: performance.now(), scanCued: false }; + } + + function driveRespawn() { + if (!respawn) return; + const u = (performance.now() - respawn.t0) / 1000 / CAM_RESET_S; + if (!respawn.scanCued && u >= RESPAWN_SCAN_AT) { + respawn.scanCued = true; + fxStart(); + } + if (respawn.scanCued) fxDrive(); + if (respawn.scanCued && fx.isDone()) { + respawn = null; + setLocked(false); + } + } + + function drive() { + driveEntrance(); + driveRespawn(); + } + + function setReveal(floor, wall = floor) { + entranceT0 = null; + grid.material.uniforms.uReveal.value = floor; + for (const m of wallMats) m.uniforms.uReveal.value = wall; + } + + function setFx(p) { + entranceT0 = null; + respawn = null; + bind(); + if (p >= 1) { + fx.start(); + fxForceFinish(); + setLocked(false); + entranceFinishedResolve(); + } else { + fx.setProgress(p); + } + } + + // Boot hidden: clip parked below the feet from the first frame. + bind(); + + return { + CAM_RESET_S, + startEntrance, + playRespawn, + drive, + bind, + setReveal, + setFx, + entranceFinished, + get entranceDone() { return entranceDone; }, + get respawnActive() { return respawn !== null; }, + }; +} diff --git a/controls/controller.js b/controls/controller.js new file mode 100644 index 0000000..23d80ea --- /dev/null +++ b/controls/controller.js @@ -0,0 +1,188 @@ +// Controller: the video-game-style input core. Aggregates pluggable input +// SOURCES (keyboard, gamepad, later touch) into one game-facing surface: +// +// - a continuous COMMAND: the [vx, vy, wz] twist the policy tracks, plus +// auxiliary analog AXES (jaw 0..1, camera-orbit rates -1..1); +// - discrete ACTIONS: edge-triggered events (roll, kicks, ball spawn...) +// dispatched to subscribers the moment a source fires them; +// - HUD support: per-source pressed-state snapshots and activity info so +// the hint keycaps can light up per physical control and per device. +// +// The game code (rl.js) instantiates the sources, registers them here, +// calls update(dt) once per render frame, reads getCommand()/getAxes() +// where it builds the policy observation, and subscribes its trigger +// functions with on(action, cb). +// +// ── Source interface contract ──────────────────────────────────────────── +// Every input source module (keyboard.js, gamepad.js, a future touch.js +// with a virtual joystick + buttons) implements: +// +// id Unique string ("keyboard", "gamepad", "touch"...). Action +// subscribers receive it as meta.source, so game code can +// attribute HUD flashes / keycap lighting to the right device. +// connected Boolean hardware presence. Keyboard: always true. Gamepad: +// a pad is currently reported by navigator.getGamepads(). +// command Float32Array(3) [vx, vy, wz], ALREADY scaled to the game's +// velocity limits (sources take a getVelocityLimits() callback +// returning [fwd, back, ang], so limits can change at runtime, +// e.g. the legs <-> rollers switch). Must be a stable array +// reference updated in place: getCommand() hands it out +// without copying, and the control loop reads it between +// render frames. +// axes { jaw, orbitX, orbitY } - auxiliary continuous channels. +// jaw in [0, 1] (mouth opening), orbit axes in [-1, 1] +// (camera orbit rate; the inertia/smoothing lives downstream +// in the camera code, sources report raw deflection). Stable +// object reference, updated in place. Channels a source does +// not drive stay 0. +// pressed Plain object of booleans (stable reference) mirroring which +// physical controls are currently down. HUD highlighting +// only - never game logic. +// isActive() Whether the source currently claims authority over the +// continuous command. Keyboard: any move key held. Gamepad: +// stick deflected, and it keeps the claim until its smoothed +// command settles back to ~zero. +// init() Attach event listeners / hardware hooks. Called by +// Controller.init(), NOT at construction (rl.js constructs +// the sources early but arms the listeners at the same point +// in the boot where they historically went live). +// dispose() Detach everything init() attached. +// poll(dt) Per-frame tick (dt in seconds, clamped by the caller). +// Read the hardware, update command/axes/pressed in place, +// and fire edge-triggered actions via this.onAction(name). +// onAction (name, meta?) => void, assigned by the Controller at +// registration. Sources may call it from poll() (gamepad +// button edges) or straight from event handlers (keyboard: +// keeps the historical press-to-effect latency). +// +// ── Arbitration ────────────────────────────────────────────────────────── +// Continuous command: sources are registered in PRIORITY order (first = +// highest). Each frame the first source reporting isActive() owns the +// twist; when none is active the LAST registered source's command is used +// as the fallback (it reads zero when idle). With [gamepad, keyboard] this +// reproduces the historical `padActive ? padCmd : velCmd` exactly: live +// sticks win over held keys, and the keyboard takes back over once the +// pad's smoothed command has settled. +// Aux axes are merged across ALL sources regardless of who owns the twist +// (the pad triggers drive the jaw even while walking on the keyboard): +// jaw = max over sources, orbit = largest-magnitude value per axis. +// +// ── Actions ────────────────────────────────────────────────────────────── +// roll one-shot roll (crouch-glide in roller mode; game decides) +// kickL, kickR one-shot kicks, explicit foot +// alternateKick one-shot kick, feet alternated by the game +// spawnBall pop / respawn the kickable ball +// sitToggle sit <-> stand (game gates it to legs mode) +// locoToggle legs <-> rollers switch +// chaseToggle chase camera on/off +// reset full sim reset (Space) +// walk back to the walk/run mode (pad DpadUp short press) +// quack chirp + jaw flap (pad RT edge, Schmitt-triggered) +// +// ── Input lock ─────────────────────────────────────────────────────────── +// setLocked(true) zeroes getCommand() - the twist gate used while the +// entrance/respawn ceremony plays. Discrete actions still dispatch: each +// game trigger applies its own lock policy (e.g. Space-reset and the +// chase-cam toggle historically work while locked, kicks don't). + +const ZERO_CMD = new Float32Array(3); + +export class Controller { + #sources = []; + #listeners = new Map(); // action -> Set(cb) + #locked = false; + #axes = { jaw: 0, orbitX: 0, orbitY: 0 }; + + constructor({ sources = [] } = {}) { + for (const s of sources) this.addSource(s); + } + + // Register in priority order (first registered wins arbitration ties). + addSource(source) { + source.onAction = (action, meta) => + this.#dispatch(action, { source: source.id, ...meta }); + this.#sources.push(source); + } + + // Read-only source list, for advanced per-source queries the merged view + // can't answer (e.g. "is ANY source commanding a turn right now?"). + get sources() { + return this.#sources; + } + + // Arm every source's listeners/hardware hooks. + init() { + for (const s of this.#sources) s.init?.(); + } + + dispose() { + for (const s of this.#sources) s.dispose?.(); + } + + // Per-frame tick: poll every source (they fire their edge actions from + // inside poll), then merge the aux axes. + update(dt) { + for (const s of this.#sources) s.poll?.(dt); + let jaw = 0, ox = 0, oy = 0; + for (const s of this.#sources) { + const a = s.axes; + if (!a) continue; + jaw = Math.max(jaw, a.jaw ?? 0); + if (Math.abs(a.orbitX ?? 0) > Math.abs(ox)) ox = a.orbitX; + if (Math.abs(a.orbitY ?? 0) > Math.abs(oy)) oy = a.orbitY; + } + this.#axes.jaw = jaw; + this.#axes.orbitX = ox; + this.#axes.orbitY = oy; + } + + setLocked(v) { + this.#locked = !!v; + } + + get locked() { + return this.#locked; + } + + // Merged continuous twist [vx, vy, wz] (see the arbitration notes above). + // Returns live source arrays without copying - treat as read-only. + getCommand() { + if (this.#locked) return ZERO_CMD; + for (const s of this.#sources) if (s.isActive()) return s.command; + const fallback = this.#sources[this.#sources.length - 1]; + return fallback ? fallback.command : ZERO_CMD; + } + + // Merged aux axes { jaw, orbitX, orbitY }, refreshed by update(). + getAxes() { + return this.#axes; + } + + // Any source claiming twist authority (HUD "user is driving" signal). + anyActive() { + return this.#sources.some((s) => s.isActive()); + } + + // Per-source pressed snapshots for HUD keycap highlighting: + // { keyboard: {...}, gamepad: {...} }. + getPressed() { + const out = {}; + for (const s of this.#sources) out[s.id] = s.pressed ?? {}; + return out; + } + + // Subscribe to a discrete action; cb(meta) with meta.source = source id. + // Returns an unsubscribe function. + on(action, cb) { + let set = this.#listeners.get(action); + if (!set) this.#listeners.set(action, (set = new Set())); + set.add(cb); + return () => set.delete(cb); + } + + #dispatch(action, meta) { + const set = this.#listeners.get(action); + if (!set) return; + for (const cb of set) cb(meta); + } +} diff --git a/controls/gamepad.js b/controls/gamepad.js new file mode 100644 index 0000000..fdec581 --- /dev/null +++ b/controls/gamepad.js @@ -0,0 +1,153 @@ +// Gamepad input source (see controller.js for the source interface +// contract). Same mapping as the robot runtime (microduck_runtime): +// +// Left stick vertical = vx (asymmetric fwd/back), horizontal = turn, +// EMA-smoothed like the runtime's cmd_alpha. +// Right stick camera orbit rate (reported raw in axes.orbitX/Y; the +// velocity smoothing / coasting lives downstream in the +// camera code). R3 toggles the chase cam. +// X roll (crouch-glide in roller mode - game decides) +// Y ball spawn/respawn +// RB / LB right / left kick +// DpadDown sit <-> stand +// DpadUp short press = back to run; HOLD ~1 s = legs <-> rollers +// (like the robot's 3 s hold, shortened for the web) +// RT / LT analog jaw (max of both); RT edge quacks through a +// Schmitt trigger (fire at >= 0.35, re-arm below 0.2 - a +// single threshold re-fires on jitter, which is how one +// squeeze used to quack several times). + +const PAD_DEADZONE = 0.15; +const PAD_ALPHA = 0.12; // EMA smoothing toward the stick target +const dz = (v) => (Math.abs(v) < PAD_DEADZONE ? 0 : v); + +// Standard-mapping button indices. +const BTN_X = 2, BTN_Y = 3, BTN_LB = 4, BTN_RB = 5, BTN_LT = 6, BTN_RT = 7; +const BTN_R3 = 11, BTN_DPAD_UP = 12, BTN_DPAD_DOWN = 13; + +const DPAD_UP_HOLD_MS = 1000; // hold-to-switch-loco duration + +export class GamepadSource { + id = "gamepad"; + connected = false; + command = new Float32Array(3); // [vx, 0, wz], EMA-smoothed + axes = { jaw: 0, orbitX: 0, orbitY: 0 }; + pressed = { + x: false, y: false, rb: false, lb: false, r3: false, + dpadDown: false, dpadUp: false, + }; + onAction = () => {}; // assigned by the Controller at registration + + #getVelocityLimits; + #active = false; // owns twist authority (stick input, until EMA settles) + #dpadUpAt = 0; // wall-clock of the current DpadUp press + #dpadUpFired = false; // latch: one loco switch per hold + #rtArmed = true; // Schmitt trigger state for the RT quack + + constructor({ getVelocityLimits }) { + this.#getVelocityLimits = getVelocityLimits; + } + + init() {} // poll-based: nothing to attach + dispose() {} + + isActive() { + return this.#active; + } + + poll() { + const prev = this.pressed; + const gp = [...(navigator.getGamepads?.() ?? [])].find((p) => p && p.connected); + this.connected = !!gp; + if (!gp) { + if (this.#active) { + this.#active = false; + this.command.fill(0); + this.axes.jaw = 0; + } + this.axes.orbitX = 0; + this.axes.orbitY = 0; + return; + } + const now = performance.now(); + + // Left stick only: vertical = forward/back, horizontal = turn. + // (No strafe; the right stick doesn't drive movement.) + const lx = dz(gp.axes[0] ?? 0), ly = dz(gp.axes[1] ?? 0); + const up = -ly; // browser sticks report up as -1 + const [limF, limB, limA] = this.#getVelocityLimits(); + const target = [ + up >= 0 ? up * limF : up * -limB, + 0, + -lx * limA, + ]; + for (let i = 0; i < 3; i++) this.command[i] += PAD_ALPHA * (target[i] - this.command[i]); + // Sticks grab command authority on first input, release when back at + // rest (then the keyboard takes over again through the Controller's + // arbitration). + const stickInput = lx !== 0 || ly !== 0; + if (stickInput) this.#active = true; + else if ( + this.#active && + Math.abs(this.command[0]) + Math.abs(this.command[1]) + Math.abs(this.command[2]) < 0.01 + ) { + this.#active = false; + this.command.fill(0); + } + + // Right stick: raw (deadzoned) orbit rate. Reported every frame - the + // downstream camera step needs the zeros too so a released stick + // coasts to a stop. + this.axes.orbitX = dz(gp.axes[2] ?? 0); + this.axes.orbitY = dz(gp.axes[3] ?? 0); + + // R3 (right stick click): chase-cam toggle, gamepad twin of KeyC. + const r3 = !!gp.buttons[BTN_R3]?.pressed; + if (r3 && !prev.r3) this.onAction("chaseToggle"); + prev.r3 = r3; + + const x = !!gp.buttons[BTN_X]?.pressed; + if (x && !prev.x) this.onAction("roll"); + prev.x = x; + + const y = !!gp.buttons[BTN_Y]?.pressed; + if (y && !prev.y) this.onAction("spawnBall"); + prev.y = y; + + const rb = !!gp.buttons[BTN_RB]?.pressed; + if (rb && !prev.rb) this.onAction("kickR"); + prev.rb = rb; + const lb = !!gp.buttons[BTN_LB]?.pressed; + if (lb && !prev.lb) this.onAction("kickL"); + prev.lb = lb; + + const dpadDown = !!gp.buttons[BTN_DPAD_DOWN]?.pressed; + if (dpadDown && !prev.dpadDown) this.onAction("sitToggle"); + prev.dpadDown = dpadDown; + + // DpadUp: short press = back to run; hold fires ONE loco switch. + const dpadUp = !!gp.buttons[BTN_DPAD_UP]?.pressed; + if (dpadUp && !prev.dpadUp) { + this.#dpadUpAt = now; + this.#dpadUpFired = false; + this.onAction("walk"); + } + if (dpadUp && !this.#dpadUpFired && now - this.#dpadUpAt >= DPAD_UP_HOLD_MS) { + this.#dpadUpFired = true; // latch: one switch per hold + this.onAction("locoToggle"); + } + prev.dpadUp = dpadUp; + + // Triggers drive the mouth (max of both); RT quacks on its + // Schmitt-triggered rising edge. + const rt = gp.buttons[BTN_RT]?.value ?? 0; + const lt = gp.buttons[BTN_LT]?.value ?? 0; + this.axes.jaw = Math.max(rt, lt); + if (this.#rtArmed && rt >= 0.35) { + this.onAction("quack"); + this.#rtArmed = false; + } else if (!this.#rtArmed && rt < 0.2) { + this.#rtArmed = true; + } + } +} diff --git a/controls/keyboard.js b/controls/keyboard.js new file mode 100644 index 0000000..510877e --- /dev/null +++ b/controls/keyboard.js @@ -0,0 +1,116 @@ +// Keyboard input source (see controller.js for the source interface +// contract). Hold-to-command movement keys plus one-shot action keys. +// +// e.code is the PHYSICAL key position, so one map covers QWERTY and AZERTY: +// arrows / WASD (ZQSD) run + turn. No strafe - vy stays 0. + +const KEYMAP = { + ArrowUp: "fwd", KeyW: "fwd", + ArrowDown: "back", KeyS: "back", + ArrowLeft: "turnl", KeyA: "turnl", + ArrowRight: "turnr", KeyD: "turnr", +}; + +// One-shot keys -> controller actions. Physical Q/E (A/E on AZERTY) are the +// explicit left / right kicks, mirroring the pad's LB / RB; F alternates. +const ACTION_KEYS = { + KeyB: "spawnBall", + KeyC: "chaseToggle", + KeyM: "locoToggle", + KeyR: "roll", + KeyQ: "kickL", + KeyE: "kickR", + KeyF: "alternateKick", +}; + +export class KeyboardSource { + id = "keyboard"; + connected = true; // a keyboard is always assumed present + command = new Float32Array(3); // [vx, 0, wz], scaled to velocity limits + axes = { jaw: 0, orbitX: 0, orbitY: 0 }; // keyboard drives none of these + pressed = { fwd: false, back: false, turnl: false, turnr: false }; + onAction = () => {}; // assigned by the Controller at registration + + #held = new Set(); + #getVelocityLimits; + // Limits snapshot so poll() only re-maps held keys when they actually + // change (the legs <-> rollers switch); event handlers do the live work. + #lims = [NaN, NaN, NaN]; + #onKeyDown = null; + #onKeyUp = null; + #onBlur = null; + + constructor({ getVelocityLimits }) { + this.#getVelocityLimits = getVelocityLimits; + } + + init() { + this.#onKeyDown = (e) => { + if (e.repeat) return; + // Space always resets, and must not scroll the page. + if (e.code === "Space") { + e.preventDefault(); + this.onAction("reset"); + return; + } + const action = ACTION_KEYS[e.code]; + if (action) { + this.onAction(action); + return; + } + const move = KEYMAP[e.code]; + if (!move) return; + e.preventDefault(); + this.#held.add(move); + this.#refresh(); + }; + this.#onKeyUp = (e) => { + const move = KEYMAP[e.code]; + if (!move) return; + this.#held.delete(move); + this.#refresh(); + }; + // Losing window focus drops every held key (keyup events are missed). + this.#onBlur = () => { + this.#held.clear(); + this.#refresh(); + }; + window.addEventListener("keydown", this.#onKeyDown); + window.addEventListener("keyup", this.#onKeyUp); + window.addEventListener("blur", this.#onBlur); + } + + dispose() { + window.removeEventListener("keydown", this.#onKeyDown); + window.removeEventListener("keyup", this.#onKeyUp); + window.removeEventListener("blur", this.#onBlur); + } + + // Claims twist authority while any movement key is held. + isActive() { + return this.#held.size > 0; + } + + // The command is event-driven (recomputed on keydown/keyup/blur, so a + // press lands the same tick, not on the next frame); poll only re-maps + // held keys onto fresh velocity limits after a locomotion switch. + poll() { + const [limF, limB, limA] = this.#getVelocityLimits(); + if (limF !== this.#lims[0] || limB !== this.#lims[1] || limA !== this.#lims[2]) { + this.#refresh(); + } + } + + #refresh() { + const lims = this.#getVelocityLimits(); + const [limF, limB, limA] = lims; + this.#lims = lims; + const held = this.#held; + this.command[0] = held.has("fwd") ? limF : held.has("back") ? limB : 0; + this.command[2] = held.has("turnl") ? limA : held.has("turnr") ? -limA : 0; + this.pressed.fwd = held.has("fwd"); + this.pressed.back = held.has("back"); + this.pressed.turnl = held.has("turnl"); + this.pressed.turnr = held.has("turnr"); + } +} diff --git a/fx/demo-glitch.html b/fx/demo-glitch.html new file mode 100644 index 0000000..91b790d --- /dev/null +++ b/fx/demo-glitch.html @@ -0,0 +1,140 @@ + + + + + + fx demo - glitch materialization + + + + +
+ +
loading rig...
+ + + diff --git a/fx/demo-wireframe.html b/fx/demo-wireframe.html new file mode 100644 index 0000000..24d7dbb --- /dev/null +++ b/fx/demo-wireframe.html @@ -0,0 +1,113 @@ + + + + + + FX demo - holographic wireframe scan-up + + + + +
+
+ FX: HOLOGRAPHIC WIREFRAME SCAN-UP
+ +
+ + + diff --git a/fx/fx-glitch.js b/fx/fx-glitch.js new file mode 100644 index 0000000..085e500 --- /dev/null +++ b/fx/fx-glitch.js @@ -0,0 +1,311 @@ +// Cyberpunk 2077-style glitch materialization for the Microduck rig. +// +// The duck appears as stacked horizontal slices that flicker in and out +// with random screen-space horizontal offsets, converging to their true +// position. Chromatic aberration is faked with two additive silhouette +// ghosts (cyan / orange) offset left-right, digital noise blocks flash +// emissive or invert the surface colour, and a final warm flash "snaps" +// everything crisp. +// +// Implementation: onBeforeCompile injections on CACHED clones of the rig +// materials (same pattern and same program-cache trap as the entrance +// dissolve in rl.js: clones are cached per original material and reused +// on every replay, each with a unique customProgramCacheKey, otherwise +// three.js would reuse a compiled program without re-binding uniforms). +// All uniforms are shared singleton objects so one JS write per frame +// drives every material. + +export const name = "glitch"; + +const DURATION = 1.15; // seconds +const TICK_HZ = 30; // discrete glitch update rate (digital feel) +const SLICES = 24; + +// ── Shared uniforms (one instance drives all cloned materials) ────────── +const uP = { value: 0 }; // progress 0..1 (0 = invisible) +const uSeed = { value: 0 }; // discrete tick, drives all hashes +const uAmp = { value: 0 }; // slice offset amplitude (view-space meters) +const uFlash = { value: 0 }; // final snap flash intensity +const uGhostOff = { value: 0 }; // RGB-split ghost separation (meters) +const uB = { value: null }; // vec4(minY, height, sliceH, 0) world bounds + +let T = null; // injected THREE namespace +let rigRef = null; +let playing = false; +let scrubbed = false; +let done = true; +let elapsed = 0; + +// Deterministic JS-side hash, mirrors the GLSL one. +const jhash = (n) => { + const s = Math.sin(n * 127.1 + 311.7) * 43758.5453; + return s - Math.floor(s); +}; + +// ── GLSL injection ────────────────────────────────────────────────────── +const GLSL_HELPERS = /* glsl */ ` +varying vec3 vGlW; +uniform float uGlP, uGlSeed; +uniform vec4 uGlB; +float glHash2(vec2 q) { return fract(sin(dot(q, vec2(127.1, 311.7))) * 43758.5453); } +vec3 glHash3(vec2 q) { return vec3(glHash2(q), glHash2(q + 17.17), glHash2(q + 31.3)); } +`; + +function injectGlitch(shader, { signU = null, ghost = false } = {}) { + shader.uniforms.uGlP = uP; + shader.uniforms.uGlSeed = uSeed; + shader.uniforms.uGlAmp = uAmp; + shader.uniforms.uGlFlash = uFlash; + shader.uniforms.uGlGhostOff = uGhostOff; + shader.uniforms.uGlSign = signU ?? { value: 0 }; + shader.uniforms.uGlB = uB; + + shader.vertexShader = shader.vertexShader + .replace( + "#include ", + "#include \n" + GLSL_HELPERS + "uniform float uGlAmp, uGlSign, uGlGhostOff;\n", + ) + .replace( + "#include ", + /* glsl */ `#include +vec4 glWp = modelMatrix * vec4(transformed, 1.0); +vGlW = glWp.xyz; +if (uGlP < 1.0) { + float glSid = floor((glWp.y - uGlB.x) / uGlB.z); + // Fraction of moving slices shrinks as p rises: convergence. + float glMove = step(0.25 + 0.62 * uGlP, glHash2(vec2(glSid * 1.71 + 4.2, uGlSeed + 13.0))); + float glOff = (glHash2(vec2(glSid, uGlSeed)) - 0.5) * 2.0 * uGlAmp * glMove; + // Occasional whole-body frame jump for one tick. + if (glHash2(vec2(uGlSeed, 3.17)) > 0.86) { + glOff += (glHash2(vec2(uGlSeed, 9.91)) - 0.5) * 3.0 * uGlAmp; + } + glOff += uGlSign * uGlGhostOff; + // View-space X = screen-horizontal, the classic spawn-glitch axis. + mvPosition.x += glOff; + gl_Position = projectionMatrix * mvPosition; +}`, + ); + + const sliceDiscard = /* glsl */ ` +float glEdge = 0.0, glInvAmt = 0.0; +vec3 glAddC = vec3(0.0); +if (uGlP < 1.0) { + float glSid = floor((vGlW.y - uGlB.x) / uGlB.z); + float glH = glHash2(vec2(glSid, uGlSeed + 0.7)); + float glVis = smoothstep(0.02, 0.82, uGlP) * 1.35; + // Two-three full-silhouette pre-flashes right at the start. + float glPre = (uGlP > 0.015 && uGlP < 0.14 && glHash2(vec2(uGlSeed, 7.7)) > 0.72) ? 1.0 : 0.0; + glVis = max(glVis, glPre); + if (glH > glVis) discard; + glEdge = max(glPre * 0.7, (1.0 - smoothstep(0.0, 0.3, glVis - glH)) * (1.0 - uGlP)); +${ghost ? "" : /* glsl */ ` + // Digital noise blocks: world-space rectangular patches, re-seeded per + // tick. Some invert the surface colour, most flash emissive. + vec2 glBq = floor((vGlW.xy + vec2(vGlW.z * 0.7, vGlW.z * 0.3)) * 52.0 + + vec2(uGlSeed * 0.373, uGlSeed * 0.719)); + vec3 glBh = glHash3(glBq + uGlSeed); + float glBGate = (1.0 - uGlP) * 0.16; + if (glBh.x > 1.0 - glBGate) { + if (glBh.y < 0.3) glInvAmt = 1.0; + else glAddC += (glBh.z < 0.6 ? vec3(0.15, 1.3, 1.5) + : (glBh.z < 0.85 ? vec3(1.4) : vec3(1.6, 0.5, 0.06))) * (0.7 + glBh.y); + } + // Faint rolling scanline shimmer. + glAddC += vec3(0.05, 0.3, 0.35) * (1.0 - uGlP) * 0.18 + * step(0.6, fract(vGlW.y * 150.0 - uGlSeed * 0.21)); +`} +}`; + + const emissiveOut = ghost + ? "" // ghosts are pre-tinted additive silhouettes, no extra emissive + : /* glsl */ ` +gl_FragColor.rgb = mix(gl_FragColor.rgb, vec3(1.1) - gl_FragColor.rgb, glInvAmt); +gl_FragColor.rgb += glAddC; +gl_FragColor.rgb += glEdge * vec3(0.25, 1.15, 1.35); +gl_FragColor.rgb += uGlFlash * vec3(1.25, 0.7, 0.35);`; + + shader.fragmentShader = shader.fragmentShader + .replace( + "#include ", + "#include \n" + GLSL_HELPERS + "uniform float uGlFlash;\n", + ) + .replace("#include ", "#include " + sliceDiscard) + .replace("#include ", "#include " + emissiveOut); +} + +// ── Material clones (cached, never disposed between replays) ──────────── +const clones = new Map(); // original material uuid -> glitch clone +let cloneNonce = 0; +let saved = null; // Array<[mesh, originalMat, cloneMat]> while active + +function cloneFor(orig) { + let m = clones.get(orig.uuid); + if (m) return m; + m = orig.clone(); + m.onBeforeCompile = (shader) => injectGlitch(shader); + const key = `microduck-glitch-${cloneNonce++}`; + m.customProgramCacheKey = () => key; + m.needsUpdate = true; + clones.set(orig.uuid, m); + return m; +} + +function applyGlitchMats() { + if (saved) return; + saved = []; + rigRef.placer.traverse((o) => { + if (!o.isMesh || o.userData.glGhost) return; + const orig = o.material; + const m = cloneFor(orig); + saved.push([o, orig, m]); + o.material = m; + }); +} + +function restoreMats() { + if (!saved) return; + for (const [mesh, orig, clone] of saved) { + if (mesh.material === clone) mesh.material = orig; + } + saved = null; +} + +// ── RGB-split ghosts ──────────────────────────────────────────────────── +// Two additive unlit silhouette copies of every rig mesh (cyan right, +// orange left), sharing the slice-discard shader so their outline always +// matches the sliced duck. Added as children of each mesh: they inherit +// the full kinematic transform for free. +let ghostMats = []; +let ghostMeshes = []; + +function makeGhostMat(r, g, b, sign) { + const m = new T.MeshBasicMaterial({ + transparent: true, + opacity: 0, + depthWrite: false, + blending: T.AdditiveBlending, + }); + m.color.setRGB(r, g, b); + const signU = { value: sign }; + m.onBeforeCompile = (shader) => injectGlitch(shader, { signU, ghost: true }); + m.customProgramCacheKey = () => `microduck-glitch-ghost-${sign > 0 ? "r" : "l"}`; + return m; +} + +function buildGhosts() { + ghostMats = [makeGhostMat(0.0, 1.1, 1.3, +1), makeGhostMat(1.3, 0.35, 0.02, -1)]; + const hosts = []; + rigRef.placer.traverse((o) => { + if (o.isMesh && !o.userData.glGhost) hosts.push(o); + }); + for (const mesh of hosts) { + for (const mat of ghostMats) { + const gm = new T.Mesh(mesh.geometry, mat); + gm.userData.glGhost = true; + gm.renderOrder = 2; + gm.frustumCulled = false; + gm.visible = false; + mesh.add(gm); + ghostMeshes.push(gm); + } + } +} + +function setGhostsVisible(v) { + for (const gm of ghostMeshes) gm.visible = v; +} + +// ── Deterministic per-progress uniform state ──────────────────────────── +// Everything derives from p alone (tick included), so setProgress(p) gives +// reproducible frames for debugging/screenshots. +function setUniformsFor(p) { + uP.value = p; + const tick = Math.floor(p * DURATION * TICK_HZ); + uSeed.value = tick; + const h = uB.value.y; // duck height + const gate = 0.35 + 0.65 * jhash(tick + 3.7); + uAmp.value = p < 0.92 ? h * 0.3 * Math.pow(1 - p, 1.3) * gate : 0; + uGhostOff.value = + p > 0.04 && p < 0.9 ? (0.06 + 0.3 * jhash(tick + 11.1)) * (1 - p) * h : 0; + uFlash.value = p > 0.86 ? 1.5 * Math.exp(-16 * (p - 0.86) * DURATION) : 0; + const ghostOp = + p > 0.04 && p < 0.9 ? (0.05 + 0.32 * (1 - p)) * (0.4 + 0.6 * jhash(tick + 5.5)) : 0; + for (const m of ghostMats) m.opacity = ghostOp; +} + +function finish() { + uP.value = 1; + uFlash.value = 0; + setGhostsVisible(false); + restoreMats(); + playing = false; + done = true; +} + +// ── Public interface ──────────────────────────────────────────────────── +export function init({ THREE, scene, rig, camera, renderer }) { + T = THREE; + rigRef = rig; + rig.placer.updateWorldMatrix(true, true); + const box = new T.Box3().setFromObject(rig.placer); + const minY = box.min.y; + const height = Math.max(1e-3, box.max.y - minY); + uB.value = new T.Vector4(minY, height, height / SLICES, 0); + buildGhosts(); + // Duck starts invisible: p=0 discards every fragment. + uP.value = 0; + applyGlitchMats(); + done = true; + playing = false; +} + +export function start() { + applyGlitchMats(); + setGhostsVisible(true); + elapsed = 0; + playing = true; + scrubbed = false; + done = false; + setUniformsFor(0); +} + +export function update(dt, t) { + if (!playing || scrubbed) return; + elapsed += dt; + const p = Math.min(1, elapsed / DURATION); + setUniformsFor(p); + if (p >= 1) finish(); +} + +export function isDone() { + return done; +} + +export function dispose() { + restoreMats(); + for (const gm of ghostMeshes) gm.parent?.remove(gm); + ghostMeshes = []; + for (const m of ghostMats) m.dispose(); + ghostMats = []; + for (const m of clones.values()) m.dispose(); + clones.clear(); + playing = false; + done = true; + rigRef = null; +} + +// Debug hook: freeze the effect at an exact progress value (deterministic, +// used by the demo page for mid-effect screenshots). start() resumes +// normal playback. +export function setProgress(p) { + scrubbed = true; + if (p >= 1) { + finish(); + return; + } + playing = true; + done = false; + applyGlitchMats(); + setGhostsVisible(true); + setUniformsFor(Math.max(0, p)); +} diff --git a/fx/fx-wireframe.js b/fx/fx-wireframe.js new file mode 100644 index 0000000..84a8338 --- /dev/null +++ b/fx/fx-wireframe.js @@ -0,0 +1,320 @@ +// Holographic wireframe scan-up materialization effect. +// +// Sequence (~0.9 s, reversible): +// 1. An invisible scan height rises from the floor through the target; +// the boundary reads on the surface itself (bright wireframe band +// right under the scan height - no standalone disc geometry). +// 2. Below the scan line the target shows as a flickering additive +// wireframe hologram (scanline stripes, brightness boost near the +// scan line). +// 3. A second, fainter "solidify" line trails behind: fragments below +// it render the real PBR materials (world-Y clip injected via +// onBeforeCompile), with a hot edge right at the line. Once it +// clears the top the original materials are restored directly. +// +// Played backwards, the same timeline de-materializes (solid peel -> +// hologram -> gone). Used for the ball despawn; the duck only ever +// materializes. +// +// createWireframeFx() returns an independent instance (duck and ball +// scan at the same time without sharing uniforms). The module-level +// named exports wrap a singleton so demo-wireframe.html is unchanged. +// +// Instance interface: +// init({ THREE, scene, rig | root, camera, renderer, hidden? }) +// start() / startReverse() / update(dt) / isDone() / restore() / dispose() +// Extra: setProgress(p), playing, reversing, TOTAL_S. + +export const name = "wireframe"; + +// ── Timeline (seconds) ────────────────────────────────────────────────── +const SCAN_S = 0.6; // wireframe scan line: floor -> top +const SOLID_DELAY_S = 0.3; // solidify line starts this long after the scan +const SOLID_S = 0.6; // solidify line: floor -> top +export const TOTAL_S = SOLID_DELAY_S + SOLID_S; // 0.9 + +const clamp01 = (x) => Math.min(Math.max(x, 0), 1); +// Ease-out only: the rise starts at full speed the instant the scan cues +// (a smoothstep ease-in reads as a stall near the feet) and lands softly. +const ease = (x) => 1 - (1 - x) * (1 - x); + +// Deterministic pseudo-random flicker so setProgress(p) captures are +// reproducible (no Math.random). +const hash = (x) => { + const s = Math.sin(x * 127.1) * 43758.5453; + return s - Math.floor(s); +}; +const flickerAt = (time) => { + let f = 0.82 + 0.18 * hash(Math.floor(time * 60) + 0.5); + if (hash(Math.floor(time * 24) + 7.7) < 0.12) f *= 0.5; // dropouts + return f; +}; + +// Unique customProgramCacheKey values across every instance: a fresh +// clone of the same source material would otherwise reuse the compiled +// program without re-running onBeforeCompile, leaving clip uniforms +// unbound (see the dissolve comment in rl.js). +let clipNonce = 0; + +const BOTTOM_PAD = 0.025; + +export function createWireframeFx() { + // Per-instance uniforms: duck and ball must not share a scan height. + const uScanY = { value: -1e3 }; + const uSolidY = { value: -1e3 }; + const uFlicker = { value: 1 }; + const uTime = { value: 0 }; + + let ctx = null; // { THREE, scene, root, camera, renderer } + let t = 0; + let dir = 1; // +1 materialize, -1 dematerialize + let playing = false; + let finished = false; + + let minY = 0; + let spanY = 0.3; + + const clipClones = new Map(); + let clipSaved = null; // Array<[mesh, originalMat, cloneMat]> while active + + let wireMat = null; + let wireMeshes = []; + + const isTargetMesh = (o) => o.isMesh && !o.userData.fxOverlay; + + function clipCloneFor(orig) { + let m = clipClones.get(orig.uuid); + if (m) return m; + m = orig.clone(); + m.onBeforeCompile = (shader) => { + shader.uniforms.uFxSolidY = uSolidY; + shader.vertexShader = shader.vertexShader + .replace("#include ", "#include \nvarying vec3 vFxW;") + .replace( + "#include ", + "#include \nvFxW = (modelMatrix * vec4(transformed, 1.0)).xyz;", + ); + shader.fragmentShader = shader.fragmentShader + .replace( + "#include ", + "#include \nvarying vec3 vFxW;\nuniform float uFxSolidY;", + ) + .replace( + "#include ", + /* glsl */ `#include +float fxEdge = 0.0; +if (vFxW.y > uFxSolidY) discard; +fxEdge = 1.0 - smoothstep(0.002, 0.018, uFxSolidY - vFxW.y);`, + ) + .replace( + "#include ", + /* glsl */ `#include +gl_FragColor.rgb += fxEdge * vec3(0.95, 0.32, 0.05);`, + ); + }; + const key = `microduck-fx-wireframe-${clipNonce++}`; + m.customProgramCacheKey = () => key; + m.needsUpdate = true; + clipClones.set(orig.uuid, m); + return m; + } + + function applyClipMaterials() { + if (clipSaved) return; + clipSaved = []; + ctx.root.traverse((o) => { + if (!isTargetMesh(o)) return; + const orig = o.material; + const clone = clipCloneFor(orig); + clipSaved.push([o, orig, clone]); + o.material = clone; + }); + } + + function restoreClipMaterials() { + if (!clipSaved) return; + for (const [mesh, orig, clone] of clipSaved) { + if (mesh.material === clone) mesh.material = orig; + } + clipSaved = null; + } + + function makeWireMaterial(THREE) { + return new THREE.ShaderMaterial({ + uniforms: { uScanY, uSolidY, uFlicker, uTime }, + vertexShader: /* glsl */ ` + varying vec3 vW; + void main() { + vec4 wp = modelMatrix * vec4(position, 1.0); + vW = wp.xyz; + gl_Position = projectionMatrix * viewMatrix * wp; + }`, + fragmentShader: /* glsl */ ` + uniform float uScanY, uSolidY, uFlicker, uTime; + varying vec3 vW; + void main() { + if (vW.y > uScanY || vW.y < uSolidY) discard; + float lead = 1.0 - smoothstep(0.0, 0.06, uScanY - vW.y); + float tail = smoothstep(0.0, 0.018, vW.y - uSolidY); + float stripes = 0.7 + 0.3 * sin(vW.y * 900.0 - uTime * 45.0); + vec3 c = vec3(1.0, 0.34, 0.06) * (0.55 + 1.6 * lead); + float a = (0.10 + 0.40 * lead) * stripes * tail * uFlicker; + gl_FragColor = vec4(c, a); + }`, + wireframe: true, + transparent: true, + blending: THREE.AdditiveBlending, + depthWrite: false, + depthTest: true, + }); + } + + function buildWireOverlays(THREE, root) { + wireMat = makeWireMaterial(THREE); + root.traverse((o) => { + if (!isTargetMesh(o)) return; + // Child of the source mesh with identity transform so the overlay + // follows whatever pose the caller writes (duck joints or ball qpos). + const w = new THREE.Mesh(o.geometry, wireMat); + w.userData.fxOverlay = true; + w.renderOrder = 5; + w.visible = false; + o.add(w); + wireMeshes.push(w); + }); + } + + function applyAt(time) { + const scanP = ease(clamp01(time / SCAN_S)); + const solidP = ease(clamp01((time - SOLID_DELAY_S) / SOLID_S)); + uFlicker.value = flickerAt(time); + uTime.value = time; + + const jitter = scanP > 0 && scanP < 1 ? (hash(time * 41.3) - 0.5) * 0.02 * spanY : 0; + uScanY.value = minY + spanY * scanP + jitter; + uSolidY.value = minY + spanY * solidP; + + const wiresOn = time > 0 && solidP < 1; + for (const w of wireMeshes) w.visible = wiresOn; + } + + function finish() { + // Forward: restore the real materials (fully solid). Reverse: leave + // the clip parked at t=0 (fully hidden) so restoring wouldn't flash + // the solid mesh for a frame; the caller hides the object then + // restore()s. + if (dir > 0) restoreClipMaterials(); + else applyAt(0); + for (const w of wireMeshes) w.visible = false; + finished = true; + playing = false; + } + + function computeRange() { + const { THREE, root } = ctx; + root.updateWorldMatrix(true, true); + const box = new THREE.Box3().setFromObject(root); + minY = Math.min(box.min.y, 0) - BOTTOM_PAD; + spanY = Math.max(box.max.y - minY, 0.04) * 1.06; + } + + function arm(nextDir, resetT) { + dir = nextDir; + if (resetT) t = nextDir > 0 ? 0 : TOTAL_S; + finished = false; + computeRange(); + applyClipMaterials(); + applyAt(t); + playing = true; + } + + function init({ THREE, scene, rig, root, camera, renderer, hidden = true }) { + const target = root ?? rig.placer; + ctx = { THREE, scene, root: target, camera, renderer }; + computeRange(); + buildWireOverlays(THREE, target); + t = 0; + dir = 1; + finished = false; + playing = false; + if (hidden) { + applyClipMaterials(); + applyAt(0); + } + } + + function start() { + // Mid-reverse: keep the current t and turn around. Fresh play: from 0. + arm(1, !playing); + } + + function startReverse() { + arm(-1, !playing); + } + + function update(dt) { + if (!playing || finished) return; + t += dir * dt; + if (dir > 0 && t >= TOTAL_S) { + t = TOTAL_S; + applyAt(t); + finish(); + return; + } + if (dir < 0 && t <= 0) { + t = 0; + applyAt(t); + finish(); + return; + } + applyAt(t); + } + + function isDone() { + return finished; + } + + function setProgress(p) { + playing = false; + finished = false; + dir = 1; + computeRange(); + applyClipMaterials(); + t = clamp01(p) * TOTAL_S; + applyAt(t); + } + + function restore() { + restoreClipMaterials(); + } + + function dispose() { + restoreClipMaterials(); + for (const w of wireMeshes) w.parent?.remove(w); + wireMeshes = []; + wireMat?.dispose(); + wireMat = null; + ctx = null; + playing = false; + finished = false; + } + + return { + init, start, startReverse, update, isDone, setProgress, restore, dispose, + get playing() { return playing; }, + get reversing() { return playing && dir < 0; }, + TOTAL_S, + }; +} + +// Singleton for the duck (and the isolated demo page). New callers that +// need a second scan (the ball) go through createWireframeFx(). +const singleton = createWireframeFx(); +export const init = (...a) => singleton.init(...a); +export const start = (...a) => singleton.start(...a); +export const startReverse = (...a) => singleton.startReverse(...a); +export const update = (...a) => singleton.update(...a); +export const isDone = (...a) => singleton.isDone(...a); +export const setProgress = (...a) => singleton.setProgress(...a); +export const restore = (...a) => singleton.restore(...a); +export const dispose = (...a) => singleton.dispose(...a); diff --git a/ghosts.js b/ghosts.js index 004202d..5353a3b 100644 --- a/ghosts.js +++ b/ghosts.js @@ -2,7 +2,7 @@ // duck state over WebRTC and renders the other visitors as translucent // ducks. There is no backend - this is a static Space - so peer discovery // uses Trystero's serverless signaling over public Nostr relays. Payloads -// are tiny (22 floats + a variant name at 10 Hz), and at most MAX_GHOSTS +// are tiny (22 floats + a variant name at SEND_HZ), and at most MAX_GHOSTS // ghosts are instantiated regardless of how many peers are in the room. // // This module deliberately imports nothing from duck.js / variants.js: @@ -13,14 +13,39 @@ const TRYSTERO_URL = "https://esm.run/trystero@0.25.3/nostr"; const APP_ID = "microduck-sandbox"; const ROOM = "lobby"; const MAX_GHOSTS = 3; -const SEND_HZ = 10; +const SEND_HZ = 15; const GHOST_OPACITY = 0.35; -// Per-frame easing toward the last received state (10 Hz updates, ~60 fps -// rendering): position/joints converge in ~4 frames, fast but smooth. -const EASE = 0.25; +// Snapshot interpolation: each ghost renders INTERP_DELAY_MS in the past, +// lerping between the two buffered snapshots that bracket the render time +// (timed by local ARRIVAL, so no clock sync and no payload change - old +// clients interoperate). This replaces the old exponential chase toward +// the latest packet, whose move-then-stall rhythm read as micro-stutter. +// The delay absorbs network jitter; past the newest snapshot the ghost +// holds pose, so a background-throttled 1 Hz sender degrades into slow +// discrete steps instead of teleports. Nobody synchronizes on ghosts, so +// the added latency is invisible. +const INTERP_DELAY_MS = 150; +const BUF_MAX = 40; // snapshots kept per ghost (~2.5 s at 15 Hz) +// Idle visitors stay invisible: a ghost is only shown once its peer has +// strayed from the pose of its first state message (everyone spawns at +// the arena center). Once revealed it stays visible for the whole peer +// session, so a reset teleporting the duck back to center doesn't blink +// the ghost out. +const REVEAL_DIST = 0.2; // m, horizontal distance from spawn +const REVEAL_YAW = 0.4; // rad (~23deg), turning in place also reveals + +// state.p is raw MuJoCo qpos[0..6]: [x, y, z, qw, qx, qy, qz], Z-up. +const yawOf = (p) => + Math.atan2(2 * (p[3] * p[6] + p[4] * p[5]), 1 - 2 * (p[5] * p[5] + p[6] * p[6])); +const hasMoved = (spawn, p) => { + const dx = p[0] - spawn.x, dy = p[1] - spawn.y; + if (dx * dx + dy * dy > REVEAL_DIST * REVEAL_DIST) return true; + const dyaw = yawOf(p) - spawn.yaw; + return Math.abs(Math.atan2(Math.sin(dyaw), Math.cos(dyaw))) > REVEAL_YAW; +}; export async function initGhosts(env) { - const noop = { update() {}, peerCount: () => 0, ghostCount: () => 0, debug: () => [] }; + const noop = { update() {}, peerCount: () => 0, ghostCount: () => 0, debug: () => [], mapDots: () => [] }; let room; try { const { joinRoom } = await import(TRYSTERO_URL); @@ -46,10 +71,36 @@ export async function initGhosts(env) { // Ghosts share geometry with the local rig (cloneRig), but get their own // transparent material clones so repainting them never touches the // visitor's own duck (variants.js caches materials globally). + // + // Rendering: a ghost reads as ONE translucent shell, not ~70 individually + // transparent meshes (through which every internal motor/PCB used to show). + // Classic depth-prepass silhouette, kept inside the transparent pass so the + // grid floor/walls (transparent ShaderMaterials at renderOrder 0) still + // draw behind ghosts instead of being depth-rejected: + // PREPASS (renderOrder 1): every ghost mesh duplicated as a color-less + // twin (colorWrite off, depthWrite on) -> the depth buffer ends up + // holding the nearest ghost surface per pixel. + // BEAUTY (renderOrder 2): the real meshes, transparent + depthWrite off; + // the default LessEqual depth test then rejects everything except the + // fragment matching the prepass depth, so internals and back shell + // layers are skipped and each pixel is shaded exactly once. + // Orders are shared by all ghosts (not per-ghost pairs): every beauty pass + // tests against every ghost's prepass depth, so overlapping ghosts occlude + // each other like solids instead of double-blending. + const PREPASS_ORDER = 1; + const BEAUTY_ORDER = 2; const ghostify = (rig) => { + // Collect first: twins are added below and must not be re-traversed. + const meshes = []; + rig.root.traverse((o) => { if (o.isMesh) meshes.push(o); }); const cache = new Map(); - rig.root.traverse((o) => { - if (!o.isMesh) return; + let depthMat = null; + for (const o of meshes) { + // Materialization-FX wire overlays cloned along with the local rig: + // drop them, they share the LIVE scan shader material and would + // flash on this ghost every time the local duck re-materializes. + if (o.userData.fxOverlay) { o.parent?.remove(o); continue; } + if (o.userData.ghostPrepass) continue; // twin from a previous ghostify let m = cache.get(o.material.uuid); if (!m) { m = o.material.clone(); @@ -59,10 +110,31 @@ export async function initGhosts(env) { cache.set(o.material.uuid, m); } o.material = m; - }); + o.renderOrder = BEAUTY_ORDER; + if (!o.userData.hasGhostTwin) { + if (!depthMat) { + // Depth-only material: cloned (no THREE import here) and muted. + // transparent stays true so the twin sorts into the transparent + // pass, after the grid but before the beauty meshes. + depthMat = m.clone(); + depthMat.colorWrite = false; + depthMat.depthWrite = true; + } + // Twin as an identity-transform child: follows joints for free. No + // userData.meshName, so applyVariant never repaints it. + const twin = new o.constructor(o.geometry, depthMat); + twin.renderOrder = PREPASS_ORDER; + twin.userData.ghostPrepass = true; + o.userData.hasGhostTwin = true; + o.add(twin); + } + } }; const ghosts = new Map(); // peerId -> ghost + // Interpolation snapshot: the pose parts of a state message, stamped + // with the local arrival time. + const snapOf = (state) => ({ p: state.p, j: state.j, w: state.w ?? 0, at: performance.now() }); const makeGhost = (state) => { const rig = cloneRig(rigFor(state.l)); applyVariant(rig, state.v); @@ -73,7 +145,7 @@ export async function initGhosts(env) { trunk.position.set(state.p[0], state.p[1], state.p[2]); trunk.quaternion.set(state.p[4], state.p[5], state.p[6], state.p[3]); return { - rig, trunk, target: state, joints: state.j.slice(), jaw: state.w ?? 0, + rig, trunk, buf: [snapOf(state)], variant: state.v, loco: state.l ?? 0, }; }; @@ -102,19 +174,31 @@ export async function initGhosts(env) { // sessions can arrive right after joining: only ghost live peers. if (!(peerId in room.getPeers())) return; g = makeGhost(state); + // Idle-visitor gate: remember where this peer first appeared and keep + // its rig invisible until it strays from there (see hasMoved above). + g.spawn = { x: state.p[0], y: state.p[1], yaw: yawOf(state.p) }; + g.revealed = false; + g.rig.placer.visible = false; ghosts.set(peerId, g); g.lastSeen = performance.now(); return; } g.lastSeen = performance.now(); - g.target = state; + g.buf.push(snapOf(state)); + if (g.buf.length > BUF_MAX) g.buf.shift(); + if (!g.revealed && hasMoved(g.spawn, state.p)) { + g.revealed = true; // latched for the rest of this peer session + g.rig.placer.visible = true; + } // Peer switched legs <-> rollers: rebuild its ghost on the other rig - // (cheap - cloneRig shares geometry). + // (cheap - cloneRig shares geometry). The snapshot buffer carries over + // so the interpolated motion stays continuous across the swap. if ((state.l ?? 0) !== g.loco) { - const seen = g.lastSeen; + const { lastSeen, spawn, revealed, buf } = g; removeGhost(peerId); g = makeGhost(state); - g.lastSeen = seen; + Object.assign(g, { lastSeen, spawn, revealed, buf }); + g.rig.placer.visible = revealed; ghosts.set(peerId, g); return; } @@ -133,8 +217,9 @@ export async function initGhosts(env) { try { room.leave(); } catch {} }); // ...and a staleness sweep for peers that vanished without leaving - // (crashed tab, dropped connection): 5 s without a state packet at - // 10 Hz means the peer is gone, not just lagging. + // (crashed tab, dropped connection): 5 s without a state packet means + // the peer is gone, not just lagging (even background-throttled tabs + // still send at ~1 Hz). const STALE_MS = 5000; // Broadcast + stale sweep on setInterval (not rAF) so both keep running @@ -148,36 +233,55 @@ export async function initGhosts(env) { } }, 1000 / SEND_HZ); - // Scratch THREE.Quaternion without importing three: slerp needs a real - // instance, cloned here from any node of the already-built local rig. - const _q = env.rig.placer.quaternion.clone(); + // Scratch THREE.Quaternions without importing three: slerp needs real + // instances, cloned here from any node of the already-built local rig. + const _qa = env.rig.placer.quaternion.clone(); + const _qb = env.rig.placer.quaternion.clone(); return { room, peerCount: () => Object.keys(room.getPeers()).length, ghostCount: () => ghosts.size, + // Minimap feed: revealed ghosts' arena positions in raw MJCF coords + // (trunk.position is set from qpos before the root's Z-up fix). + mapDots: () => + [...ghosts.values()] + .filter((g) => g.revealed) + .map((g) => ({ x: g.trunk.position.x, y: g.trunk.position.y })), debug: () => [...ghosts.values()].map((g) => { - let meshes = 0, visible = 0, op = null; + let meshes = 0, visible = 0, op = null, twins = 0; g.rig.root.traverse((o) => { - if (o.isMesh) { meshes++; if (o.visible) visible++; op ??= o.material.opacity; } + if (!o.isMesh) return; + if (o.userData.ghostPrepass) { twins++; return; } + meshes++; if (o.visible) visible++; op ??= o.material.opacity; }); const w = g.trunk.getWorldPosition(g.trunk.position.clone()); - return { p: g.trunk.position.toArray(), world: w.toArray(), inScene: !!g.rig.placer.parent, meshes, visible, op, v: g.variant }; + return { p: g.trunk.position.toArray(), world: w.toArray(), inScene: !!g.rig.placer.parent, revealed: g.revealed, meshes, visible, twins, op, v: g.variant }; }), update() { + const renderT = performance.now() - INTERP_DELAY_MS; for (const g of ghosts.values()) { - const t = g.target; + const buf = g.buf; + // Drop snapshots fully in the past; keep [0] and [1] bracketing + // renderT (or the two newest, if renderT has caught up). + while (buf.length > 2 && buf[1].at <= renderT) buf.shift(); + const a = buf[0]; + const b = buf.length > 1 ? buf[1] : a; + // Clamped: before the segment -> a, past the newest -> hold b. + const span = b.at - a.at; + const u = span > 0 ? Math.min(1, Math.max(0, (renderT - a.at) / span)) : 1; // Trunk pose in raw MJCF coords: the cloned root applies Z-up -> Y-up. - g.trunk.position.x += (t.p[0] - g.trunk.position.x) * EASE; - g.trunk.position.y += (t.p[1] - g.trunk.position.y) * EASE; - g.trunk.position.z += (t.p[2] - g.trunk.position.z) * EASE; - _q.set(t.p[4], t.p[5], t.p[6], t.p[3]); // THREE order x,y,z,w - g.trunk.quaternion.slerp(_q, EASE); + g.trunk.position.set( + a.p[0] + (b.p[0] - a.p[0]) * u, + a.p[1] + (b.p[1] - a.p[1]) * u, + a.p[2] + (b.p[2] - a.p[2]) * u, + ); + _qa.set(a.p[4], a.p[5], a.p[6], a.p[3]); // THREE order x,y,z,w + _qb.set(b.p[4], b.p[5], b.p[6], b.p[3]); + g.trunk.quaternion.copy(_qa.slerp(_qb, u)); for (let i = 0; i < jointNames.length; i++) { - g.joints[i] += (t.j[i] - g.joints[i]) * EASE; - setJoint(g.rig, jointNames[i], g.joints[i]); + setJoint(g.rig, jointNames[i], a.j[i] + (b.j[i] - a.j[i]) * u); } - g.jaw += ((t.w ?? 0) - g.jaw) * EASE; - setJawOpen(g.rig, g.jaw); + setJawOpen(g.rig, a.w + (b.w - a.w) * u); } }, }; diff --git a/index.html b/index.html index da9e167..b9e4f09 100644 --- a/index.html +++ b/index.html @@ -3,11 +3,12 @@ - Microduck RL playground + Microduck +
-
- Microduck · real trained policies
- MuJoCo + ONNX, live in your browser + + + +
+
+
+ + +
+
+
+ Mode +
Run
● REC ▶ 00:00:00
-
Run
-
+
+