Prompt specification
Build or customize the Three.js 3D scene "Legume root nodule 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: Legume root nodule section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/legume-root-nodule-section-x0741
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a legume root with several rounded nodules and one enlarged cutaway; distinguish nodules from ordinary root branches and label the associated microbial partnership schematically. Stylized solid forms.
Working Three.js Starter (legume-root-nodule-section-x0741):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Legume Root–Rhizobium Partnership</title>
<style>
:root {
--ink: #382a24;
--muted: #79675d;
--line: #ad8672;
--paper: rgba(255, 253, 248, 0.93);
--accent: #c96550;
--bacteria: #713654;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f2eee7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#leaders {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
z-index: 2;
pointer-events: none;
}
.leader {
stroke: #9b705c;
stroke-width: 1.25;
stroke-linecap: round;
opacity: 0.82;
}
.anchor-dot {
fill: #f8f4ec;
stroke: #8c5c48;
stroke-width: 1.4;
}
#title-card {
position: fixed;
top: 26px;
left: 30px;
z-index: 5;
width: min(390px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #8f5d48;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
content: "";
background: #c96550;
}
h1 {
max-width: 360px;
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(29px, 3.1vw, 46px);
font-weight: 500;
line-height: 0.99;
letter-spacing: -0.035em;
}
#title-card p {
max-width: 340px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#interaction-note {
position: fixed;
top: 27px;
right: 29px;
z-index: 5;
padding: 9px 13px;
border: 1px solid rgba(130, 95, 75, 0.22);
border-radius: 999px;
background: rgba(255, 253, 248, 0.7);
color: #806b60;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
pointer-events: none;
backdrop-filter: blur(8px);
}
#legend {
position: fixed;
left: 29px;
bottom: 25px;
z-index: 5;
display: flex;
gap: 18px;
align-items: center;
padding: 12px 15px;
border: 1px solid rgba(130, 95, 75, 0.18);
border-radius: 13px;
background: rgba(255, 253, 248, 0.84);
box-shadow: 0 10px 30px rgba(71, 45, 31, 0.06);
pointer-events: none;
backdrop-filter: blur(9px);
}
.legend-item {
display: flex;
gap: 8px;
align-items: center;
color: #715e54;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.025em;
white-space: nowrap;
}
.swatch-root {
position: relative;
width: 26px;
height: 5px;
border-radius: 99px;
background: #8c583a;
transform: rotate(-12deg);
}
.swatch-root::after {
position: absolute;
right: -1px;
top: -1px;
width: 10px;
height: 7px;
border-radius: 99px;
content: "";
background: #8c583a;
transform: rotate(35deg);
transform-origin: left center;
}
.swatch-nodule {
width: 15px;
height: 15px;
border-radius: 48% 52% 46% 54%;
background: #c96550;
box-shadow: inset -2px -2px 4px rgba(111, 43, 32, 0.12);
}
.swatch-bacteria {
width: 15px;
height: 15px;
border-radius: 50%;
background:
radial-gradient(circle at 35% 35%, #a25a82 0 19%, transparent 21%),
radial-gradient(circle at 68% 62%, #713654 0 28%, transparent 30%),
radial-gradient(circle at 35% 72%, #713654 0 24%, transparent 26%);
}
#partnership {
position: fixed;
right: 27px;
bottom: 25px;
z-index: 5;
width: min(340px, calc(100vw - 54px));
padding: 17px 18px 15px;
border: 1px solid rgba(130, 95, 75, 0.2);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 14px 38px rgba(71, 45, 31, 0.09);
pointer-events: none;
backdrop-filter: blur(10px);
}
#partnership .label {
margin-bottom: 11px;
color: #9b6049;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.exchange {
display: grid;
grid-template-columns: 1fr 28px 1fr;
gap: 7px;
align-items: center;
}
.exchange > div {
padding: 10px 11px;
border-radius: 11px;
background: #f4ebe4;
}
.exchange > div:last-child {
background: #eee4e7;
}
.exchange b,
.exchange span {
display: block;
}
.exchange b {
margin-bottom: 3px;
font-family: Georgia, "Times New Roman", serif;
font-size: 16px;
font-weight: 600;
}
.exchange span {
color: #7d6a60;
font-size: 9px;
line-height: 1.35;
}
.exchange .arrow {
color: #a2644e;
font-size: 23px;
text-align: center;
}
.process-line {
display: flex;
flex-wrap: wrap;
gap: 6px;
align-items: center;
margin-top: 12px;
color: #5f4b42;
font-size: 10px;
font-weight: 750;
}
.process-line em {
color: #a2644e;
font-size: 9px;
font-style: normal;
}
#partnership p {
margin: 10px 0 0;
color: #79675d;
font-size: 10px;
line-height: 1.45;
}
.callout {
position: fixed;
z-index: 4;
width: 168px;
padding: 10px 12px 11px;
border: 1px solid rgba(139, 94, 73, 0.24);
border-left: 3px solid #b66b52;
border-radius: 10px;
background: rgba(255, 253, 248, 0.94);
box-shadow: 0 8px 24px rgba(74, 47, 34, 0.08);
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.callout.bacteria {
border-left-color: #713654;
}
.callout strong,
.callout small {
display: block;
}
.callout strong {
margin-bottom: 3px;
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
font-weight: 600;
line-height: 1.12;
}
.callout small {
color: #806c61;
font-size: 9px;
line-height: 1.35;
}
#cutaway-tag {
position: fixed;
z-index: 4;
padding: 6px 9px;
border-radius: 999px;
background: #713654;
color: #fff8f4;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
pointer-events: none;
transform: translate(-50%, -50%);
box-shadow: 0 5px 16px rgba(73, 31, 55, 0.2);
}
@media (max-width: 760px) {
#title-card {
top: 18px;
left: 18px;
}
#title-card p {
max-width: 260px;
font-size: 11px;
}
#interaction-note {
display: none;
}
#legend {
left: 16px;
bottom: 16px;
gap: 10px;
padding: 9px 11px;
}
.legend-item {
font-size: 8px;
}
#partnership {
display: none;
}
.callout {
width: 132px;
padding: 8px 9px;
}
.callout strong {
font-size: 12px;
}
.callout small {
display: none;
}
}
</style>
</head>
<body>
<div id="title-card">
<div class="eyebrow">Botanical study · Symbiosis</div>
<h1>Legume root & rhizobia</h1>
<p>
Rounded root nodules house nitrogen-fixing bacteria, unlike the tapering
branches that simply continue the root system.
</p>
</div>
<div id="interaction-note">DRAG TO ROTATE · SCROLL TO ZOOM · DOUBLE-CLICK TO RESET</div>
<svg id="leaders" aria-hidden="true"></svg>
<div id="cutaway-tag">Enlarged cutaway · not to scale</div>
<div id="legend" aria-label="Diagram legend">
<div class="legend-item">
<span class="swatch-root"></span>
Root / branch
</div>
<div class="legend-item">
<span class="swatch-nodule"></span>
Root nodule
</div>
<div class="legend-item">
<span class="swatch-bacteria"></span>
Bacteroids
</div>
</div>
<div id="partnership">
<div class="label">The microbial partnership</div>
<div class="exchange">
<div>
<b>Rhizobium</b>
<span>N₂-fixing soil bacteria</span>
</div>
<div class="arrow">↔</div>
<div>
<b>Legume</b>
<span>Root cells provide shelter and carbon</span>
</div>
</div>
<div class="process-line">
<span>N₂</span>
<em>— Rhizobium →</em>
<span>NH₃ / NH₄⁺</span>
<em>→</em>
<span>plant nitrogen</span>
</div>
<p>
The bacteria convert atmospheric nitrogen into plant-available forms,
while the plant supplies carbohydrates.
</p>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf2eee7);
scene.fog = new THREE.Fog(0xf2eee7, 15, 28);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
60
);
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.08;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional model of a legume root with root nodules and a cutaway showing rhizobial bacteria."
);
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xfffbf4,
0xb6a38e,
2.2
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2dc, 4.2);
keyLight.position.set(5.5, 8.5, 9.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xe6c8d5, 1.5);
rimLight.position.set(-7, 2, -4);
scene.add(rimLight);
const fillLight = new THREE.PointLight(0xffdfc4, 0.65, 18);
fillLight.position.set(-4, -3, 5);
scene.add(fillLight);
// A matte backdrop receives the specimen's soft shadow.
const backdrop = new THREE.Mesh(
new THREE.PlaneGeometry(32, 24),
new THREE.MeshStandardMaterial({
color: 0xf3efe8,
roughness: 1,
metalness: 0
})
);
backdrop.position.set(0, 0, -2.45);
backdrop.receiveShadow = true;
scene.add(backdrop);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0x8a5739,
roughness: 0.88,
metalness: 0
});
const branchMaterial = new THREE.MeshStandardMaterial({
color: 0x946043,
roughness: 0.9,
metalness: 0
});
const noduleMaterial = new THREE.MeshStandardMaterial({
color: 0xc96550,
roughness: 0.72,
metalness: 0
});
const collarMaterial = new THREE.MeshStandardMaterial({
color: 0x9a523d,
roughness: 0.85,
metalness: 0
});
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0xb96648,
roughness: 0.76,
metalness: 0,
side: THREE.DoubleSide
});
const innerTissueMaterial = new THREE.MeshStandardMaterial({
color: 0xdc8b88,
roughness: 0.78,
metalness: 0,
side: THREE.DoubleSide
});
const cutRimMaterial = new THREE.MeshStandardMaterial({
color: 0xefad82,
roughness: 0.72,
metalness: 0
});
const bacteriaMaterial = new THREE.MeshStandardMaterial({
color: 0x713654,
roughness: 0.58,
metalness: 0
});
const threadMaterial = new THREE.MeshStandardMaterial({
color: 0xf2d19d,
roughness: 0.68,
metalness: 0
});
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
/**
* Builds a smooth tube with a continuously changing radius.
* Ordinary roots are made with this helper so that branches taper to a
* fine tip instead of looking like identical-width pipes.
*/
function createTaperedTube(
pointArray,
radiusArray,
material,
tubularSegments = 72,
radialSegments = 12
) {
const points = pointArray.map((p) => new THREE.Vector3(...p));
const curve = new THREE.CatmullRomCurve3(points, false, "catmullrom", 0.45);
const frames = curve.computeFrenetFrames(tubularSegments, false);
const positions = [];
const normals = [];
const uvs = [];
const indices = [];
for (let i = 0; i <= tubularSegments; i++) {
const t = i / tubularSegments;
const center = curve.getPointAt(t);
const normal = frames.normals[i];
const binormal = frames.binormals[i];
const radiusIndex = t * (radiusArray.length - 1);
const lower = Math.floor(radiusIndex);
const upper = Math.min(lower + 1, radiusArray.length - 1);
const alpha = radiusIndex - lower;
const radius = THREE.MathUtils.lerp(
radiusArray[lower],
radiusArray[upper],
alpha
);
for (let j = 0; j <= radialSegments; j++) {
const v = (j / radialSegments) * Math.PI * 2;
const sin = Math.sin(v);
const cos = -Math.cos(v);
const nx = cos * normal.x + sin * binormal.x;
const ny = cos * normal.y + sin * binormal.y;
const nz = cos * normal.z + sin * binormal.z;
positions.push(
center.x + radius * nx,
center.y + radius * ny,
center.z + radius * nz
);
normals.push(nx, ny, nz);
uvs.push(t, j / radialSegments);
}
}
for (let i = 1; i <= tubularSegments; i++) {
for (let j = 1; j <= radialSegments; j++) {
const a = radialSegments + j;
const b = radialSegments + j - 1;
const c = (tubularSegments - 1) * radialSegments + j - 1;
const d = (tubularSegments - 1) * radialSegments + j;
indices.push(a, b, d);
indices.push(b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"normal",
new THREE.Float32BufferAttribute(normals, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function addRootBranch(pointArray) {
const branch = createTaperedTube(
pointArray,
[0.115, 0.095, 0.066, 0.038, 0.016],
branchMaterial,
54,
10
);
scene.add(branch);
return branch;
}
// -------------------------------------------------------------------------
// Main root and ordinary root branches
// -------------------------------------------------------------------------
const mainRoot = createTaperedTube(
[
[-0.18, 3.60, 0.02],
[-0.26, 2.86, -0.03],
[0.04, 2.04, 0.02],
[-0.12, 1.24, 0.00],
[0.12, 0.38, 0.05],
[-0.10, -0.52, 0.02],
[0.18, -1.45, 0.00],
[-0.02, -2.36, 0.08],
[0.28, -3.38, 0.00]
],
[0.31, 0.285, 0.255, 0.225, 0.195, 0.17, 0.14, 0.095, 0.035],
rootMaterial,
120,
16
);
scene.add(mainRoot);
addRootBranch([
[-0.26, 2.78, -0.02],
[-0.82, 2.57, -0.03],
[-1.55, 2.53, -0.05],
[-2.30, 2.13, -0.02]
]);
addRootBranch([
[0.04, 2.03, 0.01],
[0.62, 2.33, -0.01],
[1.20, 2.19, -0.04],
[1.78, 1.73, 0.00]
]);
addRootBranch([
[-0.12, 1.24, 0.00],
[-0.63, 0.92, -0.03],
[-1.33, 0.77, -0.04],
[-2.07, 0.34, 0.00]
]);
addRootBranch([
[0.12, 0.38, 0.03],
[0.65, 0.66, 0.00],
[1.17, 0.46, -0.03],
[1.71, 0.78, 0.01]
]);
addRootBranch([
[-0.10, -0.52, 0.02],
[-0.55, -0.74, 0.00],
[-1.16, -0.70, -0.03],
[-1.78, -1.12, 0.00]
]);
addRootBranch([
[0.18, -1.45, 0.00],
[0.62, -1.75, -0.01],
[1.13, -1.91, -0.02],
[1.66, -2.26, 0.02]
]);
addRootBranch([
[-0.02, -2.36, 0.08],
[-0.40, -2.77, 0.02],
[-0.83, -3.20, 0.00]
]);
// -------------------------------------------------------------------------
// Ordinary rounded root nodules
// -------------------------------------------------------------------------
const noduleGeometry = new THREE.SphereGeometry(1, 32, 22);
function addSmallNodule(center, scale, attachment, stalkMiddle) {
const group = new THREE.Group();
const stalk = createTaperedTube(
[attachment, stalkMiddle, center],
[0.115, 0.095, 0.07],
rootMaterial,
28,
10
);
group.add(stalk);
const nodule = new THREE.Mesh(noduleGeometry, noduleMaterial);
nodule.position.set(...center);
nodule.scale.set(...scale);
nodule.castShadow = true;
nodule.receiveShadow = true;
group.add(nodule);
// A small swollen collar emphasizes that the nodule is attached,
// rather than a continuation of the tapering root.
const collar = new THREE.Mesh(
new THREE.SphereGeometry(1, 20, 14),
collarMaterial
);
collar.position.set(
center[0] * 0.75,
center[1] * 0.75,
center[2]
);
collar.scale.set(0.16, 0.18, 0.14);
collar.castShadow = true;
group.add(collar);
scene.add(group);
return group;
}
addSmallNodule(
[-1.03, 1.24, 0.13],
[0.47, 0.55, 0.40],
[-0.12, 1.24, 0.00],
[-0.57, 1.20, 0.06]
);
addSmallNodule(
[1.02, -0.61, 0.08],
[0.42, 0.50, 0.38],
[0.11, -0.54, 0.02],
[0.57, -0.59, 0.05]
);
addSmallNodule(
[-0.91, -1.56, 0.12],
[0.48, 0.54, 0.41],
[-0.10, -1.45, 0.02],
[-0.50, -1.52, 0.07]
);
addSmallNodule(
[0.98, -2.66, 0.10],
[0.44, 0.48, 0.39],
[0.02, -2.45, 0.07],
[0.50, -2.58, 0.08]
);
addSmallNodule(
[1.35, 1.91, 0.08],
[0.40, 0.48, 0.36],
[0.04, 2.03, 0.01],
[0.73, 2.10, 0.04]
);
// -------------------------------------------------------------------------
// Enlarged nodule cutaway
// -------------------------------------------------------------------------
const cutaway = new THREE.Group();
cutaway.position.set(2.43, 0.05, 0.32);
cutaway.scale.set(1, 1.14, 1);
scene.add(cutaway);
// The orange shell is a sphere with a broad opening facing the camera.
const outerPhiStart = 0.85 * Math.PI;
const outerPhiLength = 1.30 * Math.PI;
const outerShell = new THREE.Mesh(
new THREE.SphereGeometry(
1.02,
52,
36,
outerPhiStart,
outerPhiLength
),
shellMaterial
);
outerShell.castShadow = true;
outerShell.receiveShadow = true;
cutaway.add(outerShell);
// Pink plant tissue forms a second, smaller bowl within the shell.
const innerShell = new THREE.Mesh(
new THREE.SphereGeometry(
0.82,
48,
30,
1.03 * Math.PI,
1.05 * Math.PI
),
innerTissueMaterial
);
innerShell.castShadow = true;
innerShell.receiveShadow = true;
cutaway.add(innerShell);
function addShellEdge(radius, phi, material, thickness) {
const points = [];
for (let i = 0; i <= 40; i++) {
const theta = (i / 40) * Math.PI;
points.push(
new THREE.Vector3(
-radius * Math.cos(phi) * Math.sin(theta),
radius * Math.cos(theta),
radius * Math.sin(phi) * Math.sin(theta)
)
);
}
const curve = new THREE.CatmullRomCurve3(points);
const edge = new THREE.Mesh(
new THREE.TubeGeometry(curve, 60, thickness, 8, false),
material
);
edge.castShadow = true;
cutaway.add(edge);
}
// Rounded cut edges make the opening read as a solid cutaway.
addShellEdge(1.02, outerPhiStart, cutRimMaterial, 0.043);
addShellEdge(
1.02,
outerPhiStart + outerPhiLength,
cutRimMaterial,
0.043
);
// A pale infection strand travels inward from the root connection.
const infectionThread = createTaperedTube(
[
[-0.98, -0.11, 0.27],
[-0.70, -0.07, 0.39],
[-0.38, 0.00, 0.49],
[-0.06, 0.02, 0.59]
],
[0.065, 0.052, 0.043, 0.033],
threadMaterial,
36,
9
);
cutaway.add(infectionThread);
// Stylized bacteroids: small, elongated bacteria inside the nodule.
const bacteriumGeometry = new THREE.SphereGeometry(1, 18, 12);
const bacteroidLocations = [
[-0.29, 0.12, 0.61],
[-0.08, 0.31, 0.64],
[ 0.15, 0.23, 0.68],
[ 0.35, 0.05, 0.64],
[-0.37, -0.08, 0.59],
[-0.14, -0.18, 0.64],
[ 0.08, -0.21, 0.69],
[ 0.29, -0.15, 0.60],
[ 0.00, 0.03, 0.80],
[-0.45, 0.31, 0.47],
[ 0.43, 0.29, 0.46],
[-0.40, -0.25, 0.48],
[ 0.41, -0.24, 0.48]
];
bacteroidLocations.forEach((position, index) => {
const bacterium = new THREE.Mesh(
bacteriumGeometry,
bacteriaMaterial
);
bacterium.position.set(...position);
bacterium.scale.set(0.095, 0.19 + (index % 3) * 0.015, 0.095);
bacterium.rotation.set(
0.25 * Math.sin(index * 1.7),
0.8 * Math.cos(index * 1.2),
(index % 5 - 2) * 0.34
);
bacterium.castShadow = true;
cutaway.add(bacterium);
});
// The enlarged nodule is attached to the main root by a short, thicker
// pedicel rather than by a long root-like branch.
const cutawayStalk = createTaperedTube(
[
[0.10, 0.06, 0.02],
[0.66, 0.02, 0.10],
[1.20, 0.04, 0.22],
[1.51, 0.05, 0.30]
],
[0.18, 0.18, 0.20, 0.22],
rootMaterial,
48,
12
);
scene.add(cutawayStalk);
// -------------------------------------------------------------------------
// Screen-space annotations
// -------------------------------------------------------------------------
const svg = document.getElementById("leaders");
const svgNS = "http://www.w3.org/2000/svg";
const cutawayTag = document.getElementById("cutaway-tag");
const labelDefinitions = [
{
title: "Root branch",
detail: "A tapering continuation of the root",
anchor: new THREE.Vector3(-1.72, 2.32, -0.02),
offset: [-155, -68]
},
{
title: "Root nodule",
detail: "Rounded bacterial house attached to the root",
anchor: new THREE.Vector3(-1.03, 1.24, 0.17),
offset: [-167, 50]
},
{
title: "Short pedicel",
detail: "The narrow connection between root and nodule",
anchor: new THREE.Vector3(-0.57, 1.20, 0.08),
offset: [-124, 126]
},
{
title: "Enlarged cutaway",
detail: "The shell opens to reveal the partnership",
anchor: new THREE.Vector3(2.43, 0.05, 0.36),
offset: [82, -142]
},
{
title: "Nodule cortex",
detail: "Pink plant tissue surrounding the bacteria",
anchor: new THREE.Vector3(2.74, 0.54, 0.60),
offset: [143, -68],
className: "bacteria"
},
{
title: "Rhizobial bacteroids",
detail: "Nitrogen-fixing bacteria housed in root cells",
anchor: new THREE.Vector3(2.43, 0.08, 1.00),
offset: [147, 95],
className: "bacteria"
}
];
const annotations = labelDefinitions.map((definition) => {
const element = document.createElement("div");
element.className = `callout ${definition.className || ""}`.trim();
element.innerHTML = `
<strong>${definition.title}</strong>
<small>${definition.detail}</small>
`;
document.body.appendChild(element);
const line = document.createElementNS(svgNS, "line");
line.setAttribute("class", "leader");
svg.appendChild(line);
const dot = document.createElementNS(svgNS, "circle");
dot.setAttribute("class", "anchor-dot");
dot.setAttribute("r", "3.4");
svg.appendChild(dot);
return { ...definition, element, line, dot };
});
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotations.forEach((annotation) => {
projected.copy(annotation.anchor).project(camera);
const visible = projected.z > -1 && projected.z < 1;
annotation.element.style.display = visible ? "block" : "none";
annotation.line.style.display = visible ? "block" : "none";
annotation.dot.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 boxWidth = annotation.element.offsetWidth;
const boxHeight = annotation.element.offsetHeight;
let centerX = anchorX + annotation.offset[0];
let centerY = anchorY + annotation.offset[1];
const halfWidth = boxWidth * 0.5 + 12;
const halfHeight = boxHeight * 0.5 + 12;
centerX = THREE.MathUtils.clamp(
centerX,
halfWidth,
width - halfWidth
);
centerY = THREE.MathUtils.clamp(
centerY,
halfHeight,
height - halfHeight
);
const left = centerX - boxWidth * 0.5;
const top = centerY - boxHeight * 0.5;
const right = centerX + boxWidth * 0.5;
const bottom = centerY + boxHeight * 0.5;
annotation.element.style.left = `${centerX}px`;
annotation.element.style.top = `${centerY}px`;
let endX = THREE.MathUtils.clamp(anchorX, left, right);
let endY = THREE.MathUtils.clamp(anchorY, top, bottom);
if (
anchorX >= left && anchorX <= right &&
anchorY >= top && anchorY <= bottom
) {
endX = anchorX;
endY = anchorY;
}
annotation.line.setAttribute("x1", anchorX);
annotation.line.setAttribute("y1", anchorY);
annotation.line.setAttribute("x2", endX);
annotation.line.setAttribute("y2", endY);
annotation.dot.setAttribute("cx", anchorX);
annotation.dot.setAttribute("cy", anchorY);
});
const tagAnchor = new THREE.Vector3(
cutaway.position.x,
cutaway.position.y - 1.16,
cutaway.position.z
).project(camera);
cutawayTag.style.left =
`${(tagAnchor.x * 0.5 + 0.5) * width}px`;
cutawayTag.style.top =
`${(-tagAnchor.y * 0.5 + 0.5) * height}px`;
}
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel/pinch to zoom
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, -0.04, 0);
const desired = {
radius: 11.2,
theta: 0.78,
phi: 1.32
};
const current = {
radius: desired.radius,
theta: desired.theta,
phi: desired.phi
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function applyCamera() {
const sinPhi = Math.sin(current.phi);
camera.position.set(
cameraTarget.x +
current.radius * sinPhi * Math.sin(current.theta),
cameraTarget.y +
current.radius * Math.cos(current.phi),
cameraTarget.z +
current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
desired.theta -= deltaX * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - deltaY * 0.005,
0.56,
2.22
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("lostpointercapture", () => {
dragging = false;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
7.3,
15.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
desired.radius = 11.2;
desired.theta = 0.78;
desired.phi = 1.32;
});
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
svg.setAttribute("viewBox", `0 0 ${width} ${height}`);
if (width / height < 0.8) {
desired.radius = Math.max(desired.radius, 12.4);
}
}
window.addEventListener("resize", resize);
resize();
applyCamera();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
renderer.setAnimationLoop(() => {
const easing = 0.085;
current.radius += (desired.radius - current.radius) * easing;
current.theta += (desired.theta - current.theta) * easing;
current.phi += (desired.phi - current.phi) * easing;
applyCamera();
updateAnnotations();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```