Prompt specification
Build or customize the Three.js 3D scene "Musical Ratio Garden" 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: Musical Ratio Garden
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/musical-ratio-garden-x3328
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing string length, pitch, and musical ratios. Build an interactive garden of string-like resonators with selectable rational length ratios. Display ideal frequency relationships and label generated tones as illustrative rather than authentic instrument timbres. Voxel style.
Working Three.js Starter (musical-ratio-garden-x3328):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ratio Garden — Voxel String Resonators</title>
<style>
:root {
--ink: #173c35;
--muted: #61746d;
--paper: #f5efd9;
--paper-dark: #e8dfc2;
--accent: #e85d4a;
--border: #244c43;
--shadow: #173c35;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
min-height: 100%;
background: var(--paper);
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
button,
select {
font: inherit;
}
button {
color: inherit;
}
#app {
display: grid;
grid-template-columns: 360px minmax(0, 1fr);
width: 100%;
height: 100vh;
height: 100dvh;
overflow: hidden;
}
#panel {
position: relative;
z-index: 10;
display: flex;
flex-direction: column;
min-height: 0;
padding: 22px 18px 18px;
overflow-y: auto;
background:
linear-gradient(rgba(23, 60, 53, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(23, 60, 53, 0.035) 1px, transparent 1px),
var(--paper);
background-size: 12px 12px;
border-right: 3px solid var(--border);
box-shadow: 6px 0 0 rgba(23, 60, 53, 0.12);
}
.eyebrow {
margin-bottom: 8px;
color: #b64c3b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(35px, 4vw, 48px);
line-height: 0.88;
letter-spacing: -0.07em;
text-transform: uppercase;
}
h1 span {
color: var(--accent);
}
.intro {
margin: 16px 0 18px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.tool-row {
display: flex;
gap: 8px;
margin-bottom: 12px;
}
.tool-button {
flex: 1;
min-height: 36px;
padding: 8px;
border: 2px solid var(--border);
border-radius: 0;
background: #fff9e8;
box-shadow: 3px 3px 0 var(--shadow);
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.05em;
text-transform: uppercase;
transition: transform 100ms, box-shadow 100ms;
}
.tool-button:active {
transform: translate(2px, 2px);
box-shadow: 1px 1px 0 var(--shadow);
}
.tool-button.off {
color: #8c554c;
background: #efd9cd;
}
.reference {
margin-bottom: 18px;
padding: 12px;
border: 2px solid var(--border);
background: #fff9e8;
box-shadow: 4px 4px 0 rgba(23, 60, 53, 0.18);
}
.reference strong {
display: block;
margin-bottom: 5px;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.reference p {
margin: 5px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.formula {
display: block;
margin-top: 9px;
padding-top: 9px;
border-top: 1px dashed #a9b3a2;
color: var(--ink);
font-size: 12px;
font-weight: 800;
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin: 2px 0 10px;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.section-heading span:last-child {
color: var(--muted);
font-size: 9px;
font-weight: 600;
}
#resonatorList {
display: grid;
gap: 10px;
padding-bottom: 16px;
}
.resonator-card {
--accent: #e85d4a;
position: relative;
padding: 11px;
border: 2px solid var(--border);
background: rgba(255, 249, 232, 0.96);
box-shadow: 4px 4px 0 rgba(23, 60, 53, 0.16);
}
.resonator-card::before {
content: "";
position: absolute;
top: -2px;
bottom: -2px;
left: -2px;
width: 6px;
background: var(--accent);
border-right: 2px solid var(--border);
}
.card-head {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
padding-left: 5px;
}
.card-swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
background: var(--accent);
border: 2px solid var(--border);
}
.card-title {
min-width: 0;
flex: 1;
}
.card-title strong {
display: block;
font-size: 11px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.card-title small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.card-title small.reference-badge {
color: #b64c3b;
font-weight: 900;
}
.ratio-select {
width: 112px;
height: 31px;
padding: 4px 7px;
border: 2px solid var(--border);
border-radius: 0;
background: #fff;
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 800;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding-left: 5px;
}
.metric {
min-width: 0;
padding: 7px 6px;
background: #eee5c9;
border: 1px solid #b9b59d;
}
.metric small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 7px;
font-weight: 900;
letter-spacing: 0.08em;
}
.metric strong {
display: block;
overflow: hidden;
font-size: 11px;
text-overflow: ellipsis;
white-space: nowrap;
}
.pitch-line {
display: flex;
align-items: center;
justify-content: space-between;
margin: 8px 0 6px;
padding-left: 5px;
font-size: 10px;
}
.pitch-line .note {
font-weight: 900;
}
.pitch-line .cents {
color: var(--muted);
font-size: 9px;
}
.frequency-track {
height: 5px;
margin-left: 5px;
overflow: hidden;
background: #d6d2b9;
border: 1px solid #a9ad93;
}
.frequency-fill {
width: 50%;
height: 100%;
background: var(--accent);
transition: width 180ms ease-out;
}
.play-button {
width: calc(100% - 5px);
margin: 9px 0 0 5px;
padding: 7px;
border: 2px solid var(--border);
border-radius: 0;
background: var(--accent);
box-shadow: 2px 2px 0 var(--border);
cursor: pointer;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: transform 90ms, box-shadow 90ms;
}
.play-button:active {
transform: translate(2px, 2px);
box-shadow: none;
}
.panel-note {
margin-top: auto;
padding: 14px 5px 2px;
color: var(--muted);
font-size: 9px;
line-height: 1.55;
}
.panel-note strong {
color: #9c4033;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #cfe6df;
}
#canvasHost,
#labels {
position: absolute;
inset: 0;
}
#canvasHost canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
min-width: 106px;
padding: 6px 8px;
border: 2px solid var(--border);
background: rgba(255, 249, 232, 0.93);
box-shadow: 3px 3px 0 rgba(23, 60, 53, 0.35);
color: var(--ink);
text-align: center;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -8px;
width: 10px;
height: 6px;
background: #fff9e8;
border-right: 2px solid var(--border);
border-bottom: 2px solid var(--border);
transform: translateX(-50%) rotate(45deg);
}
.world-label .label-main {
display: flex;
justify-content: center;
gap: 7px;
font-size: 10px;
font-weight: 900;
}
.world-label .label-main span {
color: #b64c3b;
}
.world-label .label-sub {
margin-top: 3px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
}
.scene-top {
position: absolute;
z-index: 4;
top: 16px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.scene-chip {
padding: 8px 10px;
border: 2px solid var(--border);
background: rgba(255, 249, 232, 0.92);
box-shadow: 3px 3px 0 rgba(23, 60, 53, 0.22);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.scene-chip span {
color: #b64c3b;
}
.scene-help {
max-width: 245px;
padding: 8px 10px;
border: 2px solid var(--border);
background: rgba(255, 249, 232, 0.9);
box-shadow: 3px 3px 0 rgba(23, 60, 53, 0.18);
color: var(--muted);
font-size: 9px;
line-height: 1.45;
text-align: right;
}
.disclaimer {
position: absolute;
z-index: 5;
left: 50%;
bottom: 50px;
width: min(690px, calc(100% - 34px));
padding: 10px 13px;
border: 2px solid #9e4638;
background: rgba(255, 244, 218, 0.95);
box-shadow: 4px 4px 0 rgba(87, 48, 41, 0.35);
transform: translateX(-50%);
color: #684038;
font-size: 9px;
line-height: 1.45;
text-align: center;
}
.disclaimer strong {
color: #a23f31;
letter-spacing: 0.08em;
}
.scene-footer {
position: absolute;
z-index: 5;
left: 16px;
right: 16px;
bottom: 13px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
pointer-events: none;
}
.scene-footer span {
color: #355f55;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.scene-footer button {
padding: 6px 8px;
border: 2px solid var(--border);
background: rgba(255, 249, 232, 0.92);
box-shadow: 2px 2px 0 rgba(23, 60, 53, 0.3);
cursor: pointer;
pointer-events: auto;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#status {
position: absolute;
z-index: 6;
top: 75px;
left: 50%;
max-width: calc(100% - 32px);
padding: 8px 12px;
border: 2px solid var(--border);
background: #fff9e8;
box-shadow: 3px 3px 0 rgba(23, 60, 53, 0.25);
transform: translateX(-50%);
font-size: 9px;
font-weight: 800;
text-align: center;
opacity: 0;
transition: opacity 150ms;
pointer-events: none;
}
#status.visible {
opacity: 1;
}
@media (max-width: 850px) {
html,
body {
overflow: auto;
}
#app {
display: flex;
flex-direction: column;
height: auto;
min-height: 100vh;
overflow: visible;
}
#stage {
order: 0;
width: 100%;
height: 58vh;
min-height: 410px;
border-bottom: 3px solid var(--border);
}
#panel {
order: 1;
width: 100%;
overflow: visible;
border-right: 0;
box-shadow: none;
}
#resonatorList {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.scene-help {
display: none;
}
}
@media (max-width: 560px) {
#resonatorList {
grid-template-columns: 1fr;
}
.disclaimer {
bottom: 47px;
font-size: 8px;
}
.scene-footer span {
max-width: 210px;
line-height: 1.35;
}
h1 {
font-size: 38px;
}
}
</style>
</head>
<body>
<div id="app">
<aside id="panel">
<div class="eyebrow">Interactive acoustics lab</div>
<h1>Ratio<br><span>Garden</span></h1>
<p class="intro">
Grow string-like resonators from rational lengths. Each length is
compared with a fixed calibration string, so shorter strings produce
higher ideal frequencies.
</p>
<div class="tool-row">
<button id="audioToggle" class="tool-button" aria-pressed="true">
Sound On
</button>
<button id="resetGarden" class="tool-button">
Reset Garden
</button>
</div>
<section class="reference">
<strong>Calibration string</strong>
<div>A3 · 220 Hz · Length ratio 1 : 1</div>
<p>
Ratios use equal tension and equal linear density. The resulting
tones are synthesized illustrations of frequency relationships.
</p>
<span class="formula">f = 220 ÷ (L / Lref)</span>
</section>
<div class="section-heading">
<span>Resonator plots</span>
<span>Choose a length</span>
</div>
<div id="resonatorList"></div>
<div class="panel-note">
<strong>ILLUSTRATIVE TONES:</strong>
generated resonances demonstrate pitch relationships. They are not
samples or faithful models of any real instrument’s timbre.
</div>
</aside>
<main id="stage">
<div id="canvasHost"></div>
<div id="labels"></div>
<div class="scene-top">
<div class="scene-chip">
Voxel acoustics garden <span>●</span>
</div>
<div class="scene-help">
Drag to orbit · Scroll to zoom<br />
Click a colored string to sound it
</div>
</div>
<div id="status" role="status" aria-live="polite"></div>
<div class="disclaimer">
<strong>ILLUSTRATIVE TONES — NOT AUTHENTIC INSTRUMENT TIMBRES.</strong>
These synthesized resonances show ideal frequency relationships and
nearby equal-tempered pitches; they do not recreate a specific string,
guitar, violin, or other acoustic instrument.
</div>
<div class="scene-footer">
<span>Fixed tension · equal density · ratio-based tuning</span>
<button id="resetView">Reset View</button>
</div>
</main>
</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.querySelector("#stage");
const canvasHost = document.querySelector("#canvasHost");
const labelsHost = document.querySelector("#labels");
const listHost = document.querySelector("#resonatorList");
const statusEl = document.querySelector("#status");
const audioToggle = document.querySelector("#audioToggle");
const REFERENCE_FREQUENCY = 220;
const BASE_LENGTH = 4.2;
const LEFT_SUPPORT = -2.05;
const ratios = [
{ n: 1, d: 1, label: "1 : 1", hint: "unison / reference" },
{ n: 4, d: 5, label: "4 : 5", hint: "major third" },
{ n: 3, d: 4, label: "3 : 4", hint: "perfect fourth" },
{ n: 2, d: 3, label: "2 : 3", hint: "perfect fifth" },
{ n: 5, d: 7, label: "5 : 7", hint: "septimal ratio" },
{ n: 1, d: 2, label: "1 : 2", hint: "octave" },
{ n: 7, d: 12, label: "7 : 12", hint: "wide interval" }
];
const initialRatioIndexes = [0, 1, 2, 3, 4, 5];
const palette = [
0xe85d4a,
0x35a99c,
0xf0bd3f,
0x4d7eae,
0x916bb2,
0x74a84e
];
const plotCenters = [
[-6.15, -2.35],
[0, -2.35],
[6.15, -2.35],
[-6.15, 2.35],
[0, 2.35],
[6.15, 2.35]
];
// -------------------------------------------------------------------------
// Renderer, scene, and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel garden of rational-length string resonators"
);
canvasHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfe6df);
scene.fog = new THREE.Fog(0xcfe6df, 32, 65);
const camera = new THREE.OrthographicCamera(-12, 12, 9, -9, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.55, 0);
const view = {
azimuth: 0.64,
elevation: 0.72,
zoom: 1
};
function updateCamera() {
const radius = 30;
const horizontal = radius * Math.cos(view.elevation);
camera.position.set(
cameraTarget.x + Math.sin(view.azimuth) * horizontal,
cameraTarget.y + Math.sin(view.elevation) * radius,
cameraTarget.z + Math.cos(view.azimuth) * horizontal
);
camera.lookAt(cameraTarget);
camera.zoom = view.zoom;
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
function resize() {
const width = Math.max(1, stage.clientWidth);
const height = Math.max(1, stage.clientHeight);
const aspect = width / height;
const verticalSize = Math.max(14.5, 24 / aspect);
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
renderer.setSize(width, height, false);
updateCamera();
}
new ResizeObserver(resize).observe(stage);
resize();
const ambient = new THREE.HemisphereLight(0xf7fff5, 0x61775e, 2.0);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xfff0cb, 3.2);
sun.position.set(8, 16, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9d8e8, 0.85);
fill.position.set(-12, 8, -9);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared voxel materials and helpers
// -------------------------------------------------------------------------
function material(color, roughness = 0.86, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
});
}
const materials = {
soil: material(0x6f4934),
soilDark: material(0x533625),
grass: material(0x7cad57),
grassLight: material(0x94c96c),
grassDark: material(0x638c49),
path: material(0xd3b783),
wood: material(0x8c5a38),
woodLight: material(0xb57948),
stone: material(0x9ca69c),
stoneLight: material(0xc3c8b5),
gold: material(0xe5b847, 0.45, 0.18),
trunk: material(0x6d4932),
leaf: material(0x4f8a49),
leafLight: material(0x70a955),
leafDark: material(0x39723f),
stem: material(0x3d7540),
petalRed: material(0xe86b59),
petalYellow: material(0xf1cf50),
petalBlue: material(0x6d9bd2),
petalWhite: material(0xf7f0d6),
ruler: material(0x334d43)
};
function box(parent, width, height, depth, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createVoxelGround() {
const tileGeometry = new THREE.BoxGeometry(1, 0.38, 1);
const tileMaterial = material(0xffffff, 1);
const columns = 21;
const rows = 10;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
const dummy = new THREE.Object3D();
const colorA = new THREE.Color(0x82b65c);
const colorB = new THREE.Color(0x78aa56);
let instance = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < columns; x++) {
dummy.position.set(x - 10, -0.31, z - 4.5);
dummy.scale.set(0.98, 1, 0.98);
dummy.updateMatrix();
tiles.setMatrixAt(instance, dummy.matrix);
tiles.setColorAt(
instance,
(x + z) % 2 === 0 ? colorA : colorB
);
instance++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
tiles.receiveShadow = true;
scene.add(tiles);
box(scene, 21.15, 0.55, 10.15, materials.soilDark, 0, -0.69, 0);
// Paths between the six garden beds.
box(scene, 18.4, 0.12, 0.55, materials.path, 0, -0.02, 0);
box(scene, 0.55, 0.12, 8.5, materials.path, -3.08, -0.01, 0);
box(scene, 0.55, 0.12, 8.5, materials.path, 3.08, -0.01, 0);
}
function createFlower(parent, x, z, petalMaterial, height = 0.45) {
box(parent, 0.06, height, 0.06, materials.stem, x, 0.58 + height / 2, z);
const top = 0.61 + height;
box(parent, 0.16, 0.08, 0.08, petalMaterial, x - 0.08, top, z);
box(parent, 0.16, 0.08, 0.08, petalMaterial, x + 0.08, top, z);
box(parent, 0.08, 0.08, 0.16, petalMaterial, x, top, z - 0.08);
box(parent, 0.08, 0.08, 0.16, petalMaterial, x, top, z + 0.08);
box(parent, 0.09, 0.09, 0.09, materials.gold, x, top + 0.02, z);
}
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
box(tree, 0.38, 1.45, 0.38, materials.trunk, 0, 0.54, 0);
box(tree, 1.15, 0.85, 1.05, materials.leafDark, 0, 1.48, 0);
box(tree, 0.9, 0.72, 0.85, materials.leaf, -0.42, 1.92, 0.08);
box(tree, 0.8, 0.7, 0.9, materials.leafLight, 0.38, 1.95, -0.08);
box(tree, 0.72, 0.55, 0.7, materials.leaf, 0.04, 2.35, 0.02);
}
function createShrub(x, z, mat = materials.leafDark) {
box(scene, 0.72, 0.48, 0.65, mat, x, 0.12, z);
box(scene, 0.5, 0.45, 0.52, materials.leaf, x + 0.32, 0.16, z + 0.12);
box(scene, 0.46, 0.36, 0.5, materials.leafLight, x - 0.24, 0.13, z - 0.17);
}
createVoxelGround();
createTree(-9.7, -4.05, 1.05);
createTree(9.7, 4.05, 0.92);
createTree(-9.75, 4.1, 0.72);
createTree(9.75, -4.05, 0.72);
createShrub(-9.15, -3.45);
createShrub(9.1, 3.5);
createShrub(8.9, -3.55, materials.leaf);
createShrub(-9.15, 3.5, materials.leaf);
// -------------------------------------------------------------------------
// Resonator construction
// -------------------------------------------------------------------------
const resonators = [];
let pickables = [];
function disposeGroupContents(group) {
group.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
group.clear();
}
function createBed(resonator) {
const group = resonator.group;
box(group, 5.25, 0.58, 2.65, materials.soil, 0, 0, 0);
box(group, 5.1, 0.16, 2.5, materials.grass, 0, 0.36, 0);
// Voxel edging.
for (let i = 0; i < 9; i++) {
const x = -2.25 + i * 0.5625;
box(group, 0.43, 0.19, 0.17, materials.woodLight, x, 0.32, 1.31);
}
for (let i = 0; i < 5; i++) {
const z = -1.12 + i * 0.56;
box(group, 0.17, 0.19, 0.42, materials.woodLight, -2.59, 0.32, z);
box(group, 0.17, 0.19, 0.42, materials.woodLight, 2.59, 0.32, z);
}
const petalMaterials = [
materials.petalRed,
materials.petalYellow,
materials.petalBlue,
materials.petalWhite
];
const flowerPositions = [
[-2.25, 1.03, 0],
[2.22, 1.04, 1],
[-2.26, -1.04, 2],
[2.22, -1.02, 3]
];
for (const [x, z, colorIndex] of flowerPositions) {
createFlower(
group,
x,
z,
petalMaterials[colorIndex],
0.3 + (colorIndex % 2) * 0.12
);
}
// Small grass blocks add voxel texture without obscuring the strings.
const grassBlocks = [
[-1.85, 1.03, 0.16],
[-1.62, 1.02, 0.13],
[1.78, 1.0, 0.15],
[2.0, -1.0, 0.12],
[-1.96, -1.01, 0.14]
];
grassBlocks.forEach(([x, z, size], index) => {
box(
group,
size,
size,
size,
index % 2 ? materials.grassLight : materials.grassDark,
x,
0.49,
z
);
});
}
function createRuler(resonator) {
const ruler = new THREE.Group();
resonator.group.add(ruler);
for (let i = 0; i <= 8; i++) {
const x = LEFT_SUPPORT + (BASE_LENGTH * i) / 8;
const height = i % 4 === 0 ? 0.12 : 0.075;
box(
ruler,
0.045,
height,
0.12,
materials.ruler,
x,
0.5 + height / 2,
0.95
);
}
resonator.marker = box(
ruler,
0.1,
0.15,
0.22,
resonator.stringMaterial,
LEFT_SUPPORT,
0.57,
0.95
);
}
function rebuildResonator(resonator) {
for (let i = pickables.length - 1; i >= 0; i--) {
if (pickables[i].userData.resonator === resonator) {
pickables.splice(i, 1);
}
}
disposeGroupContents(resonator.dynamicGroup);
const ratio = ratios[resonator.ratioIndex];
const lengthRatio = ratio.n / ratio.d;
const actualLength = BASE_LENGTH * lengthRatio;
const rightSupport = LEFT_SUPPORT + actualLength;
resonator.lengthRatio = lengthRatio;
resonator.frequency = REFERENCE_FREQUENCY / lengthRatio;
resonator.stringMeshes = [];
// Upright voxel supports.
box(
resonator.dynamicGroup,
0.28,
1.34,
0.34,
materials.wood,
LEFT_SUPPORT,
1.13,
-0.35
);
box(
resonator.dynamicGroup,
0.28,
1.34,
0.34,
materials.wood,
rightSupport,
1.13,
-0.35
);
box(
resonator.dynamicGroup,
0.46,
0.22,
0.48,
materials.stone,
LEFT_SUPPORT,
0.54,
-0.35
);
box(
resonator.dynamicGroup,
0.46,
0.22,
0.48,
materials.stone,
rightSupport,
0.54,
-0.35
);
// Segmented top beam.
const beamBlockCount = Math.max(3, Math.ceil(actualLength / 0.38));
const beamBlockLength = actualLength / beamBlockCount;
for (let i = 0; i < beamBlockCount; i++) {
const x =
LEFT_SUPPORT +
beamBlockLength * (i + 0.5);
box(
resonator.dynamicGroup,
beamBlockLength * 0.92,
0.17,
0.19,
materials.woodLight,
x,
1.83,
-0.35
);
}
// Gold end pins and a voxel tuning peg.
box(
resonator.dynamicGroup,
0.17,
0.17,
0.25,
materials.gold,
LEFT_SUPPORT,
1.31,
-0.35
);
box(
resonator.dynamicGroup,
0.17,
0.17,
0.25,
materials.gold,
rightSupport,
1.31,
-0.35
);
box(
resonator.dynamicGroup,
0.17,
0.31,
0.48,
materials.gold,
rightSupport,
1.1,
-0.35
);
// The visible string length is exactly proportional to L / Lref.
const segmentCount = Math.max(5, Math.round(actualLength / 0.17));
const segmentLength = actualLength / segmentCount;
for (let i = 0; i < segmentCount; i++) {
const x =
LEFT_SUPPORT + segmentLength * (i + 0.5);
const segment = box(
resonator.dynamicGroup,
segmentLength * 0.74,
0.12,
0.12,
resonator.stringMaterial,
x,
1.31,
-0.35
);
segment.userData.resonator = resonator;
segment.userData.phase = (i / Math.max(1, segmentCount - 1)) * Math.PI;
segment.userData.baseY = 1.31;
resonator.stringMeshes.push(segment);
pickables.push(segment);
}
resonator.marker.position.x = rightSupport;
resonator.marker.material = resonator.stringMaterial;
}
function noteInformation(frequency) {
const noteNames = [
"C", "C♯", "D", "D♯", "E", "F",
"F♯", "G", "G♯", "A", "A♯", "B"
];
const midiFloat = 69 + 12 * Math.log2(frequency / 440);
const nearestMidi = Math.round(midiFloat);
const cents = Math.round((midiFloat - nearestMidi) * 100);
const note = noteNames[((nearestMidi % 12) + 12) % 12];
const octave = Math.floor(nearestMidi / 12) - 1;
const centsText = `${cents > 0 ? "+" : ""}${cents}¢`;
return {
name: `${note}${octave}`,
cents: centsText
};
}
function updateResonatorUI(resonator) {
const ratio = ratios[resonator.ratioIndex];
const pitchRatio = `${ratio.d} : ${ratio.n}`;
const frequency = resonator.frequency;
const note = noteInformation(frequency);
const relative = ratio.d === ratio.n ? "same" : ratio.d > ratio.n ? "higher" : "lower";
resonator.card.querySelector(".length-value").textContent = ratio.label;
resonator.card.querySelector(".pitch-value").textContent = pitchRatio;
resonator.card.querySelector(".frequency-value").textContent =
`${frequency.toFixed(1)} Hz`;
resonator.card.querySelector(".note").textContent = note.name;
resonator.card.querySelector(".cents").textContent = note.cents;
resonator.card.querySelector(".relation").textContent =
`${relative} than reference`;
const normalized = THREE.MathUtils.clamp(
Math.log(frequency / 110) / Math.log(8),
0,
1
);
resonator.card
.querySelector(".frequency-fill")
.style.width = `${18 + normalized * 82}%`;
resonator.card
.querySelector(".card-title small")
.classList.toggle("reference-badge", resonator.ratioIndex === 0);
resonator.card
.querySelector(".card-title small")
.textContent = resonator.ratioIndex === 0
? "REFERENCE"
: `${note.name} · ${relative}`;
resonator.label.innerHTML = `
<div class="label-main">
L ${ratio.label} <span>→ ${pitchRatio}</span>
</div>
<div class="label-sub">
${frequency.toFixed(1)} Hz · ${note.name}
</div>
`;
}
function createResonatorCard(resonator) {
const card = document.createElement("article");
card.className = "resonator-card";
card.style.setProperty(
"--accent",
`#${resonator.color.toString(16).padStart(6, "0")}`
);
const options = ratios
.map(
(ratio, index) =>
`<option value="${index}">L ${ratio.label} · ${ratio.hint}</option>`
)
.join("");
card.innerHTML = `
<div class="card-head">
<span class="card-swatch"></span>
<div class="card-title">
<strong>Plot ${String(resonator.index + 1).padStart(2, "0")}</strong>
<small></small>
</div>
<select class="ratio-select" aria-label="Length ratio for plot ${resonator.index + 1}">
${options}
</select>
</div>
<div class="metrics">
<div class="metric">
<small>Length</small>
<strong class="length-value"></strong>
</div>
<div class="metric">
<small>Ideal pitch</small>
<strong class="pitch-value"></strong>
</div>
<div class="metric">
<small>Frequency</small>
<strong class="frequency-value"></strong>
</div>
</div>
<div class="pitch-line">
<span class="note"></span>
<span class="cents"></span>
<span class="relation"></span>
</div>
<div class="frequency-track">
<div class="frequency-fill"></div>
</div>
<button class="play-button">▶ Play illustrative tone</button>
`;
const select = card.querySelector(".ratio-select");
select.value = String(resonator.ratioIndex);
select.addEventListener("change", () => {
resonator.ratioIndex = Number(select.value);
rebuildResonator(resonator);
updateResonatorUI(resonator);
});
card.querySelector(".play-button").addEventListener("click", () => {
playResonator(resonator);
});
listHost.appendChild(card);
resonator.card = card;
}
plotCenters.forEach(([x, z], index) => {
const color = palette[index];
const resonator = {
index,
color,
ratioIndex: initialRatioIndexes[index],
group: new THREE.Group(),
dynamicGroup: new THREE.Group(),
stringMeshes: [],
frequency: REFERENCE_FREQUENCY,
lengthRatio: 1,
vibration: 0,
glow: 0,
stringMaterial: new THREE.MeshStandardMaterial({
color,
roughness: 0.52,
metalness: 0.08,
emissive: new THREE.Color(color),
emissiveIntensity: 0
})
};
resonator.group.position.set(x, 0, z);
scene.add(resonator.group);
createBed(resonator);
createRuler(resonator);
resonator.group.add(resonator.dynamicGroup);
rebuildResonator(resonator);
const label = document.createElement("div");
label.className = "world-label";
labelsHost.appendChild(label);
resonator.label = label;
createResonatorCard(resonator);
updateResonatorUI(resonator);
resonators.push(resonator);
});
// -------------------------------------------------------------------------
// Illustrative Web Audio synthesis
// -------------------------------------------------------------------------
let audioContext = null;
let masterGain = null;
let audioEnabled = true;
let statusTimer = null;
async function ensureAudio() {
if (!audioContext) {
const AudioContextClass =
window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) {
showStatus("Audio is not available in this browser.");
return null;
}
audioContext = new AudioContextClass();
masterGain = audioContext.createGain();
masterGain.gain.value = 0.38;
masterGain.connect(audioContext.destination);
}
if (audioContext.state === "suspended") {
await audioContext.resume();
}
return audioContext;
}
async function playResonator(resonator) {
if (!audioEnabled) {
showStatus("Sound is muted. Enable sound to play the tone.");
return;
}
const context = await ensureAudio();
if (!context) return;
const now = context.currentTime;
const frequency = resonator.frequency;
const output = context.createGain();
output.gain.setValueAtTime(0.0001, now);
output.gain.exponentialRampToValueAtTime(0.25, now + 0.012);
output.gain.exponentialRampToValueAtTime(0.0001, now + 2.05);
const filter = context.createBiquadFilter();
filter.type = "lowpass";
filter.frequency.setValueAtTime(Math.min(6200, frequency * 10), now);
filter.Q.setValueAtTime(0.65, now);
const panner = context.createStereoPanner
? context.createStereoPanner()
: null;
if (panner) {
panner.pan.setValueAtTime(
THREE.MathUtils.clamp(resonator.group.position.x / 9, -0.8, 0.8),
now
);
filter.connect(output);
output.connect(panner);
panner.connect(masterGain);
} else {
filter.connect(output);
output.connect(masterGain);
}
// A quiet additive bank suggests a resonance without claiming to
// reproduce the timbre of any real instrument.
for (let harmonic = 1; harmonic <= 5; harmonic++) {
const oscillator = context.createOscillator();
const harmonicGain = context.createGain();
oscillator.type = harmonic === 1 ? "triangle" : "sine";
oscillator.frequency.setValueAtTime(frequency * harmonic, now);
harmonicGain.gain.setValueAtTime(
0.72 / Math.pow(harmonic, 1.75),
now
);
oscillator.connect(harmonicGain);
harmonicGain.connect(filter);
oscillator.start(now);
oscillator.stop(now + 2.2);
}
resonator.vibration = 1;
resonator.glow = 1;
const note = noteInformation(frequency);
showStatus(
`${note.name} · ${frequency.toFixed(1)} Hz — illustrative resonance`
);
}
function showStatus(message) {
statusEl.textContent = message;
statusEl.classList.add("visible");
clearTimeout(statusTimer);
statusTimer = setTimeout(() => {
statusEl.classList.remove("visible");
}, 2200);
}
audioToggle.addEventListener("click", async () => {
audioEnabled = !audioEnabled;
if (audioEnabled) {
await ensureAudio();
if (masterGain && audioContext) {
masterGain.gain.setTargetAtTime(
0.38,
audioContext.currentTime,
0.025
);
}
} else if (masterGain && audioContext) {
masterGain.gain.setTargetAtTime(
0.0001,
audioContext.currentTime,
0.025
);
}
audioToggle.textContent = audioEnabled ? "Sound On" : "Sound Off";
audioToggle.classList.toggle("off", !audioEnabled);
audioToggle.setAttribute("aria-pressed", String(audioEnabled));
});
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit and wheel to zoom
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickResonator(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
return intersections.length
? intersections[0].object.userData.resonator
: null;
}
canvas.addEventListener("pointerdown", (event) => {
pointerDown = {
x: event.clientX,
y: event.clientY,
id: event.pointerId,
moved: false
};
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerDown && event.pointerId === pointerDown.id) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 4) {
pointerDown.moved = true;
}
view.azimuth -= dx * 0.006;
view.elevation = THREE.MathUtils.clamp(
view.elevation + dy * 0.0045,
0.28,
1.28
);
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
updateCamera();
} else {
const resonator = pickResonator(event);
canvas.style.cursor = resonator ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (
pointerDown &&
pointerDown.id === event.pointerId &&
!pointerDown.moved
) {
const resonator = pickResonator(event);
if (resonator) playResonator(resonator);
}
pointerDown = null;
const resonator = pickResonator(event);
canvas.style.cursor = resonator ? "pointer" : "grab";
});
canvas.addEventListener("pointercancel", () => {
pointerDown = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.zoom = THREE.MathUtils.clamp(
view.zoom * Math.exp(-event.deltaY * 0.001),
0.65,
2.2
);
updateCamera();
},
{ passive: false }
);
function resetView() {
view.azimuth = 0.64;
view.elevation = 0.72;
view.zoom = 1;
updateCamera();
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#resetView").addEventListener("click", resetView);
document.querySelector("#resetGarden").addEventListener("click", () => {
resonators.forEach((resonator, index) => {
resonator.ratioIndex = initialRatioIndexes[index];
resonator.card.querySelector(".ratio-select").value =
String(resonator.ratioIndex);
rebuildResonator(resonator);
updateResonatorUI(resonator);
});
showStatus("Garden ratios reset.");
});
// -------------------------------------------------------------------------
// Animation and projected labels
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const projected = new THREE.Vector3();
function updateWorldLabels() {
const width = stage.clientWidth;
const height = stage.clientHeight;
for (const resonator of resonators) {
projected.set(
resonator.group.position.x,
2.28,
resonator.group.position.z
);
projected.project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -100 &&
x < width + 100 &&
y > -100 &&
y < height + 100;
resonator.label.style.display = visible ? "block" : "none";
resonator.label.style.left = `${x}px`;
resonator.label.style.top = `${y}px`;
}
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
for (const resonator of resonators) {
if (resonator.vibration > 0.001) {
const amplitude = 0.14 * resonator.vibration;
resonator.stringMeshes.forEach((mesh) => {
const envelope = Math.sin(mesh.userData.phase);
const wave = Math.sin(time * 54 + mesh.userData.phase * 7);
mesh.position.y =
mesh.userData.baseY + amplitude * envelope * wave;
});
resonator.vibration *= Math.exp(-delta * 2.25);
} else {
resonator.vibration = 0;
resonator.stringMeshes.forEach((mesh) => {
mesh.position.y = mesh.userData.baseY;
});
}
if (resonator.glow > 0.001) {
resonator.glow *= Math.exp(-delta * 2.8);
resonator.stringMaterial.emissiveIntensity = resonator.glow * 0.8;
} else {
resonator.glow = 0;
resonator.stringMaterial.emissiveIntensity = 0;
}
}
updateWorldLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```