Prompt specification
Build or customize the Three.js 3D scene "Corner mirror return-path demonstration" 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: Corner mirror return-path demonstration
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/corner-mirror-return-path-demonstration-x1570
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing light reflection from mirrors. Arrange two perpendicular mirrors and trace a ray through both reflections in a plane; show the resulting direction without claiming a full three-dimensional retroreflector. Stylized solid forms.
Working Three.js Starter (corner-mirror-return-path-demonstration-x1570):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two-Mirror Planar Reflection</title>
<style>
:root {
color-scheme: dark;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 24%, #173b50 0%, transparent 34%),
radial-gradient(circle at 22% 80%, #14263f 0%, transparent 38%),
linear-gradient(145deg, #09111f 0%, #0b1727 52%, #07101c 100%);
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
width: min(370px, calc(100vw - 48px));
padding: 20px 22px;
border: 1px solid rgba(157, 226, 255, 0.2);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(13, 29, 45, 0.86), rgba(8, 18, 31, 0.78));
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.34),
inset 0 1px rgba(255, 255, 255, 0.05);
backdrop-filter: blur(18px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: #79ddff;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #f1f8ff;
font-size: clamp(24px, 3vw, 34px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 17px;
color: #aac0d3;
font-size: 14px;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
padding: 12px 14px;
border: 1px solid rgba(119, 227, 207, 0.22);
border-radius: 12px;
background: rgba(36, 122, 111, 0.12);
}
.equation strong {
color: #91f3d7;
font-size: 22px;
font-weight: 650;
letter-spacing: -0.02em;
}
.equation span {
color: #a9c9c7;
font-size: 11px;
line-height: 1.35;
text-align: right;
}
.note {
margin: 14px 0 0;
color: #91a8bc;
font-size: 12px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 16px;
margin-top: 16px;
padding-top: 14px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #b6c8d8;
font-size: 11px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 19px;
height: 3px;
border-radius: 4px;
box-shadow: 0 0 8px currentColor;
}
.swatch.ray {
color: #ffd166;
background: currentColor;
}
.swatch.incoming {
color: #78a9ff;
background: currentColor;
}
.swatch.outgoing {
color: #77efd0;
background: currentColor;
}
.swatch.normal {
color: #66d9ff;
background: repeating-linear-gradient(
90deg,
currentColor 0 4px,
transparent 4px 7px
);
}
#labels {
position: fixed;
z-index: 4;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.pin {
position: absolute;
left: 0;
top: 0;
padding: 5px 8px;
border: 1px solid rgba(174, 225, 250, 0.22);
border-radius: 7px;
background: rgba(7, 17, 30, 0.78);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
color: #d9edf9;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
white-space: nowrap;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.pin.mirror {
color: #bdefff;
border-color: rgba(102, 217, 255, 0.34);
}
.pin.reflect {
color: #ffe29a;
border-color: rgba(255, 209, 102, 0.35);
}
.pin.vector-in {
color: #9ebcff;
border-color: rgba(120, 169, 255, 0.4);
}
.pin.vector-out {
color: #9af5da;
border-color: rgba(119, 239, 208, 0.4);
}
.pin.subtle {
color: #8eb6c9;
background: rgba(7, 17, 30, 0.58);
font-weight: 600;
}
.controls {
position: fixed;
z-index: 6;
right: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 10px;
}
.hint {
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(8, 18, 30, 0.65);
color: #8ea6ba;
font-size: 11px;
backdrop-filter: blur(10px);
pointer-events: none;
}
button {
padding: 10px 13px;
border: 1px solid rgba(121, 221, 255, 0.28);
border-radius: 10px;
background: rgba(16, 43, 60, 0.88);
color: #c9f0ff;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(10px);
}
button:hover {
border-color: rgba(121, 221, 255, 0.55);
background: rgba(21, 57, 76, 0.95);
}
@media (max-width: 650px) {
.hud {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 16px;
}
.note {
display: none;
}
.legend {
margin-top: 12px;
padding-top: 10px;
}
.controls {
right: 14px;
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="labels" aria-hidden="true">
<div id="pin-m1" class="pin mirror">M₁</div>
<div id="pin-m2" class="pin mirror">M₂</div>
<div id="pin-r1" class="pin reflect">R₁</div>
<div id="pin-r2" class="pin reflect">R₂</div>
<div id="pin-v0" class="pin vector-in">v₀</div>
<div id="pin-v2" class="pin vector-out">v₂ = −v₀</div>
<div id="pin-plane" class="pin subtle">RAY PLANE</div>
<div id="pin-angle" class="pin subtle">90°</div>
<div id="pin-source" class="pin subtle">SOURCE</div>
</div>
<section class="hud">
<div class="eyebrow">Optics study / planar construction</div>
<h1>Two perpendicular mirrors</h1>
<p class="subtitle">One ray traced through both reflections.</p>
<div class="equation">
<strong>v₂ = −v₀</strong>
<span>direction reversed<br>inside the ray plane</span>
</div>
<p class="note">
Only the horizontal ray plane is modeled. Reflections from two mirrors
meeting at 90° reverse the planar direction. This is a two-dimensional
construction, not a claim of full three-dimensional retroreflection.
</p>
<div class="legend">
<span class="legend-item">
<i class="swatch ray"></i> light path
</span>
<span class="legend-item">
<i class="swatch incoming"></i> incoming vector
</span>
<span class="legend-item">
<i class="swatch outgoing"></i> resulting vector
</span>
<span class="legend-item">
<i class="swatch normal"></i> mirror normal
</span>
</div>
</section>
<div class="controls">
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset</div>
<button id="reset-view" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional diagram of a ray reflecting from two perpendicular mirrors"
);
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x091321, 0.027);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Camera controls are implemented directly, without imported add-ons.
const orbit = {
target: new THREE.Vector3(1.75, 1.05, 1.55),
radius: 9.6,
azimuth: 0.72,
elevation: 0.72,
minRadius: 6.2,
maxRadius: 15,
minElevation: 0.25,
maxElevation: 1.25
};
function updateCamera() {
const c = Math.cos(orbit.elevation);
const s = Math.sin(orbit.elevation);
camera.position.set(
orbit.target.x + orbit.radius * c * Math.sin(orbit.azimuth),
orbit.target.y + orbit.radius * s,
orbit.target.z + orbit.radius * c * Math.cos(orbit.azimuth)
);
camera.lookAt(orbit.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xbceaff, 0x111827, 1.8));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.3);
keyLight.position.set(5, 9, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x72caff, 1.2);
fillLight.position.set(-4, 5, -3);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function roundedRectShape(width, height, radius) {
const w = width / 2;
const h = height / 2;
const r = Math.min(radius, w, h);
const shape = new THREE.Shape();
shape.moveTo(-w + r, -h);
shape.lineTo(w - r, -h);
shape.quadraticCurveTo(w, -h, w, -h + r);
shape.lineTo(w, h - r);
shape.quadraticCurveTo(w, h, w - r, h);
shape.lineTo(-w + r, h);
shape.quadraticCurveTo(-w, h, -w, h - r);
shape.lineTo(-w, -h + r);
shape.quadraticCurveTo(-w, -h, -w + r, -h);
shape.closePath();
return shape;
}
function roundedSolid(width, height, depth, radius, material) {
const geometry = new THREE.ExtrudeGeometry(
roundedRectShape(width, height, radius),
{
depth,
bevelEnabled: false,
curveSegments: 10,
steps: 1
}
);
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinderBetween(start, end, radius, material, radialSegments = 16) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
return mesh;
}
function coneArrow(position, direction, radius, height, material) {
const mesh = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 18),
material
);
mesh.position.copy(position);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
return mesh;
}
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x17384d,
metalness: 0.58,
roughness: 0.29
});
const footMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1c2a,
metalness: 0.5,
roughness: 0.36
});
const mirrorMaterial = new THREE.MeshPhysicalMaterial({
color: 0x9eddf1,
emissive: 0x103849,
emissiveIntensity: 0.38,
metalness: 0.72,
roughness: 0.16,
clearcoat: 0.9,
clearcoatRoughness: 0.12,
side: THREE.DoubleSide
});
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xb9e9f4,
metalness: 0.85,
roughness: 0.2
});
const beamCoreMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166
});
const beamGlowMaterial = new THREE.MeshBasicMaterial({
color: 0xffb52e,
transparent: true,
opacity: 0.22,
depthWrite: false
});
// -------------------------------------------------------------------------
// Studio floor and plinth
// -------------------------------------------------------------------------
const shadowFloor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.ShadowMaterial({
color: 0x02070d,
opacity: 0.48
})
);
shadowFloor.rotation.x = -Math.PI / 2;
shadowFloor.position.y = -0.31;
shadowFloor.receiveShadow = true;
scene.add(shadowFloor);
const plinth = new THREE.Mesh(
new THREE.CylinderGeometry(5.85, 6.05, 0.24, 96),
new THREE.MeshStandardMaterial({
color: 0x101f2f,
metalness: 0.18,
roughness: 0.62
})
);
plinth.position.set(1.7, -0.17, 1.45);
plinth.receiveShadow = true;
plinth.castShadow = true;
scene.add(plinth);
const floorRing = new THREE.Mesh(
new THREE.RingGeometry(5.28, 5.31, 128),
new THREE.MeshBasicMaterial({
color: 0x4abbd9,
transparent: true,
opacity: 0.18,
side: THREE.DoubleSide
})
);
floorRing.rotation.x = -Math.PI / 2;
floorRing.position.set(1.7, -0.045, 1.45);
scene.add(floorRing);
// -------------------------------------------------------------------------
// The two perpendicular mirrors
//
// M1 occupies z = 0 and extends along +x.
// M2 occupies x = 0 and extends along +z.
// Their reflective faces meet at the origin.
// -------------------------------------------------------------------------
const glintMaterials = [];
function createMirror() {
const group = new THREE.Group();
const frame = roundedSolid(4.7, 2.78, 0.24, 0.17, frameMaterial);
frame.position.y = 1.46;
group.add(frame);
const faceGeometry = new THREE.ShapeGeometry(
roundedRectShape(4.25, 2.31, 0.12),
12
);
const face = new THREE.Mesh(faceGeometry, mirrorMaterial);
face.position.set(0, 1.46, 0.132);
face.receiveShadow = true;
group.add(face);
// Stylized diagonal reflection streaks.
const streakA = new THREE.Shape();
streakA.moveTo(-2.00, -0.97);
streakA.lineTo(-1.56, -0.97);
streakA.lineTo(1.56, 0.97);
streakA.lineTo(1.02, 0.97);
streakA.closePath();
const streakB = new THREE.Shape();
streakB.moveTo(-1.22, -0.97);
streakB.lineTo(-1.01, -0.97);
streakB.lineTo(1.98, 0.97);
streakB.lineTo(1.77, 0.97);
streakB.closePath();
const glintMaterialA = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.16,
depthWrite: false,
side: THREE.DoubleSide
});
const glintMaterialB = glintMaterialA.clone();
glintMaterialB.opacity = 0.08;
glintMaterials.push(glintMaterialA, glintMaterialB);
const glintMeshA = new THREE.Mesh(
new THREE.ShapeGeometry(streakA),
glintMaterialA
);
glintMeshA.position.set(0, 1.46, 0.143);
const glintMeshB = new THREE.Mesh(
new THREE.ShapeGeometry(streakB),
glintMaterialB
);
glintMeshB.position.set(0, 1.46, 0.145);
group.add(glintMeshA, glintMeshB);
// Four small fasteners on the reflective frame.
const boltGeometry = new THREE.CylinderGeometry(
0.046,
0.046,
0.034,
16
);
for (const x of [-2.13, 2.13]) {
for (const y of [0.46, 2.46]) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.rotation.x = Math.PI / 2;
bolt.position.set(x, y, 0.153);
group.add(bolt);
}
}
// Solid horizontal foot.
const foot = roundedSolid(4.88, 0.52, 0.18, 0.08, footMaterial);
foot.rotation.x = -Math.PI / 2;
foot.position.set(0, 0.10, 0.12);
group.add(foot);
return group;
}
const mirror1 = createMirror();
mirror1.position.set(2.35, 0, -0.12);
scene.add(mirror1);
const mirror2 = createMirror();
mirror2.rotation.y = Math.PI / 2;
mirror2.position.set(-0.12, 0, 2.35);
scene.add(mirror2);
// -------------------------------------------------------------------------
// Subtle ray-plane grid at the height of the horizontal ray
// -------------------------------------------------------------------------
const rayPlaneGrid = new THREE.GridHelper(
4.7,
10,
0x62cce7,
0x376a80
);
rayPlaneGrid.position.set(2.35, 1.225, 2.35);
rayPlaneGrid.material.transparent = true;
rayPlaneGrid.material.opacity = 0.12;
rayPlaneGrid.material.depthWrite = false;
scene.add(rayPlaneGrid);
// -------------------------------------------------------------------------
// Ray tracing in the x-z plane
// -------------------------------------------------------------------------
const rayHeight = 1.25;
const rayStart = new THREE.Vector3(4.45, rayHeight, 2.35);
const incomingDirection = new THREE.Vector3(-0.94, 0, -0.342).normalize();
// First reflection: vertical mirror M2 at x = 0.
const t1 = -rayStart.x / incomingDirection.x;
const reflection1 = rayStart.clone().addScaledVector(incomingDirection, t1);
const middleDirection = incomingDirection.clone();
middleDirection.x *= -1;
// Second reflection: horizontal mirror M1 at z = 0.
const t2 = -reflection1.z / middleDirection.z;
const reflection2 = reflection1.clone().addScaledVector(middleDirection, t2);
const outgoingDirection = middleDirection.clone();
outgoingDirection.z *= -1;
// The result is exactly opposite to the original planar direction.
const outgoingLength = 2.9;
const rayEnd = reflection2.clone().addScaledVector(
outgoingDirection,
outgoingLength
);
const raySegments = [
[rayStart, reflection1],
[reflection1, reflection2],
[reflection2, rayEnd]
];
for (const [start, end] of raySegments) {
const glow = cylinderBetween(
start,
end,
0.052,
beamGlowMaterial,
20
);
glow.castShadow = false;
scene.add(glow);
const core = cylinderBetween(
start,
end,
0.017,
beamCoreMaterial,
16
);
core.castShadow = false;
scene.add(core);
}
// Arrowheads directly on the light path.
const pathArrowMaterial = new THREE.MeshBasicMaterial({
color: 0xffdf88
});
scene.add(
coneArrow(
rayStart.clone().lerp(reflection1, 0.69),
incomingDirection,
0.105,
0.28,
pathArrowMaterial
)
);
scene.add(
coneArrow(
reflection1.clone().lerp(reflection2, 0.57),
middleDirection,
0.105,
0.28,
pathArrowMaterial
)
);
scene.add(
coneArrow(
reflection2.clone().lerp(rayEnd, 0.58),
outgoingDirection,
0.11,
0.30,
pathArrowMaterial
)
);
// -------------------------------------------------------------------------
// Incoming and outgoing vector arrows
// -------------------------------------------------------------------------
const incomingArrow = new THREE.ArrowHelper(
incomingDirection,
rayStart.clone().add(new THREE.Vector3(0, 0.27, 0)),
rayStart.distanceTo(reflection1) * 0.82,
0x78a9ff,
0.28,
0.16
);
const outgoingArrow = new THREE.ArrowHelper(
outgoingDirection,
reflection2.clone().add(new THREE.Vector3(0, 0.27, 0)),
2.62,
0x77efd0,
0.29,
0.17
);
scene.add(incomingArrow, outgoingArrow);
// -------------------------------------------------------------------------
// Reflection markers and dashed mirror normals
// -------------------------------------------------------------------------
const reflectionMaterial = new THREE.MeshStandardMaterial({
color: 0xffe19a,
emissive: 0xffa928,
emissiveIntensity: 2.3,
metalness: 0.15,
roughness: 0.22
});
const markerGeometry = new THREE.SphereGeometry(0.082, 24, 16);
const marker1 = new THREE.Mesh(markerGeometry, reflectionMaterial);
const marker2 = new THREE.Mesh(markerGeometry, reflectionMaterial);
marker1.position.copy(reflection1);
marker2.position.copy(reflection2);
scene.add(marker1, marker2);
const pulseRingGeometry = new THREE.TorusGeometry(
0.18,
0.016,
10,
48
);
const pulseRingMaterial = new THREE.MeshBasicMaterial({
color: 0xffd166,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const pulseRing1 = new THREE.Mesh(pulseRingGeometry, pulseRingMaterial);
const pulseRing2 = new THREE.Mesh(pulseRingGeometry, pulseRingMaterial);
pulseRing1.rotation.x = Math.PI / 2;
pulseRing2.rotation.x = Math.PI / 2;
pulseRing1.position.copy(reflection1);
pulseRing2.position.copy(reflection2);
scene.add(pulseRing1, pulseRing2);
const normalMaterial = new THREE.LineDashedMaterial({
color: 0x66d9ff,
dashSize: 0.09,
gapSize: 0.065,
transparent: true,
opacity: 0.72
});
const normal1 = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
reflection1.clone(),
reflection1.clone().add(new THREE.Vector3(1.15, 0.015, 0))
]),
normalMaterial
);
const normal2 = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
reflection2.clone(),
reflection2.clone().add(new THREE.Vector3(0, 0.015, 1.15))
]),
normalMaterial
);
normal1.computeLineDistances();
normal2.computeLineDistances();
scene.add(normal1, normal2);
// A 90-degree marker on the floor beneath the mirror corner.
const anglePoints = [];
for (let i = 0; i <= 32; i++) {
const angle = (i / 32) * Math.PI * 0.5;
anglePoints.push(
new THREE.Vector3(
Math.cos(angle) * 0.78,
0.012,
Math.sin(angle) * 0.78
)
);
}
const angleArc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(anglePoints),
new THREE.LineBasicMaterial({
color: 0x61d9ff,
transparent: true,
opacity: 0.58
})
);
scene.add(angleArc);
// -------------------------------------------------------------------------
// Stylized ray source
// -------------------------------------------------------------------------
const sourceBodyPosition = rayStart.clone().addScaledVector(
incomingDirection,
-0.36
);
const sourceHousing = roundedSolid(
0.56,
0.42,
0.52,
0.12,
new THREE.MeshStandardMaterial({
color: 0x27485b,
metalness: 0.68,
roughness: 0.24
})
);
sourceHousing.position.copy(sourceBodyPosition);
sourceHousing.rotation.y = Math.atan2(
incomingDirection.x,
incomingDirection.z
);
scene.add(sourceHousing);
const nozzleStart = sourceBodyPosition.clone().addScaledVector(
incomingDirection,
0.18
);
const nozzle = cylinderBetween(
nozzleStart,
rayStart,
0.115,
new THREE.MeshStandardMaterial({
color: 0x102b3a,
metalness: 0.78,
roughness: 0.22
}),
20
);
scene.add(nozzle);
const aperture = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 24, 16),
new THREE.MeshBasicMaterial({ color: 0xffe8a4 })
);
aperture.position.copy(rayStart);
scene.add(aperture);
const sourceBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.34, 0.13, 32),
footMaterial
);
sourceBase.position.set(
sourceBodyPosition.x,
0.08,
sourceBodyPosition.z
);
sourceBase.castShadow = true;
scene.add(sourceBase);
const sourcePole = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.075, 0.94, 20),
footMaterial
);
sourcePole.position.set(
sourceBodyPosition.x,
0.60,
sourceBodyPosition.z
);
sourcePole.castShadow = true;
scene.add(sourcePole);
const sourceLight = new THREE.PointLight(0xffbd58, 12, 4.5, 2);
sourceLight.position.copy(rayStart);
scene.add(sourceLight);
const bounceLight = new THREE.PointLight(0xffd166, 7, 3.2, 2);
bounceLight.position.copy(reflection1).add(new THREE.Vector3(0, 0.12, 0));
scene.add(bounceLight);
// -------------------------------------------------------------------------
// Moving photon
// -------------------------------------------------------------------------
const photon = new THREE.Mesh(
new THREE.SphereGeometry(0.066, 20, 14),
new THREE.MeshBasicMaterial({ color: 0xffffff })
);
scene.add(photon);
const segmentLengths = raySegments.map(([a, b]) => a.distanceTo(b));
const totalRayLength = segmentLengths.reduce((sum, value) => sum + value, 0);
function photonPosition(distance) {
let remaining = distance;
for (let i = 0; i < raySegments.length; i++) {
const [start, end] = raySegments[i];
if (remaining <= segmentLengths[i] || i === raySegments.length - 1) {
return start.clone().lerp(
end,
THREE.MathUtils.clamp(remaining / segmentLengths[i], 0, 1)
);
}
remaining -= segmentLengths[i];
}
return rayEnd.clone();
}
// -------------------------------------------------------------------------
// Screen-space labels
// -------------------------------------------------------------------------
const labelDefinitions = {
"pin-m1": new THREE.Vector3(3.05, 3.04, 0.12),
"pin-m2": new THREE.Vector3(0.12, 3.04, 2.35),
"pin-r1": reflection1.clone().add(new THREE.Vector3(0.05, 0.22, -0.15)),
"pin-r2": reflection2.clone().add(new THREE.Vector3(0.05, 0.22, 0.14)),
"pin-v0": rayStart.clone()
.addScaledVector(incomingDirection, 1.55)
.add(new THREE.Vector3(0, 0.39, 0)),
"pin-v2": reflection2.clone()
.addScaledVector(outgoingDirection, 1.88)
.add(new THREE.Vector3(0, 0.39, 0)),
"pin-plane": new THREE.Vector3(4.38, 1.34, 4.33),
"pin-angle": new THREE.Vector3(0.60, 0.12, 0.60),
"pin-source": sourceBodyPosition.clone().add(
new THREE.Vector3(0.05, 0.43, 0)
)
};
function updateLabels() {
for (const [id, position] of Object.entries(labelDefinitions)) {
const element = document.getElementById(id);
const projected = position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.display = visible ? "block" : "none";
if (!visible) continue;
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 pointer, wheel, touch, and keyboard camera controls
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const activePointers = new Map();
let previousPinchDistance = 0;
let previousPointer = null;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
previousPointer = {
x: event.clientX,
y: event.clientY
};
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 1 && previousPointer) {
const dx = event.clientX - previousPointer.x;
const dy = event.clientY - previousPointer.y;
orbit.azimuth -= dx * 0.006;
orbit.elevation = THREE.MathUtils.clamp(
orbit.elevation + dy * 0.0045,
orbit.minElevation,
orbit.maxElevation
);
previousPointer = {
x: event.clientX,
y: event.clientY
};
} else if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
const scale = previousPinchDistance / distance;
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * scale,
orbit.minRadius,
orbit.maxRadius
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
activePointers.delete(event.pointerId);
previousPointer = null;
previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
orbit.minRadius,
orbit.maxRadius
);
updateCamera();
},
{ passive: false }
);
function resetView() {
orbit.radius = 9.6;
orbit.azimuth = 0.72;
orbit.elevation = 0.72;
updateCamera();
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset-view").addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "r") {
resetView();
} else if (key === "arrowleft") {
orbit.azimuth += 0.08;
} else if (key === "arrowright") {
orbit.azimuth -= 0.08;
} else if (key === "arrowup") {
orbit.elevation = Math.min(
orbit.maxElevation,
orbit.elevation + 0.05
);
} else if (key === "arrowdown") {
orbit.elevation = Math.max(
orbit.minElevation,
orbit.elevation - 0.05
);
} else if (key === "+" || key === "=") {
orbit.radius = Math.max(orbit.minRadius, orbit.radius - 0.4);
} else if (key === "-") {
orbit.radius = Math.min(orbit.maxRadius, orbit.radius + 0.4);
} else {
return;
}
updateCamera();
});
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() {
const elapsed = clock.getElapsedTime();
const travelDistance = (elapsed * 1.55) % totalRayLength;
photon.position.copy(photonPosition(travelDistance));
const pulse = 1 + 0.20 * Math.sin(elapsed * 4.2);
pulseRing1.scale.setScalar(pulse);
pulseRing2.scale.setScalar(1.7 - pulse * 0.7);
glintMaterials[0].opacity =
0.14 + 0.045 * Math.sin(elapsed * 0.8);
glintMaterials[1].opacity =
0.075 + 0.025 * Math.sin(elapsed * 0.8 + 1.2);
sourceLight.intensity =
11 + 2.2 * Math.sin(elapsed * 3.1) * Math.sin(elapsed * 3.1);
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```