Prompt specification
Build or customize the Three.js 3D scene "Water-ripple interference tray" 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: Water-ripple interference tray
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-ripple-interference-tray-x1550
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows interference between coherent wave sources. Animate a bounded analytic surface formed by two sources and display amplitude nodes; label boundary reflections omitted so the pattern is not mistaken for a full tank simulation. Stylized solid forms.
Working Three.js Starter (water-ripple-interference-tray-x1550):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Coherent Field — Analytic Interference Surface</title>
<style>
:root {
--bg: #050914;
--panel: rgba(9, 18, 32, 0.82);
--line: rgba(130, 177, 209, 0.2);
--text: #e8f2fb;
--muted: #8ea5ba;
--cyan: #51e0e6;
--violet: #9b7cff;
--gold: #ffd166;
--orange: #ff9b62;
}
* {
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;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active {
cursor: grabbing;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 20px 22px 18px;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(16, 31, 49, 0.9), rgba(7, 14, 26, 0.78));
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
.kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.kicker::before {
width: 24px;
height: 2px;
background: var(--cyan);
content: "";
box-shadow: 0 0 12px rgba(81, 224, 230, 0.8);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 36px);
font-weight: 690;
letter-spacing: -0.045em;
line-height: 1.02;
}
.subtitle {
margin: 10px 0 14px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.notice {
position: relative;
padding: 10px 12px 10px 14px;
border: 1px solid rgba(255, 209, 102, 0.23);
border-radius: 10px;
background: rgba(255, 184, 73, 0.065);
color: #ffd88f;
font-size: 11px;
line-height: 1.45;
}
.notice::before {
position: absolute;
top: 9px;
bottom: 9px;
left: 0;
width: 3px;
border-radius: 3px;
background: var(--gold);
content: "";
}
.notice strong {
display: block;
margin-bottom: 2px;
color: #fff0c9;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.equation {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(130, 177, 209, 0.12);
color: #b8c9d9;
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
font-size: 11px;
letter-spacing: 0.01em;
}
.hint {
margin-top: 9px;
color: #647d94;
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.controls {
position: absolute;
top: 24px;
right: 24px;
width: 292px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.3);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.controls-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 18px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 7px;
color: #91a7ba;
font-size: 9px;
letter-spacing: 0.1em;
}
.live-dot::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
content: "";
}
.control {
margin-bottom: 17px;
}
.control-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
color: #b7c8d8;
font-size: 12px;
}
output {
min-width: 68px;
color: var(--text);
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 11px;
text-align: right;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
outline: none;
background: linear-gradient(90deg, #274158, #142235);
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
width: 15px;
height: 15px;
border: 3px solid #0d1928;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(81, 224, 230, 0.45), 0 0 14px rgba(81, 224, 230, 0.35);
cursor: pointer;
appearance: none;
-webkit-appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid #0d1928;
border-radius: 50%;
background: var(--cyan);
cursor: pointer;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 4px;
}
button {
min-height: 37px;
padding: 8px 10px;
border: 1px solid rgba(130, 177, 209, 0.19);
border-radius: 9px;
background: rgba(22, 38, 55, 0.72);
color: #a9bdcf;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
transition:
border-color 160ms ease,
color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(81, 224, 230, 0.5);
color: white;
background: rgba(28, 54, 72, 0.9);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(255, 209, 102, 0.55);
background: rgba(255, 209, 102, 0.1);
color: var(--gold);
}
.control-note {
margin: 13px 1px 0;
color: #71899e;
font-size: 10px;
line-height: 1.45;
}
.legend {
position: absolute;
bottom: 24px;
left: 24px;
display: flex;
gap: 18px;
align-items: center;
padding: 12px 16px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(7, 15, 27, 0.78);
backdrop-filter: blur(14px);
color: #91a7ba;
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 50%;
}
.swatch.crest {
background: var(--cyan);
box-shadow: 0 0 9px rgba(81, 224, 230, 0.7);
}
.swatch.trough {
background: var(--violet);
box-shadow: 0 0 9px rgba(155, 124, 255, 0.65);
}
.swatch.node {
background: var(--gold);
box-shadow: 0 0 9px rgba(255, 209, 102, 0.7);
}
.status {
position: absolute;
right: 24px;
bottom: 24px;
padding: 11px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(7, 15, 27, 0.78);
backdrop-filter: blur(14px);
color: #91a7ba;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
line-height: 1.6;
text-align: right;
}
.status strong {
color: #d9e8f5;
font-weight: 600;
}
.source-label {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid currentColor;
border-radius: 7px;
background: rgba(5, 9, 20, 0.8);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.3);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
will-change: transform;
}
.source-label::after {
width: 5px;
height: 5px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 8px currentColor;
content: "";
}
#label1 {
color: var(--cyan);
}
#label2 {
color: var(--orange);
}
@media (max-width: 820px) {
.brand {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px 17px;
}
.brand .subtitle,
.brand .equation,
.brand .hint {
display: none;
}
.notice {
margin-top: 12px;
padding: 8px 10px 8px 12px;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: min(292px, calc(100vw - 24px));
max-height: 48vh;
padding: 14px;
}
.legend {
display: none;
}
.status {
right: 12px;
bottom: calc(48vh + 24px);
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="ui">
<section class="brand">
<div class="kicker">Analytic wave laboratory</div>
<h1>Coherent Field</h1>
<p class="subtitle">
Two monochromatic point emitters share one carrier frequency.
The surface is evaluated directly from the traveling-wave field.
</p>
<div class="notice">
<strong>Finite display window</strong>
Boundary reflections omitted — edge contours are not a full tank prediction.
</div>
<div class="equation">
ψ = cos(kr₁ − ωt) + cos(kr₂ + Δφ − ωt)
</div>
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset</div>
</section>
<section class="controls" aria-label="Field controls">
<div class="controls-title">
<span>Field Parameters</span>
<span class="live-dot">Live</span>
</div>
<div class="control">
<div class="control-row">
<label for="wavelength">Wavelength λ</label>
<output id="wavelengthValue">0.86 m</output>
</div>
<input id="wavelength" type="range" min="0.45" max="1.60" step="0.01" value="0.86">
</div>
<div class="control">
<div class="control-row">
<label for="separation">Source separation d</label>
<output id="separationValue">2.65 m</output>
</div>
<input id="separation" type="range" min="0.80" max="4.80" step="0.05" value="2.65">
</div>
<div class="control">
<div class="control-row">
<label for="phase">Source phase Δφ</label>
<output id="phaseValue">0°</output>
</div>
<input id="phase" type="range" min="-180" max="180" step="1" value="0">
</div>
<div class="buttons">
<button id="nodesButton" class="active" aria-pressed="true">Nodes On</button>
<button id="pauseButton" aria-pressed="false">Pause Field</button>
<button id="resetButton">Reset View</button>
<button id="defaultsButton">Defaults</button>
</div>
<p class="control-note">
Gold tubes mark permanent zero-envelope loci caused by destructive
interference. They are not reflecting boundary contours.
</p>
</section>
<div class="legend" aria-label="Field legend">
<div class="legend-item"><span class="swatch crest"></span>Crest</div>
<div class="legend-item"><span class="swatch trough"></span>Trough</div>
<div class="legend-item"><span class="swatch node"></span>Amplitude node</div>
</div>
<div class="status">
<div><strong>λ</strong> <span id="statusLambda">0.86</span> m</div>
<div><strong>d</strong> <span id="statusDistance">2.65</span> m · <strong>Δφ</strong> <span id="statusPhase">0</span>°</div>
<div id="nodeCount">Analytic nodal curves</div>
</div>
<div id="label1" class="source-label">S₁</div>
<div id="label2" class="source-label">S₂</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x050914);
scene.fog = new THREE.FogExp2(0x050914, 0.035);
const camera = new THREE.PerspectiveCamera(
39,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional coherent wave interference field"
);
stage.appendChild(renderer.domElement);
const WIDTH = 12;
const DEPTH = 8.4;
const SURFACE_Y = 0.26;
const params = {
wavelength: 0.86,
separation: 2.65,
phase: 0
};
let nodesVisible = true;
let paused = false;
let elapsed = 0;
let nodalBranchCount = 0;
const cameraState = {
radius: 15.7,
theta: 0.72,
phi: 0.91,
target: new THREE.Vector3(0, 0.05, 0)
};
const hemi = new THREE.HemisphereLight(0xa8dfff, 0x07101a, 1.35);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 2.1);
key.position.set(-5, 9, 6);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -10;
key.shadow.bias = -0.0005;
scene.add(key);
const rim = new THREE.DirectionalLight(0x4cdce5, 1.1);
rim.position.set(7, 4, -8);
scene.add(rim);
// Solid floor and platform.
const floor = new THREE.Mesh(
new THREE.CircleGeometry(30, 96),
new THREE.MeshStandardMaterial({
color: 0x060d18,
roughness: 0.93,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.745;
floor.receiveShadow = true;
scene.add(floor);
const floorGrid = new THREE.GridHelper(32, 32, 0x17354b, 0x0c1a2a);
floorGrid.position.y = -0.735;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.22;
scene.add(floorGrid);
const slabMaterial = new THREE.MeshStandardMaterial({
color: 0x091522,
roughness: 0.38,
metalness: 0.68
});
const deckMaterial = new THREE.MeshStandardMaterial({
color: 0x102238,
emissive: 0x06111c,
emissiveIntensity: 0.6,
roughness: 0.32,
metalness: 0.62
});
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x1b3045,
emissive: 0x071722,
emissiveIntensity: 0.7,
roughness: 0.28,
metalness: 0.75
});
const slab = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.5, 0.35, DEPTH + 0.5),
slabMaterial
);
slab.position.y = -0.52;
slab.castShadow = true;
slab.receiveShadow = true;
scene.add(slab);
const deck = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.16, 0.12, DEPTH + 0.16),
deckMaterial
);
deck.position.y = -0.30;
deck.castShadow = true;
deck.receiveShadow = true;
scene.add(deck);
function addRail(w, d, x, z) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(w, 0.18, d),
railMaterial
);
rail.position.set(x, -0.13, z);
rail.castShadow = true;
scene.add(rail);
}
addRail(WIDTH + 0.24, 0.10, 0, -DEPTH / 2 - 0.04);
addRail(WIDTH + 0.24, 0.10, 0, DEPTH / 2 + 0.04);
addRail(0.10, DEPTH + 0.24, -WIDTH / 2 - 0.04, 0);
addRail(0.10, DEPTH + 0.24, WIDTH / 2 + 0.04, 0);
// Analytic surface: all displacement and coloring are evaluated in the shader.
const surfaceUniforms = {
uTime: { value: 0 },
uLambda: { value: params.wavelength },
uSeparation: { value: params.separation },
uPhase: { value: params.phase },
uAmplitude: { value: 0.44 }
};
const surfaceMaterial = new THREE.ShaderMaterial({
uniforms: surfaceUniforms,
extensions: { derivatives: true },
vertexShader: `
uniform float uTime;
uniform float uLambda;
uniform float uSeparation;
uniform float uPhase;
uniform float uAmplitude;
varying float vWave;
varying float vEnvelope;
varying vec3 vNormal;
varying vec3 vWorldPosition;
varying vec2 vGridPosition;
float fieldAt(vec2 p) {
float k = 6.28318530718 / uLambda;
float t = uTime * 3.2;
vec2 source1 = vec2(-uSeparation * 0.5, 0.0);
vec2 source2 = vec2( uSeparation * 0.5, 0.0);
float r1 = distance(p, source1);
float r2 = distance(p, source2);
return 0.5 * (
cos(k * r1 - t) +
cos(k * r2 + uPhase - t)
);
}
void main() {
vec2 p = vec2(position.x, position.z);
float wave = fieldAt(p);
float k = 6.28318530718 / uLambda;
float r1 = distance(p, vec2(-uSeparation * 0.5, 0.0));
float r2 = distance(p, vec2( uSeparation * 0.5, 0.0));
// Permanent amplitude envelope: 1 at constructive maxima, 0 at nodes.
float envelope = abs(cos(0.5 * (k * (r1 - r2) - uPhase)));
float epsilon = 0.035;
float slopeX = (
fieldAt(p + vec2(epsilon, 0.0)) -
fieldAt(p - vec2(epsilon, 0.0))
) / (2.0 * epsilon);
float slopeZ = (
fieldAt(p + vec2(0.0, epsilon)) -
fieldAt(p - vec2(0.0, epsilon))
) / (2.0 * epsilon);
vec3 displaced = position;
displaced.y += wave * uAmplitude;
vec3 localNormal = normalize(vec3(-slopeX, 1.0, -slopeZ));
vWave = wave;
vEnvelope = envelope;
vNormal = normalize(normalMatrix * localNormal);
vWorldPosition = (modelMatrix * vec4(displaced, 1.0)).xyz;
vGridPosition = p;
gl_Position = projectionMatrix * modelViewMatrix * vec4(displaced, 1.0);
}
`,
fragmentShader: `
varying float vWave;
varying float vEnvelope;
varying vec3 vNormal;
varying vec3 vWorldPosition;
varying vec2 vGridPosition;
float gridMask(vec2 p) {
vec2 scaled = p / 0.75;
vec2 derivativeGrid = fwidth(scaled);
vec2 grid = abs(fract(scaled - 0.5) - 0.5) / derivativeGrid;
return 1.0 - min(min(grid.x, grid.y), 1.0);
}
void main() {
vec3 normal = normalize(vNormal);
vec3 viewDirection = normalize(cameraPosition - vWorldPosition);
vec3 lightDirection = normalize(vec3(-0.45, 0.86, 0.34));
float magnitude = clamp(abs(vWave), 0.0, 1.0);
vec3 crestColor = vec3(0.025, 0.62, 0.72);
vec3 troughColor = vec3(0.29, 0.12, 0.63);
vec3 baseColor = vec3(0.012, 0.038, 0.075);
vec3 color = mix(baseColor, crestColor, smoothstep(0.05, 1.0, magnitude));
color = mix(color, troughColor, smoothstep(0.05, 0.9, magnitude) * step(vWave, 0.0));
float diffuse = max(dot(normal, lightDirection), 0.0);
float rim = pow(1.0 - max(dot(normal, viewDirection), 0.0), 2.6);
float grid = gridMask(vGridPosition);
color *= 0.57 + 0.58 * diffuse;
color = mix(color, vec3(0.10, 0.28, 0.36), grid * 0.28);
color += rim * vec3(0.015, 0.15, 0.20);
float nodeGlow = 1.0 - smoothstep(0.0, 0.105, vEnvelope);
color = mix(color, vec3(0.88, 0.48, 0.13), nodeGlow * 0.26);
color += vEnvelope * vec3(0.0, 0.025, 0.035);
gl_FragColor = vec4(color, 1.0);
#include <tonemapping_fragment>
#include <colorspace_fragment>
}
`
});
const surfaceGeometry = new THREE.PlaneGeometry(WIDTH, DEPTH, 210, 148);
surfaceGeometry.rotateX(-Math.PI / 2);
const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
surface.position.y = SURFACE_Y;
surface.castShadow = true;
surface.receiveShadow = true;
scene.add(surface);
// Stylized solid point-source emitters.
function createEmitter(color) {
const group = new THREE.Group();
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x12283a,
emissive: color,
emissiveIntensity: 0.22,
metalness: 0.76,
roughness: 0.28
});
const coreMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.35,
metalness: 0.2,
roughness: 0.2
});
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.36, 0.44, 0.18, 24),
baseMaterial
);
base.position.y = 0.08;
base.castShadow = true;
group.add(base);
const column = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.18, 0.64, 20),
coreMaterial
);
column.position.y = 0.48;
column.castShadow = true;
group.add(column);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.2, 0.11, 24),
baseMaterial
);
collar.position.y = 0.76;
collar.castShadow = true;
group.add(collar);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.27, 0.035, 8, 44),
coreMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.82;
group.add(ring);
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 12, 8),
new THREE.MeshBasicMaterial({ color, toneMapped: false })
);
bead.position.set(0.27, 0.82, 0);
group.add(bead);
const light = new THREE.PointLight(color, 16, 3.5, 2);
light.position.y = 0.84;
group.add(light);
group.position.y = SURFACE_Y;
scene.add(group);
return { group, ring, bead, coreMaterial };
}
const emitter1 = createEmitter(0x51e0e6);
const emitter2 = createEmitter(0xff9b62);
// Dashed separation guide.
const separationLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-params.separation / 2, SURFACE_Y + 0.025, 0),
new THREE.Vector3( params.separation / 2, SURFACE_Y + 0.025, 0)
]),
new THREE.LineDashedMaterial({
color: 0x7990a6,
transparent: true,
opacity: 0.35,
dashSize: 0.13,
gapSize: 0.1,
depthWrite: false
})
);
separationLine.computeLineDistances();
scene.add(separationLine);
// Nodal hyperbolas are permanent zero-envelope curves.
const nodeGroup = new THREE.Group();
scene.add(nodeGroup);
const nodeMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166,
toneMapped: false
});
const nodePointMaterial = new THREE.PointsMaterial({
color: 0xffe2a0,
size: 0.072,
sizeAttenuation: true,
transparent: true,
opacity: 0.95,
depthWrite: false,
toneMapped: false
});
function clearNodes() {
for (const child of [...nodeGroup.children]) {
if (child.geometry) child.geometry.dispose();
nodeGroup.remove(child);
}
}
function rebuildNodes() {
clearNodes();
nodalBranchCount = 0;
const k = (Math.PI * 2) / params.wavelength;
const focalDistance = params.separation / 2;
const markerPositions = [];
// Destructive interference:
// k(r1-r2) - phase = (2n+1)pi.
for (let n = -12; n <= 12; n++) {
const signedDifference = ((2 * n + 1) * Math.PI + params.phase) / k;
const absoluteDifference = Math.abs(signedDifference);
if (absoluteDifference >= focalDistance - 0.0001) continue;
const hyperbolaA = absoluteDifference / 2;
const hyperbolaB = Math.sqrt(
Math.max(0, focalDistance * focalDistance - hyperbolaA * hyperbolaA)
);
const side = signedDifference < 0 ? -1 : 1;
const points = [];
for (let i = 0; i <= 260; i++) {
const t = -3.45 + (6.9 * i) / 260;
const x = side * hyperbolaA * Math.cosh(t);
const z = hyperbolaB * Math.sinh(t);
if (
Math.abs(x) <= WIDTH / 2 + 0.025 &&
Math.abs(z) <= DEPTH / 2 + 0.025
) {
points.push(new THREE.Vector3(x, SURFACE_Y + 0.027, z));
}
}
if (points.length < 3) continue;
const curve = new THREE.CatmullRomCurve3(points, false, "centripetal");
const tubeGeometry = new THREE.TubeGeometry(
curve,
Math.min(280, Math.max(42, points.length * 2)),
0.019,
6,
false
);
const tube = new THREE.Mesh(tubeGeometry, nodeMaterial);
tube.renderOrder = 4;
nodeGroup.add(tube);
for (let i = 0; i < points.length; i += 5) {
markerPositions.push(points[i].x, points[i].y + 0.018, points[i].z);
}
nodalBranchCount++;
}
if (markerPositions.length) {
const markerGeometry = new THREE.BufferGeometry();
markerGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(markerPositions, 3)
);
const markers = new THREE.Points(markerGeometry, nodePointMaterial);
markers.renderOrder = 5;
nodeGroup.add(markers);
}
nodeGroup.visible = nodesVisible;
document.getElementById("nodeCount").textContent =
`${nodalBranchCount} nodal branch${nodalBranchCount === 1 ? "" : "es"}`;
}
function updateSeparationLine() {
separationLine.geometry.dispose();
separationLine.geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-params.separation / 2, SURFACE_Y + 0.025, 0),
new THREE.Vector3( params.separation / 2, SURFACE_Y + 0.025, 0)
]);
separationLine.computeLineDistances();
}
function updateParameters() {
surfaceUniforms.uLambda.value = params.wavelength;
surfaceUniforms.uSeparation.value = params.separation;
surfaceUniforms.uPhase.value = params.phase;
emitter1.group.position.x = -params.separation / 2;
emitter2.group.position.x = params.separation / 2;
updateSeparationLine();
rebuildNodes();
document.getElementById("statusLambda").textContent = params.wavelength.toFixed(2);
document.getElementById("statusDistance").textContent = params.separation.toFixed(2);
document.getElementById("statusPhase").textContent =
Math.round(THREE.MathUtils.radToDeg(params.phase));
}
const wavelengthInput = document.getElementById("wavelength");
const separationInput = document.getElementById("separation");
const phaseInput = document.getElementById("phase");
wavelengthInput.addEventListener("input", () => {
params.wavelength = Number(wavelengthInput.value);
document.getElementById("wavelengthValue").textContent =
`${params.wavelength.toFixed(2)} m`;
updateParameters();
});
separationInput.addEventListener("input", () => {
params.separation = Number(separationInput.value);
document.getElementById("separationValue").textContent =
`${params.separation.toFixed(2)} m`;
updateParameters();
});
phaseInput.addEventListener("input", () => {
const degrees = Number(phaseInput.value);
params.phase = THREE.MathUtils.degToRad(degrees);
document.getElementById("phaseValue").textContent =
`${degrees > 0 ? "+" : ""}${degrees}°`;
updateParameters();
});
const nodesButton = document.getElementById("nodesButton");
const pauseButton = document.getElementById("pauseButton");
nodesButton.addEventListener("click", () => {
nodesVisible = !nodesVisible;
nodeGroup.visible = nodesVisible;
nodesButton.classList.toggle("active", nodesVisible);
nodesButton.setAttribute("aria-pressed", String(nodesVisible));
nodesButton.textContent = nodesVisible ? "Nodes On" : "Nodes Off";
});
function togglePause() {
paused = !paused;
pauseButton.classList.toggle("active", paused);
pauseButton.setAttribute("aria-pressed", String(paused));
pauseButton.textContent = paused ? "Resume Field" : "Pause Field";
}
pauseButton.addEventListener("click", togglePause);
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.cos(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.sin(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 15.7;
cameraState.theta = 0.72;
cameraState.phi = 0.91;
cameraState.target.set(0, 0.05, 0);
updateCamera();
}
document.getElementById("resetButton").addEventListener("click", resetCamera);
document.getElementById("defaultsButton").addEventListener("click", () => {
params.wavelength = 0.86;
params.separation = 2.65;
params.phase = 0;
wavelengthInput.value = params.wavelength;
separationInput.value = params.separation;
phaseInput.value = 0;
document.getElementById("wavelengthValue").textContent = "0.86 m";
document.getElementById("separationValue").textContent = "2.65 m";
document.getElementById("phaseValue").textContent = "0°";
updateParameters();
resetCamera();
});
// Direct camera controls: no addons or example modules.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.005,
0.36,
1.42
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
8.5,
24
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "n") {
nodesButton.click();
}
});
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener("resize", resize);
const label1 = document.getElementById("label1");
const label2 = document.getElementById("label2");
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
element.style.transform =
`translate(-50%, -50%) translate(${x.toFixed(1)}px, ${y.toFixed(1)}px)`;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) elapsed += delta;
surfaceUniforms.uTime.value = elapsed;
const carrier = elapsed * 3.2;
const phase = params.phase;
emitter1.bead.position.set(
Math.cos(carrier) * 0.27,
0.82,
Math.sin(carrier) * 0.27
);
emitter2.bead.position.set(
Math.cos(carrier + phase) * 0.27,
0.82,
Math.sin(carrier + phase) * 0.27
);
const pulse1 = 1 + 0.055 * Math.sin(carrier);
const pulse2 = 1 + 0.055 * Math.sin(carrier + phase);
emitter1.ring.scale.setScalar(pulse1);
emitter2.ring.scale.setScalar(pulse2);
emitter1.coreMaterial.emissiveIntensity = 1.3 + 0.28 * Math.sin(carrier);
emitter2.coreMaterial.emissiveIntensity =
1.3 + 0.28 * Math.sin(carrier + phase);
positionLabel(
label1,
emitter1.group.position.clone().add(new THREE.Vector3(0, 1.16, 0))
);
positionLabel(
label2,
emitter2.group.position.clone().add(new THREE.Vector3(0, 1.16, 0))
);
renderer.render(scene, camera);
}
updateCamera();
updateParameters();
animate();
</script>
</body>
</html>
```