Prompt specification
Build or customize the Three.js 3D scene "Neighborhood tree seasonal courtyard" 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: Neighborhood tree seasonal courtyard
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-tree-seasonal-courtyard-x0779
Scene Description Prompt:
A stylized courtyard tree changes through the seasons while benches and paving stay fixed. Three.js from cdnjs, one HTML file.
Working Three.js Starter (neighborhood-tree-seasonal-courtyard-x0779):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#e8e1d5" />
<title>The Courtyard Tree</title>
<style>
:root {
--ink: #3f493f;
--muted: #768076;
--paper: rgba(250, 247, 240, 0.88);
--line: rgba(63, 73, 63, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
body {
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
background:
radial-gradient(circle at 50% 34%, #faf8f1 0%, #eee8dc 52%, #d9d1c3 100%);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.chrome {
position: fixed;
inset: 0;
pointer-events: none;
}
header {
position: absolute;
top: clamp(22px, 4vw, 46px);
left: clamp(22px, 4vw, 54px);
max-width: 380px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 64px);
font-weight: 400;
line-height: 0.96;
letter-spacing: -0.045em;
}
.subtitle {
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
.scene-tag {
position: absolute;
top: clamp(24px, 4vw, 48px);
right: clamp(22px, 4vw, 54px);
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(255, 255, 255, 0.35);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.15em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.controls {
position: absolute;
bottom: clamp(20px, 4vw, 42px);
left: 50%;
display: flex;
align-items: center;
gap: 8px;
width: max-content;
max-width: calc(100vw - 28px);
padding: 8px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 50px rgba(65, 57, 44, 0.13),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
pointer-events: auto;
transform: translateX(-50%);
backdrop-filter: blur(18px);
}
.season {
display: flex;
align-items: center;
gap: 8px;
min-height: 42px;
padding: 0 13px;
border: 0;
border-radius: 11px;
color: #6c746c;
background: transparent;
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
transition:
color 180ms ease,
background 180ms ease,
transform 180ms ease,
box-shadow 180ms ease;
}
.season:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.6);
}
.season.active {
color: var(--ink);
background: rgba(255, 255, 255, 0.95);
box-shadow: 0 4px 14px rgba(61, 57, 48, 0.09);
}
.season:active {
transform: scale(0.97);
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.spring .dot { background: #e7a0b5; }
.summer .dot { background: #5f955a; }
.autumn .dot { background: #d37b35; }
.winter .dot { background: #97816d; }
.divider {
width: 1px;
height: 25px;
margin: 0 3px;
background: var(--line);
}
.auto {
min-height: 42px;
padding: 0 14px;
border: 0;
border-radius: 11px;
color: var(--muted);
background: transparent;
font: inherit;
font-size: 11px;
font-weight: 650;
white-space: nowrap;
cursor: pointer;
transition: 180ms ease;
}
.auto:hover,
.auto.active {
color: var(--ink);
background: rgba(230, 225, 214, 0.75);
}
.hint {
position: absolute;
right: clamp(22px, 4vw, 54px);
bottom: clamp(27px, 4vw, 47px);
color: rgba(63, 73, 63, 0.64);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.season-label {
position: absolute;
left: 50%;
bottom: calc(clamp(20px, 4vw, 42px) + 76px);
color: rgba(63, 73, 63, 0.68);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.22em;
text-transform: uppercase;
transform: translateX(-50%);
}
button:focus-visible {
outline: 2px solid rgba(63, 73, 63, 0.6);
outline-offset: 2px;
}
@media (max-width: 800px) {
header {
max-width: 260px;
}
.subtitle {
max-width: 220px;
font-size: 11px;
}
.scene-tag,
.hint {
display: none;
}
.controls {
gap: 2px;
padding: 6px;
border-radius: 15px;
}
.season {
min-height: 40px;
padding: 0 9px;
font-size: 11px;
}
.season .dot {
width: 7px;
height: 7px;
}
.auto {
padding: 0 8px;
font-size: 0;
}
.auto::before {
content: "Auto";
font-size: 10px;
}
}
@media (max-width: 480px) {
h1 {
font-size: 36px;
}
.eyebrow {
font-size: 8px;
}
.season {
flex-direction: column;
gap: 4px;
min-width: 50px;
padding: 5px 8px;
font-size: 9px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="chrome">
<header>
<div class="eyebrow">A living courtyard study</div>
<h1>The Courtyard<br />Tree</h1>
<p class="subtitle">
Four seasons pass through the branches while the benches and paving remain still.
</p>
</header>
<div class="scene-tag">Static courtyard · Living tree</div>
<div class="season-label" id="season-label" aria-live="polite">Summer</div>
<nav class="controls" aria-label="Choose a season">
<button class="season spring" data-season="0" aria-pressed="false">
<span class="dot"></span>
Spring
</button>
<button class="season summer active" data-season="1" aria-pressed="true">
<span class="dot"></span>
Summer
</button>
<button class="season autumn" data-season="2" aria-pressed="false">
<span class="dot"></span>
Autumn
</button>
<button class="season winter" data-season="3" aria-pressed="false">
<span class="dot"></span>
Winter
</button>
<span class="divider"></span>
<button class="auto" id="auto" aria-pressed="false">Auto cycle</button>
</nav>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</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 stage = document.querySelector("#stage");
const seasonLabel = document.querySelector("#season-label");
const autoButton = document.querySelector("#auto");
const seasonButtons = [...document.querySelectorAll(".season")];
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xe9e4d9, 30, 75);
const camera = new THREE.PerspectiveCamera(
41,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
stage.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional courtyard. Use the mouse or arrow keys to change the view."
);
// Lighting remains constant so that only the tree changes with the season.
scene.add(new THREE.HemisphereLight(0xeaf3f3, 0x88745d, 1.7));
const sun = new THREE.DirectionalLight(0xfff0d7, 3.1);
sun.position.set(-8, 15, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 42;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc8dded, 0.7);
fill.position.set(9, 8, -11);
scene.add(fill);
const material = (color, roughness = 0.9, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const stoneMaterial = material(0xb8aa93);
const borderMaterial = material(0xc8bca6);
const wallMaterial = material(0xe0d7c8);
const capMaterial = material(0xcfc2aa);
const woodMaterial = material(0x80583a, 0.86);
const metalMaterial = material(0x454846, 0.68, 0.12);
const soilMaterial = material(0x59483b);
function addBox(size, position, mat, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Fixed courtyard foundation.
addBox([16.4, 0.5, 13.6], [0, -0.25, 0], stoneMaterial);
const tileGeometry = new THREE.BoxGeometry(1.16, 0.09, 1.12);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96
});
const columns = 13;
const rows = 11;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
columns * rows
);
tiles.receiveShadow = true;
tiles.frustumCulled = false;
const matrixDummy = new THREE.Object3D();
const tileColors = [0xd4cbbc, 0xd8d0c2, 0xcfc5b6, 0xd1c8ba];
let tileIndex = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
matrixDummy.position.set(
(column - (columns - 1) / 2) * 1.22,
0.045,
(row - (rows - 1) / 2) * 1.18
);
matrixDummy.rotation.set(0, (row + column) % 2 ? 0.006 : 0, 0);
matrixDummy.updateMatrix();
tiles.setMatrixAt(tileIndex, matrixDummy.matrix);
const color = new THREE.Color(
tileColors[(row * 3 + column * 2) % tileColors.length]
);
color.multiplyScalar(0.96 + ((row + column) % 3) * 0.018);
tiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// Fixed paving borders.
addBox([16.25, 0.18, 0.38], [0, 0.08, -6.56], borderMaterial);
addBox([16.25, 0.18, 0.38], [0, 0.08, 6.56], borderMaterial);
addBox([0.38, 0.18, 13.2], [-7.99, 0.08, 0], borderMaterial);
addBox([0.38, 0.18, 13.2], [7.99, 0.08, 0], borderMaterial);
// Low courtyard walls and stone caps.
addBox([16.0, 1.35, 0.34], [0, 0.68, -6.63], wallMaterial);
addBox([16.15, 0.16, 0.48], [0, 1.39, -6.63], capMaterial);
addBox([0.34, 1.08, 13.25], [-7.96, 0.55, 0], wallMaterial);
addBox([0.48, 0.16, 13.35], [-7.96, 1.13, 0], capMaterial);
addBox([0.34, 1.08, 13.25], [7.96, 0.55, 0], wallMaterial);
addBox([0.48, 0.16, 13.35], [7.96, 1.13, 0], capMaterial);
for (const x of [-7.75, 7.75]) {
for (const z of [-6.45, 0, 6.45]) {
addBox([0.52, 1.62, 0.52], [x, 0.81, z], wallMaterial);
addBox([0.67, 0.14, 0.67], [x, 1.67, z], capMaterial);
}
}
function createBench(x, z, rotation) {
const bench = new THREE.Group();
bench.position.set(x, 0.1, z);
bench.rotation.y = rotation;
for (let slat = 0; slat < 5; slat++) {
addBox(
[2.9, 0.13, 0.14],
[0, 0.74, -0.30 + slat * 0.15],
woodMaterial,
bench
);
}
for (const side of [-1, 1]) {
for (const depth of [-0.27, 0.27]) {
addBox(
[0.12, 0.72, 0.12],
[side * 1.08, 0.37, depth],
metalMaterial,
bench
);
}
addBox(
[0.11, 0.88, 0.11],
[side * 1.08, 1.08, -0.43],
metalMaterial,
bench
);
}
addBox([2.45, 0.10, 0.10], [0, 0.39, 0], metalMaterial, bench);
addBox([2.85, 0.16, 0.12], [0, 1.04, -0.45], woodMaterial, bench);
addBox([2.85, 0.16, 0.12], [0, 1.31, -0.45], woodMaterial, bench);
scene.add(bench);
}
// Benches are deliberately static.
createBench(-4.55, 0.25, Math.PI / 2);
createBench(4.55, -0.45, -Math.PI / 2);
// Circular tree planter.
const soil = new THREE.Mesh(
new THREE.CylinderGeometry(1.13, 1.13, 0.16, 40),
soilMaterial
);
soil.position.y = 0.16;
soil.receiveShadow = true;
scene.add(soil);
const planterRing = new THREE.Mesh(
new THREE.TorusGeometry(1.2, 0.12, 8, 48),
borderMaterial
);
planterRing.rotation.x = Math.PI / 2;
planterRing.position.y = 0.2;
planterRing.castShadow = true;
planterRing.receiveShadow = true;
scene.add(planterRing);
// Static tree trunk and branches.
const barkMaterial = material(0x654b3a, 0.98);
const branchMaterial = material(0x6d503b, 0.96);
const up = new THREE.Vector3(0, 1, 0);
function addBranch(a, b, bottomRadius, topRadius, mat = branchMaterial) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
topRadius,
bottomRadius,
length,
7,
1,
false
),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function addBranchPath(points, radii) {
for (let i = 0; i < points.length - 1; i++) {
addBranch(
points[i],
points[i + 1],
radii[i],
radii[i + 1]
);
}
}
addBranchPath(
[
[0, 0.2, 0],
[0.05, 1.35, 0.02],
[-0.08, 2.45, -0.02],
[0.08, 3.45, 0.04],
[0.15, 4.2, 0]
],
[0.46, 0.34, 0.25, 0.17, 0.1]
);
addBranchPath(
[[-0.05, 2.05, 0], [-1.45, 3.25, 0.1], [-2.65, 4.15, -0.35]],
[0.24, 0.14, 0.055]
);
addBranchPath(
[[-0.04, 2.75, 0.02], [-1.0, 4.05, 0.8], [-1.45, 5.0, 1.35]],
[0.18, 0.1, 0.045]
);
addBranchPath(
[[0.02, 2.2, -0.02], [1.45, 3.3, -0.12], [2.75, 4.15, -0.5]],
[0.23, 0.13, 0.055]
);
addBranchPath(
[[0.03, 2.9, 0.01], [1.05, 4.1, 1.0], [1.45, 5.1, 1.4]],
[0.18, 0.1, 0.045]
);
addBranchPath(
[[0.08, 3.0, -0.12], [0.3, 4.35, -1.2], [-0.3, 5.2, -2.0]],
[0.17, 0.095, 0.04]
);
addBranchPath(
[[-0.04, 2.45, 0.13], [-0.55, 3.65, 1.1], [-1.05, 4.65, 1.8]],
[0.18, 0.095, 0.04]
);
addBranchPath(
[[0.12, 3.65, 0], [0.3, 5.0, 0.1], [0.08, 5.7, 0]],
[0.14, 0.08, 0.035]
);
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
addBranch(
[0, 0.28, 0],
[Math.cos(angle) * 0.85, 0.18, Math.sin(angle) * 0.85],
0.18,
0.045,
barkMaterial
);
}
// Deterministic pseudo-random generator for reproducible foliage.
function mulberry32(seed) {
return function () {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(48291);
const leafCount = 118;
const leafData = [];
for (let i = 0; i < leafCount; i++) {
const angle = random() * Math.PI * 2;
const vertical = random() * 2 - 1;
const radial = Math.sqrt(1 - vertical * vertical);
const shell = 0.28 + 0.72 * Math.cbrt(random());
leafData.push({
x: Math.cos(angle) * radial * 3.25 * shell,
y: 4.55 + vertical * 1.55 * shell,
z: Math.sin(angle) * radial * 2.6 * shell,
scale: 0.48 + random() * 0.39,
threshold: 0.04 + random() * 0.78,
phase: random() * Math.PI * 2,
variant: Math.floor(random() * 3),
shade: 0.87 + random() * 0.23
});
}
const leafGeometry = new THREE.IcosahedronGeometry(1, 1);
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
flatShading: true
});
const leaves = new THREE.InstancedMesh(
leafGeometry,
leafMaterial,
leafCount
);
leaves.castShadow = true;
leaves.receiveShadow = true;
leaves.frustumCulled = false;
scene.add(leaves);
// Season definitions. Fullness controls how much of the canopy remains.
const seasons = [
{
name: "Spring",
fullness: 0.72,
palette: [0xf0aec1, 0xe692ad, 0xffd0dc]
},
{
name: "Summer",
fullness: 1.0,
palette: [0x4d8e52, 0x6ca45e, 0x3e7d49]
},
{
name: "Autumn",
fullness: 0.86,
palette: [0xd97b31, 0xe6a13e, 0xb94b2e]
},
{
name: "Winter",
fullness: 0.0,
palette: [0x8c705a, 0x9b7c63, 0x725e50]
}
];
const palettes = seasons.map(season =>
season.palette.map(color => new THREE.Color(color))
);
// One-hot target plus smoothly interpolated weights.
let targetSeason = 1;
const seasonWeights = [0, 1, 0, 0];
// Falling spring petals and autumn leaves.
const fallingCount = 30;
const fallingGeometry = new THREE.PlaneGeometry(0.13, 0.23);
const fallingMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
side: THREE.DoubleSide,
transparent: true,
opacity: 0.95
});
const fallingLeaves = new THREE.InstancedMesh(
fallingGeometry,
fallingMaterial,
fallingCount
);
fallingLeaves.frustumCulled = false;
fallingLeaves.visible = false;
scene.add(fallingLeaves);
const fallingData = [];
for (let i = 0; i < fallingCount; i++) {
fallingData.push({
x: (random() - 0.5) * 7.2,
y: 0.3 + random() * 6.1,
z: (random() - 0.5) * 5.8,
speed: 0.45 + random() * 0.65,
phase: random() * Math.PI * 2,
spin: 0.8 + random() * 1.8,
scale: 0.65 + random() * 0.55,
variant: Math.floor(random() * 3)
});
}
let autoCycle = false;
let autoElapsed = 0;
const autoDuration = 6.5;
function setSeason(index, manual = true) {
targetSeason = index;
seasonButtons.forEach((button, buttonIndex) => {
const active = buttonIndex === index;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
seasonLabel.textContent = seasons[index].name;
if (manual) {
autoCycle = false;
autoButton.classList.remove("active");
autoButton.setAttribute("aria-pressed", "false");
autoButton.textContent = "Auto cycle";
}
}
seasonButtons.forEach(button => {
button.addEventListener("click", () => {
setSeason(Number(button.dataset.season), true);
});
});
autoButton.addEventListener("click", () => {
autoCycle = !autoCycle;
autoElapsed = 0;
autoButton.classList.toggle("active", autoCycle);
autoButton.setAttribute("aria-pressed", String(autoCycle));
autoButton.textContent = autoCycle ? "Auto: On" : "Auto cycle";
});
// Custom orbit and zoom controls, implemented without camera addons.
const orbitTarget = new THREE.Vector3(0, 2.35, 0);
let currentAzimuth = 0.72;
let desiredAzimuth = 0.72;
let currentPolar = 1.07;
let desiredPolar = 1.07;
let currentRadius = 20;
let desiredRadius = 20;
let fitRadius = 19;
const pointers = new Map();
let lastPointerX = 0;
let lastPointerY = 0;
let lastPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
lastPinchDistance = pointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
desiredAzimuth -= deltaX * 0.006;
desiredPolar = THREE.MathUtils.clamp(
desiredPolar - deltaY * 0.005,
0.68,
1.42
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (lastPinchDistance > 0 && distance > 0) {
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * (lastPinchDistance / distance),
11,
55
);
}
lastPinchDistance = distance;
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = pointers.size === 2 ? pointerDistance() : 0;
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius * Math.exp(event.deltaY * 0.001),
11,
55
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
desiredAzimuth = 0.72;
desiredPolar = 1.07;
desiredRadius = fitRadius;
});
renderer.domElement.addEventListener("keydown", event => {
const key = event.key;
if (key === "ArrowLeft") desiredAzimuth -= 0.1;
else if (key === "ArrowRight") desiredAzimuth += 0.1;
else if (key === "ArrowUp") desiredPolar -= 0.08;
else if (key === "ArrowDown") desiredPolar += 0.08;
else if (key === "+" || key === "=") desiredRadius -= 1;
else if (key === "-") desiredRadius += 1;
else return;
event.preventDefault();
desiredPolar = THREE.MathUtils.clamp(desiredPolar, 0.68, 1.42);
desiredRadius = THREE.MathUtils.clamp(desiredRadius, 11, 55);
});
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
const nextFitRadius = Math.max(19, 24 / camera.aspect);
desiredRadius *= nextFitRadius / fitRadius;
fitRadius = nextFitRadius;
}
addEventListener("resize", resize);
resize();
function smoothstep(min, max, value) {
const t = THREE.MathUtils.clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
const blendedLeafColor = new THREE.Color();
const instanceColor = new THREE.Color();
const clock = new THREE.Clock();
let elapsed = 0;
function updateFoliage(delta, time) {
const blend = 1 - Math.exp(-delta * 2.15);
for (let i = 0; i < seasonWeights.length; i++) {
const desired = i === targetSeason ? 1 : 0;
seasonWeights[i] += (desired - seasonWeights[i]) * blend;
}
const totalWeight = seasonWeights.reduce((sum, value) => sum + value, 0);
for (let i = 0; i < seasonWeights.length; i++) {
seasonWeights[i] /= totalWeight;
}
let fullness = 0;
for (let i = 0; i < seasonWeights.length; i++) {
fullness += seasonWeights[i] * seasons[i].fullness;
}
for (let i = 0; i < leafData.length; i++) {
const leaf = leafData[i];
const visible = smoothstep(
leaf.threshold,
leaf.threshold + 0.16,
fullness
);
const breeze = Math.sin(time * 0.8 + leaf.phase) * 0.045 * visible;
const breezeZ = Math.cos(time * 0.66 + leaf.phase * 1.3) * 0.035 * visible;
matrixDummy.position.set(
leaf.x + breeze,
leaf.y + Math.sin(time * 0.5 + leaf.phase) * 0.025 * visible,
leaf.z + breezeZ
);
const pulse = 1 + Math.sin(time * 1.15 + leaf.phase) * 0.025;
const scale = leaf.scale * visible * (0.62 + fullness * 0.38) * pulse;
matrixDummy.scale.setScalar(scale);
matrixDummy.rotation.set(
leaf.phase * 0.1,
leaf.phase + time * 0.05,
leaf.phase * 0.08
);
matrixDummy.updateMatrix();
leaves.setMatrixAt(i, matrixDummy.matrix);
let red = 0;
let green = 0;
let blue = 0;
for (let seasonIndex = 0; seasonIndex < seasons.length; seasonIndex++) {
const color = palettes[seasonIndex][leaf.variant];
const weight = seasonWeights[seasonIndex];
red += color.r * weight;
green += color.g * weight;
blue += color.b * weight;
}
blendedLeafColor.setRGB(red, green, blue);
instanceColor
.copy(blendedLeafColor)
.multiplyScalar(leaf.shade);
instanceColor.r = THREE.MathUtils.clamp(instanceColor.r, 0, 1);
instanceColor.g = THREE.MathUtils.clamp(instanceColor.g, 0, 1);
instanceColor.b = THREE.MathUtils.clamp(instanceColor.b, 0, 1);
leaves.setColorAt(i, instanceColor);
}
leaves.instanceMatrix.needsUpdate = true;
leaves.instanceColor.needsUpdate = true;
const fallAmount = THREE.MathUtils.clamp(
seasonWeights[0] + seasonWeights[2],
0,
1
);
fallingLeaves.visible = fallAmount > 0.01;
if (fallingLeaves.visible) {
const springWeight = seasonWeights[0];
const autumnWeight = seasonWeights[2];
const total = Math.max(0.001, springWeight + autumnWeight);
for (let i = 0; i < fallingData.length; i++) {
const leaf = fallingData[i];
leaf.y -= delta * leaf.speed;
leaf.x += Math.sin(time * 1.2 + leaf.phase) * delta * 0.28;
leaf.z += Math.cos(time * 0.9 + leaf.phase) * delta * 0.22;
if (leaf.y < 0.12) {
leaf.y = 5.2 + random() * 1.4;
leaf.x = (random() - 0.5) * 7.2;
leaf.z = (random() - 0.5) * 5.8;
}
matrixDummy.position.set(leaf.x, leaf.y, leaf.z);
matrixDummy.rotation.set(
time * leaf.spin + leaf.phase,
time * 0.75,
Math.sin(time + leaf.phase) * 0.8
);
matrixDummy.scale.setScalar(
leaf.scale * Math.min(1, fallAmount * 1.3)
);
matrixDummy.updateMatrix();
fallingLeaves.setMatrixAt(i, matrixDummy.matrix);
const springColor = palettes[0][leaf.variant];
const autumnColor = palettes[2][leaf.variant];
instanceColor.setRGB(
(springColor.r * springWeight + autumnColor.r * autumnWeight) / total,
(springColor.g * springWeight + autumnColor.g * autumnWeight) / total,
(springColor.b * springWeight + autumnColor.b * autumnWeight) / total
);
fallingLeaves.setColorAt(i, instanceColor);
}
fallingLeaves.instanceMatrix.needsUpdate = true;
fallingLeaves.instanceColor.needsUpdate = true;
}
}
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-delta * 9);
currentAzimuth += (desiredAzimuth - currentAzimuth) * smoothing;
currentPolar += (desiredPolar - currentPolar) * smoothing;
currentRadius += (desiredRadius - currentRadius) * smoothing;
const sinPolar = Math.sin(currentPolar);
camera.position.set(
orbitTarget.x + currentRadius * sinPolar * Math.sin(currentAzimuth),
orbitTarget.y + currentRadius * Math.cos(currentPolar),
orbitTarget.z + currentRadius * sinPolar * Math.cos(currentAzimuth)
);
camera.lookAt(orbitTarget);
}
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (autoCycle) {
autoElapsed += delta;
if (autoElapsed >= autoDuration) {
autoElapsed = 0;
setSeason((targetSeason + 1) % seasons.length, false);
}
}
updateCamera(delta);
updateFoliage(delta, elapsed);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```