Prompt specification
Build or customize the Three.js 3D scene "Pluck position changes a string's harmonic mix" 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: Pluck position changes a string's harmonic mix
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pluck-position-changes-a-string-s-harmonic-mix-x1585
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: pluck position changes a string's harmonic mix. Compare identical fixed-end strings plucked at midpoint and one-fifth length with equal peak displacement. Derive six mode-amplitude bars from triangular initial shapes and show the different suppressed harmonics. Return one self-contained HTML file.
Working Three.js Starter (pluck-position-changes-a-string-s-harmonic-mix-x1585):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pluck Position — Harmonic Mix Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #050a12;
--panel: #0a1220;
--panel-2: #0e1929;
--line: #203149;
--text: #eaf2ff;
--muted: #91a4bd;
--mid: #ffb45f;
--fifth: #58e0d1;
--zero: #66758a;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body { overflow-x: hidden; }
button, summary { font: inherit; }
.app {
min-height: 790px;
height: 100dvh;
display: grid;
grid-template-rows: 82px minmax(420px, 1fr) 286px;
background:
radial-gradient(circle at 18% 8%, rgba(255, 180, 95, .08), transparent 27rem),
radial-gradient(circle at 82% 22%, rgba(88, 224, 209, .07), transparent 28rem),
var(--bg);
}
.topbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 15px 24px;
border-bottom: 1px solid rgba(71, 95, 124, .25);
background: rgba(5, 10, 18, .78);
backdrop-filter: blur(18px);
z-index: 20;
}
.heading-wrap {
min-width: 0;
display: flex;
align-items: center;
gap: 15px;
}
.mark {
width: 42px;
height: 42px;
flex: 0 0 auto;
position: relative;
border: 1px solid #34506e;
border-radius: 12px;
background: linear-gradient(145deg, #16263b, #09111e);
box-shadow: inset 0 0 18px rgba(88, 224, 209, .08);
}
.mark::before,
.mark::after {
content: "";
position: absolute;
left: 8px;
right: 8px;
height: 2px;
border-radius: 2px;
transform-origin: center;
}
.mark::before {
top: 13px;
background: var(--mid);
transform: rotate(18deg);
}
.mark::after {
bottom: 12px;
background: var(--fifth);
transform: rotate(-18deg);
}
h1 {
margin: 0;
font-size: clamp(18px, 2vw, 28px);
line-height: 1.1;
letter-spacing: -.035em;
}
.subtitle {
margin: 5px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: .02em;
}
.actions {
display: flex;
align-items: center;
gap: 8px;
flex: 0 0 auto;
}
.button {
appearance: none;
border: 1px solid #2a405d;
border-radius: 9px;
padding: 9px 12px;
color: #d9e8fb;
background: #101c2c;
cursor: pointer;
font-size: 12px;
font-weight: 700;
letter-spacing: .02em;
transition: border-color .2s, background .2s, transform .2s;
}
.button:hover {
border-color: #55769d;
background: #15243a;
transform: translateY(-1px);
}
.button:focus-visible,
summary:focus-visible {
outline: 2px solid var(--fifth);
outline-offset: 3px;
}
#toggleMotion {
min-width: 100px;
color: #071019;
border-color: transparent;
background: var(--fifth);
}
#toggleMotion:hover { background: #7aeadf; }
#viewport {
position: relative;
min-height: 420px;
overflow: hidden;
isolation: isolate;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active { cursor: grabbing; }
.scene-meta {
position: absolute;
top: 18px;
left: 20px;
display: flex;
flex-wrap: wrap;
gap: 7px;
max-width: min(440px, calc(100% - 150px));
pointer-events: none;
z-index: 5;
}
.chip {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(66, 91, 120, .55);
border-radius: 999px;
color: #aebed1;
background: rgba(7, 14, 25, .72);
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 750;
letter-spacing: .075em;
text-transform: uppercase;
}
.chip::before {
content: "";
width: 5px;
height: 5px;
border-radius: 50%;
background: var(--fifth);
box-shadow: 0 0 10px rgba(88, 224, 209, .7);
}
.camera-tools {
position: absolute;
right: 18px;
top: 17px;
z-index: 6;
display: flex;
align-items: center;
gap: 9px;
}
.camera-hint {
color: #7f93ad;
font-size: 10px;
letter-spacing: .035em;
pointer-events: none;
}
.camera-tools .button {
padding: 7px 10px;
background: rgba(9, 17, 29, .8);
}
.string-label {
position: absolute;
z-index: 4;
min-width: 166px;
padding: 10px 12px;
transform: translate(-50%, -100%);
border: 1px solid rgba(75, 101, 132, .58);
border-radius: 11px;
background: rgba(7, 14, 24, .82);
backdrop-filter: blur(13px);
box-shadow: 0 12px 30px rgba(0, 0, 0, .22);
pointer-events: none;
white-space: nowrap;
}
.string-label::after {
content: "";
position: absolute;
left: 50%;
bottom: -7px;
width: 11px;
height: 11px;
transform: translateX(-50%) rotate(45deg);
border-right: 1px solid rgba(75, 101, 132, .58);
border-bottom: 1px solid rgba(75, 101, 132, .58);
background: rgba(7, 14, 24, .82);
}
.string-label.mid { border-top-color: rgba(255, 180, 95, .85); }
.string-label.fifth { border-top-color: rgba(88, 224, 209, .85); }
.label-kicker {
display: flex;
align-items: center;
gap: 7px;
margin-bottom: 4px;
color: #8296af;
font-size: 9px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
}
.label-kicker i {
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.mid .label-kicker { color: var(--mid); }
.fifth .label-kicker { color: var(--fifth); }
.string-label strong {
display: block;
font-size: 14px;
letter-spacing: -.01em;
}
.string-label small {
display: block;
margin-top: 3px;
color: #90a3ba;
font-size: 10px;
}
.analysis {
position: relative;
z-index: 10;
padding: 14px 24px 16px;
border-top: 1px solid rgba(71, 95, 124, .28);
background: rgba(7, 13, 23, .95);
box-shadow: 0 -18px 50px rgba(0, 0, 0, .18);
}
.analysis-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
}
.analysis h2 {
margin: 0;
font-size: 15px;
letter-spacing: -.015em;
}
.analysis-subtitle {
margin: 3px 0 0;
color: var(--muted);
font-size: 10px;
}
.derivation {
position: relative;
flex: 0 0 auto;
}
.derivation summary {
padding: 7px 11px;
border: 1px solid #283e59;
border-radius: 8px;
color: #a9bdd4;
background: #0e1928;
cursor: pointer;
font-size: 10px;
font-weight: 750;
list-style: none;
}
.derivation summary::-webkit-details-marker { display: none; }
.derivation summary::after { content: " +"; color: var(--fifth); }
.derivation[open] summary::after { content: " −"; }
.derivation-body {
position: absolute;
z-index: 30;
top: calc(100% + 9px);
right: 0;
width: min(500px, calc(100vw - 32px));
padding: 16px;
border: 1px solid #2a435f;
border-radius: 12px;
color: #b5c6da;
background: rgba(10, 19, 32, .98);
box-shadow: 0 18px 50px rgba(0, 0, 0, .5);
font-size: 11px;
line-height: 1.55;
}
.derivation-body p { margin: 0 0 9px; }
.derivation-body p:last-child { margin-bottom: 0; }
.math {
margin: 8px 0;
padding: 9px 10px;
border-left: 2px solid var(--fifth);
border-radius: 0 7px 7px 0;
color: #e3edf9;
background: #0a1422;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
overflow-x: auto;
white-space: nowrap;
}
.formula-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin: 10px 0;
padding: 8px 11px;
border: 1px solid rgba(44, 65, 91, .72);
border-radius: 9px;
background: #09121f;
color: #aebfd2;
font-size: 10px;
}
.equation {
color: #e4eefb;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 11px;
white-space: nowrap;
}
.scale-note { text-align: right; }
.charts {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
.chart-card {
min-width: 0;
padding: 10px 13px 9px;
border: 1px solid #20334d;
border-radius: 12px;
background: linear-gradient(150deg, rgba(17, 29, 46, .96), rgba(9, 17, 29, .96));
}
.chart-card.mid-card { border-top-color: rgba(255, 180, 95, .7); }
.chart-card.fifth-card { border-top-color: rgba(88, 224, 209, .7); }
.chart-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.chart-title {
display: flex;
align-items: baseline;
gap: 8px;
min-width: 0;
}
.chart-title strong {
font-size: 12px;
white-space: nowrap;
}
.chart-title span {
color: #8295ad;
font-size: 9px;
white-space: nowrap;
}
.suppression-pill {
flex: 0 0 auto;
padding: 4px 7px;
border: 1px solid #30445e;
border-radius: 999px;
color: #a9bbcf;
background: #0a1422;
font-size: 8px;
font-weight: 800;
letter-spacing: .065em;
text-transform: uppercase;
}
.mid-card .suppression-pill { border-color: rgba(255, 180, 95, .45); }
.fifth-card .suppression-pill { border-color: rgba(88, 224, 209, .45); }
.bar-chart {
height: 127px;
display: grid;
grid-template-columns: repeat(6, minmax(0, 1fr));
gap: clamp(7px, 1.6vw, 21px);
margin-top: 6px;
padding: 0 3px;
}
.bar-column {
min-width: 0;
display: grid;
grid-template-rows: 18px 87px 17px;
align-items: end;
text-align: center;
}
.bar-value {
align-self: end;
overflow: hidden;
color: #c7d7e8;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: clamp(7px, .8vw, 10px);
text-overflow: ellipsis;
white-space: nowrap;
}
.bar-value.zero { color: #66788f; }
.bar-track {
position: relative;
height: 87px;
border-bottom: 1px solid #354961;
background:
repeating-linear-gradient(
to top,
transparent 0,
transparent 20px,
rgba(68, 90, 117, .14) 21px
);
}
.bar {
position: absolute;
left: 50%;
bottom: 0;
width: min(58%, 42px);
min-height: 2px;
transform: translateX(-50%);
border-radius: 4px 4px 0 0;
background: var(--bar-color);
box-shadow: 0 0 13px color-mix(in srgb, var(--bar-color) 36%, transparent);
transition: height .35s ease;
}
.bar.negative {
background:
repeating-linear-gradient(
135deg,
var(--bar-color) 0,
var(--bar-color) 5px,
rgba(255, 255, 255, .28) 5px,
rgba(255, 255, 255, .28) 8px
);
}
.bar.suppressed {
height: 2px !important;
border: 1px dashed var(--bar-color);
border-bottom: 0;
border-radius: 0;
background: transparent;
box-shadow: none;
}
.mode-index {
align-self: start;
padding-top: 4px;
color: #6f829a;
font-size: 8px;
font-weight: 800;
letter-spacing: .08em;
}
.chart-note {
margin: 3px 0 0;
color: #8396ae;
font-size: 9px;
line-height: 1.35;
}
.chart-note strong { color: #c7d7e8; }
@media (max-width: 760px) {
.app {
height: auto;
min-height: 100dvh;
grid-template-rows: auto 54dvh auto;
}
.topbar {
align-items: flex-start;
padding: 13px 14px;
}
.heading-wrap { align-items: flex-start; }
.mark { width: 36px; height: 36px; border-radius: 10px; }
.mark::before { top: 10px; }
.mark::after { bottom: 9px; }
.subtitle { max-width: 280px; font-size: 10px; }
.actions .button { padding: 8px 9px; font-size: 10px; }
#restart { display: none; }
#viewport { min-height: 430px; }
.camera-hint { display: none; }
.scene-meta { left: 12px; top: 12px; max-width: calc(100% - 110px); }
.chip { font-size: 8px; padding: 6px 8px; }
.camera-tools { right: 11px; top: 11px; }
.string-label {
min-width: 136px;
padding: 8px 9px;
}
.string-label strong { font-size: 11px; }
.string-label small { font-size: 8px; }
.label-kicker { font-size: 7px; }
.analysis { padding: 14px; }
.charts { grid-template-columns: 1fr; }
.formula-line {
align-items: flex-start;
flex-direction: column;
overflow-x: auto;
}
.scale-note { text-align: left; }
.derivation-body { position: fixed; left: 16px; right: 16px; top: auto; bottom: 16px; width: auto; }
}
@media (prefers-reduced-motion: reduce) {
*, *::before, *::after { transition-duration: .001ms !important; }
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="heading-wrap">
<div class="mark" aria-hidden="true"></div>
<div>
<h1>Pluck position changes the harmonic mix.</h1>
<p class="subtitle">Identical fixed-end strings · same length · equal peak displacement</p>
</div>
</div>
<div class="actions">
<button class="button" id="toggleMotion" type="button" aria-pressed="true">❚❚ Pause</button>
<button class="button" id="restart" type="button">Restart</button>
</div>
</header>
<main id="viewport" aria-label="Interactive 3D comparison of two plucked strings">
<div class="scene-meta">
<span class="chip">L = 3.60 m</span>
<span class="chip">D = 0.80 m</span>
<span class="chip">Fixed ends</span>
</div>
<div class="camera-tools">
<span class="camera-hint">Drag to orbit · wheel to zoom</span>
<button class="button" id="cameraReset" type="button">Reset view</button>
</div>
<div class="string-label mid" id="labelMid">
<span class="label-kicker"><i></i>String A</span>
<strong>Midpoint pluck</strong>
<small>a = L/2 · 50% from the left bridge</small>
</div>
<div class="string-label fifth" id="labelFifth">
<span class="label-kicker"><i></i>String B</span>
<strong>One-fifth pluck</strong>
<small>a = L/5 · 20% from the left bridge</small>
</div>
</main>
<section class="analysis" aria-labelledby="spectrumTitle">
<div class="analysis-head">
<div>
<h2 id="spectrumTitle">First six mode amplitudes</h2>
<p class="analysis-subtitle">Bars use |Aₙ|/D; hatching marks negative phase. The 3D motion includes 40 modes.</p>
</div>
<details class="derivation">
<summary>Show derivation</summary>
<div class="derivation-body">
<p>
For fixed ends, the normal modes are
<strong>sin(nπx/L)</strong>. A triangular pluck with peak D at x = a is
</p>
<div class="math">f(x) = D·x/a, 0 ≤ x ≤ a<br>f(x) = D·(L−x)/(L−a), a ≤ x ≤ L</div>
<p>Its sine-series coefficient is</p>
<div class="math">Aₙ = (2/L) ∫₀ᴸ f(x) sin(nπx/L) dx</div>
<p>Evaluating the two linear pieces gives</p>
<div class="math">Aₙ = 2DL² / [π²n²a(L−a)] · sin(nπa/L)</div>
<p>Therefore the two special cases are</p>
<div class="math">a = L/2: Aₙ = 8D/(π²n²) · sin(nπ/2)<br>a = L/5: Aₙ = 25D/(8π²n²) · sin(nπ/5)</div>
<p>
At the midpoint, every even mode has a node there, so the even coefficients vanish.
At L/5, modes whose index is a multiple of five vanish; within the first six modes,
only n = 5 is suppressed.
</p>
</div>
</details>
</div>
<div class="formula-line">
<span class="equation">Aₙ = 2DL² sin(nπa/L) / [π²n²a(L−a)]</span>
<span class="scale-note">Same vertical scale in both charts · peak displacement D = 0.80 m</span>
</div>
<div class="charts">
<article class="chart-card mid-card">
<div class="chart-head">
<div class="chart-title">
<strong>Midpoint</strong>
<span>a = L/2</span>
</div>
<span class="suppression-pill">nodes: n = 2, 4, 6</span>
</div>
<div class="bar-chart" id="midChart" style="--bar-color: var(--mid)"></div>
<p class="chart-note">
<strong>Even modes cancel.</strong> The center is a node for every even harmonic.
</p>
</article>
<article class="chart-card fifth-card">
<div class="chart-head">
<div class="chart-title">
<strong>One-fifth</strong>
<span>a = L/5</span>
</div>
<span class="suppression-pill">node: n = 5</span>
</div>
<div class="bar-chart" id="fifthChart" style="--bar-color: var(--fifth)"></div>
<p class="chart-note">
<strong>Mode 5 cancels.</strong> The off-center pluck also excites even modes.
</p>
</article>
</div>
</section>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------- Physical and visual constants ----------
const L = 3.60; // String length in metres
const D = 0.80; // Equal initial peak displacement in metres
const BASELINE = 1.34; // String rest height in the 3D scene
const STAGE_TOP = 0.18;
const VISIBLE_MODES = 40;
const MOTION_RATE = 1.12;
const midpoint = {
id: "mid",
centerX: -2.28,
pluckFraction: 0.5,
color: 0xffb45f
};
const oneFifth = {
id: "fifth",
centerX: 2.28,
pluckFraction: 0.2,
color: 0x58e0d1
};
// Exact sine-series coefficient of a triangular initial displacement.
function modeCoefficient(n, pluckFraction) {
const a = L * pluckFraction;
return (
(2 * D * L * L) /
(Math.PI * Math.PI * n * n * a * (L - a)) *
Math.sin(Math.PI * n * a / L)
);
}
function buildSpectrum(element, pluckFraction) {
const values = Array.from({ length: 6 }, (_, i) =>
modeCoefficient(i + 1, pluckFraction)
);
element.innerHTML = values.map((coefficient, index) => {
const n = index + 1;
const normalized = coefficient / D;
const percent = Math.abs(normalized) * 100;
const isZero = Math.abs(coefficient) < 1e-12;
const isNegative = coefficient < 0;
const valueText = isZero
? "0% · NODE"
: `${isNegative ? "−" : "+"}${percent.toFixed(percent < 0.1 ? 2 : 1)}%`;
const classes = [
"bar",
isZero ? "suppressed" : "",
isNegative ? "negative" : ""
].filter(Boolean).join(" ");
return `
<div class="bar-column"
aria-label="Mode ${n}: ${valueText.replace("NODE", "suppressed by a node")}">
<div class="bar-value ${isZero ? "zero" : ""}">${valueText}</div>
<div class="bar-track">
<div class="${classes}" style="height:${percent}%"></div>
</div>
<div class="mode-index">n=${n}</div>
</div>
`;
}).join("");
}
buildSpectrum(document.getElementById("midChart"), midpoint.pluckFraction);
buildSpectrum(document.getElementById("fifthChart"), oneFifth.pluckFraction);
// ---------- Three.js scene ----------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.FogExp2(0x07111d, 0.027);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.prepend(renderer.domElement);
const cameraTarget = new THREE.Vector3(0, 0.78, 0);
const desiredAzimuth = 0;
const desiredPolar = 1.16;
const desiredRadius = 9.7;
let currentAzimuth = desiredAzimuth;
let currentPolar = desiredPolar;
let currentRadius = desiredRadius;
const hemisphere = new THREE.HemisphereLight(0xa9c8f4, 0x18202d, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.6);
keyLight.position.set(4.5, 7.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const warmLight = new THREE.PointLight(midpoint.color, 16, 8, 2);
warmLight.position.set(-4.4, 2.3, 2.2);
scene.add(warmLight);
const coolLight = new THREE.PointLight(oneFifth.color, 15, 8, 2);
coolLight.position.set(4.4, 2.1, 2.0);
scene.add(coolLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(32, 24),
new THREE.MeshStandardMaterial({ color: 0x07101b, roughness: 1, metalness: 0 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.065;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(26, 26, 0x29435f, 0x122238);
grid.position.y = -0.045;
grid.material.transparent = true;
grid.material.opacity = 0.5;
scene.add(grid);
const stage = new THREE.Mesh(
new THREE.BoxGeometry(10.65, 0.16, 2.75),
new THREE.MeshStandardMaterial({
color: 0x111e2e,
roughness: 0.78,
metalness: 0.12
})
);
stage.position.y = STAGE_TOP - 0.08;
stage.receiveShadow = true;
scene.add(stage);
const stageLip = new THREE.Mesh(
new THREE.BoxGeometry(10.95, 0.12, 3.02),
new THREE.MeshStandardMaterial({
color: 0x0b1522,
roughness: 0.88,
metalness: 0.08
})
);
stageLip.position.y = 0.005;
stageLip.receiveShadow = true;
scene.add(stageLip);
// Angular divider between the two experiments.
const divider = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.012, 2.35),
new THREE.MeshBasicMaterial({
color: 0x334b67,
transparent: true,
opacity: 0.55
})
);
divider.position.set(0, STAGE_TOP + 0.007, 0);
scene.add(divider);
// Low-poly background forms.
const mountainMaterials = [
new THREE.MeshStandardMaterial({ color: 0x10243a, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x153047, roughness: 1, flatShading: true }),
new THREE.MeshStandardMaterial({ color: 0x0b1b2c, roughness: 1, flatShading: true })
];
const mountainData = [
[-7.0, -5.5, 1.5, 3.8],
[-5.1, -6.3, 1.0, 2.8],
[-3.3, -7.1, 1.8, 4.4],
[ 3.5, -7.0, 1.6, 4.0],
[ 5.4, -6.0, 1.1, 3.0],
[ 7.3, -5.2, 1.7, 3.6]
];
mountainData.forEach((data, index) => {
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(data[2], data[3], index % 2 ? 4 : 5),
mountainMaterials[index % mountainMaterials.length]
);
mountain.position.set(data[0], data[3] / 2 - 0.02, data[1]);
mountain.rotation.y = index * 0.61;
mountain.receiveShadow = true;
scene.add(mountain);
});
// Small deterministic star field.
let seed = 93;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
const starPositions = [];
for (let i = 0; i < 150; i++) {
starPositions.push(
(random() - 0.5) * 25,
0.5 + random() * 9,
-2 - random() * 11
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x91b9d8,
size: 0.035,
transparent: true,
opacity: 0.58,
depthWrite: false
})
);
scene.add(stars);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x60758c,
roughness: 0.42,
metalness: 0.5,
flatShading: true
});
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xa5b8ca,
roughness: 0.32,
metalness: 0.62,
flatShading: true
});
function createString(config) {
const group = new THREE.Group();
group.position.set(config.centerX, BASELINE, 0);
scene.add(group);
const pluckX = -L / 2 + L * config.pluckFraction;
const modes = [];
for (let n = 1; n <= VISIBLE_MODES; n++) {
modes.push({
n,
coefficient: modeCoefficient(n, config.pluckFraction)
});
}
// Rest line.
const restLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-L / 2, 0, 0),
new THREE.Vector3( L / 2, 0, 0)
]),
new THREE.LineBasicMaterial({
color: 0x506780,
transparent: true,
opacity: 0.62
})
);
group.add(restLine);
// Dashed triangular initial shape.
const ghostLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-L / 2, 0, 0),
new THREE.Vector3(pluckX, D, 0),
new THREE.Vector3(L / 2, 0, 0)
]),
new THREE.LineDashedMaterial({
color: config.color,
dashSize: 0.11,
gapSize: 0.075,
transparent: true,
opacity: 0.34
})
);
ghostLine.computeLineDistances();
group.add(ghostLine);
// Stem identifying the initial peak.
const stem = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(pluckX, 0.02, 0),
new THREE.Vector3(pluckX, D, 0)
]),
new THREE.LineDashedMaterial({
color: config.color,
dashSize: 0.065,
gapSize: 0.065,
transparent: true,
opacity: 0.38
})
);
stem.computeLineDistances();
group.add(stem);
const nodeRing = new THREE.Mesh(
new THREE.RingGeometry(0.10, 0.155, 8),
new THREE.MeshBasicMaterial({
color: config.color,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.78
})
);
nodeRing.rotation.x = -Math.PI / 2;
nodeRing.position.set(pluckX, 0.018, 0);
group.add(nodeRing);
const peakMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.095, 0),
new THREE.MeshStandardMaterial({
color: config.color,
emissive: config.color,
emissiveIntensity: 0.28,
roughness: 0.4,
flatShading: true
})
);
peakMarker.position.set(pluckX, D, 0);
group.add(peakMarker);
// Fixed bridge supports.
const supportHeight = BASELINE - STAGE_TOP - 0.10;
for (const sign of [-1, 1]) {
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.17, supportHeight, 6),
supportMaterial
);
support.position.set(
sign * L / 2,
-(BASELINE - STAGE_TOP) + supportHeight / 2,
0
);
support.castShadow = true;
support.receiveShadow = true;
group.add(support);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.30, 0.08, 0.25),
capMaterial
);
cap.position.set(sign * L / 2, -0.045, 0);
cap.castShadow = true;
group.add(cap);
const anchor = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.075, 0),
new THREE.MeshStandardMaterial({
color: config.color,
emissive: config.color,
emissiveIntensity: 0.25,
roughness: 0.4,
flatShading: true
})
);
anchor.position.set(sign * L / 2, 0, 0);
group.add(anchor);
}
// Low-poly cylindrical string, built from instanced six-sided segments.
const segmentCount = 64;
const pointCount = segmentCount + 1;
const cylinderGeometry = new THREE.CylinderGeometry(0.018, 0.018, 1, 6, 1, false);
const cylinderMaterial = new THREE.MeshStandardMaterial({
color: config.color,
emissive: config.color,
emissiveIntensity: 0.32,
roughness: 0.32,
metalness: 0.16,
flatShading: true
});
const cylinders = new THREE.InstancedMesh(
cylinderGeometry,
cylinderMaterial,
segmentCount
);
cylinders.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
cylinders.frustumCulled = false;
group.add(cylinders);
// A fine luminous line joins the faceted segments.
const linePositions = new Float32Array(pointCount * 3);
const lineGeometry = new THREE.BufferGeometry();
lineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(linePositions, 3).setUsage(THREE.DynamicDrawUsage)
);
const luminousLine = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color: config.color,
transparent: true,
opacity: 0.82
})
);
luminousLine.frustumCulled = false;
group.add(luminousLine);
const dummy = new THREE.Object3D();
const up = new THREE.Vector3(0, 1, 0);
const direction = new THREE.Vector3();
const midpoint3 = new THREE.Vector3();
function update(time) {
const phase = time * MOTION_RATE;
const points = new Array(pointCount);
for (let i = 0; i < pointCount; i++) {
const u = (i / segmentCount) * L;
const x = -L / 2 + u;
let y = 0;
for (const mode of modes) {
y += mode.coefficient *
Math.cos(mode.n * phase) *
Math.sin(mode.n * Math.PI * u / L);
}
const point = new THREE.Vector3(x, y, 0);
points[i] = point;
linePositions[i * 3] = x;
linePositions[i * 3 + 1] = y;
linePositions[i * 3 + 2] = 0;
}
lineGeometry.attributes.position.needsUpdate = true;
for (let i = 0; i < segmentCount; i++) {
const p0 = points[i];
const p1 = points[i + 1];
direction.subVectors(p1, p0);
const length = direction.length();
midpoint3.addVectors(p0, p1).multiplyScalar(0.5);
dummy.position.copy(midpoint3);
dummy.quaternion.setFromUnitVectors(up, direction.normalize());
dummy.scale.set(1, length, 1);
dummy.updateMatrix();
cylinders.setMatrixAt(i, dummy.matrix);
}
cylinders.instanceMatrix.needsUpdate = true;
}
update(0);
return {
group,
update,
labelPosition: new THREE.Vector3(config.centerX, BASELINE + 1.03, 0)
};
}
const midpointString = createString(midpoint);
const oneFifthString = createString(oneFifth);
// ---------- Direct, dependency-free orbit camera ----------
const pointers = new Map();
let lastPointerX = 0;
let lastPointerY = 0;
let lastPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const values = [...pointers.values()];
lastPinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
canvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
desiredAzimuth -= dx * 0.006;
desiredPolar = THREE.MathUtils.clamp(
desiredPolar - dy * 0.005,
0.48,
1.46
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (lastPinchDistance > 0) {
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * (lastPinchDistance / distance),
6.8,
15.5
);
}
lastPinchDistance = distance;
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastPointerX = remaining.x;
lastPointerY = remaining.y;
} else {
lastPinchDistance = 0;
}
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
6.8,
15.5
);
}, { passive: false });
function resetCamera() {
desiredAzimuth = 0;
desiredPolar = 1.16;
desiredRadius = 9.7;
}
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("cameraReset").addEventListener("click", resetCamera);
// ---------- Motion controls ----------
let elapsed = 0;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const motionButton = document.getElementById("toggleMotion");
function updateMotionButton() {
motionButton.textContent = playing ? "❚❚ Pause" : "▶ Play";
motionButton.setAttribute("aria-pressed", String(playing));
}
motionButton.addEventListener("click", () => {
playing = !playing;
updateMotionButton();
});
document.getElementById("restart").addEventListener("click", () => {
elapsed = 0;
playing = true;
updateMotionButton();
});
updateMotionButton();
// ---------- Labels, resize, and render loop ----------
const labelMid = document.getElementById("labelMid");
const labelFifth = document.getElementById("labelFifth");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * viewport.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * viewport.clientHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) elapsed += delta;
// Damped camera motion.
currentAzimuth += (desiredAzimuth - currentAzimuth) * 0.085;
currentPolar += (desiredPolar - currentPolar) * 0.085;
currentRadius += (desiredRadius - currentRadius) * 0.085;
const aspectCompensation = Math.max(1, 1.45 / camera.aspect);
const radius = currentRadius * aspectCompensation;
camera.position.set(
cameraTarget.x + radius * Math.sin(currentPolar) * Math.sin(currentAzimuth),
cameraTarget.y + radius * Math.cos(currentPolar),
cameraTarget.z + radius * Math.sin(currentPolar) * Math.cos(currentAzimuth)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
midpointString.update(elapsed);
oneFifthString.update(elapsed);
renderer.render(scene, camera);
placeLabel(labelMid, midpointString.labelPosition);
placeLabel(labelFifth, oneFifthString.labelPosition);
}
animate();
</script>
</body>
</html>
```