Prompt specification
Build or customize the Three.js 3D scene "Epiphyte attachment on bark" 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: Epiphyte attachment on bark
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/epiphyte-attachment-on-bark-x0733
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: epiphyte attachment on bark. Show a small epiphytic plant attached to a branch, with roots contacting bark and leaves exposed to air; distinguish support contact from penetration into host tissue. Return one self-contained HTML file.
Working Three.js Starter (epiphyte-attachment-on-bark-x0733):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Epiphyte — Bark Attachment Study</title>
<style>
:root {
--ink: #eef7ee;
--muted: #a9bdb0;
--panel: rgba(12, 27, 24, 0.78);
--line: rgba(185, 225, 203, 0.2);
--green: #8bd66d;
--teal: #62d9c1;
--root: #e0aa68;
--bark: #9d6540;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #0b1715;
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; }
#overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
}
#title {
position: absolute;
top: 28px;
left: 30px;
width: min(440px, calc(100vw - 60px));
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.65);
}
#title h1 {
margin: 7px 0 6px;
font-size: clamp(25px, 3.1vw, 43px);
line-height: 0.98;
letter-spacing: -0.045em;
font-weight: 760;
}
#title p {
max-width: 390px;
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#controls {
position: absolute;
top: 26px;
right: 28px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: 430px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 7px;
padding: 9px 12px;
color: var(--ink);
background: rgba(13, 30, 26, 0.78);
backdrop-filter: blur(12px);
font: inherit;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, color 160ms ease;
}
button:hover {
border-color: rgba(98, 217, 193, 0.65);
background: rgba(24, 49, 42, 0.9);
}
button.active {
color: #09211b;
border-color: var(--teal);
background: var(--teal);
}
#leaders {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
}
#leaders line {
stroke: rgba(198, 231, 214, 0.68);
stroke-width: 1;
stroke-dasharray: 3 4;
}
#leaders circle {
fill: var(--teal);
stroke: rgba(220, 255, 240, 0.85);
stroke-width: 1.5;
}
.annotation {
position: absolute;
min-width: 164px;
padding: 10px 12px;
border-left: 2px solid var(--teal);
background: linear-gradient(90deg, rgba(10, 27, 23, 0.9), rgba(10, 27, 23, 0.58));
backdrop-filter: blur(10px);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
transform: translateY(-50%);
}
.annotation strong {
display: block;
color: #f3fff7;
font-size: 10px;
letter-spacing: 0.13em;
}
.annotation span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
#leaf-label { right: 5.5%; top: 23%; }
#root-label { right: 6%; top: 57%; border-left-color: var(--root); }
#bark-label { left: 5%; top: 39%; }
#legend {
position: absolute;
left: 28px;
bottom: 27px;
display: flex;
gap: 18px;
align-items: center;
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--panel);
backdrop-filter: blur(12px);
font-size: 10px;
color: var(--muted);
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
white-space: nowrap;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.18);
}
#inset {
position: absolute;
right: 27px;
bottom: 26px;
width: 292px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 14px 50px rgba(0, 0, 0, 0.26);
}
#inset header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
#inset h2 {
margin: 0;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#inset .cross {
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
}
.diagram {
position: relative;
height: 150px;
overflow: hidden;
border: 1px solid rgba(209, 235, 219, 0.18);
border-radius: 6px;
background: #12241f;
image-rendering: pixelated;
}
.diagram::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(rgba(255,255,255,0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(255,255,255,0.035) 1px, transparent 1px);
background-size: 12px 12px;
}
.air-zone {
position: absolute;
inset: 0 0 78px 0;
display: flex;
align-items: flex-start;
padding: 8px;
color: rgba(184, 219, 202, 0.55);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.15em;
}
.tissue {
position: absolute;
left: 0;
right: 0;
bottom: 0;
height: 78px;
background:
linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px),
linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
#6d452f;
background-size: 12px 12px;
border-top: 3px solid #b77c4f;
}
.tissue-label {
position: absolute;
left: 10px;
bottom: 9px;
color: #e7c5a2;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
}
.surface {
position: absolute;
top: 69px;
left: 0;
right: 0;
height: 3px;
background: var(--teal);
box-shadow: 0 0 9px rgba(98, 217, 193, 0.65);
z-index: 3;
}
.root-block {
position: absolute;
z-index: 4;
background: var(--root);
border: 2px solid #3a2419;
box-shadow: inset -4px -4px 0 rgba(78, 42, 24, 0.25);
}
.root-block.r1 { left: 53px; top: 12px; width: 25px; height: 23px; }
.root-block.r2 { left: 62px; top: 32px; width: 25px; height: 23px; }
.root-block.r3 { left: 72px; top: 52px; width: 25px; height: 20px; }
.contact-label {
position: absolute;
z-index: 5;
left: 111px;
top: 46px;
color: var(--teal);
font-size: 8px;
font-weight: 900;
letter-spacing: 0.1em;
text-shadow: 0 1px 3px #000;
}
.no-entry {
position: absolute;
z-index: 5;
left: 82px;
top: 86px;
color: #ffd3b0;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.12em;
}
#inset p {
margin: 11px 1px 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
#inset p b { color: var(--ink); }
#help {
position: absolute;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(206, 226, 214, 0.62);
font-size: 10px;
letter-spacing: 0.04em;
text-shadow: 0 2px 8px #000;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 20;
background: #0b1715;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
transition: opacity 500ms ease;
}
@media (max-width: 850px) {
#title { left: 18px; top: 18px; }
#title p { display: none; }
#controls { top: 18px; right: 18px; max-width: 220px; }
#controls button { padding: 8px 9px; font-size: 9px; }
#inset { right: 14px; bottom: 14px; width: 248px; transform: scale(0.9); transform-origin: bottom right; }
#legend { left: 14px; bottom: 14px; flex-direction: column; align-items: flex-start; gap: 7px; }
#help { display: none; }
.annotation { min-width: 135px; padding: 8px; }
.annotation span { font-size: 9px; }
#leaf-label { right: 3%; top: 25%; }
#root-label { right: 3%; top: 53%; }
#bark-label { left: 3%; top: 42%; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel scene of an epiphytic plant attached to bark"></canvas>
<div id="overlay">
<div id="title">
<div class="eyebrow">Voxel field study · 01</div>
<h1>Epiphyte on bark</h1>
<p>External support, not host penetration. The roots meet the bark surface while the leaves remain exposed to moving air.</p>
</div>
<div id="controls">
<button id="contact-button" class="active" aria-pressed="true">Contact zones</button>
<button id="focus-button" aria-pressed="false">Focus roots</button>
<button id="orbit-button" aria-pressed="false">Auto orbit</button>
<button id="reset-button">Reset view</button>
</div>
<svg id="leaders" aria-hidden="true">
<line id="leaf-line"></line><circle id="leaf-dot" r="3.5"></circle>
<line id="root-line"></line><circle id="root-dot" r="3.5"></circle>
<line id="bark-line"></line><circle id="bark-dot" r="3.5"></circle>
</svg>
<div id="leaf-label" class="annotation">
<strong>LEAVES IN AIR</strong>
<span>Photosynthetic surface exposed</span>
</div>
<div id="root-label" class="annotation">
<strong>SUPPORT ROOTS</strong>
<span>Tips terminate at the bark boundary</span>
</div>
<div id="bark-label" class="annotation">
<strong>HOST BARK</strong>
<span>Tissue remains unpenetrated</span>
</div>
<div id="legend">
<div class="legend-item"><span class="swatch" style="background:#78bc63"></span>Exposed foliage</div>
<div class="legend-item"><span class="swatch" style="background:#d49b5d"></span>Clinging root</div>
<div class="legend-item"><span class="swatch" style="background:#62d9c1"></span>Contact interface</div>
<div class="legend-item"><span class="swatch" style="background:#895433"></span>Host tissue</div>
</div>
<aside id="inset" aria-label="Diagram of root contact without penetration">
<header>
<h2>Contact section</h2>
<span class="cross">× NO ENTRY</span>
</header>
<div class="diagram">
<div class="air-zone">AIR · EPIPHYTE</div>
<div class="tissue"><span class="tissue-label">HOST TISSUE</span></div>
<div class="surface"></div>
<div class="root-block r1"></div>
<div class="root-block r2"></div>
<div class="root-block r3"></div>
<div class="contact-label">CONTACT ONLY</div>
<div class="no-entry">×</div>
</div>
<p><b>Support contact:</b> each root tip ends at the outer bark. No root enters the host tissue below.</p>
</aside>
<div id="help">DRAG TO ORBIT · SCROLL TO ZOOM · CONTACT ZONES SHOWN IN TEAL</div>
</div>
<div id="loading">ASSEMBLING VOXEL STUDY…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({ canvas, 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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0b1715);
scene.fog = new THREE.FogExp2(0x0b1715, 0.035);
const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 70);
const target = new THREE.Vector3(0, -0.15, 0);
const orbit = {
theta: 0.72,
phi: 1.03,
radius: 14.2,
desiredTheta: 0.72,
desiredPhi: 1.03,
desiredRadius: 14.2
};
const hemi = new THREE.HemisphereLight(0xbde7d3, 0x38251d, 1.6);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffe4b8, 3.2);
key.position.set(-4, 10, 7);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.bias = -0.0007;
scene.add(key);
const rim = new THREE.DirectionalLight(0x62d9c1, 1.55);
rim.position.set(7, 4, -8);
scene.add(rim);
const fill = new THREE.DirectionalLight(0x8fbaff, 0.65);
fill.position.set(4, -1, 8);
scene.add(fill);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const tempColor = new THREE.Color();
const VOXEL = 0.28;
const BRANCH_Y = -2.4;
const branchRadiusAt = (ix) => 7 - Math.max(0, Math.abs(ix) - 14) * 0.11;
function hash3(x, y, z) {
const n = Math.sin(x * 127.1 + y * 311.7 + z * 74.7) * 43758.5453;
return n - Math.floor(n);
}
function barkSurfaceY(x, z) {
const ix = Math.round(x / VOXEL);
const iz = Math.max(-7, Math.min(7, Math.round(z / VOXEL)));
const radius = branchRadiusAt(ix);
for (let iy = 7; iy >= -7; iy--) {
if (iy * iy + iz * iz <= radius * radius) {
return BRANCH_Y + iy * VOXEL + VOXEL / 2;
}
}
return BRANCH_Y + VOXEL / 2;
}
// ---------------------------------------------------------------------
// Voxel branch
// ---------------------------------------------------------------------
const barkPositions = [];
const barkColors = [];
for (let ix = -25; ix <= 25; ix++) {
const radius = branchRadiusAt(ix);
for (let iy = -7; iy <= 7; iy++) {
for (let iz = -7; iz <= 7; iz++) {
const r2 = iy * iy + iz * iz;
if (r2 > radius * radius) continue;
const x = ix * VOXEL;
const y = BRANCH_Y + iy * VOXEL;
const z = iz * VOXEL;
barkPositions.push([x, y, z]);
const n = hash3(ix, iy, iz);
const groove = hash3(ix * 0.37, iy * 1.7, iz * 0.23);
const top = iy > 3 ? 0.055 : 0;
let lightness = 0.25 + n * 0.13 + top;
if (groove > 0.89) lightness -= 0.075;
if (Math.abs(ix) >= 23) {
const ring = Math.floor(Math.sqrt(r2));
lightness += (ring % 2 === 0 ? 0.055 : -0.035);
}
tempColor.setHSL(
0.065 + n * 0.018,
0.38 + groove * 0.16,
Math.max(0.16, Math.min(0.48, lightness))
);
barkColors.push(tempColor.clone());
}
}
}
const barkMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
metalness: 0,
flatShading: true
});
const bark = new THREE.InstancedMesh(cubeGeometry, barkMaterial, barkPositions.length);
bark.castShadow = true;
bark.receiveShadow = true;
barkPositions.forEach((p, i) => {
dummy.position.set(p[0], p[1], p[2]);
dummy.scale.set(VOXEL, VOXEL, VOXEL);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
bark.setMatrixAt(i, dummy.matrix);
bark.setColorAt(i, barkColors[i]);
});
bark.instanceMatrix.needsUpdate = true;
bark.instanceColor.needsUpdate = true;
scene.add(bark);
// Small bark scales on the visible face add relief without changing the
// underlying support surface.
const scalePositions = [];
for (let ix = -23; ix <= 23; ix++) {
for (let iy = -4; iy <= 5; iy++) {
if (hash3(ix * 1.8, iy * 2.1, 9.2) > 0.82) {
const r = branchRadiusAt(ix);
if (iy * iy + 6 * 6 <= r * r) {
scalePositions.push([
ix * VOXEL + 0.015,
BRANCH_Y + iy * VOXEL,
6 * VOXEL + 0.025
]);
}
}
}
}
const barkScales = new THREE.InstancedMesh(
cubeGeometry,
new THREE.MeshStandardMaterial({ color: 0x69412c, roughness: 1, flatShading: true }),
scalePositions.length
);
scalePositions.forEach((p, i) => {
dummy.position.set(...p);
dummy.scale.set(VOXEL * 0.78, VOXEL * 0.62, VOXEL * 0.22);
dummy.updateMatrix();
barkScales.setMatrixAt(i, dummy.matrix);
});
barkScales.castShadow = true;
barkScales.receiveShadow = true;
scene.add(barkScales);
// ---------------------------------------------------------------------
// Clinging roots: every cube stops with its underside on the bark.
// ---------------------------------------------------------------------
const rootPositions = [];
const rootColors = [];
const contactPositions = [];
const seenRoots = new Set();
const rootSize = 0.19;
const rootAngles = [];
for (let i = 0; i < 13; i++) {
rootAngles.push((i / 13) * Math.PI * 2 + (hash3(i, 3, 7) - 0.5) * 0.16);
}
rootAngles.forEach((angle, rootIndex) => {
const length = 2.35 + hash3(rootIndex, 11, 2) * 1.1;
const steps = 15 + Math.floor(hash3(rootIndex, 8, 1) * 5);
for (let j = 0; j <= steps; j++) {
const t = j / steps;
let x = Math.cos(angle) * length * t;
let z = Math.sin(angle) * length * t;
x += Math.sin(t * Math.PI * 3 + rootIndex) * 0.08;
z += Math.cos(t * Math.PI * 2.4 + rootIndex) * 0.055;
z = Math.max(-1.55, Math.min(1.55, z));
x = Math.round(x / 0.095) * 0.095;
z = Math.round(z / 0.095) * 0.095;
const y = barkSurfaceY(x, z);
const key = `${x.toFixed(3)}:${z.toFixed(3)}:${y.toFixed(3)}`;
if (seenRoots.has(key)) continue;
seenRoots.add(key);
rootPositions.push([x, y + rootSize / 2, z]);
const isTip = j > steps - 3;
const n = hash3(rootIndex, j, 4);
tempColor.setHSL(
isTip ? 0.105 : 0.083,
isTip ? 0.66 : 0.48,
isTip ? 0.66 : 0.48 + n * 0.09
);
rootColors.push(tempColor.clone());
if (j > 2 && j % 4 === 0 && rootIndex % 2 === 0) {
contactPositions.push([x, y, z]);
}
}
});
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
flatShading: true,
emissive: 0x000000,
emissiveIntensity: 0
});
const roots = new THREE.InstancedMesh(cubeGeometry, rootMaterial, rootPositions.length);
roots.castShadow = true;
roots.receiveShadow = true;
rootPositions.forEach((p, i) => {
dummy.position.set(...p);
dummy.scale.set(rootSize, rootSize, rootSize);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
roots.setMatrixAt(i, dummy.matrix);
roots.setColorAt(i, rootColors[i]);
});
roots.instanceMatrix.needsUpdate = true;
roots.instanceColor.needsUpdate = true;
scene.add(roots);
// Thin teal bands emphasize the exact root–bark interface without
// suggesting that the root continues below the bark surface.
const contactMaterial = new THREE.MeshBasicMaterial({
color: 0x62d9c1,
transparent: true,
opacity: 0.78,
depthWrite: false,
toneMapped: false
});
const contactMarkers = new THREE.InstancedMesh(
cubeGeometry,
contactMaterial,
contactPositions.length
);
contactPositions.forEach((p, i) => {
dummy.position.set(p[0], p[1] + 0.018, p[2]);
dummy.scale.set(rootSize * 1.16, 0.036, rootSize * 1.16);
dummy.updateMatrix();
contactMarkers.setMatrixAt(i, dummy.matrix);
});
contactMarkers.instanceMatrix.needsUpdate = true;
contactMarkers.renderOrder = 3;
scene.add(contactMarkers);
// ---------------------------------------------------------------------
// Voxel rosette: exposed leaves radiate above the attachment point.
// ---------------------------------------------------------------------
const foliagePositions = [];
const foliageColors = [];
function addFoliage(x, y, z, scale, color) {
foliagePositions.push({ p: [x, y, z], s: scale });
foliageColors.push(color.clone());
}
const plantBaseY = -0.18;
// Central cup / crown.
for (let x = -1; x <= 1; x++) {
for (let z = -1; z <= 1; z++) {
for (let y = 0; y <= 2; y++) {
if (x === 0 && z === 0 && y < 2) continue;
const n = hash3(x + 9, y, z + 9);
tempColor.setHSL(0.30 + n * 0.035, 0.52, 0.32 + n * 0.07);
addFoliage(
x * 0.18,
plantBaseY + y * 0.16,
z * 0.18,
[0.18, 0.16, 0.18],
tempColor
);
}
}
}
const leafCount = 15;
for (let leaf = 0; leaf < leafCount; leaf++) {
const angle = (leaf / leafCount) * Math.PI * 2 + (hash3(leaf, 1, 4) - 0.5) * 0.18;
const length = 2.15 + hash3(leaf, 2, 8) * 0.85;
const height = 1.35 + hash3(leaf, 5, 2) * 1.0;
const segments = 13;
for (let j = 0; j <= segments; j++) {
const t = j / segments;
const radial = length * (t * 0.88 + Math.sin(t * Math.PI) * 0.07);
const x = Math.cos(angle) * radial;
const z = Math.sin(angle) * radial;
const y = plantBaseY + 0.12 + height * Math.sin(t * Math.PI * 0.88);
const across = t < 0.20 ? 1 : t < 0.78 ? 3 : 2;
const perpX = -Math.sin(angle);
const perpZ = Math.cos(angle);
for (let k = 0; k < across; k++) {
const offset = (k - (across - 1) / 2) * 0.165;
const n = hash3(leaf * 17, j, k + 30);
const tip = t > 0.82;
tempColor.setHSL(
0.34 + n * 0.055,
0.47 + n * 0.14,
tip ? 0.52 + n * 0.07 : 0.32 + n * 0.08
);
addFoliage(
x + perpX * offset,
y + (k === 1 && across === 3 ? 0.018 : 0),
z + perpZ * offset,
[0.18, 0.115, 0.18],
tempColor
);
}
// A brighter central vein reinforces the direction of each leaf.
tempColor.setHSL(0.38, 0.63, 0.52);
addFoliage(x, y + 0.072, z, [0.115, 0.055, 0.115], tempColor);
}
}
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.78,
metalness: 0,
flatShading: true,
transparent: true,
opacity: 1
});
const foliage = new THREE.InstancedMesh(
cubeGeometry,
foliageMaterial,
foliagePositions.length
);
foliage.castShadow = true;
foliage.receiveShadow = true;
foliagePositions.forEach((item, i) => {
dummy.position.set(...item.p);
dummy.scale.set(...item.s);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
foliage.setMatrixAt(i, dummy.matrix);
foliage.setColorAt(i, foliageColors[i]);
});
foliage.instanceMatrix.needsUpdate = true;
foliage.instanceColor.needsUpdate = true;
scene.add(foliage);
// ---------------------------------------------------------------------
// Sparse floating voxel motes for depth.
// ---------------------------------------------------------------------
const motePositions = [];
for (let i = 0; i < 42; i++) {
const n1 = hash3(i, 71, 2);
const n2 = hash3(i, 17, 8);
const n3 = hash3(i, 44, 3);
motePositions.push([
(n1 - 0.5) * 22,
-1 + n2 * 11,
(n3 - 0.5) * 18
]);
}
const motes = new THREE.InstancedMesh(
cubeGeometry,
new THREE.MeshBasicMaterial({
color: 0xb7d7c5,
transparent: true,
opacity: 0.15,
depthWrite: false
}),
motePositions.length
);
motePositions.forEach((p, i) => {
dummy.position.set(...p);
dummy.scale.set(0.018, 0.018, 0.018);
dummy.updateMatrix();
motes.setMatrixAt(i, dummy.matrix);
});
scene.add(motes);
// ---------------------------------------------------------------------
// Labels projected from 3D anchors.
// ---------------------------------------------------------------------
const annotationData = [
{
label: document.getElementById("leaf-label"),
line: document.getElementById("leaf-line"),
dot: document.getElementById("leaf-dot"),
point: new THREE.Vector3(2.45, 1.55, -0.15)
},
{
label: document.getElementById("root-label"),
line: document.getElementById("root-line"),
dot: document.getElementById("root-dot"),
point: new THREE.Vector3(1.62, -0.12, 1.25)
},
{
label: document.getElementById("bark-label"),
line: document.getElementById("bark-line"),
dot: document.getElementById("bark-dot"),
point: new THREE.Vector3(-3.65, -0.32, 0.95)
}
];
const projected = new THREE.Vector3();
function updateAnnotations() {
const width = window.innerWidth;
const height = window.innerHeight;
annotationData.forEach((item) => {
projected.copy(item.point).project(camera);
const visible = projected.z > -1 && projected.z < 1;
const sx = (projected.x * 0.5 + 0.5) * width;
const sy = (-projected.y * 0.5 + 0.5) * height;
const rect = item.label.getBoundingClientRect();
let x1;
let y1;
if (sx < rect.left) {
x1 = rect.left;
y1 = rect.top + rect.height / 2;
} else if (sx > rect.right) {
x1 = rect.right;
y1 = rect.top + rect.height / 2;
} else if (sy < rect.top) {
x1 = rect.left + rect.width / 2;
y1 = rect.top;
} else {
x1 = rect.left + rect.width / 2;
y1 = rect.bottom;
}
item.line.setAttribute("x1", x1);
item.line.setAttribute("y1", y1);
item.line.setAttribute("x2", sx);
item.line.setAttribute("y2", sy);
item.dot.setAttribute("cx", sx);
item.dot.setAttribute("cy", sy);
item.line.style.display = visible ? "" : "none";
item.dot.style.display = visible ? "" : "none";
});
}
// ---------------------------------------------------------------------
// UI controls
// ---------------------------------------------------------------------
const contactButton = document.getElementById("contact-button");
const focusButton = document.getElementById("focus-button");
const orbitButton = document.getElementById("orbit-button");
const resetButton = document.getElementById("reset-button");
let contactsVisible = true;
let focusRoots = false;
let autoOrbit = false;
contactButton.addEventListener("click", () => {
contactsVisible = !contactsVisible;
contactMarkers.visible = contactsVisible;
contactButton.classList.toggle("active", contactsVisible);
contactButton.setAttribute("aria-pressed", String(contactsVisible));
contactButton.textContent = contactsVisible ? "Contact zones" : "Contact zones off";
});
focusButton.addEventListener("click", () => {
focusRoots = !focusRoots;
focusButton.classList.toggle("active", focusRoots);
focusButton.setAttribute("aria-pressed", String(focusRoots));
foliageMaterial.opacity = focusRoots ? 0.23 : 1;
foliageMaterial.depthWrite = !focusRoots;
rootMaterial.emissive.setHex(focusRoots ? 0x244d31 : 0x000000);
rootMaterial.emissiveIntensity = focusRoots ? 0.7 : 0;
contactMarkers.visible = focusRoots ? true : contactsVisible;
});
orbitButton.addEventListener("click", () => {
autoOrbit = !autoOrbit;
orbitButton.classList.toggle("active", autoOrbit);
orbitButton.setAttribute("aria-pressed", String(autoOrbit));
});
resetButton.addEventListener("click", () => {
orbit.desiredTheta = 0.72;
orbit.desiredPhi = 1.03;
orbit.desiredRadius = 14.2;
});
// ---------------------------------------------------------------------
// Camera controls implemented directly, without external addons.
// ---------------------------------------------------------------------
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.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.desiredTheta -= dx * 0.006;
orbit.desiredPhi = Math.max(0.34, Math.min(1.42, orbit.desiredPhi - dy * 0.005));
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.desiredRadius = Math.max(7.5, Math.min(22, orbit.desiredRadius + event.deltaY * 0.008));
}, { passive: false });
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (autoOrbit && !dragging) {
orbit.desiredTheta += dt * 0.13;
}
orbit.theta += (orbit.desiredTheta - orbit.theta) * 0.085;
orbit.phi += (orbit.desiredPhi - orbit.phi) * 0.085;
orbit.radius += (orbit.desiredRadius - orbit.radius) * 0.085;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
target.y + orbit.radius * Math.cos(orbit.phi),
target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(target);
motes.rotation.y += dt * 0.018;
motes.position.y = Math.sin(clock.elapsedTime * 0.24) * 0.08;
renderer.render(scene, camera);
updateAnnotations();
if (firstFrame) {
firstFrame = false;
const loading = document.getElementById("loading");
loading.style.opacity = "0";
setTimeout(() => loading.remove(), 520);
}
}
animate();
</script>
</body>
</html>
```