Prompt specification
Build or customize the Three.js 3D scene "Vine follows support topology" 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: Vine follows support topology
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/vine-follows-support-topology-x1114
Scene Description Prompt:
Extend a fictional climbing vine along nearby trellis segments through incremental growth; branches stop at gaps until an additional support is introduced within the declared reach. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (vine-follows-support-topology-x1114):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Canopy — Incremental Vine Growth</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(13, 24, 22, 0.82);
--border: rgba(163, 226, 188, 0.18);
--text: #edf7ef;
--muted: #9bb4a5;
--green: #83e6a8;
--amber: #ffc96b;
--cyan: #7ed9e8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101916;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
border: 1px solid var(--border);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#heading {
position: absolute;
top: 22px;
left: 22px;
width: min(430px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
content: "";
border-radius: 2px;
background: var(--green);
box-shadow: 0 0 14px rgba(131, 230, 168, 0.8);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 34px);
line-height: 1.02;
letter-spacing: -0.035em;
}
#heading p {
max-width: 370px;
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
#stats {
position: absolute;
top: 22px;
right: 22px;
display: grid;
grid-template-columns: repeat(2, minmax(110px, 1fr));
gap: 8px;
width: min(270px, calc(100vw - 44px));
}
.stat {
min-height: 74px;
padding: 13px 14px;
border-radius: 13px;
}
.stat-label {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.stat-value {
margin-top: 6px;
font-size: 22px;
font-weight: 750;
letter-spacing: -0.04em;
}
#controls {
position: absolute;
bottom: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 17px;
border-radius: 16px;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.control-title strong {
font-size: 13px;
}
#reach-value {
color: var(--cyan);
font-size: 12px;
font-weight: 750;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 3px 0 15px;
accent-color: var(--cyan);
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
margin: -9px 0 14px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#gap-detail {
min-height: 72px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.065);
border-radius: 11px;
background: rgba(0, 0, 0, 0.15);
}
.gap-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
#gap-name {
font-size: 12px;
font-weight: 750;
}
#gap-distance {
color: var(--amber);
font-size: 11px;
font-weight: 750;
white-space: nowrap;
}
#gap-status {
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
button {
min-height: 40px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(255, 255, 255, 0.065);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease,
opacity 120ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(131, 230, 168, 0.38);
background: rgba(131, 230, 168, 0.12);
}
button:disabled {
cursor: not-allowed;
opacity: 0.42;
}
#install {
grid-column: 1 / -1;
border-color: rgba(131, 230, 168, 0.35);
background: linear-gradient(
135deg,
rgba(131, 230, 168, 0.19),
rgba(126, 217, 232, 0.08)
);
color: #dfffe9;
}
#help {
position: absolute;
right: 22px;
bottom: 22px;
width: min(285px, calc(100vw - 44px));
padding: 14px 16px;
border-radius: 14px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
#help strong {
display: block;
margin-bottom: 5px;
color: var(--text);
font-size: 11px;
}
kbd {
padding: 2px 5px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 4px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font: inherit;
font-size: 9px;
}
#toast {
position: absolute;
top: 22px;
left: 50%;
max-width: min(460px, calc(100vw - 44px));
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(13, 24, 22, 0.92);
color: var(--text);
font-size: 11px;
opacity: 0;
transform: translate(-50%, -12px);
transition:
opacity 180ms ease,
transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#toast.warn {
border-color: rgba(255, 201, 107, 0.38);
color: #ffe2aa;
}
#toast.good {
border-color: rgba(131, 230, 168, 0.38);
color: #c9ffdb;
}
@media (max-width: 720px) {
#heading {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#heading p {
display: none;
}
#stats {
top: 120px;
right: 12px;
width: 188px;
grid-template-columns: repeat(2, 1fr);
}
.stat {
min-height: 58px;
padding: 9px 10px;
}
.stat-value {
font-size: 17px;
}
#controls {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
}
#help {
display: none;
}
#toast {
top: 120px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel trellis and climbing vine"></canvas>
<div id="hud">
<section id="heading" class="panel">
<div class="eyebrow">Incremental growth study</div>
<h1>Voxel Canopy</h1>
<p>
The vine advances one voxel at a time. At unsupported gaps, branches
wait until a new trellis segment is installed inside the declared reach.
</p>
</section>
<section id="stats">
<div class="stat panel">
<div class="stat-label">Vine voxels</div>
<div class="stat-value" id="voxel-count">0</div>
</div>
<div class="stat panel">
<div class="stat-label">Active tips</div>
<div class="stat-value" id="tip-count">0</div>
</div>
<div class="stat panel">
<div class="stat-label">Waiting</div>
<div class="stat-value" id="waiting-count">0</div>
</div>
<div class="stat panel">
<div class="stat-label">Supports</div>
<div class="stat-value" id="support-count">0</div>
</div>
</section>
<section id="controls" class="panel">
<div class="control-title">
<strong>Support reach</strong>
<span id="reach-value">2.4 m</span>
</div>
<input
id="reach"
type="range"
min="1"
max="3.5"
step="0.1"
value="2.4"
aria-label="Declared support reach"
/>
<div class="range-labels">
<span>1.0 m</span>
<span>3.5 m</span>
</div>
<div id="gap-detail">
<div class="gap-row">
<span id="gap-name">Upper span</span>
<span id="gap-distance">1.2 m gap</span>
</div>
<div id="gap-status">Inside the declared reach.</div>
</div>
<div class="buttons">
<button id="install">Install selected support</button>
<button id="pause">Pause growth</button>
<button id="reset">Reset simulation</button>
</div>
</section>
<aside id="help" class="panel">
<strong>Direct controls</strong>
Drag to orbit the voxel trellis. Scroll to zoom. Click an amber gap marker
to select it, then install a support.
<br /><br />
<kbd>Space</kbd> pauses growth · <kbd>Enter</kbd> installs the selected
support · <kbd>R</kbd> resets.
</aside>
<div id="toast" role="status"></div>
</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101916);
scene.fog = new THREE.Fog(0x101916, 12, 25);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0, 2.7, 0);
let cameraYaw = 0.58;
let cameraPitch = 0.29;
let cameraRadius = 13.7;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraRadius;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xc8e8dd, 0x49382e, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe3b0, 3.1);
sun.position.set(-5, 10, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -3;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x77b7c7, 1.15);
rim.position.set(7, 5, -5);
scene.add(rim);
const unitCube = new THREE.BoxGeometry(1, 1, 1);
const supportGroup = new THREE.Group();
const dynamicSupportGroup = new THREE.Group();
const vineGroup = new THREE.Group();
const markerGroup = new THREE.Group();
const reachGroup = new THREE.Group();
scene.add(
supportGroup,
dynamicSupportGroup,
vineGroup,
markerGroup,
reachGroup
);
const materials = {
wood: new THREE.MeshStandardMaterial({
color: 0x8c684a,
roughness: 0.94,
metalness: 0.02
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x5a4335,
roughness: 1
}),
newWood: new THREE.MeshStandardMaterial({
color: 0xd4aa70,
emissive: 0x3b240c,
emissiveIntensity: 0.25,
roughness: 0.82
}),
vineA: new THREE.MeshStandardMaterial({
color: 0x3f9f55,
roughness: 0.82
}),
vineB: new THREE.MeshStandardMaterial({
color: 0x58bb67,
roughness: 0.78
}),
vineC: new THREE.MeshStandardMaterial({
color: 0x2f7f43,
roughness: 0.9
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x8ad46f,
roughness: 0.74
}),
pot: new THREE.MeshStandardMaterial({
color: 0x9f5d3d,
roughness: 0.9
}),
soil: new THREE.MeshStandardMaterial({
color: 0x3d2a1f,
roughness: 1
})
};
function box(parent, material, x, y, z, width, height, depth, shadow = true) {
const mesh = new THREE.Mesh(unitCube, material);
mesh.position.set(x, y, z);
mesh.scale.set(width, height, depth);
mesh.castShadow = shadow;
mesh.receiveShadow = shadow;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Voxel environment
// ------------------------------------------------------------
const wallMaterials = [
new THREE.MeshStandardMaterial({ color: 0x263a32, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x2a4037, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x30473c, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x22342e, roughness: 1 })
];
for (let row = 0; row < 8; row++) {
for (let column = 0; column < 15; column++) {
const material =
wallMaterials[(row * 7 + column * 3) % wallMaterials.length];
box(
scene,
material,
-5.15 + column * 0.74,
0.37 + row * 0.74,
-0.86,
0.71,
0.71,
0.25,
false
);
}
}
box(
scene,
new THREE.MeshStandardMaterial({
color: 0x18231f,
roughness: 1
}),
0,
-0.18,
-0.25,
18,
0.35,
10,
false
);
const tileMaterials = [
new THREE.MeshStandardMaterial({ color: 0x27342e, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x2d3a32, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x24322c, roughness: 1 })
];
for (let x = -5; x <= 5; x++) {
for (let z = -2; z <= 2; z++) {
box(
scene,
tileMaterials[(x + z + 10) % tileMaterials.length],
x * 0.74,
0.015,
z * 0.74,
0.69,
0.05,
0.69,
false
);
}
}
// Root planter.
box(scene, materials.pot, -3.2, 0.28, 0, 0.92, 0.5, 0.75);
box(scene, materials.soil, -3.2, 0.54, 0, 0.68, 0.08, 0.52);
box(scene, materials.pot, -3.65, 0.56, 0, 0.13, 0.1, 0.82);
box(scene, materials.pot, -2.75, 0.56, 0, 0.13, 0.1, 0.82);
box(scene, materials.pot, -3.2, 0.56, -0.39, 0.82, 0.1, 0.13);
box(scene, materials.pot, -3.2, 0.56, 0.39, 0.82, 0.1, 0.13);
// Small voxel planter on the far trellis.
box(scene, materials.pot, 1.4, 0.27, 0, 0.78, 0.46, 0.66);
box(scene, materials.soil, 1.4, 0.51, 0, 0.58, 0.07, 0.46);
// ------------------------------------------------------------
// Trellis support graph
// ------------------------------------------------------------
const supports = [];
const connections = new Map();
function point(x, y, z = 0) {
return new THREE.Vector3(x, y, z);
}
function nodeKey(vector) {
return `${vector.x.toFixed(3)},${vector.y.toFixed(3)},${vector.z.toFixed(3)}`;
}
function addSupportData(id, a, b, dynamic = false) {
const support = {
id,
a: a.clone(),
b: b.clone(),
aKey: nodeKey(a),
bKey: nodeKey(b),
length: a.distanceTo(b),
used: false,
dynamic,
mesh: null
};
supports.push(support);
if (!connections.has(support.aKey)) {
connections.set(support.aKey, []);
}
if (!connections.has(support.bKey)) {
connections.set(support.bKey, []);
}
connections.get(support.aKey).push(id);
connections.get(support.bKey).push(id);
const horizontal = Math.abs(a.y - b.y) > Math.abs(a.x - b.x);
const material = dynamic ? materials.newWood : materials.wood;
const mesh = box(
dynamic ? dynamicSupportGroup : supportGroup,
material,
(a.x + b.x) * 0.5,
(a.y + b.y) * 0.5,
0,
horizontal ? a.distanceTo(b) + 0.08 : 0.15,
horizontal ? 0.15 : a.distanceTo(b) + 0.08,
0.2
);
support.mesh = mesh;
return support;
}
// Connected route from the root.
addSupportData("stem-1", point(-3.2, 0.55), point(-3.2, 3));
addSupportData("rail-1", point(-3.2, 3), point(-1, 3));
addSupportData("stem-2a", point(-1, 3), point(-1, 4.2));
// The two outgoing branches at (-1, 4.2).
addSupportData("stem-2b", point(-1, 4.2), point(-1, 5.2));
addSupportData("branch-1", point(-1, 4.2), point(0.2, 4.2));
// Upper branch ends before the detached trellis.
addSupportData("upper-run", point(-1, 5.2), point(0.2, 5.2));
// Detached trellis network.
addSupportData("far-top", point(1.4, 5.2), point(3, 5.2));
addSupportData("far-upper", point(3, 5.2), point(3, 4.2));
addSupportData("far-middle", point(3, 4.2), point(3, 2.6));
addSupportData("far-lower", point(3, 2.6), point(3, 0.8));
addSupportData("far-cross", point(3, 2.6), point(1.4, 2.6));
addSupportData("far-side", point(1.4, 2.6), point(1.4, 0.8));
addSupportData("far-base", point(1.4, 0.8), point(3, 0.8));
const initialSupports = [...supports];
// ------------------------------------------------------------
// Gaps, markers, and reach visualization
// ------------------------------------------------------------
const markerPickables = [];
const gaps = [
{
id: "gap-upper",
name: "Upper span",
from: point(0.2, 5.2),
to: point(1.4, 5.2),
installed: false,
waitingTipId: null
},
{
id: "gap-middle",
name: "Middle span",
from: point(0.2, 4.2),
to: point(3, 4.2),
installed: false,
waitingTipId: null
}
].map((gap) => ({
...gap,
fromKey: nodeKey(gap.from),
toKey: nodeKey(gap.to),
length: gap.from.distanceTo(gap.to),
marker: null,
markerCore: null,
ghostGroup: null
}));
for (const gap of gaps) {
const ghostGroup = new THREE.Group();
const ghostMaterial = new THREE.MeshStandardMaterial({
color: 0xf3b85f,
emissive: 0x7a4a12,
emissiveIntensity: 0.38,
transparent: true,
opacity: 0.22,
depthWrite: false,
roughness: 0.7
});
const ghostCount = Math.max(3, Math.ceil(gap.length / 0.28));
for (let i = 0; i <= ghostCount; i++) {
const position = gap.from.clone().lerp(gap.to, i / ghostCount);
position.z = 0.21;
box(
ghostGroup,
ghostMaterial,
position.x,
position.y,
position.z,
0.09,
0.09,
0.09,
false
);
}
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffc96b,
emissive: 0xa96514,
emissiveIntensity: 0.85,
transparent: true,
opacity: 0.92,
depthWrite: false
});
const marker = box(
markerGroup,
markerMaterial,
(gap.from.x + gap.to.x) * 0.5,
(gap.from.y + gap.to.y) * 0.5,
0.3,
0.32,
0.32,
0.32,
false
);
marker.userData.gapId = gap.id;
markerPickables.push(marker);
gap.marker = marker;
gap.markerCore = marker;
gap.ghostGroup = ghostGroup;
markerGroup.add(ghostGroup);
}
const reachMaterial = new THREE.MeshBasicMaterial({
color: 0x7ed9e8,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const reachCubes = [];
for (let i = 0; i < 28; i++) {
const cube = box(
reachGroup,
reachMaterial,
0,
0,
0,
0.045,
0.045,
0.045,
false
);
reachCubes.push(cube);
}
let selectedGapId = "gap-upper";
let declaredReach = 2.4;
function selectedGap() {
return gaps.find((gap) => gap.id === selectedGapId);
}
function selectGap(id) {
selectedGapId = id;
for (const gap of gaps) {
const active = gap.id === id;
gap.marker.material.color.setHex(
active ? 0xffe08a : 0xffc96b
);
gap.marker.material.emissive.setHex(
active ? 0xc7871f : 0xa96514
);
gap.marker.scale.set(
active ? 0.42 : 0.32,
active ? 0.42 : 0.32,
active ? 0.42 : 0.32
);
}
updateReachRing();
updateUI();
}
function updateReachRing() {
const gap = selectedGap();
reachGroup.visible = Boolean(gap && !gap.installed);
if (!gap) return;
reachCubes.forEach((cube, index) => {
const angle = (index / reachCubes.length) * Math.PI * 2;
cube.position.set(
gap.from.x + Math.cos(angle) * declaredReach,
gap.from.y + Math.sin(angle) * declaredReach,
0.31
);
});
}
// ------------------------------------------------------------
// Incremental vine simulation
// ------------------------------------------------------------
const tips = [];
const voxelKeys = new Set();
const VOXEL_SPACING = 0.13;
const GROWTH_SPEED = 1.05;
let voxelCount = 0;
let paused = false;
let nextTipId = 1;
function supportById(id) {
return supports.find((support) => support.id === id);
}
function pointOnSupport(support, distance) {
if (support.aKey === support.aKey) {
// This branch is retained below for clarity; actual direction is
// selected by the tip's starting node.
}
const ratio = THREE.MathUtils.clamp(
distance / support.length,
0,
1
);
return support.a.clone().lerp(support.b, ratio);
}
function addVineVoxel(position, tip) {
const p = position.clone();
p.z = 0.17;
const key = `${Math.round(p.x * 20)},${Math.round(p.y * 20)},${Math.round(p.z * 20)}`;
if (voxelKeys.has(key)) return;
voxelKeys.add(key);
const colorChoice = Math.random();
const material =
colorChoice < 0.2
? materials.vineC
: colorChoice < 0.58
? materials.vineB
: materials.vineA;
const size = 0.125 + Math.random() * 0.025;
box(
vineGroup,
material,
p.x,
p.y,
p.z,
size,
size,
size
);
voxelCount++;
if (voxelCount % 10 === 0) {
const side = Math.random() > 0.5 ? 1 : -1;
const leaf = box(
vineGroup,
materials.leaf,
p.x + side * 0.11,
p.y + 0.07,
p.z + 0.025,
0.13,
0.075,
0.11
);
leaf.rotation.z = side * 0.28;
}
}
function spawnTip(startKey, supportId) {
const support = supportById(supportId);
if (!support || support.used) return null;
support.used = true;
const startsAtA = startKey === support.aKey;
const endKey = startsAtA ? support.bKey : support.aKey;
const start = startsAtA ? support.a : support.b;
const end = startsAtA ? support.b : support.a;
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xa8ffb7,
emissive: 0x3acb65,
emissiveIntensity: 1.15,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const marker = box(
vineGroup,
markerMaterial,
start.x,
start.y,
0.2,
0.19,
0.19,
0.19,
false
);
const tip = {
id: nextTipId++,
supportId,
startKey,
endKey,
distance: 0,
nextVoxelDistance: 0,
state: "growing",
marker
};
tips.push(tip);
addVineVoxel(start, tip);
return tip;
}
function outgoingSupports(nodeKey, incomingSupportId) {
const ids = connections.get(nodeKey) || [];
return ids.filter((id) => {
const support = supportById(id);
return support && id !== incomingSupportId && !support.used;
});
}
function handleArrival(tip) {
const outgoing = outgoingSupports(tip.endKey, tip.supportId);
if (outgoing.length > 0) {
tip.state = "settled";
tip.marker.visible = false;
for (const supportId of outgoing) {
spawnTip(tip.endKey, supportId);
}
return;
}
const gap = gaps.find(
(candidate) =>
!candidate.installed && candidate.fromKey === tip.endKey
);
if (gap) {
tip.state = "waiting";
tip.marker.visible = true;
tip.marker.material.color.setHex(0xffc96b);
tip.marker.material.emissive.setHex(0xb56f16);
gap.waitingTipId = tip.id;
selectGap(gap.id);
showToast(
`${gap.name}: branch stopped at a ${gap.length.toFixed(1)} m gap.`,
"warn"
);
} else {
tip.state = "settled";
tip.marker.visible = false;
}
}
function updateTips(dt) {
for (const tip of [...tips]) {
if (tip.state !== "growing") continue;
const support = supportById(tip.supportId);
if (!support) continue;
tip.distance += GROWTH_SPEED * dt;
while (tip.nextVoxelDistance <= tip.distance) {
const ratio = tip.nextVoxelDistance / support.length;
let position;
if (tip.startKey === support.aKey) {
position = support.a.clone().lerp(support.b, ratio);
} else {
position = support.b.clone().lerp(support.a, ratio);
}
addVineVoxel(position, tip);
tip.nextVoxelDistance += VOXEL_SPACING;
}
if (tip.distance >= support.length) {
const end =
tip.startKey === support.aKey ? support.b : support.a;
addVineVoxel(end, tip);
tip.distance = support.length;
const markerPosition = end;
tip.marker.position.set(
markerPosition.x,
markerPosition.y,
0.2
);
handleArrival(tip);
} else {
const ratio = tip.distance / support.length;
const position =
tip.startKey === support.aKey
? support.a.clone().lerp(support.b, ratio)
: support.b.clone().lerp(support.a, ratio);
tip.marker.position.set(position.x, position.y, 0.2);
}
}
}
// ------------------------------------------------------------
// Support installation
// ------------------------------------------------------------
function installSelectedSupport() {
const gap = selectedGap();
if (!gap) return;
if (gap.installed) {
showToast("That support is already installed.");
return;
}
if (gap.length > declaredReach + 0.001) {
showToast(
`This ${gap.length.toFixed(1)} m span exceeds the ${declaredReach.toFixed(1)} m reach.`,
"warn"
);
return;
}
const supportId = `installed-${gap.id}`;
addSupportData(supportId, gap.from, gap.to, true);
gap.installed = true;
gap.ghostGroup.visible = false;
gap.marker.visible = false;
const waitingTip = tips.find(
(tip) => tip.id === gap.waitingTipId && tip.state === "waiting"
);
if (waitingTip) {
waitingTip.state = "settled";
waitingTip.marker.visible = false;
gap.waitingTipId = null;
spawnTip(gap.fromKey, supportId);
}
showToast(
`${gap.name} support installed. The branch can continue.`,
"good"
);
updateReachRing();
updateUI();
}
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const ui = {
voxelCount: document.querySelector("#voxel-count"),
tipCount: document.querySelector("#tip-count"),
waitingCount: document.querySelector("#waiting-count"),
supportCount: document.querySelector("#support-count"),
reach: document.querySelector("#reach"),
reachValue: document.querySelector("#reach-value"),
gapName: document.querySelector("#gap-name"),
gapDistance: document.querySelector("#gap-distance"),
gapStatus: document.querySelector("#gap-status"),
install: document.querySelector("#install"),
pause: document.querySelector("#pause"),
reset: document.querySelector("#reset"),
toast: document.querySelector("#toast")
};
let toastTimer;
function showToast(message, kind = "") {
ui.toast.textContent = message;
ui.toast.className = `show ${kind}`;
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
ui.toast.className = "";
}, 3200);
}
function updateUI() {
const growingTips = tips.filter((tip) => tip.state === "growing").length;
const waitingTips = tips.filter((tip) => tip.state === "waiting").length;
ui.voxelCount.textContent = voxelCount.toLocaleString();
ui.tipCount.textContent = growingTips;
ui.waitingCount.textContent = waitingTips;
ui.supportCount.textContent = supports.length;
ui.reachValue.textContent = `${declaredReach.toFixed(1)} m`;
const gap = selectedGap();
if (gap) {
ui.gapName.textContent = gap.name;
ui.gapDistance.textContent = `${gap.length.toFixed(1)} m gap`;
if (gap.installed) {
ui.gapStatus.textContent =
"Support installed. This route is open to the vine.";
ui.install.disabled = true;
ui.install.textContent = "Support installed";
} else if (gap.length > declaredReach + 0.001) {
ui.gapStatus.textContent =
"Outside the declared reach. Increase the reach limit to install it.";
ui.install.disabled = true;
ui.install.textContent = "Support is out of reach";
} else {
ui.gapStatus.textContent =
"Inside the declared reach. Installing will connect this route.";
ui.install.disabled = false;
ui.install.textContent = "Install selected support";
}
}
ui.pause.textContent = paused ? "Resume growth" : "Pause growth";
}
ui.reach.addEventListener("input", () => {
declaredReach = Number(ui.reach.value);
updateReachRing();
updateUI();
});
ui.install.addEventListener("click", installSelectedSupport);
ui.pause.addEventListener("click", () => {
paused = !paused;
updateUI();
});
ui.reset.addEventListener("click", resetSimulation);
function resetSimulation() {
for (const support of supports) {
if (support.dynamic) {
dynamicSupportGroup.remove(support.mesh);
}
}
supports.length = 0;
initialSupports.forEach((support) => {
support.used = false;
supports.push(support);
});
connections.clear();
for (const support of supports) {
if (!connections.has(support.aKey)) {
connections.set(support.aKey, []);
}
if (!connections.has(support.bKey)) {
connections.set(support.bKey, []);
}
connections.get(support.aKey).push(support.id);
connections.get(support.bKey).push(support.id);
}
while (vineGroup.children.length) {
vineGroup.remove(vineGroup.children[0]);
}
tips.length = 0;
voxelKeys.clear();
voxelCount = 0;
nextTipId = 1;
paused = false;
for (const gap of gaps) {
gap.installed = false;
gap.waitingTipId = null;
gap.ghostGroup.visible = true;
gap.marker.visible = true;
}
spawnTip(nodeKey(point(-3.2, 0.55)), "stem-1");
selectGap(selectedGapId);
updateReachRing();
updateUI();
showToast("Simulation reset. The vine is growing again.", "good");
}
// ------------------------------------------------------------
// Camera controls implemented directly
// ------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
let pointerMoved = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerMoved = 0;
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;
pointerMoved += Math.abs(dx) + Math.abs(dy);
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.005,
0.08,
1.12
);
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (pointerMoved > 7) return;
const bounds = canvas.getBoundingClientRect();
const pointer = new THREE.Vector2(
((event.clientX - bounds.left) / bounds.width) * 2 - 1,
-((event.clientY - bounds.top) / bounds.height) * 2 + 1
);
const raycaster = new THREE.Raycaster();
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(markerPickables, false)[0];
if (hit) {
selectGap(hit.object.userData.gapId);
showToast(`${selectedGap().name} selected.`);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
8,
20
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
installSelectedSupport();
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
paused = !paused;
updateUI();
} else if (event.code === "Enter") {
installSelectedSupport();
} else if (event.key.toLowerCase() === "r") {
resetSimulation();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Start and animation loop
// ------------------------------------------------------------
spawnTip(nodeKey(point(-3.2, 0.55)), "stem-1");
selectGap(selectedGapId);
updateReachRing();
updateUI();
const clock = new THREE.Clock();
let uiTimer = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (!paused) {
updateTips(dt);
}
// Gentle pulsing of gap markers.
for (const gap of gaps) {
if (!gap.installed) {
const pulse = 1 + Math.sin(elapsed * 3 + gap.length) * 0.08;
gap.marker.rotation.y = elapsed * 0.45;
gap.marker.rotation.x = elapsed * 0.25;
gap.marker.scale.setScalar(
gap.id === selectedGapId ? 0.42 * pulse : 0.32 * pulse
);
}
}
// Active tips pulse independently.
for (const tip of tips) {
if (tip.state === "growing") {
const pulse = 1 + Math.sin(elapsed * 5 + tip.id) * 0.12;
tip.marker.scale.setScalar(0.19 * pulse);
} else if (tip.state === "waiting") {
const pulse = 1 + Math.sin(elapsed * 4 + tip.id) * 0.16;
tip.marker.scale.setScalar(0.23 * pulse);
}
}
reachMaterial.opacity =
0.12 + (Math.sin(elapsed * 1.7) * 0.5 + 0.5) * 0.08;
uiTimer += dt;
if (uiTimer > 0.15) {
uiTimer = 0;
updateUI();
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```