Prompt specification
Build or customize the Three.js 3D scene "Camera obscura and lens camera 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: Camera obscura and lens camera exhibit
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/camera-obscura-and-lens-camera-exhibit-x1559
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this voxel scene: Compose a pinhole box beside a simplified lens camera with image screens; compare their image-forming geometry without claiming photorealistic rendering or depth-of-field accuracy.
Working Three.js Starter (camera-obscura-and-lens-camera-exhibit-x1559):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Image-Formation Comparison</title>
<style>
:root {
color-scheme: dark;
--ink: #eaf4f5;
--muted: #a9bdc2;
--panel: rgba(12, 22, 31, 0.88);
--line: rgba(174, 224, 228, 0.2);
--orange: #ffb44b;
--cyan: #63dce5;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b111a;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 14px;
backdrop-filter: blur(12px);
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.24);
}
#title-card {
top: 20px;
left: 20px;
max-width: min(460px, calc(100vw - 40px));
padding: 16px 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--ink);
font-size: clamp(20px, 2.3vw, 30px);
line-height: 1.08;
}
#title-card p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#controls {
top: 20px;
right: 20px;
display: flex;
gap: 8px;
padding: 8px;
}
button {
border: 1px solid rgba(174, 224, 228, 0.28);
border-radius: 9px;
padding: 9px 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.05);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease;
}
button:hover {
background: rgba(99, 220, 229, 0.13);
border-color: rgba(99, 220, 229, 0.55);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
#note {
left: 20px;
bottom: 20px;
max-width: min(620px, calc(100vw - 40px));
padding: 13px 16px;
pointer-events: none;
}
#note strong {
color: var(--ink);
}
#note span {
color: var(--muted);
}
#legend {
right: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 14px;
padding: 11px 14px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 18px;
height: 5px;
border-radius: 3px;
background: var(--orange);
box-shadow: 0 0 8px rgba(255, 180, 75, 0.35);
}
.swatch.cyan {
background: var(--cyan);
box-shadow: 0 0 8px rgba(99, 220, 229, 0.35);
}
.hint {
color: #8299a1;
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
padding: 12px 14px;
}
#title-card p {
display: none;
}
#controls {
top: auto;
right: 12px;
bottom: 76px;
flex-direction: column;
}
#note {
left: 12px;
right: 12px;
bottom: 12px;
max-width: none;
}
#legend {
display: none;
}
}
</style>
</head>
<body>
<div id="title-card" class="panel">
<div class="eyebrow">Voxel optics exhibit</div>
<h1>Pinhole projection vs. a simplified lens camera</h1>
<p>
Drag to orbit, scroll or pinch to zoom, and use the arrow keys to
adjust the view. The displays compare idealized image-forming geometry.
</p>
</div>
<div id="controls" class="panel">
<button id="reset-view" type="button">Reset view</button>
<button id="toggle-rays" type="button" aria-pressed="true">Hide ray guides</button>
</div>
<div id="note" class="panel">
<strong>Geometric comparison only.</strong>
<span>
The lens is represented by one idealized thin-lens plane. This is not a
photorealistic renderer, a lens solver, or a depth-of-field simulation.
</span>
</div>
<div id="legend" class="panel">
<div class="legend-item">
<span class="swatch"></span>
Pinhole rays
</div>
<div class="legend-item">
<span class="swatch cyan"></span>
Lens rays
</div>
<div class="hint">Drag · Scroll · Arrow keys</div>
</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 scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b111a);
scene.fog = new THREE.Fog(0x0b111a, 25, 48);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D voxel scene comparing pinhole and lens image formation"
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbfe8ef, 0x26313a, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d7, 3.1);
keyLight.position.set(-7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -11;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x67dfe8, 24, 15, 2);
cyanLight.position.set(7, 5, 4);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffae54, 18, 12, 2);
warmLight.position.set(-7, 4, 4);
scene.add(warmLight);
const materials = {
platform: new THREE.MeshStandardMaterial({
color: 0xcac8bd,
roughness: 0.92,
metalness: 0.02
}),
platformEdge: new THREE.MeshStandardMaterial({
color: 0x768188,
roughness: 0.78,
metalness: 0.12
}),
dark: new THREE.MeshStandardMaterial({
color: 0x17232d,
roughness: 0.72,
metalness: 0.18
}),
darker: new THREE.MeshStandardMaterial({
color: 0x0c141c,
roughness: 0.64,
metalness: 0.24
}),
teal: new THREE.MeshStandardMaterial({
color: 0x2f7e83,
roughness: 0.66,
metalness: 0.12
}),
tealLight: new THREE.MeshStandardMaterial({
color: 0x66c6c6,
roughness: 0.58,
metalness: 0.08
}),
blue: new THREE.MeshStandardMaterial({
color: 0x315875,
roughness: 0.63,
metalness: 0.2
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf1ead6,
roughness: 0.9
}),
orange: new THREE.MeshStandardMaterial({
color: 0xffa83d,
roughness: 0.62,
emissive: 0x5b2800,
emissiveIntensity: 0.28
}),
green: new THREE.MeshStandardMaterial({
color: 0x8fd2a6,
roughness: 0.76
}),
red: new THREE.MeshStandardMaterial({
color: 0xee7569,
roughness: 0.68
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x6ee6ee,
roughness: 0.5,
emissive: 0x073d43,
emissiveIntensity: 0.28
}),
glass: new THREE.MeshStandardMaterial({
color: 0x63dce5,
roughness: 0.2,
metalness: 0.05,
transparent: true,
opacity: 0.48,
depthWrite: false,
side: THREE.DoubleSide
}),
cutaway: new THREE.MeshStandardMaterial({
color: 0x75aeb3,
roughness: 0.8,
transparent: true,
opacity: 0.16,
depthWrite: false
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x071018,
transparent: true,
opacity: 0.32
});
function addBox(parent, size, position, material, outlined = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(edges);
}
return mesh;
}
function addBeam(parent, start, end, color, thickness = 0.025, opacity = 0.94) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
const midpoint = a.clone().add(b).multiplyScalar(0.5);
const material = new THREE.MeshBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(thickness, length, thickness),
material
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
parent.add(mesh);
return mesh;
}
function roundRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makeTexture(canvas) {
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
// -------------------------------------------------------------------------
// Floor and exhibit platform
// -------------------------------------------------------------------------
addBox(
scene,
[18, 0.46, 9],
[0, -0.27, 0],
materials.platform
);
addBox(
scene,
[18.12, 0.16, 9.12],
[0, -0.54, 0],
materials.platformEdge
);
const gridPoints = [];
for (let x = -8; x <= 8; x += 1) {
gridPoints.push(
new THREE.Vector3(x, 0.006, -4.35),
new THREE.Vector3(x, 0.006, 4.35)
);
}
for (let z = -4; z <= 4; z += 1) {
gridPoints.push(
new THREE.Vector3(-8.75, 0.006, z),
new THREE.Vector3(8.75, 0.006, z)
);
}
const gridGeometry = new THREE.BufferGeometry().setFromPoints(gridPoints);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x52646b,
transparent: true,
opacity: 0.28
})
);
scene.add(grid);
// Small voxel markers around the exhibit.
for (let i = 0; i < 8; i++) {
const marker = addBox(
scene,
[0.18, 0.12 + (i % 3) * 0.07, 0.18],
[-7.7 + i * 2.2, 0.08, 3.95],
i % 2 ? materials.tealLight : materials.orange
);
marker.castShadow = false;
}
// -------------------------------------------------------------------------
// Canvas artwork for the comparison screens
// -------------------------------------------------------------------------
function drawArrow(ctx, x, y, scale, color, inverted = false) {
ctx.save();
ctx.translate(x, y);
ctx.scale(scale, scale * (inverted ? -1 : 1));
ctx.fillStyle = color;
ctx.beginPath();
ctx.moveTo(0, -58);
ctx.lineTo(-37, -14);
ctx.lineTo(-15, -14);
ctx.lineTo(-15, 50);
ctx.lineTo(15, 50);
ctx.lineTo(15, -14);
ctx.lineTo(37, -14);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function makePinholeDisplayTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 640;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#0e1922";
ctx.fillRect(0, 0, 1024, 640);
ctx.fillStyle = "#ffb44b";
ctx.fillRect(0, 0, 1024, 72);
ctx.fillStyle = "#102029";
ctx.font = "800 34px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.fillText("PINHOLE BOX", 34, 48);
ctx.fillStyle = "#0e1922";
ctx.font = "700 18px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.fillText("STRAIGHT-RAY PROJECTION", 990, 44);
roundRect(ctx, 28, 96, 680, 452, 18);
ctx.fillStyle = "#13222c";
ctx.fill();
ctx.strokeStyle = "#29404b";
ctx.lineWidth = 2;
ctx.stroke();
const axisY = 315;
ctx.strokeStyle = "#61747c";
ctx.lineWidth = 2;
ctx.setLineDash([9, 9]);
ctx.beginPath();
ctx.moveTo(74, axisY);
ctx.lineTo(660, axisY);
ctx.stroke();
ctx.setLineDash([]);
// Object.
drawArrow(ctx, 110, 286, 0.72, "#8fd2a6");
ctx.fillStyle = "#b6c9cd";
ctx.font = "700 17px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("UPRIGHT OBJECT", 110, 500);
// Aperture barrier with a gap.
ctx.fillStyle = "#47777c";
ctx.fillRect(319, 138, 18, 163);
ctx.fillRect(319, 325, 18, 166);
ctx.strokeStyle = "#ffb44b";
ctx.lineWidth = 4;
ctx.strokeRect(309, 289, 38, 43);
ctx.fillStyle = "#ffcf84";
ctx.font = "800 17px system-ui, sans-serif";
ctx.fillText("PINHOLE", 328, 510);
// Image plane.
ctx.fillStyle = "#efe8d4";
ctx.fillRect(584, 137, 13, 345);
// Crossing rays.
ctx.strokeStyle = "#ffb44b";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(110, 225);
ctx.lineTo(328, 311);
ctx.lineTo(590, 400);
ctx.moveTo(110, 385);
ctx.lineTo(328, 311);
ctx.lineTo(590, 214);
ctx.stroke();
ctx.fillStyle = "#f0e8d8";
ctx.font = "700 17px system-ui, sans-serif";
ctx.fillText("IMAGE PLANE", 590, 510);
// Inverted image card.
roundRect(ctx, 735, 111, 258, 386, 18);
ctx.fillStyle = "#f0ead8";
ctx.fill();
ctx.fillStyle = "#23323a";
ctx.font = "800 19px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("PROJECTED IMAGE", 864, 151);
ctx.strokeStyle = "#b5b5aa";
ctx.lineWidth = 1;
for (let x = 750; x < 990; x += 24) {
ctx.beginPath();
ctx.moveTo(x, 176);
ctx.lineTo(x, 460);
ctx.stroke();
}
for (let y = 176; y < 460; y += 24) {
ctx.beginPath();
ctx.moveTo(750, y);
ctx.lineTo(990, y);
ctx.stroke();
}
drawArrow(ctx, 864, 305, 1.0, "#32777b", true);
ctx.fillStyle = "#34454a";
ctx.font = "700 16px system-ui, sans-serif";
ctx.fillText("TOP ↔ BOTTOM INVERTED", 864, 476);
ctx.fillStyle = "#aebfc3";
ctx.font = "600 19px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.fillText("No lens: rays continue straight through one aperture.", 34, 590);
ctx.fillStyle = "#ffcb7b";
ctx.font = "800 18px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.fillText("INVERSION AT THE HOLE", 990, 590);
return makeTexture(canvas);
}
function makeLensDisplayTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 640;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#0e1922";
ctx.fillRect(0, 0, 1024, 640);
ctx.fillStyle = "#63dce5";
ctx.fillRect(0, 0, 1024, 72);
ctx.fillStyle = "#102029";
ctx.font = "800 34px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.fillText("LENS CAMERA", 34, 48);
ctx.fillStyle = "#0e1922";
ctx.font = "700 18px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.fillText("IDEALIZED THIN-LENS GEOMETRY", 990, 44);
roundRect(ctx, 28, 96, 680, 452, 18);
ctx.fillStyle = "#13222c";
ctx.fill();
ctx.strokeStyle = "#29404b";
ctx.lineWidth = 2;
ctx.stroke();
const axisY = 315;
ctx.strokeStyle = "#61747c";
ctx.lineWidth = 2;
ctx.setLineDash([9, 9]);
ctx.beginPath();
ctx.moveTo(74, axisY);
ctx.lineTo(660, axisY);
ctx.stroke();
ctx.setLineDash([]);
drawArrow(ctx, 108, 280, 0.72, "#ee7569");
ctx.fillStyle = "#b6c9cd";
ctx.font = "700 17px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("OBJECT", 108, 500);
// Idealized lens plane.
ctx.fillStyle = "#63dce5";
ctx.fillRect(340, 142, 13, 339);
ctx.strokeStyle = "#a6f6f7";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(330, 311);
ctx.quadraticCurveTo(347, 276, 364, 311);
ctx.quadraticCurveTo(347, 346, 330, 311);
ctx.stroke();
ctx.fillStyle = "#9aedf0";
ctx.font = "800 17px system-ui, sans-serif";
ctx.fillText("LENS PLANE", 347, 510);
// Image plane.
ctx.fillStyle = "#efe8d4";
ctx.fillRect(600, 137, 13, 345);
// Rays from an off-axis object point, bending at the lens plane.
ctx.strokeStyle = "#63dce5";
ctx.lineWidth = 4;
ctx.beginPath();
ctx.moveTo(108, 218);
ctx.lineTo(346, 246);
ctx.lineTo(606, 405);
ctx.moveTo(108, 218);
ctx.lineTo(346, 310);
ctx.lineTo(606, 405);
ctx.moveTo(108, 218);
ctx.lineTo(346, 374);
ctx.lineTo(606, 405);
ctx.stroke();
// A parallel bundle adds a clear contrast with the pinhole geometry.
ctx.strokeStyle = "#b8f8fa";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.moveTo(82, 258);
ctx.lineTo(346, 258);
ctx.lineTo(606, 405);
ctx.moveTo(82, 284);
ctx.lineTo(346, 284);
ctx.lineTo(606, 405);
ctx.stroke();
ctx.fillStyle = "#f0e8d8";
ctx.font = "700 17px system-ui, sans-serif";
ctx.fillText("IMAGE PLANE", 606, 510);
// Ideal thin-lens reference marks.
ctx.strokeStyle = "#6b7e85";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(473, 342);
ctx.lineTo(473, 363);
ctx.moveTo(600, 342);
ctx.lineTo(600, 363);
ctx.stroke();
ctx.fillStyle = "#93a9af";
ctx.font = "700 16px system-ui, sans-serif";
ctx.fillText("f", 473, 389);
ctx.fillText("2f", 600, 389);
// Inverted image card.
roundRect(ctx, 735, 111, 258, 386, 18);
ctx.fillStyle = "#f0ead8";
ctx.fill();
ctx.fillStyle = "#23323a";
ctx.font = "800 19px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText("SENSOR IMAGE", 864, 151);
ctx.strokeStyle = "#b5b5aa";
ctx.lineWidth = 1;
for (let x = 750; x < 990; x += 24) {
ctx.beginPath();
ctx.moveTo(x, 176);
ctx.lineTo(x, 460);
ctx.stroke();
}
for (let y = 176; y < 460; y += 24) {
ctx.beginPath();
ctx.moveTo(750, y);
ctx.lineTo(990, y);
ctx.stroke();
}
drawArrow(ctx, 864, 305, 1.0, "#356e87", true);
ctx.fillStyle = "#34454a";
ctx.font = "700 16px system-ui, sans-serif";
ctx.fillText("TOP ↔ BOTTOM INVERTED", 864, 476);
ctx.fillStyle = "#aebfc3";
ctx.font = "600 19px system-ui, sans-serif";
ctx.textAlign = "left";
ctx.fillText("Rays bend at one idealized lens plane.", 34, 590);
ctx.fillStyle = "#9debf0";
ctx.font = "800 18px system-ui, sans-serif";
ctx.textAlign = "right";
ctx.fillText("NOT A LENS SOLVER", 990, 590);
return makeTexture(canvas);
}
function makeSensorTexture(type) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 480;
const ctx = canvas.getContext("2d");
ctx.fillStyle = type === "pinhole" ? "#efe8d5" : "#dfe8e8";
ctx.fillRect(0, 0, 640, 480);
ctx.fillStyle = type === "pinhole" ? "#244c50" : "#294e68";
ctx.fillRect(0, 0, 640, 55);
ctx.fillStyle = "#f5f1e7";
ctx.font = "800 24px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.fillText(
type === "pinhole" ? "PROJECTED IMAGE" : "SENSOR IMAGE",
320,
36
);
ctx.strokeStyle = "rgba(38,61,69,0.16)";
ctx.lineWidth = 1;
for (let x = 20; x < 640; x += 30) {
ctx.beginPath();
ctx.moveTo(x, 70);
ctx.lineTo(x, 455);
ctx.stroke();
}
for (let y = 70; y < 480; y += 30) {
ctx.beginPath();
ctx.moveTo(20, y);
ctx.lineTo(620, y);
ctx.stroke();
}
drawArrow(
ctx,
320,
238,
1.85,
type === "pinhole" ? "#34797b" : "#346f8b",
true
);
ctx.fillStyle = "#43565b";
ctx.font = "700 20px system-ui, sans-serif";
ctx.fillText("INVERTED IMAGE", 320, 452);
return makeTexture(canvas);
}
function makeTitleSprite(title, subtitle, accent) {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 172;
const ctx = canvas.getContext("2d");
roundRect(ctx, 8, 8, 752, 156, 22);
ctx.fillStyle = "rgba(11,20,29,0.94)";
ctx.fill();
ctx.strokeStyle = "rgba(191,226,230,0.25)";
ctx.lineWidth = 3;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(8, 8, 16, 156);
ctx.textAlign = "left";
ctx.fillStyle = "#edf6f6";
ctx.font = "800 45px system-ui, sans-serif";
ctx.fillText(title, 48, 76);
ctx.fillStyle = "#9fb6bb";
ctx.font = "700 22px system-ui, sans-serif";
ctx.fillText(subtitle, 49, 121);
const texture = makeTexture(canvas);
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.85, 0.86, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeTagSprite(text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const ctx = canvas.getContext("2d");
roundRect(ctx, 4, 4, 504, 88, 17);
ctx.fillStyle = "rgba(10,18,25,0.92)";
ctx.fill();
ctx.fillStyle = accent;
ctx.fillRect(4, 4, 12, 88);
ctx.fillStyle = "#eaf4f5";
ctx.font = "800 28px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 258, 49);
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: makeTexture(canvas),
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(2.0, 0.375, 1);
sprite.renderOrder = 21;
return sprite;
}
function makeMonitor(x, z, texture, accentMaterial, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addBox(group, [1.28, 0.14, 0.72], [0, 0.07, 0], materials.darker);
addBox(group, [0.18, 1.35, 0.18], [0, 0.74, 0], materials.dark);
addBox(group, [3.7, 2.15, 0.18], [0, 2.18, 0], materials.darker);
// Colored corner accents.
addBox(
group,
[0.13, 0.13, 0.08],
[-1.71, 3.12, 0.13],
accentMaterial,
false
);
addBox(
group,
[0.13, 0.13, 0.08],
[1.71, 3.12, 0.13],
accentMaterial,
false
);
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(3.36, 1.82),
new THREE.MeshBasicMaterial({ map: texture, toneMapped: false })
);
screen.position.set(0, 2.18, 0.101);
group.add(screen);
scene.add(group);
return group;
}
function makeVoxelArrow(parent, material, z, scale = 1) {
const group = new THREE.Group();
group.position.z = z;
group.scale.setScalar(scale);
parent.add(group);
addBox(group, [0.15, 0.76, 0.13], [0, 1.15, 0], material);
addBox(group, [0.18, 0.18, 0.13], [0, 1.57, 0], material);
addBox(group, [0.18, 0.18, 0.13], [-0.18, 1.71, 0], material);
addBox(group, [0.18, 0.18, 0.13], [0.18, 1.71, 0], material);
addBox(group, [0.18, 0.18, 0.13], [-0.32, 1.84, 0], material);
addBox(group, [0.18, 0.18, 0.13], [0.32, 1.84, 0], material);
return group;
}
function makeSensorPanel(parent, type, position) {
const group = new THREE.Group();
group.position.set(...position);
parent.add(group);
addBox(group, [2.86, 2.42, 0.16], [0, 0, 0], materials.darker);
addBox(group, [2.62, 2.18, 0.07], [0, 0, 0.11], materials.cream);
const image = new THREE.Mesh(
new THREE.PlaneGeometry(2.47, 1.83),
new THREE.MeshBasicMaterial({
map: makeSensorTexture(type),
toneMapped: false
})
);
image.position.z = 0.151;
group.add(image);
return group;
}
// -------------------------------------------------------------------------
// Pinhole box
// -------------------------------------------------------------------------
const pinhole = new THREE.Group();
pinhole.position.set(-4.7, 0, -0.55);
scene.add(pinhole);
addBox(pinhole, [4.55, 0.26, 3.65], [0, 0.13, 0], materials.darker);
addBox(pinhole, [3.4, 0.18, 2.15], [0, 3.16, -0.28], materials.teal);
addBox(pinhole, [3.4, 0.18, 2.15], [0, 0.35, -0.28], materials.teal);
// Closed left side and open/cutaway right side.
addBox(
pinhole,
[0.18, 2.7, 2.15],
[-1.61, 1.68, -0.28],
materials.teal
);
addBox(
pinhole,
[0.11, 2.7, 2.15],
[1.61, 1.68, -0.28],
materials.cutaway,
false
);
// Front wall around a tiny square aperture.
const apertureWidth = 3.4;
const apertureHeight = 3.0;
const apertureSize = 0.22;
const sideWidth = (apertureWidth - apertureSize) / 2;
const capHeight = (apertureHeight - apertureSize) / 2;
const sideOffset = apertureSize / 2 + sideWidth / 2;
const capOffset = apertureSize / 2 + capHeight / 2;
addBox(
pinhole,
[sideWidth, apertureHeight, 0.18],
[-sideOffset, 1.68, 0.72],
materials.teal
);
addBox(
pinhole,
[sideWidth, apertureHeight, 0.18],
[sideOffset, 1.68, 0.72],
materials.teal
);
addBox(
pinhole,
[apertureWidth, capHeight, 0.18],
[0, 1.68 + capOffset, 0.72],
materials.teal
);
addBox(
pinhole,
[apertureWidth, capHeight, 0.18],
[0, 1.68 - capOffset, 0.72],
materials.teal
);
// Bright aperture rim.
const rim = materials.orange;
addBox(pinhole, [0.34, 0.045, 0.07], [0, 1.815, 0.84], rim, false);
addBox(pinhole, [0.34, 0.045, 0.07], [0, 1.545, 0.84], rim, false);
addBox(pinhole, [0.045, 0.315, 0.07], [-0.148, 1.68, 0.84], rim, false);
addBox(pinhole, [0.045, 0.315, 0.07], [0.148, 1.68, 0.84], rim, false);
makeSensorPanel(pinhole, "pinhole", [0, 1.68, -1.34]);
makeVoxelArrow(pinhole, materials.green, 1.91, 1.0);
// Voxel support under the test object.
addBox(pinhole, [0.62, 0.12, 0.55], [0, 0.67, 1.91], materials.darker);
addBox(pinhole, [0.12, 0.34, 0.12], [0, 0.44, 1.91], materials.darker);
const pinholeRays = new THREE.Group();
pinhole.add(pinholeRays);
// Top of the upright object projects below the optical axis.
addBeam(pinholeRays, [0, 1.91, 1.91], [0, 1.68, 0.79], 0xffb44b);
addBeam(pinholeRays, [0, 1.68, 0.79], [0, 0.88, -1.24], 0xffb44b);
// Bottom of the object projects above the optical axis.
addBeam(pinholeRays, [0, 0.78, 1.91], [0, 1.68, 0.79], 0xffb44b);
addBeam(pinholeRays, [0, 1.68, 0.79], [0, 2.47, -1.24], 0xffb44b);
// Optical axis dashes.
for (let z = -1.25; z < 2.0; z += 0.28) {
addBeam(
pinholeRays,
[0, 1.68, z],
[0, 1.68, z + 0.13],
0xb8c8cb,
0.018,
0.38
);
}
const pinholeTitle = makeTitleSprite(
"PINHOLE BOX",
"ONE APERTURE · STRAIGHT RAYS",
"#ffb44b"
);
pinholeTitle.position.set(0, 3.72, -0.4);
pinhole.add(pinholeTitle);
const apertureTag = makeTagSprite("TINY APERTURE", "#ffb44b");
apertureTag.position.set(-1.82, 2.86, 0.86);
pinhole.add(apertureTag);
// -------------------------------------------------------------------------
// Simplified lens camera with a cutaway side
// -------------------------------------------------------------------------
const lensCamera = new THREE.Group();
lensCamera.position.set(3.25, 0, -0.55);
scene.add(lensCamera);
addBox(lensCamera, [4.35, 0.26, 3.65], [0, 0.13, 0], materials.darker);
addBox(lensCamera, [3.1, 0.18, 2.1], [0, 3.06, -0.2], materials.blue);
addBox(lensCamera, [3.1, 0.18, 2.1], [0, 0.35, -0.2], materials.blue);
// Left wall and right-side cutaway rails.
addBox(
lensCamera,
[0.18, 2.62, 2.1],
[-1.46, 1.68, -0.2],
materials.blue
);
addBox(
lensCamera,
[0.18, 0.3, 2.1],
[1.46, 0.52, -0.2],
materials.blue
);
addBox(
lensCamera,
[0.11, 2.62, 2.1],
[1.46, 1.68, -0.2],
materials.cutaway,
false
);
// Front body panel around a square lens opening.
const cameraWidth = 3.1;
const cameraHeight = 2.8;
const lensOpening = 0.94;
const cameraSideWidth = (cameraWidth - lensOpening) / 2;
const cameraCapHeight = (cameraHeight - lensOpening) / 2;
const cameraSideOffset = lensOpening / 2 + cameraSideWidth / 2;
const cameraCapOffset = lensOpening / 2 + cameraCapHeight / 2;
addBox(
lensCamera,
[cameraSideWidth, cameraHeight, 0.18],
[-cameraSideOffset, 1.68, 0.7],
materials.blue
);
addBox(
lensCamera,
[cameraSideWidth, cameraHeight, 0.18],
[cameraSideOffset, 1.68, 0.7],
materials.blue
);
addBox(
lensCamera,
[cameraWidth, cameraCapHeight, 0.18],
[0, 1.68 + cameraCapOffset, 0.7],
materials.blue
);
addBox(
lensCamera,
[cameraWidth, cameraCapHeight, 0.18],
[0, 1.68 - cameraCapOffset, 0.7],
materials.blue
);
// Stepped square lens barrel.
function addSquareFrame(parent, outer, inner, z, depth, material) {
const side = (outer - inner) / 2;
const offset = inner / 2 + side / 2;
addBox(parent, [side, outer, depth], [-offset, 1.68, z], material);
addBox(parent, [side, outer, depth], [offset, 1.68, z], material);
addBox(parent, [inner, side, depth], [0, 1.68 + offset, z], material);
addBox(parent, [inner, side, depth], [0, 1.68 - offset, z], material);
}
addSquareFrame(lensCamera, 1.26, 0.86, 0.84, 0.18, materials.darker);
addSquareFrame(lensCamera, 1.12, 0.82, 1.08, 0.18, materials.darker);
addBox(
lensCamera,
[0.78, 0.78, 0.07],
[0, 1.68, 1.02],
materials.glass
);
// Tiny lens highlight and center marker.
addBox(
lensCamera,
[0.16, 0.04, 0.025],
[-0.2, 1.86, 1.07],
materials.cyan,
false
);
addBox(
lensCamera,
[0.04, 0.16, 0.025],
[-0.2, 1.86, 1.07],
materials.cyan,
false
);
makeSensorPanel(lensCamera, "lens", [0, 1.68, -1.22]);
makeVoxelArrow(lensCamera, materials.red, 2.2, 1.08);
addBox(lensCamera, [0.66, 0.12, 0.56], [0, 0.75, 2.2], materials.darker);
addBox(lensCamera, [0.12, 0.36, 0.12], [0, 0.48, 2.2], materials.darker);
// Camera-like top hump, viewfinder, and shutter.
addBox(lensCamera, [1.1, 0.36, 0.92], [-0.42, 3.27, -0.2], materials.darker);
addBox(lensCamera, [0.52, 0.18, 0.08], [-0.42, 3.3, 0.29], materials.glass);
addBox(lensCamera, [0.3, 0.09, 0.28], [0.95, 3.2, -0.2], materials.orange);
const lensRays = new THREE.Group();
lensCamera.add(lensRays);
// Rays from an off-axis source meet at an inverted image point.
addBeam(lensRays, [0, 2.04, 2.2], [0, 1.91, 0.98], 0x63dce5);
addBeam(lensRays, [0, 1.91, 0.98], [0, 0.77, -1.13], 0x63dce5);
addBeam(lensRays, [0, 2.04, 2.2], [0, 1.68, 0.98], 0x9cecf0);
addBeam(lensRays, [0, 1.68, 0.98], [0, 0.77, -1.13], 0x9cecf0);
addBeam(lensRays, [0, 2.04, 2.2], [0, 1.45, 0.98], 0x63dce5);
addBeam(lensRays, [0, 1.45, 0.98], [0, 0.77, -1.13], 0x63dce5);
// Lower source point projects above the optical axis.
addBeam(lensRays, [0, 0.84, 2.2], [0, 1.68, 0.98], 0x63dce5);
addBeam(lensRays, [0, 1.68, 0.98], [0, 2.54, -1.13], 0x63dce5);
for (let z = -1.12; z < 2.3; z += 0.29) {
addBeam(
lensRays,
[0, 1.68, z],
[0, 1.68, z + 0.13],
0xb8c8cb,
0.018,
0.38
);
}
const lensTitle = makeTitleSprite(
"LENS CAMERA",
"ONE IDEAL LENS PLANE · BENT RAYS",
"#63dce5"
);
lensTitle.position.set(0, 3.78, -0.35);
lensCamera.add(lensTitle);
const lensTag = makeTagSprite("THIN-LENS PLANE", "#63dce5");
lensTag.position.set(-1.82, 2.83, 0.92);
lensCamera.add(lensTag);
// -------------------------------------------------------------------------
// Comparison screens
// -------------------------------------------------------------------------
makeMonitor(
-1.43,
1.36,
makePinholeDisplayTexture(),
materials.orange,
-0.07
);
makeMonitor(
6.25,
1.36,
makeLensDisplayTexture(),
materials.cyan,
0.07
);
// -------------------------------------------------------------------------
// Custom orbit controls — no Three.js addons
// -------------------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 1.48, 0.05),
radius: innerWidth < 760 ? 25 : 20.5,
theta: 0.27,
phi: 1.045,
targetRadius: innerWidth < 760 ? 25 : 20.5,
targetTheta: 0.27,
targetPhi: 1.045
};
const pointer = {
active: false,
x: 0,
y: 0
};
const clamp = THREE.MathUtils.clamp;
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.x = event.clientX;
pointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
controls.targetTheta -= dx * 0.006;
controls.targetPhi = clamp(controls.targetPhi - dy * 0.005, 0.45, 1.43);
});
function endPointer(event) {
pointer.active = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const factor = Math.exp(event.deltaY * 0.001);
controls.targetRadius = clamp(
controls.targetRadius * factor,
11,
34
);
},
{ passive: false }
);
addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") {
controls.targetTheta += 0.12;
} else if (key === "ArrowRight") {
controls.targetTheta -= 0.12;
} else if (key === "ArrowUp") {
controls.targetPhi = clamp(controls.targetPhi - 0.08, 0.45, 1.43);
} else if (key === "ArrowDown") {
controls.targetPhi = clamp(controls.targetPhi + 0.08, 0.45, 1.43);
} else if (key === "+" || key === "=") {
controls.targetRadius = clamp(controls.targetRadius - 1.2, 11, 34);
} else if (key === "-") {
controls.targetRadius = clamp(controls.targetRadius + 1.2, 11, 34);
} else {
return;
}
event.preventDefault();
});
function resetView() {
controls.targetTheta = 0.27;
controls.targetPhi = 1.045;
controls.targetRadius = innerWidth < 760 ? 25 : 20.5;
}
document
.getElementById("reset-view")
.addEventListener("click", resetView);
const rayButton = document.getElementById("toggle-rays");
let raysVisible = true;
rayButton.addEventListener("click", () => {
raysVisible = !raysVisible;
pinholeRays.visible = raysVisible;
lensRays.visible = raysVisible;
rayButton.textContent = raysVisible ? "Hide ray guides" : "Show ray guides";
rayButton.setAttribute("aria-pressed", String(raysVisible));
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const damping = 0.085;
controls.theta += (controls.targetTheta - controls.theta) * damping;
controls.phi += (controls.targetPhi - controls.phi) * damping;
controls.radius += (controls.targetRadius - controls.radius) * damping;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```