Prompt specification
Build or customize the Three.js 3D scene "Garden soil profile monolith" 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: Garden soil profile monolith
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/garden-soil-profile-monolith-x1185
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring soil horizons. Expose a rectangular soil column with roots, dark topsoil, lighter subsoil, and parent material; keep horizon boundaries readable and label the layering as one simplified example. Low-poly.
Working Three.js Starter (garden-soil-profile-monolith-x1185):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simplified Soil Profile</title>
<style>
:root {
--ink: #263029;
--muted: #687169;
--paper: rgba(250, 249, 243, 0.9);
--line: rgba(50, 60, 50, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8ede6;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#app {
position: relative;
width: 100%;
height: 100%;
isolation: isolate;
}
#scene {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.title-panel {
position: absolute;
z-index: 5;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #566257;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: #687b60;
}
h1 {
margin: 0;
font-size: clamp(28px, 3.2vw, 45px);
font-weight: 720;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 330px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.profile-tag {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 15px;
padding: 7px 10px;
border: 1px solid rgba(57, 70, 57, 0.15);
border-radius: 999px;
background: rgba(250, 249, 243, 0.66);
color: #536052;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.profile-tag span {
width: 6px;
height: 6px;
border-radius: 50%;
background: #7c916d;
box-shadow: 0 0 0 3px rgba(124, 145, 109, 0.15);
}
#leaders {
position: absolute;
z-index: 2;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
#leaders path {
fill: none;
stroke-width: 1.4;
stroke-linecap: round;
stroke-linejoin: round;
vector-effect: non-scaling-stroke;
}
#leaders circle {
fill: #f8f6ed;
stroke-width: 2;
vector-effect: non-scaling-stroke;
}
.label-card {
position: absolute;
z-index: 4;
right: 28px;
width: 238px;
padding: 13px 15px 14px;
border: 1px solid rgba(54, 65, 54, 0.13);
border-left: 4px solid var(--accent);
border-radius: 4px 10px 10px 4px;
background: var(--paper);
box-shadow: 0 12px 35px rgba(47, 54, 43, 0.08);
pointer-events: none;
backdrop-filter: blur(12px);
}
#label-a {
top: 20%;
--accent: #4c382b;
}
#label-b {
top: 43%;
--accent: #ad7041;
}
#label-c {
top: 66%;
--accent: #c29c68;
}
.label-heading {
display: flex;
align-items: center;
gap: 10px;
}
.horizon-code {
display: grid;
width: 28px;
height: 28px;
place-items: center;
border-radius: 50%;
background: var(--accent);
color: #fffdf5;
font-size: 12px;
font-weight: 800;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}
.label-name {
color: #303830;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.label-card p {
margin: 9px 0 0 38px;
color: #697168;
font-size: 11px;
line-height: 1.45;
}
.bottom-bar {
position: absolute;
z-index: 6;
right: 28px;
bottom: 24px;
left: 30px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.hint {
max-width: 390px;
color: #69736a;
font-size: 11px;
line-height: 1.55;
}
.hint strong {
display: block;
margin-bottom: 2px;
color: #455047;
font-size: 11px;
font-weight: 750;
}
.view-controls {
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.view-controls span {
color: #707a70;
font-size: 11px;
}
button {
appearance: none;
padding: 9px 13px;
border: 1px solid rgba(48, 61, 48, 0.2);
border-radius: 8px;
background: rgba(250, 249, 243, 0.82);
color: #3f4c40;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(8px);
transition:
background 160ms ease,
transform 160ms ease;
}
button:hover {
background: #fffdf5;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
button:focus-visible {
outline: 2px solid #728467;
outline-offset: 3px;
}
@media (max-width: 760px) {
.title-panel {
top: 20px;
left: 20px;
}
.subtitle {
max-width: 260px;
font-size: 12px;
}
.profile-tag {
display: none;
}
.label-card {
right: 12px;
width: 154px;
padding: 10px 10px 11px;
}
.label-heading {
gap: 7px;
}
.horizon-code {
width: 24px;
height: 24px;
font-size: 10px;
}
.label-name {
font-size: 10px;
}
.label-card p {
display: none;
}
#label-a {
top: 18%;
}
#label-b {
top: 43%;
}
#label-c {
top: 68%;
}
.bottom-bar {
right: 14px;
bottom: 16px;
left: 20px;
}
.hint span {
display: none;
}
.view-controls span {
display: none;
}
}
</style>
</head>
<body>
<main id="app">
<canvas
id="scene"
tabindex="0"
aria-label="Interactive low-poly soil profile with labeled soil horizons and exposed roots"
></canvas>
<svg id="leaders" aria-hidden="true">
<path id="leader-a"></path>
<circle id="dot-a" r="4"></circle>
<path id="leader-b"></path>
<circle id="dot-b" r="4"></circle>
<path id="leader-c"></path>
<circle id="dot-c" r="4"></circle>
</svg>
<header class="title-panel">
<div class="eyebrow">Soil horizons</div>
<h1>A simplified<br>soil profile</h1>
<p class="subtitle">
One illustrative sequence showing a dark topsoil, a lighter subsoil,
and slowly weathering parent material.
</p>
<div class="profile-tag">
<span></span>
Low-poly cutaway · thickness not to scale
</div>
</header>
<aside class="label-card" id="label-a">
<div class="label-heading">
<span class="horizon-code">A</span>
<span class="label-name">Topsoil</span>
</div>
<p>Dark, organic-rich surface horizon with active biological processes.</p>
</aside>
<aside class="label-card" id="label-b">
<div class="label-heading">
<span class="horizon-code">B</span>
<span class="label-name">Subsoil</span>
</div>
<p>Lighter mineral layer containing weathered material and accumulated clay.</p>
</aside>
<aside class="label-card" id="label-c">
<div class="label-heading">
<span class="horizon-code">C</span>
<span class="label-name">Parent material</span>
</div>
<p>Partially weathered substrate from which the upper horizons form.</p>
</aside>
<footer class="bottom-bar">
<div class="hint">
<strong>Drag to orbit · Scroll to zoom · Double-click to reset</strong>
<span>Horizon boundaries, colors, and thickness vary by location.</span>
</div>
<div class="view-controls">
<span>Interactive cutaway</span>
<button id="reset-view" type="button">Reset view</button>
</div>
</footer>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Basic scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ede6);
scene.fog = new THREE.Fog(0xe8ede6, 19, 38);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemisphere = new THREE.HemisphereLight(0xfff8e9, 0x788373, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffe5bb, 4.2);
keyLight.position.set(-7, 12, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 11;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcbd9e7, 1.0);
fillLight.position.set(8, 5, 3);
scene.add(fillLight);
// Deterministic randomness keeps the profile consistent between loads.
let randomSeed = 481516;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
const BLOCK_WIDTH = 6;
const BLOCK_DEPTH = 4.4;
const TOTAL_HEIGHT = 6.4;
const layers = [
{
id: "a",
code: "A",
name: "Topsoil",
y0: 5.05,
height: 1.35,
color: 0x4b382b,
accent: "#4c382b",
grains: [0x654a36, 0x392a21, 0x72533b]
},
{
id: "b",
code: "B",
name: "Subsoil",
y0: 2.55,
height: 2.5,
color: 0xaa7042,
accent: "#ad7041",
grains: [0xc58a50, 0x8e5d37, 0xd09a62]
},
{
id: "c",
code: "C",
name: "Parent material",
y0: 0,
height: 2.55,
color: 0xc2a06c,
accent: "#c29c68",
grains: [0xdfc694, 0x9b805c, 0x77766b]
}
];
// ------------------------------------------------------------
// Ground and display base
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xdfe5dc,
roughness: 1,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.23;
ground.receiveShadow = true;
scene.add(ground);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(6.45, 0.22, 4.85),
new THREE.MeshStandardMaterial({
color: 0xc8c2b4,
roughness: 0.95,
flatShading: true
})
);
plinth.position.y = -0.12;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
// ------------------------------------------------------------
// Soil block construction
// ------------------------------------------------------------
function colorVariant(hex, lightness, saturation = 0) {
return new THREE.Color(hex).offsetHSL(
0,
saturation,
lightness
);
}
function makeBoxMaterials(baseHex) {
const flat = { roughness: 1, metalness: 0, flatShading: true };
// Box material order: +X, -X, +Y, -Y, +Z, -Z.
return [
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, -0.025)
}),
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, -0.075)
}),
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, 0.065)
}),
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, -0.10)
}),
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, 0.025, 0.015)
}),
new THREE.MeshStandardMaterial({
...flat,
color: colorVariant(baseHex, -0.075)
})
];
}
/*
Builds a subtly irregular, triangulated panel over a block face.
Vertex colors and flat shading create a faceted low-poly surface.
*/
function createFacetPanel(
width,
height,
y0,
baseHex,
face,
halfWidth,
halfDepth
) {
const nx = face === "front" ? 9 : 7;
const ny = 4;
const positions = [];
const colors = [];
const indices = [];
const base = new THREE.Color(baseHex);
for (let iy = 0; iy <= ny; iy++) {
for (let ix = 0; ix <= nx; ix++) {
let u = ix / nx;
let v = iy / ny;
let y = y0 + v * height;
if (iy > 0 && iy < ny) {
y += range(-0.065, 0.065);
}
if (face === "front") {
let x = -halfWidth + u * width;
if (ix > 0 && ix < nx) {
x += range(-0.11, 0.11);
}
const z = halfDepth + 0.012 + range(0, 0.024);
positions.push(x, y, z);
} else {
let z = -halfDepth + u * width;
if (ix > 0 && ix < nx) {
z += range(-0.09, 0.09);
}
const x = halfWidth + 0.012 + range(0, 0.024);
positions.push(x, y, z);
}
const shade = base
.clone()
.offsetHSL(
range(-0.009, 0.009),
range(-0.035, 0.035),
range(-0.085, 0.085)
);
colors.push(shade.r, shade.g, shade.b);
}
}
for (let iy = 0; iy < ny; iy++) {
for (let ix = 0; ix < nx; ix++) {
const a = iy * (nx + 1) + ix;
const b = a + 1;
const d = a + nx + 1;
const c = d + 1;
if (face === "front") {
indices.push(a, b, d, b, c, d);
} else {
indices.push(a, d, b, b, c, d);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
geometry.computeBoundingSphere();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
metalness: 0,
flatShading: true,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
mesh.renderOrder = 1;
return mesh;
}
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x49372a,
transparent: true,
opacity: 0.42
});
layers.forEach((layer) => {
const geometry = new THREE.BoxGeometry(
BLOCK_WIDTH,
layer.height,
BLOCK_DEPTH
);
const block = new THREE.Mesh(
geometry,
makeBoxMaterials(layer.color)
);
block.position.y = layer.y0 + layer.height / 2;
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
const frontPanel = createFacetPanel(
BLOCK_WIDTH,
layer.height,
layer.y0,
layer.color,
"front",
BLOCK_WIDTH / 2,
BLOCK_DEPTH / 2
);
scene.add(frontPanel);
const sidePanel = createFacetPanel(
BLOCK_DEPTH,
layer.height,
layer.y0,
layer.color,
"side",
BLOCK_WIDTH / 2,
BLOCK_DEPTH / 2
);
scene.add(sidePanel);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
edges.position.copy(block.position);
scene.add(edges);
});
// Strong, narrow seam bands keep the three boundaries readable.
layers.slice(0, -1).forEach((layer) => {
const seamY = layer.y0;
const frontSeam = new THREE.Mesh(
new THREE.BoxGeometry(
BLOCK_WIDTH + 0.025,
0.035,
0.035
),
new THREE.MeshStandardMaterial({
color: 0x59432f,
roughness: 1
})
);
frontSeam.position.set(
0,
seamY,
BLOCK_DEPTH / 2 + 0.032
);
frontSeam.castShadow = true;
scene.add(frontSeam);
const sideSeam = new THREE.Mesh(
new THREE.BoxGeometry(
0.035,
0.035,
BLOCK_DEPTH + 0.025
),
new THREE.MeshStandardMaterial({
color: 0x59432f,
roughness: 1
})
);
sideSeam.position.set(
BLOCK_WIDTH / 2 + 0.032,
seamY,
0
);
sideSeam.castShadow = true;
scene.add(sideSeam);
});
// ------------------------------------------------------------
// Angular mineral grains and rock fragments
// ------------------------------------------------------------
const aggregateGeometry = new THREE.DodecahedronGeometry(1, 0);
function addAggregates(layer) {
const count = layer.id === "c" ? 78 : 62;
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0,
flatShading: true
});
const aggregates = new THREE.InstancedMesh(
aggregateGeometry,
material,
count
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
for (let i = 0; i < count; i++) {
const onSide = i % 2 === 1;
const radius = layer.id === "c"
? range(0.055, 0.145)
: range(0.025, 0.072);
const y = range(
layer.y0 + radius + 0.11,
layer.y0 + layer.height - radius - 0.10
);
if (onSide) {
dummy.position.set(
BLOCK_WIDTH / 2 + 0.028,
y,
range(
-BLOCK_DEPTH / 2 + radius + 0.12,
BLOCK_DEPTH / 2 - radius - 0.12
)
);
dummy.scale.set(
range(0.55, 1.0) * radius,
radius * range(0.65, 1.25),
radius * range(0.7, 1.35)
);
} else {
dummy.position.set(
range(
-BLOCK_WIDTH / 2 + radius + 0.12,
BLOCK_WIDTH / 2 - radius - 0.12
),
y,
BLOCK_DEPTH / 2 + 0.029
);
dummy.scale.set(
radius * range(0.75, 1.35),
radius * range(0.6, 1.15),
range(0.4, 0.8) * radius
);
}
dummy.rotation.set(
range(0, Math.PI),
range(0, Math.PI),
range(0, Math.PI)
);
dummy.updateMatrix();
aggregates.setMatrixAt(i, dummy.matrix);
color.setHex(
layer.grains[
Math.floor(random() * layer.grains.length)
]
);
color.offsetHSL(
range(-0.015, 0.015),
range(-0.04, 0.04),
range(-0.055, 0.055)
);
aggregates.setColorAt(i, color);
}
aggregates.instanceMatrix.needsUpdate = true;
if (aggregates.instanceColor) {
aggregates.instanceColor.needsUpdate = true;
}
aggregates.castShadow = true;
aggregates.receiveShadow = true;
scene.add(aggregates);
}
layers.forEach(addAggregates);
// ------------------------------------------------------------
// Sparse vegetation on the upper surface
// ------------------------------------------------------------
const bladeMaterials = [
new THREE.MeshStandardMaterial({
color: 0x617348,
roughness: 1,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x81905a,
roughness: 1,
side: THREE.DoubleSide,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x4d623e,
roughness: 1,
side: THREE.DoubleSide,
flatShading: true
})
];
function createGrassTuft(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, TOTAL_HEIGHT + 0.015, z);
for (let i = 0; i < 6; i++) {
const height = range(0.26, 0.55) * scale;
const width = range(0.035, 0.075) * scale;
const tipX = range(-0.12, 0.12) * scale;
const tipZ = range(-0.10, 0.10) * scale;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
-width / 2, 0, 0,
width / 2, 0, 0,
tipX, height, tipZ
],
3
)
);
geometry.computeVertexNormals();
const blade = new THREE.Mesh(
geometry,
bladeMaterials[i % bladeMaterials.length]
);
blade.position.x = range(-0.11, 0.11) * scale;
blade.position.z = range(-0.11, 0.11) * scale;
blade.rotation.y = range(0, Math.PI * 2);
blade.castShadow = true;
group.add(blade);
}
scene.add(group);
}
createGrassTuft(-1.9, 1.68, 1.05);
createGrassTuft(0.05, 1.72, 1.12);
createGrassTuft(1.95, 1.68, 1.0);
[
[-2.5, 0.8],
[-1.25, -0.25],
[-0.55, 0.55],
[0.75, -0.7],
[1.4, 0.15],
[2.55, -0.25]
].forEach(([x, z]) => {
createGrassTuft(x, z, range(0.58, 0.82));
});
const mossMaterial = new THREE.MeshStandardMaterial({
color: 0x66764b,
roughness: 1,
flatShading: true
});
for (let i = 0; i < 12; i++) {
const patch = new THREE.Mesh(
aggregateGeometry,
mossMaterial
);
patch.position.set(
range(-2.8, 2.8),
TOTAL_HEIGHT + 0.018,
range(-1.85, 1.35)
);
patch.scale.set(
range(0.11, 0.28),
range(0.018, 0.038),
range(0.09, 0.23)
);
patch.rotation.y = range(0, Math.PI * 2);
patch.castShadow = true;
scene.add(patch);
}
// ------------------------------------------------------------
// Exposed branching roots on the front cut face
// ------------------------------------------------------------
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0xe0bd86,
roughness: 0.92,
metalness: 0,
flatShading: true
});
const rootJointMaterial = new THREE.MeshStandardMaterial({
color: 0xcba56e,
roughness: 1,
flatShading: true
});
const up = new THREE.Vector3(0, 1, 0);
function addRootSegment(start, end, startRadius, endRadius) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
if (length < 0.001) return;
const geometry = new THREE.CylinderGeometry(
endRadius,
startRadius,
length,
5,
1,
false
);
const mesh = new THREE.Mesh(geometry, rootMaterial);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
up,
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
const joint = new THREE.Mesh(
aggregateGeometry,
rootJointMaterial
);
joint.position.copy(start);
joint.scale.setScalar(startRadius * 1.08);
joint.rotation.set(
range(0, Math.PI),
range(0, Math.PI),
range(0, Math.PI)
);
joint.castShadow = true;
scene.add(joint);
}
function createRootSystem(startX, depth) {
const points = [
new THREE.Vector3(startX, 6.43, 1.72)
];
let currentX = startX;
const segmentCount = 7;
for (let i = 0; i < segmentCount; i++) {
currentX += range(-0.19, 0.19);
currentX = THREE.MathUtils.clamp(
currentX,
-2.65,
2.65
);
const y = 6.29 - (i / (segmentCount - 1)) * depth;
points.push(
new THREE.Vector3(
currentX,
y,
2.265 + Math.sin(i * 1.7) * 0.012
)
);
}
for (let i = 0; i < points.length - 1; i++) {
const t = i / (points.length - 1);
addRootSegment(
points[i],
points[i + 1],
THREE.MathUtils.lerp(0.105, 0.026, t),
THREE.MathUtils.lerp(0.085, 0.018, t)
);
}
// Lateral branches descend away from each main root.
for (let pointIndex = 2; pointIndex <= 5; pointIndex++) {
const origin = points[pointIndex];
const direction = pointIndex % 2 === 0 ? -1 : 1;
let branchX = origin.x;
let branchY = origin.y;
const branchPoints = [origin.clone()];
const branchLength = range(3, 5);
for (let j = 0; j < branchLength; j++) {
branchX += direction * range(0.17, 0.31);
branchY -= range(0.13, 0.25);
branchX = THREE.MathUtils.clamp(
branchX,
-2.78,
2.78
);
branchPoints.push(
new THREE.Vector3(
branchX,
branchY,
2.285 + range(-0.008, 0.008)
)
);
}
for (let j = 0; j < branchPoints.length - 1; j++) {
const t = j / (branchPoints.length - 1);
addRootSegment(
branchPoints[j],
branchPoints[j + 1],
THREE.MathUtils.lerp(0.046, 0.013, t),
THREE.MathUtils.lerp(0.036, 0.009, t)
);
}
}
}
createRootSystem(-1.9, 3.55);
createRootSystem(0.05, 4.75);
createRootSystem(1.95, 3.15);
// ------------------------------------------------------------
// Camera and direct HTML interaction
// ------------------------------------------------------------
const view = {
theta: 0.48,
phi: 1.15,
radius: 13.7,
target: new THREE.Vector3(0.55, 3.1, 0)
};
function resetView() {
view.theta = 0.48;
view.phi = 1.15;
view.radius = 13.7;
view.target.x =
window.innerWidth > 850 ? 0.55 : 0;
updateCamera();
}
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const mobileMultiplier =
aspect < 0.7 ? 1.42 :
aspect < 1.0 ? 1.22 :
aspect < 1.25 ? 1.08 : 1;
const radius = view.radius * mobileMultiplier;
const sinPhi = Math.sin(view.phi);
camera.position.set(
view.target.x + radius * sinPhi * Math.sin(view.theta),
view.target.y + radius * Math.cos(view.phi),
view.target.z + radius * sinPhi * Math.cos(view.theta)
);
camera.lookAt(view.target);
camera.updateMatrixWorld();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
view.theta = THREE.MathUtils.clamp(
view.theta - deltaX * 0.0055,
-0.18,
1.02
);
view.phi = THREE.MathUtils.clamp(
view.phi - deltaY * 0.0045,
0.73,
1.43
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius + event.deltaY * 0.008,
9.5,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
document.querySelector("#reset-view").addEventListener(
"click",
resetView
);
// ------------------------------------------------------------
// Screen-space annotation leaders
// ------------------------------------------------------------
const anchors = layers.map((layer) => ({
id: layer.id,
color: layer.accent,
position: new THREE.Vector3(
BLOCK_WIDTH / 2 - 0.06,
layer.y0 + layer.height * 0.54,
BLOCK_DEPTH / 2 + 0.07
)
}));
function updateLeaders() {
for (const anchor of anchors) {
const projected = anchor.position
.clone()
.project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
const card = document.querySelector(
`#label-${anchor.id}`
);
const rect = card.getBoundingClientRect();
const startX = rect.left;
const startY = rect.top + rect.height / 2;
const elbowX = Math.max(30, x - 24);
const path = document.querySelector(
`#leader-${anchor.id}`
);
const dot = document.querySelector(
`#dot-${anchor.id}`
);
path.setAttribute(
"d",
[
`M ${startX} ${startY}`,
`L ${startX - 13} ${startY}`,
`L ${elbowX} ${y}`,
`L ${x} ${y}`
].join(" ")
);
path.setAttribute("stroke", anchor.color);
dot.setAttribute("cx", x);
dot.setAttribute("cy", y);
dot.setAttribute("stroke", anchor.color);
}
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function onResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight,
false
);
view.target.x =
window.innerWidth > 850 ? 0.55 : 0;
updateCamera();
}
window.addEventListener("resize", onResize);
resetView();
onResize();
renderer.setAnimationLoop(() => {
updateCamera();
updateLeaders();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```