Prompt specification
Build or customize the Three.js 3D scene "Horizontal louvers under two sun angles" 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: Horizontal louvers under two sun angles
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/horizontal-louvers-under-two-sun-angles-x2194
Scene Description Prompt:
Horizontal louvers under two sun angles. Create one louvered window and switch between high and low sun positions. Keep louver spacing fixed so the change in admitted light follows only the illumination geometry. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (horizontal-louvers-under-two-sun-angles-x2194):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Horizontal Louvers — Sun Geometry Study</title>
<style>
:root {
color-scheme: dark;
--bg: #07141b;
--panel: rgba(9, 24, 32, 0.78);
--line: rgba(219, 237, 240, 0.14);
--text: #eef7f7;
--muted: #9fb3b8;
--sun: #ffc66d;
--cyan: #83d7d2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: 30px;
left: 32px;
max-width: 430px;
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.5);
}
.eyebrow {
margin-bottom: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(30px, 4vw, 54px);
font-weight: 620;
line-height: 0.98;
letter-spacing: -0.045em;
}
header p {
max-width: 350px;
margin: 15px 0 0;
color: #c0d0d2;
font-size: 14px;
line-height: 1.55;
}
.panel {
position: absolute;
top: 28px;
right: 28px;
width: min(330px, calc(100vw - 40px));
padding: 19px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(18px) saturate(120%);
pointer-events: auto;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.live-dot {
display: inline-flex;
align-items: center;
gap: 7px;
color: #c6d8da;
letter-spacing: 0.08em;
}
.live-dot::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--sun);
box-shadow: 0 0 12px rgba(255, 198, 109, 0.8);
content: "";
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
button {
border: 0;
color: inherit;
font: inherit;
cursor: pointer;
}
.sun-button {
min-height: 68px;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
text-align: left;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
.sun-button:hover {
transform: translateY(-1px);
border-color: rgba(255, 198, 109, 0.36);
}
.sun-button.active {
border-color: rgba(255, 198, 109, 0.65);
background: rgba(255, 198, 109, 0.12);
}
.sun-button strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
letter-spacing: 0.1em;
}
.sun-button span {
color: var(--muted);
font-size: 12px;
}
.sun-button.active span {
color: var(--sun);
}
.angle-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
gap: 14px;
margin-top: 18px;
padding-top: 16px;
border-top: 1px solid var(--line);
}
.angle-label {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.angle-value {
margin-top: 3px;
font-size: 29px;
font-weight: 580;
letter-spacing: -0.04em;
}
.angle-value small {
color: var(--muted);
font-size: 12px;
font-weight: 500;
letter-spacing: 0;
}
.angle-track {
position: relative;
width: 125px;
height: 5px;
margin-bottom: 7px;
border-radius: 10px;
background: linear-gradient(
90deg,
rgba(255, 198, 109, 0.3),
rgba(255, 198, 109, 0.95)
);
}
.angle-track::before,
.angle-track::after {
position: absolute;
top: 11px;
color: #789096;
font-size: 9px;
}
.angle-track::before {
left: 0;
content: "0°";
}
.angle-track::after {
right: 0;
content: "90°";
}
#angleNeedle {
position: absolute;
top: 50%;
left: 22%;
width: 13px;
height: 13px;
border: 2px solid #fff3d5;
border-radius: 50%;
background: var(--sun);
box-shadow: 0 0 14px rgba(255, 198, 109, 0.75);
transform: translate(-50%, -50%);
transition: left 80ms linear;
}
.meter {
margin-top: 23px;
padding-top: 17px;
border-top: 1px solid var(--line);
}
.meter-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
.meter-heading span {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#admittedValue {
font-size: 26px;
font-weight: 600;
letter-spacing: -0.04em;
}
.meter-track {
height: 7px;
margin-top: 10px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.08);
}
#meterFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #ef9e4c, #ffd98b);
box-shadow: 0 0 18px rgba(255, 198, 109, 0.48);
transition: width 80ms linear;
}
.meter-note {
margin-top: 8px;
color: #82999e;
font-size: 10px;
line-height: 1.45;
}
.assembly-note {
position: absolute;
bottom: 28px;
left: 32px;
max-width: 390px;
padding: 14px 16px;
border-left: 2px solid var(--cyan);
background: linear-gradient(90deg, rgba(7, 20, 27, 0.88), transparent);
text-shadow: 0 2px 12px #000;
}
.assembly-note strong {
display: block;
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.assembly-note span {
color: #c0d0d2;
font-size: 12px;
line-height: 1.5;
}
.camera-hint {
position: absolute;
right: 30px;
bottom: 29px;
display: flex;
align-items: center;
gap: 10px;
color: #a9bdc1;
font-size: 11px;
text-shadow: 0 2px 12px #000;
}
.camera-hint button {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(9, 24, 32, 0.7);
color: #d7e5e6;
font-size: 10px;
pointer-events: auto;
}
.camera-hint button:hover {
border-color: rgba(131, 215, 210, 0.5);
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 10;
background: var(--bg);
color: var(--muted);
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 400ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
header {
top: 20px;
left: 20px;
max-width: calc(100vw - 40px);
}
header p {
display: none;
}
.panel {
top: auto;
right: 16px;
bottom: 18px;
width: calc(100vw - 32px);
padding: 14px;
}
.panel-title,
.angle-row,
.meter-note {
display: none;
}
.segmented {
margin-bottom: 13px;
}
.sun-button {
min-height: 51px;
padding: 9px 12px;
}
.meter {
margin-top: 0;
padding-top: 12px;
}
.assembly-note {
top: 105px;
bottom: auto;
left: 20px;
max-width: calc(100vw - 40px);
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="ui">
<header>
<div class="eyebrow">Solar Geometry Study / 01</div>
<h1>Horizontal<br>Louvers</h1>
<p>
One fixed voxel louver assembly, illuminated from two sun positions.
Only the light direction changes.
</p>
</header>
<section class="panel" aria-label="Sun position controls">
<div class="panel-title">
Sun position
<span class="live-dot" id="sunState">Low sun</span>
</div>
<div class="segmented">
<button
class="sun-button"
id="highButton"
type="button"
aria-pressed="false"
>
<strong>HIGH SUN</strong>
<span>58° elevation</span>
</button>
<button
class="sun-button active"
id="lowButton"
type="button"
aria-pressed="true"
>
<strong>LOW SUN</strong>
<span>20° elevation</span>
</button>
</div>
<div class="angle-row">
<div>
<div class="angle-label">Current elevation</div>
<div class="angle-value">
<span id="elevationValue">20°</span>
<small> / 0° azimuth</small>
</div>
</div>
<div>
<div class="angle-track"><i id="angleNeedle"></i></div>
</div>
</div>
<div class="meter" role="progressbar" aria-label="Admitted light"
aria-valuemin="0" aria-valuemax="100" aria-valuenow="29">
<div class="meter-heading">
<span>Admitted light</span>
<strong id="admittedValue">29%</strong>
</div>
<div class="meter-track"><div id="meterFill"></div></div>
<div class="meter-note">
Parallel-ray estimate through the unchanged blade array.
</div>
</div>
</section>
<div class="assembly-note">
<strong>Assembly locked</strong>
<span>
9 voxel blades · 0.50 m pitch · 0.64 m depth<br>
Geometry stays fixed while the sun moves.
</span>
</div>
<div class="camera-hint">
<span>Drag to orbit · Scroll to zoom</span>
<button id="resetView" type="button">Reset view</button>
</div>
</div>
<div id="loading">Building voxel study…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene and renderer
// ------------------------------------------------------------
const host = document.getElementById("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07141b);
scene.fog = new THREE.Fog(0x07141b, 18, 39);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
// ------------------------------------------------------------
// Fixed lighting. The directional sun is the only moving light.
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xbdd9ed,
0x251b15,
0.85
);
scene.add(hemisphere);
const ambient = new THREE.AmbientLight(0xffffff, 0.12);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xffd29a, 4.2);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 38;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
// ------------------------------------------------------------
// Louver geometry constants: these never change with sun mode.
// ------------------------------------------------------------
const CELL = 0.5;
const WALL_COLUMNS = 22;
const WALL_ROWS = 16;
const APERTURE_BOTTOM = 1.5;
const APERTURE_TOP = 5.5;
const LOUVER_COUNT = 9;
const LOUVER_PITCH = 0.5;
const LOUVER_THICKNESS = 0.12;
const LOUVER_DEPTH = 0.64;
const LOUVER_CENTERS = Array.from(
{ length: LOUVER_COUNT },
(_, i) => APERTURE_BOTTOM + i * LOUVER_PITCH
);
// ------------------------------------------------------------
// Instanced voxel construction
// ------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function hash(value) {
const x = Math.sin(value * 127.1 + 311.7) * 43758.5453123;
return x - Math.floor(x);
}
function makeInstances(blocks, material, options = {}) {
const {
castShadow = true,
receiveShadow = true,
renderOrder = 0
} = options;
const mesh = new THREE.InstancedMesh(
unitBox,
material,
blocks.length
);
blocks.forEach((block, index) => {
dummy.position.set(
block.x,
block.y,
block.z ?? 0
);
dummy.rotation.set(
block.rx ?? 0,
block.ry ?? 0,
block.rz ?? 0
);
dummy.scale.set(
block.sx,
block.sy,
block.sz
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (block.color !== undefined) {
mesh.setColorAt(index, new THREE.Color(block.color));
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
mesh.renderOrder = renderOrder;
scene.add(mesh);
return mesh;
}
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
metalness: 0.04
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.66,
metalness: 0.12
});
const louverMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.62,
metalness: 0.16
});
const wallPalette = [
0x857767,
0x91816e,
0x776b5e,
0x9b8872,
0x807361
];
const floorPalette = [
0x27343a,
0x2d3a3f,
0x223038,
0x344146
];
const framePalette = [
0x17272d,
0x20343a,
0x1b2d33
];
const louverPalette = [
0x294149,
0x35545b,
0x223940,
0x3c5960
];
// Main wall, built from individual visible blocks.
const wallBlocks = [];
let wallIndex = 0;
for (let row = 0; row < WALL_ROWS; row++) {
for (let column = 0; column < WALL_COLUMNS; column++) {
const x = (column - (WALL_COLUMNS - 1) / 2) * CELL;
const y = (row + 0.5) * CELL;
// Leave one exact rectangular opening.
if (
Math.abs(x) < 2 &&
y > APERTURE_BOTTOM &&
y < APERTURE_TOP
) {
continue;
}
const tint = hash(wallIndex++);
wallBlocks.push({
x,
y,
z: 0,
sx: 0.48,
sy: 0.48,
sz: 0.46,
color: wallPalette[Math.floor(tint * wallPalette.length)]
});
}
}
makeInstances(wallBlocks, wallMaterial);
// Interior floor, also made from visible square blocks.
const floorBlocks = [];
let floorIndex = 0;
for (let row = 0; row < 28; row++) {
for (let column = 0; column < WALL_COLUMNS; column++) {
const x = (column - (WALL_COLUMNS - 1) / 2) * CELL;
const z = -5.75 + row * CELL;
const tint = hash(floorIndex++);
floorBlocks.push({
x,
y: -0.12,
z,
sx: 0.48,
sy: 0.24,
sz: 0.48,
color: floorPalette[Math.floor(tint * floorPalette.length)]
});
}
}
makeInstances(floorBlocks, floorMaterial, {
castShadow: false,
receiveShadow: true
});
// Rear wall gives the transmitted light a visible projection surface.
const rearWallBlocks = [];
let rearIndex = 0;
for (let row = 0; row < 14; row++) {
for (let column = 0; column < WALL_COLUMNS; column++) {
const x = (column - (WALL_COLUMNS - 1) / 2) * CELL;
const y = (row + 0.5) * CELL;
const tint = hash(rearIndex++);
rearWallBlocks.push({
x,
y,
z: -5.75,
sx: 0.48,
sy: 0.48,
sz: 0.42,
color: new THREE.Color(
wallPalette[Math.floor(tint * wallPalette.length)]
).multiplyScalar(0.52).getHex()
});
}
}
makeInstances(rearWallBlocks, wallMaterial);
// Window frame: stacked voxel blocks.
const frameBlocks = [];
let frameIndex = 0;
for (const side of [-1, 1]) {
for (let i = 0; i < 16; i++) {
frameBlocks.push({
x: side * 2.125,
y: 1.625 + i * 0.25,
z: 0,
sx: 0.25,
sy: 0.24,
sz: 0.74,
color: framePalette[frameIndex++ % framePalette.length]
});
}
}
for (const y of [1.375, 5.625]) {
for (let i = 0; i < 16; i++) {
frameBlocks.push({
x: -1.875 + i * 0.25,
y,
z: 0,
sx: 0.24,
sy: 0.25,
sz: 0.74,
color: framePalette[frameIndex++ % framePalette.length]
});
}
}
// Projecting sill, still assembled from blocks.
for (let i = 0; i < 8; i++) {
frameBlocks.push({
x: -1.75 + i * 0.5,
y: 1.18,
z: 0.12,
sx: 0.48,
sy: 0.24,
sz: 1.0,
color: framePalette[frameIndex++ % framePalette.length]
});
}
makeInstances(frameBlocks, frameMaterial);
// Nine fixed horizontal louvers, each visibly assembled from blocks.
const louverBlocks = [];
let louverIndex = 0;
for (let row = 0; row < LOUVER_COUNT; row++) {
for (let segment = 0; segment < 8; segment++) {
louverBlocks.push({
x: -1.75 + segment * 0.5,
y: LOUVER_CENTERS[row],
z: 0,
sx: 0.48,
sy: LOUVER_THICKNESS,
sz: LOUVER_DEPTH,
color: louverPalette[louverIndex++ % louverPalette.length]
});
}
}
makeInstances(louverBlocks, louverMaterial);
// ------------------------------------------------------------
// Geometric transmission calculation
//
// For parallel rays at elevation E, the vertical shadow projected
// by a horizontal blade is:
//
// tan(E) * bladeDepth + bladeThickness
//
// Merging those fixed blade shadows reveals the open ray intervals.
// This is used only to display the estimate and to place the visible
// voxel light particles. It does not alter the louver assembly.
// ------------------------------------------------------------
function getAdmittedIntervals(elevationDegrees) {
const elevation = THREE.MathUtils.degToRad(elevationDegrees);
const projectedBladeHeight =
Math.tan(elevation) * LOUVER_DEPTH + LOUVER_THICKNESS;
const blocked = LOUVER_CENTERS.map((center) => ({
min: center - projectedBladeHeight / 2,
max: center + projectedBladeHeight / 2
})).sort((a, b) => a.min - b.min);
const merged = [];
for (const interval of blocked) {
const last = merged[merged.length - 1];
if (!last || interval.min > last.max) {
merged.push({ ...interval });
} else {
last.max = Math.max(last.max, interval.max);
}
}
const admitted = [];
let cursor = APERTURE_BOTTOM;
for (const interval of merged) {
if (interval.min > cursor) {
admitted.push({
min: cursor,
max: Math.min(interval.min, APERTURE_TOP)
});
}
cursor = Math.max(cursor, interval.max);
if (cursor >= APERTURE_TOP) {
break;
}
}
if (cursor < APERTURE_TOP) {
admitted.push({
min: cursor,
max: APERTURE_TOP
});
}
return admitted.filter(
(interval) => interval.max - interval.min > 0.012
);
}
// ------------------------------------------------------------
// Voxel light particles
// ------------------------------------------------------------
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0xffd27c,
transparent: true,
opacity: 0.44,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const beamMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
beamMaterial,
800
);
beamMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
beamMesh.count = 0;
beamMesh.frustumCulled = false;
beamMesh.renderOrder = 3;
scene.add(beamMesh);
function updateLightParticles(elevationDegrees, time) {
const intervals = getAdmittedIntervals(elevationDegrees);
const tangent = Math.tan(
THREE.MathUtils.degToRad(elevationDegrees)
);
let instance = 0;
intervals.forEach((interval, intervalIndex) => {
const centerY = (interval.min + interval.max) / 2;
const laneCount = 6;
const particlesPerLane = 14;
for (let lane = 0; lane < laneCount; lane++) {
const x = -1.6 + lane * 0.64;
for (let particle = 0; particle < particlesPerLane; particle++) {
const progress = (
time * 0.105 +
particle / particlesPerLane +
lane * 0.071 +
intervalIndex * 0.037
) % 1;
const z = 4.8 - progress * 10.8;
const y = centerY + tangent * z;
dummy.position.set(x, y, z);
dummy.rotation.set(0, 0, 0);
const pulse = 0.055 + 0.018 * Math.sin(
time * 4 + particle * 0.8 + lane
);
dummy.scale.set(pulse, pulse, pulse);
dummy.updateMatrix();
beamMesh.setMatrixAt(instance, dummy.matrix);
instance++;
}
}
});
beamMesh.count = instance;
beamMesh.instanceMatrix.needsUpdate = true;
}
// ------------------------------------------------------------
// Camera controls implemented directly, without addons
// ------------------------------------------------------------
const controls = {
target: new THREE.Vector3(0, 3.0, -0.3),
radius: 14.5,
theta: 0.32,
phi: 1.20
};
const initialControls = {
radius: controls.radius,
theta: controls.theta,
phi: controls.phi
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y +
controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function resetCamera() {
controls.radius = initialControls.radius;
controls.theta = initialControls.theta;
controls.phi = initialControls.phi;
updateCamera();
}
const canvas = renderer.domElement;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
controls.theta = THREE.MathUtils.clamp(
controls.theta - dx * 0.005,
-1.15,
1.15
);
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.0045,
0.68,
1.48
);
updateCamera();
});
function endDrag(event) {
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),
8.5,
23
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetView").addEventListener(
"click",
resetCamera
);
updateCamera();
// ------------------------------------------------------------
// Sun mode controls and interface
// ------------------------------------------------------------
const highButton = document.getElementById("highButton");
const lowButton = document.getElementById("lowButton");
const sunState = document.getElementById("sunState");
const elevationValue = document.getElementById("elevationValue");
const admittedValue = document.getElementById("admittedValue");
const meterFill = document.getElementById("meterFill");
const angleNeedle = document.getElementById("angleNeedle");
const meter = document.querySelector(".meter");
const HIGH_ELEVATION = 58;
const LOW_ELEVATION = 20;
let currentElevation = LOW_ELEVATION;
let targetElevation = LOW_ELEVATION;
const reducedMotion = matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function setSunMode(mode) {
targetElevation = mode === "high"
? HIGH_ELEVATION
: LOW_ELEVATION;
const high = mode === "high";
highButton.classList.toggle("active", high);
lowButton.classList.toggle("active", !high);
highButton.setAttribute("aria-pressed", String(high));
lowButton.setAttribute("aria-pressed", String(!high));
sunState.textContent = high ? "High sun" : "Low sun";
}
highButton.addEventListener("click", () => setSunMode("high"));
lowButton.addEventListener("click", () => setSunMode("low"));
document.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "h") {
setSunMode("high");
}
if (event.key.toLowerCase() === "l") {
setSunMode("low");
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
function updateInterface() {
const intervals = getAdmittedIntervals(currentElevation);
const admittedLength = intervals.reduce(
(sum, interval) => sum + interval.max - interval.min,
0
);
const apertureHeight = APERTURE_TOP - APERTURE_BOTTOM;
const percent = THREE.MathUtils.clamp(
admittedLength / apertureHeight * 100,
0,
100
);
elevationValue.textContent =
`${Math.round(currentElevation)}°`;
admittedValue.textContent = `${Math.round(percent)}%`;
meterFill.style.width = `${percent}%`;
angleNeedle.style.left = `${currentElevation / 90 * 100}%`;
meter.setAttribute("aria-valuenow", String(Math.round(percent)));
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
currentElevation = reducedMotion
? targetElevation
: THREE.MathUtils.damp(
currentElevation,
targetElevation,
5.0,
dt
);
const elevation = THREE.MathUtils.degToRad(currentElevation);
const sunDistance = 14;
// The sun moves on a fixed 0° azimuth arc. Intensity does not
// change, so the visual difference comes from illumination geometry.
sun.position.set(
0,
3 + sunDistance * Math.sin(elevation),
sunDistance * Math.cos(elevation)
);
sun.target.position.set(0, 3, 0);
sun.target.updateMatrixWorld();
updateLightParticles(currentElevation, time);
updateInterface();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
setSunMode("low");
updateInterface();
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("hidden");
});
</script>
</body>
</html>
```