Prompt specification
Build or customize the Three.js 3D scene "Fallen leaf decomposition layers" 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: Fallen leaf decomposition layers
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/fallen-leaf-decomposition-layers-x0725
Scene Description Prompt:
Show an intact leaf, vein-bearing fragments, and decomposed organic particles across three stylized ground tiles. Make one HTML scene with Three.js from cdnjs.
Working Three.js Starter (fallen-leaf-decomposition-layers-x0725):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Forest Floor — Three Stages of Decomposition</title>
<style>
:root {
--ink: #273127;
--muted: #6e7468;
--paper: #ece8dc;
--panel: rgba(255, 253, 246, 0.86);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 28px;
left: 32px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #65705f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: #87927e;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 54px);
font-weight: 500;
letter-spacing: -0.04em;
line-height: 0.95;
}
.subtitle {
max-width: 430px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.reset {
position: fixed;
top: 30px;
right: 32px;
z-index: 10;
padding: 10px 14px;
border: 1px solid rgba(65, 76, 61, 0.2);
border-radius: 999px;
background: rgba(255, 253, 246, 0.74);
color: #566051;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
background 160ms ease,
transform 160ms ease;
}
.reset:hover {
background: rgba(255, 253, 246, 0.98);
transform: translateY(-1px);
}
#annotations {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
}
.annotation {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 10px;
min-width: 190px;
padding: 9px 13px 9px 10px;
border: 1px solid rgba(68, 76, 61, 0.14);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 10px 30px rgba(53, 58, 47, 0.1);
backdrop-filter: blur(10px);
transform: translate(-50%, -100%);
transition: opacity 150ms ease;
}
.annotation::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 20px;
content: "";
background: linear-gradient(rgba(65, 76, 61, 0.32), transparent);
}
.annotation .number {
display: grid;
flex: 0 0 32px;
width: 32px;
height: 32px;
place-items: center;
border-radius: 50%;
background: var(--accent);
color: white;
font-size: 10px;
font-weight: 800;
}
.annotation strong {
display: block;
margin-bottom: 2px;
font-size: 12px;
font-weight: 800;
}
.annotation small {
display: block;
color: #73796e;
font-size: 9px;
line-height: 1.35;
}
.footer {
position: fixed;
right: 30px;
bottom: 24px;
left: 30px;
z-index: 5;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
color: #72796e;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.footer .rule {
width: min(32vw, 260px);
height: 1px;
margin-right: 12px;
background: rgba(83, 92, 78, 0.25);
}
.instructions {
display: flex;
align-items: center;
}
@media (max-width: 720px) {
.header {
top: 20px;
left: 20px;
}
.subtitle {
max-width: 270px;
font-size: 11px;
}
.reset {
top: 20px;
right: 18px;
padding: 9px 11px;
font-size: 9px;
}
.annotation {
min-width: 0;
padding: 7px 9px 7px 7px;
}
.annotation .number {
flex-basis: 26px;
width: 26px;
height: 26px;
}
.annotation strong {
font-size: 10px;
}
.annotation small {
display: none;
}
.footer {
right: 18px;
bottom: 16px;
left: 18px;
}
.footer .rule {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive forest-floor decomposition diorama"></main>
<header class="header">
<div class="eyebrow">Forest floor study · 03 stages</div>
<h1>From leaf<br />to loam.</h1>
<p class="subtitle">
A tactile study of organic decay: an intact blade, torn vascular
fragments, and the fine matter that returns to the soil.
</p>
</header>
<button class="reset" id="reset" type="button">Reset view</button>
<div id="annotations">
<div
class="annotation"
data-annotation="0"
style="--accent: #69934c"
>
<span class="number">01</span>
<div>
<strong>Intact leaf</strong>
<small>Continuous blade and branching venation</small>
</div>
</div>
<div
class="annotation"
data-annotation="1"
style="--accent: #a2773f"
>
<span class="number">02</span>
<div>
<strong>Vein-bearing fragments</strong>
<small>Torn tissue with exposed vascular paths</small>
</div>
</div>
<div
class="annotation"
data-annotation="2"
style="--accent: #665748"
>
<span class="number">03</span>
<div>
<strong>Organic particles</strong>
<small>Broken litter, humus and fungal threads</small>
</div>
</div>
</div>
<footer class="footer">
<div class="instructions">
<span class="rule"></span>
<span>Drag to orbit · Scroll to zoom · Double-click to reset</span>
</div>
<span>Procedural Three.js diorama</span>
</footer>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute(
"aria-label",
"Three stylized soil tiles showing an intact leaf, torn leaf fragments, and decomposed organic particles."
);
renderer.domElement.tabIndex = 0;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xece8dc);
scene.fog = new THREE.Fog(0xece8dc, 20, 43);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
90
);
const controls = {
target: new THREE.Vector3(0, 0.12, 0),
theta: 0.09,
phi: 0.94,
radius: 13.4,
desiredTheta: 0.09,
desiredPhi: 0.94,
desiredRadius: 13.4,
minRadius: 8,
maxRadius: 42
};
// -------------------------------------------------------------------------
// Lighting and ground plane
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfff6df, 0x596150, 1.9);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff1cf, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc6dded, 0.85);
fillLight.position.set(8, 5, -7);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffdfb1, 0.55);
rimLight.position.set(-2, 4, -9);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(200, 200),
new THREE.MeshStandardMaterial({
color: 0xe7e3d7,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.405;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
let seed = 42871;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function rand(min, max) {
return min + (max - min) * random();
}
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
function makeTube(points, radius, material, closed = false) {
const curve = new THREE.CatmullRomCurve3(
points,
closed,
"catmullrom",
0.35
);
const geometry = new THREE.TubeGeometry(
curve,
Math.max(12, points.length * 5),
radius,
6,
closed
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function makePatch(centerX, centerZ, radiusX, radiusZ, color) {
const shape = new THREE.Shape();
const count = 15;
for (let i = 0; i < count; i++) {
const angle = (i / count) * Math.PI * 2;
const wobble = rand(0.82, 1.12);
const x = Math.cos(angle) * radiusX * wobble;
const y = Math.sin(angle) * radiusZ * wobble;
if (i === 0) shape.moveTo(x, y);
else shape.lineTo(x, y);
}
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape);
geometry.rotateX(-Math.PI / 2);
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({
color,
roughness: 1,
transparent: true,
opacity: 0.2,
depthWrite: false
})
);
mesh.position.set(centerX, -0.003, centerZ);
mesh.receiveShadow = true;
mesh.renderOrder = 1;
scene.add(mesh);
}
// -------------------------------------------------------------------------
// Three beveled, rounded ground tiles
// -------------------------------------------------------------------------
const tileWidth = 3.25;
const tileHeight = 3.25;
const tileCenters = [-3.6, 0, 3.6];
function createTile(centerX, topColor, sideColor) {
const shape = roundedRectShape(tileWidth, tileHeight, 0.38);
const topMaterial = new THREE.MeshStandardMaterial({
color: topColor,
roughness: 0.98
});
const sideMaterial = new THREE.MeshStandardMaterial({
color: sideColor,
roughness: 1
});
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.22,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.07,
bevelThickness: 0.06,
curveSegments: 14
});
geometry.rotateX(-Math.PI / 2);
const tile = new THREE.Mesh(geometry, [topMaterial, sideMaterial]);
tile.position.set(centerX, -0.3, 0);
tile.castShadow = true;
tile.receiveShadow = true;
scene.add(tile);
const surfaceGeometry = new THREE.ShapeGeometry(shape, 14);
surfaceGeometry.rotateX(-Math.PI / 2);
const surface = new THREE.Mesh(surfaceGeometry, topMaterial);
surface.position.set(centerX, -0.005, 0);
surface.receiveShadow = true;
scene.add(surface);
return tile;
}
createTile(tileCenters[0], 0x756e49, 0x654936);
createTile(tileCenters[1], 0x82684b, 0x654832);
createTile(tileCenters[2], 0x504638, 0x3e332a);
makePatch(-3.9, -0.7, 0.95, 0.55, 0x596b3e);
makePatch(-2.9, 0.8, 0.7, 0.42, 0x9a8957);
makePatch(-4.2, 0.6, 0.48, 0.36, 0x435638);
makePatch(-0.6, 0.3, 0.9, 0.62, 0x5e4933);
makePatch(0.65, -0.75, 0.72, 0.46, 0xa07c50);
makePatch(0.5, 0.75, 0.6, 0.38, 0x594332);
makePatch(3.35, -0.35, 0.95, 0.72, 0x342d27);
makePatch(4.05, 0.6, 0.72, 0.55, 0x6b5941);
makePatch(2.9, 0.9, 0.43, 0.36, 0x3a352c);
// -------------------------------------------------------------------------
// Small stones and organic surface details
// -------------------------------------------------------------------------
const pebbleGeometry = new THREE.DodecahedronGeometry(1, 0);
const pebbleMaterials = [
new THREE.MeshStandardMaterial({ color: 0x716b5d, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x94856b, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x514c40, roughness: 1 })
];
function addPebbles(centerX, count, maxRadius = 1.4) {
for (let i = 0; i < count; i++) {
const pebble = new THREE.Mesh(
pebbleGeometry,
pebbleMaterials[Math.floor(random() * pebbleMaterials.length)]
);
const size = rand(0.035, 0.105);
pebble.scale.set(
size * rand(0.8, 1.4),
size * rand(0.55, 0.9),
size * rand(0.8, 1.3)
);
pebble.position.set(
centerX + rand(-maxRadius, maxRadius),
0.025,
rand(-maxRadius, maxRadius)
);
pebble.rotation.set(rand(0, Math.PI), rand(0, Math.PI), rand(0, Math.PI));
pebble.castShadow = true;
pebble.receiveShadow = true;
scene.add(pebble);
}
}
addPebbles(tileCenters[0], 19);
addPebbles(tileCenters[1], 15);
addPebbles(tileCenters[2], 20, 1.45);
// -------------------------------------------------------------------------
// Stylized grass tufts
// -------------------------------------------------------------------------
function bladeGeometry(height, width, bend) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
-width / 2, 0, 0,
width / 2, 0, 0,
width * 0.35, height * 0.56, bend * 0.35,
0, height, bend,
-width * 0.35, height * 0.56, bend * 0.35
],
3
)
);
geometry.setIndex([
0, 1, 2,
0, 2, 3,
0, 3, 4
]);
geometry.computeVertexNormals();
return geometry;
}
function createGrassTuft(x, z, scale, color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
side: THREE.DoubleSide
});
for (let i = 0; i < 4; i++) {
const blade = new THREE.Mesh(
bladeGeometry(rand(0.23, 0.42), rand(0.045, 0.075), rand(0.07, 0.16)),
material
);
blade.rotation.y = rand(0, Math.PI * 2);
blade.rotation.z = rand(-0.18, 0.18);
blade.castShadow = true;
group.add(blade);
}
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
}
[
[-4.98, -1.16, 1.05],
[-4.88, 1.15, 0.9],
[-3.35, -1.43, 0.8],
[-2.62, 1.43, 1.05],
[-4.18, -1.46, 0.72],
[-1.42, 1.38, 0.72],
[1.42, -1.36, 0.64]
].forEach(([x, z, scale]) => {
createGrassTuft(x, z, scale, x < -2 ? 0x789151 : 0x8c8552);
});
// -------------------------------------------------------------------------
// Stage 1: intact leaf with a curved blade, midrib, and branching veins
// -------------------------------------------------------------------------
function createIntactLeaf() {
const group = new THREE.Group();
const length = 2.85;
const maximumWidth = 1.18;
const segments = 38;
const crossSegments = 10;
const positions = [];
const indices = [];
function leafWidth(t) {
return (
maximumWidth *
Math.pow(Math.sin(Math.PI * t), 0.72) *
(0.82 + 0.18 * t)
);
}
function leafCenterHeight(t) {
return (
0.076 * Math.sin(Math.PI * t) +
0.018 * Math.sin(Math.PI * 2 * t)
);
}
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const x = -length / 2 + length * t;
const width = leafWidth(t);
const centerHeight = leafCenterHeight(t);
for (let j = 0; j <= crossSegments; j++) {
const v = -1 + (j / crossSegments) * 2;
const sideBias = v < 0 ? 1.025 : 1;
const z = v * width * sideBias;
const y =
centerHeight * (1 - 0.22 * Math.abs(v)) +
0.014 * Math.sin(Math.PI * t) * v * v;
positions.push(x, y, z);
}
}
for (let i = 0; i < segments; i++) {
for (let j = 0; j < crossSegments; j++) {
const a = i * (crossSegments + 1) + j;
const b = (i + 1) * (crossSegments + 1) + j;
const c = a + 1;
const d = b + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x4f8a43,
roughness: 0.78,
side: THREE.DoubleSide
});
const leaf = new THREE.Mesh(geometry, leafMaterial);
leaf.castShadow = true;
leaf.receiveShadow = true;
group.add(leaf);
const veinMaterial = new THREE.MeshStandardMaterial({
color: 0xb7cc72,
roughness: 0.85
});
const midribMaterial = new THREE.MeshStandardMaterial({
color: 0xd0d985,
roughness: 0.82
});
const midribPoints = [];
for (let i = 0; i <= 30; i++) {
const t = 0.025 + (i / 30) * 0.95;
midribPoints.push(
new THREE.Vector3(
-length / 2 + length * t,
leafCenterHeight(t) + 0.022,
0
)
);
}
group.add(makeTube(midribPoints, 0.022, midribMaterial));
for (let i = 0; i < 8; i++) {
const t = 0.13 + i * 0.092;
const side = i % 2 === 0 ? 1 : -1;
const nextT = Math.min(0.94, t + 0.115);
const start = new THREE.Vector3(
-length / 2 + length * t,
leafCenterHeight(t) + 0.024,
0
);
const middleT = (t + nextT) * 0.5;
const middle = new THREE.Vector3(
-length / 2 + length * middleT,
leafCenterHeight(middleT) * 0.82 + 0.023,
side * leafWidth(middleT) * 0.46
);
const end = new THREE.Vector3(
-length / 2 + length * nextT,
leafCenterHeight(nextT) * 0.78 + 0.021,
side * leafWidth(nextT) * 0.88
);
group.add(makeTube([start, middle, end], 0.0085, veinMaterial));
}
const edgePoints = [];
for (let i = 0; i <= 28; i++) {
const t = i / 28;
edgePoints.push(
new THREE.Vector3(
-length / 2 + length * t,
leafCenterHeight(t) * 0.78,
leafWidth(t) * 1.025
)
);
}
for (let i = 28; i >= 0; i--) {
const t = i / 28;
edgePoints.push(
new THREE.Vector3(
-length / 2 + length * t,
leafCenterHeight(t) * 0.78,
-leafWidth(t)
)
);
}
const edgeGeometry = new THREE.BufferGeometry().setFromPoints(edgePoints);
const edge = new THREE.LineLoop(
edgeGeometry,
new THREE.LineBasicMaterial({ color: 0x315e32 })
);
group.add(edge);
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x607b38,
roughness: 0.9
});
const stem = makeTube(
[
new THREE.Vector3(-1.38, 0.025, 0),
new THREE.Vector3(-1.62, 0.025, 0.03),
new THREE.Vector3(-1.83, 0.018, 0.17)
],
0.034,
stemMaterial
);
group.add(stem);
group.position.set(-3.55, 0.018, 0.12);
group.rotation.y = -0.19;
scene.add(group);
}
createIntactLeaf();
// -------------------------------------------------------------------------
// Stage 2: torn leaf fragments with retained branching veins
// -------------------------------------------------------------------------
const fragmentMaterials = [
new THREE.MeshStandardMaterial({
color: 0xa67943,
roughness: 0.94,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xb18750,
roughness: 0.96,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x87673e,
roughness: 0.98,
side: THREE.DoubleSide
})
];
const fragmentVeinMaterial = new THREE.MeshStandardMaterial({
color: 0xd0aa6b,
roughness: 0.9
});
const fragmentEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x60442d
});
function createFragment({
points,
veins,
position,
rotation,
scale,
material
}) {
const group = new THREE.Group();
const shape = new THREE.Shape();
points.forEach(([x, z], index) => {
if (index === 0) shape.moveTo(x, -z);
else shape.lineTo(x, -z);
});
shape.closePath();
const geometry = new THREE.ShapeGeometry(shape);
geometry.rotateX(-Math.PI / 2);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edgePoints = points.map(
([x, z]) => new THREE.Vector3(x, 0.012, z)
);
edgePoints.push(edgePoints[0].clone());
const edgeGeometry = new THREE.BufferGeometry().setFromPoints(edgePoints);
group.add(new THREE.Line(edgeGeometry, fragmentEdgeMaterial));
veins.forEach((vein, index) => {
const curvePoints = vein.map(
([x, z]) => new THREE.Vector3(x, 0.026 + index * 0.001, z)
);
group.add(
makeTube(
curvePoints,
index === 0 ? 0.014 : 0.009,
fragmentVeinMaterial
)
);
});
group.position.set(position[0], 0.014, position[1]);
group.rotation.y = rotation;
group.scale.setScalar(scale);
scene.add(group);
}
createFragment({
points: [
[-0.65, -0.22],
[-0.42, -0.48],
[0.02, -0.38],
[0.4, -0.08],
[0.22, 0.28],
[-0.1, 0.43],
[-0.48, 0.22]
],
veins: [
[[-0.55, -0.04], [-0.2, -0.02], [0.25, 0.04]],
[[-0.32, -0.02], [-0.24, -0.28], [-0.16, -0.4]],
[[-0.08, 0], [-0.03, 0.27], [0.03, 0.36]],
[[0.1, 0.02], [0.25, 0.18], [0.3, 0.24]]
],
position: [-0.45, 0.15],
rotation: -0.25,
scale: 1.13,
material: fragmentMaterials[0]
});
createFragment({
points: [
[0.38, -0.35],
[0.66, -0.18],
[0.78, 0.14],
[0.55, 0.38],
[0.27, 0.26],
[0.2, -0.05]
],
veins: [
[[0.28, -0.06], [0.5, 0.02], [0.68, 0.1]],
[[0.48, 0.02], [0.57, -0.15], [0.61, -0.24]],
[[0.58, 0.08], [0.5, 0.25], [0.43, 0.3]]
],
position: [0.67, -0.57],
rotation: 0.72,
scale: 0.83,
material: fragmentMaterials[1]
});
createFragment({
points: [
[-0.52, -0.05],
[-0.25, -0.22],
[0.1, -0.11],
[0.35, 0.08],
[0.08, 0.21],
[-0.31, 0.17]
],
veins: [
[[-0.46, 0.01], [-0.17, 0.03], [0.26, 0.06]],
[[-0.22, 0.02], [-0.13, -0.13], [-0.03, -0.16]],
[[0, 0.04], [0.06, 0.16], [0.11, 0.19]]
],
position: [0.62, 0.83],
rotation: -0.82,
scale: 0.7,
material: fragmentMaterials[2]
});
createFragment({
points: [
[-0.47, -0.12],
[-0.12, -0.18],
[0.08, -0.04],
[0.04, 0.17],
[-0.31, 0.21]
],
veins: [
[[-0.42, 0.01], [-0.16, 0.02], [0.02, 0.05]],
[[-0.24, 0.01], [-0.12, -0.1], [-0.04, -0.12]]
],
position: [-0.95, -0.78],
rotation: 0.38,
scale: 0.6,
material: fragmentMaterials[1]
});
// -------------------------------------------------------------------------
// Stage 3: decomposed crumbs, flakes, twigs, and fungal threads
// -------------------------------------------------------------------------
const crumbGeometry = new THREE.IcosahedronGeometry(1, 0);
const crumbMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true,
vertexColors: true
});
const crumbCount = 115;
const crumbs = new THREE.InstancedMesh(
crumbGeometry,
crumbMaterial,
crumbCount
);
crumbs.castShadow = true;
crumbs.receiveShadow = true;
const dummy = new THREE.Object3D();
const crumbPalette = [
0x302820,
0x4a3929,
0x675039,
0x73704a,
0x3d442d,
0x877051
];
for (let i = 0; i < crumbCount; i++) {
const size = rand(0.025, 0.085);
dummy.position.set(
tileCenters[2] + rand(-1.45, 1.45),
0.018 + size * 0.45,
rand(-1.45, 1.45)
);
dummy.rotation.set(rand(0, Math.PI), rand(0, Math.PI), rand(0, Math.PI));
dummy.scale.set(
size * rand(0.8, 1.45),
size * rand(0.45, 0.8),
size * rand(0.8, 1.35)
);
dummy.updateMatrix();
crumbs.setMatrixAt(i, dummy.matrix);
crumbs.setColorAt(
i,
new THREE.Color(
crumbPalette[Math.floor(random() * crumbPalette.length)]
)
);
}
crumbs.instanceMatrix.needsUpdate = true;
crumbs.instanceColor.needsUpdate = true;
scene.add(crumbs);
const flakeGeometry = new THREE.CircleGeometry(1, 3);
flakeGeometry.rotateX(-Math.PI / 2);
const flakeMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
side: THREE.DoubleSide,
vertexColors: true
});
const flakeCount = 48;
const flakes = new THREE.InstancedMesh(
flakeGeometry,
flakeMaterial,
flakeCount
);
flakes.castShadow = true;
flakes.receiveShadow = true;
const flakePalette = [0x657044, 0x8b7547, 0x55462e, 0x9a8754, 0x424a2d];
for (let i = 0; i < flakeCount; i++) {
const size = rand(0.055, 0.15);
dummy.position.set(
tileCenters[2] + rand(-1.42, 1.42),
0.012 + random() * 0.012,
rand(-1.42, 1.42)
);
dummy.rotation.set(0, rand(0, Math.PI * 2), rand(-0.08, 0.08));
dummy.scale.set(size, 1, size * rand(0.45, 0.9));
dummy.updateMatrix();
flakes.setMatrixAt(i, dummy.matrix);
flakes.setColorAt(
i,
new THREE.Color(
flakePalette[Math.floor(random() * flakePalette.length)]
)
);
}
flakes.instanceMatrix.needsUpdate = true;
flakes.instanceColor.needsUpdate = true;
scene.add(flakes);
const twigMaterial = new THREE.MeshStandardMaterial({
color: 0x463528,
roughness: 1
});
for (let i = 0; i < 8; i++) {
const twig = new THREE.Mesh(
new THREE.CylinderGeometry(0.014, 0.025, rand(0.25, 0.58), 5),
twigMaterial
);
twig.position.set(
tileCenters[2] + rand(-1.3, 1.3),
0.035,
rand(-1.3, 1.3)
);
twig.rotation.set(0, rand(0, Math.PI * 2), Math.PI / 2);
twig.castShadow = true;
scene.add(twig);
}
const hyphaMaterial = new THREE.MeshStandardMaterial({
color: 0xc6b990,
roughness: 0.95
});
const hyphae = [
[
[2.65, -0.92],
[2.95, -0.74],
[3.18, -0.82],
[3.43, -0.61]
],
[
[3.42, -0.62],
[3.65, -0.42],
[3.92, -0.5]
],
[
[3.38, 0.16],
[3.7, 0.34],
[4.04, 0.22],
[4.3, 0.38]
],
[
[3.72, 0.34],
[3.82, 0.64],
[4.08, 0.78]
],
[
[2.7, 0.72],
[2.98, 0.58],
[3.24, 0.7]
]
];
hyphae.forEach((points) => {
const worldPoints = points.map(
([x, z]) => new THREE.Vector3(x, 0.035, z)
);
scene.add(makeTube(worldPoints, 0.007, hyphaMaterial));
});
// -------------------------------------------------------------------------
// World-space annotation cards
// -------------------------------------------------------------------------
const annotationElements = [
document.querySelector('[data-annotation="0"]'),
document.querySelector('[data-annotation="1"]'),
document.querySelector('[data-annotation="2"]')
];
const annotationPoints = [
new THREE.Vector3(-3.6, 0.83, -1.18),
new THREE.Vector3(0, 0.83, -1.18),
new THREE.Vector3(3.6, 0.83, -1.18)
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotationElements.forEach((element, index) => {
projected.copy(annotationPoints[index]).project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -100%)`;
element.style.opacity = projected.z > 1 ? "0" : "1";
});
}
// -------------------------------------------------------------------------
// Custom camera controls implemented without addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
controls.desiredTheta -= deltaX * 0.006;
controls.desiredPhi = THREE.MathUtils.clamp(
controls.desiredPhi + deltaY * 0.005,
0.55,
1.27
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.desiredRadius = THREE.MathUtils.clamp(
controls.desiredRadius * Math.exp(event.deltaY * 0.001),
controls.minRadius,
controls.maxRadius
);
},
{ passive: false }
);
function resetView() {
controls.desiredTheta = 0.09;
controls.desiredPhi = 0.94;
controls.desiredRadius = getFitRadius();
}
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("reset").addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetView();
});
// -------------------------------------------------------------------------
// Responsive sizing and camera fitting
// -------------------------------------------------------------------------
function getFitRadius() {
const verticalFov = THREE.MathUtils.degToRad(camera.fov);
const horizontalFov = 2 * Math.atan(Math.tan(verticalFov / 2) * camera.aspect);
const sceneWidth = 11.8;
return THREE.MathUtils.clamp(
Math.max(13.4, sceneWidth / (2 * Math.tan(horizontalFov / 2))),
controls.minRadius,
controls.maxRadius
);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
const fitRadius = getFitRadius();
controls.desiredRadius = fitRadius;
controls.radius = fitRadius;
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
controls.theta +=
(controls.desiredTheta - controls.theta) * 0.075;
controls.phi +=
(controls.desiredPhi - controls.phi) * 0.075;
controls.radius +=
(controls.desiredRadius - controls.radius) * 0.075;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y +
controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
updateAnnotations();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```