Prompt specification
Build or customize the Three.js 3D scene "Lighthouse Courtyard Beam Route" 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: Lighthouse Courtyard Beam Route
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/lighthouse-courtyard-beam-route-x3471
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts reflected beam paths between mirrors and targets. Create an original coastal puzzle courtyard with three mirrors, shutters, and receivers. Use bounded ray segments, readable sight lines, and clearly fictional activation rules. Voxel style.
Working Three.js Starter (lighthouse-courtyard-beam-route-x3471):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Brineglass Courtyard</title>
<style>
:root {
--ink: #12333a;
--paper: rgba(246, 241, 218, 0.92);
--line: rgba(18, 51, 58, 0.18);
--mint: #58e6d2;
--coral: #ff6b62;
--gold: #ffd166;
--violet: #9b6cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #9bd9df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid rgba(255, 255, 255, 0.56);
background: var(--paper);
box-shadow:
0 18px 50px rgba(17, 51, 59, 0.16),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title-card {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 18px 20px 16px;
border-radius: 4px 20px 4px 4px;
}
.eyebrow {
margin-bottom: 7px;
color: #477078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 37px);
line-height: 0.98;
letter-spacing: -0.045em;
text-transform: uppercase;
}
#title-card p {
margin: 10px 0 0;
max-width: 320px;
color: #4c6d72;
font-size: 13px;
line-height: 1.45;
}
#rules-card {
top: 22px;
right: 22px;
width: min(340px, calc(100vw - 44px));
max-height: calc(100vh - 132px);
overflow: auto;
padding: 17px 18px;
border-radius: 20px 4px 4px 4px;
}
.rules-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
}
.rules-heading h2 {
margin: 0;
font-size: 12px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.fiction-tag {
padding: 5px 8px;
border-radius: 999px;
background: #183e46;
color: #f7f0d9;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.rule {
display: grid;
grid-template-columns: 28px 1fr;
gap: 9px;
margin: 11px 0;
color: #45666c;
font-size: 12px;
line-height: 1.45;
}
.rule b {
display: grid;
place-items: center;
width: 25px;
height: 25px;
border: 1px solid rgba(18, 51, 58, 0.18);
border-radius: 50%;
color: var(--ink);
font-size: 10px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 13px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #3f6269;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
display: inline-block;
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--label, var(--mint));
box-shadow: 0 0 10px var(--label, var(--mint));
}
#dock {
left: 50%;
bottom: 20px;
width: min(760px, calc(100vw - 44px));
transform: translateX(-50%);
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 13px 15px 13px 18px;
border-radius: 4px 18px 18px 18px;
}
.status-block {
min-width: 210px;
}
#status-title {
display: flex;
align-items: baseline;
gap: 8px;
margin-bottom: 6px;
}
#status-title strong {
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#awake-count {
color: #52737a;
font-size: 11px;
}
#chips {
display: flex;
gap: 6px;
}
.chip {
padding: 4px 8px;
border: 1px solid rgba(18, 51, 58, 0.15);
border-radius: 999px;
background: rgba(255, 255, 255, 0.42);
color: #557078;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
}
.chip.active {
border-color: rgba(88, 230, 210, 0.8);
background: rgba(88, 230, 210, 0.2);
color: #145d58;
}
.chip.blocked {
border-color: rgba(155, 108, 255, 0.6);
background: rgba(155, 108, 255, 0.13);
}
.controls-copy {
flex: 1;
color: #4c6d72;
font-size: 11px;
line-height: 1.5;
text-align: center;
}
.controls-copy strong {
color: var(--ink);
}
.button-row {
display: flex;
gap: 7px;
}
.action {
cursor: pointer;
padding: 9px 11px;
border: 1px solid rgba(18, 51, 58, 0.22);
border-radius: 4px;
background: #f5efd8;
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
transition:
transform 120ms ease,
background 120ms ease;
}
.action:hover {
transform: translateY(-1px);
background: #fff9e5;
}
.action.primary {
border-color: #173e46;
background: #173e46;
color: #f8f1dc;
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
overflow: hidden;
}
.world-label {
position: absolute;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 7px;
transform: translate(-50%, -100%);
border: 1px solid rgba(255, 255, 255, 0.62);
border-radius: 3px;
background: rgba(19, 49, 56, 0.8);
box-shadow: 0 5px 15px rgba(11, 34, 39, 0.13);
color: #f9f1db;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
white-space: nowrap;
}
.world-label .dot {
width: 6px;
height: 6px;
}
#toast {
position: fixed;
left: 50%;
bottom: 112px;
z-index: 20;
max-width: calc(100vw - 40px);
padding: 10px 14px;
transform: translate(-50%, 12px);
border-radius: 4px;
background: rgba(19, 49, 56, 0.92);
color: #f8f1dc;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.03em;
opacity: 0;
pointer-events: none;
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
@media (max-width: 820px) {
#rules-card {
top: auto;
right: 14px;
bottom: 100px;
width: 255px;
max-height: 43vh;
padding: 13px;
}
#title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
#title-card p {
display: none;
}
#dock {
bottom: 12px;
width: calc(100vw - 24px);
padding: 10px;
}
.controls-copy {
display: none;
}
.status-block {
min-width: 0;
flex: 1;
}
.action {
padding: 8px;
font-size: 8px;
}
}
@media (max-width: 520px) {
#rules-card {
display: none;
}
.button-row {
flex-direction: column;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive voxel coastal optics puzzle"></main>
<div id="labels" aria-hidden="true"></div>
<section id="title-card" class="panel">
<div class="eyebrow">Coastal signal puzzle · Voxel scene</div>
<h1>Brineglass<br />Courtyard</h1>
<p>
Redirect three numbered harbor beacons through reflective shutters until
every receiver accepts its matching beam.
</p>
</section>
<aside id="rules-card" class="panel">
<div class="rules-heading">
<h2>Fictional rules</h2>
<span class="fiction-tag">FICTION</span>
</div>
<div class="rule">
<b>01</b>
<span>Numbered beacons feed only the mirror with the same number.</span>
</div>
<div class="rule">
<b>02</b>
<span>
A receiver wakes only when exactly one matching beam enters through
its open shutter.
</span>
</div>
<div class="rule">
<b>03</b>
<span>
A ray may reflect twice, then it ends. Every ray is cut off at 24
courtyard metres.
</span>
</div>
<div class="rule">
<b>04</b>
<span>
Closed shutters absorb light. Wrong-number beams are rejected at the
receiver mouth.
</span>
</div>
<div class="legend">
<span><i class="dot" style="--label:#58e6d2"></i>Channel 1</span>
<span><i class="dot" style="--label:#ff6b62"></i>Channel 2</span>
<span><i class="dot" style="--label:#ffd166"></i>Channel 3</span>
<span><i class="dot" style="--label:#9b6cff"></i>Closed shutter</span>
</div>
</aside>
<section id="dock" class="panel">
<div class="status-block">
<div id="status-title">
<strong>Receiver status</strong>
<span id="awake-count">0 / 3 awake</span>
</div>
<div id="chips">
<span class="chip" id="chip-0">R1 · WAITING</span>
<span class="chip" id="chip-1">R2 · WAITING</span>
<span class="chip" id="chip-2">R3 · WAITING</span>
</div>
</div>
<div class="controls-copy">
<strong>Click mirror</strong> to rotate ·
<strong>Click shutter</strong> to open/close<br />
Drag to orbit · Right-drag to pan · Wheel to zoom
</div>
<div class="button-row">
<button class="action" id="reset-button" type="button">Reset</button>
<button class="action primary" id="open-button" type="button">Open all</button>
</div>
</section>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x9bd9df);
scene.fog = new THREE.Fog(0x9bd9df, 28, 70);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance",
});
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;
document.querySelector("#scene").appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-16, 16, 11, -11, 0.1, 100);
const cameraState = {
target: new THREE.Vector3(0, 0.7, 0),
azimuth: -0.64,
elevation: 0.78,
zoom: 1,
};
const GROUND = 0.54;
const BEAM_Y = 1.67;
const MAX_RAY_LENGTH = 24;
const MIRROR_HALF_LENGTH = 1.35;
const ANGLE_STEPS = [45, 135, 0, 22.5];
const channelColors = [0x58e6d2, 0xff6b62, 0xffd166];
const channelCss = ["#58e6d2", "#ff6b62", "#ffd166"];
const shutterColor = 0x9b6cff;
const sources = [
{ x: -3, z: -10.4, id: 0 },
{ x: 3, z: -5.0, id: 1 },
{ x: -3, z: -1.0, id: 2 },
];
const mirrors = [
{ x: -3, z: -6, angle: 45, angleIndex: 0, id: 0 },
{ x: 3, z: 0, angle: 135, angleIndex: 1, id: 1 },
{ x: -3, z: 4, angle: 22.5, angleIndex: 3, id: 2 },
];
const receivers = [
{ x: 6, z: -6, id: 0, open: true },
{ x: 8, z: 0, id: 1, open: false },
{ x: 6, z: 4, id: 2, open: true },
];
const initialAngles = [45, 135, 22.5];
const initialShutters = [true, false, true];
const interactiveMeshes = [];
const worldLabels = [];
const labelRoot = document.querySelector("#labels");
const beamGroup = new THREE.Group();
scene.add(beamGroup);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const tempObject = new THREE.Object3D();
const tempColor = new THREE.Color();
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
...options,
});
}
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material,
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createWorldLabel(text, position, color, className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.style.setProperty("--label", color);
const dot = document.createElement("i");
dot.className = "dot";
const labelText = document.createElement("span");
labelText.textContent = text;
element.append(dot, labelText);
labelRoot.appendChild(element);
const entry = {
element,
textElement: labelText,
position: position.clone(),
};
worldLabels.push(entry);
return entry;
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xd9fbff, 0x8b684f, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cc, 3.2);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x79cfff, 1.15);
rimLight.position.set(12, 8, -12);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Coastal courtyard
// -------------------------------------------------------------------------
const oceanMaterial = standardMaterial(0x319eb0, {
roughness: 0.34,
metalness: 0.08,
});
const ocean = new THREE.Mesh(
new THREE.BoxGeometry(110, 0.25, 110),
oceanMaterial,
);
ocean.position.y = -0.72;
ocean.receiveShadow = true;
scene.add(ocean);
const sandMaterial = standardMaterial(0xc9aa7b);
const edgeMaterial = standardMaterial(0xa9825e);
const paleStone = standardMaterial(0xe3c99d);
const darkStone = standardMaterial(0x45666a);
const woodMaterial = standardMaterial(0x865844);
const roofMaterial = standardMaterial(0x315c63);
addBox(
scene,
new THREE.Vector3(24, 0.8, 22),
new THREE.Vector3(0, 0, 0),
sandMaterial,
);
addBox(
scene,
new THREE.Vector3(23.4, 0.22, 21.4),
new THREE.Vector3(0, 0.41, 0),
edgeMaterial,
);
// Individually colored voxel paving.
const tileGeometry = new THREE.BoxGeometry(1.86, 0.13, 1.86);
const tileMaterial = standardMaterial(0xffffff);
const tileCount = 12 * 10;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
tiles.receiveShadow = true;
let tileIndex = 0;
for (let row = 0; row < 10; row++) {
for (let column = 0; column < 12; column++) {
tempObject.position.set(-10.5 + column * 1.91, 0.5, -8.55 + row * 1.9);
tempObject.rotation.y = 0;
tempObject.updateMatrix();
tiles.setMatrixAt(tileIndex, tempObject.matrix);
const variation = Math.sin(column * 12.31 + row * 7.77);
tempColor.setHSL(
0.105,
0.34,
0.67 + variation * 0.035,
);
tiles.setColorAt(tileIndex, tempColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
if (tiles.instanceColor) tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Low broken seawall pieces.
const wallPieces = [
[-10.7, -8.8, 2.4, 0.75, 1.0],
[-10.7, 8.8, 2.4, 0.75, 1.0],
[10.7, -8.6, 2.4, 0.75, 1.0],
[10.7, 8.6, 2.4, 0.75, 1.0],
[-7.5, 10.1, 2.8, 0.7, 0.8],
[0.2, 10.1, 2.8, 0.7, 0.8],
[7.7, 10.1, 2.8, 0.7, 0.8],
];
for (const [x, z, width, height, depth] of wallPieces) {
addBox(
scene,
new THREE.Vector3(width, height, depth),
new THREE.Vector3(x, GROUND + height / 2, z),
paleStone,
);
}
// Corner capstones.
for (const x of [-10.7, 10.7]) {
for (const z of [-10, 10]) {
addBox(
scene,
new THREE.Vector3(1.2, 1.5, 1.2),
new THREE.Vector3(x, GROUND + 0.75, z),
darkStone,
);
addBox(
scene,
new THREE.Vector3(1.45, 0.22, 1.45),
new THREE.Vector3(x, GROUND + 1.58, z),
paleStone,
);
}
}
// Steps to the water.
for (let i = 0; i < 3; i++) {
addBox(
scene,
new THREE.Vector3(4.5 - i * 0.35, 0.28, 1.15),
new THREE.Vector3(-7.3, 0.22 - i * 0.2, -10.1 - i * 0.8),
paleStone,
);
}
// Voxel shoreline rocks.
const rockMaterials = [
standardMaterial(0x6b7771),
standardMaterial(0x79827b),
standardMaterial(0x596b68),
];
let randomSeed = 9341;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
for (let i = 0; i < 24; i++) {
const angle = random() * Math.PI * 2;
const radius = 15 + random() * 13;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const size = 0.7 + random() * 1.8;
const rock = addBox(
scene,
new THREE.Vector3(size, size * (0.7 + random()), size),
new THREE.Vector3(x, -0.2 + size * 0.25, z),
rockMaterials[i % rockMaterials.length],
);
rock.rotation.y = random() * Math.PI;
}
// Small harbor hut in the northeast corner.
const hut = new THREE.Group();
hut.position.set(8.5, GROUND, 7.4);
scene.add(hut);
addBox(
hut,
new THREE.Vector3(2.8, 1.8, 2.4),
new THREE.Vector3(0, 0.9, 0),
paleStone,
);
addBox(
hut,
new THREE.Vector3(3.3, 0.3, 2.9),
new THREE.Vector3(0, 1.95, 0),
roofMaterial,
);
addBox(
hut,
new THREE.Vector3(0.7, 1.05, 0.12),
new THREE.Vector3(-0.45, 0.55, -1.22),
darkStone,
);
// Sparse voxel grass.
const grassMaterial = standardMaterial(0x4e8c72);
for (const [x, z] of [
[-9, -2],
[-8, 7],
[7.5, -8],
[9, -2],
[-6, 8.5],
[5, 8],
]) {
for (let i = 0; i < 3; i++) {
const blade = addBox(
scene,
new THREE.Vector3(0.12, 0.45 + random() * 0.35, 0.12),
new THREE.Vector3(x + (random() - 0.5) * 0.7, GROUND + 0.28, z + (random() - 0.5) * 0.7),
grassMaterial,
);
blade.rotation.z = (random() - 0.5) * 0.35;
}
}
// Animated sea glints.
const waveGroup = new THREE.Group();
scene.add(waveGroup);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0xbceff0,
transparent: true,
opacity: 0.36,
depthWrite: false,
});
const waves = [];
for (let i = 0; i < 38; i++) {
const length = 0.8 + random() * 2.4;
const wave = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.035, 0.09),
waveMaterial,
);
wave.position.set(
(random() - 0.5) * 58,
-0.56,
(random() - 0.5) * 58,
);
wave.rotation.y = random() * 0.6;
wave.userData.phase = random() * Math.PI * 2;
waveGroup.add(wave);
waves.push(wave);
}
// -------------------------------------------------------------------------
// Numbered beacons
// -------------------------------------------------------------------------
const sourceRootMaterial = standardMaterial(0x34545a);
const sourceLightMaterial = standardMaterial(0xf4dfb4);
for (const source of sources) {
const root = new THREE.Group();
root.position.set(source.x, GROUND, source.z);
scene.add(root);
addBox(
root,
new THREE.Vector3(1.15, 0.28, 1.15),
new THREE.Vector3(0, 0.14, 0),
darkStone,
);
addBox(
root,
new THREE.Vector3(0.7, 1.45, 0.7),
new THREE.Vector3(0, 0.95, 0),
sourceRootMaterial,
);
addBox(
root,
new THREE.Vector3(1.05, 0.22, 1.05),
new THREE.Vector3(0, 1.72, 0),
paleStone,
);
const lensMaterial = standardMaterial(channelColors[source.id], {
emissive: channelColors[source.id],
emissiveIntensity: 1.6,
});
addBox(
root,
new THREE.Vector3(0.62, 0.56, 0.28),
new THREE.Vector3(0, 1.34, 0.47),
lensMaterial,
);
addBox(
root,
new THREE.Vector3(0.8, 0.14, 0.15),
new THREE.Vector3(0, 1.76, 0.43),
paleStone,
);
createWorldLabel(
`S${source.id + 1}`,
new THREE.Vector3(source.x, GROUND + 2.35, source.z),
channelCss[source.id],
);
}
// -------------------------------------------------------------------------
// Mirrors
// -------------------------------------------------------------------------
const mirrorFrameMaterial = standardMaterial(0x244d58, {
roughness: 0.56,
metalness: 0.18,
});
const mirrorFaceMaterial = standardMaterial(0x9ff4e8, {
roughness: 0.2,
metalness: 0.25,
emissive: 0x1f746f,
emissiveIntensity: 0.22,
});
for (const mirror of mirrors) {
const root = new THREE.Group();
root.position.set(mirror.x, GROUND, mirror.z);
scene.add(root);
addBox(
root,
new THREE.Vector3(1.3, 0.32, 1.3),
new THREE.Vector3(0, 0.16, 0),
darkStone,
);
addBox(
root,
new THREE.Vector3(0.42, 0.72, 0.42),
new THREE.Vector3(0, 0.62, 0),
mirrorFrameMaterial,
);
const visual = new THREE.Group();
root.add(visual);
mirror.visual = visual;
const backing = addBox(
visual,
new THREE.Vector3(3.0, 2.0, 0.18),
new THREE.Vector3(0, 1.75, 0),
mirrorFrameMaterial,
);
const face = addBox(
visual,
new THREE.Vector3(2.68, 1.68, 0.07),
new THREE.Vector3(0, 1.75, -0.12),
mirrorFaceMaterial,
);
face.userData = { type: "mirror", index: mirror.id };
backing.userData = { type: "mirror", index: mirror.id };
interactiveMeshes.push(face, backing);
// Frame rails.
addBox(
visual,
new THREE.Vector3(3.12, 0.14, 0.3),
new THREE.Vector3(0, 2.77, 0),
paleStone,
);
addBox(
visual,
new THREE.Vector3(3.12, 0.14, 0.3),
new THREE.Vector3(0, 0.73, 0),
paleStone,
);
for (const x of [-1.48, 1.48]) {
addBox(
visual,
new THREE.Vector3(0.14, 2.12, 0.3),
new THREE.Vector3(x, 1.75, 0),
paleStone,
);
}
// Reflective tile divisions.
for (const x of [-0.72, 0, 0.72]) {
addBox(
visual,
new THREE.Vector3(0.055, 1.6, 0.035),
new THREE.Vector3(x, 1.75, -0.17),
mirrorFrameMaterial,
false,
);
}
const badgeMaterial = standardMaterial(channelColors[mirror.id], {
emissive: channelColors[mirror.id],
emissiveIntensity: 0.7,
});
addBox(
root,
new THREE.Vector3(0.42, 0.12, 0.42),
new THREE.Vector3(0, 0.35, 0),
badgeMaterial,
);
mirror.label = createWorldLabel(
`M${mirror.id + 1} · ${mirror.angle}°`,
new THREE.Vector3(mirror.x, GROUND + 3.03, mirror.z),
channelCss[mirror.id],
);
setMirrorAngle(mirror, mirror.angle);
}
function setMirrorAngle(mirror, angle) {
mirror.angle = angle;
mirror.visual.rotation.y = -THREE.MathUtils.degToRad(angle);
mirror.label.textElement.textContent = `M${mirror.id + 1} · ${angle}°`;
}
// -------------------------------------------------------------------------
// Receivers and shutters
// -------------------------------------------------------------------------
for (const receiver of receivers) {
const root = new THREE.Group();
root.position.set(receiver.x, GROUND, receiver.z);
scene.add(root);
addBox(
root,
new THREE.Vector3(2.15, 0.3, 2.15),
new THREE.Vector3(0, 0.15, 0),
darkStone,
);
addBox(
root,
new THREE.Vector3(1.4, 2.25, 1.75),
new THREE.Vector3(0, 1.35, 0),
roofMaterial,
);
addBox(
root,
new THREE.Vector3(1.85, 0.28, 2.05),
new THREE.Vector3(0, 2.62, 0),
paleStone,
);
// West-facing mouth.
addBox(
root,
new THREE.Vector3(0.08, 1.18, 1.18),
new THREE.Vector3(-0.73, 1.56, 0),
standardMaterial(0x142d33),
);
const coreMaterial = standardMaterial(0x7d5737, {
emissive: 0x1d1710,
emissiveIntensity: 0.12,
});
const core = addBox(
root,
new THREE.Vector3(0.32, 0.72, 0.72),
new THREE.Vector3(-0.86, 1.55, 0),
coreMaterial,
);
core.userData = { type: "receiver", index: receiver.id };
interactiveMeshes.push(core);
const shutterMaterial = standardMaterial(shutterColor, {
emissive: shutterColor,
emissiveIntensity: 0.18,
roughness: 0.62,
});
const shutter = addBox(
root,
new THREE.Vector3(0.17, 1.48, 1.3),
new THREE.Vector3(-0.98, 1.55, 0),
shutterMaterial,
);
shutter.userData = { type: "receiver", index: receiver.id };
interactiveMeshes.push(shutter);
// Vertical shutter rails.
for (const z of [-0.73, 0.73]) {
addBox(
root,
new THREE.Vector3(0.18, 2.05, 0.13),
new THREE.Vector3(-1.01, 1.65, z),
paleStone,
);
}
// Voxel status crown.
const crownMaterial = standardMaterial(channelColors[receiver.id], {
emissive: channelColors[receiver.id],
emissiveIntensity: 0.25,
});
addBox(
root,
new THREE.Vector3(0.45, 0.36, 0.45),
new THREE.Vector3(0.45, 2.96, -0.45),
crownMaterial,
);
receiver.root = root;
receiver.shutter = shutter;
receiver.core = core;
receiver.coreMaterial = coreMaterial;
receiver.crownMaterial = crownMaterial;
receiver.shutterTargetY = receiver.open ? 3.18 : 1.55;
receiver.label = createWorldLabel(
`R${receiver.id + 1}`,
new THREE.Vector3(receiver.x, GROUND + 3.55, receiver.z),
channelCss[receiver.id],
);
}
// -------------------------------------------------------------------------
// Beam simulation
// -------------------------------------------------------------------------
const beamMaterials = channelColors.map(
(color) =>
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.22,
depthWrite: false,
blending: THREE.AdditiveBlending,
toneMapped: false,
}),
);
const beamCoreMaterials = channelColors.map(
(color) =>
new THREE.MeshBasicMaterial({
color,
toneMapped: false,
}),
);
const mismatchMaterial = new THREE.MeshBasicMaterial({
color: 0xff7bd5,
toneMapped: false,
});
function cross2(a, b) {
return a.x * b.y - a.y * b.x;
}
function reflectDirection(direction, tangent) {
const dot = direction.x * tangent.x + direction.y * tangent.y;
return new THREE.Vector2(
2 * dot * tangent.x - direction.x,
2 * dot * tangent.y - direction.y,
).normalize();
}
function addBeamSegment(start, end, channel) {
const start3 = new THREE.Vector3(start.x, BEAM_Y, start.y);
const end3 = new THREE.Vector3(end.x, BEAM_Y, end.y);
const direction = end3.clone().sub(start3);
const length = direction.length();
if (length < 0.001) return;
const midpoint = start3.clone().add(end3).multiplyScalar(0.5);
const quaternion = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize(),
);
const outerGeometry = new THREE.CylinderGeometry(0.105, 0.105, length, 6);
const outer = new THREE.Mesh(outerGeometry, beamMaterials[channel]);
outer.position.copy(midpoint);
outer.quaternion.copy(quaternion);
outer.renderOrder = 3;
beamGroup.add(outer);
const coreGeometry = new THREE.CylinderGeometry(0.034, 0.034, length, 6);
const core = new THREE.Mesh(coreGeometry, beamCoreMaterials[channel]);
core.position.copy(midpoint);
core.quaternion.copy(quaternion);
core.renderOrder = 4;
beamGroup.add(core);
}
function addImpact(position, colorMaterial) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.22, 0.22),
colorMaterial,
);
marker.position.set(position.x, BEAM_Y, position.y);
marker.rotation.set(0.45, 0.45, 0.45);
marker.renderOrder = 5;
beamGroup.add(marker);
}
function clearBeamGroup() {
for (const child of [...beamGroup.children]) {
child.traverse((object) => {
if (object.geometry) object.geometry.dispose();
});
beamGroup.remove(child);
}
}
function findMirrorIntersection(origin, direction, mirror, visited) {
if (visited.has(mirror.id)) return null;
const angle = THREE.MathUtils.degToRad(mirror.angle);
const tangent = new THREE.Vector2(Math.cos(angle), Math.sin(angle));
const center = new THREE.Vector2(mirror.x, mirror.z);
const relative = center.clone().sub(origin);
const denominator = cross2(direction, tangent);
if (Math.abs(denominator) < 0.00001) return null;
const distance = cross2(relative, tangent) / denominator;
const alongMirror = cross2(relative, direction) / denominator;
if (
distance <= 0.025 ||
Math.abs(alongMirror) > MIRROR_HALF_LENGTH
) {
return null;
}
return {
distance,
point: origin.clone().addScaledVector(direction, distance),
tangent,
};
}
function findReceiverIntersection(origin, direction, receiver) {
// All receiver mouths face west.
if (direction.x <= 0.0001) return null;
const surfaceX = receiver.x - (receiver.open ? 0.83 : 0.98);
const distance = (surfaceX - origin.x) / direction.x;
if (distance <= 0.025) return null;
const z = origin.y + direction.y * distance;
if (Math.abs(z - receiver.z) > 0.59) return null;
return {
distance,
point: origin.clone().addScaledVector(direction, distance),
};
}
function traceSource(source) {
let origin = new THREE.Vector2(source.x, source.z + 0.73);
let direction = new THREE.Vector2(0, 1);
const visited = new Set();
let traveled = 0;
let reflections = 0;
while (traveled < MAX_RAY_LENGTH) {
const remaining = MAX_RAY_LENGTH - traveled;
let collision = null;
for (const mirror of mirrors) {
const intersection = findMirrorIntersection(
origin,
direction,
mirror,
visited,
);
if (
intersection &&
intersection.distance < remaining &&
(!collision || intersection.distance < collision.distance)
) {
collision = {
type: "mirror",
distance: intersection.distance,
point: intersection.point,
tangent: intersection.tangent,
mirror,
};
}
}
for (let index = 0; index < receivers.length; index++) {
const receiver = receivers[index];
const intersection = findReceiverIntersection(
origin,
direction,
receiver,
);
if (
intersection &&
intersection.distance < remaining &&
(!collision || intersection.distance < collision.distance)
) {
collision = {
type: "receiver",
distance: intersection.distance,
point: intersection.point,
receiver,
receiverIndex: index,
};
}
}
if (!collision) {
const end = origin.clone().addScaledVector(direction, remaining);
addBeamSegment(origin, end, source.id);
addImpact(end, mismatchMaterial);
break;
}
addBeamSegment(origin, collision.point, source.id);
traveled += collision.distance;
if (collision.type === "mirror") {
visited.add(collision.mirror.id);
addImpact(collision.point, beamCoreMaterials[source.id]);
if (reflections >= 2) {
break;
}
direction = reflectDirection(direction, collision.tangent);
origin = collision.point.clone().addScaledVector(direction, 0.035);
reflections++;
} else {
const receiver = collision.receiver;
const matching = receiver.id === source.id;
receiverHits[collision.receiverIndex].push({
sourceId: source.id,
matching,
open: receiver.open,
});
if (!matching || !receiver.open) {
addImpact(collision.point, mismatchMaterial);
}
break;
}
}
}
let receiverHits = [];
function updateReceiverState() {
let awake = 0;
for (let index = 0; index < receivers.length; index++) {
const receiver = receivers[index];
const matchingHits = receiverHits[index].filter((hit) => hit.matching);
const wrongHits = receiverHits[index].filter((hit) => !hit.matching);
receiver.active = receiver.open && matchingHits.length === 1;
receiver.matchingHits = matchingHits.length;
receiver.wrongHits = wrongHits.length;
if (receiver.active) awake++;
let status = "WAITING";
if (receiver.active) status = "AWAKE";
else if (!receiver.open && matchingHits.length) status = "SHUTTERED";
else if (!receiver.open) status = "CLOSED";
else if (wrongHits.length) status = "REJECTED";
receiver.status = status;
const chip = document.querySelector(`#chip-${index}`);
chip.textContent = `R${index + 1} · ${status}`;
chip.className = "chip";
if (receiver.active) chip.classList.add("active");
if (status === "SHUTTERED") chip.classList.add("blocked");
receiver.label.textElement.textContent = `R${receiver.id + 1} · ${status}`;
receiver.shutterTargetY = receiver.open ? 3.18 : 1.55;
}
document.querySelector("#awake-count").textContent = `${awake} / 3 awake`;
}
function rebuildRays() {
clearBeamGroup();
receiverHits = receivers.map(() => []);
for (const source of sources) {
traceSource(source);
}
updateReceiverState();
}
// -------------------------------------------------------------------------
// Direct camera controls
// -------------------------------------------------------------------------
function updateProjection() {
const aspect = window.innerWidth / window.innerHeight;
const halfHeight = Math.max(10.5, 14 / aspect) / cameraState.zoom;
const halfWidth = halfHeight * aspect;
camera.left = -halfWidth;
camera.right = halfWidth;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
}
function updateCamera() {
const distance = 34;
const horizontal = Math.cos(cameraState.elevation) * distance;
camera.position.set(
cameraState.target.x +
Math.sin(cameraState.azimuth) * horizontal,
cameraState.target.y + Math.sin(cameraState.elevation) * distance,
cameraState.target.z +
Math.cos(cameraState.azimuth) * horizontal,
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
updateProjection();
}
window.addEventListener("resize", resize);
const pointerState = {
down: false,
button: 0,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false,
};
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.button = event.button;
pointerState.startX = event.lastX;
pointerState.startY = event.lastY;
pointerState.lastX = event.lastX;
pointerState.lastY = event.lastY;
pointerState.moved = false;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerState.down) {
updateHover(event);
return;
}
const dx = event.clientX - pointerState.lastX;
const dy = event.clientY - pointerState.lastY;
if (
Math.hypot(
event.clientX - pointerState.startX,
event.clientY - pointerState.startY,
) > 5
) {
pointerState.moved = true;
}
if (pointerState.button === 2 || event.shiftKey) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3().crossVectors(
forward,
new THREE.Vector3(0, 1, 0),
);
const panScale = 0.018 / cameraState.zoom;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, dy * panScale);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-9,
9,
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-9,
9,
);
} else {
cameraState.azimuth -= dx * 0.006;
cameraState.elevation = THREE.MathUtils.clamp(
cameraState.elevation + dy * 0.005,
0.36,
1.25,
);
}
pointerState.lastX = event.clientX;
pointerState.lastY = event.clientY;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
canvas.releasePointerCapture(event.pointerId);
if (!pointerState.moved) {
handlePick(event);
}
pointerState.down = false;
});
canvas.addEventListener("pointercancel", () => {
pointerState.down = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
1.8,
);
updateProjection();
},
{ passive: false },
);
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 pickAt(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
return raycaster.intersectObjects(interactiveMeshes, false)[0];
}
function updateHover(event) {
const hit = pickAt(event);
canvas.style.cursor = hit ? "pointer" : "grab";
}
let toastTimer;
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 1500);
}
function handlePick(event) {
const hit = pickAt(event);
if (!hit) return;
const data = hit.object.userData;
if (data.type === "mirror") {
const mirror = mirrors[data.index];
mirror.angleIndex = (mirror.angleIndex + 1) % ANGLE_STEPS.length;
setMirrorAngle(mirror, ANGLE_STEPS[mirror.angleIndex]);
rebuildRays();
showToast(`M${mirror.id + 1} rotated to ${mirror.angle}°.`);
}
if (data.type === "receiver") {
const receiver = receivers[data.index];
receiver.open = !receiver.open;
rebuildRays();
showToast(
`R${receiver.id + 1} shutter ${receiver.open ? "raised" : "closed"}.`,
);
}
}
// -------------------------------------------------------------------------
// Buttons
// -------------------------------------------------------------------------
document.querySelector("#open-button").addEventListener("click", () => {
for (const receiver of receivers) receiver.open = true;
rebuildRays();
showToast("All receiver shutters raised.");
});
document.querySelector("#reset-button").addEventListener("click", () => {
for (let index = 0; index < mirrors.length; index++) {
mirrors[index].angleIndex = ANGLE_STEPS.indexOf(initialAngles[index]);
setMirrorAngle(mirrors[index], initialAngles[index]);
}
for (let index = 0; index < receivers.length; index++) {
receivers[index].open = initialShutters[index];
}
rebuildRays();
showToast("Courtyard reset to the initial tide.");
});
// -------------------------------------------------------------------------
// Animation and label projection
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
for (const wave of waves) {
wave.position.y =
-0.56 + Math.sin(elapsed * 0.7 + wave.userData.phase) * 0.035;
}
for (const receiver of receivers) {
receiver.shutter.position.y = THREE.MathUtils.lerp(
receiver.shutter.position.y,
receiver.shutterTargetY,
0.13,
);
const pulse = 1 + Math.sin(elapsed * 4.2 + receiver.id) * 0.08;
if (receiver.active) {
receiver.core.scale.setScalar(pulse);
receiver.coreMaterial.color.setHex(0xfff3a6);
receiver.coreMaterial.emissive.setHex(channelColors[receiver.id]);
receiver.coreMaterial.emissiveIntensity = 2.3;
receiver.crownMaterial.emissiveIntensity = 1.4;
} else {
receiver.core.scale.setScalar(1);
receiver.coreMaterial.color.setHex(0x7d5737);
receiver.coreMaterial.emissive.setHex(0x17130f);
receiver.coreMaterial.emissiveIntensity = 0.1;
receiver.crownMaterial.emissiveIntensity = 0.25;
}
}
for (const label of worldLabels) {
const projected = label.position.clone().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
projected.x < -1.1 ||
projected.x > 1.1 ||
projected.y < -1.1 ||
projected.y > 1.1
) {
label.element.style.display = "none";
} else {
label.element.style.display = "flex";
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateProjection();
updateCamera();
rebuildRays();
animate();
</script>
</body>
</html>
```