Prompt specification
Build or customize the Three.js 3D scene "Rainy-day reading niche" 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: Rainy-day reading niche
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rainy-day-reading-niche-x0239
Scene Description Prompt:
Compose a window seat with books, blanket, plant, and rainy courtyard beyond; animate a reader shifting between book and view while preserving the room's quiet geometry. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (rainy-day-reading-niche-x0239):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Rain, Held Still</title>
<style>
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #202a30;
}
body {
font-family: Georgia, "Times New Roman", serif;
color: #f3eadb;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
user-select: none;
}
.masthead {
position: absolute;
top: clamp(22px, 4vw, 48px);
left: clamp(22px, 4vw, 52px);
text-shadow: 0 2px 14px rgba(12, 18, 22, 0.42);
}
.eyebrow {
margin: 0 0 8px;
font: 600 10px/1.2 system-ui, sans-serif;
letter-spacing: 0.28em;
text-transform: uppercase;
opacity: 0.72;
}
h1 {
margin: 0;
font-size: clamp(26px, 4vw, 48px);
font-weight: 400;
letter-spacing: -0.025em;
}
.subtitle {
max-width: 320px;
margin: 10px 0 0;
font: 12px/1.6 system-ui, sans-serif;
color: rgba(243, 234, 219, 0.72);
}
.controls {
position: absolute;
right: clamp(18px, 3vw, 36px);
bottom: clamp(18px, 3vw, 32px);
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.hint {
font: 10px/1.4 system-ui, sans-serif;
letter-spacing: 0.13em;
text-transform: uppercase;
color: rgba(243, 234, 219, 0.68);
text-shadow: 0 1px 10px rgba(12, 18, 22, 0.6);
}
button {
border: 1px solid rgba(243, 234, 219, 0.34);
border-radius: 2px;
padding: 9px 12px;
color: #f3eadb;
background: rgba(24, 32, 37, 0.42);
backdrop-filter: blur(8px);
font: 10px/1 system-ui, sans-serif;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
button:hover {
background: rgba(24, 32, 37, 0.72);
border-color: rgba(243, 234, 219, 0.72);
}
.chapter {
position: absolute;
left: clamp(22px, 4vw, 52px);
bottom: clamp(20px, 4vw, 42px);
writing-mode: vertical-rl;
transform: rotate(180deg);
font: 9px/1 system-ui, sans-serif;
letter-spacing: 0.3em;
text-transform: uppercase;
opacity: 0.48;
}
@media (max-width: 680px) {
.subtitle,
.hint,
.chapter {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="A voxel reader in a quiet room beside a rainy courtyard window"></canvas>
<div class="hud">
<div class="masthead">
<p class="eyebrow">A small interior study</p>
<h1>Rain, Held Still</h1>
<p class="subtitle">
A reader moves between the printed page and the wet courtyard beyond.
</p>
</div>
<div class="chapter">Chapter 01 — The Window Seat</div>
<div class="controls">
<span class="hint">Drag to look · Scroll to move</span>
<button id="reset" type="button">Reset view</button>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const resetButton = document.querySelector("#reset");
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(0x64747c);
scene.fog = new THREE.Fog(0x68777d, 14, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
70
);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color, options = {}) {
const key = `${color}:${JSON.stringify(options)}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
...options
})
);
}
return materialCache.get(key);
}
function block(parent, sx, sy, sz, x, y, z, color, options = {}) {
const mesh = new THREE.Mesh(unitBox, getMaterial(color, options));
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = !options.transparent;
mesh.receiveShadow = !options.transparent;
parent.add(mesh);
return mesh;
}
function group(parent, x = 0, y = 0, z = 0) {
const object = new THREE.Group();
object.position.set(x, y, z);
parent.add(object);
return object;
}
function smoothstep(min, max, value) {
const t = THREE.MathUtils.clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xffe9ce, 0x33434d, 1.45));
scene.add(new THREE.AmbientLight(0xbfc9ce, 0.32));
const warmLight = new THREE.DirectionalLight(0xffdfb4, 2.5);
warmLight.position.set(6, 11, 7);
warmLight.target.position.set(0, 2, -2);
warmLight.castShadow = true;
warmLight.shadow.mapSize.set(2048, 2048);
warmLight.shadow.camera.left = -11;
warmLight.shadow.camera.right = 11;
warmLight.shadow.camera.top = 12;
warmLight.shadow.camera.bottom = -8;
warmLight.shadow.camera.near = 0.5;
warmLight.shadow.camera.far = 35;
warmLight.shadow.bias = -0.0003;
scene.add(warmLight, warmLight.target);
const windowLight = new THREE.DirectionalLight(0xb8d7e7, 1.35);
windowLight.position.set(-4, 9, -10);
windowLight.target.position.set(0, 2.4, -1);
scene.add(windowLight, windowLight.target);
const courtyardGlow = new THREE.PointLight(0xffbd72, 24, 15, 2);
courtyardGlow.position.set(2, 4.2, -15.3);
scene.add(courtyardGlow);
// ---------------------------------------------------------------------
// Room shell
// ---------------------------------------------------------------------
const floorColors = [
0x9b7655,
0xa47c59,
0x916f51,
0xaa8260,
0x987255
];
// Staggered voxel floorboards.
for (let row = 0; row < 29; row++) {
const z = -4.82 + row * 0.46;
const shift = row % 2 === 0 ? 0 : 2;
const cuts = [-6.05, -2.1 + shift, 1.85 + shift, 6.05];
for (let piece = 0; piece < 3; piece++) {
const x0 = THREE.MathUtils.clamp(cuts[piece], -6.05, 6.05);
const x1 = THREE.MathUtils.clamp(cuts[piece + 1], -6.05, 6.05);
if (x1 - x0 <= 0.03) continue;
const color = floorColors[Math.floor(Math.random() * floorColors.length)];
block(scene, x1 - x0 - 0.025, 0.16, 0.43, (x0 + x1) / 2, -0.08, z, color);
}
}
// Back wall with a large rectangular opening.
block(scene, 12, 1.75, 0.3, 0, 0.875, -5.05, 0xd4c5ac);
block(scene, 12, 1.45, 0.3, 0, 7.075, -5.05, 0xd4c5ac);
block(scene, 1.75, 7.8, 0.3, -5.125, 3.9, -5.05, 0xd4c5ac);
block(scene, 1.75, 7.8, 0.3, 5.125, 3.9, -5.05, 0xd4c5ac);
// Left wall and simple wood trim.
block(scene, 0.22, 7.8, 13.5, -6.05, 3.9, 1.65, 0xc9baa3);
block(scene, 0.16, 1.02, 13.45, -5.88, 0.51, 1.65, 0x846044);
block(scene, 0.18, 0.14, 13.45, -5.86, 1.08, 1.65, 0x60462f);
block(scene, 12, 1.03, 0.15, 0, 0.515, -4.86, 0x846044);
block(scene, 12, 0.14, 0.2, 0, 1.08, -4.82, 0x60462f);
// Quiet vertical wall battens.
for (const x of [-4.8, -3.9, 3.9, 4.8]) {
block(scene, 0.055, 5.7, 0.055, x, 4.05, -4.86, 0xb5a68f);
}
// Window frame.
const frameColor = 0x4d493f;
block(scene, 0.24, 4.8, 0.32, -4.28, 4.03, -4.75, frameColor);
block(scene, 0.24, 4.8, 0.32, 4.28, 4.03, -4.75, frameColor);
block(scene, 8.8, 0.24, 0.34, 0, 1.68, -4.74, frameColor);
block(scene, 8.8, 0.24, 0.34, 0, 6.42, -4.74, frameColor);
// Subtle mullions, kept away from the central view.
block(scene, 0.09, 4.48, 0.18, -2.72, 4.04, -4.78, 0x565148);
block(scene, 0.09, 4.48, 0.18, 2.72, 4.04, -4.78, 0x565148);
block(scene, 8.32, 0.08, 0.18, 0, 6.05, -4.78, 0x565148);
// Sill.
block(scene, 9.12, 0.16, 0.78, 0, 1.5, -4.57, 0x7b6148);
block(scene, 8.85, 0.08, 0.48, 0, 1.39, -4.52, 0xa28766);
// Almost invisible glazing.
block(
scene,
8.35,
4.55,
0.025,
0,
4.04,
-4.87,
0xb8d2d9,
{
transparent: true,
opacity: 0.055,
depthWrite: false,
roughness: 0.12,
side: THREE.DoubleSide
}
);
// ---------------------------------------------------------------------
// Rainy courtyard
// ---------------------------------------------------------------------
block(scene, 24, 0.22, 14, 0, -0.18, -11.6, 0x59656a);
// Wet paving.
for (let row = 0; row < 8; row++) {
for (let col = 0; col < 10; col++) {
const x = -8.6 + col * 1.9 + (row % 2) * 0.35;
const z = -5.8 - row * 1.25;
const color = (row + col) % 3 === 0 ? 0x687479 : 0x616d72;
block(scene, 1.78, 0.07, 1.12, x, -0.035, z, color);
}
}
// Thin puddles with a darker, glossier surface.
block(scene, 2.4, 0.015, 0.8, -2.9, 0.015, -8.1, 0x41545c, {
roughness: 0.18,
metalness: 0.12
});
block(scene, 1.5, 0.016, 0.55, 2.6, 0.018, -10.6, 0x3c4e56, {
roughness: 0.16,
metalness: 0.1
});
block(scene, 2.0, 0.014, 0.6, 5.1, 0.016, -7.3, 0x43555c, {
roughness: 0.2,
metalness: 0.08
});
// Opposite building.
block(scene, 20, 9.4, 0.55, 0, 4.6, -16.35, 0x59666b);
for (let row = 0; row < 7; row++) {
block(scene, 19.5, 0.055, 0.08, 0, 0.65 + row * 1.28, -16.04, 0x4b585d);
}
// A few voxel brick accents.
const brickPositions = [
[-7.8, 1.2], [-5.9, 2.45], [4.3, 1.2], [6.2, 3.72],
[-1.5, 6.3], [3.8, 7.55], [-7.3, 7.55], [8.1, 6.3]
];
for (const [x, y] of brickPositions) {
block(scene, 0.72, 0.28, 0.09, x, y, -16.02, 0x6a6f6b);
}
// Warm windows across the courtyard.
const litWindowMaterial = {
emissive: 0xffb35d,
emissiveIntensity: 1.25,
roughness: 0.4
};
for (const x of [-6.4, -2.4, 2.4, 6.4]) {
for (const y of [2.25, 5.25]) {
block(scene, 1.55, 1.45, 0.09, x, y, -16.01, 0xd9b879, litWindowMaterial);
block(scene, 0.08, 1.48, 0.13, x, y, -15.93, 0x3e484b);
block(scene, 1.58, 0.08, 0.13, x, y, -15.93, 0x3e484b);
}
}
// Courtyard door.
block(scene, 1.45, 2.7, 0.11, 0, 1.35, -16.0, 0x3f4b50);
block(scene, 0.08, 2.68, 0.14, 0, 1.35, -15.9, 0x707777);
block(scene, 0.12, 0.12, 0.12, 0.48, 1.35, -15.78, 0xc59b55);
// Voxel tree.
block(scene, 0.42, 4.1, 0.42, -4.3, 2.0, -12.55, 0x50453b);
block(scene, 0.34, 1.65, 0.34, -3.75, 3.35, -12.55, 0x50453b);
block(scene, 0.34, 1.55, 0.34, -4.85, 3.6, -12.55, 0x50453b);
const foliage = [
[-4.3, 4.65, -12.55, 2.5, 1.25, 1.8],
[-3.15, 4.15, -12.6, 1.45, 1.2, 1.5],
[-5.3, 4.0, -12.6, 1.5, 1.3, 1.5],
[-4.35, 5.4, -12.55, 1.35, 0.9, 1.25]
];
for (const f of foliage) {
block(scene, ...f.slice(3, 6), ...f.slice(0, 3), 0x3f5549);
}
// Planters against the far wall.
for (const x of [-8.2, 8.2]) {
block(scene, 1.15, 0.72, 0.8, x, 0.36, -15.45, 0x6a5141);
block(scene, 0.95, 0.55, 0.65, x, 0.88, -15.45, 0x3f5947);
}
// ---------------------------------------------------------------------
// Window seat
// ---------------------------------------------------------------------
// Soft rug in front of the bench.
block(scene, 5.9, 0.045, 2.2, -0.1, 0.025, -0.65, 0x9d8870);
block(scene, 5.5, 0.018, 1.82, -0.1, 0.054, -0.65, 0xaa967d);
// Bench carcass and drawers.
block(scene, 8.4, 0.9, 2.1, -0.4, 0.67, -3.55, 0x78583e);
block(scene, 8.2, 0.73, 0.14, -0.4, 0.68, -2.46, 0x8b6748);
for (const x of [-2.9, -0.4, 2.1]) {
block(scene, 2.36, 0.56, 0.08, x, 0.69, -2.365, 0x97724f);
block(scene, 0.33, 0.07, 0.08, x, 0.83, -2.305, 0x4f4437);
}
// Three separate cushions.
for (const x of [-2.95, -0.4, 2.15]) {
block(scene, 2.48, 0.28, 2.12, x, 1.25, -3.52, 0xb9a68a);
block(scene, 2.4, 0.035, 2.0, x, 1.405, -3.52, 0xc6b397);
}
// Blanket folded over the right cushion.
const blanketColor = 0x657b82;
const blanketStripe = 0xb77b62;
block(scene, 2.4, 0.09, 1.78, 2.15, 1.45, -3.48, blanketColor);
block(scene, 2.4, 0.82, 0.11, 2.15, 1.0, -2.52, blanketColor);
block(scene, 0.12, 0.84, 1.8, 3.31, 0.98, -3.48, blanketColor);
for (const x of [1.48, 2.05, 2.62]) {
block(scene, 0.13, 0.018, 1.68, x, 1.505, -3.48, blanketStripe);
block(scene, 0.13, 0.75, 0.025, x, 1.0, -2.455, blanketStripe);
}
block(scene, 2.3, 0.018, 0.14, 2.15, 1.507, -3.1, blanketStripe);
block(scene, 2.3, 0.75, 0.025, 2.15, 1.0, -2.45, blanketStripe);
// ---------------------------------------------------------------------
// Books and side table
// ---------------------------------------------------------------------
function closedBook(parent, x, y, z, width, height, depth, color, rotation = 0) {
const book = group(parent, x, y, z);
book.rotation.y = rotation;
block(book, width * 0.92, height * 0.58, depth * 0.92, 0, 0, 0, 0xe6d9bd);
block(book, width, 0.07, depth, 0, height * 0.38, 0, color);
block(book, width, 0.07, depth, 0, -height * 0.38, 0, color);
block(book, 0.06, height * 0.61, depth * 0.94, -width * 0.46, 0, 0, color);
return book;
}
closedBook(scene, -2.95, 1.58, -3.35, 1.2, 0.23, 0.8, 0x8b4f46, -0.08);
closedBook(scene, -2.88, 1.83, -3.28, 1.08, 0.22, 0.74, 0x526d70, 0.06);
closedBook(scene, -2.98, 2.07, -3.35, 1.0, 0.2, 0.7, 0xb18a56, -0.04);
// Side table.
block(scene, 1.45, 0.14, 1.15, -4.83, 1.08, -3.25, 0x73543b);
block(scene, 0.16, 1.0, 0.16, -5.34, 0.52, -3.25, 0x5f4936);
block(scene, 0.16, 1.0, 0.16, -4.33, 0.52, -3.25, 0x5f4936);
block(scene, 0.16, 1.0, 0.16, -4.83, 0.52, -3.68, 0x5f4936);
block(scene, 0.16, 1.0, 0.16, -4.83, 0.52, -2.82, 0x5f4936);
// Stacked books and a cup.
closedBook(scene, -4.95, 1.24, -3.35, 0.75, 0.18, 0.56, 0x6b7773, 0.08);
closedBook(scene, -4.86, 1.44, -3.32, 0.67, 0.17, 0.52, 0x9a6954, -0.05);
block(scene, 0.32, 0.36, 0.32, -4.43, 1.35, -3.05, 0xb8a487);
block(scene, 0.25, 0.04, 0.25, -4.43, 1.55, -3.05, 0x493b31);
block(scene, 0.12, 0.23, 0.12, -4.25, 1.42, -3.05, 0xb8a487);
// Voxel reading lamp.
block(scene, 0.42, 0.12, 0.42, -5.03, 1.2, -3.72, 0x4a4439);
block(scene, 0.11, 1.25, 0.11, -5.03, 1.8, -3.72, 0x8b6c45);
block(scene, 0.72, 0.18, 0.62, -5.03, 2.45, -3.72, 0xc3a574);
block(scene, 0.55, 0.38, 0.47, -5.03, 2.65, -3.72, 0xe2c28d, {
emissive: 0xffbd68,
emissiveIntensity: 0.34,
roughness: 0.65
});
const lampLight = new THREE.PointLight(0xffbd72, 7, 6, 2);
lampLight.position.set(-5.03, 2.55, -3.55);
scene.add(lampLight);
// ---------------------------------------------------------------------
// Potted plant
// ---------------------------------------------------------------------
block(scene, 1.15, 0.75, 1.0, 4.7, 0.38, -3.55, 0x72513d);
block(scene, 1.24, 0.12, 1.08, 4.7, 0.8, -3.55, 0x876347);
block(scene, 0.77, 0.68, 0.77, 4.7, 1.13, -3.55, 0xa76548);
block(scene, 0.86, 0.14, 0.86, 4.7, 1.49, -3.55, 0xc27a56);
block(scene, 0.66, 0.08, 0.66, 4.7, 1.51, -3.55, 0x3e4935);
block(scene, 0.13, 1.8, 0.13, 4.7, 2.27, -3.55, 0x496347);
block(scene, 0.1, 0.8, 0.1, 4.42, 2.42, -3.55, 0x496347);
block(scene, 0.1, 0.85, 0.1, 4.98, 2.55, -3.55, 0x496347);
const leaves = [
[4.18, 2.7, -3.55, 0.65, 0.22, 0.48, -0.4],
[5.18, 2.82, -3.55, 0.65, 0.22, 0.48, 0.4],
[4.38, 3.03, -3.55, 0.48, 0.22, 0.48, -0.15],
[5.02, 3.2, -3.55, 0.5, 0.22, 0.46, 0.2],
[4.68, 3.48, -3.55, 0.42, 0.22, 0.42, 0]
];
for (const leaf of leaves) {
const mesh = block(scene, ...leaf.slice(3, 6), ...leaf.slice(0, 3), 0x587556);
mesh.rotation.z = leaf[6];
}
// ---------------------------------------------------------------------
// Reader
// ---------------------------------------------------------------------
const reader = group(scene, -0.1, 0, -3.2);
const trousers = 0x3b4752;
const sweater = 0x718075;
const sweaterDark = 0x5b695f;
const skin = 0xc99170;
const hair = 0x382b26;
// Seated lower body.
block(reader, 1.08, 0.45, 0.72, 0, 1.55, 0, trousers);
block(reader, 0.48, 0.42, 0.98, -0.28, 1.48, 0.48, trousers);
block(reader, 0.48, 0.42, 0.98, 0.28, 1.48, 0.48, trousers);
block(reader, 0.42, 0.83, 0.42, -0.28, 0.87, 0.88, trousers);
block(reader, 0.42, 0.83, 0.42, 0.28, 0.87, 0.88, trousers);
block(reader, 0.5, 0.22, 0.66, -0.28, 0.42, 1.03, 0x292d2f);
block(reader, 0.5, 0.22, 0.66, 0.28, 0.42, 1.03, 0x292d2f);
const upperBody = group(reader, 0, 1.7, 0);
block(upperBody, 1.08, 1.18, 0.66, 0, 0.55, 0, sweater);
block(upperBody, 0.3, 0.52, 0.025, 0, 0.58, 0.345, sweaterDark);
block(upperBody, 0.28, 0.22, 0.28, 0, 1.21, 0, skin);
// Arms, posed toward the book.
function makeArm(side) {
const shoulder = group(upperBody, side * 0.64, 1.02, 0.03);
shoulder.rotation.x = -0.55;
block(shoulder, 0.29, 0.69, 0.3, 0, -0.29, 0, sweater);
const elbow = group(shoulder, 0, -0.55, 0);
elbow.rotation.x = -0.77;
block(elbow, 0.25, 0.53, 0.26, 0, -0.23, 0, sweater);
block(elbow, 0.26, 0.12, 0.27, 0, -0.48, 0, sweaterDark);
block(elbow, 0.27, 0.18, 0.25, 0, -0.57, 0.015, skin);
return { shoulder, elbow };
}
const leftArm = makeArm(-1);
const rightArm = makeArm(1);
// Head and face.
const head = group(upperBody, 0, 1.55, 0);
block(head, 0.7, 0.72, 0.66, 0, 0, 0, skin);
block(head, 0.73, 0.24, 0.68, 0, 0.28, -0.015, hair);
block(head, 0.72, 0.43, 0.2, 0, 0.1, -0.26, hair);
block(head, 0.13, 0.13, 0.14, 0, -0.04, 0.39, skin);
block(head, 0.075, 0.06, 0.025, -0.18, 0.08, 0.34, 0x2b2522);
block(head, 0.075, 0.06, 0.025, 0.18, 0.08, 0.34, 0x2b2522);
block(head, 0.12, 0.23, 0.13, -0.37, -0.03, 0.01, skin);
block(head, 0.12, 0.23, 0.13, 0.37, -0.03, 0.01, skin);
block(head, 0.18, 0.045, 0.025, 0, -0.2, 0.34, 0x8d5d4b);
// Open book resting across the reader's lap.
const openBook = group(upperBody, 0, 0.23, 0.73);
block(openBook, 1.48, 0.07, 0.83, 0, 0, 0, 0x7e3e3c);
block(openBook, 0.065, 0.095, 0.86, 0, 0.035, 0, 0x59302e);
const leftPage = group(openBook, -0.35, 0.075, 0);
leftPage.rotation.z = -0.075;
block(leftPage, 0.66, 0.055, 0.73, 0, 0, 0, 0xe8ddc4);
for (let i = 0; i < 5; i++) {
block(leftPage, 0.42 - (i % 2) * 0.06, 0.012, 0.018, -0.025, 0.035, -0.24 + i * 0.12, 0x887d69);
}
const rightPage = group(openBook, 0.35, 0.075, 0);
rightPage.rotation.z = 0.075;
block(rightPage, 0.66, 0.055, 0.73, 0, 0, 0, 0xe8ddc4);
for (let i = 0; i < 5; i++) {
block(rightPage, 0.42 - (i % 2) * 0.06, 0.012, 0.018, 0.025, 0.035, -0.24 + i * 0.12, 0x887d69);
}
// ---------------------------------------------------------------------
// Courtyard rain and rain on the glass
// ---------------------------------------------------------------------
const rainCount = 180;
const rainMaterial = getMaterial(0xc3dce6, {
transparent: true,
opacity: 0.46,
depthWrite: false,
roughness: 0.3
});
const rain = new THREE.InstancedMesh(unitBox, rainMaterial, rainCount);
rain.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
rain.frustumCulled = false;
scene.add(rain);
const rainData = [];
const rainDummy = new THREE.Object3D();
for (let i = 0; i < rainCount; i++) {
rainData.push({
x: Math.random() * 20 - 10,
y: Math.random() * 8.5,
z: -5.35 - Math.random() * 10.2,
length: 0.18 + Math.random() * 0.42,
speed: 4.5 + Math.random() * 5
});
}
const glassRain = [];
const glassRainMaterial = getMaterial(0xd7e8eb, {
transparent: true,
opacity: 0.2,
depthWrite: false,
roughness: 0.15
});
for (let i = 0; i < 26; i++) {
const drop = new THREE.Mesh(unitBox, glassRainMaterial);
const length = 0.14 + Math.random() * 0.28;
drop.scale.set(0.018, length, 0.008);
drop.position.set(
-4.05 + Math.random() * 8.1,
1.9 + Math.random() * 4.25,
-4.79
);
scene.add(drop);
glassRain.push({ mesh: drop, speed: 1.0 + Math.random() * 1.3 });
}
// ---------------------------------------------------------------------
// Camera controls implemented directly, without addons
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 2.72, -1.35);
const view = {
azimuth: 0.52,
elevation: 0.24,
radius: 11.4,
targetAzimuth: 0.52,
targetElevation: 0.24,
targetRadius: 11.4
};
function resetView() {
view.targetAzimuth = 0.52;
view.targetElevation = 0.24;
view.targetRadius = 11.4;
}
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-delta * 7.5);
view.azimuth += (view.targetAzimuth - view.azimuth) * smoothing;
view.elevation += (view.targetElevation - view.elevation) * smoothing;
view.radius += (view.targetRadius - view.radius) * smoothing;
const horizontalRadius = Math.cos(view.elevation) * view.radius;
camera.position.set(
cameraTarget.x + Math.sin(view.azimuth) * horizontalRadius,
cameraTarget.y + Math.sin(view.elevation) * view.radius,
cameraTarget.z + Math.cos(view.azimuth) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
view.targetAzimuth = THREE.MathUtils.clamp(
view.targetAzimuth - dx * 0.004,
-0.12,
1.12
);
view.targetElevation = THREE.MathUtils.clamp(
view.targetElevation + dy * 0.003,
0.09,
0.49
);
});
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();
view.targetRadius = THREE.MathUtils.clamp(
view.targetRadius + event.deltaY * 0.008,
8.4,
15
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
resetButton.addEventListener("click", resetView);
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
updateCamera(delta);
// Fourteen-second cycle:
// reading → glance out → look at the courtyard → return to the page.
const cycle = 14;
const phase = (elapsed % cycle) / cycle;
let viewWeight = 0;
if (phase >= 0.27 && phase < 0.42) {
viewWeight = smoothstep(0.27, 0.42, phase);
} else if (phase >= 0.42 && phase < 0.62) {
viewWeight = 1;
} else if (phase >= 0.62 && phase < 0.78) {
viewWeight = 1 - smoothstep(0.62, 0.78, phase);
}
// The reader looks down at the book, then turns toward the window.
head.rotation.x = THREE.MathUtils.lerp(0.4, 0.025, viewWeight);
head.rotation.y = THREE.MathUtils.lerp(0, Math.PI * 0.94, viewWeight);
head.rotation.z = THREE.MathUtils.lerp(-0.025, 0.025, viewWeight);
upperBody.rotation.x = THREE.MathUtils.lerp(0.055, -0.015, viewWeight);
upperBody.rotation.y = THREE.MathUtils.lerp(0, 0.085, viewWeight);
// A restrained breathing motion keeps the room calm and alive.
const breathing = Math.sin(elapsed * 1.18) * 0.006;
upperBody.scale.y = 1 + breathing;
upperBody.position.y = 1.7 + Math.sin(elapsed * 1.18) * 0.006;
// The book lowers a little when attention moves outside.
openBook.position.y = THREE.MathUtils.lerp(0.23, 0.19, viewWeight);
openBook.rotation.x = THREE.MathUtils.lerp(0.03, 0.0, viewWeight);
leftArm.shoulder.rotation.x = THREE.MathUtils.lerp(-0.55, -0.49, viewWeight);
rightArm.shoulder.rotation.x = THREE.MathUtils.lerp(-0.55, -0.49, viewWeight);
// Courtyard rain.
for (let i = 0; i < rainCount; i++) {
const drop = rainData[i];
drop.y -= drop.speed * delta;
drop.x -= delta * 0.22;
if (drop.y < -0.1) {
drop.y = 7.2 + Math.random() * 2.3;
drop.x = Math.random() * 20 - 10;
}
rainDummy.position.set(drop.x, drop.y, drop.z);
rainDummy.rotation.set(0, 0, -0.075);
rainDummy.scale.set(0.022, drop.length, 0.022);
rainDummy.updateMatrix();
rain.setMatrixAt(i, rainDummy.matrix);
}
rain.instanceMatrix.needsUpdate = true;
// Slower rivulets on the window itself.
for (const drop of glassRain) {
drop.mesh.position.y -= drop.speed * delta;
if (drop.mesh.position.y < 1.85) {
drop.mesh.position.y = 6.15 + Math.random() * 0.25;
drop.mesh.position.x = -4.05 + Math.random() * 8.1;
}
}
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = width / height;
camera.fov = camera.aspect < 1 ? 50 : 38;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
updateCamera(1);
animate();
</script>
</body>
</html>
```