Prompt specification
Build or customize the Three.js 3D scene "Handoff Timing Window Challenge" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Handoff Timing Window Challenge
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/handoff-timing-window-challenge-x3636
Scene Description Prompt:
Make a low-poly Three.js scene in a self-contained HTML file using cdnjs: handoff timing window challenge. Create a playable relay timing puzzle where early or late exchanges fail under explicit rules. Display the actual overlap interval and allow deterministic replay from identical starts.
Working Three.js Starter (handoff-timing-window-challenge-x3636):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Overlap — Relay Timing Challenge</title>
<style>
:root {
color-scheme: dark;
--bg: #0b1320;
--panel: rgba(12, 22, 36, 0.9);
--line: rgba(255, 255, 255, 0.13);
--text: #f4f8fb;
--muted: #93a8bb;
--cyan: #41d9ff;
--orange: #ffb34d;
--green: #76f0a4;
--red: #ff657a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.glass {
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
#intro {
position: absolute;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 20px 22px;
border-radius: 18px;
}
.eyebrow {
color: var(--cyan);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 10px;
font-size: clamp(31px, 4vw, 48px);
line-height: 0.95;
letter-spacing: -0.055em;
}
h1 span {
color: var(--cyan);
}
#intro p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.clock-row {
display: flex;
align-items: baseline;
gap: 7px;
margin-top: 17px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
#clock {
font: 700 28px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: -0.04em;
}
.clock-row small {
color: var(--muted);
font: 600 11px ui-monospace, SFMono-Regular, Menlo, monospace;
text-transform: uppercase;
letter-spacing: 0.12em;
}
#panel {
position: absolute;
top: 22px;
right: 22px;
width: min(390px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 17px;
border-radius: 18px;
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: #385069 transparent;
}
.status {
padding: 14px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.045);
transition: border-color 160ms, background 160ms;
}
.status[data-kind="wait"] {
border-color: rgba(147, 168, 187, 0.35);
}
.status[data-kind="live"] {
border-color: rgba(118, 240, 164, 0.8);
background: rgba(118, 240, 164, 0.09);
}
.status[data-kind="success"] {
border-color: rgba(118, 240, 164, 0.9);
background: rgba(118, 240, 164, 0.13);
}
.status[data-kind="early"],
.status[data-kind="late"] {
border-color: rgba(255, 101, 122, 0.9);
background: rgba(255, 101, 122, 0.11);
}
#statusTitle {
color: var(--green);
font: 800 13px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status[data-kind="wait"] #statusTitle {
color: #c8d5df;
}
.status[data-kind="early"] #statusTitle,
.status[data-kind="late"] #statusTitle {
color: var(--red);
}
#statusDetail {
margin-top: 6px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.state-pills {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 10px 0 15px;
}
.state-pill {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
}
.state-pill span:last-child {
color: #75899a;
}
.state-pill.on {
border-color: currentColor;
background: rgba(255, 255, 255, 0.045);
}
#receiverPill.on {
color: var(--cyan);
}
#carrierPill.on {
color: var(--orange);
}
.state-pill.on span:last-child {
color: currentColor;
}
h2 {
margin: 0 0 9px;
font: 800 11px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.rules {
margin: 0 0 16px;
padding-left: 18px;
color: #b9c8d4;
font-size: 12px;
line-height: 1.5;
}
.rules li + li {
margin-top: 5px;
}
.rules strong {
color: #fff;
}
.telemetry {
display: grid;
gap: 7px;
margin-bottom: 16px;
}
.telemetry-row {
display: grid;
grid-template-columns: 90px 1fr;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 11px;
}
.telemetry-row b {
text-align: right;
font: 700 11px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.telemetry-row.receiver b {
color: var(--cyan);
}
.telemetry-row.carrier b {
color: var(--orange);
}
.telemetry-row.overlap {
margin-top: 4px;
padding: 10px;
border: 1px solid rgba(118, 240, 164, 0.34);
border-radius: 10px;
background: rgba(118, 240, 164, 0.06);
}
.telemetry-row.overlap b {
color: var(--green);
font-size: 14px;
}
.timeline {
margin-bottom: 16px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(0, 0, 0, 0.12);
}
.timeline-title {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
color: var(--muted);
font: 700 9px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.timeline-row {
position: relative;
height: 23px;
margin: 4px 0;
}
.timeline-track {
position: absolute;
inset: 6px 0;
overflow: hidden;
border-radius: 4px;
background: rgba(255, 255, 255, 0.07);
}
.range {
position: absolute;
top: 0;
bottom: 0;
border-radius: 4px;
}
#receiverRange {
background: rgba(65, 217, 255, 0.72);
}
#carrierRange {
background: rgba(255, 179, 77, 0.72);
}
#overlapRange {
top: 5px;
bottom: 5px;
background: var(--green);
box-shadow: 0 0 14px rgba(118, 240, 164, 0.55);
}
#playhead {
position: absolute;
top: -4px;
bottom: -5px;
width: 2px;
left: 0;
background: white;
box-shadow: 0 0 8px white;
transform: translateX(-1px);
z-index: 2;
}
.timeline-labels {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #718699;
font: 600 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.buttons {
display: grid;
gap: 8px;
}
.buttons button {
min-height: 43px;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
cursor: pointer;
font-weight: 750;
transition: transform 120ms, background 120ms, opacity 120ms;
}
.buttons button:hover:not(:disabled) {
background: rgba(255, 255, 255, 0.1);
transform: translateY(-1px);
}
.buttons button:active:not(:disabled) {
transform: translateY(1px);
}
.buttons button:disabled {
cursor: default;
opacity: 0.42;
}
.buttons .primary {
display: flex;
align-items: center;
justify-content: space-between;
border-color: transparent;
color: #062013;
background: var(--green);
}
.buttons .primary:hover:not(:disabled) {
background: #9af8bd;
}
kbd {
padding: 3px 7px;
border: 1px solid rgba(0, 0, 0, 0.25);
border-radius: 5px;
background: rgba(0, 0, 0, 0.13);
font: 800 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.replay-note {
margin: 10px 1px 0;
color: #7890a4;
font-size: 10px;
line-height: 1.5;
}
#cameraHint {
position: absolute;
bottom: 18px;
left: 22px;
padding: 9px 12px;
border-radius: 9px;
color: rgba(255, 255, 255, 0.72);
font: 600 10px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
}
@media (max-width: 900px) {
#intro {
width: 245px;
padding: 15px 17px;
}
#intro p {
display: none;
}
h1 {
font-size: 31px;
}
#panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(360px, calc(100vw - 24px));
max-height: 54vh;
padding: 13px;
}
#cameraHint {
display: none;
}
}
@media (max-width: 520px) {
#intro {
top: 10px;
left: 10px;
width: 185px;
}
.eyebrow {
font-size: 9px;
}
h1 {
font-size: 25px;
}
.clock-row {
margin-top: 10px;
padding-top: 9px;
}
#clock {
font-size: 21px;
}
#panel {
right: 8px;
bottom: 8px;
width: calc(100vw - 16px);
max-height: 59vh;
}
.rules {
font-size: 11px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<section id="intro" class="glass">
<div class="eyebrow">Fixed Start / Relay 04</div>
<h1>Handoff<br /><span>Window</span></h1>
<p>
One command. One legal instant. Read the overlap and transfer the baton
before the athletes separate.
</p>
<div class="clock-row">
<span id="clock">0.000</span><small>seconds</small>
</div>
</section>
<aside id="panel" class="glass">
<div id="status" class="status" data-kind="wait" aria-live="polite">
<div id="statusTitle">WAIT FOR OVERLAP</div>
<div id="statusDetail">
Both runners must occupy the exchange pad at the same instant.
</div>
</div>
<div class="state-pills">
<div id="receiverPill" class="state-pill">
<span>RECEIVER</span><span>APPROACH</span>
</div>
<div id="carrierPill" class="state-pill">
<span>CARRIER</span><span>WAITING</span>
</div>
</div>
<h2>Explicit Rules</h2>
<ol class="rules">
<li>The exchange pad is <strong>2.50 m long</strong>.</li>
<li>A handoff succeeds only when <strong>both runners are inside it</strong>.</li>
<li>Before the overlap begins is <strong>EARLY</strong>; after it ends is <strong>LATE</strong>.</li>
<li>Only one handoff command is accepted per run.</li>
</ol>
<h2>Reach Telemetry</h2>
<div class="telemetry">
<div class="telemetry-row receiver">
<span>Receiver in pad</span><b id="receiverInterval">—</b>
</div>
<div class="telemetry-row carrier">
<span>Carrier in pad</span><b id="carrierInterval">—</b>
</div>
<div class="telemetry-row overlap">
<span>Actual overlap</span><b id="overlapInterval">—</b>
</div>
</div>
<div class="timeline" aria-label="Timing timeline">
<div class="timeline-title">
<span>Reach windows</span>
<span id="timelineTime">0.000 s</span>
</div>
<div class="timeline-row">
<div class="timeline-track">
<div id="receiverRange" class="range"></div>
</div>
</div>
<div class="timeline-row">
<div class="timeline-track">
<div id="carrierRange" class="range"></div>
</div>
</div>
<div class="timeline-row">
<div class="timeline-track">
<div id="overlapRange" class="range"></div>
</div>
<div id="playhead"></div>
</div>
<div class="timeline-labels">
<span>0 s</span><span>2 s</span><span>4 s</span><span>6 s</span><span>8 s</span>
</div>
</div>
<div class="buttons">
<button id="handoff" class="primary" type="button">
<span>Issue handoff</span><kbd>SPACE</kbd>
</button>
<button id="reset" type="button">Reset identical start</button>
<button id="replay" type="button" disabled>Replay exact input</button>
</div>
<p class="replay-note">
Deterministic replay: the fixed 120 Hz run restarts at T+0.000 and
reissues your last command at the exact recorded simulation time.
</p>
</aside>
<div id="cameraHint" class="glass">DRAG TO ORBIT · WHEEL TO ZOOM</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Deterministic simulation constants. Every position is a pure function
// of simulation time, so repeated starts and replayed inputs are identical.
// -------------------------------------------------------------------------
const FIXED_DT = 1 / 120;
const RUN_END = 8;
const ZONE_HALF_LENGTH = 1.25;
const RECEIVER_START_Z = 9.5;
const RECEIVER_SPEED = 1.8;
const CARRIER_START_Z = 2.0;
const CARRIER_WAIT_UNTIL = 2.8;
const CARRIER_SLOW_SPEED = 1.0;
const CARRIER_SWITCH_TIME = 4.7;
const CARRIER_SWITCH_Z = 0.3;
const CARRIER_FAST_SPEED = 2.8;
const RECEIVER_X = -0.38;
const CARRIER_X = 0.38;
function receiverZ(t) {
return RECEIVER_START_Z - RECEIVER_SPEED * t;
}
function carrierZ(t) {
if (t < CARRIER_WAIT_UNTIL) {
return CARRIER_START_Z;
}
if (t < CARRIER_SWITCH_TIME) {
return (
CARRIER_START_Z -
CARRIER_SLOW_SPEED * (t - CARRIER_WAIT_UNTIL)
);
}
return (
CARRIER_SWITCH_Z -
CARRIER_FAST_SPEED * (t - CARRIER_SWITCH_TIME)
);
}
// Exact longitudinal entry/exit times for the common exchange pad.
const receiverInStart =
(RECEIVER_START_Z - ZONE_HALF_LENGTH) / RECEIVER_SPEED;
const receiverInEnd =
(RECEIVER_START_Z + ZONE_HALF_LENGTH) / RECEIVER_SPEED;
const carrierInStart =
CARRIER_WAIT_UNTIL +
(CARRIER_START_Z - ZONE_HALF_LENGTH) / CARRIER_SLOW_SPEED;
const carrierInEnd =
CARRIER_SWITCH_TIME +
(CARRIER_SWITCH_Z + ZONE_HALF_LENGTH) / CARRIER_FAST_SPEED;
const overlapStart = Math.max(receiverInStart, carrierInStart);
const overlapEnd = Math.min(receiverInEnd, carrierInEnd);
const overlapDuration = Math.max(0, overlapEnd - overlapStart);
function inPad(z) {
return Math.abs(z) <= ZONE_HALF_LENGTH + 1e-9;
}
// -------------------------------------------------------------------------
// DOM references.
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const clockEl = document.querySelector("#clock");
const timelineTimeEl = document.querySelector("#timelineTime");
const statusEl = document.querySelector("#status");
const statusTitleEl = document.querySelector("#statusTitle");
const statusDetailEl = document.querySelector("#statusDetail");
const receiverPill = document.querySelector("#receiverPill");
const carrierPill = document.querySelector("#carrierPill");
const handoffButton = document.querySelector("#handoff");
const resetButton = document.querySelector("#reset");
const replayButton = document.querySelector("#replay");
const playhead = document.querySelector("#playhead");
const receiverIntervalEl = document.querySelector("#receiverInterval");
const carrierIntervalEl = document.querySelector("#carrierInterval");
const overlapIntervalEl = document.querySelector("#overlapInterval");
function formatTime(t) {
return `${t.toFixed(3)} s`;
}
receiverIntervalEl.textContent =
`${receiverInStart.toFixed(3)}–${receiverInEnd.toFixed(3)}`;
carrierIntervalEl.textContent =
`${carrierInStart.toFixed(3)}–${carrierInEnd.toFixed(3)}`;
overlapIntervalEl.textContent =
`${overlapStart.toFixed(3)}–${overlapEnd.toFixed(3)} · ${Math.round(
overlapDuration * 1000
)} ms`;
const timelineEnd = RUN_END;
const percent = (time) => `${(time / timelineEnd) * 100}%`;
function positionRange(element, start, end) {
element.style.left = percent(start);
element.style.width = percent(end - start);
}
positionRange(
document.querySelector("#receiverRange"),
receiverInStart,
receiverInEnd
);
positionRange(
document.querySelector("#carrierRange"),
carrierInStart,
carrierInEnd
);
positionRange(
document.querySelector("#overlapRange"),
overlapStart,
overlapEnd
);
// -------------------------------------------------------------------------
// Three.js renderer, camera and lighting.
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb7dce8);
scene.fog = new THREE.Fog(0xb7dce8, 38, 85);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Low-poly relay track. Drag to orbit the camera and scroll to zoom."
);
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
45,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemisphere = new THREE.HemisphereLight(0xdff4ff, 0x6d7650, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.1);
sun.position.set(-10, 18, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24;
sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24;
sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0003;
scene.add(sun);
// -------------------------------------------------------------------------
// Materials and geometry helpers.
// -------------------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
...options
});
}
function addMesh(geometry, material, position, parent = scene) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const grassMat = standard(0x789b65);
const trackMat = standard(0xa95f42);
const trackEdgeMat = standard(0x704a39);
const concreteMat = standard(0x87929a);
const whiteMat = standard(0xf4f0df);
const darkMat = standard(0x1b2631);
const woodMat = standard(0x8b5b3d);
// -------------------------------------------------------------------------
// Stadium, track and environmental low-poly geometry.
// -------------------------------------------------------------------------
const ground = addMesh(
new THREE.PlaneGeometry(120, 120),
grassMat,
[0, -0.075, 0]
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
addMesh(new THREE.BoxGeometry(5.4, 0.09, 31), trackMat, [0, 0, 2.5]);
addMesh(new THREE.BoxGeometry(5.8, 0.11, 31.5), trackEdgeMat, [0, -0.025, 2.5]);
for (const x of [-1.9, 0, 1.9]) {
addMesh(new THREE.BoxGeometry(0.075, 0.018, 30), whiteMat, [x, 0.062, 2.5]);
}
// Start gantry.
addMesh(new THREE.BoxGeometry(0.28, 3.6, 0.28), darkMat, [-2.65, 1.8, 12]);
addMesh(new THREE.BoxGeometry(0.28, 3.6, 0.28), darkMat, [2.65, 1.8, 12]);
addMesh(new THREE.BoxGeometry(5.6, 0.45, 0.35), darkMat, [0, 3.45, 12]);
addMesh(new THREE.BoxGeometry(3.1, 0.13, 0.08), whiteMat, [0, 3.45, 11.8]);
// Exchange pad.
const zoneMat = new THREE.MeshStandardMaterial({
color: 0x40d7ff,
emissive: 0x174f60,
emissiveIntensity: 0.7,
transparent: true,
opacity: 0.38,
roughness: 0.55,
depthWrite: false
});
const zoneMesh = addMesh(
new THREE.BoxGeometry(2.2, 0.035, ZONE_HALF_LENGTH * 2),
zoneMat,
[0, 0.075, 0]
);
zoneMesh.castShadow = false;
const zoneEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(zoneMesh.geometry),
new THREE.LineBasicMaterial({ color: 0xbff7ff, transparent: true, opacity: 0.8 })
);
zoneEdges.position.copy(zoneMesh.position);
scene.add(zoneEdges);
// Visible entry/exit boundary lines.
const boundaryMat = new THREE.MeshBasicMaterial({
color: 0xe8fbff,
transparent: true,
opacity: 0.72
});
for (const z of [-ZONE_HALF_LENGTH, ZONE_HALF_LENGTH]) {
addMesh(
new THREE.BoxGeometry(2.2, 0.025, 0.045),
boundaryMat,
[0, 0.1, z]
);
}
// Four small exchange-pad corner lights.
const lampMat = standard(0xe7fbff, {
emissive: 0x5adfff,
emissiveIntensity: 1.1
});
for (const x of [-1.05, 1.05]) {
for (const z of [-1.2, 1.2]) {
addMesh(new THREE.CylinderGeometry(0.07, 0.09, 0.18, 6), lampMat, [x, 0.15, z]);
}
}
// Grandstands.
function makeStand(side) {
const group = new THREE.Group();
scene.add(group);
addMesh(
new THREE.BoxGeometry(5.2, 1.0, 27),
concreteMat,
[side * 7.2, 0.5, 2],
group
);
const seatGeometry = new THREE.BoxGeometry(0.5, 0.42, 0.72);
const seatMaterials = [
standard(0x3f87a7),
standard(0xdb765c),
standard(0xe0b84f)
];
for (let row = 0; row < 3; row++) {
const seats = new THREE.InstancedMesh(
seatGeometry,
seatMaterials[row],
18
);
seats.castShadow = true;
seats.receiveShadow = true;
const matrix = new THREE.Matrix4();
for (let i = 0; i < 18; i++) {
matrix.makeTranslation(
side * (5.45 + row * 0.73),
1.18 + row * 0.39,
-10.5 + i * 1.38
);
seats.setMatrixAt(i, matrix);
}
seats.instanceMatrix.needsUpdate = true;
group.add(seats);
}
}
makeStand(-1);
makeStand(1);
// Low-poly trees.
const trunkMat = standard(0x70452e);
const foliageMats = [
standard(0x3f7950),
standard(0x4e8c57),
standard(0x679b5c)
];
function makeTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
addMesh(
new THREE.CylinderGeometry(0.18, 0.25, 1.35, 6),
trunkMat,
[0, 0.68, 0],
group
);
addMesh(
new THREE.ConeGeometry(1.05, 2.2, 7),
foliageMats[Math.abs(Math.round(x + z)) % foliageMats.length],
[0, 1.85, 0],
group
);
addMesh(
new THREE.ConeGeometry(0.76, 1.65, 7),
foliageMats[(Math.abs(Math.round(x - z)) + 1) % foliageMats.length],
[0, 2.75, 0],
group
);
}
[
[-15, -9, 1.2],
[-16, 4, 0.95],
[-14, 15, 1.15],
[15, -10, 1.1],
[16, 3, 1.25],
[14, 15, 0.9],
[-21, 22, 1.1],
[20, 23, 1.2]
].forEach((v) => makeTree(...v));
// Floodlights.
const poleMat = standard(0x697985);
const lightMat = standard(0xfff5cf, {
emissive: 0xffe9a2,
emissiveIntensity: 1.3
});
for (const x of [-9.2, 9.2]) {
for (const z of [-10, 15]) {
addMesh(new THREE.CylinderGeometry(0.1, 0.14, 6.5, 7), poleMat, [x, 3.25, z]);
addMesh(new THREE.BoxGeometry(1.6, 0.62, 0.2), darkMat, [x, 6.45, z]);
for (let i = -1; i <= 1; i++) {
addMesh(
new THREE.BoxGeometry(0.36, 0.4, 0.08),
lightMat,
[x + i * 0.48, 6.45, z - 0.13]
);
}
}
}
// Chunky low-poly clouds.
const cloudMat = standard(0xf7fbfc);
const cloudGeometry = new THREE.IcosahedronGeometry(1, 1);
function makeCloud(x, y, z) {
const group = new THREE.Group();
group.position.set(x, y, z);
scene.add(group);
const pieces = [
[-1.2, 0, 0, 1.1],
[0, 0.35, 0, 1.45],
[1.3, 0.05, 0.1, 1.0],
[0.45, -0.25, 0.3, 1.15]
];
for (const p of pieces) {
const cloud = new THREE.Mesh(cloudGeometry, cloudMat);
cloud.position.set(p[0], p[1], p[2]);
cloud.scale.set(p[3], p[3] * 0.65, p[3] * 0.7);
cloud.castShadow = true;
group.add(cloud);
}
}
makeCloud(-17, 11, -18);
makeCloud(13, 13, -23);
makeCloud(23, 9, 8);
// -------------------------------------------------------------------------
// Low-poly runners.
// -------------------------------------------------------------------------
function createRunner(jerseyColor, skinColor, hairColor) {
const group = new THREE.Group();
scene.add(group);
const jerseyMat = standard(jerseyColor);
const skinMat = standard(skinColor);
const hairMat = standard(hairColor);
const shoeMat = standard(0xf1f4ef);
const eyeMat = standard(0x17212a);
addMesh(new THREE.BoxGeometry(0.64, 0.82, 0.37), jerseyMat, [0, 1.43, 0], group);
addMesh(new THREE.BoxGeometry(0.54, 0.3, 0.35), jerseyMat, [0, 0.93, 0], group);
const head = addMesh(
new THREE.SphereGeometry(0.25, 8, 5),
skinMat,
[0, 2.13, -0.015],
group
);
head.scale.set(0.95, 1.08, 0.92);
addMesh(new THREE.BoxGeometry(0.39, 0.13, 0.35), hairMat, [0, 2.34, 0.015], group);
addMesh(new THREE.BoxGeometry(0.07, 0.055, 0.035), eyeMat, [-0.085, 2.16, -0.235], group);
addMesh(new THREE.BoxGeometry(0.07, 0.055, 0.035), eyeMat, [0.085, 2.16, -0.235], group);
addMesh(new THREE.BoxGeometry(0.07, 0.055, 0.05), skinMat, [0, 2.08, -0.245], group);
const legs = [];
const arms = [];
const hands = [];
for (const side of [-1, 1]) {
const legPivot = new THREE.Group();
legPivot.position.set(side * 0.18, 1.03, 0);
group.add(legPivot);
addMesh(
new THREE.BoxGeometry(0.22, 0.7, 0.24),
jerseyMat,
[0, -0.34, 0],
legPivot
);
addMesh(
new THREE.BoxGeometry(0.25, 0.14, 0.4),
shoeMat,
[0, -0.72, -0.075],
legPivot
);
legs.push(legPivot);
const armPivot = new THREE.Group();
armPivot.position.set(side * 0.43, 1.79, 0);
group.add(armPivot);
addMesh(
new THREE.BoxGeometry(0.16, 0.61, 0.18),
skinMat,
[0, -0.29, 0],
armPivot
);
const hand = addMesh(
new THREE.SphereGeometry(0.105, 7, 5),
skinMat,
[0, -0.62, 0],
armPivot
);
hands.push(hand);
arms.push(armPivot);
}
return {
group,
jerseyMat,
legs,
arms,
hands,
phase: 0
};
}
const receiver = createRunner(0x258cc8, 0xd99972, 0x352820);
const carrier = createRunner(0xde674a, 0x8a573e, 0x18191c);
// Qualification rings underneath the two athletes.
function makeQualificationRing(color) {
const mat = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.88,
side: THREE.DoubleSide,
depthWrite: false
});
const ring = new THREE.Mesh(new THREE.RingGeometry(0.31, 0.43, 16), mat);
ring.rotation.x = -Math.PI / 2;
ring.position.y = 0.115;
scene.add(ring);
return ring;
}
const receiverRing = makeQualificationRing(0x41d9ff);
const carrierRing = makeQualificationRing(0xffb34d);
// Baton.
const baton = new THREE.Group();
scene.add(baton);
const batonYellow = standard(0xffcf3f, {
emissive: 0x5b3900,
emissiveIntensity: 0.2
});
const batonWhite = standard(0xf8f4df);
const batonBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 0.78, 8),
batonYellow
);
batonBody.rotation.z = Math.PI / 2;
batonBody.castShadow = true;
baton.add(batonBody);
const batonBand = new THREE.Mesh(
new THREE.CylinderGeometry(0.058, 0.058, 0.12, 8),
batonWhite
);
batonBand.rotation.z = Math.PI / 2;
baton.add(batonBand);
// -------------------------------------------------------------------------
// Runner animation. All motion is derived directly from simulation time.
// -------------------------------------------------------------------------
function updateRunner(runner, x, z, t, isReceiver) {
const stride = Math.sin(t * 9.2 + runner.phase) * 0.66;
const bob = Math.abs(Math.sin(t * 9.2 + runner.phase)) * 0.035;
runner.group.position.set(x, bob, z);
runner.group.rotation.y = 0;
runner.legs[0].rotation.x = stride;
runner.legs[1].rotation.x = -stride;
const inside = inPad(z);
const normalArm = Math.sin(t * 9.2 + runner.phase + Math.PI) * 0.62;
runner.arms[0].rotation.x = inside ? 1.0 : normalArm;
runner.arms[1].rotation.x = inside ? 1.15 : -normalArm;
runner.arms[0].rotation.z = inside ? -0.1 : 0;
runner.arms[1].rotation.z = inside ? 0.1 : 0;
runner.jerseyMat.emissive.setHex(inside ? 0x123d46 : 0x000000);
runner.jerseyMat.emissiveIntensity = inside ? 0.5 : 0;
}
function updateRunners(t) {
updateRunner(receiver, RECEIVER_X, receiverZ(t), t, true);
updateRunner(carrier, CARRIER_X, carrierZ(t), t, false);
receiverRing.position.z = receiverZ(t);
carrierRing.position.z = carrierZ(t);
receiverRing.visible = inPad(receiverZ(t));
carrierRing.visible = inPad(carrierZ(t));
}
// -------------------------------------------------------------------------
// Deterministic game state.
// -------------------------------------------------------------------------
let simulationTime = 0;
let accumulator = 0;
let lastFrameTime = performance.now();
let activeAttempt = null;
let lastAttempt = null;
let pendingReplay = null;
const tmpA = new THREE.Vector3();
const tmpB = new THREE.Vector3();
function updateBaton(t) {
scene.updateMatrixWorld(true);
receiver.hands[1].getWorldPosition(tmpA);
if (!activeAttempt || activeAttempt.result !== "success") {
baton.position.copy(tmpA);
return;
}
if (t <= activeAttempt.time) {
baton.position.copy(tmpA);
return;
}
const transfer = THREE.MathUtils.clamp(
(t - activeAttempt.time) / 0.38,
0,
1
);
const smooth = transfer * transfer * (3 - 2 * transfer);
carrier.hands[0].getWorldPosition(tmpB);
baton.position.lerpVectors(activeAttempt.batonStart, tmpB, smooth);
}
function setStatus(kind, title, detail) {
statusEl.dataset.kind = kind;
statusTitleEl.textContent = title;
statusDetailEl.textContent = detail;
}
function performAttempt(t, replayed = false) {
if (activeAttempt) {
return;
}
const receiverInside = inPad(receiverZ(t));
const carrierInside = inPad(carrierZ(t));
const legal = receiverInside && carrierInside;
let result;
if (t < overlapStart - 1e-9) {
result = "early";
} else if (t > overlapEnd + 1e-9) {
result = "late";
} else if (legal) {
result = "success";
} else {
result = "invalid";
}
updateRunners(t);
scene.updateMatrixWorld(true);
const batonStart = new THREE.Vector3();
receiver.hands[1].getWorldPosition(batonStart);
activeAttempt = {
time: t,
result,
replayed,
batonStart
};
lastAttempt = {
time: t,
result
};
if (result === "success") {
setStatus(
"success",
replayed ? "REPLAY ACCEPTED" : "HANDOFF ACCEPTED",
`Both athletes were inside the exchange pad at T+${t.toFixed(3)} s.`
);
} else if (result === "early") {
const difference = Math.round((overlapStart - t) * 1000);
setStatus(
"early",
"REJECTED · EARLY",
`T+${t.toFixed(3)} s was ${difference} ms before the legal overlap.`
);
} else if (result === "late") {
const difference = Math.round((t - overlapEnd) * 1000);
setStatus(
"late",
"REJECTED · LATE",
`T+${t.toFixed(3)} s was ${difference} ms after the legal overlap.`
);
} else {
setStatus(
"late",
"REJECTED",
"At least one athlete was outside the exchange pad."
);
}
handoffButton.disabled = true;
replayButton.disabled = true;
}
function issueHandoff() {
if (activeAttempt || pendingReplay !== null) {
return;
}
performAttempt(simulationTime, false);
}
function resetRun() {
simulationTime = 0;
accumulator = 0;
lastFrameTime = performance.now();
activeAttempt = null;
pendingReplay = null;
handoffButton.disabled = false;
replayButton.disabled = !lastAttempt;
if (lastAttempt) {
replayButton.textContent =
`Replay exact input · ${lastAttempt.time.toFixed(3)} s`;
} else {
replayButton.textContent = "Replay exact input";
}
updateRunners(0);
updateBaton(0);
}
function replayExactInput() {
if (!lastAttempt || activeAttempt || pendingReplay !== null) {
return;
}
resetRun();
pendingReplay = lastAttempt.time;
replayButton.disabled = true;
replayButton.textContent = `Replaying · ${lastAttempt.time.toFixed(3)} s`;
setStatus(
"wait",
"DETERMINISTIC REPLAY",
"Restarting at T+0.000 with the exact recorded input timestamp."
);
}
handoffButton.addEventListener("click", issueHandoff);
resetButton.addEventListener("click", resetRun);
replayButton.addEventListener("click", replayExactInput);
window.addEventListener("keydown", (event) => {
const target = event.target;
const canSpace =
target === document.body ||
target === renderer.domElement ||
target === handoffButton;
if (event.code === "Space" && canSpace) {
event.preventDefault();
if (!event.repeat) {
issueHandoff();
}
}
});
// -------------------------------------------------------------------------
// HUD updates.
// -------------------------------------------------------------------------
function updateHUD() {
const shownTime = Math.min(simulationTime, RUN_END);
clockEl.textContent = shownTime.toFixed(3);
timelineTimeEl.textContent = `${shownTime.toFixed(3)} s`;
playhead.style.left = percent(Math.min(shownTime, RUN_END));
const receiverInside = inPad(receiverZ(shownTime));
const carrierInside = inPad(carrierZ(shownTime));
receiverPill.classList.toggle("on", receiverInside);
carrierPill.classList.toggle("on", carrierInside);
receiverPill.querySelector("span:last-child").textContent = receiverInside
? "IN PAD"
: shownTime < receiverInStart
? "APPROACH"
: "PAST PAD";
carrierPill.querySelector("span:last-child").textContent = carrierInside
? "IN PAD"
: shownTime < carrierInStart
? "WAITING"
: "PAST PAD";
if (!activeAttempt && pendingReplay === null) {
if (shownTime < overlapStart) {
setStatus(
"wait",
"WAIT FOR OVERLAP",
"The legal shared window has not opened yet."
);
} else if (shownTime <= overlapEnd) {
const remaining = Math.max(0, overlapEnd - shownTime);
setStatus(
"live",
"OVERLAP LIVE · HAND OFF NOW",
`Both runners qualify. ${Math.ceil(remaining * 1000)} ms remain.`
);
} else {
setStatus(
"wait",
"OVERLAP CLOSED",
"Any command from this point onward is rejected as LATE."
);
}
}
if (activeAttempt) {
const kind =
activeAttempt.result === "success"
? "success"
: activeAttempt.result === "early"
? "early"
: "late";
const zoneColor =
kind === "success"
? 0x65f09b
: kind === "late" || kind === "early"
? 0xff657a
: shownTime >= overlapStart && shownTime <= overlapEnd
? 0x65f09b
: 0x40d7ff;
zoneMat.color.setHex(zoneColor);
zoneMat.emissive.setHex(zoneColor);
zoneMat.emissiveIntensity = kind === "wait" ? 0.75 : 1.15;
zoneMat.opacity = kind === "wait" ? 0.43 : 0.58;
} else {
const live =
shownTime >= overlapStart && shownTime <= overlapEnd;
const color = live ? 0x65f09b : 0x40d7ff;
zoneMat.color.setHex(color);
zoneMat.emissive.setHex(color);
zoneMat.emissiveIntensity = live ? 1.15 : 0.65;
zoneMat.opacity = live ? 0.58 : 0.38;
}
}
// -------------------------------------------------------------------------
// Direct camera controls: pointer orbit and wheel zoom.
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.05, 3.2);
let cameraYaw = 0.69;
let cameraPitch = 0.56;
let cameraDistance = 23;
let targetYaw = cameraYaw;
let targetPitch = cameraPitch;
let targetDistance = cameraDistance;
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.focus({ preventScroll: true });
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
targetYaw -= dx * 0.006;
targetPitch = THREE.MathUtils.clamp(
targetPitch + dy * 0.0045,
0.22,
1.18
);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("lostpointercapture", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
targetDistance = THREE.MathUtils.clamp(
targetDistance * Math.exp(event.deltaY * 0.001),
10,
38
);
},
{ passive: false }
);
function updateCamera() {
cameraYaw += (targetYaw - cameraYaw) * 0.12;
cameraPitch += (targetPitch - cameraPitch) * 0.12;
cameraDistance += (targetDistance - cameraDistance) * 0.12;
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
// -------------------------------------------------------------------------
// Resize and fixed-step animation loop.
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
document.addEventListener("visibilitychange", () => {
lastFrameTime = performance.now();
accumulator = 0;
});
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - lastFrameTime) / 1000, 0.1);
lastFrameTime = now;
if (!document.hidden) {
accumulator += delta;
while (accumulator >= FIXED_DT) {
const nextTime = simulationTime + FIXED_DT;
if (
pendingReplay !== null &&
nextTime >= pendingReplay - 1e-9
) {
const replayTime = pendingReplay;
pendingReplay = null;
simulationTime = replayTime;
performAttempt(replayTime, true);
accumulator = 0;
break;
}
simulationTime = Math.min(nextTime, RUN_END);
accumulator -= FIXED_DT;
if (simulationTime >= RUN_END) {
accumulator = 0;
break;
}
}
}
updateRunners(simulationTime);
updateBaton(simulationTime);
updateCamera();
updateHUD();
renderer.render(scene, camera);
}
resetRun();
requestAnimationFrame(animate);
</script>
</body>
</html>
```