Prompt specification
Build or customize the Three.js 3D scene "Two-source phase comparison model" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Two-source phase comparison model
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-source-phase-comparison-model-x1549
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a scene with two identical point sources beside an observation line with calculated phase differences; distinguish constructive and destructive locations under a stated equal-frequency ideal model. Low-poly.
Working Three.js Starter (two-source-phase-comparison-model-x1549):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Two-Source Interference</title>
<style>
:root {
color-scheme: dark;
--bg: #071116;
--panel: rgba(8, 20, 27, 0.88);
--line: rgba(142, 214, 218, 0.18);
--text: #e5f5f4;
--muted: #91adb0;
--constructive: #62f0b1;
--destructive: #ff7282;
--source: #ffd166;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#labels {
position: fixed;
inset: 0;
pointer-events: none;
overflow: hidden;
z-index: 2;
}
.scene-label {
position: absolute;
transform: translate(-50%, -115%);
min-width: 46px;
padding: 4px 7px;
border: 1px solid rgba(255,255,255,0.22);
border-radius: 5px;
background: rgba(4, 12, 16, 0.82);
box-shadow: 0 4px 14px rgba(0,0,0,0.28);
text-align: center;
white-space: nowrap;
font-size: 10px;
line-height: 1.25;
letter-spacing: 0.03em;
backdrop-filter: blur(7px);
}
.scene-label b {
display: block;
font-size: 11px;
}
.scene-label span {
color: var(--muted);
}
.scene-label.constructive {
border-color: rgba(98, 240, 177, 0.7);
color: var(--constructive);
}
.scene-label.destructive {
border-color: rgba(255, 114, 130, 0.72);
color: var(--destructive);
}
.scene-label.source {
border-color: rgba(255, 209, 102, 0.72);
color: var(--source);
}
header {
position: fixed;
top: 18px;
left: 20px;
z-index: 5;
max-width: min(560px, calc(100vw - 420px));
pointer-events: none;
text-shadow: 0 2px 12px #000;
}
.eyebrow {
margin-bottom: 7px;
color: var(--source);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.035em;
}
header p {
margin: 10px 0 0;
max-width: 540px;
color: #a9c4c6;
font-size: 13px;
line-height: 1.5;
}
#panel {
position: fixed;
top: 16px;
right: 16px;
z-index: 6;
width: 360px;
max-height: calc(100vh - 32px);
overflow: auto;
padding: 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 20px 55px rgba(0,0,0,0.36);
backdrop-filter: blur(16px);
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 12px;
}
.panel-title h2 {
margin: 0;
font-size: 15px;
letter-spacing: 0.01em;
}
.chip {
flex: none;
padding: 4px 7px;
border: 1px solid rgba(98, 240, 177, 0.35);
border-radius: 999px;
color: var(--constructive);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.model-note {
margin: 0 0 13px;
color: var(--muted);
font-size: 12px;
line-height: 1.48;
}
.formula {
margin: 11px 0 14px;
padding: 11px 12px;
border: 1px solid rgba(142,214,218,0.14);
border-radius: 9px;
background: rgba(16, 42, 49, 0.48);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.75;
color: #d5eeee;
}
.formula .condition {
color: var(--constructive);
}
.formula .cancel {
color: var(--destructive);
}
.parameters {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 14px;
}
.parameter {
padding: 8px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255,255,255,0.025);
}
.parameter small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.09em;
}
.parameter strong {
font-size: 13px;
}
.table-wrap {
max-height: 230px;
overflow: auto;
border: 1px solid var(--line);
border-radius: 9px;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 11px;
}
th, td {
padding: 7px 8px;
border-bottom: 1px solid rgba(142,214,218,0.09);
text-align: right;
white-space: nowrap;
}
th {
position: sticky;
top: 0;
z-index: 1;
background: #0d2028;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
th:first-child, td:first-child { text-align: left; }
tr:last-child td { border-bottom: 0; }
td.c { color: var(--constructive); }
td.d { color: var(--destructive); }
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 13px;
color: var(--muted);
font-size: 10px;
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
transform: rotate(45deg);
border-radius: 2px;
}
.swatch.c { background: var(--constructive); }
.swatch.d { background: var(--destructive); border-radius: 50%; }
.swatch.s { background: var(--source); border-radius: 50%; }
#help {
position: fixed;
left: 18px;
bottom: 16px;
z-index: 5;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(7,17,22,0.72);
color: var(--muted);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(10px);
}
#reset {
position: fixed;
right: 18px;
bottom: 16px;
z-index: 7;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(7,17,22,0.88);
color: var(--text);
font: inherit;
font-size: 10px;
cursor: pointer;
}
#reset:hover {
border-color: rgba(142,214,218,0.45);
background: #102630;
}
@media (max-width: 760px) {
header {
top: 12px;
left: 14px;
max-width: calc(100vw - 28px);
}
header p { display: none; }
h1 { font-size: 25px; }
#panel {
top: auto;
right: 10px;
bottom: 10px;
width: min(360px, calc(100vw - 20px));
max-height: 42vh;
padding: 13px;
}
.formula, .parameters { display: none; }
.table-wrap { max-height: 150px; }
#help { display: none; }
#reset { top: 75px; bottom: auto; right: 12px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header>
<div class="eyebrow">Coherent pair / interference field</div>
<h1>Path difference becomes phase.</h1>
<p>
Two identical point sources sit beside a finite observation line.
Green diamonds mark constructive interference; red tetrahedra mark
destructive interference.
</p>
</header>
<aside id="panel">
<div class="panel-title">
<h2>Equal-frequency ideal model</h2>
<span class="chip">Coherent</span>
</div>
<p class="model-note">
The sources are identical, equal-amplitude, equal-frequency, and fixed in
phase at t = 0. Propagation is lossless and the observation line is
parallel to the source baseline. With equal amplitudes, destructive
interference gives complete cancellation.
</p>
<div class="formula">
r<sub>1,2</sub>(x) = √((x ∓ d/2)² + h²)<br />
Δr = |r₁ − r₂|<br />
Δφ = 2πΔr/λ mod 2π<br />
<span class="condition">Constructive: Δr = mλ → Δφ = 0°</span><br />
<span class="cancel">Destructive: Δr = (m+½)λ → Δφ = 180°</span>
</div>
<div class="parameters">
<div class="parameter">
<small>Wavelength λ</small>
<strong>1.00 u</strong>
</div>
<div class="parameter">
<small>Separation d</small>
<strong>3.60 u</strong>
</div>
<div class="parameter">
<small>Offset h</small>
<strong>2.00 u</strong>
</div>
</div>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Point</th>
<th>x</th>
<th>|Δr|</th>
<th>Δφ</th>
</tr>
</thead>
<tbody id="results"></tbody>
</table>
</div>
<div class="legend">
<span><i class="swatch s"></i> Source</span>
<span><i class="swatch c"></i> Constructive</span>
<span><i class="swatch d"></i> Destructive</span>
</div>
</aside>
<div id="help">Drag to orbit · wheel or pinch to zoom · double-click to reset</div>
<button id="reset" type="button">Reset camera</button>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Model constants
// ---------------------------------------------------------------------
const MODEL = Object.freeze({
wavelength: 1.0,
separation: 3.6,
observationOffset: 2.0,
lineHalfLength: 5.2,
height: 0.55
});
const SOURCE_X = MODEL.separation / 2;
const SOURCE_Y = MODEL.height;
const OBS_Z = MODEL.observationOffset;
const OBS_Y = MODEL.height;
const COLORS = {
constructive: 0x62f0b1,
destructive: 0xff7282,
source: 0xffd166,
observation: 0xa8dadd
};
// ---------------------------------------------------------------------
// Renderer, camera, lighting
// ---------------------------------------------------------------------
const host = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.FogExp2(0x071116, 0.037);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
radius: 11.2,
theta: 0.64,
phi: 0.94,
target: new THREE.Vector3(0, 0.35, 0.72)
};
function updateCamera() {
const { radius, theta, phi, target } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xbceef0, 0x172329, 1.55));
const keyLight = new THREE.DirectionalLight(0xffefd0, 2.25);
keyLight.position.set(-5, 10, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.bias = -0.001;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x52cbd2, 1.15);
rimLight.position.set(7, 4, -7);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Low-poly ground
// ---------------------------------------------------------------------
const terrainGeometry = new THREE.PlaneGeometry(28, 18, 14, 9);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
const terrainColors = [];
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
const rough =
0.07 * Math.sin(x * 0.48 + z * 0.2) +
0.045 * Math.cos(z * 0.75 - x * 0.18);
const central = Math.abs(x) < 6.2 && z > -3.2 && z < 4.2;
const y = central ? -0.025 : rough - 0.025;
terrainPositions.setY(i, y);
const c = new THREE.Color().setHSL(
0.51,
0.28,
0.115 + 0.018 * Math.sin(x * 0.55 + z * 0.3)
);
terrainColors.push(c.r, c.g, c.b);
}
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
flatShading: true,
roughness: 0.96,
metalness: 0.02
})
);
terrain.receiveShadow = true;
scene.add(terrain);
const grid = new THREE.GridHelper(26, 26, 0x25565d, 0x173b42);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.22;
grid.material.depthWrite = false;
scene.add(grid);
// Low-poly environmental facets, kept away from the experiment.
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x16343a,
flatShading: true,
roughness: 1
});
const rockData = [
[-8.2, -4.6, 1.2, 1.1],
[-6.4, 5.3, 0.9, 1.5],
[ 7.8, -3.8, 1.4, 1.0],
[ 6.2, 5.5, 0.8, 1.2],
[-10.0, 1.8, 0.7, 0.9],
[ 9.5, 2.4, 0.8, 1.3]
];
for (const [x, z, radius, height] of rockData) {
const rock = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 5, 1),
rockMaterial
);
rock.position.set(x, height / 2 - 0.03, z);
rock.rotation.y = x * 0.71 + z;
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
// ---------------------------------------------------------------------
// Exact path-difference calculation
// ---------------------------------------------------------------------
function distanceFromLeftSource(x) {
return Math.hypot(x + SOURCE_X, OBS_Z);
}
function distanceFromRightSource(x) {
return Math.hypot(x - SOURCE_X, OBS_Z);
}
// Signed path difference: right-source distance minus left-source distance.
function signedDelta(x) {
return distanceFromRightSource(x) - distanceFromLeftSource(x);
}
function absoluteDelta(x) {
return Math.abs(signedDelta(x));
}
function findPositivePosition(targetDelta) {
const maximum = absoluteDelta(MODEL.lineHalfLength);
if (targetDelta > maximum + 1e-9) return null;
let low = 0;
let high = MODEL.lineHalfLength;
for (let i = 0; i < 70; i++) {
const middle = (low + high) / 2;
if (absoluteDelta(middle) < targetDelta) {
low = middle;
} else {
high = middle;
}
}
return (low + high) / 2;
}
const markers = [];
const maximumOrder = absoluteDelta(MODEL.lineHalfLength) / MODEL.wavelength;
// Constructive points: |Δr| = mλ.
for (let m = 0; m <= Math.floor(maximumOrder + 1e-8); m++) {
const target = m * MODEL.wavelength;
if (m === 0) {
markers.push(makeMarker(0, "C0", "constructive", m));
} else {
const positiveX = findPositivePosition(target);
if (positiveX !== null) {
markers.push(makeMarker(positiveX, `C${m}`, "constructive", m));
markers.push(makeMarker(-positiveX, `C${m}`, "constructive", m));
}
}
}
// Destructive points: |Δr| = (m + 1/2)λ.
for (
let m = 0;
m + 0.5 <= maximumOrder + 1e-8;
m++
) {
const target = (m + 0.5) * MODEL.wavelength;
const positiveX = findPositivePosition(target);
if (positiveX !== null) {
markers.push(makeMarker(positiveX, `D${m}`, "destructive", m));
markers.push(makeMarker(-positiveX, `D${m}`, "destructive", m));
}
}
function makeMarker(x, name, type, order) {
const signed = signedDelta(x);
const gap = Math.abs(signed);
let phase = THREE.MathUtils.euclideanModulo(
360 * signed / MODEL.wavelength,
360
);
// Avoid displaying a floating-point 360° at constructive points.
if (Math.abs(phase - 360) < 1e-8 || Math.abs(phase) < 1e-8) {
phase = 0;
}
return {
x,
name,
type,
order,
gap,
phase,
rLeft: distanceFromLeftSource(x),
rRight: distanceFromRightSource(x)
};
}
markers.sort((a, b) => a.x - b.x);
// ---------------------------------------------------------------------
// Sources, wave shells, axes, and observation line
// ---------------------------------------------------------------------
const sourceCenters = [];
const waveShells = [];
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x18323a,
roughness: 0.85,
metalness: 0.15,
flatShading: true
});
const sourceMaterial = new THREE.MeshStandardMaterial({
color: COLORS.source,
emissive: 0x4a2b00,
emissiveIntensity: 0.7,
roughness: 0.36,
metalness: 0.32,
flatShading: true
});
function addSource(x, labelText) {
const center = new THREE.Vector3(x, SOURCE_Y, 0);
sourceCenters.push({ center, labelText });
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.42, 0.52, 0.22, 6),
pedestalMaterial
);
pedestal.position.set(x, 0.11, 0);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.065, 0.34, 6),
pedestalMaterial
);
stem.position.set(x, 0.34, 0);
scene.add(stem);
const source = new THREE.Mesh(
new THREE.OctahedronGeometry(0.32, 0),
sourceMaterial
);
source.position.copy(center);
source.castShadow = true;
scene.add(source);
const light = new THREE.PointLight(COLORS.source, 2.4, 4.2, 2);
light.position.copy(center);
scene.add(light);
// Low-poly spherical wavefronts. Both sources use identical timing.
for (let i = 0; i < 3; i++) {
const shellMaterial = new THREE.MeshBasicMaterial({
color: COLORS.source,
wireframe: true,
transparent: true,
opacity: 0.12,
depthWrite: false
});
const shell = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
shellMaterial
);
shell.position.copy(center);
scene.add(shell);
waveShells.push({ mesh: shell, phaseOffset: i / 3 });
}
}
addSource(-SOURCE_X, "S₁");
addSource( SOURCE_X, "S₂");
// Source-to-source baseline.
const baselineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-SOURCE_X, SOURCE_Y, 0),
new THREE.Vector3( SOURCE_X, SOURCE_Y, 0)
]);
const baseline = new THREE.Line(
baselineGeometry,
new THREE.LineDashedMaterial({
color: 0x806c43,
dashSize: 0.12,
gapSize: 0.1,
transparent: true,
opacity: 0.75
})
);
baseline.computeLineDistances();
scene.add(baseline);
// Main observation line.
const observationGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-MODEL.lineHalfLength, OBS_Y, OBS_Z),
new THREE.Vector3( MODEL.lineHalfLength, OBS_Y, OBS_Z)
]);
const observationLine = new THREE.Line(
observationGeometry,
new THREE.LineDashedMaterial({
color: COLORS.observation,
dashSize: 0.18,
gapSize: 0.11,
transparent: true,
opacity: 0.85
})
);
observationLine.computeLineDistances();
scene.add(observationLine);
// End supports and unit ticks.
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x26454d,
roughness: 0.8,
flatShading: true
});
for (const x of [-MODEL.lineHalfLength, MODEL.lineHalfLength]) {
const support = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.055, OBS_Y, 6),
supportMaterial
);
support.position.set(x, OBS_Y / 2, OBS_Z);
scene.add(support);
}
const tickPoints = [];
for (let x = -5; x <= 5; x++) {
tickPoints.push(
new THREE.Vector3(x, OBS_Y, OBS_Z - 0.08),
new THREE.Vector3(x, OBS_Y, OBS_Z + 0.08)
);
}
scene.add(
new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(tickPoints),
new THREE.LineBasicMaterial({
color: 0x739da0,
transparent: true,
opacity: 0.48
})
)
);
// Exact source-to-marker path lines.
const pathPoints = [];
for (const marker of markers) {
const endpoint = new THREE.Vector3(marker.x, OBS_Y, OBS_Z);
pathPoints.push(
new THREE.Vector3(-SOURCE_X, SOURCE_Y, 0), endpoint.clone(),
new THREE.Vector3( SOURCE_X, SOURCE_Y, 0), endpoint
);
}
const paths = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints(pathPoints),
new THREE.LineBasicMaterial({
color: 0x8ac9ca,
transparent: true,
opacity: 0.15,
depthWrite: false
})
);
scene.add(paths);
// ---------------------------------------------------------------------
// Constructive/destructive marker geometry and labels
// ---------------------------------------------------------------------
const labelContainer = document.getElementById("labels");
const resultBody = document.getElementById("results");
const projected = new THREE.Vector3();
const constructiveMaterial = new THREE.MeshStandardMaterial({
color: COLORS.constructive,
emissive: 0x0b6b47,
emissiveIntensity: 0.75,
roughness: 0.35,
metalness: 0.18,
flatShading: true
});
const destructiveMaterial = new THREE.MeshStandardMaterial({
color: COLORS.destructive,
emissive: 0x741a28,
emissiveIntensity: 0.65,
roughness: 0.42,
metalness: 0.15,
flatShading: true
});
const markerStemMaterial = new THREE.MeshStandardMaterial({
color: 0x416068,
roughness: 0.8,
flatShading: true
});
function addMarker(marker) {
const isConstructive = marker.type === "constructive";
const material = isConstructive
? constructiveMaterial
: destructiveMaterial;
const geometry = isConstructive
? new THREE.OctahedronGeometry(0.135, 0)
: new THREE.TetrahedronGeometry(0.17, 0);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(marker.x, OBS_Y + 0.19, OBS_Z);
mesh.rotation.set(0.2, marker.order * 0.43, 0.15);
mesh.castShadow = true;
scene.add(mesh);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(
isConstructive ? 0.018 : 0.025,
isConstructive ? 0.018 : 0.025,
0.19,
5
),
markerStemMaterial
);
stem.position.set(marker.x, OBS_Y + 0.095, OBS_Z);
scene.add(stem);
const label = document.createElement("div");
label.className = `scene-label ${marker.type}`;
label.innerHTML =
`<b>${marker.name}</b><span>Δφ ${Math.round(marker.phase)}°</span>`;
labelContainer.appendChild(label);
marker.mesh = mesh;
marker.label = label;
marker.labelPosition = new THREE.Vector3(
marker.x,
OBS_Y + 0.43,
OBS_Z
);
const row = document.createElement("tr");
row.innerHTML =
`<td class="${isConstructive ? "c" : "d"}">${marker.name}</td>` +
`<td>${marker.x.toFixed(3)}</td>` +
`<td>${marker.gap.toFixed(3)} u</td>` +
`<td class="${isConstructive ? "c" : "d"}">${Math.round(marker.phase)}°</td>`;
resultBody.appendChild(row);
}
markers.forEach(addMarker);
for (const source of sourceCenters) {
const label = document.createElement("div");
label.className = "scene-label source";
label.innerHTML = `<b>${source.labelText}</b><span>equal phase</span>`;
labelContainer.appendChild(label);
source.label = label;
source.labelPosition = source.center.clone().add(
new THREE.Vector3(0, 0.55, 0)
);
}
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// ---------------------------------------------------------------------
// Direct camera controls: drag orbit, wheel zoom, pinch zoom
// ---------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = null;
function resetCamera() {
cameraState.radius = 11.2;
cameraState.theta = 0.64;
cameraState.phi = 0.94;
cameraState.target.set(0, 0.35, 0.72);
}
function pinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return null;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", event => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
previousPinchDistance = pinchDistance();
}
});
canvas.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.25,
1.35
);
} else if (pointers.size === 2) {
const currentDistance = pinchDistance();
if (currentDistance && previousPinchDistance) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * previousPinchDistance / currentDistance,
5.2,
18
);
}
previousPinchDistance = currentDistance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? pinchDistance() : null;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.2,
18
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const waveProgress = (elapsed * 0.30) % 1;
for (const shell of waveShells) {
const progress = (waveProgress + shell.phaseOffset) % 1;
const radius = 0.42 + progress * 3.25;
shell.mesh.scale.setScalar(radius);
shell.mesh.material.opacity = (1 - progress) * 0.13;
shell.mesh.rotation.y = elapsed * 0.08;
shell.mesh.rotation.x = elapsed * 0.045;
}
for (const marker of markers) {
marker.mesh.rotation.y += 0.006;
marker.mesh.rotation.z += 0.003;
positionLabel(marker.label, marker.labelPosition);
}
for (const source of sourceCenters) {
positionLabel(source.label, source.labelPosition);
}
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```