Prompt specification
Build or customize the Three.js 3D scene "Character Changes From Repair to Gardening" 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: Character Changes From Repair to Gardening
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/character-changes-from-repair-to-gardening-x3746
Scene Description Prompt:
Use Three.js from cdnjs to animate a character putting away one tool set and taking another before changing task. Preserve object locations and show the activity transition through action. Deliver one self-contained HTML file with stylized solid shapes.
Working Three.js Starter (character-changes-from-repair-to-gardening-x3746):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Workshop Shift — Tool Change Animation</title>
<style>
:root {
--ink: #26383d;
--muted: #66777a;
--paper: rgba(250, 247, 239, 0.92);
--blue: #247f88;
--amber: #dc872e;
--line: rgba(38, 56, 61, 0.15);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8e3d8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 16px 18px 15px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 14px 40px rgba(44, 53, 48, 0.13);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(19px, 2vw, 25px);
line-height: 1.1;
letter-spacing: -0.035em;
}
.subtitle {
margin: 7px 0 12px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.54);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--blue);
box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.45);
}
.dot.amber {
background: var(--amber);
}
.timeline-card {
position: absolute;
right: 22px;
bottom: 22px;
width: min(390px, calc(100vw - 44px));
padding: 13px 15px 12px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 15px;
background: var(--paper);
box-shadow: 0 14px 40px rgba(44, 53, 48, 0.13);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.timeline-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 8px;
}
#phase {
overflow: hidden;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.07em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
#time {
flex: none;
color: var(--muted);
font-size: 10px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
90deg,
var(--blue) 0%,
var(--blue) var(--progress, 0%),
rgba(38, 56, 61, 0.13) var(--progress, 0%),
rgba(38, 56, 61, 0.13) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 3px solid white;
border-radius: 50%;
appearance: none;
background: var(--blue);
box-shadow: 0 2px 8px rgba(38, 56, 61, 0.25);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid white;
border-radius: 50%;
background: var(--blue);
box-shadow: 0 2px 8px rgba(38, 56, 61, 0.25);
}
.controls {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 9px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
button {
padding: 7px 11px;
border: 1px solid var(--line);
border-radius: 9px;
outline: none;
background: white;
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: #f2f5f2;
transform: translateY(-1px);
}
.callout {
position: absolute;
top: 0;
left: 0;
max-width: 235px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 11px;
background: rgba(255, 255, 255, 0.92);
box-shadow: 0 8px 24px rgba(39, 49, 47, 0.14);
color: var(--ink);
font-size: 11px;
font-weight: 750;
line-height: 1.3;
text-align: center;
opacity: 0;
transform: translate(-50%, -100%);
transition: opacity 150ms ease;
white-space: nowrap;
}
.callout::after {
position: absolute;
bottom: -5px;
left: 50%;
width: 9px;
height: 9px;
border-right: 1px solid rgba(255, 255, 255, 0.8);
border-bottom: 1px solid rgba(255, 255, 255, 0.8);
background: white;
content: "";
transform: translateX(-50%) rotate(45deg);
}
.loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #e8e3d8;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
@media (max-width: 640px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.subtitle {
display: none;
}
.timeline-card {
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
}
.controls .desktop-hint {
display: none;
}
}
</style>
</head>
<body>
<div class="loading" id="loading">Building the workshop…</div>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Workshop / Shift Change</div>
<h1>One task ends. Another begins.</h1>
<p class="subtitle">
The character stores the blue fastening kit, takes the amber building kit,
and moves straight into the next task.
</p>
<div class="legend">
<span><i class="dot"></i>Fastening kit</span>
<span><i class="dot amber"></i>Building kit</span>
</div>
</section>
<div class="callout" id="callout"></div>
<section class="timeline-card">
<div class="timeline-top">
<div id="phase">Preparing</div>
<div id="time">0.0 / 16.0</div>
</div>
<input
id="timeline"
type="range"
min="0"
max="16"
step="0.01"
value="0"
aria-label="Animation timeline"
/>
<div class="controls">
<span class="desktop-hint">Drag to orbit · wheel to zoom · right-drag to pan</span>
<button id="replay" type="button">Replay</button>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const loading = document.querySelector("#loading");
const phaseLabel = document.querySelector("#phase");
const timeLabel = document.querySelector("#time");
const timeline = document.querySelector("#timeline");
const replayButton = document.querySelector("#replay");
const callout = document.querySelector("#callout");
// -------------------------------------------------------------------------
// Renderer and camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8e3d8);
scene.fog = new THREE.Fog(0xe8e3d8, 20, 38);
const camera = new THREE.PerspectiveCamera(
35,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff8e8, 0x657174, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d6, 3.2);
sun.position.set(-6, 12, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d8e5, 1.15);
fill.position.set(9, 7, 2);
scene.add(fill);
const target = new THREE.Vector3(0, 1.05, -0.65);
let orbitTheta = 0.69;
let orbitPhi = 1.02;
let orbitRadius = 16.7;
function updateCamera() {
const sinPhi = Math.sin(orbitPhi);
camera.position.set(
target.x + orbitRadius * sinPhi * Math.sin(orbitTheta),
target.y + orbitRadius * Math.cos(orbitPhi),
target.z + orbitRadius * sinPhi * Math.cos(orbitTheta)
);
camera.lookAt(target);
}
updateCamera();
// Direct camera controls, implemented without any Three.js addons.
let dragMode = null;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
dragMode = event.button === 2 ? "pan" : "orbit";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragMode) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "orbit") {
orbitTheta -= dx * 0.006;
orbitPhi = THREE.MathUtils.clamp(orbitPhi + dy * 0.005, 0.55, 1.36);
} else {
const scale = orbitRadius * 0.0012;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
target.addScaledVector(right, -dx * scale);
target.addScaledVector(up, dy * scale);
target.x = THREE.MathUtils.clamp(target.x, -6, 6);
target.y = THREE.MathUtils.clamp(target.y, 0.2, 4);
target.z = THREE.MathUtils.clamp(target.z, -5, 4);
}
updateCamera();
});
function stopDragging(event) {
dragMode = null;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbitRadius = THREE.MathUtils.clamp(
orbitRadius * Math.exp(event.deltaY * 0.001),
9.5,
25
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const palette = {
wall: 0xd8d1c1,
wallSide: 0xc7bda9,
floor: 0xb8a589,
floorAlt: 0xc2b096,
darkWood: 0x715844,
wood: 0xa77c54,
woodLight: 0xc99b68,
blue: 0x247f88,
blueDark: 0x185961,
amber: 0xdc872e,
amberDark: 0x9b571e,
cream: 0xf0eadc,
charcoal: 0x28383d,
steel: 0x738287,
steelLight: 0xa8b2b1,
skin: 0xd7a074,
hair: 0x40332e,
shoe: 0x20282b
};
function material(color, roughness = 0.82) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0.02,
flatShading: true
});
}
const mats = {};
for (const [key, value] of Object.entries(palette)) {
mats[key] = material(value);
}
function box(parent, size, pos, mat, shadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(pos[0], pos[1], pos[2]);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(
parent,
radiusTop,
radiusBottom,
height,
pos,
mat,
radialSegments = 12,
shadow = true
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
mat
);
mesh.position.set(pos[0], pos[1], pos[2]);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(parent, radius, pos, mat, scale = null) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(radius, 16, 12),
mat
);
mesh.position.set(pos[0], pos[1], pos[2]);
if (scale) mesh.scale.set(...scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Static workshop
// -------------------------------------------------------------------------
const workshop = new THREE.Group();
scene.add(workshop);
// Individual floorboards keep the room grounded without textures.
for (let i = 0; i < 20; i++) {
const x = -7.6 + i * 0.8;
const floorMat = i % 2 === 0 ? mats.floor : mats.floorAlt;
box(
workshop,
[0.79, 0.16, 10],
[x, -0.08, 0],
floorMat,
false
);
}
box(workshop, [16.2, 5.1, 0.22], [0, 2.55, -4.75], mats.wall);
box(workshop, [0.22, 5.1, 10], [-8.05, 2.55, 0], mats.wallSide);
box(workshop, [16, 0.12, 0.12], [0, 0.16, -4.61], mats.darkWood);
box(workshop, [0.12, 0.12, 10], [-7.9, 0.16, 0], mats.darkWood);
// Pegboard and its solid recessed holes.
box(
workshop,
[4.85, 2.45, 0.12],
[1.72, 2.52, -4.54],
mats.blueDark
);
const holeMat = material(0x173f45);
for (let row = 0; row < 4; row++) {
for (let col = 0; col < 8; col++) {
const hole = cylinder(
workshop,
0.026,
0.026,
0.018,
[-0.43 + col * 0.61, 1.62 + row * 0.57, -4.465],
holeMat,
10,
false
);
hole.rotation.x = Math.PI / 2;
}
}
// Color plaques identify the two storage locations.
box(workshop, [0.82, 0.13, 0.06], [0.8, 3.43, -4.42], mats.blue);
box(workshop, [0.82, 0.13, 0.06], [2.7, 3.43, -4.42], mats.amber);
// Shelf and fixed hooks.
box(workshop, [4.2, 0.12, 0.55], [1.75, 1.61, -4.35], mats.woodLight);
for (const x of [0.8, 2.7]) {
const hook = new THREE.Mesh(
new THREE.TorusGeometry(0.09, 0.025, 8, 14, Math.PI),
mats.steel
);
hook.position.set(x, 1.77, -4.18);
hook.rotation.z = Math.PI;
hook.castShadow = true;
workshop.add(hook);
}
function createWorkbench(x, width, cabinetColor) {
const group = new THREE.Group();
group.position.x = x;
group.position.z = -0.45;
workshop.add(group);
box(group, [width, 0.22, 1.5], [0, 1.35, 0], mats.wood);
box(group, [width - 0.15, 0.12, 1.28], [0, 1.19, 0], mats.darkWood);
const legX = width * 0.36;
for (const lx of [-legX, legX]) {
box(group, [0.18, 1.2, 0.18], [lx, 0.62, 0.48], mats.darkWood);
box(group, [0.18, 1.2, 0.18], [lx, 0.62, -0.48], mats.darkWood);
}
box(
group,
[width * 0.29, 0.78, 1.16],
[width * 0.3, 0.75, 0],
cabinetColor
);
for (let i = 0; i < 3; i++) {
box(
group,
[width * 0.24, 0.015, 0.025],
[width * 0.3, 0.53 + i * 0.23, 0.595],
mats.steel
);
}
return group;
}
const leftBench = createWorkbench(-3.1, 3.35, mats.blueDark);
const rightBench = createWorkbench(3.65, 3.35, mats.amberDark);
// Workpieces remain fixed on their benches.
box(
workshop,
[1.05, 0.08, 0.72],
[-3.1, 1.50, -0.24],
mats.cream
);
cylinder(
workshop,
0.065,
0.065,
0.035,
[-3.12, 1.56, -0.25],
mats.steel,
12
);
box(
workshop,
[1.15, 0.22, 0.52],
[3.65, 1.57, -0.02],
mats.woodLight
);
for (const x of [3.42, 3.88]) {
cylinder(
workshop,
0.07,
0.07,
0.035,
[x, 1.7, -0.02],
mats.steel,
12
);
}
// A few fixed workshop objects reinforce that the room itself stays still.
box(workshop, [0.64, 0.16, 0.48], [-5.9, 0.12, 1.8], mats.darkWood);
cylinder(workshop, 0.23, 0.25, 0.54, [-5.9, 0.47, 1.8], mats.blue, 12);
cylinder(workshop, 0.24, 0.24, 0.28, [-6.55, 0.98, 2.2], mats.amber, 12);
// -------------------------------------------------------------------------
// Tool kits
// -------------------------------------------------------------------------
function createCaddy(baseColor, darkColor) {
const group = new THREE.Group();
const baseMat = material(baseColor);
const darkMat = material(darkColor);
box(group, [0.86, 0.13, 0.48], [0, 0.065, 0], baseMat);
box(group, [0.86, 0.20, 0.055], [0, 0.14, 0.235], darkMat);
box(group, [0.055, 0.20, 0.48], [-0.405, 0.14, 0], darkMat);
box(group, [0.055, 0.20, 0.48], [0.405, 0.14, 0], darkMat);
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.24, 0.035, 8, 18, Math.PI),
darkMat
);
handle.position.set(0, 0.14, -0.05);
handle.castShadow = true;
group.add(handle);
// A fixed wrench establishes the kit's purpose.
box(group, [0.075, 0.39, 0.055], [0.24, 0.33, -0.02], mats.steelLight);
box(group, [0.17, 0.075, 0.055], [0.24, 0.55, -0.02], mats.steelLight);
box(group, [0.045, 0.16, 0.055], [0.19, 0.61, -0.02], mats.steelLight);
box(group, [0.045, 0.16, 0.055], [0.29, 0.61, -0.02], mats.steelLight);
return group;
}
const blueKit = createCaddy(palette.blue, palette.blueDark);
const amberKit = createCaddy(palette.amber, palette.amberDark);
scene.add(blueKit, amberKit);
// Blue screwdriver, removed from the workbench and later returned.
const screwdriver = new THREE.Group();
const screwGrip = cylinder(
screwdriver,
0.055,
0.045,
0.22,
[0, 0, 0.075],
mats.blueDark,
10
);
screwGrip.rotation.x = Math.PI / 2;
const screwShaft = cylinder(
screwdriver,
0.022,
0.022,
0.27,
[0, 0, 0.29],
mats.steelLight,
8
);
screwShaft.rotation.x = Math.PI / 2;
const screwTip = new THREE.Mesh(
new THREE.ConeGeometry(0.032, 0.09, 8),
mats.steel
);
screwTip.rotation.x = Math.PI / 2;
screwTip.position.z = 0.47;
screwdriver.add(screwTip);
scene.add(screwdriver);
// Amber hammer. Its origin is at the head so it can strike cleanly.
const hammer = new THREE.Group();
cylinder(
hammer,
0.035,
0.045,
0.53,
[0, -0.265, 0],
mats.wood,
10
);
box(hammer, [0.36, 0.14, 0.15], [0, 0, 0], mats.steel);
box(hammer, [0.12, 0.17, 0.17], [0.17, 0, 0], mats.steelLight);
scene.add(hammer);
// A fixed brush remains in the amber kit.
box(amberKit, [0.055, 0.45, 0.055], [-0.25, 0.36, 0.02], mats.wood);
box(amberKit, [0.16, 0.24, 0.08], [-0.25, 0.58, 0.02], mats.cream);
const BLUE_RACK = new THREE.Vector3(0.8, 1.85, -4.05);
const AMBER_RACK = new THREE.Vector3(2.7, 1.85, -4.05);
const BLUE_BENCH = new THREE.Vector3(-3.1, 1.46, -0.23);
const AMBER_BENCH = new THREE.Vector3(3.65, 1.46, -0.12);
// -------------------------------------------------------------------------
// Stylized solid-shape character
// -------------------------------------------------------------------------
const character = new THREE.Group();
scene.add(character);
const skinMat = material(palette.skin);
const shirtMat = material(palette.charcoal);
const pantsMat = material(0x354348);
const apronMat = material(0xb87c48);
const shoeMat = material(palette.shoe);
const eyeMat = material(0x1e2628);
const hairMat = material(palette.hair);
// Hips and torso.
box(character, [0.66, 0.28, 0.42], [0, 0.91, 0], pantsMat);
box(character, [0.73, 0.74, 0.43], [0, 1.31, 0], shirtMat);
box(character, [0.57, 0.59, 0.055], [0, 1.17, 0.245], apronMat);
box(character, [0.28, 0.18, 0.06], [0, 1.24, 0.285], apronMat);
// Head, hair, eyes, and nose. Character faces local +Z.
const head = new THREE.Group();
head.position.y = 2.02;
character.add(head);
sphere(head, 0.31, [0, 0, 0], skinMat, [1, 1.08, 0.95]);
sphere(head, 0.33, [0, 0.18, -0.015], hairMat, [1, 0.58, 0.96]);
sphere(head, 0.032, [-0.105, 0.015, 0.285], eyeMat);
sphere(head, 0.032, [0.105, 0.015, 0.285], eyeMat);
sphere(head, 0.055, [0, -0.055, 0.302], skinMat);
function createArm(side) {
const arm = new THREE.Group();
arm.position.set(side * 0.43, 1.59, 0);
character.add(arm);
cylinder(arm, 0.115, 0.10, 0.40, [0, -0.20, 0], shirtMat, 10);
sphere(arm, 0.115, [0, 0, 0], shirtMat);
const forearm = new THREE.Group();
forearm.position.y = -0.40;
arm.add(forearm);
cylinder(forearm, 0.095, 0.08, 0.35, [0, -0.175, 0], skinMat, 10);
const hand = new THREE.Group();
hand.position.y = -0.37;
forearm.add(hand);
sphere(hand, 0.095, [0, 0, 0], skinMat, [1, 1.05, 0.9]);
return { arm, forearm, hand };
}
const left = createArm(-1);
const right = createArm(1);
function createLeg(side) {
const leg = new THREE.Group();
leg.position.set(side * 0.19, 0.85, 0);
character.add(leg);
cylinder(leg, 0.125, 0.11, 0.60, [0, -0.30, 0], pantsMat, 10);
box(leg, [0.24, 0.15, 0.39], [0, -0.66, 0.085], shoeMat);
return leg;
}
const leftLeg = createLeg(-1);
const rightLeg = createLeg(1);
// -------------------------------------------------------------------------
// Animation helpers
// -------------------------------------------------------------------------
const TOTAL_TIME = 16;
let animationTime = 0;
let playing = true;
let lastTime = performance.now();
const tmpA = new THREE.Vector3();
const tmpB = new THREE.Vector3();
const tmpQ = new THREE.Quaternion();
const identityQ = new THREE.Quaternion();
function clamp01(value) {
return THREE.MathUtils.clamp(value, 0, 1);
}
function smooth(value) {
const x = clamp01(value);
return x * x * (3 - 2 * x);
}
function range(t, start, end) {
return clamp01((t - start) / (end - start));
}
function mix(a, b, amount) {
return a.clone().lerp(b, amount);
}
function easePosition(from, to, amount, arc = 0) {
const result = mix(from, to, amount);
result.y += Math.sin(amount * Math.PI) * arc;
return result;
}
function pathLength(path) {
let length = 0;
for (let i = 1; i < path.length; i++) {
length += Math.hypot(
path[i][0] - path[i - 1][0],
path[i][1] - path[i - 1][1]
);
}
return length;
}
function samplePath(path, progress, position, direction) {
const lengths = [];
let total = 0;
for (let i = 1; i < path.length; i++) {
const length = Math.hypot(
path[i][0] - path[i - 1][0],
path[i][1] - path[i - 1][1]
);
lengths.push(length);
total += length;
}
let distance = total * clamp01(progress);
let segment = 0;
while (segment < lengths.length - 1 && distance > lengths[segment]) {
distance -= lengths[segment];
segment++;
}
const a = path[segment];
const b = path[segment + 1];
const local = lengths[segment] ? distance / lengths[segment] : 0;
position.set(
THREE.MathUtils.lerp(a[0], b[0], local),
0,
THREE.MathUtils.lerp(a[1], b[1], local)
);
direction.set(b[0] - a[0], 0, b[1] - a[1]).normalize();
return direction;
}
const PATH_TO_BLUE = [
[-3.1, 0.65],
[-5.05, 0.65],
[-5.05, -3.15],
[0.8, -2.38],
[0.8, -3.15]
];
const PATH_TO_AMBER = [
[2.7, -3.15],
[5.55, -3.15],
[5.55, 1.45],
[3.65, 1.45],
[3.65, 0.65]
];
const pathDirection = new THREE.Vector3();
function setArm(armData, shoulderX, forearmX, shoulderZ = 0) {
armData.arm.rotation.set(shoulderX, 0, shoulderZ);
armData.forearm.rotation.set(forearmX, 0, 0);
}
function worldHand(hand) {
return hand.getWorldPosition(new THREE.Vector3());
}
function characterCarryPosition() {
character.updateMatrixWorld(true);
return character.localToWorld(new THREE.Vector3(0, 1.13, 0.62));
}
function characterYawQuaternion() {
character.updateMatrixWorld(true);
return character.getWorldQuaternion(new THREE.Quaternion());
}
function resetCharacterPose() {
setArm(left, 0.08, -0.12);
setArm(right, 0.08, -0.12);
leftLeg.rotation.set(0, 0, 0);
rightLeg.rotation.set(0, 0, 0);
}
// -------------------------------------------------------------------------
// Scene state animation
// -------------------------------------------------------------------------
function animate(t) {
resetCharacterPose();
let walking = false;
let walkPhase = t * 8.2;
let rootYaw = Math.PI;
let rootPosition = new THREE.Vector3(-3.1, 0, 0.65);
// Initial fastening task.
if (t < 1.5) {
rootPosition.set(-3.1, 0, 0.65);
rootYaw = Math.PI;
const work = Math.sin(t * 12.5);
setArm(right, -0.83 + work * 0.08, -0.18, -0.10);
setArm(left, -0.70, -0.28, 0.12);
}
// Return the screwdriver to the blue kit.
else if (t < 2.3) {
rootPosition.set(-3.1, 0, 0.65);
rootYaw = Math.PI;
setArm(right, -0.92, -0.30, -0.12);
setArm(left, -0.67, -0.25, 0.12);
}
// Lift the blue kit from its stable bench position.
else if (t < 2.9) {
rootPosition.set(-3.1, 0, 0.65);
rootYaw = Math.PI;
setArm(left, -0.95, -0.38, 0.20);
setArm(right, -0.95, -0.38, -0.20);
}
// Carry the blue kit to its fixed wall position.
else if (t < 6.1) {
walking = true;
const p = range(t, 2.9, 6.1);
samplePath(PATH_TO_BLUE, p, rootPosition, pathDirection);
rootYaw = Math.atan2(pathDirection.x, pathDirection.z);
setArm(left, -0.93, -0.37, 0.20);
setArm(right, -0.93, -0.37, -0.20);
}
// Hang the blue kit.
else if (t < 6.8) {
rootPosition.set(0.8, 0, -3.15);
rootYaw = Math.PI;
setArm(left, -1.18, -0.25, 0.18);
setArm(right, -1.28, -0.18, -0.18);
}
// Step across to the amber kit.
else if (t < 7.4) {
const p = range(t, 6.8, 7.4);
rootPosition.set(
THREE.MathUtils.lerp(0.8, 2.7, p),
0,
-3.15
);
rootYaw = p < 0.72 ? Math.PI / 2 : THREE.MathUtils.lerp(
Math.PI / 2,
Math.PI,
range(p, 0.72, 1)
);
walking = p > 0.05 && p < 0.9;
setArm(left, -0.78, -0.28, 0.15);
setArm(right, -0.78, -0.28, -0.15);
}
// Take the amber kit down.
else if (t < 8.0) {
rootPosition.set(2.7, 0, -3.15);
rootYaw = Math.PI;
setArm(left, -0.96, -0.38, 0.20);
setArm(right, -0.96, -0.38, -0.20);
}
// Carry the amber kit to the second station.
else if (t < 10.8) {
walking = true;
const p = range(t, 8.0, 10.8);
samplePath(PATH_TO_AMBER, p, rootPosition, pathDirection);
rootYaw = Math.atan2(pathDirection.x, pathDirection.z);
setArm(left, -0.93, -0.37, 0.20);
setArm(right, -0.93, -0.37, -0.20);
}
// Set the amber kit on the right bench.
else if (t < 11.4) {
rootPosition.set(3.65, 0, 0.65);
rootYaw = Math.PI;
setArm(left, -0.75, -0.33, 0.18);
setArm(right, -0.75, -0.33, -0.18);
}
// Reach into the amber kit and take the hammer.
else if (t < 12.0) {
rootPosition.set(3.65, 0, 0.65);
rootYaw = Math.PI;
setArm(left, -0.72, -0.32, 0.14);
setArm(right, -0.80, -0.38, -0.14);
}
// New task: hammer the fixed wooden block.
else {
rootPosition.set(3.65, 0, 0.65);
rootYaw = Math.PI;
const cycle = (t - 12.0) * 4.4;
const strike = Math.max(0, Math.sin(cycle));
const shoulder = -0.57 - strike * 0.28;
setArm(right, shoulder, -0.10, -0.10);
setArm(left, -0.72, -0.30, 0.12);
}
// Walking gait.
if (walking) {
const stride = Math.sin(walkPhase);
leftLeg.rotation.x = stride * 0.34;
rightLeg.rotation.x = -stride * 0.34;
rootPosition.y = Math.abs(Math.sin(walkPhase)) * 0.035;
}
character.position.copy(rootPosition);
character.rotation.y = rootYaw;
character.updateMatrixWorld(true);
// -----------------------------------------------------------------------
// Blue kit and screwdriver
// -----------------------------------------------------------------------
const blueTarget = new THREE.Vector3();
const blueQuaternion = new THREE.Quaternion();
if (t < 2.3) {
blueKit.position.copy(BLUE_BENCH);
blueKit.quaternion.copy(identityQ);
} else if (t < 2.9) {
const p = smooth(range(t, 2.3, 2.9));
const carry = characterCarryPosition();
blueKit.position.copy(
easePosition(BLUE_BENCH, carry, p, 0.22)
);
blueKit.quaternion
.copy(identityQ)
.slerp(characterYawQuaternion(), p);
} else if (t < 6.1) {
blueKit.position.copy(characterCarryPosition());
blueKit.quaternion.copy(characterYawQuaternion());
} else if (t < 6.8) {
const p = smooth(range(t, 6.1, 6.8));
const carry = characterCarryPosition();
blueKit.position.copy(
easePosition(carry, BLUE_RACK, p, 0.25)
);
blueKit.quaternion
.copy(characterYawQuaternion())
.slerp(identityQ, p);
} else {
blueKit.position.copy(BLUE_RACK);
blueKit.quaternion.copy(identityQ);
}
blueKit.updateMatrixWorld(true);
// The screwdriver is held, returned to the caddy, then rides with it.
if (t < 1.5) {
const hand = worldHand(right.hand);
screwdriver.position.copy(hand);
screwdriver.quaternion.copy(characterYawQuaternion());
screwdriver.rotateX(Math.sin(t * 12.5) * 0.055);
} else if (t < 2.3) {
const p = smooth(range(t, 1.5, 2.3));
const hand = worldHand(right.hand);
const target = blueKit.localToWorld(
new THREE.Vector3(-0.15, 0.25, 0.04)
);
screwdriver.position.copy(
easePosition(hand, target, p, 0.16)
);
screwdriver.quaternion
.copy(characterYawQuaternion())
.slerp(blueKit.quaternion, p);
} else {
screwdriver.position.copy(
blueKit.localToWorld(
new THREE.Vector3(-0.15, 0.25, 0.04)
)
);
screwdriver.quaternion.copy(blueKit.quaternion);
}
// -----------------------------------------------------------------------
// Amber kit and hammer
// -----------------------------------------------------------------------
if (t < 7.4) {
amberKit.position.copy(AMBER_RACK);
amberKit.quaternion.copy(identityQ);
} else if (t < 8.0) {
const p = smooth(range(t, 7.4, 8.0));
const carry = characterCarryPosition();
amberKit.position.copy(
easePosition(AMBER_RACK, carry, p, 0.22)
);
amberKit.quaternion
.copy(identityQ)
.slerp(characterYawQuaternion(), p);
} else if (t < 10.8) {
amberKit.position.copy(characterCarryPosition());
amberKit.quaternion.copy(characterYawQuaternion());
} else if (t < 11.4) {
const p = smooth(range(t, 10.8, 11.4));
const carry = characterCarryPosition();
amberKit.position.copy(
easePosition(carry, AMBER_BENCH, p, 0.12)
);
amberKit.quaternion
.copy(characterYawQuaternion())
.slerp(identityQ, p);
} else {
amberKit.position.copy(AMBER_BENCH);
amberKit.quaternion.copy(identityQ);
}
amberKit.updateMatrixWorld(true);
const hammerRest = amberKit.localToWorld(
new THREE.Vector3(0.12, 0.60, 0.02)
);
if (t < 11.4) {
hammer.position.copy(hammerRest);
hammer.quaternion.copy(amberKit.quaternion);
} else if (t < 12.0) {
const p = smooth(range(t, 11.4, 12.0));
const hand = worldHand(right.hand);
const heldPosition = hand.clone().add(new THREE.Vector3(0, 0.54, 0));
hammer.position.copy(
easePosition(hammerRest, heldPosition, p, 0.12)
);
hammer.quaternion
.copy(amberKit.quaternion)
.slerp(characterYawQuaternion(), p);
} else {
const hand = worldHand(right.hand);
const cycle = (t - 12.0) * 4.4;
const strike = Math.max(0, Math.sin(cycle));
const swing = -0.18 + strike * 0.55;
hammer.quaternion.copy(characterYawQuaternion());
hammer.rotateX(swing);
hammer.position.copy(hand).add(new THREE.Vector3(0, 0.54, 0));
hammer.position.z -= strike * 0.035;
}
// -----------------------------------------------------------------------
// Interface and projected speech callout
// -----------------------------------------------------------------------
let phase = "";
let calloutText = "";
if (t < 1.5) {
phase = "Fastening with the blue kit";
calloutText = "Finishing the fastening task";
} else if (t < 2.3) {
phase = "Returning the screwdriver";
calloutText = "Putting the tool back";
} else if (t < 2.9) {
phase = "Lifting the blue kit";
calloutText = "One more trip to the rack";
} else if (t < 6.1) {
phase = "Carrying the blue kit";
calloutText = "Returning the fastening kit";
} else if (t < 6.8) {
phase = "Storing the blue kit";
calloutText = "Stored in its wall position";
} else if (t < 7.4) {
phase = "Moving to the amber kit";
calloutText = "Next: the building kit";
} else if (t < 8.0) {
phase = "Taking the amber kit";
calloutText = "Taking the building kit";
} else if (t < 10.8) {
phase = "Moving to the build station";
calloutText = "Carrying the kit across";
} else if (t < 11.4) {
phase = "Setting down the amber kit";
calloutText = "Kit ready at the bench";
} else if (t < 12.0) {
phase = "Taking the hammer";
calloutText = "Starting the new task";
} else {
phase = "Building with the amber kit";
calloutText = "New task underway";
}
phaseLabel.textContent = phase;
callout.textContent = calloutText;
timeLabel.textContent = `${t.toFixed(1)} / ${TOTAL_TIME.toFixed(1)}`;
timeline.value = t.toString();
timeline.style.setProperty("--progress", `${(t / TOTAL_TIME) * 100}%`);
const calloutWorld = character.localToWorld(
new THREE.Vector3(0, 2.72, 0)
);
const projected = calloutWorld.project(camera);
const visible = projected.z > -1 && projected.z < 1;
callout.style.opacity = visible ? "1" : "0";
callout.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
callout.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
// -------------------------------------------------------------------------
// Playback and resize
// -------------------------------------------------------------------------
timeline.addEventListener("input", () => {
animationTime = Number(timeline.value);
playing = false;
animate(animationTime);
});
replayButton.addEventListener("click", () => {
animationTime = 0;
playing = true;
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function frame(now) {
const dt = Math.min((now - lastTime) / 1000, 0.05);
lastTime = now;
if (playing) {
animationTime = Math.min(TOTAL_TIME, animationTime + dt);
if (animationTime >= TOTAL_TIME) playing = false;
}
animate(animationTime);
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
loading.remove();
requestAnimationFrame(frame);
</script>
</body>
</html>
```