Prompt specification
Build or customize the Three.js 3D scene "Rotate louvers to meet a light target" 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: Rotate louvers to meet a light target
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rotate-louvers-to-meet-a-light-target-x2196
Scene Description Prompt:
Let users adjust louver angles while viewing direct light on a desk plane. Show the tradeoff between blocking sunlight and reducing the visible opening to the outside. Build this with Three.js from cdnjs as a low-poly scene in one HTML file.
Working Three.js Starter (rotate-louvers-to-meet-a-light-target-x2196):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Louver Lab — Sunlight vs. View</title>
<style>
:root {
--ink: #12212b;
--muted: #60717c;
--panel: rgba(250, 252, 250, 0.92);
--line: rgba(25, 48, 61, 0.13);
--teal: #168f8b;
--blue: #4384c5;
--amber: #e9a43a;
--green: #36a77b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9dce7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(12, 32, 43, 0.12), transparent 25%),
linear-gradient(0deg, rgba(12, 32, 43, 0.13), transparent 22%);
}
.brand {
position: fixed;
top: 26px;
left: 28px;
pointer-events: none;
text-shadow: 0 1px 20px rgba(255, 255, 255, 0.8);
}
.brand h1 {
margin: 0;
font-size: clamp(25px, 3vw, 42px);
line-height: 0.95;
letter-spacing: -0.055em;
font-weight: 800;
}
.brand p {
margin: 9px 0 0;
color: #304b58;
font-size: 13px;
font-weight: 600;
letter-spacing: 0.01em;
}
.live-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 13px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
background: rgba(255, 255, 255, 0.58);
backdrop-filter: blur(12px);
color: #31505c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
}
.live-badge::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(54, 167, 123, 0.15);
}
.panel {
position: fixed;
top: 24px;
right: 24px;
width: min(340px, calc(100vw - 32px));
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 22px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(25, 47, 58, 0.19);
backdrop-filter: blur(20px) saturate(1.12);
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 18px;
}
.panel-head h2 {
margin: 0;
font-size: 16px;
letter-spacing: -0.02em;
}
.angle-number {
min-width: 65px;
padding: 7px 9px;
border-radius: 12px;
background: #173f49;
color: white;
text-align: center;
font-variant-numeric: tabular-nums;
font-size: 18px;
font-weight: 800;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 10px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.control-label strong {
color: var(--ink);
font-size: 12px;
letter-spacing: 0;
text-transform: none;
}
input[type="range"] {
--pct: 40%;
width: 100%;
height: 7px;
margin: 4px 0 8px;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--teal) 0,
var(--teal) var(--pct),
rgba(22, 53, 64, 0.14) var(--pct),
rgba(22, 53, 64, 0.14) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 21px;
height: 21px;
border: 4px solid white;
border-radius: 50%;
appearance: none;
background: var(--teal);
box-shadow: 0 2px 8px rgba(18, 43, 53, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 4px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(18, 43, 53, 0.28);
}
.range-scale {
display: flex;
justify-content: space-between;
color: #72838b;
font-size: 10px;
font-weight: 650;
}
.divider {
height: 1px;
margin: 18px 0;
background: var(--line);
}
.metric-row {
display: grid;
grid-template-columns: 1fr auto;
align-items: baseline;
gap: 8px;
margin: 15px 0 7px;
font-size: 12px;
font-weight: 750;
}
.metric-row span {
display: inline-flex;
align-items: center;
gap: 8px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
}
.dot.sun {
background: var(--amber);
}
.dot.view {
background: var(--blue);
}
.metric-row strong {
font-size: 15px;
font-variant-numeric: tabular-nums;
}
.bar {
height: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(20, 45, 57, 0.09);
}
.bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 120ms ease-out;
}
.bar.sun i {
background: linear-gradient(90deg, #f1bc5a, #dc8f28);
}
.bar.view i {
background: linear-gradient(90deg, #72acd9, #3675b6);
}
.direct-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 15px;
padding: 11px 12px;
border: 1px solid rgba(54, 167, 123, 0.18);
border-radius: 12px;
background: rgba(54, 167, 123, 0.07);
color: #41665a;
font-size: 11px;
font-weight: 700;
}
.direct-row strong {
color: var(--green);
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.tradeoff-copy {
min-height: 52px;
margin: 14px 0 0;
color: #566a74;
font-size: 12px;
line-height: 1.5;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 13px;
}
button {
border: 1px solid var(--line);
border-radius: 11px;
padding: 10px 11px;
background: rgba(255, 255, 255, 0.72);
color: #27434e;
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(22, 143, 139, 0.38);
background: white;
}
button:active {
transform: translateY(0);
}
.views {
position: fixed;
left: 50%;
bottom: 24px;
display: flex;
gap: 4px;
padding: 5px;
transform: translateX(-50%);
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 14px;
background: rgba(250, 252, 250, 0.83);
box-shadow: 0 12px 35px rgba(25, 47, 58, 0.16);
backdrop-filter: blur(15px);
}
.views button {
min-width: 78px;
border: 0;
background: transparent;
}
.views button.active {
background: #173f49;
color: white;
}
.hint {
position: fixed;
left: 28px;
bottom: 27px;
color: rgba(20, 45, 57, 0.72);
font-size: 11px;
font-weight: 700;
pointer-events: none;
text-shadow: 0 1px 10px white;
}
@media (max-width: 760px) {
.brand {
top: 18px;
left: 18px;
}
.brand h1 {
font-size: 27px;
}
.brand p,
.live-badge {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 74px;
width: min(330px, calc(100vw - 24px));
padding: 15px;
}
.panel-head {
margin-bottom: 12px;
}
.divider {
margin: 12px 0;
}
.metric-row {
margin-top: 10px;
}
.tradeoff-copy {
min-height: 0;
}
.views {
bottom: 12px;
}
.views button {
min-width: 68px;
padding: 9px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive louver and sunlight simulation"></canvas>
<div class="vignette"></div>
<header class="brand">
<h1>Louver Lab</h1>
<p>Balance direct sunlight with a view of the outside.</p>
<div class="live-badge">LIVE SHADOW SIMULATION</div>
</header>
<aside class="panel">
<div class="panel-head">
<h2>Louver angle</h2>
<div class="angle-number" id="angleValue">24°</div>
</div>
<div class="control-label">
<span>Adjust the blades</span>
<strong id="angleState">Balanced</strong>
</div>
<input
id="angleInput"
type="range"
min="0"
max="60"
step="1"
value="24"
aria-label="Louver angle"
/>
<div class="range-scale">
<span>0° · broad view</span>
<span>60° · steep</span>
</div>
<div class="divider"></div>
<div class="metric-row">
<span><i class="dot sun"></i>Sun blocked</span>
<strong id="sunBlockedValue">—</strong>
</div>
<div class="bar sun"><i id="sunBar"></i></div>
<div class="metric-row">
<span><i class="dot view"></i>Outside visible</span>
<strong id="outsideValue">—</strong>
</div>
<div class="bar view"><i id="outsideBar"></i></div>
<div class="direct-row">
<span>Direct sun reaching desk</span>
<strong id="directValue">—</strong>
</div>
<p class="tradeoff-copy" id="tradeoffCopy">
Adjust the louvers to compare the light on the desk with the remaining exterior view.
</p>
<div class="button-row">
<button id="bestSunButton" type="button">Best sunlight</button>
<button id="bestViewButton" type="button">Best view</button>
</div>
</aside>
<nav class="views" aria-label="Camera presets">
<button class="active" data-view="room" type="button">Room</button>
<button data-view="desk" type="button">Desk</button>
<button data-view="window" type="button">Window</button>
<button data-view="reset" type="button">Reset</button>
</nav>
<div class="hint">Drag to orbit · Scroll to zoom · Golden arrow shows sun direction</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8dce8);
scene.fog = new THREE.Fog(0xb8dce8, 18, 42);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.05,
100
);
const clock = new THREE.Clock();
const ui = {
angleInput: document.querySelector("#angleInput"),
angleValue: document.querySelector("#angleValue"),
angleState: document.querySelector("#angleState"),
sunBlockedValue: document.querySelector("#sunBlockedValue"),
outsideValue: document.querySelector("#outsideValue"),
directValue: document.querySelector("#directValue"),
sunBar: document.querySelector("#sunBar"),
outsideBar: document.querySelector("#outsideBar"),
tradeoffCopy: document.querySelector("#tradeoffCopy"),
bestSunButton: document.querySelector("#bestSunButton"),
bestViewButton: document.querySelector("#bestViewButton")
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
flatShading: true,
...options
});
}
const materials = {
floor: standardMaterial(0xd9c8aa),
wall: standardMaterial(0xe9e6dc),
wallSide: standardMaterial(0xd9ded9),
frame: standardMaterial(0xf3f0e6),
louver: standardMaterial(0x4f7778, {
roughness: 0.55,
metalness: 0.18,
side: THREE.DoubleSide
}),
desk: standardMaterial(0xb97e4e),
deskEdge: standardMaterial(0x8d5d3c),
dark: standardMaterial(0x263b42),
outsideGround: standardMaterial(0x7ea978),
mountain: standardMaterial(0x6f8f82),
tree: standardMaterial(0x4e8064),
trunk: standardMaterial(0x75553a),
paper: standardMaterial(0xf6f1df),
pot: standardMaterial(0xc56f4d)
};
function addBox(
width,
height,
depth,
x,
y,
z,
material,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow ?? true;
mesh.receiveShadow = options.receiveShadow ?? true;
if (options.parent) {
options.parent.add(mesh);
} else {
scene.add(mesh);
}
return mesh;
}
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xdff4ff, 0x8e785d, 1.55);
scene.add(hemisphere);
const fillLight = new THREE.DirectionalLight(0xcde7ef, 0.7);
fillLight.position.set(5, 4, 7);
scene.add(fillLight);
const sun = new THREE.DirectionalLight(0xffd98f, 3.8);
sun.position.set(-1.8, 6.0, -11.0);
sun.target.position.set(-0.35, 0.65, -0.05);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -4;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.018;
scene.add(sun);
scene.add(sun.target);
const sunVector = new THREE.Vector3()
.subVectors(sun.position, sun.target.position)
.normalize();
const sunTravelDirection = sunVector.clone().negate();
const sunArrow = new THREE.ArrowHelper(
sunTravelDirection,
new THREE.Vector3(-0.8, 3.25, -4.2),
2.45,
0xffd77b,
0.34,
0.19
);
scene.add(sunArrow);
// -------------------------------------------------------------------------
// Room and window opening
// -------------------------------------------------------------------------
const opening = {
left: -2.4,
right: 2.4,
bottom: 0.85,
top: 3.4,
z: -2.7
};
const roomFloor = new THREE.Mesh(
new THREE.PlaneGeometry(12, 12),
materials.floor
);
roomFloor.rotation.x = -Math.PI / 2;
roomFloor.position.y = -0.01;
roomFloor.receiveShadow = true;
scene.add(roomFloor);
// Back wall built around the aperture so sunlight can pass through it.
const wallZ = opening.z;
const wallDepth = 0.18;
const wallLeft = -5;
const wallRight = 5;
const wallTop = 5;
const sunBlockers = [];
const wallLeftPiece = addBox(
opening.left - wallLeft,
wallTop,
wallDepth,
(wallLeft + opening.left) / 2,
wallTop / 2,
wallZ,
materials.wall
);
const wallRightPiece = addBox(
wallRight - opening.right,
wallTop,
wallDepth,
(opening.right + wallRight) / 2,
wallTop / 2,
wallZ,
materials.wall
);
const wallBottomPiece = addBox(
opening.right - opening.left,
opening.bottom,
wallDepth,
0,
opening.bottom / 2,
wallZ,
materials.wall
);
const wallTopPiece = addBox(
opening.right - opening.left,
wallTop - opening.top,
wallDepth,
0,
(opening.top + wallTop) / 2,
wallZ,
materials.wall
);
sunBlockers.push(
wallLeftPiece,
wallRightPiece,
wallBottomPiece,
wallTopPiece
);
addBox(
0.16,
4.6,
9,
-5,
2.3,
0.7,
materials.wallSide
);
// Window frame.
const frameZ = -2.55;
const frameLeft = addBox(
0.13,
2.65,
0.23,
opening.left - 0.02,
(opening.bottom + opening.top) / 2,
frameZ,
materials.frame
);
const frameRight = addBox(
0.13,
2.65,
0.23,
opening.right + 0.02,
(opening.bottom + opening.top) / 2,
frameZ,
materials.frame
);
const frameTop = addBox(
5.05,
0.13,
0.23,
0,
opening.top + 0.02,
frameZ,
materials.frame
);
const frameBottom = addBox(
5.05,
0.13,
0.23,
0,
opening.bottom - 0.02,
frameZ,
materials.frame
);
const sill = addBox(
5.25,
0.13,
0.46,
0,
0.77,
-2.42,
materials.frame
);
sunBlockers.push(frameLeft, frameRight, frameTop, frameBottom, sill);
// -------------------------------------------------------------------------
// Louver blades
// -------------------------------------------------------------------------
const louverGroup = new THREE.Group();
scene.add(louverGroup);
const louvers = [];
const louverCount = 8;
const louverWidth = 4.5;
const louverThickness = 0.065;
const louverDepth = 0.36;
const louverZ = -2.52;
for (let i = 0; i < louverCount; i++) {
const blade = new THREE.Mesh(
new THREE.BoxGeometry(
louverWidth,
louverThickness,
louverDepth
),
materials.louver
);
blade.position.set(0, 1.03 + i * 0.31, louverZ);
blade.castShadow = true;
blade.receiveShadow = true;
blade.userData.baseY = blade.position.y;
louverGroup.add(blade);
louvers.push(blade);
sunBlockers.push(blade);
}
// Small angle guide beside the window.
const guidePivot = new THREE.Vector3(2.66, 2.1, louverZ);
const guideRadius = 0.52;
const guideSegments = 32;
const guideArcGeometry = new THREE.BufferGeometry();
const guideArcPositions = new Float32Array((guideSegments + 1) * 3);
guideArcGeometry.setAttribute(
"position",
new THREE.BufferAttribute(guideArcPositions, 3)
);
const guideArc = new THREE.Line(
guideArcGeometry,
new THREE.LineBasicMaterial({ color: 0xe4a23b })
);
scene.add(guideArc);
const guideReferenceGeometry = new THREE.BufferGeometry().setFromPoints([
guidePivot,
new THREE.Vector3(
guidePivot.x,
guidePivot.y,
guidePivot.z + guideRadius
)
]);
const guideReference = new THREE.Line(
guideReferenceGeometry,
new THREE.LineBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.75
})
);
scene.add(guideReference);
const guidePivotMesh = new THREE.Mesh(
new THREE.SphereGeometry(0.045, 10, 6),
new THREE.MeshBasicMaterial({ color: 0xf1b64f })
);
guidePivotMesh.position.copy(guidePivot);
scene.add(guidePivotMesh);
function updateAngleGuide(angleDegrees) {
const angle = THREE.MathUtils.degToRad(angleDegrees);
for (let i = 0; i <= guideSegments; i++) {
const t = angle * (i / guideSegments);
const index = i * 3;
guideArcPositions[index] = guidePivot.x;
guideArcPositions[index + 1] =
guidePivot.y - Math.sin(t) * guideRadius;
guideArcPositions[index + 2] =
guidePivot.z + Math.cos(t) * guideRadius;
}
guideArcGeometry.attributes.position.needsUpdate = true;
}
function setLouverAngle(angleDegrees) {
const angle = THREE.MathUtils.degToRad(angleDegrees);
for (const blade of louvers) {
blade.rotation.x = angle;
}
scene.updateMatrixWorld(true);
updateAngleGuide(angleDegrees);
}
// -------------------------------------------------------------------------
// Exterior
// -------------------------------------------------------------------------
const outsideGround = new THREE.Mesh(
new THREE.PlaneGeometry(34, 34),
materials.outsideGround
);
outsideGround.rotation.x = -Math.PI / 2;
outsideGround.position.set(0, -0.035, -14);
outsideGround.receiveShadow = true;
scene.add(outsideGround);
function addMountain(x, z, radius, height, color) {
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 5),
standardMaterial(color)
);
mountain.position.set(x, height / 2 - 0.03, z);
mountain.rotation.y = Math.random() * Math.PI;
mountain.castShadow = true;
mountain.receiveShadow = true;
scene.add(mountain);
}
addMountain(-7.5, -16, 5.6, 6.2, 0x718d82);
addMountain(-2.2, -18, 6.5, 7.2, 0x668477);
addMountain(4.5, -17, 5.8, 5.8, 0x789486);
addMountain(10, -20, 7.5, 8.4, 0x627c72);
function addTree(x, z, scale = 1) {
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.16, 1.1, 5),
materials.trunk
);
trunk.position.y = 0.55;
trunk.castShadow = true;
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.75, 0),
materials.tree
);
crown.position.y = 1.48;
crown.scale.set(0.9, 1.25, 0.9);
crown.castShadow = true;
tree.add(trunk, crown);
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
scene.add(tree);
}
addTree(-3.6, -6.1, 0.9);
addTree(-2.1, -8.2, 1.1);
addTree(1.8, -6.6, 0.82);
addTree(3.4, -8.8, 1.12);
addTree(-5.4, -10.4, 1.3);
addTree(5.8, -11.3, 1.45);
const sunDisc = new THREE.Mesh(
new THREE.SphereGeometry(0.58, 12, 8),
new THREE.MeshBasicMaterial({
color: 0xffd66c,
toneMapped: false
})
);
sunDisc.position.set(-1.5, 3.45, -10.5);
scene.add(sunDisc);
function addCloud(x, y, z, scale) {
const cloud = new THREE.Group();
const material = standardMaterial(0xf2f6ef, {
roughness: 1,
emissive: 0x334b55,
emissiveIntensity: 0.04
});
for (let i = 0; i < 5; i++) {
const puff = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.45, 0),
material
);
puff.position.set(
(i - 2) * 0.47,
Math.sin(i * 1.7) * 0.13,
Math.cos(i) * 0.08
);
puff.scale.set(1.15, 0.65, 0.75);
cloud.add(puff);
}
cloud.position.set(x, y, z);
cloud.scale.setScalar(scale);
scene.add(cloud);
}
addCloud(-3.2, 4.6, -8.8, 0.85);
addCloud(3.8, 4.1, -11.5, 1.05);
// -------------------------------------------------------------------------
// Desk and desk details
// -------------------------------------------------------------------------
const deskCenterX = -0.35;
const deskCenterZ = -0.05;
const deskWidth = 3.8;
const deskDepth = 1.55;
const deskTop = addBox(
deskWidth,
0.12,
deskDepth,
deskCenterX,
0.72,
deskCenterZ,
materials.desk
);
addBox(
deskWidth - 0.08,
0.07,
deskDepth - 0.08,
deskCenterX,
0.635,
deskCenterZ,
materials.deskEdge
);
const legPositions = [
[deskCenterX - 1.55, deskCenterZ - 0.57],
[deskCenterX + 1.55, deskCenterZ - 0.57],
[deskCenterX - 1.55, deskCenterZ + 0.57],
[deskCenterX + 1.55, deskCenterZ + 0.57]
];
for (const [x, z] of legPositions) {
addBox(0.13, 0.64, 0.13, x, 0.31, z, materials.dark);
}
// Fine measurement grid makes the louver shadows easy to read.
const gridVertices = [];
const gridY = 0.787;
for (let x = deskCenterX - 1.8; x <= deskCenterX + 1.8; x += 0.3) {
gridVertices.push(
x, gridY, deskCenterZ - 0.7,
x, gridY, deskCenterZ + 0.7
);
}
for (let z = deskCenterZ - 0.65; z <= deskCenterZ + 0.65; z += 0.25) {
gridVertices.push(
deskCenterX - 1.82, gridY, z,
deskCenterX + 1.82, gridY, z
);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const deskGrid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x704c35,
transparent: true,
opacity: 0.24
})
);
scene.add(deskGrid);
// Notebook.
addBox(
0.72,
0.035,
0.48,
0.65,
0.805,
-0.34,
materials.paper
).rotation.y = -0.13;
// Pencil.
const pencil = new THREE.Mesh(
new THREE.CylinderGeometry(0.022, 0.022, 0.58, 6),
standardMaterial(0xe3a738)
);
pencil.rotation.z = Math.PI / 2;
pencil.rotation.y = -0.22;
pencil.position.set(0.7, 0.84, -0.04);
pencil.castShadow = true;
scene.add(pencil);
// Small polygonal plant.
const plantPot = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.13, 0.28, 6),
materials.pot
);
plantPot.position.set(-1.7, 0.92, -0.48);
plantPot.castShadow = true;
scene.add(plantPot);
const leafMaterial = standardMaterial(0x4b8b68);
for (let i = 0; i < 5; i++) {
const leaf = new THREE.Mesh(
new THREE.ConeGeometry(0.12, 0.55, 5),
leafMaterial
);
leaf.position.set(
-1.7 + Math.cos(i * 1.25) * 0.1,
1.22 + (i % 2) * 0.08,
-0.48 + Math.sin(i * 1.25) * 0.1
);
leaf.rotation.z = Math.cos(i * 1.25) * 0.32;
leaf.rotation.x = Math.sin(i * 1.25) * 0.32;
leaf.castShadow = true;
scene.add(leaf);
}
// -------------------------------------------------------------------------
// Direct-light visualization projected onto the desk
// -------------------------------------------------------------------------
const projectionCanvas = document.createElement("canvas");
projectionCanvas.width = 36;
projectionCanvas.height = 18;
const projectionContext = projectionCanvas.getContext("2d");
const projectionTexture = new THREE.CanvasTexture(projectionCanvas);
projectionTexture.colorSpace = THREE.SRGBColorSpace;
projectionTexture.minFilter = THREE.LinearFilter;
projectionTexture.magFilter = THREE.LinearFilter;
const projectionMaterial = new THREE.MeshBasicMaterial({
map: projectionTexture,
transparent: true,
opacity: 0.68,
depthWrite: false,
blending: THREE.AdditiveBlending,
toneMapped: false
});
const projectionPlane = new THREE.Mesh(
new THREE.PlaneGeometry(deskWidth - 0.06, deskDepth - 0.06),
projectionMaterial
);
projectionPlane.rotation.x = -Math.PI / 2;
projectionPlane.position.set(deskCenterX, 0.791, deskCenterZ);
projectionPlane.renderOrder = 3;
scene.add(projectionPlane);
const deskBounds = {
xMin: deskCenterX - deskWidth / 2 + 0.05,
xMax: deskCenterX + deskWidth / 2 - 0.05,
zMin: deskCenterZ - deskDepth / 2 + 0.05,
zMax: deskCenterZ + deskDepth / 2 - 0.05,
y: 0.793
};
const sunRaycaster = new THREE.Raycaster();
const sunRayOrigin = new THREE.Vector3();
const sunRayDirection = new THREE.Vector3();
function isDirectSunAt(x, z) {
sunRayOrigin
.set(x, deskBounds.y, z)
.addScaledVector(sunVector, 24);
sunRayDirection.copy(sunVector).negate();
sunRaycaster.set(sunRayOrigin, sunRayDirection);
sunRaycaster.near = 0;
sunRaycaster.far = 24;
return sunRaycaster.intersectObjects(sunBlockers, false).length === 0;
}
function projectSunOntoDesk() {
const width = projectionCanvas.width;
const height = projectionCanvas.height;
const image = projectionContext.createImageData(width, height);
let directCount = 0;
for (let py = 0; py < height; py++) {
const z =
deskBounds.zMin +
(py / Math.max(1, height - 1)) *
(deskBounds.zMax - deskBounds.zMin);
for (let px = 0; px < width; px++) {
const x =
deskBounds.xMin +
(px / Math.max(1, width - 1)) *
(deskBounds.xMax - deskBounds.xMin);
const direct = isDirectSunAt(x, z);
const index = (py * width + px) * 4;
if (direct) {
directCount++;
image.data[index] = 255;
image.data[index + 1] = 211;
image.data[index + 2] = 112;
image.data[index + 3] = 205;
} else {
image.data[index + 3] = 0;
}
}
}
projectionContext.putImageData(image, 0, 0);
projectionTexture.needsUpdate = true;
return (directCount / (width * height)) * 100;
}
function measureDirectPercentage(columns = 16, rows = 10) {
let direct = 0;
for (let row = 0; row < rows; row++) {
const z =
deskBounds.zMin +
((row + 0.5) / rows) *
(deskBounds.zMax - deskBounds.zMin);
for (let column = 0; column < columns; column++) {
const x =
deskBounds.xMin +
((column + 0.5) / columns) *
(deskBounds.xMax - deskBounds.xMin);
if (isDirectSunAt(x, z)) {
direct++;
}
}
}
return (direct / (columns * rows)) * 100;
}
// -------------------------------------------------------------------------
// Exterior visibility measured from the current camera
// -------------------------------------------------------------------------
const viewRaycaster = new THREE.Raycaster();
const viewDirection = new THREE.Vector3();
function measureVisibleOutside(columns = 22, rows = 16) {
let visible = 0;
for (let row = 0; row < rows; row++) {
const y =
opening.bottom +
((row + 0.5) / rows) *
(opening.top - opening.bottom);
for (let column = 0; column < columns; column++) {
const x =
opening.left +
((column + 0.5) / columns) *
(opening.right - opening.left);
// The target is just beyond the window, so any slat in front of it
// blocks this exterior sightline.
const target = new THREE.Vector3(x, y, wallZ - 0.08);
viewDirection.subVectors(target, camera.position);
const targetDistance = viewDirection.length();
viewDirection.normalize();
viewRaycaster.set(camera.position, viewDirection);
viewRaycaster.near = 0;
viewRaycaster.far = targetDistance;
const hits = viewRaycaster.intersectObjects(louvers, false);
if (hits.length === 0) {
visible++;
}
}
}
return (visible / (columns * rows)) * 100;
}
// -------------------------------------------------------------------------
// Camera controls, implemented without external addons
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.52,
pitch: 0.28,
distance: 8.4,
target: new THREE.Vector3(-0.25, 1.55, -0.65)
};
let viewMetricDirty = true;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function applyCamera() {
const horizontalDistance =
cameraState.distance * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
horizontalDistance * Math.sin(cameraState.yaw),
cameraState.target.y +
cameraState.distance * Math.sin(cameraState.pitch),
cameraState.target.z +
horizontalDistance * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
viewMetricDirty = true;
}
const cameraPresets = {
room: {
yaw: 0.52,
pitch: 0.28,
distance: 8.4,
target: new THREE.Vector3(-0.25, 1.55, -0.65)
},
desk: {
yaw: 0.58,
pitch: 0.43,
distance: 4.65,
target: new THREE.Vector3(-0.35, 0.93, -0.12)
},
window: {
yaw: 0.08,
pitch: 0.13,
distance: 4.7,
target: new THREE.Vector3(0, 2.05, -2.25)
}
};
function setCameraPreset(name) {
const preset = cameraPresets[name];
if (!preset) {
return;
}
cameraState.yaw = preset.yaw;
cameraState.pitch = preset.pitch;
cameraState.distance = preset.distance;
cameraState.target.copy(preset.target);
applyCamera();
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle("active", button.dataset.view === name);
});
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= deltaX * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch - deltaY * 0.004,
0.04,
0.88
);
applyCamera();
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.remove("active");
});
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.006,
2.8,
11.5
);
applyCamera();
},
{ passive: false }
);
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
const view = button.dataset.view;
if (view === "reset") {
setCameraPreset("room");
ui.angleInput.value = "24";
setLouverAngle(24);
refreshMetrics();
} else {
setCameraPreset(view);
}
});
});
// -------------------------------------------------------------------------
// Find the strongest sunlight setting
// -------------------------------------------------------------------------
function findBestSunAngle() {
let bestAngle = 0;
let bestValue = -1;
for (let angle = 0; angle <= 60; angle += 3) {
setLouverAngle(angle);
const value = measureDirectPercentage(14, 8);
if (value > bestValue) {
bestValue = value;
bestAngle = angle;
}
}
for (
let angle = Math.max(0, bestAngle - 3);
angle <= Math.min(60, bestAngle + 3);
angle++
) {
setLouverAngle(angle);
const value = measureDirectPercentage(20, 12);
if (value > bestValue) {
bestValue = value;
bestAngle = angle;
}
}
return bestAngle;
}
// -------------------------------------------------------------------------
// UI and metrics
// -------------------------------------------------------------------------
let bestSunAngle = 30;
let directPercent = 0;
let outsidePercent = 0;
function updateTradeoffCopy(angle) {
if (angle < bestSunAngle - 7) {
ui.angleState.textContent = "Open view";
ui.tradeoffCopy.textContent =
"The louvers still admit some sun, but the exterior remains easier to see. " +
"Tilt them toward the best-sun setting for a brighter desk.";
} else if (Math.abs(angle - bestSunAngle) <= 7) {
ui.angleState.textContent = "Balanced";
ui.tradeoffCopy.textContent =
"This is near the strongest direct-sun setting. " +
"The desk brightens, but each tilted blade removes more of the exterior view.";
} else {
ui.angleState.textContent = "Over-tilted";
ui.tradeoffCopy.textContent =
"Extra tilt is beginning to block the sun again while hiding more of the outside. " +
"A slightly shallower angle can improve both outcomes.";
}
}
function refreshMetrics() {
directPercent = projectSunOntoDesk();
outsidePercent = measureVisibleOutside();
const blockedPercent = 100 - directPercent;
ui.angleInput.value = String(Math.round(currentAngle));
ui.angleValue.textContent = `${Math.round(currentAngle)}°`;
ui.sunBlockedValue.textContent = `${Math.round(blockedPercent)}%`;
ui.outsideValue.textContent = `${Math.round(outsidePercent)}%`;
ui.directValue.textContent = `${Math.round(directPercent)}%`;
ui.sunBar.style.width = `${blockedPercent}%`;
ui.outsideBar.style.width = `${outsidePercent}%`;
const sliderPercent = (currentAngle / 60) * 100;
ui.angleInput.style.setProperty("--pct", `${sliderPercent}%`);
updateTradeoffCopy(currentAngle);
}
let currentAngle = 24;
ui.angleInput.addEventListener("input", () => {
currentAngle = Number(ui.angleInput.value);
setLouverAngle(currentAngle);
refreshMetrics();
});
ui.bestSunButton.addEventListener("click", () => {
currentAngle = bestSunAngle;
setLouverAngle(currentAngle);
refreshMetrics();
});
ui.bestViewButton.addEventListener("click", () => {
currentAngle = 0;
setLouverAngle(currentAngle);
refreshMetrics();
});
// -------------------------------------------------------------------------
// 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);
viewMetricDirty = true;
});
applyCamera();
// Let the scene settle, then discover the best sunlight angle.
bestSunAngle = findBestSunAngle();
ui.bestSunButton.textContent = `Best sun · ${bestSunAngle}°`;
currentAngle = Math.max(0, bestSunAngle - 4);
setLouverAngle(currentAngle);
refreshMetrics();
renderer.setAnimationLoop(() => {
if (viewMetricDirty) {
outsidePercent = measureVisibleOutside();
ui.outsideValue.textContent = `${Math.round(outsidePercent)}%`;
ui.outsideBar.style.width = `${outsidePercent}%`;
viewMetricDirty = false;
}
renderer.render(scene, camera);
});
</script>
</body>
</html>
```