Prompt specification
Build or customize the Three.js 3D scene "Window light reaches a seat" 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: Window light reaches a seat
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/window-light-reaches-a-seat-x0238
Scene Description Prompt:
Move sunlight across a low-poly window seat, making its light patch travel over cushion and floor through a fixed opening. Three.js from cdnjs, one HTML file.
Working Three.js Starter (window-light-reaches-a-seat-x0238):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Window Seat in Passing Light</title>
<style>
:root {
--ink: #3f352d;
--paper: #f4ecdd;
--accent: #b76545;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d3c7;
color: var(--ink);
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.dragging {
cursor: grabbing;
}
.interface {
position: fixed;
inset: 0;
z-index: 10;
pointer-events: none;
}
.heading {
position: absolute;
top: clamp(24px, 4vw, 48px);
left: clamp(24px, 4vw, 56px);
max-width: 360px;
text-shadow: 0 1px 18px rgba(255, 248, 235, 0.45);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: rgba(63, 53, 45, 0.68);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 26px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(32px, 4.2vw, 62px);
font-weight: 400;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 15px 0 0;
color: rgba(63, 53, 45, 0.7);
font-size: 12px;
letter-spacing: 0.05em;
}
.status {
position: absolute;
top: clamp(24px, 4vw, 48px);
right: clamp(24px, 4vw, 56px);
display: flex;
align-items: center;
gap: 9px;
padding: 10px 14px;
border: 1px solid rgba(71, 58, 47, 0.14);
border-radius: 999px;
background: rgba(246, 239, 225, 0.62);
box-shadow: 0 8px 28px rgba(66, 45, 30, 0.08);
backdrop-filter: blur(12px);
color: rgba(63, 53, 45, 0.76);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #c17b45;
box-shadow: 0 0 0 4px rgba(193, 123, 69, 0.14);
animation: breathe 2.4s ease-in-out infinite;
}
.status.paused .status-dot {
background: #948b80;
box-shadow: none;
animation: none;
}
@keyframes breathe {
50% {
box-shadow: 0 0 0 7px rgba(193, 123, 69, 0.04);
}
}
.controls {
position: absolute;
right: clamp(20px, 4vw, 56px);
bottom: clamp(20px, 4vw, 42px);
display: grid;
grid-template-columns: auto minmax(130px, 220px) auto;
align-items: center;
gap: 15px;
padding: 12px 13px 12px 18px;
border: 1px solid rgba(71, 58, 47, 0.14);
border-radius: 16px;
background: rgba(246, 239, 225, 0.76);
box-shadow: 0 12px 38px rgba(66, 45, 30, 0.12);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.control-label {
color: rgba(63, 53, 45, 0.64);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
white-space: nowrap;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
accent-color: var(--accent);
cursor: ew-resize;
}
button {
min-width: 76px;
padding: 9px 12px;
border: 0;
border-radius: 10px;
background: var(--ink);
color: var(--paper);
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
button:hover {
background: #59493d;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.hint {
position: absolute;
bottom: clamp(27px, 4vw, 48px);
left: clamp(24px, 4vw, 56px);
color: rgba(63, 53, 45, 0.55);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.status {
display: none;
}
.subtitle {
display: none;
}
.controls {
right: 16px;
bottom: 16px;
left: 16px;
grid-template-columns: auto minmax(80px, 1fr) auto;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="interface">
<header class="heading">
<div class="eyebrow">Interior light study · 03</div>
<h1>Window Seat,<br>in Passing Light</h1>
<p class="subtitle">A low-poly room shaped by a moving afternoon sun.</p>
</header>
<div class="status" id="status">
<span class="status-dot"></span>
<span id="statusText">Live sun path</span>
</div>
<div class="hint">Drag to orbit · Scroll to dolly</div>
<div class="controls">
<label class="control-label" for="sunPath">Sun path</label>
<input
id="sunPath"
type="range"
min="0"
max="360"
value="32"
aria-label="Sun position"
>
<button id="playToggle" type="button" aria-label="Pause sun animation">
Ⅱ Pause
</button>
</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 renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.04;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Low-poly window seat in moving sunlight");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d3c7);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
40
);
const clock = new THREE.Clock();
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const standardMaterial = (color, roughness = 0.85) =>
new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0,
flatShading: true
});
const materials = {
wall: standardMaterial(0xded8cb),
floor: standardMaterial(0xa96f54, 0.94),
floorLine: standardMaterial(0x895641, 1),
frame: standardMaterial(0xf0e5d1, 0.76),
wood: standardMaterial(0x6f4d39, 0.88),
woodLight: standardMaterial(0x946a4b, 0.86),
cushionGold: standardMaterial(0xd4a15a, 0.94),
cushionRust: standardMaterial(0xbd704e, 0.94),
cushionCream: standardMaterial(0xe2cda1, 0.96),
pillowSage: standardMaterial(0x8d9d83, 0.96),
pillowBlue: standardMaterial(0x71858b, 0.96),
button: standardMaterial(0x9b6c43, 0.95),
pot: standardMaterial(0xb76745, 0.9),
soil: standardMaterial(0x4d3a2c, 1),
leaf: standardMaterial(0x617957, 0.94),
leafLight: standardMaterial(0x82906b, 0.94)
};
function addBox(width, height, depth, x, y, z, material, cast = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function roundedBoxGeometry(width, height, depth, radius) {
const x = -width / 2;
const y = -height / 2;
const r = Math.min(radius, width / 2, height / 2);
const shape = new THREE.Shape();
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
curveSegments: 2,
bevelEnabled: true,
bevelSegments: 1,
bevelSize: Math.min(0.055, radius * 0.48),
bevelThickness: Math.min(0.045, radius * 0.42)
});
geometry.center();
geometry.computeVertexNormals();
return geometry;
}
function addRoundedBox(
width,
height,
depth,
radius,
x,
y,
z,
material,
cast = true
) {
const mesh = new THREE.Mesh(
roundedBoxGeometry(width, height, depth, radius),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Room with a genuine, fixed opening in the back wall
// -------------------------------------------------------------------------
const wallWidth = 8.8;
const wallHeight = 4.7;
const wallDepth = 0.24;
const openingWidth = 2.8;
const openingBottom = 1.3;
const openingTop = 3.55;
const openingHeight = openingTop - openingBottom;
const sideWidth = (wallWidth - openingWidth) / 2;
// Floor
addBox(11, 0.12, 8.2, 0, -0.06, 3.9, materials.floor, false);
// Subtle floor-board seams
for (let x = -4.5; x <= 4.5; x += 1.5) {
addBox(0.012, 0.004, 8.1, x, 0.003, 3.9, materials.floorLine, false);
}
// Wall pieces around the empty window aperture
addBox(
sideWidth,
wallHeight,
wallDepth,
-(openingWidth / 2 + sideWidth / 2),
wallHeight / 2,
-wallDepth / 2,
materials.wall
);
addBox(
sideWidth,
wallHeight,
wallDepth,
openingWidth / 2 + sideWidth / 2,
wallHeight / 2,
-wallDepth / 2,
materials.wall
);
addBox(
openingWidth,
openingBottom,
wallDepth,
0,
openingBottom / 2,
-wallDepth / 2,
materials.wall
);
addBox(
openingWidth,
wallHeight - openingTop,
wallDepth,
0,
openingTop + (wallHeight - openingTop) / 2,
-wallDepth / 2,
materials.wall
);
// Baseboard
addBox(wallWidth, 0.16, 0.08, 0, 0.08, 0.05, materials.frame);
// Window frame, intentionally stationary
addBox(
0.14,
openingHeight + 0.28,
0.16,
-openingWidth / 2 - 0.035,
(openingBottom + openingTop) / 2,
0.04,
materials.frame
);
addBox(
0.14,
openingHeight + 0.28,
0.16,
openingWidth / 2 + 0.035,
(openingBottom + openingTop) / 2,
0.04,
materials.frame
);
addBox(
openingWidth + 0.25,
0.14,
0.16,
0,
openingBottom - 0.065,
0.04,
materials.frame
);
addBox(
openingWidth + 0.25,
0.14,
0.16,
0,
openingTop + 0.065,
0.04,
materials.frame
);
// A slender fixed mullion casts a line through the moving patch
addBox(
0.065,
openingHeight,
0.13,
0,
(openingBottom + openingTop) / 2,
0.045,
materials.frame
);
// Deep sill
addBox(
openingWidth + 0.42,
0.12,
0.52,
0,
openingBottom - 0.075,
0.09,
materials.frame
);
// -------------------------------------------------------------------------
// Low-poly exterior view
// -------------------------------------------------------------------------
const skyGeometry = new THREE.PlaneGeometry(14, 8, 1, 10);
const skyPositions = skyGeometry.attributes.position;
const skyColors = [];
const lowerSky = new THREE.Color(0xf0c9a2);
const upperSky = new THREE.Color(0x86b5c8);
const skyColor = new THREE.Color();
for (let i = 0; i < skyPositions.count; i++) {
const t = THREE.MathUtils.clamp(
(skyPositions.getY(i) + 4) / 8,
0,
1
);
skyColor.copy(lowerSky).lerp(upperSky, t);
skyColors.push(skyColor.r, skyColor.g, skyColor.b);
}
skyGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(skyColors, 3)
);
const sky = new THREE.Mesh(
skyGeometry,
new THREE.MeshBasicMaterial({
vertexColors: true,
side: THREE.DoubleSide
})
);
sky.position.set(0, 2.25, -3.25);
scene.add(sky);
function addLandscape(points, color, z) {
const shape = new THREE.Shape();
shape.moveTo(points[0][0], points[0][1]);
for (let i = 1; i < points.length; i++) {
shape.lineTo(points[i][0], points[i][1]);
}
shape.closePath();
const mesh = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
new THREE.MeshBasicMaterial({
color,
side: THREE.DoubleSide
})
);
mesh.position.z = z;
scene.add(mesh);
return mesh;
}
addLandscape(
[
[-7, -2],
[-7, 1.25],
[-5.5, 1.65],
[-4.2, 1.3],
[-2.9, 1.95],
[-1.6, 1.38],
[0, 1.82],
[1.8, 1.3],
[3.4, 1.72],
[5.2, 1.2],
[7, 1.48],
[7, -2]
],
0x809782,
-2.92
);
addLandscape(
[
[-7, -2],
[-7, 0.85],
[-4.8, 1.2],
[-3.4, 0.88],
[-1.8, 1.27],
[0, 0.83],
[1.8, 1.18],
[3.6, 0.9],
[5.2, 1.22],
[7, 0.82],
[7, -2]
],
0x5f7768,
-2.7
);
const cloudMaterial = new THREE.MeshBasicMaterial({ color: 0xf7edda });
const cloudGeometry = new THREE.DodecahedronGeometry(0.42, 0);
const clouds = [
[-1.05, 3.05, -2.8, 1.15, 0.55, 0.5],
[-0.55, 3.14, -2.82, 0.8, 0.45, 0.42],
[1.65, 2.72, -2.85, 0.75, 0.38, 0.4]
];
clouds.forEach(([x, y, z, sx, sy, sz]) => {
const cloud = new THREE.Mesh(cloudGeometry, cloudMaterial);
cloud.position.set(x, y, z);
cloud.scale.set(sx, sy, sz);
scene.add(cloud);
});
// Stylized visible sun. It never casts a shadow; the directional light does.
const sunDisk = new THREE.Mesh(
new THREE.CircleGeometry(0.21, 16),
new THREE.MeshBasicMaterial({ color: 0xffedb2 })
);
sunDisk.position.set(0.75, 2.78, -2.48);
scene.add(sunDisk);
// -------------------------------------------------------------------------
// Window seat
// -------------------------------------------------------------------------
// Wooden base and rails
addRoundedBox(3.8, 0.18, 1.26, 0.07, 0, 0.48, 0.68, materials.wood);
addBox(3.7, 0.08, 1.14, 0, 0.605, 0.68, materials.woodLight);
addRoundedBox(3.78, 0.28, 0.13, 0.04, 0, 0.64, 1.27, materials.wood);
// Six-sided tapered legs
const legGeometry = new THREE.CylinderGeometry(0.105, 0.14, 0.47, 6);
const legPositions = [
[-1.57, 0.25, 0.28],
[1.57, 0.25, 0.28],
[-1.57, 0.25, 1.07],
[1.57, 0.25, 1.07]
];
legPositions.forEach(([x, y, z]) => {
const leg = new THREE.Mesh(legGeometry, materials.wood);
leg.position.set(x, y, z);
leg.castShadow = true;
leg.receiveShadow = true;
scene.add(leg);
});
// Cushions
const cushionData = [
[-1.15, materials.cushionGold],
[0, materials.cushionRust],
[1.15, materials.cushionGold]
];
// Correct the material reference without introducing another library.
cushionData[0][1] = materials.cushionGold;
cushionData.forEach(([x, material]) => {
const cushion = addRoundedBox(
1.02,
0.24,
1.0,
0.12,
x,
0.79,
0.68,
material
);
cushion.rotation.y = (x / 1.15) * 0.012;
const button = new THREE.Mesh(
new THREE.CylinderGeometry(0.027, 0.027, 0.014, 8),
materials.button
);
button.position.set(x, 0.969, 0.69);
button.castShadow = true;
scene.add(button);
});
// Soft upright pillows
const leftPillow = addRoundedBox(
0.64,
0.48,
0.18,
0.15,
-1.16,
1.12,
0.29,
materials.pillowSage
);
leftPillow.rotation.x = -0.15;
leftPillow.rotation.z = 0.06;
const rightPillow = addRoundedBox(
0.64,
0.48,
0.18,
0.15,
1.17,
1.12,
0.29,
materials.pillowBlue
);
rightPillow.rotation.x = -0.15;
rightPillow.rotation.z = -0.05;
// -------------------------------------------------------------------------
// Small low-poly plant
// -------------------------------------------------------------------------
const plant = new THREE.Group();
plant.position.set(-3.12, 0, 1.12);
scene.add(plant);
const pot = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.19, 0.42, 7),
materials.pot
);
pot.position.y = 0.21;
pot.castShadow = true;
pot.receiveShadow = true;
plant.add(pot);
const soil = new THREE.Mesh(
new THREE.CylinderGeometry(0.225, 0.225, 0.025, 7),
materials.soil
);
soil.position.y = 0.418;
plant.add(soil);
const leafGeometry = new THREE.IcosahedronGeometry(0.3, 0);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2 + 0.2;
const leaf = new THREE.Mesh(
leafGeometry,
i % 2 ? materials.leafLight : materials.leaf
);
const reach = 0.16 + (i % 3) * 0.055;
leaf.position.set(
Math.cos(angle) * reach,
0.78 + (i % 3) * 0.12,
Math.sin(angle) * reach
);
leaf.scale.set(0.48, 1.85 + (i % 2) * 0.22, 0.42);
leaf.rotation.z = Math.cos(angle) * 0.48;
leaf.rotation.x = Math.sin(angle) * 0.48;
leaf.castShadow = true;
leaf.receiveShadow = true;
plant.add(leaf);
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xcbdde3,
0x80523d,
1.05
);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0xfff1d7, 0.16);
scene.add(ambient);
const sunlight = new THREE.DirectionalLight(0xffd49a, 4.4);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -7.5;
sunlight.shadow.camera.right = 7.5;
sunlight.shadow.camera.top = 7.5;
sunlight.shadow.camera.bottom = -7.5;
sunlight.shadow.camera.near = 0.5;
sunlight.shadow.camera.far = 35;
sunlight.shadow.bias = -0.00025;
sunlight.shadow.normalBias = 0.025;
sunlight.shadow.radius = 2;
const lightTarget = new THREE.Vector3(0, 0.35, 1.15);
sunlight.target.position.copy(lightTarget);
sunlight.position.set(3.45, 6.42, -5.5);
scene.add(sunlight);
scene.add(sunlight.target);
// -------------------------------------------------------------------------
// Dust suspended in the aperture beam
// -------------------------------------------------------------------------
const dustCount = 170;
const dustGeometry = new THREE.BufferGeometry();
const dustPositions = new Float32Array(dustCount * 3);
const dustSeeds = [];
for (let i = 0; i < dustCount; i++) {
dustSeeds.push({
apertureX: (Math.random() - 0.5) * 2.55,
apertureY: 1.36 + Math.random() * 2.05,
depth: 0.25 + Math.random() * 3.1,
drift: Math.random() * Math.PI * 2
});
}
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dustMaterial = new THREE.PointsMaterial({
color: 0xffdf9e,
size: 0.026,
transparent: true,
opacity: 0.42,
depthWrite: false,
blending: THREE.AdditiveBlending,
sizeAttenuation: true
});
const dust = new THREE.Points(dustGeometry, dustMaterial);
dust.frustumCulled = false;
scene.add(dust);
// -------------------------------------------------------------------------
// Custom camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.25, 0.82);
const spherical = new THREE.Spherical(8.2, 1.24, 0.56);
const cameraOffset = new THREE.Vector3();
function updateCamera() {
cameraOffset.setFromSpherical(spherical);
camera.position.copy(cameraTarget).add(cameraOffset);
camera.lookAt(cameraTarget);
}
updateCamera();
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
spherical.theta -= deltaX * 0.005;
spherical.phi = THREE.MathUtils.clamp(
spherical.phi + deltaY * 0.004,
0.68,
1.45
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
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();
spherical.radius = THREE.MathUtils.clamp(
spherical.radius + event.deltaY * 0.007,
5.7,
11.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
spherical.set(8.2, 1.24, 0.56);
updateCamera();
});
// -------------------------------------------------------------------------
// Sun-path UI
// -------------------------------------------------------------------------
const slider = document.getElementById("sunPath");
const playToggle = document.getElementById("playToggle");
const status = document.getElementById("status");
const statusText = document.getElementById("statusText");
let phase = THREE.MathUtils.degToRad(Number(slider.value));
let playing = true;
function updatePlaybackUI() {
playToggle.textContent = playing ? "Ⅱ Pause" : "▶ Play";
playToggle.setAttribute(
"aria-label",
playing ? "Pause sun animation" : "Play sun animation"
);
status.classList.toggle("paused", !playing);
statusText.textContent = playing ? "Live sun path" : "Sun paused";
}
playToggle.addEventListener("click", () => {
playing = !playing;
updatePlaybackUI();
});
slider.addEventListener("input", () => {
phase = THREE.MathUtils.degToRad(Number(slider.value));
playing = false;
updatePlaybackUI();
});
updatePlaybackUI();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function updateSun(time) {
const drift = Math.sin(time * 0.37) * 0.018;
// The source follows an elliptical path. Its fixed target keeps the
// directional rays parallel while the aperture projects a moving patch.
sunlight.position.set(
Math.sin(phase) * 6.5,
4.9 + Math.cos(phase) * 1.8,
-5.5
);
sunlight.target.position.copy(lightTarget);
sunlight.target.updateMatrixWorld();
sunlight.updateMatrixWorld();
sunDisk.position.x = sunlight.position.x * 0.23 + drift;
sunDisk.position.y =
2.72 + (sunlight.position.y - 4.9) * 0.12;
if (playing) {
slider.value = String(
THREE.MathUtils.radToDeg(phase).toFixed(1)
);
}
}
function updateDust(time) {
const dx = lightTarget.x - sunlight.position.x;
const dy = lightTarget.y - sunlight.position.y;
const dz = lightTarget.z - sunlight.position.z;
const attribute = dustGeometry.attributes.position;
for (let i = 0; i < dustCount; i++) {
const seed = dustSeeds[i];
const depth = seed.depth + Math.sin(time * 0.35 + seed.drift) * 0.035;
const scale = depth / dz;
attribute.setXYZ(
i,
seed.apertureX + dx * scale + Math.sin(time * 0.22 + seed.drift) * 0.018,
seed.apertureY + dy * scale + Math.cos(time * 0.28 + seed.drift) * 0.014,
depth
);
}
attribute.needsUpdate = true;
dustMaterial.opacity = 0.36 + Math.sin(time * 0.6) * 0.055;
}
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
if (playing) {
phase = (phase + delta * 0.22) % (Math.PI * 2);
}
updateSun(time);
updateDust(time);
renderer.render(scene, camera);
}
animate();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = window.innerWidth < 700 ? 49 : 40;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```