Prompt specification
Build or customize the Three.js 3D scene "Tall and low crop canopy pair" 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: Tall and low crop canopy pair
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tall-and-low-crop-canopy-pair-x1077
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring intercropping. Show a tall narrow plant beside a low spreading plant with root and canopy envelopes; compare their occupied spaces without asserting that all such pairings are beneficial. Stylized solid forms.
Working Three.js Starter (tall-and-low-crop-canopy-pair-x1077):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Intercropping — Occupied Space Comparison</title>
<style>
:root {
--bg: #071713;
--panel: rgba(9, 28, 24, 0.84);
--panel-border: rgba(171, 224, 199, 0.18);
--text: #eef8f2;
--muted: #a8bdb2;
--green: #80d878;
--green-dark: #3f9b61;
--amber: #e6ad64;
--amber-dark: #a36a35;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#webgl {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas.grabbing {
cursor: grabbing;
}
.glass {
background: var(--panel);
border: 1px solid var(--panel-border);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.header {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(430px, calc(100vw - 36px));
padding: 18px 20px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--green);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.35rem, 2.4vw, 2rem);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 0.87rem;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 14px;
margin-top: 13px;
color: #d7e6dc;
font-size: 0.74rem;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.green {
color: var(--green);
background: var(--green);
}
.dot.amber {
color: var(--amber);
background: var(--amber);
}
.controls {
position: fixed;
z-index: 12;
top: 18px;
right: 18px;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 15px;
}
button {
border: 1px solid rgba(194, 232, 211, 0.17);
border-radius: 10px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
font: inherit;
font-size: 0.78rem;
font-weight: 700;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(128, 216, 120, 0.13);
border-color: rgba(128, 216, 120, 0.45);
}
button:active {
transform: translateY(1px);
}
button:focus-visible {
outline: 2px solid var(--green);
outline-offset: 2px;
}
.help {
position: fixed;
z-index: 9;
right: 20px;
bottom: 20px;
padding: 8px 11px;
border-radius: 999px;
background: rgba(4, 16, 13, 0.64);
color: var(--muted);
font-size: 0.7rem;
pointer-events: none;
}
.comparison {
position: fixed;
z-index: 11;
left: 50%;
bottom: 18px;
width: min(900px, calc(100vw - 36px));
transform: translateX(-50%);
padding: 13px;
border-radius: 18px;
}
.cards {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.plant-card {
min-width: 0;
padding: 13px 15px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.plant-card.tall {
border-top: 2px solid var(--green-dark);
}
.plant-card.spreading {
border-top: 2px solid #6abf67;
}
.plant-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
}
.plant-heading strong {
font-size: 0.93rem;
letter-spacing: -0.01em;
}
.plant-heading span {
color: var(--muted);
font-size: 0.67rem;
text-transform: uppercase;
letter-spacing: 0.1em;
white-space: nowrap;
}
.metric-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 12px;
}
.metric-label {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 0.65rem;
line-height: 1.2;
}
.metric-value {
display: block;
font-size: 0.78rem;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.track {
height: 3px;
margin-top: 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.track i {
display: block;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #3e8d5e, var(--green));
}
.track.amber i {
background: linear-gradient(90deg, #9b6335, var(--amber));
}
.disclaimer {
margin: 10px 3px 0;
color: #aebfb5;
font-size: 0.68rem;
line-height: 1.45;
}
.disclaimer strong {
color: #e6eee9;
}
#labels {
position: fixed;
z-index: 5;
inset: 0;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
top: 0;
left: 0;
min-width: max-content;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
background: rgba(5, 19, 15, 0.78);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.2);
color: #edf7f1;
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.055em;
text-transform: uppercase;
white-space: nowrap;
will-change: transform;
}
.scene-label.green {
border-color: rgba(128, 216, 120, 0.36);
}
.scene-label.amber {
border-color: rgba(230, 173, 100, 0.42);
color: #f5d5aa;
}
@media (max-width: 760px) {
.header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px 15px;
}
.header .subtitle,
.legend {
display: none;
}
.controls {
top: 112px;
right: 10px;
flex-wrap: wrap;
justify-content: flex-end;
max-width: calc(100vw - 20px);
}
button {
padding: 8px 10px;
font-size: 0.7rem;
}
.comparison {
bottom: 8px;
width: calc(100vw - 16px);
padding: 8px;
}
.cards {
gap: 6px;
}
.plant-card {
padding: 9px;
}
.plant-heading {
display: block;
}
.plant-heading span {
display: block;
margin-top: 2px;
}
.metric-grid {
grid-template-columns: 1fr;
gap: 6px;
}
.metric:nth-child(3) {
display: none;
}
.disclaimer {
font-size: 0.61rem;
}
.help {
display: none;
}
.scene-label {
font-size: 0.56rem;
}
}
</style>
</head>
<body>
<main id="app">
<div id="webgl" aria-label="Interactive Three.js intercropping scene"></div>
<div id="labels" aria-hidden="true">
<div id="label-tall-canopy" class="scene-label green">Tall / narrow canopy</div>
<div id="label-tall-root" class="scene-label amber">Root envelope</div>
<div id="label-low-canopy" class="scene-label green">Low / spreading canopy</div>
<div id="label-low-root" class="scene-label amber">Root envelope</div>
</div>
<header class="header glass">
<div class="eyebrow">Intercropping · shared bed</div>
<h1>Comparing occupied space</h1>
<p class="subtitle">
A tall, narrow plant beside a low, spreading plant, with schematic
canopy and root envelopes shown in the same soil bed.
</p>
<div class="legend">
<span><i class="dot green"></i>Canopy envelope</span>
<span><i class="dot amber"></i>Root envelope</span>
</div>
</header>
<nav class="controls glass" aria-label="Scene controls">
<button id="toggle-envelopes" type="button" aria-pressed="true">
Envelopes: on
</button>
<button id="top-view" type="button">Top view</button>
<button id="reset-view" type="button">Reset</button>
</nav>
<section class="comparison glass" aria-label="Occupied space comparison">
<div class="cards">
<article class="plant-card tall">
<div class="plant-heading">
<strong>Tall / narrow</strong>
<span>Form A</span>
</div>
<div class="metric-grid">
<div class="metric">
<span class="metric-label">Above-ground reach</span>
<span class="metric-value">5.2 units</span>
<div class="track"><i style="width:100%"></i></div>
</div>
<div class="metric">
<span class="metric-label">Canopy span · w × d</span>
<span class="metric-value">1.9 × 1.5</span>
<div class="track"><i style="width:50%"></i></div>
</div>
<div class="metric">
<span class="metric-label">Root span · w × d</span>
<span class="metric-value">1.5 × 1.3</span>
<div class="track amber"><i style="width:52%"></i></div>
</div>
</div>
</article>
<article class="plant-card spreading">
<div class="plant-heading">
<strong>Low / spreading</strong>
<span>Form B</span>
</div>
<div class="metric-grid">
<div class="metric">
<span class="metric-label">Above-ground reach</span>
<span class="metric-value">1.6 units</span>
<div class="track"><i style="width:31%"></i></div>
</div>
<div class="metric">
<span class="metric-label">Canopy span · w × d</span>
<span class="metric-value">3.8 × 2.9</span>
<div class="track"><i style="width:100%"></i></div>
</div>
<div class="metric">
<span class="metric-label">Root span · w × d</span>
<span class="metric-value">2.9 × 2.2</span>
<div class="track amber"><i style="width:100%"></i></div>
</div>
</div>
</article>
</div>
<p class="disclaimer">
<strong>Spatial comparison only.</strong> Dimensions are schematic scene
units, not field measurements. This arrangement does not imply that
tall–low pairings, or any particular pairing, are beneficial. Real
outcomes depend on species, timing, soil, water, light, and management.
</p>
</section>
<div class="help">Drag to orbit · Scroll or pinch to zoom</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#webgl");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071713);
scene.fog = new THREE.Fog(0x071713, 18, 31);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xd8fff0, 0x382416, 2.0);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1ca, 3.1);
sun.position.set(-5, 11, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 35;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ed5ff, 1.25);
fill.position.set(7, 5, -8);
scene.add(fill);
const material = (color, roughness = 0.82) =>
new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0
});
const materials = {
tallStem: material(0x347849),
tallStemLight: material(0x4b9954),
tallLeaf: material(0x68bb5c),
tallLeafDark: material(0x3f9450),
lowStem: material(0x3d8244),
lowLeaf: material(0x7bc45c),
lowLeafDark: material(0x519d48),
root: material(0x9b6737),
rootDark: material(0x754827)
};
const leafGeometry = new THREE.SphereGeometry(1, 12, 8);
const yAxis = new THREE.Vector3(0, 1, 0);
const envelopeObjects = [];
function addSegment(
parent,
a,
b,
radiusA,
radiusB,
meshMaterial,
segments = 8
) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radiusB,
radiusA,
length,
segments,
1,
false
);
const mesh = new THREE.Mesh(geometry, meshMaterial);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addLeaf(parent, position, direction, size, leafMaterial) {
const leaf = new THREE.Mesh(leafGeometry, leafMaterial);
leaf.position.copy(position);
leaf.quaternion.setFromUnitVectors(yAxis, direction.clone().normalize());
leaf.scale.set(size.x, size.y, size.z);
leaf.castShadow = true;
leaf.receiveShadow = true;
parent.add(leaf);
return leaf;
}
function addCircle(parent, plane, material, opacity = 0.55) {
const points = [];
const count = 72;
for (let i = 0; i <= count; i++) {
const a = (i / count) * Math.PI * 2;
const c = Math.cos(a);
const s = Math.sin(a);
if (plane === "xy") points.push(new THREE.Vector3(c, s, 0));
if (plane === "yz") points.push(new THREE.Vector3(0, c, s));
if (plane === "xz") points.push(new THREE.Vector3(c, 0, s));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const lineMaterial = new THREE.LineBasicMaterial({
color: material.color,
transparent: true,
opacity,
depthWrite: false
});
const line = new THREE.Line(geometry, lineMaterial);
line.renderOrder = 3;
parent.add(line);
}
function createEnvelope(position, scale, color) {
const group = new THREE.Group();
group.position.copy(position);
group.scale.copy(scale);
const fill = new THREE.Mesh(
new THREE.SphereGeometry(1, 32, 20),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.075,
depthWrite: false,
side: THREE.DoubleSide
})
);
fill.renderOrder = 2;
group.add(fill);
const contourMaterial = new THREE.MeshBasicMaterial({ color });
addCircle(group, "xy", contourMaterial, 0.52);
addCircle(group, "yz", contourMaterial, 0.4);
addCircle(group, "xz", contourMaterial, 0.6);
scene.add(group);
envelopeObjects.push(group);
return group;
}
// Shared cutaway soil bed.
const soilMaterial = new THREE.MeshStandardMaterial({
color: 0x8c5c35,
roughness: 0.95,
transparent: true,
opacity: 0.18,
depthWrite: false,
side: THREE.DoubleSide
});
const soil = new THREE.Mesh(
new THREE.BoxGeometry(11.5, 2.2, 5.4),
soilMaterial
);
soil.position.y = -1.1;
scene.add(soil);
const soilEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(soil.geometry),
new THREE.LineBasicMaterial({
color: 0xb98252,
transparent: true,
opacity: 0.42
})
);
soilEdges.position.copy(soil.position);
scene.add(soilEdges);
const soilSurface = new THREE.Mesh(
new THREE.PlaneGeometry(11.5, 5.4),
new THREE.MeshStandardMaterial({
color: 0xa47648,
roughness: 1,
transparent: true,
opacity: 0.31,
depthWrite: false,
side: THREE.DoubleSide
})
);
soilSurface.rotation.x = -Math.PI / 2;
soilSurface.position.y = 0.012;
soilSurface.receiveShadow = true;
scene.add(soilSurface);
// Subtle strata on the front cutaway face.
for (const y of [-0.45, -1.0, -1.62]) {
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-5.74, y, 2.705),
new THREE.Vector3(5.74, y + 0.025, 2.705)
]);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0xd39a63,
transparent: true,
opacity: 0.25
})
);
scene.add(line);
}
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
material(0x06130f, 1)
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -2.23;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 60, 0x315c4b, 0x17392f);
grid.position.y = -2.215;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// Tall, narrow plant.
const tallPlant = new THREE.Group();
tallPlant.position.set(-2.5, 0, 0);
scene.add(tallPlant);
const tallStemPoints = [
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.04, 1.25, 0.02),
new THREE.Vector3(-0.07, 2.48, 0.04),
new THREE.Vector3(0.05, 3.72, 0),
new THREE.Vector3(0, 5.02, 0.03)
];
for (let i = 0; i < tallStemPoints.length - 1; i++) {
addSegment(
tallPlant,
tallStemPoints[i],
tallStemPoints[i + 1],
0.13 - i * 0.018,
0.112 - i * 0.018,
i % 2 ? materials.tallStemLight : materials.tallStem,
9
);
}
for (let i = 0; i < 9; i++) {
const y = 0.72 + i * 0.52;
const angle = i * 2.399 + 0.3;
const length = 0.72 + (1 - i / 10) * 0.23;
const start = new THREE.Vector3(0, y, 0);
const elbow = new THREE.Vector3(
Math.cos(angle) * length * 0.58,
y + 0.12,
Math.sin(angle) * length * 0.58
);
const end = new THREE.Vector3(
Math.cos(angle) * length,
y + 0.19,
Math.sin(angle) * length
);
addSegment(
tallPlant,
start,
elbow,
0.055,
0.036,
materials.tallStem,
7
);
addSegment(
tallPlant,
elbow,
end,
0.036,
0.018,
materials.tallStemLight,
7
);
const leafDirection = end.clone().sub(start).normalize();
leafDirection.y += 0.28;
leafDirection.normalize();
addLeaf(
tallPlant,
end.clone().addScaledVector(leafDirection, 0.08),
leafDirection,
new THREE.Vector3(0.25, 0.56, 0.105),
i % 2 ? materials.tallLeaf : materials.tallLeafDark
);
addLeaf(
tallPlant,
elbow.clone().addScaledVector(leafDirection, 0.04),
leafDirection.clone().add(new THREE.Vector3(0, 0.15, 0)),
new THREE.Vector3(0.21, 0.43, 0.09),
materials.tallLeaf
);
}
addLeaf(
tallPlant,
new THREE.Vector3(0, 5.05, 0.03),
new THREE.Vector3(0.1, 1, 0),
new THREE.Vector3(0.19, 0.45, 0.085),
materials.tallLeaf
);
// Tall plant root network.
addSegment(
tallPlant,
new THREE.Vector3(0, -0.04, 0),
new THREE.Vector3(0.02, -1.92, 0),
0.17,
0.025,
materials.rootDark,
9
);
for (let i = 0; i < 8; i++) {
const depth = 0.28 + i * 0.19;
const angle = i * 2.399;
const length = 0.48 + (i % 3) * 0.11;
const start = new THREE.Vector3(0, -depth, 0);
const elbow = new THREE.Vector3(
Math.cos(angle) * length * 0.52,
-depth - 0.12,
Math.sin(angle) * length * 0.44
);
const end = new THREE.Vector3(
Math.cos(angle) * length,
-depth - 0.2,
Math.sin(angle) * length * 0.78
);
addSegment(
tallPlant,
start,
elbow,
0.075,
0.045,
materials.root,
7
);
addSegment(
tallPlant,
elbow,
end,
0.045,
0.012,
materials.rootDark,
7
);
const sideAngle = angle + 0.55;
addSegment(
tallPlant,
elbow,
new THREE.Vector3(
elbow.x + Math.cos(sideAngle) * 0.25,
elbow.y - 0.22,
elbow.z + Math.sin(sideAngle) * 0.21
),
0.025,
0.008,
materials.root,
6
);
}
createEnvelope(
new THREE.Vector3(-2.5, 2.65, 0),
new THREE.Vector3(1.13, 2.65, 0.85),
0x79db74
);
createEnvelope(
new THREE.Vector3(-2.5, -1.02, 0),
new THREE.Vector3(0.75, 1.08, 0.65),
0xe5a65b
);
// Low, spreading plant.
const lowPlant = new THREE.Group();
lowPlant.position.set(2.4, 0, 0.12);
scene.add(lowPlant);
addSegment(
lowPlant,
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.02, 0.62, 0),
0.16,
0.105,
materials.lowStem,
9
);
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2 + 0.18;
const radius = i % 2 === 0 ? 1.52 : 1.3;
const start = new THREE.Vector3(
Math.cos(angle) * 0.08,
0.32 + (i % 3) * 0.08,
Math.sin(angle) * 0.08
);
const elbow = new THREE.Vector3(
Math.cos(angle) * radius * 0.52,
0.42 + (i % 2) * 0.08,
Math.sin(angle) * radius * 0.52
);
const end = new THREE.Vector3(
Math.cos(angle) * radius,
0.37 + (i % 3) * 0.09,
Math.sin(angle) * radius
);
addSegment(
lowPlant,
start,
elbow,
0.075,
0.044,
materials.lowStem,
7
);
addSegment(
lowPlant,
elbow,
end,
0.044,
0.018,
materials.lowStem,
7
);
const leafDirection = end.clone().sub(start).normalize();
leafDirection.y = 0.48;
leafDirection.normalize();
addLeaf(
lowPlant,
end.clone().addScaledVector(leafDirection, 0.06),
leafDirection,
new THREE.Vector3(0.42, 0.76, 0.13),
i % 2 ? materials.lowLeaf : materials.lowLeafDark
);
const sideDirection = new THREE.Vector3(
-Math.sin(angle),
0.45,
Math.cos(angle)
).normalize();
addLeaf(
lowPlant,
elbow.clone().addScaledVector(sideDirection, 0.08),
sideDirection,
new THREE.Vector3(0.34, 0.61, 0.11),
materials.lowLeaf
);
}
// Low plant broad root network.
addSegment(
lowPlant,
new THREE.Vector3(0, -0.04, 0),
new THREE.Vector3(0.03, -1.48, 0),
0.19,
0.03,
materials.rootDark,
9
);
for (let i = 0; i < 9; i++) {
const angle = (i / 9) * Math.PI * 2 + 0.24;
const depth = 0.25 + (i % 4) * 0.18;
const length = 1.02 + (i % 3) * 0.16;
const start = new THREE.Vector3(0, -depth, 0);
const elbow = new THREE.Vector3(
Math.cos(angle) * length * 0.5,
-depth - 0.12,
Math.sin(angle) * length * 0.45
);
const end = new THREE.Vector3(
Math.cos(angle) * length,
-depth - 0.22,
Math.sin(angle) * length * 0.78
);
addSegment(
lowPlant,
start,
elbow,
0.095,
0.05,
materials.root,
7
);
addSegment(
lowPlant,
elbow,
end,
0.05,
0.012,
materials.rootDark,
7
);
const branchAngle = angle + (i % 2 === 0 ? 0.62 : -0.62);
const branchStart = elbow.clone();
const branchEnd = new THREE.Vector3(
elbow.x + Math.cos(branchAngle) * 0.43,
elbow.y - 0.25,
elbow.z + Math.sin(branchAngle) * 0.36
);
addSegment(
lowPlant,
branchStart,
branchEnd,
0.032,
0.009,
materials.root,
6
);
}
createEnvelope(
new THREE.Vector3(2.4, 0.76, 0.12),
new THREE.Vector3(1.9, 0.76, 1.45),
0x79db74
);
createEnvelope(
new THREE.Vector3(2.4, -0.82, 0.12),
new THREE.Vector3(1.45, 0.92, 1.1),
0xe5a65b
);
// Camera controls implemented directly, without external add-ons.
const currentView = {
theta: 0.58,
phi: 1.13,
radius: 15.6,
target: new THREE.Vector3(0, 1.25, 0)
};
const goalView = {
theta: currentView.theta,
phi: currentView.phi,
radius: currentView.radius,
target: currentView.target.clone()
};
const clamp = THREE.MathUtils.clamp;
const canvas = renderer.domElement;
let dragging = false;
let lastX = 0;
let lastY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("grabbing");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
goalView.theta -= dx * 0.006;
goalView.phi = clamp(goalView.phi - dy * 0.005, 0.08, 1.5);
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("grabbing");
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
goalView.radius = clamp(
goalView.radius * Math.exp(event.deltaY * 0.001),
9.5,
23
);
},
{ passive: false }
);
let previousTouchDistance = null;
canvas.addEventListener(
"touchstart",
(event) => {
if (event.touches.length === 2) {
previousTouchDistance = event.touches[0].distance -
event.touches[1].distance;
}
},
{ passive: true }
);
canvas.addEventListener(
"touchmove",
(event) => {
if (event.touches.length === 2 && previousTouchDistance !== null) {
const distance = event.touches[0].distance -
event.touches[1].distance;
const factor = previousTouchDistance / Math.max(distance, 1);
goalView.radius = clamp(goalView.radius * factor, 9.5, 23);
previousTouchDistance = distance;
}
},
{ passive: true }
);
const resetButton = document.querySelector("#reset-view");
const topButton = document.querySelector("#top-view");
const envelopeButton = document.querySelector("#toggle-envelopes");
resetButton.addEventListener("click", () => {
goalView.theta = 0.58;
goalView.phi = 1.13;
goalView.radius = 15.6;
goalView.target.set(0, 1.25, 0);
});
topButton.addEventListener("click", () => {
goalView.theta = 0;
goalView.phi = 0.075;
goalView.radius = 14.8;
goalView.target.set(0, 0.35, 0);
});
let envelopesVisible = true;
envelopeButton.addEventListener("click", () => {
envelopesVisible = !envelopesVisible;
envelopeObjects.forEach((object) => {
object.visible = envelopesVisible;
});
envelopeButton.textContent = envelopesVisible
? "Envelopes: on"
: "Envelopes: off";
envelopeButton.setAttribute(
"aria-pressed",
String(envelopesVisible)
);
});
// HTML labels projected from world-space anchor points.
const labels = [
{
element: document.querySelector("#label-tall-canopy"),
position: new THREE.Vector3(-2.5, 4.85, 0)
},
{
element: document.querySelector("#label-tall-root"),
position: new THREE.Vector3(-2.5, -1.15, 0)
},
{
element: document.querySelector("#label-low-canopy"),
position: new THREE.Vector3(2.4, 1.52, 0.12)
},
{
element: document.querySelector("#label-low-root"),
position: new THREE.Vector3(2.4, -0.85, 0.12)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
labels.forEach((label) => {
projected.copy(label.position).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.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
label.element.style.transform =
`translate(-50%, -50%) translate(${x}px, ${y}px)`;
});
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const damping = 1 - Math.exp(-8 * dt);
currentView.theta = THREE.MathUtils.lerp(
currentView.theta,
goalView.theta,
damping
);
currentView.phi = THREE.MathUtils.lerp(
currentView.phi,
goalView.phi,
damping
);
currentView.radius = THREE.MathUtils.lerp(
currentView.radius,
goalView.radius,
damping
);
currentView.target.lerp(goalView.target, damping);
const offset = new THREE.Vector3(
currentView.radius *
Math.sin(currentView.phi) *
Math.sin(currentView.theta),
currentView.radius * Math.cos(currentView.phi),
currentView.radius *
Math.sin(currentView.phi) *
Math.cos(currentView.theta)
);
camera.position.copy(currentView.target).add(offset);
camera.lookAt(currentView.target);
camera.updateMatrixWorld();
updateLabels();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```