Prompt specification
Build or customize the Three.js 3D scene "Rainwater reaches an epiphyte" 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: Rainwater reaches an epiphyte
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rainwater-reaches-an-epiphyte-x0734
Scene Description Prompt:
Rainwater reaches an epiphyte. Animate droplets collecting on an epiphyte and along branch surfaces; symbolic uptake occurs from exposed surfaces while the host branch remains a supporting structure. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (rainwater-reaches-an-epiphyte-x0734):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rainwater on an Epiphyte</title>
<style>
:root {
color-scheme: light;
--ink: #17332d;
--muted: #5e746d;
--panel: rgba(248, 252, 249, 0.82);
--line: rgba(28, 72, 60, 0.15);
--water: #45bce8;
--uptake: #00d5c7;
--branch: #b47b45;
--leaf: #3b8c58;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 15%, rgba(255, 255, 255, 0.92), transparent 38%),
linear-gradient(145deg, #e5eee8 0%, #cfdfd7 56%, #b9cec5 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.intro {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
width: min(420px, calc(100vw - 52px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: #487066;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
content: "";
background: var(--uptake);
border-radius: 4px;
}
h1 {
max-width: 390px;
margin: 0;
font-size: clamp(28px, 4.1vw, 52px);
font-weight: 750;
line-height: 0.98;
letter-spacing: -0.045em;
}
.intro p {
max-width: 385px;
margin: 14px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 15px;
margin-top: 17px;
color: #46645b;
font-size: 11px;
font-weight: 650;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.45);
}
.dot.water {
background: var(--water);
}
.dot.uptake {
background: var(--uptake);
}
.dot.branch {
background: var(--branch);
}
.controls {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 20;
width: min(286px, calc(100vw - 48px));
padding: 15px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(37, 69, 58, 0.15);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
color: #416158;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live {
display: inline-flex;
align-items: center;
gap: 5px;
color: #23876f;
letter-spacing: 0.08em;
}
.live::before {
width: 6px;
height: 6px;
content: "";
background: #27b48f;
border-radius: 50%;
box-shadow: 0 0 0 4px rgba(39, 180, 143, 0.12);
}
.range-row {
display: grid;
grid-template-columns: 50px 1fr 34px;
align-items: center;
gap: 9px;
margin: 7px 0 13px;
color: var(--muted);
font-size: 11px;
}
input[type="range"] {
width: 100%;
height: 4px;
accent-color: #178f7b;
cursor: pointer;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 36px;
padding: 8px 10px;
border: 1px solid rgba(30, 85, 69, 0.16);
border-radius: 10px;
color: #31564c;
background: rgba(255, 255, 255, 0.62);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 150ms ease,
background 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.95);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.check-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 11px;
color: #557168;
font-size: 11px;
cursor: pointer;
user-select: none;
}
.check-row input {
accent-color: #00b9aa;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.metric strong {
display: block;
color: #244c42;
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.metric span {
display: block;
margin-top: 2px;
color: #71867f;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.world-label {
position: fixed;
z-index: 8;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 10px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 999px;
background: rgba(249, 253, 250, 0.78);
box-shadow: 0 6px 24px rgba(30, 64, 54, 0.1);
color: #45645b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
.world-label::before {
width: 7px;
height: 7px;
content: "";
border-radius: 50%;
}
.world-label.plant::before {
background: var(--leaf);
}
.world-label.host::before {
background: var(--branch);
}
.hint {
position: fixed;
left: 26px;
bottom: 25px;
z-index: 9;
color: rgba(47, 79, 70, 0.72);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.07em;
pointer-events: none;
}
@media (max-width: 700px) {
.intro {
top: 18px;
left: 18px;
}
.intro p {
max-width: 330px;
font-size: 11px;
}
.legend {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
width: 245px;
padding: 12px;
}
.hint,
.world-label.host {
display: none;
}
}
</style>
</head>
<body>
<header class="intro">
<div class="eyebrow">Rainwater · Epiphyte</div>
<h1>Uptake begins at the exposed surface.</h1>
<p>
Rain beads collect on the epiphyte’s leaves and on the supporting bark.
Cyan pulses indicate water entering exposed plant tissue. The host branch
provides support only.
</p>
<div class="legend" aria-label="Animation legend">
<span><i class="dot water"></i>Surface water</span>
<span><i class="dot uptake"></i>Plant uptake</span>
<span><i class="dot branch"></i>Support only</span>
</div>
</header>
<aside class="controls" aria-label="Animation controls">
<div class="control-title">
Rain cycle
<span class="live" id="live-status">Live</span>
</div>
<label class="range-row" for="rain-intensity">
<span>Rain</span>
<input
id="rain-intensity"
type="range"
min="0"
max="100"
value="58"
aria-label="Rainfall intensity"
/>
<span id="rain-value">58%</span>
</label>
<div class="button-row">
<button id="pause-button" type="button">Pause</button>
<button id="reset-button" type="button">Restart cycle</button>
</div>
<label class="check-row">
<input id="uptake-toggle" type="checkbox" checked />
Show uptake pathways
</label>
<div class="metrics">
<div class="metric">
<strong id="surface-count">0</strong>
<span>Collected beads</span>
</div>
<div class="metric">
<strong id="uptake-count">0</strong>
<span>Uptake events</span>
</div>
</div>
</aside>
<div class="world-label plant" id="plant-label">Exposed epiphyte surfaces</div>
<div class="world-label host" id="host-label">Host branch · support only</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom · Double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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;
renderer.domElement.setAttribute("aria-label", "Animated rainwater on a low-poly epiphyte");
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xf6fff8, 0x577366, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5d7, 3.1);
sun.position.set(4.5, 8, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0005;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x8fd8ff, 1.35);
rim.position.set(-5, 3, -5);
scene.add(rim);
const shadowCatcher = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.ShadowMaterial({ opacity: 0.13 })
);
shadowCatcher.rotation.x = -Math.PI / 2;
shadowCatcher.position.y = -3.2;
shadowCatcher.receiveShadow = true;
scene.add(shadowCatcher);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, pinch, and damping
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-0.05, 0.45, 0),
yaw: 0.54,
pitch: 0.35,
radius: window.innerWidth < 700 ? 13.4 : 11.4,
targetYaw: 0.54,
targetPitch: 0.35,
targetRadius: window.innerWidth < 700 ? 13.4 : 11.4
};
const pointers = new Map();
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
let pinchDistance = 0;
function updateCamera(dt = 1) {
const damping = 1 - Math.exp(-dt * 9);
cameraState.yaw += (cameraState.targetYaw - cameraState.yaw) * damping;
cameraState.pitch += (cameraState.targetPitch - cameraState.pitch) * damping;
cameraState.radius += (cameraState.targetRadius - cameraState.radius) * damping;
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x +
cameraState.radius * cp * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.radius * Math.sin(cameraState.pitch),
cameraState.target.z +
cameraState.radius * cp * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.targetYaw = 0.54;
cameraState.targetPitch = 0.35;
cameraState.targetRadius = window.innerWidth < 700 ? 13.4 : 11.4;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0 && distance > 0) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * (pinchDistance / distance),
6.4,
18
);
}
pinchDistance = distance;
return;
}
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.targetYaw -= dx * 0.006;
cameraState.targetPitch = THREE.MathUtils.clamp(
cameraState.targetPitch + dy * 0.005,
-0.08,
1.12
);
});
function releasePointer(event) {
pointers.delete(event.pointerId);
dragging = pointers.size > 0;
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
6.4,
18
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Supporting host branch
// -------------------------------------------------------------------------
const branchStart = new THREE.Vector3(-5.8, -2.35, 0.1);
const branchEnd = new THREE.Vector3(4.8, 2.55, -0.05);
const branchVector = branchEnd.clone().sub(branchStart);
const branchLength = branchVector.length();
const branchDirection = branchVector.clone().normalize();
const branchTop = new THREE.Vector3(0, 1, 0)
.addScaledVector(branchDirection, -branchDirection.y)
.normalize();
const branchFront = new THREE.Vector3()
.crossVectors(branchDirection, branchTop)
.normalize();
function branchRadiusAt(s) {
const t = THREE.MathUtils.clamp(s / branchLength, 0, 1);
return THREE.MathUtils.lerp(0.62, 0.37, t);
}
function branchCenterAt(s) {
return branchStart.clone().addScaledVector(branchDirection, s);
}
function branchSurfaceAt(s, angle, extra = 0) {
const radial = branchTop
.clone()
.multiplyScalar(Math.cos(angle))
.addScaledVector(branchFront, Math.sin(angle))
.normalize();
return branchCenterAt(s).addScaledVector(
radial,
branchRadiusAt(s) + extra
);
}
function cylinderBetween(a, b, radiusA, radiusB, material, segments = 8) {
const direction = b.clone().sub(a);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusB, radiusA, direction.length(), segments, 2),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
const barkMaterial = new THREE.MeshStandardMaterial({
color: 0x765039,
roughness: 0.94,
metalness: 0,
flatShading: true
});
const darkBarkMaterial = new THREE.MeshStandardMaterial({
color: 0x4d382c,
roughness: 1,
flatShading: true
});
cylinderBetween(
branchStart,
branchEnd,
0.62,
0.37,
barkMaterial,
9
);
const twigA = branchCenterAt(branchLength * 0.74);
const twigEndA = twigA.clone().add(new THREE.Vector3(1.25, 0.95, -0.78));
cylinderBetween(twigA, twigEndA, 0.19, 0.075, barkMaterial, 7);
const twigB = branchCenterAt(branchLength * 0.31);
const twigEndB = twigB.clone().add(new THREE.Vector3(-0.8, 0.82, -0.55));
cylinderBetween(twigB, twigEndB, 0.17, 0.065, barkMaterial, 7);
// Faceted bark ridges.
for (let ridgeIndex = 0; ridgeIndex < 4; ridgeIndex++) {
const angle = -0.75 + ridgeIndex * 0.43;
const points = [];
for (let i = 0; i <= 14; i++) {
const s = 0.3 + (i / 14) * (branchLength - 0.6);
points.push(branchSurfaceAt(s, angle, 0.012));
}
const ridgeCurve = new THREE.CatmullRomCurve3(points);
const ridge = new THREE.Mesh(
new THREE.TubeGeometry(ridgeCurve, 28, 0.018, 4, false),
darkBarkMaterial
);
ridge.castShadow = true;
scene.add(ridge);
}
// Low-poly knots.
for (const [s, angle] of [
[branchLength * 0.2, 0.48],
[branchLength * 0.79, -0.48]
]) {
const knot = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.14, 0),
darkBarkMaterial
);
knot.position.copy(branchSurfaceAt(s, angle, 0.035));
knot.scale.set(1.25, 0.72, 0.72);
knot.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
branchTop.clone().multiplyScalar(Math.cos(angle))
.addScaledVector(branchFront, Math.sin(angle))
.normalize()
);
scene.add(knot);
}
// -------------------------------------------------------------------------
// Low-poly epiphyte
// -------------------------------------------------------------------------
const plantBaseS = branchLength * 0.52;
const plantBase = branchSurfaceAt(plantBaseS, 0, 0.13);
const plant = new THREE.Group();
plant.position.copy(plantBase);
scene.add(plant);
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x327a47,
roughness: 0.82,
flatShading: true,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x3f9254,
roughness: 0.8,
flatShading: true,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x276b43,
roughness: 0.86,
flatShading: true,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x55a15c,
roughness: 0.8,
flatShading: true,
side: THREE.DoubleSide
})
];
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0x8b7047,
roughness: 0.98,
flatShading: true
});
// Clasping roots touch the bark, but no water path is drawn from the host.
for (let rootIndex = 0; rootIndex < 5; rootIndex++) {
const points = [];
const angleOffset = -1.05 + rootIndex * 0.52;
for (let i = 0; i <= 12; i++) {
const t = i / 12;
const s = plantBaseS - 0.34 + t * 0.68;
const angle = angleOffset + Math.sin(t * Math.PI) * 0.42;
points.push(branchSurfaceAt(s, angle, 0.035));
}
const curve = new THREE.CatmullRomCurve3(points);
const root = new THREE.Mesh(
new THREE.TubeGeometry(curve, 20, 0.027, 5, false),
rootMaterial
);
root.castShadow = true;
scene.add(root);
}
const cup = new THREE.Mesh(
new THREE.SphereGeometry(0.43, 8, 5),
leafMaterials[0]
);
cup.scale.set(1, 0.82, 1);
cup.position.y = 0.16;
cup.castShadow = true;
plant.add(cup);
const leaves = [];
function leafHalfWidth(leaf, t) {
return Math.max(
0.014,
leaf.width *
Math.pow(Math.sin(Math.PI * THREE.MathUtils.clamp(t, 0, 1)), 0.72) *
(0.88 + 0.12 * t)
);
}
function leafFold(leaf, t) {
return leaf.fold * Math.sin(Math.PI * t) * (1 - 0.15 * t);
}
function createLeafGeometry(length, width, fold) {
const segments = 8;
const positions = [];
const indices = [];
for (let row = 0; row <= segments; row++) {
const t = row / segments;
const x = t * length;
const half = Math.max(
0.014,
width * Math.pow(Math.sin(Math.PI * t), 0.72) * (0.88 + 0.12 * t)
);
const ridge = fold * Math.sin(Math.PI * t) * (1 - 0.15 * t);
positions.push(x, 0, ridge); // center
positions.push(x, -half, 0); // left edge
positions.push(x, half, 0); // right edge
}
for (let row = 0; row < segments; row++) {
const c = row * 3;
const l = c + 1;
const r = c + 2;
const nc = c + 3;
const nl = c + 4;
const nr = c + 5;
indices.push(c, nl, nc);
indices.push(c, l, nl);
indices.push(c, nc, nr);
indices.push(c, nr, r);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function orientLeaf(mesh, direction) {
const xAxis = direction.clone().normalize();
let zAxis = new THREE.Vector3(0, 1, 0);
zAxis.addScaledVector(xAxis, -zAxis.dot(xAxis));
if (zAxis.lengthSq() < 0.0001) {
zAxis = new THREE.Vector3(0, 0, 1);
zAxis.addScaledVector(xAxis, -zAxis.dot(xAxis));
}
zAxis.normalize();
const yAxis = new THREE.Vector3().crossVectors(zAxis, xAxis).normalize();
const basis = new THREE.Matrix4().makeBasis(xAxis, yAxis, zAxis);
mesh.quaternion.setFromRotationMatrix(basis);
}
function addLeaf(direction, length, width, material, tierOffset) {
const fold = width * 0.25;
const geometry = createLeafGeometry(length, width, fold);
const mesh = new THREE.Mesh(geometry, material);
orientLeaf(mesh, direction);
mesh.position.y = tierOffset;
mesh.castShadow = true;
mesh.receiveShadow = true;
plant.add(mesh);
const midribPoints = [];
for (let i = 0; i <= 10; i++) {
const t = i / 10;
midribPoints.push(
new THREE.Vector3(
t * length,
0,
leafFold({ fold }, t) + 0.012
)
);
}
const midrib = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(midribPoints),
new THREE.LineBasicMaterial({
color: 0xa2c887,
transparent: true,
opacity: 0.58
})
);
mesh.add(midrib);
leaves.push({
mesh,
length,
width,
fold,
worldMatrix: new THREE.Matrix4(),
worldQuaternion: new THREE.Quaternion(),
worldNormal: new THREE.Vector3(0, 0, 1)
});
}
function createRosette() {
const tiers = [
{ count: 9, length: 2.65, width: 0.44, rise: 0.24, offset: 0.00 },
{ count: 7, length: 2.05, width: 0.38, rise: 0.48, offset: 0.43 },
{ count: 5, length: 1.42, width: 0.30, rise: 0.72, offset: 0.88 }
];
tiers.forEach((tier, tierIndex) => {
for (let i = 0; i < tier.count; i++) {
const angle =
(i / tier.count) * Math.PI * 2 +
tier.offset +
tierIndex * 0.21;
const radial = new THREE.Vector3(
Math.cos(angle),
0,
Math.sin(angle)
);
const direction = radial
.multiplyScalar(0.96)
.add(new THREE.Vector3(0, tier.rise, 0));
const length = tier.length * (0.88 + Math.random() * 0.25);
const width = tier.width * (0.92 + Math.random() * 0.16);
const material =
leafMaterials[(i + tierIndex * 2) % leafMaterials.length];
addLeaf(direction, length, width, material, tierIndex * 0.035);
}
});
}
createRosette();
scene.updateMatrixWorld(true);
for (const leaf of leaves) {
leaf.mesh.updateWorldMatrix(true, false);
leaf.worldMatrix.copy(leaf.mesh.matrixWorld);
leaf.mesh.getWorldQuaternion(leaf.worldQuaternion);
leaf.worldNormal
.set(0, 0, 1)
.applyQuaternion(leaf.worldQuaternion)
.normalize();
}
function leafPoint(leaf, t, lateral, lift = 0) {
const half = leafHalfWidth(leaf, t);
const fold = leafFold(leaf, t);
const surfaceZ = fold * (1 - Math.abs(lateral) / half);
const local = new THREE.Vector3(
t * leaf.length,
lateral,
surfaceZ + lift
);
return local.applyMatrix4(leaf.worldMatrix);
}
// -------------------------------------------------------------------------
// Rain and surface-bead pools
// -------------------------------------------------------------------------
const rainGeometry = new THREE.CylinderGeometry(0.018, 0.03, 0.19, 5);
const rainMaterial = new THREE.MeshBasicMaterial({
color: 0x64c9ed,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const beadGeometry = new THREE.IcosahedronGeometry(1, 1);
const leafWaterMaterial = new THREE.MeshStandardMaterial({
color: 0x48bde5,
emissive: 0x0a6f94,
emissiveIntensity: 0.28,
roughness: 0.16,
metalness: 0.05,
transparent: true,
opacity: 0.88,
flatShading: true
});
const branchWaterMaterial = new THREE.MeshStandardMaterial({
color: 0x318fb8,
emissive: 0x073d57,
emissiveIntensity: 0.2,
roughness: 0.22,
metalness: 0.03,
transparent: true,
opacity: 0.9,
flatShading: true
});
const rainDrops = [];
const surfaceBeads = [];
const uptakeRings = [];
const uptakeParticles = [];
let rainIntensity = 58;
let paused = false;
let showUptake = true;
let rainAccumulator = 0;
let uptakeCount = 0;
let activeRainCount = 0;
function randomLeafTarget() {
const leaf = leaves[Math.floor(Math.random() * leaves.length)];
const t = 0.16 + Math.random() * 0.72;
const half = leafHalfWidth(leaf, t);
const lateral = (Math.random() * 2 - 1) * half * 0.58;
return {
type: "leaf",
leaf,
t,
lateral,
position: leafPoint(leaf, t, lateral, 0.045)
};
}
function randomBranchTarget() {
const s = 0.4 + Math.random() * (branchLength - 0.8);
const angle = -0.8 + Math.random() * 1.6;
return {
type: "branch",
s,
angle,
position: branchSurfaceAt(s, angle, 0.045)
};
}
function spawnRain(initial = false) {
if (activeRainCount >= 145) return;
const target =
Math.random() < 0.68 ? randomLeafTarget() : randomBranchTarget();
const mesh = new THREE.Mesh(rainGeometry, rainMaterial);
mesh.position.copy(target.position);
mesh.position.y += initial
? 1.2 + Math.random() * 7
: 5.0 + Math.random() * 4.2;
mesh.scale.set(1, 0.75 + Math.random() * 0.65, 1);
scene.add(mesh);
rainDrops.push({
mesh,
target,
speed: 4.6 + Math.random() * 4.2
});
activeRainCount++;
}
function createBead(target) {
const mesh = new THREE.Mesh(
beadGeometry,
target.type === "leaf" ? leafWaterMaterial : branchWaterMaterial
);
const radius =
target.type === "leaf"
? 0.052 + Math.random() * 0.045
: 0.045 + Math.random() * 0.035;
mesh.scale.setScalar(radius);
scene.add(mesh);
const bead = {
mesh,
type: target.type,
leaf: target.leaf,
t: target.t,
lateral: target.lateral,
s: target.s,
angle: target.angle,
radius,
age: 0,
growDuration: 0.9 + Math.random() * 0.6,
absorbAt: 3.8 + Math.random() * 4.2,
absorbing: false,
removalAge: 0
};
surfaceBeads.push(bead);
updateBeadPosition(bead);
}
function updateBeadPosition(bead) {
if (bead.type === "leaf") {
const lift = bead.radius * 0.65;
bead.mesh.position.copy(
leafPoint(bead.leaf, bead.t, bead.lateral, lift)
);
} else {
bead.mesh.position.copy(
branchSurfaceAt(bead.s, bead.angle, bead.radius * 0.55)
);
}
}
// -------------------------------------------------------------------------
// Symbolic uptake: only leaf surfaces emit uptake particles
// -------------------------------------------------------------------------
const uptakeRingGeometry = new THREE.TorusGeometry(0.115, 0.014, 4, 14);
const uptakeParticleGeometry = new THREE.IcosahedronGeometry(0.038, 0);
const uptakeParticleMaterial = new THREE.MeshBasicMaterial({
color: 0x00dfcf,
transparent: true,
opacity: 0.95,
depthWrite: false
});
function tangentVector(normal) {
const tangent = new THREE.Vector3(
Math.random() - 0.5,
Math.random() - 0.5,
Math.random() - 0.5
);
tangent.addScaledVector(normal, -tangent.dot(normal));
if (tangent.lengthSq() < 0.0001) {
tangent.set(1, 0, 0);
tangent.addScaledVector(normal, -tangent.dot(normal));
}
return tangent.normalize();
}
function spawnUptake(bead) {
uptakeCount++;
const normal = bead.leaf.worldNormal;
const tangent = tangentVector(normal);
const contact = leafPoint(
bead.leaf,
bead.t,
bead.lateral,
0.018
);
const ringMaterial = new THREE.MeshBasicMaterial({
color: 0x00d9c9,
transparent: true,
opacity: 0.9,
depthWrite: false
});
const ring = new THREE.Mesh(uptakeRingGeometry, ringMaterial);
ring.position.copy(contact);
ring.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
normal
);
ring.scale.setScalar(0.35);
scene.add(ring);
uptakeRings.push({
mesh: ring,
material: ringMaterial,
age: 0,
duration: 0.9
});
for (let i = 0; i < 3; i++) {
const offset = tangent
.clone()
.multiplyScalar((i - 1) * 0.045)
.addScaledVector(normal, Math.random() * 0.02);
const start = contact.clone().add(offset);
const end = start
.clone()
.addScaledVector(normal, -(0.22 + Math.random() * 0.1));
const particle = new THREE.Mesh(
uptakeParticleGeometry,
uptakeParticleMaterial
);
particle.position.copy(start);
scene.add(particle);
uptakeParticles.push({
mesh: particle,
start,
end,
age: 0,
duration: 0.72 + Math.random() * 0.25
});
}
}
function removeFromScene(mesh) {
scene.remove(mesh);
}
function updateUptake(dt) {
for (let i = uptakeRings.length - 1; i >= 0; i--) {
const ring = uptakeRings[i];
ring.age += dt;
const p = Math.min(ring.age / ring.duration, 1);
ring.mesh.scale.setScalar(0.35 + p * 1.45);
ring.material.opacity = (1 - p) * 0.9;
if (p >= 1) {
removeFromScene(ring.mesh);
ring.material.dispose();
uptakeRings.splice(i, 1);
}
}
for (let i = uptakeParticles.length - 1; i >= 0; i--) {
const particle = uptakeParticles[i];
particle.age += dt;
const p = Math.min(particle.age / particle.duration, 1);
particle.mesh.position.lerpVectors(
particle.start,
particle.end,
p
);
particle.mesh.scale.setScalar(Math.max(0.05, 1 - p));
if (p >= 1) {
removeFromScene(particle.mesh);
uptakeParticles.splice(i, 1);
}
}
}
// -------------------------------------------------------------------------
// Simulation
// -------------------------------------------------------------------------
function updateRain(dt) {
const spawnRate = rainIntensity * 0.28;
rainAccumulator += dt * spawnRate;
while (rainAccumulator >= 1) {
spawnRain();
rainAccumulator--;
}
for (let i = rainDrops.length - 1; i >= 0; i--) {
const drop = rainDrops[i];
drop.mesh.position.y -= drop.speed * dt;
if (drop.mesh.position.y <= drop.target.position.y + 0.035) {
createBead(drop.target);
removeFromScene(drop.mesh);
rainDrops.splice(i, 1);
activeRainCount--;
}
}
}
function updateBeads(dt) {
for (let i = surfaceBeads.length - 1; i >= 0; i--) {
const bead = surfaceBeads[i];
bead.age += dt;
if (bead.type === "leaf") {
const growth = Math.min(bead.age / bead.growDuration, 1);
let scale = bead.radius * (0.3 + growth * 0.7);
if (!bead.absorbing && bead.age > bead.absorbAt) {
bead.absorbing = true;
if (showUptake) spawnUptake(bead);
}
if (bead.absorbing) {
bead.removalAge += dt;
scale *= Math.max(0.03, 1 - bead.removalAge * 0.62);
if (bead.removalAge > 1.35) {
removeFromScene(bead.mesh);
surfaceBeads.splice(i, 1);
continue;
}
} else {
// A slight downhill creep toward the leaf base.
if (bead.age > 1.3) {
bead.t = Math.max(0.12, bead.t - dt * 0.009);
bead.lateral *= Math.exp(-dt * 0.18);
}
}
bead.mesh.scale.setScalar(scale);
} else {
// Water on the host persists and slowly travels downslope.
bead.s = Math.max(0.35, bead.s - dt * 0.045);
bead.angle += Math.sin(bead.age * 1.8) * dt * 0.035;
const growth = Math.min(bead.age / bead.growDuration, 1);
const endFade = Math.max(0.08, 1 - Math.max(0, bead.age - 13) / 2);
bead.mesh.scale.setScalar(
bead.radius * (0.35 + growth * 0.65) * endFade
);
if (bead.age > 15) {
removeFromScene(bead.mesh);
surfaceBeads.splice(i, 1);
continue;
}
}
updateBeadPosition(bead);
}
}
function clearCycle() {
for (const drop of rainDrops) removeFromScene(drop.mesh);
for (const bead of surfaceBeads) removeFromScene(bead.mesh);
for (const ring of uptakeRings) {
removeFromScene(ring.mesh);
ring.material.dispose();
}
for (const particle of uptakeParticles) {
removeFromScene(particle.mesh);
}
rainDrops.length = 0;
surfaceBeads.length = 0;
uptakeRings.length = 0;
uptakeParticles.length = 0;
activeRainCount = 0;
rainAccumulator = 0;
uptakeCount = 0;
// Begin with a light shower and a few visible bark beads.
for (let i = 0; i < 32; i++) spawnRain(true);
for (let i = 0; i < 10; i++) {
const s = 0.7 + Math.random() * (branchLength - 1.4);
const angle = -0.7 + Math.random() * 1.4;
createBead({ type: "branch", s, angle });
}
}
clearCycle();
// -------------------------------------------------------------------------
// Interface
// -------------------------------------------------------------------------
const rainInput = document.getElementById("rain-intensity");
const rainValue = document.getElementById("rain-value");
const pauseButton = document.getElementById("pause-button");
const resetButton = document.getElementById("reset-button");
const uptakeToggle = document.getElementById("uptake-toggle");
const liveStatus = document.getElementById("live-status");
const surfaceCountElement = document.getElementById("surface-count");
const uptakeCountElement = document.getElementById("uptake-count");
const plantLabel = document.getElementById("plant-label");
const hostLabel = document.getElementById("host-label");
rainInput.addEventListener("input", () => {
rainIntensity = Number(rainInput.value);
rainValue.textContent = `${rainIntensity}%`;
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
liveStatus.textContent = paused ? "Paused" : "Live";
});
resetButton.addEventListener("click", () => {
clearCycle();
resetCamera();
});
uptakeToggle.addEventListener("change", () => {
showUptake = uptakeToggle.checked;
});
const plantLabelAnchor = plant.localToWorld(
new THREE.Vector3(0.1, 2.25, 0)
);
const hostLabelAnchor = branchSurfaceAt(
branchLength * 0.31,
0.3,
0.5
);
function placeWorldLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
element.style.display = visible ? "flex" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
let metricTimer = 0;
function updateMetrics(dt) {
metricTimer += dt;
if (metricTimer < 0.2) return;
metricTimer = 0;
surfaceCountElement.textContent = String(surfaceBeads.length);
uptakeCountElement.textContent = String(uptakeCount);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.04);
previousTime = now;
if (!paused) {
updateRain(dt);
updateBeads(dt);
updateUptake(dt);
updateMetrics(dt);
}
updateCamera(dt);
placeWorldLabel(plantLabel, plantLabelAnchor);
placeWorldLabel(hostLabel, hostLabelAnchor);
renderer.render(scene, camera);
}
updateCamera(1);
requestAnimationFrame(animate);
</script>
</body>
</html>
```