Prompt specification
Build or customize the Three.js 3D scene "Stem transport tissue section" 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: Stem transport tissue section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/stem-transport-tissue-section-x0797
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts plant vascular transport. Show a magnified stem cross-section with distinct labeled transport regions and a full plant silhouette; use color consistently between the enlarged tissue and whole-organ reference. Stylized solid forms.
Working Three.js Starter (stem-transport-tissue-section-x0797):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Plant Vascular Transport</title>
<style>
:root {
--ink: #20332b;
--muted: #68746d;
--paper: #f3efe5;
--card: rgba(255, 253, 247, 0.94);
--xylem: #2788c5;
--phloem: #e36d4f;
--cambium: #9fbd4c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
header {
position: absolute;
top: 28px;
left: 32px;
z-index: 4;
max-width: 470px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 8px;
color: #758078;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(31px, 4vw, 55px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
max-width: 420px;
margin: 13px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.5;
}
#legend {
position: absolute;
top: 28px;
right: 30px;
z-index: 4;
display: grid;
gap: 8px;
width: 190px;
padding: 13px;
border: 1px solid rgba(61, 78, 67, 0.12);
border-radius: 14px;
background: var(--card);
box-shadow: 0 12px 35px rgba(55, 65, 54, 0.09);
backdrop-filter: blur(8px);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #35483f;
font-size: 12px;
font-weight: 700;
}
.legend-row span:last-child {
margin-left: auto;
color: #89918b;
font-size: 11px;
font-weight: 600;
}
.swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--color);
box-shadow: 0 0 0 4px color-mix(in srgb, var(--color) 15%, transparent);
}
#annotation-layer,
#leaders {
position: absolute;
inset: 0;
z-index: 3;
width: 100%;
height: 100%;
overflow: hidden;
pointer-events: none;
}
#leaders {
z-index: 2;
}
.tag {
position: absolute;
min-width: 116px;
padding: 8px 10px 8px 12px;
border: 1px solid rgba(55, 70, 59, 0.12);
border-left: 4px solid var(--accent);
border-radius: 8px;
background: var(--card);
box-shadow: 0 8px 24px rgba(48, 58, 49, 0.1);
backdrop-filter: blur(8px);
transform: translateY(-50%);
white-space: nowrap;
}
.tag strong {
display: block;
color: #2d4036;
font-size: 12px;
font-weight: 800;
line-height: 1.1;
}
.tag small {
display: block;
margin-top: 3px;
color: #7a837d;
font-size: 10px;
line-height: 1.2;
}
#flow-key {
position: absolute;
bottom: 26px;
left: 30px;
z-index: 4;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 490px;
pointer-events: none;
}
.flow-pill {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 12px;
border: 1px solid rgba(61, 78, 67, 0.12);
border-radius: 999px;
background: var(--card);
box-shadow: 0 8px 22px rgba(48, 58, 49, 0.08);
color: #43544b;
font-size: 11px;
font-weight: 700;
}
.flow-pill i {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--color);
}
#controls {
position: absolute;
right: 28px;
bottom: 26px;
z-index: 5;
display: flex;
align-items: center;
gap: 8px;
}
button {
padding: 9px 13px;
border: 1px solid rgba(61, 78, 67, 0.15);
border-radius: 999px;
background: rgba(255, 253, 247, 0.95);
color: #405149;
box-shadow: 0 7px 20px rgba(48, 58, 49, 0.08);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
button:hover {
background: #fffdf7;
transform: translateY(-1px);
}
.hint {
margin-right: 7px;
color: #778078;
font-size: 11px;
font-weight: 650;
pointer-events: none;
}
#loading {
position: absolute;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: var(--paper);
color: #657169;
font-size: 13px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 760px) {
header {
top: 18px;
left: 18px;
max-width: 62%;
}
.subtitle {
display: none;
}
#legend {
top: 18px;
right: 16px;
width: 142px;
padding: 10px;
}
.legend-row span:last-child {
display: none;
}
.tag {
min-width: 92px;
padding: 6px 8px;
}
.tag small {
display: none;
}
#flow-key {
left: 16px;
bottom: 16px;
max-width: calc(100% - 32px);
}
.flow-pill {
padding: 7px 9px;
font-size: 9px;
}
#controls {
right: 16px;
bottom: 65px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive 3D diagram of plant vascular transport">
<svg id="leaders" aria-hidden="true"></svg>
<div id="annotation-layer" aria-hidden="true"></div>
<header>
<p class="eyebrow">Botany / Interactive Study</p>
<h1>Vascular<br />Transport</h1>
<p class="subtitle">
Xylem carries water and minerals toward the leaves, while phloem
distributes sugars through the plant.
</p>
</header>
<aside id="legend">
<div class="legend-row" style="--color: var(--xylem)">
<i class="swatch"></i>
<span>Xylem</span>
<span>upward</span>
</div>
<div class="legend-row" style="--color: var(--phloem)">
<i class="swatch"></i>
<span>Phloem</span>
<span>sugar flow</span>
</div>
<div class="legend-row" style="--color: var(--cambium)">
<i class="swatch"></i>
<span>Cambium</span>
<span>growth</span>
</div>
</aside>
<aside id="flow-key">
<div class="flow-pill" style="--color: var(--xylem)">
<i></i> H₂O + minerals ↑
</div>
<div class="flow-pill" style="--color: var(--phloem)">
<i></i> Sugars ↕
</div>
</aside>
<div id="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="flow-toggle" type="button">Pause flow</button>
<button id="reset" type="button">Reset view</button>
</div>
<div id="loading">Loading vascular system…</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 sceneElement = document.getElementById("scene");
const annotationLayer = document.getElementById("annotation-layer");
const leaders = document.getElementById("leaders");
const loading = document.getElementById("loading");
const COLORS = {
paper: 0xf3efe5,
ink: 0x20332b,
bark: 0x65513e,
cortex: 0x789560,
phloem: 0xe36d4f,
phloemLight: 0xf39a76,
cambium: 0x9fbd4c,
xylem: 0x2788c5,
xylemDark: 0x176da7,
pith: 0xf1dfad,
stem: 0x4f7149,
leaf: 0x5b9560,
leafDark: 0x477c50,
leafVein: 0x9abd7b,
root: 0x896745,
soil: 0xc9b99c,
frame: 0xd3c4a8
};
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneElement.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.paper);
scene.fog = new THREE.Fog(COLORS.paper, 19, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
target: new THREE.Vector3(0.35, 0, 0),
radius: 12.5,
theta: 0,
phi: Math.PI / 2
};
const hemisphere = new THREE.HemisphereLight(0xfffdf5, 0x8a7c67, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff7e8, 3.2);
keyLight.position.set(-4, 8, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc9e3ff, 1.2);
fillLight.position.set(7, 2, 6);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd9ad, 1);
rimLight.position.set(-7, -2, -5);
scene.add(rimLight);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const mats = {
bark: material(COLORS.bark),
cortex: material(COLORS.cortex),
phloem: material(COLORS.phloem),
phloemLight: material(COLORS.phloemLight),
cambium: material(COLORS.cambium),
xylem: material(COLORS.xylem),
xylemDark: material(COLORS.xylemDark),
pith: material(COLORS.pith),
stem: material(COLORS.stem),
leaf: material(COLORS.leaf),
leafDark: material(COLORS.leafDark),
leafVein: material(COLORS.leafVein),
root: material(COLORS.root),
soil: material(COLORS.soil),
frame: material(COLORS.frame),
connector: material(0x978c78)
};
function addMesh(geometry, meshMaterial, position, parent = scene) {
const mesh = new THREE.Mesh(geometry, meshMaterial);
if (position) mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, meshMaterial, parent = scene, segments = 14) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
meshMaterial
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Soft ground shadow.
const ground = addMesh(
new THREE.PlaneGeometry(30, 20),
new THREE.ShadowMaterial({ opacity: 0.12 }),
new THREE.Vector3(0, -3.02, 0)
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
// ------------------------------------------------------------
// Magnified stem cross-section
// ------------------------------------------------------------
const crossSection = new THREE.Group();
crossSection.position.set(-2.45, -0.15, 0);
scene.add(crossSection);
const crossBase = addMesh(
new THREE.CylinderGeometry(1.78, 1.78, 0.32, 96),
mats.bark,
new THREE.Vector3(0, 0, 0),
crossSection
);
crossBase.rotation.x = Math.PI / 2;
addMesh(
new THREE.CircleGeometry(1.68, 96),
mats.cortex,
new THREE.Vector3(0, 0, 0.175),
crossSection
);
addMesh(
new THREE.RingGeometry(1.36, 1.62, 96),
mats.phloem,
new THREE.Vector3(0, 0, 0.195),
crossSection
);
addMesh(
new THREE.RingGeometry(1.27, 1.36, 96),
mats.cambium,
new THREE.Vector3(0, 0, 0.213),
crossSection
);
addMesh(
new THREE.RingGeometry(0.62, 1.27, 96),
mats.xylem,
new THREE.Vector3(0, 0, 0.222),
crossSection
);
addMesh(
new THREE.CircleGeometry(0.62, 72),
mats.pith,
new THREE.Vector3(0, 0, 0.232),
crossSection
);
const crossRim = addMesh(
new THREE.TorusGeometry(1.79, 0.045, 12, 96),
mats.frame,
new THREE.Vector3(0, 0, 0.24),
crossSection
);
crossRim.castShadow = false;
// Stylized sieve cells in the phloem ring.
for (let i = 0; i < 14; i++) {
const angle = (i / 14) * Math.PI * 2;
const radius = 1.49;
addMesh(
new THREE.CircleGeometry(0.047, 16),
mats.phloemLight,
new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0.247
),
crossSection
);
}
// Xylem vessel openings.
for (let i = 0; i < 10; i++) {
const angle = (i / 10) * Math.PI * 2 + 0.18;
const radius = 0.94;
addMesh(
new THREE.CircleGeometry(0.085, 20),
mats.xylemDark,
new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0.252
),
crossSection
);
}
// ------------------------------------------------------------
// Whole plant reference
// ------------------------------------------------------------
const plant = new THREE.Group();
plant.position.x = 3.2;
scene.add(plant);
const soil = addMesh(
new THREE.CircleGeometry(0.9, 48),
mats.soil,
new THREE.Vector3(0, -2.93, 0.02),
plant
);
soil.rotation.x = -Math.PI / 2;
cylinderBetween(
new THREE.Vector3(0, -2.22, 0),
new THREE.Vector3(0, 2.06, 0),
0.16,
mats.stem,
plant,
16
);
// Roots.
cylinderBetween(
new THREE.Vector3(0, -2.16, 0),
new THREE.Vector3(-0.04, -2.76, 0.02),
0.11,
mats.root,
plant,
12
);
const roots = [
[new THREE.Vector3(-0.02, -2.42, 0), new THREE.Vector3(-0.62, -2.76, 0.02)],
[new THREE.Vector3(-0.01, -2.50, 0), new THREE.Vector3(-0.91, -2.88, -0.01)],
[new THREE.Vector3(-0.02, -2.48, 0), new THREE.Vector3(0.58, -2.78, 0.03)],
[new THREE.Vector3(-0.03, -2.58, 0), new THREE.Vector3(0.84, -2.91, 0)],
[new THREE.Vector3(-0.02, -2.58, 0), new THREE.Vector3(0.14, -2.94, 0.02)]
];
roots.forEach(([a, b]) => {
cylinderBetween(a, b, 0.047, mats.root, plant, 10);
});
// Xylem continues into the main root to indicate water uptake.
cylinderBetween(
new THREE.Vector3(-0.045, -2.14, 0.13),
new THREE.Vector3(-0.07, -2.69, 0.13),
0.024,
mats.xylem,
plant,
10
);
const leafShape = new THREE.Shape();
leafShape.moveTo(0, 0);
leafShape.bezierCurveTo(0.55, 0.34, 0.46, 0.98, 0, 1.34);
leafShape.bezierCurveTo(-0.46, 0.98, -0.55, 0.34, 0, 0);
const leafGeometry = new THREE.ExtrudeGeometry(leafShape, {
depth: 0.08,
bevelEnabled: true,
bevelThickness: 0.025,
bevelSize: 0.025,
bevelSegments: 1,
steps: 1,
curveSegments: 14
});
leafGeometry.translate(0, 0, -0.04);
function addLeaf(position, angle, scale = 1, dark = false) {
const group = new THREE.Group();
group.position.copy(position);
group.rotation.z = angle;
group.scale.setScalar(scale);
plant.add(group);
addMesh(
leafGeometry,
dark ? mats.leafDark : mats.leaf,
new THREE.Vector3(0, 0, 0),
group
);
cylinderBetween(
new THREE.Vector3(0, 0.04, 0.085),
new THREE.Vector3(0, 1.14, 0.085),
0.013,
mats.leafVein,
group,
8
);
return group;
}
const leafDefinitions = [
{
branch: new THREE.Vector3(0, 1.28, 0),
base: new THREE.Vector3(0.1, 1.29, 0),
angle: -0.78,
scale: 0.91
},
{
branch: new THREE.Vector3(0, 1.03, 0),
base: new THREE.Vector3(-0.1, 1.04, 0),
angle: 0.86,
scale: 0.88
},
{
branch: new THREE.Vector3(0, 0.31, 0),
base: new THREE.Vector3(0.13, 0.32, 0),
angle: -1.13,
scale: 0.94
},
{
branch: new THREE.Vector3(0, 0.22, 0),
base: new THREE.Vector3(-0.12, 0.23, 0),
angle: 1.1,
scale: 0.91
},
{
branch: new THREE.Vector3(0, -0.58, 0),
base: new THREE.Vector3(0.13, -0.58, 0),
angle: -1.23,
scale: 0.82
},
{
branch: new THREE.Vector3(0, -0.7, 0),
base: new THREE.Vector3(-0.12, -0.69, 0),
angle: 1.19,
scale: 0.81
}
];
leafDefinitions.forEach((definition, index) => {
cylinderBetween(
definition.branch,
definition.base,
0.075,
mats.stem,
plant,
12
);
addLeaf(
definition.base,
definition.angle,
definition.scale,
index % 2 === 1
);
});
addLeaf(new THREE.Vector3(0, 2.03, 0), 0, 0.98, false);
addMesh(
new THREE.IcosahedronGeometry(0.13, 1),
mats.leafDark,
new THREE.Vector3(0, 2.06, 0.01),
plant
);
// Visible vascular pathways on the whole plant.
cylinderBetween(
new THREE.Vector3(-0.045, -1.84, 0.19),
new THREE.Vector3(-0.045, 1.91, 0.19),
0.034,
mats.xylem,
plant,
10
);
cylinderBetween(
new THREE.Vector3(0.058, 1.92, 0.205),
new THREE.Vector3(0.058, -1.72, 0.205),
0.029,
mats.phloem,
plant,
10
);
const xylemArrow = addMesh(
new THREE.ConeGeometry(0.105, 0.25, 16),
mats.xylem,
new THREE.Vector3(-0.045, 2.02, 0.19),
plant
);
xylemArrow.castShadow = false;
const phloemArrow = addMesh(
new THREE.ConeGeometry(0.095, 0.24, 16),
mats.phloem,
new THREE.Vector3(0.058, -1.84, 0.205),
plant
);
phloemArrow.rotation.z = Math.PI;
phloemArrow.castShadow = false;
// Moving transport particles.
const blueBeads = [];
const orangeBeads = [];
const blueBeadMaterial = material(COLORS.xylem, {
roughness: 0.32,
emissive: COLORS.xylem,
emissiveIntensity: 0.2
});
const orangeBeadMaterial = material(COLORS.phloem, {
roughness: 0.35,
emissive: COLORS.phloem,
emissiveIntensity: 0.16
});
for (let i = 0; i < 6; i++) {
const bead = addMesh(
new THREE.SphereGeometry(0.058, 14, 10),
blueBeadMaterial,
new THREE.Vector3(-0.045, 0, 0.235),
plant
);
bead.castShadow = false;
blueBeads.push(bead);
}
for (let i = 0; i < 5; i++) {
const bead = addMesh(
new THREE.SphereGeometry(0.052, 14, 10),
orangeBeadMaterial,
new THREE.Vector3(0.058, 0, 0.245),
plant
);
bead.castShadow = false;
orangeBeads.push(bead);
}
// Magnification collar and dashed guide.
const collar = addMesh(
new THREE.TorusGeometry(0.235, 0.028, 10, 40),
mats.frame,
new THREE.Vector3(0, 0.06, 0.03),
plant
);
collar.castShadow = false;
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.67, 0.06, 0.22),
new THREE.Vector3(2.95, 0.06, 0.22)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0x9d927f,
dashSize: 0.13,
gapSize: 0.11,
transparent: true,
opacity: 0.68
})
);
guide.computeLineDistances();
scene.add(guide);
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const annotations = [];
function addAnnotation({
anchor,
offset,
title,
subtitle,
color,
line = true
}) {
const label = document.createElement("div");
label.className = "tag";
label.style.setProperty("--accent", color);
label.innerHTML = `
<strong>${title}</strong>
${subtitle ? `<small>${subtitle}</small>` : ""}
`;
annotationLayer.appendChild(label);
const leaderLine = document.createElementNS(
"http://www.w3.org/2000/svg",
"line"
);
leaderLine.setAttribute("stroke", color);
leaderLine.setAttribute("stroke-width", "1.5");
leaderLine.setAttribute("stroke-opacity", "0.65");
leaderLine.setAttribute("stroke-linecap", "round");
const leaderDot = document.createElementNS(
"http://www.w3.org/2000/svg",
"circle"
);
leaderDot.setAttribute("r", "3.5");
leaderDot.setAttribute("fill", color);
leaderDot.setAttribute("stroke", "#fffdf7");
leaderDot.setAttribute("stroke-width", "1.5");
leaders.appendChild(leaderLine);
leaders.appendChild(leaderDot);
const annotation = {
anchor: anchor.clone(),
offset: new THREE.Vector2(offset[0], offset[1]),
label,
leaderLine,
leaderDot,
line
};
annotations.push(annotation);
return annotation;
}
addAnnotation({
anchor: new THREE.Vector3(-2.45, 1.66, 0.25),
offset: [0, -58],
title: "Stem cross-section",
subtitle: "8× magnification",
color: "#8b7658"
});
addAnnotation({
anchor: new THREE.Vector3(-3.97, 0.67, 0.25),
offset: [-160, -48],
title: "Outer cortex",
subtitle: "protective tissue",
color: "#789560"
});
addAnnotation({
anchor: new THREE.Vector3(-3.28, 0.9, 0.25),
offset: [-125, -74],
title: "Phloem",
subtitle: "sugar-conducting tissue",
color: "#e36d4f"
});
addAnnotation({
anchor: new THREE.Vector3(-2.75, -1.49, 0.25),
offset: [-122, 52],
title: "Cambium",
subtitle: "new growth layer",
color: "#9fbd4c"
});
addAnnotation({
anchor: new THREE.Vector3(-1.63, -0.47, 0.25),
offset: [82, 28],
title: "Xylem",
subtitle: "water + minerals",
color: "#2788c5"
});
addAnnotation({
anchor: new THREE.Vector3(-2.45, -0.15, 0.25),
offset: [-45, 142],
title: "Pith",
subtitle: "soft central tissue",
color: "#b69a54"
});
addAnnotation({
anchor: new THREE.Vector3(3.2, 3.38, 0.02),
offset: [0, -42],
title: "Whole plant",
subtitle: "organ reference",
color: "#4f7149"
});
addAnnotation({
anchor: new THREE.Vector3(3.155, 0.62, 0.24),
offset: [-142, -36],
title: "Upward water flow",
subtitle: "xylem pathway",
color: "#2788c5"
});
addAnnotation({
anchor: new THREE.Vector3(3.258, -0.7, 0.25),
offset: [100, 38],
title: "Downward sugar flow",
subtitle: "phloem pathway",
color: "#e36d4f"
});
function updateAnnotations() {
const width = sceneElement.clientWidth;
const height = sceneElement.clientHeight;
const compact = width < 760;
const offsetScale = compact ? 0.58 : 1;
annotations.forEach((annotation) => {
const projected = annotation.anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
annotation.label.style.display = visible ? "block" : "none";
annotation.leaderLine.style.display = visible ? "block" : "none";
annotation.leaderDot.style.display = visible ? "block" : "none";
if (!visible) return;
const anchorX = (projected.x * 0.5 + 0.5) * width;
const anchorY = (-projected.y * 0.5 + 0.5) * height;
const labelWidth = annotation.label.offsetWidth;
const labelHeight = annotation.label.offsetHeight;
let labelX = anchorX + annotation.offset.x * offsetScale;
let labelY = anchorY + annotation.offset.y * offsetScale;
labelX = THREE.MathUtils.clamp(
labelX,
12,
Math.max(12, width - labelWidth - 12)
);
labelY = THREE.MathUtils.clamp(
labelY,
95,
Math.max(95, height - labelHeight - 82)
);
annotation.label.style.left = `${labelX}px`;
annotation.label.style.top = `${labelY}px`;
const endX =
labelX > anchorX ? labelX : labelX + labelWidth;
const endY = labelY + labelHeight * 0.5;
annotation.leaderLine.setAttribute("x1", anchorX);
annotation.leaderLine.setAttribute("y1", anchorY);
annotation.leaderLine.setAttribute("x2", endX);
annotation.leaderLine.setAttribute("y2", endY);
annotation.leaderDot.setAttribute("cx", anchorX);
annotation.leaderDot.setAttribute("cy", anchorY);
});
}
// ------------------------------------------------------------
// Custom orbit and zoom controls
// ------------------------------------------------------------
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
const cosPhi = Math.cos(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * cosPhi,
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
function fittedRadius() {
const verticalHalfFov = THREE.MathUtils.degToRad(camera.fov * 0.5);
const aspect = window.innerWidth / window.innerHeight;
return Math.max(
11.5,
10.2 / (2 * Math.tan(verticalHalfFov) * aspect)
);
}
function resetView() {
controls.target.set(0.35, 0, 0);
controls.theta = 0;
controls.phi = Math.PI / 2;
controls.radius = fittedRadius();
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
controls.theta -= dx * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.48,
2.48
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
6,
34
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetView);
document.getElementById("reset").addEventListener("click", resetView);
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
let flowRunning = !reducedMotion;
let flowTime = 0;
const flowButton = document.getElementById("flow-toggle");
function updateFlowButton() {
flowButton.textContent = flowRunning ? "Pause flow" : "Play flow";
flowButton.setAttribute("aria-pressed", String(!flowRunning));
}
flowButton.addEventListener("click", () => {
flowRunning = !flowRunning;
updateFlowButton();
});
updateFlowButton();
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") {
resetView();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (flowRunning) flowTime += delta;
blueBeads.forEach((bead, index) => {
const t = (flowTime * 0.17 + index / blueBeads.length) % 1;
bead.position.y = -1.88 + t * 3.88;
bead.position.x = -0.045 + Math.sin(t * Math.PI * 5) * 0.008;
});
orangeBeads.forEach((bead, index) => {
const t = (flowTime * 0.13 + index / orangeBeads.length) % 1;
bead.position.y = 1.92 - t * 3.65;
bead.position.x = 0.058 + Math.sin(t * Math.PI * 4) * 0.007;
});
renderer.render(scene, camera);
updateAnnotations();
if (firstFrame) {
loading.remove();
firstFrame = false;
}
}
resetView();
animate();
</script>
</body>
</html>
```