Prompt specification
Build or customize the Three.js 3D scene "Instrument tuning classroom scene" 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: Instrument tuning classroom scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/instrument-tuning-classroom-scene-x1523
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: instrument tuning classroom scene. Compose a string instrument, reference source, and visual beat indicator; connect slower beats to closer frequencies while avoiding a false indication of which source is higher. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (instrument-tuning-classroom-scene-x1523):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Beat Lab — Instrument Tuning Classroom</title>
<style>
:root {
--ink: #18262d;
--muted: #5e6b6e;
--paper: rgba(248, 244, 232, 0.91);
--line: rgba(24, 38, 45, 0.15);
--teal: #168c87;
--gold: #d69a2d;
--red: #c95b43;
--green: #2c9c6b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d0bd;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
display: flex;
align-items: center;
gap: 12px;
pointer-events: none;
user-select: none;
}
.brand-mark {
display: grid;
place-items: center;
width: 42px;
height: 42px;
border: 1px solid rgba(24, 38, 45, 0.28);
border-radius: 12px;
background: rgba(248, 244, 232, 0.8);
box-shadow: 0 8px 24px rgba(24, 38, 45, 0.08);
backdrop-filter: blur(10px);
}
.brand-mark::before {
content: "";
width: 17px;
height: 17px;
background:
linear-gradient(90deg, var(--teal) 0 33%, transparent 33% 39%,
var(--teal) 39% 72%, transparent 72% 78%, var(--teal) 78%),
linear-gradient(0deg, rgba(24, 38, 45, 0.35) 0 25%, transparent 25% 35%,
rgba(24, 38, 45, 0.35) 35% 65%, transparent 65% 75%,
rgba(24, 38, 45, 0.35) 75%);
}
.brand-copy small {
display: block;
margin-bottom: 2px;
color: rgba(24, 38, 45, 0.62);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand-copy strong {
display: block;
font-size: 17px;
letter-spacing: -0.02em;
}
.panel {
position: fixed;
z-index: 20;
top: 24px;
right: 24px;
bottom: 24px;
width: min(370px, calc(100vw - 48px));
padding: 25px;
overflow-y: auto;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 24px;
background: var(--paper);
box-shadow: 0 24px 70px rgba(24, 38, 45, 0.18);
backdrop-filter: blur(18px) saturate(1.15);
scrollbar-width: thin;
scrollbar-color: rgba(24, 38, 45, 0.25) transparent;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 11px;
color: var(--teal);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 7px;
height: 7px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(22, 140, 135, 0.12);
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 42px);
line-height: 0.98;
letter-spacing: -0.055em;
}
.intro {
margin: 13px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.readout {
padding: 17px 18px 15px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(255, 255, 255, 0.47);
}
.readout-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.beat-number {
display: flex;
align-items: baseline;
gap: 7px;
min-width: 0;
}
#beatValue {
font-variant-numeric: tabular-nums;
font-size: 43px;
font-weight: 800;
line-height: 1;
letter-spacing: -0.06em;
}
.unit {
color: var(--muted);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.state {
padding: 7px 9px;
border-radius: 999px;
background: rgba(22, 140, 135, 0.11);
color: var(--teal);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
line-height: 1;
text-align: center;
text-transform: uppercase;
white-space: nowrap;
}
.state[data-level="medium"] {
color: #9c6810;
background: rgba(214, 154, 45, 0.14);
}
.state[data-level="wide"] {
color: var(--red);
background: rgba(201, 91, 67, 0.13);
}
.state[data-level="matched"] {
color: var(--green);
background: rgba(44, 156, 107, 0.13);
}
.pulse-track {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 5px;
height: 22px;
margin-top: 16px;
align-items: center;
}
.pulse-track span {
height: 7px;
border-radius: 2px;
background: var(--teal);
opacity: 0.16;
transform: scaleY(0.65);
transition: background-color 180ms ease;
}
.pulse-caption {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: rgba(24, 38, 45, 0.5);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.controls {
display: grid;
gap: 17px;
margin: 22px 0 18px;
}
.control label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.04em;
}
.control output {
padding: 4px 7px;
border-radius: 7px;
background: rgba(24, 38, 45, 0.07);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 99px;
appearance: none;
background: linear-gradient(
90deg,
var(--teal) 0%,
var(--teal) var(--fill, 10%),
rgba(24, 38, 45, 0.13) var(--fill, 10%),
rgba(24, 38, 45, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
border: 3px solid #f7f1e4;
border-radius: 5px;
appearance: none;
background: var(--ink);
box-shadow: 0 2px 8px rgba(24, 38, 45, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #f7f1e4;
border-radius: 5px;
background: var(--ink);
box-shadow: 0 2px 8px rgba(24, 38, 45, 0.25);
}
.scale {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: rgba(24, 38, 45, 0.42);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.buttons {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
}
button {
min-height: 44px;
border: 0;
border-radius: 12px;
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform 150ms ease, box-shadow 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-1px);
}
button:active {
transform: translateY(1px);
}
#audioButton {
color: #f8f4e8;
background: var(--ink);
box-shadow: 0 8px 18px rgba(24, 38, 45, 0.18);
}
#audioButton[data-on="true"] {
color: var(--ink);
background: #5fd0bd;
}
#resetButton {
padding: 0 14px;
border: 1px solid var(--line);
color: var(--ink);
background: rgba(255, 255, 255, 0.44);
}
.method-note {
margin-top: 18px;
padding: 13px 14px;
border-left: 3px solid var(--teal);
border-radius: 0 10px 10px 0;
background: rgba(22, 140, 135, 0.07);
color: #4b5d60;
font-size: 11px;
line-height: 1.55;
}
.method-note strong {
color: var(--ink);
}
.camera-hint {
position: fixed;
z-index: 10;
left: 26px;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid rgba(24, 38, 45, 0.14);
border-radius: 12px;
background: rgba(248, 244, 232, 0.72);
color: rgba(24, 38, 45, 0.7);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.03em;
pointer-events: none;
backdrop-filter: blur(10px);
}
.camera-hint span {
color: var(--teal);
font-size: 16px;
}
#loading {
position: fixed;
z-index: 50;
inset: 0;
display: grid;
place-items: center;
background: #d8d0bd;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
top: 14px;
left: 14px;
}
.brand-copy strong {
font-size: 14px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 49vh;
padding: 19px;
border-radius: 20px;
}
h1 {
font-size: 29px;
}
.intro {
margin-bottom: 15px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive voxel classroom showing a string instrument, reference speaker, and beat indicator"></div>
<header class="topbar">
<div class="brand-mark" aria-hidden="true"></div>
<div class="brand-copy">
<small>Voxel Physics</small>
<strong>Beat Lab Classroom</strong>
</div>
</header>
<aside class="panel" aria-label="Beat tuning controls">
<div class="eyebrow">Instrument Tuning / Audio Lab</div>
<h1>Match the beat.</h1>
<p class="intro">
Adjust the string instrument and reference source. A slower visual beat means
their frequencies are closer.
</p>
<section class="readout" aria-live="polite">
<div class="readout-top">
<div class="beat-number">
<span id="beatValue">1.2</span>
<span class="unit">Hz</span>
</div>
<div id="matchState" class="state" data-level="close">Close · slow pulses</div>
</div>
<div id="pulseTrack" class="pulse-track" aria-hidden="true"></div>
<div class="pulse-caption">
<span>Visual beat rate</span>
<span>Slower = closer</span>
</div>
</section>
<section class="controls">
<div class="control">
<label for="instrumentFrequency">
<span>String instrument</span>
<output id="instrumentOutput" for="instrumentFrequency">220.0 Hz</output>
</label>
<input
id="instrumentFrequency"
type="range"
min="196"
max="440"
step="0.1"
value="220"
/>
<div class="scale"><span>196 Hz</span><span>440 Hz</span></div>
</div>
<div class="control">
<label for="referenceFrequency">
<span>Reference source</span>
<output id="referenceOutput" for="referenceFrequency">221.2 Hz</output>
</label>
<input
id="referenceFrequency"
type="range"
min="196"
max="440"
step="0.1"
value="221.2"
/>
<div class="scale"><span>196 Hz</span><span>440 Hz</span></div>
</div>
</section>
<div class="buttons">
<button id="audioButton" type="button" data-on="false">▶ Start the two tones</button>
<button id="resetButton" type="button" title="Reset frequencies">Reset</button>
</div>
<div class="method-note">
<strong>Order-neutral method.</strong> The displayed beat rate is
<strong>|instrument − reference|</strong>. Its sign is intentionally hidden,
so the scene does not claim which source has the higher pitch. At an exact
match, the lamp becomes steady and the beat rate is zero.
</div>
</aside>
<div class="camera-hint" aria-hidden="true">
<span>↔</span> Drag to orbit · Scroll to zoom
</div>
<div id="loading">Building voxel classroom…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const loading = document.getElementById("loading");
const instrumentInput = document.getElementById("instrumentFrequency");
const referenceInput = document.getElementById("referenceFrequency");
const instrumentOutput = document.getElementById("instrumentOutput");
const referenceOutput = document.getElementById("referenceOutput");
const beatValue = document.getElementById("beatValue");
const matchState = document.getElementById("matchState");
const pulseTrack = document.getElementById("pulseTrack");
const audioButton = document.getElementById("audioButton");
const resetButton = document.getElementById("resetButton");
const pulseDots = [];
for (let i = 0; i < 12; i++) {
const dot = document.createElement("span");
pulseTrack.appendChild(dot);
pulseDots.push(dot);
}
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d0bd);
scene.fog = new THREE.Fog(0xd8d0bd, 15, 28);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff8e8, 0x71807d, 2.25);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1d8, 3.2);
keyLight.position.set(-4, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xa8d9dd, 1.15);
fillLight.position.set(8, 6, -4);
scene.add(fillLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(w, h, d, x, y, z, material, parent = scene) {
const mesh = new THREE.Mesh(unitBox, getMaterial(material));
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Classroom shell and tiled floor.
box(17, 0.42, 12, 0, -0.27, 0.3, 0xb9aa8f);
const tileColors = [0xd7cdb7, 0xcfc4ad, 0xdcd3bf, 0xc9bea7];
for (let x = -4; x <= 3; x++) {
for (let z = -2; z <= 2; z++) {
const index = (x + 4) + (z + 2) * 4;
box(
1.96,
0.055,
1.96,
x * 2,
0.005,
z * 2 + 0.3,
tileColors[index % tileColors.length]
);
}
}
box(17, 7, 0.3, 0, 3.25, -5.55, 0xded6c2);
box(0.3, 7, 11, -8.35, 3.25, 0, 0xcac1ab);
box(17, 0.18, 0.12, 0, 0.16, -5.34, 0x6f746e);
box(0.12, 0.18, 11, -8.14, 0.16, 0, 0x6f746e);
// Chalkboard and a voxel waveform.
box(4.65, 2.85, 0.16, -4.95, 3.75, -5.28, 0x775947);
box(4.35, 2.55, 0.12, -4.95, 3.75, -5.16, 0x244a43);
box(4.8, 0.12, 0.22, -4.95, 2.37, -5.1, 0x604a3d);
box(0.18, 0.55, 0.16, -6.18, 2.22, -5.03, 0xd9c99c);
box(0.18, 0.55, 0.16, -5.88, 2.22, -5.03, 0xd9c99c);
for (let i = 0; i < 21; i++) {
const x = -6.75 + i * 0.18;
const y = 3.78 + Math.sin(i * 0.72) * 0.38;
box(
0.095,
0.095,
0.07,
x,
y,
-5.075,
i % 3 === 0 ? 0xf1e6c7 : 0xb7d5c8
);
}
box(3.1, 0.035, 0.04, -4.95, 3.18, -5.06, 0x7fa59a);
box(2.2, 0.035, 0.04, -5.35, 2.74, -5.06, 0x7fa59a);
// Demonstration table.
const tableTop = box(8.8, 0.32, 3.25, 0, 1.2, 0.05, 0xa96f47);
tableTop.material = new THREE.MeshStandardMaterial({
color: 0xa96f47,
roughness: 0.72
});
box(8.35, 0.18, 2.85, 0, 0.96, 0.05, 0x805238);
for (const x of [-3.7, 3.7]) {
for (const z of [-1.05, 1.15]) {
box(0.28, 1.35, 0.28, x, 0.46, z, 0x5f493b);
}
}
box(7.8, 0.18, 0.18, 0, 0.42, -0.95, 0x5f493b);
// Voxel string instrument.
const guitar = new THREE.Group();
guitar.position.set(-2.55, 2.14, 0.55);
guitar.rotation.y = -0.14;
scene.add(guitar);
box(1.92, 1.44, 0.48, 0, 0, 0, 0x9e4e32, guitar);
box(1.46, 0.72, 0.52, 0, 0.78, 0.025, 0x9e4e32, guitar);
box(1.82, 1.1, 0.5, 0, 1.42, 0, 0xad5938, guitar);
box(1.74, 0.22, 0.57, 0, 1.05, 0.02, 0xb9633f, guitar);
box(0.34, 2.65, 0.3, 0, 2.93, -0.01, 0x513b30, guitar);
box(0.68, 0.84, 0.34, 0, 4.56, 0, 0x513b30, guitar);
// Stepped highlights make the body read as a hand-built voxel prop.
box(1.8, 0.13, 0.08, 0, -0.59, 0.27, 0xc77549, guitar);
box(1.54, 0.13, 0.08, 0, 0.84, 0.3, 0xc77549, guitar);
box(0.44, 0.44, 0.06, 0, 0.73, 0.29, 0x302a25, guitar);
// Bridge, frets, and six strings.
box(0.86, 0.12, 0.1, 0, 0.17, 0.31, 0xe0b06e, guitar);
for (let i = 0; i < 8; i++) {
box(0.42, 0.035, 0.045, 0, 1.83 + i * 0.31, 0.18, 0xc8bfa6, guitar);
}
for (let i = 0; i < 6; i++) {
const x = -0.115 + i * 0.046;
box(0.018, 4.75, 0.018, x, 2.61, 0.24, 0xe9dfc6, guitar);
}
for (let side = -1; side <= 1; side += 2) {
for (let i = 0; i < 3; i++) {
box(0.18, 0.09, 0.09, side * 0.43, 4.28 + i * 0.25, 0.02, 0xc6a66b, guitar);
box(0.08, 0.23, 0.08, side * 0.52, 4.28 + i * 0.25, 0.02, 0xd4c09b, guitar);
}
}
box(0.34, 0.28, 0.25, 0, 0.04, -0.05, 0x3c2d25, guitar);
// Reference loudspeaker.
const speaker = new THREE.Group();
speaker.position.set(2.55, 2.68, 0.42);
scene.add(speaker);
box(2.18, 2.58, 1.12, 0, 0, 0, 0x394448, speaker);
box(2.02, 2.38, 0.09, 0, 0, 0.6, 0x242d31, speaker);
box(1.48, 1.48, 0.12, 0, -0.18, 0.68, 0x596266, speaker);
box(1.17, 1.17, 0.15, 0, -0.18, 0.77, 0x20282b, speaker);
box(0.72, 0.72, 0.13, 0, -0.18, 0.86, 0x3c4548, speaker);
box(0.24, 0.24, 0.09, 0, -0.18, 0.95, 0x171e21, speaker);
for (let i = -3; i <= 3; i++) {
box(0.025, 1.05, 0.025, i * 0.16, -0.18, 0.95, 0x8b9495, speaker);
box(1.05, 0.025, 0.025, 0, -0.18 + i * 0.16, 0.95, 0x8b9495, speaker);
}
box(0.22, 0.18, 0.08, 0.7, 0.91, 0.69, 0xd89b3e, speaker);
box(0.12, 0.12, 0.08, 0.35, 0.91, 0.69, 0x62b8a8, speaker);
for (const x of [-0.77, 0.77]) {
box(0.28, 0.18, 0.75, x, -1.29, 0, 0x22292c, speaker);
}
// Central beat indicator on the table.
const beatRig = new THREE.Group();
beatRig.position.set(0, 0, 0.12);
scene.add(beatRig);
box(1.02, 0.16, 1.02, 0, 1.46, 0, 0x36464b, beatRig);
box(0.18, 1.2, 0.18, 0, 2.08, 0, 0x526166, beatRig);
box(1.42, 1.42, 0.52, 0, 3.02, 0, 0x344247, beatRig);
box(1.22, 1.22, 0.08, 0, 3.02, 0.29, 0x182428, beatRig);
const initialBeatColor = new THREE.Color(0x42c8b6);
const beatCoreMaterial = new THREE.MeshStandardMaterial({
color: initialBeatColor,
emissive: initialBeatColor,
emissiveIntensity: 1.2,
roughness: 0.32,
metalness: 0.05
});
const haloMaterial = new THREE.MeshBasicMaterial({
color: initialBeatColor,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const halo = box(1.02, 1.02, 0.06, 0, 3.02, 0.35, haloMaterial, beatRig);
halo.castShadow = false;
halo.receiveShadow = false;
const beatCore = box(0.66, 0.66, 0.15, 0, 3.02, 0.43, beatCoreMaterial, beatRig);
box(0.3, 0.08, 0.08, 0, 3.87, 0.08, 0x7fd8ca, beatRig);
// Two symmetric, non-directional signal rails connect the sources to the meter.
for (let i = 0; i < 6; i++) {
const x = -1.55 + i * 0.2;
box(0.16, 0.055, 0.12, x, 1.41, -0.7, 0xcf7a45);
}
for (let i = 0; i < 6; i++) {
const x = 0.55 + i * 0.2;
box(0.16, 0.055, 0.12, x, 1.41, -0.7, 0x4ca9a0);
}
// Wall-mounted voxel beat-rate display.
box(4.65, 1.35, 0.17, 0.05, 4.3, -5.25, 0x66574a);
box(4.4, 1.12, 0.12, 0.05, 4.3, -5.12, 0x25383a);
box(1.55, 0.16, 0.1, -0.95, 5.22, -5.05, 0xd6a34c);
box(0.36, 0.16, 0.1, 0.05, 5.22, -5.05, 0x62b8a8);
box(1.55, 0.16, 0.1, 1.05, 5.22, -5.05, 0x62b8a8);
const rowLampMaterials = [];
const rowLamps = [];
for (let i = 0; i < 8; i++) {
const material = new THREE.MeshStandardMaterial({
color: 0x31504d,
emissive: 0x42c8b6,
emissiveIntensity: 0.1,
roughness: 0.45
});
rowLampMaterials.push(material);
const lamp = box(
0.32,
0.52,
0.12,
-1.48 + i * 0.44,
4.3,
-5.01,
material
);
rowLamps.push(lamp);
}
// Classroom details.
const poster = new THREE.Group();
poster.position.set(5.75, 3.65, -5.23);
scene.add(poster);
box(2.35, 2.7, 0.12, 0, 0, 0, 0xe3d8bd, poster);
box(1.9, 0.24, 0.09, 0, 0.82, 0.09, 0x3d7774, poster);
box(1.42, 0.16, 0.09, -0.1, 0.42, 0.09, 0xd69a2d, poster);
box(1.72, 0.13, 0.09, 0.05, 0.08, 0.09, 0xb75d45, poster);
for (let i = 0; i < 4; i++) {
box(0.3, 0.3, 0.1, -0.65 + i * 0.43, -0.58, 0.1, 0x7ea59a, poster);
}
// Small voxel stool.
box(1.45, 0.22, 1.3, -5.65, 1.12, 2.25, 0x718d83);
for (const x of [-6.12, -5.18]) {
for (const z of [1.82, 2.68]) {
box(0.18, 1.25, 0.18, x, 0.48, z, 0x6c5747);
}
}
// Books on the floor.
box(1.0, 0.16, 0.72, 5.55, 0.12, 2.65, 0x3f7472);
box(0.85, 0.14, 0.66, 5.5, 0.28, 2.62, 0xd29a3d);
box(0.92, 0.12, 0.7, 5.6, 0.41, 2.68, 0xa95043);
// Direct camera controls implemented without addons.
const cameraState = {
theta: 0.56,
phi: 1.08,
radius: 13.4,
target: new THREE.Vector3(-0.45, 2.65, 0)
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.42,
1.38
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
7.8,
20
);
updateCamera();
},
{ passive: false }
);
updateCamera();
// Web Audio: two equal-level sine sources make the beat phenomenon audible.
let audioContext = null;
let masterGain = null;
let instrumentOscillator = null;
let referenceOscillator = null;
let audioOn = false;
async function ensureAudio() {
if (!audioContext) {
const AudioContextClass = window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) {
audioButton.textContent = "Audio unavailable";
audioButton.disabled = true;
return false;
}
audioContext = new AudioContextClass();
masterGain = audioContext.createGain();
masterGain.gain.value = 0;
masterGain.connect(audioContext.destination);
instrumentOscillator = audioContext.createOscillator();
instrumentOscillator.type = "sine";
referenceOscillator = audioContext.createOscillator();
referenceOscillator.type = "sine";
const instrumentGain = audioContext.createGain();
const referenceGain = audioContext.createGain();
instrumentGain.gain.value = 0.115;
referenceGain.gain.value = 0.115;
// A small start offset prevents exact-frequency cancellation while
// preserving the natural phase drift responsible for audible beats.
const referenceDelay = audioContext.createDelay(0.05);
referenceDelay.delayTime.value = 0.017;
instrumentOscillator.connect(instrumentGain);
instrumentGain.connect(masterGain);
referenceOscillator.connect(referenceGain);
referenceGain.connect(referenceDelay);
referenceDelay.connect(masterGain);
const now = audioContext.currentTime;
instrumentOscillator.start(now);
referenceOscillator.start(now + 0.035);
updateAudioFrequencies();
}
if (audioContext.state === "suspended") {
await audioContext.resume();
}
return true;
}
function updateAudioFrequencies() {
if (!audioContext || !instrumentOscillator || !referenceOscillator) return;
const now = audioContext.currentTime;
instrumentOscillator.frequency.setTargetAtTime(
Number(instrumentInput.value),
now,
0.015
);
referenceOscillator.frequency.setTargetAtTime(
Number(referenceInput.value),
now,
0.015
);
}
audioButton.addEventListener("click", async () => {
if (!audioOn) {
const ready = await ensureAudio();
if (!ready) return;
audioOn = true;
masterGain.gain.cancelScheduledValues(audioContext.currentTime);
masterGain.gain.setTargetAtTime(0.62, audioContext.currentTime, 0.035);
audioButton.dataset.on = "true";
audioButton.textContent = "■ Pause the two tones";
} else {
audioOn = false;
masterGain.gain.cancelScheduledValues(audioContext.currentTime);
masterGain.gain.setTargetAtTime(0, audioContext.currentTime, 0.025);
audioButton.dataset.on = "false";
audioButton.textContent = "▶ Start the two tones";
}
});
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--fill", `${percent}%`);
}
function getBeatRate() {
return Math.abs(
Number(instrumentInput.value) - Number(referenceInput.value)
);
}
function getBeatColor(rate) {
if (rate < 0.05) return new THREE.Color(0x53d98b);
if (rate < 2) return new THREE.Color(0x42c8b6);
if (rate < 8) return new THREE.Color(0xe0ac43);
return new THREE.Color(0xe06b4d);
}
function updateReadout() {
const instrument = Number(instrumentInput.value);
const reference = Number(referenceInput.value);
const rate = getBeatRate();
instrumentOutput.value = `${instrument.toFixed(1)} Hz`;
referenceOutput.value = `${reference.toFixed(1)} Hz`;
beatValue.textContent = rate < 0.05 ? "0.0" : rate.toFixed(1);
if (rate < 0.05) {
matchState.textContent = "Matched · steady";
matchState.dataset.level = "matched";
} else if (rate < 1.5) {
matchState.textContent = "Close · slow pulses";
matchState.dataset.level = "close";
} else if (rate < 6) {
matchState.textContent = "Tuning · medium pulses";
matchState.dataset.level = "medium";
} else {
matchState.textContent = "Separated · fast pulses";
matchState.dataset.level = "wide";
}
updateRangeFill(instrumentInput);
updateRangeFill(referenceInput);
updateAudioFrequencies();
}
instrumentInput.addEventListener("input", updateReadout);
referenceInput.addEventListener("input", updateReadout);
resetButton.addEventListener("click", () => {
instrumentInput.value = "220";
referenceInput.value = "221.2";
updateReadout();
});
updateReadout();
let lastColorLevel = "";
function animate() {
const elapsed = performance.now() / 1000;
const rate = getBeatRate();
// The display is capped at 12 Hz to avoid unreadable browser strobing.
// The numerical readout always shows the exact physical beat rate.
const visualRate = Math.min(rate, 12);
const phase = visualRate > 0 ? (elapsed * visualRate) % 1 : 0;
const pulse =
visualRate > 0
? Math.pow(Math.max(0, Math.cos(phase * Math.PI * 2)), 7)
: 0.28;
const color = getBeatColor(rate);
const colorLevel =
rate < 0.05 ? "matched" : rate < 2 ? "close" : rate < 8 ? "medium" : "wide";
if (colorLevel !== lastColorLevel) {
beatCoreMaterial.color.copy(color);
beatCoreMaterial.emissive.copy(color);
haloMaterial.color.copy(color);
rowLampMaterials.forEach((material) => material.emissive.copy(color));
lastColorLevel = colorLevel;
}
beatCoreMaterial.emissiveIntensity = 0.55 + pulse * 3.3;
haloMaterial.opacity = 0.055 + pulse * 0.28;
halo.scale.set(
1.02 * (1 + pulse * 0.22),
1.02 * (1 + pulse * 0.22),
0.06
);
beatCore.scale.set(
0.66 * (1 + pulse * 0.12),
0.66 * (1 + pulse * 0.12),
0.15
);
// Chasing row lamps reinforces the periodic beat without implying
// any relationship or ordering between the two sources.
rowLamps.forEach((lamp, index) => {
const p = index / rowLampMaterials.length;
let distance = Math.abs(p - phase);
distance = Math.min(distance, 1 - distance);
const light = visualRate > 0 ? Math.max(0, 1 - distance * 8) : 0.18;
rowLampMaterials[index].emissiveIntensity = 0.12 + light * 2.8 + pulse * 0.45;
lamp.scale.y = 0.52 * (1 + light * 0.22);
});
pulseDots.forEach((dot, index) => {
const p = index / pulseDots.length;
let distance = Math.abs(p - phase);
distance = Math.min(distance, 1 - distance);
const light = visualRate > 0 ? Math.max(0, 1 - distance * 6) : 0.14;
dot.style.opacity = String(0.13 + light * 0.87);
dot.style.transform = `scaleY(${0.62 + light * 0.72})`;
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
});
animate();
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```