Prompt specification
Build or customize the Three.js 3D scene "Microscope optical path cutaway" 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: Microscope optical path cutaway
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/microscope-optical-path-cutaway-x1561
Scene Description Prompt:
Build a low-poly microscope cutaway tracing rays through objective, intermediate image, and eyepiece. Distinguish specimen from image. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (microscope-optical-path-cutaway-x1561):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Compound Microscope — Ray Trace</title>
<style>
:root {
--ink: #17272d;
--muted: #5d6b70;
--paper: #f5f1e8;
--panel: rgba(255, 253, 248, 0.9);
--line: rgba(28, 48, 54, 0.13);
--amber: #f3a343;
--cyan: #43c9dc;
--violet: #8e7cf0;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, #fffefb 0%, #f5f1e8 50%, #e8e3d9 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body::after {
content: "";
position: fixed;
inset: 0;
pointer-events: none;
box-shadow: inset 0 0 140px rgba(64, 75, 75, 0.08);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.title-card {
position: fixed;
z-index: 5;
top: 22px;
left: 24px;
width: min(420px, calc(100vw - 48px));
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
padding: 6px 10px;
border: 1px solid rgba(25, 53, 61, 0.15);
border-radius: 999px;
background: rgba(255, 255, 255, 0.7);
color: #426069;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.eyebrow::before {
content: "";
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(67, 201, 220, 0.13);
}
h1 {
margin: 0;
font-size: clamp(28px, 3.4vw, 46px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 790;
}
.subtitle {
max-width: 350px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 8;
top: 22px;
right: 22px;
width: 292px;
padding: 18px;
border: 1px solid rgba(30, 48, 54, 0.12);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(42, 51, 48, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(16px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.panel h2 {
margin: 0;
font-size: 13px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.not-scale {
padding: 4px 7px;
border-radius: 6px;
background: #ebe6dc;
color: #6c6a62;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.chain {
display: grid;
gap: 3px;
margin-bottom: 15px;
}
.chain-row {
position: relative;
display: grid;
grid-template-columns: 29px 1fr;
gap: 10px;
padding: 9px 0;
}
.chain-row:not(:last-child)::after {
content: "";
position: absolute;
top: 35px;
left: 13px;
width: 1px;
height: 19px;
background: #cfd5d2;
}
.step {
display: grid;
place-items: center;
width: 27px;
height: 27px;
border-radius: 8px;
color: white;
font-size: 10px;
font-weight: 800;
box-shadow: inset 0 -2px 0 rgba(0,0,0,0.12);
}
.step.amber { background: var(--amber); }
.step.cyan { background: var(--cyan); }
.step.violet { background: var(--violet); }
.chain-row strong {
display: block;
margin: 0 0 3px;
font-size: 12px;
}
.chain-row small {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin: 0 0 15px;
padding: 12px;
border-radius: 12px;
background: rgba(231, 229, 220, 0.48);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #4a5a60;
font-size: 10px;
font-weight: 700;
}
.legend-item i {
flex: 0 0 auto;
width: 9px;
height: 9px;
border-radius: 3px;
}
.controls {
display: grid;
gap: 11px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
color: #46575d;
font-size: 11px;
font-weight: 650;
}
.control-row input[type="checkbox"] {
width: 16px;
height: 16px;
accent-color: #315e64;
cursor: pointer;
}
.range-wrap {
display: grid;
grid-template-columns: 1fr 34px;
align-items: center;
gap: 8px;
color: #46575d;
font-size: 11px;
font-weight: 650;
}
.range-wrap input {
width: 100%;
accent-color: #315e64;
cursor: pointer;
}
.range-value {
color: #768287;
text-align: right;
font-size: 10px;
}
.reset {
width: 100%;
margin-top: 1px;
padding: 9px 12px;
border: 1px solid rgba(34, 67, 73, 0.17);
border-radius: 9px;
background: #315e64;
color: white;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform .15s ease, background .15s ease;
}
.reset:hover {
transform: translateY(-1px);
background: #244d53;
}
.scene-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 8px;
padding: 7px 10px 7px 8px;
border: 1px solid rgba(27, 47, 52, 0.12);
border-radius: 10px;
background: rgba(255, 253, 248, 0.88);
box-shadow: 0 7px 20px rgba(45, 55, 52, 0.1);
pointer-events: none;
white-space: nowrap;
backdrop-filter: blur(9px);
transition: opacity .2s ease;
}
.scene-label .dot {
width: 9px;
height: 9px;
border: 2px solid white;
border-radius: 50%;
box-shadow: 0 0 0 1px rgba(0,0,0,0.08);
}
.scene-label strong {
display: block;
font-size: 10px;
line-height: 1.1;
}
.scene-label small {
display: block;
margin-top: 3px;
color: #68777b;
font-size: 8px;
line-height: 1;
}
.scene-label.amber .dot { background: var(--amber); }
.scene-label.cyan .dot { background: var(--cyan); }
.scene-label.violet .dot { background: var(--violet); }
.scene-label.neutral .dot { background: #71838a; }
.hint {
position: fixed;
z-index: 5;
bottom: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 11px;
border: 1px solid rgba(30, 48, 54, 0.12);
border-radius: 999px;
background: rgba(255, 253, 248, 0.82);
color: #58686d;
font-size: 10px;
font-weight: 650;
pointer-events: none;
backdrop-filter: blur(10px);
}
.hint span {
display: inline-grid;
place-items: center;
width: 18px;
height: 18px;
border-radius: 50%;
background: #e5e2da;
color: #3f565c;
font-size: 11px;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #f5f1e8;
color: #385b61;
font-size: 12px;
font-weight: 800;
letter-spacing: .12em;
text-transform: uppercase;
transition: opacity .4s ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.title-card {
top: 15px;
left: 15px;
width: calc(100vw - 30px);
}
h1 {
max-width: 290px;
font-size: 30px;
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
border-radius: 14px;
}
.chain {
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 9px;
}
.chain-row {
display: block;
padding: 0;
}
.chain-row::after { display: none; }
.chain-row .step {
width: 23px;
height: 23px;
margin-bottom: 5px;
}
.chain-row small { display: none; }
.chain-row strong { font-size: 10px; }
.legend {
margin-bottom: 9px;
padding: 8px;
}
.controls {
grid-template-columns: 1fr 1fr;
gap: 8px 14px;
padding-top: 9px;
}
.reset { grid-column: 1 / -1; }
.hint { display: none; }
.scene-label { transform: scale(.86); }
.scene-label.optional { display: none !important; }
}
</style>
</head>
<body>
<div id="loading">Building optical bench…</div>
<header class="title-card">
<div class="eyebrow">Interactive optics · low-poly study</div>
<h1>Inside a compound<br />microscope</h1>
<p class="subtitle">
Follow one illuminated specimen point through the objective. The objective
creates a real, inverted intermediate image; the eyepiece turns it into
a virtual image at infinity.
</p>
</header>
<aside class="panel">
<div class="panel-heading">
<h2>Optical chain</h2>
<span class="not-scale">Not to scale</span>
</div>
<div class="chain">
<div class="chain-row">
<div class="step amber">1</div>
<div>
<strong>Specimen</strong>
<small>Physical object resting on the slide</small>
</div>
</div>
<div class="chain-row">
<div class="step cyan">2</div>
<div>
<strong>Objective lens</strong>
<small>Forms a real, inverted intermediate image</small>
</div>
</div>
<div class="chain-row">
<div class="step violet">3</div>
<div>
<strong>Eyepiece</strong>
<small>Produces a magnified virtual image at infinity</small>
</div>
</div>
</div>
<div class="legend">
<div class="legend-item">
<i style="background:var(--amber)"></i> Real object
</div>
<div class="legend-item">
<i style="background:var(--cyan)"></i> Real image
</div>
<div class="legend-item">
<i style="background:var(--violet)"></i> Virtual image
</div>
<div class="legend-item">
<i style="background:#71838a"></i> Optical housing
</div>
</div>
<div class="controls">
<label class="control-row" for="rayToggle">
<span>Ray trace</span>
<input id="rayToggle" type="checkbox" checked />
</label>
<label class="control-row" for="labelToggle">
<span>Scene labels</span>
<input id="labelToggle" type="checkbox" checked />
</label>
<label class="range-wrap" for="shellOpacity">
<span>Housing opacity</span>
<input id="shellOpacity" type="range" min="12" max="100" value="72" />
<span class="range-value" id="opacityValue">72%</span>
</label>
<button class="reset" id="resetView" type="button">Reset camera</button>
</div>
</aside>
<div class="scene-label amber" id="labelSpecimen">
<span class="dot"></span>
<div><strong>Specimen</strong><small>physical object · amber</small></div>
</div>
<div class="scene-label neutral" id="labelObjective">
<span class="dot"></span>
<div><strong>Objective</strong><small>converging lens</small></div>
</div>
<div class="scene-label cyan" id="labelImage">
<span class="dot"></span>
<div><strong>Intermediate image</strong><small>real · inverted · cyan</small></div>
</div>
<div class="scene-label violet" id="labelEyepiece">
<span class="dot"></span>
<div><strong>Eyepiece</strong><small>virtual image · at infinity</small></div>
</div>
<div class="scene-label neutral optional" id="labelEye">
<span class="dot"></span>
<div><strong>Observer</strong><small>parallel output rays</small></div>
</div>
<div class="scene-label amber optional" id="labelLamp">
<span class="dot"></span>
<div><strong>Illuminator</strong><small>light enters from below</small></div>
</div>
<div class="hint">
<span>↔</span> Drag to orbit · scroll to zoom · double-click to reset
</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();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xf5f1e8, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
azimuth: 0.58,
polar: 1.20,
radius: 9.4,
target: new THREE.Vector3(0, 2.72, 0),
dragging: false,
pointerX: 0,
pointerY: 0
};
function updateCamera() {
const sinPolar = Math.sin(controls.polar);
camera.position.set(
controls.target.x + controls.radius * sinPolar * Math.sin(controls.azimuth),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z + controls.radius * sinPolar * Math.cos(controls.azimuth)
);
camera.lookAt(controls.target);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xffffff, 0x53616a, 2.1));
const keyLight = new THREE.DirectionalLight(0xfff2d9, 3.0);
keyLight.position.set(5, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xbfe8ff, 1.1);
fillLight.position.set(-5, 4, 2);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd49c, 0.8);
rimLight.position.set(1, 4, -6);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials and helpers
// ------------------------------------------------------------
const microscope = new THREE.Group();
scene.add(microscope);
const rayGroup = new THREE.Group();
microscope.add(rayGroup);
const shellMaterials = [];
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.12,
flatShading: true,
...options
});
}
function shellMaterial(color, opacity = 0.72) {
const material = standard(color, {
transparent: true,
opacity,
depthWrite: opacity > 0.5,
side: THREE.DoubleSide
});
shellMaterials.push(material);
return material;
}
function addMesh(geometry, material, position, parent = microscope) {
const mesh = new THREE.Mesh(geometry, material);
if (position) mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(a, b, radius, material, parent = microscope, sides = 8) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = addMesh(
new THREE.CylinderGeometry(radius, radius, length, sides),
material,
null,
parent
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
function horizontalRing(radius, tube, y, material, parent = microscope) {
const ring = addMesh(
new THREE.TorusGeometry(radius, tube, 6, 24),
material,
[0, y, 0],
parent
);
ring.rotation.x = Math.PI / 2;
return ring;
}
function arrowBetween(a, b, color, radius = 0.034, parent = rayGroup) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const arrow = addMesh(
new THREE.ConeGeometry(radius, Math.min(0.13, length * 0.22), 6),
new THREE.MeshBasicMaterial({ color, toneMapped: false }),
null,
parent
);
arrow.position.copy(start).lerp(end, 0.60);
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
arrow.castShadow = false;
arrow.receiveShadow = false;
return arrow;
}
function raySegment(a, b, color, radius = 0.011) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const glowMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.16,
depthWrite: false,
toneMapped: false
});
const core = addMesh(
new THREE.CylinderGeometry(radius, radius, length, 6),
new THREE.MeshBasicMaterial({ color, toneMapped: false }),
null,
rayGroup
);
core.position.copy(start).add(end).multiplyScalar(0.5);
core.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
core.castShadow = false;
core.receiveShadow = false;
const glow = addMesh(
new THREE.CylinderGeometry(radius * 2.4, radius * 2.4, length, 6),
glowMaterial,
null,
rayGroup
);
glow.position.copy(core.position);
glow.quaternion.copy(core.quaternion);
glow.castShadow = false;
glow.receiveShadow = false;
}
const materials = {
base: standard(0x263640, { roughness: 0.64, metalness: 0.22 }),
baseTop: standard(0x35505a),
body: shellMaterial(0x315b60),
bodyEdge: standard(0x48777a, { roughness: 0.6, metalness: 0.22 }),
dark: standard(0x1e2c33, { roughness: 0.58, metalness: 0.24 }),
stage: standard(0x3b4b50),
brass: shellMaterial(0xd2a04a, 0.78),
brassRim: standard(0xe1b864, { roughness: 0.45, metalness: 0.48 }),
specimen: standard(0xf29b3e, { roughness: 0.66 }),
specimenCore: standard(0xb85f25, { roughness: 0.72 }),
glass: standard(0xd8f4f3, {
transparent: true,
opacity: 0.28,
depthWrite: false,
roughness: 0.16,
metalness: 0.05
}),
lens: new THREE.MeshStandardMaterial({
color: 0x70dce6,
transparent: true,
opacity: 0.42,
roughness: 0.12,
metalness: 0.08,
depthWrite: false,
side: THREE.DoubleSide
}),
warmLens: new THREE.MeshStandardMaterial({
color: 0xffca74,
emissive: 0xff9b35,
emissiveIntensity: 1.4,
transparent: true,
opacity: 0.8,
roughness: 0.28
})
};
// ------------------------------------------------------------
// Ground and base
// ------------------------------------------------------------
const ground = addMesh(
new THREE.CircleGeometry(5.2, 48),
standard(0xe8e3d9, { roughness: 1, metalness: 0 }),
[0, 0.015, 0],
scene
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
const base = addMesh(
new THREE.CylinderGeometry(1.55, 1.72, 0.34, 12),
materials.base,
[0, 0.29, 0]
);
base.scale.z = 0.72;
const baseTop = addMesh(
new THREE.CylinderGeometry(1.18, 1.43, 0.18, 12),
materials.baseTop,
[0, 0.53, 0]
);
baseTop.scale.z = 0.72;
const baseInset = addMesh(
new THREE.CylinderGeometry(0.62, 0.72, 0.10, 12),
materials.dark,
[0, 0.66, 0]
);
baseInset.scale.z = 0.72;
// ------------------------------------------------------------
// Faceted rear arm
// ------------------------------------------------------------
beamBetween(
[0.04, 0.66, -0.43],
[-0.40, 1.72, -0.57],
0.33,
materials.bodyEdge
);
beamBetween(
[-0.40, 1.72, -0.57],
[-0.62, 4.10, -0.57],
0.29,
materials.body
);
beamBetween(
[-0.62, 4.10, -0.57],
[0, 4.10, -0.10],
0.24,
materials.bodyEdge
);
beamBetween(
[-0.42, 1.74, -0.57],
[-0.18, 1.95, -0.18],
0.25,
materials.body
);
// Focus knob on the rear arm.
const focusKnob = addMesh(
new THREE.CylinderGeometry(0.30, 0.30, 0.17, 12),
materials.dark,
[-0.68, 1.83, -0.53]
);
focusKnob.rotation.z = Math.PI / 2;
// ------------------------------------------------------------
// Stage, slide, specimen, and illuminator
// ------------------------------------------------------------
const stage = addMesh(
new THREE.BoxGeometry(1.72, 0.14, 1.04),
materials.stage,
[0, 1.97, 0]
);
const stageEdge = addMesh(
new THREE.BoxGeometry(1.80, 0.045, 1.10),
materials.dark,
[0, 1.89, 0]
);
const slide = addMesh(
new THREE.BoxGeometry(1.02, 0.026, 0.60),
materials.glass,
[0, 2.065, 0]
);
const slideEdge = addMesh(
new THREE.BoxGeometry(1.06, 0.012, 0.64),
new THREE.MeshBasicMaterial({
color: 0xc8e5e5,
transparent: true,
opacity: 0.22,
depthWrite: false
}),
[0, 2.052, 0]
);
const illuminator = addMesh(
new THREE.CylinderGeometry(0.29, 0.35, 0.34, 12),
materials.dark,
[0, 1.67, 0]
);
addMesh(
new THREE.CylinderGeometry(0.23, 0.23, 0.025, 24),
materials.warmLens,
[0, 1.855, 0]
);
const lampLight = new THREE.PointLight(0xffa847, 3.2, 2.2, 2);
lampLight.position.set(0, 1.84, 0);
scene.add(lampLight);
// Low-poly specimen cells. The selected cell is at x = -0.34.
const specimenPositions = [-0.34, 0, 0.34];
specimenPositions.forEach((x, index) => {
const cell = addMesh(
new THREE.IcosahedronGeometry(0.16, 1),
materials.specimen,
[x, 2.15, 0]
);
cell.scale.set(1.13, 0.32, 0.82);
cell.rotation.y = index * 0.7;
const nucleusX = index === 0 ? x - 0.07 : x + 0.025;
const nucleus = addMesh(
new THREE.IcosahedronGeometry(0.044, 0),
materials.specimenCore,
[nucleusX, 2.205, 0.015]
);
nucleus.scale.y = 0.48;
});
// ------------------------------------------------------------
// Objective and cutaway tube
// ------------------------------------------------------------
// A 270-degree cylinder leaves the camera-facing side open.
const cutawayStart = Math.PI / 2;
const cutawayLength = Math.PI * 1.5;
const objectiveShell = addMesh(
new THREE.CylinderGeometry(
0.46, 0.60, 0.64, 16, 1, true,
cutawayStart, cutawayLength
),
materials.brass,
[0, 2.61, 0]
);
horizontalRing(0.60, 0.055, 2.29, materials.brassRim);
horizontalRing(0.46, 0.045, 2.93, materials.brassRim);
horizontalRing(0.49, 0.055, 2.25, materials.dark);
addMesh(
new THREE.CylinderGeometry(0.38, 0.38, 0.035, 32),
materials.lens,
[0, 2.78, 0]
);
horizontalRing(0.39, 0.022, 2.78, materials.brassRim);
const tubeShell = addMesh(
new THREE.CylinderGeometry(
0.57, 0.57, 1.32, 16, 1, true,
cutawayStart, cutawayLength
),
materials.body,
[0, 3.72, 0]
);
horizontalRing(0.57, 0.045, 3.06, materials.bodyEdge);
horizontalRing(0.57, 0.045, 4.38, materials.bodyEdge);
// Small reinforcing strips along the cut edges.
const stripGeometry = new THREE.BoxGeometry(0.055, 1.30, 0.055);
addMesh(stripGeometry, materials.bodyEdge, [0.57, 3.72, 0]);
addMesh(stripGeometry, materials.bodyEdge, [-0.57, 3.72, 0]);
// ------------------------------------------------------------
// Eyepiece
// ------------------------------------------------------------
const eyepieceShell = addMesh(
new THREE.CylinderGeometry(
0.34, 0.43, 0.54, 14, 1, true,
cutawayStart, cutawayLength
),
materials.brass,
[0, 4.65, 0]
);
horizontalRing(0.43, 0.05, 4.38, materials.brassRim);
horizontalRing(0.35, 0.055, 4.92, materials.dark);
addMesh(
new THREE.CylinderGeometry(0.275, 0.275, 0.035, 32),
materials.lens,
[0, 4.45, 0]
);
horizontalRing(0.28, 0.02, 4.45, materials.brassRim);
// A stylized observer above the eyepiece.
const eyeX = -0.36;
horizontalRing(0.25, 0.042, 5.40, materials.dark);
const pupil = addMesh(
new THREE.CircleGeometry(0.09, 20),
standard(0x2f6f7d, { roughness: 0.42 }),
[eyeX, 5.405, 0]
);
pupil.rotation.x = -Math.PI / 2;
const pupilCore = addMesh(
new THREE.CircleGeometry(0.035, 16),
new THREE.MeshBasicMaterial({ color: 0x111b20 }),
[eyeX, 5.411, 0]
);
pupilCore.rotation.x = -Math.PI / 2;
// ------------------------------------------------------------
// Intermediate image
// ------------------------------------------------------------
const imagePlane = addMesh(
new THREE.BoxGeometry(1.25, 0.009, 0.55),
new THREE.MeshBasicMaterial({
color: 0x57d9e7,
transparent: true,
opacity: 0.07,
depthWrite: false
}),
[0, 3.55, 0]
);
imagePlane.castShadow = false;
// Image positions are laterally reversed relative to the specimen.
const imagePositions = [0.42, 0, -0.42];
imagePositions.forEach((x, index) => {
const cell = addMesh(
new THREE.CircleGeometry(0.12, 16),
new THREE.MeshBasicMaterial({
color: 0x5be0eb,
transparent: true,
opacity: 0.56,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
}),
[x, 3.555, 0]
);
cell.rotation.x = -Math.PI / 2;
cell.scale.x = 1.12;
cell.castShadow = false;
const nucleusX = index === 0 ? x + 0.065 : x - 0.018;
const nucleus = addMesh(
new THREE.CircleGeometry(0.034, 12),
new THREE.MeshBasicMaterial({
color: 0xb9f8ff,
transparent: true,
opacity: 0.78,
depthWrite: false,
side: THREE.DoubleSide,
toneMapped: false
}),
[nucleusX, 3.562, 0]
);
nucleus.rotation.x = -Math.PI / 2;
nucleus.castShadow = false;
});
// ------------------------------------------------------------
// Optical axis
// ------------------------------------------------------------
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 2.05, 0),
new THREE.Vector3(0, 5.35, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x77898e,
dashSize: 0.07,
gapSize: 0.055,
transparent: true,
opacity: 0.45
})
);
axis.computeLineDistances();
microscope.add(axis);
// ------------------------------------------------------------
// Ray trace
//
// Thin-lens construction:
// objective:
// object distance s = yObjective - ySpecimen
// image distance s' = yIntermediate - yObjective
// f = s*s'/(s+s')
// magnification = -s'/s
//
// eyepiece:
// intermediate image lies at its front focal plane,
// so each outgoing bundle is parallel.
// ------------------------------------------------------------
const specimenY = 2.15;
const objectiveY = 2.78;
const intermediateY = 3.55;
const eyepieceY = 4.45;
const eyeY = 5.34;
const objectiveObjectDistance = objectiveY - specimenY;
const objectiveImageDistance = intermediateY - objectiveY;
const objectiveFocalLength =
objectiveObjectDistance * objectiveImageDistance /
(objectiveObjectDistance + objectiveImageDistance);
const objectiveMagnification =
-objectiveImageDistance / objectiveObjectDistance;
const specimenPointX = -0.34;
const intermediatePointX = specimenPointX * objectiveMagnification;
const specimenPoint = [specimenPointX, specimenY, 0];
const intermediatePoint = [intermediatePointX, intermediateY, 0];
const objectiveHits = [-0.25, -0.05, 0.14];
const eyepieceHits = [-0.15, 0.05, 0.25];
const amberRay = 0xffbd62;
const cyanRay = 0x57dfeb;
const violetRay = 0xb3a0ff;
objectiveHits.forEach((x, index) => {
const objectiveHit = [x, objectiveY, 0];
// Incident light: physical specimen to objective.
raySegment(specimenPoint, objectiveHit, amberRay);
arrowBetween(specimenPoint, objectiveHit, amberRay);
// Objective-refracted light converges at the real image.
raySegment(objectiveHit, intermediatePoint, cyanRay);
arrowBetween(objectiveHit, intermediatePoint, cyanRay);
// Diverging rays leave the intermediate image and enter the eyepiece.
const eyepieceHit = [eyepieceHits[index], eyepieceY, 0];
raySegment(intermediatePoint, eyepieceHit, cyanRay);
// The eyepiece converts the diverging bundle into parallel rays.
const eyepieceFocalLength = eyepieceY - intermediateY;
const outputSlope = -intermediatePointX / eyepieceFocalLength;
const outputY = eyeY - eyepieceY;
const outputX = eyepieceHit[0] + outputSlope * outputY;
const outputEnd = [outputX, eyeY, 0];
raySegment(eyepieceHit, outputEnd, violetRay);
arrowBetween(eyepieceHit, outputEnd, violetRay);
// Small lens-hit markers.
const objectiveMarker = addMesh(
new THREE.SphereGeometry(0.019, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xfff7df }),
objectiveHit,
rayGroup
);
objectiveMarker.castShadow = false;
const eyepieceMarker = addMesh(
new THREE.SphereGeometry(0.018, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xd8d0ff }),
eyepieceHit,
rayGroup
);
eyepieceMarker.castShadow = false;
});
const imageCrossing = addMesh(
new THREE.SphereGeometry(0.027, 8, 6),
new THREE.MeshBasicMaterial({ color: 0xb8f8ff }),
intermediatePoint,
rayGroup
);
imageCrossing.castShadow = false;
// ------------------------------------------------------------
// Labels projected from world space
// ------------------------------------------------------------
const labels = [
{
element: document.getElementById("labelSpecimen"),
position: new THREE.Vector3(-0.38, 2.19, 0),
offset: [-108, 25]
},
{
element: document.getElementById("labelObjective"),
position: new THREE.Vector3(0.39, 2.78, 0),
offset: [92, 4]
},
{
element: document.getElementById("labelImage"),
position: new THREE.Vector3(0.42, 3.56, 0),
offset: [98, -5]
},
{
element: document.getElementById("labelEyepiece"),
position: new THREE.Vector3(0.28, 4.61, 0),
offset: [96, -9]
},
{
element: document.getElementById("labelEye"),
position: new THREE.Vector3(eyeX, 5.43, 0),
offset: [-25, -34]
},
{
element: document.getElementById("labelLamp"),
position: new THREE.Vector3(-0.22, 1.76, 0),
offset: [-90, 17]
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labels.forEach(label => {
projected.copy(label.position).project(camera);
const x = (projected.x * 0.5 + 0.5) * width + label.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + label.offset[1];
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -150 &&
x < width + 150 &&
y > -80 &&
y < height + 80;
label.element.style.opacity = visible ? "1" : "0";
label.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
});
}
// ------------------------------------------------------------
// Direct camera controls: no add-ons
// ------------------------------------------------------------
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
controls.dragging = true;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!controls.dragging) return;
const dx = event.clientX - controls.pointerX;
const dy = event.clientY - controls.pointerY;
controls.pointerX = event.clientX;
controls.pointerY = event.clientY;
controls.azimuth -= dx * 0.007;
controls.polar = THREE.MathUtils.clamp(
controls.polar + dy * 0.006,
0.63,
1.45
);
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
6.0,
14.0
);
updateCamera();
}, { passive: false });
function resetCamera() {
controls.azimuth = 0.58;
controls.polar = 1.20;
controls.radius = 9.4;
controls.target.set(0, 2.72, 0);
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetView").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// UI controls
// ------------------------------------------------------------
const rayToggle = document.getElementById("rayToggle");
const labelToggle = document.getElementById("labelToggle");
const shellOpacity = document.getElementById("shellOpacity");
const opacityValue = document.getElementById("opacityValue");
rayToggle.addEventListener("change", () => {
rayGroup.visible = rayToggle.checked;
});
labelToggle.addEventListener("change", () => {
labels.forEach(label => {
label.element.style.visibility = labelToggle.checked ? "visible" : "hidden";
});
});
function applyOpacity(value) {
shellMaterials.forEach(material => {
material.opacity = value / 100;
material.depthWrite = value > 55;
material.needsUpdate = true;
});
opacityValue.textContent = `${value}%`;
}
shellOpacity.addEventListener("input", () => {
applyOpacity(Number(shellOpacity.value));
});
applyOpacity(Number(shellOpacity.value));
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
updateLabels();
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```