Prompt specification
Build or customize the Three.js 3D scene "Toddler playroom sorting corner" 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: Toddler playroom sorting corner
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/toddler-playroom-sorting-corner-x0375
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a shape sorter. Compose a rug, sorter, baskets, and simple seated character; the character tests one mismatch, rotates the block, and then completes a visibly correct insertion. Stylized solid forms.
Working Three.js Starter (toddler-playroom-sorting-corner-x0375):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Shape Sorter</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-rounded, "Arial Rounded MT Bold", system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #eee5d7;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.title-card {
position: fixed;
left: 24px;
top: 22px;
padding: 15px 18px 14px;
border-radius: 18px;
color: #243d3c;
background: rgba(255, 252, 244, 0.86);
border: 1px solid rgba(66, 75, 67, 0.12);
box-shadow: 0 12px 35px rgba(73, 59, 40, 0.12);
backdrop-filter: blur(10px);
pointer-events: none;
}
.title-card h1 {
margin: 0 0 3px;
font-size: clamp(20px, 3vw, 30px);
letter-spacing: -0.04em;
}
.title-card p {
margin: 0;
font-size: 12px;
font-weight: 700;
opacity: 0.62;
letter-spacing: 0.02em;
}
.controls {
position: fixed;
right: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
color: #314746;
user-select: none;
}
.hint {
padding: 9px 12px;
border-radius: 999px;
background: rgba(255, 252, 244, 0.78);
border: 1px solid rgba(66, 75, 67, 0.1);
font-size: 11px;
font-weight: 750;
backdrop-filter: blur(8px);
}
button {
border: 0;
border-radius: 999px;
padding: 11px 16px;
color: #fffdf7;
background: #315f5b;
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
box-shadow: 0 7px 18px rgba(38, 79, 75, 0.22);
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
background: #244f4c;
transform: translateY(-1px);
}
button:active { transform: translateY(1px); }
#status {
position: fixed;
left: 50%;
bottom: 24px;
transform: translateX(-50%);
min-width: 160px;
padding: 10px 18px;
border-radius: 999px;
text-align: center;
color: #fffdf7;
background: rgba(45, 73, 69, 0.9);
box-shadow: 0 8px 22px rgba(50, 60, 55, 0.2);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.02em;
pointer-events: none;
}
@media (max-width: 640px) {
.title-card { left: 12px; top: 12px; }
.hint { display: none; }
#status { bottom: 12px; }
.controls { right: 12px; bottom: 12px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="title-card">
<h1>Shape Sorter</h1>
<p>Turn, test, and fit the triangle.</p>
</div>
<div id="status">Getting ready…</div>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="replay" type="button">Replay demo</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneContainer = document.querySelector("#scene");
const statusEl = document.querySelector("#status");
const replayButton = document.querySelector("#replay");
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
sceneContainer.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee5d7);
scene.fog = new THREE.Fog(0xeee5d7, 15, 27);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0.35, 1.1, 0.15);
const orbit = {
yaw: 0.58,
pitch: 0.39,
radius: 10.8
};
function updateCamera() {
const s = Math.sin(orbit.pitch);
camera.position.set(
cameraTarget.x + orbit.radius * s * Math.sin(orbit.yaw),
cameraTarget.y + orbit.radius * Math.cos(orbit.pitch),
cameraTarget.z + orbit.radius * s * Math.cos(orbit.yaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff9ec, 0x8ca69d, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d4, 3.4);
sun.position.set(4.5, 8.5, 6.5);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -7;
sun.shadow.camera.right = 7;
sun.shadow.camera.top = 7;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 22;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0xb7d9e2, 1.1);
rimLight.position.set(-5, 4, -5);
scene.add(rimLight);
const mat = {
floor: new THREE.MeshStandardMaterial({ color: 0xe9dfcf, roughness: 0.92 }),
rug: new THREE.MeshStandardMaterial({ color: 0xc87864, roughness: 0.95 }),
rugBorder: new THREE.MeshStandardMaterial({ color: 0xf3c7a4, roughness: 0.9 }),
wood: new THREE.MeshStandardMaterial({ color: 0xe2ad67, roughness: 0.68 }),
woodDark: new THREE.MeshStandardMaterial({ color: 0xb87545, roughness: 0.75 }),
cabinet: new THREE.MeshStandardMaterial({ color: 0xd98c52, roughness: 0.72 }),
cabinetDark: new THREE.MeshStandardMaterial({ color: 0xb9683f, roughness: 0.78 }),
teal: new THREE.MeshStandardMaterial({ color: 0x2e8f89, roughness: 0.48 }),
tealDark: new THREE.MeshStandardMaterial({ color: 0x176a69, roughness: 0.52 }),
coral: new THREE.MeshStandardMaterial({ color: 0xe46755, roughness: 0.5 }),
yellow: new THREE.MeshStandardMaterial({ color: 0xf3c65e, roughness: 0.46 }),
navy: new THREE.MeshStandardMaterial({ color: 0x314c69, roughness: 0.75 }),
skin: new THREE.MeshStandardMaterial({ color: 0xd99b73, roughness: 0.72 }),
hair: new THREE.MeshStandardMaterial({ color: 0x46352f, roughness: 0.92 }),
white: new THREE.MeshStandardMaterial({ color: 0xfffcf3, roughness: 0.62 }),
black: new THREE.MeshStandardMaterial({ color: 0x263031, roughness: 0.8 })
};
function box(parent, size, position, material, cast = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = cast;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(parent, radius, position, material, scale = null) {
const mesh = new THREE.Mesh(
new THREE.SphereGeometry(radius, 24, 16),
material
);
mesh.position.copy(position);
if (scale) mesh.scale.copy(scale);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, radius, height, position, material, radialSegments = 20) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, radialSegments),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
const up = new THREE.Vector3(0, 1, 0);
function setCylinderBetween(mesh, a, b) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(up, direction.normalize());
mesh.scale.set(1, length, 1);
}
function cylinderBetween(parent, a, b, radius, material) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, 1, 14),
material
);
setCylinderBetween(mesh, a, b);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Floor and rug.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
mat.floor
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.065;
floor.receiveShadow = true;
scene.add(floor);
const rug = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 0.07, 64),
mat.rug
);
rug.position.set(0.35, 0.005, 0.15);
rug.scale.set(5.35, 1, 3.45);
rug.receiveShadow = true;
scene.add(rug);
const rugRing = new THREE.Mesh(
new THREE.RingGeometry(0.875, 0.925, 64),
mat.rugBorder
);
rugRing.rotation.x = -Math.PI / 2;
rugRing.position.set(0.35, 0.044, 0.15);
rugRing.scale.set(5.35, 3.45, 1);
rugRing.receiveShadow = true;
scene.add(rugRing);
// Shape sorter cabinet.
const sorter = new THREE.Group();
scene.add(sorter);
box(sorter, new THREE.Vector3(3.75, 1.18, 2.28),
new THREE.Vector3(0.45, 0.67, 0), mat.cabinet);
box(sorter, new THREE.Vector3(3.48, 0.78, 0.09),
new THREE.Vector3(0.45, 0.72, 1.17), mat.cabinetDark);
box(sorter, new THREE.Vector3(2.9, 0.14, 0.12),
new THREE.Vector3(0.45, 0.35, 1.25), mat.wood);
sphere(sorter, 0.09, new THREE.Vector3(0.45, 0.82, 1.26), mat.yellow);
for (const x of [-1.25, 2.15]) {
for (const z of [-0.91, 0.91]) {
box(sorter, new THREE.Vector3(0.15, 0.34, 0.15),
new THREE.Vector3(x, 1.37, z), mat.woodDark);
}
}
// A board with three actual cut-out holes.
const boardShape = new THREE.Shape();
boardShape.moveTo(-2.1, -1.4);
boardShape.lineTo(2.1, -1.4);
boardShape.lineTo(2.1, 1.4);
boardShape.lineTo(-2.1, 1.4);
boardShape.closePath();
const targetX = -1.0;
const targetY = -0.1;
const triangleHole = new THREE.Path();
triangleHole.moveTo(targetX - 0.48, targetY + 0.30);
triangleHole.lineTo(targetX + 0.48, targetY + 0.30);
triangleHole.lineTo(targetX, targetY - 0.46);
triangleHole.closePath();
boardShape.holes.push(triangleHole);
const squareHole = new THREE.Path();
squareHole.moveTo(-0.42, -0.45);
squareHole.lineTo(-0.42, 0.35);
squareHole.lineTo(0.38, 0.35);
squareHole.lineTo(0.38, -0.45);
squareHole.closePath();
boardShape.holes.push(squareHole);
const circleHole = new THREE.Path();
circleHole.absarc(1.35, 0.15, 0.42, 0, Math.PI * 2, true);
boardShape.holes.push(circleHole);
const boardGeometry = new THREE.ExtrudeGeometry(boardShape, {
depth: 0.38,
bevelEnabled: true,
bevelSegments: 2,
steps: 1,
bevelSize: 0.035,
bevelThickness: 0.025,
curveSegments: 32
});
const boardGroup = new THREE.Group();
boardGroup.position.set(0.45, 1.50, 0);
boardGroup.rotation.x = -Math.PI / 2;
scene.add(boardGroup);
const board = new THREE.Mesh(boardGeometry, mat.wood);
board.castShadow = true;
board.receiveShadow = true;
boardGroup.add(board);
// Raised trim around the board.
box(boardGroup, new THREE.Vector3(4.35, 0.10, 0.12),
new THREE.Vector3(0, 1.43, 0.36), mat.woodDark);
box(boardGroup, new THREE.Vector3(4.35, 0.10, 0.12),
new THREE.Vector3(0, -1.43, 0.36), mat.woodDark);
box(boardGroup, new THREE.Vector3(0.10, 2.92, 0.12),
new THREE.Vector3(-2.13, 0, 0.36), mat.woodDark);
box(boardGroup, new THREE.Vector3(0.10, 2.92, 0.12),
new THREE.Vector3(2.13, 0, 0.36), mat.woodDark);
// Colored rim around the triangular target.
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0xf09a48,
roughness: 0.5
});
cylinderBetween(
boardGroup,
new THREE.Vector3(targetX - 0.48, targetY + 0.30, 0.415),
new THREE.Vector3(targetX + 0.48, targetY + 0.30, 0.415),
0.026, rimMaterial
);
cylinderBetween(
boardGroup,
new THREE.Vector3(targetX + 0.48, targetY + 0.30, 0.415),
new THREE.Vector3(targetX, targetY - 0.46, 0.415),
0.026, rimMaterial
);
cylinderBetween(
boardGroup,
new THREE.Vector3(targetX, targetY - 0.46, 0.415),
new THREE.Vector3(targetX - 0.48, targetY + 0.30, 0.415),
0.026, rimMaterial
);
const squareRim = new THREE.MeshStandardMaterial({
color: 0x7597b8,
roughness: 0.55
});
cylinderBetween(boardGroup,
new THREE.Vector3(-0.42, -0.45, 0.415),
new THREE.Vector3(-0.42, 0.35, 0.415), 0.024, squareRim);
cylinderBetween(boardGroup,
new THREE.Vector3(-0.42, 0.35, 0.415),
new THREE.Vector3(0.38, 0.35, 0.415), 0.024, squareRim);
cylinderBetween(boardGroup,
new THREE.Vector3(0.38, 0.35, 0.415),
new THREE.Vector3(0.38, -0.45, 0.415), 0.024, squareRim);
cylinderBetween(boardGroup,
new THREE.Vector3(0.38, -0.45, 0.415),
new THREE.Vector3(-0.42, -0.45, 0.415), 0.024, squareRim);
const circleRim = new THREE.Mesh(
new THREE.TorusGeometry(0.445, 0.024, 8, 48),
mat.tealDark
);
circleRim.position.set(1.35, 0.15, 0.415);
circleRim.castShadow = true;
boardGroup.add(circleRim);
// Matching triangular prism.
const triangleShape = new THREE.Shape();
triangleShape.moveTo(-0.48, 0.30);
triangleShape.lineTo(0.48, 0.30);
triangleShape.lineTo(0, -0.46);
triangleShape.closePath();
const triangleGeometry = new THREE.ExtrudeGeometry(triangleShape, {
depth: 0.34,
bevelEnabled: false,
steps: 1
});
const blockAnchor = new THREE.Group();
const blockFlat = new THREE.Group();
const blockSpin = new THREE.Group();
blockFlat.rotation.x = -Math.PI / 2;
blockAnchor.add(blockFlat);
blockFlat.add(blockSpin);
scene.add(blockAnchor);
const blockCapMaterial = new THREE.MeshStandardMaterial({
color: 0x2fa49a,
roughness: 0.42
});
const blockSideMaterial = new THREE.MeshStandardMaterial({
color: 0x197773,
roughness: 0.5
});
const triangleBlock = new THREE.Mesh(
triangleGeometry,
[blockCapMaterial, blockSideMaterial]
);
triangleBlock.castShadow = true;
triangleBlock.receiveShadow = true;
blockSpin.add(triangleBlock);
// A small top marker makes the rotation especially easy to read.
const marker = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.015, 20),
mat.yellow
);
marker.rotation.x = Math.PI / 2;
marker.position.set(0, 0.08, 0.348);
marker.castShadow = true;
blockSpin.add(marker);
// Baskets made from simple slats.
function makeBasket(x, z, color, accent) {
const group = new THREE.Group();
group.position.set(x, 0.05, z);
scene.add(group);
box(group, new THREE.Vector3(1.18, 0.12, 0.88),
new THREE.Vector3(0, 0.10, 0), mat.woodDark);
const slat = new THREE.MeshStandardMaterial({
color,
roughness: 0.72
});
const trim = new THREE.MeshStandardMaterial({
color: accent,
roughness: 0.65
});
for (const y of [0.25, 0.43, 0.61]) {
box(group, new THREE.Vector3(1.18, 0.10, 0.065),
new THREE.Vector3(0, y, 0.43), slat);
box(group, new THREE.Vector3(1.18, 0.10, 0.065),
new THREE.Vector3(0, y, -0.43), slat);
box(group, new THREE.Vector3(0.065, 0.10, 0.86),
new THREE.Vector3(0.57, y, 0), slat);
box(group, new THREE.Vector3(0.065, 0.10, 0.86),
new THREE.Vector3(-0.57, y, 0), slat);
}
box(group, new THREE.Vector3(1.27, 0.10, 0.09),
new THREE.Vector3(0, 0.72, 0.46), trim);
box(group, new THREE.Vector3(1.27, 0.10, 0.09),
new THREE.Vector3(0, 0.72, -0.46), trim);
box(group, new THREE.Vector3(0.09, 0.10, 0.95),
new THREE.Vector3(0.61, 0.72, 0), trim);
box(group, new THREE.Vector3(0.09, 0.10, 0.95),
new THREE.Vector3(-0.61, 0.72, 0), trim);
return group;
}
makeBasket(3.32, 0.95, 0x76a9a2, 0x3d7773);
makeBasket(3.55, -0.85, 0xd8895c, 0x9a573d);
// Toys inside the baskets.
const basketCylinder = cylinder(
scene, 0.24, 0.35, new THREE.Vector3(3.25, 0.72, 0.93), mat.coral
);
basketCylinder.rotation.y = 0.2;
const basketCube = box(
scene,
new THREE.Vector3(0.45, 0.45, 0.45),
new THREE.Vector3(3.56, 0.68, -0.85),
mat.yellow
);
basketCube.rotation.y = -0.18;
// A couple of loose toys on the rug.
const looseCube = box(
scene,
new THREE.Vector3(0.48, 0.48, 0.48),
new THREE.Vector3(-0.55, 0.28, 1.35),
mat.coral
);
looseCube.rotation.y = 0.3;
const loosePeg = cylinder(
scene, 0.20, 0.48, new THREE.Vector3(0.05, 0.28, 1.43), mat.navy
);
loosePeg.rotation.z = 0.16;
// Seated character.
const character = new THREE.Group();
scene.add(character);
// Stool.
box(character, new THREE.Vector3(0.82, 0.13, 0.78),
new THREE.Vector3(-2.57, 0.52, 0.44), mat.wood);
for (const x of [-2.87, -2.27]) {
for (const z of [0.17, 0.71]) {
box(character, new THREE.Vector3(0.11, 0.48, 0.11),
new THREE.Vector3(x, 0.26, z), mat.woodDark);
}
}
// Torso and hips.
sphere(character, 1, new THREE.Vector3(-2.32, 1.16, 0.44),
mat.coral, new THREE.Vector3(0.39, 0.55, 0.32));
sphere(character, 1, new THREE.Vector3(-2.34, 0.79, 0.44),
mat.navy, new THREE.Vector3(0.39, 0.25, 0.34));
// Seated legs.
for (const z of [0.27, 0.61]) {
cylinderBetween(character,
new THREE.Vector3(-2.29, 0.79, z),
new THREE.Vector3(-1.67, 0.52, z),
0.13, mat.navy);
sphere(character, 0.15,
new THREE.Vector3(-1.67, 0.52, z), mat.navy);
cylinderBetween(character,
new THREE.Vector3(-1.67, 0.52, z),
new THREE.Vector3(-1.54, 0.17, z),
0.115, mat.navy);
sphere(character, 1,
new THREE.Vector3(-1.47, 0.13, z),
mat.hair, new THREE.Vector3(0.23, 0.13, 0.16));
}
// Head group.
const headGroup = new THREE.Group();
headGroup.position.set(-2.12, 1.91, 0.44);
character.add(headGroup);
sphere(headGroup, 0.35, new THREE.Vector3(0, 0, 0), mat.skin);
const hairCap = new THREE.Mesh(
new THREE.SphereGeometry(0.365, 24, 14, 0, Math.PI * 2, 0, Math.PI * 0.52),
mat.hair
);
hairCap.position.y = 0.035;
hairCap.castShadow = true;
headGroup.add(hairCap);
// Face points toward +X.
sphere(headGroup, 0.042, new THREE.Vector3(0.315, 0.055, -0.115), mat.black);
sphere(headGroup, 0.042, new THREE.Vector3(0.315, 0.055, 0.115), mat.black);
sphere(headGroup, 0.065, new THREE.Vector3(0.355, -0.035, 0), mat.skin);
const mouth = sphere(headGroup, 0.045,
new THREE.Vector3(0.327, -0.15, 0), mat.black,
new THREE.Vector3(0.45, 0.55, 1.25));
// Collar.
const collar = new THREE.Mesh(
new THREE.TorusGeometry(0.21, 0.045, 8, 24),
mat.white
);
collar.position.set(-2.18, 1.58, 0.44);
collar.rotation.z = Math.PI / 2;
collar.scale.set(1, 1, 0.78);
collar.castShadow = true;
character.add(collar);
// Articulated arms.
function makeArm(shoulder, bendSign) {
const upper = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.115, 1, 14),
mat.coral
);
const lower = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.085, 1, 14),
mat.skin
);
const hand = sphere(character, 0.115, new THREE.Vector3(), mat.skin);
upper.castShadow = lower.castShadow = true;
character.add(upper, lower);
sphere(character, 0.14, shoulder, mat.coral);
return { shoulder: shoulder.clone(), upper, lower, hand, bendSign };
}
const nearArm = makeArm(new THREE.Vector3(-2.17, 1.43, 0.73), 1);
const farArm = makeArm(new THREE.Vector3(-2.17, 1.43, 0.16), -1);
function solveArm(arm, target) {
const delta = new THREE.Vector3().subVectors(target, arm.shoulder);
const distance = Math.max(delta.length(), 0.001);
const direction = delta.clone().normalize();
const l1 = 0.90;
const l2 = 0.90;
const along = THREE.MathUtils.clamp(
(l1 * l1 - l2 * l2 + distance * distance) / (2 * distance),
0,
l1
);
const height = Math.sqrt(Math.max(0, l1 * l1 - along * along));
const bend = new THREE.Vector3(0, -1, 0);
bend.addScaledVector(direction, -bend.dot(direction));
bend.z += arm.bendSign * 0.12;
bend.addScaledVector(direction, -bend.dot(direction));
if (bend.lengthSq() < 0.001) {
bend.set(0, 0, arm.bendSign);
}
bend.normalize();
const elbow = arm.shoulder.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
setCylinderBetween(arm.upper, arm.shoulder, elbow);
setCylinderBetween(arm.lower, elbow, target);
arm.hand.position.copy(target);
}
// Animation timeline.
const holdPosition = new THREE.Vector3(-1.55, 1.55, 0.44);
const testPosition = new THREE.Vector3(-0.55, 1.91, 0.10);
const turnPosition = new THREE.Vector3(-0.73, 2.28, 0.10);
const abovePosition = new THREE.Vector3(-0.55, 2.55, 0.10);
const insertedPosition = new THREE.Vector3(-0.55, 1.54, 0.10);
const animationDuration = 9.4;
let animationStart = performance.now();
function smooth(t) {
t = THREE.MathUtils.clamp(t, 0, 1);
return t * t * (3 - 2 * t);
}
function updateStatus(t) {
if (t < 1.0) statusEl.textContent = "Trying the triangle…";
else if (t < 2.6) statusEl.textContent = "That orientation does not fit.";
else if (t < 4.2) statusEl.textContent = "Turning it around…";
else if (t < 5.3) statusEl.textContent = "Lineing it up…";
else if (t < 6.5) statusEl.textContent = "Push, push…";
else if (t < 7.2) statusEl.textContent = "Perfect fit!";
else statusEl.textContent = "A perfect triangle!";
}
function updateCharacter(elapsed) {
const t = Math.min(elapsed, animationDuration);
const blockPosition = new THREE.Vector3();
let angle = Math.PI;
if (t < 1.0) {
blockPosition.copy(holdPosition);
} else if (t < 2.6) {
const u = smooth((t - 1.0) / 1.6);
blockPosition.lerpVectors(holdPosition, testPosition, u);
// A small testing shake emphasizes the mismatch.
if (t > 1.75) {
blockPosition.x += Math.sin((t - 1.75) * 28) * 0.025;
blockPosition.y += Math.sin((t - 1.75) * 20) * 0.012;
}
} else if (t < 3.0) {
const u = smooth((t - 2.6) / 0.4);
blockPosition.lerpVectors(testPosition, turnPosition, u);
} else if (t < 4.2) {
blockPosition.copy(turnPosition);
const u = smooth((t - 3.0) / 1.2);
angle = Math.PI * (1 - u);
} else if (t < 5.3) {
const u = smooth((t - 4.2) / 1.1);
blockPosition.lerpVectors(turnPosition, abovePosition, u);
angle = 0;
} else if (t < 6.5) {
const u = smooth((t - 5.3) / 1.2);
blockPosition.lerpVectors(abovePosition, insertedPosition, u);
angle = 0;
} else {
blockPosition.copy(insertedPosition);
angle = 0;
}
blockAnchor.position.copy(blockPosition);
blockSpin.rotation.z = angle;
let nearHand = new THREE.Vector3(
blockPosition.x,
blockPosition.y + 0.105,
blockPosition.z + 0.30
);
let farHand = new THREE.Vector3(
blockPosition.x,
blockPosition.y + 0.105,
blockPosition.z - 0.30
);
if (t >= 6.45) {
const release = smooth((t - 6.45) / 0.72);
const nearRelease = new THREE.Vector3(-1.92, 1.78, 0.73);
const farRelease = new THREE.Vector3(-2.25, 1.23, 0.16);
nearHand.lerp(nearRelease, release);
farHand.lerp(farRelease, release);
if (t > 7.15) {
const wave = Math.sin((elapsed - 7.15) * 4.2) * 0.085;
nearHand.y += wave;
nearHand.x += wave * 0.4;
}
}
solveArm(nearArm, nearHand);
solveArm(farArm, farHand);
// Head follows the activity, then lifts with a pleased expression.
const workingNod = t < 6.45 ? 0.13 : -0.035;
const tinyBob = t > 7.15 ? Math.sin(elapsed * 3.2) * 0.018 : 0;
headGroup.rotation.z = workingNod + tinyBob;
mouth.scale.set(
t > 6.8 ? 0.56 : 0.45,
t > 6.8 ? 0.85 : 0.55,
t > 6.8 ? 1.35 : 1.25
);
// A subtle blink.
const blinkPhase = elapsed % 4.6;
const eyeScale = blinkPhase > 4.42 && blinkPhase < 4.57 ? 0.12 : 1;
headGroup.children[1].scale.y = eyeScale;
headGroup.children[2].scale.y = eyeScale;
updateStatus(t);
}
// Direct camera orbit controls, implemented without addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.yaw -= dx * 0.005;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.004,
0.20,
1.02
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
7.0,
14.0
);
updateCamera();
}, { passive: false });
replayButton.addEventListener("click", () => {
animationStart = performance.now();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
animationStart = performance.now();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const animationElapsed = (performance.now() - animationStart) / 1000;
updateCharacter(animationElapsed);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```