Prompt specification
Build or customize the Three.js 3D scene "Alternating Hops Through Four Tiles" 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: Alternating Hops Through Four Tiles
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/alternating-hops-through-four-tiles-x3618
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this voxel scene: Animate a stylized figure following a short hop pattern with visible takeoff and landing. Keep feet aligned with the selected tiles and preserve the support sequence.
Working Three.js Starter (alternating-hops-through-four-tiles-x3618):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Hop Sequence</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(12, 19, 31, 0.86);
--border: rgba(157, 220, 255, 0.18);
--text: #eaf7ff;
--muted: #91a9bb;
--accent: #69e6c1;
--accent-2: #ffbd66;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #09111d;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
position: absolute;
top: 18px;
left: 18px;
width: min(360px, calc(100vw - 36px));
padding: 18px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
pointer-events: auto;
}
.eyebrow {
margin-bottom: 6px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
line-height: 1.1;
letter-spacing: -0.03em;
}
.instructions {
margin: 10px 0 14px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.instructions strong {
color: var(--text);
font-weight: 650;
}
.route {
min-height: 38px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 12px;
background: rgba(0, 0, 0, 0.16);
color: var(--accent);
font-size: 13px;
font-weight: 750;
letter-spacing: 0.06em;
overflow-wrap: anywhere;
}
.status-row {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 13px;
color: var(--muted);
font-size: 12px;
}
#status {
color: var(--text);
font-weight: 650;
}
.progress {
height: 5px;
margin-top: 8px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
#progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--accent), var(--accent-2));
transition: width 80ms linear;
}
.controls {
position: absolute;
right: 18px;
bottom: 18px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: min(620px, calc(100vw - 36px));
padding: 10px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.06);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(105, 230, 193, 0.45);
background: rgba(105, 230, 193, 0.11);
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: rgba(105, 230, 193, 0.45);
background: rgba(105, 230, 193, 0.16);
color: #baffeb;
}
.camera-hint {
position: absolute;
left: 18px;
bottom: 18px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 12px;
background: rgba(12, 19, 31, 0.7);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(12px);
}
@media (max-width: 720px) {
.panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 14px;
}
h1 {
font-size: 20px;
}
.instructions {
margin-bottom: 10px;
}
.controls {
right: 10px;
bottom: 10px;
left: 10px;
justify-content: center;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel hopping scene"></canvas>
<div class="hud">
<section class="panel">
<div class="eyebrow">Voxel locomotion lab</div>
<h1>Hop Sequence</h1>
<p class="instructions">
<strong>Click tiles</strong> to append supports. Click a numbered tile to remove it
without changing the remaining order. The figure takes off, tucks its feet,
and lands with both feet aligned inside the next selected tile.
</p>
<div id="route" class="route">01 → 02 → 03 → 04 → 05 → 06</div>
<div class="status-row">
<span id="status">Ready</span>
<span id="support-count">6 supports</span>
</div>
<div class="progress"><div id="progress-fill"></div></div>
</section>
<div class="controls">
<button id="play" class="primary">Pause</button>
<button id="replay">Replay</button>
<button id="undo">Undo</button>
<button id="default-route">Default route</button>
<button id="clear-route">Clear</button>
<button id="reset-camera">Reset camera</button>
</div>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Click a tile to edit the route</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const routeElement = document.querySelector("#route");
const statusElement = document.querySelector("#status");
const supportCountElement = document.querySelector("#support-count");
const progressFill = document.querySelector("#progress-fill");
const playButton = document.querySelector("#play");
const GRID_SIZE = 9;
const TILE_SIZE = 0.98;
const TILE_SPACING = 1.08;
const TILE_TOP = 0.18;
const HOP_DURATION = 1.12;
const HOP_HEIGHT = 1.0;
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x091321);
scene.fog = new THREE.Fog(0x091321, 15, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0.65, 0);
let cameraAzimuth = 0.72;
let cameraElevation = 0.96;
let cameraRadius = 13.4;
let desiredAzimuth = cameraAzimuth;
let desiredElevation = cameraElevation;
let desiredRadius = cameraRadius;
function positionCamera(immediate = false) {
const sinElevation = Math.sin(cameraElevation);
const targetPosition = new THREE.Vector3(
Math.sin(cameraAzimuth) * sinElevation * cameraRadius,
Math.cos(cameraElevation) * cameraRadius,
Math.cos(cameraAzimuth) * sinElevation * cameraRadius
).add(cameraTarget);
if (immediate) {
camera.position.copy(targetPosition);
}
camera.lookAt(cameraTarget);
}
positionCamera(true);
const hemisphereLight = new THREE.HemisphereLight(0x9edcff, 0x243044, 1.8);
scene.add(hemisphereLight);
const keyLight = new THREE.DirectionalLight(0xfff1d6, 3.1);
keyLight.position.set(6, 11, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5ca8ff, 1.35);
rimLight.position.set(-7, 5, -8);
scene.add(rimLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0d1928,
roughness: 0.92,
metalness: 0.05
});
const ground = new THREE.Mesh(new THREE.PlaneGeometry(60, 60), groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.34;
ground.receiveShadow = true;
scene.add(ground);
const board = new THREE.Mesh(
new THREE.BoxGeometry(11.4, 0.3, 11.4),
new THREE.MeshStandardMaterial({
color: 0x132338,
roughness: 0.78,
metalness: 0.12
})
);
board.position.y = -0.17;
board.receiveShadow = true;
board.castShadow = true;
scene.add(board);
const boardInset = new THREE.Mesh(
new THREE.BoxGeometry(10.75, 0.035, 10.75),
new THREE.MeshStandardMaterial({
color: 0x0b1725,
roughness: 0.9
})
);
boardInset.position.y = -0.005;
boardInset.receiveShadow = true;
scene.add(boardInset);
const tiles = [];
const tileMeshes = [];
const tileByKey = new Map();
function tilePosition(column, row) {
return new THREE.Vector3(
(column - (GRID_SIZE - 1) / 2) * TILE_SPACING,
0,
(row - (GRID_SIZE - 1) / 2) * TILE_SPACING
);
}
for (let row = 0; row < GRID_SIZE; row++) {
for (let column = 0; column < GRID_SIZE; column++) {
const group = new THREE.Group();
group.position.copy(tilePosition(column, row));
const material = new THREE.MeshStandardMaterial({
color: 0x1a2a3e,
emissive: 0x000000,
emissiveIntensity: 0,
roughness: 0.74,
metalness: 0.12
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(TILE_SIZE, TILE_TOP, TILE_SIZE),
material
);
mesh.position.y = TILE_TOP / 2;
mesh.receiveShadow = true;
mesh.castShadow = true;
const tile = {
key: `${column},${row}`,
column,
row,
group,
mesh,
material,
position: group.position.clone(),
label: null
};
mesh.userData.tile = tile;
group.add(mesh);
scene.add(group);
tiles.push(tile);
tileMeshes.push(mesh);
tileByKey.set(tile.key, tile);
}
}
// Small voxel structures frame the play area.
const decorMaterial = new THREE.MeshStandardMaterial({
color: 0x1c3348,
roughness: 0.7,
metalness: 0.2
});
const decorLightMaterial = new THREE.MeshStandardMaterial({
color: 0x315c74,
emissive: 0x153548,
emissiveIntensity: 0.6,
roughness: 0.55
});
for (const side of [-1, 1]) {
for (let i = -4; i <= 4; i += 2) {
const pillar = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.85 + (i + 4) * 0.05, 0.22),
decorMaterial
);
pillar.position.set(i * 1.05, 0.3, side * 5.25);
pillar.castShadow = true;
scene.add(pillar);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.12, 0.32),
decorLightMaterial
);
cap.position.set(i * 1.05, pillar.position.y + 0.48, side * 5.25);
scene.add(cap);
}
}
const routeGroup = new THREE.Group();
scene.add(routeGroup);
let route = [];
let hoveredTile = null;
let playing = true;
let elapsed = 0;
let takeoffFired = new Set();
let impactFired = new Set();
function makeOrderSprite(number) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 128;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 128, 128);
context.beginPath();
context.arc(64, 64, 48, 0, Math.PI * 2);
context.fillStyle = "#dffff4";
context.fill();
context.lineWidth = 9;
context.strokeStyle = "#203f50";
context.stroke();
context.fillStyle = "#15343d";
context.font = "800 54px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(String(number), 64, 67);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.setScalar(0.48);
return sprite;
}
function disposeRouteLines() {
while (routeGroup.children.length) {
const child = routeGroup.children.pop();
child.geometry?.dispose();
child.material?.dispose();
}
}
function rebuildRouteVisuals() {
for (const tile of tiles) {
if (tile.label) {
tile.group.remove(tile.label);
tile.label.material.map?.dispose();
tile.label.material.dispose();
tile.label = null;
}
}
disposeRouteLines();
route.forEach((tile, index) => {
const hue = (0.47 + index * 0.055) % 1;
const color = new THREE.Color().setHSL(hue, 0.72, 0.48);
tile.material.color.copy(color);
tile.material.emissive.copy(color).multiplyScalar(0.22);
tile.material.emissiveIntensity = 0.28;
const label = makeOrderSprite(index + 1);
label.position.set(0, 0.77, 0);
tile.group.add(label);
tile.label = label;
});
for (let i = 0; i < route.length - 1; i++) {
const a = route[i].position;
const b = route[i + 1].position;
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const line = new THREE.Mesh(
new THREE.BoxGeometry(length, 0.035, 0.065),
new THREE.MeshBasicMaterial({
color: 0x7df6d5,
transparent: true,
opacity: 0.34,
depthWrite: false
})
);
line.position.set(
(a.x + b.x) / 2,
TILE_TOP + 0.022,
(a.z + b.z) / 2
);
line.rotation.y = -Math.atan2(dz, dx);
routeGroup.add(line);
}
updateRouteUI();
}
function updateRouteUI() {
routeElement.textContent = route.length
? route.map((_, index) => String(index + 1).padStart(2, "0")).join(" → ")
: "No supports selected";
supportCountElement.textContent = `${route.length} support${route.length === 1 ? "" : "s"}`;
playButton.textContent = playing ? "Pause" : "Play";
}
function resetAnimation() {
elapsed = 0;
takeoffFired = new Set();
impactFired = new Set();
contactPulse.age = 999;
contactPulse.group.visible = false;
updateRouteUI();
}
function setRoute(nextRoute) {
route = nextRoute.slice();
rebuildRouteVisuals();
resetAnimation();
}
function toggleTile(tile) {
const index = route.indexOf(tile);
if (index >= 0) {
route.splice(index, 1);
} else {
route.push(tile);
}
rebuildRouteVisuals();
resetAnimation();
}
// Character rig.
const character = new THREE.Group();
scene.add(character);
const skinMaterial = new THREE.MeshStandardMaterial({
color: 0xf1bd91,
roughness: 0.78
});
const jacketMaterial = new THREE.MeshStandardMaterial({
color: 0xf06b4f,
roughness: 0.62,
metalness: 0.03
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x172638,
roughness: 0.72
});
const pantsMaterial = new THREE.MeshStandardMaterial({
color: 0x29475c,
roughness: 0.82
});
const shoeMaterial = new THREE.MeshStandardMaterial({
color: 0xeff8ff,
roughness: 0.55
});
const soleMaterial = new THREE.MeshStandardMaterial({
color: 0x45e2bf,
emissive: 0x123e37,
emissiveIntensity: 0.45,
roughness: 0.55
});
const hairMaterial = new THREE.MeshStandardMaterial({
color: 0x26303c,
roughness: 0.85
});
const eyeMaterial = new THREE.MeshBasicMaterial({ color: 0x111820 });
const bodyRoot = new THREE.Group();
character.add(bodyRoot);
const pelvis = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.22, 0.38),
pantsMaterial
);
pelvis.castShadow = true;
bodyRoot.add(pelvis);
const torso = new THREE.Mesh(
new THREE.BoxGeometry(0.68, 0.68, 0.4),
jacketMaterial
);
torso.position.y = 0.4;
torso.castShadow = true;
torso.receiveShadow = true;
bodyRoot.add(torso);
const chestStripe = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.58, 0.415),
soleMaterial
);
chestStripe.position.set(0.13, 0.4, 0.005);
bodyRoot.add(chestStripe);
const neck = new THREE.Mesh(
new THREE.BoxGeometry(0.2, 0.14, 0.2),
skinMaterial
);
neck.position.y = 0.8;
bodyRoot.add(neck);
const head = new THREE.Group();
head.position.y = 1.08;
bodyRoot.add(head);
const headMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.58, 0.56),
skinMaterial
);
headMesh.castShadow = true;
head.add(headMesh);
const hair = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.16, 0.6),
hairMaterial
);
hair.position.y = 0.25;
hair.castShadow = true;
head.add(hair);
for (const side of [-1, 1]) {
const eye = new THREE.Mesh(
new THREE.BoxGeometry(0.07, 0.09, 0.035),
eyeMaterial
);
eye.position.set(side * 0.13, 0.055, 0.292);
head.add(eye);
}
const nose = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.08, 0.08),
skinMaterial
);
nose.position.set(0, -0.035, 0.31);
head.add(nose);
const mouth = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.035, 0.025),
eyeMaterial
);
mouth.position.set(0, -0.14, 0.293);
head.add(mouth);
function makeArm(side) {
const arm = new THREE.Group();
arm.position.set(side * 0.44, 0.64, 0);
bodyRoot.add(arm);
const sleeve = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.37, 0.2),
jacketMaterial
);
sleeve.position.y = -0.18;
sleeve.castShadow = true;
arm.add(sleeve);
const forearm = new THREE.Group();
forearm.position.y = -0.35;
arm.add(forearm);
const forearmMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.34, 0.18),
jacketMaterial
);
forearmMesh.position.y = -0.17;
forearmMesh.castShadow = true;
forearm.add(forearmMesh);
const hand = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 0.18, 0.2),
skinMaterial
);
hand.position.y = -0.4;
hand.castShadow = true;
forearm.add(hand);
return { arm, forearm };
}
const leftArm = makeArm(-1);
const rightArm = makeArm(1);
const legGeometry = new THREE.BoxGeometry(1, 1, 1);
const upperLegs = [];
const lowerLegs = [];
const shoes = [];
for (const side of [-1, 1]) {
const upper = new THREE.Mesh(legGeometry, pantsMaterial);
const lower = new THREE.Mesh(legGeometry, pantsMaterial);
upper.castShadow = true;
lower.castShadow = true;
character.add(upper, lower);
const shoeGroup = new THREE.Group();
const shoe = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.16, 0.42),
shoeMaterial
);
shoe.position.set(0, -0.08, 0.035);
shoe.castShadow = true;
const sole = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.04, 0.45),
soleMaterial
);
sole.position.set(0, -0.16, 0.035);
shoeGroup.add(shoe, sole);
character.add(shoeGroup);
upperLegs.push(upper);
lowerLegs.push(lower);
shoes.push(shoeGroup);
}
const unitY = new THREE.Vector3(0, 1, 0);
const tempDirection = new THREE.Vector3();
const tempBend = new THREE.Vector3();
const tempKnee = new THREE.Vector3();
const tempMidpoint = new THREE.Vector3();
function setBone(mesh, start, end, width, depth) {
tempDirection.subVectors(end, start);
const length = Math.max(tempDirection.length(), 0.0001);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(unitY, tempDirection.normalize());
mesh.scale.set(width, length, depth);
}
function solveLeg(legIndex, ankleWorld, hipY, hipX) {
const hipLocal = new THREE.Vector3(hipX, hipY, 0);
const ankleLocal = character.worldToLocal(ankleWorld.clone());
const direction = new THREE.Vector3().subVectors(ankleLocal, hipLocal);
const distance = Math.max(direction.length(), 0.0001);
direction.normalize();
const upperLength = 0.65;
const lowerLength = 0.67;
const along = (
upperLength * upperLength -
lowerLength * lowerLength +
distance * distance
) / (2 * distance);
const bendHeight = Math.sqrt(Math.max(
upperLength * upperLength - along * along,
0.0001
));
tempBend.set(0, 0, 1);
tempBend.addScaledVector(direction, -tempBend.dot(direction));
if (tempBend.lengthSq() < 0.0001) {
tempBend.set(1, 0, 0);
tempBend.addScaledVector(direction, -tempBend.dot(direction));
}
tempBend.normalize();
tempKnee.copy(hipLocal)
.addScaledVector(direction, along)
.addScaledVector(tempBend, bendHeight);
setBone(upperLegs[legIndex], hipLocal, tempKnee, 0.2, 0.21);
setBone(lowerLegs[legIndex], tempKnee, ankleLocal, 0.18, 0.19);
shoes[legIndex].position.copy(ankleLocal);
}
function footPositionOnTile(tile, yaw, side) {
const right = new THREE.Vector3(Math.cos(yaw), 0, -Math.sin(yaw));
return tile.position.clone().addScaledVector(right, side * 0.18);
}
function shortestAngle(start, end) {
return Math.atan2(Math.sin(end - start), Math.cos(end - start));
}
function getRouteYaw(index) {
if (route.length < 2) return 0;
if (index < route.length - 1) {
const a = route[index].position;
const b = route[index + 1].position;
return Math.atan2(b.x - a.x, b.z - a.z);
}
const a = route[index - 1].position;
const b = route[index].position;
return Math.atan2(b.x - a.x, b.z - a.z);
}
function bell(value, start, end) {
const t = THREE.MathUtils.clamp((value - start) / (end - start), 0, 1);
return Math.sin(t * Math.PI);
}
function smooth(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
// Landing contact pulse.
const contactPulse = {
group: new THREE.Group(),
age: 999,
material: new THREE.MeshBasicMaterial({
color: 0x9affdf,
transparent: true,
opacity: 0,
depthWrite: false
})
};
const halfTile = TILE_SIZE / 2;
const pulseParts = [
[TILE_SIZE + 0.08, 0.035, 0.04, 0, 0, -halfTile - 0.035],
[TILE_SIZE + 0.08, 0.035, 0.04, 0, 0, halfTile + 0.035],
[0.04, 0.035, TILE_SIZE + 0.08, -halfTile - 0.035, 0, 0],
[0.04, 0.035, TILE_SIZE + 0.08, halfTile + 0.035, 0, 0]
];
for (const [w, h, d, x, y, z] of pulseParts) {
const part = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), contactPulse.material);
part.position.set(x, y, z);
contactPulse.group.add(part);
}
contactPulse.group.position.y = TILE_TOP + 0.025;
contactPulse.group.visible = false;
scene.add(contactPulse.group);
// Voxel dust particles.
const particles = [];
for (let i = 0; i < 48; i++) {
const material = new THREE.MeshStandardMaterial({
color: 0xb8f8e5,
emissive: 0x234c45,
emissiveIntensity: 0.4,
transparent: true,
opacity: 0
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.09, 0.09, 0.09),
material
);
mesh.visible = false;
mesh.castShadow = true;
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(),
age: 99,
life: 1
});
}
let particleCursor = 0;
function spawnBurst(position, color, count, force = 1) {
for (let i = 0; i < count; i++) {
const particle = particles[particleCursor++ % particles.length];
const angle = Math.random() * Math.PI * 2;
const speed = (0.45 + Math.random() * 1.15) * force;
particle.mesh.position.copy(position);
particle.mesh.position.y += 0.06;
particle.mesh.scale.setScalar(0.7 + Math.random() * 0.9);
particle.mesh.material.color.set(color);
particle.mesh.material.opacity = 0.95;
particle.mesh.visible = true;
particle.velocity.set(
Math.cos(angle) * speed,
(0.65 + Math.random() * 1.05) * force,
Math.sin(angle) * speed
);
particle.age = 0;
particle.life = 0.42 + Math.random() * 0.32;
}
}
function triggerContact(tile) {
contactPulse.group.position.x = tile.position.x;
contactPulse.group.position.z = tile.position.z;
contactPulse.group.scale.setScalar(0.72);
contactPulse.material.opacity = 0.9;
contactPulse.group.visible = true;
contactPulse.age = 0;
spawnBurst(
tile.position.clone().setY(TILE_TOP),
0xb8f8e5,
13,
1
);
}
function updateParticles(delta) {
for (const particle of particles) {
if (!particle.mesh.visible) continue;
particle.age += delta;
if (particle.age >= particle.life) {
particle.mesh.visible = false;
continue;
}
particle.velocity.y -= 4.8 * delta;
particle.mesh.position.addScaledVector(particle.velocity, delta);
particle.mesh.rotation.x += delta * 7;
particle.mesh.rotation.z += delta * 5;
particle.mesh.material.opacity = 1 - particle.age / particle.life;
particle.mesh.scale.multiplyScalar(1 - delta * 0.45);
}
}
function updateContactPulse(delta) {
contactPulse.age += delta;
if (contactPulse.age > 0.58) {
contactPulse.group.visible = false;
return;
}
const t = contactPulse.age / 0.58;
contactPulse.group.scale.setScalar(0.72 + t * 1.05);
contactPulse.material.opacity = (1 - t) * 0.85;
}
function updateRestPose(tile, yaw, time) {
character.position.set(tile.position.x, 0, tile.position.z);
character.rotation.y = yaw;
const anticipation = 0.025 * Math.sin(time * 2.2);
const hipY = 1.16 + anticipation;
bodyRoot.position.y = hipY;
bodyRoot.scale.set(1, 1, 1);
head.rotation.z = Math.sin(time * 1.5) * 0.018;
leftArm.arm.rotation.x = 0.05;
rightArm.arm.rotation.x = -0.05;
leftArm.forearm.rotation.x = -0.05;
rightArm.forearm.rotation.x = -0.05;
character.updateMatrixWorld(true);
for (let i = 0; i < 2; i++) {
const side = i === 0 ? -1 : 1;
const foot = footPositionOnTile(tile, yaw, side);
const ankleWorld = foot.clone().setY(TILE_TOP + 0.18);
solveLeg(i, ankleWorld, hipY, side * 0.19);
}
}
function updateHopPose(source, destination, sourceYaw, destinationYaw, t, time) {
const horizontalT = smooth((t - 0.25) / 0.55);
const flightT = THREE.MathUtils.clamp((t - 0.25) / 0.55, 0, 1);
const hop = t > 0.25 && t < 0.8
? HOP_HEIGHT * Math.sin(flightT * Math.PI)
: 0;
const rootX = THREE.MathUtils.lerp(source.position.x, destination.position.x, horizontalT);
const rootZ = THREE.MathUtils.lerp(source.position.z, destination.position.z, horizontalT);
const yaw = sourceYaw + shortestAngle(sourceYaw, destinationYaw) * horizontalT;
character.position.set(rootX, hop, rootZ);
character.rotation.y = yaw;
const anticipationCrouch = bell(t, 0.0, 0.16) * 0.2;
const landingCrouch = bell(t, 0.78, 0.91) * 0.25;
const crouch = anticipationCrouch + landingCrouch;
const hipY = 1.16 - crouch * 0.19;
const breathing = Math.sin(time * 2.4) * 0.006;
bodyRoot.position.y = hipY + breathing;
bodyRoot.scale.set(
1 + crouch * 0.055,
1 - crouch * 0.37,
1 + crouch * 0.055
);
head.rotation.z = Math.sin(time * 1.6) * 0.015 + crouch * 0.03;
const airborne = t > 0.25 && t < 0.8;
const push = bell(t, 0.13, 0.29);
const armSwing = airborne ? 0.68 : push * 0.6 - landingCrouch * 1.4;
leftArm.arm.rotation.x = armSwing + 0.08;
rightArm.arm.rotation.x = armSwing - 0.08;
leftArm.forearm.rotation.x = -0.08 - Math.max(0, armSwing) * 0.45;
rightArm.forearm.rotation.x = -0.08 - Math.max(0, armSwing) * 0.45;
character.updateMatrixWorld(true);
const sourceFeet = [-1, 1].map(side =>
footPositionOnTile(source, sourceYaw, side)
);
const destinationFeet = [-1, 1].map(side =>
footPositionOnTile(destination, destinationYaw, side)
);
for (let i = 0; i < 2; i++) {
const side = i === 0 ? -1 : 1;
// The trailing foot follows a fraction later, making the support order readable.
const footProgress = i === 0
? smooth((t - 0.23) / 0.57)
: smooth((t - 0.25) / 0.55);
const footX = THREE.MathUtils.lerp(
sourceFeet[i].x,
destinationFeet[i].x,
footProgress
);
const footZ = THREE.MathUtils.lerp(
sourceFeet[i].z,
destinationFeet[i].z,
footProgress
);
const footLift = airborne
? HOP_HEIGHT * 0.68 * Math.sin(flightT * Math.PI)
: 0;
const ankleWorld = new THREE.Vector3(
footX,
TILE_TOP + footLift + 0.18,
footZ
);
solveLeg(i, ankleWorld, hipY, side * 0.19);
}
}
function updateActiveTiles(activeIndex) {
for (const tile of tiles) {
const selectedIndex = route.indexOf(tile);
if (selectedIndex < 0) {
tile.material.color.setHex(0x1a2a3e);
tile.material.emissive.setHex(0x000000);
tile.material.emissiveIntensity = 0;
} else {
const hue = (0.47 + selectedIndex * 0.055) % 1;
const color = new THREE.Color().setHSL(hue, 0.72, 0.48);
tile.material.color.copy(color);
tile.material.emissive.copy(color).multiplyScalar(0.24);
tile.material.emissiveIntensity = tile === activeIndex ? 0.72 : 0.25;
}
if (tile === hoveredTile && tile !== activeIndex) {
tile.material.emissiveIntensity += 0.22;
}
}
}
function defaultRoute() {
const centerIndex = (GRID_SIZE - 1) / 2;
return [
[-3, 1],
[-2, 1],
[-1, 0],
[0, 0],
[1, -1],
[2, -1]
].map(([column, row]) => tileByKey.get(`${column + centerIndex},${row + centerIndex}`));
}
// Raycasting and tile editing.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function tileUnderPointer(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(tileMeshes, false);
return hits.length ? hits[0].object.userData.tile : null;
}
let pointerDown = false;
let pointerMoved = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
pointerDown = true;
pointerMoved = false;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (pointerDown) {
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
if (Math.hypot(event.clientX - pointerX, event.clientY - pointerY) > 5) {
pointerMoved = true;
}
desiredAzimuth -= dx * 0.006;
desiredElevation = THREE.MathUtils.clamp(
desiredElevation - dy * 0.005,
0.36,
1.38
);
pointerX = event.clientX;
pointerY = event.clientY;
} else {
hoveredTile = tileUnderPointer(event);
canvas.style.cursor = hoveredTile ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", event => {
if (pointerDown && !pointerMoved) {
const tile = tileUnderPointer(event);
if (tile) toggleTile(tile);
}
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
pointerMoved = false;
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius + event.deltaY * 0.009,
7,
21
);
}, { passive: false });
playButton.addEventListener("click", () => {
playing = !playing;
updateRouteUI();
});
document.querySelector("#replay").addEventListener("click", () => {
resetAnimation();
playing = true;
updateRouteUI();
});
document.querySelector("#undo").addEventListener("click", () => {
route.pop();
rebuildRouteVisuals();
resetAnimation();
});
document.querySelector("#default-route").addEventListener("click", () => {
setRoute(defaultRoute());
});
document.querySelector("#clear-route").addEventListener("click", () => {
setRoute([]);
});
document.querySelector("#reset-camera").addEventListener("click", () => {
desiredAzimuth = 0.72;
desiredElevation = 0.96;
desiredRadius = 13.4;
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
setRoute(defaultRoute());
const clock = new THREE.Clock();
let elapsedTime = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsedTime += delta;
if (playing && route.length >= 2) {
const totalDuration = (route.length - 1) * HOP_DURATION;
elapsed = Math.min(elapsed + delta, totalDuration);
}
let segmentIndex = 0;
let localTime = 0;
let activeIndex = -1;
if (route.length === 1) {
const tile = route[0];
character.visible = true;
updateRestPose(tile, getRouteYaw(0), elapsedTime);
activeIndex = 0;
statusElement.textContent = playing ? "Single support" : "Paused";
progressFill.style.width = "0%";
} else if (route.length >= 2) {
character.visible = true;
segmentIndex = Math.min(
route.length - 2,
Math.floor(elapsed / HOP_DURATION)
);
localTime = THREE.MathUtils.clamp(
elapsed / HOP_DURATION - segmentIndex,
0,
1
);
const source = route[segmentIndex];
const destination = route[segmentIndex + 1];
const sourceYaw = getRouteYaw(segmentIndex);
const destinationYaw = getRouteYaw(segmentIndex + 1);
updateHopPose(
source,
destination,
sourceYaw,
destinationYaw,
localTime,
elapsedTime
);
if (localTime < 0.78) {
activeIndex = segmentIndex;
} else {
activeIndex = segmentIndex + 1;
}
if (localTime >= 0.235 && !takeoffFired.has(segmentIndex)) {
takeoffFired.add(segmentIndex);
spawnBurst(
source.position.clone().setY(TILE_TOP),
0x8bf4d2,
7,
0.75
);
}
if (localTime >= 0.79 && !impactFired.has(segmentIndex)) {
impactFired.add(segmentIndex);
triggerContact(destination);
}
const finished = elapsed >= (route.length - 1) * HOP_DURATION - 0.001;
const completeText = finished ? "Sequence complete" : `Hop ${segmentIndex + 1} / ${route.length - 1}`;
statusElement.textContent = playing ? completeText : "Paused";
progressFill.style.width = `${((segmentIndex + localTime) / (route.length - 1)) * 100}%`;
} else {
character.visible = false;
activeIndex = -1;
statusElement.textContent = "Select a support tile";
progressFill.style.width = "0%";
}
updateActiveTiles(activeIndex);
updateParticles(delta);
updateContactPulse(delta);
const cameraBlend = 1 - Math.exp(-delta * 9);
cameraAzimuth = THREE.MathUtils.lerp(cameraAzimuth, desiredAzimuth, cameraBlend);
cameraElevation = THREE.MathUtils.lerp(cameraElevation, desiredElevation, cameraBlend);
cameraRadius = THREE.MathUtils.lerp(cameraRadius, desiredRadius, cameraBlend);
const desiredCameraPosition = new THREE.Vector3(
Math.sin(cameraAzimuth) * Math.sin(cameraElevation) * cameraRadius,
Math.cos(cameraElevation) * cameraRadius,
Math.cos(cameraAzimuth) * Math.sin(cameraElevation) * cameraRadius
).add(cameraTarget);
camera.position.lerp(desiredCameraPosition, cameraBlend);
camera.lookAt(cameraTarget);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```