Prompt specification
Build or customize the Three.js 3D scene "Two-wave addition ribbon" 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: Two-wave addition ribbon
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-wave-addition-ribbon-x1589
Scene Description Prompt:
Two-wave addition ribbon. Build two sinusoidal wave strips above a third showing their pointwise sum; align sample positions so addition can be checked visually at every marked location. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (two-wave-addition-ribbon-x1589):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Two-Wave Addition Ribbon</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(8, 20, 34, 0.78);
--line: rgba(153, 201, 231, 0.18);
--text: #eaf6ff;
--muted: #88a6bd;
--a: #54d8ff;
--b: #ffb45f;
--sum: #a7ef75;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body {
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.topbar {
position: absolute;
top: 25px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #9cc1d9;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--sum);
box-shadow: 0 0 14px rgba(167, 239, 117, 0.9);
}
h1 {
margin: 0;
max-width: 560px;
font-size: clamp(27px, 4vw, 51px);
line-height: 0.98;
font-weight: 760;
letter-spacing: -0.045em;
text-transform: uppercase;
}
h1 span {
color: #8ba8bd;
font-weight: 430;
}
.subtitle {
max-width: 490px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: grid;
gap: 8px;
min-width: 188px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(6, 16, 28, 0.62);
backdrop-filter: blur(14px);
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
color: #b8ccda;
font-size: 10px;
font-weight: 720;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.swatch {
width: 18px;
height: 4px;
border-radius: 4px;
box-shadow: 0 0 10px currentColor;
}
.swatch.a {
color: var(--a);
background: var(--a);
}
.swatch.b {
color: var(--b);
background: var(--b);
}
.swatch.sum {
color: var(--sum);
background: var(--sum);
}
.instructions {
position: absolute;
left: 28px;
bottom: 132px;
color: #718da2;
font-size: 10px;
font-weight: 650;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control-dock {
position: absolute;
right: 24px;
bottom: 22px;
left: 24px;
display: grid;
grid-template-columns: 1fr auto 1.4fr;
align-items: center;
gap: 18px;
max-width: 1080px;
margin: 0 auto;
padding: 14px 16px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.transport {
display: flex;
align-items: center;
gap: 10px;
}
button {
border: 1px solid rgba(153, 201, 231, 0.2);
border-radius: 10px;
color: var(--text);
background: rgba(112, 180, 221, 0.08);
font: inherit;
cursor: pointer;
}
button:hover {
background: rgba(112, 180, 221, 0.15);
border-color: rgba(153, 201, 231, 0.34);
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--a);
outline-offset: 3px;
}
#playToggle {
min-width: 112px;
padding: 10px 13px;
color: #dff8ff;
font-size: 10px;
font-weight: 780;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.phase {
display: grid;
gap: 3px;
min-width: 84px;
}
.phase span:first-child {
color: #6f8ca1;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#phaseValue {
color: #c8dfec;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 12px;
}
.divider {
width: 1px;
height: 48px;
background: var(--line);
}
.inspector {
min-width: 0;
}
.inspector-head {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
color: #7f9db1;
font-size: 9px;
font-weight: 760;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#sampleTag {
color: #c9dfeb;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
display: grid;
grid-template-columns: 1fr auto 1fr auto 1fr;
align-items: center;
gap: 9px;
}
.value {
min-width: 0;
padding: 7px 9px;
border: 1px solid rgba(153, 201, 231, 0.12);
border-radius: 9px;
background: rgba(0, 8, 18, 0.26);
}
.value-label {
display: flex;
align-items: center;
gap: 6px;
color: #7996aa;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.value strong {
display: block;
margin-top: 3px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 15px;
font-weight: 650;
}
.value.a strong {
color: var(--a);
}
.value.b strong {
color: var(--b);
}
.value.sum strong {
color: var(--sum);
}
.operator {
color: #567387;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 15px;
}
.scrub {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 10px;
margin-top: 11px;
}
.scrub span {
color: #6e8da2;
font-size: 8px;
font-weight: 760;
letter-spacing: 0.1em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 3px;
margin: 0;
border-radius: 3px;
appearance: none;
background: linear-gradient(
90deg,
rgba(84, 216, 255, 0.75),
rgba(167, 239, 117, 0.55)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 13px;
height: 13px;
border: 2px solid #dff8ff;
border-radius: 50%;
appearance: none;
background: #0c2336;
box-shadow: 0 0 12px rgba(84, 216, 255, 0.55);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 2px solid #dff8ff;
border-radius: 50%;
background: #0c2336;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #91b0c4;
background: var(--bg);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 0.35s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.topbar {
top: 18px;
left: 18px;
right: 18px;
}
.subtitle {
max-width: 330px;
font-size: 11px;
}
.legend {
display: none;
}
.instructions {
left: 18px;
bottom: 245px;
}
.control-dock {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr;
gap: 12px;
padding: 13px;
}
.divider {
width: 100%;
height: 1px;
}
.transport {
justify-content: space-between;
}
.equation {
gap: 5px;
}
.value {
padding: 7px 6px;
}
.value strong {
font-size: 12px;
}
.value-label {
font-size: 7px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive two-wave pointwise addition visualization"></canvas>
<div class="hud">
<header class="topbar">
<div>
<div class="eyebrow">
<span class="live-dot"></span>
Synchronized sample stations
</div>
<h1>Two-Wave<br /><span>Addition Ribbon</span></h1>
<p class="subtitle">
Two phase-shifted sine waves share the same 17 sample positions.
Their lower ribbon is the exact pointwise sum.
</p>
</div>
<div class="legend" aria-label="Wave legend">
<div class="legend-item">
<span class="swatch a"></span>
Wave A · sin(x)
</div>
<div class="legend-item">
<span class="swatch b"></span>
Wave B · sin(x + φ)
</div>
<div class="legend-item">
<span class="swatch sum"></span>
Pointwise sum · A + B
</div>
</div>
</header>
<div class="instructions">Drag to orbit · scroll to zoom · inspect a marker</div>
<section class="control-dock" aria-label="Wave controls and sample inspector">
<div class="transport">
<button id="playToggle" type="button">Pause wave</button>
<div class="phase">
<span>Phase</span>
<strong id="phaseValue">0.25 rad</strong>
</div>
</div>
<div class="divider"></div>
<div class="inspector">
<div class="inspector-head">
<span>Signed displacement / unit amplitude</span>
<span id="sampleTag">Sample 09 / 17</span>
</div>
<div class="equation">
<div class="value a">
<div class="value-label">Wave A</div>
<strong id="valueA">+0.00</strong>
</div>
<div class="operator">+</div>
<div class="value b">
<div class="value-label">Wave B</div>
<strong id="valueB">+0.00</strong>
</div>
<div class="operator">=</div>
<div class="value sum">
<div class="value-label">Sum</div>
<strong id="valueSum">+0.00</strong>
</div>
</div>
<div class="scrub">
<span>Sample</span>
<input
id="sampleSlider"
type="range"
min="0"
max="16"
step="1"
value="8"
aria-label="Selected sample station"
/>
<span id="xValue">x = +0.00</span>
</div>
</div>
</section>
</div>
<div id="loading">Building synchronized ribbons…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const ui = {
playToggle: document.querySelector("#playToggle"),
phaseValue: document.querySelector("#phaseValue"),
valueA: document.querySelector("#valueA"),
valueB: document.querySelector("#valueB"),
valueSum: document.querySelector("#valueSum"),
sampleTag: document.querySelector("#sampleTag"),
sampleSlider: document.querySelector("#sampleSlider"),
xValue: document.querySelector("#xValue")
};
const COLORS = {
a: new THREE.Color("#54d8ff"),
b: new THREE.Color("#ffb45f"),
sum: new THREE.Color("#a7ef75")
};
const SAMPLE_COUNT = 17;
const X_MIN = -6.2;
const X_MAX = 6.2;
const RIBBON_WIDTH = 1.45;
const RIBBON_THICKNESS = 0.085;
const AMPLITUDE = 0.82;
const sampleX = Array.from(
{ length: SAMPLE_COUNT },
(_, i) => THREE.MathUtils.lerp(X_MIN, X_MAX, i / (SAMPLE_COUNT - 1))
);
let phase = 0.25;
let playing = !window.matchMedia("(prefers-reduced-motion: reduce)").matches;
let selectedSample = 8;
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color("#07111f");
scene.fog = new THREE.Fog("#07111f", 23, 43);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
const cameraTarget = new THREE.Vector3(0, 0.25, 0);
let cameraRadius = 18;
let cameraTheta = 0.055;
let cameraPhi = 1.32;
let adjustedForPortrait = false;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const ambient = new THREE.HemisphereLight("#b8e8ff", "#06101a", 1.55);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight("#e8f7ff", 3.1);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(COLORS.a, 22, 20, 2);
cyanLight.position.set(-6, 6, 5);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(COLORS.b, 17, 18, 2);
amberLight.position.set(7, 1, 5);
scene.add(amberLight);
const greenLight = new THREE.PointLight(COLORS.sum, 19, 18, 2);
greenLight.position.set(0, -5, 4);
scene.add(greenLight);
// Flat faceted backdrop.
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(36, 22),
new THREE.MeshStandardMaterial({
color: "#081724",
roughness: 0.98,
metalness: 0.02
})
);
backdrop.position.set(0, 0, -2.45);
backdrop.receiveShadow = true;
scene.add(backdrop);
const gridVertices = [];
for (let x = -12; x <= 12; x += 1.2) {
gridVertices.push(x, -6.5, -2.42, x, 8, -2.42);
}
for (let y = -6; y <= 8; y += 1.2) {
gridVertices.push(-12, y, -2.42, 12, y, -2.42);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: "#31506a",
transparent: true,
opacity: 0.16,
depthWrite: false
})
);
scene.add(grid);
// Utility for creating a rectangular label sprite.
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width * 0.5, height * 0.5);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 640;
labelCanvas.height = 160;
const ctx = labelCanvas.getContext("2d");
roundedRect(ctx, 5, 5, 630, 150, 28);
ctx.fillStyle = "rgba(5, 16, 28, 0.88)";
ctx.fill();
ctx.strokeStyle = color;
ctx.globalAlpha = 0.5;
ctx.lineWidth = 3;
ctx.stroke();
ctx.globalAlpha = 1;
roundedRect(ctx, 24, 34, 9, 92, 5);
ctx.fillStyle = color;
ctx.fill();
ctx.font = "700 34px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.fillStyle = "#eaf6ff";
ctx.fillText(text, 54, 80);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.8, 0.7, 1);
sprite.renderOrder = 20;
return sprite;
}
const labels = [
{ sprite: makeLabel("A / SINE WAVE", "#54d8ff"), y: 3.9 },
{ sprite: makeLabel("B / SINE WAVE", "#ffb45f"), y: 1.15 },
{ sprite: makeLabel("SUM / A + B", "#a7ef75"), y: -2.35 }
];
labels.forEach(({ sprite, y }) => {
sprite.position.set(7.35, y, 1.0);
scene.add(sprite);
});
// Baseline references.
function addBaseline(y, color, opacity) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(X_MIN - 0.35, y, -0.88),
new THREE.Vector3(X_MAX + 0.35, y, -0.88)
]);
const line = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.12,
gapSize: 0.1
})
);
line.computeLineDistances();
scene.add(line);
}
addBaseline(3.9, COLORS.a, 0.2);
addBaseline(1.15, COLORS.b, 0.2);
addBaseline(-2.35, COLORS.sum, 0.16);
// Vertical guides place every marker in a shared X column.
for (let i = 0; i < SAMPLE_COUNT; i++) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(sampleX[i], -4.3, -0.88),
new THREE.Vector3(sampleX[i], 5.1, -0.88)
]);
const guide = new THREE.Line(
geometry,
new THREE.LineDashedMaterial({
color: i % 4 === 0 ? "#7fa6be" : "#52738b",
transparent: true,
opacity: i % 4 === 0 ? 0.25 : 0.13,
dashSize: 0.08,
gapSize: 0.105,
depthWrite: false
})
);
guide.computeLineDistances();
scene.add(guide);
}
// Bottom sample rail and tick marks.
const railVertices = [
X_MIN - 0.45, -4.55, 0.78,
X_MAX + 0.45, -4.55, 0.78
];
sampleX.forEach((x) => {
railVertices.push(
x, -4.69, 0.78,
x, -4.40, 0.78
);
});
const railGeometry = new THREE.BufferGeometry();
railGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(railVertices, 3)
);
const rail = new THREE.LineSegments(
railGeometry,
new THREE.LineBasicMaterial({
color: "#7597ad",
transparent: true,
opacity: 0.48
})
);
scene.add(rail);
// Dynamic low-poly ribbon construction.
function createDynamicGeometry(vertexCount) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(vertexCount * 3), 3)
);
return geometry;
}
function createRibbon(baseY, color) {
const segmentCount = SAMPLE_COUNT - 1;
const topGeometry = createDynamicGeometry(segmentCount * 6);
const frontGeometry = createDynamicGeometry(segmentCount * 6);
const backGeometry = createDynamicGeometry(segmentCount * 6);
const bottomGeometry = createDynamicGeometry(segmentCount * 6);
const topMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color.clone().multiplyScalar(0.045),
roughness: 0.58,
metalness: 0.08,
flatShading: true,
side: THREE.DoubleSide
});
const sideMaterial = new THREE.MeshStandardMaterial({
color: color.clone().multiplyScalar(0.48),
emissive: color.clone().multiplyScalar(0.025),
roughness: 0.74,
metalness: 0.05,
flatShading: true,
side: THREE.DoubleSide
});
const topMesh = new THREE.Mesh(topGeometry, topMaterial);
const frontMesh = new THREE.Mesh(frontGeometry, sideMaterial);
const backMesh = new THREE.Mesh(backGeometry, sideMaterial);
const bottomMesh = new THREE.Mesh(bottomGeometry, sideMaterial);
[topMesh, frontMesh, backMesh, bottomMesh].forEach((mesh) => {
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
});
const edgeGeometry = new THREE.BufferGeometry();
edgeGeometry.setAttribute(
"position",
new THREE.BufferAttribute(
new Float32Array(SAMPLE_COUNT * 3),
3
)
);
const edge = new THREE.Line(
edgeGeometry,
new THREE.LineBasicMaterial({
color: color.clone().lerp(new THREE.Color("#ffffff"), 0.52),
transparent: true,
opacity: 0.82
})
);
scene.add(edge);
return {
baseY,
color,
amplitude: AMPLITUDE,
values: new Array(SAMPLE_COUNT).fill(0),
topGeometry,
frontGeometry,
backGeometry,
bottomGeometry,
edgeGeometry,
topMesh,
frontMesh,
backMesh,
bottomMesh,
edge
};
}
const ribbons = [
createRibbon(3.9, COLORS.a),
createRibbon(1.15, COLORS.b),
createRibbon(-2.35, COLORS.sum)
];
function putVertex(array, offset, x, y, z) {
array[offset] = x;
array[offset + 1] = y;
array[offset + 2] = z;
}
function updateRibbonGeometry(ribbon) {
const {
values,
baseY,
amplitude,
topGeometry,
frontGeometry,
backGeometry,
bottomGeometry,
edgeGeometry
} = ribbon;
const top = topGeometry.attributes.position.array;
const front = frontGeometry.attributes.position.array;
const back = backGeometry.attributes.position.array;
const bottom = bottomGeometry.attributes.position.array;
const edge = edgeGeometry.attributes.position.array;
const halfWidth = RIBBON_WIDTH * 0.5;
const frontZ = halfWidth;
const backZ = -halfWidth;
const thickness = RIBBON_THICKNESS;
for (let i = 0; i < SAMPLE_COUNT; i++) {
const x = sampleX[i];
const y = baseY + values[i] * amplitude;
putVertex(edge, i * 3, x, y, frontZ + 0.012);
}
for (let i = 0; i < SAMPLE_COUNT - 1; i++) {
const x0 = sampleX[i];
const x1 = sampleX[i + 1];
const y0 = baseY + values[i] * amplitude;
const y1 = baseY + values[i + 1] * amplitude;
let offset = i * 18;
// Top face.
putVertex(top, offset, x0, y0, frontZ);
putVertex(top, offset + 3, x1, y1, frontZ);
putVertex(top, offset + 6, x1, y1, backZ);
offset += 9;
putVertex(top, offset, x0, y0, frontZ);
putVertex(top, offset + 3, x1, y1, backZ);
putVertex(top, offset + 6, x0, y0, backZ);
offset += 9;
offset = i * 18;
// Front face.
putVertex(front, offset, x0, y0, frontZ);
putVertex(front, offset + 3, x0, y0 - thickness, frontZ);
putVertex(front, offset + 6, x1, y1 - thickness, frontZ);
offset += 9;
putVertex(front, offset, x0, y0, frontZ);
putVertex(front, offset + 3, x1, y1 - thickness, frontZ);
putVertex(front, offset + 6, x1, y1, frontZ);
offset += 9;
offset = i * 18;
// Back face.
putVertex(back, offset, x0, y0, backZ);
putVertex(back, offset + 3, x1, y1, backZ);
putVertex(back, offset + 6, x1, y1 - thickness, backZ);
offset += 9;
putVertex(back, offset, x0, y0, backZ);
putVertex(back, offset + 3, x1, y1 - thickness, backZ);
putVertex(back, offset + 6, x0, y0 - thickness, backZ);
offset += 9;
offset = i * 18;
// Bottom face.
putVertex(bottom, offset, x0, y0 - thickness, frontZ);
putVertex(bottom, offset + 3, x1, y1 - thickness, frontZ);
putVertex(bottom, offset + 6, x1, y1 - thickness, backZ);
offset += 9;
putVertex(bottom, offset, x0, y0 - thickness, frontZ);
putVertex(bottom, offset + 3, x1, y1 - thickness, backZ);
putVertex(bottom, offset + 6, x0, y0 - thickness, backZ);
offset += 9;
}
[
topGeometry,
frontGeometry,
backGeometry,
bottomGeometry
].forEach((geometry) => {
geometry.attributes.position.needsUpdate = true;
geometry.computeVertexNormals();
});
edgeGeometry.attributes.position.needsUpdate = true;
}
// Octahedral station markers.
const markerGeometry = new THREE.OctahedronGeometry(0.078, 0);
const markerMaterials = {
a: new THREE.MeshStandardMaterial({
color: "#b8f2ff",
emissive: COLORS.a,
emissiveIntensity: 0.55,
roughness: 0.42,
flatShading: true
}),
b: new THREE.MeshStandardMaterial({
color: "#ffe0b5",
emissive: COLORS.b,
emissiveIntensity: 0.55,
roughness: 0.42,
flatShading: true
}),
sum: new THREE.MeshStandardMaterial({
color: "#dcffc5",
emissive: COLORS.sum,
emissiveIntensity: 0.58,
roughness: 0.42,
flatShading: true
})
};
const markerSets = [];
const hitTargets = [];
const hitGeometry = new THREE.SphereGeometry(0.17, 6, 4);
const hitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
ribbons.forEach((ribbon, ribbonIndex) => {
const key = ribbonIndex === 0 ? "a" : ribbonIndex === 1 ? "b" : "sum";
const markers = [];
const hits = [];
for (let i = 0; i < SAMPLE_COUNT; i++) {
const marker = new THREE.Mesh(
markerGeometry,
markerMaterials[key]
);
marker.castShadow = true;
scene.add(marker);
markers.push(marker);
const hit = new THREE.Mesh(hitGeometry, hitMaterial);
hit.userData.sample = i;
scene.add(hit);
hits.push(hit);
hitTargets.push(hit);
}
markerSets.push({ key, markers, hits });
});
// Selected-station rings.
const ringGeometry = new THREE.RingGeometry(0.115, 0.16, 20);
const selectedRings = [];
ribbons.forEach((ribbon, index) => {
const material = new THREE.MeshBasicMaterial({
color: ribbon.color.clone().lerp(new THREE.Color("#ffffff"), 0.65),
transparent: true,
opacity: 0.8,
side: THREE.DoubleSide,
depthTest: false,
toneMapped: false
});
const ring = new THREE.Mesh(ringGeometry, material);
ring.renderOrder = 15;
scene.add(ring);
selectedRings.push(ring);
});
const selectedGuideGeometry = new THREE.BufferGeometry();
selectedGuideGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const selectedGuide = new THREE.Line(
selectedGuideGeometry,
new THREE.LineDashedMaterial({
color: "#e7f8ff",
transparent: true,
opacity: 0.55,
dashSize: 0.11,
gapSize: 0.075,
depthTest: false
})
);
selectedGuide.renderOrder = 12;
scene.add(selectedGuide);
function signed(value, digits = 2) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(digits)}`;
}
function updateReadout() {
const a = ribbons[0].values[selectedSample];
const b = ribbons[1].values[selectedSample];
const sum = ribbons[2].values[selectedSample];
const x = sampleX[selectedSample];
ui.valueA.textContent = signed(a);
ui.valueB.textContent = signed(b);
ui.valueSum.textContent = signed(sum);
ui.sampleTag.textContent =
`Sample ${String(selectedSample + 1).padStart(2, "0")} / ${SAMPLE_COUNT}`;
ui.xValue.textContent = `x = ${signed(x)}`;
}
function setSelectedSample(index) {
selectedSample = THREE.MathUtils.clamp(
Math.round(index),
0,
SAMPLE_COUNT - 1
);
ui.sampleSlider.value = String(selectedSample);
updateReadout();
}
function updateWaveScene(time) {
const frequency = 0.78;
for (let i = 0; i < SAMPLE_COUNT; i++) {
const x = sampleX[i];
const a = Math.sin(x * frequency + phase);
const b = Math.sin(x * frequency + phase + 2.05);
const sum = a + b;
ribbons[0].values[i] = a;
ribbons[1].values[i] = b;
ribbons[2].values[i] = sum;
ribbons.forEach((ribbon, ribbonIndex) => {
const y =
ribbon.baseY + ribbon.values[i] * ribbon.amplitude;
const marker = markerSets[ribbonIndex].markers[i];
const hit = markerSets[ribbonIndex].hits[i];
marker.position.set(x, y, RIBBON_WIDTH * 0.5 + 0.045);
hit.position.copy(marker.position);
});
}
ribbons.forEach(updateRibbonGeometry);
const selectedX = sampleX[selectedSample];
selectedRings.forEach((ring, ribbonIndex) => {
const ribbon = ribbons[ribbonIndex];
const marker = markerSets[ribbonIndex].markers[selectedSample];
ring.position.copy(marker.position);
ring.position.z += 0.018;
const pulse = 1 + Math.sin(time * 4.2) * 0.1;
ring.scale.setScalar(pulse);
ring.material.opacity = 0.64 + Math.sin(time * 4.2) * 0.16;
});
const selectedArray =
selectedGuideGeometry.attributes.position.array;
const topY =
ribbons[0].baseY +
ribbons[0].values[selectedSample] * ribbons[0].amplitude;
const bottomY =
ribbons[2].baseY +
ribbons[2].values[selectedSample] * ribbons[2].amplitude;
putVertex(selectedArray, 0, selectedX, topY, 0.93);
putVertex(selectedArray, 3, selectedX, bottomY, 0.93);
selectedGuideGeometry.attributes.position.needsUpdate = true;
selectedGuide.computeLineDistances();
ui.phaseValue.textContent = `${phase.toFixed(2)} rad`;
updateReadout();
}
ui.sampleSlider.addEventListener("input", (event) => {
setSelectedSample(Number(event.target.value));
});
function updatePlayButton() {
ui.playToggle.textContent = playing ? "Pause wave" : "Play wave";
ui.playToggle.setAttribute("aria-pressed", String(!playing));
}
ui.playToggle.addEventListener("click", () => {
playing = !playing;
updatePlayButton();
});
updatePlayButton();
// Direct camera controls, implemented without external addons.
const pointer = {
down: false,
startX: 0,
startY: 0,
previousX: 0,
previousY: 0,
moved: false
};
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function getPointerNDC(event) {
const rect = canvas.getBoundingClientRect();
pointerNDC.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function inspectPointer(event) {
getPointerNDC(event);
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(hitTargets, false);
if (hits.length > 0) {
setSelectedSample(hits[0].object.userData.sample);
}
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.down = true;
pointer.moved = false;
pointer.startX = event.clientX;
pointer.startY = event.clientY;
pointer.previousX = event.clientX;
pointer.previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.down) {
inspectPointer(event);
return;
}
const dx = event.clientX - pointer.previousX;
const dy = event.clientY - pointer.previousY;
pointer.previousX = event.clientX;
pointer.previousY = event.clientY;
const totalDistance = Math.hypot(
event.clientX - pointer.startX,
event.clientY - pointer.startY
);
if (totalDistance > 4) pointer.moved = true;
if (pointer.moved) {
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.004,
0.72,
1.67
);
updateCamera();
}
});
function releasePointer(event) {
if (!pointer.down) return;
if (!pointer.moved) inspectPointer(event);
pointer.down = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
12,
29
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
playing = !playing;
updatePlayButton();
}
if (event.key === "ArrowRight") {
setSelectedSample(selectedSample + 1);
}
if (event.key === "ArrowLeft") {
setSelectedSample(selectedSample - 1);
}
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
camera.aspect = aspect;
camera.fov = aspect < 0.8 ? 52 : 42;
if (!adjustedForPortrait && aspect < 0.8) {
cameraRadius = 25;
adjustedForPortrait = true;
}
camera.updateProjectionMatrix();
updateCamera();
labels.forEach(({ sprite }) => {
sprite.visible = aspect > 0.72;
});
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (playing) {
phase = (phase + delta * 0.55) % (Math.PI * 2);
}
updateWaveScene(elapsed);
renderer.render(scene, camera);
}
updateWaveScene(0);
renderer.render(scene, camera);
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```