Prompt specification
Build or customize the Three.js 3D scene "Extended light source shadow rig" 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: Extended light source shadow rig
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/extended-light-source-shadow-rig-x1581
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a scene with a disk-shaped light source, opaque ball, and screen; trace edge rays to distinguish full shadow and partial shadow regions geometrically. Low-poly.
Working Three.js Starter (extended-light-source-shadow-rig-x1581):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Finite Source Shadow Lab</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(9, 18, 34, 0.78);
--border: rgba(164, 196, 224, 0.18);
--text: #e9f3ff;
--muted: #9eb1c8;
--umbra: #151b38;
--penumbra: #72548e;
--amber: #ffb454;
--red: #ff6178;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07101d;
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;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: min(430px, calc(100vw - 52px));
}
.eyebrow {
margin-bottom: 8px;
color: var(--amber);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--text);
font-size: clamp(25px, 4vw, 42px);
font-weight: 720;
line-height: 1.02;
letter-spacing: -0.035em;
}
.subtitle {
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend,
.metrics,
.controls {
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.legend {
position: absolute;
left: 26px;
bottom: 26px;
width: 280px;
padding: 15px 16px;
border-radius: 15px;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr;
gap: 11px;
align-items: start;
margin: 10px 0;
}
.legend-row:first-child {
margin-top: 0;
}
.legend-row:last-child {
margin-bottom: 0;
}
.swatch {
width: 12px;
height: 12px;
margin-top: 2px;
border-radius: 4px;
box-shadow: 0 0 18px currentColor;
}
.swatch.umbra {
color: #121933;
background: var(--umbra);
border: 1px solid rgba(255, 255, 255, 0.14);
}
.swatch.penumbra {
color: var(--penumbra);
background: var(--penumbra);
}
.legend strong {
display: block;
color: var(--text);
font-size: 12px;
letter-spacing: 0.03em;
}
.legend small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.metrics {
position: absolute;
right: 26px;
top: 26px;
width: 225px;
padding: 14px 16px;
border-radius: 15px;
color: var(--muted);
font-size: 11px;
line-height: 1.8;
}
.metrics b {
color: var(--text);
font-weight: 650;
}
.metric-line {
display: flex;
justify-content: space-between;
gap: 12px;
}
.controls {
position: absolute;
right: 26px;
bottom: 26px;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 14px;
pointer-events: auto;
}
button {
border: 1px solid rgba(174, 204, 232, 0.16);
border-radius: 9px;
padding: 9px 12px;
color: var(--muted);
background: rgba(255, 255, 255, 0.035);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 160ms ease;
}
button:hover {
color: var(--text);
border-color: rgba(174, 204, 232, 0.32);
background: rgba(255, 255, 255, 0.075);
}
button.active {
color: #101827;
border-color: transparent;
background: var(--amber);
}
.hint {
position: absolute;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(190, 207, 225, 0.58);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: var(--muted);
background: #07101d;
font-size: 12px;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.subtitle,
.hint {
display: none;
}
.legend {
left: 18px;
bottom: 18px;
width: 225px;
}
.metrics {
top: 18px;
right: 18px;
width: 180px;
}
.controls {
right: 18px;
bottom: 18px;
}
}
</style>
</head>
<body>
<div id="loading">Building geometric shadow scene…</div>
<div class="hud">
<header class="brand">
<div class="eyebrow">Finite-source shadow lab</div>
<h1>Umbra & Penumbra</h1>
<p class="subtitle">
A disk-shaped emitter, an opaque low-poly sphere, and a receiver screen.
Tangent rays are traced directly from the source edge to the sphere.
</p>
</header>
<section class="legend">
<div class="legend-row">
<span class="swatch umbra"></span>
<div>
<strong>Full shadow / umbra</strong>
<small>Every ray connecting the source disk to this point intersects the ball.</small>
</div>
</div>
<div class="legend-row">
<span class="swatch penumbra"></span>
<div>
<strong>Partial shadow / penumbra</strong>
<small>Some source rays intersect the ball while others pass around it.</small>
</div>
</div>
</section>
<section class="metrics" id="metrics">
<div class="metric-line">
<span>Umbra radius</span>
<b id="umbraMetric">—</b>
</div>
<div class="metric-line">
<span>Penumbra radius</span>
<b id="penumbraMetric">—</b>
</div>
<div class="metric-line">
<span>Tangent traces</span>
<b id="rayMetric">—</b>
</div>
<div class="metric-line">
<span>Source / sphere</span>
<b id="sourceMetric">—</b>
</div>
</section>
<div class="controls">
<button id="rayButton" class="active" type="button">Rays on</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="hint">Drag to orbit · scroll to zoom · double-click to reset</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Geometric setup
// ------------------------------------------------------------
const SOURCE_Z = -4.5;
const SOURCE_RADIUS = 1.5;
const SPHERE_RADIUS = 1.0;
const SPHERE_Z = 0.0;
const SCREEN_Z = 4.2;
const SCREEN_WIDTH = 10.0;
const SCREEN_HEIGHT = 7.0;
const SOURCE_TO_SPHERE = SPHERE_Z - SOURCE_Z;
const SPHERE_TO_SCREEN = SCREEN_Z - SPHERE_Z;
const TOTAL_DISTANCE = SCREEN_Z - SOURCE_Z;
/*
In a meridional plane, a receiver point is P = (rho, SCREEN_Z).
A source-to-receiver ray has x(z) = rho + m(z - SCREEN_Z).
Requiring this line to be tangent to the sphere gives:
(rho - SCREEN_Z * m)^2 = SPHERE_RADIUS^2 * (1 + m^2)
The smaller tangent slope produces the upper endpoint of the
blocked interval on the source plane. The source disk is fully
blocked while that interval contains [-SOURCE_RADIUS, SOURCE_RADIUS].
*/
function upperBlockedSourceCoordinate(rho) {
const denominator = SPHERE_TO_SCREEN * SPHERE_TO_SCREEN
- SPHERE_RADIUS * SPHERE_RADIUS;
const discriminant =
SPHERE_TO_SCREEN * SPHERE_TO_SCREEN
+ rho * rho
- SPHERE_RADIUS * SPHERE_RADIUS;
const tangentSlope = (
rho * SPHERE_TO_SCREEN
- SPHERE_RADIUS * Math.sqrt(Math.max(0, discriminant))
) / denominator;
return rho - TOTAL_DISTANCE * tangentSlope;
}
function solveRadius(targetCoordinate) {
let low = 0;
let high = 12;
const f = (x) => upperBlockedSourceCoordinate(x) - targetCoordinate;
while (Math.abs(f(low)) < 1e-10) {
return low;
}
// Expand the upper bound if necessary.
for (let i = 0; i < 8 && f(low) * f(high) > 0; i++) {
high *= 1.5;
}
for (let i = 0; i < 100; i++) {
const middle = (low + high) * 0.5;
if (f(low) * f(middle) <= 0) {
high = middle;
} else {
low = middle;
}
}
return (low + high) * 0.5;
}
const umbraRadius = solveRadius(SOURCE_RADIUS);
const penumbraRadius = solveRadius(-SOURCE_RADIUS);
const rayCount = 16;
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101d);
scene.fog = new THREE.Fog(0x07101d, 21, 38);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const orbit = {
theta: 0.58,
phi: 1.13,
radius: 18.0,
target: new THREE.Vector3(0, -0.25, 0.1)
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
// ------------------------------------------------------------
// Lighting and environment
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa8caff, 0x101522, 1.35));
const keyLight = new THREE.DirectionalLight(0xd7e8ff, 2.7);
keyLight.position.set(6, 9, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffa95c, 1.15);
rimLight.position.set(-7, 3, -6);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 24),
new THREE.MeshStandardMaterial({
color: 0x0b1524,
roughness: 0.96,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -4.0;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x34506b, 0x1c3047);
grid.position.y = -3.985;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.45, 1.7, 0.35, 12),
new THREE.MeshStandardMaterial({
color: 0x1a2b3c,
roughness: 0.68,
metalness: 0.35,
flatShading: true
})
);
pedestal.position.set(0, -1.2, 0);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
scene.add(pedestal);
const pedestalRing = new THREE.Mesh(
new THREE.TorusGeometry(1.27, 0.035, 6, 36),
new THREE.MeshBasicMaterial({
color: 0x4c8aa5,
transparent: true,
opacity: 0.55
})
);
pedestalRing.rotation.x = Math.PI / 2;
pedestalRing.position.set(0, -1.015, 0);
scene.add(pedestalRing);
// ------------------------------------------------------------
// Disk-shaped source
// ------------------------------------------------------------
const sourceStand = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.2, 2.8, 8),
new THREE.MeshStandardMaterial({
color: 0x26384a,
roughness: 0.58,
metalness: 0.5,
flatShading: true
})
);
sourceStand.position.set(0, -2.6, SOURCE_Z - 0.1);
sourceStand.castShadow = true;
scene.add(sourceStand);
const sourceHousing = new THREE.Mesh(
new THREE.CylinderGeometry(1.58, 1.58, 0.32, 24),
new THREE.MeshStandardMaterial({
color: 0x283648,
emissive: 0x4c2c0e,
emissiveIntensity: 0.35,
roughness: 0.46,
metalness: 0.58,
flatShading: true
})
);
sourceHousing.rotation.x = Math.PI / 2;
sourceHousing.position.z = SOURCE_Z;
sourceHousing.castShadow = true;
scene.add(sourceHousing);
const emitterMaterial = new THREE.MeshBasicMaterial({
color: 0xffd078,
transparent: true,
opacity: 0.92,
side: THREE.DoubleSide
});
const emitter = new THREE.Mesh(
new THREE.CircleGeometry(1.35, 24),
emitterMaterial
);
emitter.position.z = SOURCE_Z + 0.175;
scene.add(emitter);
const sourceRim = new THREE.Mesh(
new THREE.TorusGeometry(1.47, 0.075, 8, 32),
new THREE.MeshStandardMaterial({
color: 0xffb65a,
emissive: 0xff7a19,
emissiveIntensity: 1.4,
roughness: 0.35,
metalness: 0.45
})
);
sourceRim.position.z = SOURCE_Z + 0.19;
scene.add(sourceRim);
const sourceLight = new THREE.PointLight(0xffb35b, 42, 19, 2);
sourceLight.position.set(0, 0, SOURCE_Z + 0.45);
scene.add(sourceLight);
// A soft procedural halo behind the disk.
const glowCanvas = document.createElement("canvas");
glowCanvas.width = glowCanvas.height = 256;
const glowContext = glowCanvas.getContext("2d");
const glowGradient = glowContext.createRadialGradient(
128, 128, 20,
128, 128, 128
);
glowGradient.addColorStop(0, "rgba(255, 205, 120, 0.75)");
glowGradient.addColorStop(0.35, "rgba(255, 145, 45, 0.28)");
glowGradient.addColorStop(1, "rgba(255, 110, 20, 0)");
glowContext.fillStyle = glowGradient;
glowContext.fillRect(0, 0, 256, 256);
const glowTexture = new THREE.CanvasTexture(glowCanvas);
glowTexture.colorSpace = THREE.SRGBColorSpace;
const sourceGlow = new THREE.Mesh(
new THREE.PlaneGeometry(4.3, 4.3),
new THREE.MeshBasicMaterial({
map: glowTexture,
transparent: true,
opacity: 0.58,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
sourceGlow.position.z = SOURCE_Z - 0.18;
scene.add(sourceGlow);
// ------------------------------------------------------------
// Opaque low-poly ball
// ------------------------------------------------------------
const ballGeometry = new THREE.IcosahedronGeometry(
SPHERE_RADIUS,
1
);
const ball = new THREE.Mesh(
ballGeometry,
new THREE.MeshStandardMaterial({
color: 0x365473,
roughness: 0.42,
metalness: 0.28,
flatShading: true
})
);
ball.position.z = SPHERE_Z;
ball.castShadow = true;
ball.receiveShadow = true;
scene.add(ball);
const ballEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(ballGeometry, 18),
new THREE.LineBasicMaterial({
color: 0x85a8c4,
transparent: true,
opacity: 0.3
})
);
ballEdges.position.copy(ball.position);
scene.add(ballEdges);
// ------------------------------------------------------------
// Receiver screen and geometrically colored shadow texture
// ------------------------------------------------------------
const shadowCanvas = document.createElement("canvas");
shadowCanvas.width = 1000;
shadowCanvas.height = 700;
const shadowContext = shadowCanvas.getContext("2d");
shadowContext.clearRect(0, 0, shadowCanvas.width, shadowCanvas.height);
// Subtle receiver grid.
shadowContext.strokeStyle = "rgba(214, 235, 255, 0.075)";
shadowContext.lineWidth = 1;
for (let x = 0; x <= shadowCanvas.width; x += 50) {
shadowContext.beginPath();
shadowContext.moveTo(x, 0);
shadowContext.lineTo(x, shadowCanvas.height);
shadowContext.stroke();
}
for (let y = 0; y <= shadowCanvas.height; y += 50) {
shadowContext.beginPath();
shadowContext.moveTo(0, y);
shadowContext.lineTo(shadowCanvas.width, y);
shadowContext.stroke();
}
const pixelsPerWorldUnit = shadowCanvas.width / SCREEN_WIDTH;
const innerPixels = umbraRadius * pixelsPerWorldUnit;
const outerPixels = penumbraRadius * pixelsPerWorldUnit;
const shadowGradient = shadowContext.createRadialGradient(
shadowCanvas.width / 2,
shadowCanvas.height / 2,
0,
shadowCanvas.width / 2,
shadowCanvas.height / 2,
outerPixels + 2
);
shadowGradient.addColorStop(0, "rgba(7, 12, 31, 0.96)");
shadowGradient.addColorStop(
innerPixels / (outerPixels + 2),
"rgba(16, 20, 45, 0.91)"
);
shadowGradient.addColorStop(
outerPixels / (outerPixels + 2),
"rgba(92, 61, 122, 0.34)"
);
shadowGradient.addColorStop(1, "rgba(92, 61, 122, 0)");
shadowContext.fillStyle = shadowGradient;
shadowContext.fillRect(0, 0, shadowCanvas.width, shadowCanvas.height);
// Boundary contours reinforce the two geometric classifications.
shadowContext.strokeStyle = "rgba(255, 97, 120, 0.78)";
shadowContext.lineWidth = 2.2;
shadowContext.beginPath();
shadowContext.arc(
shadowCanvas.width / 2,
shadowCanvas.height / 2,
innerPixels,
0,
Math.PI * 2
);
shadowContext.stroke();
shadowContext.strokeStyle = "rgba(255, 180, 84, 0.72)";
shadowContext.lineWidth = 2;
shadowContext.beginPath();
shadowContext.arc(
shadowCanvas.width / 2,
shadowCanvas.height / 2,
outerPixels,
0,
Math.PI * 2
);
shadowContext.stroke();
const shadowTexture = new THREE.CanvasTexture(shadowCanvas);
shadowTexture.colorSpace = THREE.SRGBColorSpace;
shadowTexture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const screenPanel = new THREE.Mesh(
new THREE.PlaneGeometry(SCREEN_WIDTH, SCREEN_HEIGHT),
new THREE.MeshStandardMaterial({
color: 0xb8d1df,
transparent: true,
opacity: 0.2,
roughness: 0.72,
metalness: 0.05,
side: THREE.DoubleSide,
depthWrite: false
})
);
screenPanel.position.z = SCREEN_Z;
screenPanel.renderOrder = 2;
scene.add(screenPanel);
const shadowReceiver = new THREE.Mesh(
new THREE.PlaneGeometry(SCREEN_WIDTH, SCREEN_HEIGHT),
new THREE.MeshBasicMaterial({
map: shadowTexture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
})
);
shadowReceiver.position.z = SCREEN_Z + 0.035;
shadowReceiver.renderOrder = 3;
scene.add(shadowReceiver);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x2b4054,
roughness: 0.4,
metalness: 0.62
});
const horizontalFrameGeometry = new THREE.BoxGeometry(
SCREEN_WIDTH + 0.22,
0.1,
0.16
);
const verticalFrameGeometry = new THREE.BoxGeometry(
0.1,
SCREEN_HEIGHT + 0.22,
0.16
);
const topFrame = new THREE.Mesh(horizontalFrameGeometry, frameMaterial);
topFrame.position.set(0, SCREEN_HEIGHT / 2 + 0.06, SCREEN_Z);
const bottomFrame = new THREE.Mesh(horizontalFrameGeometry, frameMaterial);
bottomFrame.position.set(0, -SCREEN_HEIGHT / 2 - 0.06, SCREEN_Z);
const leftFrame = new THREE.Mesh(verticalFrameGeometry, frameMaterial);
leftFrame.position.set(-SCREEN_WIDTH / 2 - 0.06, 0, SCREEN_Z);
const rightFrame = new THREE.Mesh(verticalFrameGeometry, frameMaterial);
rightFrame.position.set(SCREEN_WIDTH / 2 + 0.06, 0, SCREEN_Z);
scene.add(topFrame, bottomFrame, leftFrame, rightFrame);
// ------------------------------------------------------------
// Tangent ray construction
// ------------------------------------------------------------
const rayGroup = new THREE.Group();
scene.add(rayGroup);
const innerRayVertices = [];
const outerRayVertices = [];
const tangentMarkerGeometry = new THREE.SphereGeometry(
0.052,
6,
4
);
const innerMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xff6178
});
const outerMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xffb454
});
function appendTangentRay(
boundaryRadius,
sourceSign,
vertices,
markerMaterial
) {
const sourceRadial = sourceSign * SOURCE_RADIUS;
const slope =
(boundaryRadius - sourceRadial) / TOTAL_DISTANCE;
/*
The ray in a meridional plane is:
x(z) = boundaryRadius + slope * (z - SCREEN_Z)
Written as x - slope*z - c = 0, the perpendicular foot
from the sphere center is the tangent point.
*/
const c = boundaryRadius - slope * SCREEN_Z;
const tangentRadial = c / (1 + slope * slope);
const tangentZ =
-slope * c / (1 + slope * slope);
for (let i = 0; i < rayCount; i++) {
const angle = (i / rayCount) * Math.PI * 2;
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const sourcePoint = new THREE.Vector3(
sourceRadial * cos,
sourceRadial * sin,
SOURCE_Z
);
const tangentPoint = new THREE.Vector3(
tangentRadial * cos,
tangentRadial * sin,
tangentZ
);
const screenPoint = new THREE.Vector3(
boundaryRadius * cos,
boundaryRadius * sin,
SCREEN_Z + 0.05
);
vertices.push(
sourcePoint.x, sourcePoint.y, sourcePoint.z,
tangentPoint.x, tangentPoint.y, tangentPoint.z,
tangentPoint.x, tangentPoint.y, tangentPoint.z,
screenPoint.x, screenPoint.y, screenPoint.z
);
const marker = new THREE.Mesh(
tangentMarkerGeometry,
markerMaterial
);
marker.position.copy(tangentPoint);
rayGroup.add(marker);
}
}
// Inner boundary: the same-side source edge becomes tangent.
appendTangentRay(
umbraRadius,
1,
innerRayVertices,
innerMarkerMaterial
);
// Outer boundary: the opposite-side source edge becomes tangent.
appendTangentRay(
penumbraRadius,
-1,
outerRayVertices,
outerMarkerMaterial
);
function makeRayLines(vertices, color, opacity) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const lines = new THREE.LineSegments(geometry, material);
lines.renderOrder = 4;
rayGroup.add(lines);
return lines;
}
makeRayLines(innerRayVertices, 0xff6178, 0.76);
makeRayLines(outerRayVertices, 0xffb454, 0.7);
// Small markers identify the sampled source-edge positions.
for (let i = 0; i < rayCount; i++) {
const angle = (i / rayCount) * Math.PI * 2;
const edgeMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 5, 3),
new THREE.MeshBasicMaterial({ color: 0xffd18a })
);
edgeMarker.position.set(
SOURCE_RADIUS * Math.cos(angle),
SOURCE_RADIUS * Math.sin(angle),
SOURCE_Z + 0.2
);
rayGroup.add(edgeMarker);
}
// A faint construction axis.
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, SOURCE_Z - 0.4),
new THREE.Vector3(0, 0, SCREEN_Z)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0xaac8e8,
transparent: true,
opacity: 0.16,
dashSize: 0.12,
gapSize: 0.13
})
);
axis.computeLineDistances();
scene.add(axis);
// ------------------------------------------------------------
// HUD readouts
// ------------------------------------------------------------
document.getElementById("umbraMetric").textContent =
`${umbraRadius.toFixed(3)} units`;
document.getElementById("penumbraMetric").textContent =
`${penumbraRadius.toFixed(3)} units`;
document.getElementById("rayMetric").textContent =
`${rayCount * 2}`;
document.getElementById("sourceMetric").textContent =
`r ${SOURCE_RADIUS.toFixed(1)} / ${SPHERE_RADIUS.toFixed(1)}`;
// ------------------------------------------------------------
// Custom camera controls — no addon imports
// ------------------------------------------------------------
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 dx = event.clientX - previousX;
const dy = event.clientY - previousY;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.32,
1.47
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
10,
30
);
updateCamera();
},
{ passive: false }
);
function resetView() {
orbit.theta = 0.58;
orbit.phi = 1.13;
orbit.radius = 18.0;
updateCamera();
}
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("resetButton").addEventListener(
"click",
resetView
);
const rayButton = document.getElementById("rayButton");
rayButton.addEventListener("click", () => {
rayGroup.visible = !rayGroup.visible;
rayButton.classList.toggle("active", rayGroup.visible);
rayButton.textContent = rayGroup.visible ? "Rays on" : "Rays off";
});
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
if (event.code === "Space") {
event.preventDefault();
rayButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
const pulse = 0.5 + 0.5 * Math.sin(elapsed * 2.1);
emitterMaterial.opacity = 0.82 + pulse * 0.14;
sourceLight.intensity = 38 + pulse * 8;
sourceGlow.material.opacity = 0.47 + pulse * 0.15;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```