Prompt specification
Build or customize the Three.js 3D scene "One branch through four seasons" 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: One branch through four seasons
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/one-branch-through-four-seasons-x0778
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts seasonal deciduous foliage. Animate a selected temperate branch through budding, leaf expansion, color change, and leaf fall; label this as one regional seasonal pattern rather than a global rule. Low-poly.
Working Three.js Starter (one-branch-through-four-seasons-x0778):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Seasonal Deciduous Branch — Regional Pattern</title>
<style>
:root {
--ink: #24332e;
--muted: #63716b;
--panel: rgba(248, 246, 235, 0.90);
--line: rgba(46, 66, 57, 0.16);
--accent: #c97838;
--green: #68874d;
--shadow: 0 16px 45px rgba(35, 49, 43, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce7e1;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#app canvas {
display: block;
touch-action: none;
cursor: grab;
}
#app canvas.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
pointer-events: none;
}
.title-block {
top: 28px;
left: 30px;
max-width: 470px;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
padding: 6px 10px;
border: 1px solid rgba(48, 72, 61, 0.15);
border-radius: 999px;
background: rgba(248, 246, 235, 0.72);
color: #52685c;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
content: "";
box-shadow: 0 0 0 4px rgba(201, 120, 56, 0.12);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4.3vw, 56px);
font-weight: 500;
letter-spacing: -0.035em;
line-height: 0.98;
text-wrap: balance;
text-shadow: 0 2px 20px rgba(238, 244, 236, 0.45);
}
.subtitle {
max-width: 430px;
margin: 13px 0 0;
color: #53655d;
font-size: 14px;
line-height: 1.55;
}
.context-card {
top: 28px;
right: 28px;
width: min(340px, calc(100vw - 40px));
padding: 18px 19px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.context-card h2 {
margin: 0 0 11px;
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.context-card p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.context-card strong {
color: var(--ink);
}
.regional-note {
margin-top: 13px !important;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.regional-note b {
color: #9b572e;
}
.branch-marker {
top: 0;
left: 0;
z-index: 4;
display: flex;
align-items: center;
gap: 8px;
opacity: 1;
transform: translate(-50%, -100%);
transition: opacity 150ms ease;
pointer-events: none;
}
.branch-marker.hidden {
opacity: 0;
}
.branch-marker .dot {
width: 10px;
height: 10px;
border: 2px solid #fff5dc;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 5px rgba(201, 120, 56, 0.16);
}
.branch-marker .label {
padding: 6px 9px;
border: 1px solid rgba(48, 72, 61, 0.13);
border-radius: 8px;
background: rgba(250, 247, 235, 0.90);
box-shadow: 0 5px 18px rgba(35, 49, 43, 0.10);
color: #526158;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.05em;
white-space: nowrap;
}
.season-panel {
position: fixed;
z-index: 6;
right: 28px;
bottom: 25px;
left: 28px;
width: min(930px, calc(100vw - 56px));
margin: 0 auto;
padding: 17px 19px 15px;
border: 1px solid var(--line);
border-radius: 20px;
background: rgba(249, 247, 237, 0.91);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.season-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
margin-bottom: 12px;
}
.season-title {
display: flex;
align-items: baseline;
gap: 10px;
flex-wrap: wrap;
}
.season-title strong {
font-family: Georgia, "Times New Roman", serif;
font-size: 22px;
font-weight: 500;
}
.season-title span {
color: var(--muted);
font-size: 12px;
}
.season-description {
max-width: 510px;
margin: 4px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.transport {
display: flex;
align-items: center;
gap: 8px;
flex: 0 0 auto;
}
.transport button,
.reset-view {
min-height: 34px;
padding: 7px 12px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.62);
cursor: pointer;
font-size: 12px;
font-weight: 700;
transition:
background 140ms ease,
transform 140ms ease;
}
.transport button:hover,
.reset-view:hover {
background: #fff;
transform: translateY(-1px);
}
.transport #playButton {
min-width: 70px;
border-color: transparent;
background: var(--ink);
color: #f8f4e6;
}
#timeline {
--progress: 0%;
width: 100%;
height: 6px;
margin: 8px 0 13px;
border-radius: 999px;
outline: none;
appearance: none;
background:
linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--progress),
rgba(47, 67, 58, 0.13) var(--progress),
rgba(47, 67, 58, 0.13) 100%
);
cursor: pointer;
}
#timeline::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #f8f4e6;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 2px 8px rgba(61, 53, 38, 0.28);
}
#timeline::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #f8f4e6;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(61, 53, 38, 0.28);
}
.stages {
display: grid;
grid-template-columns: repeat(5, 1fr);
gap: 6px;
}
.stage {
position: relative;
padding: 7px 8px 8px;
border: 0;
border-top: 2px solid rgba(47, 67, 58, 0.12);
background: transparent;
color: #7b857f;
cursor: pointer;
text-align: left;
font-size: 11px;
transition:
color 140ms ease,
border-color 140ms ease;
}
.stage:hover,
.stage.active {
border-top-color: var(--accent);
color: var(--ink);
}
.stage span {
display: block;
margin-top: 2px;
color: #8c958f;
font-size: 9px;
}
.camera-hint {
position: fixed;
z-index: 5;
bottom: 29px;
left: 30px;
padding: 8px 11px;
border: 1px solid rgba(46, 66, 57, 0.13);
border-radius: 9px;
background: rgba(248, 246, 235, 0.72);
color: #65736c;
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(10px);
}
.reset-view {
position: fixed;
z-index: 7;
right: 30px;
bottom: 185px;
background: rgba(248, 246, 235, 0.86);
backdrop-filter: blur(10px);
}
@media (max-width: 900px) {
.context-card {
top: auto;
right: 18px;
bottom: 188px;
width: 290px;
}
.title-block {
top: 20px;
left: 20px;
}
.camera-hint {
display: none;
}
.reset-view {
right: 20px;
bottom: 188px;
}
}
@media (max-width: 680px) {
.title-block {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
h1 {
max-width: 320px;
font-size: 34px;
}
.subtitle {
display: none;
}
.context-card {
display: none;
}
.season-panel {
right: 12px;
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px;
}
.season-head {
display: block;
}
.transport {
margin-top: 10px;
}
.stage {
padding-right: 2px;
font-size: 9px;
}
.stage span {
display: none;
}
.reset-view {
right: 14px;
bottom: 164px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header class="hud title-block">
<div class="eyebrow">Low-poly field study</div>
<h1>Seasonal deciduous branch</h1>
<p class="subtitle">
A selected temperate branch moves through budding, leaf expansion,
autumn color, and leaf fall.
</p>
</header>
<aside class="hud context-card">
<h2>Regional example</h2>
<p>
<strong>Example region:</strong> a temperate mid-latitude woodland with
deciduous trees.
</p>
<p class="regional-note">
<b>One regional pattern, not a global rule.</b>
Budding, color change, and leaf fall vary with species, latitude,
elevation, weather, and the specific year.
</p>
</aside>
<div id="branchMarker" class="branch-marker hidden" aria-hidden="true">
<span class="dot"></span>
<span class="label">Selected branch</span>
</div>
<footer class="season-panel">
<div class="season-head">
<div>
<div class="season-title">
<strong id="stageName">Budding</strong>
<span id="phaseValue">0% through illustrative cycle</span>
</div>
<p id="stageDescription" class="season-description">
Swollen buds emerge along the selected twig.
</p>
</div>
<div class="transport">
<button id="playButton" type="button" aria-label="Pause animation">
Pause
</button>
<button id="speedButton" type="button" aria-label="Change playback speed">
1× speed
</button>
</div>
</div>
<input
id="timeline"
type="range"
min="0"
max="1000"
value="0"
aria-label="Seasonal timeline"
>
<div class="stages">
<button class="stage active" type="button" data-start="0">
Budding <span>0–18%</span>
</button>
<button class="stage" type="button" data-start="0.18">
Leaf expansion <span>18–48%</span>
</button>
<button class="stage" type="button" data-start="0.48">
Color change <span>48–74%</span>
</button>
<button class="stage" type="button" data-start="0.74">
Leaf fall <span>74–94%</span>
</button>
<button class="stage" type="button" data-start="0.94">
Dormant branch <span>94–100%</span>
</button>
</div>
</footer>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Click the branch to highlight it</div>
<button id="resetView" class="reset-view" type="button">Reset view</button>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, lights
// -------------------------------------------------------------------------
const app = document.querySelector("#app");
const timeline = document.querySelector("#timeline");
const playButton = document.querySelector("#playButton");
const speedButton = document.querySelector("#speedButton");
const stageName = document.querySelector("#stageName");
const stageDescription = document.querySelector("#stageDescription");
const phaseValue = document.querySelector("#phaseValue");
const stageButtons = [...document.querySelectorAll(".stage")];
const branchMarker = document.querySelector("#branchMarker");
const resetViewButton = document.querySelector("#resetView");
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.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce7e1);
scene.fog = new THREE.Fog(0xdce7e1, 10, 23);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const target = new THREE.Vector3(0, 1.35, 0);
let yaw = 0.42;
let pitch = 0.34;
let cameraRadius = 9.7;
function updateCamera() {
const sinPitch = Math.sin(pitch);
camera.position.set(
target.x + cameraRadius * sinPitch * Math.sin(yaw),
target.y + cameraRadius * Math.cos(pitch),
target.z + cameraRadius * sinPitch * Math.cos(yaw)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff8df, 0x66775f, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc5, 3.1);
sun.position.set(5, 9, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(1024, 1024);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -5;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 25;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc8dfdc, 0.8);
fill.position.set(-6, 4, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and low-poly helpers
// -------------------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
const trunkMaterial = new THREE.MeshStandardMaterial({
color: 0x68503b,
roughness: 1,
flatShading: true
});
const selectedBranchMaterial = new THREE.MeshStandardMaterial({
color: 0x74553b,
emissive: 0x9f5d24,
emissiveIntensity: 0.07,
roughness: 0.94,
flatShading: true
});
const twigMaterial = new THREE.MeshStandardMaterial({
color: 0x806144,
roughness: 1,
flatShading: true
});
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x899a6d,
roughness: 1,
flatShading: true
});
function cylinderBetween(a, b, radiusA, radiusB, material, radialSegments = 5) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radiusB,
radiusA,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(UP, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground and distant low-poly woodland
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(12, 24),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.43;
ground.receiveShadow = true;
scene.add(ground);
const rockMaterial = new THREE.MeshStandardMaterial({
color: 0x9a9b86,
roughness: 1,
flatShading: true
});
const rockGeometry = new THREE.DodecahedronGeometry(1, 0);
[
[-3.8, -1.16, -1.7, 0.55],
[3.9, -1.19, -2.5, 0.42],
[1.4, -1.28, -4.4, 0.65],
[-5.5, -1.22, -3.4, 0.36]
].forEach(([x, y, z, scale]) => {
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(x, y, z);
rock.scale.set(scale * 1.3, scale * 0.65, scale);
rock.rotation.set(0.3, x, 0.15);
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
});
const distantCanopyGeometry = new THREE.IcosahedronGeometry(1, 1);
function addDistantTree(x, z, scale, canopyColor) {
const group = new THREE.Group();
group.position.set(x, -1.4, z);
group.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.14, 0.24, 2.8, 5),
trunkMaterial
);
trunk.position.y = 1.4;
trunk.castShadow = true;
group.add(trunk);
const canopyMaterial = new THREE.MeshStandardMaterial({
color: canopyColor,
roughness: 1,
flatShading: true
});
const clusters = [
[-0.45, 2.8, 0.0, 1.25],
[0.45, 3.0, 0.1, 1.30],
[0.00, 3.65, -0.1, 1.15],
[-0.75, 3.55, 0.15, 0.90],
[0.85, 3.65, -0.15, 0.85]
];
clusters.forEach(([cx, cy, cz, s]) => {
const canopy = new THREE.Mesh(
distantCanopyGeometry,
canopyMaterial
);
canopy.position.set(cx, cy, cz);
canopy.scale.set(s * 1.15, s * 0.9, s);
canopy.rotation.set(cx, cy, cz);
canopy.castShadow = true;
group.add(canopy);
});
scene.add(group);
}
addDistantTree(-6.4, -5.4, 1.15, 0x81916b);
addDistantTree(5.4, -6.8, 1.05, 0x758766);
addDistantTree(1.0, -8.3, 0.85, 0x8a9877);
// -------------------------------------------------------------------------
// Main tree and selected branch
// -------------------------------------------------------------------------
const trunkPoints = [
new THREE.Vector3(-3.0, -1.4, 0.0),
new THREE.Vector3(-2.85, -0.45, 0.05),
new THREE.Vector3(-2.62, 0.55, 0.0),
new THREE.Vector3(-2.42, 1.55, -0.1),
new THREE.Vector3(-2.24, 2.45, -0.16)
];
for (let i = 0; i < trunkPoints.length - 1; i++) {
cylinderBetween(
trunkPoints[i],
trunkPoints[i + 1],
0.34 - i * 0.045,
0.29 - i * 0.045,
trunkMaterial,
6
);
}
cylinderBetween(
new THREE.Vector3(-2.65, 0.45, 0),
new THREE.Vector3(-3.75, 1.35, -0.8),
0.19,
0.07,
trunkMaterial
);
cylinderBetween(
new THREE.Vector3(-2.48, 1.28, -0.05),
new THREE.Vector3(-1.6, 2.25, -1.0),
0.15,
0.045,
trunkMaterial
);
const selectedCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-2.48, 0.48, 0.04),
new THREE.Vector3(-1.72, 0.88, 0.08),
new THREE.Vector3(-0.78, 1.40, 0.02),
new THREE.Vector3(0.18, 1.92, 0.08),
new THREE.Vector3(1.15, 2.39, 0.02),
new THREE.Vector3(2.13, 2.98, 0.14),
new THREE.Vector3(3.05, 3.58, 0.25)
]);
const selectedMeshes = [];
const branchSegments = 13;
for (let i = 0; i < branchSegments; i++) {
const t0 = i / branchSegments;
const t1 = (i + 1) / branchSegments;
const a = selectedCurve.getPointAt(t0);
const b = selectedCurve.getPointAt(t1);
const mesh = cylinderBetween(
a,
b,
THREE.MathUtils.lerp(0.145, 0.042, t0),
THREE.MathUtils.lerp(0.145, 0.042, t1),
selectedBranchMaterial
);
selectedMeshes.push(mesh);
}
const twigData = [];
for (let i = 0; i < 11; i++) {
const t = 0.10 + (i / 10) * 0.78;
const start = selectedCurve.getPointAt(t);
const side = i % 2 === 0 ? 1 : -1;
const end = start.clone().add(
new THREE.Vector3(
side * (0.50 + Math.random() * 0.38),
0.28 + Math.random() * 0.44,
(Math.random() - 0.5) * 0.68
)
);
const twig = cylinderBetween(
start,
end,
0.044,
0.016,
twigMaterial
);
selectedMeshes.push(twig);
twigData.push({ start, end });
}
const selectionPin = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.095, 0),
new THREE.MeshStandardMaterial({
color: 0xf3b05b,
emissive: 0xd27628,
emissiveIntensity: 0.45,
roughness: 0.8,
flatShading: true
})
);
selectionPin.position.copy(selectedCurve.getPointAt(0.025));
scene.add(selectionPin);
// -------------------------------------------------------------------------
// Faceted leaf geometry: four triangular facets with a raised center
// -------------------------------------------------------------------------
const leafGeometry = new THREE.BufferGeometry();
leafGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
0.00, 0.00, 0.00,
-0.34, 0.28, 0.00,
0.00, 0.76, 0.00,
0.34, 0.28, 0.00,
0.00, 0.31, 0.10
], 3)
);
leafGeometry.setIndex([
0, 1, 4,
1, 2, 4,
2, 3, 4,
3, 0, 4
]);
leafGeometry.computeVertexNormals();
const budGeometry = new THREE.IcosahedronGeometry(0.105, 0);
const budMaterial = new THREE.MeshStandardMaterial({
color: 0x78513e,
roughness: 0.95,
flatShading: true
});
const leafRecords = [];
const autumnPalette = [
0xd6a72f,
0xd37a2f,
0xc05b32,
0xa84635,
0xd0a13a
];
function randomAutumnColor() {
return new THREE.Color(
autumnPalette[
Math.floor(Math.random() * autumnPalette.length)
]
);
}
function makeLeafRecord(position, direction, sizeFactor = 1) {
const green = new THREE.Color().setHSL(
0.235 + Math.random() * 0.065,
0.38 + Math.random() * 0.15,
0.28 + Math.random() * 0.10
);
const material = new THREE.MeshStandardMaterial({
color: green,
roughness: 0.88,
metalness: 0,
side: THREE.DoubleSide,
flatShading: true
});
const mesh = new THREE.Mesh(leafGeometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
const baseQuaternion = new THREE.Quaternion().setFromUnitVectors(
UP,
direction.clone().normalize()
);
const twist = new THREE.Quaternion().setFromAxisAngle(
UP,
Math.random() * Math.PI * 2
);
baseQuaternion.multiply(twist);
const lieQuaternion = new THREE.Quaternion().setFromUnitVectors(
UP,
new THREE.Vector3(
Math.cos(Math.random() * Math.PI * 2),
0,
Math.sin(Math.random() * Math.PI * 2)
)
);
const bud = new THREE.Mesh(budGeometry, budMaterial);
bud.position.copy(position).addScaledVector(direction, 0.065);
bud.castShadow = true;
scene.add(bud);
leafRecords.push({
mesh,
bud,
green,
autumn: randomAutumnColor(),
basePosition: position.clone(),
baseQuaternion,
lieQuaternion,
growthStart: 0.075 + Math.random() * 0.075,
growthEnd: 0.34 + Math.random() * 0.145,
fallStart: 0.745 + Math.random() * 0.065,
fallDuration: 0.11 + Math.random() * 0.048,
size: (0.76 + Math.random() * 0.40) * sizeFactor,
drift: new THREE.Vector3(
(Math.random() - 0.5) * 1.55,
0,
(Math.random() - 0.5) * 1.15
)
});
}
for (let i = 0; i < 18; i++) {
const t = 0.075 + (i / 17) * 0.855;
const position = selectedCurve.getPointAt(t);
const tangent = selectedCurve.getTangentAt(t);
const side = new THREE.Vector3(
-tangent.z,
0,
tangent.x
);
if (side.lengthSq() < 0.0001) {
side.set(1, 0, 0);
} else {
side.normalize();
}
const direction = tangent.clone()
.multiplyScalar(0.18)
.addScaledVector(side, (i % 2 ? 1 : -1) * (0.58 + Math.random() * 0.55))
.add(
new THREE.Vector3(
(Math.random() - 0.5) * 0.35,
0.22 + Math.random() * 0.54,
(Math.random() - 0.5) * 0.44
)
)
.normalize();
makeLeafRecord(position, direction, 1);
}
for (let i = 0; i < 18; i++) {
const twig = twigData[i % twigData.length];
const t = 0.32 + Math.random() * 0.68;
const position = twig.start.clone().lerp(twig.end, t);
const tangent = twig.end.clone().sub(twig.start).normalize();
const direction = new THREE.Vector3(
(Math.random() - 0.5) * 1.25,
0.28 + Math.random() * 0.72,
(Math.random() - 0.5) * 1.25
)
.addScaledVector(tangent, 0.28)
.normalize();
makeLeafRecord(position, direction, 0.86);
}
// -------------------------------------------------------------------------
// Seasonal animation
// -------------------------------------------------------------------------
const stages = [
{
start: 0,
end: 0.18,
name: "Budding",
description: "Swollen buds emerge along the selected twig."
},
{
start: 0.18,
end: 0.48,
name: "Leaf expansion",
description: "New leaves unfurl, enlarge, and form a loose canopy."
},
{
start: 0.48,
end: 0.74,
name: "Color change",
description: "Leaves shift through yellow, orange, and red."
},
{
start: 0.74,
end: 0.94,
name: "Leaf fall",
description: "Leaves detach, drift downward, and settle on the ground."
},
{
start: 0.94,
end: 1.001,
name: "Dormant branch",
description: "The selected branch is bare before the next regional bud cycle."
}
];
const yellow = new THREE.Color(0xd2b43c);
const landingY = -1.365;
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smoothstep(edge0, edge1, value) {
const x = clamp01((value - edge0) / (edge1 - edge0));
return x * x * (3 - 2 * x);
}
function updateSeason(phase) {
for (const leaf of leafRecords) {
const growth = smoothstep(
leaf.growthStart,
leaf.growthEnd,
phase
);
const scale = 0.005 + growth * 0.995;
leaf.mesh.scale.setScalar(scale * leaf.size);
const budGrowth = smoothstep(0.0, 0.10, phase);
const budFade = 1 - smoothstep(
leaf.growthStart - 0.025,
leaf.growthEnd - 0.07,
phase
);
const budScale = (0.18 + budGrowth * 0.82) * budFade;
leaf.bud.scale.setScalar(Math.max(0.001, budScale));
if (phase < 0.48) {
leaf.mesh.material.color.copy(leaf.green);
} else if (phase < 0.64) {
leaf.mesh.material.color
.copy(leaf.green)
.lerp(yellow, smoothstep(0.48, 0.64, phase));
} else if (phase < 0.74) {
leaf.mesh.material.color
.copy(yellow)
.lerp(leaf.autumn, smoothstep(0.64, 0.74, phase));
} else {
leaf.mesh.material.color.copy(leaf.autumn);
}
const fall = clamp01(
(phase - leaf.fallStart) / leaf.fallDuration
);
if (fall <= 0) {
leaf.mesh.position.copy(leaf.basePosition);
leaf.mesh.quaternion.copy(leaf.baseQuaternion);
} else {
const gravity = fall * fall;
const horizontal = fall;
leaf.mesh.position.set(
leaf.basePosition.x + leaf.drift.x * horizontal,
THREE.MathUtils.lerp(
leaf.basePosition.y,
landingY,
gravity
),
leaf.basePosition.z + leaf.drift.z * horizontal
);
leaf.mesh.quaternion
.copy(leaf.baseQuaternion)
.slerp(leaf.lieQuaternion, smoothstep(0.10, 1.0, fall));
leaf.mesh.scale.setScalar(
leaf.size * (1 - 0.16 * fall)
);
}
}
}
// -------------------------------------------------------------------------
// Custom camera controls — no addons or example modules
// -------------------------------------------------------------------------
let dragging = false;
let lastX = 0;
let lastY = 0;
let pointerTravel = 0;
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let selectionPulse = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerTravel = 0;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
pointerTravel += Math.hypot(dx, dy);
yaw -= dx * 0.006;
pitch = THREE.MathUtils.clamp(
pitch + dy * 0.005,
0.08,
1.05
);
lastX = event.clientX;
lastY = event.clientY;
updateCamera();
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragging) return;
dragging = false;
renderer.domElement.classList.remove("dragging");
if (
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
if (pointerTravel < 6) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(
selectedMeshes,
false
);
if (hit.length > 0) {
selectionPulse = 1;
}
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
5.7,
14
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
yaw = 0.42;
pitch = 0.34;
cameraRadius = 9.7;
updateCamera();
});
resetViewButton.addEventListener("click", () => {
yaw = 0.42;
pitch = 0.34;
cameraRadius = 9.7;
updateCamera();
});
// -------------------------------------------------------------------------
// Playback and interface
// -------------------------------------------------------------------------
let phase = 0;
let playing = true;
let playbackSpeed = 1;
const cycleDuration = 28;
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
playButton.setAttribute(
"aria-label",
playing ? "Pause animation" : "Play animation"
);
});
speedButton.addEventListener("click", () => {
const speeds = [0.5, 1, 1.5, 2];
const nextIndex =
(speeds.indexOf(playbackSpeed) + 1) % speeds.length;
playbackSpeed = speeds[nextIndex];
speedButton.textContent = `${playbackSpeed}× speed`;
});
timeline.addEventListener("input", (event) => {
phase = Number(event.target.value) / 1000;
updateSeason(phase);
updateInterface();
});
stageButtons.forEach((button) => {
button.addEventListener("click", () => {
phase = Number(button.dataset.start) + 0.002;
playing = false;
playButton.textContent = "Play";
playButton.setAttribute("aria-label", "Play animation");
updateSeason(phase);
updateInterface();
});
});
function updateInterface() {
const stage =
stages.find(
(candidate) =>
phase >= candidate.start && phase < candidate.end
) || stages[stages.length - 1];
stageName.textContent = stage.name;
stageDescription.textContent = stage.description;
phaseValue.textContent =
`${Math.round(phase * 100)}% through illustrative cycle`;
timeline.value = Math.round(phase * 1000);
timeline.style.setProperty(
"--progress",
`${phase * 100}%`
);
stageButtons.forEach((button) => {
button.classList.toggle(
"active",
Math.abs(
Number(button.dataset.start) - stage.start
) < 0.001
);
});
}
// -------------------------------------------------------------------------
// Marker projection and render loop
// -------------------------------------------------------------------------
const markerAnchor = selectedCurve.getPointAt(0.46);
const projectedAnchor = new THREE.Vector3();
function updateMarker() {
projectedAnchor.copy(markerAnchor).project(camera);
const visible =
projectedAnchor.z > -1 &&
projectedAnchor.z < 1 &&
Math.abs(projectedAnchor.x) < 1.15 &&
Math.abs(projectedAnchor.y) < 1.15;
branchMarker.classList.toggle("hidden", !visible);
if (visible) {
const x =
(projectedAnchor.x * 0.5 + 0.5) * window.innerWidth;
const y =
(-projectedAnchor.y * 0.5 + 0.5) * window.innerHeight;
branchMarker.style.left = `${x}px`;
branchMarker.style.top = `${y}px`;
}
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
phase =
(phase + (delta * playbackSpeed) / cycleDuration) % 1;
}
selectionPulse = Math.max(0, selectionPulse - delta * 1.8);
selectedBranchMaterial.emissiveIntensity =
0.07 + selectionPulse * 0.35;
selectionPin.scale.setScalar(1 + selectionPulse * 0.55);
updateSeason(phase);
updateInterface();
updateMarker();
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
);
});
updateSeason(phase);
updateInterface();
animate();
</script>
</body>
</html>
```