Prompt specification
Build or customize the Three.js 3D scene "Four Dancers Change Formation" 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: Four Dancers Change Formation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/four-dancers-change-formation-x3298
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring dance formations, spacing, and movement paths. Animate four original figures moving from a line into a square with synchronized timing. Keep their paths visible and prevent bodies from passing through each other. Voxel style.
Working Three.js Starter (four-dancers-change-formation-x3298):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Formation — Line to Square</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(8, 17, 31, 0.82);
--line: rgba(160, 190, 220, 0.2);
--text: #edf7ff;
--muted: #9db0c5;
--accent: #70e1ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
font-family: Inter, ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
color: var(--text);
}
canvas {
display: block;
touch-action: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #07111f;
color: #9eeeff;
letter-spacing: 0.16em;
font-size: 13px;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
transition: opacity 0.35s ease;
}
#hud {
position: fixed;
z-index: 10;
top: 20px;
left: 20px;
width: min(340px, calc(100vw - 40px));
padding: 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
user-select: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 21px;
line-height: 1.15;
letter-spacing: -0.04em;
}
.description {
margin: 10px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 14px;
}
.stat {
min-height: 54px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.stat-label {
display: block;
margin-bottom: 5px;
color: #7890a8;
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stat-value {
color: #f3fbff;
font-size: 12px;
font-weight: 700;
}
.stat-value.safe {
color: #77f2bd;
}
.legend {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 6px;
margin: 12px 0 15px;
}
.legend-item {
display: flex;
align-items: center;
justify-content: center;
gap: 5px;
color: #aebed0;
font-size: 9px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 2px;
box-shadow: 0 0 10px currentColor;
}
.progress-shell {
height: 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#progress {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #ff5d73, #29d3c2, #ffd166, #9b7cff);
box-shadow: 0 0 16px rgba(112, 225, 255, 0.35);
}
.progress-meta {
display: flex;
justify-content: space-between;
margin: 8px 0 14px;
color: #8da2b8;
font-size: 10px;
}
#phase {
color: #dff8ff;
font-weight: 700;
}
.controls {
display: flex;
gap: 8px;
}
button {
flex: 1;
padding: 10px 12px;
border: 1px solid rgba(145, 185, 220, 0.24);
border-radius: 9px;
background: rgba(112, 225, 255, 0.08);
color: #e8faff;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 0.18s ease,
border-color 0.18s ease,
transform 0.18s ease;
}
button:hover {
border-color: rgba(112, 225, 255, 0.55);
background: rgba(112, 225, 255, 0.15);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
#camera-tip {
position: fixed;
z-index: 10;
right: 20px;
bottom: 18px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(8, 17, 31, 0.72);
color: #91a8bf;
font-size: 10px;
backdrop-filter: blur(10px);
pointer-events: none;
}
@media (max-width: 650px) {
#hud {
top: 10px;
left: 10px;
width: min(320px, calc(100vw - 20px));
padding: 14px;
}
.description {
display: none;
}
.stats {
margin-bottom: 10px;
}
#camera-tip {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<div id="loading">BUILDING VOXEL FORMATIONS…</div>
<section id="hud" aria-label="Scene controls">
<div class="eyebrow">Synchronized movement study</div>
<h1>Line → Square</h1>
<p class="description">
Four original voxel dancers travel in lockstep. Colored routes and safety
halos remain visible throughout the formation change.
</p>
<div class="stats">
<div class="stat">
<span class="stat-label">Dancers</span>
<span class="stat-value">4 / 4 synced</span>
</div>
<div class="stat">
<span class="stat-label">Transition</span>
<span class="stat-value">8.0 seconds</span>
</div>
<div class="stat">
<span class="stat-label">Min route gap</span>
<span class="stat-value safe" id="route-gap">Calculating</span>
</div>
<div class="stat">
<span class="stat-label">Collision guard</span>
<span class="stat-value safe">Armed</span>
</div>
</div>
<div class="legend" aria-label="Dancer color legend">
<div class="legend-item">
<span class="dot" style="color:#ff5d73;background:#ff5d73"></span>01
</div>
<div class="legend-item">
<span class="dot" style="color:#29d3c2;background:#29d3c2"></span>02
</div>
<div class="legend-item">
<span class="dot" style="color:#ffd166;background:#ffd166"></span>03
</div>
<div class="legend-item">
<span class="dot" style="color:#9b7cff;background:#9b7cff"></span>04
</div>
</div>
<div class="progress-shell">
<div id="progress"></div>
</div>
<div class="progress-meta">
<span id="phase">LINE // READY</span>
<span id="time">0.0 s</span>
</div>
<div class="controls">
<button id="replay" type="button">Replay</button>
<button id="pause" type="button">Pause</button>
</div>
</section>
<div id="camera-tip">Drag to orbit · Scroll to zoom · Space: pause · R: replay</div>
<script type="module">
let THREE;
const loadingElement = document.getElementById("loading");
try {
THREE = await import(
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js"
);
} catch (error) {
loadingElement.textContent = "Unable to load the Three.js core module.";
throw error;
}
// -------------------------------------------------------------------------
// Renderer, scene, camera, and custom orbit controls
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 18, 37);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
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.12;
document.body.appendChild(renderer.domElement);
const cameraTarget = new THREE.Vector3(0, 0.65, 0.8);
const orbit = {
theta: 0.67,
phi: 0.68,
radius: 16.5
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
cameraTarget.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
cameraTarget.y + orbit.radius * Math.cos(orbit.phi),
cameraTarget.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
orbit.theta -= deltaX * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi + deltaY * 0.005, 0.28, 1.38);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.012,
9,
27
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Lighting and voxel environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xbfe8ff, 0x182039, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x29d3c2, 24, 11, 2);
cyanLight.position.set(-6, 4, 4);
scene.add(cyanLight);
const violetLight = new THREE.PointLight(0x9b7cff, 26, 11, 2);
violetLight.position.set(6, 4, -4);
scene.add(violetLight);
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x111d2d,
roughness: 0.92,
metalness: 0.03
});
const floor = new THREE.Mesh(
new THREE.BoxGeometry(28, 0.3, 24),
floorMaterial
);
floor.position.y = -0.15;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x4b7898, 0x26394d);
grid.position.y = 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.32;
grid.material.depthWrite = false;
scene.add(grid);
const platformMaterial = new THREE.MeshStandardMaterial({
color: 0x1b2c40,
roughness: 0.78,
metalness: 0.08
});
const platformEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x31536d,
roughness: 0.62,
metalness: 0.12,
emissive: 0x102838,
emissiveIntensity: 0.4
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Decorative voxel perimeter blocks.
for (const x of [-6.8, 6.8]) {
for (const z of [-5.4, -2.7, 0, 2.7, 5.4]) {
addBox(
scene,
[0.42, 0.42 + (z === 0 ? 0.42 : 0), 0.42],
[x, 0.21 + (z === 0 ? 0.21 : 0), z],
platformEdgeMaterial
);
}
}
// -------------------------------------------------------------------------
// Formation layout
// -------------------------------------------------------------------------
const MOVE_START = 1.2;
const MOVE_DURATION = 8.0;
const BODY_RADIUS = 0.62;
const SAFE_DISTANCE = BODY_RADIUS * 2;
const dancerDefinitions = [
{
start: new THREE.Vector3(-3.6, 0, 5.2),
target: new THREE.Vector3(-2.4, 0, 2.4),
color: 0xff5d73,
accent: 0xffd166,
hair: 0x182033,
skin: 0xf0b18b,
route: 0xff5d73
},
{
start: new THREE.Vector3(-1.2, 0, 5.2),
target: new THREE.Vector3(-2.4, 0, -2.4),
color: 0x29d3c2,
accent: 0xff7b54,
hair: 0x5a291d,
skin: 0x8d573b,
route: 0x29d3c2
},
{
start: new THREE.Vector3(1.2, 0, 5.2),
target: new THREE.Vector3(2.4, 0, -2.4),
color: 0xffd166,
accent: 0x4d7dff,
hair: 0x27334c,
skin: 0xd99870,
route: 0xffd166
},
{
start: new THREE.Vector3(3.6, 0, 5.2),
target: new THREE.Vector3(2.4, 0, 2.4),
color: 0x9b7cff,
accent: 0x62f5cb,
hair: 0x123b3c,
skin: 0xb97856,
route: 0x9b7cff
}
];
// Floor markings for the starting line.
addBox(
scene,
[8.4, 0.035, 0.08],
[0, 0.025, 5.2],
new THREE.MeshStandardMaterial({
color: 0x7690a8,
emissive: 0x263f55,
emissiveIntensity: 0.7
}),
false
);
// Target square outline.
const squareMaterial = new THREE.MeshStandardMaterial({
color: 0x486c86,
emissive: 0x17364a,
emissiveIntensity: 0.8,
transparent: true,
opacity: 0.65
});
addBox(scene, [5.65, 0.035, 0.065], [0, 0.028, 2.4], squareMaterial, false);
addBox(scene, [5.65, 0.035, 0.065], [0, 0.028, -2.4], squareMaterial, false);
addBox(scene, [0.065, 0.035, 4.8], [-2.4, 0.028, 0], squareMaterial, false);
addBox(scene, [0.065, 0.035, 4.8], [2.4, 0.028, 0], squareMaterial, false);
function makeTextSprite(text, color = "#dff8ff") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(7, 17, 31, 0.76)";
context.fillRect(12, 20, 488, 88);
context.strokeStyle = color;
context.lineWidth = 3;
context.strokeRect(12, 20, 488, 88);
context.fillStyle = color;
context.font = "700 48px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.5, 0.875, 1);
sprite.renderOrder = 20;
return sprite;
}
const lineLabel = makeTextSprite("START LINE", "#b9d5e8");
lineLabel.position.set(0, 0.52, 6.35);
scene.add(lineLabel);
const squareLabel = makeTextSprite("SQUARE", "#72e4ff");
squareLabel.position.set(0, 0.52, -3.35);
scene.add(squareLabel);
// -------------------------------------------------------------------------
// Original voxel dancers
// -------------------------------------------------------------------------
function createDancer(definition, index) {
const root = new THREE.Group();
root.position.copy(definition.start);
const body = new THREE.Group();
root.add(body);
const outfitMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
roughness: 0.68,
metalness: 0.04
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: definition.accent,
roughness: 0.62,
metalness: 0.05
});
const skinMaterial = new THREE.MeshStandardMaterial({
color: definition.skin,
roughness: 0.86
});
const hairMaterial = new THREE.MeshStandardMaterial({
color: definition.hair,
roughness: 0.92
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x101622,
roughness: 0.78
});
// Torso, pelvis, chest panel, and neck.
addBox(body, [0.62, 0.68, 0.38], [0, 1.02, 0], outfitMaterial);
addBox(body, [0.54, 0.16, 0.34], [0, 0.66, 0], outfitMaterial);
addBox(body, [0.34, 0.38, 0.045], [0, 1.04, 0.208], accentMaterial);
addBox(body, [0.18, 0.18, 0.18], [0, 1.42, 0], skinMaterial);
// Head and face.
addBox(body, [0.44, 0.44, 0.44], [0, 1.62, 0], skinMaterial);
addBox(body, [0.07, 0.07, 0.035], [-0.105, 1.67, 0.225], darkMaterial);
addBox(body, [0.07, 0.07, 0.035], [0.105, 1.67, 0.225], darkMaterial);
addBox(body, [0.12, 0.025, 0.025], [0, 1.51, 0.229], darkMaterial);
// Four distinct block hairstyles.
if (index === 0) {
addBox(body, [0.47, 0.12, 0.46], [0, 1.88, 0], hairMaterial);
addBox(body, [0.13, 0.34, 0.43], [-0.2, 1.72, -0.015], hairMaterial);
} else if (index === 1) {
addBox(body, [0.47, 0.1, 0.46], [0, 1.87, 0], hairMaterial);
addBox(body, [0.12, 0.28, 0.43], [0.19, 1.72, -0.015], hairMaterial);
} else if (index === 2) {
addBox(body, [0.46, 0.1, 0.46], [0, 1.87, 0], hairMaterial);
addBox(body, [0.13, 0.22, 0.13], [-0.13, 2.01, -0.03], hairMaterial);
addBox(body, [0.13, 0.28, 0.13], [0, 2.06, -0.03], hairMaterial);
addBox(body, [0.13, 0.22, 0.13], [0.13, 2.01, -0.03], hairMaterial);
} else {
addBox(body, [0.46, 0.13, 0.46], [0, 1.87, 0], hairMaterial);
addBox(body, [0.48, 0.08, 0.48], [0, 1.75, 0], accentMaterial);
addBox(body, [0.16, 0.34, 0.16], [0, 1.68, -0.28], hairMaterial);
}
function createArm(side) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.43, 1.29, 0);
body.add(pivot);
addBox(pivot, [0.2, 0.46, 0.22], [0, -0.22, 0], outfitMaterial);
addBox(pivot, [0.22, 0.2, 0.22], [0, -0.5, 0], skinMaterial);
return pivot;
}
function createLeg(side) {
const pivot = new THREE.Group();
pivot.position.set(side * 0.17, 0.62, 0);
body.add(pivot);
addBox(pivot, [0.22, 0.46, 0.24], [0, -0.23, 0], outfitMaterial);
addBox(pivot, [0.26, 0.18, 0.4], [0, -0.52, 0.07], darkMaterial);
return pivot;
}
const leftArm = createArm(-1);
const rightArm = createArm(1);
const leftLeg = createLeg(-1);
const rightLeg = createLeg(1);
// Floor halo visualizes each dancer's personal collision radius.
const halo = new THREE.Mesh(
new THREE.RingGeometry(BODY_RADIUS, BODY_RADIUS + 0.055, 32),
new THREE.MeshBasicMaterial({
color: definition.route,
transparent: true,
opacity: 0.32,
depthWrite: false,
side: THREE.DoubleSide
})
);
halo.rotation.x = -Math.PI / 2;
halo.position.y = 0.055;
root.add(halo);
scene.add(root);
return {
root,
body,
leftArm,
rightArm,
leftLeg,
rightLeg,
halo,
definition,
index
};
}
const dancers = dancerDefinitions.map(createDancer);
// -------------------------------------------------------------------------
// Visible routes, voxel arrows, and synchronized route pulses
// -------------------------------------------------------------------------
const routePulses = [];
function createRoute(definition, index) {
const routeMaterial = new THREE.LineDashedMaterial({
color: definition.route,
dashSize: 0.22,
gapSize: 0.13,
transparent: true,
opacity: 0.86,
depthWrite: false
});
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(definition.start.x, 0.05, definition.start.z),
new THREE.Vector3(definition.target.x, 0.05, definition.target.z)
]);
const line = new THREE.Line(geometry, routeMaterial);
line.computeLineDistances();
scene.add(line);
const tileMaterial = new THREE.MeshBasicMaterial({
color: definition.route,
transparent: true,
opacity: 0.36,
depthWrite: false
});
const length = definition.start.distanceTo(definition.target);
const tileCount = Math.max(8, Math.floor(length / 0.3));
for (let tileIndex = 0; tileIndex <= tileCount; tileIndex++) {
const t = tileIndex / tileCount;
const tile = addBox(
scene,
[0.075, 0.025, 0.075],
[
THREE.MathUtils.lerp(definition.start.x, definition.target.x, t),
0.045,
THREE.MathUtils.lerp(definition.start.z, definition.target.z, t)
],
tileMaterial,
false
);
tile.rotation.y = Math.PI / 4;
}
// Voxel arrow pointing toward the destination.
const arrow = new THREE.Group();
const arrowMaterial = new THREE.MeshBasicMaterial({
color: definition.route,
transparent: true,
opacity: 0.95
});
addBox(arrow, [0.09, 0.04, 0.46], [0, 0, 0], arrowMaterial, false);
addBox(arrow, [0.09, 0.04, 0.31], [-0.12, 0, 0.16], arrowMaterial, false);
addBox(arrow, [0.09, 0.04, 0.31], [0.12, 0, 0.16], arrowMaterial, false);
addBox(arrow, [0.09, 0.04, 0.26], [-0.19, 0, 0.27], arrowMaterial, false);
addBox(arrow, [0.09, 0.04, 0.26], [0.19, 0, 0.27], arrowMaterial, false);
const arrowT = 0.73;
arrow.position.set(
THREE.MathUtils.lerp(definition.start.x, definition.target.x, arrowT),
0.075,
THREE.MathUtils.lerp(definition.start.z, definition.target.z, arrowT)
);
const direction = definition.target.clone().sub(definition.start);
arrow.rotation.y = Math.atan2(direction.x, direction.z);
scene.add(arrow);
const pulseMaterial = new THREE.MeshStandardMaterial({
color: definition.route,
emissive: definition.route,
emissiveIntensity: 1.8,
roughness: 0.35
});
const pulse = addBox(
scene,
[0.15, 0.15, 0.15],
[definition.start.x, 0.18, definition.start.z],
pulseMaterial
);
routePulses.push(pulse);
}
dancerDefinitions.forEach(createRoute);
// -------------------------------------------------------------------------
// Route audit and runtime collision separation
// -------------------------------------------------------------------------
function interpolatedPositions(progress) {
return dancerDefinitions.map((definition) => ({
x: THREE.MathUtils.lerp(
definition.start.x,
definition.target.x,
progress
),
z: THREE.MathUtils.lerp(
definition.start.z,
definition.target.z,
progress
)
}));
}
function minimumPairDistance(positions) {
let minimum = Infinity;
for (let first = 0; first < positions.length; first++) {
for (let second = first + 1; second < positions.length; second++) {
const dx = positions[first].x - positions[second].x;
const dz = positions[first].z - positions[second].z;
minimum = Math.min(minimum, Math.hypot(dx, dz));
}
}
return minimum;
}
const routeAuditMinimum = (() => {
let minimum = Infinity;
for (let sample = 0; sample <= 1200; sample++) {
const progress = sample / 1200;
minimum = Math.min(
minimum,
minimumPairDistance(interpolatedPositions(progress))
);
}
return minimum;
})();
document.getElementById("route-gap").textContent =
`${routeAuditMinimum.toFixed(2)} m`;
// The precomputed routes already maintain more than the required distance.
// This continuous guard also corrects any future numerical or control drift.
function enforceSpacing(positions) {
const corrected = positions.map((position) => ({ ...position }));
for (let iteration = 0; iteration < 4; iteration++) {
for (let first = 0; first < corrected.length; first++) {
for (
let second = first + 1;
second < corrected.length;
second++
) {
let dx = corrected[second].x - corrected[first].x;
let dz = corrected[second].z - corrected[first].z;
let distance = Math.hypot(dx, dz);
if (distance < SAFE_DISTANCE) {
let normalX;
let normalZ;
if (distance < 0.0001) {
const angle = (first * 2.399 + second) % (Math.PI * 2);
normalX = Math.cos(angle);
normalZ = Math.sin(angle);
} else {
normalX = dx / distance;
normalZ = dz / distance;
}
const correction = (SAFE_DISTANCE - distance) * 0.5;
corrected[first].x -= normalX * correction;
corrected[first].z -= normalZ * correction;
corrected[second].x += normalX * correction;
corrected[second].z += normalZ * correction;
}
}
}
}
return corrected;
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function smootherStep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * t * (t * (t * 6 - 15) + 10);
}
function lerpAngle(first, second, amount) {
let difference = ((second - first + Math.PI) % (Math.PI * 2)) - Math.PI;
if (difference < -Math.PI) difference += Math.PI * 2;
return first + difference * amount;
}
let elapsed = 0;
let paused = false;
const progressElement = document.getElementById("progress");
const phaseElement = document.getElementById("phase");
const timeElement = document.getElementById("time");
const pauseButton = document.getElementById("pause");
const replayButton = document.getElementById("replay");
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
function replay() {
elapsed = 0;
paused = false;
pauseButton.textContent = "Pause";
}
pauseButton.addEventListener("click", togglePause);
replayButton.addEventListener("click", replay);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
replay();
}
});
function updateDancers(time) {
const rawProgress = THREE.MathUtils.clamp(
(time - MOVE_START) / MOVE_DURATION,
0,
1
);
const easedProgress = smootherStep(rawProgress);
const desiredPositions = interpolatedPositions(easedProgress);
const positions = enforceSpacing(desiredPositions);
const currentGap = minimumPairDistance(positions);
dancers.forEach((dancer) => {
const { definition, index } = dancer;
const position = positions[index];
dancer.root.position.x = position.x;
dancer.root.position.z = position.z;
const direction = definition.target.clone().sub(definition.start);
const pathYaw = Math.atan2(direction.x, direction.z);
// Turn toward the camera during the final part of each route.
const turnAmount = smootherStep(
THREE.MathUtils.clamp((rawProgress - 0.68) / 0.32, 0, 1)
);
dancer.root.rotation.y = lerpAngle(pathYaw, 0, turnAmount);
const mirrored = index % 2 === 0 ? 1 : -1;
const cadence = time * 6.4;
const movingEnvelope = rawProgress < 1
? 0.18 + Math.sin(rawProgress * Math.PI) * 0.82
: 0.48;
const step = Math.sin(cadence) * mirrored;
const lift = Math.abs(Math.sin(cadence));
dancer.leftArm.rotation.x = -0.12 - step * 0.74 * movingEnvelope;
dancer.rightArm.rotation.x = 0.12 + step * 0.74 * movingEnvelope;
dancer.leftArm.rotation.z = 0.07 + mirrored * 0.05;
dancer.rightArm.rotation.z = -0.07 + mirrored * 0.05;
dancer.leftLeg.rotation.x = step * 0.52 * movingEnvelope;
dancer.rightLeg.rotation.x = -step * 0.52 * movingEnvelope;
dancer.body.position.y = lift * 0.08 * movingEnvelope;
dancer.body.rotation.y = Math.sin(cadence * 0.5) * 0.055;
dancer.body.rotation.z = Math.sin(cadence) * 0.025 * mirrored;
const haloOpacity =
currentGap < SAFE_DISTANCE * 1.15 ? 0.65 : 0.28;
dancer.halo.material.opacity = haloOpacity;
});
routePulses.forEach((pulse, index) => {
const definition = dancerDefinitions[index];
pulse.position.x = THREE.MathUtils.lerp(
definition.start.x,
definition.target.x,
easedProgress
);
pulse.position.z = THREE.MathUtils.lerp(
definition.start.z,
definition.target.z,
easedProgress
);
pulse.position.y = 0.18 + Math.sin(time * 5 + index) * 0.035;
pulse.rotation.x = time * 1.8;
pulse.rotation.y = time * 2.2;
});
progressElement.style.width = `${rawProgress * 100}%`;
timeElement.textContent = `${time.toFixed(1)} s`;
if (time < MOVE_START) {
phaseElement.textContent = "LINE // READY";
} else if (rawProgress < 1) {
phaseElement.textContent = "TRANSIT // LOCKSTEP";
} else {
phaseElement.textContent = "SQUARE // GROOVE LOCKED";
}
}
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
elapsed += delta;
}
updateDancers(elapsed);
renderer.render(scene, camera);
});
loadingElement.classList.add("hidden");
setTimeout(() => loadingElement.remove(), 400);
</script>
</body>
</html>
```