Prompt specification
Build or customize the Three.js 3D scene "Seedling directional-light diagram" 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: Seedling directional-light diagram
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/seedling-directional-light-diagram-x0761
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows plant phototropism. Show a young stem beside a single side lamp, with a straight reference stem and growth-direction arrow; distinguish gradual growth from immediate mechanical bending. Stylized solid forms.
Working Three.js Starter (seedling-directional-light-diagram-x0761):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Phototropism — Growing Toward Light</title>
<style>
:root {
--ink: #193b37;
--muted: #637773;
--paper: #f3efe6;
--green: #4c9b67;
--green-light: #b9d98d;
--teal: #39777a;
--amber: #dc8a3d;
--yellow: #f3c84b;
--panel: rgba(255, 253, 247, 0.88);
--line: rgba(25, 59, 55, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
background: var(--panel);
border: 1px solid var(--line);
border-radius: 20px;
box-shadow: 0 18px 50px rgba(35, 58, 52, 0.10);
backdrop-filter: blur(16px);
}
.title-panel {
top: 24px;
left: 24px;
width: 350px;
padding: 24px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 3px;
border-radius: 999px;
background: var(--green);
content: "";
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(42px, 4.4vw, 62px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.92;
}
.intro {
margin: 17px 0 21px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.intro strong {
color: var(--ink);
font-weight: 750;
}
.control-row {
display: flex;
align-items: center;
gap: 8px;
}
.control-button {
min-height: 38px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
cursor: pointer;
font-size: 12px;
font-weight: 750;
transition: transform 160ms ease, background 160ms ease;
}
.control-button:hover {
transform: translateY(-1px);
background: white;
}
.control-button.primary {
border-color: var(--ink);
background: var(--ink);
color: white;
}
.timeline {
display: grid;
grid-template-columns: 1fr auto;
gap: 7px 12px;
align-items: center;
margin-top: 18px;
}
.timeline label {
grid-column: 1 / -1;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#growth-slider {
width: 100%;
height: 5px;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(
to right,
var(--green) 0%,
var(--green) var(--progress, 28%),
rgba(25, 59, 55, 0.13) var(--progress, 28%),
rgba(25, 59, 55, 0.13) 100%
);
cursor: pointer;
}
#growth-slider::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
border: 4px solid white;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 8px rgba(25, 59, 55, 0.22);
}
#growth-slider::-moz-range-thumb {
width: 11px;
height: 11px;
border: 4px solid white;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 8px rgba(25, 59, 55, 0.22);
}
#progress-value {
min-width: 34px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
text-align: right;
}
.compare-panel {
bottom: 24px;
left: 24px;
width: 390px;
padding: 18px;
}
.compare-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-bottom: 13px;
}
.compare-heading h2 {
margin: 0;
font-size: 13px;
font-weight: 850;
letter-spacing: 0.02em;
}
.switch {
display: inline-flex;
align-items: center;
gap: 8px;
color: var(--muted);
cursor: pointer;
font-size: 10px;
font-weight: 750;
white-space: nowrap;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: relative;
width: 32px;
height: 18px;
border-radius: 999px;
background: rgba(25, 59, 55, 0.18);
transition: background 180ms ease;
}
.switch-track::after {
position: absolute;
top: 3px;
left: 3px;
width: 12px;
height: 12px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
content: "";
transition: transform 180ms ease;
}
.switch input:checked + .switch-track {
background: var(--amber);
}
.switch input:checked + .switch-track::after {
transform: translateX(14px);
}
.compare-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.mechanic {
min-width: 0;
padding: 11px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.55);
}
.mechanic h3 {
display: flex;
align-items: center;
gap: 7px;
margin: 0 0 4px;
font-size: 11px;
font-weight: 850;
}
.mechanic h3::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
content: "";
}
.mechanic.mechanical h3::before {
background: var(--amber);
}
.mechanic p {
margin: 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.diagram {
display: block;
width: 100%;
height: 58px;
margin: 6px 0 7px;
}
.legend-panel {
right: 24px;
bottom: 24px;
padding: 15px 17px;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 7px 0;
color: var(--muted);
font-size: 11px;
font-weight: 650;
}
.legend-row:first-child {
margin-top: 0;
}
.legend-row:last-child {
margin-bottom: 0;
}
.swatch {
width: 17px;
height: 7px;
border-radius: 999px;
background: var(--green);
}
.swatch.reference {
background: var(--teal);
}
.swatch.light {
width: 8px;
height: 8px;
margin: 0 4px;
border-radius: 50%;
background: var(--yellow);
box-shadow: 0 0 0 4px rgba(243, 200, 75, 0.18);
}
.camera-tools {
position: fixed;
z-index: 11;
top: 24px;
right: 24px;
display: flex;
align-items: center;
gap: 8px;
}
.camera-button {
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
color: var(--ink);
box-shadow: 0 10px 30px rgba(35, 58, 52, 0.08);
cursor: pointer;
font-size: 11px;
font-weight: 800;
}
.camera-hint {
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 253, 247, 0.72);
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.world-label {
position: fixed;
z-index: 5;
padding: 7px 10px;
border: 1px solid rgba(25, 59, 55, 0.13);
border-radius: 999px;
background: rgba(255, 253, 247, 0.88);
color: var(--ink);
box-shadow: 0 6px 22px rgba(35, 58, 52, 0.08);
pointer-events: none;
transform: translate(-50%, -100%);
white-space: nowrap;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label::after {
position: absolute;
bottom: -12px;
left: 50%;
width: 1px;
height: 11px;
background: rgba(25, 59, 55, 0.28);
content: "";
}
.world-label.growth {
border-color: rgba(76, 155, 103, 0.32);
color: #34784e;
}
.world-label.reference {
border-color: rgba(57, 119, 122, 0.30);
color: var(--teal);
}
.world-label.lamp {
border-color: rgba(243, 200, 75, 0.55);
color: #8b6a13;
}
.world-label.mechanical {
border-color: rgba(220, 138, 61, 0.50);
color: #a05b1c;
}
@media (max-width: 900px) {
.title-panel {
top: 12px;
left: 12px;
width: min(340px, calc(100vw - 24px));
padding: 18px;
}
.title-panel h1 {
font-size: 40px;
}
.intro {
margin: 12px 0 14px;
font-size: 12px;
}
.compare-panel {
bottom: 12px;
left: 12px;
width: min(390px, calc(100vw - 24px));
padding: 13px;
}
.legend-panel,
.camera-hint {
display: none;
}
.camera-tools {
top: auto;
right: 12px;
bottom: 12px;
}
}
@media (max-width: 560px) {
.compare-grid {
gap: 6px;
}
.mechanic {
padding: 8px;
}
.mechanic p {
font-size: 9px;
}
.diagram {
height: 45px;
}
.intro {
display: none;
}
.timeline {
margin-top: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive three-dimensional plant phototropism demonstration"></canvas>
<section class="panel title-panel">
<div class="eyebrow">Botany lab · Light and growth</div>
<h1>Phototropism</h1>
<p class="intro">
A young stem finds a side light by changing the direction of
<strong>new growth</strong>. Watch the growth marks become a curve
while the straight reference keeps its original axis.
</p>
<div class="control-row">
<button id="play-button" class="control-button primary" type="button">
Pause growth
</button>
<button id="restart-button" class="control-button" type="button">
Restart
</button>
</div>
<div class="timeline">
<label for="growth-slider">Growth sequence</label>
<input
id="growth-slider"
type="range"
min="0"
max="100"
value="28"
aria-label="Growth sequence"
/>
<span id="progress-value">28%</span>
</div>
</section>
<section class="panel compare-panel">
<div class="compare-heading">
<h2>Two different kinds of bending</h2>
<label class="switch">
<input id="mechanical-toggle" type="checkbox" />
<span class="switch-track"></span>
Instant-bend ghost
</label>
</div>
<div class="compare-grid">
<article class="mechanic">
<svg class="diagram" viewBox="0 0 120 60" aria-hidden="true">
<defs>
<marker
id="green-arrow"
markerWidth="6"
markerHeight="6"
refX="5"
refY="3"
orient="auto"
>
<path d="M0,0 L6,3 L0,6 Z" fill="#4c9b67" />
</marker>
</defs>
<path
d="M18 54 C18 37 30 25 48 22 C65 19 79 12 99 9"
fill="none"
stroke="#4c9b67"
stroke-width="6"
stroke-linecap="round"
marker-end="url(#green-arrow)"
/>
<g fill="#f3efe6" stroke="#4c9b67" stroke-width="3">
<circle cx="18" cy="54" r="4" />
<circle cx="31" cy="33" r="4" />
<circle cx="49" cy="22" r="4" />
<circle cx="72" cy="16" r="4" />
</g>
</svg>
<h3>Gradual growth</h3>
<p>
Each new segment turns toward the lamp. Older sections keep
the angle they grew with.
</p>
</article>
<article class="mechanic mechanical">
<svg class="diagram" viewBox="0 0 120 60" aria-hidden="true">
<defs>
<marker
id="amber-arrow"
markerWidth="6"
markerHeight="6"
refX="5"
refY="3"
orient="auto"
>
<path d="M0,0 L6,3 L0,6 Z" fill="#dc8a3d" />
</marker>
</defs>
<path
d="M24 55 L24 25"
fill="none"
stroke="#b8b6ad"
stroke-width="4"
stroke-dasharray="5 5"
/>
<path
d="M24 55 C24 35 31 24 48 23 C66 22 80 17 99 14"
fill="none"
stroke="#dc8a3d"
stroke-width="6"
stroke-linecap="round"
marker-end="url(#amber-arrow)"
/>
<circle cx="24" cy="55" r="6" fill="#f3efe6" stroke="#dc8a3d" stroke-width="3" />
</svg>
<h3>Mechanical bending</h3>
<p>
The existing stem changes shape all at once. It can lean
without any new growth being added.
</p>
</article>
</div>
</section>
<section class="panel legend-panel" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch"></span>
Growing stem and growth marks
</div>
<div class="legend-row">
<span class="swatch reference"></span>
Straight reference stem
</div>
<div class="legend-row">
<span class="swatch light"></span>
Single side light
</div>
</section>
<div class="camera-tools">
<div class="camera-hint">Drag to orbit · Scroll to zoom</div>
<button id="camera-reset" class="camera-button" type="button">
Reset view
</button>
</div>
<div id="growth-label" class="world-label growth">Growth direction</div>
<div id="reference-label" class="world-label reference">Straight reference</div>
<div id="lamp-label" class="world-label lamp">Side light</div>
<div id="mechanical-label" class="world-label mechanical" style="display:none">
Instant mechanical bend
</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.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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3efe6);
scene.fog = new THREE.Fog(0xf3efe6, 15, 27);
const camera = new THREE.PerspectiveCamera(
37,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(-0.35, 1.65, 0),
yaw: -0.025,
pitch: 0.09,
radius: 10.7
};
function updateCamera() {
const { target, yaw, pitch, radius } = cameraState;
camera.position.set(
target.x + Math.sin(yaw) * Math.cos(pitch) * radius,
target.y + Math.sin(pitch) * radius,
target.z + Math.cos(yaw) * Math.cos(pitch) * radius
);
camera.lookAt(target);
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xfff9e9, 0x7b8c82, 2.2));
const keyLight = new THREE.DirectionalLight(0xfff5dc, 3.0);
keyLight.position.set(-4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e0dd, 1.2);
fillLight.position.set(4, 4, -5);
scene.add(fillLight);
// Shared materials
const materials = {
desk: new THREE.MeshStandardMaterial({
color: 0xe8e2d5,
roughness: 0.93
}),
mainPot: new THREE.MeshStandardMaterial({
color: 0xd87855,
roughness: 0.8
}),
mainPotRim: new THREE.MeshStandardMaterial({
color: 0xe58a64,
roughness: 0.75
}),
referencePot: new THREE.MeshStandardMaterial({
color: 0x789c9a,
roughness: 0.85
}),
soil: new THREE.MeshStandardMaterial({
color: 0x554238,
roughness: 1
}),
stem: new THREE.MeshStandardMaterial({
color: 0x4c9b67,
roughness: 0.68
}),
stemTip: new THREE.MeshStandardMaterial({
color: 0x78b66d,
roughness: 0.66
}),
growthMark: new THREE.MeshStandardMaterial({
color: 0xb8d98d,
roughness: 0.58
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x5da36b,
roughness: 0.72
}),
leafDark: new THREE.MeshStandardMaterial({
color: 0x3f825b,
roughness: 0.76
}),
referenceStem: new THREE.MeshStandardMaterial({
color: 0x39777a,
roughness: 0.72
}),
lamp: new THREE.MeshStandardMaterial({
color: 0x274b49,
roughness: 0.45,
metalness: 0.22
}),
lampTrim: new THREE.MeshStandardMaterial({
color: 0xf2bd4e,
roughness: 0.36,
metalness: 0.18
}),
bulb: new THREE.MeshStandardMaterial({
color: 0xffed91,
emissive: 0xffc83d,
emissiveIntensity: 2.5,
roughness: 0.28
}),
mechanical: new THREE.MeshStandardMaterial({
color: 0xdc8a3d,
roughness: 0.68,
transparent: true,
opacity: 0.58,
depthWrite: false
})
};
// Desk
const desk = new THREE.Mesh(
new THREE.PlaneGeometry(30, 24),
materials.desk
);
desk.rotation.x = -Math.PI / 2;
desk.position.y = -0.025;
desk.receiveShadow = true;
scene.add(desk);
function addCylinderBetween(a, b, radius, material, radialSegments = 14) {
const direction = new THREE.Vector3().subVectors(b, a);
const geometry = new THREE.CylinderGeometry(
radius,
radius,
direction.length(),
radialSegments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function addPot(x, height, topRadius, bottomRadius, material, rimMaterial) {
const pot = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, height, 36),
material
);
pot.position.set(x, height / 2, 0);
pot.castShadow = true;
pot.receiveShadow = true;
scene.add(pot);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(topRadius, 0.055, 12, 36),
rimMaterial
);
rim.position.set(x, height - 0.015, 0);
rim.rotation.x = Math.PI / 2;
rim.castShadow = true;
scene.add(rim);
const soil = new THREE.Mesh(
new THREE.CylinderGeometry(
topRadius - 0.07,
topRadius - 0.07,
0.055,
36
),
materials.soil
);
soil.position.set(x, height - 0.015, 0);
soil.receiveShadow = true;
scene.add(soil);
const coaster = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius + 0.19, topRadius + 0.19, 0.035, 40),
new THREE.MeshStandardMaterial({
color: 0xf7f3e9,
roughness: 0.94
})
);
coaster.position.set(x, 0.012, 0);
coaster.receiveShadow = true;
scene.add(coaster);
return new THREE.Vector3(x, height + 0.04, 0);
}
const mainBase = addPot(
0,
0.74,
0.47,
0.34,
materials.mainPot,
materials.mainPotRim
);
const referenceBase = addPot(
-1.38,
0.64,
0.39,
0.29,
materials.referencePot,
materials.referencePot
);
// Lamp
const lampPosition = new THREE.Vector3(3.55, 3.22, 0);
const lampDirection = new THREE.Vector3(-0.98, -0.18, 0).normalize();
const bulbPosition = lampPosition.clone().addScaledVector(lampDirection, 0.25);
const lampBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.54, 0.62, 0.18, 36),
materials.lamp
);
lampBase.position.set(3.68, 0.1, 0);
lampBase.castShadow = true;
lampBase.receiveShadow = true;
scene.add(lampBase);
const lampBaseRing = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.035, 10, 36),
materials.lampTrim
);
lampBaseRing.position.set(3.68, 0.2, 0);
lampBaseRing.rotation.x = Math.PI / 2;
scene.add(lampBaseRing);
addCylinderBetween(
new THREE.Vector3(3.68, 0.2, 0),
new THREE.Vector3(4.02, 2.68, 0.02),
0.07,
materials.lamp
);
const elbow = new THREE.Mesh(
new THREE.SphereGeometry(0.14, 18, 12),
materials.lamp
);
elbow.position.set(4.02, 2.68, 0.02);
elbow.castShadow = true;
scene.add(elbow);
const shadeBack = lampPosition.clone().addScaledVector(lampDirection, -0.34);
addCylinderBetween(
new THREE.Vector3(4.02, 2.68, 0.02),
shadeBack,
0.065,
materials.lamp
);
const shadeGroup = new THREE.Group();
shadeGroup.position.copy(lampPosition);
shadeGroup.quaternion.setFromUnitVectors(
new THREE.Vector3(0, -1, 0),
lampDirection
);
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.17, 0.53, 0.68, 36, 1, true),
materials.lamp
);
shade.castShadow = true;
shadeGroup.add(shade);
const shadeRim = new THREE.Mesh(
new THREE.TorusGeometry(0.52, 0.035, 10, 36),
materials.lampTrim
);
shadeRim.position.y = -0.34;
shadeRim.rotation.x = Math.PI / 2;
shadeGroup.add(shadeRim);
const bulb = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 24, 16),
materials.bulb
);
bulb.position.y = -0.22;
shadeGroup.add(bulb);
scene.add(shadeGroup);
const spotTarget = new THREE.Object3D();
spotTarget.position.set(0, 1.7, 0);
scene.add(spotTarget);
const spotLight = new THREE.SpotLight(
0xffd85b,
85,
10,
0.46,
0.55,
1.4
);
spotLight.position.copy(bulbPosition);
spotLight.target = spotTarget;
spotLight.castShadow = true;
spotLight.shadow.mapSize.set(1024, 1024);
spotLight.shadow.bias = -0.0005;
scene.add(spotLight);
const bulbGlow = new THREE.PointLight(0xffcf4f, 18, 5, 2);
bulbGlow.position.copy(bulbPosition);
scene.add(bulbGlow);
// A soft, solid translucent cone makes the side-light direction readable.
const beamEnd = new THREE.Vector3(0, 1.45, 0);
const beamAxis = bulbPosition.clone().sub(beamEnd);
const beamLength = beamAxis.length();
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.95, beamLength, 36, 1, true),
new THREE.MeshBasicMaterial({
color: 0xffd35b,
transparent: true,
opacity: 0.065,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending
})
);
beam.position.copy(bulbPosition).add(beamEnd).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
beamAxis.normalize()
);
scene.add(beam);
const photons = [];
const photonGeometry = new THREE.SphereGeometry(0.038, 10, 8);
const photonMaterial = new THREE.MeshBasicMaterial({
color: 0xffda58
});
for (let i = 0; i < 6; i++) {
const photon = new THREE.Mesh(photonGeometry, photonMaterial);
scene.add(photon);
photons.push(photon);
}
// Precompute a growth history. Every segment is integrated separately:
// old segments never move when a new segment is added.
const growthPoints = [mainBase.clone()];
const growthDirections = [];
let currentPoint = mainBase.clone();
let currentAngle = 0;
for (let i = 0; i < 80; i++) {
const toLight = bulbPosition.clone().sub(currentPoint);
const targetAngle = Math.atan2(toLight.x, toLight.y);
const turn = THREE.MathUtils.clamp(
targetAngle - currentAngle,
-0.075,
0.075
);
currentAngle += turn;
const direction = new THREE.Vector3(
Math.sin(currentAngle),
Math.cos(currentAngle),
0
).normalize();
const nextPoint = currentPoint.clone().addScaledVector(direction, 0.07);
growthPoints.push(nextPoint);
growthDirections.push(direction);
currentPoint = nextPoint;
if (currentPoint.y > 3.18) break;
}
const segmentGeometry = new THREE.CylinderGeometry(
0.054,
0.068,
1,
14
);
const stemSegments = [];
for (let i = 0; i < growthDirections.length; i++) {
const a = growthPoints[i];
const b = growthPoints[i + 1];
const direction = b.clone().sub(a);
const taper = 1 - (i / growthDirections.length) * 0.24;
const segment = new THREE.Mesh(segmentGeometry, materials.stem);
segment.position.copy(a).add(b).multiplyScalar(0.5);
segment.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
segment.scale.set(taper, direction.length(), taper);
segment.castShadow = true;
segment.receiveShadow = true;
segment.visible = false;
scene.add(segment);
stemSegments.push(segment);
}
const growthMarkGeometry = new THREE.SphereGeometry(0.072, 14, 10);
const growthMarks = [];
for (let i = 3; i < growthPoints.length - 1; i += 4) {
const mark = new THREE.Mesh(growthMarkGeometry, materials.growthMark);
mark.position.copy(growthPoints[i]);
mark.castShadow = true;
mark.visible = false;
scene.add(mark);
growthMarks.push({ mesh: mark, index: i });
}
const leafGeometry = new THREE.SphereGeometry(1, 18, 10);
const mainLeaves = [];
for (let i = 7; i < growthPoints.length - 3; i += 6) {
const point = growthPoints[i];
const side = ((i / 6) | 0) % 2 === 0 ? 1 : -1;
const leafDirection = new THREE.Vector3(
side * 0.93,
0.36,
side === 1 ? 0.10 : -0.08
).normalize();
const leaf = new THREE.Mesh(
leafGeometry,
i % 12 === 7 ? materials.leafDark : materials.leaf
);
leaf.position.copy(point).addScaledVector(leafDirection, 0.23);
leaf.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
leafDirection
);
leaf.scale.set(0.32, 0.058, 0.13);
leaf.castShadow = true;
leaf.visible = false;
scene.add(leaf);
mainLeaves.push({ mesh: leaf, index: i });
}
const tipBud = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 18, 12),
materials.stemTip
);
tipBud.scale.set(0.82, 1.3, 0.82);
tipBud.castShadow = true;
scene.add(tipBud);
// Straight reference stem
const referenceTop = referenceBase.clone().add(
new THREE.Vector3(0, 2.42, 0)
);
addCylinderBetween(
referenceBase,
referenceTop,
0.057,
materials.referenceStem,
16
);
for (let i = 0; i < 3; i++) {
const y = referenceBase.y + 0.65 + i * 0.62;
const side = i % 2 === 0 ? -1 : 1;
const leaf = new THREE.Mesh(
leafGeometry,
materials.referenceStem
);
leaf.position.set(
referenceBase.x + side * 0.21,
y,
side * 0.025
);
leaf.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(side * 0.94, 0.34, 0).normalize()
);
leaf.scale.set(0.27, 0.052, 0.115);
leaf.castShadow = true;
scene.add(leaf);
}
// Solid arrow helper, implemented without addons.
function makeArrow(color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.5,
emissive: color,
emissiveIntensity: 0.08
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.024, 0.024, 1, 10),
material
);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.085, 0.2, 14),
material
);
group.add(shaft, head);
group.userData = { shaft, head, material };
scene.add(group);
return group;
}
function setArrow(group, origin, direction, length = 0.62) {
const dir = direction.clone().normalize();
const shaftLength = Math.max(0.05, length - 0.17);
const { shaft, head } = group.userData;
shaft.scale.set(1, shaftLength, 1);
shaft.position.copy(origin).addScaledVector(dir, shaftLength / 2);
shaft.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
dir
);
head.position.copy(origin).addScaledVector(dir, shaftLength + 0.07);
head.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
dir
);
}
const growthArrow = makeArrow(0xf1c943);
const referenceArrow = makeArrow(0x39777a);
setArrow(
referenceArrow,
referenceTop,
new THREE.Vector3(0, 1, 0),
0.56
);
// Optional amber ghost: an already-existing stem changing shape at once.
const mechanicalGhost = new THREE.Group();
const mechanicalPoints = [
mainBase.clone().add(new THREE.Vector3(0, 0.03, -0.26)),
mainBase.clone().add(new THREE.Vector3(0, 0.54, -0.26)),
mainBase.clone().add(new THREE.Vector3(0.38, 0.96, -0.26)),
mainBase.clone().add(new THREE.Vector3(1.13, 1.25, -0.26))
];
const mechanicalCurve = new THREE.CatmullRomCurve3(mechanicalPoints);
const mechanicalStem = new THREE.Mesh(
new THREE.TubeGeometry(mechanicalCurve, 32, 0.068, 12, false),
materials.mechanical
);
mechanicalGhost.add(mechanicalStem);
const hinge = new THREE.Mesh(
new THREE.TorusGeometry(0.13, 0.024, 10, 24),
new THREE.MeshStandardMaterial({
color: 0xdc8a3d,
roughness: 0.55
})
);
hinge.position.copy(mainBase).add(new THREE.Vector3(0, 0.03, -0.26));
mechanicalGhost.add(hinge);
mechanicalGhost.visible = false;
scene.add(mechanicalGhost);
// Growth state
let progress = 0.28;
let playing = true;
let visibleSegmentCount = -1;
const durationSeconds = 20;
const slider = document.querySelector("#growth-slider");
const progressValue = document.querySelector("#progress-value");
const playButton = document.querySelector("#play-button");
const restartButton = document.querySelector("#restart-button");
const mechanicalToggle = document.querySelector("#mechanical-toggle");
function syncProgressUI() {
const percentage = Math.round(progress * 100);
slider.value = String(percentage);
slider.style.setProperty("--progress", `${percentage}%`);
progressValue.textContent = `${percentage}%`;
if (progress >= 1) {
playButton.textContent = "Replay growth";
} else {
playButton.textContent = playing ? "Pause growth" : "Resume growth";
}
}
playButton.addEventListener("click", () => {
if (progress >= 1) {
progress = 0;
playing = true;
} else {
playing = !playing;
}
syncProgressUI();
});
restartButton.addEventListener("click", () => {
progress = 0;
playing = true;
syncProgressUI();
});
slider.addEventListener("input", () => {
progress = Number(slider.value) / 100;
playing = false;
syncProgressUI();
});
mechanicalToggle.addEventListener("change", () => {
mechanicalGhost.visible = mechanicalToggle.checked;
document.querySelector("#mechanical-label").style.display =
mechanicalToggle.checked ? "block" : "none";
});
syncProgressUI();
function updateGrowth() {
const count = Math.max(
2,
Math.min(
stemSegments.length,
Math.floor(progress * stemSegments.length) + 1
)
);
if (count !== visibleSegmentCount) {
visibleSegmentCount = count;
stemSegments.forEach((segment, index) => {
segment.visible = index < count;
});
growthMarks.forEach(({ mesh, index }) => {
mesh.visible = index < count;
});
mainLeaves.forEach(({ mesh, index }) => {
mesh.visible = index < count;
});
}
const tipIndex = Math.min(count - 1, growthPoints.length - 1);
const tip = growthPoints[tipIndex];
const direction = growthDirections[tipIndex];
tipBud.position.copy(tip);
tipBud.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction
);
setArrow(
growthArrow,
tip.clone().addScaledVector(direction, 0.04),
direction,
0.68
);
}
// Screen-space labels
const growthLabel = document.querySelector("#growth-label");
const referenceLabel = document.querySelector("#reference-label");
const lampLabel = document.querySelector("#lamp-label");
const mechanicalLabel = document.querySelector("#mechanical-label");
function placeLabel(element, position) {
const projected = position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.visibility = visible ? "visible" : "hidden";
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() {
const tipIndex = Math.max(
0,
Math.min(
visibleSegmentCount - 1,
growthPoints.length - 1
)
);
const tip = growthPoints[tipIndex];
const direction = growthDirections[tipIndex];
placeLabel(
growthLabel,
tip.clone().add(new THREE.Vector3(0.12, 0.62, 0))
);
placeLabel(
referenceLabel,
referenceTop.clone().add(new THREE.Vector3(0, 0.38, 0))
);
placeLabel(
lampLabel,
lampPosition.clone().add(new THREE.Vector3(0, 0.74, 0))
);
if (mechanicalGhost.visible) {
placeLabel(
mechanicalLabel,
mechanicalPoints[3].clone().add(new THREE.Vector3(0, 0.42, 0))
);
}
}
// Direct camera controls: no external controls addon.
let dragging = false;
let previousX = 0;
let previousY = 0;
let userMovedCamera = false;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.005;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0035,
-0.04,
0.55
);
userMovedCamera = true;
updateCamera();
});
canvas.addEventListener("pointerup", (event) => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
7.2,
15.5
);
userMovedCamera = true;
updateCamera();
},
{ passive: false }
);
document.querySelector("#camera-reset").addEventListener("click", () => {
cameraState.target.set(-0.35, 1.65, 0);
cameraState.yaw = -0.025;
cameraState.pitch = 0.09;
cameraState.radius = window.innerWidth < 700 ? 12.2 : 10.7;
userMovedCamera = false;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (playing && progress < 1) {
progress = Math.min(1, progress + delta / durationSeconds);
syncProgressUI();
}
updateGrowth();
updateCamera();
const pulse = 0.93 + Math.sin(elapsed * 2.1) * 0.04;
spotLight.intensity = 85 * pulse;
bulbGlow.intensity = 18 * pulse;
photons.forEach((photon, index) => {
const t = (index / photons.length + elapsed * 0.075) % 1;
photon.position.lerpVectors(bulbPosition, beamEnd, t);
photon.position.z += Math.sin(t * Math.PI) * 0.16;
const scale = 0.72 + Math.sin(t * Math.PI) * 0.5;
photon.scale.setScalar(scale);
});
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```