diff --git a/ball-actor.js b/ball-actor.js index a5cc7e2..62b1739 100644 --- a/ball-actor.js +++ b/ball-actor.js @@ -12,8 +12,8 @@ // 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) +const BALL_APPEAR_S = 0.35; // spawn pop-in (scan up) +const BALL_DISAPPEAR_S = 0.25; // despawn/park peel-away (reverse scan) export function createBallActor({ THREE, scene, camera, renderer, fxModule, mesh, group }) { const fx = fxModule.createWireframeFx(); diff --git a/controls/touch.js b/controls/touch.js new file mode 100644 index 0000000..4ad30a6 --- /dev/null +++ b/controls/touch.js @@ -0,0 +1,172 @@ +// Touch input source (see controller.js for the source interface +// contract). Game Boy layout, two thumbs: +// +// Left thumb FLOATING analog stick: the lower-left quadrant of the +// screen (#touch-zone) is the grab area, and the stick base +// (#touch-stick) re-anchors under the finger wherever it +// lands - so the thumb never has to find a fixed circle - +// then snaps back to its resting spot on release. Vertical +// = vx (asymmetric fwd/back limits), horizontal = turn. The +// nub tracks the finger clamped to the base circle; the +// command is EMA-smoothed like the gamepad's so releases +// don't snap. +// Right thumb two round caps: A (#touch-a) fires alternateKick - the +// game alternates feet; B (#touch-b) quacks on press and +// holds the beak open (axes.jaw = 1) while held. +// +// The overlay DOM lives in index.html (#touch-ui); this module only wires +// pointer events to it. `connected` mirrors (pointer: coarse) - rl.js +// reflects it onto body.touch-mode, which is what actually shows the +// overlay - so a desktop never sees the thumbs and a phone always does, +// even before the first touch. + +const TOUCH_ALPHA = 0.18; // EMA smoothing toward the stick target +const TOUCH_DEADZONE = 0.12; // normalized deflection ignored around center + +export class TouchSource { + id = "touch"; + connected = false; + command = new Float32Array(3); // [vx, 0, wz], EMA-smoothed + axes = { jaw: 0, orbitX: 0, orbitY: 0 }; + pressed = { stick: false, a: false, b: false }; + onAction = () => {}; // assigned by the Controller at registration + + #getVelocityLimits; + #active = false; // owns twist authority (stick engaged, until EMA settles) + #target = [0, 0]; // normalized deflection [x, right+] [y, up+] + #mq = null; + #onMq = null; + #disposers = []; + + constructor({ getVelocityLimits }) { + this.#getVelocityLimits = getVelocityLimits; + } + + init() { + this.#mq = window.matchMedia("(pointer: coarse)"); + this.#onMq = () => { this.connected = this.#mq.matches; }; + this.#mq.addEventListener("change", this.#onMq); + this.#onMq(); + + const zone = document.getElementById("touch-zone"); + const stick = document.getElementById("touch-stick"); + const nub = stick?.querySelector(".nub"); + if (zone && stick && nub) this.#bindStick(zone, stick, nub); + + this.#bindButton("touch-a", "a", () => this.onAction("alternateKick")); + this.#bindButton("touch-b", "b", () => this.onAction("quack")); + } + + dispose() { + this.#mq?.removeEventListener("change", this.#onMq); + for (const off of this.#disposers) off(); + this.#disposers = []; + } + + isActive() { + return this.#active; + } + + poll() { + const [x, y] = this.#target; + const [limF, limB, limA] = this.#getVelocityLimits(); + const tvx = y >= 0 ? y * limF : y * -limB; + const twz = -x * limA; + this.command[0] += TOUCH_ALPHA * (tvx - this.command[0]); + this.command[2] += TOUCH_ALPHA * (twz - this.command[2]); + // Same authority rule as the gamepad sticks: grab on input, release + // once the smoothed command has settled back to ~zero. + if (this.pressed.stick) this.#active = true; + else if (this.#active && Math.abs(this.command[0]) + Math.abs(this.command[2]) < 0.01) { + this.#active = false; + this.command.fill(0); + } + // B holds the beak open; quack itself fired on the press edge. + this.axes.jaw = this.pressed.b ? 1 : 0; + } + + #on(el, type, fn) { + el.addEventListener(type, fn); + this.#disposers.push(() => el.removeEventListener(type, fn)); + } + + #bindStick(zone, stick, nub) { + let pointerId = null; + let center = null; // stick center while grabbed, zone-local px + const setFrom = (e) => { + const zr = zone.getBoundingClientRect(); + const R = stick.offsetWidth / 2; + const travel = R * 0.62; // max nub travel inside the base circle + let dx = e.clientX - zr.left - center.x; + let dy = e.clientY - zr.top - center.y; + const d = Math.hypot(dx, dy); + if (d > travel) { dx *= travel / d; dy *= travel / d; } + nub.style.transform = `translate(${dx}px, ${dy}px)`; + const nx = dx / travel, ny = -dy / travel; // up is +y + const mag = Math.hypot(nx, ny); + const live = mag >= TOUCH_DEADZONE; + this.#target[0] = live ? nx : 0; + this.#target[1] = live ? ny : 0; + }; + const release = () => { + pointerId = null; + center = null; + this.pressed.stick = false; + stick.classList.remove("live"); + nub.style.transform = ""; + // Back to the CSS resting spot until the next grab. + stick.style.left = ""; + stick.style.top = ""; + stick.style.bottom = ""; + this.#target[0] = 0; + this.#target[1] = 0; + }; + this.#on(zone, "pointerdown", (e) => { + e.preventDefault(); + pointerId = e.pointerId; + // Anchor the base exactly under the finger - a clamped anchor would + // start the stick with a phantom deflection near the zone edges. The + // circle clipping off-screen there is the standard floating-stick + // look, and it takes no pointer events anyway. + const zr = zone.getBoundingClientRect(); + const R = stick.offsetWidth / 2; + center = { x: e.clientX - zr.left, y: e.clientY - zr.top }; + stick.style.left = `${center.x - R}px`; + stick.style.top = `${center.y - R}px`; + stick.style.bottom = "auto"; + this.pressed.stick = true; + stick.classList.add("live"); + setFrom(e); // zero deflection at grab + // Last: capture can throw on exotic pointers and must not eat the + // press state above. + try { zone.setPointerCapture(e.pointerId); } catch {} + }); + this.#on(zone, "pointermove", (e) => { + if (e.pointerId === pointerId) setFrom(e); + }); + this.#on(zone, "pointerup", (e) => { + if (e.pointerId === pointerId) release(); + }); + this.#on(zone, "pointercancel", (e) => { + if (e.pointerId === pointerId) release(); + }); + } + + #bindButton(elId, key, fire) { + const el = document.getElementById(elId); + if (!el) return; + this.#on(el, "pointerdown", (e) => { + e.preventDefault(); + this.pressed[key] = true; + el.classList.add("down"); + fire(); // on the press edge, not the release: arcade latency + try { el.setPointerCapture(e.pointerId); } catch {} + }); + const release = () => { + this.pressed[key] = false; + el.classList.remove("down"); + }; + this.#on(el, "pointerup", release); + this.#on(el, "pointercancel", release); + } +} diff --git a/fx/fx-wireframe.js b/fx/fx-wireframe.js index 84a8338..064307b 100644 --- a/fx/fx-wireframe.js +++ b/fx/fx-wireframe.js @@ -175,7 +175,10 @@ gl_FragColor.rgb += fxEdge * vec3(0.95, 0.32, 0.05);`, 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); + // userData.fxWireGeometry substitutes a coarser geometry for the + // wireframe pass only (the ball's render sphere is too dense to read + // as a hologram); the solidify clip still runs on the real mesh. + const w = new THREE.Mesh(o.userData.fxWireGeometry ?? o.geometry, wireMat); w.userData.fxOverlay = true; w.renderOrder = 5; w.visible = false; diff --git a/index.html b/index.html index e5c9f5b..15f0811 100644 --- a/index.html +++ b/index.html @@ -405,6 +405,104 @@ #quickbar .dots button .dot { width: 1.2rem; height: 1.2rem; } #quickbar .seg button { height: 2rem; padding: 0 0.8rem; font-size: 0.68rem; } } + /* Touch overlay: Game Boy thumbs, coarse-pointer devices only + (rl.js mirrors the touch source's connected flag onto + body.touch-mode). Left thumb = virtual stick, right thumb = A/B + caps on the classic diagonal (A up-right, B down-left). */ + #touch-ui { display: none; } + body.touch-mode #touch-ui { display: block; } + /* Touch mode strips the HUD down to the thumbs: no quickbar, no mode + readout, no telemetry - the stick and caps own the bottom edge. + Back stays (top-left) to reach the menu. */ + body.touch-mode #quickbar, + body.touch-mode #mode-wrap, + body.touch-mode #ctrl-hz { display: none !important; } + /* Floating-stick zone: the whole lower-left quadrant grabs the stick, + and the base re-anchors under the finger wherever it lands. Sits + BELOW the quickbar/back button in z so their taps still land; the + resting stick inside marks the zone when idle. */ + #touch-zone { + position: fixed; + left: 0; + right: 50%; + top: 30%; + bottom: 0; + z-index: 9; + touch-action: none; + } + #touch-stick { + position: absolute; + left: 1.3rem; + bottom: 2rem; + width: 8.2rem; + height: 8.2rem; + border-radius: 50%; + border: 1px solid rgba(255, 255, 255, 0.18); + background: rgba(8, 8, 12, 0.4); + pointer-events: none; /* the zone owns the pointer */ + } + #touch-stick .nub { + position: absolute; + left: 50%; + top: 50%; + width: 3.6rem; + height: 3.6rem; + margin: -1.8rem 0 0 -1.8rem; + border-radius: 50%; + border: 1px solid rgba(255, 255, 255, 0.35); + background: rgba(255, 255, 255, 0.14); + pointer-events: none; + } + #touch-stick.live { border-color: rgba(255, 122, 47, 0.55); } + #touch-stick.live .nub { + background: rgba(255, 122, 47, 0.45); + border-color: var(--orange); + } + #touch-btns { + position: fixed; + right: 1.3rem; + bottom: 2.2rem; + z-index: 12; + width: 9.4rem; + height: 8.4rem; + } + #touch-btns .capwrap { + position: absolute; + display: flex; + flex-direction: column; + align-items: center; + gap: 0.3rem; + } + #touch-btns .cap-a { right: 0; top: 0; } + #touch-btns .cap-b { left: 0; bottom: 0; } + #touch-btns button { + appearance: none; + width: 4.2rem; + height: 4.2rem; + border-radius: 50%; + border: 1px solid rgba(255, 255, 255, 0.22); + background: rgba(8, 8, 12, 0.5); + color: rgba(255, 255, 255, 0.85); + font: inherit; + font-size: 1.3rem; + font-weight: 700; + cursor: pointer; + touch-action: none; + user-select: none; + -webkit-user-select: none; + } + #touch-btns button.down { + background: var(--orange); + border-color: var(--orange); + color: var(--ink); + } + #touch-btns .capwrap span { + font-size: 0.55rem; + font-weight: 600; + letter-spacing: 0.18em; + text-transform: uppercase; + color: rgba(255, 255, 255, 0.45); + } /* Colour swatches */ #swatches { gap: 0.6rem; } #swatches button { @@ -602,6 +700,7 @@ body.welcome-open #stats-wrap, body.welcome-open #back-btn, body.welcome-open #quickbar, + body.welcome-open #touch-ui, body.welcome-open #ctrl-hz { display: none !important; } /* ── Title / pause overlay (full viewport, no card) ────────────── */ #welcome[hidden] { display: none; } @@ -633,9 +732,11 @@ from { transform: translateY(10px) scale(0.94); opacity: 0; } to { transform: none; opacity: 1; } } + /* Wide enough for the two-line title; the controls grid below keeps + its own 36rem cap so the tiles don't stretch. */ #welcome .wstep { width: 100%; - max-width: 36rem; + max-width: min(48rem, 92vw); text-align: center; } #welcome .wstep[hidden] { display: none; } @@ -719,7 +820,7 @@ (600, tight tracking, line-height 1), the loudest line on the page. */ #welcome .title { margin: 0 auto; - max-width: 20ch; + max-width: 24ch; font-size: clamp(2.4rem, 5vw, 3.75rem); font-weight: 600; letter-spacing: -0.03em; @@ -748,14 +849,26 @@ display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.55rem; - margin-top: 1.4rem; + margin: 1.4rem auto 0; + max-width: 36rem; } #welcome .grid-pad { display: none; } - body.pad-connected #welcome .grid-kb { display: none; } - body.pad-connected #welcome .grid-pad { display: grid; } + /* Touch device: the tutorial shows the two thumbs instead of the + keycaps. A plugged-in gamepad still wins (pad rules below). */ + #welcome .grid-touch { display: none; } + #welcome .touch-hint { display: none; } + body.touch-mode #welcome .grid-kb { display: none; } + body.touch-mode #welcome .grid-touch { display: grid; } + body.touch-mode #welcome .kb-hint { display: none; } + body.touch-mode #welcome .touch-hint { display: block; } + #welcome .grid-touch kbd.cap { border-radius: 50%; min-width: 1.9rem; } #welcome .kb-hint { display: block; } #welcome .pad-hint { display: none; } - body.pad-connected #welcome .kb-hint { display: none; } + body.pad-connected #welcome .grid-kb, + body.pad-connected #welcome .grid-touch { display: none; } + body.pad-connected #welcome .grid-pad { display: grid; } + body.pad-connected #welcome .kb-hint, + body.pad-connected #welcome .touch-hint { display: none; } body.pad-connected #welcome .pad-hint { display: block; } #welcome .tile { padding: 0.85rem 0.45rem 0.75rem; @@ -860,6 +973,8 @@ #welcome .title { font-size: 2rem; } #welcome .tagline { font-size: 0.95rem; } #welcome .grid { grid-template-columns: repeat(2, 1fr); } + /* Three touch tiles hold one line even on a phone */ + #welcome .grid-touch { grid-template-columns: repeat(3, 1fr); } #welcome .tile { padding: 0.65rem 0.35rem 0.6rem; } #welcome .caps { height: 2.8rem; } #welcome .wbtn { font-size: 0.95rem; padding: 0.75rem 1.5rem; } @@ -889,6 +1004,19 @@ +
Same body, same policies as the real robot.
+The exact same trained policies that drive the real robot.
drag to orbit · scroll to zoom
R3 chase · RT quack
+drag to orbit · pinch to zoom