Prompt specification
Build or customize the Three.js 3D scene "Apartment laundry balcony" 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: Apartment laundry balcony
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/apartment-laundry-balcony-x0047
Scene Description Prompt:
A stylized laundry balcony with gently swaying fabric, basket, clothespins, sliding door, and clear walkway. Build standalone HTML using Three.js from cdnjs.
Working Three.js Starter (apartment-laundry-balcony-x0047):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Laundry Balcony</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9ed;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.title-card {
position: fixed;
top: 24px;
left: 24px;
z-index: 10;
max-width: min(360px, calc(100vw - 48px));
padding: 17px 20px 16px;
color: #31484a;
background: rgba(255, 250, 242, 0.78);
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 18px;
box-shadow: 0 12px 40px rgba(55, 77, 79, 0.12);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin: 0 0 5px;
color: #78918f;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 4vw, 34px);
font-weight: 500;
letter-spacing: -0.025em;
}
.subtitle {
margin: 7px 0 0;
color: #6d7d7c;
font-size: 12px;
line-height: 1.45;
}
.controls {
position: fixed;
right: 22px;
bottom: 22px;
z-index: 10;
display: flex;
align-items: center;
gap: 9px;
}
.hint {
padding: 9px 13px;
color: #526867;
background: rgba(255, 250, 242, 0.76);
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 999px;
box-shadow: 0 8px 30px rgba(55, 77, 79, 0.1);
backdrop-filter: blur(10px);
font-size: 11px;
pointer-events: none;
}
button {
min-height: 38px;
padding: 0 15px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 999px;
color: #fdfaf4;
background: #405c5d;
box-shadow: 0 8px 24px rgba(45, 67, 68, 0.2);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 700;
transition: transform 160ms ease, background 160ms ease;
}
button:hover {
transform: translateY(-2px);
background: #324f50;
}
button:active {
transform: translateY(0);
}
@media (max-width: 650px) {
.title-card {
top: 14px;
left: 14px;
padding: 13px 16px;
}
.subtitle {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="title-card">
<p class="eyebrow">A quiet afternoon</p>
<h1>Laundry Balcony</h1>
<p class="subtitle">Warm sunlight, a soft breeze, and a clear path home.</p>
</div>
<div class="controls">
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<button id="resetView" type="button" aria-label="Reset camera view">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, scene, and lighting
// ---------------------------------------------------------------------
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.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9ed);
scene.fog = new THREE.Fog(0xdce9ed, 18, 36);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.05, 0);
const defaultView = {
theta: 0.48,
phi: 1.02,
radius: 13.1
};
const view = { ...defaultView };
function updateCamera() {
const sinPhi = Math.sin(view.phi);
camera.position.set(
cameraTarget.x + view.radius * sinPhi * Math.sin(view.theta),
cameraTarget.y + view.radius * Math.cos(view.phi),
cameraTarget.z + view.radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xeaf7ff, 0x8e7868, 2.2);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe4bc, 3.4);
sun.position.set(-6, 11, 8);
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 = 30;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d9e8, 0.85);
fill.position.set(8, 5, 4);
scene.add(fill);
// ---------------------------------------------------------------------
// Materials and geometry helpers
// ---------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const mats = {
wall: material(0xe8d8c4),
wallTrim: material(0xf2e7d8),
slab: material(0xbda58b),
grout: material(0x9d8976),
tile: material(0xd8c7b0),
pathTile: material(0xeee2d0),
rail: material(0x405758, { roughness: 0.58, metalness: 0.18 }),
dark: material(0x263a3c),
frame: material(0x526968, { roughness: 0.52, metalness: 0.2 }),
wood: material(0xb98558),
woodLight: material(0xd2a06f),
pole: material(0x7a8c87, { roughness: 0.5, metalness: 0.35 }),
rope: material(0x53615c, { roughness: 0.95 }),
terracotta: material(0xc87552),
soil: material(0x4b392f),
leaf: material(0x547b61, { roughness: 0.9 }),
leafLight: material(0x739774),
glass: material(0x9bcbd2, {
roughness: 0.18,
metalness: 0.08,
transparent: true,
opacity: 0.58,
depthWrite: false
}),
reflection: new THREE.MeshBasicMaterial({
color: 0xe7ffff,
transparent: true,
opacity: 0.2,
depthWrite: false,
side: THREE.DoubleSide
}),
lamp: material(0xffd995, {
emissive: 0xffb84d,
emissiveIntensity: 1.7,
roughness: 0.4
})
};
function box(
width,
height,
depth,
mat,
x = 0,
y = 0,
z = 0,
parent = scene,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function cylinder(
radiusTop,
radiusBottom,
height,
mat,
x = 0,
y = 0,
z = 0,
parent = scene,
segments = 12
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(radius, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(radius, 16, 12),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Balcony platform and tiled floor
// ---------------------------------------------------------------------
box(10.25, 0.42, 5.55, mats.slab, 0, -0.19, 0.05);
box(10.05, 0.08, 5.35, mats.grout, 0, 0.04, 0.05);
const tileWidth = 0.96;
const tileDepth = 1.01;
for (let row = 0; row < 5; row++) {
for (let col = 0; col < 10; col++) {
const x = -4.5 + col;
const z = -2.05 + row * 1.05;
const isWalkway = col === 6 || col === 7;
box(
tileWidth,
0.075,
tileDepth,
isWalkway ? mats.pathTile : mats.tile,
x,
0.115,
z,
scene,
false,
true
);
}
}
// ---------------------------------------------------------------------
// Facade and sliding glass door
// ---------------------------------------------------------------------
box(10.45, 6.1, 0.38, mats.wall, 0, 3.02, -2.78);
box(10.55, 0.18, 0.5, mats.wallTrim, 0, 6.02, -2.73);
box(10.45, 0.16, 0.48, mats.wallTrim, 0, 0.08, -2.7);
// Recess and frame.
box(3.2, 3.62, 0.11, mats.dark, 2, 2.03, -2.52);
box(0.14, 3.78, 0.18, mats.frame, 0.42, 2.03, -2.4);
box(0.14, 3.78, 0.18, mats.frame, 3.58, 2.03, -2.4);
box(3.3, 0.15, 0.18, mats.frame, 2, 3.86, -2.4);
box(3.3, 0.13, 0.18, mats.frame, 2, 0.22, -2.4);
// Two overlapping glass panels indicate a sliding door.
box(1.38, 3.38, 0.035, mats.glass, 1.43, 2.03, -2.36, scene, false, false);
box(1.38, 3.38, 0.035, mats.glass, 2.61, 2.03, -2.32, scene, false, false);
box(0.055, 3.38, 0.055, mats.frame, 0.74, 2.03, -2.3);
box(0.055, 3.38, 0.055, mats.frame, 2.12, 2.03, -2.3);
box(0.055, 3.38, 0.055, mats.frame, 3.26, 2.03, -2.28);
// Top and bottom sliding tracks.
box(2.58, 0.065, 0.075, mats.frame, 2, 3.72, -2.27);
box(2.58, 0.055, 0.075, mats.frame, 2, 0.35, -2.27);
// Door handle.
box(0.06, 0.42, 0.07, mats.rail, 2.17, 1.95, -2.2);
// Stylized reflections.
const reflectionA = box(
0.08,
2.8,
0.01,
mats.reflection,
1.88,
2.17,
-2.285,
scene,
false,
false
);
reflectionA.rotation.z = -0.32;
const reflectionB = box(
0.055,
2.55,
0.01,
mats.reflection,
2.88,
2.13,
-2.245,
scene,
false,
false
);
reflectionB.rotation.z = -0.32;
// Wall sconce.
box(0.08, 0.08, 0.4, mats.rail, -1.55, 3.32, -2.48);
const lampShade = cylinder(
0.24,
0.31,
0.38,
mats.lamp,
-1.55,
3.35,
-2.18,
scene,
16
);
lampShade.rotation.x = Math.PI;
const lampGlow = new THREE.PointLight(0xffc36a, 0.7, 4.5, 2);
lampGlow.position.set(-1.55, 3.1, -1.95);
scene.add(lampGlow);
// ---------------------------------------------------------------------
// Balcony railings
// ---------------------------------------------------------------------
const frontZ = 2.73;
const sideX = 4.9;
// Front rails.
box(9.95, 0.1, 0.11, mats.rail, 0, 1.27, frontZ);
box(9.95, 0.075, 0.09, mats.rail, 0, 0.31, frontZ);
for (let x = -4.85; x <= 4.86; x += 0.62) {
cylinder(0.034, 0.034, 1.05, mats.rail, x, 0.78, frontZ, scene, 8);
}
for (const x of [-sideX, sideX]) {
cylinder(0.075, 0.085, 1.46, mats.rail, x, 0.69, frontZ, scene, 10);
}
// Side rails.
for (const x of [-sideX, sideX]) {
box(0.11, 0.1, 5.22, mats.rail, x, 1.27, 0.03);
box(0.09, 0.075, 5.22, mats.rail, x, 0.31, 0.03);
for (let z = -2.43; z <= 2.75; z += 0.64) {
cylinder(0.034, 0.034, 1.05, mats.rail, x, 0.78, z, scene, 8);
}
cylinder(0.075, 0.085, 1.46, mats.rail, x, 0.69, -2.57, scene, 10);
}
// ---------------------------------------------------------------------
// Drying rack, sagging rope, animated laundry, and clothespins
// ---------------------------------------------------------------------
const lineLeft = -4.35;
const lineRight = 0.65;
const lineZ = -0.68;
cylinder(0.045, 0.06, 4.35, mats.pole, lineLeft, 2.16, lineZ, scene, 10);
cylinder(0.045, 0.06, 4.35, mats.pole, lineRight, 2.16, lineZ, scene, 10);
sphere(0.075, mats.pole, lineLeft, 4.35, lineZ);
sphere(0.075, mats.pole, lineRight, 4.35, lineZ);
const lineCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(lineLeft, 4.35, lineZ),
new THREE.Vector3(-1.9, 4.05, lineZ),
new THREE.Vector3(lineRight, 4.35, lineZ)
]);
const lineMesh = new THREE.Mesh(
new THREE.TubeGeometry(lineCurve, 40, 0.022, 6, false),
mats.rope
);
lineMesh.castShadow = true;
scene.add(lineMesh);
function lineHeightAt(x) {
const t = THREE.MathUtils.clamp(
(x - lineLeft) / (lineRight - lineLeft),
0,
1
);
return 4.05 + 0.3 * Math.pow(t * 2 - 1, 2);
}
function makeFabricTexture(baseColor, accentColor, pattern = "border") {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = baseColor;
ctx.fillRect(0, 0, 128, 256);
ctx.strokeStyle = accentColor;
ctx.fillStyle = accentColor;
ctx.lineWidth = 5;
if (pattern === "check") {
for (let x = 10; x < 128; x += 24) {
ctx.globalAlpha = 0.45;
ctx.fillRect(x, 0, 3, 256);
}
for (let y = 15; y < 256; y += 32) {
ctx.globalAlpha = 0.38;
ctx.fillRect(0, y, 128, 3);
}
} else if (pattern === "stripe") {
ctx.globalAlpha = 0.75;
for (let y = 26; y < 250; y += 45) {
ctx.fillRect(0, y, 128, 5);
}
} else {
ctx.globalAlpha = 0.7;
ctx.fillRect(0, 12, 128, 4);
ctx.fillRect(0, 23, 128, 3);
ctx.fillRect(0, 229, 128, 4);
ctx.fillRect(0, 240, 128, 3);
}
ctx.globalAlpha = 1;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const cloths = [];
function addCloth({
x,
width,
height,
texture,
phase,
z = lineZ
}) {
const geometry = new THREE.PlaneGeometry(width, height, 12, 20);
const position = geometry.attributes.position;
const base = new Float32Array(position.array);
const clothMaterial = material(0xffffff, {
map: texture,
roughness: 0.96,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, clothMaterial);
const topY = lineHeightAt(x);
mesh.position.set(x, topY - height / 2, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
cloths.push({
mesh,
geometry,
base,
height,
phase,
topY
});
// Two wooden clothespins per item.
for (const side of [-1, 1]) {
const pin = new THREE.Group();
pin.position.set(
x + side * width * 0.31,
topY + 0.025,
z + 0.025
);
pin.rotation.z = side * 0.07;
box(0.075, 0.27, 0.065, mats.woodLight, 0, 0, 0, pin);
box(0.095, 0.065, 0.085, mats.wood, 0, -0.115, 0, pin);
box(0.095, 0.065, 0.085, mats.wood, 0, 0.115, 0, pin);
const spring = new THREE.Mesh(
new THREE.TorusGeometry(0.032, 0.008, 5, 10),
mats.pole
);
spring.position.z = 0.047;
pin.add(spring);
scene.add(pin);
}
}
addCloth({
x: -3.55,
width: 1.15,
height: 1.55,
texture: makeFabricTexture("#e7d4ae", "#69908b", "border"),
phase: 0.2
});
addCloth({
x: -2.2,
width: 1.22,
height: 1.86,
texture: makeFabricTexture("#7faeba", "#dce9e6", "stripe"),
phase: 2.1
});
addCloth({
x: -0.72,
width: 1.3,
height: 1.55,
texture: makeFabricTexture("#d78672", "#f2c5a4", "check"),
phase: 4.0
});
// ---------------------------------------------------------------------
// Woven laundry basket
// ---------------------------------------------------------------------
const basket = new THREE.Group();
basket.position.set(-3.55, 0.16, 1.15);
basket.rotation.y = -0.08;
scene.add(basket);
box(1.48, 0.13, 0.95, mats.wood, 0, 0.07, 0, basket);
// Horizontal woven strips.
for (const y of [0.25, 0.43, 0.61, 0.78]) {
box(1.58, 0.075, 0.07, mats.woodLight, 0, y, 0.47, basket);
box(1.58, 0.075, 0.07, mats.woodLight, 0, y, -0.47, basket);
box(0.07, 0.075, 0.91, mats.woodLight, 0.76, y, 0, basket);
box(0.07, 0.075, 0.91, mats.woodLight, -0.76, y, 0, basket);
}
// Vertical woven ribs.
for (let x = -0.62; x <= 0.63; x += 0.21) {
box(0.045, 0.64, 0.045, mats.wood, x, 0.45, 0.47, basket);
box(0.045, 0.64, 0.045, mats.wood, x, 0.45, -0.47, basket);
}
for (let z = -0.34; z <= 0.35; z += 0.22) {
box(0.045, 0.64, 0.045, mats.wood, 0.76, 0.45, z, basket);
box(0.045, 0.64, 0.045, mats.wood, -0.76, 0.45, z, basket);
}
// Thick basket rim.
box(1.66, 0.12, 0.1, mats.wood, 0, 0.84, 0.49, basket);
box(1.66, 0.12, 0.1, mats.wood, 0, 0.84, -0.49, basket);
box(0.1, 0.12, 0.98, mats.wood, 0.79, 0.84, 0, basket);
box(0.1, 0.12, 0.98, mats.wood, -0.79, 0.84, 0, basket);
// Laundry peeking over the rim.
const laundryBlue = material(0x729cab);
const laundryCream = material(0xe6d2ad);
const laundryCoral = material(0xc97868);
const rolledTowel = cylinder(
0.27,
0.27,
0.78,
laundryBlue,
0.2,
0.93,
0.03,
basket,
16
);
rolledTowel.rotation.z = Math.PI / 2;
rolledTowel.rotation.y = 0.2;
const bundleA = sphere(0.38, laundryCream, -0.3, 0.89, 0.15, basket);
bundleA.scale.set(1, 0.58, 0.9);
const bundleB = sphere(0.33, laundryCoral, 0.25, 0.88, -0.23, basket);
bundleB.scale.set(1.1, 0.62, 0.95);
// ---------------------------------------------------------------------
// Small balcony plant
// ---------------------------------------------------------------------
const plant = new THREE.Group();
plant.position.set(4.2, 0.15, -1.85);
scene.add(plant);
cylinder(0.36, 0.27, 0.52, mats.terracotta, 0, 0.26, 0, plant, 16);
cylinder(0.31, 0.31, 0.035, mats.soil, 0, 0.53, 0, plant, 16);
const leafMatA = mats.leaf;
const leafMatB = mats.leafLight;
for (let i = 0; i < 9; i++) {
const angle = (i / 9) * Math.PI * 2;
const leaf = sphere(
0.24,
i % 2 ? leafMatA : leafMatB,
Math.cos(angle) * 0.2,
0.83 + (i % 3) * 0.08,
Math.sin(angle) * 0.2,
plant
);
leaf.scale.set(0.42, 1.65, 0.25);
leaf.rotation.z = Math.cos(angle) * 0.5;
leaf.rotation.x = Math.sin(angle) * 0.5;
leaf.rotation.y = angle;
}
// ---------------------------------------------------------------------
// Subtle sunlit atmosphere
// ---------------------------------------------------------------------
const glowGeometry = new THREE.BufferGeometry();
const glowPositions = new Float32Array(28 * 3);
for (let i = 0; i < 28; i++) {
glowPositions[i * 3] = THREE.MathUtils.randFloatSpread(10);
glowPositions[i * 3 + 1] = THREE.MathUtils.randFloat(1, 6);
glowPositions[i * 3 + 2] = THREE.MathUtils.randFloat(-2, 3);
}
glowGeometry.setAttribute(
"position",
new THREE.BufferAttribute(glowPositions, 3)
);
const glow = new THREE.Points(
glowGeometry,
new THREE.PointsMaterial({
color: 0xfff3cf,
size: 0.035,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
scene.add(glow);
// ---------------------------------------------------------------------
// Custom camera controls — no external controls addon
// ---------------------------------------------------------------------
const pointers = new Map();
let lastPointer = null;
let pinchDistance = 0;
let dragging = false;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
dragging = true;
lastPointer = {
x: event.clientX,
y: event.clientY
};
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && lastPointer) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
view.theta -= dx * 0.006;
view.phi = THREE.MathUtils.clamp(
view.phi + dy * 0.005,
0.42,
1.4
);
lastPointer = {
x: event.clientX,
y: event.clientY
};
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const nextDistance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
view.radius = THREE.MathUtils.clamp(
view.radius * (pinchDistance / nextDistance),
7.5,
19
);
}
pinchDistance = nextDistance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size > 0;
lastPointer = dragging
? {
x: [...pointers.values()][0].x,
y: [...pointers.values()][0].y
}
: null;
pinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius * Math.exp(event.deltaY * 0.001),
7.5,
19
);
updateCamera();
},
{ passive: false }
);
document.getElementById("resetView").addEventListener("click", () => {
Object.assign(view, defaultView);
updateCamera();
});
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") view.theta += 0.12;
if (event.key === "ArrowRight") view.theta -= 0.12;
if (event.key === "ArrowUp") view.phi -= 0.08;
if (event.key === "ArrowDown") view.phi += 0.08;
if (event.key === "+" || event.key === "=") view.radius -= 0.5;
if (event.key === "-") view.radius += 0.5;
view.phi = THREE.MathUtils.clamp(view.phi, 0.42, 1.4);
view.radius = THREE.MathUtils.clamp(view.radius, 7.5, 19);
updateCamera();
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let frame = 0;
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
frame++;
// Deform each hanging cloth while keeping the pinned top edge fixed.
for (const cloth of cloths) {
const positions = cloth.geometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const bx = cloth.base[i * 3];
const by = cloth.base[i * 3 + 1];
const vertical = (by + cloth.height / 2) / cloth.height;
const freedom = 1 - vertical;
const breeze =
Math.sin(elapsed * 1.25 + cloth.phase + bx * 2.1) *
0.075 *
freedom +
Math.sin(elapsed * 0.72 + cloth.phase * 0.6) *
0.028 *
freedom;
positions.setXYZ(
i,
bx + Math.sin(elapsed * 0.58 + cloth.phase) * 0.018 * freedom,
by + Math.sin(elapsed * 0.85 + bx) * 0.008 * freedom,
breeze
);
}
positions.needsUpdate = true;
if (frame % 3 === 0) {
cloth.geometry.computeVertexNormals();
}
}
// A barely perceptible movement in the sunlight motes.
glow.rotation.y = elapsed * 0.012;
glow.position.y = Math.sin(elapsed * 0.18) * 0.045;
renderer.render(scene, camera);
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```