Prompt specification
Build or customize the Three.js 3D scene "Window-box light competition" 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: Window-box light competition
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/window-box-light-competition-x0763
Scene Description Prompt:
Create a self-contained HTML scene using Three.js from cdnjs with a voxel window planter containing three young seedlings. A wall return and window frame cast shade over different parts of the soil, and a movable lamp provides an alternate light direction. Show each seedling's existing stem separately from newly added growth segments during a slow illustrative time-lapse. Changing the lamp position should affect the direction of subsequent growth rather than rotate the whole plant like a rigid object. Keep the seedlings' roots attached to their original positions and allow the viewer to compare their distinct paths. Label the response as a simplified growth model instead of a species-specific prediction.
Working Three.js Starter (window-box-light-competition-x0763):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Windowbox Seedlings</title>
<style>
:root {
color-scheme: light;
--ink: #20302b;
--muted: #60716a;
--panel: rgba(249, 250, 245, 0.94);
--line: rgba(38, 62, 52, 0.16);
--accent: #2e7555;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cfd9dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
top: 22px;
left: 24px;
z-index: 5;
width: min(490px, calc(100vw - 390px));
pointer-events: none;
text-shadow: 0 1px 18px rgba(255, 255, 255, 0.55);
}
#hud .eyebrow {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
color: #376d55;
}
#hud h1 {
margin: 7px 0 5px;
font-size: clamp(25px, 3vw, 42px);
line-height: 1.04;
letter-spacing: -0.035em;
}
#hud p {
margin: 0;
max-width: 450px;
color: #44564e;
font-size: 13px;
line-height: 1.45;
}
.model-badge {
display: inline-block;
margin-top: 12px;
padding: 7px 10px;
border: 1px solid rgba(47, 95, 73, 0.28);
border-radius: 999px;
background: rgba(240, 249, 241, 0.82);
color: #2c6048;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.075em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
right: 18px;
bottom: 18px;
width: 330px;
padding: 18px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(31, 54, 44, 0.2);
backdrop-filter: blur(14px);
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 0 10px;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.section {
padding: 14px 0;
border-bottom: 1px solid var(--line);
}
.section:last-child { border-bottom: 0; }
.row {
display: flex;
align-items: center;
gap: 8px;
}
.row + .row { margin-top: 9px; }
button {
appearance: none;
border: 0;
border-radius: 11px;
padding: 10px 13px;
background: var(--accent);
color: white;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, filter 120ms ease;
}
button:hover { filter: brightness(1.07); }
button:active { transform: translateY(1px); }
button.secondary {
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.62);
color: var(--ink);
}
#playBtn { flex: 1; }
#resetBtn { flex: 0 0 auto; }
.progress-track {
height: 7px;
margin: 13px 0 7px;
overflow: hidden;
border-radius: 999px;
background: rgba(42, 73, 59, 0.12);
}
#progressFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #438b63, #98ca58);
transition: width 150ms linear;
}
.small {
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.between { justify-content: space-between; }
label.control {
display: grid;
grid-template-columns: 1fr 47px;
gap: 7px;
align-items: center;
margin-top: 10px;
color: #40544b;
font-size: 11px;
font-weight: 650;
}
label.control output {
color: #75857e;
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
grid-column: 1 / -1;
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--accent);
cursor: pointer;
}
.check {
display: flex;
align-items: center;
gap: 8px;
margin-top: 9px;
color: #40544b;
font-size: 12px;
}
.check input { accent-color: var(--accent); }
.source-list {
display: grid;
gap: 7px;
margin-top: 10px;
}
.source-row {
display: grid;
grid-template-columns: 23px 1fr auto;
align-items: center;
gap: 8px;
font-size: 11px;
}
.plant-dot {
display: inline-grid;
width: 21px;
height: 21px;
place-items: center;
border-radius: 7px;
color: #173027;
font-size: 10px;
font-weight: 900;
}
.source-chip {
padding: 3px 7px;
border-radius: 999px;
background: #e8eee9;
color: #52645b;
font-size: 9px;
font-weight: 800;
text-transform: uppercase;
}
.source-chip.lamp {
background: #fff0cf;
color: #82571c;
}
.source-chip.sun {
background: #e2f3d0;
color: #3c6f2e;
}
.legend {
display: grid;
gap: 8px;
margin-top: 12px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #4a5e54;
font-size: 11px;
}
.swatch {
width: 26px;
height: 9px;
border-radius: 3px;
}
.swatch.old { background: #536b49; }
.swatch.new { background: #8dcc55; }
.swatch.root { background: #79513a; }
.note {
margin-top: 13px;
padding: 11px;
border-radius: 12px;
background: rgba(48, 89, 69, 0.075);
color: #4e6258;
font-size: 10.5px;
line-height: 1.5;
}
.note strong { color: #315b47; }
#hint {
position: fixed;
z-index: 4;
left: 24px;
bottom: 18px;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.66);
border-radius: 10px;
background: rgba(249, 250, 245, 0.74);
color: #52665b;
font-size: 10.5px;
backdrop-filter: blur(8px);
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
.plant-label,
#lampLabel {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.plant-label {
display: grid;
width: 24px;
height: 24px;
place-items: center;
border: 2px solid rgba(255, 255, 255, 0.86);
border-radius: 8px;
color: #193027;
font-size: 10px;
font-weight: 900;
box-shadow: 0 4px 15px rgba(27, 48, 39, 0.22);
}
#lampLabel {
padding: 5px 8px;
border-radius: 8px;
background: rgba(69, 52, 27, 0.84);
color: #ffe1a4;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
box-shadow: 0 5px 18px rgba(34, 27, 15, 0.2);
}
@media (max-width: 800px) {
#hud {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
}
#hud p { display: none; }
#hud h1 { font-size: 27px; }
.model-badge { margin-top: 8px; font-size: 8px; }
#panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 43vh;
padding: 14px;
border-radius: 16px;
}
#hint { display: none; }
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels">
<div id="labelA" class="plant-label" style="background:#58d5ff">A</div>
<div id="labelB" class="plant-label" style="background:#ffca68">B</div>
<div id="labelC" class="plant-label" style="background:#ff78c8">C</div>
<div id="lampLabel">Movable lamp</div>
</div>
<header id="hud">
<div class="eyebrow">Illustrative light & growth study</div>
<h1>Windowbox seedlings</h1>
<p>
Three seedlings grow from fixed root anchors. Move the lamp to change the
direction of subsequently added stem segments; earlier growth remains in place.
</p>
<div class="model-badge">
Simplified growth model · not a species-specific prediction
</div>
</header>
<aside id="panel">
<div class="section">
<div class="panel-title">
<span>Slow time-lapse</span>
<span id="speedValue" class="small">1×</span>
</div>
<div class="row">
<button id="playBtn" type="button">Pause</button>
<button id="resetBtn" class="secondary" type="button">Reset growth</button>
</div>
<div class="progress-track">
<div id="progressFill"></div>
</div>
<div class="row between small">
<span>Illustrative growth steps</span>
<span id="progressText">0 / 30</span>
</div>
<label class="control">
<span>Playback speed</span>
<output id="speedOutput">1.0×</output>
<input id="speed" type="range" min="0.5" max="3" step="0.1" value="1">
</label>
</div>
<div class="section">
<div class="panel-title">
<span>Alternate lamp</span>
<span class="small">Drag the lamp in the scene</span>
</div>
<label class="control">
<span>Left / right</span>
<output id="lampXOutput">−3.7</output>
<input id="lampX" type="range" min="-5" max="5" step="0.1" value="-3.7">
</label>
<label class="control">
<span>Height</span>
<output id="lampYOutput">3.8</output>
<input id="lampY" type="range" min="2" max="6" step="0.1" value="3.8">
</label>
<label class="control">
<span>Front / back</span>
<output id="lampZOutput">3.5</output>
<input id="lampZ" type="range" min="-1" max="5" step="0.1" value="3.5">
</label>
<label class="control">
<span>Lamp intensity</span>
<output id="lampPowerOutput">60%</output>
<input id="lampPower" type="range" min="0" max="100" step="1" value="60">
</label>
<div id="lampReadout" class="small" style="margin-top:10px">
Window sun enters through the mullion and transom; the right wall return shades seedling C.
</div>
</div>
<div class="section">
<div class="panel-title"><span>Comparison view</span></div>
<label class="check">
<input id="rootsToggle" type="checkbox">
Reveal fixed root systems
</label>
<label class="check">
<input id="tracesToggle" type="checkbox" checked>
Show distinct growth traces
</label>
<div class="source-list">
<div class="source-row">
<span class="plant-dot" style="background:#58d5ff">A</span>
<span>Seedling A</span>
<span id="sourceA" class="source-chip">—</span>
</div>
<div class="source-row">
<span class="plant-dot" style="background:#ffca68">B</span>
<span>Seedling B</span>
<span id="sourceB" class="source-chip">—</span>
</div>
<div class="source-row">
<span class="plant-dot" style="background:#ff78c8">C</span>
<span>Seedling C</span>
<span id="sourceC" class="source-chip">—</span>
</div>
</div>
</div>
<div class="section">
<div class="panel-title"><span>Stem history</span></div>
<div class="legend">
<div class="legend-row">
<span class="swatch old"></span>
<span>Existing stem — fixed from the beginning</span>
</div>
<div class="legend-row">
<span class="swatch new"></span>
<span>New growth — appended during the time-lapse</span>
</div>
<div class="legend-row">
<span class="swatch root"></span>
<span>Root system — remains attached to its original anchor</span>
</div>
</div>
<div class="note">
<strong>Qualitative model.</strong>
Each new segment samples the available window and lamp light at the
moment it is added. Moving the lamp does not rotate a plant rigidly:
it changes only the direction of later segments. Root anchors and old
stem geometry never move.
</div>
</div>
</aside>
<div id="hint">
Drag empty space to orbit · drag the lamp to move it · wheel to zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
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.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcfd9dc);
scene.fog = new THREE.Fog(0xcfd9dc, 17, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 2.35, 0);
let cameraYaw = 0.58;
let cameraPitch = 0.36;
let cameraRadius = 13.2;
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();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(
0xe9f5ff,
0x68533d,
1.25
);
scene.add(hemisphere);
const fill = new THREE.DirectionalLight(0xcce8ff, 0.42);
fill.position.set(5, 4, 8);
scene.add(fill);
const sun = new THREE.DirectionalLight(0xffedc4, 3.15);
sun.position.set(7, 10, -7);
sun.target.position.set(0, 1, 0);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -4;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun, sun.target);
const lampLight = new THREE.PointLight(0xffb85f, 57, 16, 2);
lampLight.castShadow = true;
lampLight.shadow.mapSize.set(1024, 1024);
lampLight.shadow.bias = -0.0005;
lampLight.shadow.normalBias = 0.025;
scene.add(lampLight);
// ------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------
const UP = new THREE.Vector3(0, 1, 0);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const segmentBox = new THREE.BoxGeometry(1, 1, 1);
segmentBox.translate(0, 0.5, 0);
const soilMaterials = [
new THREE.MeshStandardMaterial({ color: 0x584332, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x624b37, roughness: 1 }),
new THREE.MeshStandardMaterial({ color: 0x503b2d, roughness: 1 })
];
const woodMaterials = [
new THREE.MeshStandardMaterial({ color: 0xa66d3c, roughness: 0.92 }),
new THREE.MeshStandardMaterial({ color: 0xba7d45, roughness: 0.92 }),
new THREE.MeshStandardMaterial({ color: 0x925b32, roughness: 0.92 })
];
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xf3efe4,
roughness: 0.8
});
const wallMaterial = new THREE.MeshStandardMaterial({
color: 0xded8cb,
roughness: 0.96
});
const returnMaterial = new THREE.MeshStandardMaterial({
color: 0xd4c8b6,
roughness: 0.96
});
const oldStemMaterial = new THREE.MeshStandardMaterial({
color: 0x526a47,
roughness: 0.92
});
const rootMaterial = new THREE.MeshStandardMaterial({
color: 0x765039,
roughness: 1
});
const shadeCasters = [];
function block(
width, height, depth, material,
x, y, z,
parent = scene,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function orientedSegment(
parent, base, direction, length,
width, depth, material
) {
const mesh = new THREE.Mesh(segmentBox, material);
mesh.position.copy(base);
mesh.quaternion.setFromUnitVectors(UP, direction.clone().normalize());
mesh.scale.set(width, length, depth);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Sill, voxel planter, and soil
// ------------------------------------------------------------
const SOIL_TOP = 1.05;
block(
11.4, 0.30, 4.3,
new THREE.MeshStandardMaterial({ color: 0xc8c3b8, roughness: 0.95 }),
0, -0.13, -0.25,
scene, false, true
);
// Planter base and voxel slats.
block(7.65, 0.18, 2.85, woodMaterials[1], 0, 0.06, 0);
for (let row = 0; row < 4; row++) {
const y = 0.23 + row * 0.22;
const material = woodMaterials[(row + 1) % woodMaterials.length];
block(7.58, 0.18, 0.18, material, 0, y, 1.34);
block(7.58, 0.18, 0.18, material, 0, y, -1.34);
block(0.18, 0.18, 2.52, material, -3.70, y, 0);
block(0.18, 0.18, 2.52, material, 3.70, y, 0);
}
for (const x of [-3.72, 3.72]) {
for (const z of [-1.36, 1.36]) {
block(0.24, 1.02, 0.24, woodMaterials[2], x, 0.53, z);
}
}
// A grid of small soil voxels.
for (let ix = 0; ix < 21; ix++) {
for (let iz = 0; iz < 7; iz++) {
const material = soilMaterials[(ix * 3 + iz * 5) % soilMaterials.length];
const x = (ix - 10) * 0.34;
const z = (iz - 3) * 0.34;
block(
0.322, 0.43, 0.322,
material,
x, 0.815, z,
scene, false, true
);
}
}
// ------------------------------------------------------------
// Window, frame, and wall return
// ------------------------------------------------------------
function addShadeCaster(mesh) {
shadeCasters.push(mesh);
return mesh;
}
// Back wall around a large opening.
addShadeCaster(block(
2.3, 7.8, 0.40, wallMaterial,
-5.35, 3.0, -2.75
));
addShadeCaster(block(
2.3, 7.8, 0.40, wallMaterial,
5.35, 3.0, -2.75
));
addShadeCaster(block(
8.4, 1.2, 0.40, wallMaterial,
0, 7.1, -2.75
));
addShadeCaster(block(
8.4, 1.9, 0.40, wallMaterial,
0, 0.08, -2.75
));
// Perpendicular wall return on the right.
// With the sun at upper-right/back, it shades the right portion of the bed.
addShadeCaster(block(
0.38, 7.8, 5.55, returnMaterial,
4.55, 3.0, 0.05
));
// Window frame.
addShadeCaster(block(
0.28, 5.55, 0.32, frameMaterial,
-4.20, 3.72, -2.48
));
addShadeCaster(block(
0.28, 5.55, 0.32, frameMaterial,
4.20, 3.72, -2.48
));
addShadeCaster(block(
8.65, 0.28, 0.32, frameMaterial,
0, 1.10, -2.48
));
addShadeCaster(block(
8.65, 0.28, 0.32, frameMaterial,
0, 6.36, -2.48
));
// This vertical mullion places a shadow stripe over seedling A.
addShadeCaster(block(
0.28, 5.28, 0.32, frameMaterial,
0.65, 3.72, -2.48
));
// This transom creates a different stripe over seedling B.
addShadeCaster(block(
8.42, 0.22, 0.32, frameMaterial,
0, 4.18, -2.48
));
const glassMaterial = new THREE.MeshBasicMaterial({
color: 0xb9e6ef,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
});
block(
8.15, 5.18, 0.025,
glassMaterial,
0, 3.72, -2.57,
scene, false, false
);
// ------------------------------------------------------------
// Movable voxel lamp
// ------------------------------------------------------------
const lampState = {
x: -3.7,
y: 3.8,
z: 3.5,
power: 60
};
const lampStand = new THREE.Group();
scene.add(lampStand);
const lampMetal = new THREE.MeshStandardMaterial({
color: 0x4c5551,
roughness: 0.58,
metalness: 0.35
});
const lampShadeMaterial = new THREE.MeshStandardMaterial({
color: 0xd99335,
roughness: 0.72
});
const bulbMaterial = new THREE.MeshStandardMaterial({
color: 0xffe1a1,
emissive: 0xffa43b,
emissiveIntensity: 2.6,
roughness: 0.4
});
block(
0.78, 0.12, 0.78, lampMetal,
0, 0.06, 0, lampStand, false, true
);
const lampPole = block(
0.10, 1.0, 0.10, lampMetal,
0, 0.5, 0, lampStand, false, true
);
const lampHead = new THREE.Group();
lampStand.add(lampHead);
const lampPickMeshes = [];
lampPickMeshes.push(block(
0.70, 0.44, 0.56, lampShadeMaterial,
0, 0, 0, lampHead, false, true
));
lampPickMeshes.push(block(
0.40, 0.22, 0.40, bulbMaterial,
0, -0.25, 0, lampHead, false, false
));
block(
0.80, 0.08, 0.64, lampMetal,
0, 0.25, 0, lampHead, false, true
);
function setLampPosition(x, y, z) {
lampState.x = THREE.MathUtils.clamp(x, -5, 5);
lampState.y = THREE.MathUtils.clamp(y, 2, 6);
lampState.z = THREE.MathUtils.clamp(z, -1, 5);
const height = lampState.y - SOIL_TOP;
lampStand.position.set(lampState.x, SOIL_TOP, lampState.z);
lampPole.scale.y = height;
lampPole.position.y = height * 0.5;
lampHead.position.set(0, height, 0);
lampLight.position.set(lampState.x, lampState.y, lampState.z);
}
function setLampPower(value) {
lampState.power = THREE.MathUtils.clamp(Number(value), 0, 100);
lampLight.intensity = 95 * lampState.power / 100;
}
setLampPosition(
lampState.x,
lampState.y,
lampState.z
);
setLampPower(lampState.power);
// ------------------------------------------------------------
// Seedlings and fixed root systems
// ------------------------------------------------------------
const plantDefinitions = [
{
id: "A",
accent: 0x58d5ff,
newColor: 0x87d45b,
leafColor: 0xa8e36c,
root: new THREE.Vector3(-2.05, SOIL_TOP + 0.025, 0.25),
response: 0.55,
bias: new THREE.Vector3(-0.035, 0, 0.025),
offsets: [
[0, 0, 0],
[-0.03, 0.17, -0.02],
[0.02, 0.34, 0.03],
[-0.05, 0.51, 0],
[0, 0.68, 0.05]
]
},
{
id: "B",
accent: 0xffca68,
newColor: 0xa7d957,
leafColor: 0xc1eb74,
root: new THREE.Vector3(0, SOIL_TOP + 0.025, -0.15),
response: 0.82,
bias: new THREE.Vector3(0.055, 0, -0.045),
offsets: [
[0, 0, 0],
[0.04, 0.17, 0.02],
[-0.03, 0.34, -0.03],
[0.06, 0.51, -0.01],
[0.02, 0.68, 0.02]
]
},
{
id: "C",
accent: 0xff78c8,
newColor: 0x65ca7e,
leafColor: 0x8bdc91,
root: new THREE.Vector3(2.05, SOIL_TOP + 0.025, 0.18),
response: 0.43,
bias: new THREE.Vector3(-0.025, 0, -0.055),
offsets: [
[0, 0, 0],
[-0.03, 0.16, 0.02],
[0.04, 0.32, -0.02],
[0.01, 0.48, 0.03],
[0.05, 0.64, 0]
]
}
];
function makePlant(definition) {
const accentMaterial = new THREE.MeshStandardMaterial({
color: definition.accent,
emissive: definition.accent,
emissiveIntensity: 0.12,
roughness: 0.58
});
const newStemMaterial = new THREE.MeshStandardMaterial({
color: definition.newColor,
roughness: 0.78
});
const leafMaterial = new THREE.MeshStandardMaterial({
color: definition.leafColor,
roughness: 0.76
});
const rootGroup = new THREE.Group();
rootGroup.position.copy(definition.root);
rootGroup.visible = false;
scene.add(rootGroup);
// Fixed central root.
orientedSegment(
rootGroup,
new THREE.Vector3(0, -0.02, 0),
new THREE.Vector3(0, -1, 0),
0.42,
0.065,
0.065,
rootMaterial
);
// Fixed lateral roots.
const rootBranches = [
[new THREE.Vector3(0, -0.18, 0), new THREE.Vector3(-0.34, -0.35, 0.10)],
[new THREE.Vector3(0, -0.22, 0), new THREE.Vector3(0.29, -0.31, -0.13)],
[new THREE.Vector3(0, -0.29, 0), new THREE.Vector3(-0.10, -0.49, -0.24)],
[new THREE.Vector3(0, -0.31, 0), new THREE.Vector3(0.16, -0.46, 0.22)]
];
for (const [a, b] of rootBranches) {
const direction = b.clone().sub(a);
orientedSegment(
rootGroup,
a,
direction,
direction.length(),
0.045,
0.045,
rootMaterial
);
}
// Always-visible root anchor marker.
block(
0.22, 0.075, 0.22,
accentMaterial,
definition.root.x,
definition.root.y + 0.025,
definition.root.z,
scene, false, true
);
const oldGroup = new THREE.Group();
oldGroup.name = `Seedling ${definition.id}: existing stem`;
scene.add(oldGroup);
const newGroup = new THREE.Group();
newGroup.name = `Seedling ${definition.id}: newly added growth`;
scene.add(newGroup);
const oldPoints = definition.offsets.map(offset =>
definition.root.clone().add(new THREE.Vector3(...offset))
);
for (let i = 0; i < oldPoints.length - 1; i++) {
const a = oldPoints[i];
const b = oldPoints[i + 1];
const direction = b.clone().sub(a);
orientedSegment(
oldGroup,
a,
direction,
direction.length(),
0.115,
0.115,
oldStemMaterial
);
}
const oldTrace = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(oldPoints),
new THREE.LineDashedMaterial({
color: definition.accent,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.42,
depthTest: false
})
);
oldTrace.computeLineDistances();
oldTrace.renderOrder = 5;
scene.add(oldTrace);
const newTrace = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(oldPoints),
new THREE.LineBasicMaterial({
color: definition.accent,
transparent: true,
opacity: 0.88,
depthTest: false
})
);
newTrace.renderOrder = 6;
scene.add(newTrace);
const oldTip = oldPoints[oldPoints.length - 1].clone();
const oldLastDirection = oldPoints[oldPoints.length - 1]
.clone()
.sub(oldPoints[oldPoints.length - 2])
.normalize();
return {
...definition,
rootGroup,
oldGroup,
newGroup,
oldTrace,
newTrace,
oldPoints,
tracePoints: oldPoints.map(p => p.clone()),
tip: oldTip.clone(),
oldTip,
prevDirection: oldLastDirection.clone(),
growthCount: 0,
newSegments: [],
lastSource: "—"
};
}
const plants = plantDefinitions.map(makePlant);
// ------------------------------------------------------------
// Qualitative light sampling and incremental growth
// ------------------------------------------------------------
const shadowRay = new THREE.Raycaster();
const sunDirection = new THREE.Vector3()
.subVectors(sun.position, sun.target.position)
.normalize();
function hasLineOfSight(point, direction) {
shadowRay.set(point, direction);
shadowRay.near = 0.035;
shadowRay.far = 40;
return shadowRay.intersectObjects(shadeCasters, false).length === 0;
}
function sampleIllumination(point) {
const lampDirection = lampLight.position.clone().sub(point);
const lampDistance = lampDirection.length();
lampDirection.normalize();
const sunClear = hasLineOfSight(point, sunDirection);
const lampClear = hasLineOfSight(point, lampDirection);
// Directional window light is reduced when the frame/return blocks it.
const sunWeight = sunClear ? 2.8 : 0.055;
// The movable point lamp is strongest near the seedlings.
const lampWeight =
(lampState.power / 100) *
45 / (lampDistance * lampDistance + 1) *
(lampClear ? 1 : 0.04);
const blend = sunDirection.clone().multiplyScalar(sunWeight)
.addScaledVector(lampDirection, lampWeight);
let source;
if (lampWeight > sunWeight * 1.08) {
source = "lamp";
} else if (sunWeight > lampWeight * 1.08) {
source = "sun";
} else {
source = "mixed";
}
return { blend, source };
}
function updateSourceChip(plant) {
const chip = document.getElementById(`source${plant.id}`);
chip.textContent = plant.lastSource;
chip.className = "source-chip";
if (plant.lastSource === "lamp") chip.classList.add("lamp");
if (plant.lastSource === "sun") chip.classList.add("sun");
}
function updateTrace(plant) {
plant.newTrace.geometry.dispose();
plant.newTrace.geometry = new THREE.BufferGeometry()
.setFromPoints(plant.tracePoints);
}
function addNewSegment(plant) {
const illumination = sampleIllumination(plant.tip);
const horizontal = illumination.blend.clone();
horizontal.y = 0;
const blendMagnitude = horizontal.length();
if (blendMagnitude > 0.0001) {
horizontal.normalize();
} else {
horizontal.set(
Math.sin(plant.growthCount * 1.4 + plant.response * 4),
0,
Math.cos(plant.growthCount * 1.7 + plant.response * 3)
);
}
const pull = Math.min(1.1, blendMagnitude) *
(0.50 + plant.response * 0.64);
const wander = new THREE.Vector3(
Math.sin((plant.growthCount + 1) * 1.73 + plant.response * 5),
Math.sin((plant.growthCount + 1) * 0.91) * 0.12,
Math.cos((plant.growthCount + 1) * 1.37 + plant.response * 4)
).multiplyScalar(0.045);
const desired = new THREE.Vector3()
.addScaledVector(UP, 1.05)
.addScaledVector(horizontal, pull)
.addScaledVector(plant.prevDirection, 0.26)
.add(plant.bias)
.add(wander);
desired.y = Math.max(0.24, desired.y);
desired.normalize();
const length = 0.135 + plant.response * 0.035;
const base = plant.tip.clone();
const nextTip = base.clone().addScaledVector(desired, length);
const mesh = orientedSegment(
plant.newGroup,
base,
desired,
length,
0.105,
0.105,
plant.newColor ? plant.newStemMaterial : plant.newStemMaterial
);
// The segment starts at its base and reveals along its length.
mesh.scale.y = 0.001;
const segment = {
mesh,
length,
reveal: 0,
leaves: []
};
// Voxel leaves are added only to newly generated growth.
if (plant.growthCount % 3 === 2) {
const side = new THREE.Vector3(-desired.z, 0, desired.x);
if (side.lengthSq() < 0.001) side.set(1, 0, 0);
side.normalize();
for (const sign of [-1, 1]) {
const leaf = new THREE.Mesh(unitBox, plant.leafMaterial);
leaf.position.copy(nextTip)
.addScaledVector(side, sign * 0.13)
.add(new THREE.Vector3(0, -0.015, 0));
leaf.rotation.set(
0,
plant.response * 2.2 + sign * 0.38,
sign * 0.28
);
leaf.scale.set(0, 0, 0);
leaf.castShadow = true;
plant.newGroup.add(leaf);
segment.leaves.push({
mesh: leaf,
baseScale: new THREE.Vector3(0.27, 0.065, 0.135)
});
}
}
plant.newSegments.push(segment);
plant.tip.copy(nextTip);
plant.prevDirection.copy(desired);
plant.growthCount++;
plant.tracePoints.push(nextTip.clone());
plant.lastSource = illumination.source;
updateTrace(plant);
updateSourceChip(plant);
}
function animateNewSegments(delta) {
for (const plant of plants) {
for (const segment of plant.newSegments) {
segment.reveal = Math.min(1, segment.reveal + delta * 4.5);
const eased = 1 - Math.pow(1 - segment.reveal, 3);
segment.mesh.scale.y = Math.max(0.001, segment.length * eased);
for (const leaf of segment.leaves) {
leaf.mesh.scale.copy(leaf.baseScale).multiplyScalar(eased);
}
}
}
}
// ------------------------------------------------------------
// Time-lapse controls
// ------------------------------------------------------------
const MAX_STEPS = 30;
const GROWTH_INTERVAL = 1.25;
let running = true;
let growthStep = 0;
let simulationAccumulator = 0;
let playbackSpeed = 1;
const playButton = document.getElementById("playBtn");
const resetButton = document.getElementById("resetBtn");
const speedInput = document.getElementById("speed");
const speedValue = document.getElementById("speedValue");
const speedOutput = document.getElementById("speedOutput");
const progressFill = document.getElementById("progressFill");
const progressText = document.getElementById("progressText");
function updateProgress() {
const progress = growthStep / MAX_STEPS;
progressFill.style.width = `${progress * 100}%`;
progressText.textContent = `${growthStep} / ${MAX_STEPS}`;
}
function resetSimulation() {
growthStep = 0;
simulationAccumulator = 0;
for (const plant of plants) {
plant.newGroup.clear();
plant.newSegments.length = 0;
plant.tip.copy(plant.oldTip);
plant.prevDirection.copy(plant.oldTip)
.sub(plant.oldPoints[plant.oldPoints.length - 2])
.normalize();
plant.growthCount = 0;
plant.lastSource = "—";
plant.tracePoints = plant.oldPoints.map(p => p.clone());
updateTrace(plant);
updateSourceChip(plant);
}
running = true;
playButton.textContent = "Pause";
updateProgress();
}
playButton.addEventListener("click", () => {
if (growthStep >= MAX_STEPS) {
resetSimulation();
return;
}
running = !running;
playButton.textContent = running ? "Pause" : "Resume";
});
resetButton.addEventListener("click", resetSimulation);
speedInput.addEventListener("input", () => {
playbackSpeed = Number(speedInput.value);
speedValue.textContent = `${playbackSpeed.toFixed(1)}×`;
speedOutput.textContent = `${playbackSpeed.toFixed(1)}×`;
});
window.addEventListener("keydown", event => {
if (event.code === "Space" && event.target.tagName !== "INPUT") {
event.preventDefault();
playButton.click();
}
});
// ------------------------------------------------------------
// Lamp UI and comparison toggles
// ------------------------------------------------------------
const lampX = document.getElementById("lampX");
const lampY = document.getElementById("lampY");
const lampZ = document.getElementById("lampZ");
const lampPower = document.getElementById("lampPower");
const lampXOutput = document.getElementById("lampXOutput");
const lampYOutput = document.getElementById("lampYOutput");
const lampZOutput = document.getElementById("lampZOutput");
const lampPowerOutput = document.getElementById("lampPowerOutput");
function updateLampOutputs() {
lampX.value = lampState.x;
lampY.value = lampState.y;
lampZ.value = lampState.z;
lampPower.value = lampState.power;
lampXOutput.textContent = lampState.x.toFixed(1);
lampYOutput.textContent = lampState.y.toFixed(1);
lampZOutput.textContent = lampState.z.toFixed(1);
lampPowerOutput.textContent = `${Math.round(lampState.power)}%`;
}
lampX.addEventListener("input", () => {
setLampPosition(
Number(lampX.value),
lampState.y,
lampState.z
);
updateLampOutputs();
});
lampY.addEventListener("input", () => {
setLampPosition(
lampState.x,
Number(lampY.value),
lampState.z
);
updateLampOutputs();
});
lampZ.addEventListener("input", () => {
setLampPosition(
lampState.x,
lampState.y,
Number(lampZ.value)
);
updateLampOutputs();
});
lampPower.addEventListener("input", () => {
setLampPower(lampPower.value);
updateLampOutputs();
});
const rootsToggle = document.getElementById("rootsToggle");
const tracesToggle = document.getElementById("tracesToggle");
rootsToggle.addEventListener("change", () => {
const reveal = rootsToggle.checked;
for (const material of soilMaterials) {
material.transparent = reveal;
material.opacity = reveal ? 0.25 : 1;
material.depthWrite = !reveal;
material.needsUpdate = true;
}
for (const plant of plants) {
plant.rootGroup.visible = reveal;
}
});
tracesToggle.addEventListener("change", () => {
const visible = tracesToggle.checked;
for (const plant of plants) {
plant.oldTrace.visible = visible;
plant.newTrace.visible = visible;
}
});
// ------------------------------------------------------------
// Direct camera controls and direct lamp dragging
// ------------------------------------------------------------
const pointer = new THREE.Vector2();
const pointerRay = new THREE.Raycaster();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), -lampState.y);
let orbiting = false;
let draggingLamp = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
updatePointer(event);
pointerRay.setFromCamera(pointer, camera);
const lampHits = pointerRay.intersectObjects(lampPickMeshes, false);
draggingLamp = lampHits.length > 0;
orbiting = !draggingLamp;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = draggingLamp ? "grabbing" : "move";
});
renderer.domElement.addEventListener("pointermove", event => {
updatePointer(event);
if (draggingLamp) {
pointerRay.setFromCamera(pointer, camera);
dragPlane.constant = -lampState.y;
const intersection = new THREE.Vector3();
if (pointerRay.ray.intersectPlane(dragPlane, intersection)) {
setLampPosition(
intersection.x,
lampState.y,
intersection.z
);
updateLampOutputs();
}
return;
}
if (orbiting) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
cameraYaw -= dx * 0.0055;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.0045,
0.12,
1.08
);
updateCamera();
}
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
function endPointer() {
orbiting = false;
draggingLamp = false;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
8.2,
19
);
updateCamera();
}, { passive: false });
// ------------------------------------------------------------
// Projected labels
// ------------------------------------------------------------
const labelElements = {
A: document.getElementById("labelA"),
B: document.getElementById("labelB"),
C: document.getElementById("labelC")
};
const lampLabel = document.getElementById("lampLabel");
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function updateLabels() {
for (const plant of plants) {
positionLabel(
labelElements[plant.id],
plant.tip.clone().add(new THREE.Vector3(0, 0.14, 0))
);
}
const lampWorld = new THREE.Vector3();
lampHead.getWorldPosition(lampWorld);
positionLabel(lampLabel, lampWorld.add(new THREE.Vector3(0, 0.48, 0)));
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
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 delta = Math.min(clock.getDelta(), 0.05);
if (running && growthStep < MAX_STEPS) {
simulationAccumulator += delta * playbackSpeed;
while (
simulationAccumulator >= GROWTH_INTERVAL &&
growthStep < MAX_STEPS
) {
simulationAccumulator -= GROWTH_INTERVAL;
for (const plant of plants) {
addNewSegment(plant);
}
growthStep++;
updateProgress();
}
if (growthStep >= MAX_STEPS) {
running = false;
playButton.textContent = "Replay";
}
}
animateNewSegments(delta);
updateLabels();
renderer.render(scene, camera);
}
updateProgress();
updateLampOutputs();
animate();
</script>
</body>
</html>
```