Prompt specification
Build or customize the Three.js 3D scene "Stroke Phase Matching Game" 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: Stroke Phase Matching Game
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/stroke-phase-matching-game-x3644
Scene Description Prompt:
Create a voxel rowing timing game in one HTML file using Three.js from cdnjs. Let players align two repeated rowing stroke cycles. Score phase agreement directly and avoid presenting the toy score as a real rowing-performance measure.
Working Three.js Starter (stroke-phase-matching-game-x3644):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Phase Crew</title>
<style>
:root {
--ink: #123044;
--muted: #5d7484;
--panel: rgba(247, 252, 255, 0.91);
--line: rgba(31, 86, 112, 0.14);
--blue: #3978ff;
--orange: #ff8738;
--green: #20a67a;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #9ed9e4;
font-family:
Inter, ui-rounded, "SF Pro Rounded", "Trebuchet MS",
system-ui, sans-serif;
}
button {
font: inherit;
}
[hidden] {
display: none !important;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
body::after {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 42%,
transparent 48%,
rgba(13, 68, 88, 0.13) 115%
);
z-index: 1;
}
.topbar {
position: fixed;
top: 18px;
left: 18px;
right: 18px;
z-index: 3;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 18px;
pointer-events: none;
}
.brand,
.metrics,
.guide,
.deck,
#toast {
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
min-width: 245px;
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(247, 252, 255, 0.88);
box-shadow: 0 12px 35px rgba(16, 67, 87, 0.12);
}
.eyebrow {
color: var(--muted);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.brand h1 {
margin: 3px 0 2px;
font-size: 23px;
line-height: 1;
letter-spacing: -0.04em;
}
.brand p {
margin: 7px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.metrics {
display: flex;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(247, 252, 255, 0.9);
box-shadow: 0 12px 35px rgba(16, 67, 87, 0.12);
}
.metric {
min-width: 122px;
padding: 12px 15px;
border-left: 1px solid var(--line);
}
.metric:first-child {
border-left: 0;
}
.metric-label {
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric-value {
margin-top: 3px;
font-size: 23px;
font-weight: 900;
letter-spacing: -0.04em;
font-variant-numeric: tabular-nums;
}
.metric-value.orange {
color: var(--orange);
}
.metric-value.blue {
color: var(--blue);
}
.guide {
position: fixed;
top: 120px;
left: 18px;
z-index: 3;
width: 245px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(247, 252, 255, 0.8);
box-shadow: 0 12px 35px rgba(16, 67, 87, 0.09);
pointer-events: none;
}
.guide strong {
display: block;
margin-bottom: 5px;
font-size: 13px;
}
.guide p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.guide .camera-help {
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--line);
font-size: 10px;
}
#toast {
position: fixed;
top: 105px;
left: 50%;
z-index: 8;
max-width: min(480px, calc(100vw - 32px));
padding: 11px 16px;
border: 1px solid rgba(32, 166, 122, 0.3);
border-radius: 12px;
background: rgba(240, 255, 249, 0.95);
color: #12684f;
box-shadow: 0 12px 35px rgba(16, 67, 87, 0.15);
font-size: 13px;
font-weight: 800;
text-align: center;
opacity: 0;
transform: translate(-50%, -10px);
transition:
opacity 180ms ease,
transform 180ms ease;
pointer-events: none;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.deck {
position: fixed;
bottom: 16px;
left: 50%;
z-index: 4;
width: min(780px, calc(100vw - 36px));
padding: 15px 17px 12px;
border: 1px solid var(--line);
border-radius: 19px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(16, 67, 87, 0.17);
transform: translateX(-50%);
}
.deck-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
}
.deck h2 {
margin: 3px 0 2px;
font-size: 17px;
letter-spacing: -0.025em;
}
.deck-subtitle {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#status {
max-width: 290px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
line-height: 1.35;
text-align: right;
}
.agreement-line {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 10px;
margin: 12px 0 10px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
}
.agreement-track {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(38, 91, 115, 0.11);
}
#agreementFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #ffad61, #20a67a);
transition: width 100ms linear;
}
.phase-rows {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.phase-row {
display: grid;
grid-template-columns: 43px 1fr;
align-items: center;
gap: 8px;
}
.phase-tag {
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
}
.phase-tag.reference {
color: var(--blue);
}
.phase-tag.player {
color: var(--orange);
}
.phase-track {
position: relative;
height: 22px;
border: 1px solid rgba(34, 85, 108, 0.1);
border-radius: 7px;
overflow: hidden;
background:
repeating-linear-gradient(
90deg,
rgba(28, 78, 101, 0.07) 0,
rgba(28, 78, 101, 0.07) 1px,
transparent 1px,
transparent 25%
),
rgba(255, 255, 255, 0.55);
}
.needle {
position: absolute;
top: 3px;
left: 0;
width: 8px;
height: 14px;
border-radius: 3px;
transform: translateX(-50%);
box-shadow: 0 2px 5px rgba(18, 48, 68, 0.18);
}
#leaderNeedle {
background: var(--blue);
}
#playerNeedle {
background: var(--orange);
}
.controls {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
}
.button-row {
display: flex;
align-items: center;
gap: 7px;
}
.control-button {
min-height: 39px;
padding: 8px 12px;
border: 1px solid rgba(35, 87, 111, 0.16);
border-radius: 11px;
background: #fff;
color: var(--ink);
cursor: pointer;
font-size: 13px;
font-weight: 900;
box-shadow: 0 3px 0 rgba(27, 75, 98, 0.1);
user-select: none;
touch-action: manipulation;
}
.control-button:hover:not(:disabled) {
transform: translateY(-1px);
background: #f3fbff;
}
.control-button:active:not(:disabled) {
transform: translateY(1px);
box-shadow: none;
}
.control-button:focus-visible {
outline: 3px solid rgba(57, 120, 255, 0.28);
outline-offset: 2px;
}
.control-button:disabled {
cursor: not-allowed;
opacity: 0.42;
}
.control-button.nudge {
min-width: 54px;
font-size: 19px;
}
.control-button.fine[aria-pressed="true"] {
border-color: rgba(255, 135, 56, 0.5);
background: #fff1e8;
color: #b9561a;
}
.control-button.primary {
border-color: transparent;
background: var(--green);
color: white;
box-shadow: 0 3px 0 #147a59;
}
.control-button.next {
border-color: transparent;
background: var(--blue);
color: white;
box-shadow: 0 3px 0 #255ac5;
}
.keyboard-hint {
flex: 1;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
text-align: center;
}
.disclaimer {
margin: 10px 0 0;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.35;
text-align: center;
}
@media (max-width: 760px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
gap: 8px;
}
.brand {
min-width: 0;
padding: 10px 12px;
}
.brand h1 {
font-size: 18px;
}
.brand p,
.guide {
display: none;
}
.metrics {
border-radius: 13px;
}
.metric {
min-width: 80px;
padding: 10px;
}
.metric-label {
font-size: 7px;
}
.metric-value {
font-size: 18px;
}
.deck {
bottom: 9px;
width: calc(100vw - 18px);
padding: 12px;
}
.deck-header {
gap: 8px;
}
.deck h2 {
font-size: 15px;
}
#status {
max-width: 42%;
font-size: 9px;
}
.keyboard-hint {
display: none;
}
.controls {
justify-content: center;
}
.phase-rows {
gap: 8px;
}
.phase-row {
grid-template-columns: 34px 1fr;
gap: 5px;
}
.control-button {
padding: 8px 10px;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Voxel rowing phase alignment scene"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Voxel timing puzzle</div>
<h1>Phase Crew</h1>
<p>Align two repeating stroke cycles.</p>
</div>
<div class="metrics" aria-label="Phase agreement statistics">
<div class="metric">
<div class="metric-label">Current agreement</div>
<div id="agreementValue" class="metric-value orange">—</div>
</div>
<div class="metric">
<div class="metric-label">Phase error</div>
<div id="phaseError" class="metric-value">—</div>
</div>
<div class="metric">
<div class="metric-label">Best agreement</div>
<div id="bestValue" class="metric-value blue">—</div>
</div>
</div>
</header>
<aside class="guide">
<strong>Match the two cycles</strong>
<p>
Blue is the reference. Nudge orange until their oars and beat lights move
together, then lock the phase.
</p>
<p class="camera-help">
Drag the scene to orbit · wheel to zoom · double-click to reset view
</p>
</aside>
<div id="toast" role="status" aria-live="polite"></div>
<main class="deck">
<div class="deck-header">
<div>
<div class="eyebrow" id="roundLabel">Round 1</div>
<h2 id="relationText">Orange lags the reference cycle</h2>
<p class="deck-subtitle">
Direct score: 100 × (1 − circular phase error ÷ 180°)
</p>
</div>
<div id="status">
Nudge orange with A/D or arrow keys. Shift uses 0.2° steps.
</div>
</div>
<div class="agreement-line">
<span>PHASE AGREEMENT</span>
<div class="agreement-track">
<div id="agreementFill"></div>
</div>
<span id="offsetLabel">OFFSET 0°</span>
</div>
<div class="phase-rows" aria-label="Cycle phase indicators">
<div class="phase-row">
<span class="phase-tag reference">REF</span>
<div class="phase-track">
<i id="leaderNeedle" class="needle"></i>
</div>
</div>
<div class="phase-row">
<span class="phase-tag player">YOU</span>
<div class="phase-track">
<i id="playerNeedle" class="needle"></i>
</div>
</div>
</div>
<div class="controls">
<div class="button-row">
<button
id="minusButton"
class="control-button nudge"
type="button"
aria-label="Decrease phase offset"
title="Decrease phase offset"
>−</button>
<button
id="fineToggle"
class="control-button fine"
type="button"
aria-pressed="false"
title="Toggle fine adjustment"
>Fine 0.2°</button>
<button
id="plusButton"
class="control-button nudge"
type="button"
aria-label="Increase phase offset"
title="Increase phase offset"
>+</button>
</div>
<div class="keyboard-hint">
A/D or ←/→ to nudge · Space to lock · N for next cycle
</div>
<div class="button-row">
<button id="lockButton" class="control-button primary" type="button">
Lock phase
</button>
<button id="nextButton" class="control-button next" type="button" hidden>
Next cycle
</button>
</div>
</div>
<p class="disclaimer">
Toy phase-agreement score only. It measures alignment between these two
repeated animation cycles—not rowing speed, technique, fitness, or any
real-world rowing performance.
</p>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const TAU = Math.PI * 2;
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const ui = {
agreement: $("agreementValue"),
phaseError: $("phaseError"),
best: $("bestValue"),
fill: $("agreementFill"),
relation: $("relationText"),
offset: $("offsetLabel"),
round: $("roundLabel"),
status: $("status"),
leaderNeedle: $("leaderNeedle"),
playerNeedle: $("playerNeedle"),
minus: $("minusButton"),
plus: $("plusButton"),
fine: $("fineToggle"),
lock: $("lockButton"),
next: $("nextButton"),
toast: $("toast")
};
const state = {
round: 1,
offset: 76,
best: 0,
locked: false,
fine: false,
tempo: 0.82,
cycleStart: 0,
elapsed: 0
};
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9ed9e4);
scene.fog = new THREE.Fog(0x9ed9e4, 24, 48);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
$("scene").appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xe9fbff, 0x438b96, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d8, 3.1);
sun.position.set(-6, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0007;
scene.add(sun);
const cameraTarget = new THREE.Vector3(0, 0.45, 0);
const cameraState = {
yaw: 0.52,
pitch: 0.72,
radius: 19
};
function updateCamera() {
const { yaw, pitch, radius } = cameraState;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * Math.cos(pitch) * radius,
cameraTarget.y + Math.sin(pitch) * radius,
cameraTarget.z + Math.cos(yaw) * Math.cos(pitch) * radius
);
camera.lookAt(cameraTarget);
}
updateCamera();
// Direct camera controls: no Three.js addons are used.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.yaw = THREE.MathUtils.clamp(
cameraState.yaw - dx * 0.006,
-0.45,
1.28
);
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.38,
1.18
);
updateCamera();
});
function stopDragging() {
dragging = false;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
13,
27
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.yaw = 0.52;
cameraState.pitch = 0.72;
cameraState.radius = 19;
updateCamera();
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
function makeMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.02,
...options
});
}
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Water bed.
const waterBed = new THREE.Mesh(
new THREE.PlaneGeometry(42, 30),
makeMaterial(0x4caebf, { roughness: 0.9 })
);
waterBed.rotation.x = -Math.PI / 2;
waterBed.position.y = -0.36;
waterBed.receiveShadow = true;
scene.add(waterBed);
// Animated voxel water.
const waterCountX = 29;
const waterCountZ = 19;
const waterCount = waterCountX * waterCountZ;
const waterGeometry = new THREE.BoxGeometry(0.66, 0.12, 0.66);
const waterMaterial = makeMaterial(0x5bc6d2, { roughness: 0.58 });
const water = new THREE.InstancedMesh(
waterGeometry,
waterMaterial,
waterCount
);
water.receiveShadow = true;
scene.add(water);
const waterData = [];
const waterDummy = new THREE.Object3D();
const waterColor = new THREE.Color();
let waterIndex = 0;
for (let x = 0; x < waterCountX; x++) {
for (let z = 0; z < waterCountZ; z++) {
const px = -10.8 + x * 0.77;
const pz = -7.1 + z * 0.79;
const py = -0.26 + Math.random() * 0.08;
const phase = Math.random() * TAU;
waterData.push({ px, py, pz, phase });
waterDummy.position.set(px, py, pz);
waterDummy.scale.setScalar(0.92 + Math.random() * 0.13);
waterDummy.updateMatrix();
water.setMatrixAt(waterIndex, waterDummy.matrix);
waterColor.setHSL(
0.51 + Math.random() * 0.025,
0.48 + Math.random() * 0.12,
0.54 + Math.random() * 0.08
);
water.setColorAt(waterIndex, waterColor);
waterIndex++;
}
}
water.instanceMatrix.needsUpdate = true;
if (water.instanceColor) water.instanceColor.needsUpdate = true;
// Lane overlays and dashed borders.
const laneCenters = [-2.2, 2.2];
const laneColors = [0x3978ff, 0xff8738];
laneCenters.forEach((z, laneIndex) => {
const lane = new THREE.Mesh(
new THREE.PlaneGeometry(14, 3.2),
new THREE.MeshBasicMaterial({
color: laneColors[laneIndex],
transparent: true,
opacity: 0.105,
depthWrite: false
})
);
lane.rotation.x = -Math.PI / 2;
lane.position.set(0, -0.09, z);
scene.add(lane);
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0xe9fbff,
transparent: true,
opacity: 0.62
});
[-1.67, 1.67].forEach((relativeZ) => {
for (let x = -7; x <= 7; x += 1.1) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.46, 0.035, 0.07),
markerMaterial
);
marker.position.set(x, -0.045, z + relativeZ);
scene.add(marker);
}
});
});
// Decorative voxel clouds.
const cloudMaterial = makeMaterial(0xf6fbfc, {
roughness: 1,
emissive: 0x9acbd1,
emissiveIntensity: 0.08
});
function addCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
addBox(
group,
new THREE.Vector3(2.2, 0.7, 0.8),
new THREE.Vector3(0, 0, 0),
cloudMaterial,
false
);
addBox(
group,
new THREE.Vector3(1.0, 1.0, 0.8),
new THREE.Vector3(-0.7, 0.25, 0),
cloudMaterial,
false
);
addBox(
group,
new THREE.Vector3(1.3, 0.9, 0.8),
new THREE.Vector3(0.8, 0.2, 0),
cloudMaterial,
false
);
scene.add(group);
}
addCloud(-8.5, 6.2, -9, 1.0);
addCloud(0.5, 7.0, -10, 0.8);
addCloud(8.5, 5.8, -8, 1.15);
// Shared geometry/materials for dynamic voxel oars.
const barGeometry = new THREE.BoxGeometry(1, 1, 1);
const darkMaterial = makeMaterial(0x263d4b);
const woodMaterial = makeMaterial(0xc58b54);
const skinMaterial = makeMaterial(0xf1b58e);
const shirtMaterial = makeMaterial(0xf6f0e3);
const hullMaterial = makeMaterial(0xf4f1e7);
const deckMaterial = makeMaterial(0xd9e4e3);
const cockpitMaterial = makeMaterial(0x254452);
function makeBar(material) {
const mesh = new THREE.Mesh(barGeometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function setBarBetween(mesh, start, end, thickness = 0.08) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.scale.set(thickness, Math.max(length, 0.001), thickness);
if (length > 0.0001) {
direction.normalize();
mesh.quaternion.setFromUnitVectors(Y_AXIS, direction);
}
}
function createBoat(z, accentColor, isReference) {
const group = new THREE.Group();
group.position.z = z;
scene.add(group);
const accent = makeMaterial(accentColor, {
emissive: accentColor,
emissiveIntensity: 0.08
});
// Voxel hull.
addBox(
group,
new THREE.Vector3(3.8, 0.38, 1.08),
new THREE.Vector3(0, 0.13, 0),
hullMaterial
);
addBox(
group,
new THREE.Vector3(0.85, 0.32, 0.78),
new THREE.Vector3(2.15, 0.16, 0),
hullMaterial
);
addBox(
group,
new THREE.Vector3(0.7, 0.3, 0.8),
new THREE.Vector3(-2.1, 0.14, 0),
hullMaterial
);
addBox(
group,
new THREE.Vector3(2.55, 0.13, 0.87),
new THREE.Vector3(-0.25, 0.37, 0),
deckMaterial
);
addBox(
group,
new THREE.Vector3(1.15, 0.06, 0.54),
new THREE.Vector3(-0.22, 0.47, 0),
cockpitMaterial
);
addBox(
group,
new THREE.Vector3(1.55, 0.09, 0.1),
new THREE.Vector3(0.15, 0.47, 0.43),
accent
);
addBox(
group,
new THREE.Vector3(1.55, 0.09, 0.1),
new THREE.Vector3(0.15, 0.47, -0.43),
accent
);
// Seat and sliding body.
const seat = addBox(
group,
new THREE.Vector3(0.5, 0.13, 0.6),
new THREE.Vector3(0.2, 0.48, 0),
darkMaterial
);
const body = new THREE.Group();
group.add(body);
addBox(
body,
new THREE.Vector3(0.46, 0.63, 0.48),
new THREE.Vector3(0, 0.55, 0),
shirtMaterial
);
addBox(
body,
new THREE.Vector3(0.38, 0.38, 0.38),
new THREE.Vector3(0.04, 1.02, 0),
skinMaterial
);
addBox(
body,
new THREE.Vector3(0.42, 0.13, 0.42),
new THREE.Vector3(0.03, 1.23, 0),
accent
);
addBox(
body,
new THREE.Vector3(0.68, 0.18, 0.34),
new THREE.Vector3(-0.18, 0.22, 0),
darkMaterial
);
// Oars and arms.
const oars = [];
for (const side of [-1, 1]) {
const handle = makeBar(darkMaterial);
const shaft = makeBar(woodMaterial);
const arm = makeBar(skinMaterial);
const blade = addBox(
group,
new THREE.Vector3(0.36, 0.11, 0.25),
new THREE.Vector3(),
accent
);
group.add(handle, shaft, arm);
oars.push({ side, handle, shaft, arm, blade });
}
// Beat-light tower.
const tower = new THREE.Group();
tower.position.set(-3.35, 0, 0.82);
group.add(tower);
addBox(
tower,
new THREE.Vector3(0.75, 0.15, 0.75),
new THREE.Vector3(0, -0.05, 0),
deckMaterial
);
const towerSegments = [];
const segmentCount = 10;
for (let i = 0; i < segmentCount; i++) {
const segmentMaterial = makeMaterial(0x53717c, {
emissive: 0x000000,
emissiveIntensity: 0
});
const segment = addBox(
tower,
new THREE.Vector3(0.43, 0.22, 0.43),
new THREE.Vector3(0, 0.2 + i * 0.28, 0),
segmentMaterial
);
towerSegments.push(segment);
}
// Small voxel splashes.
const splashMaterial = new THREE.MeshBasicMaterial({
color: isReference ? 0xb9edff : 0xffd1ad,
transparent: true,
opacity: 0.8
});
const splashes = [];
for (let i = 0; i < 14; i++) {
const splash = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.12, 0.12),
splashMaterial
);
splash.userData.baseX = (Math.random() - 0.5) * 0.75;
splash.userData.baseZ = (Math.random() < 0.5 ? -1 : 1) *
(0.87 + Math.random() * 0.27);
splash.userData.baseY = Math.random() * 0.12;
splash.userData.speed = 0.8 + Math.random() * 1.5;
group.add(splash);
splashes.push(splash);
}
return {
group,
body,
seat,
oars,
towerSegments,
splashes,
accentColor
};
}
const referenceBoat = createBoat(laneCenters[0], laneColors[0], true);
const playerBoat = createBoat(laneCenters[1], laneColors[1], false);
const towerDark = new THREE.Color(0x526e78);
const towerActive = new THREE.Color();
function updateBoat(boat, phase, time) {
const stroke = 0.5 - 0.5 * Math.cos(phase * TAU);
const seatX = 0.48 - stroke * 0.96;
boat.seat.position.x = seatX;
boat.body.position.x = seatX;
boat.body.rotation.z = stroke * 0.34;
boat.group.position.y = Math.sin(time * 2.2 + phase * TAU) * 0.025;
boat.group.rotation.x = Math.sin(time * 1.7 + phase * TAU) * 0.018;
for (const oar of boat.oars) {
const side = oar.side;
const pivot = new THREE.Vector3(-0.08, 0.68, side * 0.34);
const hand = new THREE.Vector3(
0.52 - stroke * 1.04,
0.84,
side * 0.3
);
const bladePosition = new THREE.Vector3(
1.36 - stroke * 2.72,
0.19,
side * 1.02
);
setBarBetween(oar.handle, pivot, hand, 0.075);
setBarBetween(oar.shaft, hand, bladePosition, 0.065);
const shoulder = new THREE.Vector3(
boat.body.position.x + 0.02,
1.05,
side * 0.17
);
setBarBetween(oar.arm, shoulder, hand, 0.12);
oar.blade.position.copy(bladePosition);
oar.blade.rotation.y = side * 0.16;
}
const activeSegment = Math.min(
boat.towerSegments.length - 1,
Math.floor(phase * boat.towerSegments.length)
);
towerActive.setHex(boat.accentColor);
boat.towerSegments.forEach((segment, index) => {
const isActive = index === activeSegment;
segment.material.color.copy(isActive ? towerActive : towerDark);
segment.material.emissive.copy(towerActive);
segment.material.emissiveIntensity = isActive ? 0.38 : 0;
segment.scale.y = isActive ? 1.35 : 1;
segment.position.y = 0.2 + index * 0.28;
});
const catchStrength = Math.pow(
Math.abs(Math.cos(phase * Math.PI)),
9
);
const finishStrength = Math.pow(
Math.abs(Math.sin(phase * Math.PI)),
9
);
const splashStrength = Math.max(catchStrength, finishStrength);
const splashX =
phase < 0.25 || phase > 0.75 ? 1.32 : -1.32;
for (const splash of boat.splashes) {
const scale = Math.max(
0.001,
splashStrength *
(0.55 + Math.random() * 0.65) *
splash.userData.speed
);
splash.scale.setScalar(scale);
splash.position.set(
splashX + splash.userData.baseX,
-0.03 + splash.userData.baseY + splashStrength * 0.18,
splash.userData.baseZ
);
}
}
// -------------------------------------------------------------------------
// Game state and direct phase scoring
// -------------------------------------------------------------------------
function phaseErrorDegrees() {
// state.offset is already normalized to [-180°, 180°].
return Math.abs(state.offset);
}
function agreementPercent() {
const error = phaseErrorDegrees();
return 100 * (1 - error / 180);
}
function updateScoreUI() {
const error = phaseErrorDegrees();
const agreement = agreementPercent();
ui.agreement.textContent = `${agreement.toFixed(1)}%`;
ui.phaseError.textContent = `${error.toFixed(1)}°`;
ui.best.textContent = state.best > 0
? `${state.best.toFixed(1)}%`
: "—";
ui.fill.style.width = `${agreement.toFixed(2)}%`;
ui.offset.textContent =
`OFFSET ${state.offset >= 0 ? "+" : ""}${state.offset.toFixed(1)}°`;
if (error < 0.05) {
ui.relation.textContent = "The two cycles are in phase";
} else {
const relation = state.offset > 0 ? "leads" : "lags";
ui.relation.textContent =
`Orange ${relation} the reference by ${error.toFixed(1)}°`;
}
ui.round.textContent = `Round ${state.round}`;
}
let toastTimer = 0;
function showToast(message) {
clearTimeout(toastTimer);
ui.toast.textContent = message;
ui.toast.classList.add("show");
toastTimer = setTimeout(() => {
ui.toast.classList.remove("show");
}, 2600);
}
function adjustOffset(delta) {
if (state.locked) return;
state.offset =
(((state.offset + delta + 180) % 360) + 360) % 360 - 180;
updateScoreUI();
}
function setLocked(locked) {
state.locked = locked;
ui.minus.disabled = locked;
ui.plus.disabled = locked;
ui.fine.disabled = locked;
ui.lock.disabled = locked;
ui.next.hidden = !locked;
if (locked) {
ui.status.textContent =
"Phase locked. Start another cycle when you are ready.";
} else {
ui.status.textContent =
"Nudge orange with A/D or arrow keys. Shift uses 0.2° steps.";
}
}
function lockPhase() {
if (state.locked) return;
const agreement = agreementPercent();
state.best = Math.max(state.best, agreement);
updateScoreUI();
setLocked(true);
showToast(
`Round ${state.round} locked — ${agreement.toFixed(1)}% phase agreement.`
);
}
function nextCycle() {
if (!state.locked) return;
state.round++;
const magnitude = 62 + Math.random() * 72;
state.offset = (Math.random() < 0.5 ? -1 : 1) * magnitude;
state.tempo = 0.68 + Math.random() * 0.28;
state.cycleStart = state.elapsed;
setLocked(false);
updateScoreUI();
showToast(`Round ${state.round}: find and match the new phase offset.`);
}
ui.minus.addEventListener("click", () => {
adjustOffset(state.fine ? -0.2 : -1);
});
ui.plus.addEventListener("click", () => {
adjustOffset(state.fine ? 0.2 : 1);
});
ui.fine.addEventListener("click", () => {
state.fine = !state.fine;
ui.fine.setAttribute("aria-pressed", String(state.fine));
});
ui.lock.addEventListener("click", lockPhase);
ui.next.addEventListener("click", nextCycle);
// Press-and-hold adjustment for the on-screen buttons.
function bindHold(button, direction) {
let holdTimer = 0;
let repeatTimer = 0;
function start(event) {
if (state.locked) return;
event.preventDefault();
adjustOffset(direction * (state.fine ? 0.2 : 1));
holdTimer = setTimeout(() => {
repeatTimer = setInterval(() => {
adjustOffset(direction * (state.fine ? 0.2 : 1));
}, 55);
}, 320);
}
function stop() {
clearTimeout(holdTimer);
clearInterval(repeatTimer);
}
button.addEventListener("pointerdown", start);
button.addEventListener("pointerup", stop);
button.addEventListener("pointerleave", stop);
button.addEventListener("pointercancel", stop);
}
bindHold(ui.minus, -1);
bindHold(ui.plus, 1);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (
["arrowleft", "arrowright", "a", "d", " "].includes(key)
) {
event.preventDefault();
}
if (key === "arrowleft" || key === "a") {
adjustOffset(event.shiftKey ? -0.2 : -1);
} else if (key === "arrowright" || key === "d") {
adjustOffset(event.shiftKey ? 0.2 : 1);
} else if (key === " ") {
lockPhase();
} else if (key === "n") {
nextCycle();
} else if (key === "c") {
cameraState.yaw = 0.52;
cameraState.pitch = 0.72;
cameraState.radius = 19;
updateCamera();
}
});
updateScoreUI();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const time = clock.getElapsedTime();
state.elapsed = time;
const leaderPhase =
((time - state.cycleStart) * state.tempo) % 1;
const playerPhase =
(leaderPhase + state.offset / 360 + 1) % 1;
updateBoat(referenceBoat, leaderPhase, time);
updateBoat(playerBoat, playerPhase, time + 0.2);
ui.leaderNeedle.style.left = `${leaderPhase * 100}%`;
ui.playerNeedle.style.left = `${playerPhase * 100}%`;
waterData.forEach((item, index) => {
waterDummy.position.set(
item.px,
item.py +
Math.sin(time * 1.45 + item.phase + item.px * 0.2) * 0.035,
item.pz
);
waterDummy.scale.setScalar(1);
waterDummy.updateMatrix();
water.setMatrixAt(index, waterDummy.matrix);
});
water.instanceMatrix.needsUpdate = true;
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>
```