Prompt specification
Build or customize the Three.js 3D scene "Harbor wave overlap exhibit" 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: Harbor wave overlap exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/harbor-wave-overlap-exhibit-x1591
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring wave superposition and pulse addition. Compose two simplified wave sources entering a small basin with an overhead sum surface; label the model linear and separate decorative boats from the calculated field. Voxel style.
Working Three.js Starter (harbor-wave-overlap-exhibit-x1591):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Wave Lab — Linear Superposition</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(9, 22, 38, 0.82);
--line: rgba(132, 191, 224, 0.18);
--text: #e7f4ff;
--muted: #8ca8bd;
--a: #5c9dff;
--b: #ff875f;
--sum: #4ce3bd;
--negative: #8c64ff;
--probe: #ffd166;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.brand {
position: fixed;
top: 22px;
left: 24px;
z-index: 10;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--sum);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.2vw, 34px);
font-weight: 750;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.02em;
}
.model-chip {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(76, 227, 189, 0.35);
border-radius: 4px;
background: rgba(76, 227, 189, 0.08);
color: #baf9e7;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.model-chip::before {
width: 7px;
height: 7px;
border-radius: 1px;
background: var(--sum);
content: "";
box-shadow: 0 0 12px rgba(76, 227, 189, 0.8);
}
.controls,
.readout,
.legend {
position: fixed;
z-index: 12;
border-radius: 8px;
}
.controls {
left: 24px;
bottom: 24px;
width: 286px;
padding: 16px;
}
.readout {
top: 24px;
right: 24px;
width: 286px;
padding: 16px;
}
.legend {
right: 24px;
bottom: 24px;
width: 286px;
padding: 15px 16px;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel-title span:last-child {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
.source-control + .source-control {
margin-top: 12px;
}
.source-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
font-size: 11px;
}
.source-name {
display: flex;
align-items: center;
gap: 8px;
font-weight: 700;
}
.dot {
display: inline-block;
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.dot.a {
color: var(--a);
background: var(--a);
}
.dot.b {
color: var(--b);
background: var(--b);
}
output {
color: var(--muted);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--sum);
cursor: pointer;
}
#amp-a {
accent-color: var(--a);
}
#amp-b {
accent-color: var(--b);
}
.button-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 15px;
}
button {
min-height: 34px;
padding: 8px 10px;
border: 1px solid var(--line);
border-radius: 5px;
background: rgba(22, 46, 67, 0.72);
color: var(--text);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
cursor: pointer;
transition: background 140ms, border-color 140ms, transform 140ms;
}
button:hover {
border-color: rgba(157, 218, 246, 0.5);
background: rgba(34, 69, 94, 0.9);
}
button:active {
transform: translateY(1px);
}
button.fire-a {
border-color: rgba(92, 157, 255, 0.42);
}
button.fire-b {
border-color: rgba(255, 135, 95, 0.42);
}
button.primary {
grid-column: 1 / -1;
border-color: rgba(76, 227, 189, 0.38);
background: rgba(76, 227, 189, 0.1);
color: #c9fff1;
}
.button-grid.secondary {
grid-template-columns: 1fr 1fr;
margin-top: 8px;
}
.check-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.check-row input {
accent-color: var(--sum);
}
.hint {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.55;
}
.equation {
display: flex;
align-items: baseline;
justify-content: center;
gap: 9px;
margin: 5px 0 15px;
padding: 12px 8px;
border: 1px solid rgba(76, 227, 189, 0.18);
border-radius: 6px;
background: rgba(76, 227, 189, 0.055);
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 16px;
}
.equation .a-text {
color: var(--a);
}
.equation .b-text {
color: var(--b);
}
.equation strong {
color: var(--sum);
font-size: 20px;
}
.probe-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 8px;
}
.probe-cell {
padding: 10px 8px;
border: 1px solid var(--line);
border-radius: 5px;
background: rgba(5, 15, 27, 0.45);
text-align: center;
}
.probe-cell span {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.probe-cell strong {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.probe-cell.a-cell strong {
color: var(--a);
}
.probe-cell.b-cell strong {
color: var(--b);
}
.probe-cell.sum-cell strong {
color: var(--sum);
}
.bar-track {
height: 3px;
margin-top: 8px;
overflow: hidden;
border-radius: 3px;
background: rgba(255, 255, 255, 0.08);
}
.bar-track i {
display: block;
width: 0;
height: 100%;
transition: width 60ms linear;
}
.bar-a {
background: var(--a);
}
.bar-b {
background: var(--b);
}
.bar-sum {
background: var(--sum);
}
.legend-item {
display: flex;
align-items: center;
gap: 10px;
margin-top: 10px;
font-size: 11px;
}
.legend-item small {
margin-left: auto;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.gradient {
width: 38px;
height: 8px;
border-radius: 2px;
background: linear-gradient(90deg, #8c64ff, #19364f, #4ce3bd);
}
.boat-swatch {
width: 22px;
height: 10px;
border: 1px dashed #e6d3a8;
border-radius: 2px;
background: #e6d3a8;
}
.view-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 13px;
}
.view-row button {
min-height: 29px;
padding: 6px;
font-size: 9px;
}
.view-row button.active {
border-color: rgba(76, 227, 189, 0.45);
background: rgba(76, 227, 189, 0.1);
color: #c9fff1;
}
.pin {
position: fixed;
z-index: 8;
padding: 6px 8px;
border: 1px solid rgba(144, 200, 228, 0.3);
border-radius: 4px;
background: rgba(5, 15, 27, 0.78);
color: #dceefa;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
line-height: 1.3;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
}
.pin::after {
position: absolute;
bottom: -7px;
left: 50%;
width: 1px;
height: 7px;
background: currentColor;
content: "";
opacity: 0.65;
}
.pin.a {
border-color: rgba(92, 157, 255, 0.52);
color: #a9caff;
}
.pin.b {
border-color: rgba(255, 135, 95, 0.52);
color: #ffc0a7;
}
.pin.sum {
border-color: rgba(76, 227, 189, 0.5);
color: #a9f5df;
}
.pin.decor {
border-style: dashed;
border-color: rgba(230, 211, 168, 0.55);
color: #ead9b2;
}
.camera-help {
position: fixed;
bottom: 25px;
left: 50%;
z-index: 8;
padding: 8px 11px;
border-radius: 5px;
background: rgba(6, 17, 29, 0.55);
color: rgba(190, 215, 232, 0.68);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
pointer-events: none;
transform: translateX(-50%);
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: var(--bg);
color: var(--sum);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 300ms;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 980px) {
.legend {
display: none;
}
.readout {
top: 132px;
}
}
@media (max-width: 680px) {
.brand {
top: 14px;
left: 14px;
}
.subtitle {
display: none;
}
.model-chip {
margin-top: 8px;
}
.controls {
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
padding: 12px;
}
.controls .hint,
.controls .view-row {
display: none;
}
.readout {
top: 108px;
right: 10px;
width: 220px;
padding: 11px;
}
.equation {
margin-bottom: 8px;
padding: 8px;
}
.probe-cell {
padding: 7px 3px;
}
.camera-help {
display: none;
}
.pin {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="brand">
<div class="eyebrow">Voxel Wave Lab / 02</div>
<h1>Wave Superposition</h1>
<div class="subtitle">Two independent sources. One linear sum field.</div>
<div class="model-chip">Linear model · no source interaction</div>
</header>
<section class="controls glass">
<div class="panel-title">
<span>Source Control</span>
<span id="time-readout">t = 0.00 s</span>
</div>
<div class="source-control">
<div class="source-row">
<label class="source-name" for="amp-a">
<i class="dot a"></i>
Source A
</label>
<output id="amp-a-value">1.00</output>
</div>
<input id="amp-a" type="range" min="0.2" max="1.4" step="0.05" value="1">
</div>
<div class="source-control">
<div class="source-row">
<label class="source-name" for="amp-b">
<i class="dot b"></i>
Source B
</label>
<output id="amp-b-value">1.00</output>
</div>
<input id="amp-b" type="range" min="0.2" max="1.4" step="0.05" value="1">
</div>
<div class="button-grid">
<button id="fire-a" class="fire-a">Fire A</button>
<button id="fire-b" class="fire-b">Fire B</button>
<button id="fire-pair" class="primary">Fire A + B Together</button>
</div>
<div class="button-grid secondary">
<button id="pause">Pause</button>
<button id="reset">Reset Field</button>
</div>
<label class="check-row">
<input id="auto-repeat" type="checkbox" checked>
Repeat source pairs every 6 seconds
</label>
<div class="view-row">
<button data-view="overview" class="active">Overview</button>
<button data-view="basin">Basin</button>
<button data-view="sum">Sum View</button>
</div>
<div class="hint">
Click the basin to move the probe. Pulses are Gaussian-carrier wave
packets; their signed amplitudes are added directly.
<br>
Shortcuts: <b>A</b>, <b>B</b>, <b>S</b>, <b>Space</b>.
</div>
</section>
<section class="readout glass">
<div class="panel-title">
<span>Linear Probe</span>
<span>click basin</span>
</div>
<div class="equation">
<span class="a-text">h<sub>A</sub></span>
+
<span class="b-text">h<sub>B</sub></span>
=
<strong>h<sub>Σ</sub></strong>
</div>
<div class="probe-grid">
<div class="probe-cell a-cell">
<span>A</span>
<strong id="probe-a">+0.000</strong>
<div class="bar-track"><i id="bar-a" class="bar-a"></i></div>
</div>
<div class="probe-cell b-cell">
<span>B</span>
<strong id="probe-b">+0.000</strong>
<div class="bar-track"><i id="bar-b" class="bar-b"></i></div>
</div>
<div class="probe-cell sum-cell">
<span>Sum</span>
<strong id="probe-sum">+0.000</strong>
<div class="bar-track"><i id="bar-sum" class="bar-sum"></i></div>
</div>
</div>
</section>
<aside class="legend glass">
<div class="panel-title">
<span>Field Key</span>
<span>voxel tiles</span>
</div>
<div class="legend-item">
<i class="dot a"></i>
Source A wave packet
<small>independent</small>
</div>
<div class="legend-item">
<i class="dot b"></i>
Source B wave packet
<small>independent</small>
</div>
<div class="legend-item">
<i class="gradient"></i>
Signed sum height
<small>negative → positive</small>
</div>
<div class="legend-item">
<i class="boat-swatch"></i>
Decorative boats
<small>not in calculation</small>
</div>
</aside>
<div id="pin-a" class="pin a">Pulse source A</div>
<div id="pin-b" class="pin b">Pulse source B</div>
<div id="pin-basin" class="pin">Calculated basin</div>
<div id="pin-sum" class="pin sum">Overhead sum surface</div>
<div id="pin-boats" class="pin decor">Decorative boats · excluded from h</div>
<div class="camera-help">Drag to orbit · wheel to zoom · click basin to probe</div>
<div id="loading">Building voxel wave field…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("stage");
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
// ------------------------------------------------------------
// Renderer, scene, lights
// ------------------------------------------------------------
let renderer;
try {
renderer = new THREE.WebGLRenderer({ antialias: true });
} catch (error) {
$("loading").textContent = "WebGL is required to run this scene.";
throw error;
}
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x07111f);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 48, 100);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
160
);
scene.add(new THREE.HemisphereLight(0xb8ddff, 0x102032, 1.8));
const keyLight = new THREE.DirectionalLight(0xe8f5ff, 3.1);
keyLight.position.set(-12, 26, 17);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -34;
keyLight.shadow.camera.right = 34;
keyLight.shadow.camera.top = 30;
keyLight.shadow.camera.bottom = -24;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 75;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x3e7cff, 1.3);
fillLight.position.set(22, 12, -18);
scene.add(fillLight);
// ------------------------------------------------------------
// Model and layout constants
// ------------------------------------------------------------
const COLS = 36;
const ROWS = 24;
const CELL = 0.58;
const COUNT = COLS * ROWS;
const BASIN_WIDTH = COLS * CELL;
const BASIN_DEPTH = ROWS * CELL;
const FLOOR_Y = -1.45;
const BASIN_CENTER = new THREE.Vector3(-8, 0, 0);
const PANEL_CENTER = new THREE.Vector3(8, 8.2, 0);
const PULSE_SPEED = 3.1;
const PULSE_WIDTH = 1.55;
const CARRIER_K = (Math.PI * 2) / 1.85;
const MAX_PULSE_AGE = 10.5;
const sourceLocal = [
{ x: -5.25, z: -BASIN_DEPTH / 2 + 0.82 },
{ x: 5.10, z: -BASIN_DEPTH / 2 + 0.82 }
];
const simulation = {
time: 0,
paused: false,
nextAuto: 6.2,
probe: { x: 0, z: 1.8 }
};
const sources = [
{
amp: 1,
pulses: [],
lastFire: -100,
fxParticles: []
},
{
amp: 1,
pulses: [],
lastFire: -100,
fxParticles: []
}
];
const localX = new Float32Array(COUNT);
const localZ = new Float32Array(COUNT);
const fieldA = new Float32Array(COUNT);
const fieldB = new Float32Array(COUNT);
const fieldSum = new Float32Array(COUNT);
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const i = row * COLS + col;
localX[i] = (col + 0.5) * CELL - BASIN_WIDTH / 2;
localZ[i] = (row + 0.5) * CELL - BASIN_DEPTH / 2;
}
}
// ------------------------------------------------------------
// Materials and helper geometry
// ------------------------------------------------------------
const basinMaterial = new THREE.MeshStandardMaterial({
color: 0x14283a,
roughness: 0.78,
metalness: 0.08
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x203b51,
roughness: 0.72,
metalness: 0.12
});
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1b2b,
roughness: 0.82,
metalness: 0.05
});
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58,
metalness: 0.06
});
const upperVoxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.46,
metalness: 0.04
});
const sourceMaterials = [
new THREE.MeshStandardMaterial({
color: 0x5c9dff,
emissive: 0x173f87,
emissiveIntensity: 0.6,
roughness: 0.45,
metalness: 0.08
}),
new THREE.MeshStandardMaterial({
color: 0xff875f,
emissive: 0x792a12,
emissiveIntensity: 0.6,
roughness: 0.45,
metalness: 0.08
})
];
const sourceCapMaterials = sourceMaterials.map((material) => {
return material.clone();
});
function addBox(
width,
height,
depth,
x,
y,
z,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
scene.add(mesh);
return mesh;
}
// Ground and subtle laboratory grid.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(140, 140),
new THREE.MeshStandardMaterial({
color: 0x07121e,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.65;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(100, 50, 0x28465d, 0x13283b);
grid.position.y = -2.63;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// Basin pedestal, floor, and walls.
addBox(
BASIN_WIDTH + 2.3,
0.7,
BASIN_DEPTH + 2.3,
BASIN_CENTER.x,
-2.05,
BASIN_CENTER.z,
panelMaterial
);
addBox(
BASIN_WIDTH + 0.8,
0.55,
BASIN_DEPTH + 0.8,
BASIN_CENTER.x,
-1.68,
BASIN_CENTER.z,
basinMaterial
);
addBox(
0.7,
1.8,
BASIN_DEPTH + 1.35,
BASIN_CENTER.x - BASIN_WIDTH / 2 - 0.35,
-0.45,
BASIN_CENTER.z,
rimMaterial
);
addBox(
0.7,
1.8,
BASIN_DEPTH + 1.35,
BASIN_CENTER.x + BASIN_WIDTH / 2 + 0.35,
-0.45,
BASIN_CENTER.z,
rimMaterial
);
addBox(
BASIN_WIDTH + 1.35,
1.8,
0.7,
BASIN_CENTER.x,
-0.45,
BASIN_CENTER.z - BASIN_DEPTH / 2 - 0.35,
rimMaterial
);
// A low front lip keeps the basin visible from the camera.
addBox(
BASIN_WIDTH + 1.35,
0.65,
0.7,
BASIN_CENTER.x,
-0.98,
BASIN_CENTER.z + BASIN_DEPTH / 2 + 0.35,
rimMaterial
);
// ------------------------------------------------------------
// Calculated voxel fields
// ------------------------------------------------------------
const lowerGeometry = new THREE.BoxGeometry(CELL * 0.86, 1, CELL * 0.86);
const lowerVoxels = new THREE.InstancedMesh(
lowerGeometry,
voxelMaterial,
COUNT
);
lowerVoxels.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
lowerVoxels.frustumCulled = false;
lowerVoxels.receiveShadow = true;
scene.add(lowerVoxels);
const upperGeometry = new THREE.BoxGeometry(CELL * 0.79, 0.13, CELL * 0.79);
const upperVoxels = new THREE.InstancedMesh(
upperGeometry,
upperVoxelMaterial,
COUNT
);
upperVoxels.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
upperVoxels.frustumCulled = false;
scene.add(upperVoxels);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
function setFieldColor(height, output, upper = false) {
const amount = clamp(Math.abs(height) / 1.25, 0, 1);
if (height >= 0) {
output.setRGB(
lerp(0.055, 0.30, amount),
lerp(0.25, 0.95, amount),
lerp(0.36, 0.72, amount)
);
} else {
output.setRGB(
lerp(0.09, 0.53, amount),
lerp(0.14, 0.29, amount),
lerp(0.34, 0.98, amount)
);
}
if (upper) {
output.multiplyScalar(1.06);
}
}
// ------------------------------------------------------------
// Overhead sum panel and exploded-view guides
// ------------------------------------------------------------
addBox(
BASIN_WIDTH + 1.6,
0.38,
BASIN_DEPTH + 1.6,
PANEL_CENTER.x,
PANEL_CENTER.y - 0.32,
PANEL_CENTER.z,
panelMaterial
);
addBox(
BASIN_WIDTH + 1.75,
0.18,
0.18,
PANEL_CENTER.x,
PANEL_CENTER.y - 0.07,
PANEL_CENTER.z - BASIN_DEPTH / 2 - 0.82,
rimMaterial
);
addBox(
BASIN_WIDTH + 1.75,
0.18,
0.18,
PANEL_CENTER.x,
PANEL_CENTER.y - 0.07,
PANEL_CENTER.z + BASIN_DEPTH / 2 + 0.82,
rimMaterial
);
addBox(
0.18,
0.18,
BASIN_DEPTH + 1.75,
PANEL_CENTER.x - BASIN_WIDTH / 2 - 0.82,
PANEL_CENTER.y - 0.07,
PANEL_CENTER.z,
rimMaterial
);
addBox(
0.18,
0.18,
BASIN_DEPTH + 1.75,
PANEL_CENTER.x + BASIN_WIDTH / 2 + 0.82,
PANEL_CENTER.y - 0.07,
PANEL_CENTER.z,
rimMaterial
);
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x3d7894,
dashSize: 0.38,
gapSize: 0.28,
transparent: true,
opacity: 0.38
});
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(
BASIN_CENTER.x + sx * BASIN_WIDTH / 2,
0.5,
BASIN_CENTER.z + sz * BASIN_DEPTH / 2
),
new THREE.Vector3(
PANEL_CENTER.x + sx * BASIN_WIDTH / 2,
PANEL_CENTER.y - 0.15,
PANEL_CENTER.z + sz * BASIN_DEPTH / 2
)
]);
const line = new THREE.Line(geometry, guideMaterial);
line.computeLineDistances();
scene.add(line);
}
}
// ------------------------------------------------------------
// Source towers
// ------------------------------------------------------------
function createSource(index) {
const local = sourceLocal[index];
const worldX = BASIN_CENTER.x + local.x;
const worldZ = BASIN_CENTER.z + local.z;
const group = new THREE.Group();
group.position.set(worldX, 0, worldZ);
scene.add(group);
const pedestal = new THREE.Mesh(
new THREE.BoxGeometry(1.35, 0.38, 1.2),
basinMaterial
);
pedestal.position.y = 0.17;
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.76, 0.88, 0.76),
sourceMaterials[index]
);
body.position.y = 0.78;
body.castShadow = true;
group.add(body);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(1.02, 0.24, 1.02),
sourceCapMaterials[index]
);
cap.position.y = 1.34;
cap.castShadow = true;
group.add(cap);
const nozzle = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.34, 1.25),
rimMaterial
);
nozzle.position.set(0, 0.58, 0.83);
nozzle.castShadow = true;
group.add(nozzle);
const light = new THREE.PointLight(
index === 0 ? 0x5c9dff : 0xff875f,
1.8,
7,
2
);
light.position.set(0, 1.55, 0.2);
group.add(light);
const panelMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.16, 0.55),
sourceCapMaterials[index]
);
panelMarker.position.set(
PANEL_CENTER.x + local.x,
PANEL_CENTER.y + 0.32,
PANEL_CENTER.z + local.z
);
scene.add(panelMarker);
sources[index].group = group;
sources[index].cap = cap;
sources[index].light = light;
sources[index].panelMarker = panelMarker;
sources[index].worldX = worldX;
sources[index].worldZ = worldZ;
}
createSource(0);
createSource(1);
// ------------------------------------------------------------
// Decorative voxel boats
// These are visual props only and never enter the field solver.
// ------------------------------------------------------------
const hullMaterial = new THREE.MeshStandardMaterial({
color: 0xe6d3a8,
roughness: 0.78
});
const sailMaterial = new THREE.MeshStandardMaterial({
color: 0xf5f0df,
roughness: 0.88
});
const boatAccentMaterial = new THREE.MeshStandardMaterial({
color: 0xff875f,
roughness: 0.7
});
function createBoat(scale, position, rotation, phase) {
const group = new THREE.Group();
const addBoatBox = (
width,
height,
depth,
x,
y,
z,
material
) => {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
};
addBoatBox(0.92, 0.22, 0.46, 0, 0, 0, hullMaterial);
addBoatBox(0.34, 0.22, 0.46, 0.62, 0, 0, hullMaterial);
addBoatBox(0.32, 0.22, 0.46, -0.61, 0, 0, hullMaterial);
addBoatBox(0.72, 0.10, 0.34, 0, 0.15, 0, boatAccentMaterial);
addBoatBox(0.38, 0.32, 0.30, -0.18, 0.34, 0, hullMaterial);
addBoatBox(0.09, 0.76, 0.09, 0.28, 0.49, 0, hullMaterial);
addBoatBox(0.44, 0.17, 0.07, 0.10, 0.66, 0, sailMaterial);
addBoatBox(0.32, 0.15, 0.07, 0.03, 0.83, 0, sailMaterial);
group.scale.setScalar(scale);
group.position.copy(position);
group.rotation.y = rotation;
scene.add(group);
return {
group,
baseY: position.y,
phase
};
}
const boats = [
createBoat(
0.9,
new THREE.Vector3(
BASIN_CENTER.x + 6.3,
0.52,
BASIN_CENTER.z + 3.05
),
-0.38,
0
),
createBoat(
0.66,
new THREE.Vector3(
BASIN_CENTER.x - 6.2,
0.50,
BASIN_CENTER.z + 3.55
),
0.55,
1.7
)
];
// ------------------------------------------------------------
// Probe markers and mapping line
// ------------------------------------------------------------
const probeMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x8c5b00,
emissiveIntensity: 0.7,
roughness: 0.4
});
const lowerProbe = new THREE.Mesh(
new THREE.BoxGeometry(0.3, 0.3, 0.3),
probeMaterial
);
scene.add(lowerProbe);
const upperProbe = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.18, 0.32),
probeMaterial
);
scene.add(upperProbe);
const probeLineGeometry = new THREE.BufferGeometry();
probeLineGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const probeLine = new THREE.Line(
probeLineGeometry,
new THREE.LineDashedMaterial({
color: 0xffd166,
dashSize: 0.22,
gapSize: 0.18,
transparent: true,
opacity: 0.56
})
);
scene.add(probeLine);
// ------------------------------------------------------------
// Linear pulse model
//
// For one emitted packet:
// age = t - t_emit
// q = distance - speed * age
// h = A * exp(-0.5*(q/width)^2) * cos(k*q)
//
// The total field is exactly hA + hB.
// ------------------------------------------------------------
function sourceField(source, x, z) {
let value = 0;
for (const pulse of source.pulses) {
const age = simulation.time - pulse.t0;
if (age < 0 || age > MAX_PULSE_AGE) {
continue;
}
const distance = Math.hypot(x - sourceLocal[source === sources[0] ? 0 : 1].x,
z - sourceLocal[source === sources[0] ? 0 : 1].z);
const q = distance - PULSE_SPEED * age;
const envelope = Math.exp(-0.5 * Math.pow(q / PULSE_WIDTH, 2));
const carrier = Math.cos(CARRIER_K * q);
const attenuation = 1 / (1 + 0.025 * distance);
const onset = clamp(age / 0.18, 0, 1);
const smoothOnset = onset * onset * (3 - 2 * onset);
value += pulse.amp * envelope * carrier * attenuation * smoothOnset;
}
return value;
}
function fireSource(index, delay = 0) {
const source = sources[index];
const birth = simulation.time + delay;
source.pulses.push({
t0: birth,
amp: source.amp
});
source.lastFire = birth;
// Small voxel blocks are only a visual indication of emission.
// The actual wave is computed by sourceField().
for (let i = 0; i < 4; i++) {
const particle = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.22, 0.22),
sourceMaterials[index]
);
particle.position.set(
source.worldX + (i - 1.5) * 0.12,
0.45,
source.worldZ + 1.25
);
particle.castShadow = true;
scene.add(particle);
source.fxParticles.push({
mesh: particle,
born: birth + i * 0.075
});
}
}
function updateSourceEffects() {
for (let index = 0; index < sources.length; index++) {
const source = sources[index];
const flashAge = simulation.time - source.lastFire;
const flash = flashAge >= 0 ? Math.max(0, 1 - flashAge / 0.55) : 0;
sourceCapMaterials[index].emissiveIntensity = 0.55 + flash * 2.5;
source.light.intensity = 1.4 + flash * 5.0;
source.cap.scale.y = 1 + flash * 0.45;
for (let i = source.fxParticles.length - 1; i >= 0; i--) {
const particle = source.fxParticles[i];
const age = simulation.time - particle.born;
if (age < 0) {
particle.mesh.visible = false;
continue;
}
if (age > 1.15) {
scene.remove(particle.mesh);
particle.mesh.geometry.dispose();
source.fxParticles.splice(i, 1);
continue;
}
particle.mesh.visible = true;
particle.mesh.position.z = source.worldZ + 1.15 + age * 2.55;
particle.mesh.position.y = 0.48 + Math.sin(age * Math.PI) * 0.18;
const size = 1 - age / 1.45;
particle.mesh.scale.setScalar(Math.max(0.08, size));
}
}
}
function updateField() {
for (let i = 0; i < COUNT; i++) {
const x = localX[i];
const z = localZ[i];
const a = sourceField(sources[0], x, z);
const b = sourceField(sources[1], x, z);
const sum = a + b;
fieldA[i] = a;
fieldB[i] = b;
fieldSum[i] = sum;
// Lower calculated basin.
const waterTop = Math.max(
FLOOR_Y + 0.1,
0.12 + sum * 1.28
);
const waterHeight = waterTop - FLOOR_Y;
dummy.position.set(
BASIN_CENTER.x + x,
FLOOR_Y + waterHeight / 2,
BASIN_CENTER.z + z
);
dummy.scale.set(1, waterHeight, 1);
dummy.updateMatrix();
lowerVoxels.setMatrixAt(i, dummy.matrix);
setFieldColor(sum, tempColor, false);
lowerVoxels.setColorAt(i, tempColor);
// Overhead top-view sum surface.
dummy.position.set(
PANEL_CENTER.x + x,
PANEL_CENTER.y + clamp(sum, -1.5, 1.5) * 0.22,
PANEL_CENTER.z + z
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
upperVoxels.setMatrixAt(i, dummy.matrix);
setFieldColor(sum, tempColor, true);
upperVoxels.setColorAt(i, tempColor);
}
lowerVoxels.instanceMatrix.needsUpdate = true;
upperVoxels.instanceMatrix.needsUpdate = true;
if (lowerVoxels.instanceColor) {
lowerVoxels.instanceColor.needsUpdate = true;
}
if (upperVoxels.instanceColor) {
upperVoxels.instanceColor.needsUpdate = true;
}
}
// Initialize instance colors before the animation loop.
for (let i = 0; i < COUNT; i++) {
lowerVoxels.setColorAt(i, new THREE.Color(0x19364f));
upperVoxels.setColorAt(i, new THREE.Color(0x19364f));
}
if (lowerVoxels.instanceColor) {
lowerVoxels.instanceColor.setUsage(THREE.DynamicDrawUsage);
}
if (upperVoxels.instanceColor) {
upperVoxels.instanceColor.setUsage(THREE.DynamicDrawUsage);
}
updateField();
// ------------------------------------------------------------
// Probe UI
// ------------------------------------------------------------
function signed(value) {
const rounded = Math.abs(value) < 0.0005 ? 0 : value;
return `${rounded >= 0 ? "+" : "−"}${Math.abs(rounded).toFixed(3)}`;
}
function updateProbe() {
const a = sourceField(sources[0], simulation.probe.x, simulation.probe.z);
const b = sourceField(sources[1], simulation.probe.x, simulation.probe.z);
const sum = a + b;
$("probe-a").textContent = signed(a);
$("probe-b").textContent = signed(b);
$("probe-sum").textContent = signed(sum);
$("bar-a").style.width = `${clamp(Math.abs(a) / 1.3, 0, 1) * 100}%`;
$("bar-b").style.width = `${clamp(Math.abs(b) / 1.3, 0, 1) * 100}%`;
$("bar-sum").style.width = `${clamp(Math.abs(sum) / 1.6, 0, 1) * 100}%`;
const lowerTop = 0.12 + sum * 1.28;
lowerProbe.position.set(
BASIN_CENTER.x + simulation.probe.x,
lowerTop + 0.18,
BASIN_CENTER.z + simulation.probe.z
);
upperProbe.position.set(
PANEL_CENTER.x + simulation.probe.x,
PANEL_CENTER.y + clamp(sum, -1.5, 1.5) * 0.22 + 0.19,
PANEL_CENTER.z + simulation.probe.z
);
const positions = probeLineGeometry.attributes.position;
positions.setXYZ(
0,
lowerProbe.position.x,
lowerProbe.position.y,
lowerProbe.position.z
);
positions.setXYZ(
1,
upperProbe.position.x,
upperProbe.position.y,
upperProbe.position.z
);
positions.needsUpdate = true;
probeLine.computeLineDistances();
}
// ------------------------------------------------------------
// Direct camera controls — no external controls/addon required
// ------------------------------------------------------------
const cameraRig = {
theta: 0.68,
phi: 0.86,
radius: 48,
target: new THREE.Vector3(0, 3.2, 0)
};
let cameraGoal = null;
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
function overviewRadius() {
return innerWidth / innerHeight < 1 ? 66 : 48;
}
function setView(name) {
if (name === "basin") {
cameraGoal = {
theta: 0.68,
phi: 1.04,
radius: 30,
target: new THREE.Vector3(BASIN_CENTER.x, 0.8, BASIN_CENTER.z)
};
} else if (name === "sum") {
cameraGoal = {
theta: 0.08,
phi: 0.22,
radius: 29,
target: PANEL_CENTER.clone()
};
} else {
cameraGoal = {
theta: 0.68,
phi: 0.86,
radius: overviewRadius(),
target: new THREE.Vector3(0, 3.2, 0)
};
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === name);
});
}
let dragging = false;
let dragMoved = 0;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragMoved = 0;
previousX = event.clientX;
previousY = event.clientY;
cameraGoal = null;
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;
dragMoved += Math.abs(dx) + Math.abs(dy);
cameraRig.theta -= dx * 0.006;
cameraRig.phi = clamp(cameraRig.phi - dy * 0.006, 0.12, 1.36);
});
renderer.domElement.addEventListener("pointerup", (event) => {
dragging = false;
if (dragMoved < 6) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(lowerVoxels, false)[0];
if (hit && Number.isInteger(hit.instanceId)) {
const col = hit.instanceId % COLS;
const row = Math.floor(hit.instanceId / COLS);
simulation.probe.x = localX[hit.instanceId];
simulation.probe.z = localZ[hit.instanceId];
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal = null;
cameraRig.radius = clamp(
cameraRig.radius * Math.exp(event.deltaY * 0.001),
17,
82
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
setView("overview");
});
function updateCamera(dt) {
if (cameraGoal) {
const alpha = 1 - Math.exp(-dt * 7);
cameraRig.theta = lerp(cameraRig.theta, cameraGoal.theta, alpha);
cameraRig.phi = lerp(cameraRig.phi, cameraGoal.phi, alpha);
cameraRig.radius = lerp(cameraRig.radius, cameraGoal.radius, alpha);
cameraRig.target.lerp(cameraGoal.target, alpha);
if (
Math.abs(cameraRig.theta - cameraGoal.theta) < 0.002 &&
Math.abs(cameraRig.phi - cameraGoal.phi) < 0.002 &&
Math.abs(cameraRig.radius - cameraGoal.radius) < 0.02 &&
cameraRig.target.distanceTo(cameraGoal.target) < 0.02
) {
cameraGoal = null;
}
}
const sinPhi = Math.sin(cameraRig.phi);
const cosPhi = Math.cos(cameraRig.phi);
camera.position.set(
cameraRig.target.x +
cameraRig.radius * sinPhi * Math.sin(cameraRig.theta),
cameraRig.target.y + cameraRig.radius * cosPhi,
cameraRig.target.z +
cameraRig.radius * sinPhi * Math.cos(cameraRig.theta)
);
camera.lookAt(cameraRig.target);
}
// ------------------------------------------------------------
// Interface controls
// ------------------------------------------------------------
$("amp-a").addEventListener("input", (event) => {
sources[0].amp = Number(event.target.value);
$("amp-a-value").textContent = sources[0].amp.toFixed(2);
});
$("amp-b").addEventListener("input", (event) => {
sources[1].amp = Number(event.target.value);
$("amp-b-value").textContent = sources[1].amp.toFixed(2);
});
$("fire-a").addEventListener("click", () => fireSource(0));
$("fire-b").addEventListener("click", () => fireSource(1));
$("fire-pair").addEventListener("click", () => {
fireSource(0);
fireSource(1);
});
$("pause").addEventListener("click", () => {
simulation.paused = !simulation.paused;
$("pause").textContent = simulation.paused ? "Resume" : "Pause";
});
$("reset").addEventListener("click", () => {
simulation.time = 0;
simulation.nextAuto = 6.2;
for (const source of sources) {
source.pulses.length = 0;
source.lastFire = -100;
}
fireSource(0);
fireSource(1, 1.25);
});
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setView(button.dataset.view);
});
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
const key = event.key.toLowerCase();
if (key === "a") {
fireSource(0);
} else if (key === "b") {
fireSource(1);
} else if (key === "s") {
fireSource(0);
fireSource(1);
} else if (event.code === "Space") {
event.preventDefault();
$("pause").click();
}
});
// ------------------------------------------------------------
// Projected annotation labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function positionPin(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updatePins() {
positionPin(
$("pin-a"),
new THREE.Vector3(
sources[0].worldX,
2.05,
sources[0].worldZ
)
);
positionPin(
$("pin-b"),
new THREE.Vector3(
sources[1].worldX,
2.05,
sources[1].worldZ
)
);
positionPin(
$("pin-basin"),
new THREE.Vector3(
BASIN_CENTER.x - BASIN_WIDTH / 2 + 1.1,
1.25,
BASIN_CENTER.z + BASIN_DEPTH / 2
)
);
positionPin(
$("pin-sum"),
new THREE.Vector3(
PANEL_CENTER.x - BASIN_WIDTH / 2 + 2.0,
PANEL_CENTER.y + 1.0,
PANEL_CENTER.z + BASIN_DEPTH / 2
)
);
positionPin(
$("pin-boats"),
boats[0].group.position.clone().add(
new THREE.Vector3(0, 1.65, 0)
)
);
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
});
let previousTime = performance.now();
let firstFrame = true;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!simulation.paused) {
simulation.time += dt;
if (
$("auto-repeat").checked &&
simulation.time >= simulation.nextAuto
) {
fireSource(0);
fireSource(1, 1.2);
simulation.nextAuto += 6.4;
}
}
for (const source of sources) {
source.pulses = source.pulses.filter((pulse) => {
return simulation.time - pulse.t0 < MAX_PULSE_AGE;
});
}
updateField();
updateSourceEffects();
updateProbe();
for (const boat of boats) {
boat.group.position.y =
boat.baseY + Math.sin(simulation.time * 1.3 + boat.phase) * 0.035;
boat.group.rotation.z =
Math.sin(simulation.time * 1.05 + boat.phase) * 0.018;
}
updateCamera(dt);
updatePins();
$("time-readout").textContent = `t = ${simulation.time.toFixed(2)} s`;
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
$("loading").classList.add("hidden");
setTimeout(() => {
$("loading").style.display = "none";
}, 350);
}
}
// Initial demonstration: A starts immediately, B follows shortly.
fireSource(0);
fireSource(1, 1.25);
requestAnimationFrame(animate);
</script>
</body>
</html>
```