Prompt specification
Build or customize the Three.js 3D scene "Caterpillar and butterfly body plans" 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: Caterpillar and butterfly body plans
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/caterpillar-and-butterfly-body-plans-x0661
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Place a caterpillar, chrysalis, and butterfly beside a host-plant leaf; distinguish stage-specific body structures and label their shared developmental sequence without treating them as separate species.
Working Three.js Starter (caterpillar-and-butterfly-body-plans-x0661):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>One Butterfly, Four Life-Cycle Stages</title>
<style>
:root {
--ink: #17392f;
--muted: #52685f;
--paper: #f8fbf2;
--leaf: #4f8c3c;
--larva: #85b83f;
--pupa: #258e83;
--adult: #ed7a35;
--egg: #d8bd63;
--line: rgba(31, 80, 60, 0.22);
--shadow: 0 16px 45px rgba(31, 67, 49, 0.15);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #e7eddc;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene canvas.grabbing {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
background:
radial-gradient(circle at 52% 45%, transparent 38%, rgba(24, 63, 45, 0.06) 100%),
linear-gradient(180deg, rgba(255, 255, 255, 0.15), transparent 28%);
}
header {
position: fixed;
z-index: 5;
top: 24px;
left: 28px;
width: min(490px, calc(100vw - 56px));
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 8px;
margin-bottom: 10px;
padding: 6px 10px;
border: 1px solid rgba(38, 94, 68, 0.16);
border-radius: 999px;
background: rgba(249, 252, 243, 0.76);
color: #376b51;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--adult);
content: "";
box-shadow: 0 0 0 4px rgba(237, 122, 53, 0.14);
}
h1 {
margin: 0;
max-width: 470px;
color: #163d31;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 4vw, 54px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
text-wrap: balance;
}
header p {
max-width: 440px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
header p strong {
color: var(--ink);
}
#leaders {
position: fixed;
z-index: 3;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders line {
stroke: rgba(28, 81, 58, 0.48);
stroke-width: 1.2;
stroke-dasharray: 3 4;
vector-effect: non-scaling-stroke;
}
.annotation,
.mini-label {
position: fixed;
z-index: 4;
pointer-events: none;
transition: opacity 180ms ease;
}
.annotation {
width: 248px;
padding: 13px 14px 14px;
border: 1px solid rgba(36, 82, 62, 0.15);
border-radius: 15px;
background: rgba(250, 252, 246, 0.9);
box-shadow: var(--shadow);
backdrop-filter: blur(13px);
}
.annotation-head {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
}
.stage-number {
display: grid;
width: 27px;
height: 27px;
flex: 0 0 auto;
place-items: center;
border-radius: 50%;
color: white;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.04em;
}
#larva-card .stage-number {
background: var(--larva);
}
#pupa-card .stage-number {
background: var(--pupa);
}
#adult-card .stage-number {
background: var(--adult);
}
.annotation h2 {
margin: 0;
color: var(--ink);
font-size: 13px;
line-height: 1.15;
}
.annotation p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.48;
}
.annotation p strong {
color: #355647;
font-weight: 750;
}
.mini-label {
padding: 6px 9px;
border: 1px solid rgba(36, 82, 62, 0.15);
border-radius: 999px;
background: rgba(250, 252, 246, 0.88);
box-shadow: 0 7px 22px rgba(31, 67, 49, 0.1);
color: #426652;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(10px);
}
#plant-label {
border-color: rgba(70, 125, 59, 0.28);
color: #42713a;
}
#egg-label {
color: #876b25;
}
#controls {
position: fixed;
z-index: 6;
right: 22px;
bottom: 135px;
display: flex;
gap: 7px;
}
.control-button {
min-height: 36px;
padding: 8px 12px;
border: 1px solid rgba(35, 81, 61, 0.18);
border-radius: 999px;
background: rgba(249, 252, 245, 0.9);
box-shadow: 0 7px 22px rgba(31, 67, 49, 0.1);
color: var(--ink);
font-size: 10px;
font-weight: 750;
cursor: pointer;
backdrop-filter: blur(10px);
transition:
transform 150ms ease,
background 150ms ease;
}
.control-button:hover {
transform: translateY(-1px);
background: white;
}
.control-button[aria-pressed="true"] {
border-color: #4d8163;
background: #2f6f55;
color: white;
}
#timeline {
position: fixed;
z-index: 6;
left: 50%;
bottom: 20px;
width: min(860px, calc(100vw - 32px));
padding: 15px 20px 13px;
transform: translateX(-50%);
border: 1px solid rgba(36, 82, 62, 0.15);
border-radius: 19px;
background: rgba(250, 252, 246, 0.93);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.timeline-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 18px;
margin-bottom: 12px;
}
.timeline-heading strong {
font-size: 11px;
letter-spacing: 0.015em;
}
.timeline-heading span {
color: var(--muted);
font-size: 10px;
}
.sequence {
display: grid;
grid-template-columns: 1fr 24px 1fr 24px 1fr 24px 1fr;
align-items: start;
}
.stage {
display: flex;
align-items: center;
gap: 9px;
min-width: 0;
}
.stage-dot {
display: grid;
width: 29px;
height: 29px;
flex: 0 0 auto;
place-items: center;
border-radius: 50%;
color: white;
font-size: 10px;
font-weight: 850;
}
.stage:nth-child(1) .stage-dot {
background: var(--egg);
}
.stage:nth-child(3) .stage-dot {
background: var(--larva);
}
.stage:nth-child(5) .stage-dot {
background: var(--pupa);
}
.stage:nth-child(7) .stage-dot {
background: var(--adult);
}
.stage strong {
display: block;
font-size: 11px;
line-height: 1.15;
}
.stage small {
display: block;
margin-top: 2px;
overflow: hidden;
color: var(--muted);
font-size: 9px;
line-height: 1.2;
text-overflow: ellipsis;
white-space: nowrap;
}
.arrow {
align-self: center;
margin-top: 1px;
color: #789083;
font-size: 18px;
text-align: center;
}
.hide-labels .annotation,
.hide-labels .mini-label,
.hide-labels #leaders {
opacity: 0;
}
@media (max-width: 760px) {
header {
top: 15px;
left: 16px;
width: calc(100vw - 32px);
}
.eyebrow {
margin-bottom: 7px;
font-size: 8px;
}
h1 {
max-width: 320px;
font-size: 31px;
}
header p {
display: none;
}
.annotation {
width: 154px;
padding: 8px 9px;
border-radius: 12px;
}
.annotation-head {
gap: 6px;
margin-bottom: 0;
}
.stage-number {
width: 22px;
height: 22px;
font-size: 8px;
}
.annotation h2 {
font-size: 10px;
}
.annotation p {
display: none;
}
.mini-label {
font-size: 7px;
}
#controls {
right: 12px;
bottom: 125px;
}
.control-button {
min-height: 32px;
padding: 7px 9px;
font-size: 9px;
}
#timeline {
bottom: 10px;
width: calc(100vw - 20px);
padding: 11px 10px 10px;
border-radius: 15px;
}
.timeline-heading {
display: block;
margin-bottom: 9px;
text-align: center;
}
.timeline-heading strong,
.timeline-heading span {
display: block;
}
.timeline-heading strong {
font-size: 9px;
}
.timeline-heading span {
margin-top: 2px;
font-size: 8px;
}
.sequence {
grid-template-columns: 1fr 12px 1fr 12px 1fr 12px 1fr;
}
.stage {
display: block;
text-align: center;
}
.stage-dot {
width: 23px;
height: 23px;
margin: 0 auto 4px;
font-size: 8px;
}
.stage strong {
font-size: 9px;
}
.stage small {
display: none;
}
.arrow {
font-size: 12px;
}
}
@media (prefers-reduced-motion: reduce) {
* {
scroll-behavior: auto !important;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="vignette"></div>
<header>
<div class="eyebrow">Complete metamorphosis</div>
<h1>One butterfly.<br />Four life stages.</h1>
<p>
The caterpillar, chrysalis, and butterfly are <strong>stages of one species</strong>,
not separate animals or species. Their bodies are rebuilt for different roles.
</p>
</header>
<svg id="leaders" aria-hidden="true">
<line id="leader-larva"></line>
<line id="leader-pupa"></line>
<line id="leader-adult"></line>
<line id="leader-egg"></line>
</svg>
<article class="annotation" id="larva-card">
<div class="annotation-head">
<span class="stage-number">02</span>
<h2>Larva · caterpillar</h2>
</div>
<p>
<strong>Long, segmented body.</strong> Chewing mouthparts, three pairs of true
legs, and abdominal prolegs support feeding and growth.
</p>
</article>
<article class="annotation" id="pupa-card">
<div class="annotation-head">
<span class="stage-number">03</span>
<h2>Pupa · chrysalis</h2>
</div>
<p>
<strong>Compact, non-feeding casing.</strong> Folded wing and antenna
outlines are visible; the cremaster anchors it to a silk pad.
</p>
</article>
<article class="annotation" id="adult-card">
<div class="annotation-head">
<span class="stage-number">04</span>
<h2>Adult · butterfly</h2>
</div>
<p>
<strong>Scaled wings, six legs, antennae,</strong> and a coiled proboscis
adapted for nectar feeding and reproduction.
</p>
</article>
<div class="mini-label" id="plant-label">Host plant · food + attachment</div>
<div class="mini-label" id="egg-label">Egg · on the leaf</div>
<div id="controls" aria-label="Scene controls">
<button class="control-button" id="reset-button" type="button">Reset view</button>
<button class="control-button" id="label-button" type="button" aria-pressed="true">
Labels
</button>
<button class="control-button" id="orbit-button" type="button" aria-pressed="false">
Auto orbit
</button>
</div>
<section id="timeline" aria-label="Shared butterfly developmental sequence">
<div class="timeline-heading">
<strong>Shared developmental sequence — the same species throughout</strong>
<span>Identity stays the same; the body plan changes.</span>
</div>
<div class="sequence">
<div class="stage">
<span class="stage-dot">1</span>
<div>
<strong>Egg</strong>
<small>laid on host plant</small>
</div>
</div>
<div class="arrow" aria-hidden="true">→</div>
<div class="stage">
<span class="stage-dot">2</span>
<div>
<strong>Larva</strong>
<small>eats and grows</small>
</div>
</div>
<div class="arrow" aria-hidden="true">→</div>
<div class="stage">
<span class="stage-dot">3</span>
<div>
<strong>Pupa</strong>
<small>body is remodeled</small>
</div>
</div>
<div class="arrow" aria-hidden="true">→</div>
<div class="stage">
<span class="stage-dot">4</span>
<div>
<strong>Adult</strong>
<small>nectar, mates, lays eggs</small>
</div>
</div>
</div>
</section>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional scene showing the egg, caterpillar, chrysalis, and adult butterfly stages on and beside a host plant leaf."
);
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7eddc);
scene.fog = new THREE.Fog(0xe7eddc, 12, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraState = {
theta: 0.34,
phi: 1.16,
radius: 9.6,
target: new THREE.Vector3(0.25, 1.52, 0)
};
const hemisphere = new THREE.HemisphereLight(0xfff8e7, 0x55775b, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(5.5, 8.5, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -6;
sun.shadow.camera.right = 6;
sun.shadow.camera.top = 6;
sun.shadow.camera.bottom = -3;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb7ddc2, 1.2);
fill.position.set(-5, 3, 4);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(35, 35),
new THREE.MeshStandardMaterial({
color: 0xdde3cd,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.1;
ground.receiveShadow = true;
scene.add(ground);
const stageBase = new THREE.Mesh(
new THREE.CylinderGeometry(4.55, 4.8, 0.16, 96),
new THREE.MeshStandardMaterial({
color: 0xd3ddc2,
roughness: 0.95,
metalness: 0
})
);
stageBase.position.y = -0.02;
stageBase.receiveShadow = true;
stageBase.castShadow = true;
scene.add(stageBase);
function addTube(points, radius, material, parent, segments = 32) {
const curve = new THREE.CatmullRomCurve3(points);
const mesh = new THREE.Mesh(
new THREE.TubeGeometry(curve, segments, radius, 8, false),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderBetween(start, end, radius, material, parent, topRadius = radius) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius, radius, length, 10),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function leafPoint(t, u, length, width) {
const profile = Math.max(0, Math.sin(Math.PI * t));
const halfWidth = width * Math.pow(profile, 0.72) * (1 - 0.12 * t);
const x = u * halfWidth;
const y = t * length;
const z =
0.105 * (1 - u * u) * Math.sin(Math.PI * t) +
0.055 * t * t +
0.012 * Math.sin(t * Math.PI * 2) * (1 - Math.abs(u));
return new THREE.Vector3(x, y, z);
}
function createLeaf(length, width, material) {
const group = new THREE.Group();
const rows = 28;
const columns = 12;
const positions = [];
const uvs = [];
const indices = [];
for (let row = 0; row <= rows; row++) {
const t = row / rows;
for (let column = 0; column <= columns; column++) {
const u = -1 + (2 * column) / columns;
const p = leafPoint(t, u, length, width);
positions.push(p.x, p.y, p.z);
uvs.push(column / columns, t);
}
}
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const a = row * (columns + 1) + column;
const b = a + columns + 1;
indices.push(a, b, a + 1, b, b + 1, a + 1);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const leafMesh = new THREE.Mesh(geometry, material);
leafMesh.castShadow = true;
leafMesh.receiveShadow = true;
group.add(leafMesh);
const veinMaterial = new THREE.MeshStandardMaterial({
color: 0xb1d77d,
roughness: 0.9
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x477a39,
roughness: 0.9
});
const centerPoints = [];
for (let i = 0; i <= 20; i++) {
const p = leafPoint(i / 20, 0, length, width);
p.z += 0.012;
centerPoints.push(p);
}
addTube(centerPoints, 0.025, veinMaterial, group, 48);
for (const t of [0.16, 0.3, 0.44, 0.58, 0.72, 0.84]) {
for (const side of [-1, 1]) {
const points = [];
for (let i = 0; i <= 8; i++) {
const q = i / 8;
const p = leafPoint(
Math.min(0.97, t + q * 0.095),
side * q * 0.88,
length,
width
);
p.z += 0.014;
points.push(p);
}
addTube(points, 0.009, veinMaterial, group, 18);
}
}
for (const side of [-1, 1]) {
const edge = [];
for (let i = 0; i <= 24; i++) {
const p = leafPoint(i / 24, side, length, width);
p.z += 0.008;
edge.push(p);
}
addTube(edge, 0.011, edgeMaterial, group, 42);
}
return group;
}
const leafMaterial = new THREE.MeshStandardMaterial({
color: 0x5b963f,
roughness: 0.82,
metalness: 0,
side: THREE.DoubleSide
});
const smallLeafMaterial = new THREE.MeshStandardMaterial({
color: 0x6aa447,
roughness: 0.86,
metalness: 0,
side: THREE.DoubleSide
});
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x4e7a3e,
roughness: 0.95
});
addTube(
[
new THREE.Vector3(-1.62, 0.02, -0.06),
new THREE.Vector3(-1.38, 0.42, -0.03),
new THREE.Vector3(-0.92, 0.92, 0),
new THREE.Vector3(-0.48, 1.08, 0.04)
],
0.065,
stemMaterial,
scene,
40
);
const mainLeaf = createLeaf(2.85, 1.58, leafMaterial);
mainLeaf.position.set(-0.48, 1.08, 0.05);
const mainLeafBaseRotation = -0.72;
mainLeaf.rotation.z = mainLeafBaseRotation;
scene.add(mainLeaf);
const smallLeaf = createLeaf(1.25, 0.67, smallLeafMaterial);
smallLeaf.position.set(-1.28, 0.61, -0.02);
smallLeaf.rotation.z = -2.43;
scene.add(smallLeaf);
// A tiny ribbed egg attached to the host leaf.
const egg = new THREE.Group();
const eggBody = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 20, 14),
new THREE.MeshStandardMaterial({
color: 0xf0d98f,
roughness: 0.62
})
);
eggBody.scale.set(0.82, 1.25, 0.82);
eggBody.castShadow = true;
egg.add(eggBody);
const ribMaterial = new THREE.MeshStandardMaterial({
color: 0xc6a84f,
roughness: 0.7
});
for (const y of [-0.035, -0.012, 0.012, 0.035]) {
const rib = new THREE.Mesh(
new THREE.TorusGeometry(0.051, 0.005, 6, 18),
ribMaterial
);
rib.rotation.x = Math.PI / 2;
rib.position.y = y;
egg.add(rib);
}
const eggLocalPosition = leafPoint(0.69, 0.42, 2.85, 1.58);
eggLocalPosition.z += 0.085;
egg.position.copy(eggLocalPosition);
mainLeaf.add(egg);
// Caterpillar / larva.
const caterpillar = new THREE.Group();
caterpillar.position.set(0, 0.62, 0.25);
caterpillar.rotation.z = 0.22;
caterpillar.scale.setScalar(0.82);
mainLeaf.add(caterpillar);
const caterpillarGreen = new THREE.MeshStandardMaterial({
color: 0x70ad39,
roughness: 0.72
});
const caterpillarLight = new THREE.MeshStandardMaterial({
color: 0x9ac94b,
roughness: 0.72
});
const caterpillarDark = new THREE.MeshStandardMaterial({
color: 0x315e2f,
roughness: 0.78
});
const creamMaterial = new THREE.MeshStandardMaterial({
color: 0xf1dda0,
roughness: 0.72
});
const bodyParts = [];
for (let i = 0; i < 12; i++) {
const x = -0.76 + i * 0.142;
const radius = 0.135 + Math.sin((i / 11) * Math.PI) * 0.032;
const segment = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 14),
i % 2 ? caterpillarGreen : caterpillarLight
);
segment.position.set(x, Math.sin(i * 0.72) * 0.012, 0);
segment.scale.set(0.115, radius, radius * 0.93);
segment.castShadow = true;
segment.receiveShadow = true;
caterpillar.add(segment);
bodyParts.push(segment);
const band = new THREE.Mesh(
new THREE.TorusGeometry(radius * 0.86, 0.012, 7, 24),
i % 3 === 0 ? creamMaterial : caterpillarDark
);
band.rotation.y = Math.PI / 2;
band.position.set(x + 0.024, segment.position.y, 0);
band.castShadow = true;
caterpillar.add(band);
if (i > 1 && i < 10) {
const spot = new THREE.Mesh(
new THREE.SphereGeometry(0.018, 8, 6),
creamMaterial
);
spot.position.set(x - 0.012, 0.067, radius * 0.88);
spot.scale.z = 0.35;
caterpillar.add(spot);
}
}
const head = new THREE.Mesh(
new THREE.SphereGeometry(1, 24, 16),
caterpillarDark
);
head.position.set(0.88, 0, 0);
head.scale.set(0.16, 0.165, 0.145);
head.castShadow = true;
caterpillar.add(head);
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0x14211b,
roughness: 0.28
});
for (const y of [0.075, -0.075]) {
const eye = new THREE.Mesh(
new THREE.SphereGeometry(0.027, 10, 8),
eyeMaterial
);
eye.position.set(0.946, y, 0.112);
eye.scale.z = 0.55;
caterpillar.add(eye);
}
// Three pairs of true legs near the thorax.
for (const x of [0.48, 0.61, 0.73]) {
addCylinderBetween(
new THREE.Vector3(x, -0.09, 0.02),
new THREE.Vector3(x + 0.015, -0.29, 0.055),
0.022,
caterpillarDark,
caterpillar,
0.015
);
}
// Four pairs of thicker abdominal prolegs.
for (const x of [-0.56, -0.34, -0.12, 0.1]) {
addCylinderBetween(
new THREE.Vector3(x, -0.1, 0.01),
new THREE.Vector3(x, -0.275, 0.075),
0.039,
caterpillarGreen,
caterpillar,
0.027
);
const foot = new THREE.Mesh(
new THREE.SphereGeometry(0.043, 10, 8),
caterpillarDark
);
foot.scale.set(0.8, 0.5, 0.6);
foot.position.set(x, -0.285, 0.075);
caterpillar.add(foot);
}
for (const side of [-1, 1]) {
addTube(
[
new THREE.Vector3(0.91, 0.09, 0.04),
new THREE.Vector3(1.0, 0.17, 0.055),
new THREE.Vector3(1.08, 0.21, 0.06)
],
0.009,
caterpillarDark,
caterpillar,
12
);
}
// Chrysalis / pupa.
const chrysalis = new THREE.Group();
chrysalis.position.set(0.52, 2.5, 0.38);
scene.add(chrysalis);
const chrysalisMaterial = new THREE.MeshStandardMaterial({
color: 0x278d78,
roughness: 0.42,
metalness: 0.04
});
const chrysalisGold = new THREE.MeshStandardMaterial({
color: 0xd6ad45,
roughness: 0.38,
metalness: 0.18
});
const profile = [
new THREE.Vector2(0, -1.18),
new THREE.Vector2(0.075, -1.08),
new THREE.Vector2(0.19, -0.88),
new THREE.Vector2(0.275, -0.57),
new THREE.Vector2(0.3, -0.3),
new THREE.Vector2(0.27, -0.02),
new THREE.Vector2(0.15, 0.15),
new THREE.Vector2(0, 0.2)
];
const chrysalisBody = new THREE.Mesh(
new THREE.LatheGeometry(profile, 40),
chrysalisMaterial
);
chrysalisBody.castShadow = true;
chrysalisBody.receiveShadow = true;
chrysalis.add(chrysalisBody);
for (const [radius, y] of [
[0.18, -0.9],
[0.26, -0.65],
[0.295, -0.39],
[0.285, -0.15]
]) {
const ridge = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.009, 6, 32),
new THREE.MeshStandardMaterial({
color: 0x55a88d,
roughness: 0.6
})
);
ridge.rotation.x = Math.PI / 2;
ridge.position.y = y;
chrysalis.add(ridge);
}
for (const [x, y, z] of [
[-0.08, -0.88, 0.18],
[0.1, -0.73, 0.22],
[-0.13, -0.5, 0.25],
[0.08, -0.29, 0.27],
[-0.05, -0.1, 0.25],
[0.13, -0.88, 0.13]
]) {
const spot = new THREE.Mesh(
new THREE.SphereGeometry(0.032, 10, 8),
chrysalisGold
);
spot.position.set(x, y, z);
spot.scale.z = 0.28;
spot.castShadow = true;
chrysalis.add(spot);
}
const silkPad = new THREE.Mesh(
new THREE.SphereGeometry(0.09, 14, 10),
new THREE.MeshStandardMaterial({
color: 0x81765c,
roughness: 1
})
);
silkPad.scale.set(1.2, 0.35, 0.8);
silkPad.position.y = 0.2;
chrysalis.add(silkPad);
const cremaster = new THREE.Mesh(
new THREE.ConeGeometry(0.035, 0.15, 10),
new THREE.MeshStandardMaterial({
color: 0x5d513e,
roughness: 0.9
})
);
cremaster.position.y = 0.25;
chrysalis.add(cremaster);
// A small perch for the adult butterfly.
addTube(
[
new THREE.Vector3(2.78, 0.01, -0.12),
new THREE.Vector3(2.93, 0.65, -0.08),
new THREE.Vector3(2.82, 1.25, 0.02),
new THREE.Vector3(2.58, 1.93, 0.26)
],
0.035,
stemMaterial,
scene,
36
);
// Adult butterfly.
const butterfly = new THREE.Group();
butterfly.position.set(2.58, 1.93, 0.32);
butterfly.scale.setScalar(0.83);
scene.add(butterfly);
const wingDark = new THREE.MeshStandardMaterial({
color: 0x25251f,
roughness: 0.72,
side: THREE.DoubleSide
});
const wingOrange = new THREE.MeshStandardMaterial({
color: 0xf08a3d,
roughness: 0.7,
side: THREE.DoubleSide
});
const wingCream = new THREE.MeshStandardMaterial({
color: 0xfff0cf,
roughness: 0.7
});
const bodyDark = new THREE.MeshStandardMaterial({
color: 0x20221e,
roughness: 0.6
});
function forewingShape() {
const shape = new THREE.Shape();
shape.moveTo(0.035, 0.25);
shape.bezierCurveTo(0.38, 0.54, 0.9, 0.95, 1.25, 0.82);
shape.bezierCurveTo(1.47, 0.72, 1.43, 0.39, 1.18, 0.19);
shape.bezierCurveTo(0.78, 0.02, 0.33, 0.09, 0.035, 0.25);
return shape;
}
function hindwingShape() {
const shape = new THREE.Shape();
shape.moveTo(0.035, 0.13);
shape.bezierCurveTo(0.39, 0.28, 0.82, 0.23, 0.95, -0.04);
shape.bezierCurveTo(1.08, -0.36, 0.73, -0.7, 0.45, -0.66);
shape.bezierCurveTo(0.19, -0.61, 0.015, -0.37, 0.035, 0.13);
return shape;
}
function extrudeWing(shape) {
return new THREE.ExtrudeGeometry(shape, {
depth: 0.045,
bevelEnabled: true,
bevelSegments: 2,
steps: 1,
bevelSize: 0.014,
bevelThickness: 0.012,
curveSegments: 28
});
}
function makeForewing() {
const group = new THREE.Group();
const outer = new THREE.Mesh(extrudeWing(forewingShape()), wingDark);
outer.castShadow = true;
group.add(outer);
const inner = new THREE.Mesh(extrudeWing(forewingShape()), wingOrange);
inner.scale.set(0.86, 0.86, 1);
inner.position.z = 0.046;
inner.castShadow = true;
group.add(inner);
const veinPaths = [
[
[0.06, 0.23],
[0.43, 0.43],
[1.1, 0.68]
],
[
[0.06, 0.23],
[0.47, 0.31],
[1.23, 0.35]
],
[
[0.06, 0.23],
[0.42, 0.17],
[1.05, 0.18]
]
];
for (const path of veinPaths) {
addTube(
path.map(([x, y]) => new THREE.Vector3(x, y, 0.092)),
0.012,
wingDark,
group,
18
);
}
const spots = [
[1.16, 0.67, 0.032],
[1.24, 0.54, 0.026],
[1.25, 0.39, 0.025],
[1.05, 0.77, 0.025]
];
for (const [x, y, radius] of spots) {
const spot = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 8),
wingCream
);
spot.position.set(x, y, 0.103);
spot.scale.z = 0.28;
group.add(spot);
}
return group;
}
function makeHindwing() {
const group = new THREE.Group();
const outer = new THREE.Mesh(extrudeWing(hindwingShape()), wingDark);
outer.castShadow = true;
group.add(outer);
const inner = new THREE.Mesh(extrudeWing(hindwingShape()), wingOrange);
inner.scale.set(0.86, 0.86, 1);
inner.position.z = 0.046;
inner.castShadow = true;
group.add(inner);
const veinPaths = [
[
[0.05, 0.11],
[0.42, 0.04],
[0.82, -0.07]
],
[
[0.05, 0.11],
[0.36, -0.16],
[0.68, -0.45]
],
[
[0.05, 0.11],
[0.24, -0.18],
[0.35, -0.57]
]
];
for (const path of veinPaths) {
addTube(
path.map(([x, y]) => new THREE.Vector3(x, y, 0.092)),
0.012,
wingDark,
group,
18
);
}
for (const [x, y, radius] of [
[0.83, -0.11, 0.028],
[0.83, -0.28, 0.025],
[0.69, -0.53, 0.024],
[0.54, -0.61, 0.021]
]) {
const spot = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 8),
wingCream
);
spot.position.set(x, y, 0.103);
spot.scale.z = 0.28;
group.add(spot);
}
return group;
}
const rightForewing = makeForewing();
const rightHindwing = makeHindwing();
const leftForewing = rightForewing.clone(true);
const leftHindwing = rightHindwing.clone(true);
leftForewing.scale.x = -1;
leftHindwing.scale.x = -1;
butterfly.add(rightHindwing, rightForewing, leftHindwing, leftForewing);
const thorax = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 20, 14),
bodyDark
);
thorax.scale.set(0.72, 1.35, 0.72);
thorax.position.set(0, 0.15, 0.1);
thorax.castShadow = true;
butterfly.add(thorax);
const abdomen = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 18, 12),
bodyDark
);
abdomen.position.set(0, -0.28, 0.09);
abdomen.scale.set(0.7, 1.8, 0.65);
abdomen.castShadow = true;
butterfly.add(abdomen);
const butterflyHead = new THREE.Mesh(
new THREE.SphereGeometry(0.115, 16, 12),
bodyDark
);
butterflyHead.position.set(0, 0.48, 0.11);
butterflyHead.castShadow = true;
butterfly.add(butterflyHead);
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(
new THREE.SphereGeometry(0.034, 10, 8),
new THREE.MeshStandardMaterial({
color: 0x0b100d,
roughness: 0.22
})
);
eye.position.set(side * 0.07, 0.51, 0.18);
butterfly.add(eye);
addTube(
[
new THREE.Vector3(side * 0.04, 0.56, 0.11),
new THREE.Vector3(side * 0.14, 0.74, 0.12),
new THREE.Vector3(side * 0.2, 0.91, 0.13)
],
0.012,
bodyDark,
butterfly,
16
);
const club = new THREE.Mesh(
new THREE.SphereGeometry(0.035, 10, 8),
bodyDark
);
club.position.set(side * 0.2, 0.91, 0.13);
butterfly.add(club);
}
// Six legs, arranged as three pairs.
for (const side of [-1, 1]) {
for (let pair = 0; pair < 3; pair++) {
const y = 0.23 - pair * 0.13;
addTube(
[
new THREE.Vector3(side * 0.07, y, 0.07),
new THREE.Vector3(side * 0.22, y - 0.03, 0.03),
new THREE.Vector3(side * 0.25, y - 0.19, 0.07)
],
0.009,
bodyDark,
butterfly,
10
);
}
}
const proboscis = new THREE.Mesh(
new THREE.TorusGeometry(0.105, 0.013, 8, 30, Math.PI * 1.7),
bodyDark
);
proboscis.position.set(0.09, 0.39, 0.19);
proboscis.rotation.z = -0.25;
butterfly.add(proboscis);
// Annotation anchors.
const larvaAnchor = new THREE.Object3D();
larvaAnchor.position.set(0.75, 0.13, 0.14);
caterpillar.add(larvaAnchor);
const pupaAnchor = new THREE.Object3D();
pupaAnchor.position.set(0, -0.48, 0.27);
chrysalis.add(pupaAnchor);
const adultAnchor = new THREE.Object3D();
adultAnchor.position.set(0, 0.15, 0.2);
butterfly.add(adultAnchor);
const eggAnchor = new THREE.Object3D();
eggAnchor.position.set(0, 0, 0.02);
egg.add(eggAnchor);
const plantAnchor = new THREE.Object3D();
plantAnchor.position.copy(leafPoint(0.4, -0.4, 2.85, 1.58));
mainLeaf.add(plantAnchor);
const annotations = [
{
id: "larva",
card: document.querySelector("#larva-card"),
line: document.querySelector("#leader-larva"),
anchor: larvaAnchor,
desktop: [-292, -14],
mobile: [-92, -126]
},
{
id: "pupa",
card: document.querySelector("#pupa-card"),
line: document.querySelector("#leader-pupa"),
anchor: pupaAnchor,
desktop: [-72, 108],
mobile: [-78, 65]
},
{
id: "adult",
card: document.querySelector("#adult-card"),
line: document.querySelector("#leader-adult"),
anchor: adultAnchor,
desktop: [100, -64],
mobile: [40, -132]
},
{
id: "egg",
card: document.querySelector("#egg-label"),
line: document.querySelector("#leader-egg"),
anchor: eggAnchor,
desktop: [16, -43],
mobile: [8, -38]
},
{
id: "plant",
card: document.querySelector("#plant-label"),
line: null,
anchor: plantAnchor,
desktop: [-20, -32],
mobile: [-34, 20]
}
];
const projected = new THREE.Vector3();
const worldPoint = new THREE.Vector3();
function updateAnnotations() {
const mobile = window.innerWidth <= 760;
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of annotations) {
item.anchor.getWorldPosition(worldPoint);
projected.copy(worldPoint).project(camera);
const visible = projected.z > -1 && projected.z < 1;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const offset = mobile ? item.mobile : item.desktop;
const cardWidth = item.card.offsetWidth;
const cardHeight = item.card.offsetHeight;
let left = x + offset[0];
let top = y + offset[1];
left = THREE.MathUtils.clamp(left, 10, width - cardWidth - 10);
top = THREE.MathUtils.clamp(top, 82, height - cardHeight - 128);
item.card.style.left = `${left}px`;
item.card.style.top = `${top}px`;
item.card.style.opacity = visible ? "1" : "0";
if (item.line) {
item.line.setAttribute("x1", x);
item.line.setAttribute("y1", y);
item.line.setAttribute("x2", left + cardWidth * 0.5);
item.line.setAttribute("y2", top + cardHeight * 0.5);
item.line.style.opacity = visible ? "1" : "0";
}
}
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resize() {
const mobile = window.innerWidth <= 760;
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = mobile ? 55 : 42;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
cameraState.radius = mobile
? Math.max(cameraState.radius, 12.4)
: Math.max(cameraState.radius, 9.6);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
// Camera controls implemented directly, without external addons.
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.classList.add("grabbing");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.55,
1.48
);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("grabbing");
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.006,
6.6,
16
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") cameraState.theta += 0.09;
else if (key === "ArrowRight") cameraState.theta -= 0.09;
else if (key === "ArrowUp") {
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - 0.06, 0.55, 1.48);
} else if (key === "ArrowDown") {
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + 0.06, 0.55, 1.48);
} else if (key === "+" || key === "=") {
cameraState.radius = Math.max(6.6, cameraState.radius - 0.5);
} else if (key === "-") {
cameraState.radius = Math.min(16, cameraState.radius + 0.5);
} else return;
event.preventDefault();
updateCamera();
});
const resetButton = document.querySelector("#reset-button");
const labelButton = document.querySelector("#label-button");
const orbitButton = document.querySelector("#orbit-button");
let autoOrbit = false;
resetButton.addEventListener("click", () => {
cameraState.theta = 0.34;
cameraState.phi = 1.16;
cameraState.radius = window.innerWidth <= 760 ? 12.4 : 9.6;
updateCamera();
});
labelButton.addEventListener("click", () => {
const labelsVisible = !document.body.classList.contains("hide-labels");
document.body.classList.toggle("hide-labels", labelsVisible);
labelButton.setAttribute("aria-pressed", String(!labelsVisible));
});
orbitButton.addEventListener("click", () => {
autoOrbit = !autoOrbit;
orbitButton.setAttribute("aria-pressed", String(autoOrbit));
});
renderer.domElement.addEventListener("dblclick", () => {
cameraState.theta = 0.34;
cameraState.phi = 1.16;
cameraState.radius = window.innerWidth <= 760 ? 12.4 : 9.6;
updateCamera();
});
// Subtle atmospheric pollen.
const pollenCount = 42;
const pollenPositions = new Float32Array(pollenCount * 3);
for (let i = 0; i < pollenCount; i++) {
pollenPositions[i * 3] = (Math.random() - 0.5) * 10;
pollenPositions[i * 3 + 1] = 0.3 + Math.random() * 4.4;
pollenPositions[i * 3 + 2] = -1.8 - Math.random() * 3;
}
const pollenGeometry = new THREE.BufferGeometry();
pollenGeometry.setAttribute(
"position",
new THREE.BufferAttribute(pollenPositions, 3)
);
const pollen = new THREE.Points(
pollenGeometry,
new THREE.PointsMaterial({
color: 0xfff1b7,
size: 0.035,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
scene.add(pollen);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (autoOrbit && !dragging && !reducedMotion) {
cameraState.theta += dt * 0.075;
updateCamera();
}
if (!reducedMotion) {
mainLeaf.rotation.z =
mainLeafBaseRotation + Math.sin(time * 0.56) * 0.009;
smallLeaf.rotation.z =
-2.43 + Math.sin(time * 0.63 + 1.2) * 0.012;
chrysalis.rotation.z = Math.sin(time * 0.78) * 0.017;
butterfly.position.y =
1.93 + Math.sin(time * 1.25) * 0.035;
butterfly.rotation.z = Math.sin(time * 0.62) * 0.012;
const flutter = Math.sin(time * 2.15) * 0.12;
rightForewing.rotation.y = flutter;
leftForewing.rotation.y = -flutter;
rightHindwing.rotation.y = flutter * 0.72;
leftHindwing.rotation.y = -flutter * 0.72;
bodyParts.forEach((part, index) => {
const scale = 1 + Math.sin(time * 1.7 + index * 0.4) * 0.012;
part.scale.y = part.scale.y * 0.997 + 0.135 * scale * 0.003;
});
pollen.rotation.y = time * 0.018;
}
renderer.render(scene, camera);
updateAnnotations();
}
animate();
</script>
</body>
</html>
```