Prompt specification
Build or customize the Three.js 3D scene "Damped and lightly damped resonance curves" 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: Damped and lightly damped resonance curves
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/damped-and-lightly-damped-resonance-curves-x1578
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows resonance in a driven oscillator. Place two matched oscillators beside their amplitude-versus-frequency curves; compare peak width and height with different damping while keeping all other parameters fixed. Stylized solid forms.
Working Three.js Starter (damped-and-lightly-damped-resonance-curves-x1578):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Resonance Lab — Driven Oscillator</title>
<style>
:root {
--ink: #202532;
--muted: #6f7580;
--paper: #f3f0e9;
--glass: rgba(255, 255, 255, 0.84);
--line: rgba(32, 37, 50, 0.12);
--blue: #356ae6;
--coral: #df604b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
}
.topbar {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--blue);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 680px;
font-size: clamp(25px, 3.2vw, 46px);
line-height: 1.04;
letter-spacing: -0.045em;
}
.intro {
margin: 10px 0 0;
max-width: 590px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.legend {
display: grid;
gap: 8px;
min-width: 240px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--glass);
box-shadow: 0 12px 35px rgba(32, 37, 50, 0.08);
backdrop-filter: blur(14px);
}
.legend-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
font-size: 12px;
}
.legend-name {
display: flex;
align-items: center;
gap: 8px;
font-weight: 750;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
}
.swatch.blue {
background: var(--blue);
}
.swatch.coral {
background: var(--coral);
}
.legend-value {
color: var(--muted);
font-variant-numeric: tabular-nums;
}
.legend-note {
padding-top: 7px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.dock {
position: fixed;
z-index: 8;
left: 50%;
bottom: 18px;
width: min(1120px, calc(100% - 32px));
transform: translateX(-50%);
display: grid;
grid-template-columns: 1.35fr 1fr 1fr auto;
align-items: center;
gap: 22px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 20px;
background: var(--glass);
box-shadow: 0 18px 60px rgba(32, 37, 50, 0.14);
backdrop-filter: blur(18px);
}
.control {
min-width: 0;
}
.control-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
.control label {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
output {
color: var(--muted);
font-size: 12px;
font-weight: 700;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0;
border-radius: 99px;
appearance: none;
background: #dfe2e7;
cursor: pointer;
accent-color: var(--blue);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid white;
border-radius: 50%;
appearance: none;
background: var(--blue);
box-shadow: 0 2px 7px rgba(32, 37, 50, 0.24);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid white;
border-radius: 50%;
background: var(--blue);
box-shadow: 0 2px 7px rgba(32, 37, 50, 0.24);
}
#heavyDamping {
accent-color: var(--coral);
}
#heavyDamping::-webkit-slider-thumb {
background: var(--coral);
}
#heavyDamping::-moz-range-thumb {
background: var(--coral);
}
.fixed {
padding-left: 18px;
border-left: 1px solid var(--line);
}
.fixed-title {
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.fixed-values {
display: flex;
flex-wrap: wrap;
gap: 6px 12px;
color: var(--ink);
font-size: 11px;
font-weight: 700;
white-space: nowrap;
}
.actions {
display: flex;
gap: 7px;
}
button {
height: 37px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: white;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: #f7f8fa;
}
button:active {
transform: translateY(0);
}
.help {
position: fixed;
z-index: 6;
right: 24px;
bottom: 128px;
color: rgba(32, 37, 50, 0.52);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 900px) {
.topbar {
top: 18px;
left: 18px;
right: 18px;
}
.intro,
.legend {
display: none;
}
.dock {
grid-template-columns: 1fr 1fr;
gap: 12px 18px;
padding: 13px 15px;
}
.frequency-control {
grid-column: 1 / -1;
}
.fixed {
padding-left: 0;
border-left: 0;
}
.help {
display: none;
}
}
@media (max-width: 560px) {
h1 {
max-width: 310px;
font-size: 27px;
}
.dock {
grid-template-columns: 1fr 1fr;
bottom: 10px;
width: calc(100% - 20px);
}
.actions,
.fixed {
grid-column: 1 / -1;
}
.actions button {
flex: 1;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive resonance comparison"></canvas>
<header class="topbar">
<div>
<div class="eyebrow">Driven System / Resonance Lab</div>
<h1>Same oscillator.<br>Different damping.</h1>
<p class="intro">
Two matched mass–spring systems respond to the same sinusoidal drive.
Move through frequency to see how damping changes resonance height and width.
</p>
</div>
<aside class="legend" aria-label="Comparison legend">
<div class="legend-row">
<span class="legend-name">
<span class="swatch blue"></span>
Light damping
</span>
<span class="legend-value" id="lightLegend">c = 0.80 kg/s</span>
</div>
<div class="legend-row">
<span class="legend-name">
<span class="swatch coral"></span>
Heavy damping
</span>
<span class="legend-value" id="heavyLegend">c = 3.20 kg/s</span>
</div>
<div class="legend-note">
Curves share one vertical scale. The dark bracket on each curve marks its
3 dB bandwidth: narrow and tall versus broad and short.
</div>
</aside>
</header>
<section class="dock" aria-label="Simulation controls">
<div class="control frequency-control">
<div class="control-head">
<label for="frequency">Drive frequency</label>
<output id="frequencyOutput" for="frequency">0.800 Hz</output>
</div>
<input id="frequency" type="range" min="0.25" max="1.25" step="0.001" value="0.8">
</div>
<div class="control">
<div class="control-head">
<label for="lightDamping">Light damping</label>
<output id="lightOutput" for="lightDamping">0.80 kg/s</output>
</div>
<input id="lightDamping" type="range" min="0.25" max="1.40" step="0.05" value="0.8">
</div>
<div class="control">
<div class="control-head">
<label for="heavyDamping">Heavy damping</label>
<output id="heavyOutput" for="heavyDamping">3.20 kg/s</output>
</div>
<input id="heavyDamping" type="range" min="2.00" max="5.00" step="0.10" value="3.2">
</div>
<div class="fixed">
<div class="fixed-title">Fixed parameters</div>
<div class="fixed-values">
<span>m = 1.00 kg</span>
<span>k = 25 N/m</span>
<span>F₀ = 1.00 N</span>
</div>
</div>
<div class="actions">
<button id="pauseButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset</button>
</div>
</section>
<div class="help">Drag to orbit · scroll to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Physical model
// ---------------------------------------------------------------------
const MASS = 1.0;
const SPRING_K = 25.0;
const FORCE_AMPLITUDE = 1.0;
const NATURAL_FREQUENCY = Math.sqrt(SPRING_K / MASS) / (2 * Math.PI);
const FREQUENCY_MAX = 1.4;
const RESPONSE_MAX = 7.0;
const VISUAL_DISPLACEMENT_SCALE = 1.25;
const palette = {
paper: 0xf3f0e9,
ground: 0xe9e4da,
board: 0xfffefb,
ink: 0x273040,
muted: 0x8a909a,
grid: 0xdde1e6,
blue: 0x356ae6,
coral: 0xdf604b,
metal: 0x394454,
metalDark: 0x222a36
};
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(palette.paper);
scene.fog = new THREE.Fog(palette.paper, 22, 38);
const camera = new THREE.OrthographicCamera(-7, 7, 4.2, -4.2, 0.1, 80);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0xb8ad9c, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8ccff, 1.1);
fillLight.position.set(7, 5, 3);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 28),
new THREE.MeshStandardMaterial({
color: palette.ground,
roughness: 0.96
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Geometry and label helpers
// ---------------------------------------------------------------------
const Y_AXIS = new THREE.Vector3(0, 1, 0);
const tempA = new THREE.Vector3();
const tempB = new THREE.Vector3();
const tempMid = new THREE.Vector3();
const tempDirection = new THREE.Vector3();
function solidMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.68,
metalness: options.metalness ?? 0.04,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 0
});
}
function addBox(parent, width, height, depth, color, x, y, z, options = {}) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = options.material ?? solidMaterial(color, options);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
parent.add(mesh);
if (options.edges) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
new THREE.LineBasicMaterial({
color: options.edgeColor ?? 0x273040,
transparent: true,
opacity: 0.18
})
);
mesh.add(edge);
}
return mesh;
}
function addCylinder(parent, radius, color, options = {}) {
const geometry = new THREE.CylinderGeometry(
radius,
radius,
1,
options.segments ?? 16
);
const mesh = new THREE.Mesh(
geometry,
options.material ?? solidMaterial(color, options)
);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function alignSegment(mesh, a, b) {
tempDirection.subVectors(b, a);
const length = tempDirection.length();
tempMid.addVectors(a, b).multiplyScalar(0.5);
mesh.position.copy(tempMid);
mesh.scale.set(1, length, 1);
if (length > 0.00001) {
mesh.quaternion.setFromUnitVectors(
Y_AXIS,
tempDirection.normalize()
);
}
}
function makeLine(points, color, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const line = new THREE.Line(geometry, material);
return line;
}
function makeTitleSprite(title, subtitle, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 1024;
labelCanvas.height = 220;
const context = labelCanvas.getContext("2d");
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
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(4.0, 4.0 * 220 / 1024, 1);
sprite.renderOrder = 20;
function update(nextTitle, nextSubtitle) {
context.clearRect(0, 0, labelCanvas.width, labelCanvas.height);
context.fillStyle = accent;
context.font = "800 55px system-ui, sans-serif";
context.textBaseline = "top";
context.fillText(nextTitle, 10, 8);
context.fillStyle = "#69707c";
context.font = "600 29px system-ui, sans-serif";
context.fillText(nextSubtitle, 12, 94);
texture.needsUpdate = true;
}
update(title, subtitle);
sprite.userData.update = update;
return sprite;
}
function makeSmallLabel(text, color = "#69707c", width = 0.30) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 256;
labelCanvas.height = 96;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 256, 96);
context.fillStyle = color;
context.font = "700 39px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 48);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, width * 96 / 256, 1);
sprite.renderOrder = 18;
return sprite;
}
// ---------------------------------------------------------------------
// Frequency-response graph
// ---------------------------------------------------------------------
class ResponseGraph {
constructor(x, accentHex, titleText) {
this.group = new THREE.Group();
this.group.position.set(x, 2.48, 0);
scene.add(this.group);
this.accent = new THREE.Color(accentHex);
this.accentHex = accentHex;
this.titleText = titleText;
this.c = 0.8;
this.width = 2.66;
this.height = 2.30;
this.originX = -1.33;
this.originY = -1.15;
this.samples = 240;
this.board = addBox(
this.group,
2.96,
2.76,
0.14,
palette.board,
0,
0,
-0.11,
{
roughness: 0.86,
castShadow: true,
edges: true,
edgeColor: 0x8d929b
}
);
// Solid support and foot.
addBox(
this.group,
0.16,
1.04,
0.16,
0xb7bcc4,
0,
-1.86,
-0.13,
{ roughness: 0.72 }
);
addBox(
this.group,
0.78,
0.09,
0.54,
0xc5c8ce,
0,
-2.38,
-0.11,
{ roughness: 0.8 }
);
this.makeAxes();
this.title = makeTitleSprite(
titleText,
"Updating response…",
accentHex
);
this.title.position.set(0, 1.72, 0.45);
this.group.add(this.title);
this.makeCurveObjects();
}
makeAxes() {
const x0 = this.originX;
const y0 = this.originY;
const xTicks = [0, 0.4, 0.8, 1.2];
const yTicks = [0, 2, 4, 6];
for (const tick of xTicks) {
const px = x0 + tick / FREQUENCY_MAX * this.width;
const grid = makeLine(
[
new THREE.Vector3(px, y0, 0.005),
new THREE.Vector3(px, y0 + this.height, 0.005)
],
palette.grid,
0.8
);
this.group.add(grid);
const label = makeSmallLabel(tick.toFixed(1), "#707783", 0.25);
label.position.set(px, y0 - 0.18, 0.08);
this.group.add(label);
}
for (const tick of yTicks) {
const py = y0 + tick / RESPONSE_MAX * this.height;
const grid = makeLine(
[
new THREE.Vector3(x0, py, 0.005),
new THREE.Vector3(x0 + this.width, py, 0.005)
],
palette.grid,
0.85
);
this.group.add(grid);
const label = makeSmallLabel(String(tick), "#707783", 0.23);
label.position.set(x0 - 0.13, py, 0.08);
this.group.add(label);
}
this.group.add(
makeLine(
[
new THREE.Vector3(x0, y0, 0.015),
new THREE.Vector3(x0 + this.width, y0, 0.015)
],
palette.ink,
0.88
)
);
this.group.add(
makeLine(
[
new THREE.Vector3(x0, y0, 0.015),
new THREE.Vector3(x0, y0 + this.height, 0.015)
],
palette.ink,
0.88
)
);
const frequencyLabel = makeSmallLabel(
"DRIVE FREQUENCY (Hz)",
"#59616e",
1.03
);
frequencyLabel.position.set(x0 + this.width / 2, y0 - 0.46, 0.09);
this.group.add(frequencyLabel);
const amplitudeLabel = makeSmallLabel(
"RELATIVE AMPLITUDE",
"#59616e",
1.10
);
amplitudeLabel.position.set(x0 - 0.38, y0 + this.height / 2, 0.09);
amplitudeLabel.rotation.z = Math.PI / 2;
this.group.add(amplitudeLabel);
}
makeCurveObjects() {
const linePositions = new Float32Array(this.samples * 3);
const lineGeometry = new THREE.BufferGeometry();
lineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(linePositions, 3)
);
this.curve = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color: this.accent,
linewidth: 1
})
);
this.curve.frustumCulled = false;
this.curve.renderOrder = 6;
this.group.add(this.curve);
const fillPositions = new Float32Array(this.samples * 2 * 3);
const fillIndices = [];
for (let i = 0; i < this.samples - 1; i++) {
const a = i * 2;
fillIndices.push(a, a + 1, a + 2);
fillIndices.push(a + 1, a + 3, a + 2);
}
const fillGeometry = new THREE.BufferGeometry();
fillGeometry.setAttribute(
"position",
new THREE.BufferAttribute(fillPositions, 3)
);
fillGeometry.setIndex(fillIndices);
this.fill = new THREE.Mesh(
fillGeometry,
new THREE.MeshBasicMaterial({
color: this.accent,
transparent: true,
opacity: 0.09,
side: THREE.DoubleSide,
depthWrite: false
})
);
this.fill.frustumCulled = false;
this.fill.renderOrder = 4;
this.group.add(this.fill);
this.bandGeometry = new THREE.BufferGeometry();
this.bandGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(18), 3)
);
this.band = new THREE.Line(
this.bandGeometry,
new THREE.LineBasicMaterial({
color: 0x59616e,
transparent: true,
opacity: 0.9
})
);
this.band.frustumCulled = false;
this.band.renderOrder = 8;
this.group.add(this.band);
this.peakDot = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 16, 12),
new THREE.MeshBasicMaterial({ color: this.accent })
);
this.peakDot.renderOrder = 9;
this.group.add(this.peakDot);
this.currentGeometry = new THREE.BufferGeometry();
this.currentGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
this.currentLine = new THREE.Line(
this.currentGeometry,
new THREE.LineBasicMaterial({
color: this.accent,
transparent: true,
opacity: 0.58
})
);
this.currentLine.frustumCulled = false;
this.currentLine.renderOrder = 7;
this.group.add(this.currentLine);
this.currentDot = new THREE.Mesh(
new THREE.SphereGeometry(0.048, 16, 12),
new THREE.MeshBasicMaterial({ color: this.accent })
);
this.currentDot.renderOrder = 10;
this.group.add(this.currentDot);
}
response(frequency) {
const ratio = frequency / NATURAL_FREQUENCY;
const dampingTerm =
(this.c / Math.sqrt(MASS * SPRING_K)) * ratio;
return 1 / Math.sqrt(
Math.pow(1 - ratio * ratio, 2) +
Math.pow(dampingTerm, 2)
);
}
setDamping(value) {
this.c = value;
const values = [];
const curvePoints = [];
const fillPoints = [];
let peak = 0;
let peakFrequency = 0;
let peakIndex = 0;
for (let i = 0; i < this.samples; i++) {
const f = i / (this.samples - 1) * FREQUENCY_MAX;
const response = this.response(f);
values.push(response);
const px = this.originX + f / FREQUENCY_MAX * this.width;
const py = this.originY + response / RESPONSE_MAX * this.height;
curvePoints.push(px, py, 0.045);
fillPoints.push(
px, this.originY, 0.02,
px, py, 0.02
);
if (response > peak) {
peak = response;
peakFrequency = f;
peakIndex = i;
}
}
// Find the contiguous half-power region around the peak.
const threshold = peak / Math.SQRT2;
let lowIndex = peakIndex;
let highIndex = peakIndex;
while (
lowIndex > 0 &&
values[lowIndex - 1] >= threshold
) {
lowIndex--;
}
while (
highIndex < values.length - 1 &&
values[highIndex + 1] >= threshold
) {
highIndex++;
}
const lowEdge = this.interpolateBoundary(
lowIndex,
lowIndex > 0 ? lowIndex - 1 : 0,
values,
threshold
);
const highEdge = this.interpolateBoundary(
highIndex,
highIndex < values.length - 1 ? highIndex + 1 : values.length - 1,
values,
threshold
);
const bandwidth = Math.max(0, highEdge - lowEdge);
this.curve.geometry.attributes.position.array.set(curvePoints);
this.curve.geometry.attributes.position.needsUpdate = true;
this.fill.geometry.attributes.position.array.set(fillPoints);
this.fill.geometry.attributes.position.needsUpdate = true;
const bandY = this.originY + threshold / RESPONSE_MAX * this.height;
const bandX1 = this.originX + lowEdge / FREQUENCY_MAX * this.width;
const bandX2 = this.originX + highEdge / FREQUENCY_MAX * this.width;
const tick = 0.075;
this.bandGeometry.attributes.position.array.set([
bandX1, bandY - tick, 0.065,
bandX1, bandY + tick, 0.065,
bandX1, bandY, 0.065,
bandX2, bandY, 0.065,
bandX2, bandY - tick, 0.065,
bandX2, bandY + tick, 0.065
]);
this.bandGeometry.attributes.position.needsUpdate = true;
this.peakDot.position.set(
this.originX + peakFrequency / FREQUENCY_MAX * this.width,
this.originY + peak / RESPONSE_MAX * this.height,
0.09
);
this.peak = peak;
this.peakFrequency = peakFrequency;
this.bandwidth = bandwidth;
this.title.userData.update(
this.titleText,
`c ${value.toFixed(2)} kg/s • peak ${peak.toFixed(2)}× • 3 dB BW ${bandwidth.toFixed(2)} Hz`
);
}
interpolateBoundary(indexA, indexB, values, threshold) {
const a = values[indexA];
const b = values[indexB];
if (Math.abs(a - b) < 0.000001) {
return (indexA / (this.samples - 1)) * FREQUENCY_MAX;
}
const t = (threshold - a) / (b - a);
const index = indexA + t * (indexB - indexA);
return index / (this.samples - 1) * FREQUENCY_MAX;
}
updateCurrent(frequency) {
const response = this.response(frequency);
const px = this.originX + frequency / FREQUENCY_MAX * this.width;
const py = this.originY + response / RESPONSE_MAX * this.height;
this.currentGeometry.attributes.position.array.set([
px, this.originY, 0.075,
px, py, 0.075
]);
this.currentGeometry.attributes.position.needsUpdate = true;
this.currentDot.position.set(px, py, 0.11);
}
}
// ---------------------------------------------------------------------
// Stylized driven mass–spring–damper assembly
// ---------------------------------------------------------------------
class Oscillator {
constructor(x, accentHex) {
this.group = new THREE.Group();
this.group.position.set(x, 0, 0.25);
scene.add(this.group);
this.accent = new THREE.Color(accentHex);
this.massMaterial = solidMaterial(accentHex, {
roughness: 0.55,
metalness: 0.08,
emissive: accentHex,
emissiveIntensity: 0.06
});
addBox(
this.group,
3.10,
0.24,
1.76,
0xd8d4cc,
-1.55,
0.13,
0,
{
roughness: 0.88,
edges: true,
edgeColor: 0x555b63
}
);
addBox(
this.group,
0.27,
1.64,
0.82,
0x46505e,
-2.77,
1.01,
0,
{
roughness: 0.58,
edges: true
}
);
addBox(
this.group,
0.20,
1.52,
0.72,
0x46505e,
-0.12,
0.94,
0,
{
roughness: 0.58,
edges: true
}
);
this.mass = addBox(
this.group,
0.64,
0.64,
0.76,
accentHex,
-1.30,
1.08,
0,
{
material: this.massMaterial,
roughness: 0.5,
metalness: 0.08,
edges: true,
edgeColor: 0x202938
}
);
// Small inset on the mass gives it a solid, designed appearance.
addBox(
this.mass,
0.39,
0.34,
0.018,
0xffffff,
0,
0,
0.389,
{
material: solidMaterial(0xf7f9ff, {
roughness: 0.48,
metalness: 0.03
}),
castShadow: false
}
);
// Solid helical spring, assembled from short cylindrical segments.
this.springGeometry = new THREE.CylinderGeometry(
0.043,
0.043,
1,
9
);
this.springMaterial = solidMaterial(palette.metalDark, {
roughness: 0.46,
metalness: 0.35
});
this.springSegments = [];
this.springSegmentCount = 46;
for (let i = 0; i < this.springSegmentCount; i++) {
const segment = new THREE.Mesh(
this.springGeometry,
this.springMaterial
);
segment.castShadow = true;
this.group.add(segment);
this.springSegments.push(segment);
}
// Damper barrel and sliding rod.
this.damperBody = addCylinder(
this.group,
0.115,
0x737d8b,
{ roughness: 0.48, metalness: 0.30 }
);
alignSegment(
this.damperBody,
new THREE.Vector3(-2.55, 0.53, 0.25),
new THREE.Vector3(-1.92, 0.53, 0.25)
);
this.damperRod = addCylinder(
this.group,
0.047,
0xb8bec7,
{ roughness: 0.32, metalness: 0.55 }
);
// Drive actuator and wall.
this.actuatorBody = addCylinder(
this.group,
0.16,
0x596473,
{ roughness: 0.5, metalness: 0.2 }
);
alignSegment(
this.actuatorBody,
new THREE.Vector3(-0.72, 1.08, 0),
new THREE.Vector3(-0.39, 1.08, 0)
);
this.actuatorRod = addCylinder(
this.group,
0.058,
0xc1c6ce,
{ roughness: 0.3, metalness: 0.58 }
);
// Force-direction cone.
this.forceArrow = new THREE.Mesh(
new THREE.ConeGeometry(0.11, 0.28, 20),
solidMaterial(accentHex, {
roughness: 0.48,
emissive: accentHex,
emissiveIntensity: 0.08
})
);
this.forceArrow.rotation.z = Math.PI / 2;
this.forceArrow.position.set(-0.56, 1.53, 0.48);
this.forceArrow.castShadow = true;
this.group.add(this.forceArrow);
const arrowStem = addCylinder(
this.group,
0.035,
accentHex,
{
material: solidMaterial(accentHex, {
roughness: 0.48,
emissive: accentHex,
emissiveIntensity: 0.08
})
}
);
alignSegment(
arrowStem,
new THREE.Vector3(-1.03, 1.53, 0.48),
new THREE.Vector3(-0.63, 1.53, 0.48)
);
}
update(displacement, forcePhase) {
const massX = -1.30 + displacement;
this.mass.position.x = massX;
this.massMaterial.emissiveIntensity =
0.045 + 0.075 * (0.5 + 0.5 * Math.sin(forcePhase));
const springStart = new THREE.Vector3(
-2.62,
1.08,
0
);
const springEnd = new THREE.Vector3(
massX - 0.33,
1.08,
0
);
const length = springEnd.x - springStart.x;
const turns = 8.5;
const radius = 0.145;
for (let i = 0; i < this.springSegmentCount; i++) {
const t0 = i / this.springSegmentCount;
const t1 = (i + 1) / this.springSegmentCount;
tempA.set(
springStart.x + length * t0,
springStart.y + radius * Math.sin(turns * Math.PI * 2 * t0),
springStart.z + radius * Math.cos(turns * Math.PI * 2 * t0)
);
tempB.set(
springStart.x + length * t1,
springStart.y + radius * Math.sin(turns * Math.PI * 2 * t1),
springStart.z + radius * Math.cos(turns * Math.PI * 2 * t1)
);
alignSegment(this.springSegments[i], tempA, tempB);
}
alignSegment(
this.damperRod,
new THREE.Vector3(-1.92, 0.53, 0.25),
new THREE.Vector3(massX - 0.33, 0.53, 0.25)
);
alignSegment(
this.actuatorRod,
new THREE.Vector3(massX + 0.33, 1.08, 0),
new THREE.Vector3(-0.72, 1.08, 0)
);
this.forceArrow.position.y = 1.53 + 0.018 * Math.sin(forcePhase);
this.forceArrow.scale.setScalar(1 + 0.08 * Math.sin(forcePhase));
}
}
// ---------------------------------------------------------------------
// Scene assembly
// ---------------------------------------------------------------------
const lowGraph = new ResponseGraph(
-3.15 + 1.45,
"#356ae6",
"LIGHT DAMPING"
);
const heavyGraph = new ResponseGraph(
3.15 + 1.45,
"#df604b",
"HEAVY DAMPING"
);
const lowOscillator = new Oscillator(
-3.15 - 1.55,
"#356ae6"
);
const heavyOscillator = new Oscillator(
3.15 - 1.55,
"#df604b"
);
// ---------------------------------------------------------------------
// UI state
// ---------------------------------------------------------------------
const frequencyInput = document.querySelector("#frequency");
const lightInput = document.querySelector("#lightDamping");
const heavyInput = document.querySelector("#heavyDamping");
const frequencyOutput = document.querySelector("#frequencyOutput");
const lightOutput = document.querySelector("#lightOutput");
const heavyOutput = document.querySelector("#heavyOutput");
const lightLegend = document.querySelector("#lightLegend");
const heavyLegend = document.querySelector("#heavyLegend");
const pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
let frequency = Number(frequencyInput.value);
let lightDamping = Number(lightInput.value);
let heavyDamping = Number(heavyInput.value);
let running = true;
let phaseTime = 0;
function refreshDamping() {
lowGraph.setDamping(lightDamping);
heavyGraph.setDamping(heavyDamping);
lightOutput.textContent = `${lightDamping.toFixed(2)} kg/s`;
heavyOutput.textContent = `${heavyDamping.toFixed(2)} kg/s`;
lightLegend.textContent = `c = ${lightDamping.toFixed(2)} kg/s`;
heavyLegend.textContent = `c = ${heavyDamping.toFixed(2)} kg/s`;
}
frequencyInput.addEventListener("input", () => {
frequency = Number(frequencyInput.value);
frequencyOutput.textContent = `${frequency.toFixed(3)} Hz`;
});
lightInput.addEventListener("input", () => {
lightDamping = Number(lightInput.value);
refreshDamping();
});
heavyInput.addEventListener("input", () => {
heavyDamping = Number(heavyInput.value);
refreshDamping();
});
pauseButton.addEventListener("click", () => {
running = !running;
pauseButton.textContent = running ? "Pause" : "Play";
});
resetButton.addEventListener("click", () => {
frequencyInput.value = "0.8";
lightInput.value = "0.8";
heavyInput.value = "3.2";
frequency = 0.8;
lightDamping = 0.8;
heavyDamping = 3.2;
phaseTime = 0;
frequencyOutput.textContent = "0.800 Hz";
refreshDamping();
resetCamera();
});
refreshDamping();
// ---------------------------------------------------------------------
// Direct camera controls: orbit, pan-free drag, and wheel zoom
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.72, 0);
const cameraState = {
theta: 0,
phi: 1.12,
radius: 16,
targetTheta: 0,
targetPhi: 1.12,
targetRadius: 16
};
function resetCamera() {
cameraState.targetTheta = 0;
cameraState.targetPhi = 1.12;
cameraState.targetRadius = 16;
}
function updateCamera() {
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * 0.12;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * 0.12;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * 0.12;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y +
cameraState.radius * cosPhi,
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + dy * 0.005,
0.62,
1.38
);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined &&
canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
11,
22
);
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Responsive camera and animation loop
// ---------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
// Keep both experiment panels visible, including on narrow displays.
const viewHeight = Math.max(8.3, 13.0 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const omega = 2 * Math.PI * frequency;
if (running) {
phaseTime += omega * dt;
}
lowGraph.updateCurrent(frequency);
heavyGraph.updateCurrent(frequency);
const lightResponse = lowGraph.response(frequency);
const heavyResponse = heavyGraph.response(frequency);
const lightDisplacement =
FORCE_AMPLITUDE / SPRING_K *
lightResponse *
VISUAL_DISPLACEMENT_SCALE *
Math.sin(phaseTime);
const heavyDisplacement =
FORCE_AMPLITUDE / SPRING_K *
heavyResponse *
VISUAL_DISPLACEMENT_SCALE *
Math.sin(phaseTime);
const lightPhaseLag = Math.atan2(
lightDamping * omega,
SPRING_K - MASS * omega * omega
);
const heavyPhaseLag = Math.atan2(
heavyDamping * omega,
SPRING_K - MASS * omega * omega
);
// The physical positions use the same drive phase but each system has
// its own phase lag relative to the applied force.
lowOscillator.update(
lightDisplacement,
phaseTime
);
heavyOscillator.update(
heavyDisplacement,
phaseTime
);
// Small phase-specific visual feedback on the force arrows.
lowOscillator.forceArrow.position.x = -0.56;
heavyOscillator.forceArrow.position.x = -0.56;
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```