Prompt specification
Build or customize the Three.js 3D scene "Root-to-leaf water route" 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: Root-to-leaf water route
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/root-to-leaf-water-route-x0798
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a voxel plant with a continuous root-to-leaf water route. Use a small soil cutaway, branching roots, one upright stem, and several broad leaves at different heights. Enlarge the internal transport path enough to see it, with cubic water markers traveling from the roots through the stem and into the leaves. Their path should remain connected at every branch, rather than jumping between isolated colored sections. Keep the tracers symbolic and label them as water movement; do not reuse those same arrows to imply that sugars follow an identical route. The surrounding plant can remain still.
Working Three.js Starter (root-to-leaf-water-route-x0798):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Plant — Water Movement</title>
<style>
:root {
--ink: #183334;
--muted: #557071;
--paper: rgba(255, 253, 244, 0.92);
--line: rgba(24, 51, 52, 0.14);
--water: #12b8c8;
--water-bright: #bdfcff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #eee8d8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--paper);
box-shadow: 0 12px 32px rgba(45, 52, 38, 0.10);
backdrop-filter: blur(10px);
}
.title-panel {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 17px 19px 16px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--water);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.legend-panel {
bottom: 22px;
left: 22px;
width: min(365px, calc(100vw - 44px));
padding: 15px 17px;
}
.legend-row {
display: flex;
align-items: center;
gap: 12px;
}
.legend-row + .legend-row {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.route-swatch {
position: relative;
flex: 0 0 auto;
width: 38px;
height: 8px;
border-radius: 999px;
background: var(--water);
box-shadow: 0 0 0 4px rgba(18, 184, 200, 0.12);
}
.cube-swatch {
position: relative;
flex: 0 0 auto;
width: 20px;
height: 20px;
margin: 0 9px;
border: 1px solid rgba(6, 130, 145, 0.40);
border-radius: 3px;
background: var(--water-bright);
box-shadow:
inset -4px -4px 0 rgba(18, 184, 200, 0.15),
0 0 15px rgba(18, 184, 200, 0.48);
transform: rotate(-8deg);
}
.legend-copy b {
display: block;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.10em;
text-transform: uppercase;
}
.legend-copy small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 12px;
line-height: 1.35;
}
.controls-panel {
top: 22px;
right: 22px;
padding: 11px 12px;
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 11px;
}
#reset-view {
border: 1px solid var(--line);
border-radius: 9px;
padding: 7px 9px;
background: white;
color: var(--ink);
font: inherit;
font-weight: 700;
cursor: pointer;
}
#reset-view:hover {
border-color: rgba(18, 184, 200, 0.55);
color: #087b89;
}
.soil-tag {
position: fixed;
right: 24px;
bottom: 25px;
z-index: 8;
color: rgba(66, 65, 47, 0.72);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.world-label {
position: fixed;
z-index: 6;
transform: translate(-50%, -50%);
pointer-events: none;
padding: 5px 8px;
border: 1px solid rgba(18, 184, 200, 0.35);
border-radius: 999px;
background: rgba(255, 253, 244, 0.90);
color: #176c75;
box-shadow: 0 5px 16px rgba(37, 65, 63, 0.10);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
.world-label::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin-right: 5px;
border-radius: 50%;
background: var(--water);
vertical-align: 1px;
}
@media (max-width: 700px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.legend-panel {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.controls-panel {
top: auto;
right: 12px;
bottom: 158px;
}
.controls-panel .hint {
display: none;
}
.soil-tag {
display: none;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section class="panel title-panel">
<div class="eyebrow">Connected botanical schematic</div>
<h1>Voxel plant</h1>
<p class="subtitle">
An enlarged x-ray route connects the root network, upright stem,
and leaves. Cubic tracers represent water moving through the plant.
</p>
</section>
<section class="panel legend-panel" aria-label="Water movement legend">
<div class="legend-row">
<span class="route-swatch" aria-hidden="true"></span>
<div class="legend-copy">
<b>Enlarged connected route</b>
<small>Schematic pathway shown clearly through the plant.</small>
</div>
</div>
<div class="legend-row">
<span class="cube-swatch" aria-hidden="true"></span>
<div class="legend-copy">
<b>Water movement</b>
<small>
Cubic tracers travel continuously from the roots, through the
stem, and into the leaves. The tracers represent water only.
</small>
</div>
</div>
</section>
<section class="panel controls-panel">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="reset-view" type="button">Reset view</button>
</section>
<div class="soil-tag">Small soil cutaway</div>
<div id="root-label" class="world-label">Root</div>
<div id="route-label" class="world-label">Water route</div>
<div id="leaf-label" class="world-label">Leaf</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee8d8);
scene.fog = new THREE.Fog(0xeee8d8, 15, 29);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel plant with a connected root-to-leaf water route"
);
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(
0xfff8df,
0x53644c,
2.0
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d2, 3.2);
sun.position.set(5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -6;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbceeff, 1.25);
fill.position.set(-5, 5, 4);
scene.add(fill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const unitCube = new THREE.BoxGeometry(1, 1, 1);
const materials = {
soil: new THREE.MeshStandardMaterial({
color: 0x70452c,
roughness: 1
}),
soilTop: new THREE.MeshStandardMaterial({
color: 0x8a5b35,
roughness: 1
}),
soilDark: new THREE.MeshStandardMaterial({
color: 0x4d3326,
roughness: 1
}),
soilLight: new THREE.MeshStandardMaterial({
color: 0xa16b3d,
roughness: 1
}),
root: new THREE.MeshStandardMaterial({
color: 0x9a663c,
roughness: 0.95
}),
rootLight: new THREE.MeshStandardMaterial({
color: 0xb47a45,
roughness: 0.95
}),
stem: new THREE.MeshStandardMaterial({
color: 0x28703b,
roughness: 0.82
}),
stemLight: new THREE.MeshStandardMaterial({
color: 0x3f8847,
roughness: 0.82
}),
route: new THREE.MeshStandardMaterial({
color: 0x0daec0,
emissive: 0x073f48,
emissiveIntensity: 0.65,
roughness: 0.35,
metalness: 0.05,
transparent: true,
opacity: 0.94
}),
junction: new THREE.MeshStandardMaterial({
color: 0x0eb4c5,
emissive: 0x07515d,
emissiveIntensity: 0.7,
roughness: 0.35
}),
marker: new THREE.MeshStandardMaterial({
color: 0xc8fdff,
emissive: 0x20dce9,
emissiveIntensity: 1.25,
roughness: 0.18,
metalness: 0.02
}),
floor: new THREE.MeshStandardMaterial({
color: 0xe8e0cc,
roughness: 1
})
};
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0x2f7f42,
roughness: 0.88
}),
new THREE.MeshStandardMaterial({
color: 0x429a4d,
roughness: 0.88
}),
new THREE.MeshStandardMaterial({
color: 0x59aa57,
roughness: 0.88
}),
new THREE.MeshStandardMaterial({
color: 0x347343,
roughness: 0.88
})
];
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size.x, size.y, size.z);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.92;
floor.receiveShadow = true;
scene.add(floor);
// -------------------------------------------------------------------------
// Soil cutaway
// -------------------------------------------------------------------------
addBox(
scene,
new THREE.Vector3(5.7, 1.68, 2.28),
new THREE.Vector3(0, -0.84, -0.60),
materials.soil
);
addBox(
scene,
new THREE.Vector3(5.78, 0.16, 2.34),
new THREE.Vector3(0, -0.08, -0.60),
materials.soilTop
);
addBox(
scene,
new THREE.Vector3(5.98, 0.23, 3.05),
new THREE.Vector3(0, -1.79, -0.28),
materials.soilDark
);
// Thin strata on the exposed vertical face.
for (const [y, material] of [
[-0.39, materials.soilLight],
[-0.73, materials.soilDark],
[-1.15, materials.soilLight]
]) {
addBox(
scene,
new THREE.Vector3(5.64, 0.065, 0.035),
new THREE.Vector3(0, y, 0.557),
material,
false
);
}
// A few small voxel soil clumps on the top surface.
const soilClumpPositions = [
[-2.15, 0.08, -0.75],
[-1.78, 0.06, 0.05],
[-2.48, 0.10, -1.22],
[1.88, 0.07, -0.80],
[2.30, 0.09, -0.30],
[2.05, 0.06, 0.12],
[-0.85, 0.05, -1.16],
[0.95, 0.06, -1.18]
];
soilClumpPositions.forEach((p, index) => {
addBox(
scene,
new THREE.Vector3(
0.27 + (index % 3) * 0.035,
0.16,
0.25 + (index % 2) * 0.05
),
new THREE.Vector3(p[0], p[1], p[2]),
index % 2 ? materials.soilLight : materials.soilTop
);
});
// -------------------------------------------------------------------------
// Voxel helpers
// -------------------------------------------------------------------------
function hash01(value) {
const x = Math.sin(value * 127.1 + 311.7) * 43758.5453;
return x - Math.floor(x);
}
function addVoxelChain(
points,
material,
size = 0.22,
spacing = 0.18,
alternateMaterial = null
) {
const seen = new Set();
for (let segment = 0; segment < points.length - 1; segment++) {
const a = points[segment];
const b = points[segment + 1];
const length = a.distanceTo(b);
const count = Math.max(1, Math.ceil(length / spacing));
for (let i = 0; i <= count; i++) {
const t = i / count;
const p = new THREE.Vector3().lerpVectors(a, b, t);
p.x = Math.round(p.x / 0.10) * 0.10;
p.y = Math.round(p.y / 0.10) * 0.10;
p.z = Math.round(p.z / 0.10) * 0.10;
const key = `${p.x.toFixed(2)},${p.y.toFixed(2)},${p.z.toFixed(2)}`;
if (seen.has(key)) continue;
seen.add(key);
const chosenMaterial =
alternateMaterial && hash01(segment * 17 + i) > 0.58
? alternateMaterial
: material;
const scale = size * (0.90 + hash01(i + segment * 7) * 0.18);
addBox(
scene,
new THREE.Vector3(scale, scale, scale),
p,
chosenMaterial
);
}
}
}
function addVoxelLeaf(base, axis, length, width, seed) {
const direction = axis.clone().normalize();
const across = new THREE.Vector3(
-direction.y,
direction.x,
0
).normalize();
const seen = new Set();
const rows = 12;
for (let row = 0; row < rows; row++) {
const t = 0.055 + (row / (rows - 1)) * 0.94;
const profile = Math.pow(Math.sin(Math.PI * t), 0.58);
const halfWidth = Math.max(0.10, width * 0.5 * profile);
const acrossCount = Math.floor(halfWidth / 0.17);
for (let column = -acrossCount; column <= acrossCount; column++) {
const u = column * 0.17;
if (Math.abs(u) > halfWidth) continue;
const p = base.clone()
.add(direction.clone().multiplyScalar(length * t))
.add(across.clone().multiplyScalar(u));
p.x = Math.round(p.x / 0.16) * 0.16;
p.y = Math.round(p.y / 0.16) * 0.16;
const depthLayer = (row + column + seed) % 3;
p.z = 0.19 + depthLayer * 0.035;
const key = `${p.x.toFixed(2)},${p.y.toFixed(2)},${p.z.toFixed(2)}`;
if (seen.has(key)) continue;
seen.add(key);
const materialIndex = Math.min(
leafMaterials.length - 1,
Math.floor(hash01(seed * 31 + row * 7 + column * 3) * leafMaterials.length)
);
const cubeSize = 0.19 + hash01(seed + row + column) * 0.025;
addBox(
scene,
new THREE.Vector3(cubeSize, cubeSize * 0.88, cubeSize * 0.82),
p,
leafMaterials[materialIndex]
);
// Occasional lower voxels give the broad leaves a little thickness.
if (hash01(seed * 13 + row * 5 + column * 11) > 0.79) {
const lower = p.clone();
lower.z -= 0.09;
addBox(
scene,
new THREE.Vector3(cubeSize, cubeSize * 0.8, cubeSize * 0.75),
lower,
leafMaterials[(materialIndex + 1) % leafMaterials.length]
);
}
}
}
}
// -------------------------------------------------------------------------
// Plant body: one upright stem, branches, and four broad leaves
// -------------------------------------------------------------------------
const stemBottom = new THREE.Vector3(0, -0.04, 0.18);
const stemTop = new THREE.Vector3(0, 5.26, 0.18);
addVoxelChain(
[stemBottom, stemTop],
materials.stem,
0.34,
0.27,
materials.stemLight
);
const leafDefinitions = [
{
base: new THREE.Vector3(-0.20, 1.65, 0.20),
axis: new THREE.Vector3(-1, 0.35, 0),
length: 2.10,
width: 1.10,
seed: 11
},
{
base: new THREE.Vector3(0.20, 2.75, 0.20),
axis: new THREE.Vector3(1, 0.35, 0),
length: 2.10,
width: 1.16,
seed: 23
},
{
base: new THREE.Vector3(-0.20, 3.85, 0.20),
axis: new THREE.Vector3(-1, 0.34, 0),
length: 2.12,
width: 1.13,
seed: 37
},
{
base: new THREE.Vector3(0.20, 4.90, 0.20),
axis: new THREE.Vector3(1, 0.29, 0),
length: 2.08,
width: 1.08,
seed: 51
}
];
const greenBranchStarts = [
new THREE.Vector3(0, 1.05, 0.18),
new THREE.Vector3(0, 2.15, 0.18),
new THREE.Vector3(0, 3.25, 0.18),
new THREE.Vector3(0, 4.30, 0.18)
];
leafDefinitions.forEach((leaf, index) => {
addVoxelChain(
[greenBranchStarts[index], leaf.base],
materials.stem,
0.26,
0.20,
materials.stemLight
);
addVoxelLeaf(
leaf.base,
leaf.axis,
leaf.length,
leaf.width,
leaf.seed
);
});
// Small top bud.
addBox(
scene,
new THREE.Vector3(0.34, 0.38, 0.34),
new THREE.Vector3(0, 5.37, 0.18),
materials.stemLight
);
// -------------------------------------------------------------------------
// Branching voxel roots
// -------------------------------------------------------------------------
const rootChains = [
[
new THREE.Vector3(-2.25, -1.34, 0.58),
new THREE.Vector3(-1.35, -1.08, 0.58),
new THREE.Vector3(0, -0.74, 0.58)
],
[
new THREE.Vector3(2.25, -1.48, 0.58),
new THREE.Vector3(1.25, -1.13, 0.58),
new THREE.Vector3(0, -1.02, 0.58)
],
[
new THREE.Vector3(0, -1.56, 0.58),
new THREE.Vector3(0, -1.02, 0.58)
]
];
rootChains.forEach((chain, index) => {
addVoxelChain(
chain,
materials.root,
0.23,
0.17,
index === 1 ? materials.rootLight : null
);
});
// Small lateral rootlets.
const rootlets = [
[
new THREE.Vector3(-1.35, -1.08, 0.58),
new THREE.Vector3(-1.62, -1.34, 0.58),
new THREE.Vector3(-2.02, -1.43, 0.58)
],
[
new THREE.Vector3(1.25, -1.13, 0.58),
new THREE.Vector3(1.62, -1.38, 0.58),
new THREE.Vector3(2.05, -1.43, 0.58)
],
[
new THREE.Vector3(0, -1.25, 0.58),
new THREE.Vector3(-0.48, -1.43, 0.58),
new THREE.Vector3(-0.74, -1.58, 0.58)
]
];
rootlets.forEach((chain) => {
addVoxelChain(
chain,
materials.rootLight,
0.17,
0.14
);
});
// -------------------------------------------------------------------------
// Connected enlarged water-route graph
//
// Every graph edge shares exact endpoint coordinates with the next edge.
// Small cyan junction cubes cover the joins so the network reads as one
// continuous route rather than separate colored sections.
// -------------------------------------------------------------------------
const nodes = {};
const edges = {};
function addNode(id, x, y, z = 0.66) {
nodes[id] = new THREE.Vector3(x, y, z);
return nodes[id];
}
function addEdge(id, from, to) {
const curve = new THREE.LineCurve3(nodes[from], nodes[to]);
const length = curve.getLength();
edges[id] = {
id,
from,
to,
curve,
length
};
const geometry = new THREE.TubeGeometry(
curve,
2,
0.060,
7,
false
);
const mesh = new THREE.Mesh(geometry, materials.route);
mesh.renderOrder = 2;
scene.add(mesh);
}
// Root network.
addNode("rootLeftTip", -2.25, -1.34);
addNode("rootLeftMid", -1.35, -1.08);
addNode("rootLeftJoin", 0, -0.74);
addNode("rootRightTip", 2.25, -1.48);
addNode("rootRightMid", 1.25, -1.13);
addNode("rootRightJoin", 0, -1.02);
addNode("rootCenterTip", 0, -1.56);
addNode("rootBottom", 0, -0.45);
addNode("crown", 0, 0);
addEdge("rootL1", "rootLeftTip", "rootLeftMid");
addEdge("rootL2", "rootLeftMid", "rootLeftJoin");
addEdge("rootLJoin", "rootLeftJoin", "rootBottom");
addEdge("rootR1", "rootRightTip", "rootRightMid");
addEdge("rootR2", "rootRightMid", "rootRightJoin");
addEdge("rootRJoin", "rootRightJoin", "rootBottom");
addEdge("rootC1", "rootCenterTip", "rootBottom");
addEdge("rootToCrown", "rootBottom", "crown");
// Upright stem route.
addNode("stem1", 0, 1.05);
addNode("stem2", 0, 2.15);
addNode("stem3", 0, 3.25);
addNode("stem4", 0, 4.30);
addNode("stem5", 0, 5.25);
addEdge("stemA", "crown", "stem1");
addEdge("stemB", "stem1", "stem2");
addEdge("stemC", "stem2", "stem3");
addEdge("stemD", "stem3", "stem4");
addEdge("stemE", "stem4", "stem5");
// Leaf routes, including the main vein and connected side veins.
leafDefinitions.forEach((leaf, index) => {
const number = index + 1;
const direction = leaf.axis.clone().normalize();
const across = new THREE.Vector3(
-direction.y,
direction.x,
0
).normalize();
const routeBase = new THREE.Vector3(
leaf.base.x,
leaf.base.y,
0.66
);
const mid1 = routeBase.clone().add(
direction.clone().multiplyScalar(leaf.length * 0.34)
);
const mid2 = routeBase.clone().add(
direction.clone().multiplyScalar(leaf.length * 0.66)
);
const tip = routeBase.clone().add(
direction.clone().multiplyScalar(leaf.length)
);
const sideA = mid1.clone()
.add(direction.clone().multiplyScalar(leaf.length * 0.12))
.add(across.clone().multiplyScalar(leaf.width * 0.45));
const sideB = mid2.clone()
.add(direction.clone().multiplyScalar(leaf.length * 0.09))
.add(across.clone().multiplyScalar(-leaf.width * 0.43));
addNode(`leaf${number}Base`, routeBase.x, routeBase.y, routeBase.z);
addNode(`leaf${number}Mid1`, mid1.x, mid1.y, mid1.z);
addNode(`leaf${number}Mid2`, mid2.x, mid2.y, mid2.z);
addNode(`leaf${number}Tip`, tip.x, tip.y, tip.z);
addNode(`leaf${number}SideA`, sideA.x, sideA.y, sideA.z);
addNode(`leaf${number}SideB`, sideB.x, sideB.y, sideB.z);
addEdge(`branch${number}`, `stem${number}`, `leaf${number}Base`);
addEdge(`leaf${number}A`, `leaf${number}Base`, `leaf${number}Mid1`);
addEdge(`leaf${number}B`, `leaf${number}Mid1`, `leaf${number}Mid2`);
addEdge(`leaf${number}C`, `leaf${number}Mid2`, `leaf${number}Tip`);
addEdge(`leaf${number}SideA`, `leaf${number}Mid1`, `leaf${number}SideA`);
addEdge(`leaf${number}SideB`, `leaf${number}Mid2`, `leaf${number}SideB`);
});
// Junction blocks make every branch connection visibly continuous.
const junctionIds = [
"rootLeftJoin",
"rootRightJoin",
"rootBottom",
"crown",
"stem1",
"stem2",
"stem3",
"stem4",
"stem5",
"leaf1Base",
"leaf1Mid1",
"leaf1Mid2",
"leaf2Base",
"leaf2Mid1",
"leaf2Mid2",
"leaf3Base",
"leaf3Mid1",
"leaf3Mid2",
"leaf4Base",
"leaf4Mid1",
"leaf4Mid2"
];
junctionIds.forEach((id) => {
addBox(
scene,
new THREE.Vector3(0.125, 0.125, 0.125),
nodes[id],
materials.junction,
false
);
});
// -------------------------------------------------------------------------
// Continuous tracer lanes
//
// Each lane is an ordered list of connected graph edges. The tracer samples
// one edge at a time; at an edge boundary it lands exactly on the shared
// junction before entering the next edge.
// -------------------------------------------------------------------------
const laneDefinitions = [
{
edges: [
"rootL1", "rootL2", "rootLJoin", "rootToCrown",
"stemA", "branch1", "leaf1A", "leaf1B", "leaf1C"
]
},
{
edges: [
"rootR1", "rootR2", "rootRJoin", "rootToCrown",
"stemA", "stemB", "branch2", "leaf2A", "leaf2B", "leaf2C"
]
},
{
edges: [
"rootC1", "rootToCrown",
"stemA", "stemB", "stemC", "branch3",
"leaf3A", "leaf3B", "leaf3C"
]
},
{
edges: [
"rootL1", "rootL2", "rootLJoin", "rootToCrown",
"stemA", "stemB", "stemC", "stemD",
"branch4", "leaf4A", "leaf4B", "leaf4C"
]
},
{
edges: [
"rootR1", "rootR2", "rootRJoin", "rootToCrown",
"stemA", "branch1", "leaf1A", "leaf1B", "leaf1C"
]
},
{
edges: [
"rootC1", "rootToCrown",
"stemA", "stemB", "branch2",
"leaf2A", "leaf2B", "leaf2C"
]
}
];
const lanes = laneDefinitions.map((definition, laneIndex) => {
const edgeList = definition.edges.map((id) => edges[id]);
const totalLength = edgeList.reduce(
(sum, edge) => sum + edge.length,
0
);
return {
edgeList,
totalLength,
laneIndex,
speed: 0.90 + laneIndex * 0.035
};
});
const tracers = [];
lanes.forEach((lane) => {
for (let markerIndex = 0; markerIndex < 3; markerIndex++) {
const marker = new THREE.Mesh(unitCube, materials.marker);
marker.scale.setScalar(0.145);
marker.castShadow = false;
marker.renderOrder = 4;
scene.add(marker);
const fractionalOffset =
(markerIndex / 3 + lane.laneIndex * 0.137) % 1;
tracers.push({
marker,
lane,
offset: fractionalOffset * lane.totalLength
});
}
});
function sampleLane(lane, distance) {
let remaining = distance;
for (const edge of lane.edgeList) {
if (remaining <= edge.length) {
const t = edge.length === 0 ? 0 : remaining / edge.length;
return edge.curve.getPoint(Math.min(1, Math.max(0, t)));
}
remaining -= edge.length;
}
return lane.edgeList[lane.edgeList.length - 1].curve.getPoint(1);
}
// -------------------------------------------------------------------------
// Custom camera controls: orbit and zoom without addons
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, 2.05, 0),
radius: 10.8,
theta: 0.43,
phi: 1.13
};
const defaultCameraState = { ...cameraState };
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
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.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.43
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event && 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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
7.0,
15.5
);
updateCamera();
},
{ passive: false }
);
document.getElementById("reset-view").addEventListener("click", () => {
cameraState.target.copy(defaultCameraState.target);
cameraState.radius = defaultCameraState.radius;
cameraState.theta = defaultCameraState.theta;
cameraState.phi = defaultCameraState.phi;
updateCamera();
});
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labelDefinitions = [
{
element: document.getElementById("root-label"),
point: nodes.rootLeftTip,
offsetX: -5,
offsetY: 24
},
{
element: document.getElementById("route-label"),
point: nodes.stem2,
offsetX: 46,
offsetY: -22
},
{
element: document.getElementById("leaf-label"),
point: nodes.leaf3Tip,
offsetX: 15,
offsetY: -26
}
];
const projected = new THREE.Vector3();
function updateLabels() {
for (const label of labelDefinitions) {
projected.copy(label.point).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
label.element.style.left = `${x + label.offsetX}px`;
label.element.style.top = `${y + label.offsetY}px`;
}
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
for (const tracer of tracers) {
const lane = tracer.lane;
const distance =
(elapsed * lane.speed + tracer.offset) % lane.totalLength;
const position = sampleLane(lane, distance);
tracer.marker.position.copy(position);
// Fade only at the source/destination so recycling is not a visible jump.
const fadeIn = Math.min(1, distance / 0.28);
const fadeOut = Math.min(
1,
(lane.totalLength - distance) / 0.28
);
const scale = Math.max(0.001, Math.min(fadeIn, fadeOut));
tracer.marker.scale.setScalar(0.145 * scale);
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```