Prompt specification
Build or customize the Three.js 3D scene "Equal seeds different layouts" 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: Equal seeds different layouts
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/equal-seeds-different-layouts-x1106
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts seed spacing. Arrange the same seed count in crowded and evenly spaced grids; expand fictional root and leaf footprints to reveal different overlap patterns without predicting actual yield. Low-poly.
Working Three.js Starter (equal-seeds-different-layouts-x1106):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Seed Spacing — Schematic Footprint Study</title>
<style>
:root {
--ink: #26352c;
--muted: #657269;
--panel: rgba(250, 251, 244, 0.9);
--border: rgba(54, 72, 60, 0.16);
--green: #4f895d;
--plum: #9b5f78;
--gold: #e8b84f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7ebdf;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
#intro {
position: fixed;
z-index: 3;
top: 24px;
left: 26px;
max-width: 490px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: #758174;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 43px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 1.03;
}
#intro p {
max-width: 460px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.pill {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 13px;
padding: 6px 10px;
border: 1px solid rgba(76, 113, 80, 0.22);
border-radius: 999px;
background: rgba(247, 250, 240, 0.72);
color: #537057;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.pill::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
content: "";
}
#plot-labels {
position: fixed;
z-index: 2;
inset: 0;
pointer-events: none;
}
.plot-label {
position: absolute;
min-width: 174px;
padding: 9px 12px 10px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(251, 251, 245, 0.88);
box-shadow: 0 8px 24px rgba(55, 63, 45, 0.09);
transform: translate(-50%, -100%);
backdrop-filter: blur(10px);
}
.plot-label::after {
position: absolute;
bottom: -5px;
left: calc(50% - 5px);
width: 9px;
height: 9px;
border-right: 1px solid var(--border);
border-bottom: 1px solid var(--border);
background: rgba(251, 251, 245, 0.88);
content: "";
transform: rotate(45deg);
}
.plot-label strong {
display: block;
font-size: 13px;
letter-spacing: -0.015em;
}
.plot-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.plot-label small {
display: block;
margin-top: 5px;
color: #788177;
font-size: 9px;
line-height: 1.35;
}
#crowded-label {
border-top: 3px solid #c67b77;
}
#even-label {
border-top: 3px solid var(--green);
}
#controls {
position: fixed;
z-index: 5;
right: 24px;
bottom: 24px;
width: 304px;
padding: 16px;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(48, 59, 43, 0.15);
backdrop-filter: blur(16px);
}
.button-row {
display: flex;
gap: 8px;
}
button {
min-height: 38px;
padding: 0 12px;
border: 1px solid rgba(55, 75, 61, 0.2);
border-radius: 10px;
background: rgba(255, 255, 255, 0.65);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
border-color: rgba(55, 75, 61, 0.4);
background: #fff;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button.primary {
flex: 1;
border-color: #426d4d;
background: #426d4d;
color: white;
}
button.primary:hover {
background: #365f40;
}
.range-block {
margin-top: 15px;
}
.range-heading {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #536158;
font-size: 10px;
font-weight: 750;
}
#percent {
color: #899188;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 18px;
margin: 0;
accent-color: #4f895d;
cursor: pointer;
}
.switches {
display: flex;
gap: 16px;
margin-top: 12px;
}
.switch {
display: flex;
align-items: center;
gap: 7px;
color: #536158;
font-size: 10px;
font-weight: 700;
cursor: pointer;
}
.switch input {
width: 14px;
height: 14px;
margin: 0;
accent-color: #4f895d;
cursor: pointer;
}
.switch.root input {
accent-color: var(--plum);
}
.legend {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px 12px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--border);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #68736a;
font-size: 9px;
font-weight: 650;
}
.swatch {
width: 13px;
height: 13px;
flex: 0 0 auto;
border-radius: 4px;
}
.swatch.seed {
border-radius: 50%;
background: var(--gold);
box-shadow: inset -2px -2px 0 rgba(125, 83, 19, 0.15);
}
.swatch.root {
border: 1px solid rgba(116, 61, 87, 0.65);
background: rgba(155, 95, 120, 0.35);
}
.swatch.leaf {
border: 1px solid rgba(50, 112, 64, 0.62);
background: rgba(79, 137, 93, 0.35);
}
.swatch.overlap {
border: 1px solid rgba(55, 78, 57, 0.2);
background:
linear-gradient(135deg, rgba(79, 137, 93, 0.42), rgba(155, 95, 120, 0.52));
}
.note {
margin: 13px 0 0;
padding: 10px 11px;
border-radius: 10px;
background: rgba(76, 103, 79, 0.08);
color: #657168;
font-size: 9px;
line-height: 1.45;
}
.note strong {
color: #46594b;
}
#hint {
position: fixed;
z-index: 3;
bottom: 28px;
left: 26px;
color: rgba(57, 71, 60, 0.65);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
}
@media (max-width: 760px) {
#intro {
top: 14px;
left: 15px;
max-width: calc(100vw - 30px);
}
h1 {
font-size: 27px;
}
#intro p {
max-width: 310px;
margin-top: 7px;
font-size: 11px;
}
.pill {
margin-top: 8px;
font-size: 8px;
}
#controls {
right: 12px;
bottom: 12px;
width: min(304px, calc(100vw - 24px));
padding: 13px;
}
.plot-label {
min-width: 0;
padding: 7px 9px;
}
.plot-label strong {
font-size: 11px;
}
.plot-label span {
font-size: 9px;
}
.plot-label small {
display: none;
}
#hint {
display: none;
}
}
@media (max-height: 650px) {
#intro p,
.pill,
.legend {
display: none;
}
#controls {
padding: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly seed spacing comparison"></canvas>
<header id="intro">
<div class="eyebrow">Schematic spacing study · 01</div>
<h1>Same seeds.<br />Different spacing.</h1>
<p>
Two equal-sized beds hold the same 36 seeds. Expand the fictional root
and leaf-reach footprints to compare how shared space changes.
</p>
<div class="pill">Illustrative geometry only · no yield prediction</div>
</header>
<div id="plot-labels">
<div class="plot-label" id="crowded-label">
<strong>Crowded grid</strong>
<span>36 seeds</span>
<small id="crowded-detail">0.58 × 0.54 grid spacing</small>
</div>
<div class="plot-label" id="even-label">
<strong>Evenly spaced grid</strong>
<span>36 seeds</span>
<small id="even-detail">1.05 × 1.02 grid spacing</small>
</div>
</div>
<section id="controls" aria-label="Scene controls">
<div class="button-row">
<button id="expand" class="primary" type="button">Expand footprints</button>
<button id="reset" type="button">Seeds only</button>
<button id="view" type="button">Top view</button>
</div>
<div class="range-block">
<div class="range-heading">
<label for="expansion">Footprint expansion</label>
<span id="percent">0%</span>
</div>
<input
id="expansion"
type="range"
min="0"
max="100"
value="0"
step="1"
aria-label="Footprint expansion"
/>
</div>
<div class="switches">
<label class="switch root">
<input id="roots" type="checkbox" checked />
Root projection
</label>
<label class="switch">
<input id="leaves" type="checkbox" checked />
Leaf reach
</label>
</div>
<div class="legend" aria-label="Legend">
<div class="legend-item">
<span class="swatch seed"></span>
Seed
</div>
<div class="legend-item">
<span class="swatch root"></span>
Root projection
</div>
<div class="legend-item">
<span class="swatch leaf"></span>
Leaf reach
</div>
<div class="legend-item">
<span class="swatch overlap"></span>
Shared area
</div>
</div>
<p class="note">
<strong>Schematic only.</strong> Footprint sizes and shapes are invented.
Darker shared areas indicate geometric overlap—not crop performance,
competition outcomes, or expected yield.
</p>
</section>
<div id="hint">Drag to orbit · scroll or pinch to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7ebdf);
scene.fog = new THREE.Fog(0xe7ebdf, 31, 58);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xfffdf2, 0x77866a, 1.9);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d9, 2.8);
sun.position.set(-8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -15;
sun.shadow.camera.right = 15;
sun.shadow.camera.top = 13;
sun.shadow.camera.bottom = -13;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc7d9e2, 0.65);
fill.position.set(10, 6, -10);
scene.add(fill);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.MeshStandardMaterial({
color: 0xdce3cf,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.42;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Shared visual settings
// -------------------------------------------------------------------------
const GRID_SIZE = 6;
const SEEDS_PER_BED = GRID_SIZE * GRID_SIZE;
const ROOT_RADIUS = 0.83;
const LEAF_RADIUS = 0.62;
const crowdedSpacing = { x: 0.58, z: 0.54 };
const evenSpacing = { x: 1.05, z: 1.02 };
const rootMaterial = new THREE.MeshBasicMaterial({
color: 0x9b5f78,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const rootLineMaterial = new THREE.LineBasicMaterial({
color: 0x74465c,
transparent: true,
opacity: 0,
depthWrite: false
});
const leafMaterial = new THREE.MeshBasicMaterial({
color: 0x4f895d,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const leafLineMaterial = new THREE.LineBasicMaterial({
color: 0x376c47,
transparent: true,
opacity: 0,
depthWrite: false
});
const stemMaterial = new THREE.MeshStandardMaterial({
color: 0x52764b,
roughness: 0.9,
flatShading: true
});
const canopyMaterials = [
new THREE.MeshStandardMaterial({
color: 0x5e965f,
roughness: 1,
flatShading: true,
transparent: true,
opacity: 0.76,
depthWrite: false
}),
new THREE.MeshStandardMaterial({
color: 0x79a85f,
roughness: 1,
flatShading: true,
transparent: true,
opacity: 0.74,
depthWrite: false
}),
new THREE.MeshStandardMaterial({
color: 0x4f8358,
roughness: 1,
flatShading: true,
transparent: true,
opacity: 0.78,
depthWrite: false
})
];
const seedMaterials = [
new THREE.MeshStandardMaterial({
color: 0xe9b84f,
roughness: 0.86,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xf0c969,
roughness: 0.86,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd8a33c,
roughness: 0.86,
flatShading: true
})
];
const soilMaterial = new THREE.MeshStandardMaterial({
color: 0x74523d,
roughness: 1,
flatShading: true
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xb48a63,
roughness: 0.95,
flatShading: true
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0xc4b89d,
roughness: 1,
flatShading: true
});
const pebbleMaterials = [
new THREE.MeshStandardMaterial({
color: 0x9b7658,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x604b3d,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xc09a6d,
roughness: 1,
flatShading: true
})
];
const seedGeometry = new THREE.DodecahedronGeometry(0.095, 0);
const stemGeometry = new THREE.CylinderGeometry(0.018, 0.027, 1, 5);
stemGeometry.translate(0, 0.5, 0);
const canopyGeometry = new THREE.DodecahedronGeometry(1, 0);
const pebbleGeometry = new THREE.TetrahedronGeometry(1, 0);
const plants = [];
const beds = [];
// -------------------------------------------------------------------------
// Deterministic helpers
// -------------------------------------------------------------------------
function hash(value) {
const x = Math.sin(value * 127.1 + 311.7) * 43758.5453123;
return x - Math.floor(x);
}
function randomBetween(min, max) {
return min + (max - min) * Math.random();
}
function makeFootprint(seedIndex, layerOffset = 0) {
const sides = 12;
const perimeter = [];
for (let i = 0; i < sides; i++) {
const angle = (i / sides) * Math.PI * 2;
const alternating = i % 2 === 0 ? 1 : 0.79;
const variation =
0.88 + hash(seedIndex * 19 + i * 7 + layerOffset * 31) * 0.22;
const radius = alternating * variation;
perimeter.push(
new THREE.Vector3(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
)
);
}
const positions = [];
for (let i = 0; i < sides; i++) {
const a = perimeter[i];
const b = perimeter[(i + 1) % sides];
positions.push(
0, 0, 0,
a.x, a.y, a.z,
b.x, b.y, b.z
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const outlineGeometry = new THREE.BufferGeometry().setFromPoints(
perimeter.map((point) => point.clone())
);
return { geometry, outlineGeometry };
}
function gridPositions(spacing) {
const positions = [];
const half = (GRID_SIZE - 1) / 2;
const halfX = half * spacing.x;
const halfZ = half * spacing.z;
for (let row = 0; row < GRID_SIZE; row++) {
const z = (row - half) * spacing.z;
positions.push(-halfX, 0.012, z, halfX, 0.012, z);
}
for (let col = 0; col < GRID_SIZE; col++) {
const x = (col - half) * spacing.x;
positions.push(x, 0.012, -halfZ, x, 0.012, halfZ);
}
return positions;
}
// -------------------------------------------------------------------------
// Bed and plant construction
// -------------------------------------------------------------------------
function createBed(centerX, spacing, typeIndex) {
const bed = new THREE.Group();
bed.position.x = centerX;
scene.add(bed);
beds.push(bed);
const base = new THREE.Mesh(
new THREE.BoxGeometry(8.15, 0.22, 6.95),
baseMaterial
);
base.position.y = -0.31;
base.castShadow = true;
base.receiveShadow = true;
bed.add(base);
const soil = new THREE.Mesh(
new THREE.BoxGeometry(7.62, 0.34, 6.38),
soilMaterial
);
soil.position.y = -0.17;
soil.castShadow = true;
soil.receiveShadow = true;
bed.add(soil);
const railLong = new THREE.BoxGeometry(7.98, 0.24, 0.17);
const railShort = new THREE.BoxGeometry(0.17, 0.24, 6.58);
for (const z of [-3.27, 3.27]) {
const rail = new THREE.Mesh(railLong, frameMaterial);
rail.position.set(0, 0.045, z);
rail.castShadow = true;
rail.receiveShadow = true;
bed.add(rail);
}
for (const x of [-3.91, 3.91]) {
const rail = new THREE.Mesh(railShort, frameMaterial);
rail.position.set(x, 0.045, 0);
rail.castShadow = true;
rail.receiveShadow = true;
bed.add(rail);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions(spacing), 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0xd2b694,
transparent: true,
opacity: 0.22,
depthWrite: false
})
);
grid.renderOrder = 0;
bed.add(grid);
const localPositions = [];
const half = (GRID_SIZE - 1) / 2;
for (let row = 0; row < GRID_SIZE; row++) {
for (let col = 0; col < GRID_SIZE; col++) {
const x = (col - half) * spacing.x;
const z = (row - half) * spacing.z;
localPositions.push({ x, z });
const plant = new THREE.Group();
plant.position.set(x, 0, z);
bed.add(plant);
const seedIndex = typeIndex * 100 + row * GRID_SIZE + col;
const seed = new THREE.Mesh(
seedGeometry,
seedMaterials[Math.floor(hash(seedIndex + 4) * seedMaterials.length)]
);
seed.position.y = 0.11;
seed.scale.set(1, 1.35, 0.72);
seed.rotation.set(
hash(seedIndex * 2) * 0.5,
hash(seedIndex * 3) * Math.PI,
hash(seedIndex * 5) * 0.5
);
seed.castShadow = true;
plant.add(seed);
const rootShape = makeFootprint(seedIndex, 0);
const root = new THREE.Mesh(rootShape.geometry, rootMaterial);
root.position.y = 0.032;
root.renderOrder = 1;
plant.add(root);
const rootOutline = new THREE.LineLoop(
rootShape.outlineGeometry,
rootLineMaterial
);
rootOutline.position.y = 0.041;
rootOutline.renderOrder = 2;
plant.add(rootOutline);
const leafShape = makeFootprint(seedIndex, 1);
const leaf = new THREE.Mesh(leafShape.geometry, leafMaterial);
leaf.position.y = 0.43;
leaf.renderOrder = 3;
plant.add(leaf);
const leafOutline = new THREE.LineLoop(
leafShape.outlineGeometry,
leafLineMaterial
);
leafOutline.position.y = 0.442;
leafOutline.renderOrder = 4;
plant.add(leafOutline);
const stem = new THREE.Mesh(stemGeometry, stemMaterial);
stem.position.y = 0.05;
stem.castShadow = true;
plant.add(stem);
const canopy = new THREE.Mesh(
canopyGeometry,
canopyMaterials[
Math.floor(hash(seedIndex + 17) * canopyMaterials.length)
]
);
canopy.position.set(
(hash(seedIndex + 8) - 0.5) * 0.1,
0.49,
(hash(seedIndex + 12) - 0.5) * 0.1
);
canopy.rotation.set(
hash(seedIndex + 6) * 0.6,
hash(seedIndex + 9) * Math.PI,
hash(seedIndex + 13) * 0.6
);
canopy.castShadow = true;
canopy.renderOrder = 5;
plant.add(canopy);
plants.push({
root,
rootOutline,
leaf,
leafOutline,
stem,
canopy,
canopyScale: 0.27 + hash(seedIndex + 28) * 0.07,
canopyHeight: 0.46 + hash(seedIndex + 31) * 0.07
});
}
}
// A handful of low-poly soil clods, kept away from the seed centers.
for (let i = 0; i < 27; i++) {
let x = randomBetween(-3.45, 3.45);
let z = randomBetween(-2.95, 2.95);
for (const position of localPositions) {
const dx = x - position.x;
const dz = z - position.z;
if (dx * dx + dz * dz < 0.075) {
x += 0.35;
z += 0.2;
}
}
const pebble = new THREE.Mesh(
pebbleGeometry,
pebbleMaterials[
Math.floor(hash(i + typeIndex * 40) * pebbleMaterials.length)
]
);
const size = randomBetween(0.035, 0.09);
pebble.position.set(x, 0.045, z);
pebble.scale.set(size, size * 0.65, size * 1.2);
pebble.rotation.set(
Math.random() * Math.PI,
Math.random() * Math.PI,
Math.random() * Math.PI
);
pebble.castShadow = true;
bed.add(pebble);
}
return localPositions;
}
const crowdedPositions = createBed(
-4.45,
crowdedSpacing,
0
);
const evenPositions = createBed(
4.45,
evenSpacing,
1
);
// -------------------------------------------------------------------------
// Geometric overlap counts. These are descriptive only, not yield metrics.
// -------------------------------------------------------------------------
function countOverlapPairs(positions, radius) {
let count = 0;
const diameter = radius * 2;
for (let i = 0; i < positions.length; i++) {
for (let j = i + 1; j < positions.length; j++) {
const dx = positions[i].x - positions[j].x;
const dz = positions[i].z - positions[j].z;
if (dx * dx + dz * dz <= diameter * diameter) {
count++;
}
}
}
return count;
}
const crowdedRootPairs = countOverlapPairs(
crowdedPositions,
ROOT_RADIUS
);
const evenRootPairs = countOverlapPairs(
evenPositions,
ROOT_RADIUS
);
const crowdedLeafPairs = countOverlapPairs(
crowdedPositions,
LEAF_RADIUS
);
const evenLeafPairs = countOverlapPairs(
evenPositions,
LEAF_RADIUS
);
document.querySelector("#crowded-detail").textContent =
`0.58 × 0.54 spacing · ${crowdedRootPairs} root / ` +
`${crowdedLeafPairs} leaf overlap pairs`;
document.querySelector("#even-detail").textContent =
`1.05 × 1.02 spacing · ${evenRootPairs} root / ` +
`${evenLeafPairs} leaf overlap pairs`;
// -------------------------------------------------------------------------
// Direct camera controls: orbit and zoom
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.28,
phi: 0.89,
radius: window.innerWidth < 760 ? 29 : 21,
targetTheta: 0.28,
targetPhi: 0.89,
targetRadius: window.innerWidth < 760 ? 29 : 21
};
const cameraTarget = new THREE.Vector3(0, 0.1, 0);
let dragging = false;
let previousX = 0;
let previousY = 0;
let topView = false;
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi - dy * 0.005,
0.08,
1.35
);
topView = false;
document.querySelector("#view").textContent = "Top view";
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
12,
46
);
},
{ passive: false }
);
// Pinch zoom.
const activePointers = new Map();
let pinchDistance = null;
canvas.addEventListener("pointerdown", (event) => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance !== null) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius *
(pinchDistance / Math.max(distance, 1)),
12,
46
);
}
pinchDistance = distance;
}
});
function removePointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size < 2) pinchDistance = null;
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
// -------------------------------------------------------------------------
// UI controls
// -------------------------------------------------------------------------
const expansionInput = document.querySelector("#expansion");
const percentLabel = document.querySelector("#percent");
const rootsInput = document.querySelector("#roots");
const leavesInput = document.querySelector("#leaves");
let targetExpansion = 0;
let currentExpansion = 0;
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function setExpansion(value) {
targetExpansion = THREE.MathUtils.clamp(value, 0, 1);
expansionInput.value = Math.round(targetExpansion * 100);
percentLabel.textContent = `${Math.round(targetExpansion * 100)}%`;
}
expansionInput.addEventListener("input", () => {
setExpansion(Number(expansionInput.value) / 100);
});
document.querySelector("#expand").addEventListener("click", () => {
setExpansion(1);
});
document.querySelector("#reset").addEventListener("click", () => {
setExpansion(0);
});
document.querySelector("#view").addEventListener("click", (event) => {
topView = !topView;
if (topView) {
cameraState.targetTheta = 0;
cameraState.targetPhi = 0.08;
event.currentTarget.textContent = "Orbit view";
} else {
cameraState.targetTheta = 0.28;
cameraState.targetPhi = 0.89;
event.currentTarget.textContent = "Top view";
}
});
function applyLayerVisibility() {
for (const plant of plants) {
plant.root.visible = rootsInput.checked;
plant.rootOutline.visible = rootsInput.checked;
plant.leaf.visible = leavesInput.checked;
plant.leafOutline.visible = leavesInput.checked;
plant.stem.visible = leavesInput.checked;
plant.canopy.visible = leavesInput.checked;
}
}
rootsInput.addEventListener("change", applyLayerVisibility);
leavesInput.addEventListener("change", applyLayerVisibility);
applyLayerVisibility();
// -------------------------------------------------------------------------
// Screen-space plot labels
// -------------------------------------------------------------------------
const labels = [
{
element: document.querySelector("#crowded-label"),
anchor: new THREE.Vector3(-4.45, 0.35, -3.48)
},
{
element: document.querySelector("#even-label"),
anchor: new THREE.Vector3(4.45, 0.35, -3.48)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
label.element.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Resize
// -------------------------------------------------------------------------
let firstResize = true;
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (firstResize) {
cameraState.radius = width < 760 ? 29 : 21;
cameraState.targetRadius = cameraState.radius;
firstResize = false;
}
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function updatePlants(delta) {
const smoothing = reducedMotion
? 1
: 1 - Math.exp(-2.7 * delta);
currentExpansion +=
(targetExpansion - currentExpansion) * smoothing;
if (Math.abs(targetExpansion - currentExpansion) < 0.0001) {
currentExpansion = targetExpansion;
}
const expansion = currentExpansion;
const fade = Math.min(1, expansion * 3);
rootMaterial.opacity = 0.22 * fade;
rootLineMaterial.opacity = 0.52 * fade;
leafMaterial.opacity = 0.21 * fade;
leafLineMaterial.opacity = 0.46 * fade;
for (const plant of plants) {
const rootScale = Math.max(0.0001, expansion * ROOT_RADIUS);
const leafScale = Math.max(0.0001, expansion * LEAF_RADIUS);
plant.root.scale.set(rootScale, rootScale, rootScale);
plant.rootOutline.scale.set(rootScale, 1, rootScale);
plant.leaf.scale.set(leafScale, leafScale, leafScale);
plant.leafOutline.scale.set(leafScale, 1, leafScale);
plant.stem.scale.set(1, Math.max(0.0001, expansion), 1);
const canopySize =
Math.max(0.0001, expansion * plant.canopyScale);
plant.canopy.scale.set(
canopySize,
canopySize * 0.72,
canopySize
);
plant.canopy.position.y =
plant.canopyHeight * Math.max(0.35, expansion);
}
}
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-8 * delta);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * smoothing;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * smoothing;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + cameraState.radius * cosPhi,
cameraTarget.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
updatePlants(delta);
updateCamera(delta);
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```