Prompt specification
Build or customize the Three.js 3D scene "Reflections and polarizing glasses exhibit" 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: Reflections and polarizing glasses exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/reflections-and-polarizing-glasses-exhibit-x1567
Scene Description Prompt:
Reflections and polarizing glasses exhibit. Compose a stylized water surface and glasses beside a separate ideal-filter demonstration; label natural reflections as qualitative and keep the exact law confined to the ideal setup. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (reflections-and-polarizing-glasses-exhibit-x1567):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Optics — Reflections and Polarizing Glasses</title>
<style>
:root {
color-scheme: dark;
--bg: #070c16;
--panel: rgba(12, 20, 34, 0.84);
--line: rgba(150, 203, 255, 0.22);
--text: #e9f3ff;
--muted: #9aacc2;
--cyan: #73e5ff;
--amber: #ffc66d;
--pink: #ff6fca;
}
* { 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;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging { cursor: grabbing; }
.header {
position: fixed;
z-index: 10;
top: 22px;
left: 26px;
right: 26px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 24px;
pointer-events: none;
}
.brand {
max-width: 440px;
text-shadow: 0 2px 18px #000;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 4px;
font-size: clamp(22px, 2.6vw, 38px);
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.controls {
pointer-events: auto;
display: flex;
align-items: center;
gap: 10px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(8, 15, 27, 0.78);
backdrop-filter: blur(12px);
box-shadow: 0 12px 45px rgba(0, 0, 0, 0.25);
}
button {
border: 1px solid rgba(142, 205, 255, 0.25);
border-radius: 9px;
padding: 8px 11px;
background: rgba(104, 188, 235, 0.08);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 150ms, border-color 150ms;
}
button:hover {
background: rgba(104, 188, 235, 0.17);
border-color: rgba(142, 205, 255, 0.5);
}
button.active {
color: #061019;
background: var(--cyan);
border-color: var(--cyan);
}
.angle-control {
display: grid;
grid-template-columns: auto 100px auto;
align-items: center;
gap: 8px;
padding-left: 6px;
font-size: 11px;
color: var(--muted);
}
input[type="range"] {
width: 100px;
accent-color: var(--cyan);
cursor: pointer;
}
#angle-value {
min-width: 34px;
color: var(--text);
font-variant-numeric: tabular-nums;
}
#labels {
position: fixed;
z-index: 5;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
transform: translate(-50%, -100%);
max-width: 250px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(7, 14, 25, 0.82);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(9px);
text-align: center;
white-space: nowrap;
}
.scene-label strong {
display: block;
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.scene-label span {
display: block;
margin-top: 3px;
color: #c3d3e4;
font-size: 10px;
}
.scene-label.qualitative strong { color: var(--amber); }
.scene-label.glasses strong { color: #a7f1d1; }
.scene-label.ideal strong { color: var(--pink); }
.scene-label.tag {
min-width: 50px;
padding: 5px 8px;
transform: translate(-50%, -50%);
border-radius: 7px;
font-size: 10px;
font-weight: 800;
}
.cards {
position: fixed;
z-index: 10;
left: 26px;
right: 26px;
bottom: 22px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 18px;
pointer-events: none;
}
.card {
width: min(390px, 46vw);
padding: 16px 17px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.card.natural { border-top-color: rgba(255, 198, 109, 0.8); }
.card.ideal { border-top-color: rgba(255, 111, 202, 0.8); }
.card-kicker {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 12px currentColor;
}
.ideal .dot { background: var(--pink); }
.card h2 {
margin: 8px 0 6px;
font-size: 17px;
letter-spacing: -0.02em;
}
.card p {
margin: 0;
color: #afbed0;
font-size: 12px;
line-height: 1.55;
}
.formula {
margin: 11px 0 9px;
padding: 10px 12px;
border: 1px solid rgba(255, 111, 202, 0.22);
border-radius: 9px;
background: rgba(255, 111, 202, 0.055);
color: #ffd4f0;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
text-align: center;
}
.readout {
display: flex;
justify-content: space-between;
gap: 12px;
color: var(--muted);
font-size: 11px;
}
.readout strong {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.meter {
height: 5px;
margin-top: 8px;
overflow: hidden;
border-radius: 5px;
background: rgba(255, 255, 255, 0.08);
}
#meter-fill {
width: 25%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--pink));
box-shadow: 0 0 12px rgba(255, 111, 202, 0.65);
transition: width 80ms linear;
}
.hint {
position: fixed;
z-index: 8;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
padding: 7px 11px;
border: 1px solid rgba(150, 203, 255, 0.13);
border-radius: 999px;
background: rgba(7, 13, 24, 0.68);
color: #8498b0;
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(8px);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 300ms;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 820px) {
.header {
top: 14px;
left: 14px;
right: 14px;
gap: 10px;
}
.subtitle { display: none; }
.controls {
padding: 7px;
gap: 6px;
}
.angle-control {
grid-template-columns: auto 74px auto;
}
input[type="range"] { width: 74px; }
button {
padding: 7px 8px;
font-size: 10px;
}
.cards {
left: 14px;
right: 14px;
bottom: 14px;
gap: 10px;
}
.card {
width: 50%;
padding: 11px 12px;
}
.card h2 { font-size: 14px; }
.card p { font-size: 10px; }
.formula { padding: 7px; font-size: 11px; }
.hint { display: none; }
.scene-label span { display: none; }
}
@media (max-width: 560px) {
.brand .eyebrow { font-size: 8px; }
h1 { font-size: 20px; }
.controls { flex-wrap: wrap; max-width: 185px; justify-content: flex-end; }
.angle-control { grid-template-columns: auto 58px auto; }
input[type="range"] { width: 58px; }
.cards { display: grid; grid-template-columns: 1fr 1fr; }
.card { width: auto; }
.card-kicker { font-size: 8px; }
.card h2 { font-size: 12px; }
.card p { display: none; }
.formula { margin-top: 7px; font-size: 9px; }
.readout { font-size: 9px; }
.scene-label { max-width: 130px; padding: 5px 7px; }
.scene-label strong { font-size: 8px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel optics exhibit"></canvas>
<header class="header">
<div class="brand">
<div class="eyebrow">Voxel optics lab · exhibit 04</div>
<h1>Reflections & Polarizing Glasses</h1>
<p class="subtitle">
A stylized natural scene beside a separate, mathematically ideal filter bench.
</p>
</div>
<div class="controls">
<button id="sweep" class="active" type="button">Auto sweep</button>
<label class="angle-control">
θ
<input id="angle" type="range" min="0" max="90" step="1" value="45">
<output id="angle-value">45°</output>
</label>
<button id="reset" type="button">Reset view</button>
</div>
</header>
<div id="labels" aria-hidden="true">
<div id="natural-label" class="scene-label qualitative">
<strong>Natural reflection · qualitative</strong>
<span>Stylized blocks suggest changing glare, not a ray law.</span>
</div>
<div id="glasses-label" class="scene-label glasses">
<strong>Polarizing glasses</strong>
<span>Aligned microscopic crystals favor one glare component.</span>
</div>
<div id="ideal-label" class="scene-label ideal">
<strong>Ideal filter bench · exact</strong>
<span>Unpolarized → polarizer → analyzer → detector</span>
</div>
<div id="p-label" class="scene-label tag">P₁ · 0°</div>
<div id="a-label" class="scene-label tag">A(θ)</div>
</div>
<section class="cards">
<article class="card natural">
<div class="card-kicker"><span class="dot"></span>Natural water · qualitative</div>
<h2>Real reflections are not an ideal law.</h2>
<p>
Viewing angle, surface roughness, and sky illumination can strengthen or
weaken glare. The blocky reflected colors are illustrative only.
Real polarizing glasses help suppress a dominant reflected component.
</p>
</article>
<article class="card ideal">
<div class="card-kicker"><span class="dot"></span>Separate ideal setup · exact law</div>
<h2>Two ideal linear filters.</h2>
<div class="formula">I<sub>out</sub> = (I<sub>in</sub> / 2) cos²(θ)</div>
<div class="readout">
<span>Analyzer angle <strong id="read-angle">45°</strong></span>
<span>Output <strong id="read-output">25.0% of I<sub>in</sub></strong></span>
</div>
<div class="meter"><div id="meter-fill"></div></div>
</article>
</section>
<div class="hint">Drag to orbit · scroll to zoom · shift-drag to pan · double-click to reset</div>
<div id="loading">Building voxel optics exhibit…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, camera
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070c16);
scene.fog = new THREE.Fog(0x070c16, 22, 43);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const initialDistance = window.innerWidth < 700 ? 27 : 21;
const orbit = {
yaw: 0.62,
pitch: 0.47,
distance: initialDistance,
target: new THREE.Vector3(0.1, 1.05, -0.05)
};
function updateCamera() {
const cp = Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * cp * orbit.distance,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * cp * orbit.distance
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xb9ddff, 0x161126, 2.0);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(4, 11, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -14;
key.shadow.camera.right = 14;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -10;
key.shadow.camera.near = 1;
key.shadow.camera.far = 35;
key.shadow.bias = -0.0004;
scene.add(key);
const cyanLight = new THREE.PointLight(0x40d8ff, 35, 13, 2);
cyanLight.position.set(-5.2, 3.2, 2.0);
scene.add(cyanLight);
const amberLight = new THREE.PointLight(0xffa53d, 28, 12, 2);
amberLight.position.set(5.5, 4.2, 0.4);
scene.add(amberLight);
const pinkLight = new THREE.PointLight(0xff54bf, 20, 9, 2);
pinkLight.position.set(8.0, 2.7, 0.6);
scene.add(pinkLight);
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
function standard(color, roughness = 0.65, metalness = 0.05, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true,
...extra
});
}
const materials = {
floor: standard(0x111b2b, 0.92),
plinth: standard(0x17243a, 0.76),
poolBase: standard(0x071522, 0.66),
border: standard(0x29425b, 0.48, 0.18),
darkFrame: standard(0x172434, 0.32, 0.38),
frameEdge: standard(0x7ce6ff, 0.28, 0.42, {
emissive: 0x174b61,
emissiveIntensity: 0.35
}),
lens: standard(0x48b9d7, 0.22, 0.08, {
transparent: true,
opacity: 0.52,
depthWrite: false,
side: THREE.DoubleSide
}),
filmAxis: standard(0xffc866, 0.35, 0.18, {
emissive: 0x7a4100,
emissiveIntensity: 0.65
}),
filterCyan: standard(0x69ddff, 0.28, 0.38, {
emissive: 0x12536d,
emissiveIntensity: 0.5
}),
filterAmber: standard(0xffbd65, 0.28, 0.38, {
emissive: 0x70400c,
emissiveIntensity: 0.5
}),
source: standard(0x29384c, 0.4, 0.3),
sourceLight: standard(0xfff0b0, 0.2, 0.05, {
emissive: 0xffc94d,
emissiveIntensity: 2.3
}),
detector: standard(0x263348, 0.4, 0.35),
detectorLight: standard(0xff8bd7, 0.25, 0.05, {
emissive: 0xff2ca8,
emissiveIntensity: 1.8
})
};
function box(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function instanceBlocks(points, size, material, parent = scene) {
const geometry = new THREE.BoxGeometry(1, 1, 1);
const mesh = new THREE.InstancedMesh(geometry, material, points.length);
const dummy = new THREE.Object3D();
points.forEach((point, index) => {
dummy.position.set(point.x, point.y, point.z);
const scale = point.scale ?? [size, size, size];
dummy.scale.set(scale[0], scale[1], scale[2]);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function hash(n) {
const value = Math.sin(n * 127.1 + 311.7) * 43758.5453123;
return value - Math.floor(value);
}
// -------------------------------------------------------------------------
// Voxel floor
// -------------------------------------------------------------------------
const floorPoints = [];
for (let x = 0; x < 24; x++) {
for (let z = 0; z < 15; z++) {
const px = -11 + x * 0.96;
const pz = -6.7 + z * 0.96;
const shade = 0.72 + hash(x * 31 + z * 7) * 0.25;
floorPoints.push({
x: px,
y: -0.12 + hash(x + z * 19) * 0.025,
z: pz,
scale: [0.92, 0.22, 0.92]
});
}
}
const floor = instanceBlocks(floorPoints, 1, materials.floor);
floor.receiveShadow = true;
// Section plinths.
box(scene, [6.95, 0.36, 5.15], [-4.18, 0.15, -0.6], materials.plinth);
box(scene, [7.65, 0.36, 3.45], [5.25, 0.15, 0], materials.plinth);
// -------------------------------------------------------------------------
// Stylized natural water pool
// -------------------------------------------------------------------------
box(
scene,
[6.82, 0.42, 4.84],
[-4.2, 0.32, -0.6],
materials.poolBase
);
const borderPoints = [];
for (let i = 0; i < 15; i++) {
const x = -7.46 + i * 0.47;
borderPoints.push({ x, y: 0.57, z: -3.03 });
borderPoints.push({ x, y: 0.57, z: 1.83 });
}
for (let i = 0; i < 11; i++) {
const z = -3.03 + i * 0.486;
borderPoints.push({ x: -7.51, y: 0.57, z });
borderPoints.push({ x: -0.89, y: 0.57, z });
}
instanceBlocks(
borderPoints,
0.34,
materials.border
);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.24,
metalness: 0.28,
flatShading: true,
vertexColors: true
});
const waterTiles = [];
const waterBaseColors = [];
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
const cols = 14;
const rows = 10;
const waterMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.43, 0.19, 0.43),
waterMaterial,
cols * rows
);
waterMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waterMesh.receiveShadow = true;
waterMesh.castShadow = false;
let waterIndex = 0;
for (let ix = 0; ix < cols; ix++) {
for (let iz = 0; iz < rows; iz++) {
const x = -7.26 + ix * 0.47;
const z = -2.79 + iz * 0.485;
const phase = hash(ix * 17 + iz * 29) * Math.PI * 2;
const baseY = 0.61 + hash(ix * 41 + iz * 13) * 0.025;
const color = new THREE.Color().setHSL(
0.53 + hash(ix * 3 + iz * 11) * 0.035,
0.56,
0.28 + hash(ix * 23 + iz * 7) * 0.10
);
waterTiles.push({ x, z, baseY, phase });
waterBaseColors.push(color.clone());
dummy.position.set(x, baseY, z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
waterMesh.setMatrixAt(waterIndex, dummy.matrix);
waterMesh.setColorAt(waterIndex, color);
waterIndex++;
}
}
waterMesh.instanceMatrix.needsUpdate = true;
waterMesh.instanceColor.needsUpdate = true;
scene.add(waterMesh);
// Voxel lights behind the pool create qualitative reflected streaks.
const towerDefinitions = [
{ x: -6.45, z: -3.55, height: 1.2, color: 0xffb04d },
{ x: -5.35, z: -3.7, height: 1.75, color: 0x69e5ff },
{ x: -3.8, z: -3.55, height: 1.35, color: 0xffd06c },
{ x: -2.45, z: -3.72, height: 2.05, color: 0x58caff }
];
for (let towerIndex = 0; towerIndex < towerDefinitions.length; towerIndex++) {
const tower = towerDefinitions[towerIndex];
const towerMat = standard(tower.color, 0.3, 0.18, {
emissive: tower.color,
emissiveIntensity: 0.85
});
const levels = Math.ceil(tower.height / 0.29);
for (let level = 0; level < levels; level++) {
box(
scene,
[0.29, 0.27, 0.29],
[tower.x, 0.62 + level * 0.285, tower.z],
towerMat
);
}
}
// Deliberately irregular reflection patches: qualitative, not ray-traced.
const reflectionData = [];
const reflectionMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.72,
depthWrite: false
}),
34
);
reflectionMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
for (let i = 0; i < 34; i++) {
const tower = towerDefinitions[i % towerDefinitions.length];
reflectionData.push({
x: tower.x + (hash(i * 5) - 0.5) * 0.52,
z: -2.35 + hash(i * 7) * 0.78,
width: 0.10 + hash(i * 11) * 0.24,
depth: 0.07 + hash(i * 13) * 0.14,
phase: hash(i * 17) * Math.PI * 2,
color: new THREE.Color(tower.color)
});
}
reflectionMesh.instanceMatrix.needsUpdate = true;
scene.add(reflectionMesh);
// -------------------------------------------------------------------------
// Voxel polarizing glasses beside the water
// -------------------------------------------------------------------------
const glasses = new THREE.Group();
glasses.position.set(0.25, 0.78, 1.12);
glasses.rotation.set(-0.035, -0.12, 0.015);
scene.add(glasses);
const framePoints = [];
const lensPoints = [];
const axisPoints = [];
function addLensFrame(cx, cy) {
const width = 1.12;
const height = 0.78;
const step = 0.14;
for (let i = 0; i <= 8; i++) {
const x = cx - width / 2 + i * step;
framePoints.push({ x, y: cy - height / 2, z: 0 });
framePoints.push({ x, y: cy + height / 2, z: 0 });
}
for (let i = 1; i < 5; i++) {
const y = cy - height / 2 + i * step;
framePoints.push({ x: cx - width / 2, y, z: 0 });
framePoints.push({ x: cx + width / 2, y, z: 0 });
}
for (let ix = 0; ix < 7; ix++) {
for (let iy = 0; iy < 5; iy++) {
lensPoints.push({
x: cx - 0.42 + ix * 0.14,
y: cy - 0.28 + iy * 0.14,
z: 0
});
}
}
for (let i = 0; i < 7; i++) {
axisPoints.push({
x: cx - 0.42 + i * 0.14,
y: cy,
z: 0.095
});
}
}
addLensFrame(-0.73, 0.58);
addLensFrame(0.73, 0.58);
// Bridge.
for (let i = 0; i < 5; i++) {
framePoints.push({ x: -0.28 + i * 0.14, y: 0.69, z: 0 });
}
// Temples extending backward.
for (const side of [-1, 1]) {
for (let i = 0; i < 7; i++) {
framePoints.push({
x: side * 1.29,
y: 0.83,
z: -0.08 - i * 0.14
});
}
for (let i = 0; i < 3; i++) {
framePoints.push({
x: side * (1.29 - i * 0.035),
y: 0.83 - i * 0.09,
z: -1.05
});
}
}
instanceBlocks(framePoints, 0.13, materials.darkFrame, glasses);
instanceBlocks(lensPoints, 0.115, materials.lens, glasses);
instanceBlocks(axisPoints, 0.075, materials.filmAxis, glasses);
// Cyan corner accents.
const glassesAccents = [];
for (const cx of [-1.29, 1.29]) {
for (const cy of [0.19, 0.97]) {
glassesAccents.push({ x: cx, y: cy, z: 0.035 });
}
}
instanceBlocks(glassesAccents, 0.14, materials.frameEdge, glasses);
// Glasses stand.
box(scene, [1.85, 0.18, 1.05], [0.25, 0.49, 1.12], materials.border);
box(scene, [0.18, 0.63, 0.18], [0.25, 0.76, 1.18], materials.darkFrame);
box(scene, [0.68, 0.12, 0.24], [0.25, 1.02, 1.18], materials.border);
// -------------------------------------------------------------------------
// Ideal filter bench
// -------------------------------------------------------------------------
const beamY = 2.38;
const sourceX = 2.32;
const polarizerX = 4.18;
const analyzerX = 6.65;
const detectorX = 8.28;
function makeFilter(x, colorMaterial, axisMaterial) {
const group = new THREE.Group();
group.position.set(x, beamY, 0);
scene.add(group);
const frame = [];
// Square frame in the YZ plane.
for (let i = 0; i <= 8; i++) {
const z = -0.68 + i * 0.17;
frame.push({ x: 0, y: 0.68, z });
frame.push({ x: 0, y: -0.68, z });
}
for (let i = 1; i < 8; i++) {
const y = -0.68 + i * 0.17;
frame.push({ x: 0, y, z: -0.68 });
frame.push({ x: 0, y, z: 0.68 });
}
instanceBlocks(frame, 0.15, colorMaterial, group);
// Thin tiled filter medium.
const medium = [];
for (let iy = 0; iy < 7; iy++) {
for (let iz = 0; iz < 7; iz++) {
medium.push({
x: 0,
y: -0.51 + iy * 0.17,
z: -0.51 + iz * 0.17,
scale: [0.055, 0.13, 0.13]
});
}
}
instanceBlocks(medium, 1, colorMaterial, group);
// The bright bar is the ideal transmission axis.
const axisPivot = new THREE.Group();
group.add(axisPivot);
box(
axisPivot,
[0.12, 1.28, 0.11],
[0.025, 0, 0],
axisMaterial,
false
);
box(
axisPivot,
[0.14, 0.12, 0.14],
[0.025, 0.72, 0],
axisMaterial,
false
);
return { group, axisPivot };
}
const polarizer = makeFilter(
polarizerX,
materials.filterCyan,
materials.frameEdge
);
const analyzer = makeFilter(
analyzerX,
materials.filterAmber,
materials.filmAxis
);
// Filter stands.
for (const x of [polarizerX, analyzerX]) {
box(scene, [0.52, 0.15, 1.32], [x, 0.47, 0], materials.border);
box(scene, [0.18, 1.53, 0.18], [x, 1.22, 0], materials.darkFrame);
box(scene, [0.58, 0.13, 0.58], [x, 1.92, 0], materials.border);
}
// Voxel source.
box(scene, [0.85, 0.92, 0.92], [sourceX, beamY, 0], materials.source);
box(scene, [0.16, 0.54, 0.54], [sourceX + 0.47, beamY, 0], materials.sourceLight);
box(scene, [0.72, 0.16, 0.72], [sourceX, 0.48, 0], materials.border);
box(scene, [0.16, 1.05, 0.16], [sourceX, 0.98, 0], materials.darkFrame);
// Detector frame and pixel blocks.
const detectorFrame = [];
for (let i = 0; i <= 8; i++) {
const z = -0.68 + i * 0.17;
detectorFrame.push({ x: detectorX, y: beamY + 0.68, z });
detectorFrame.push({ x: detectorX, y: beamY - 0.68, z });
}
for (let i = 1; i < 8; i++) {
const y = beamY - 0.68 + i * 0.17;
detectorFrame.push({ x: detectorX, y, z: -0.68 });
detectorFrame.push({ x: detectorX, y, z: 0.68 });
}
instanceBlocks(detectorFrame, 0.15, materials.detector);
const detectorPixels = [];
const detectorPixelData = [];
for (let iy = 0; iy < 5; iy++) {
for (let iz = 0; iz < 5; iz++) {
const y = beamY - 0.32 + iy * 0.16;
const z = -0.32 + iz * 0.16;
const order = Math.abs(iy - 2) + Math.abs(iz - 2);
detectorPixels.push({ x: detectorX + 0.025, y, z });
detectorPixelData.push({ y, z, order });
}
}
const detectorMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.07, 0.13, 0.13),
new THREE.MeshBasicMaterial({ color: 0xffffff }),
detectorPixels.length
);
detectorPixels.forEach((p, i) => {
dummy.position.set(p.x, p.y, p.z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
detectorMesh.setMatrixAt(i, dummy.matrix);
detectorMesh.setColorAt(i, new THREE.Color(0x281331));
});
detectorMesh.instanceMatrix.needsUpdate = true;
detectorMesh.instanceColor.needsUpdate = true;
scene.add(detectorMesh);
box(scene, [0.18, 1.0, 0.18], [detectorX, 1.25, 0], materials.darkFrame);
box(scene, [0.75, 0.16, 0.95], [detectorX, 0.48, 0], materials.border);
// -------------------------------------------------------------------------
// Moving voxel field dashes
// -------------------------------------------------------------------------
function createStream(startX, endX, count, color, kind) {
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.94
}),
count
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(mesh);
return {
mesh,
startX,
endX,
count,
color: new THREE.Color(color),
kind
};
}
const streams = [
createStream(sourceX + 0.56, polarizerX - 0.13, 15, 0xffdf75, "unpolarized"),
createStream(polarizerX + 0.13, analyzerX - 0.13, 17, 0x69e4ff, "polarized"),
createStream(analyzerX + 0.13, detectorX - 0.15, 16, 0xff70c9, "analyzed")
];
// -------------------------------------------------------------------------
// UI and exact ideal-filter calculation
// -------------------------------------------------------------------------
const angleInput = document.querySelector("#angle");
const angleValue = document.querySelector("#angle-value");
const readAngle = document.querySelector("#read-angle");
const readOutput = document.querySelector("#read-output");
const meterFill = document.querySelector("#meter-fill");
const sweepButton = document.querySelector("#sweep");
let autoSweep = true;
let theta = THREE.MathUtils.degToRad(Number(angleInput.value));
let outputIntensity = 0.5 * Math.cos(theta) ** 2;
function updateAngleUI() {
const degrees = Math.round(THREE.MathUtils.radToDeg(theta));
angleValue.textContent = `${degrees}°`;
readAngle.textContent = `${degrees}°`;
readOutput.innerHTML = `${(outputIntensity * 100).toFixed(1)}% of I<sub>in</sub>`;
meterFill.style.width = `${outputIntensity * 100}%`;
document.querySelector("#a-label").textContent = `A(${degrees}°)`;
}
function calculateIdealOutput() {
theta = THREE.MathUtils.degToRad(Number(angleInput.value));
outputIntensity = 0.5 * Math.cos(theta) ** 2;
analyzer.axisPivot.rotation.x = theta;
updateAngleUI();
}
angleInput.addEventListener("input", () => {
autoSweep = false;
sweepButton.classList.remove("active");
sweepButton.textContent = "Auto sweep";
calculateIdealOutput();
});
sweepButton.addEventListener("click", () => {
autoSweep = !autoSweep;
sweepButton.classList.toggle("active", autoSweep);
sweepButton.textContent = autoSweep ? "Auto sweep" : "Sweep paused";
});
// -------------------------------------------------------------------------
// Camera controls implemented directly, without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (event.shiftKey || event.buttons === 2) {
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(camera.quaternion);
orbit.target.addScaledVector(right, -dx * orbit.distance * 0.0014);
orbit.target.addScaledVector(up, dy * orbit.distance * 0.0012);
} else {
orbit.yaw -= dx * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.0045,
0.16,
1.15
);
}
updateCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.distance = THREE.MathUtils.clamp(
orbit.distance + event.deltaY * 0.012,
12,
34
);
updateCamera();
},
{ passive: false }
);
function resetView() {
orbit.yaw = 0.62;
orbit.pitch = 0.47;
orbit.distance = window.innerWidth < 700 ? 27 : 21;
orbit.target.set(0.1, 1.05, -0.05);
updateCamera();
}
document.querySelector("#reset").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
// -------------------------------------------------------------------------
// Projected scene annotations
// -------------------------------------------------------------------------
function placeLabel(element, worldPosition, offsetX = 0, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth + offsetX}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY}px`;
}
const naturalAnchor = new THREE.Vector3(-4.8, 3.15, -2.8);
const glassesAnchor = new THREE.Vector3(0.3, 2.15, 1.1);
const idealAnchor = new THREE.Vector3(5.15, 3.8, -1.5);
const pAnchor = new THREE.Vector3(polarizerX, 3.35, 0);
const aAnchor = new THREE.Vector3(analyzerX, 3.35, 0);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
if (autoSweep) {
const degrees = 45 + 45 * Math.sin(elapsed * 0.38);
angleInput.value = String(Math.round(degrees));
calculateIdealOutput();
}
// Gentle quantized water motion.
waterTiles.forEach((tile, index) => {
const wave = Math.sin(elapsed * 0.85 + tile.phase);
const smallWave = Math.sin(elapsed * 1.4 + tile.phase * 0.7);
dummy.position.set(
tile.x,
tile.baseY + wave * 0.022 + smallWave * 0.008,
tile.z
);
dummy.scale.set(1, 1 + wave * 0.025, 1);
dummy.updateMatrix();
waterMesh.setMatrixAt(index, dummy.matrix);
tempColor
.copy(waterBaseColors[index])
.multiplyScalar(0.92 + wave * 0.08);
waterMesh.setColorAt(index, tempColor);
});
waterMesh.instanceMatrix.needsUpdate = true;
waterMesh.instanceColor.needsUpdate = true;
// Qualitative reflected patches shimmer and change shape.
reflectionData.forEach((patch, index) => {
const flicker = 0.62 + 0.38 * Math.sin(elapsed * 1.3 + patch.phase);
const broken = 0.72 + 0.28 * Math.sin(elapsed * 1.9 + patch.phase * 1.7);
dummy.position.set(
patch.x,
0.735 + Math.sin(elapsed + patch.phase) * 0.006,
patch.z
);
dummy.scale.set(
patch.width * broken,
0.035,
patch.depth * flicker
);
dummy.updateMatrix();
reflectionMesh.setMatrixAt(index, dummy.matrix);
tempColor.copy(patch.color).multiplyScalar(0.55 + flicker * 0.55);
reflectionMesh.setColorAt(index, tempColor);
});
reflectionMesh.instanceMatrix.needsUpdate = true;
reflectionMesh.instanceColor.needsUpdate = true;
// Animate the three optical streams.
streams.forEach((stream, streamIndex) => {
for (let i = 0; i < stream.count; i++) {
const progress = (elapsed * 0.19 + i / stream.count) % 1;
const x = THREE.MathUtils.lerp(stream.startX, stream.endX, progress);
let rotation = 0;
let yOffset = 0;
let zOffset = 0;
let brightness = 1;
if (stream.kind === "unpolarized") {
rotation = i * 2.39996 + elapsed * 0.17;
yOffset = Math.sin(rotation) * 0.075;
zOffset = Math.cos(rotation) * 0.075;
} else if (stream.kind === "polarized") {
rotation = 0;
brightness = 0.64;
} else {
rotation = theta;
brightness = 0.12 + 0.88 * Math.sqrt(outputIntensity);
if (outputIntensity < 0.001) brightness = 0.02;
}
dummy.position.set(x, beamY + yOffset, zOffset);
dummy.rotation.set(rotation, 0, 0);
const dashLength = stream.kind === "unpolarized" ? 0.14 : 0.19;
dummy.scale.set(
0.085,
dashLength * brightness,
0.045 * brightness
);
dummy.updateMatrix();
stream.mesh.setMatrixAt(i, dummy.matrix);
tempColor
.copy(stream.color)
.multiplyScalar(streamIndex === 2 ? brightness : 0.86);
stream.mesh.setColorAt(i, tempColor);
}
stream.mesh.instanceMatrix.needsUpdate = true;
stream.mesh.instanceColor.needsUpdate = true;
});
// Detector illumination, quantized into visible voxel pixels.
const litCount = Math.round(outputIntensity * detectorPixelData.length);
detectorPixelData.forEach((pixel, index) => {
const lit = pixel.order < litCount;
tempColor.set(lit ? 0xff64c7 : 0x281331);
if (lit) {
tempColor.multiplyScalar(0.72 + outputIntensity * 0.55);
}
detectorMesh.setColorAt(index, tempColor);
});
detectorMesh.instanceColor.needsUpdate = true;
updateCamera();
placeLabel(document.querySelector("#natural-label"), naturalAnchor, 0, -8);
placeLabel(document.querySelector("#glasses-label"), glassesAnchor, 0, -8);
placeLabel(document.querySelector("#ideal-label"), idealAnchor, 0, -8);
placeLabel(document.querySelector("#p-label"), pAnchor);
placeLabel(document.querySelector("#a-label"), aAnchor);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
});
calculateIdealOutput();
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
});
</script>
</body>
</html>
```