Prompt specification
Build or customize the Three.js 3D scene "Pick Up Place and Release a Block" 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 Up Place and Release a Block
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pick-up-place-and-release-a-block-x3730
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a voxel character moving a wooden block from a low table onto a shelf. Give the character a simple jointed arm and leave enough space between the table and shelving unit for the whole transfer to be visible. Begin with the block resting on the tabletop, bring the hand into contact, then lift and carry it in one continuous motion; the block should follow the hand only while being held. After its underside reaches the shelf, release it and withdraw the arm without moving the placed block. Include a replay button that restores both the character and block to their starting positions. Pause at pickup and placement so the fingers, supporting surface, and block can be inspected before the next part of the transfer.
Working Three.js Starter (pick-up-place-and-release-a-block-x3730):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Block Transfer</title>
<style>
:root {
color-scheme: light;
--ink: #25313a;
--panel: rgba(255, 255, 255, 0.9);
--line: rgba(37, 49, 58, 0.13);
--accent: #d9822b;
--accent-dark: #9b5314;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e3d9;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
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(370px, calc(100vw - 44px));
padding: 17px 18px 15px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 12px 34px rgba(54, 45, 31, 0.12);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 5px;
color: var(--accent-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 3vw, 27px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: #687078;
font-size: 13px;
line-height: 1.45;
}
.phase-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 15px;
}
.phase-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(217, 130, 43, 0.14);
}
#phase {
font-size: 11px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.progress-track {
height: 5px;
margin-top: 12px;
overflow: hidden;
border-radius: 999px;
background: rgba(37, 49, 58, 0.1);
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #d9822b, #e7b356);
transition: width 100ms linear;
}
.controls {
position: absolute;
right: 22px;
bottom: 22px;
left: 22px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 16px;
}
.status-card {
width: min(480px, 100%);
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 10px 30px rgba(54, 45, 31, 0.1);
backdrop-filter: blur(12px);
}
#status {
min-height: 38px;
color: #59646d;
font-size: 13px;
line-height: 1.45;
}
.hint {
margin-top: 7px;
color: #879097;
font-size: 11px;
}
.buttons {
display: flex;
flex: 0 0 auto;
gap: 9px;
pointer-events: auto;
}
button {
min-height: 43px;
padding: 0 17px;
border: 1px solid rgba(37, 49, 58, 0.13);
border-radius: 12px;
background: rgba(255, 255, 255, 0.94);
color: var(--ink);
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 7px 18px rgba(54, 45, 31, 0.09);
transition:
transform 120ms ease,
box-shadow 120ms ease,
opacity 120ms ease;
}
button:hover {
transform: translateY(-1px);
box-shadow: 0 10px 22px rgba(54, 45, 31, 0.14);
}
button:active {
transform: translateY(1px);
}
button.primary {
border-color: transparent;
background: var(--accent);
color: white;
}
button.primary:hover {
background: #c96f1d;
}
button.hidden {
display: none;
}
button:disabled {
opacity: 0.45;
cursor: default;
transform: none;
}
@media (max-width: 650px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.subtitle {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
left: 12px;
align-items: stretch;
flex-direction: column;
}
.status-card {
width: 100%;
}
.buttons {
justify-content: flex-end;
}
button {
flex: 1;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Three.js / Voxel Study</div>
<h1>One careful transfer</h1>
<p class="subtitle">
A voxel character picks up a wooden block, carries it across the open
space, and places it on the shelf.
</p>
<div class="phase-row">
<div class="phase-dot"></div>
<div id="phase">Reaching</div>
</div>
<div class="progress-track">
<div id="progress"></div>
</div>
</section>
<section class="controls">
<div class="status-card">
<div id="status" aria-live="polite"></div>
<div class="hint">
Drag to orbit · Scroll to zoom · The motion pauses at both contacts
</div>
</div>
<div class="buttons">
<button id="focusBtn" class="hidden">Inspect close-up</button>
<button id="continueBtn" class="primary hidden">Continue transfer</button>
<button id="replayBtn">↻ 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";
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Voxel character transferring a wooden block to a shelf"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e3d9);
scene.fog = new THREE.Fog(0xe9e3d9, 12, 23);
const camera = new THREE.OrthographicCamera(-5, 5, 3, -3, 0.1, 50);
camera.position.set(6, 5.4, 7.5);
const hemisphere = new THREE.HemisphereLight(0xfff9ec, 0x7b8b93, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2d9, 3.2);
sun.position.set(-4.5, 8, 5.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 = 0.5;
sun.shadow.camera.far = 20;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d8e7, 0.85);
fill.position.set(5, 3, -4);
scene.add(fill);
// -------------------------------------------------------------------------
// Small helpers
// -------------------------------------------------------------------------
const V3 = THREE.Vector3;
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smooth(value) {
const t = clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function smoother(value) {
const t = clamp(value, 0, 1);
return t * t * t * (t * (t * 6 - 15) + 10);
}
function makeBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 256;
const context = canvas.getContext("2d");
const gradient = context.createLinearGradient(0, 0, 256, 256);
gradient.addColorStop(0, "#c98a4c");
gradient.addColorStop(0.5, "#b9783e");
gradient.addColorStop(1, "#d39a5b");
context.fillStyle = gradient;
context.fillRect(0, 0, 256, 256);
for (let i = 0; i < 42; i += 1) {
const y = Math.random() * 256;
const amplitude = 2 + Math.random() * 9;
context.beginPath();
context.moveTo(0, y);
for (let x = 0; x <= 256; x += 8) {
context.lineTo(
x,
y + Math.sin(x * 0.035 + i) * amplitude * 0.18
);
}
context.strokeStyle =
i % 3 === 0
? "rgba(88, 42, 16, 0.23)"
: "rgba(255, 225, 170, 0.14)";
context.lineWidth = 1 + Math.random() * 2;
context.stroke();
}
context.strokeStyle = "rgba(89, 44, 18, 0.24)";
context.lineWidth = 4;
context.strokeRect(3, 3, 250, 250);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1.6, 1.6);
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
function makeLabel(text, color = "#36434a") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 64;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(255,255,255,0.9)";
context.fillRect(0, 0, 256, 64);
context.fillStyle = color;
context.font = "700 25px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 128, 33);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
})
);
sprite.scale.set(0.92, 0.23, 1);
return sprite;
}
// -------------------------------------------------------------------------
// Materials and room
// -------------------------------------------------------------------------
const woodTexture = makeWoodTexture();
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xdcd4c8,
roughness: 0.96,
}),
wall: new THREE.MeshStandardMaterial({
color: 0xeee8df,
roughness: 1,
}),
wood: new THREE.MeshStandardMaterial({
color: 0xd0965e,
map: woodTexture,
roughness: 0.78,
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x8b5934,
map: woodTexture,
roughness: 0.86,
}),
block: new THREE.MeshStandardMaterial({
color: 0xe0a15c,
map: woodTexture,
roughness: 0.72,
}),
shirt: new THREE.MeshStandardMaterial({
color: 0xe3a23b,
roughness: 0.8,
}),
shirtDark: new THREE.MeshStandardMaterial({
color: 0xb97724,
roughness: 0.85,
}),
pants: new THREE.MeshStandardMaterial({
color: 0x315d67,
roughness: 0.87,
}),
shoes: new THREE.MeshStandardMaterial({
color: 0x343b40,
roughness: 0.9,
}),
skin: new THREE.MeshStandardMaterial({
color: 0xe6b38b,
roughness: 0.86,
}),
hair: new THREE.MeshStandardMaterial({
color: 0x4a3429,
roughness: 0.95,
}),
eye: new THREE.MeshStandardMaterial({
color: 0x242a2d,
roughness: 0.8,
}),
orange: new THREE.MeshStandardMaterial({
color: 0xd97928,
roughness: 0.78,
}),
teal: new THREE.MeshStandardMaterial({
color: 0x4c9a94,
roughness: 0.82,
}),
};
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(15, 10),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const wall = makeBox(
scene,
new V3(15, 5.4, 0.12),
new V3(0, 2.7, -2.8),
materials.wall,
false
);
wall.receiveShadow = true;
const grid = new THREE.GridHelper(14, 28, 0x9d9588, 0xb0a89b);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
const transferRug = makeBox(
scene,
new V3(2.45, 0.025, 1.65),
new V3(0, 0.018, 0.08),
new THREE.MeshStandardMaterial({
color: 0xbfc9bd,
roughness: 1,
}),
false
);
transferRug.receiveShadow = true;
// -------------------------------------------------------------------------
// Low table
// -------------------------------------------------------------------------
const TABLE_TOP = 1.05;
const tableCenterX = -2.22;
const tableWidth = 1.9;
const tableDepth = 1.28;
makeBox(
scene,
new V3(tableWidth, 0.18, tableDepth),
new V3(tableCenterX, TABLE_TOP - 0.09, 0),
materials.wood
);
const tableLegPositions = [
[-0.78, -0.49],
[0.78, -0.49],
[-0.78, 0.49],
[0.78, 0.49],
];
for (const [x, z] of tableLegPositions) {
makeBox(
scene,
new V3(0.16, TABLE_TOP - 0.18, 0.16),
new V3(tableCenterX + x, (TABLE_TOP - 0.18) / 2, z),
materials.darkWood
);
}
makeBox(
scene,
new V3(1.55, 0.12, 0.12),
new V3(tableCenterX, 0.35, -0.49),
materials.darkWood
);
// -------------------------------------------------------------------------
// Open shelving unit
// -------------------------------------------------------------------------
const SHELF_TOP = 1.7;
const shelfCenterX = 2.08;
const shelfWidth = 1.72;
const shelfDepth = 1.18;
makeBox(
scene,
new V3(shelfWidth + 0.16, 0.14, shelfDepth),
new V3(shelfCenterX, SHELF_TOP - 0.07, 0),
materials.wood
);
makeBox(
scene,
new V3(shelfWidth + 0.16, 0.14, shelfDepth),
new V3(shelfCenterX, 2.68, 0),
materials.wood
);
const shelfPostX = [1.19, 2.97];
const shelfPostZ = [-0.54, 0.54];
for (const x of shelfPostX) {
for (const z of shelfPostZ) {
makeBox(
scene,
new V3(0.15, 2.82, 0.15),
new V3(x, 1.41, z),
materials.darkWood
);
}
}
// Rear cross brace leaves the placement area open and easy to inspect.
makeBox(
scene,
new V3(1.74, 0.13, 0.12),
new V3(shelfCenterX, 2.19, -0.54),
materials.darkWood
);
const pickupLabel = makeLabel("PICKUP", "#a45719");
pickupLabel.position.set(-1.5, TABLE_TOP + 0.29, 0.7);
scene.add(pickupLabel);
const placeLabel = makeLabel("PLACE", "#28736d");
placeLabel.position.set(1.48, SHELF_TOP + 0.28, 0.69);
scene.add(placeLabel);
// -------------------------------------------------------------------------
// Voxel character
// -------------------------------------------------------------------------
const character = new THREE.Group();
character.position.set(0, 0, -0.52);
scene.add(character);
makeBox(
character,
new V3(0.21, 0.62, 0.24),
new V3(-0.16, 0.39, 0),
materials.pants
);
makeBox(
character,
new V3(0.21, 0.62, 0.24),
new V3(0.16, 0.39, 0),
materials.pants
);
makeBox(
character,
new V3(0.25, 0.14, 0.37),
new V3(-0.16, 0.09, 0.065),
materials.shoes
);
makeBox(
character,
new V3(0.25, 0.14, 0.37),
new V3(0.16, 0.09, 0.065),
materials.shoes
);
makeBox(
character,
new V3(0.65, 0.72, 0.42),
new V3(0, 1.03, 0),
materials.shirt
);
makeBox(
character,
new V3(0.67, 0.13, 0.44),
new V3(0, 0.7, 0),
materials.shirtDark
);
makeBox(
character,
new V3(0.18, 0.13, 0.18),
new V3(0, 1.47, 0),
materials.skin
);
makeBox(
character,
new V3(0.49, 0.48, 0.46),
new V3(0, 1.77, 0),
materials.skin
);
makeBox(
character,
new V3(0.51, 0.13, 0.48),
new V3(0, 1.99, -0.005),
materials.hair
);
makeBox(
character,
new V3(0.12, 0.23, 0.08),
new V3(-0.17, 1.91, 0.215),
materials.hair
);
makeBox(
character,
new V3(0.12, 0.23, 0.08),
new V3(0.17, 1.91, 0.215),
materials.hair
);
makeBox(
character,
new V3(0.07, 0.07, 0.035),
new V3(-0.105, 1.81, 0.244),
materials.eye
);
makeBox(
character,
new V3(0.07, 0.07, 0.035),
new V3(0.105, 1.81, 0.244),
materials.eye
);
makeBox(
character,
new V3(0.13, 0.035, 0.035),
new V3(0, 1.68, 0.244),
materials.eye
);
const shoulderAnchor = new THREE.Object3D();
shoulderAnchor.position.set(0.36, 1.37, 0.1);
character.add(shoulderAnchor);
// -------------------------------------------------------------------------
// Jointed arm and gripper
// -------------------------------------------------------------------------
const upperArm = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 1, 0.16),
materials.shirt
);
upperArm.castShadow = true;
upperArm.receiveShadow = true;
scene.add(upperArm);
const forearm = new THREE.Mesh(
new THREE.BoxGeometry(0.135, 1, 0.135),
materials.skin
);
forearm.castShadow = true;
forearm.receiveShadow = true;
scene.add(forearm);
const shoulderJoint = new THREE.Mesh(
new THREE.BoxGeometry(0.23, 0.23, 0.23),
materials.shirtDark
);
shoulderJoint.castShadow = true;
scene.add(shoulderJoint);
const elbowJoint = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.2, 0.2),
materials.skin
);
elbowJoint.castShadow = true;
scene.add(elbowJoint);
const wristJoint = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.17, 0.17),
materials.skin
);
wristJoint.castShadow = true;
scene.add(wristJoint);
const gripper = new THREE.Group();
scene.add(gripper);
makeBox(
gripper,
new V3(0.36, 0.12, 0.44),
new V3(0, -0.06, 0),
materials.skin
);
const fingers = [];
for (const side of [-1, 1]) {
for (const z of [-0.115, 0.115]) {
const finger = makeBox(
gripper,
new V3(0.07, 0.34, 0.09),
new V3(side * 0.34, -0.3, z),
materials.skin
);
fingers.push({ mesh: finger, side });
}
}
// -------------------------------------------------------------------------
// Wooden block
// -------------------------------------------------------------------------
const BLOCK_SIZE = 0.42;
const GRIP_OFFSET = 0.34;
const startBlockPosition = new V3(
-1.5,
TABLE_TOP + BLOCK_SIZE / 2,
0
);
const placeBlockPosition = new V3(
1.48,
SHELF_TOP + BLOCK_SIZE / 2,
0
);
const block = new THREE.Group();
block.position.copy(startBlockPosition);
const blockMesh = new THREE.Mesh(
new THREE.BoxGeometry(BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE),
materials.block
);
blockMesh.castShadow = true;
blockMesh.receiveShadow = true;
block.add(blockMesh);
const blockEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(blockMesh.geometry),
new THREE.LineBasicMaterial({
color: 0x6f3b1b,
transparent: true,
opacity: 0.58,
})
);
block.add(blockEdges);
scene.add(block);
// -------------------------------------------------------------------------
// Motion paths
// -------------------------------------------------------------------------
const pickupWrist = startBlockPosition.clone().add(
new V3(0, GRIP_OFFSET, 0)
);
const placeWrist = placeBlockPosition.clone().add(
new V3(0, GRIP_OFFSET, 0)
);
const restWrist = new V3(0.56, 1.48, -0.05);
const approachCurve = new THREE.CatmullRomCurve3(
[
restWrist,
new V3(-0.15, 1.5, 0.14),
new V3(-0.92, 1.55, 0.11),
pickupWrist,
],
false,
"centripetal"
);
// Lift, carry, and lower are one uninterrupted path.
const transferCurve = new THREE.CatmullRomCurve3(
[
pickupWrist,
new V3(-1.5, 2.38, 0),
new V3(-0.8, 2.72, 0.05),
new V3(0.55, 2.72, 0.05),
new V3(1.48, 2.44, 0),
placeWrist,
],
false,
"centripetal"
);
const withdrawCurve = new THREE.CatmullRomCurve3(
[
placeWrist,
new V3(1.58, 2.42, 0.18),
new V3(1.15, 1.92, 0.36),
restWrist,
],
false,
"centripetal"
);
// -------------------------------------------------------------------------
// Analytic two-bone IK
// -------------------------------------------------------------------------
const upperLength = 1.05;
const foreLength = 1.08;
const yAxis = new V3(0, 1, 0);
function placeBeam(mesh, start, end) {
const direction = end.clone().sub(start);
const length = direction.length();
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.scale.set(1, length, 1);
}
function solveElbow(shoulder, target) {
const direction = target.clone().sub(shoulder);
const actualDistance = direction.length();
const distance = Math.min(
actualDistance,
upperLength + foreLength - 0.001
);
direction.normalize();
const along =
(upperLength * upperLength -
foreLength * foreLength +
distance * distance) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, upperLength * upperLength - along * along)
);
// The elbow bends down and slightly forward, keeping the arm readable.
const bendHint = new V3(0, -1, 0.55);
const bend = bendHint
.clone()
.addScaledVector(direction, -bendHint.dot(direction))
.normalize();
return shoulder
.clone()
.addScaledVector(direction, along)
.addScaledVector(bend, height);
}
function updateArm(wristPosition) {
character.updateMatrixWorld(true);
const shoulder = shoulderAnchor.getWorldPosition(new V3());
const elbow = solveElbow(shoulder, wristPosition);
placeBeam(upperArm, shoulder, elbow);
placeBeam(forearm, elbow, wristPosition);
shoulderJoint.position.copy(shoulder);
elbowJoint.position.copy(elbow);
wristJoint.position.copy(wristPosition);
}
function updateFingers(amount) {
const x = THREE.MathUtils.lerp(0.34, 0.235, amount);
for (const finger of fingers) {
finger.mesh.position.x = finger.side * x;
}
}
// -------------------------------------------------------------------------
// Camera controls implemented directly, without addons
// -------------------------------------------------------------------------
const orbit = {
theta: 0.68,
phi: 0.83,
zoom: 1,
target: new V3(0, 1.2, 0),
};
const desiredOrbit = {
theta: orbit.theta,
phi: orbit.phi,
zoom: orbit.zoom,
target: orbit.target.clone(),
};
function updateCamera(delta) {
const amount = 1 - Math.exp(-delta * 8);
orbit.theta = THREE.MathUtils.lerp(
orbit.theta,
desiredOrbit.theta,
amount
);
orbit.phi = THREE.MathUtils.lerp(
orbit.phi,
desiredOrbit.phi,
amount
);
orbit.zoom = THREE.MathUtils.lerp(
orbit.zoom,
desiredOrbit.zoom,
amount
);
orbit.target.lerp(desiredOrbit.target, amount);
const radius = 10;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + radius * Math.cos(orbit.phi),
orbit.target.z + radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function setWideView(immediate = false) {
desiredOrbit.target.set(0, 1.2, 0);
desiredOrbit.zoom = 1;
if (immediate) {
orbit.target.copy(desiredOrbit.target);
orbit.zoom = desiredOrbit.zoom;
}
}
function focusOnContact() {
if (state === "pausedPickup") {
desiredOrbit.target.copy(startBlockPosition);
desiredOrbit.target.y += 0.03;
} else if (state === "pausedPlacement") {
desiredOrbit.target.copy(placeBlockPosition);
desiredOrbit.target.y += 0.03;
} else {
return;
}
desiredOrbit.theta = 0.38;
desiredOrbit.phi = 0.87;
desiredOrbit.zoom = 1.7;
}
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
desiredOrbit.theta -= dx * 0.006;
desiredOrbit.phi = clamp(desiredOrbit.phi - dy * 0.005, 0.45, 1.28);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
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();
desiredOrbit.zoom = clamp(
desiredOrbit.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.6
);
},
{ passive: false }
);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height);
// The wide framing always includes both furniture pieces and the gap.
const viewHeight = Math.max(6.2, 10.2 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Animation state machine
// -------------------------------------------------------------------------
const phaseElement = document.querySelector("#phase");
const statusElement = document.querySelector("#status");
const progressElement = document.querySelector("#progress");
const continueButton = document.querySelector("#continueBtn");
const focusButton = document.querySelector("#focusBtn");
const replayButton = document.querySelector("#replayBtn");
let state = "approach";
let stateTime = 0;
let fingerAmount = 0;
let currentWrist = restWrist.clone();
const APPROACH_DURATION = 1.75;
const CLOSE_DURATION = 0.42;
const TRANSFER_DURATION = 4.1;
const RELEASE_DURATION = 0.48;
const WITHDRAW_DURATION = 1.35;
function enterState(nextState) {
state = nextState;
stateTime = 0;
updateInterface();
}
function updateInterface() {
const paused = state === "pausedPickup" || state === "pausedPlacement";
continueButton.classList.toggle("hidden", !paused);
focusButton.classList.toggle("hidden", !paused);
let phase = "";
let status = "";
let progress = 0;
switch (state) {
case "approach":
phase = "01 · Reaching";
status =
"The character is moving its open hand toward the block on the table.";
progress = 0.02 + 0.18 * smooth(stateTime / APPROACH_DURATION);
break;
case "closing":
phase = "02 · Gripping";
status =
"The fingers close around the block. The block remains on the tabletop.";
progress = 0.22;
break;
case "pausedPickup":
phase = "Paused · Pickup";
status =
"Pickup contact is paused. Inspect the closed fingers, wooden block, and tabletop.";
progress = 0.25;
break;
case "transfer":
phase = "03 · Lift and carry";
status =
"The held block rises, crosses the open gap, and lowers toward the shelf in one continuous motion.";
progress =
0.25 + 0.55 * smooth(stateTime / TRANSFER_DURATION);
break;
case "pausedPlacement":
phase = "Paused · Placement";
status =
"The block's underside is resting on the shelf while the fingers still hold it. Inspect the contact before release.";
progress = 0.82;
break;
case "release":
phase = "04 · Releasing";
status =
"The fingers open and let go. The block remains supported by the shelf.";
progress = 0.84;
break;
case "withdraw":
phase = "05 · Withdrawing";
status =
"The empty hand returns to rest without disturbing the placed block.";
progress =
0.86 + 0.14 * smooth(stateTime / WITHDRAW_DURATION);
break;
case "done":
phase = "Complete";
status =
"Transfer complete. The block is supported by the shelf and the character’s arm is withdrawn.";
progress = 1;
break;
}
phaseElement.textContent = phase;
statusElement.textContent = status;
progressElement.style.width = `${clamp(progress, 0, 1) * 100}%`;
}
function attachBlockToHand() {
gripper.updateMatrixWorld(true);
gripper.attach(block);
// Exact grip offset: the palm sits just above the block.
block.position.set(0, -GRIP_OFFSET, 0);
block.quaternion.identity();
}
function releaseBlock() {
const releasedPosition = block.getWorldPosition(new V3());
scene.attach(block);
block.position.copy(releasedPosition);
block.quaternion.identity();
}
function resetScene() {
if (block.parent !== scene) {
scene.attach(block);
}
block.position.copy(startBlockPosition);
block.quaternion.identity();
block.scale.set(1, 1, 1);
block.visible = true;
currentWrist.copy(restWrist);
gripper.position.copy(restWrist);
gripper.quaternion.identity();
fingerAmount = 0;
updateFingers(fingerAmount);
updateArm(currentWrist);
setWideView(true);
enterState("approach");
}
continueButton.addEventListener("click", () => {
setWideView(false);
if (state === "pausedPickup") {
enterState("transfer");
} else if (state === "pausedPlacement") {
enterState("release");
}
});
focusButton.addEventListener("click", focusOnContact);
replayButton.addEventListener("click", resetScene);
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
stateTime += delta;
switch (state) {
case "approach": {
const t = clamp(stateTime / APPROACH_DURATION, 0, 1);
currentWrist.copy(approachCurve.getPoint(smoother(t)));
fingerAmount = 0;
if (t >= 1) {
enterState("closing");
}
break;
}
case "closing": {
const t = clamp(stateTime / CLOSE_DURATION, 0, 1);
currentWrist.copy(pickupWrist);
fingerAmount = smoother(t);
if (t >= 1) {
fingerAmount = 1;
attachBlockToHand();
enterState("pausedPickup");
}
break;
}
case "pausedPickup": {
currentWrist.copy(pickupWrist);
fingerAmount = 1;
break;
}
case "transfer": {
const t = clamp(stateTime / TRANSFER_DURATION, 0, 1);
currentWrist.copy(transferCurve.getPoint(smoother(t)));
fingerAmount = 1;
if (t >= 1) {
currentWrist.copy(placeWrist);
gripper.position.copy(placeWrist);
block.position.set(0, -GRIP_OFFSET, 0);
enterState("pausedPlacement");
}
break;
}
case "pausedPlacement": {
currentWrist.copy(placeWrist);
fingerAmount = 1;
break;
}
case "release": {
const t = clamp(stateTime / RELEASE_DURATION, 0, 1);
currentWrist.copy(placeWrist);
fingerAmount = 1 - smoother(t);
if (t >= 1) {
fingerAmount = 0;
releaseBlock();
enterState("withdraw");
}
break;
}
case "withdraw": {
const t = clamp(stateTime / WITHDRAW_DURATION, 0, 1);
currentWrist.copy(withdrawCurve.getPoint(smoother(t)));
fingerAmount = 0;
if (t >= 1) {
currentWrist.copy(restWrist);
enterState("done");
}
break;
}
case "done": {
currentWrist.copy(restWrist);
fingerAmount = 0;
break;
}
}
gripper.position.copy(currentWrist);
gripper.quaternion.identity();
updateFingers(fingerAmount);
updateArm(currentWrist);
updateCamera(delta);
renderer.render(scene, camera);
}
updateInterface();
updateFingers(0);
updateArm(restWrist);
animate();
</script>
</body>
</html>
```