Prompt specification
Build or customize the Three.js 3D scene "Host-plant garden lifecycle corner" 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: Host-plant garden lifecycle corner
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/host-plant-garden-lifecycle-corner-x0663
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring butterfly metamorphosis. Build a garden patch containing host leaves, caterpillars, chrysalises, and nectar flowers; highlight that larval food resources and adult feeding resources need not be identical. Stylized solid forms.
Working Three.js Starter (host-plant-garden-lifecycle-corner-x0663):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Metamorphosis Garden</title>
<style>
:root {
--ink: #243a32;
--muted: #617169;
--cream: #fffaf0;
--leaf: #34785b;
--nectar: #e76f51;
--gold: #dca83d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eef1df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.card {
position: absolute;
padding: 16px 18px;
border: 1px solid rgba(36, 58, 50, 0.12);
border-radius: 18px;
background: rgba(255, 250, 240, 0.88);
box-shadow: 0 14px 35px rgba(47, 67, 53, 0.12);
backdrop-filter: blur(12px);
}
#title-card {
top: 22px;
left: 22px;
max-width: 370px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--leaf);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(28px, 3vw, 42px);
font-weight: 500;
line-height: 1;
letter-spacing: -0.025em;
}
#title-card p {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#lesson-card {
top: 22px;
right: 22px;
width: 320px;
}
#lesson-card h2 {
margin: 0 0 8px;
font-family: Georgia, "Times New Roman", serif;
font-size: 20px;
font-weight: 500;
}
#lesson-card p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.food-row {
display: flex;
gap: 8px;
margin-top: 13px;
}
.food-chip {
flex: 1;
padding: 9px 10px;
border-radius: 12px;
color: white;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.02em;
text-align: center;
}
.food-chip.larval {
background: var(--leaf);
}
.food-chip.adult {
background: var(--nectar);
}
.anchor-label {
position: absolute;
left: 0;
top: 0;
padding: 7px 10px;
border-radius: 10px;
color: white;
font-size: 10px;
font-weight: 800;
line-height: 1.15;
letter-spacing: 0.05em;
text-align: center;
white-space: nowrap;
box-shadow: 0 6px 16px rgba(30, 48, 39, 0.18);
transform: translate(-50%, -100%);
will-change: left, top;
}
.anchor-label small {
display: block;
margin-top: 2px;
font-size: 8px;
font-weight: 700;
opacity: 0.82;
letter-spacing: 0.08em;
}
.resource-label {
border: 2px solid rgba(255, 255, 255, 0.7);
}
.resource-label.host {
background: var(--leaf);
}
.resource-label.nectar {
background: var(--nectar);
}
.stage-label {
padding: 4px 7px;
border: 1px solid rgba(36, 58, 50, 0.12);
background: rgba(255, 250, 240, 0.9);
color: var(--ink);
font-size: 8px;
letter-spacing: 0.12em;
box-shadow: 0 4px 10px rgba(30, 48, 39, 0.08);
}
#control-note {
position: absolute;
bottom: 22px;
left: 22px;
padding: 9px 12px;
border-radius: 999px;
background: rgba(36, 58, 50, 0.78);
color: #fffdf4;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
backdrop-filter: blur(8px);
}
#reset-note {
margin-left: 7px;
color: #cce4c2;
}
@media (max-width: 800px) {
#title-card {
top: 12px;
left: 12px;
max-width: 265px;
padding: 12px 14px;
}
#title-card p {
display: none;
}
#lesson-card {
top: auto;
right: 12px;
bottom: 56px;
width: 270px;
padding: 12px 14px;
}
#lesson-card h2 {
font-size: 17px;
}
#lesson-card p {
font-size: 11px;
}
.stage-label {
display: none;
}
#control-note {
bottom: 12px;
left: 12px;
}
}
@media (max-width: 520px) {
#lesson-card {
width: calc(100% - 24px);
right: 12px;
}
#lesson-card p {
display: none;
}
.food-row {
margin-top: 9px;
}
}
</style>
</head>
<body>
<div class="ui">
<section id="title-card" class="card">
<div class="eyebrow">A garden of transformations</div>
<h1>Metamorphosis Garden</h1>
<p>
Follow the egg, caterpillar, chrysalis, and butterfly while the garden
reveals an important ecological distinction.
</p>
</section>
<section id="lesson-card" class="card">
<h2>Different stages, different tables</h2>
<p>
Caterpillars require suitable <strong>host leaves</strong> to grow.
Adults usually feed on <strong>nectar</strong>. The plants that nourish
the larva and the adult need not be the same species.
</p>
<div class="food-row">
<div class="food-chip larval">LARVA · LEAVES</div>
<div class="food-chip adult">ADULT · NECTAR</div>
</div>
</section>
<div id="host-label" class="anchor-label resource-label host">
HOST LEAVES
<small>LARVAL FOOD</small>
</div>
<div id="nectar-label" class="anchor-label resource-label nectar">
NECTAR FLOWERS
<small>ADULT FOOD</small>
</div>
<div id="egg-label" class="anchor-label stage-label">1 · EGG</div>
<div id="larva-label" class="anchor-label stage-label">2 · LARVA</div>
<div id="pupa-label" class="anchor-label stage-label">3 · PUPA</div>
<div id="adult-label" class="anchor-label stage-label">4 · ADULT</div>
<div id="control-note">
DRAG TO ORBIT · SCROLL TO ZOOM <span id="reset-note">· DOUBLE-CLICK TO RESET</span>
</div>
</div>
<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 scene = new THREE.Scene();
scene.background = new THREE.Color(0xeef1df);
scene.fog = new THREE.Fog(0xeef1df, 17, 31);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xfff8dc, 0x66816d, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2cc, 3.2);
sun.position.set(6, 10, 7);
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 = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xcde8ff, 1.0);
fill.position.set(-7, 5, -6);
scene.add(fill);
const palette = {
soil: 0x80523b,
soilTop: 0x976346,
stone: 0xd8ccb1,
stem: 0x39764e,
leaf: 0x4f955c,
leafLight: 0x72ad65,
vein: 0x91c577,
larva: 0x83c95b,
larvaStripe: 0xf0d65d,
larvaHead: 0x31513a,
pupa: 0xd5a43e,
pupaLight: 0xf0d377,
wing: 0xef8937,
wingDark: 0x283d36,
wingLight: 0xffd68b,
coral: 0xe96d5d,
pink: 0xe98aa2,
yellow: 0xf0c84d,
flowerCenter: 0x735137
};
function material(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...extra
});
}
const mats = {
soil: material(palette.soil),
soilTop: material(palette.soilTop),
stone: material(palette.stone),
stem: material(palette.stem),
leaf: material(palette.leaf),
leafLight: material(palette.leafLight),
vein: material(palette.vein),
larva: material(palette.larva),
larvaStripe: material(palette.larvaStripe),
larvaHead: material(palette.larvaHead),
pupa: material(palette.pupa),
pupaLight: material(palette.pupaLight),
wing: material(palette.wing, { side: THREE.DoubleSide }),
wingDark: material(palette.wingDark, { side: THREE.DoubleSide }),
wingLight: material(palette.wingLight, { side: THREE.DoubleSide }),
coral: material(palette.coral),
pink: material(palette.pink),
yellow: material(palette.yellow),
flowerCenter: material(palette.flowerCenter),
gold: material(palette.gold)
};
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function roundedRectangleShape(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 mesh(geometry, mat, parent = scene) {
const object = new THREE.Mesh(geometry, mat);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function branch(a, b, radius = 0.035, mat = mats.stem, parent = scene) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius * 0.78,
radius,
length,
7
);
const object = mesh(geometry, mat, parent);
object.position.copy(start).add(end).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return object;
}
function makeLeaf(base, scale = 1, rotation = [0, 0, 0], light = false) {
const group = new THREE.Group();
group.position.set(...base);
group.rotation.set(...rotation);
group.scale.setScalar(scale);
const shape = new THREE.Shape();
shape.moveTo(0, 0);
shape.bezierCurveTo(0.40, 0.10, 0.62, 0.46, 0, 0.94);
shape.bezierCurveTo(-0.62, 0.46, -0.40, 0.10, 0, 0);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.065,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.025,
bevelThickness: 0.02
});
mesh(geometry, light ? mats.leafLight : mats.leaf, group);
const vein = branch(
[0, 0.04, 0.075],
[0, 0.78, 0.075],
0.012,
mats.vein,
group
);
vein.castShadow = false;
scene.add(group);
return group;
}
function makeCaterpillar(scale = 1) {
const group = new THREE.Group();
const segmentGeometry = new THREE.SphereGeometry(0.14, 10, 7);
for (let i = 0; i < 8; i++) {
const segment = mesh(
segmentGeometry,
i % 2 === 0 ? mats.larva : mats.larvaStripe,
group
);
segment.position.x = (i - 3.5) * 0.12;
segment.scale.set(1.08, 0.92, 0.92);
}
const head = mesh(
new THREE.SphereGeometry(0.16, 10, 7),
mats.larvaHead,
group
);
head.position.set(0.52, 0.015, 0);
head.scale.set(1.05, 0.95, 0.95);
const eyeGeometry = new THREE.SphereGeometry(0.025, 6, 4);
for (const z of [-0.09, 0.09]) {
const eye = mesh(eyeGeometry, material(0x111a16), group);
eye.position.set(0.62, 0.07, z);
eye.castShadow = false;
}
const legGeometry = new THREE.CylinderGeometry(0.018, 0.014, 0.16, 5);
for (let i = 0; i < 6; i++) {
const leg = mesh(legGeometry, mats.larvaHead, group);
leg.position.set((i - 2.5) * 0.17, -0.14, i % 2 ? 0.11 : -0.11);
leg.rotation.z = Math.PI / 2.4;
}
group.scale.setScalar(scale);
scene.add(group);
return group;
}
function makeChrysalis(scale = 1) {
const pivot = new THREE.Group();
const profile = [
new THREE.Vector2(0.015, 0),
new THREE.Vector2(0.12, 0.06),
new THREE.Vector2(0.17, 0.22),
new THREE.Vector2(0.15, 0.46),
new THREE.Vector2(0.09, 0.63),
new THREE.Vector2(0.015, 0.68)
];
const body = mesh(
new THREE.LatheGeometry(profile, 10),
mats.pupa,
pivot
);
body.position.y = -0.68;
const highlight = mesh(
new THREE.SphereGeometry(0.105, 8, 5),
mats.pupaLight,
pivot
);
highlight.scale.set(0.55, 1.5, 0.55);
highlight.position.set(0.06, -0.36, 0.045);
for (let i = 0; i < 3; i++) {
const ridge = mesh(
new THREE.TorusGeometry(0.125 - i * 0.025, 0.012, 5, 10),
mats.pupaLight,
pivot
);
ridge.rotation.x = Math.PI / 2;
ridge.position.y = -0.25 - i * 0.12;
ridge.scale.setScalar(1 - i * 0.12);
}
pivot.scale.setScalar(scale);
scene.add(pivot);
return pivot;
}
function wingShape() {
const shape = new THREE.Shape();
shape.moveTo(0, 0);
shape.bezierCurveTo(0.23, 0.17, 0.69, 0.33, 0.94, 0.10);
shape.bezierCurveTo(0.86, -0.08, 0.58, -0.13, 0.50, -0.43);
shape.bezierCurveTo(0.27, -0.55, 0.07, -0.34, 0, 0);
return shape;
}
function makeButterfly(scale = 1) {
const group = new THREE.Group();
const bodyMaterial = mats.wingDark;
const body = mesh(
new THREE.CylinderGeometry(0.055, 0.075, 0.46, 8),
bodyMaterial,
group
);
for (const y of [-0.24, 0.24]) {
const cap = mesh(
new THREE.SphereGeometry(0.067, 8, 5),
bodyMaterial,
group
);
cap.position.y = y;
}
const head = mesh(
new THREE.SphereGeometry(0.085, 8, 6),
bodyMaterial,
group
);
head.position.y = 0.28;
for (const side of [-1, 1]) {
branch(
[0, 0.31, 0],
[side * 0.13, 0.52, 0],
0.012,
bodyMaterial,
group
);
}
const shape = wingShape();
const outerGeometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.035,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.018,
bevelThickness: 0.012
});
const innerGeometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.025,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.01,
bevelThickness: 0.01
});
const wings = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.x = side * 0.015;
const outer = mesh(outerGeometry, mats.wingDark, pivot);
outer.scale.set(side, 1, 1);
const inner = mesh(innerGeometry, mats.wing, pivot);
inner.position.z = 0.035;
inner.scale.set(side * 0.91, 0.91, 1);
const spotGeometry = new THREE.SphereGeometry(0.055, 8, 5);
const spots = [
[0.58, 0.10],
[0.72, 0.02],
[0.42, -0.18]
];
for (const [x, y] of spots) {
const spot = mesh(spotGeometry, mats.wingLight, pivot);
spot.position.set(side * x, y, 0.075);
spot.scale.set(1, 0.8, 0.32);
}
group.add(pivot);
wings.push(pivot);
}
group.scale.setScalar(scale);
scene.add(group);
return { group, wings };
}
function makeFlower(base, height, colorMat, scale = 1) {
const group = new THREE.Group();
branch(
[base[0], 0.34, base[2]],
[base[0], base[1] + height, base[2]],
0.035,
mats.stem
);
const bloom = new THREE.Group();
bloom.position.set(base[0], base[1] + height, base[2]);
bloom.scale.setScalar(scale);
bloom.rotation.y = (base[0] * 0.17 + base[2] * 0.11) % 0.7;
const petalGeometry = new THREE.SphereGeometry(0.19, 8, 5);
for (let i = 0; i < 7; i++) {
const angle = (i / 7) * Math.PI * 2;
const petal = mesh(petalGeometry, colorMat, bloom);
petal.position.set(Math.cos(angle) * 0.23, Math.sin(angle) * 0.23, 0);
petal.scale.set(0.65, 1.05, 0.34);
petal.rotation.z = angle - Math.PI / 2;
}
const center = mesh(
new THREE.SphereGeometry(0.16, 10, 6),
mats.flowerCenter,
bloom
);
center.scale.z = 0.52;
const leaf = makeLeaf(
[base[0] + 0.05, 0.55 + height * 0.35, base[2]],
0.42,
[-0.45, -0.5, -0.75],
true
);
leaf.rotation.y += 0.5;
group.add(bloom);
scene.add(group);
return bloom;
}
// -------------------------------------------------------------------------
// Garden bed and surroundings
// -------------------------------------------------------------------------
const ground = mesh(
new THREE.PlaneGeometry(50, 50),
material(0xdfe8c8)
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.08;
ground.castShadow = false;
const bedShape = roundedRectangleShape(10.4, 6.2, 0.75);
const bedGeometry = new THREE.ExtrudeGeometry(bedShape, {
depth: 0.28,
bevelEnabled: true,
bevelSegments: 1,
steps: 1,
bevelSize: 0.08,
bevelThickness: 0.055
});
bedGeometry.rotateX(-Math.PI / 2);
const bed = mesh(bedGeometry, mats.soil);
bed.position.y = 0.02;
const bedTopGeometry = new THREE.ShapeGeometry(bedShape, 10);
bedTopGeometry.rotateX(-Math.PI / 2);
const bedTop = mesh(bedTopGeometry, mats.soilTop);
bedTop.position.y = 0.34;
bedTop.castShadow = false;
// Low-poly stones around the soil border.
const stoneGeometry = new THREE.DodecahedronGeometry(0.17, 0);
for (let i = 0; i < 30; i++) {
const side = i % 3;
let x;
let z;
if (side === 0) {
x = -4.8 + (i / 29) * 9.6;
z = i % 2 ? 3.02 : -3.02;
} else if (side === 1) {
x = i % 2 ? 5.12 : -5.12;
z = -2.7 + ((i * 0.61) % 5.4);
} else {
x = -4.5 + ((i * 0.83) % 9);
z = -3.03;
}
const stone = mesh(stoneGeometry, mats.stone);
stone.position.set(x, 0.34, z);
stone.scale.set(
0.7 + (i % 3) * 0.16,
0.45 + (i % 2) * 0.12,
0.75 + (i % 4) * 0.08
);
stone.rotation.set(i * 0.7, i * 0.33, i * 0.19);
}
// -------------------------------------------------------------------------
// Host-leaf plant: larval food on the left
// -------------------------------------------------------------------------
const hostBase = [-2.8, 0.35, -0.55];
branch(hostBase, [-2.8, 1.55, -0.65], 0.065);
const hostLeaves = [
{ base: [-3.25, 0.85, -0.15], scale: 0.95, rot: [-0.3, 0.55, -0.72], light: false },
{ base: [-2.4, 0.92, -0.18], scale: 1.0, rot: [-0.2, -0.75, 0.7], light: true },
{ base: [-3.48, 1.45, -0.93], scale: 0.88, rot: [-0.45, 0.1, -0.4], light: true },
{ base: [-2.42, 1.48, -1.08], scale: 0.92, rot: [-0.35, -0.3, 0.46], light: false },
{ base: [-1.88, 1.2, -0.55], scale: 0.78, rot: [-0.25, 0.8, 0.65], light: true }
];
for (const leaf of hostLeaves) {
branch(hostBase, leaf.base, 0.035);
makeLeaf(leaf.base, leaf.scale, leaf.rot, leaf.light);
}
// A broad, nearly horizontal host leaf carrying a caterpillar.
makeLeaf(
[-3.02, 0.92, 0.42],
1.04,
[-Math.PI / 2, 0.04, 0.08],
false
);
const hostCaterpillar = makeCaterpillar(1.05);
hostCaterpillar.position.set(-3.02, 1.015, 0.10);
hostCaterpillar.rotation.y = Math.PI / 2;
// Egg cluster on another host leaf.
const eggLeaf = makeLeaf(
[-2.35, 1.18, 0.12],
0.62,
[-Math.PI / 2 + 0.12, 0, -0.28],
true
);
const eggMaterial = material(0xf3e6bd);
const eggGeometry = new THREE.SphereGeometry(0.065, 9, 6);
for (let i = 0; i < 5; i++) {
const egg = mesh(eggGeometry, eggMaterial);
egg.position.set(-2.46 + (i % 3) * 0.09, 1.255, 0.04 + Math.floor(i / 3) * 0.08);
egg.scale.y = 1.25;
}
// A visible chrysalis hanging from a host-side twig.
branch(
[-2.42, 1.48, -1.08],
[-1.12, 2.14, -1.45],
0.043
);
branch(
[-1.12, 2.14, -1.45],
[-0.82, 2.02, -1.62],
0.027
);
const mainChrysalis = makeChrysalis(1.0);
mainChrysalis.position.set(-1.12, 2.12, -1.45);
branch(
[0, -0.02, 0],
[0, -0.48, 0],
0.012,
material(0xe8dfc8),
mainChrysalis
);
// -------------------------------------------------------------------------
// Nectar flowers: adult food on the right
// -------------------------------------------------------------------------
const flowerDefinitions = [
{ base: [2.05, 0.35, -0.95], height: 1.15, mat: mats.coral, scale: 1.0 },
{ base: [2.72, 0.35, -1.2], height: 1.7, mat: mats.pink, scale: 0.9 },
{ base: [3.35, 0.35, -0.55], height: 1.28, mat: mats.yellow, scale: 1.08 },
{ base: [2.55, 0.35, 0.02], height: 1.95, mat: mats.coral, scale: 0.84 },
{ base: [3.45, 0.35, 0.34], height: 1.55, mat: mats.pink, scale: 0.95 },
{ base: [1.85, 0.35, 0.28], height: 1.42, mat: mats.yellow, scale: 0.72 }
];
const blooms = flowerDefinitions.map((flower) =>
makeFlower(flower.base, flower.height, flower.mat, flower.scale)
);
const adultButterfly = makeButterfly(0.78);
adultButterfly.group.position.set(2.55, 2.48, 0.02);
adultButterfly.group.rotation.y = -0.22;
// -------------------------------------------------------------------------
// Foreground metamorphosis sequence
// -------------------------------------------------------------------------
const stagePositions = [
[-3.65, 1.35],
[-1.48, 1.35],
[0.68, 1.35],
[3.02, 1.35]
];
const stageDiskGeometry = new THREE.CylinderGeometry(0.64, 0.72, 0.12, 24);
for (const [x, z] of stagePositions) {
const disk = mesh(stageDiskGeometry, mats.stone);
disk.position.set(x, 0.40, z);
disk.scale.z = 0.84;
}
// Stage 1: eggs on a miniature leaf.
makeLeaf(
[-3.83, 0.51, 1.53],
0.38,
[-Math.PI / 2, 0, -0.2],
true
);
for (let i = 0; i < 5; i++) {
const egg = mesh(eggGeometry, eggMaterial);
egg.position.set(
-3.82 + (i % 3) * 0.065,
0.57,
1.47 + Math.floor(i / 3) * 0.065
);
egg.scale.set(0.65, 0.8, 0.65);
}
// Stage 2: caterpillar.
const timelineCaterpillar = makeCaterpillar(0.68);
timelineCaterpillar.position.set(-1.52, 0.64, 1.32);
// Stage 3: chrysalis hanging from a tiny twig.
branch(
[0.52, 0.47, 1.36],
[0.52, 1.57, 1.36],
0.026
);
branch(
[0.52, 1.57, 1.36],
[0.87, 1.5, 1.36],
0.018
);
const timelineChrysalis = makeChrysalis(0.62);
timelineChrysalis.position.set(0.52, 1.55, 1.36);
branch(
[0, -0.02, 0],
[0, -0.32, 0],
0.008,
material(0xe8dfc8),
timelineChrysalis
);
// Stage 4: adult at a small nectar flower.
branch(
[3.02, 0.46, 1.36],
[3.02, 1.05, 1.36],
0.024
);
const miniBloom = new THREE.Group();
miniBloom.position.set(3.02, 1.08, 1.36);
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const petal = mesh(
new THREE.SphereGeometry(0.13, 7, 4),
mats.coral,
miniBloom
);
petal.position.set(Math.cos(angle) * 0.17, Math.sin(angle) * 0.17, 0);
petal.scale.set(0.55, 0.85, 0.28);
petal.rotation.z = angle - Math.PI / 2;
}
const miniCenter = mesh(
new THREE.SphereGeometry(0.11, 8, 5),
mats.flowerCenter,
miniBloom
);
miniCenter.scale.z = 0.5;
const timelineButterfly = makeButterfly(0.43);
timelineButterfly.group.position.set(3.02, 1.29, 1.39);
// Gold directional arrows connect the four stages.
const arrowShaftGeometry = new THREE.BoxGeometry(0.42, 0.045, 0.09);
const arrowHeadGeometry = new THREE.ConeGeometry(0.105, 0.22, 5);
for (const x of [-2.62, 0.0, 2.08]) {
const shaft = mesh(arrowShaftGeometry, mats.gold);
shaft.position.set(x, 0.405, 1.35);
const head = mesh(arrowHeadGeometry, mats.gold);
head.position.set(x + 0.29, 0.405, 1.35);
head.rotation.z = -Math.PI / 2;
}
// A few decorative tufts outside the garden.
const grassGeometry = new THREE.ConeGeometry(0.055, 0.42, 4);
for (let i = 0; i < 34; i++) {
const angle = (i / 34) * Math.PI * 2 + 0.2;
const radius = 6.1 + (i % 4) * 0.42;
const grass = mesh(grassGeometry, i % 2 ? mats.leaf : mats.leafLight);
grass.position.set(
Math.cos(angle) * radius,
0.12,
Math.sin(angle) * radius * 0.66
);
grass.rotation.z = ((i % 5) - 2) * 0.09;
grass.rotation.y = i * 0.71;
}
// -------------------------------------------------------------------------
// Custom camera controls — no external controls addon
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 1.05, 0),
radius: 12.2,
theta: 0.72,
phi: 1.02
};
const initialCamera = { ...cameraState };
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
const pointers = new Map();
let previousPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const points = [...pointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const current = { x: event.clientX, y: event.clientY };
pointers.set(event.pointerId, current);
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.43,
1.42
);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / Math.max(distance, 1)),
7.2,
17.5
);
previousPinchDistance = distance;
}
updateCamera();
});
function endPointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.2,
17.5
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.target.copy(initialCamera.target);
cameraState.radius = initialCamera.radius;
cameraState.theta = initialCamera.theta;
cameraState.phi = initialCamera.phi;
updateCamera();
});
updateCamera();
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelData = [
{
element: document.getElementById("host-label"),
position: new THREE.Vector3(-2.72, 3.0, -0.72),
offsetY: -8
},
{
element: document.getElementById("nectar-label"),
position: new THREE.Vector3(2.68, 3.15, -0.75),
offsetY: -8
},
{
element: document.getElementById("egg-label"),
position: new THREE.Vector3(-3.65, 0.88, 1.35),
offsetY: -4
},
{
element: document.getElementById("larva-label"),
position: new THREE.Vector3(-1.48, 0.92, 1.35),
offsetY: -4
},
{
element: document.getElementById("pupa-label"),
position: new THREE.Vector3(0.68, 1.74, 1.35),
offsetY: -4
},
{
element: document.getElementById("adult-label"),
position: new THREE.Vector3(3.02, 1.62, 1.35),
offsetY: -4
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labelData) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
label.element.style.left = `${x}px`;
label.element.style.top = `${y + label.offsetY}px`;
}
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const bloomPositions = blooms.map((bloom) => bloom.position.clone());
const adultStart = bloomPositions[3].clone().add(new THREE.Vector3(0, 0.2, 0));
function smoothstep(value) {
return value * value * (3 - 2 * value);
}
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
// Caterpillars make small, believable progress movements.
hostCaterpillar.position.x = -3.02 + Math.sin(time * 0.58) * 0.055;
hostCaterpillar.rotation.z = Math.sin(time * 1.7) * 0.025;
timelineCaterpillar.position.x = -1.48 + Math.sin(time * 0.85) * 0.085;
timelineCaterpillar.rotation.z = Math.sin(time * 2.1) * 0.04;
// Chrysalis cases sway gently beneath their silk.
mainChrysalis.rotation.z = Math.sin(time * 0.72) * 0.035;
mainChrysalis.rotation.x = Math.sin(time * 0.51) * 0.018;
timelineChrysalis.rotation.z = Math.sin(time * 0.8 + 1.3) * 0.04;
// The adult butterfly moves between nectar flowers.
const segmentLength = 6.2;
const segment = Math.floor(time / segmentLength) % bloomPositions.length;
const next = (segment + 1) % bloomPositions.length;
const localTime = (time % segmentLength) / segmentLength;
const travel = smoothstep(Math.min(localTime * 1.2, 1));
const from =
segment === 0
? adultStart
: bloomPositions[segment].clone().add(new THREE.Vector3(0, 0.2, 0));
const to =
bloomPositions[next].clone().add(new THREE.Vector3(0, 0.2, 0));
adultButterfly.group.position.lerpVectors(from, to, travel);
adultButterfly.group.position.y += Math.sin(time * 2.8) * 0.045;
adultButterfly.group.rotation.y =
-0.18 + Math.sin(time * 0.75) * 0.13;
const mainFlap = Math.sin(time * 11.5) * 0.52;
adultButterfly.wings[0].rotation.y = mainFlap;
adultButterfly.wings[1].rotation.y = -mainFlap;
const miniFlap = Math.sin(time * 7.5 + 0.6) * 0.34;
timelineButterfly.wings[0].rotation.y = miniFlap;
timelineButterfly.wings[1].rotation.y = -miniFlap;
// Flowers nod almost imperceptibly.
blooms.forEach((bloom, index) => {
bloom.rotation.z = Math.sin(time * 0.7 + index) * 0.025;
});
updateLabels();
renderer.render(scene, camera);
}
animate();
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```