Prompt specification
Build or customize the Three.js 3D scene "Two-Mirror Receiver Puzzle" 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-Mirror Receiver Puzzle
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-mirror-receiver-puzzle-x3470
Scene Description Prompt:
Two-mirror receiver puzzle. Build a tabletop puzzle with rotatable mirrors and one target. Recalculate the ray path at each adjustment and stop the beam at genuine blocking surfaces. Please build this with Three.js from cdnjs in one self-contained HTML file, using a 3D cutaway.
Working Three.js Starter (two-mirror-receiver-puzzle-x3470):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two-Mirror Receiver</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(9, 20, 30, 0.86);
--line: rgba(126, 222, 255, 0.18);
--text: #e7f7ff;
--muted: #86a6b7;
--cyan: #78e8ff;
--green: #82ffc4;
--red: #ff6f79;
--amber: #ffd37c;
}
* {
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;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 430px;
text-shadow: 0 2px 18px #000;
}
.eyebrow {
margin-bottom: 7px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3.2vw, 43px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 1;
}
.subtitle {
margin: 10px 0 0;
color: #afc6d2;
font-size: 13px;
line-height: 1.55;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
width: 330px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 17px;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(20, 39, 52, 0.9), rgba(7, 15, 23, 0.92));
box-shadow:
0 24px 70px rgba(0, 0, 0, 0.45),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: #315063 transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.panel-title strong {
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 13px var(--cyan);
}
.mirror-card {
margin-top: 11px;
padding: 13px;
border: 1px solid rgba(126, 222, 255, 0.12);
border-radius: 13px;
background: rgba(4, 11, 17, 0.47);
}
.mirror-heading {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
font-size: 13px;
font-weight: 700;
}
.mirror-badge {
display: grid;
width: 25px;
height: 25px;
place-items: center;
border: 1px solid rgba(120, 232, 255, 0.4);
border-radius: 8px;
color: var(--cyan);
background: rgba(120, 232, 255, 0.08);
font-size: 11px;
font-weight: 850;
}
.angle-value {
margin-left: auto;
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 13px;
}
.mirror-card input[type="range"] {
width: 100%;
height: 20px;
margin: 0;
accent-color: var(--cyan);
cursor: ew-resize;
}
.scale {
display: flex;
justify-content: space-between;
margin-top: 3px;
color: #617d8d;
font-size: 9px;
font-family: ui-monospace, monospace;
}
.hint {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
button {
min-height: 38px;
padding: 9px 11px;
border: 1px solid rgba(126, 222, 255, 0.18);
border-radius: 10px;
color: var(--text);
background: rgba(24, 49, 63, 0.72);
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(126, 222, 255, 0.55);
background: rgba(35, 72, 90, 0.85);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.audit {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(126, 222, 255, 0.1);
color: #7895a5;
font-size: 10px;
line-height: 1.55;
}
.audit b {
color: #b6d5e3;
font-weight: 650;
}
.status-wrap {
position: absolute;
left: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 13px;
min-width: 305px;
padding: 13px 16px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 15px 45px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(15px);
}
.status-symbol {
display: grid;
width: 35px;
height: 35px;
flex: 0 0 auto;
place-items: center;
border: 1px solid rgba(255, 111, 121, 0.45);
border-radius: 50%;
color: var(--red);
background: rgba(255, 111, 121, 0.08);
font-family: ui-monospace, monospace;
font-weight: 900;
}
.status-wrap.locked {
border-color: rgba(130, 255, 196, 0.35);
}
.status-wrap.locked .status-symbol {
color: var(--green);
border-color: rgba(130, 255, 196, 0.5);
background: rgba(130, 255, 196, 0.08);
box-shadow: 0 0 20px rgba(130, 255, 196, 0.1);
}
.status-wrap.open .status-symbol {
color: var(--amber);
border-color: rgba(255, 211, 124, 0.45);
background: rgba(255, 211, 124, 0.08);
}
.status-title {
font-size: 12px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.status-detail {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.controls-note {
position: absolute;
right: 25px;
bottom: 20px;
color: rgba(173, 204, 218, 0.57);
font-size: 10px;
text-align: right;
line-height: 1.6;
text-shadow: 0 2px 8px #000;
}
#toast {
position: fixed;
z-index: 5;
left: 50%;
top: 24px;
max-width: min(440px, calc(100vw - 30px));
padding: 11px 16px;
border: 1px solid rgba(126, 222, 255, 0.25);
border-radius: 12px;
color: #dff8ff;
background: rgba(7, 18, 27, 0.93);
box-shadow: 0 15px 50px rgba(0, 0, 0, 0.4);
font-size: 12px;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 16px;
left: 16px;
right: 16px;
}
.subtitle {
display: none;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: min(330px, calc(100vw - 24px));
max-height: 56vh;
}
.status-wrap {
top: 76px;
bottom: auto;
left: 12px;
min-width: 0;
max-width: calc(100vw - 24px);
}
.controls-note {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div class="hud">
<header class="brand">
<div class="eyebrow">Optical bench / calibration 02</div>
<h1>Two-Mirror Receiver</h1>
<p class="subtitle">
Rotate the mirrors to carry the cyan beam through two reflections
and into the single receiver lens.
</p>
</header>
<aside class="controls">
<div class="panel-title">
<strong>Mirror adjustment</strong>
<span class="live-dot"></span>
</div>
<section class="mirror-card">
<div class="mirror-heading">
<span class="mirror-badge">A</span>
Primary mirror
<output class="angle-value" id="angleA">35.0°</output>
</div>
<input
id="sliderA"
type="range"
min="-180"
max="180"
step="0.1"
value="35"
aria-label="Mirror A angle"
>
<div class="scale">
<span>−180°</span>
<span>0°</span>
<span>180°</span>
</div>
<p class="hint">Drag mirror A directly, or use the calibrated dial.</p>
</section>
<section class="mirror-card">
<div class="mirror-heading">
<span class="mirror-badge">B</span>
Secondary mirror
<output class="angle-value" id="angleB">−20.0°</output>
</div>
<input
id="sliderB"
type="range"
min="-180"
max="180"
step="0.1"
value="-20"
aria-label="Mirror B angle"
>
<div class="scale">
<span>−180°</span>
<span>0°</span>
<span>180°</span>
</div>
<p class="hint">The receiver accepts light only after the second bounce.</p>
</section>
<div class="button-row">
<button id="resetButton" type="button">Reset bench</button>
<button id="hintButton" type="button">Reveal calibration</button>
</div>
<div class="audit">
<b>Live ray solver.</b>
Every adjustment re-tests the beam against the visible mirror panels,
receiver lens, walls, tabletop, and opaque baffles. A beam segment ends
at the first genuine surface intersection.
</div>
</aside>
<div class="status-wrap" id="statusWrap">
<div class="status-symbol" id="statusSymbol">×</div>
<div>
<div class="status-title" id="statusTitle">Receiver offline</div>
<div class="status-detail" id="statusDetail">Tracing optical path…</div>
</div>
</div>
<div class="controls-note">
Left-drag a mirror to rotate · Left-drag room to orbit<br>
Right-drag to pan · Scroll to zoom
</div>
</div>
<div id="toast"></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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 15, 28);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.05,
80
);
const cameraTarget = new THREE.Vector3(0, 0.8, -0.4);
const cameraState = {
radius: 13.4,
theta: 0.72,
phi: 1.02
};
function updateCamera() {
const { radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xbceaff, 0x10151b, 1.5));
const keyLight = new THREE.DirectionalLight(0xd9f4ff, 3.2);
keyLight.position.set(-4, 9, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4fc9ff, 1.8);
rimLight.position.set(6, 4, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffbd72, 13, 9, 2);
warmLight.position.set(3.7, 2.6, 1.8);
scene.add(warmLight);
// ---------------------------------------------------------------------
// Materials and helpers
// ---------------------------------------------------------------------
const materials = {
table: new THREE.MeshStandardMaterial({
color: 0x17232b,
roughness: 0.8,
metalness: 0.12
}),
tableEdge: new THREE.MeshStandardMaterial({
color: 0x263945,
roughness: 0.48,
metalness: 0.45
}),
wall: new THREE.MeshStandardMaterial({
color: 0x1c2a34,
roughness: 0.88,
metalness: 0.08
}),
baffle: new THREE.MeshStandardMaterial({
color: 0x35434a,
roughness: 0.66,
metalness: 0.3
}),
frame: new THREE.MeshStandardMaterial({
color: 0xc18c45,
roughness: 0.28,
metalness: 0.78
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x18262e,
roughness: 0.4,
metalness: 0.75
}),
mirror: new THREE.MeshPhysicalMaterial({
color: 0xc9f4ff,
emissive: 0x163d4d,
emissiveIntensity: 0.65,
metalness: 0.86,
roughness: 0.08,
clearcoat: 1,
clearcoatRoughness: 0.06,
side: THREE.DoubleSide
}),
receiver: new THREE.MeshPhysicalMaterial({
color: 0x79ffd0,
emissive: 0x29d994,
emissiveIntensity: 1.1,
roughness: 0.12,
metalness: 0.15,
transparent: true,
opacity: 0.94,
side: THREE.DoubleSide
}),
source: new THREE.MeshBasicMaterial({
color: 0x9af5ff,
transparent: true,
opacity: 0.95,
side: THREE.DoubleSide
})
};
function makeBox(
width,
height,
depth,
material,
x,
y,
z,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeCylinder(
radiusTop,
radiusBottom,
height,
material,
x,
y,
z,
segments = 24,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, color = "#a9ecff") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 512, 160);
context.fillStyle = "rgba(5, 13, 20, 0.82)";
context.strokeStyle = "rgba(140, 230, 255, 0.35)";
context.lineWidth = 5;
context.beginPath();
context.roundRect(8, 8, 496, 144, 34);
context.fill();
context.stroke();
context.fillStyle = color;
context.font = "700 76px ui-monospace, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 82);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.86, 0.27, 1);
sprite.renderOrder = 20;
return sprite;
}
// ---------------------------------------------------------------------
// Collision surfaces
// ---------------------------------------------------------------------
const collisionObjects = [];
const pickObjects = [];
function registerBlocker(mesh, name) {
mesh.userData.type = "blocker";
mesh.userData.name = name;
collisionObjects.push(mesh);
return mesh;
}
// Tabletop and cutaway enclosure.
registerBlocker(
makeBox(10.9, 0.36, 6.9, materials.table, 0, -0.18, 0),
"tabletop"
);
registerBlocker(
makeBox(10.9, 0.12, 0.18, materials.tableEdge, 0, -0.02, 3.4),
"front cutaway lip"
);
registerBlocker(
makeBox(10.8, 2.55, 0.18, materials.wall, 0, 1.275, -3.35),
"rear wall"
);
registerBlocker(
makeBox(0.18, 2.55, 6.85, materials.wall, -5.4, 1.275, 0),
"left wall"
);
registerBlocker(
makeBox(0.18, 2.55, 6.85, materials.wall, 5.4, 1.275, 0),
"right wall"
);
// Genuine opaque internal blockers.
registerBlocker(
makeBox(0.24, 1.65, 1.35, materials.baffle, 0.15, 0.825, 1.45),
"front baffle"
);
registerBlocker(
makeBox(0.24, 1.65, 1.45, materials.baffle, 2.65, 0.825, 0.1),
"center baffle"
);
registerBlocker(
makeBox(0.72, 1.4, 0.72, materials.baffle, -0.15, 0.7, -2.15),
"rear pillar"
);
// Small trim caps make the baffles visibly solid.
makeBox(0.36, 0.08, 1.48, materials.frame, 0.15, 1.69, 1.45);
makeBox(0.36, 0.08, 1.58, materials.frame, 2.65, 1.69, 0.1);
makeBox(0.84, 0.08, 0.84, materials.frame, -0.15, 1.45, -2.15);
const grid = new THREE.GridHelper(10.6, 26, 0x315565, 0x18313d);
grid.position.y = 0.012;
grid.scale.z = 0.63;
grid.material.transparent = true;
grid.material.opacity = 0.45;
scene.add(grid);
// Emphasize the cutaway edge.
const cutawayMaterial = new THREE.MeshBasicMaterial({
color: 0x4fc7e8,
transparent: true,
opacity: 0.48
});
makeBox(10.5, 0.025, 0.035, cutawayMaterial, 0, 0.025, 3.31);
// ---------------------------------------------------------------------
// Mirrors
// ---------------------------------------------------------------------
const SOURCE = new THREE.Vector3(-4.25, 1.3, 0);
const SOURCE_DIRECTION = new THREE.Vector3(1, 0, 0);
const MIRROR_A_POSITION = new THREE.Vector3(-1.6, 1.3, 0);
const MIRROR_B_POSITION = new THREE.Vector3(1.4, 1.3, -1.7);
const TARGET_POSITION = new THREE.Vector3(4.1, 1.3, -2.7);
const mirrors = [];
function createMirror(id, position, initialAngle) {
const width = 1.9;
const height = 1.4;
makeCylinder(
0.68,
0.76,
0.12,
materials.darkMetal,
position.x,
0.09,
position.z,
40
);
makeCylinder(
0.12,
0.16,
1.08,
materials.frame,
position.x,
0.61,
position.z,
24
);
const collar = makeCylinder(
0.26,
0.26,
0.16,
materials.darkMetal,
position.x,
1.13,
position.z,
28
);
collar.castShadow = true;
const pivot = new THREE.Group();
pivot.position.copy(position);
scene.add(pivot);
const mirror = {
id,
pivot,
plane: null,
angle: initialAngle
};
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
materials.mirror
);
plane.castShadow = true;
plane.receiveShadow = true;
pivot.add(plane);
const framePieces = [
makeBox(width + 0.18, 0.11, 0.13, materials.frame, 0, height / 2, -0.025, pivot),
makeBox(width + 0.18, 0.11, 0.13, materials.frame, 0, -height / 2, -0.025, pivot),
makeBox(0.11, height + 0.12, 0.13, materials.frame, -width / 2, 0, -0.025, pivot),
makeBox(0.11, height + 0.12, 0.13, materials.frame, width / 2, 0, -0.025, pivot)
];
const knob = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 20, 14),
new THREE.MeshStandardMaterial({
color: 0x78e8ff,
emissive: 0x176b7e,
emissiveIntensity: 0.7,
roughness: 0.25,
metalness: 0.55
})
);
knob.position.set(0, height / 2 + 0.22, 0.03);
knob.castShadow = true;
pivot.add(knob);
plane.userData = {
type: "mirror",
mirror
};
for (const piece of framePieces) {
piece.userData = {
type: "mirror",
mirror
};
}
knob.userData = {
type: "mirror",
mirror
};
collisionObjects.push(plane);
pickObjects.push(plane, knob, ...framePieces);
// Floor calibration ring.
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.79, 0.82, 64),
new THREE.MeshBasicMaterial({
color: id === "A" ? 0x70dfff : 0xffc36e,
transparent: true,
opacity: 0.52,
side: THREE.DoubleSide
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(position.x, 0.024, position.z);
scene.add(ring);
// Tick marks around the calibration ring.
const tickMaterial = new THREE.MeshBasicMaterial({
color: 0x7395a5,
transparent: true,
opacity: 0.65
});
for (let i = 0; i < 24; i++) {
const angle = (i / 24) * Math.PI * 2;
const tick = makeBox(
0.025,
0.012,
i % 6 === 0 ? 0.17 : 0.1,
tickMaterial,
position.x + Math.sin(angle) * 0.92,
0.026,
position.z + Math.cos(angle) * 0.92
);
tick.rotation.y = angle;
}
const label = makeLabel(id);
label.position.set(position.x, position.y + 1.22, position.z);
scene.add(label);
mirrors.push(mirror);
return mirror;
}
const mirrorA = createMirror("A", MIRROR_A_POSITION, 35);
const mirrorB = createMirror("B", MIRROR_B_POSITION, -20);
// ---------------------------------------------------------------------
// Source and receiver
// ---------------------------------------------------------------------
const sourceGroup = new THREE.Group();
sourceGroup.position.set(-4.62, 1.3, 0);
scene.add(sourceGroup);
makeBox(0.56, 0.55, 0.62, materials.darkMetal, 0, 0, 0, sourceGroup);
const barrel = makeCylinder(
0.24,
0.28,
0.6,
materials.frame,
0.28,
0,
0,
32,
sourceGroup
);
barrel.rotation.z = Math.PI / 2;
const sourceAperture = new THREE.Mesh(
new THREE.CircleGeometry(0.18, 40),
materials.source
);
sourceAperture.position.copy(SOURCE);
sourceAperture.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(1, 0, 0)
);
scene.add(sourceAperture);
const sourceLight = new THREE.PointLight(0x64eaff, 6, 3.2, 2);
sourceLight.position.copy(SOURCE);
scene.add(sourceLight);
const sourceLabel = makeLabel("SOURCE", "#8fefff");
sourceLabel.position.set(-4.3, 2.12, 0);
scene.add(sourceLabel);
// Receiver lens faces Mirror B.
const receiverNormal = MIRROR_B_POSITION.clone()
.sub(TARGET_POSITION)
.normalize();
const receiverGroup = new THREE.Group();
receiverGroup.position.copy(TARGET_POSITION);
receiverGroup.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
receiverNormal
);
scene.add(receiverGroup);
makeBox(
0.98,
0.9,
0.4,
materials.darkMetal,
0,
0,
-0.25,
receiverGroup
);
const targetLens = new THREE.Mesh(
new THREE.CircleGeometry(0.44, 64),
materials.receiver
);
targetLens.userData = {
type: "receiver",
name: "receiver lens"
};
receiverGroup.add(targetLens);
collisionObjects.push(targetLens);
const receiverRing = new THREE.Mesh(
new THREE.TorusGeometry(0.51, 0.035, 12, 64),
new THREE.MeshBasicMaterial({
color: 0x83ffc9,
transparent: true,
opacity: 0.8
})
);
receiverRing.position.z = -0.015;
receiverGroup.add(receiverRing);
makeCylinder(
0.12,
0.16,
1.12,
materials.frame,
TARGET_POSITION.x,
0.6,
TARGET_POSITION.z,
24
);
const receiverLabel = makeLabel("RX", "#91ffd0");
receiverLabel.position.set(
TARGET_POSITION.x,
TARGET_POSITION.y + 1.13,
TARGET_POSITION.z
);
scene.add(receiverLabel);
// Decorative, animated receiver halo.
const receiverHalo = new THREE.Mesh(
new THREE.RingGeometry(0.58, 0.61, 64),
new THREE.MeshBasicMaterial({
color: 0x82ffc4,
transparent: true,
opacity: 0.4,
side: THREE.DoubleSide,
depthWrite: false
})
);
receiverHalo.position.copy(TARGET_POSITION);
receiverHalo.quaternion.copy(receiverGroup.quaternion);
scene.add(receiverHalo);
// Source cable, purely decorative.
const cableCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-4.75, 0.12, 0.25),
new THREE.Vector3(-4.95, 0.14, 0.9),
new THREE.Vector3(-4.65, 0.12, 1.65),
new THREE.Vector3(-3.9, 0.12, 1.85)
]);
const cable = new THREE.Mesh(
new THREE.TubeGeometry(cableCurve, 32, 0.035, 8, false),
materials.darkMetal
);
cable.castShadow = true;
scene.add(cable);
// ---------------------------------------------------------------------
// Beam renderer and optical solver
// ---------------------------------------------------------------------
const beamGroup = new THREE.Group();
scene.add(beamGroup);
const beamCoreMaterial = new THREE.MeshBasicMaterial({
color: 0x91f6ff,
transparent: true,
opacity: 0.98,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const beamGlowMaterial = new THREE.MeshBasicMaterial({
color: 0x50dfff,
transparent: true,
opacity: 0.16,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const terminalMaterial = new THREE.MeshBasicMaterial({
color: 0xff6672,
transparent: true,
opacity: 0.85,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const raycaster = new THREE.Raycaster();
raycaster.near = 0.002;
raycaster.far = 30;
let lastTrace = null;
function traceBeam() {
scene.updateMatrixWorld(true);
const segments = [];
let origin = SOURCE.clone();
let direction = SOURCE_DIRECTION.clone().normalize();
let outcome = "open";
let terminalPoint = null;
let terminalName = "open cutaway";
let bounceCount = 0;
for (let bounce = 0; bounce < 10; bounce++) {
raycaster.set(origin, direction);
const intersections = raycaster.intersectObjects(
collisionObjects,
false
);
if (intersections.length === 0) {
// The beam exits through the deliberately open front of the cutaway.
terminalPoint = origin.clone().addScaledVector(direction, 5.2);
segments.push({
a: origin.clone(),
b: terminalPoint.clone()
});
outcome = "open";
terminalName = "open cutaway";
break;
}
const hit = intersections[0];
const data = hit.object.userData;
if (data.type === "mirror") {
segments.push({
a: origin.clone(),
b: hit.point.clone()
});
const worldQuaternion = new THREE.Quaternion();
hit.object.getWorldQuaternion(worldQuaternion);
const normal = new THREE.Vector3(0, 0, 1)
.applyQuaternion(worldQuaternion)
.normalize();
direction.reflect(normal).normalize();
// Move just beyond the reflective plane before tracing again.
origin = hit.point.clone().addScaledVector(direction, 0.004);
bounceCount++;
continue;
}
if (data.type === "receiver") {
segments.push({
a: origin.clone(),
b: hit.point.clone()
});
outcome = "success";
terminalPoint = hit.point.clone();
terminalName = "receiver lens";
break;
}
// A real opaque surface stopped the ray.
segments.push({
a: origin.clone(),
b: hit.point.clone()
});
outcome = "blocked";
terminalPoint = hit.point.clone();
terminalName = data.name || "opaque surface";
break;
}
return {
segments,
outcome,
terminalPoint,
terminalName,
bounceCount
};
}
function clearBeam() {
while (beamGroup.children.length) {
const child = beamGroup.children.pop();
child.traverse((object) => {
if (object.geometry) {
object.geometry.dispose();
}
});
}
}
function addBeamSegment(a, b) {
const delta = b.clone().sub(a);
const length = delta.length();
if (length < 0.0001) {
return;
}
const midpoint = a.clone().add(b).multiplyScalar(0.5);
const direction = delta.clone().normalize();
const quaternion = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
const core = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, length, 10, 1, true),
beamCoreMaterial
);
core.position.copy(midpoint);
core.quaternion.copy(quaternion);
core.renderOrder = 4;
beamGroup.add(core);
const glow = new THREE.Mesh(
new THREE.CylinderGeometry(0.072, 0.072, length, 12, 1, true),
beamGlowMaterial
);
glow.position.copy(midpoint);
glow.quaternion.copy(quaternion);
glow.renderOrder = 3;
beamGroup.add(glow);
}
function renderTrace(trace) {
clearBeam();
const successful = trace.outcome === "success";
beamCoreMaterial.color.setHex(successful ? 0x99ffd3 : 0x91f6ff);
beamGlowMaterial.color.setHex(successful ? 0x54ffb3 : 0x50dfff);
for (const segment of trace.segments) {
addBeamSegment(segment.a, segment.b);
}
if (trace.outcome === "blocked" && trace.terminalPoint) {
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.095, 20, 14),
terminalMaterial
);
marker.position.copy(trace.terminalPoint);
beamGroup.add(marker);
}
}
const statusWrap = document.querySelector("#statusWrap");
const statusSymbol = document.querySelector("#statusSymbol");
const statusTitle = document.querySelector("#statusTitle");
const statusDetail = document.querySelector("#statusDetail");
function updateStatus(trace) {
statusWrap.classList.remove("locked", "open");
if (trace.outcome === "success") {
statusWrap.classList.add("locked");
statusSymbol.textContent = "✓";
statusTitle.textContent = "Receiver locked";
statusDetail.textContent =
`Two reflections confirmed · terminal hit at ${trace.terminalName}.`;
} else if (trace.outcome === "blocked") {
statusSymbol.textContent = "×";
statusTitle.textContent = "Beam blocked";
statusDetail.textContent =
`Path stopped by ${trace.terminalName}. Adjust a mirror and retrace.`;
} else {
statusWrap.classList.add("open");
statusSymbol.textContent = "○";
statusTitle.textContent = "Beam lost";
statusDetail.textContent =
"The ray left through the open cutaway before reaching the receiver.";
}
}
function recomputeBeam() {
lastTrace = traceBeam();
renderTrace(lastTrace);
updateStatus(lastTrace);
}
// ---------------------------------------------------------------------
// Mirror controls
// ---------------------------------------------------------------------
const sliderA = document.querySelector("#sliderA");
const sliderB = document.querySelector("#sliderB");
const angleA = document.querySelector("#angleA");
const angleB = document.querySelector("#angleB");
setMirrorAngle(mirrorA, mirrorA.angle, false);
setMirrorAngle(mirrorB, mirrorB.angle, false);
function wrapAngle(angle) {
return ((angle + 180) % 360 + 360) % 360 - 180;
}
function setMirrorAngle(mirror, angle, recalculate = true) {
mirror.angle = wrapAngle(angle);
mirror.pivot.rotation.y = THREE.MathUtils.degToRad(mirror.angle);
const slider = mirror === mirrorA ? sliderA : sliderB;
const output = mirror === mirrorA ? angleA : angleB;
slider.value = mirror.angle.toFixed(1);
output.textContent = `${mirror.angle.toFixed(1)}°`;
if (recalculate) {
recomputeBeam();
}
}
sliderA.addEventListener("input", () => {
setMirrorAngle(mirrorA, Number(sliderA.value));
});
sliderB.addEventListener("input", () => {
setMirrorAngle(mirrorB, Number(sliderB.value));
});
document.querySelector("#resetButton").addEventListener("click", () => {
setMirrorAngle(mirrorA, 35);
setMirrorAngle(mirrorB, -20);
showToast("Bench reset. The primary beam now terminates at the rear wall.");
});
document.querySelector("#hintButton").addEventListener("click", () => {
// Exact geometry-derived calibration for the fixed bench layout.
setMirrorAngle(mirrorA, 14.77);
setMirrorAngle(mirrorB, 24.93);
showToast("Calibration revealed: A ≈ 14.8°, B ≈ 24.9°.");
});
const toast = document.querySelector("#toast");
let toastTimer;
function showToast(message) {
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 3000);
}
// ---------------------------------------------------------------------
// Direct pointer interaction and custom camera controls
// ---------------------------------------------------------------------
const pointer = new THREE.Vector2();
let interaction = null;
let draggedMirror = null;
let dragStartX = 0;
let dragStartAngle = 0;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickMirror(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickObjects, false);
return hits.length ? hits[0].object.userData.mirror : null;
}
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
if (event.button === 0) {
const mirror = pickMirror(event);
if (mirror) {
draggedMirror = mirror;
dragStartX = event.clientX;
dragStartAngle = mirror.angle;
interaction = "mirror";
canvas.style.cursor = "ew-resize";
return;
}
interaction = "orbit";
} else {
interaction = "pan";
}
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!interaction) {
if (event.buttons === 0) {
canvas.style.cursor =
pickMirror(event) ? "ew-resize" : "grab";
}
return;
}
const movementX = event.movementX || 0;
const movementY = event.movementY || 0;
if (interaction === "mirror" && draggedMirror) {
const nextAngle = dragStartAngle + movementX * 0.42;
setMirrorAngle(draggedMirror, nextAngle);
return;
}
if (interaction === "orbit") {
cameraState.theta -= movementX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - movementY * 0.005,
0.28,
1.38
);
updateCamera();
return;
}
if (interaction === "pan") {
const right = new THREE.Vector3();
camera.getWorldDirection(right);
right.y = 0;
right.normalize();
const forward = new THREE.Vector3(
-Math.sin(cameraState.theta),
0,
-Math.cos(cameraState.theta)
);
const panScale = cameraState.radius * 0.0014;
cameraTarget.addScaledVector(right, -movementX * panScale);
cameraTarget.addScaledVector(forward, -movementY * panScale);
cameraTarget.x = THREE.MathUtils.clamp(
cameraTarget.x,
-5,
5
);
cameraTarget.z = THREE.MathUtils.clamp(
cameraTarget.z,
-4,
4
);
updateCamera();
}
});
function endPointerInteraction(event) {
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
interaction = null;
draggedMirror = null;
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", endPointerInteraction);
canvas.addEventListener("pointercancel", endPointerInteraction);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
21
);
updateCamera();
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const success = lastTrace?.outcome === "success";
sourceAperture.material.opacity =
0.82 + Math.sin(elapsed * 4.2) * 0.13;
sourceLight.intensity =
5.5 + Math.sin(elapsed * 4.2) * 0.8;
receiverHalo.material.opacity = success
? 0.55 + Math.sin(elapsed * 4.5) * 0.22
: 0.18;
const haloScale = success
? 1 + Math.sin(elapsed * 3.4) * 0.08
: 1;
receiverHalo.scale.setScalar(haloScale);
beamGlowMaterial.opacity =
0.13 + Math.sin(elapsed * 6.5) * 0.025;
renderer.render(scene, camera);
}
recomputeBeam();
animate();
</script>
</body>
</html>
```