Prompt specification
Build or customize the Three.js 3D scene "Period Change Without Phase Surprise" 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: Period Change Without Phase Surprise
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/period-change-without-phase-surprise-x3480
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: period change without phase surprise. Create a playable timing puzzle with adjustable speed and a visible phase clock. Change period continuously, preserve current phase, and offer reset to a known starting state. Return one self-contained HTML file.
Working Three.js Starter (period-change-without-phase-surprise-x3480):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PHASE / HOLD — Voxel Timing Puzzle</title>
<style>
:root {
--bg: #07101d;
--panel: rgba(9, 19, 34, 0.78);
--panel-strong: rgba(8, 17, 30, 0.94);
--line: rgba(143, 208, 255, 0.18);
--text: #e9f6ff;
--muted: #7f9db5;
--cyan: #63e8ff;
--cyan-soft: rgba(99, 232, 255, 0.18);
--amber: #ffb45c;
--amber-soft: rgba(255, 180, 92, 0.16);
--green: #78f2a6;
--red: #ff6f7e;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body::before {
content: "";
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
opacity: 0.2;
background: repeating-linear-gradient(
to bottom,
transparent 0,
transparent 3px,
rgba(120, 210, 255, 0.025) 4px
);
}
button, input { font: inherit; }
button {
color: var(--text);
border: 1px solid var(--line);
background: rgba(19, 38, 57, 0.75);
border-radius: 10px;
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
button:hover {
border-color: rgba(99, 232, 255, 0.55);
background: rgba(25, 51, 75, 0.88);
}
button:active { transform: translateY(1px); }
button:focus-visible, input:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 3px;
}
#scene {
position: fixed;
inset: 0;
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
#vignette {
position: fixed;
inset: 0;
z-index: 1;
pointer-events: none;
background:
radial-gradient(circle at 50% 47%, transparent 35%, rgba(2, 8, 16, 0.22) 70%, rgba(2, 7, 14, 0.72) 100%),
linear-gradient(180deg, rgba(3, 10, 18, 0.25), transparent 25%, transparent 75%, rgba(3, 10, 18, 0.38));
}
.hud {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.interactive, .panel, button, input { pointer-events: auto; }
.topbar {
position: absolute;
top: 20px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 18px;
}
.brand {
min-width: 215px;
padding: 13px 16px 12px;
border-left: 3px solid var(--cyan);
background: linear-gradient(90deg, rgba(5, 15, 27, 0.82), rgba(5, 15, 27, 0.14));
filter: drop-shadow(0 10px 25px rgba(0, 0, 0, 0.25));
}
.brand h1 {
margin: 0;
font-size: 19px;
line-height: 1;
letter-spacing: 0.18em;
font-weight: 800;
}
.brand p {
margin: 8px 0 0;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.stats {
display: flex;
align-items: stretch;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 14px 45px rgba(0, 0, 0, 0.22);
}
.stat {
min-width: 86px;
padding: 5px 11px;
border-right: 1px solid rgba(143, 208, 255, 0.1);
}
.stat:last-child { border-right: 0; }
.stat-label {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.stat-value {
display: block;
margin-top: 4px;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 17px;
font-weight: 700;
letter-spacing: 0.04em;
}
#timeStat.warning { color: var(--red); }
#targetStat { color: var(--amber); }
.top-actions {
display: flex;
gap: 8px;
}
.top-actions button {
min-height: 43px;
padding: 0 13px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
backdrop-filter: blur(14px);
}
.panel {
border: 1px solid var(--line);
background: var(--panel);
backdrop-filter: blur(16px);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
}
.control-panel {
position: absolute;
top: 102px;
right: 22px;
width: 284px;
padding: 17px;
border-radius: 14px;
}
.panel-kicker {
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.period-readout {
display: flex;
align-items: flex-end;
justify-content: space-between;
margin: 10px 0 13px;
}
#periodValue {
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 33px;
line-height: 1;
letter-spacing: -0.05em;
}
.unit {
margin-left: 5px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.rate-badge {
padding: 6px 8px;
border: 1px solid rgba(99, 232, 255, 0.19);
border-radius: 7px;
background: var(--cyan-soft);
color: var(--cyan);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.06em;
}
input[type="range"] {
width: 100%;
height: 22px;
margin: 0;
appearance: none;
background: transparent;
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 5px;
border-radius: 10px;
background: linear-gradient(90deg, var(--cyan), #477a9c 65%, #253a50);
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
margin-top: -6px;
appearance: none;
border: 2px solid #d9f8ff;
border-radius: 4px;
background: var(--cyan);
box-shadow: 0 0 16px rgba(99, 232, 255, 0.55);
}
input[type="range"]::-moz-range-track {
height: 5px;
border-radius: 10px;
background: linear-gradient(90deg, var(--cyan), #477a9c 65%, #253a50);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 2px solid #d9f8ff;
border-radius: 4px;
background: var(--cyan);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #68859c;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 8px;
letter-spacing: 0.08em;
}
.nudge-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.nudge-row button {
min-height: 39px;
padding: 8px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
}
.nudge-row button span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 7px;
font-weight: 600;
}
.continuity {
display: flex;
gap: 9px;
align-items: flex-start;
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid rgba(143, 208, 255, 0.11);
}
.continuity-dot {
flex: 0 0 auto;
width: 7px;
height: 7px;
margin-top: 3px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 10px var(--cyan);
}
.continuity strong {
display: block;
color: var(--cyan);
font-size: 9px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.continuity p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
#holdStatus {
transition: color 150ms ease, text-shadow 150ms ease;
}
#holdStatus.active {
color: var(--green);
text-shadow: 0 0 16px rgba(120, 242, 166, 0.55);
}
.clock-panel {
position: absolute;
left: 22px;
bottom: 22px;
width: 252px;
padding: 14px;
border-radius: 14px;
}
.clock-layout {
display: grid;
grid-template-columns: 126px 1fr;
gap: 10px;
align-items: center;
margin-top: 8px;
}
#miniClock {
width: 126px;
height: 126px;
overflow: visible;
filter: drop-shadow(0 0 13px rgba(99, 232, 255, 0.08));
}
#miniClock .track {
fill: rgba(7, 21, 34, 0.66);
stroke: rgba(120, 205, 255, 0.18);
stroke-width: 2;
}
#miniClock .minor {
stroke: rgba(142, 201, 235, 0.22);
stroke-width: 1;
}
#miniClock .major {
stroke: rgba(142, 201, 235, 0.52);
stroke-width: 2;
}
.hand-player {
stroke: var(--cyan);
stroke-width: 3;
stroke-linecap: round;
filter: drop-shadow(0 0 5px var(--cyan));
}
.hand-target {
stroke: var(--amber);
stroke-width: 2;
stroke-dasharray: 4 3;
stroke-linecap: round;
filter: drop-shadow(0 0 4px var(--amber));
}
.clock-numbers {
display: grid;
gap: 9px;
}
.clock-number span {
display: block;
color: var(--muted);
font-size: 7px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.clock-number strong {
display: block;
margin-top: 3px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 13px;
}
#playerPhase { color: var(--cyan); }
#targetPhase { color: var(--amber); }
.action-dock {
position: absolute;
left: 50%;
bottom: 25px;
width: min(420px, calc(100vw - 590px));
transform: translateX(-50%);
text-align: center;
}
#captureBtn {
min-width: 230px;
min-height: 58px;
padding: 12px 24px;
border-color: rgba(99, 232, 255, 0.56);
background: linear-gradient(135deg, rgba(24, 92, 116, 0.9), rgba(8, 36, 55, 0.94));
box-shadow: 0 0 30px rgba(35, 200, 238, 0.13), inset 0 0 20px rgba(99, 232, 255, 0.05);
font-size: 13px;
font-weight: 900;
letter-spacing: 0.18em;
}
#captureBtn:hover {
border-color: var(--cyan);
background: linear-gradient(135deg, rgba(32, 119, 145, 0.96), rgba(10, 48, 70, 0.98));
box-shadow: 0 0 38px rgba(35, 200, 238, 0.23);
}
kbd {
display: inline-block;
margin-left: 9px;
padding: 4px 7px;
border: 1px solid rgba(210, 247, 255, 0.27);
border-radius: 5px;
background: rgba(3, 13, 23, 0.45);
color: #c5efff;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 9px;
letter-spacing: 0.06em;
box-shadow: none;
}
.action-help {
margin-top: 8px;
color: #7894aa;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.11em;
text-transform: uppercase;
text-shadow: 0 2px 8px #000;
}
.camera-help {
position: absolute;
right: 23px;
bottom: 22px;
color: #69869c;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.1em;
text-align: right;
line-height: 1.7;
text-transform: uppercase;
}
#toast {
position: absolute;
top: 104px;
left: 50%;
max-width: min(520px, calc(100vw - 40px));
padding: 10px 16px;
transform: translate(-50%, -12px);
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel-strong);
color: var(--text);
opacity: 0;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-align: center;
text-transform: uppercase;
transition: opacity 160ms ease, transform 160ms ease;
backdrop-filter: blur(14px);
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.good {
border-color: rgba(120, 242, 166, 0.45);
color: var(--green);
}
#toast.bad {
border-color: rgba(255, 111, 126, 0.45);
color: var(--red);
}
#toast.hold {
border-color: rgba(99, 232, 255, 0.45);
color: var(--cyan);
}
#overlay {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
padding: 22px;
background: rgba(2, 8, 15, 0.54);
backdrop-filter: blur(7px);
transition: opacity 220ms ease, visibility 220ms ease;
}
#overlay.hidden {
opacity: 0;
visibility: hidden;
pointer-events: none;
}
.modal {
width: min(560px, 100%);
padding: 30px;
border: 1px solid rgba(127, 215, 255, 0.24);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(18, 40, 59, 0.96), rgba(6, 15, 27, 0.97)),
var(--panel-strong);
box-shadow: 0 35px 100px rgba(0, 0, 0, 0.55), inset 0 1px rgba(255, 255, 255, 0.035);
}
.modal-top {
display: flex;
justify-content: space-between;
align-items: center;
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.modal-code {
padding: 5px 8px;
border: 1px solid rgba(99, 232, 255, 0.23);
border-radius: 6px;
background: rgba(99, 232, 255, 0.08);
font-family: "SFMono-Regular", Consolas, monospace;
}
.modal h2 {
margin: 20px 0 12px;
font-size: clamp(32px, 7vw, 52px);
line-height: 0.95;
letter-spacing: -0.05em;
}
.modal h2 span { color: var(--cyan); }
.modal p {
margin: 0;
color: #9bb3c6;
font-size: 13px;
line-height: 1.7;
}
.initial-state {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
margin: 22px 0;
}
.state-cell {
padding: 10px;
border: 1px solid rgba(143, 208, 255, 0.12);
border-radius: 8px;
background: rgba(3, 12, 21, 0.28);
}
.state-cell span {
display: block;
color: #67859a;
font-size: 7px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.state-cell strong {
display: block;
margin-top: 5px;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 12px;
}
.modal-actions {
display: flex;
gap: 10px;
align-items: center;
margin-top: 22px;
}
#startBtn {
flex: 1;
min-height: 52px;
border-color: rgba(99, 232, 255, 0.56);
background: linear-gradient(135deg, rgba(27, 102, 127, 0.95), rgba(9, 44, 64, 0.96));
font-size: 11px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.modal-note {
margin-top: 13px;
color: #668399;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.11em;
text-align: center;
text-transform: uppercase;
}
@media (max-width: 900px) {
.brand { min-width: 180px; }
.brand h1 { font-size: 16px; }
.stat { min-width: 67px; padding-inline: 8px; }
.control-panel { width: 245px; }
.action-dock { width: min(360px, calc(100vw - 500px)); }
.camera-help { display: none; }
}
@media (max-width: 700px) {
.topbar {
top: 10px;
left: 10px;
right: 10px;
flex-wrap: wrap;
}
.brand { min-width: 0; flex: 1; }
.brand p { display: none; }
.stats { order: 3; width: 100%; justify-content: space-between; }
.stat { flex: 1; }
.top-actions button { min-height: 37px; padding-inline: 10px; }
.control-panel {
top: 128px;
right: 10px;
width: 210px;
padding: 12px;
}
#periodValue { font-size: 27px; }
.rate-badge, .continuity p { display: none; }
.clock-panel {
left: 10px;
bottom: 82px;
width: 190px;
padding: 10px;
}
.clock-layout { grid-template-columns: 88px 1fr; }
#miniClock { width: 88px; height: 88px; }
.clock-number strong { font-size: 10px; }
.action-dock {
left: 10px;
right: 10px;
bottom: 12px;
width: auto;
transform: none;
}
#captureBtn { min-height: 50px; }
.initial-state { grid-template-columns: repeat(2, 1fr); }
#toast { top: 115px; }
}
@media (max-width: 440px) {
.clock-panel { display: none; }
.control-panel { width: 190px; }
.stats .stat:nth-child(3) { display: none; }
.modal { padding: 22px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="vignette"></div>
<main class="hud">
<div class="topbar">
<div class="brand">
<h1>PHASE / HOLD</h1>
<p>Continuous period · preserved phase</p>
</div>
<div class="stats panel">
<div class="stat">
<span class="stat-label">Lock</span>
<span class="stat-value" id="lockStat">1 / 6</span>
</div>
<div class="stat">
<span class="stat-label">Score</span>
<span class="stat-value" id="scoreStat">00000</span>
</div>
<div class="stat" id="timeStat">
<span class="stat-label">Time</span>
<span class="stat-value" id="timeValue">01:15</span>
</div>
<div class="stat">
<span class="stat-label">Target drift</span>
<span class="stat-value" id="targetStat">8.00s</span>
</div>
</div>
<div class="top-actions interactive">
<button id="soundBtn" aria-label="Toggle sound">SOUND ON</button>
<button id="resetBtn">RESET STATE</button>
</div>
</div>
<section class="control-panel panel">
<div class="panel-kicker">Player clock / period</div>
<div class="period-readout">
<div>
<span id="periodValue">2.00</span><span class="unit">sec / rev</span>
</div>
<div class="rate-badge" id="rateBadge">30.0 RPM</div>
</div>
<input
id="periodSlider"
type="range"
min="0.55"
max="4"
step="0.01"
value="2"
aria-label="Rotation period in seconds per revolution"
/>
<div class="range-labels">
<span>0.55s / FAST</span>
<span>4.00s / SLOW</span>
</div>
<div class="nudge-row">
<button id="fasterBtn">FASTER <span>− PERIOD</span></button>
<button id="slowerBtn">SLOWER <span>+ PERIOD</span></button>
</div>
<div class="continuity">
<span class="continuity-dot"></span>
<div>
<strong id="holdStatus">Phase continuity active</strong>
<p>Changing the period changes speed only. The current phase is never reset or jumped.</p>
</div>
</div>
</section>
<section class="clock-panel panel">
<div class="panel-kicker">Visible phase clock</div>
<div class="clock-layout">
<svg id="miniClock" viewBox="0 0 160 160" aria-label="Phase clock">
<circle class="track" cx="80" cy="80" r="61"></circle>
<g id="dialTicks"></g>
<g id="targetHand">
<line class="hand-target" x1="80" y1="80" x2="80" y2="26"></line>
<circle cx="80" cy="26" r="5" fill="#ffb45c"></circle>
</g>
<g id="playerHand">
<line class="hand-player" x1="80" y1="80" x2="80" y2="21"></line>
<rect x="75" y="16" width="10" height="10" rx="2" fill="#63e8ff"></rect>
</g>
<circle cx="80" cy="80" r="8" fill="#0b1b2b" stroke="#63e8ff" stroke-width="2"></circle>
<circle cx="80" cy="80" r="2.5" fill="#e9faff"></circle>
</svg>
<div class="clock-numbers">
<div class="clock-number">
<span>Player phase</span>
<strong id="playerPhase">0.100</strong>
</div>
<div class="clock-number">
<span>Target phase</span>
<strong id="targetPhase">0.620</strong>
</div>
<div class="clock-number">
<span>Phase error</span>
<strong id="phaseError">−0.520</strong>
</div>
</div>
</div>
</section>
<div class="action-dock interactive">
<button id="captureBtn">CAPTURE PHASE <kbd>SPACE</kbd></button>
<div class="action-help">Match the cyan runner with the amber target</div>
</div>
<div class="camera-help">
Drag to orbit<br />
Scroll to zoom<br />
↑ / ↓ tune period
</div>
<div id="toast" role="status" aria-live="polite"></div>
</main>
<div id="overlay">
<section class="modal">
<div class="modal-top">
<span>Timing protocol / 01</span>
<span class="modal-code">PH-06</span>
</div>
<h2 id="overlayTitle">PHASE <span>/ HOLD</span></h2>
<p id="overlayText">
Tune your cyan phase runner until it meets the amber drift target, then capture the lock.
Every period change is continuous: speed changes, but phase stays exactly where it was.
</p>
<div class="initial-state" id="initialState">
<div class="state-cell">
<span>Period</span>
<strong>2.00s</strong>
</div>
<div class="state-cell">
<span>Phase</span>
<strong>0.100</strong>
</div>
<div class="state-cell">
<span>Target drift</span>
<strong>8.00s</strong>
</div>
<div class="state-cell">
<span>Locks</span>
<strong>0 / 6</strong>
</div>
</div>
<div class="modal-actions">
<button id="startBtn">Initialize run</button>
</div>
<div class="modal-note">Space to capture · R to reset · Arrow keys tune period</div>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM / state
// -------------------------------------------------------------------------
const $ = (id) => document.getElementById(id);
const TAU = Math.PI * 2;
const wrap = (v) => ((v % 1) + 1) % 1;
const shortestDelta = (a, b) => {
let d = wrap(a - b);
if (d > 0.5) d -= 1;
return d;
};
const TARGET_PERIODS = [8.0, 5.2, 3.4, 2.6, 2.0, 1.45];
const CAPTURE_WINDOW = 0.042;
const INITIAL = {
period: 2.0,
phase: 0.1,
targetPhase: 0.62,
targetPeriod: 8.0,
stage: 0,
score: 0,
combo: 0,
attempts: 0,
time: 75
};
let period = INITIAL.period;
let phase = INITIAL.phase;
let targetPhase = INITIAL.targetPhase;
let targetPeriod = INITIAL.targetPeriod;
let stage = INITIAL.stage;
let score = INITIAL.score;
let combo = INITIAL.combo;
let attempts = INITIAL.attempts;
let remaining = INITIAL.time;
let mode = "ready";
let holdUntil = 0;
let toastUntil = 0;
let heldDirection = 0;
let lastFrame = performance.now();
let soundEnabled = true;
let audioContext = null;
const keys = { faster: false, slower: false };
const dialTicks = $("dialTicks");
for (let i = 0; i < 24; i++) {
const angle = (i / 24) * TAU;
const inner = i % 6 === 0 ? 51 : 55;
const x1 = 80 + Math.sin(angle) * inner;
const y1 = 80 - Math.cos(angle) * inner;
const x2 = 80 + Math.sin(angle) * 59;
const y2 = 80 - Math.cos(angle) * 59;
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
line.setAttribute("x1", x1);
line.setAttribute("y1", y1);
line.setAttribute("x2", x2);
line.setAttribute("y2", y2);
line.setAttribute("class", i % 6 === 0 ? "major" : "minor");
dialTicks.appendChild(line);
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.FogExp2(0x07101d, 0.024);
const camera = new THREE.PerspectiveCamera(43, innerWidth / innerHeight, 0.1, 120);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
$("scene").appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0x9ad9ff, 0x101727, 1.7);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xc7eaff, 2.5);
keyLight.position.set(6, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1536, 1536);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -15;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffa95f, 1.15);
rimLight.position.set(-8, 7, -10);
scene.add(rimLight);
const centerLight = new THREE.PointLight(0x50dfff, 5, 11, 2);
centerLight.position.set(0, 3, 0);
scene.add(centerLight);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const materials = {
floor: new THREE.MeshStandardMaterial({ color: 0x0a1725, roughness: 0.96, metalness: 0.02 }),
platform: new THREE.MeshStandardMaterial({ color: 0x101f30, roughness: 0.8, metalness: 0.18 }),
platformEdge: new THREE.MeshStandardMaterial({ color: 0x1b354d, roughness: 0.65, metalness: 0.3 }),
dark: new THREE.MeshStandardMaterial({ color: 0x13263a, roughness: 0.7, metalness: 0.25 }),
steel: new THREE.MeshStandardMaterial({ color: 0x34526b, roughness: 0.5, metalness: 0.48 }),
cyan: new THREE.MeshStandardMaterial({
color: 0x63e8ff,
emissive: 0x20bfe5,
emissiveIntensity: 2.1,
roughness: 0.3,
metalness: 0.22
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffb45c,
emissive: 0xf27a1d,
emissiveIntensity: 2.1,
roughness: 0.34,
metalness: 0.2
}),
green: new THREE.MeshStandardMaterial({
color: 0x78f2a6,
emissive: 0x35d875,
emissiveIntensity: 2.4,
roughness: 0.3
}),
window: new THREE.MeshStandardMaterial({
color: 0x6dd9ff,
emissive: 0x278eb8,
emissiveIntensity: 1.5,
roughness: 0.4
})
};
function addBox(parent, sx, sy, sz, material, x = 0, y = 0, z = 0, castShadow = true) {
const mesh = new THREE.Mesh(cubeGeometry, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Ground platform.
addBox(scene, 29, 0.4, 29, materials.floor, 0, -0.34, 0, false);
// Instanced voxel floor tiles.
const tileCount = 22 * 22;
const tileGeometry = new THREE.BoxGeometry(0.57, 0.075, 0.57);
const tileMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 0.88, metalness: 0.08 });
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
tiles.receiveShadow = true;
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < 22; ix++) {
for (let iz = 0; iz < 22; iz++) {
const x = (ix - 10.5) * 0.66;
const z = (iz - 10.5) * 0.66;
dummy.position.set(x, -0.09 + ((ix + iz) % 3) * 0.008, z);
dummy.rotation.y = ((ix * 7 + iz * 11) % 4) * 0.012;
dummy.updateMatrix();
tiles.setMatrixAt(tileIndex, dummy.matrix);
const shade = 0.032 + ((ix * 13 + iz * 17) % 11) * 0.0025;
tempColor.setRGB(shade * 0.55, shade * 0.95, shade * 1.7);
tiles.setColorAt(tileIndex, tempColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Central layered voxel platform.
addBox(scene, 8.4, 0.28, 8.4, materials.platform, 0, -0.02, 0, false);
addBox(scene, 7.7, 0.18, 7.7, materials.platformEdge, 0, 0.16, 0, false);
addBox(scene, 6.9, 0.12, 6.9, materials.dark, 0, 0.3, 0, false);
// Phase rings made from individual voxels.
const ringSegments = [];
const outerRingMat = new THREE.MeshStandardMaterial({
color: 0x25465f,
emissive: 0x0b293c,
emissiveIntensity: 0.8,
roughness: 0.45,
metalness: 0.4
});
for (let i = 0; i < 72; i++) {
const angle = (i / 72) * TAU;
const mat = outerRingMat.clone();
const mesh = new THREE.Mesh(cubeGeometry, mat);
mesh.scale.set(0.34, 0.22, 0.55);
mesh.position.set(Math.cos(angle) * 4.62, 0.28, Math.sin(angle) * 4.62);
mesh.rotation.y = Math.PI / 2 - angle;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
ringSegments.push(mesh);
}
for (let i = 0; i < 48; i++) {
const angle = (i / 48) * TAU;
const mat = (i % 4 === 0 ? materials.cyan : materials.steel).clone();
if (i % 4 !== 0) {
mat.color.setHex(0x29445a);
mat.emissive.setHex(0x081a27);
mat.emissiveIntensity = 0.6;
}
const mesh = new THREE.Mesh(cubeGeometry, mat);
mesh.scale.set(0.22, 0.16, 0.39);
mesh.position.set(Math.cos(angle) * 3.62, 0.39, Math.sin(angle) * 3.62);
mesh.rotation.y = Math.PI / 2 - angle;
mesh.castShadow = true;
scene.add(mesh);
}
// Central phase spindle.
const spindle = new THREE.Group();
scene.add(spindle);
addBox(spindle, 1.7, 0.38, 1.7, materials.steel, 0, 0.56, 0);
addBox(spindle, 1.25, 0.9, 1.25, materials.dark, 0, 1.17, 0);
addBox(spindle, 0.9, 0.82, 0.9, materials.platformEdge, 0, 2.02, 0);
const core = addBox(spindle, 0.56, 0.56, 0.56, materials.cyan, 0, 2.82, 0);
addBox(spindle, 0.24, 0.42, 0.24, materials.steel, 0, 3.28, 0);
addBox(spindle, 0.44, 0.16, 0.44, materials.cyan, 0, 3.55, 0);
// Player phase hand and target phase hand.
const playerHand = new THREE.Group();
const playerHandMesh = new THREE.Mesh(
new THREE.BoxGeometry(4.18, 0.035, 0.075),
new THREE.MeshBasicMaterial({ color: 0x63e8ff, transparent: true, opacity: 0.64 })
);
playerHandMesh.position.x = 2.09;
playerHand.add(playerHandMesh);
playerHand.position.y = 0.56;
scene.add(playerHand);
const targetHand = new THREE.Group();
const targetHandMesh = new THREE.Mesh(
new THREE.BoxGeometry(3.5, 0.03, 0.055),
new THREE.MeshBasicMaterial({ color: 0xffb45c, transparent: true, opacity: 0.48 })
);
targetHandMesh.position.x = 1.75;
targetHand.add(targetHandMesh);
targetHand.position.y = 0.64;
scene.add(targetHand);
// Player runner.
const runner = new THREE.Group();
scene.add(runner);
addBox(runner, 0.66, 0.66, 0.66, materials.cyan, 0, 0.7, 0);
addBox(runner, 0.24, 0.24, 0.24, materials.cyan, 0.42, 0.7, 0.2);
addBox(runner, 0.24, 0.24, 0.24, materials.cyan, -0.42, 0.7, 0.2);
addBox(runner, 0.24, 0.24, 0.24, materials.cyan, 0, 0.7, -0.43);
addBox(runner, 0.34, 0.12, 0.34, materials.cyan, 0, 1.08, 0);
const runnerLight = new THREE.PointLight(0x4ee7ff, 2.8, 4, 2);
runnerLight.position.y = 0.8;
runner.add(runnerLight);
// Amber target.
const target = new THREE.Group();
scene.add(target);
addBox(target, 0.66, 0.66, 0.66, materials.amber, 0, 0.7, 0);
addBox(target, 0.22, 0.22, 0.22, materials.amber, 0.43, 0.7, 0.42);
addBox(target, 0.22, 0.22, 0.22, materials.amber, -0.43, 0.7, 0.42);
addBox(target, 0.22, 0.22, 0.22, materials.amber, 0, 0.7, -0.43);
addBox(target, 0.3, 0.12, 0.3, materials.amber, 0, 1.08, 0);
const targetLight = new THREE.PointLight(0xff9c3d, 2.8, 4, 2);
targetLight.position.y = 0.8;
target.add(targetLight);
// Capture tolerance arc.
const arcBlocks = [];
const arcMat = new THREE.MeshStandardMaterial({
color: 0xffa34d,
emissive: 0x7b2d00,
emissiveIntensity: 1.4,
transparent: true,
opacity: 0.82,
roughness: 0.4
});
for (let i = 0; i < 11; i++) {
const block = new THREE.Mesh(cubeGeometry, arcMat.clone());
block.scale.set(0.24, 0.18, 0.4);
scene.add(block);
arcBlocks.push(block);
}
// Deterministic voxel skyline.
const towerPalette = [0x142b40, 0x193247, 0x102536, 0x21394c];
function addTower(x, z, height, width, depth, index) {
const group = new THREE.Group();
group.position.set(x, 0, z);
const bodyMat = new THREE.MeshStandardMaterial({
color: towerPalette[index % towerPalette.length],
roughness: 0.82,
metalness: 0.14
});
addBox(group, width, height, depth, bodyMat, 0, height / 2 - 0.04, 0);
addBox(group, width * 0.68, 0.38, depth * 0.68, materials.steel, 0, height + 0.16, 0);
addBox(group, width * 0.22, 0.52, depth * 0.22, materials.dark, 0, height + 0.58, 0);
const rows = Math.max(1, Math.floor(height / 1.35));
for (let row = 0; row < rows; row++) {
const y = 0.65 + row * 1.2;
if (y > height - 0.25) continue;
const columns = Math.max(1, Math.floor(width / 0.75));
for (let col = 0; col < columns; col++) {
if ((row + col + index) % 3 === 0) continue;
const wx = -width / 2 + 0.42 + col * 0.72;
addBox(group, 0.18, 0.25, 0.035, materials.window, wx, y, depth / 2 + 0.02, false);
}
}
scene.add(group);
}
const towerData = [
[-10, -6, 4.4, 2.1, 2.0], [-7, -10, 6.2, 2.5, 2.4], [-3, -12, 3.8, 2.0, 1.8],
[3, -12, 5.4, 2.2, 2.3], [8, -9, 3.4, 2.4, 2.0], [11, -4, 5.8, 2.6, 2.6],
[-12, 1, 3.8, 2.4, 2.0], [12, 2, 4.5, 2.8, 2.2], [-10, 7, 5.3, 2.2, 2.3],
[-5, 11, 3.2, 2.0, 2.1], [4, 11, 4.2, 2.4, 2.2], [10, 8, 6.1, 2.5, 2.6]
];
towerData.forEach((data, i) => addTower(...data, i));
// Small floating voxel motes.
const moteCount = 36;
const moteGeometry = new THREE.BoxGeometry(0.08, 0.08, 0.08);
const moteMaterial = new THREE.MeshBasicMaterial({ color: 0x7cdcff, transparent: true, opacity: 0.5 });
const motes = new THREE.InstancedMesh(moteGeometry, moteMaterial, moteCount);
const moteData = [];
for (let i = 0; i < moteCount; i++) {
const a = (i * 2.39996) % TAU;
const radius = 6.2 + (i % 9) * 0.72;
moteData.push({
a,
radius,
y: 0.8 + ((i * 0.73) % 4.6),
speed: 0.025 + (i % 5) * 0.007,
phase: (i * 0.61) % TAU
});
}
scene.add(motes);
// -------------------------------------------------------------------------
// Custom camera controls (no addons)
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.72,
pitch: 0.58,
distance: 15.8,
target: new THREE.Vector3(0, 1.05, 0)
};
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x + cameraState.distance * cp * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.distance * Math.sin(cameraState.pitch),
cameraState.target.z + cameraState.distance * cp * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let dragMoved = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMoved = false;
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;
if (Math.abs(dx) + Math.abs(dy) > 2) dragMoved = true;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(cameraState.pitch + dy * 0.0045, 0.3, 1.22);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function stopDragging(event) {
dragging = false;
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.012,
10.5,
24
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraState.yaw = 0.72;
cameraState.pitch = 0.58;
cameraState.distance = 15.8;
});
// -------------------------------------------------------------------------
// Game mechanics
// -------------------------------------------------------------------------
function setPeriod(value, announce = true) {
const next = THREE.MathUtils.clamp(value, 0.55, 4);
if (Math.abs(next - period) < 0.0001) return;
// The phase variable is deliberately untouched here.
period = next;
holdUntil = performance.now() + 1050;
updatePeriodUI();
if (announce && mode === "running") {
showToast(`Period ${period.toFixed(2)}s · phase held`, "hold", 900);
}
}
function updatePeriodUI() {
$("periodSlider").value = period.toFixed(2);
$("periodValue").textContent = period.toFixed(2);
$("rateBadge").textContent = `${(60 / period).toFixed(1)} RPM`;
}
function showToast(message, tone = "", duration = 1300) {
const toast = $("toast");
toast.textContent = message;
toast.className = `show ${tone}`;
toastUntil = performance.now() + duration;
}
function formatTime(seconds) {
const value = Math.max(0, Math.ceil(seconds));
const minutes = Math.floor(value / 60).toString().padStart(2, "0");
const remainder = (value % 60).toString().padStart(2, "0");
return `${minutes}:${remainder}`;
}
function updateStats() {
$("lockStat").textContent = `${Math.min(stage + 1, 6)} / 6`;
$("scoreStat").textContent = Math.max(0, score).toString().padStart(5, "0");
$("timeValue").textContent = formatTime(remaining);
$("timeStat").classList.toggle("warning", remaining <= 15);
$("targetStat").textContent = `${targetPeriod.toFixed(2)}s`;
}
function playTone(frequency, duration = 0.09, volume = 0.035, type = "sine") {
if (!soundEnabled) return;
try {
if (!audioContext) audioContext = new (window.AudioContext || window.webkitAudioContext)();
if (audioContext.state === "suspended") audioContext.resume();
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = type;
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
oscillator.frequency.exponentialRampToValueAtTime(Math.max(40, frequency * 0.72), audioContext.currentTime + duration);
gain.gain.setValueAtTime(volume, audioContext.currentTime);
gain.gain.exponentialRampToValueAtTime(0.0001, audioContext.currentTime + duration);
oscillator.connect(gain).connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + duration);
} catch (_) {}
}
function showOverlay(kind) {
const overlay = $("overlay");
overlay.classList.remove("hidden");
if (kind === "ready") {
$("overlayTitle").innerHTML = "PHASE <span>/ HOLD</span>";
$("overlayText").textContent =
"Tune your cyan phase runner until it meets the amber drift target, then capture the lock. Every period change is continuous: speed changes, but phase stays exactly where it was.";
$("initialState").style.display = "grid";
$("startBtn").textContent = "Initialize run";
} else if (kind === "won") {
$("overlayTitle").innerHTML = "ALL LOCKS <span>SYNCED</span>";
$("overlayText").textContent =
`You completed all six phase locks with ${score.toLocaleString()} points. The clock remained continuous through every period edit.`;
$("initialState").style.display = "none";
$("startBtn").textContent = "Run again";
} else {
$("overlayTitle").innerHTML = "TIME <span>EXPIRED</span>";
$("overlayText").textContent =
`You secured ${stage} of 6 locks and scored ${score.toLocaleString()} points. Reset to the known starting state and tune the pursuit more precisely.`;
$("initialState").style.display = "none";
$("startBtn").textContent = "Reset and retry";
}
}
function resetGame() {
period = INITIAL.period;
phase = INITIAL.phase;
targetPhase = INITIAL.targetPhase;
targetPeriod = INITIAL.targetPeriod;
stage = INITIAL.stage;
score = INITIAL.score;
combo = INITIAL.combo;
attempts = INITIAL.attempts;
remaining = INITIAL.time;
mode = "ready";
heldDirection = 0;
keys.faster = false;
keys.slower = false;
holdUntil = 0;
cameraState.yaw = 0.72;
cameraState.pitch = 0.58;
cameraState.distance = 15.8;
updatePeriodUI();
updateStats();
showOverlay("ready");
}
function beginRun() {
if (mode === "won" || mode === "lost") resetGame();
mode = "running";
$("overlay").classList.add("hidden");
showToast("Run initialized · phase 0.100", "hold", 1100);
playTone(420, 0.1, 0.028, "triangle");
}
function capture() {
if (mode !== "running") return;
attempts++;
const error = shortestDelta(phase, targetPhase);
const absoluteError = Math.abs(error);
if (absoluteError <= CAPTURE_WINDOW) {
const precision = 1 - absoluteError / CAPTURE_WINDOW;
const base = Math.round(500 + precision * 500);
const comboBonus = combo * 75;
score += base + comboBonus;
combo++;
playTone(620 + precision * 260, 0.13, 0.045, "triangle");
if (stage >= TARGET_PERIODS.length - 1) {
stage = TARGET_PERIODS.length;
mode = "won";
updateStats();
showOverlay("won");
playTone(940, 0.24, 0.04, "sine");
return;
}
stage++;
targetPeriod = TARGET_PERIODS[stage];
// Target phase remains continuous as its drift rate changes.
showToast(`Lock ${stage} / 6 · new drift ${targetPeriod.toFixed(2)}s`, "good", 1450);
} else {
combo = 0;
score = Math.max(0, score - 80);
playTone(125, 0.12, 0.03, "sawtooth");
showToast(`Out of phase · error ${error >= 0 ? "+" : ""}${error.toFixed(3)}`, "bad", 1050);
}
updateStats();
}
// -------------------------------------------------------------------------
// Input
// -------------------------------------------------------------------------
$("periodSlider").addEventListener("input", (event) => {
setPeriod(parseFloat(event.target.value));
});
$("fasterBtn").addEventListener("pointerdown", (event) => {
event.preventDefault();
heldDirection = -1;
});
$("slowerBtn").addEventListener("pointerdown", (event) => {
event.preventDefault();
heldDirection = 1;
});
window.addEventListener("pointerup", () => { heldDirection = 0; });
window.addEventListener("pointercancel", () => { heldDirection = 0; });
$("fasterBtn").addEventListener("pointerleave", () => {
if (heldDirection === -1) heldDirection = 0;
});
$("slowerBtn").addEventListener("pointerleave", () => {
if (heldDirection === 1) heldDirection = 0;
});
$("captureBtn").addEventListener("click", capture);
$("startBtn").addEventListener("click", beginRun);
$("resetBtn").addEventListener("click", resetGame);
$("soundBtn").addEventListener("click", () => {
soundEnabled = !soundEnabled;
$("soundBtn").textContent = soundEnabled ? "SOUND ON" : "SOUND OFF";
if (soundEnabled) playTone(500, 0.08, 0.025);
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
if (mode === "running") {
if (!event.repeat) capture();
} else if (mode === "ready") {
beginRun();
}
}
if (event.code === "ArrowUp" || event.code === "ArrowRight") {
event.preventDefault();
keys.faster = true;
}
if (event.code === "ArrowDown" || event.code === "ArrowLeft") {
event.preventDefault();
keys.slower = true;
}
if (event.code === "KeyR" && !event.repeat) {
resetGame();
}
});
window.addEventListener("keyup", (event) => {
if (event.code === "ArrowUp" || event.code === "ArrowRight") keys.faster = false;
if (event.code === "ArrowDown" || event.code === "ArrowLeft") keys.slower = false;
});
window.addEventListener("blur", () => {
keys.faster = false;
keys.slower = false;
heldDirection = 0;
});
// -------------------------------------------------------------------------
// Rendering and animation
// -------------------------------------------------------------------------
const phaseColor = new THREE.Color();
const runnerAngle = (p) => p * TAU;
const targetAngle = (p) => p * TAU;
function updateVisuals(now) {
const pAngle = runnerAngle(phase);
const tAngle = targetAngle(targetPhase);
const error = shortestDelta(phase, targetPhase);
const near = Math.abs(error) <= CAPTURE_WINDOW;
runner.position.set(
Math.cos(pAngle) * 4.62,
0.53 + Math.sin(now * 0.0032) * 0.07,
Math.sin(pAngle) * 4.62
);
runner.rotation.y = -pAngle;
target.position.set(
Math.cos(tAngle) * 4.62,
0.53 + Math.sin(now * 0.0027 + 1.8) * 0.06,
Math.sin(tAngle) * 4.62
);
target.rotation.y = -tAngle;
playerHand.rotation.y = -pAngle;
targetHand.rotation.y = -tAngle;
const pulse = 1 + Math.sin(now * 0.004) * 0.06;
core.scale.set(0.56 * pulse, 0.56 * pulse, 0.56 * pulse);
core.rotation.y = now * 0.00035;
for (let i = 0; i < arcBlocks.length; i++) {
const offset = (i / (arcBlocks.length - 1) - 0.5) * CAPTURE_WINDOW * 2;
const a = targetAngle(wrap(targetPhase + offset));
const block = arcBlocks[i];
block.position.set(Math.cos(a) * 4.62, 0.51, Math.sin(a) * 4.62);
block.rotation.y = Math.PI / 2 - a;
block.material.color.setHex(near ? 0x78f2a6 : 0xffa34d);
block.material.emissive.setHex(near ? 0x25bd59 : 0x7b2d00);
block.material.emissiveIntensity = near ? 2.6 : 1.4;
}
// Light the ring near either clock marker.
for (let i = 0; i < ringSegments.length; i++) {
const segmentPhase = i / 72;
const distanceToRunner = Math.abs(shortestDelta(segmentPhase, phase));
const distanceToTarget = Math.abs(shortestDelta(segmentPhase, targetPhase));
const runnerGlow = Math.max(0, 1 - distanceToRunner * 24);
const targetGlow = Math.max(0, 1 - distanceToTarget * 18);
const material = ringSegments[i].material;
phaseColor.setRGB(0.055, 0.14, 0.21);
phaseColor.lerp(new THREE.Color(0x37dfff), runnerGlow * 0.9);
phaseColor.lerp(new THREE.Color(0xffa548), targetGlow * 0.55);
material.color.copy(phaseColor);
material.emissive.setRGB(0.015, 0.07, 0.11);
material.emissive.lerp(new THREE.Color(0x0aa9d4), runnerGlow * 0.7);
}
// Motes.
for (let i = 0; i < moteCount; i++) {
const data = moteData[i];
const a = data.a + now * 0.001 * data.speed;
dummy.position.set(
Math.cos(a) * data.radius,
data.y + Math.sin(now * 0.001 + data.phase) * 0.42,
Math.sin(a) * data.radius
);
dummy.rotation.set(a, a * 0.7, now * 0.0004 + i);
dummy.updateMatrix();
motes.setMatrixAt(i, dummy.matrix);
}
motes.instanceMatrix.needsUpdate = true;
// HUD phase clock.
$("playerHand").setAttribute("transform", `rotate(${phase * 360} 80 80)`);
$("targetHand").setAttribute("transform", `rotate(${targetPhase * 360} 80 80)`);
$("playerPhase").textContent = phase.toFixed(3);
$("targetPhase").textContent = targetPhase.toFixed(3);
$("phaseError").textContent = `${error >= 0 ? "+" : "−"}${Math.abs(error).toFixed(3)}`;
const holding = now < holdUntil;
$("holdStatus").textContent = holding ? "Phase held at edit" : "Phase continuity active";
$("holdStatus").classList.toggle("active", holding);
if (now > toastUntil) $("toast").classList.remove("show");
}
function frame(now) {
const dt = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (mode === "running") {
let direction = 0;
if (keys.faster || heldDirection === -1) direction -= 1;
if (keys.slower || heldDirection === 1) direction += 1;
if (direction !== 0) {
setPeriod(period + direction * dt * 0.72, false);
}
// Only elapsed time advances phase. A period edit itself never edits phase.
phase = wrap(phase + dt / period);
targetPhase = wrap(targetPhase + dt / targetPeriod);
remaining -= dt;
if (remaining <= 0) {
remaining = 0;
mode = "lost";
updateStats();
showOverlay("lost");
}
}
updateStats();
updateVisuals(now);
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
});
updatePeriodUI();
updateStats();
updateCamera();
requestAnimationFrame(frame);
</script>
</body>
</html>
```