Prompt specification
Build or customize the Three.js 3D scene "School vegetable-bed planting day" 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: School vegetable-bed planting day
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/school-vegetable-bed-planting-day-x1107
Scene Description Prompt:
Animate voxel volunteers planting seedlings on a visible school-garden grid while keeping paths clear. Use Three.js from cdnjs in one HTML file.
Working Three.js Starter (school-vegetable-bed-planting-day-x1107):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>School Garden Crew — Voxel Volunteers</title>
<style>
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #bfe3f7;
font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
#app { position: fixed; inset: 0; }
canvas { display: block; touch-action: none; }
.panel {
position: fixed; background: rgba(18,24,34,.78); color:#eaf2f8;
border: 1px solid rgba(255,255,255,.14); border-radius: 12px;
padding: 12px 14px; backdrop-filter: blur(6px);
box-shadow: 0 10px 30px rgba(0,0,0,.35); font-size: 12px; line-height: 1.55;
}
#title { top: 14px; left: 14px; max-width: 320px; }
#title h1 { margin: 0 0 4px; font-size: 15px; letter-spacing: .12em; text-transform: uppercase; }
#title p { margin: 0; opacity: .72; font-size: 11px; }
#stats { top: 14px; right: 14px; min-width: 210px; }
.row { display: flex; justify-content: space-between; gap: 18px; }
.row b { color: #9ee493; font-weight: 600; }
#bar { bottom: 14px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px;
align-items: center; flex-wrap: wrap; justify-content: center; }
button {
font: inherit; font-size: 11px; color: #eaf2f8; background: rgba(255,255,255,.08);
border: 1px solid rgba(255,255,255,.18); border-radius: 8px; padding: 7px 11px; cursor: pointer;
}
button:hover { background: rgba(255,255,255,.18); }
button.on { background: #2a9d8f; border-color: #2a9d8f; color: #06231f; }
.hint { opacity: .6; font-size: 10.5px; }
.legend { display:flex; gap:12px; margin-top:6px; font-size:10.5px; opacity:.85; flex-wrap:wrap;}
.sw { display:inline-block; width:9px; height:9px; border-radius:2px; margin-right:4px; vertical-align:middle;}
</style>
</head>
<body>
<div id="app"></div>
<div id="title" class="panel">
<h1>School Garden Crew</h1>
<p>Voxel volunteers plant seedlings bed-by-bed. Walkways stay clear at all times.</p>
<div class="legend">
<span><i class="sw" style="background:#c8b08a"></i>raised bed</span>
<span><i class="sw" style="background:#d8d4cb"></i>path</span>
<span><i class="sw" style="background:#6ec36b"></i>seedling</span>
<span><i class="sw" style="background:#ef476f"></i>click a bed to prioritise</span>
</div>
</div>
<div id="stats" class="panel">
<div class="row"><span>Seedlings planted</span><b id="sPlanted">0 / 0</b></div>
<div class="row"><span>Beds remaining</span><b id="sLeft">0</b></div>
<div class="row"><span>Volunteers</span><b id="sCrew">0</b></div>
<div class="row"><span>Paths</span><b id="sPaths">clear</b></div>
</div>
<div id="bar" class="panel">
<button id="bPause">Pause</button>
<button id="bSpeed" class="on">Speed 1×</button>
<button id="bGrid" class="on">Grid</button>
<button id="bCam">Reset camera</button>
<span class="hint">drag = orbit · shift-drag = pan · wheel = zoom · click bed = priority</span>
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ------------------------------------------------------------------ setup */
const container = document.getElementById('app');
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbfe3f7);
scene.fog = new THREE.Fog(0xcfe8ff, 46, 96);
const camera = new THREE.PerspectiveCamera(46, innerWidth / innerHeight, 0.1, 300);
scene.add(new THREE.HemisphereLight(0xcfe8ff, 0x8fa56a, 1.05));
const sun = new THREE.DirectionalLight(0xfff2d6, 2.3);
sun.position.set(16, 22, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -24; sun.shadow.camera.right = 24;
sun.shadow.camera.top = 24; sun.shadow.camera.bottom = -24;
sun.shadow.camera.near = 1; sun.shadow.camera.far = 80;
sun.shadow.bias = -0.0006;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fc9ff, 0.45);
fill.position.set(-14, 10, -10);
scene.add(fill);
/* ------------------------------------------------------- garden geometry */
const CELL = 2.0, COLS = 13, ROWS = 10;
const PATH_TOP = 0.0, SOIL_TOP = 0.28;
const GW = COLS * CELL, GD = ROWS * CELL;
const isPath = (c, r) => (c % 3 === 0) || (r % 3 === 0);
const cellX = c => (c - (COLS - 1) / 2) * CELL;
const cellZ = r => (r - (ROWS - 1) / 2) * CELL;
const key = (c, r) => c + ',' + r;
const soilCells = [], pathCells = [], soilIndex = new Map();
for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
const o = { c, r, x: cellX(c), z: cellZ(r) };
if (isPath(c, r)) pathCells.push(o); else { soilIndex.set(key(c, r), soilCells.length); soilCells.push(o); }
}
const mat = (color, rough = 0.9, extra = {}) =>
new THREE.MeshStandardMaterial({ color, roughness: rough, ...extra });
function box(w, h, d, color, x, y, z, parent = scene, m = null) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), m || mat(color));
mesh.position.set(x, y, z);
mesh.castShadow = true; mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
/* lawn */
const lawn = new THREE.Mesh(new THREE.BoxGeometry(GW + 34, 0.4, GD + 34), mat(0x86a96a));
lawn.position.y = -0.32; lawn.receiveShadow = true; scene.add(lawn);
/* raised beds (instanced soil) */
const soilGeo = new THREE.BoxGeometry(CELL - 0.16, 0.4, CELL - 0.16);
const soilMesh = new THREE.InstancedMesh(soilGeo, mat(0xffffff), soilCells.length);
const dummy = new THREE.Object3D();
soilCells.forEach((cell, i) => {
dummy.position.set(cell.x, SOIL_TOP - 0.2, cell.z);
dummy.rotation.set(0, 0, 0); dummy.updateMatrix();
soilMesh.setMatrixAt(i, dummy.matrix);
const shade = 0x8a6642;
const jitter = (Math.sin(i * 12.9898) * 43758.5453) % 1;
soilMesh.setColorAt(i, new THREE.Color(shade).offsetHSL(0, 0, jitter * 0.06));
});
soilMesh.instanceMatrix.needsUpdate = true;
soilMesh.instanceColor.needsUpdate = true;
soilMesh.castShadow = true; soilMesh.receiveShadow = true;
scene.add(soilMesh);
/* paving (paths) */
const pathGeo = new THREE.BoxGeometry(CELL - 0.07, 0.12, CELL - 0.07);
const pathMesh = new THREE.InstancedMesh(pathGeo, mat(0xffffff), pathCells.length);
pathCells.forEach((cell, i) => {
dummy.position.set(cell.x, PATH_TOP - 0.06, cell.z);
dummy.updateMatrix(); pathMesh.setMatrixAt(i, dummy.matrix);
const j = (Math.sin(i * 7.13) * 22131.7) % 1;
pathMesh.setColorAt(i, new THREE.Color(0xd6d1c6).offsetHSL(0, 0, j * 0.05));
});
pathMesh.instanceMatrix.needsUpdate = true;
pathMesh.instanceColor.needsUpdate = true;
pathMesh.receiveShadow = true;
scene.add(pathMesh);
/* wooden bed edging (flush with soil) */
const railMatrices = [];
for (const cell of soilCells) {
const dirs = [[1, 0], [-1, 0], [0, 1], [0, -1]];
for (const [dx, dr] of dirs) {
const nc = cell.c + dx, nr = cell.r + dr;
if (nc >= 0 && nc < COLS && nr >= 0 && nr < ROWS && !isPath(nc, nr)) continue;
const mx = cell.x + dx * (CELL / 2 - 0.05);
const mz = cell.z + dr * (CELL / 2 - 0.05);
dummy.position.set(mx, 0.08, mz);
dummy.rotation.set(0, dx !== 0 ? Math.PI / 2 : 0, 0);
dummy.scale.set(1, 1, 1); dummy.updateMatrix();
railMatrices.push(dummy.matrix.clone());
}
}
const railMesh = new THREE.InstancedMesh(new THREE.BoxGeometry(CELL - 0.1, 0.4, 0.12), mat(0xffffff), railMatrices.length);
railMatrices.forEach((m, i) => {
railMesh.setMatrixAt(i, m);
railMesh.setColorAt(i, new THREE.Color(0x9c6b3f).offsetHSL(0, 0, (i % 5) * 0.012));
});
railMesh.instanceMatrix.needsUpdate = true; railMesh.instanceColor.needsUpdate = true;
railMesh.castShadow = true; railMesh.receiveShadow = true;
scene.add(railMesh);
/* painted path dashes (keep-clear markings) */
const dashMatrices = [];
for (const cell of pathCells) {
const vertical = cell.c % 3 === 0, horizontal = cell.r % 3 === 0;
if (vertical === horizontal) continue;
dummy.position.set(cell.x, PATH_TOP + 0.012, cell.z);
dummy.rotation.set(0, vertical ? Math.PI / 2 : 0, 0);
dummy.updateMatrix(); dashMatrices.push(dummy.matrix.clone());
}
const dashMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.55, 0.02, 0.09),
mat(0xf7f4ec, 0.6), dashMatrices.length);
dashMatrices.forEach((m, i) => dashMesh.setMatrixAt(i, m));
dashMesh.instanceMatrix.needsUpdate = true;
scene.add(dashMesh);
/* ------------------------------------------------------------ bed blocks */
const BED_COLS = [[1, 2], [4, 5], [7, 8], [10, 11]];
const BED_ROWS = [[1, 2], [4, 5], [7, 8]];
const TYPES = ['lettuce', 'bean', 'carrot'];
const TYPE_COLOR = { lettuce: 0x8fd16a, bean: 0x4fae55, carrot: 0xe2a33c };
soilCells.forEach(cell => {
const bi = BED_COLS.findIndex(g => g.includes(cell.c));
const bj = BED_ROWS.findIndex(g => g.includes(cell.r));
cell.type = TYPES[(bi + bj) % 3];
cell.block = bi + ',' + bj;
});
/* small stake + flag per bed, tucked on the soil so paths stay empty */
for (let bi = 0; bi < BED_COLS.length; bi++) for (let bj = 0; bj < BED_ROWS.length; bj++) {
const type = TYPES[(bi + bj) % 3];
const c0 = BED_COLS[bi][0], r0 = BED_ROWS[bj][0];
const px = cellX(c0) - CELL / 2 + 0.28, pz = cellZ(r0) - CELL / 2 + 0.28;
box(0.06, 0.5, 0.06, 0x8a6642, px, SOIL_TOP + 0.2, pz);
box(0.28, 0.2, 0.05, TYPE_COLOR[type], px + 0.15, SOIL_TOP + 0.38, pz);
}
/* ------------------------------------------------------------ grid lines */
const gridPos = [];
for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) {
const cx = cellX(c), cz = cellZ(r), h = (isPath(c, r) ? PATH_TOP : SOIL_TOP) + 0.02;
const hx = CELL / 2, hz = CELL / 2;
const s = (x1, z1, x2, z2) => gridPos.push(x1, h, z1, x2, h, z2);
s(cx - hx, cz - hz, cx + hx, cz - hz);
s(cx - hx, cz + hz, cx + hx, cz + hz);
s(cx - hx, cz - hz, cx - hx, cz + hz);
s(cx + hx, cz - hz, cx + hx, cz + hz);
}
const gridGeo = new THREE.BufferGeometry();
gridGeo.setAttribute('position', new THREE.Float32BufferAttribute(gridPos, 3));
const gridLines = new THREE.LineSegments(gridGeo,
new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.3 }));
scene.add(gridLines);
/* --------------------------------------------------------- scenery props */
function fenceLine(x1, z1, x2, z2, step) {
const n = Math.max(1, Math.round(Math.hypot(x2 - x1, z2 - z1) / step));
for (let i = 0; i <= n; i++) {
const t = i / n, px = x1 + (x2 - x1) * t, pz = z1 + (z2 - z1) * t;
box(0.16, 1.15, 0.16, 0xb99b6d, px, 0.45, pz);
if (i < n) {
const t2 = (i + 0.5) / n, mx = x1 + (x2 - x1) * t2, mz = z1 + (z2 - z1) * t2;
const horiz = Math.abs(x2 - x1) > Math.abs(z2 - z1);
box(horiz ? (CELL + 0.2) : 0.09, 0.11, horiz ? 0.09 : (CELL + 0.2), 0xd9c39a, mx, 0.72, mz);
box(horiz ? (CELL + 0.2) : 0.09, 0.11, horiz ? 0.09 : (CELL + 0.2), 0xd9c39a, mx, 0.32, mz);
}
}
}
const fx = GW / 2 + 0.6, fz = GD / 2 + 0.6;
fenceLine(-fx, -fz, fx, -fz, CELL);
fenceLine(-fx, fz, fx, fz, CELL);
fenceLine(-fx, -fz, -fx, fz, CELL);
fenceLine(fx, -fz, fx, fz, CELL);
/* school building behind the garden */
const school = new THREE.Group();
school.position.set(0, 0, -GD / 2 - 8.5);
scene.add(school);
box(14, 5.4, 5.6, 0xe6d5b8, 0, 2.7, 0, school);
box(15, 0.6, 6.6, 0x9c4a3c, 0, 5.7, 0, school);
box(1.3, 2.2, 0.2, 0x6b4a32, 0, 1.1, 2.85, school);
const winMat = mat(0x9fdcea, 0.35, { emissive: 0x2e6f80, emissiveIntensity: 0.5 });
for (let i = -2; i <= 2; i++) for (let j = 0; j < 2; j++) {
box(1.1, 1.1, 0.15, 0xffffff, i * 2.6, 2.0 + j * 2.2, 2.85, school, winMat);
box(1.1, 1.1, 0.15, 0xffffff, i * 2.6, 2.0 + j * 2.2, -2.85, school, winMat);
}
box(0.12, 3.2, 0.12, 0xcfd6dd, 5.2, 7.2, 1.6, school);
box(1.4, 0.9, 0.05, 0xf2c14e, 5.9, 8.2, 1.6, school);
/* voxel trees */
function tree(x, z, s = 1) {
box(0.55 * s, 1.8 * s, 0.55 * s, 0x7a5230, x, 0.78 * s, z);
box(2.6 * s, 1.5 * s, 2.6 * s, 0x4f8f46, x, 2.2 * s, z);
box(1.9 * s, 1.3 * s, 1.9 * s, 0x5da24f, x, 3.1 * s, z);
box(1.1 * s, 1.0 * s, 1.1 * s, 0x6cb45c, x, 3.9 * s, z);
}
tree(-fx - 3, -fz - 2, 1.1); tree(fx + 3.5, -fz + 1, 0.9);
tree(-fx - 2.5, fz + 2.5, 1.0); tree(fx + 2.5, fz + 3, 1.15);
/* compost bin + watering can on the lawn (never on a path) */
box(2.0, 1.2, 1.6, 0x7d5a3c, -fx - 3.6, 0.48, 3.5);
box(2.1, 0.16, 1.7, 0x5e4229, -fx - 3.6, 1.08, 3.5);
/* drifting clouds */
const clouds = [];
for (let i = 0; i < 4; i++) {
const g = new THREE.Group();
const cm = mat(0xffffff, 1);
box(4.5, 1.3, 2.6, 0xffffff, 0, 0, 0, g, cm);
box(3.0, 1.6, 2.2, 0xffffff, -2.0, 0.2, 0, g, cm);
box(2.6, 1.4, 2.0, 0xffffff, 2.1, 0.15, 0, g, cm);
g.position.set(-30 + i * 18, 16 + (i % 3) * 2.2, -24 - (i % 2) * 14);
scene.add(g); clouds.push(g);
}
/* ------------------------------------------------------------- seedlings */
const plants = [];
const planted = new Set();
function makePlant(type) {
const g = new THREE.Group();
const stemMat = mat(0x5a9e46);
const leafMat = mat(0x9ad86f);
const head = { stemMat, leafMat };
const leaf = (w, h, d, x, y, z, ry, rx) => {
const m = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), leafMat);
m.position.set(x, y, z); m.rotation.set(rx || 0, ry, 0);
m.castShadow = true; g.add(m);
};
const stem = (w, h, d, x, y, z) => {
const m = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), stemMat);
m.position.set(x, y, z); m.castShadow = true; g.add(m);
};
if (type === 'lettuce') {
stem(0.09, 0.16, 0.09, 0, 0.08, 0);
for (let i = 0; i < 6; i++) {
const a = i * Math.PI / 3;
leaf(0.3, 0.05, 0.14, Math.cos(a) * 0.15, 0.18, Math.sin(a) * 0.15, a, -0.55);
}
leaf(0.16, 0.06, 0.16, 0, 0.24, 0, 0.6, 0);
} else if (type === 'bean') {
stem(0.07, 0.46, 0.07, 0, 0.23, 0);
leaf(0.26, 0.05, 0.12, 0.13, 0.24, 0, 0.4, -0.3);
leaf(0.26, 0.05, 0.12, -0.13, 0.32, 0, 2.6, -0.3);
leaf(0.22, 0.05, 0.11, 0.12, 0.42, 0, 1.1, -0.2);
leaf(0.22, 0.05, 0.11, -0.12, 0.44, 0, 3.6, -0.2);
} else {
stem(0.08, 0.12, 0.08, 0, 0.06, 0);
for (let i = 0; i < 5; i++) {
const a = i * 1.257;
leaf(0.05, 0.3, 0.05, Math.cos(a) * 0.07, 0.26, Math.sin(a) * 0.07, a, 0.25);
}
}
return { group: g, mats: head };
}
const TARGET_GREEN = { lettuce: 0x8fd16a, bean: 0x4fae55, carrot: 0x86c85c };
const PALE_GREEN = 0xd8eba8;
function plantSeedling(cell) {
const p = makePlant(cell.type);
p.group.position.set(cell.x, SOIL_TOP, cell.z);
p.group.scale.setScalar(0.02);
scene.add(p.group);
plants.push({ g: p.group, mats: p.mats, t: 0, color: TARGET_GREEN[cell.type] });
planted.add(key(cell.c, cell.r));
}
const easeOutBack = x => { const c1 = 1.70158, c3 = c1 + 1; return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2); };
const smooth = (a, b, x) => { const t = Math.min(1, Math.max(0, (x - a) / (b - a))); return t * t * (3 - 2 * t); };
function updatePlants(dt) {
for (const p of plants) {
p.t += dt;
const g = Math.min(1, p.t / 0.8);
const e = easeOutBack(g);
p.g.scale.setScalar(Math.max(0.02, e));
p.mats.leafMat.color.copy(new THREE.Color(PALE_GREEN)).lerp(new THREE.Color(p.color), g);
p.mats.stemMat.color.copy(new THREE.Color(0xa8c97a)).lerp(new THREE.Color(0x4f8f46), g);
}
}
/* ------------------------------------------------------------ volunteers */
const CREW_COLORS = [0xe4572e, 0x2a9d8f, 0xf4a261, 0x8e7dbe, 0x6a994e, 0xef476f];
function buildVolunteer(color) {
const root = new THREE.Group();
const skin = mat(0xf0c6a0), shirt = mat(color), pants = mat(0x3a4661);
const shoe = mat(0x232a33), hat = mat(0xf4c542), pack = mat(0x9c6b3f);
const body = new THREE.Group(); root.add(body); // bob + kneel
const hips = new THREE.Group(); body.add(hips);
const legPiv = (x) => {
const piv = new THREE.Group(); piv.position.set(x, 0.5, 0); hips.add(piv);
const l = new THREE.Mesh(new THREE.BoxGeometry(0.17, 0.46, 0.19), pants);
l.position.y = -0.23; l.castShadow = true; piv.add(l);
const s = new THREE.Mesh(new THREE.BoxGeometry(0.19, 0.09, 0.26), shoe);
s.position.set(0, -0.44, 0.04); s.castShadow = true; piv.add(s);
return piv;
};
const legL = legPiv(-0.13), legR = legPiv(0.13);
const torso = new THREE.Mesh(new THREE.BoxGeometry(0.52, 0.5, 0.32), shirt);
torso.position.y = 0.75; torso.castShadow = true; body.add(torso);
const bib = new THREE.Mesh(new THREE.BoxGeometry(0.3, 0.26, 0.34), pack);
bib.position.set(0, 0.72, -0.02); body.add(bib);
const armPiv = (x) => {
const piv = new THREE.Group(); piv.position.set(x, 0.98, 0); body.add(piv);
const a = new THREE.Mesh(new THREE.BoxGeometry(0.14, 0.42, 0.15), shirt);
a.position.y = -0.21; a.castShadow = true; piv.add(a);
const h = new THREE.Mesh(new THREE.BoxGeometry(0.15, 0.14, 0.16), skin);
h.position.y = -0.47; h.castShadow = true; piv.add(h);
return piv;
};
const armL = armPiv(-0.33), armR = armPiv(0.33);
const head = new THREE.Mesh(new THREE.BoxGeometry(0.33, 0.33, 0.33), skin);
head.position.y = 1.18; head.castShadow = true; body.add(head);
const brim = new THREE.Mesh(new THREE.BoxGeometry(0.44, 0.05, 0.44), hat);
brim.position.y = 1.36; brim.castShadow = true; body.add(brim);
const crown = new THREE.Mesh(new THREE.BoxGeometry(0.29, 0.11, 0.29), hat);
crown.position.y = 1.43; crown.castShadow = true; body.add(crown);
const eye = new THREE.Mesh(new THREE.BoxGeometry(0.23, 0.05, 0.02), mat(0x2b2b2b));
eye.position.set(0, 1.21, 0.17); body.add(eye);
/* seedling carried in the right hand */
const carry = new THREE.Group();
carry.position.set(0, -0.5, 0.06);
const cStem = new THREE.Mesh(new THREE.BoxGeometry(0.05, 0.16, 0.05), mat(0x4f8f46));
cStem.position.y = 0.08; carry.add(cStem);
const cLeaf = new THREE.Mesh(new THREE.BoxGeometry(0.16, 0.04, 0.1), mat(0x8fd16a));
cLeaf.position.y = 0.16; carry.add(cLeaf);
carry.visible = false; armR.add(carry);
scene.add(root);
return { root, body, legL, legR, armL, armR, head, carry };
}
const volunteers = [];
const STAFF = [[1, 0], [6, 0], [11, 3], [0, 6], [9, 6], [6, 9]];
STAFF.forEach((s, i) => {
const v = buildVolunteer(CREW_COLORS[i % CREW_COLORS.length]);
v.cell = { c: s[0], r: s[1] };
v.root.position.set(cellX(s[0]), PATH_TOP, cellZ(s[1]));
v.root.rotation.y = Math.random() * Math.PI * 2;
v.state = 'idle';
v.idle = Math.random() * 1.5;
v.phase = Math.random() * 6.28;
v.facing = v.root.rotation.y;
v.lane = (Math.random() - 0.5) * 0.5;
v.speed = 1.5 + Math.random() * 0.45;
v.plantT = 0;
v.route = [];
v.target = null;
v.carry.visible = false;
const blob = new THREE.Mesh(new THREE.CircleGeometry(0.42, 18),
new THREE.MeshBasicMaterial({ color: 0x14200f, transparent: true, opacity: 0.22, depthWrite: false }));
blob.rotation.x = -Math.PI / 2; blob.position.y = 0.012; scene.add(blob);
v.blob = blob;
volunteers.push(v);
});
/* --------------------------------------------------------- path finding */
function pathNeighbours(c, r) {
const out = [];
if (c + 1 < COLS && isPath(c + 1, r)) out.push({ c: c + 1, r });
if (c - 1 >= 0 && isPath(c - 1, r)) out.push({ c: c - 1, r });
if (r + 1 < ROWS && isPath(c, r + 1)) out.push({ c, r: r + 1 });
if (r - 1 >= 0 && isPath(c, r - 1)) out.push({ c, r: r - 1 });
return out;
}
function bfs(start) {
const dist = new Map(), prev = new Map(), q = [start];
dist.set(key(start.c, start.r), 0);
while (q.length) {
const cur = q.shift();
for (const n of pathNeighbours(cur.c, cur.r)) {
const k = key(n.c, n.r);
if (!dist.has(k)) { dist.set(k, dist.get(key(cur.c, cur.r)) + 1); prev.set(k, cur); q.push(n); }
}
}
return { dist, prev };
}
function routeBetween(start, goal, prev) {
const path = [goal]; let cur = goal;
while (!(cur.c === start.c && cur.r === start.r)) {
cur = prev.get(key(cur.c, cur.r));
if (!cur) return null;
path.unshift(cur);
}
return path;
}
let priorityCell = null;
function assignTask(v) {
const { dist, prev } = bfs(v.cell);
let best = null, bestPath = null, bestD = Infinity;
const consider = (cell) => {
if (planted.has(key(cell.c, cell.r))) return;
for (const n of pathNeighbours(cell.c, cell.r)) {
const d = dist.get(key(n.c, n.r));
if (d === undefined) continue;
if (d < bestD) { bestD = d; best = cell; bestPath = n; }
}
};
if (priorityCell) { consider(priorityCell); priorityCell = null; }
if (!best) for (const cell of soilCells) consider(cell);
if (!best) { v.idle = 2; return; }
const route = routeBetween(v.cell, bestPath, prev);
if (!route) { v.idle = 2; return; }
v.route = route.slice(1);
v.target = best;
v.pathCell = bestPath;
v.state = v.route.length ? 'walk' : 'enter';
v.carry.visible = true;
}
/* ------------------------------------------------------------- movement */
function moveTowards(v, tx, ty, tz, speed, dt) {
const dx = tx - v.root.position.x, dz = tz - v.root.position.z;
const d = Math.hypot(dx, dz);
v.moving = false;
if (d < 1e-3) { v.root.position.y = ty; return true; }
const step = speed * dt;
v.facingTarget = Math.atan2(dx, dz);
v.moving = true;
if (d <= step) {
v.root.position.x = tx; v.root.position.z = tz; v.root.position.y = ty;
return true;
}
v.root.position.x += dx / d * step;
v.root.position.z += dz / d * step;
v.root.position.y += (ty - v.root.position.y) * Math.min(1, dt * 8);
return false;
}
const PLANT_TIME = 1.7;
function updateVolunteer(v, dt) {
v.moving = false;
switch (v.state) {
case 'idle':
v.idle -= dt;
if (v.idle <= 0) assignTask(v);
break;
case 'walk': {
if (!v.route.length) { v.state = 'enter'; break; }
const wp = v.route[0];
const tx = cellX(wp.c), tz = cellZ(wp.r);
/* slight lateral lane so volunteers don't stack on the same paving */
const ang = Math.atan2(tx - v.root.position.x, tz - v.root.position.z);
const ox = Math.cos(ang) * v.lane, oz = -Math.sin(ang) * v.lane;
if (moveTowards(v, tx + ox, PATH_TOP, tz + oz, v.speed, dt)) {
v.route.shift();
if (!v.route.length) { v.state = 'enter'; v.cell = { c: v.pathCell.c, r: v.pathCell.r }; }
}
break;
}
case 'enter': {
const t = v.target;
const dirX = cellX(v.pathCell.c) - t.x, dirZ = cellZ(v.pathCell.r) - t.z;
const len = Math.hypot(dirX, dirZ) || 1;
const standX = t.x + dirX / len * 0.62, standZ = t.z + dirZ / len * 0.62;
if (moveTowards(v, standX, SOIL_TOP, standZ, v.speed * 0.8, dt)) {
v.state = 'plant'; v.plantT = 0;
}
break;
}
case 'plant': {
v.plantT += dt;
if (v.plantT >= PLANT_TIME * 0.45 && !v.target.planted) {
v.target.planted = true;
plantSeedling(v.target);
}
if (v.plantT >= PLANT_TIME) {
v.state = 'exit';
v.carry.visible = false;
}
break;
}
case 'exit': {
const tx = cellX(v.pathCell.c), tz = cellZ(v.pathCell.r);
if (moveTowards(v, tx, PATH_TOP, tz, v.speed * 0.85, dt)) {
v.state = 'idle'; v.idle = 0.4 + Math.random() * 1.4;
v.root.position.y = PATH_TOP;
}
break;
}
}
/* --- pose --- */
let targetRot = v.facingTarget !== undefined ? v.facingTarget : v.facing;
let diff = ((targetRot - v.facing + Math.PI * 3) % (Math.PI * 2)) - Math.PI;
v.facing += diff * Math.min(1, dt * 9);
v.root.rotation.y = v.facing;
const walking = v.moving;
if (walking) v.phase += dt * 9.5;
const swing = walking ? Math.sin(v.phase) : 0;
let kneel = 0;
if (v.state === 'plant') {
const p = v.plantT / PLANT_TIME;
kneel = smooth(0, 0.22, p) * (1 - smooth(0.78, 1, p));
}
v.legL.rotation.x = swing * 0.55 * (1 - kneel) - 1.15 * kneel;
v.legR.rotation.x = -swing * 0.55 * (1 - kneel) - 0.25 * kneel;
v.armL.rotation.x = -swing * 0.42 * (1 - kneel) + 0.35 * kneel;
v.armR.rotation.x = swing * 0.42 * (1 - kneel) - 1.25 * kneel;
v.head.rotation.x = 0.32 * kneel;
v.body.position.y = (walking ? Math.abs(Math.sin(v.phase)) * 0.045 : 0) - 0.26 * kneel;
v.body.rotation.x = 0.12 * kneel;
v.blob.position.set(v.root.position.x,
(v.root.position.y > 0.1 ? SOIL_TOP : PATH_TOP) + 0.015, v.root.position.z);
v.blob.scale.setScalar(v.state === 'plant' ? 1.15 : 1);
}
/* ------------------------------------------------------- camera controls */
const cam = {
target: new THREE.Vector3(0, 0, 0), goal: new THREE.Vector3(0, 0, 0),
radius: 30, tRadius: 30, theta: 0.72, tTheta: 0.72, phi: 0.92, tPhi: 0.92
};
const sph = new THREE.Spherical();
const tmp = new THREE.Vector3();
function updateCamera(dt) {
cam.radius += (cam.tRadius - cam.radius) * Math.min(1, dt * 7);
cam.theta += (cam.tTheta - cam.theta) * Math.min(1, dt * 9);
cam.phi += (cam.tPhi - cam.phi) * Math.min(1, dt * 9);
cam.target.lerp(cam.goal, Math.min(1, dt * 6));
sph.set(cam.radius, cam.phi, cam.theta);
tmp.setFromSpherical(sph);
camera.position.copy(cam.target).add(tmp);
camera.lookAt(cam.target);
}
let dragging = false, panning = false, lastX = 0, lastY = 0;
renderer.domElement.addEventListener('pointerdown', e => {
dragging = true; panning = e.shiftKey || e.button === 2;
lastX = e.clientX; lastY = e.clientY;
renderer.domElement.setPointerCapture(e.pointerId);
});
renderer.domElement.addEventListener('pointermove', e => {
if (!dragging) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY;
if (panning) {
const forward = tmp.clone().sub(cam.goal).setY(0).normalize();
const right = new THREE.Vector3().crossVectors(forward, camera.up).normalize();
const s = cam.radius * 0.0016;
cam.goal.addScaledVector(right, -dx * s).addScaledVector(forward, dy * s);
cam.goal.y = 0;
} else {
cam.tTheta -= dx * 0.005;
cam.tPhi = THREE.MathUtils.clamp(cam.tPhi - dy * 0.004, 0.18, 1.42);
}
});
const endDrag = () => { dragging = false; panning = false; };
renderer.domElement.addEventListener('pointerup', endDrag);
renderer.domElement.addEventListener('pointercancel', endDrag);
renderer.domElement.addEventListener('contextmenu', e => e.preventDefault());
renderer.domElement.addEventListener('wheel', e => {
e.preventDefault();
cam.tRadius = THREE.MathUtils.clamp(cam.tRadius * (1 + Math.sign(e.deltaY) * 0.09), 9, 62);
}, { passive: false });
/* click a bed -> priority + camera focus */
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
renderer.domElement.addEventListener('click', e => {
pointer.x = (e.clientX / innerWidth) * 2 - 1;
pointer.y = -(e.clientY / innerHeight) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObject(soilMesh);
if (hit.length) {
const cell = soilCells[hit[0].instanceId];
if (!planted.has(key(cell.c, cell.r))) priorityCell = cell;
cam.goal.set(cell.x, 0, cell.z);
}
});
/* -------------------------------------------------------------- UI hooks */
const sPlanted = document.getElementById('sPlanted');
const sLeft = document.getElementById('sLeft');
const sCrew = document.getElementById('sCrew');
const sPaths = document.getElementById('sPaths');
const bPause = document.getElementById('bPause');
const bSpeed = document.getElementById('bSpeed');
const bGrid = document.getElementById('bGrid');
const bCam = document.getElementById('bCam');
let paused = false, timeScale = 1;
bPause.onclick = () => { paused = !paused; bPause.textContent = paused ? 'Resume' : 'Pause'; bPause.classList.toggle('on', paused); };
bSpeed.onclick = () => { timeScale = timeScale === 1 ? 2 : 1; bSpeed.textContent = 'Speed ' + timeScale + '×'; bSpeed.classList.toggle('on', timeScale === 2); };
bGrid.onclick = () => { gridLines.visible = !gridLines.visible; bGrid.classList.toggle('on', gridLines.visible); };
bCam.onclick = () => { cam.tRadius = 30; cam.tTheta = 0.72; cam.tPhi = 0.92; cam.goal.set(0, 0, 0); };
/* ------------------------------------------------------------- main loop */
const clock = new THREE.Clock();
let hudTimer = 0;
function animate() {
requestAnimationFrame(animate);
const raw = Math.min(clock.getDelta(), 0.05);
const dt = paused ? 0 : raw * timeScale;
if (dt > 0) {
updatePlants(dt);
for (const v of volunteers) updateVolunteer(v, dt);
for (const c of clouds) {
c.position.x += dt * 0.55;
if (c.position.x > 34) c.position.x = -34;
}
} else {
for (const v of volunteers) { v.blob.position.set(v.root.position.x, (v.root.position.y > 0.1 ? SOIL_TOP : PATH_TOP) + 0.015, v.root.position.z); }
}
updateCamera(raw);
renderer.render(scene, camera);
hudTimer += raw;
if (hudTimer > 0.2) {
hudTimer = 0;
sPlanted.textContent = planted.size + ' / ' + soilCells.length;
sLeft.textContent = soilCells.length - planted.size;
sCrew.textContent = volunteers.length;
const blocked = planted.size && [...planted].some(k => isPath(...k.split(',').map(Number)));
sPaths.textContent = blocked ? 'blocked!' : 'clear ✓';
sPaths.style.color = blocked ? '#ff6b6b' : '#9ee493';
}
}
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
animate();
</script>
</body>
</html>
```