Prompt specification
Build or customize the Three.js 3D scene "Pick-fill-collect harvest cycle" 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: Pick-fill-collect harvest cycle
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pick-fill-collect-harvest-cycle-x1074
Scene Description Prompt:
Animate a low-poly worker picking fruit, filling a crate, and transferring it to a cart without duplication. Three.js from cdnjs, one HTML file.
Working Three.js Starter (pick-fill-collect-harvest-cycle-x1074):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Orchard Worker</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c9e4de;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 18px 20px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 18px;
background: rgba(245, 251, 247, 0.82);
box-shadow: 0 16px 45px rgba(37, 69, 58, 0.17);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
user-select: none;
}
.eyebrow {
margin-bottom: 5px;
color: #4e806b;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #203d31;
font-size: clamp(20px, 3vw, 28px);
line-height: 1.1;
}
.subtitle {
margin: 7px 0 16px;
color: #587267;
font-size: 13px;
line-height: 1.45;
}
.status-row {
display: flex;
align-items: center;
gap: 9px;
min-height: 24px;
color: #284c3c;
font-size: 14px;
font-weight: 700;
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: #f29a38;
box-shadow: 0 0 0 5px rgba(242, 154, 56, 0.14);
}
.track {
height: 6px;
margin-top: 14px;
overflow: hidden;
border-radius: 999px;
background: rgba(45, 89, 70, 0.13);
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #4f9c76, #f2a03c);
transition: width 80ms linear;
}
.controls {
display: flex;
gap: 8px;
margin-top: 15px;
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 9px 13px;
color: white;
background: #285b47;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #1e4938;
transform: translateY(-1px);
}
button.secondary {
color: #315744;
background: rgba(49, 87, 68, 0.1);
}
button.secondary:hover {
background: rgba(49, 87, 68, 0.17);
}
.hint {
position: fixed;
right: 20px;
bottom: 18px;
padding: 9px 12px;
border-radius: 999px;
color: rgba(31, 65, 50, 0.72);
background: rgba(248, 252, 249, 0.7);
font-size: 11px;
font-weight: 650;
pointer-events: none;
backdrop-filter: blur(8px);
}
@media (max-width: 600px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
margin-bottom: 10px;
}
.hint {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated low-poly orchard worker"></canvas>
<section class="panel">
<div class="eyebrow">Orchard Sequence</div>
<h1>Harvest & Transfer</h1>
<p class="subtitle">
A single worker picks four fruit, fills one wooden crate, then carries
the same crate to the cart.
</p>
<div class="status-row" aria-live="polite">
<span class="status-dot"></span>
<span id="phase">Ready</span>
</div>
<div class="track"><div id="progress"></div></div>
<div class="controls">
<button id="pause" type="button">Pause</button>
<button id="replay" class="secondary" type="button">Replay</button>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const phaseElement = document.querySelector("#phase");
const progressElement = document.querySelector("#progress");
const pauseButton = document.querySelector("#pause");
const replayButton = document.querySelector("#replay");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc7e4df);
scene.fog = new THREE.Fog(0xc7e4df, 13, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(-0.2, 0.95, 0.05);
let cameraTheta = 0.38;
let cameraPhi = 1.22;
let cameraRadius = 9.1;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf3fff8, 0x6a7259, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0cf, 3.1);
sun.position.set(-4, 8, 6);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 24;
sun.shadow.bias = -0.0003;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaedcff, 0.8);
fill.position.set(6, 4, -5);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const materials = {
grass: new THREE.MeshStandardMaterial({
color: 0x8ebc78,
roughness: 1,
flatShading: true
}),
path: new THREE.MeshStandardMaterial({
color: 0xd8bd88,
roughness: 1,
flatShading: true
}),
soil: new THREE.MeshStandardMaterial({
color: 0x8a6546,
roughness: 1,
flatShading: true
}),
bark: new THREE.MeshStandardMaterial({
color: 0x765039,
roughness: 1,
flatShading: true
}),
leafA: new THREE.MeshStandardMaterial({
color: 0x4f8c55,
roughness: 0.95,
flatShading: true
}),
leafB: new THREE.MeshStandardMaterial({
color: 0x66a25d,
roughness: 0.95,
flatShading: true
}),
leafC: new THREE.MeshStandardMaterial({
color: 0x39784d,
roughness: 0.95,
flatShading: true
}),
orange: new THREE.MeshStandardMaterial({
color: 0xf28a28,
roughness: 0.68,
flatShading: true
}),
red: new THREE.MeshStandardMaterial({
color: 0xde5939,
roughness: 0.68,
flatShading: true
}),
stem: new THREE.MeshStandardMaterial({
color: 0x69452d,
roughness: 1,
flatShading: true
}),
wood: new THREE.MeshStandardMaterial({
color: 0xb98249,
roughness: 0.9,
flatShading: true
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x8c5b35,
roughness: 0.95,
flatShading: true
}),
metal: new THREE.MeshStandardMaterial({
color: 0x51635d,
roughness: 0.55,
metalness: 0.35,
flatShading: true
}),
rubber: new THREE.MeshStandardMaterial({
color: 0x28322f,
roughness: 0.92,
flatShading: true
}),
shirt: new THREE.MeshStandardMaterial({
color: 0xe7862e,
roughness: 0.88,
flatShading: true
}),
shirtDark: new THREE.MeshStandardMaterial({
color: 0xc96824,
roughness: 0.9,
flatShading: true
}),
pants: new THREE.MeshStandardMaterial({
color: 0x315a70,
roughness: 0.92,
flatShading: true
}),
skin: new THREE.MeshStandardMaterial({
color: 0xd99a70,
roughness: 0.88,
flatShading: true
}),
hair: new THREE.MeshStandardMaterial({
color: 0x49362d,
roughness: 1,
flatShading: true
}),
cap: new THREE.MeshStandardMaterial({
color: 0xf0c449,
roughness: 0.88,
flatShading: true
}),
apron: new THREE.MeshStandardMaterial({
color: 0x3f6a76,
roughness: 0.9,
flatShading: true
}),
black: new THREE.MeshStandardMaterial({
color: 0x17221f,
roughness: 0.7
})
};
const UP = new THREE.Vector3(0, 1, 0);
function mesh(geometry, material, parent = scene) {
const object = new THREE.Mesh(geometry, material);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
function box(parent, size, position, material) {
const object = mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material,
parent
);
object.position.copy(position);
return object;
}
function staticBone(parent, a, b, topRadius, bottomRadius, material, sides = 7) {
const direction = b.clone().sub(a);
const length = direction.length();
const object = mesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, length, sides),
material,
parent
);
object.position.copy(a).add(b).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(UP, direction.normalize());
return object;
}
function setBoneBetween(object, a, b) {
const direction = b.clone().sub(a);
const length = direction.length();
object.position.copy(a).add(b).multiplyScalar(0.5);
object.quaternion.setFromUnitVectors(UP, direction.normalize());
object.scale.set(1, length, 1);
}
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const ground = mesh(
new THREE.PlaneGeometry(30, 22),
materials.grass
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.045;
ground.castShadow = false;
const path = mesh(
new THREE.PlaneGeometry(14, 2.15),
materials.path
);
path.rotation.x = -Math.PI / 2;
path.position.set(0.1, -0.025, 0.66);
path.castShadow = false;
// -------------------------------------------------------------------------
// Fruit tree
// -------------------------------------------------------------------------
const tree = new THREE.Group();
tree.position.set(-2.9, 0, -0.3);
scene.add(tree);
const treeSoil = mesh(
new THREE.CylinderGeometry(1.25, 1.38, 0.12, 12),
materials.soil
);
treeSoil.position.set(tree.position.x, -0.01, tree.position.z);
staticBone(
tree,
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0.05, 2.25, 0),
0.17,
0.28,
materials.bark,
7
);
staticBone(
tree,
new THREE.Vector3(0.02, 1.35, 0),
new THREE.Vector3(-0.62, 2.25, 0.08),
0.09,
0.14,
materials.bark
);
staticBone(
tree,
new THREE.Vector3(0.03, 1.6, 0),
new THREE.Vector3(0.62, 2.35, -0.08),
0.09,
0.13,
materials.bark
);
staticBone(
tree,
new THREE.Vector3(0.04, 2.0, 0),
new THREE.Vector3(0.1, 2.72, 0.02),
0.08,
0.11,
materials.bark
);
const canopy = new THREE.Group();
tree.add(canopy);
const canopyPieces = [
[-0.58, 2.32, 0.03, 0.88, materials.leafA],
[0.54, 2.43, -0.04, 0.92, materials.leafB],
[0.02, 2.85, 0.03, 1.0, materials.leafC],
[-0.12, 2.3, -0.38, 0.83, materials.leafB],
[0.2, 2.47, 0.35, 0.85, materials.leafA]
];
for (const [x, y, z, scale, material] of canopyPieces) {
const leafCluster = mesh(
new THREE.DodecahedronGeometry(scale, 0),
material,
canopy
);
leafCluster.position.set(x, y, z);
leafCluster.rotation.set(
x * 0.4,
y * 0.7,
z * 0.3
);
}
const fruitOriginalPositions = [
new THREE.Vector3(-0.18, 1.83, 0.48),
new THREE.Vector3(0.25, 2.08, 0.52),
new THREE.Vector3(-0.42, 1.61, 0.5),
new THREE.Vector3(0.1, 1.56, 0.55)
];
const fruitDropSlots = [
new THREE.Vector3(-0.2, 0.27, -0.16),
new THREE.Vector3(0.2, 0.27, -0.15),
new THREE.Vector3(-0.19, 0.29, 0.16),
new THREE.Vector3(0.19, 0.29, 0.17)
];
const fruits = fruitOriginalPositions.map((position, index) => {
const fruit = new THREE.Group();
tree.add(fruit);
fruit.position.copy(position);
const fruitMaterial = index === 1 ? materials.red : materials.orange;
const body = mesh(
new THREE.IcosahedronGeometry(0.13, 1),
fruitMaterial,
fruit
);
body.scale.set(1, 1.08, 1);
const fruitStem = mesh(
new THREE.CylinderGeometry(0.014, 0.018, 0.09, 5),
materials.stem,
fruit
);
fruitStem.position.y = 0.13;
const fruitLeaf = mesh(
new THREE.SphereGeometry(0.065, 5, 3),
materials.leafC,
fruit
);
fruitLeaf.scale.set(1, 0.22, 0.48);
fruitLeaf.position.set(0.045, 0.145, 0);
fruitLeaf.rotation.z = -0.35;
fruit.userData.index = index;
return fruit;
});
// -------------------------------------------------------------------------
// One reusable crate
// -------------------------------------------------------------------------
const crate = new THREE.Group();
crate.position.set(-1.42, 0, 0.62);
scene.add(crate);
// Bottom boards.
for (let i = 0; i < 5; i++) {
box(
crate,
new THREE.Vector3(0.16, 0.09, 0.76),
new THREE.Vector3(-0.36 + i * 0.18, 0.055, 0),
i % 2 ? materials.wood : materials.woodDark
);
}
// Side slats.
for (const y of [0.17, 0.36]) {
box(
crate,
new THREE.Vector3(0.94, 0.105, 0.065),
new THREE.Vector3(0, y, 0.37),
materials.wood
);
box(
crate,
new THREE.Vector3(0.94, 0.105, 0.065),
new THREE.Vector3(0, y, -0.37),
materials.wood
);
box(
crate,
new THREE.Vector3(0.065, 0.105, 0.72),
new THREE.Vector3(0.455, y, 0),
materials.woodDark
);
box(
crate,
new THREE.Vector3(0.065, 0.105, 0.72),
new THREE.Vector3(-0.455, y, 0),
materials.woodDark
);
}
// Corner posts.
for (const x of [-0.455, 0.455]) {
for (const z of [-0.37, 0.37]) {
box(
crate,
new THREE.Vector3(0.085, 0.5, 0.085),
new THREE.Vector3(x, 0.27, z),
materials.woodDark
);
}
}
// Raised handle rails.
box(
crate,
new THREE.Vector3(0.13, 0.075, 0.76),
new THREE.Vector3(-0.45, 0.5, 0),
materials.wood
);
box(
crate,
new THREE.Vector3(0.13, 0.075, 0.76),
new THREE.Vector3(0.45, 0.5, 0),
materials.wood
);
// -------------------------------------------------------------------------
// Cart
// -------------------------------------------------------------------------
const cart = new THREE.Group();
const cartStartPosition = new THREE.Vector3(2.55, 0, 0.62);
cart.position.copy(cartStartPosition);
scene.add(cart);
box(
cart,
new THREE.Vector3(1.35, 0.12, 1.04),
new THREE.Vector3(0, 0.65, 0),
materials.wood
);
for (const z of [-0.5, 0.5]) {
box(
cart,
new THREE.Vector3(1.38, 0.22, 0.075),
new THREE.Vector3(0, 0.82, z),
materials.woodDark
);
}
for (const x of [-0.64, 0.64]) {
box(
cart,
new THREE.Vector3(0.075, 0.22, 1.02),
new THREE.Vector3(x, 0.82, 0),
materials.woodDark
);
}
staticBone(
cart,
new THREE.Vector3(0.7, 0.48, -0.38),
new THREE.Vector3(0.8, 1.3, -0.38),
0.045,
0.055,
materials.metal
);
staticBone(
cart,
new THREE.Vector3(0.7, 0.48, 0.38),
new THREE.Vector3(0.8, 1.3, 0.38),
0.045,
0.055,
materials.metal
);
staticBone(
cart,
new THREE.Vector3(0.8, 1.3, -0.38),
new THREE.Vector3(0.8, 1.3, 0.38),
0.05,
0.05,
materials.metal
);
const wheelPivots = [];
for (const x of [-0.48, 0.48]) {
for (const z of [-0.54, 0.54]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.3, z);
cart.add(pivot);
const wheel = mesh(
new THREE.CylinderGeometry(0.29, 0.29, 0.12, 10),
materials.rubber,
pivot
);
wheel.rotation.x = Math.PI / 2;
const hub = mesh(
new THREE.CylinderGeometry(0.11, 0.11, 0.135, 8),
materials.metal,
pivot
);
hub.rotation.x = Math.PI / 2;
wheelPivots.push(pivot);
}
}
// -------------------------------------------------------------------------
// Low-poly worker
// -------------------------------------------------------------------------
const worker = new THREE.Group();
const workerStartPosition = new THREE.Vector3(-2.1, 0, 0.68);
const grabPosition = new THREE.Vector3(-1.42, 0, 1.18);
worker.position.copy(workerStartPosition);
worker.rotation.y = Math.PI / 2;
scene.add(worker);
const bodyPivot = new THREE.Group();
bodyPivot.position.y = 0.72;
worker.add(bodyPivot);
box(
bodyPivot,
new THREE.Vector3(0.62, 0.68, 0.36),
new THREE.Vector3(0, 0.43, 0),
materials.shirt
);
box(
bodyPivot,
new THREE.Vector3(0.43, 0.47, 0.035),
new THREE.Vector3(0, 0.36, -0.198),
materials.apron
);
for (const x of [-0.17, 0.17]) {
box(
bodyPivot,
new THREE.Vector3(0.065, 0.52, 0.035),
new THREE.Vector3(x, 0.61, -0.2),
materials.apron
);
}
box(
bodyPivot,
new THREE.Vector3(0.62, 0.15, 0.38),
new THREE.Vector3(0, 0.04, 0),
materials.pants
);
const neck = mesh(
new THREE.CylinderGeometry(0.095, 0.11, 0.16, 7),
materials.skin,
bodyPivot
);
neck.position.set(0, 0.84, 0);
const head = new THREE.Group();
head.position.set(0, 1.02, 0);
bodyPivot.add(head);
const headMesh = mesh(
new THREE.IcosahedronGeometry(0.27, 1),
materials.skin,
head
);
const hair = mesh(
new THREE.SphereGeometry(
0.278,
8,
5,
0,
Math.PI * 2,
0,
Math.PI * 0.57
),
materials.hair,
head
);
hair.position.y = 0.035;
const cap = mesh(
new THREE.SphereGeometry(
0.29,
8,
4,
0,
Math.PI * 2,
0,
Math.PI * 0.48
),
materials.cap,
head
);
cap.position.y = 0.11;
box(
head,
new THREE.Vector3(0.34, 0.035, 0.23),
new THREE.Vector3(0, 0.105, -0.19),
materials.cap
);
for (const x of [-0.095, 0.095]) {
const eye = mesh(
new THREE.SphereGeometry(0.022, 6, 4),
materials.black,
head
);
eye.position.set(x, 0.015, -0.245);
}
const nose = mesh(
new THREE.ConeGeometry(0.045, 0.11, 5),
materials.skin,
head
);
nose.position.set(0, -0.045, -0.26);
nose.rotation.x = -Math.PI / 2;
const mouth = box(
head,
new THREE.Vector3(0.095, 0.016, 0.018),
new THREE.Vector3(0, -0.13, -0.235),
materials.hair
);
// Legs.
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.16, 0.75, 0);
rightLeg.position.set(0.16, 0.75, 0);
worker.add(leftLeg, rightLeg);
function buildLeg(leg) {
const thigh = mesh(
new THREE.CylinderGeometry(0.105, 0.12, 0.36, 7),
materials.pants,
leg
);
thigh.position.y = -0.18;
const shin = mesh(
new THREE.CylinderGeometry(0.09, 0.105, 0.34, 7),
materials.pants,
leg
);
shin.position.y = -0.51;
box(
leg,
new THREE.Vector3(0.22, 0.14, 0.34),
new THREE.Vector3(0, -0.7, -0.07),
materials.hair
);
}
buildLeg(leftLeg);
buildLeg(rightLeg);
// Invisible anchor used when carrying the same crate.
const carryAnchor = new THREE.Group();
carryAnchor.position.set(0, 0.92, -0.72);
worker.add(carryAnchor);
// Arms are solved with a small two-bone IK implementation.
const UPPER_ARM_LENGTH = 0.53;
const LOWER_ARM_LENGTH = 0.5;
function buildArm(side) {
const upper = mesh(
new THREE.CylinderGeometry(0.09, 0.105, 1, 7),
materials.shirtDark
);
const lower = mesh(
new THREE.CylinderGeometry(0.07, 0.082, 1, 7),
materials.skin
);
const elbow = mesh(
new THREE.IcosahedronGeometry(0.087, 1),
materials.skin
);
const hand = new THREE.Group();
const palm = mesh(
new THREE.IcosahedronGeometry(0.095, 1),
materials.skin,
hand
);
palm.scale.set(0.85, 1.1, 0.8);
return {
side,
upper,
lower,
elbow,
hand,
shoulderLocal: new THREE.Vector3(side * 0.35, 0.69, 0)
};
}
const leftArm = buildArm(-1);
const rightArm = buildArm(1);
function solveArm(arm, targetWorld) {
const shoulder = bodyPivot.localToWorld(arm.shoulderLocal.clone());
const rawDirection = targetWorld.clone().sub(shoulder);
const rawDistance = Math.max(rawDirection.length(), 0.0001);
const distance = Math.min(
rawDistance,
UPPER_ARM_LENGTH + LOWER_ARM_LENGTH - 0.002
);
const direction = rawDirection.normalize();
const along =
(
UPPER_ARM_LENGTH * UPPER_ARM_LENGTH -
LOWER_ARM_LENGTH * LOWER_ARM_LENGTH +
distance * distance
) / (2 * distance);
const height = Math.sqrt(
Math.max(
0,
UPPER_ARM_LENGTH * UPPER_ARM_LENGTH - along * along
)
);
const preferredBend = new THREE.Vector3(
arm.side * 0.9,
-0.35,
0.15
).applyQuaternion(worker.quaternion).normalize();
let bend = preferredBend.clone().addScaledVector(
direction,
-preferredBend.dot(direction)
);
if (bend.lengthSq() < 0.0001) {
bend = new THREE.Vector3(0, -1, 0).addScaledVector(
direction,
-direction.y
);
}
bend.normalize();
const elbowPosition = shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
const handPosition = shoulder.clone().addScaledVector(direction, distance);
setBoneBetween(arm.upper, shoulder, elbowPosition);
setBoneBetween(arm.lower, elbowPosition, handPosition);
arm.elbow.position.copy(elbowPosition);
arm.hand.position.copy(handPosition);
arm.hand.quaternion.copy(worker.quaternion);
}
// -------------------------------------------------------------------------
// Animation timeline
// -------------------------------------------------------------------------
const DURATION = 14;
const APPROACH_START = 6.95;
const CRATE_REACH_END = 7.65;
const CARRY_ATTACH = 8.0;
const LIFT_END = 8.45;
const TURN_END = 8.85;
const WALK_END = 11.0;
const CART_ATTACH = 11.15;
const LOWER_END = 11.55;
const cycles = fruits.map((fruit, index) => {
const start = 0.75 + index * 1.55;
return {
start,
reachEnd: start + 0.38,
pluck: start + 0.48,
crateEnd: start + 1.02,
release: start + 1.08,
settle: start + 1.34
};
});
let elapsed = 0;
let playing = true;
let crateInCarry = false;
let crateInCart = false;
let carryStartPosition = new THREE.Vector3();
let carryStartQuaternion = new THREE.Quaternion();
let cartStartLocalPosition = new THREE.Vector3();
let cartStartLocalQuaternion = new THREE.Quaternion();
const identityQuaternion = new THREE.Quaternion();
const zeroVector = new THREE.Vector3();
function clamp01(value) {
return THREE.MathUtils.clamp(value, 0, 1);
}
function smooth(value) {
const x = clamp01(value);
return x * x * (3 - 2 * x);
}
function mixVector(a, b, amount) {
return a.clone().lerp(b, amount);
}
function workerLocalToWorld(local) {
return worker.localToWorld(local.clone());
}
function setWorkerTransform(time) {
const position = workerStartPosition.clone();
let rotation = Math.PI / 2;
if (time >= APPROACH_START) {
const approachAmount = smooth(
(time - APPROACH_START) / (CRATE_REACH_END - APPROACH_START)
);
position.lerp(grabPosition, approachAmount);
rotation = THREE.MathUtils.lerp(
Math.PI / 2,
0,
approachAmount
);
}
if (time >= CARRY_ATTACH) {
position.copy(grabPosition);
rotation = 0;
}
if (time >= LIFT_END) {
const turnAmount = smooth(
(time - LIFT_END) / (TURN_END - LIFT_END)
);
rotation = THREE.MathUtils.lerp(0, -Math.PI / 2, turnAmount);
position.z = THREE.MathUtils.lerp(
grabPosition.z,
cartStartPosition.z,
turnAmount
);
}
if (time >= TURN_END) {
const walkAmount = smooth(
(time - TURN_END) / (WALK_END - TURN_END)
);
position.x = THREE.MathUtils.lerp(
grabPosition.x,
1.76,
walkAmount
);
position.z = cartStartPosition.z;
rotation = -Math.PI / 2;
}
if (time >= 11.25) {
const stepBackAmount = smooth(
(time - 11.25) / (12.3 - 11.25)
);
position.x = THREE.MathUtils.lerp(
1.76,
1.1,
stepBackAmount
);
}
worker.position.copy(position);
worker.rotation.y = rotation;
// Bend toward the crate, then stand upright.
let bend = 0;
if (time >= 7.2 && time < CARRY_ATTACH) {
bend = smooth((time - 7.2) / (7.95 - 7.2));
} else if (time >= CARRY_ATTACH && time < LIFT_END) {
bend = 1 - smooth((time - CARRY_ATTACH) / (LIFT_END - CARRY_ATTACH));
}
bodyPivot.rotation.x = -0.44 * bend;
// Walking legs.
let leftSwing = 0;
let rightSwing = 0;
let bob = 0;
if (time >= TURN_END && time <= 12.3) {
const phase = (time - TURN_END) * 8.2;
leftSwing = Math.sin(phase) * 0.48;
rightSwing = -leftSwing;
bob = Math.abs(Math.sin(phase)) * 0.026;
}
leftLeg.rotation.x = leftSwing;
rightLeg.rotation.x = rightSwing;
bodyPivot.position.y = 0.72 + bob;
worker.updateMatrixWorld(true);
}
function getFruitPickWorld(index) {
return tree.localToWorld(fruitOriginalPositions[index].clone());
}
function getCrateHandleWorld(side) {
return crate.localToWorld(
new THREE.Vector3(side * 0.42, 0.5, 0)
);
}
function getFruitReleaseWorld(index) {
const slot = fruitDropSlots[index];
return crate.localToWorld(
new THREE.Vector3(slot.x, 0.72, slot.z)
);
}
function updateHands(time) {
const defaultRight = workerLocalToWorld(
new THREE.Vector3(0.35, 0.84, -0.08)
);
const defaultLeft = workerLocalToWorld(
new THREE.Vector3(-0.35, 0.84, 0.08)
);
const brace = workerLocalToWorld(
new THREE.Vector3(-0.35, 1.52, -0.28)
);
let rightTarget = defaultRight.clone();
let leftTarget = defaultLeft.clone();
// Picking cycles.
for (let index = 0; index < cycles.length; index++) {
const cycle = cycles[index];
if (time < cycle.start || time > cycle.settle) {
continue;
}
const pickWorld = getFruitPickWorld(index);
const releaseWorld = getFruitReleaseWorld(index);
if (time <= cycle.reachEnd) {
const amount = smooth(
(time - cycle.start) / (cycle.reachEnd - cycle.start)
);
rightTarget = mixVector(defaultRight, pickWorld, amount);
const braceAmount = smooth(
(time - cycle.start) / (cycle.reachEnd - cycle.start)
);
leftTarget = mixVector(defaultLeft, brace, braceAmount);
} else if (time <= cycle.pluck) {
rightTarget = pickWorld.clone();
leftTarget = brace.clone();
} else if (time <= cycle.crateEnd) {
const amount = smooth(
(time - cycle.pluck) / (cycle.crateEnd - cycle.pluck)
);
rightTarget = mixVector(pickWorld, releaseWorld, amount);
leftTarget = mixVector(brace, defaultLeft, amount);
} else if (time <= cycle.release) {
rightTarget = releaseWorld.clone();
leftTarget = defaultLeft.clone();
} else {
const amount = smooth(
(time - cycle.release) / (cycle.settle - cycle.release)
);
rightTarget = mixVector(releaseWorld, defaultRight, amount);
leftTarget = defaultLeft.clone();
}
break;
}
// Reach for and lift the crate.
if (time >= 7.0 && time < CARRY_ATTACH) {
const amount = smooth((time - 7.0) / (7.85 - 7.0));
rightTarget = mixVector(
rightTarget,
getCrateHandleWorld(1),
amount
);
leftTarget = mixVector(
leftTarget,
getCrateHandleWorld(-1),
amount
);
}
// Carry the crate with both hands.
if (time >= CARRY_ATTACH && time < CART_ATTACH) {
rightTarget = getCrateHandleWorld(1);
leftTarget = getCrateHandleWorld(-1);
}
// Release the crate into the cart.
if (time >= CART_ATTACH) {
const amount = smooth((time - CART_ATTACH) / (LOWER_END - CART_ATTACH));
rightTarget = mixVector(
getCrateHandleWorld(1),
defaultRight,
amount
);
leftTarget = mixVector(
getCrateHandleWorld(-1),
defaultLeft,
amount
);
}
solveArm(leftArm, leftTarget);
solveArm(rightArm, rightTarget);
}
function updateFruitEvents(time) {
fruits.forEach((fruit, index) => {
const cycle = cycles[index];
if (
!fruit.userData.plucked &&
time >= cycle.pluck
) {
fruit.userData.plucked = true;
rightArm.hand.attach(fruit);
fruit.position.set(0, 0, 0);
fruit.quaternion.identity();
fruit.scale.setScalar(1);
}
if (
fruit.userData.plucked &&
!fruit.userData.released &&
time >= cycle.release
) {
fruit.userData.released = true;
crate.attach(fruit);
fruit.userData.dropStart = fruit.position.clone();
fruit.userData.dropStartQuaternion = fruit.quaternion.clone();
}
if (
fruit.userData.released &&
fruit.userData.dropStart
) {
const amount = smooth(
(time - cycle.release) / 0.24
);
fruit.position.lerpVectors(
fruit.userData.dropStart,
fruitDropSlots[index],
amount
);
fruit.quaternion.slerpQuaternions(
fruit.userData.dropStartQuaternion,
identityQuaternion,
amount
);
fruit.scale.setScalar(1 + Math.sin(amount * Math.PI) * 0.08);
}
});
}
function updateCrateEvents(time) {
if (!crateInCarry && time >= CARRY_ATTACH) {
crateInCarry = true;
worker.updateMatrixWorld(true);
carryAnchor.attach(crate);
carryStartPosition = crate.position.clone();
carryStartQuaternion = crate.quaternion.clone();
}
if (crateInCarry) {
const amount = smooth(
(time - CARRY_ATTACH) / (LIFT_END - CARRY_ATTACH)
);
crate.position.lerpVectors(
carryStartPosition,
zeroVector,
amount
);
crate.quaternion.slerpQuaternions(
carryStartQuaternion,
identityQuaternion,
amount
);
}
if (!crateInCart && time >= CART_ATTACH) {
crateInCart = true;
cart.updateMatrixWorld(true);
cart.attach(crate);
cartStartLocalPosition = crate.position.clone();
cartStartLocalQuaternion = crate.quaternion.clone();
}
if (crateInCart) {
const amount = smooth(
(time - CART_ATTACH) / (LOWER_END - CART_ATTACH)
);
crate.position.lerpVectors(
cartStartLocalPosition,
new THREE.Vector3(0, 0.73, 0),
amount
);
crate.quaternion.slerpQuaternions(
cartStartLocalQuaternion,
identityQuaternion,
amount
);
}
cart.updateMatrixWorld(true);
}
function updateCart(time) {
const rollAmount = smooth((time - 11.75) / (13.3 - 11.75));
const distance = rollAmount * 0.34;
cart.position.x = cartStartPosition.x + distance;
for (const wheel of wheelPivots) {
wheel.rotation.z = -distance / 0.29;
}
}
function updatePhase(time) {
let text = "Ready";
if (time >= cycles[0].start) {
text = "Approaching the tree";
}
for (let index = 0; index < cycles.length; index++) {
const cycle = cycles[index];
if (time >= cycle.start && time <= cycle.settle) {
if (time <= cycle.reachEnd) {
text = `Picking fruit ${index + 1} of ${fruits.length}`;
} else if (time <= cycle.pluck) {
text = `Plucking fruit ${index + 1}`;
} else if (time <= cycle.release) {
text = `Filling crate · fruit ${index + 1}`;
} else {
text = `Fruit ${index + 1} placed in crate`;
}
break;
}
}
if (time >= APPROACH_START && time < CARRY_ATTACH) {
text = "Lifting the filled crate";
} else if (time >= CARRY_ATTACH && time < CART_ATTACH) {
text = "Carrying crate to cart";
} else if (time >= CART_ATTACH && time < LOWER_END) {
text = "Placing crate into cart";
} else if (time >= LOWER_END) {
text = "Transfer complete";
}
phaseElement.textContent = text;
progressElement.style.width = `${(time / DURATION) * 100}%`;
}
function evaluate(time) {
setWorkerTransform(time);
updateCart(time);
// Crate parenting is handled before solving the hands so both hands
// remain attached to the moving handle positions.
updateCrateEvents(time);
updateHands(time);
updateFruitEvents(time);
updatePhase(time);
}
// -------------------------------------------------------------------------
// Reset and playback
// -------------------------------------------------------------------------
function resetAnimation() {
elapsed = 0;
playing = true;
crateInCarry = false;
crateInCart = false;
crate.removeFromParent();
scene.add(crate);
crate.position.set(-1.42, 0, 0.62);
crate.quaternion.identity();
crate.scale.setScalar(1);
cart.position.copy(cartStartPosition);
fruits.forEach((fruit, index) => {
fruit.removeFromParent();
tree.add(fruit);
fruit.position.copy(fruitOriginalPositions[index]);
fruit.quaternion.identity();
fruit.scale.setScalar(1);
fruit.userData.plucked = false;
fruit.userData.released = false;
fruit.userData.dropStart = null;
fruit.userData.dropStartQuaternion = null;
});
worker.position.copy(workerStartPosition);
worker.rotation.set(0, Math.PI / 2, 0);
bodyPivot.position.y = 0.72;
bodyPivot.rotation.set(0, 0, 0);
leftLeg.rotation.set(0, 0, 0);
rightLeg.rotation.set(0, 0, 0);
pauseButton.textContent = "Pause";
evaluate(0);
}
pauseButton.addEventListener("click", () => {
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Resume";
});
replayButton.addEventListener("click", resetAnimation);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Resume";
}
});
// -------------------------------------------------------------------------
// Direct camera controls, without examples or addons
// -------------------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.55,
1.46
);
previousPointerX = event.clientX;
previousPointerY = event.clientY;
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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.006,
5.2,
13
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
resetAnimation();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (playing && elapsed < DURATION) {
elapsed = Math.min(DURATION, elapsed + delta);
}
evaluate(elapsed);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```