Prompt specification
Build or customize the Three.js 3D scene "Open and closed tube mode comparison" 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: Open and closed tube mode comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/open-and-closed-tube-mode-comparison-x1586
Scene Description Prompt:
Open and closed tube mode comparison. Place simplified air-column models side by side with pressure or displacement fields explicitly identified; avoid mixing their different boundary conditions or node labels. Please build this with Three.js from cdnjs in one self-contained HTML file, using stylized solid shapes.
Working Three.js Starter (open-and-closed-tube-mode-comparison-x1586):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Open vs. Closed Tube — Resonance Comparison</title>
<style>
:root {
--bg: #f4f1e9;
--panel: #fffdf8;
--ink: #20272a;
--muted: #687174;
--line: #d9d8d1;
--pressure-pos: #e76557;
--pressure-neg: #4f78df;
--displacement-pos: #159b88;
--displacement-neg: #eea23b;
--closed: #4e6662;
--open: #687c8c;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow-x: hidden;
}
button {
font: inherit;
}
.app {
width: min(1440px, 100%);
margin: 0 auto;
padding: 26px 28px 34px;
}
.masthead {
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 30px;
margin-bottom: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
border-radius: 2px;
background: var(--displacement-pos);
}
h1 {
margin: 0;
max-width: 760px;
font-size: clamp(30px, 4.2vw, 54px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.intro {
max-width: 560px;
margin: 13px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.mode-badge {
flex: 0 0 auto;
padding: 10px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 253, 248, 0.72);
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 12px;
padding: 9px 10px 9px 14px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(255, 253, 248, 0.78);
backdrop-filter: blur(12px);
}
.toolbar-group {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.control-label {
margin-right: 3px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.segmented {
display: flex;
gap: 4px;
padding: 4px;
border-radius: 12px;
background: #ebe9e2;
}
.segmented button,
.plain-button {
border: 0;
cursor: pointer;
transition:
color 160ms ease,
background 160ms ease,
transform 160ms ease,
box-shadow 160ms ease;
}
.segmented button {
padding: 8px 12px;
border-radius: 9px;
background: transparent;
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
.segmented button:hover {
color: var(--ink);
}
.segmented button[aria-pressed="true"] {
background: var(--panel);
color: var(--ink);
box-shadow: 0 2px 8px rgba(34, 42, 45, 0.1);
}
.plain-button {
display: inline-flex;
align-items: center;
gap: 7px;
min-height: 38px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 11px;
background: var(--panel);
color: var(--ink);
font-size: 12px;
font-weight: 750;
}
.plain-button:hover {
transform: translateY(-1px);
box-shadow: 0 5px 13px rgba(34, 42, 45, 0.08);
}
.button-icon {
font-size: 13px;
line-height: 1;
}
.viewport {
position: relative;
height: clamp(540px, 66vh, 760px);
min-height: 540px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 26px;
background:
radial-gradient(circle at 50% 40%, rgba(255, 255, 255, 0.88), transparent 47%),
#ece9e1;
box-shadow:
0 24px 70px rgba(47, 54, 54, 0.08),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
isolation: isolate;
}
.viewport canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
.viewport canvas:active {
cursor: grabbing;
}
.labels-layer {
position: absolute;
inset: 0;
z-index: 3;
overflow: hidden;
pointer-events: none;
}
.model-heading {
position: absolute;
top: 22px;
z-index: 4;
width: min(270px, 30%);
pointer-events: none;
}
.model-heading.closed {
left: 5%;
}
.model-heading.open {
right: 5%;
text-align: right;
}
.model-kicker {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.model-heading.open .model-kicker {
justify-content: flex-end;
}
.model-number {
display: grid;
width: 22px;
height: 22px;
place-items: center;
border: 1px solid var(--line);
border-radius: 50%;
background: rgba(255, 253, 248, 0.9);
color: var(--ink);
font-size: 9px;
letter-spacing: 0;
}
.model-heading h2 {
margin: 0;
font-size: clamp(18px, 2vw, 26px);
letter-spacing: -0.035em;
}
.model-heading p {
margin: 4px 0 0;
color: var(--muted);
font-size: 11px;
}
.anchor {
position: absolute;
left: 0;
top: 0;
pointer-events: none;
will-change: transform;
}
.endpoint {
width: 142px;
padding: 9px 10px;
border: 1px solid rgba(196, 198, 193, 0.9);
border-radius: 11px;
background: rgba(255, 253, 248, 0.9);
box-shadow: 0 8px 24px rgba(40, 48, 50, 0.09);
backdrop-filter: blur(8px);
}
.endpoint b {
display: block;
margin-bottom: 5px;
color: var(--ink);
font-size: 9px;
letter-spacing: 0.12em;
}
.endpoint span {
display: flex;
align-items: center;
gap: 6px;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
white-space: nowrap;
}
.field-icon {
display: inline-grid;
width: 15px;
height: 15px;
flex: 0 0 auto;
place-items: center;
border-radius: 4px;
color: white;
font-size: 8px;
font-style: normal;
font-weight: 900;
}
.field-icon.displacement {
background: var(--displacement-pos);
}
.field-icon.pressure {
background: var(--pressure-pos);
}
.legend {
position: absolute;
bottom: 18px;
left: 50%;
z-index: 5;
display: flex;
align-items: center;
gap: 17px;
max-width: calc(100% - 36px);
padding: 10px 14px;
border: 1px solid rgba(205, 205, 198, 0.9);
border-radius: 14px;
background: rgba(255, 253, 248, 0.88);
box-shadow: 0 8px 28px rgba(42, 49, 50, 0.08);
backdrop-filter: blur(10px);
transform: translateX(-50%);
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
white-space: nowrap;
transition: opacity 180ms ease;
}
.legend-item.dim {
opacity: 0.32;
}
.legend-swatch {
position: relative;
width: 29px;
height: 13px;
}
.legend-swatch.pressure::before,
.legend-swatch.pressure::after {
content: "";
position: absolute;
top: 1px;
width: 12px;
height: 11px;
border-radius: 4px;
}
.legend-swatch.pressure::before {
left: 0;
background: var(--pressure-pos);
}
.legend-swatch.pressure::after {
right: 0;
background: var(--pressure-neg);
}
.legend-swatch.displacement::before,
.legend-swatch.displacement::after {
content: "";
position: absolute;
top: 5px;
width: 18px;
height: 3px;
border-radius: 2px;
}
.legend-swatch.displacement::before {
left: 0;
background: var(--displacement-pos);
transform: rotate(-20deg);
transform-origin: left center;
}
.legend-swatch.displacement::after {
right: 0;
background: var(--displacement-neg);
transform: rotate(20deg);
transform-origin: right center;
}
.camera-hint {
position: absolute;
right: 17px;
bottom: 19px;
z-index: 4;
color: rgba(57, 66, 68, 0.68);
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
.status-pill {
position: absolute;
top: 22px;
left: 50%;
z-index: 4;
padding: 7px 10px;
border: 1px solid rgba(208, 207, 199, 0.88);
border-radius: 999px;
background: rgba(255, 253, 248, 0.76);
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
transform: translateX(-50%);
pointer-events: none;
}
.boundary-key {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
margin-top: 12px;
}
.key-card {
position: relative;
overflow: hidden;
padding: 17px 18px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(255, 253, 248, 0.76);
}
.key-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--closed);
}
.key-card.open::before {
background: var(--open);
}
.key-card h3 {
margin: 0 0 10px;
font-size: 14px;
letter-spacing: -0.015em;
}
.key-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 6px;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.key-row strong {
color: var(--ink);
font-weight: 750;
}
.key-note {
margin-top: 11px;
padding-top: 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
footer {
display: flex;
justify-content: space-between;
gap: 20px;
margin-top: 15px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
footer strong {
color: var(--ink);
}
@media (max-width: 780px) {
.app {
padding: 18px 14px 24px;
}
.masthead {
align-items: flex-start;
}
.mode-badge {
display: none;
}
.toolbar {
align-items: flex-start;
flex-direction: column;
}
.toolbar-group {
width: 100%;
justify-content: space-between;
}
.viewport {
height: 540px;
min-height: 540px;
}
.model-heading {
width: 42%;
}
.model-heading.closed {
left: 3%;
}
.model-heading.open {
right: 3%;
}
.model-heading p {
display: none;
}
.status-pill,
.endpoint,
.camera-hint {
display: none;
}
.legend {
width: calc(100% - 28px);
justify-content: center;
gap: 12px;
}
.legend-item {
font-size: 9px;
}
.boundary-key {
grid-template-columns: 1fr;
}
footer {
flex-direction: column;
gap: 7px;
}
}
</style>
</head>
<body>
<main class="app">
<header class="masthead">
<div>
<div class="eyebrow">Resonance field comparison</div>
<h1>The boundary sets the nodes.</h1>
<p class="intro">
Compare the fundamental standing-wave patterns of an open–open tube
and a closed–closed tube. Pressure and longitudinal displacement are
shown as separate, explicitly labeled fields.
</p>
</div>
<div class="mode-badge">Fundamental mode · equal visual length</div>
</header>
<nav class="toolbar" aria-label="Visualization controls">
<div class="toolbar-group">
<span class="control-label">Visible field</span>
<div class="segmented" role="group" aria-label="Choose visible field">
<button type="button" data-field="pressure" aria-pressed="false">
Pressure
</button>
<button type="button" data-field="displacement" aria-pressed="false">
Displacement
</button>
<button type="button" data-field="both" aria-pressed="true">
Both
</button>
</div>
</div>
<div class="toolbar-group">
<button class="plain-button" id="playButton" type="button">
<span class="button-icon" id="playIcon">Ⅱ</span>
<span id="playText">Pause</span>
</button>
<button class="plain-button" id="resetButton" type="button">
<span class="button-icon">⌖</span>
Reset view
</button>
</div>
</nav>
<section class="viewport" id="viewport" aria-label="Interactive 3D tube comparison">
<div class="status-pill" id="statusPill">Showing pressure + displacement</div>
<div class="model-heading closed">
<div class="model-kicker">
<span class="model-number">01</span>
Closed–closed
</div>
<h2>Sealed air column</h2>
<p>Both ends are rigid caps.</p>
</div>
<div class="model-heading open">
<div class="model-kicker">
Open–open
<span class="model-number">02</span>
</div>
<h2>Open air column</h2>
<p>Both ends vent to the surroundings.</p>
</div>
<div class="labels-layer" id="labelsLayer"></div>
<div class="legend" aria-label="Field legend">
<div class="legend-item" id="pressureLegend">
<span class="legend-swatch pressure"></span>
Signed pressure field
</div>
<div class="legend-item" id="displacementLegend">
<span class="legend-swatch displacement"></span>
Signed longitudinal displacement
</div>
</div>
<div class="camera-hint">Drag to orbit · wheel to zoom</div>
</section>
<section class="boundary-key" aria-label="Boundary condition key">
<article class="key-card closed">
<h3>Closed–closed tube</h3>
<div class="key-row">
<span class="field-icon displacement">D</span>
<span><strong>Sealed end:</strong> displacement node</span>
</div>
<div class="key-row">
<span class="field-icon pressure">P</span>
<span><strong>Sealed end:</strong> pressure antinode</span>
</div>
<div class="key-note">
At the center, the pattern swaps: displacement antinode and pressure node.
</div>
</article>
<article class="key-card open">
<h3>Open–open tube</h3>
<div class="key-row">
<span class="field-icon pressure">P</span>
<span><strong>Open end:</strong> pressure node</span>
</div>
<div class="key-row">
<span class="field-icon displacement">D</span>
<span><strong>Open end:</strong> displacement antinode</span>
</div>
<div class="key-note">
At the center, the pattern swaps: displacement node and pressure antinode.
</div>
</article>
</section>
<footer>
<div>
<strong>How to read it:</strong>
solid colored slices encode signed pressure; arrows encode signed
longitudinal displacement.
</div>
<div>
Positive and negative colors show opposite instantaneous phase.
</div>
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const labelsLayer = document.getElementById("labelsLayer");
const playButton = document.getElementById("playButton");
const playIcon = document.getElementById("playIcon");
const playText = document.getElementById("playText");
const resetButton = document.getElementById("resetButton");
const statusPill = document.getElementById("statusPill");
const pressureLegend = document.getElementById("pressureLegend");
const displacementLegend = document.getElementById("displacementLegend");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xece9e1);
scene.fog = new THREE.Fog(0xece9e1, 16, 31);
const camera = new THREE.PerspectiveCamera(
35,
viewport.clientWidth / viewport.clientHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(viewport.clientWidth, viewport.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xffffff,
0xb6b3aa,
2.15
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff9ec, 3.1);
keyLight.position.set(-4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xb8d9ef, 1.6);
rimLight.position.set(7, 3, -5);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xe7e3da,
roughness: 0.94,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.38;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Shared materials and geometries
// -------------------------------------------------------------------------
const pressurePositive = new THREE.Color(0xe76557);
const pressureNegative = new THREE.Color(0x4f78df);
const pressureNeutral = new THREE.Color(0xa8afb1);
const displacementPositive = new THREE.Color(0x159b88);
const displacementNegative = new THREE.Color(0xeea23b);
const tubeShellMaterial = new THREE.MeshPhysicalMaterial({
color: 0xcbd8da,
roughness: 0.25,
metalness: 0.04,
transparent: true,
opacity: 0.3,
side: THREE.DoubleSide,
depthWrite: false,
clearcoat: 0.75,
clearcoatRoughness: 0.28
});
const closedCapMaterial = new THREE.MeshStandardMaterial({
color: 0x536966,
roughness: 0.4,
metalness: 0.2
});
const openRimMaterial = new THREE.MeshStandardMaterial({
color: 0x728695,
roughness: 0.38,
metalness: 0.24
});
const axisMaterial = new THREE.MeshStandardMaterial({
color: 0x879295,
roughness: 0.55,
metalness: 0.15
});
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0xfffdf8,
roughness: 0.78,
metalness: 0
});
const pedestalEdgeMaterial = new THREE.LineBasicMaterial({
color: 0xbfc2be,
transparent: true,
opacity: 0.72
});
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0xa8b0ae,
roughness: 0.6,
metalness: 0.18
});
const arrowShaftGeometry = new THREE.CylinderGeometry(
0.034,
0.034,
1,
12
);
arrowShaftGeometry.rotateZ(-Math.PI / 2);
const arrowHeadGeometry = new THREE.ConeGeometry(
0.105,
0.22,
18
);
arrowHeadGeometry.rotateZ(-Math.PI / 2);
const arrowPositiveMaterial = new THREE.MeshStandardMaterial({
color: displacementPositive,
roughness: 0.36,
metalness: 0.05,
emissive: displacementPositive,
emissiveIntensity: 0.07
});
const arrowNegativeMaterial = new THREE.MeshStandardMaterial({
color: displacementNegative,
roughness: 0.36,
metalness: 0.05,
emissive: displacementNegative,
emissiveIntensity: 0.07
});
const pressureDiscGeometry = new THREE.CylinderGeometry(
0.34,
0.34,
0.055,
32
);
pressureDiscGeometry.rotateZ(Math.PI / 2);
const modelCenters = {
closed: -2.47,
open: 2.47
};
const tubeLength = 3.9;
// -------------------------------------------------------------------------
// Model construction
// -------------------------------------------------------------------------
function createPedestal(centerX) {
const pedestal = new THREE.Group();
const geometry = new THREE.BoxGeometry(4.23, 0.18, 1.47);
const slab = new THREE.Mesh(geometry, pedestalMaterial);
slab.position.set(centerX, -0.98, 0);
slab.castShadow = true;
slab.receiveShadow = true;
pedestal.add(slab);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
pedestalEdgeMaterial
);
edges.position.copy(slab.position);
pedestal.add(edges);
return pedestal;
}
function createTubeShell(group, type) {
const shell = new THREE.Mesh(
new THREE.CylinderGeometry(
0.49,
0.49,
tubeLength,
48,
1,
true
),
tubeShellMaterial
);
shell.rotation.z = Math.PI / 2;
shell.castShadow = true;
shell.renderOrder = 2;
group.add(shell);
const axis = new THREE.Mesh(
new THREE.CylinderGeometry(0.012, 0.012, tubeLength - 0.14, 8),
axisMaterial
);
axis.rotation.z = Math.PI / 2;
axis.position.y = -0.02;
group.add(axis);
if (type === "closed") {
for (const x of [-tubeLength / 2, tubeLength / 2]) {
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.51, 0.51, 0.16, 48),
closedCapMaterial
);
cap.rotation.z = Math.PI / 2;
cap.position.x = x;
cap.castShadow = true;
cap.receiveShadow = true;
group.add(cap);
const capEdge = new THREE.Mesh(
new THREE.TorusGeometry(0.49, 0.027, 10, 48),
openRimMaterial
);
capEdge.rotation.y = Math.PI / 2;
capEdge.position.x = x + (x < 0 ? 0.09 : -0.09);
group.add(capEdge);
}
} else {
for (const x of [-tubeLength / 2, tubeLength / 2]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.058, 12, 48),
openRimMaterial
);
rim.rotation.y = Math.PI / 2;
rim.position.x = x;
rim.castShadow = true;
group.add(rim);
}
}
for (const x of [-1.13, 1.13]) {
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.57, 0.18),
supportMaterial
);
support.position.set(x, -0.69, 0);
support.castShadow = true;
group.add(support);
}
}
function createModel(type) {
const centerX = modelCenters[type];
const group = new THREE.Group();
group.position.set(centerX, 0.14, 0);
scene.add(group);
createPedestal(centerX);
createTubeShell(group, type);
const pressureSamples = [];
const displacementSamples = [];
const sampleCount = 29;
// The air-column domain differs slightly:
// sealed caps bound the air just inside the tube ends.
const airHalfLength = type === "closed" ? 1.78 : 1.9;
for (let i = 0; i < sampleCount; i++) {
const s = i / (sampleCount - 1);
const x = -airHalfLength + 2 * airHalfLength * s;
/*
Fundamental field shapes:
Closed–closed:
displacement = sin(pi*s): nodes at sealed ends
pressure = cos(pi*s): antinodes at sealed ends
Open–open:
displacement = cos(pi*s): antinodes at open ends
pressure = sin(pi*s): nodes at open ends
*/
const pressureShape =
type === "closed"
? Math.cos(Math.PI * s)
: Math.sin(Math.PI * s);
const displacementShape =
type === "closed"
? Math.sin(Math.PI * s)
: Math.cos(Math.PI * s);
const pressureMaterial = new THREE.MeshStandardMaterial({
color: pressurePositive,
roughness: 0.42,
metalness: 0.02,
transparent: true,
opacity: 0.82,
emissive: 0x000000,
emissiveIntensity: 0
});
const pressureDisc = new THREE.Mesh(
pressureDiscGeometry,
pressureMaterial
);
pressureDisc.position.x = x;
pressureDisc.scale.set(1, 0.02, 0.02);
pressureDisc.castShadow = true;
pressureDisc.receiveShadow = true;
pressureDisc.renderOrder = 3;
group.add(pressureDisc);
pressureSamples.push({
mesh: pressureDisc,
material: pressureMaterial,
shape: pressureShape
});
const arrowGroup = new THREE.Group();
arrowGroup.position.x = x;
const shaft = new THREE.Mesh(
arrowShaftGeometry,
arrowPositiveMaterial
);
shaft.scale.x = 0.15;
shaft.castShadow = true;
const head = new THREE.Mesh(
arrowHeadGeometry,
arrowPositiveMaterial
);
head.position.x = 0.185;
head.castShadow = true;
arrowGroup.add(shaft, head);
arrowGroup.renderOrder = 4;
group.add(arrowGroup);
displacementSamples.push({
group: arrowGroup,
shaft,
head,
shape: displacementShape
});
}
return {
type,
group,
pressureSamples,
displacementSamples
};
}
const closedModel = createModel("closed");
const openModel = createModel("open");
const models = [closedModel, openModel];
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const anchors = [];
function addAnchor(position, html, className, offsetX = 0, offsetY = 0) {
const element = document.createElement("div");
element.className = `anchor ${className}`;
element.innerHTML = html;
labelsLayer.appendChild(element);
anchors.push({
element,
position,
offsetX,
offsetY
});
return element;
}
addAnchor(
new THREE.Vector3(modelCenters.closed - 1.96, 0.37, 0),
`
<b>SEALED END</b>
<span><i class="field-icon displacement">D</i>displacement node</span>
<span><i class="field-icon pressure">P</i>pressure antinode</span>
`,
"endpoint",
-10,
-12
);
addAnchor(
new THREE.Vector3(modelCenters.closed + 1.96, 0.37, 0),
`
<b>SEALED END</b>
<span><i class="field-icon displacement">D</i>displacement node</span>
<span><i class="field-icon pressure">P</i>pressure antinode</span>
`,
"endpoint",
-76,
-12
);
addAnchor(
new THREE.Vector3(modelCenters.open - 1.96, 0.37, 0),
`
<b>OPEN END</b>
<span><i class="field-icon pressure">P</i>pressure node</span>
<span><i class="field-icon displacement">D</i>displacement antinode</span>
`,
"endpoint",
76,
-12
);
addAnchor(
new THREE.Vector3(modelCenters.open + 1.96, 0.37, 0),
`
<b>OPEN END</b>
<span><i class="field-icon pressure">P</i>pressure node</span>
<span><i class="field-icon displacement">D</i>displacement antinode</span>
`,
"endpoint",
10,
-12
);
// -------------------------------------------------------------------------
// Camera controls implemented directly, without Three.js addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, -0.03, 0);
let yaw = 0;
let pitch = 0.34;
let zoom = 1;
let baseRadius = 12.8;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const radius = baseRadius * zoom;
const horizontalRadius = Math.cos(pitch) * radius;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontalRadius,
cameraTarget.y + Math.sin(pitch) * radius,
cameraTarget.z + Math.cos(yaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
yaw = 0;
pitch = 0.34;
zoom = 1;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
yaw = THREE.MathUtils.clamp(yaw - dx * 0.005, -0.82, 0.82);
pitch = THREE.MathUtils.clamp(pitch + dy * 0.004, 0.12, 0.78);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
zoom = THREE.MathUtils.clamp(
zoom + event.deltaY * 0.00075,
0.72,
1.65
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
resetButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Field controls and animation
// -------------------------------------------------------------------------
let visibleField = "both";
let playing = true;
let phase = 0.62;
const fieldButtons = [
...document.querySelectorAll("[data-field]")
];
function setVisibleField(field) {
visibleField = field;
for (const button of fieldButtons) {
button.setAttribute(
"aria-pressed",
String(button.dataset.field === visibleField)
);
}
const showPressure = visibleField !== "displacement";
const showDisplacement = visibleField !== "pressure";
pressureLegend.classList.toggle("dim", !showPressure);
displacementLegend.classList.toggle("dim", !showDisplacement);
if (visibleField === "both") {
statusPill.textContent = "Showing pressure + displacement";
} else if (visibleField === "pressure") {
statusPill.textContent = "Showing pressure field";
} else {
statusPill.textContent = "Showing displacement field";
}
}
for (const button of fieldButtons) {
button.addEventListener("click", () => {
setVisibleField(button.dataset.field);
});
}
function setPlaying(value) {
playing = value;
playIcon.textContent = playing ? "Ⅱ" : "▶";
playText.textContent = playing ? "Pause" : "Play";
}
playButton.addEventListener("click", () => {
setPlaying(!playing);
});
function updateFields() {
const showPressure = visibleField !== "displacement";
const showDisplacement = visibleField !== "pressure";
const pressureTemporal = Math.sin(phase);
const displacementTemporal = Math.cos(phase);
for (const model of models) {
for (const sample of model.pressureSamples) {
const value = sample.shape * pressureTemporal;
const magnitude = Math.abs(value);
const radialScale = Math.max(0.018, magnitude);
sample.mesh.visible = showPressure;
sample.mesh.scale.set(1, radialScale, radialScale);
if (magnitude < 0.025) {
sample.material.color.copy(pressureNeutral);
sample.material.emissive.setHex(0x000000);
} else if (value > 0) {
sample.material.color.copy(pressurePositive);
sample.material.emissive.copy(pressurePositive);
sample.material.emissiveIntensity = 0.07;
} else {
sample.material.color.copy(pressureNegative);
sample.material.emissive.copy(pressureNegative);
sample.material.emissiveIntensity = 0.07;
}
}
for (const sample of model.displacementSamples) {
const value = sample.shape * displacementTemporal;
const spatialNode = Math.abs(sample.shape) < 0.035;
sample.group.visible =
showDisplacement && !spatialNode;
if (!sample.group.visible) continue;
const length = 0.12 + Math.abs(value) * 0.5;
const material = value >= 0
? arrowPositiveMaterial
: arrowNegativeMaterial;
sample.group.scale.x = value >= 0 ? 1 : -1;
sample.shaft.scale.x = length;
sample.shaft.material = material;
sample.head.material = material;
sample.head.position.x = length / 2 + 0.11;
}
}
}
// -------------------------------------------------------------------------
// Resize and projected label positioning
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateAnchors() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const anchor of anchors) {
projected.copy(anchor.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
anchor.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width + anchor.offsetX;
const y = (-projected.y * 0.5 + 0.5) * height + anchor.offsetY;
anchor.element.style.transform =
`translate(-50%, -100%) translate(${x}px, ${y}px)`;
}
}
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
if (aspect < 0.9) {
baseRadius = 18;
} else if (aspect < 1.35) {
baseRadius = 15.2;
} else {
baseRadius = 12.8;
}
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
phase += dt * 1.18;
}
updateFields();
updateAnchors();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```