Prompt specification
Build or customize the Three.js 3D scene "Occluder With Umbra and Penumbra Cones" 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: Occluder With Umbra and Penumbra Cones
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/occluder-with-umbra-and-penumbra-cones-x3773
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring umbra and penumbra geometry. Model an extended light source, opaque sphere, and receiving plane with schematic shadow boundaries. Distinguish full and partial occlusion using explicitly labeled geometry. Low-poly.
Working Three.js Starter (occluder-with-umbra-and-penumbra-cones-x3773):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Extended Source — Umbra and Penumbra</title>
<style>
:root {
color-scheme: dark;
--amber: #ffc36b;
--cyan: #62e6ff;
--panel: rgba(8, 17, 30, 0.82);
--border: rgba(151, 190, 221, 0.22);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 3;
}
.panel {
position: absolute;
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 17px 19px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 16px 42px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 6px;
color: #8eb4cf;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #eef8ff;
font-size: clamp(19px, 2.2vw, 27px);
line-height: 1.08;
letter-spacing: -0.025em;
}
.subtitle {
margin: 8px 0 15px;
color: #a8bfd1;
font-size: 12px;
line-height: 1.5;
}
.legend {
display: grid;
gap: 8px;
padding-top: 12px;
border-top: 1px solid rgba(151, 190, 221, 0.14);
}
.legend-row {
display: grid;
grid-template-columns: 30px 80px 1fr;
align-items: center;
gap: 8px;
color: #b8cad9;
font-size: 11px;
}
.legend-row strong {
color: #eff8ff;
font-size: 11px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.swatch {
display: block;
width: 28px;
height: 12px;
border-radius: 3px;
}
.swatch.umbra {
border: 1px solid var(--cyan);
background: #142744;
}
.swatch.penumbra {
border: 1px solid var(--amber);
background: rgba(255, 181, 92, 0.55);
}
.controls {
position: absolute;
top: 20px;
right: 20px;
display: flex;
gap: 7px;
pointer-events: auto;
}
button {
appearance: none;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 8px;
color: #dcecf8;
background: rgba(10, 21, 36, 0.88);
font: inherit;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.04em;
cursor: pointer;
backdrop-filter: blur(10px);
}
button:hover {
border-color: rgba(98, 230, 255, 0.62);
color: white;
background: rgba(18, 39, 58, 0.94);
}
.scene-label {
position: absolute;
left: 0;
top: 0;
min-width: 112px;
padding: 7px 10px;
transform: translate(-50%, -100%);
border: 1px solid rgba(164, 198, 221, 0.35);
border-left: 3px solid #9eb7ca;
border-radius: 6px;
color: #eaf6ff;
background: rgba(5, 13, 24, 0.86);
box-shadow: 0 7px 20px rgba(0, 0, 0, 0.3);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
line-height: 1.25;
text-transform: uppercase;
white-space: nowrap;
will-change: left, top;
}
.scene-label small {
display: block;
margin-top: 2px;
color: #91aabd;
font-size: 8px;
font-weight: 650;
letter-spacing: 0.09em;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: currentColor;
opacity: 0.55;
}
.scene-label.umbra {
border-left-color: var(--cyan);
color: var(--cyan);
}
.scene-label.penumbra {
border-left-color: var(--amber);
color: var(--amber);
}
.scene-label.source {
border-left-color: #ffe2a4;
color: #ffe2a4;
}
.scene-label.occluder {
border-left-color: #b5c6d7;
color: #b5c6d7;
}
.scene-label.plane {
border-left-color: #708ca4;
color: #9eb8cc;
}
.note {
position: absolute;
right: 20px;
bottom: 18px;
max-width: 360px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 8px;
color: #91aabd;
background: rgba(7, 17, 31, 0.74);
font-size: 10px;
line-height: 1.45;
text-align: right;
backdrop-filter: blur(8px);
}
.hint {
position: absolute;
left: 20px;
bottom: 20px;
color: #7892a8;
font-size: 10px;
letter-spacing: 0.05em;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
}
.note {
display: none;
}
.hint {
left: 12px;
bottom: 58px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div class="hud">
<section class="panel">
<div class="eyebrow">Geometric optics study</div>
<h1>Extended-source shadow</h1>
<p class="subtitle">
A finite-width emitter illuminates a faceted opaque sphere.
Tangent-ray intersections distinguish complete and partial occlusion.
</p>
<div class="legend">
<div class="legend-row">
<span class="swatch umbra"></span>
<strong>Umbra</strong>
<span>Full occlusion — the complete source is blocked.</span>
</div>
<div class="legend-row">
<span class="swatch penumbra"></span>
<strong>Penumbra</strong>
<span>Partial occlusion — only part of the source is blocked.</span>
</div>
</div>
</section>
<nav class="controls" aria-label="Camera views">
<button id="obliqueButton" type="button">Oblique</button>
<button id="topButton" type="button">Top view</button>
<button id="resetButton" type="button">Reset</button>
</nav>
<div id="label-source" class="scene-label source">
Extended source
<small>Finite-width emitter</small>
</div>
<div id="label-occluder" class="scene-label occluder">
Opaque sphere
<small>Faceted occluder</small>
</div>
<div id="label-umbra" class="scene-label umbra">
Umbra
<small>Full occlusion</small>
</div>
<div id="label-penumbra" class="scene-label penumbra">
Penumbra
<small>Partial occlusion</small>
</div>
<div id="label-plane" class="scene-label plane">
Receiving plane
<small>Boundary construction</small>
</div>
<div class="hint">
DRAG TO ORBIT · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET
</div>
<div class="note">
Schematic construction: 41 samples span the emitter. The union of
their tangent-shadow footprints forms the penumbra; the intersection
forms the umbra.
</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 viewport = document.querySelector("#viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 18, 37);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.45, 1.45);
const view = {
yaw: 2.48,
pitch: 0.57,
radius: 14.2
};
function updateCamera() {
const horizontalRadius = Math.cos(view.pitch) * view.radius;
camera.position.set(
cameraTarget.x + Math.sin(view.yaw) * horizontalRadius,
cameraTarget.y + Math.sin(view.pitch) * view.radius,
cameraTarget.z + Math.cos(view.yaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
updateCamera();
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9dc8e8, 0x16202d, 1.15));
const fillLight = new THREE.DirectionalLight(0xc6dded, 2.0);
fillLight.position.set(-5, 9, 5);
scene.add(fillLight);
const sourceLight = new THREE.PointLight(0xffd28b, 38, 14, 2);
scene.add(sourceLight);
// ---------------------------------------------------------------------
// Model constants
// ---------------------------------------------------------------------
const SOURCE_Y = 4.4;
const SOURCE_Z = -2.2;
const SOURCE_HALF_LENGTH = 0.75;
sourceLight.position.set(0, SOURCE_Y, SOURCE_Z);
const SPHERE_Y = 1.45;
const SPHERE_RADIUS = 1.05;
const FLOOR_Y = 0;
// ---------------------------------------------------------------------
// Receiving plane
// ---------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x172536,
roughness: 0.94,
metalness: 0.05,
flatShading: true
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(16, 0.16, 12),
floorMaterial
);
floor.position.set(0, -0.08, 1);
scene.add(floor);
const floorEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(floor.geometry),
new THREE.LineBasicMaterial({
color: 0x6c8aa1,
transparent: true,
opacity: 0.35
})
);
floorEdges.position.copy(floor.position);
scene.add(floorEdges);
// Subtle construction grid.
const gridPositions = [];
for (let x = -8; x <= 8; x += 1) {
gridPositions.push(x, 0.006, -5, x, 0.006, 7);
}
for (let z = -5; z <= 7; z += 1) {
gridPositions.push(-8, 0.006, z, 8, 0.006, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
scene.add(
new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x7592aa,
transparent: true,
opacity: 0.13
})
)
);
// ---------------------------------------------------------------------
// Extended light source
// ---------------------------------------------------------------------
const sourceGroup = new THREE.Group();
sourceGroup.position.set(0, SOURCE_Y, SOURCE_Z);
scene.add(sourceGroup);
const sourceMaterial = new THREE.MeshStandardMaterial({
color: 0xffe0a3,
emissive: 0xffb64e,
emissiveIntensity: 3.2,
roughness: 0.42,
metalness: 0,
flatShading: true
});
const sourceBar = new THREE.Mesh(
new THREE.BoxGeometry(SOURCE_HALF_LENGTH * 2, 0.18, 0.2),
sourceMaterial
);
sourceGroup.add(sourceBar);
const sourceEndMaterial = new THREE.MeshStandardMaterial({
color: 0xffefd0,
emissive: 0xffc76e,
emissiveIntensity: 2.2,
roughness: 0.35,
flatShading: true
});
for (const x of [-SOURCE_HALF_LENGTH, SOURCE_HALF_LENGTH]) {
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.065, 0.29, 0.29),
sourceEndMaterial
);
cap.position.x = x;
sourceGroup.add(cap);
}
// Low-poly translucent shells suggest emitted light without textures.
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0xffb758,
transparent: true,
opacity: 0.085,
depthWrite: false,
side: THREE.DoubleSide
});
const glowOuter = new THREE.Mesh(
new THREE.BoxGeometry(1.95, 0.48, 0.48),
glowMaterial
);
sourceGroup.add(glowOuter);
const glowInnerMaterial = glowMaterial.clone();
glowInnerMaterial.opacity = 0.14;
const glowInner = new THREE.Mesh(
new THREE.BoxGeometry(1.68, 0.31, 0.31),
glowInnerMaterial
);
sourceGroup.add(glowInner);
// Dashed construction axes from the source to the occluder.
const axisMaterial = new THREE.LineDashedMaterial({
color: 0xffd28b,
transparent: true,
opacity: 0.28,
dashSize: 0.14,
gapSize: 0.12
});
const centerAxis = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, SOURCE_Y, SOURCE_Z),
new THREE.Vector3(0, SPHERE_Y, 0)
]),
axisMaterial
);
centerAxis.computeLineDistances();
scene.add(centerAxis);
const sideAxisMaterial = axisMaterial.clone();
sideAxisMaterial.opacity = 0.12;
for (const x of [-SOURCE_HALF_LENGTH, SOURCE_HALF_LENGTH]) {
const sideAxis = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x, SOURCE_Y, SOURCE_Z),
new THREE.Vector3(0, SPHERE_Y, 0)
]),
sideAxisMaterial
);
sideAxis.computeLineDistances();
scene.add(sideAxis);
}
// ---------------------------------------------------------------------
// Opaque low-poly sphere
// ---------------------------------------------------------------------
const occluderMaterial = new THREE.MeshStandardMaterial({
color: 0x71869a,
roughness: 0.82,
metalness: 0.12,
flatShading: true
});
const occluder = new THREE.Mesh(
new THREE.SphereGeometry(SPHERE_RADIUS, 12, 8),
occluderMaterial
);
occluder.position.set(0, SPHERE_Y, 0);
scene.add(occluder);
// ---------------------------------------------------------------------
// Tangent-ray shadow construction
//
// Each sampled point on the extended source is treated as a point
// source. For a receiver coordinate, tangent lines from that source
// to the sphere define its blocked interval.
//
// Union over all source samples = partial/full shadow footprint.
// Intersection over every sample = complete umbra.
// ---------------------------------------------------------------------
function tangentInterval(sourceX, receiverZ) {
const source = new THREE.Vector3(sourceX, SOURCE_Y, SOURCE_Z);
const center = new THREE.Vector3(0, SPHERE_Y, 0);
const vx = center.x - source.x;
const vy = center.y - source.y;
const vz = center.z - source.z;
const wx0 = -sourceX;
const wy = -SOURCE_Y;
const wz = receiverZ - SOURCE_Z;
// Cross product v × w, with w = (x + wx0, wy, wz).
const cx = vy * wz - vz * wy;
const cyCoefficient = vz;
const cyConstant = vz * wx0 - vx * wz;
const czCoefficient = -vy;
const czConstant = vx * wy - vy * wx0;
// |v × w|² - R²|w|² = 0 is quadratic in receiver X.
const A =
cyCoefficient * cyCoefficient +
czCoefficient * czCoefficient -
SPHERE_RADIUS * SPHERE_RADIUS;
const B =
2 *
(cyCoefficient * cyConstant + czCoefficient * czConstant) -
2 * SPHERE_RADIUS * SPHERE_RADIUS * wx0;
const C =
cx * cx +
cyConstant * cyConstant +
czConstant * czConstant -
SPHERE_RADIUS *
SPHERE_RADIUS *
(wx0 * wx0 + wy * wy + wz * wz);
const discriminant = B * B - 4 * A * C;
if (discriminant < 0 || Math.abs(A) < 1e-10) {
return null;
}
const squareRoot = Math.sqrt(discriminant);
const rootA = (-B - squareRoot) / (2 * A);
const rootB = (-B + squareRoot) / (2 * A);
const validRoots = [];
for (const root of [rootA, rootB]) {
const wx = root + wx0;
const lengthSquared = wx * wx + wy * wy + wz * wz;
// Closest point parameter along the source-to-receiver line.
const t =
(vx * wx + vy * wy + vz * wz) / lengthSquared;
// The sphere must occur before the receiver.
if (t > 0 && t < 1) {
validRoots.push(root);
}
}
if (validRoots.length < 2) {
return null;
}
validRoots.sort((a, b) => a - b);
return {
low: validRoots[0],
high: validRoots[1]
};
}
const shadowSamples = [];
const sourceSampleCount = 41;
const receiverSliceCount = 100;
const receiverZStart = 0.58;
const receiverZEnd = 3.18;
for (let slice = 0; slice < receiverSliceCount; slice++) {
const t = slice / (receiverSliceCount - 1);
const z = THREE.MathUtils.lerp(receiverZStart, receiverZEnd, t);
const intervals = [];
for (let sample = 0; sample < sourceSampleCount; sample++) {
const sourceX = THREE.MathUtils.lerp(
-SOURCE_HALF_LENGTH,
SOURCE_HALF_LENGTH,
sample / (sourceSampleCount - 1)
);
const interval = tangentInterval(sourceX, z);
if (interval) {
intervals.push(interval);
}
}
if (intervals.length === 0) {
continue;
}
let outerLow = Infinity;
let outerHigh = -Infinity;
for (const interval of intervals) {
outerLow = Math.min(outerLow, interval.low);
outerHigh = Math.max(outerHigh, interval.high);
}
let innerLow = -Infinity;
let innerHigh = Infinity;
let fullyOccluded = false;
// Full occlusion requires every source sample to be blocked.
if (intervals.length === sourceSampleCount) {
for (const interval of intervals) {
innerLow = Math.max(innerLow, interval.low);
innerHigh = Math.min(innerHigh, interval.high);
}
fullyOccluded = innerLow <= innerHigh;
}
shadowSamples.push({
z,
outerLow,
outerHigh,
innerLow: fullyOccluded ? innerLow : null,
innerHigh: fullyOccluded ? innerHigh : null,
fullyOccluded
});
}
// ---------------------------------------------------------------------
// Shadow ribbon geometry
// ---------------------------------------------------------------------
function contiguousRuns(samples, predicate) {
const runs = [];
let current = [];
for (const sample of samples) {
if (predicate(sample)) {
current.push(sample);
} else if (current.length) {
runs.push(current);
current = [];
}
}
if (current.length) {
runs.push(current);
}
return runs;
}
function makeRibbon(runs, leftKey, rightKey, height, material) {
const positions = [];
const indices = [];
for (const run of runs) {
if (run.length < 2) continue;
const vertexOffset = positions.length / 3;
for (const sample of run) {
positions.push(
sample[leftKey], height, sample.z,
sample[rightKey], height, sample.z
);
}
for (let i = 0; i < run.length - 1; i++) {
const a = vertexOffset + i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 2;
return mesh;
}
function makeBoundaryStrip(samples, key, side, height, material) {
const positions = [];
const indices = [];
let vertexOffset = 0;
for (const sample of samples) {
const x = sample[key];
if (x === null || !Number.isFinite(x)) {
vertexOffset = 0;
continue;
}
if (side === "left") {
positions.push(x, height, sample.z, x + 0.035, height, sample.z);
} else {
positions.push(x - 0.035, height, sample.z, x, height, sample.z);
}
}
for (let i = 0; i < samples.length - 1; i++) {
const a = vertexOffset + i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, b, c, b, d, c);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
const mesh = new THREE.Mesh(geometry, material);
mesh.renderOrder = 4;
return mesh;
}
const penumbraMaterial = new THREE.MeshBasicMaterial({
color: 0xf2a65a,
transparent: true,
opacity: 0.43,
depthWrite: false,
side: THREE.DoubleSide
});
const umbraMaterial = new THREE.MeshBasicMaterial({
color: 0x142744,
transparent: true,
opacity: 0.9,
depthWrite: false,
side: THREE.DoubleSide
});
const outerBoundaryMaterial = new THREE.MeshBasicMaterial({
color: 0xffc36b,
transparent: true,
opacity: 0.95,
depthWrite: false,
side: THREE.DoubleSide
});
const innerBoundaryMaterial = new THREE.MeshBasicMaterial({
color: 0x62e6ff,
transparent: true,
opacity: 0.95,
depthWrite: false,
side: THREE.DoubleSide
});
const allRuns = [shadowSamples];
const umbraRuns = contiguousRuns(
shadowSamples,
sample => sample.fullyOccluded
);
scene.add(
makeRibbon(
allRuns,
"outerLow",
"outerHigh",
0.018,
penumbraMaterial
)
);
scene.add(
makeRibbon(
umbraRuns,
"innerLow",
"innerHigh",
0.026,
umbraMaterial
)
);
scene.add(
makeBoundaryStrip(
shadowSamples,
"outerLow",
"left",
0.034,
outerBoundaryMaterial
)
);
scene.add(
makeBoundaryStrip(
shadowSamples,
"outerHigh",
"right",
0.034,
outerBoundaryMaterial
)
);
for (const run of umbraRuns) {
scene.add(
makeBoundaryStrip(
run,
"innerLow",
"left",
0.036,
innerBoundaryMaterial
)
);
scene.add(
makeBoundaryStrip(
run,
"innerHigh",
"right",
0.036,
innerBoundaryMaterial
)
);
}
// ---------------------------------------------------------------------
// Label anchors
// ---------------------------------------------------------------------
const validSamples = shadowSamples.filter(
sample => sample.fullyOccluded
);
let labelSample = validSamples[0];
if (validSamples.length) {
labelSample = validSamples.reduce((best, sample) => {
const bestDistance = Math.abs(best.z - 1.55);
const sampleDistance = Math.abs(sample.z - 1.55);
return sampleDistance < bestDistance ? sample : best;
}, validSamples[0]);
}
const umbraAnchor = new THREE.Vector3(
labelSample
? (labelSample.innerLow + labelSample.innerHigh) * 0.5
: 0,
0.08,
labelSample ? labelSample.z : 1.5
);
const penumbraAnchor = new THREE.Vector3(
labelSample
? labelSample.outerHigh - 0.28
: 1.2,
0.07,
labelSample ? labelSample.z : 1.5
);
const sourceAnchor = new THREE.Vector3(
0.45,
SOURCE_Y + 0.2,
SOURCE_Z
);
const occluderAnchor = new THREE.Vector3(
0.72,
SPHERE_Y + 0.32,
0.42
);
const planeAnchor = new THREE.Vector3(
4.7,
0.06,
4.4
);
const markerMaterialUmbra = new THREE.MeshBasicMaterial({
color: 0x62e6ff
});
const markerMaterialPenumbra = new THREE.MeshBasicMaterial({
color: 0xffc36b
});
const markerGeometry = new THREE.SphereGeometry(0.055, 8, 6);
scene.add(
new THREE.Mesh(markerGeometry, markerMaterialUmbra)
.translateX(umbraAnchor.x)
.translateY(umbraAnchor.y)
.translateZ(umbraAnchor.z)
);
scene.add(
new THREE.Mesh(markerGeometry, markerMaterialPenumbra)
.translateX(penumbraAnchor.x)
.translateY(penumbraAnchor.y)
.translateZ(penumbraAnchor.z)
);
const labelElements = [
{
element: document.querySelector("#label-source"),
anchor: sourceAnchor,
offsetX: -10,
offsetY: -8
},
{
element: document.querySelector("#label-occluder"),
anchor: occluderAnchor,
offsetX: 76,
offsetY: -4
},
{
element: document.querySelector("#label-umbra"),
anchor: umbraAnchor,
offsetX: 0,
offsetY: -8
},
{
element: document.querySelector("#label-penumbra"),
anchor: penumbraAnchor,
offsetX: 0,
offsetY: -8
},
{
element: document.querySelector("#label-plane"),
anchor: planeAnchor,
offsetX: 0,
offsetY: -4
}
];
function updateLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const item of labelElements) {
const projected = item.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
item.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width + item.offsetX;
const y = (-projected.y * 0.5 + 0.5) * height + item.offsetY;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
// ---------------------------------------------------------------------
// Direct camera controls: no controls addon is used
// ---------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
view.yaw -= deltaX * 0.006;
view.pitch = THREE.MathUtils.clamp(
view.pitch + deltaY * 0.0045,
0.16,
1.42
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
7.2,
24
);
updateCamera();
},
{ passive: false }
);
function setView(yaw, pitch, radius) {
view.yaw = yaw;
view.pitch = pitch;
view.radius = radius;
updateCamera();
}
function resetView() {
setView(2.48, 0.57, 14.2);
}
document.querySelector("#obliqueButton").addEventListener(
"click",
resetView
);
document.querySelector("#topButton").addEventListener("click", () => {
setView(2.48, 1.38, 14.7);
});
document.querySelector("#resetButton").addEventListener(
"click",
resetView
);
renderer.domElement.addEventListener("dblclick", resetView);
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
updateLabels();
}
animate();
</script>
</body>
</html>
```