Prompt specification
Build or customize the Three.js 3D scene "Office facade with varied shading bays" 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: Office facade with varied shading bays
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/office-facade-with-varied-shading-bays-x2195
Scene Description Prompt:
Model a small facade with unshaded, vertical-fin, and horizontal-louver bays. Use the same interior depth and sunlight for each bay to make comparison visually fair. Build this with Three.js from cdnjs as a low-poly scene in one self-contained HTML file.
Working Three.js Starter (office-facade-with-varied-shading-bays-x2195):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Facade Shading Study — Unshaded vs. Vertical Fins vs. Horizontal Louvers</title>
<style>
:root { color-scheme: light; }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #dfe8ee;
font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Helvetica, Arial, sans-serif; }
#app { position: fixed; inset: 0; }
#app canvas { display: block; touch-action: none; }
.panel {
position: fixed; top: 16px; left: 16px; width: 268px;
background: rgba(255,255,255,.86); backdrop-filter: blur(8px);
border: 1px solid rgba(30,40,50,.14); border-radius: 12px;
padding: 14px 15px 13px; color: #22282e;
box-shadow: 0 8px 26px rgba(20,30,40,.16); user-select: none;
}
.panel h1 { font-size: 13px; letter-spacing:.09em; text-transform: uppercase; margin: 0 0 4px; }
.panel p { font-size: 11.5px; line-height:1.45; color:#5a646c; margin: 0 0 10px; }
.row { display:flex; align-items:center; justify-content:space-between; gap:8px; margin:7px 0; }
.row label { font-size: 11.5px; color:#39424a; }
.row .val { font-size: 11px; color:#7a838b; font-variant-numeric: tabular-nums; min-width:52px; text-align:right; }
input[type=range] { width:100%; margin:2px 0 6px; accent-color:#3f7f96; }
.checks { display:grid; grid-template-columns:1fr 1fr; gap:3px 8px; margin-top:8px; }
.checks label { font-size:11px; color:#39424a; display:flex; align-items:center; gap:5px; cursor:pointer; }
.btns { display:flex; gap:6px; margin-top:10px; flex-wrap:wrap; }
button {
font: inherit; font-size:11px; padding:5px 9px; border-radius:7px; cursor:pointer;
border:1px solid rgba(30,40,50,.18); background:#fff; color:#2b3238;
}
button:hover { background:#eef2f5; }
.note { font-size:10.5px; color:#8a9299; margin-top:9px; line-height:1.4; }
.legend {
position: fixed; left:16px; bottom:16px; background:rgba(255,255,255,.82);
border:1px solid rgba(30,40,50,.12); border-radius:10px; padding:9px 12px; font-size:11px; color:#39424a;
display:flex; gap:14px; align-items:center;
}
.legend i { display:inline-block; width:9px; height:9px; border-radius:2px; margin-right:5px; vertical-align:middle; }
#hint { position:fixed; right:16px; bottom:16px; font-size:10.5px; color:#69737b; background:rgba(255,255,255,.7);
padding:7px 11px; border-radius:8px; }
</style>
</head>
<body>
<div id="app"></div>
<div class="panel">
<h1>Facade Shading Study</h1>
<p>Three identical bays on one coplanar wall — same 4.0 m opening, same 4.0 m interior depth, one shared sun vector.</p>
<div class="row"><label>Sun azimuth</label><span class="val" id="azVal">38°</span></div>
<input id="az" type="range" min="-70" max="70" step="1" value="38" />
<div class="row"><label>Sun elevation</label><span class="val" id="elVal">42°</span></div>
<input id="el" type="range" min="8" max="80" step="1" value="42" />
<div class="checks">
<label><input type="checkbox" id="guides" checked> Depth / width guides</label>
<label><input type="checkbox" id="sunmark" checked> Sun marker</label>
<label><input type="checkbox" id="rays" checked> Incident rays</label>
<label><input type="checkbox" id="names" checked> Bay labels</label>
<label><input type="checkbox" id="shadows" checked> Shadows</label>
<label><input type="checkbox" id="rooms" checked> Interiors</label>
</div>
<div class="btns">
<button data-view="front">Front elevation</button>
<button data-view="oblique">Oblique</button>
<button data-view="room">Look inside</button>
<button data-view="reset">Reset</button>
</div>
<div class="note">Drag = orbit · Right-drag / Shift-drag = pan · Wheel or pinch = zoom.<br>
Every bay sees the same directional light; only the shading device differs.</div>
</div>
<div class="legend">
<span><i style="background:#ff7a45"></i>4.0 m interior depth</span>
<span><i style="background:#3aa8a0"></i>4.0 m bay width</span>
<span><i style="background:#8496a0"></i>shading device</span>
</div>
<div id="hint">One sun · three bays · one depth</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ───────────────────────── constants ───────────────────────── */
const WALL_H = 6.5; // total facade height
const SILL_Y = 0.8; // opening sill
const HEAD_Y = 4.4; // opening head
const BAY_W = 4.0; // bay / opening width
const DEPTH = 4.0; // IDENTICAL interior depth for every bay
const PIER = 1.5; // pier between bays
const FACE_Z = 0.2; // exterior face of wall
const IN_Z = -0.2; // interior face of wall
const BACK_Z = IN_Z - DEPTH; // -4.2 (interior face of back wall)
const OPEN_H = HEAD_Y - SILL_Y; // 3.6
const DEVICE_D = 0.40; // shading device projection depth (same for both devices)
const BAY_X = [-5.5, 0, 5.5];
/* ───────────────────────── renderer / scene ───────────────────────── */
const app = document.getElementById('app');
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdfe8ee);
scene.fog = new THREE.Fog(0xdfe8ee, 80, 200);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 400);
/* ───────────────────────── materials ───────────────────────── */
const M = {
extWall: new THREE.MeshLambertMaterial({ color: 0xe9e2d4 }),
trim: new THREE.MeshLambertMaterial({ color: 0xe2dbcb }),
intWall: new THREE.MeshLambertMaterial({ color: 0xded7c8 }),
floor: new THREE.MeshLambertMaterial({ color: 0xbcae9c }),
ceil: new THREE.MeshLambertMaterial({ color: 0xf4f0e8 }),
back: new THREE.MeshLambertMaterial({ color: 0xeae4d8 }),
device: new THREE.MeshLambertMaterial({ color: 0x8496a0, flatShading: true }),
ground: new THREE.MeshLambertMaterial({ color: 0xcdd2c2 }),
paving: new THREE.MeshLambertMaterial({ color: 0xd8d5cc }),
trunk: new THREE.MeshLambertMaterial({ color: 0x8a7358, flatShading: true }),
leaf: new THREE.MeshLambertMaterial({ color: 0x86a06f, flatShading: true })
};
function box(w, h, d, x, y, z, mat, cast = true, recv = true) {
const m = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
m.position.set(x, y, z);
m.castShadow = cast; m.receiveShadow = recv;
scene.add(m);
return m;
}
/* ───────────────────────── ground & context ───────────────────────── */
const ground = new THREE.Mesh(new THREE.PlaneGeometry(200, 200), M.ground);
ground.rotation.x = -Math.PI / 2; ground.position.y = -0.02;
ground.receiveShadow = true; scene.add(ground);
box(34, 0.06, 12, 0, -0.03, 5.0, M.paving, false, true);
function tree(x, z, s) {
const t = new THREE.Mesh(new THREE.CylinderGeometry(0.16, 0.22, 2.2, 6), M.trunk);
t.position.set(x, 1.1 * s, z); t.castShadow = true; scene.add(t);
const c = new THREE.Mesh(new THREE.IcosahedronGeometry(1.75 * s, 0), M.leaf);
c.position.set(x, 3.1 * s, z); c.castShadow = true; scene.add(c);
}
tree(-17, 9, 1.0); tree(16.5, 12, 1.25);
/* ───────────────────────── facade shell (with three openings) ───────────────────────── */
const W = 18; // total facade width
// sill band
box(W, SILL_Y, 0.4, 0, SILL_Y / 2, 0, M.extWall);
// head band
box(W, WALL_H - HEAD_Y, 0.4, 0, (WALL_H + HEAD_Y) / 2, 0, M.extWall);
// piers
const pierX = [-8.25, -2.75, 2.75, 8.25];
for (const px of pierX) box(PIER, OPEN_H, 0.4, px, (SILL_Y + HEAD_Y) / 2, 0, M.extWall);
// cornice + plinth
box(W + 0.7, 0.24, 0.72, 0, WALL_H + 0.12, 0.02, M.trim);
box(W + 0.4, 0.30, 0.56, 0, 0.15, 0.03, M.trim);
/* ───────────────────────── identical interiors (same depth!) ───────────────────────── */
const roomGroup = new THREE.Group();
scene.add(roomGroup);
for (const bx of BAY_X) {
const floorSlab = box(BAY_W, 0.12, DEPTH + 0.2, bx, -0.06, IN_Z - DEPTH / 2, M.floor);
const ceilSlab = box(BAY_W, 0.12, DEPTH + 0.2, bx, HEAD_Y + 0.06, IN_Z - DEPTH / 2, M.ceil);
const backWall = box(BAY_W + 0.3, HEAD_Y + 0.24, 0.2, bx, (HEAD_Y + 0.24) / 2 - 0.12, BACK_Z - 0.1, M.back);
const sideL = box(0.15, HEAD_Y + 0.24, DEPTH + 0.4, bx - BAY_W / 2 - 0.075, (HEAD_Y + 0.24) / 2 - 0.12, IN_Z - DEPTH / 2, M.intWall);
const sideR = box(0.15, HEAD_Y + 0.24, DEPTH + 0.4, bx + BAY_W / 2 + 0.075, (HEAD_Y + 0.24) / 2 - 0.12, IN_Z - DEPTH / 2, M.intWall);
for (const m of [floorSlab, ceilSlab, backWall, sideL, sideR]) roomGroup.add(m);
}
/* ───────────────────────── shading devices ───────────────────────── */
// BAY A (x = -5.5) : unshaded — nothing added.
// BAY B (x = 0.0) : 8 vertical fins, 0.50 m pitch, 0.40 m projection
for (let i = 0; i < 8; i++) {
const x = BAY_X[1] - 1.75 + i * 0.5;
box(0.08, OPEN_H, DEVICE_D, x, (SILL_Y + HEAD_Y) / 2, FACE_Z + DEVICE_D / 2, M.device);
}
// BAY C (x = 5.5) : 9 horizontal louvers, 0.40 m pitch, 0.40 m projection, 30° tilt
const louverGroup = new THREE.Group();
for (let i = 0; i < 9; i++) {
const y = SILL_Y + 0.1 + i * 0.4;
const m = new THREE.Mesh(new THREE.BoxGeometry(BAY_W, 0.07, DEVICE_D), M.device);
m.position.set(BAY_X[2], y, FACE_Z + DEVICE_D / 2);
m.rotation.x = -Math.PI / 6; // 30° tilt
m.castShadow = true; m.receiveShadow = true;
louverGroup.add(m);
}
scene.add(louverGroup);
/* ───────────────────────── lighting ───────────────────────── */
scene.add(new THREE.HemisphereLight(0xcfe3f2, 0xbcae98, 0.5));
scene.add(new THREE.AmbientLight(0xffffff, 0.10));
const sun = new THREE.DirectionalLight(0xfff3e0, 3.1);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20; sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20; sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 2; sun.shadow.camera.far = 90;
sun.shadow.bias = -0.0004;
sun.shadow.normalBias = 0.03;
scene.add(sun, sun.target);
const sunTarget = new THREE.Vector3(0, 2.0, -1.0);
/* ───────────────────────── annotation sprites ───────────────────────── */
function roundRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + w, y, x + w, y + h, r);
ctx.arcTo(x + w, y + h, x, y + h, r);
ctx.arcTo(x, y + h, x, y, r);
ctx.arcTo(x, y, x + w, y, r);
ctx.closePath();
}
function makeLabel(title, sub, opts = {}) {
const { worldH = 0.62, accent = null, depthTest = false, bg = 'rgba(255,255,255,0.86)',
titleColor = '#22282e', subColor = '#667078' } = opts;
const s = 2;
const c = document.createElement('canvas');
const probe = c.getContext('2d');
const tF = `700 ${42 * s}px ui-sans-serif, system-ui, Helvetica, Arial, sans-serif`;
const sF = `500 ${25 * s}px ui-sans-serif, system-ui, Helvetica, Arial, sans-serif`;
probe.font = tF; const tw = probe.measureText(title).width;
probe.font = sF; const sw = sub ? probe.measureText(sub).width : 0;
const padX = 20 * s, padY = 14 * s;
const w = Math.ceil(Math.max(tw, sw) + padX * 2);
const h = Math.ceil((sub ? 42 * s + 10 * s + 25 * s : 42 * s) + padY * 2);
c.width = w; c.height = h;
const g = c.getContext('2d');
g.fillStyle = bg; roundRect(g, 0, 0, w, h, 11 * s); g.fill();
if (accent) { g.fillStyle = accent; roundRect(g, 0, 0, 5 * s, h, 2.5 * s); g.fill(); }
g.textBaseline = 'top';
g.font = tF; g.fillStyle = titleColor; g.fillText(title, padX, padY);
if (sub) { g.font = sF; g.fillStyle = subColor; g.fillText(sub, padX, padY + 42 * s + 10 * s); }
const tex = new THREE.CanvasTexture(c);
tex.colorSpace = THREE.SRGBColorSpace;
tex.anisotropy = 4;
const spr = new THREE.Sprite(new THREE.SpriteMaterial({
map: tex, transparent: true, depthTest, depthWrite: false
}));
spr.scale.set(worldH * w / h, worldH, 1);
spr.renderOrder = 6;
return spr;
}
function makeSunSprite() {
const s = 128, c = document.createElement('canvas');
c.width = c.height = s;
const g = c.getContext('2d');
const gr = g.createRadialGradient(s/2, s/2, 6, s/2, s/2, s/2);
gr.addColorStop(0, 'rgba(255,246,206,1)');
gr.addColorStop(0.42, 'rgba(255,214,102,0.95)');
gr.addColorStop(1, 'rgba(255,196,60,0)');
g.fillStyle = gr; g.fillRect(0, 0, s, s);
const tex = new THREE.CanvasTexture(c); tex.colorSpace = THREE.SRGBColorSpace;
const spr = new THREE.Sprite(new THREE.SpriteMaterial({ map: tex, transparent: true, depthWrite: false }));
spr.scale.set(5.2, 5.2, 1);
return spr;
}
/* bay labels */
const labelDefs = [
['BAY A · UNSHADED', 'no device — direct beam', '#c0392b'],
['BAY B · VERTICAL FINS', '8 fins · 0.50 m pitch · 0.40 m deep', '#3f7f96'],
['BAY C · HORIZONTAL LOUVERS', '9 slats · 0.40 m pitch · 30° tilt', '#7a6aa8']
];
const bayLabels = [];
labelDefs.forEach((d, i) => {
const l = makeLabel(d[0], d[1], { worldH: 0.78, accent: d[2] });
l.position.set(BAY_X[i], HEAD_Y + 0.75, FACE_Z + 0.5);
scene.add(l); bayLabels.push(l);
});
/* dimension labels */
const depthLabels = [], widthLabels = [];
for (const bx of BAY_X) {
const dl = makeLabel('4.0 m', 'interior depth', { worldH: 0.46, depthTest: true, bg: 'rgba(255,247,240,0.9)', accent: '#ff7a45' });
dl.position.set(bx, 0.55, IN_Z - DEPTH / 2);
scene.add(dl); depthLabels.push(dl);
const wl = makeLabel('4.0 m', 'bay width', { worldH: 0.46, depthTest: true, bg: 'rgba(240,252,251,0.9)', accent: '#3aa8a0' });
wl.position.set(bx, 0.45, 1.6);
scene.add(wl); widthLabels.push(wl);
}
/* guide lines */
const guideGroup = new THREE.Group();
scene.add(guideGroup);
const depthMat = new THREE.LineBasicMaterial({ color: 0xff7a45 });
const widthMat = new THREE.LineBasicMaterial({ color: 0x3aa8a0 });
for (const bx of BAY_X) {
const g = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(bx, 0.07, IN_Z), new THREE.Vector3(bx, 0.07, BACK_Z),
new THREE.Vector3(bx, 0.07, IN_Z), new THREE.Vector3(bx, 0.30, IN_Z),
new THREE.Vector3(bx, 0.07, BACK_Z), new THREE.Vector3(bx, 0.30, BACK_Z),
new THREE.Vector3(bx - BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx + BAY_W / 2, 0.07, 1.4),
new THREE.Vector3(bx - BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx - BAY_W / 2, 0.30, 1.4),
new THREE.Vector3(bx + BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx + BAY_W / 2, 0.30, 1.4)
]);
const ls = new THREE.LineSegments(g, depthMat);
ls.material = depthMat;
guideGroup.add(ls);
// split colours: rebuild two line objects for clarity
const ls2 = new THREE.LineSegments(new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(bx - BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx + BAY_W / 2, 0.07, 1.4),
new THREE.Vector3(bx - BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx - BAY_W / 2, 0.30, 1.4),
new THREE.Vector3(bx + BAY_W / 2, 0.07, 1.4), new THREE.Vector3(bx + BAY_W / 2, 0.30, 1.4)
]), widthMat);
guideGroup.add(ls2);
}
/* sun marker + incident rays */
const sunSprite = makeSunSprite();
scene.add(sunSprite);
const sunLabel = makeLabel('SHARED SUN', 'one vector for all three bays', { worldH: 0.7, accent: '#e8a33d', bg: 'rgba(255,252,240,0.88)' });
scene.add(sunLabel);
const sunLine = new THREE.Line(new THREE.BufferGeometry(), new THREE.LineDashedMaterial({ color: 0xe8b45c, dashSize: 1.2, gapSize: 0.9, transparent: true, opacity: 0.75 }));
scene.add(sunLine);
const rayLines = new THREE.LineSegments(new THREE.BufferGeometry(), new THREE.LineDashedMaterial({ color: 0xf6c453, dashSize: 0.55, gapSize: 0.35, transparent: true, opacity: 0.95 }));
scene.add(rayLines);
/* ───────────────────────── sun update ───────────────────────── */
const azEl = document.getElementById('az'), elEl = document.getElementById('el');
const azVal = document.getElementById('azVal'), elVal = document.getElementById('elVal');
function currentDir() {
const az = THREE.MathUtils.degToRad(+azEl.value);
const el = THREE.MathUtils.degToRad(+elEl.value);
return new THREE.Vector3(Math.sin(az) * Math.cos(el), Math.sin(el), Math.cos(az) * Math.cos(el));
}
function updateSun() {
const dir = currentDir();
sun.position.copy(sunTarget).addScaledVector(dir, 35);
sun.target.position.copy(sunTarget);
sun.target.updateMatrixWorld();
sunSprite.position.copy(sunTarget).addScaledVector(dir, 42);
sunLabel.position.copy(sunSprite.position).add(new THREE.Vector3(0, 3.4, 0));
// dashed line sun → facade centre
const sg = new THREE.BufferGeometry().setFromPoints([sunSprite.position.clone(), sunTarget.clone()]);
sunLine.geometry.dispose(); sunLine.geometry = sg; sunLine.computeLineDistances();
// identical incident rays, one per bay, striking the facade plane
const t = dir.clone().negate();
const pts = [];
for (const bx of BAY_X) {
const cross = new THREE.Vector3(bx, HEAD_Y - 0.7, FACE_Z);
pts.push(cross.clone().addScaledVector(t, -7.0), cross);
}
const rg = new THREE.BufferGeometry().setFromPoints(pts);
rayLines.geometry.dispose(); rayLines.geometry = rg; rayLines.computeLineDistances();
azVal.textContent = (+azEl.value).toFixed(0) + '°';
elVal.textContent = (+elEl.value).toFixed(0) + '°';
}
azEl.addEventListener('input', updateSun);
elEl.addEventListener('input', updateSun);
updateSun();
/* ───────────────────────── UI toggles ───────────────────────── */
const cb = id => document.getElementById(id);
cb('guides').addEventListener('change', e => {
guideGroup.visible = e.target.checked;
depthLabels.forEach(l => l.visible = e.target.checked);
widthLabels.forEach(l => l.visible = e.target.checked);
});
cb('sunmark').addEventListener('change', e => {
sunSprite.visible = e.target.checked; sunLabel.visible = e.target.checked; sunLine.visible = e.target.checked;
});
cb('rays').addEventListener('change', e => { rayLines.visible = e.target.checked; });
cb('names').addEventListener('change', e => { bayLabels.forEach(l => l.visible = e.target.checked); });
cb('shadows').addEventListener('change', e => {
renderer.shadowMap.enabled = e.target.checked;
scene.traverse(o => { if (o.isMesh && o.material) o.material.needsUpdate = true; });
});
cb('rooms').addEventListener('change', e => { roomGroup.visible = e.target.checked; });
/* ───────────────────────── camera controls (hand-rolled) ───────────────────────── */
const camState = {
r: 26, theta: 0.62, phi: 1.16,
target: new THREE.Vector3(0, 2.4, -0.6)
};
const camGoal = { r: 26, theta: 0.62, phi: 1.16, target: new THREE.Vector3(0, 2.4, -0.6) };
const sph = new THREE.Spherical(), off = new THREE.Vector3();
function applyCamera() {
off.setFromSpherical(sph.set(camState.r, camState.phi, camState.theta)).add(camState.target);
camera.position.copy(off);
camera.lookAt(camState.target);
}
applyCamera();
const pointers = new Map();
let mode = 'rotate', lastX = 0, lastY = 0, pinchPrev = 0;
const el = renderer.domElement;
el.style.touchAction = 'none';
el.addEventListener('contextmenu', e => e.preventDefault());
function pinchDist() {
const p = [...pointers.values()];
return Math.hypot(p[0].x - p[1].x, p[0].y - p[1].y);
}
el.addEventListener('pointerdown', e => {
el.setPointerCapture(e.pointerId);
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 1) {
mode = (e.button === 2 || e.shiftKey) ? 'pan' : 'rotate';
lastX = e.clientX; lastY = e.clientY;
} else if (pointers.size === 2) {
pinchPrev = pinchDist();
}
});
el.addEventListener('pointermove', e => {
if (!pointers.has(e.pointerId)) return;
pointers.set(e.pointerId, { x: e.clientX, y: e.clientY });
if (pointers.size === 2) {
const d = pinchDist();
if (pinchPrev > 0) camGoal.r = THREE.MathUtils.clamp(camGoal.r * (pinchPrev / d), 5, 80);
pinchPrev = d;
return;
}
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY;
if (mode === 'rotate') {
camGoal.theta -= dx * 0.005;
camGoal.phi = THREE.MathUtils.clamp(camGoal.phi - dy * 0.005, 0.12, 1.52);
} else {
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const s = camGoal.r * 0.0013;
camGoal.target.addScaledVector(right, -dx * s).addScaledVector(up, dy * s);
camGoal.target.y = THREE.MathUtils.clamp(camGoal.target.y, -1, 12);
}
});
const endPointer = e => { pointers.delete(e.pointerId); if (pointers.size < 2) pinchPrev = 0; };
el.addEventListener('pointerup', endPointer);
el.addEventListener('pointercancel', endPointer);
el.addEventListener('pointerleave', endPointer);
el.addEventListener('wheel', e => {
e.preventDefault();
camGoal.r = THREE.MathUtils.clamp(camGoal.r * Math.exp(e.deltaY * 0.0011), 5, 80);
}, { passive: false });
/* keyboard */
addEventListener('keydown', e => {
if (e.key === 'ArrowLeft') camGoal.theta -= 0.08;
if (e.key === 'ArrowRight') camGoal.theta += 0.08;
if (e.key === 'ArrowUp') camGoal.phi = THREE.MathUtils.clamp(camGoal.phi - 0.06, 0.12, 1.52);
if (e.key === 'ArrowDown') camGoal.phi = THREE.MathUtils.clamp(camGoal.phi + 0.06, 0.12, 1.52);
});
/* preset views */
const VIEWS = {
front: { r: 24, theta: 0.0, phi: 1.49, target: new THREE.Vector3(0, 2.6, -0.6) },
oblique: { r: 26, theta: 0.62, phi: 1.16, target: new THREE.Vector3(0, 2.4, -0.6) },
room: { r: 12, theta: 0.18, phi: 1.28, target: new THREE.Vector3(2.6, 2.2, -2.0) },
reset: { r: 26, theta: 0.62, phi: 1.16, target: new THREE.Vector3(0, 2.4, -0.6) }
};
document.querySelectorAll('button[data-view]').forEach(b => {
b.addEventListener('click', () => {
const v = VIEWS[b.dataset.view];
camGoal.r = v.r; camGoal.theta = v.theta; camGoal.phi = v.phi; camGoal.target.copy(v.target);
if (b.dataset.view === 'reset') { azEl.value = 38; elEl.value = 42; updateSun(); }
});
});
/* ───────────────────────── resize + loop ───────────────────────── */
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const k = 1 - Math.pow(0.0009, dt);
camState.r += (camGoal.r - camState.r) * k;
camState.theta += (camGoal.theta - camState.theta) * k;
camState.phi += (camGoal.phi - camState.phi) * k;
camState.target.lerp(camGoal.target, k);
applyCamera();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```