Prompt specification
Build or customize the Three.js 3D scene "Focal-plane exploration tool" 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: Focal-plane exploration tool
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/focal-plane-exploration-tool-x1036
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows optical focus in the eye. Let viewers move an object and adjust a toy lens parameter; display whether ray intersections meet the marked screen and identify the limitations of the simplified optical model. Low-poly.
Working Three.js Starter (focal-plane-exploration-tool-x1036):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Lens Lab — Optical Focus in the Eye</title>
<style>
:root {
--bg: #071018;
--panel: rgba(9, 23, 33, 0.88);
--panel-border: rgba(132, 214, 238, 0.18);
--text: #eaf7fb;
--muted: #95afbc;
--cyan: #69d7ff;
--gold: #f7c95c;
--green: #56f0c2;
--red: #ff6b7a;
}
* {
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;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 16px;
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
header {
position: absolute;
top: 20px;
left: 22px;
max-width: 440px;
}
.eyebrow {
color: var(--cyan);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 6px;
font-size: clamp(1.5rem, 3vw, 2.25rem);
line-height: 1.05;
letter-spacing: -0.035em;
}
header p {
margin: 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.45;
}
#controls {
position: absolute;
top: 140px;
left: 22px;
width: 300px;
padding: 17px;
}
.control-title {
margin-bottom: 17px;
color: var(--muted);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.control {
margin-bottom: 18px;
}
.control:last-of-type {
margin-bottom: 12px;
}
.control label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
font-size: 0.84rem;
font-weight: 700;
}
output {
color: var(--gold);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 5px 0 0;
appearance: none;
border-radius: 99px;
background: linear-gradient(90deg, #24556a, #69d7ff);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 3px solid #0a1822;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 1px rgba(247, 201, 92, 0.6);
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #0a1822;
border-radius: 50%;
background: var(--gold);
}
.button-row {
display: flex;
gap: 8px;
margin-top: 16px;
}
button {
flex: 1;
padding: 9px 10px;
border: 1px solid rgba(105, 215, 255, 0.25);
border-radius: 9px;
background: rgba(105, 215, 255, 0.08);
color: var(--text);
font: inherit;
font-size: 0.78rem;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(105, 215, 255, 0.16);
border-color: rgba(105, 215, 255, 0.45);
}
.toggle {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
color: var(--muted);
font-size: 0.78rem;
}
.toggle input {
accent-color: var(--cyan);
}
#readout {
position: absolute;
top: 22px;
right: 22px;
width: 340px;
padding: 17px;
}
.status-card {
border-left: 4px solid var(--muted);
padding: 2px 0 2px 12px;
}
.status-card.good {
border-color: var(--green);
}
.status-card.warn {
border-color: var(--gold);
}
.status-card.bad {
border-color: var(--red);
}
#verdict {
margin-bottom: 5px;
font-size: 0.98rem;
font-weight: 850;
letter-spacing: 0.025em;
}
#status-detail {
min-height: 40px;
color: var(--muted);
font-size: 0.78rem;
line-height: 1.45;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin-top: 15px;
}
.metric {
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric strong {
font-size: 0.9rem;
font-variant-numeric: tabular-nums;
}
#equation {
margin-top: 10px;
padding: 10px;
border-radius: 9px;
background: rgba(0, 0, 0, 0.18);
color: #c9e8f2;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.72rem;
text-align: center;
}
#limits {
position: absolute;
right: 22px;
bottom: 22px;
width: 420px;
padding: 14px 16px;
color: var(--muted);
font-size: 0.76rem;
line-height: 1.5;
}
#limits summary {
color: var(--text);
font-size: 0.78rem;
font-weight: 800;
cursor: pointer;
}
#limits ul {
margin: 10px 0 2px;
padding-left: 18px;
}
#limits li {
margin: 6px 0;
}
.model-note {
margin-top: 9px;
padding-top: 9px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: #b8ced8;
}
.hint {
position: absolute;
bottom: 22px;
left: 22px;
max-width: 300px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
background: rgba(7, 16, 24, 0.64);
color: var(--muted);
font-size: 0.72rem;
line-height: 1.45;
}
.hint strong {
color: var(--text);
}
.tag {
position: absolute;
display: none;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid rgba(105, 215, 255, 0.25);
border-radius: 6px;
background: rgba(7, 16, 24, 0.78);
color: #dff7ff;
font-size: 0.67rem;
font-weight: 750;
letter-spacing: 0.04em;
white-space: nowrap;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.22);
}
.tag.focus-tag {
border-color: rgba(247, 201, 92, 0.45);
color: var(--gold);
}
@media (max-width: 820px) {
header {
top: 12px;
left: 12px;
max-width: calc(100% - 24px);
}
header p {
display: none;
}
#controls {
top: 82px;
left: 12px;
width: 270px;
padding: 13px;
}
#readout {
top: 82px;
right: 12px;
width: 285px;
padding: 13px;
}
#limits {
right: 12px;
bottom: 12px;
width: min(380px, calc(100% - 24px));
max-height: 170px;
overflow: auto;
}
.hint {
display: none;
}
}
@media (max-width: 590px) {
h1 {
font-size: 1.35rem;
}
#controls {
top: 70px;
width: calc(100% - 24px);
}
#readout {
top: 338px;
left: 12px;
right: 12px;
width: auto;
}
#limits {
max-height: 145px;
}
.metrics {
margin-top: 10px;
}
}
</style>
</head>
<body>
<div class="hud">
<header>
<div class="eyebrow">Interactive Optics · Low-Poly Lab</div>
<h1>Can the eye bring this object into focus?</h1>
<p>
Move the object, change the toy lens, and watch where the sampled rays
converge relative to the marked retinal screen.
</p>
</header>
<section id="controls" class="panel" aria-label="Optical controls">
<div class="control-title">Model controls</div>
<div class="control">
<label for="object-distance">
Object distance
<output id="object-output" for="object-distance">4.50 D.U.</output>
</label>
<input
id="object-distance"
type="range"
min="3.4"
max="7"
step="0.01"
value="4.5"
/>
</div>
<div class="control">
<label for="focal-length">
Toy lens focal length
<output id="focal-output" for="focal-length">1.48 D.U.</output>
</label>
<input
id="focal-length"
type="range"
min="0.9"
max="1.8"
step="0.01"
value="1.48"
/>
</div>
<label class="toggle">
<input id="show-rays" type="checkbox" checked />
Show sampled rays
</label>
<div class="button-row">
<button id="reset-model" type="button">Reset model</button>
<button id="reset-view" type="button">Reset view</button>
</div>
</section>
<section id="readout" class="panel" aria-label="Focus status" aria-live="polite">
<div id="status-card" class="status-card bad">
<div id="verdict">CHECKING RAY INTERSECTIONS…</div>
<div id="status-detail"></div>
</div>
<div class="metrics">
<div class="metric">
<span>Ideal focus</span>
<strong id="focus-value">—</strong>
</div>
<div class="metric">
<span>Screen hits</span>
<strong id="hit-value">—</strong>
</div>
<div class="metric">
<span>Retinal plane</span>
<strong>2.20 D.U.</strong>
</div>
<div class="metric">
<span>Focus offset</span>
<strong id="offset-value">—</strong>
</div>
</div>
<div id="equation"></div>
</section>
<details id="limits" class="panel" open>
<summary>What this simplified model omits</summary>
<ul>
<li>
The real eye combines a powerful refracting cornea, the aqueous humor,
and a variable crystalline lens. Here they are replaced by one thin
lens in air.
</li>
<li>
The calculation is paraxial and monochromatic. Chromatic aberration,
spherical aberration, diffraction, and detailed wave optics are not modeled.
</li>
<li>
Only three rays from one off-axis point are sampled. Retinal curvature,
field of view, neural resolution, and the inverted retinal image are
represented only schematically.
</li>
<li>
The pupil is exaggerated, accommodation is reduced to one adjustable
focal length, and all distances are diagram units rather than anatomically
scaled millimeters.
</li>
</ul>
<div class="model-note">
Model: one thin lens, a fixed retinal plane at 2.20 D.U., and three sampled rays.
</div>
</details>
<div class="hint">
<strong>Direct manipulation:</strong> drag the gold object along the optical
axis. Drag the background to orbit, and use the wheel to zoom.
</div>
<div id="tag-object" class="tag">OBJECT</div>
<div id="tag-lens" class="tag">TOY EYE LENS</div>
<div id="tag-retina" class="tag">MARKED RETINAL SCREEN</div>
<div id="tag-focus" class="tag focus-tag">IDEAL FOCUS</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.FogExp2(0x071018, 0.018);
const camera = new THREE.PerspectiveCamera(
45,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbceeff, 0x18252d, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(-3, 8, 7);
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x42c9ff, 18, 12, 2);
rimLight.position.set(1.5, 1.8, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffbd55, 10, 8, 2);
warmLight.position.set(-4, 0.8, 2);
scene.add(warmLight);
// ------------------------------------------------------------
// Optical constants, expressed in diagram units
// ------------------------------------------------------------
const RETINA_X = 2.2;
const TARGET_Y = -0.12;
const SCREEN_RADIUS = 0.11;
const SOURCE_Y = 0.32;
const APERTURE_HEIGHTS = [-0.38, 0, 0.38];
const objectInput = document.getElementById("object-distance");
const focalInput = document.getElementById("focal-length");
const objectOutput = document.getElementById("object-output");
const focalOutput = document.getElementById("focal-output");
const showRaysInput = document.getElementById("show-rays");
const verdict = document.getElementById("verdict");
const statusDetail = document.getElementById("status-detail");
const statusCard = document.getElementById("status-card");
const focusValue = document.getElementById("focus-value");
const hitValue = document.getElementById("hit-value");
const offsetValue = document.getElementById("offset-value");
const equation = document.getElementById("equation");
// ------------------------------------------------------------
// Grid and optical axis
// ------------------------------------------------------------
const grid = new THREE.GridHelper(18, 18, 0x244957, 0x142a34);
grid.position.y = -1.22;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-7.4, 0, 0),
new THREE.Vector3(4.15, 0, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x527b8d,
dashSize: 0.13,
gapSize: 0.09,
transparent: true,
opacity: 0.5
})
);
axis.computeLineDistances();
scene.add(axis);
const dimensionGeometry = new THREE.BufferGeometry();
const dimensionLine = new THREE.LineSegments(
dimensionGeometry,
new THREE.LineBasicMaterial({
color: 0x78a9bc,
transparent: true,
opacity: 0.55
})
);
scene.add(dimensionLine);
// ------------------------------------------------------------
// Low-poly movable object
// ------------------------------------------------------------
const objectGroup = new THREE.Group();
scene.add(objectGroup);
const objectMaterial = new THREE.MeshStandardMaterial({
color: 0xf7c95c,
emissive: 0x4a2d00,
emissiveIntensity: 0.65,
roughness: 0.56,
metalness: 0.12,
flatShading: true
});
const objectCore = new THREE.Mesh(
new THREE.OctahedronGeometry(0.4, 0),
objectMaterial
);
objectCore.position.y = -0.08;
objectCore.rotation.set(0.35, 0.2, 0.1);
objectCore.scale.set(0.9, 1.12, 0.9);
objectGroup.add(objectCore);
const objectEmitter = new THREE.Mesh(
new THREE.SphereGeometry(0.075, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xfff1a8 })
);
objectEmitter.position.y = SOURCE_Y;
objectGroup.add(objectEmitter);
const objectBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.22, 0.28, 0.12, 6),
new THREE.MeshStandardMaterial({
color: 0x9c6921,
roughness: 0.75,
flatShading: true
})
);
objectBase.position.y = -0.5;
objectGroup.add(objectBase);
const objectPickMeshes = [objectCore, objectEmitter, objectBase];
// ------------------------------------------------------------
// Toy eye lens
// ------------------------------------------------------------
const lensGroup = new THREE.Group();
scene.add(lensGroup);
const lensGlass = new THREE.Mesh(
new THREE.SphereGeometry(0.45, 12, 8),
new THREE.MeshPhongMaterial({
color: 0x83ddff,
transparent: true,
opacity: 0.23,
shininess: 85,
specular: 0xffffff,
depthWrite: false,
flatShading: true
})
);
lensGlass.scale.x = 0.18;
lensGlass.position.x = 0.02;
lensGroup.add(lensGlass);
const lensRing = new THREE.Mesh(
new THREE.TorusGeometry(0.45, 0.045, 6, 20),
new THREE.MeshStandardMaterial({
color: 0x69d7ff,
emissive: 0x0d4053,
emissiveIntensity: 0.9,
roughness: 0.4,
flatShading: true
})
);
lensRing.rotation.y = Math.PI / 2;
lensGroup.add(lensRing);
const pupilRing = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.018, 5, 18),
new THREE.MeshBasicMaterial({
color: 0x244c5e,
transparent: true,
opacity: 0.9
})
);
pupilRing.rotation.y = Math.PI / 2;
pupilRing.position.x = -0.035;
lensGroup.add(pupilRing);
// ------------------------------------------------------------
// Low-poly eye shell, iris, retina, and optic nerve
// ------------------------------------------------------------
const eyeShell = new THREE.Mesh(
new THREE.SphereGeometry(1, 12, 8),
new THREE.MeshPhongMaterial({
color: 0xd9f4ff,
transparent: true,
opacity: 0.11,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true,
shininess: 35
})
);
eyeShell.scale.set(1.25, 0.95, 0.78);
eyeShell.position.x = 1.1;
scene.add(eyeShell);
const eyeWire = new THREE.Mesh(
new THREE.SphereGeometry(1, 12, 8),
new THREE.MeshBasicMaterial({
color: 0x91d7ea,
wireframe: true,
transparent: true,
opacity: 0.075,
depthWrite: false
})
);
eyeWire.scale.copy(eyeShell.scale);
eyeWire.position.copy(eyeShell.position);
scene.add(eyeWire);
const iris = new THREE.Mesh(
new THREE.TorusGeometry(0.32, 0.055, 6, 18),
new THREE.MeshStandardMaterial({
color: 0xe99b52,
emissive: 0x3a1703,
emissiveIntensity: 0.5,
roughness: 0.55,
flatShading: true
})
);
iris.rotation.y = Math.PI / 2;
iris.position.x = -0.12;
scene.add(iris);
const irisDisk = new THREE.Mesh(
new THREE.CircleGeometry(0.3, 18),
new THREE.MeshBasicMaterial({
color: 0x15222b,
transparent: true,
opacity: 0.68,
side: THREE.DoubleSide
})
);
irisDisk.rotation.y = Math.PI / 2;
irisDisk.position.x = -0.13;
scene.add(irisDisk);
const retinaFill = new THREE.Mesh(
new THREE.CircleGeometry(0.69, 24),
new THREE.MeshBasicMaterial({
color: 0x16343a,
transparent: true,
opacity: 0.84,
side: THREE.DoubleSide
})
);
retinaFill.rotation.y = Math.PI / 2;
retinaFill.position.set(RETINA_X, TARGET_Y, 0);
scene.add(retinaFill);
const retinaBorder = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.PlaneGeometry(1.38, 1.38)),
new THREE.LineBasicMaterial({
color: 0x4d8495,
transparent: true,
opacity: 0.75
})
);
retinaBorder.rotation.y = Math.PI / 2;
retinaBorder.position.set(RETINA_X + 0.006, TARGET_Y, 0);
scene.add(retinaBorder);
const targetRing = new THREE.Mesh(
new THREE.RingGeometry(SCREEN_RADIUS, SCREEN_RADIUS + 0.018, 28),
new THREE.MeshBasicMaterial({
color: 0xff6b7a,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.95
})
);
targetRing.rotation.y = Math.PI / 2;
targetRing.position.set(RETINA_X + 0.015, TARGET_Y, 0);
scene.add(targetRing);
const targetCrossMaterial = new THREE.LineBasicMaterial({
color: 0xff9aa5,
transparent: true,
opacity: 0.7
});
const targetCross = new THREE.LineSegments(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(RETINA_X + 0.02, TARGET_Y - 0.18, 0),
new THREE.Vector3(RETINA_X + 0.02, TARGET_Y + 0.18, 0),
new THREE.Vector3(RETINA_X + 0.02, TARGET_Y, -0.18),
new THREE.Vector3(RETINA_X + 0.02, TARGET_Y, 0.18)
]),
targetCrossMaterial
);
scene.add(targetCross);
const nerve = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.18, 0.65, 6),
new THREE.MeshStandardMaterial({
color: 0xc86b7d,
roughness: 0.8,
flatShading: true
})
);
nerve.rotation.z = -Math.PI / 2;
nerve.position.set(2.54, -0.12, 0);
scene.add(nerve);
// ------------------------------------------------------------
// Sampled ray bundles
// ------------------------------------------------------------
const rayColors = [0x69d7ff, 0xf7c95c, 0xb58cff];
const rayObjects = [];
for (let i = 0; i < APERTURE_HEIGHTS.length; i++) {
const material = new THREE.LineBasicMaterial({
color: rayColors[i],
transparent: true,
opacity: 0.92
});
const incoming = new THREE.Line(new THREE.BufferGeometry(), material);
const outgoing = new THREE.Line(new THREE.BufferGeometry(), material);
scene.add(incoming, outgoing);
const hitDot = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xff6b7a })
);
scene.add(hitDot);
const lensPoint = new THREE.Mesh(
new THREE.SphereGeometry(0.025, 8, 6),
new THREE.MeshBasicMaterial({ color: rayColors[i] })
);
scene.add(lensPoint);
rayObjects.push({ incoming, outgoing, hitDot, lensPoint, material });
}
const focusMarker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075, 0),
new THREE.MeshBasicMaterial({ color: 0xf7c95c })
);
scene.add(focusMarker);
const focusHalo = new THREE.Mesh(
new THREE.RingGeometry(0.095, 0.108, 20),
new THREE.MeshBasicMaterial({
color: 0xf7c95c,
transparent: true,
opacity: 0.85,
side: THREE.DoubleSide
})
);
focusHalo.rotation.y = Math.PI / 2;
scene.add(focusHalo);
// ------------------------------------------------------------
// Optical model and UI update
// ------------------------------------------------------------
let objectDistance = Number(objectInput.value);
let focalLength = Number(focalInput.value);
let idealFocus = 2.2;
let idealImageY = TARGET_Y;
function replaceLineGeometry(line, points) {
line.geometry.dispose();
line.geometry = new THREE.BufferGeometry().setFromPoints(points);
}
function updateOptics() {
objectDistance = Number(objectInput.value);
focalLength = Number(focalInput.value);
objectGroup.position.x = -objectDistance;
objectOutput.textContent = `${objectDistance.toFixed(2)} D.U.`;
focalOutput.textContent = `${focalLength.toFixed(2)} D.U.`;
// Thin-lens equation: 1/f = 1/u + 1/v.
idealFocus = (focalLength * objectDistance) / (objectDistance - focalLength);
// Negative magnification gives an inverted image for the off-axis source.
idealImageY = -(idealFocus / objectDistance) * SOURCE_Y;
focusMarker.position.set(idealFocus, idealImageY, 0);
focusHalo.position.set(idealFocus, idealImageY, 0);
const focusOffset = idealFocus - RETINA_X;
let hitCount = 0;
for (let i = 0; i < APERTURE_HEIGHTS.length; i++) {
const h = APERTURE_HEIGHTS[i];
const ray = rayObjects[i];
const sourcePoint = new THREE.Vector3(-objectDistance, SOURCE_Y, 0);
const lensPoint = new THREE.Vector3(0, h, 0);
const focusPoint = new THREE.Vector3(idealFocus, idealImageY, 0);
replaceLineGeometry(ray.incoming, [sourcePoint, lensPoint]);
// Continue the outgoing line beyond the ideal focus when necessary.
const endX = Math.max(idealFocus, RETINA_X) + 0.5;
const slope = (idealImageY - h) / idealFocus;
const endPoint = new THREE.Vector3(endX, h + slope * endX, 0);
replaceLineGeometry(ray.outgoing, [lensPoint, focusPoint, endPoint]);
ray.lensPoint.position.copy(lensPoint);
const intersectionY = h + slope * RETINA_X;
const isHit = Math.abs(intersectionY - TARGET_Y) <= SCREEN_RADIUS;
ray.hitDot.position.set(RETINA_X + 0.025, intersectionY, 0);
ray.hitDot.material.color.setHex(isHit ? 0x56f0c2 : 0xff6b7a);
ray.material.color.setHex(isHit ? rayColors[i] : 0xff6b7a);
ray.material.opacity = isHit ? 0.92 : 0.82;
if (isHit) hitCount++;
}
const allHit = hitCount === APERTURE_HEIGHTS.length;
const closeFocus = Math.abs(focusOffset) <= 0.035;
if (allHit && closeFocus) {
verdict.textContent = "FOCUSED ON THE MARKED SCREEN";
statusDetail.textContent =
"All sampled rays intersect the target. The ideal image plane coincides with the retinal screen.";
statusCard.className = "status-card good";
} else if (allHit) {
verdict.textContent = "RAYS MEET THE SCREEN — NEAR FOCUS";
statusDetail.textContent =
"The intersections are still inside the target, but the ideal image plane is not exactly on the retina.";
statusCard.className = "status-card warn";
} else {
verdict.textContent = "RAYS MISS THE MARKED SCREEN";
statusDetail.textContent =
focusOffset < 0
? "The ideal focus is in front of the retina. The rays have already crossed and spread again by the screen."
: "The ideal focus is behind the retina. The rays have not converged enough when they reach the screen.";
statusCard.className = "status-card bad";
}
focusValue.textContent = `${idealFocus.toFixed(2)} D.U.`;
hitValue.textContent = `${hitCount} / ${APERTURE_HEIGHTS.length}`;
offsetValue.textContent =
`${focusOffset >= 0 ? "+" : ""}${focusOffset.toFixed(2)} D.U.`;
equation.textContent =
`1/f = 1/u + 1/v · ` +
`1/${focalLength.toFixed(2)} = 1/${objectDistance.toFixed(2)} + 1/${idealFocus.toFixed(2)}`;
targetRing.material.color.setHex(allHit ? 0x56f0c2 : 0xff6b7a);
targetCrossMaterial.color.setHex(allHit ? 0x73ffd4 : 0xff9aa5);
retinaFill.material.color.setHex(allHit ? 0x143c35 : 0x40252c);
focusMarker.material.color.setHex(closeFocus ? 0x56f0c2 : 0xf7c95c);
focusHalo.material.color.setHex(closeFocus ? 0x56f0c2 : 0xf7c95c);
// Object-to-lens dimension bracket.
const x0 = -objectDistance;
const y = -0.78;
const points = [
new THREE.Vector3(x0, y, 0),
new THREE.Vector3(0, y, 0),
new THREE.Vector3(x0, y - 0.09, 0),
new THREE.Vector3(x0, y + 0.09, 0),
new THREE.Vector3(0, y - 0.09, 0),
new THREE.Vector3(0, y + 0.09, 0)
];
dimensionGeometry.dispose();
dimensionGeometry.setFromPoints(points);
}
function setRaysVisible(visible) {
for (const ray of rayObjects) {
ray.incoming.visible = visible;
ray.outgoing.visible = visible;
ray.hitDot.visible = visible;
ray.lensPoint.visible = visible;
}
focusMarker.visible = visible;
focusHalo.visible = visible;
}
objectInput.addEventListener("input", updateOptics);
focalInput.addEventListener("input", updateOptics);
showRaysInput.addEventListener("change", () => {
setRaysVisible(showRaysInput.checked);
});
document.getElementById("reset-model").addEventListener("click", () => {
objectInput.value = "4.5";
focalInput.value = "1.48";
showRaysInput.checked = true;
setRaysVisible(true);
updateOptics();
});
// ------------------------------------------------------------
// Custom camera controls: orbit, zoom, and object dragging
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-1.8, 0, 0);
let cameraYaw = 0.46;
let cameraPitch = 0.38;
let cameraRadius = 11.8;
function updateCamera() {
const horizontal = cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.sin(cameraPitch),
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetView() {
cameraYaw = 0.46;
cameraPitch = 0.38;
cameraRadius = 11.8;
cameraTarget.set(-1.8, 0, 0);
updateCamera();
}
document.getElementById("reset-view").addEventListener("click", resetView);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
const planeIntersection = new THREE.Vector3();
let pointerMode = null;
let previousX = 0;
let previousY = 0;
let dragOffset = 0;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const objectHits = raycaster.intersectObjects(objectPickMeshes, false);
previousX = event.clientX;
previousY = event.clientY;
if (objectHits.length > 0 && raycaster.ray.intersectPlane(dragPlane, planeIntersection)) {
pointerMode = "object";
dragOffset = objectGroup.position.x - planeIntersection.x;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
} else {
pointerMode = "orbit";
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerMode === "object") {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planeIntersection)) {
const desiredX = planeIntersection.x + dragOffset;
const clampedDistance = THREE.MathUtils.clamp(-desiredX, 3.4, 7);
objectInput.value = clampedDistance.toFixed(2);
updateOptics();
}
} else if (pointerMode === "orbit") {
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(cameraPitch + dy * 0.005, 0.08, 1.05);
updateCamera();
}
previousX = event.clientX;
previousY = event.clientY;
});
function endPointer(event) {
pointerMode = null;
renderer.domElement.style.cursor = "grab";
if (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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
7.0,
18.0
);
updateCamera();
}, { passive: false });
renderer.domElement.style.cursor = "grab";
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const tags = {
object: document.getElementById("tag-object"),
lens: document.getElementById("tag-lens"),
retina: document.getElementById("tag-retina"),
focus: document.getElementById("tag-focus")
};
const projected = new THREE.Vector3();
function positionTag(element, worldPosition, verticalOffset = 0) {
projected.copy(worldPosition);
projected.y += verticalOffset;
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (visible) {
element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
function updateTags() {
positionTag(
tags.object,
new THREE.Vector3(objectGroup.position.x, -0.02, 0),
0.78
);
positionTag(
tags.lens,
new THREE.Vector3(0, 0, 0),
0.78
);
positionTag(
tags.retina,
new THREE.Vector3(RETINA_X, TARGET_Y, 0),
0.92
);
positionTag(
tags.focus,
new THREE.Vector3(idealFocus, idealImageY, 0),
0.27
);
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", onResize);
updateOptics();
updateCamera();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
objectCore.rotation.y = elapsed * 0.35;
objectCore.rotation.z = 0.1 + Math.sin(elapsed * 0.8) * 0.05;
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.12;
focusMarker.scale.setScalar(pulse);
focusHalo.scale.setScalar(1 + Math.sin(elapsed * 2.4) * 0.08);
if (showRaysInput.checked) {
focusHalo.quaternion.copy(camera.quaternion);
}
updateTags();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```