Prompt specification
Build or customize the Three.js 3D scene "Compare Readable and Abrupt Action Timing" 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: Compare Readable and Abrupt Action Timing
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compare-readable-and-abrupt-action-timing-x3692
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: compare readable and abrupt action timing. Create two matched animations of a character lifting a box. Toggle anticipation and follow-through while holding the main action path fixed to isolate their visual effect.
Working Three.js Starter (compare-readable-and-abrupt-action-timing-x3692):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Lift — Readable vs. Abrupt Timing</title>
<style>
:root {
color-scheme: dark;
--bg: #111827;
--panel: #1b2537;
--panel-2: #222e43;
--line: #35435b;
--text: #f7f9fc;
--muted: #aebbd0;
--accent: #55d6be;
--accent-off: #64748b;
--orange: #f6ad42;
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
padding: 24px;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1440px, 100%);
margin: 0 auto;
}
header {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 18px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font-size: 0.72rem;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.7rem);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 690px;
margin: 10px 0 0;
color: var(--muted);
font-size: 0.94rem;
line-height: 1.5;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.tool-button {
min-height: 38px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
cursor: pointer;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
.tool-button:hover {
background: var(--panel-2);
border-color: #52627d;
}
.tool-button:active {
transform: translateY(1px);
}
.tool-button.primary {
color: #09241e;
border-color: transparent;
background: var(--accent);
font-weight: 800;
}
.tool-button.primary:hover {
background: #78e3d0;
}
.path-toggle {
display: inline-flex;
align-items: center;
gap: 8px;
min-height: 38px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 9px;
background: var(--panel);
color: var(--muted);
cursor: pointer;
user-select: none;
}
.path-toggle input {
accent-color: var(--orange);
}
.timing-controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 14px;
margin-bottom: 14px;
}
.timing-card {
padding: 15px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
}
.timing-card.readable {
border-top: 3px solid var(--accent);
}
.timing-card.abrupt {
border-top: 3px solid #8390a6;
}
.timing-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.timing-heading h2 {
margin: 0;
font-size: 1rem;
}
.timing-heading span {
color: var(--muted);
font-size: 0.76rem;
}
.toggle-row {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.toggle {
display: inline-grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 9px;
min-height: 42px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: #151e2e;
cursor: pointer;
transition:
border-color 140ms ease,
background 140ms ease;
}
.toggle:hover {
border-color: #566680;
}
.toggle.is-on {
border-color: rgba(85, 214, 190, 0.72);
background: rgba(85, 214, 190, 0.1);
}
.toggle .track {
position: relative;
width: 30px;
height: 17px;
border-radius: 999px;
background: #3a465b;
transition: background 140ms ease;
}
.toggle .track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 11px;
height: 11px;
border-radius: 50%;
background: #cbd5e1;
transition:
left 140ms ease,
background 140ms ease;
}
.toggle.is-on .track {
background: rgba(85, 214, 190, 0.65);
}
.toggle.is-on .track::after {
left: 16px;
background: #effffb;
}
.toggle b {
min-width: 25px;
color: var(--muted);
font-size: 0.68rem;
letter-spacing: 0.06em;
text-align: right;
}
.toggle.is-on b {
color: var(--accent);
}
.stage {
position: relative;
height: clamp(520px, 66vh, 700px);
overflow: hidden;
border: 1px solid var(--line);
border-radius: 15px;
background: #dfe6ee;
box-shadow: 0 22px 60px rgba(0, 0, 0, 0.25);
}
.stage canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.stage canvas:focus-visible {
outline: 3px solid var(--accent);
outline-offset: -4px;
}
.divider {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
z-index: 2;
width: 2px;
background: rgba(35, 48, 68, 0.32);
pointer-events: none;
}
.panel-label {
position: absolute;
top: 16px;
z-index: 3;
min-width: 150px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.48);
border-radius: 10px;
background: rgba(20, 30, 46, 0.78);
box-shadow: 0 8px 25px rgba(24, 35, 52, 0.15);
backdrop-filter: blur(8px);
pointer-events: none;
}
.panel-label.left {
left: 16px;
}
.panel-label.right {
right: 16px;
text-align: right;
}
.panel-label .name {
display: block;
color: #fff;
font-size: 0.78rem;
font-weight: 900;
letter-spacing: 0.12em;
}
.panel-label.readable .name {
color: #8aefdc;
}
.panel-label.abrupt .name {
color: #d1d9e6;
}
.panel-label .phase {
display: block;
margin-top: 4px;
color: #bdc8d9;
font-size: 0.72rem;
}
.stage.stacked {
height: 780px;
}
.stage.stacked .divider {
top: 50%;
right: 0;
bottom: auto;
left: 0;
width: auto;
height: 2px;
}
.stage.stacked .panel-label.right {
top: calc(50% + 16px);
right: 16px;
left: 16px;
}
.note {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
gap: 8px 18px;
margin: 12px 2px 0;
color: var(--muted);
font-size: 0.79rem;
line-height: 1.5;
}
.note strong {
color: #dce5f2;
}
@media (max-width: 760px) {
body {
padding: 14px;
}
header {
align-items: flex-start;
flex-direction: column;
gap: 14px;
}
.toolbar {
width: 100%;
}
.timing-controls {
grid-template-columns: 1fr;
}
.stage {
height: 780px;
}
.divider {
top: 50%;
right: 0;
bottom: auto;
left: 0;
width: auto;
height: 2px;
}
.panel-label.right {
top: calc(50% + 16px);
right: 16px;
left: 16px;
}
}
</style>
</head>
<body>
<main class="app">
<header>
<div>
<p class="eyebrow">Voxel Motion Study</p>
<h1>Readable vs. Abrupt Lift</h1>
<p class="subtitle">
Two matched voxel characters share exactly the same lift route and
action timing. Toggle the lead-in and settle phases to isolate how
anticipation and follow-through change the same movement.
</p>
</div>
<div class="toolbar" aria-label="Playback and view controls">
<button class="tool-button primary" id="pause-button" type="button">
Pause
</button>
<button class="tool-button" id="restart-button" type="button">
Restart
</button>
<button class="tool-button" id="camera-button" type="button">
Reset Camera
</button>
<button class="tool-button" id="copy-button" type="button">
Copy Left → Right
</button>
<label class="path-toggle">
<input id="path-checkbox" type="checkbox" checked />
Show lift path
</label>
</div>
</header>
<section class="timing-controls" aria-label="Timing controls">
<article class="timing-card readable">
<div class="timing-heading">
<h2>Readable timing</h2>
<span>Intent → action → settle</span>
</div>
<div class="toggle-row">
<button
class="toggle is-on"
id="readable-anticipation"
type="button"
aria-pressed="true"
>
<span class="track" aria-hidden="true"></span>
<span>Anticipation</span>
<b>ON</b>
</button>
<button
class="toggle is-on"
id="readable-follow"
type="button"
aria-pressed="true"
>
<span class="track" aria-hidden="true"></span>
<span>Follow-through</span>
<b>ON</b>
</button>
</div>
</article>
<article class="timing-card abrupt">
<div class="timing-heading">
<h2>Abrupt timing</h2>
<span>No lead-in · hard stop</span>
</div>
<div class="toggle-row">
<button
class="toggle"
id="abrupt-anticipation"
type="button"
aria-pressed="false"
>
<span class="track" aria-hidden="true"></span>
<span>Anticipation</span>
<b>OFF</b>
</button>
<button
class="toggle"
id="abrupt-follow"
type="button"
aria-pressed="false"
>
<span class="track" aria-hidden="true"></span>
<span>Follow-through</span>
<b>OFF</b>
</button>
</div>
</article>
</section>
<section class="stage" id="stage" aria-label="Two-panel voxel animation">
<div class="divider" aria-hidden="true"></div>
<div class="panel-label left readable">
<span class="name">READABLE TIMING</span>
<span class="phase" id="readable-phase">Ready</span>
</div>
<div class="panel-label right abrupt">
<span class="name">ABRUPT TIMING</span>
<span class="phase" id="abrupt-phase">Ready</span>
</div>
</section>
<p class="note">
<span>
<strong>Controlled comparison:</strong>
disabling both switches on the left makes the two panels match
frame-for-frame.
</span>
<span>Drag to orbit · Scroll to zoom · Arrow keys also control the camera</span>
</p>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and state
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const pauseButton = document.getElementById("pause-button");
const restartButton = document.getElementById("restart-button");
const cameraButton = document.getElementById("camera-button");
const copyButton = document.getElementById("copy-button");
const pathCheckbox = document.getElementById("path-checkbox");
const readablePhase = document.getElementById("readable-phase");
const abruptPhase = document.getElementById("abrupt-phase");
const timing = {
readable: {
anticipation: true,
follow: true
},
abrupt: {
anticipation: false,
follow: false
}
};
const buttons = {
readable: {
anticipation: document.getElementById("readable-anticipation"),
follow: document.getElementById("readable-follow")
},
abrupt: {
anticipation: document.getElementById("abrupt-anticipation"),
follow: document.getElementById("abrupt-follow")
}
};
function updateToggleAppearance(button, enabled) {
button.classList.toggle("is-on", enabled);
button.setAttribute("aria-pressed", String(enabled));
button.querySelector("b").textContent = enabled ? "ON" : "OFF";
}
function refreshToggleButtons() {
for (const side of ["readable", "abrupt"]) {
updateToggleAppearance(
buttons[side].anticipation,
timing[side].anticipation
);
updateToggleAppearance(
buttons[side].follow,
timing[side].follow
);
}
}
for (const side of ["readable", "abrupt"]) {
buttons[side].anticipation.addEventListener("click", () => {
timing[side].anticipation = !timing[side].anticipation;
refreshToggleButtons();
});
buttons[side].follow.addEventListener("click", () => {
timing[side].follow = !timing[side].follow;
refreshToggleButtons();
});
}
copyButton.addEventListener("click", () => {
timing.abrupt.anticipation = timing.readable.anticipation;
timing.abrupt.follow = timing.readable.follow;
refreshToggleButtons();
});
// -------------------------------------------------------------------------
// Renderer and custom camera controls
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.autoClear = false;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Voxel animation. Drag or use arrow keys to orbit, scroll to zoom."
);
stage.prepend(renderer.domElement);
const cameraState = {
yaw: 0.38,
pitch: 0.24,
zoom: 1.04
};
const defaultCameraState = { ...cameraState };
let dragging = false;
let previousPointer = { x: 0, y: 0 };
function clamp(value, minimum, maximum) {
return Math.max(minimum, Math.min(maximum, value));
}
function resetCamera() {
cameraState.yaw = defaultCameraState.yaw;
cameraState.pitch = defaultCameraState.pitch;
cameraState.zoom = defaultCameraState.zoom;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
previousPointer.x = event.clientX;
previousPointer.y = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointer.x;
const deltaY = event.clientY - previousPointer.y;
cameraState.yaw -= deltaX * 0.006;
cameraState.pitch = clamp(
cameraState.pitch + deltaY * 0.0045,
0.04,
0.78
);
previousPointer.x = event.clientX;
previousPointer.y = event.clientY;
});
function endDrag(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
event.pointerId !== undefined
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.65
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
cameraState.yaw += 0.08;
break;
case "ArrowRight":
cameraState.yaw -= 0.08;
break;
case "ArrowUp":
cameraState.pitch = clamp(cameraState.pitch + 0.05, 0.04, 0.78);
break;
case "ArrowDown":
cameraState.pitch = clamp(cameraState.pitch - 0.05, 0.04, 0.78);
break;
case "+":
case "=":
cameraState.zoom = clamp(cameraState.zoom + 0.06, 0.68, 1.65);
break;
case "-":
case "_":
cameraState.zoom = clamp(cameraState.zoom - 0.06, 0.68, 1.65);
break;
default:
handled = false;
}
if (handled) event.preventDefault();
});
cameraButton.addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Shared geometry and materials
// -------------------------------------------------------------------------
const COLORS = {
background: 0xdfe6ee,
platform: 0xb8c4d0,
platformSide: 0x95a5b7,
grid: 0x9baabd,
gridCenter: 0x8293a8,
shirt: 0x347fe5,
shirtDark: 0x245db0,
pants: 0x25324a,
shoe: 0x172033,
skin: 0xf1b98e,
skinLight: 0xffd1aa,
hair: 0x3b2c31,
eye: 0x172033,
box: 0xee9d32,
boxDark: 0xc97920,
tape: 0xffd68a,
edge: 0x725126
};
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0,
...options
});
}
const materials = {
platform: standardMaterial(COLORS.platform),
platformSide: standardMaterial(COLORS.platformSide),
shirt: standardMaterial(COLORS.shirt),
shirtDark: standardMaterial(COLORS.shirtDark),
pants: standardMaterial(COLORS.pants),
shoe: standardMaterial(COLORS.shoe),
skin: standardMaterial(COLORS.skin),
skinLight: standardMaterial(COLORS.skinLight),
hair: standardMaterial(COLORS.hair),
eye: standardMaterial(COLORS.eye),
box: standardMaterial(COLORS.box),
boxDark: standardMaterial(COLORS.boxDark),
tape: standardMaterial(COLORS.tape)
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.set(position.x, position.y, position.z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeCamera() {
const camera = new THREE.OrthographicCamera(-3, 3, 2.5, -2.5, 0.1, 50);
return camera;
}
// -------------------------------------------------------------------------
// Voxel scene construction
// -------------------------------------------------------------------------
function createCharacter(scene) {
const root = new THREE.Group();
scene.add(root);
const upper = new THREE.Group();
upper.position.y = 1.02;
root.add(upper);
// Pelvis and torso.
addBox(
upper,
{ x: 0.57, y: 0.29, z: 0.36 },
{ x: 0, y: 0.02, z: 0 },
materials.pants
);
addBox(
upper,
{ x: 0.72, y: 0.72, z: 0.43 },
{ x: 0, y: 0.45, z: 0 },
materials.shirt
);
// Small shirt collar.
addBox(
upper,
{ x: 0.25, y: 0.09, z: 0.45 },
{ x: 0, y: 0.81, z: 0.015 },
materials.shirtDark
);
// Head group permits a small expressive nod.
const head = new THREE.Group();
head.position.set(0, 1.13, 0);
upper.add(head);
addBox(
head,
{ x: 0.52, y: 0.5, z: 0.48 },
{ x: 0, y: 0, z: 0 },
materials.skin
);
addBox(
head,
{ x: 0.55, y: 0.13, z: 0.5 },
{ x: 0, y: 0.22, z: -0.01 },
materials.hair
);
// Pixel-style eyes and nose, facing +Z.
addBox(
head,
{ x: 0.075, y: 0.085, z: 0.025 },
{ x: -0.115, y: 0.015, z: 0.252 },
materials.eye
);
addBox(
head,
{ x: 0.075, y: 0.085, z: 0.025 },
{ x: 0.115, y: 0.015, z: 0.252 },
materials.eye
);
addBox(
head,
{ x: 0.075, y: 0.075, z: 0.055 },
{ x: 0, y: -0.075, z: 0.266 },
materials.skinLight
);
// Static block legs and shoes.
for (const side of [-1, 1]) {
addBox(
root,
{ x: 0.25, y: 0.49, z: 0.28 },
{ x: side * 0.18, y: 0.7, z: 0 },
materials.pants
);
addBox(
root,
{ x: 0.22, y: 0.4, z: 0.25 },
{ x: side * 0.18, y: 0.25, z: 0.01 },
materials.pants
);
addBox(
root,
{ x: 0.29, y: 0.13, z: 0.39 },
{ x: side * 0.18, y: 0.075, z: 0.075 },
materials.shoe
);
}
function createArm(side) {
const shoulder = new THREE.Group();
shoulder.position.set(side * 0.49, 0.69, 0);
upper.add(shoulder);
addBox(
shoulder,
{ x: 0.2, y: 0.47, z: 0.2 },
{ x: 0, y: -0.235, z: 0 },
materials.shirt
);
const elbow = new THREE.Group();
elbow.position.set(0, -0.47, 0);
shoulder.add(elbow);
addBox(
elbow,
{ x: 0.18, y: 0.4, z: 0.18 },
{ x: 0, y: -0.2, z: 0 },
materials.skin
);
addBox(
elbow,
{ x: 0.22, y: 0.18, z: 0.22 },
{ x: 0, y: -0.46, z: 0 },
materials.skinLight
);
return { shoulder, elbow };
}
const leftArm = createArm(-1);
const rightArm = createArm(1);
// The box is held separately so its main route remains explicit.
const box = new THREE.Group();
root.add(box);
const boxBody = addBox(
box,
{ x: 0.8, y: 0.72, z: 0.68 },
{ x: 0, y: 0, z: 0 },
materials.box
);
addBox(
box,
{ x: 0.12, y: 0.735, z: 0.695 },
{ x: 0, y: 0, z: 0 },
materials.tape
);
addBox(
box,
{ x: 0.815, y: 0.735, z: 0.11 },
{ x: 0, y: 0, z: 0 },
materials.tape
);
const boxEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(boxBody.geometry),
new THREE.LineBasicMaterial({
color: COLORS.edge,
transparent: true,
opacity: 0.62
})
);
box.add(boxEdges);
return {
root,
upper,
head,
leftArm,
rightArm,
box
};
}
function createScene() {
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.background);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x68778d, 2.25);
scene.add(hemisphere);
const key = new THREE.DirectionalLight(0xfff4df, 3.25);
key.position.set(4.5, 7.5, 5.5);
key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
key.shadow.camera.left = -4;
key.shadow.camera.right = 4;
key.shadow.camera.top = 6;
key.shadow.camera.bottom = -2;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 20;
key.shadow.bias = -0.0005;
scene.add(key);
const fill = new THREE.DirectionalLight(0xc9ddff, 1.05);
fill.position.set(-4, 3.5, 2.5);
scene.add(fill);
// Voxel platform.
addBox(
scene,
{ x: 4.45, y: 0.22, z: 4.45 },
{ x: 0, y: -0.14, z: 0 },
materials.platformSide,
false
);
addBox(
scene,
{ x: 4.4, y: 0.035, z: 4.4 },
{ x: 0, y: -0.012, z: 0 },
materials.platform,
false
);
const grid = new THREE.GridHelper(4.4, 11, COLORS.gridCenter, COLORS.grid);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
// Subtle corner blocks provide scale without distracting from the lift.
addBox(
scene,
{ x: 0.38, y: 0.38, z: 0.38 },
{ x: -1.62, y: 0.19, z: -1.45 },
materials.platformSide,
false
);
addBox(
scene,
{ x: 0.38, y: 0.72, z: 0.38 },
{ x: 1.65, y: 0.36, z: -1.5 },
materials.platformSide,
false
);
const character = createCharacter(scene);
// Main action path: waist/front to overhead.
const pathPoints = [];
for (let index = 0; index <= 48; index++) {
const p = index / 48;
pathPoints.push(
new THREE.Vector3(
0,
THREE.MathUtils.lerp(1.16, 2.98, p),
THREE.MathUtils.lerp(0.62, 0.08, p)
)
);
}
const pathGeometry = new THREE.BufferGeometry().setFromPoints(pathPoints);
const pathMaterial = new THREE.LineDashedMaterial({
color: COLORS.boxDark,
dashSize: 0.08,
gapSize: 0.06,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const pathLine = new THREE.Line(pathGeometry, pathMaterial);
pathLine.computeLineDistances();
scene.add(pathLine);
return {
scene,
character,
pathLine,
camera: makeCamera()
};
}
const readable = createScene();
const abrupt = createScene();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const DURATION = 4.8;
const ACTION_START = 0.2;
const ACTION_END = 0.66;
const FOLLOW_END = 0.84;
let elapsed = 0;
let paused = false;
let previousFrame = performance.now();
function smoothstep(value) {
return value * value * (3 - 2 * value);
}
function phaseFor(time, options) {
if (time < ACTION_START) {
return options.anticipation ? "Anticipation" : "Ready";
}
if (time < ACTION_END) {
return "Main lift";
}
if (options.follow && time < FOLLOW_END) {
return "Follow-through";
}
return "Hold";
}
function applyCharacter(rig, normalizedTime, options) {
const actionProgress = clamp(
(normalizedTime - ACTION_START) / (ACTION_END - ACTION_START),
0,
1
);
/*
* The action curve is intentionally identical in both panels.
* With linear action progress, the start and stop are abrupt.
* Anticipation and follow-through are the only variables.
*/
const p = actionProgress;
let anticipation = 0;
if (options.anticipation && normalizedTime < ACTION_START) {
const windup = clamp(normalizedTime / ACTION_START, 0, 1);
anticipation = Math.sin(Math.PI * windup) ** 2;
}
let followThrough = 0;
if (options.follow && normalizedTime > ACTION_END) {
const settle = clamp(
(normalizedTime - ACTION_END) / (FOLLOW_END - ACTION_END),
0,
1
);
// A small upward overshoot that settles back to the main endpoint.
followThrough = 4 * settle * (1 - settle);
}
// Shared main action route.
const boxX = 0;
const boxY = THREE.MathUtils.lerp(1.16, 2.98, p);
const boxZ = THREE.MathUtils.lerp(0.62, 0.08, p);
// Optional extra poses.
const finalBoxY =
boxY - anticipation * 0.07 + followThrough * 0.13;
const finalBoxZ =
boxZ - anticipation * 0.11 + followThrough * 0.06;
rig.character.box.position.set(boxX, finalBoxY, finalBoxZ);
// Upper body dips before the lift and counters the settle afterward.
rig.character.upper.position.y =
1.02 - anticipation * 0.07;
rig.character.upper.rotation.x =
-anticipation * 0.055 - p * 0.018 + followThrough * 0.045;
rig.character.head.rotation.x =
anticipation * 0.075 - followThrough * 0.055;
const shoulderAngle = THREE.MathUtils.lerp(-0.44, -2.55, p);
const elbowAngle = THREE.MathUtils.lerp(-0.46, -0.2, p);
const finalShoulderAngle =
shoulderAngle + anticipation * 0.1 - followThrough * 0.11;
const finalElbowAngle =
elbowAngle + anticipation * 0.055 - followThrough * 0.035;
rig.character.leftArm.shoulder.rotation.x = finalShoulderAngle;
rig.character.rightArm.shoulder.rotation.x = finalShoulderAngle;
rig.character.leftArm.elbow.rotation.x = finalElbowAngle;
rig.character.rightArm.elbow.rotation.x = finalElbowAngle;
}
function updatePhaseLabel(element, label) {
if (element.textContent !== label) {
element.textContent = label;
}
}
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
});
restartButton.addEventListener("click", () => {
elapsed = 0;
});
pathCheckbox.addEventListener("change", () => {
readable.pathLine.visible = pathCheckbox.checked;
abrupt.pathLine.visible = pathCheckbox.checked;
});
// -------------------------------------------------------------------------
// Layout and split-screen rendering
// -------------------------------------------------------------------------
let stacked = false;
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
stacked = width < 760;
stage.classList.toggle("stacked", stacked);
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
function configureCamera(camera, aspect) {
const verticalSize = 4.9;
camera.left = (-verticalSize * aspect) / 2;
camera.right = (verticalSize * aspect) / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
const distance = 10;
const target = new THREE.Vector3(0, 1.55, 0);
camera.position.set(
target.x + Math.sin(cameraState.yaw) * Math.cos(cameraState.pitch) * distance,
target.y + Math.sin(cameraState.pitch) * distance,
target.z + Math.cos(cameraState.yaw) * Math.cos(cameraState.pitch) * distance
);
camera.lookAt(target);
}
function renderPanel(rig, viewport) {
renderer.setViewport(viewport.x, viewport.y, viewport.width, viewport.height);
renderer.setScissor(viewport.x, viewport.y, viewport.width, viewport.height);
renderer.clear(true, true, true);
configureCamera(rig.camera, viewport.width / viewport.height);
renderer.render(rig.scene, rig.camera);
}
function render() {
const width = stage.clientWidth;
const height = stage.clientHeight;
let viewports;
if (stacked) {
const halfHeight = Math.floor(height / 2);
viewports = [
{
x: 0,
y: height - halfHeight,
width,
height: halfHeight
},
{
x: 0,
y: 0,
width,
height: height - halfHeight
}
];
} else {
const halfWidth = Math.floor(width / 2);
viewports = [
{
x: 0,
y: 0,
width: halfWidth,
height
},
{
x: halfWidth,
y: 0,
width: width - halfWidth,
height
}
];
}
renderer.setScissorTest(true);
renderPanel(readable, viewports[0]);
renderPanel(abrupt, viewports[1]);
}
function frame(now) {
requestAnimationFrame(frame);
const delta = Math.min((now - previousFrame) / 1000, 0.05);
previousFrame = now;
if (!paused) {
elapsed += delta;
}
const normalizedTime = (elapsed % DURATION) / DURATION;
applyCharacter(readable, normalizedTime, timing.readable);
applyCharacter(abrupt, normalizedTime, timing.abrupt);
updatePhaseLabel(
readablePhase,
phaseFor(normalizedTime, timing.readable)
);
updatePhaseLabel(
abruptPhase,
phaseFor(normalizedTime, timing.abrupt)
);
render();
}
refreshToggleButtons();
requestAnimationFrame(frame);
</script>
</body>
</html>
```