Prompt specification
Build or customize the Three.js 3D scene "Mechanical Music Box Phrase Sequencer" 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: Mechanical Music Box Phrase Sequencer
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/mechanical-music-box-phrase-sequencer-x3314
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows musical phrases and repeated forms. Animate a cylinder whose pegs trigger visual chimes in repeating phrases. Show the active phrase and permit a silent mode with identical timing. Voxel style.
Working Three.js Starter (mechanical-music-box-phrase-sequencer-x3314):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#101827">
<title>Phrase Box — Voxel Musical Cylinder</title>
<style>
:root {
--accent: #5ad2c7;
--panel: rgba(18, 28, 46, 0.88);
--border: rgba(255, 255, 255, 0.13);
--muted: #8796ad;
--text: #f4f1e8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101827;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
user-select: none;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
box-shadow: inset 0 0 150px rgba(0, 0, 0, 0.45);
background:
linear-gradient(180deg, rgba(8, 13, 24, 0.26), transparent 24%),
linear-gradient(0deg, rgba(8, 13, 24, 0.35), transparent 34%);
z-index: 2;
}
.topbar {
position: fixed;
z-index: 5;
top: 0;
left: 0;
right: 0;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 24px;
padding: 24px 28px;
pointer-events: none;
}
.brand {
max-width: 440px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: color 250ms ease;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
background: currentColor;
box-shadow: 3px 3px 0 rgba(255, 255, 255, 0.15);
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 48px);
line-height: 0.98;
letter-spacing: -0.055em;
font-weight: 850;
text-transform: uppercase;
}
.brand p {
margin: 10px 0 0;
color: #aab5c7;
font-size: 12px;
line-height: 1.5;
letter-spacing: 0.03em;
}
.top-controls {
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.status-pill,
.sound-button {
min-height: 42px;
border: 1px solid var(--border);
background: rgba(19, 29, 47, 0.78);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.16);
backdrop-filter: blur(12px);
}
.status-pill {
display: flex;
align-items: center;
gap: 10px;
padding: 0 14px;
color: #c8d1df;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
background: var(--accent);
box-shadow: 0 0 12px var(--accent);
transition: background 250ms ease, box-shadow 250ms ease;
}
.sound-button {
display: flex;
align-items: center;
gap: 10px;
padding: 0 15px;
color: var(--text);
font: inherit;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
transition: border-color 180ms ease, background 180ms ease, transform 180ms ease;
}
.sound-button:hover {
border-color: rgba(255, 255, 255, 0.32);
transform: translateY(-1px);
}
.sound-button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 3px;
}
.sound-button.is-on {
border-color: rgba(90, 210, 199, 0.55);
background: rgba(46, 83, 79, 0.48);
}
.sound-icon {
display: grid;
grid-template-columns: repeat(3, 4px);
align-items: end;
gap: 2px;
width: 16px;
height: 16px;
}
.sound-icon i {
display: block;
width: 4px;
background: currentColor;
}
.sound-icon i:nth-child(1) {
height: 6px;
}
.sound-icon i:nth-child(2) {
height: 13px;
}
.sound-icon i:nth-child(3) {
height: 9px;
}
.sound-button:not(.is-on) .sound-icon {
opacity: 0.45;
}
.phrase-panel {
position: fixed;
z-index: 5;
left: 50%;
bottom: 22px;
width: min(900px, calc(100% - 36px));
padding: 18px;
transform: translateX(-50%);
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(18px);
}
.panel-grid {
display: grid;
grid-template-columns: 220px 1fr;
gap: 18px;
}
.active-block {
padding-right: 18px;
border-right: 1px solid var(--border);
}
.panel-label {
display: block;
margin-bottom: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.active-heading {
display: flex;
align-items: baseline;
gap: 10px;
}
#activeIndex {
color: var(--accent);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.08em;
transition: color 250ms ease;
}
#activeName {
font-size: 25px;
line-height: 1;
letter-spacing: -0.04em;
text-transform: uppercase;
}
#activeMotif {
min-height: 32px;
margin-top: 9px;
color: #aab6c8;
font-size: 11px;
line-height: 1.5;
letter-spacing: 0.04em;
}
.step-readout {
display: flex;
justify-content: space-between;
margin-top: 11px;
color: #6f7d94;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
font-variant-numeric: tabular-nums;
}
.phrase-list {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.phrase-card {
position: relative;
min-width: 0;
padding: 10px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.08);
background: rgba(255, 255, 255, 0.035);
transition: border-color 160ms ease, background 160ms ease, transform 160ms ease;
}
.phrase-card.active {
border-color: color-mix(in srgb, var(--pc) 70%, transparent);
background: color-mix(in srgb, var(--pc) 10%, transparent);
transform: translateY(-2px);
}
.phrase-top {
display: flex;
justify-content: space-between;
color: #738198;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
}
.phrase-card.active .phrase-top {
color: var(--pc);
}
.phrase-card strong {
display: block;
margin-top: 7px;
overflow: hidden;
color: #dce2eb;
font-size: 11px;
letter-spacing: 0.08em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.phrase-card.active strong {
color: #fff;
}
.progress {
height: 3px;
margin-top: 12px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
}
.progress i {
display: block;
width: 0;
height: 100%;
background: var(--pc);
}
.timeline-row {
display: grid;
grid-template-columns: 90px 1fr;
align-items: center;
gap: 10px;
margin-top: 14px;
}
.timeline {
display: grid;
grid-template-columns: repeat(32, 1fr);
gap: 3px;
height: 13px;
}
.timeline-step {
position: relative;
overflow: hidden;
background: color-mix(in srgb, var(--pc) 14%, transparent);
}
.timeline-step::after {
content: "";
position: absolute;
inset: 0;
transform: scaleX(0);
transform-origin: left;
background: var(--pc);
opacity: 0.65;
transition: transform 60ms linear, opacity 100ms ease;
}
.timeline-step.passed::after {
transform: scaleX(1);
}
.timeline-step.current::after {
transform: scaleX(1);
opacity: 1;
}
.timeline-step.current {
box-shadow: 0 0 10px color-mix(in srgb, var(--pc) 50%, transparent);
}
.panel-footer {
display: flex;
justify-content: space-between;
gap: 15px;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: #718097;
font-size: 9px;
line-height: 1.4;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.panel-footer strong {
color: #aab6c7;
font-weight: 750;
}
.camera-hint {
position: fixed;
z-index: 4;
right: 25px;
bottom: 242px;
color: rgba(203, 214, 231, 0.55);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 720px) {
.topbar {
padding: 17px;
}
.brand p,
.status-pill {
display: none;
}
h1 {
font-size: 29px;
}
.phrase-panel {
bottom: 12px;
width: calc(100% - 24px);
padding: 13px;
}
.panel-grid {
grid-template-columns: 1fr;
gap: 12px;
}
.active-block {
padding: 0 0 11px;
border-right: 0;
border-bottom: 1px solid var(--border);
}
#activeName {
font-size: 21px;
}
#activeMotif {
min-height: 0;
}
.step-readout {
margin-top: 8px;
}
.phrase-card {
padding: 8px;
}
.phrase-card strong {
font-size: 9px;
}
.timeline-row {
grid-template-columns: 1fr;
gap: 6px;
}
.timeline {
gap: 2px;
}
.panel-footer {
display: block;
}
.panel-footer span {
display: block;
margin-top: 4px;
}
.camera-hint {
display: none;
}
}
@media (max-height: 650px) {
.brand p,
.camera-hint {
display: none;
}
.phrase-panel {
bottom: 10px;
padding: 12px;
}
.panel-grid {
grid-template-columns: 190px 1fr;
}
.active-block {
padding-right: 12px;
border-right: 1px solid var(--border);
border-bottom: 0;
}
#activeMotif {
display: none;
}
.timeline-row {
margin-top: 9px;
}
.panel-footer {
margin-top: 8px;
padding-top: 7px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel musical cylinder"></canvas>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Repeating form / 108 BPM</div>
<h1>Phrase Box</h1>
<p>A rotating voxel cylinder turning four musical ideas into a repeating form.</p>
</div>
<div class="top-controls">
<div class="status-pill">
<span class="status-dot"></span>
<span>Visual clock locked</span>
</div>
<button
class="sound-button"
id="soundToggle"
type="button"
aria-pressed="false"
title="Toggle audio. Silent mode does not change the animation timing."
>
<span class="sound-icon" aria-hidden="true"><i></i><i></i><i></i></span>
<span id="soundLabel">Silent mode</span>
</button>
</div>
</header>
<section class="phrase-panel" aria-label="Phrase and form display">
<div class="panel-grid">
<div class="active-block">
<span class="panel-label">Active phrase</span>
<div class="active-heading">
<span id="activeIndex">01 / A</span>
<strong id="activeName" aria-live="polite">Lift</strong>
</div>
<div id="activeMotif">C4 · E4 · G4 · A4</div>
<div class="step-readout">
<span id="stepReadout">Step 01 / 32</span>
<span id="loopReadout">Form 01</span>
</div>
</div>
<div>
<div class="phrase-list" id="phraseList"></div>
<div class="timeline-row">
<span class="panel-label">32-step form</span>
<div class="timeline" id="timeline" aria-hidden="true"></div>
</div>
</div>
</div>
<div class="panel-footer">
<span><strong>Form:</strong> A → B → C → D → repeat</span>
<span>Silent mode changes audio only — visual timing stays identical.</span>
</div>
</section>
<div class="camera-hint">Drag to orbit · Wheel to zoom · M for mute · Double-click to reset</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Musical data
// -------------------------------------------------------------------------
const BPM = 108;
const BEAT = 60 / BPM;
const STEP = BEAT / 2; // Eighth-note step
const STEPS_PER_PHRASE = 8;
const PHRASE_COUNT = 4;
const TOTAL_STEPS = STEPS_PER_PHRASE * PHRASE_COUNT;
const FORM_DURATION = TOTAL_STEPS * STEP;
const NOTE_NAMES = ["C4", "D4", "E4", "F4", "G4", "A4", "B4", "C5"];
const MIDI_VALUES = [60, 62, 64, 65, 67, 69, 71, 72];
const phrases = [
{
code: "A",
name: "Lift",
color: "#5ad2c7",
motif: [0, 2, 4, 5, 4, 2, 1, 0]
},
{
code: "B",
name: "Turn",
color: "#ff8066",
motif: [3, 4, 6, 7, 6, 4, 2, 3]
},
{
code: "C",
name: "Hollow",
color: "#f2c94c",
motif: [4, 3, 1, 2, 4, 6, 5, 4]
},
{
code: "D",
name: "Return",
color: "#789cff",
motif: [7, 6, 4, 2, 3, 1, 0, 0]
}
];
const events = [];
phrases.forEach((phrase, phraseIndex) => {
phrase.motif.forEach((noteIndex, phraseStep) => {
events.push({
phraseIndex,
phraseStep,
noteIndex,
globalStep: phraseIndex * STEPS_PER_PHRASE + phraseStep
});
});
});
// -------------------------------------------------------------------------
// DOM setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const soundToggle = document.querySelector("#soundToggle");
const soundLabel = document.querySelector("#soundLabel");
const activeIndex = document.querySelector("#activeIndex");
const activeName = document.querySelector("#activeName");
const activeMotif = document.querySelector("#activeMotif");
const stepReadout = document.querySelector("#stepReadout");
const loopReadout = document.querySelector("#loopReadout");
const phraseList = document.querySelector("#phraseList");
const timeline = document.querySelector("#timeline");
const phraseCards = phrases.map((phrase, index) => {
const card = document.createElement("div");
card.className = "phrase-card";
card.style.setProperty("--pc", phrase.color);
card.innerHTML = `
<div class="phrase-top">
<span>${String(index + 1).padStart(2, "0")}</span>
<span>${phrase.code}</span>
</div>
<strong>${phrase.name}</strong>
<div class="progress"><i></i></div>
`;
phraseList.appendChild(card);
return card;
});
const phraseBars = [...phraseList.querySelectorAll(".progress i")];
const timelineSteps = [];
for (let i = 0; i < TOTAL_STEPS; i++) {
const cell = document.createElement("div");
const phrase = phrases[Math.floor(i / STEPS_PER_PHRASE)];
cell.className = "timeline-step";
cell.style.setProperty("--pc", phrase.color);
timeline.appendChild(cell);
timelineSteps.push(cell);
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101827);
scene.fog = new THREE.Fog(0x101827, 13, 28);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xc7e3ff, 0x24182e, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.2);
keyLight.position.set(5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const tealLight = new THREE.PointLight(0x53dbc9, 32, 13, 2);
tealLight.position.set(-5, 3, 4);
scene.add(tealLight);
const coralLight = new THREE.PointLight(0xff8066, 26, 12, 2);
coralLight.position.set(5, 1.5, 3);
scene.add(coralLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x111a2a,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.76;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x34425b, 0x202d43);
grid.position.y = -1.745;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
const platform = new THREE.Mesh(
new THREE.BoxGeometry(9.6, 0.3, 6.1),
new THREE.MeshStandardMaterial({
color: 0x1b2940,
roughness: 0.78,
metalness: 0.12
})
);
platform.position.set(0, -1.52, 0.35);
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformTop = new THREE.Mesh(
new THREE.BoxGeometry(8.9, 0.12, 5.45),
new THREE.MeshStandardMaterial({
color: 0x293950,
roughness: 0.72,
metalness: 0.08
})
);
platformTop.position.set(0, -1.31, 0.35);
platformTop.receiveShadow = true;
scene.add(platformTop);
// Decorative voxel blocks around the platform.
const blockColors = [0x5ad2c7, 0xff8066, 0xf2c94c, 0x789cff, 0xddc7a5];
const decorativeBlocks = [
[-4.0, -1.08, 1.7, 0.48],
[-3.65, -0.84, 1.7, 0.26],
[4.0, -1.03, 1.65, 0.52],
[3.68, -0.78, 1.65, 0.28],
[-4.15, -1.12, -1.25, 0.3],
[4.2, -1.14, -1.15, 0.34],
[3.88, -0.91, -1.15, 0.22],
[-3.88, -0.92, -1.18, 0.21]
];
decorativeBlocks.forEach((block, index) => {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(block[3], block[3], block[3]),
new THREE.MeshStandardMaterial({
color: blockColors[index % blockColors.length],
roughness: 0.62,
metalness: 0.06
})
);
mesh.position.set(block[0], block[1], block[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
});
// Static support blocks.
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x26364f,
roughness: 0.62,
metalness: 0.2
});
[-3.18, 3.18].forEach((x) => {
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.48, 2.05, 1.42),
supportMaterial
);
support.position.set(x, -0.26, 0);
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.8, 0.25, 1.75),
supportMaterial
);
foot.position.set(x, -1.13, 0);
foot.castShadow = true;
scene.add(foot);
});
// -------------------------------------------------------------------------
// Rotating voxel cylinder
// -------------------------------------------------------------------------
const cylinder = new THREE.Group();
cylinder.position.y = 1.1;
scene.add(cylinder);
const axle = new THREE.Mesh(
new THREE.BoxGeometry(7.4, 0.28, 0.28),
new THREE.MeshStandardMaterial({
color: 0x172235,
roughness: 0.38,
metalness: 0.55
})
);
axle.castShadow = true;
cylinder.add(axle);
const bodyMaterials = [
new THREE.MeshStandardMaterial({
color: 0xd5bd94,
roughness: 0.72,
metalness: 0.04,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xb79b72,
roughness: 0.78,
metalness: 0.04,
flatShading: true
})
];
const bodyRadius = 1.24;
const plankCount = 12;
for (let i = 0; i < plankCount; i++) {
const angle = (i / plankCount) * Math.PI * 2;
const plank = new THREE.Mesh(
new THREE.BoxGeometry(5.82, 0.34, 0.42),
bodyMaterials[i % 2]
);
plank.position.set(0, Math.sin(angle) * bodyRadius, Math.cos(angle) * bodyRadius);
plank.rotation.x = -angle;
plank.castShadow = true;
plank.receiveShadow = true;
cylinder.add(plank);
}
const capGeometry = new THREE.CylinderGeometry(1.43, 1.43, 0.3, 8, 1, false);
const leftCapMaterial = new THREE.MeshStandardMaterial({
color: 0x4eb9ae,
roughness: 0.5,
metalness: 0.18,
flatShading: true
});
const rightCapMaterial = new THREE.MeshStandardMaterial({
color: 0xe87059,
roughness: 0.52,
metalness: 0.16,
flatShading: true
});
[-3.02, 3.02].forEach((x, index) => {
const cap = new THREE.Mesh(
capGeometry,
index === 0 ? leftCapMaterial : rightCapMaterial
);
cap.rotation.z = Math.PI / 2;
cap.position.x = x;
cap.castShadow = true;
cap.receiveShadow = true;
cylinder.add(cap);
const hub = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.58, 0.58),
new THREE.MeshStandardMaterial({
color: index === 0 ? 0x193f4a : 0x542a31,
roughness: 0.35,
metalness: 0.5
})
);
hub.position.x = x + (index === 0 ? -0.2 : 0.2);
hub.castShadow = true;
cylinder.add(hub);
});
// Peg positions encode pitch along X and time around the circumference.
const noteX = (noteIndex) => (noteIndex - 3.5) * 0.62;
const pegRadius = 1.56;
const pinGeometry = new THREE.BoxGeometry(0.17, 0.17, 0.34);
const pins = [];
events.forEach((event) => {
const angle = (event.globalStep / TOTAL_STEPS) * Math.PI * 2;
const phrase = phrases[event.phraseIndex];
const material = new THREE.MeshStandardMaterial({
color: phrase.color,
emissive: phrase.color,
emissiveIntensity: 0.08,
roughness: 0.34,
metalness: 0.16
});
const pin = new THREE.Mesh(pinGeometry, material);
pin.position.set(
noteX(event.noteIndex),
Math.sin(angle) * pegRadius,
Math.cos(angle) * pegRadius
);
pin.rotation.x = -angle;
pin.castShadow = true;
cylinder.add(pin);
pins.push({ mesh: pin, pulse: 0 });
});
// -------------------------------------------------------------------------
// Strike rail, comb and chime tines
// -------------------------------------------------------------------------
const strikeRail = new THREE.Mesh(
new THREE.BoxGeometry(5.9, 0.16, 0.22),
new THREE.MeshStandardMaterial({
color: 0x162236,
roughness: 0.4,
metalness: 0.45
})
);
strikeRail.position.set(0, -0.51, 0);
strikeRail.castShadow = true;
scene.add(strikeRail);
const combBase = new THREE.Mesh(
new THREE.BoxGeometry(6.35, 0.32, 0.62),
new THREE.MeshStandardMaterial({
color: 0x31445e,
roughness: 0.48,
metalness: 0.26
})
);
combBase.position.set(0, -0.98, 0.93);
combBase.castShadow = true;
combBase.receiveShadow = true;
scene.add(combBase);
const tines = [];
const contacts = [];
for (let i = 0; i < NOTE_NAMES.length; i++) {
const x = noteX(i);
const length = 1.88 - i * 0.105;
const color = new THREE.Color().setHSL(0.105 + i * 0.012, 0.74, 0.62);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.02,
roughness: 0.3,
metalness: 0.48
});
const tine = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.13, length),
material
);
const baseY = -0.69;
tine.position.set(x, baseY, 0.85 + length / 2);
tine.castShadow = true;
scene.add(tine);
const tip = new THREE.Mesh(
new THREE.BoxGeometry(0.23, 0.2, 0.22),
material
);
tip.position.z = length / 2;
tip.castShadow = true;
tine.add(tip);
tines.push({ mesh: tine, material, baseY, pulse: 0 });
const contactMaterial = new THREE.MeshStandardMaterial({
color: color.clone().multiplyScalar(0.72),
emissive: color,
emissiveIntensity: 0.04,
roughness: 0.38,
metalness: 0.3
});
const contact = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.2, 0.24),
contactMaterial
);
contact.position.set(x, -0.46, 0);
contact.castShadow = true;
scene.add(contact);
contacts.push({ mesh: contact, material: contactMaterial, pulse: 0 });
}
// -------------------------------------------------------------------------
// Pooled voxel chime particles
// -------------------------------------------------------------------------
const particleGeometry = new THREE.BoxGeometry(0.14, 0.14, 0.14);
const particleMaterials = phrases.map((phrase) =>
new THREE.MeshBasicMaterial({ color: phrase.color })
);
const particles = [];
const PARTICLE_COUNT = 120;
for (let i = 0; i < PARTICLE_COUNT; i++) {
const mesh = new THREE.Mesh(particleGeometry, particleMaterials[0]);
mesh.visible = false;
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(),
life: 0,
maxLife: 1,
active: false
});
}
function spawnChimeParticles(noteIndex, phraseIndex) {
let spawned = 0;
for (const particle of particles) {
if (particle.active) continue;
const angle = Math.random() * Math.PI * 2;
const spread = 0.65 + Math.random() * 1.15;
particle.active = true;
particle.life = 0.5 + Math.random() * 0.42;
particle.maxLife = particle.life;
particle.mesh.visible = true;
particle.mesh.material = particleMaterials[phraseIndex];
particle.mesh.position.set(
noteX(noteIndex) + (Math.random() - 0.5) * 0.18,
-0.38 + Math.random() * 0.12,
1.08 + (Math.random() - 0.5) * 0.18
);
particle.mesh.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
particle.mesh.scale.setScalar(0.75 + Math.random() * 0.65);
particle.velocity.set(
Math.cos(angle) * spread,
1.35 + Math.random() * 1.55,
0.35 + Math.random() * 1.05
);
if (++spawned >= 8) break;
}
}
function updateParticles(dt) {
for (const particle of particles) {
if (!particle.active) continue;
particle.life -= dt;
if (particle.life <= 0) {
particle.active = false;
particle.mesh.visible = false;
continue;
}
particle.velocity.y -= 3.1 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += dt * 3.8;
particle.mesh.rotation.y += dt * 4.7;
const ratio = particle.life / particle.maxLife;
particle.mesh.scale.setScalar(Math.max(0.05, ratio));
}
}
// -------------------------------------------------------------------------
// Optional audio. Audio is never used as the animation clock.
// -------------------------------------------------------------------------
let audioContext = null;
let masterGain = null;
let soundEnabled = false;
function createAudio() {
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) return false;
audioContext = new AudioContextClass();
masterGain = audioContext.createGain();
masterGain.gain.value = 0.75;
masterGain.connect(audioContext.destination);
return true;
}
function playTone(noteIndex) {
if (!soundEnabled || !audioContext || audioContext.state !== "running") return;
const now = audioContext.currentTime;
const midi = MIDI_VALUES[noteIndex];
const frequency = 440 * Math.pow(2, (midi - 69) / 12);
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
const panner = audioContext.createStereoPanner();
oscillator.type = "triangle";
oscillator.frequency.setValueAtTime(frequency, now);
panner.pan.value = (noteIndex - 3.5) / 4.2;
gain.gain.setValueAtTime(0.0001, now);
gain.gain.exponentialRampToValueAtTime(0.115, now + 0.008);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.48);
oscillator.connect(gain);
gain.connect(panner);
panner.connect(masterGain);
oscillator.start(now);
oscillator.stop(now + 0.5);
const harmonic = audioContext.createOscillator();
const harmonicGain = audioContext.createGain();
harmonic.type = "sine";
harmonic.frequency.setValueAtTime(frequency * 2, now);
harmonicGain.gain.setValueAtTime(0.0001, now);
harmonicGain.gain.exponentialRampToValueAtTime(0.025, now + 0.006);
harmonicGain.gain.exponentialRampToValueAtTime(0.0001, now + 0.25);
harmonic.connect(harmonicGain);
harmonicGain.connect(masterGain);
harmonic.start(now);
harmonic.stop(now + 0.26);
}
async function setSound(enabled) {
if (enabled) {
if (!audioContext && !createAudio()) return;
try {
await audioContext.resume();
} catch {
return;
}
soundEnabled = true;
masterGain.gain.setTargetAtTime(0.75, audioContext.currentTime, 0.015);
} else {
soundEnabled = false;
if (audioContext && masterGain) {
masterGain.gain.setTargetAtTime(0, audioContext.currentTime, 0.012);
}
}
soundToggle.classList.toggle("is-on", soundEnabled);
soundToggle.setAttribute("aria-pressed", String(soundEnabled));
soundLabel.textContent = soundEnabled ? "Sound on" : "Silent mode";
}
soundToggle.addEventListener("click", () => {
setSound(!soundEnabled);
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "m") {
setSound(!soundEnabled);
}
});
// -------------------------------------------------------------------------
// Direct camera controls: no external control modules
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.53,
pitch: 0.34,
radius: 11.1,
target: new THREE.Vector3(0, 0.38, 0.25)
};
function updateCamera() {
const horizontal = cameraState.radius * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.radius * Math.sin(cameraState.pitch),
cameraState.target.z + horizontal * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 0.53;
cameraState.pitch = 0.34;
cameraState.radius = 11.1;
cameraState.target.set(0, 0.38, 0.25);
updateCamera();
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + deltaY * 0.005,
0.06,
1.08
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7.2,
16.5
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Phrase clock and triggering
// -------------------------------------------------------------------------
const startTime = performance.now();
let previousStep = -1;
let previousPhrase = -1;
let previousFrameTime = startTime;
function triggerEvent(event) {
pins[event.globalStep].pulse = 1;
tines[event.noteIndex].pulse = 1;
contacts[event.noteIndex].pulse = 1;
spawnChimeParticles(event.noteIndex, event.phraseIndex);
playTone(event.noteIndex);
}
function updatePhraseDisplay(phraseIndex) {
const phrase = phrases[phraseIndex];
document.documentElement.style.setProperty("--accent", phrase.color);
activeIndex.textContent = `${String(phraseIndex + 1).padStart(2, "0")} / ${phrase.code}`;
activeName.textContent = phrase.name;
activeMotif.textContent = phrase.motif
.map((noteIndex) => NOTE_NAMES[noteIndex])
.join(" · ");
phraseCards.forEach((card, index) => {
card.classList.toggle("active", index === phraseIndex);
});
}
function animate(now) {
const dt = Math.min((now - previousFrameTime) / 1000, 0.05);
previousFrameTime = now;
const elapsed = (now - startTime) / 1000;
const formNumber = Math.floor(elapsed / FORM_DURATION) + 1;
const withinForm = elapsed % FORM_DURATION;
const continuousStep = withinForm / STEP;
const currentStep = Math.min(TOTAL_STEPS - 1, Math.floor(continuousStep));
const currentPhrase = Math.floor(currentStep / STEPS_PER_PHRASE);
const phrasePosition = continuousStep - currentPhrase * STEPS_PER_PHRASE;
// Cylinder angle is derived exclusively from the visual clock.
cylinder.rotation.x = Math.PI / 2 + (continuousStep / TOTAL_STEPS) * Math.PI * 2;
if (currentStep !== previousStep) {
triggerEvent(events[currentStep]);
previousStep = currentStep;
}
if (currentPhrase !== previousPhrase) {
updatePhraseDisplay(currentPhrase);
previousPhrase = currentPhrase;
}
stepReadout.textContent =
`Step ${String(currentStep + 1).padStart(2, "0")} / ${TOTAL_STEPS}`;
loopReadout.textContent = `Form ${String(formNumber).padStart(2, "0")}`;
phraseBars.forEach((bar, index) => {
let progress = 0;
if (index < currentPhrase) progress = 1;
if (index === currentPhrase) progress = phrasePosition / STEPS_PER_PHRASE;
bar.style.width = `${THREE.MathUtils.clamp(progress, 0, 1) * 100}%`;
});
timelineSteps.forEach((cell, index) => {
cell.classList.toggle("passed", index < currentStep);
cell.classList.toggle("current", index === currentStep);
});
for (const pin of pins) {
pin.pulse *= Math.exp(-dt * 7.5);
pin.mesh.material.emissiveIntensity = 0.08 + pin.pulse * 2.3;
pin.mesh.scale.set(
1 + pin.pulse * 0.18,
1 + pin.pulse * 0.18,
1 + pin.pulse * 0.38
);
}
for (const tine of tines) {
tine.pulse *= Math.exp(-dt * 8.5);
tine.mesh.position.y = tine.baseY + tine.pulse * 0.17;
tine.mesh.rotation.x = -tine.pulse * 0.11;
tine.material.emissiveIntensity = 0.02 + tine.pulse * 1.7;
}
for (const contact of contacts) {
contact.pulse *= Math.exp(-dt * 9.5);
contact.material.emissiveIntensity = 0.04 + contact.pulse * 2.1;
contact.mesh.scale.setScalar(1 + contact.pulse * 0.35);
}
updateParticles(dt);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```