Prompt specification
Build or customize the Three.js 3D scene "Chain reaction with a gap" 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: Chain reaction with a gap
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/chain-reaction-with-a-gap-x0330
Scene Description Prompt:
Chain reaction with a gap. Animate a short domino chain until a deliberate gap stops propagation; users move one tile to bridge the gap and restart from the same initial state. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (chain-reaction-with-a-gap-x0330):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Chain Reaction — Three.js</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(15, 26, 34, 0.88);
--border: rgba(255, 255, 255, 0.12);
--muted: #92a8b2;
--text: #f1f7f8;
--amber: #ffb45b;
--mint: #66dbc2;
--red: #ff776f;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #101a22;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
body {
user-select: none;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(365px, calc(100vw - 36px));
padding: 22px;
border: 1px solid var(--border);
border-radius: 20px;
background: var(--panel);
box-shadow: 0 22px 70px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(18px);
}
.eyebrow {
margin-bottom: 8px;
color: var(--mint);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(28px, 5vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.intro {
margin: 12px 0 18px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.status-card {
display: flex;
align-items: flex-start;
gap: 10px;
min-height: 52px;
padding: 13px 14px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 13px;
background: rgba(255, 255, 255, 0.045);
}
#statusDot {
flex: 0 0 auto;
width: 9px;
height: 9px;
margin-top: 5px;
border-radius: 50%;
background: var(--amber);
box-shadow: 0 0 0 5px rgba(255, 180, 91, 0.12);
transition: background 180ms ease, box-shadow 180ms ease;
}
#statusDot.success {
background: var(--mint);
box-shadow: 0 0 0 5px rgba(102, 219, 194, 0.12);
}
#status {
color: #dce8eb;
font-size: 13px;
line-height: 1.45;
}
.actions {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
margin-top: 14px;
}
button {
min-height: 43px;
padding: 0 16px;
border: 1px solid var(--border);
border-radius: 11px;
background: rgba(255, 255, 255, 0.065);
color: var(--text);
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
opacity 140ms ease;
}
button:hover:not(:disabled) {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.11);
}
button:active:not(:disabled) {
transform: translateY(0);
}
button:disabled {
opacity: 0.36;
cursor: not-allowed;
}
#restartBtn {
border-color: rgba(255, 180, 91, 0.4);
background: var(--amber);
color: #23170b;
}
#restartBtn:hover:not(:disabled) {
background: #ffc47d;
}
#resetBtn {
color: var(--muted);
}
.progress-wrap {
margin-top: 17px;
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.progress-track {
height: 4px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.09);
}
#progressFill {
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--mint), var(--amber));
transform: scaleX(0);
transform-origin: left center;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 16px;
color: var(--muted);
font-size: 11px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 3px;
background: var(--mint);
}
.swatch.amber {
background: var(--amber);
}
.swatch.gap {
background: var(--red);
}
.scene-label {
position: fixed;
z-index: 8;
left: 50%;
top: 50%;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
opacity: 0;
transition: opacity 150ms ease;
}
#gapLabel {
padding: 5px 9px;
border: 1px solid rgba(255, 119, 111, 0.45);
border-radius: 7px;
background: rgba(80, 24, 26, 0.76);
color: #ffaaa5;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.18em;
}
#tileLabel {
padding: 6px 10px;
border: 1px solid rgba(255, 180, 91, 0.5);
border-radius: 999px;
background: rgba(73, 45, 18, 0.82);
color: #ffd19c;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.12em;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
#controlsHint {
position: fixed;
z-index: 8;
right: 18px;
bottom: 16px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(10, 19, 25, 0.68);
color: #91a6af;
font-size: 11px;
backdrop-filter: blur(10px);
pointer-events: none;
}
@media (max-width: 620px) {
#panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 17px;
}
.intro {
margin-bottom: 13px;
}
#controlsHint {
right: 10px;
bottom: 10px;
max-width: calc(100vw - 20px);
text-align: right;
}
}
</style>
</head>
<body>
<section id="panel">
<div class="eyebrow">Kinetic Lab / Experiment 03</div>
<h1>Chain Reaction</h1>
<p class="intro">
A deliberate gap interrupts the wave. Move the spare amber tile into
the socket, reset every domino upright, and test the complete chain.
</p>
<div class="status-card">
<span id="statusDot"></span>
<span id="status" aria-live="polite">Preparing the first run…</span>
</div>
<div class="actions">
<button id="restartBtn" type="button" disabled>Restart chain</button>
<button id="resetBtn" type="button">Reset scene</button>
</div>
<div class="progress-wrap">
<div class="progress-label">
<span>Reaction progress</span>
<span id="phaseLabel">Gap test</span>
</div>
<div class="progress-track">
<div id="progressFill"></div>
</div>
</div>
<div class="legend">
<span><i class="swatch"></i> Chain A</span>
<span><i class="swatch" style="background:#77a7ff"></i> Chain B</span>
<span><i class="swatch amber"></i> Movable tile</span>
<span><i class="swatch gap"></i> Gap</span>
</div>
</section>
<div id="gapLabel" class="scene-label">GAP</div>
<div id="tileLabel" class="scene-label">DRAG ME</div>
<div id="controlsHint">
Drag empty space to orbit · Scroll to zoom · Space to restart
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const statusEl = document.querySelector("#status");
const statusDot = document.querySelector("#statusDot");
const restartBtn = document.querySelector("#restartBtn");
const resetBtn = document.querySelector("#resetBtn");
const progressFill = document.querySelector("#progressFill");
const phaseLabel = document.querySelector("#phaseLabel");
const gapLabel = document.querySelector("#gapLabel");
const tileLabel = document.querySelector("#tileLabel");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x101a22);
scene.fog = new THREE.Fog(0x101a22, 15, 32);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
target: new THREE.Vector3(-0.2, 0.65, 0),
radius: 13.6,
theta: 0.57,
phi: 1.02
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(
0xc8efff,
0x24332b,
2.15
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.25);
sun.position.set(-4, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 28;
sun.shadow.bias = -0.00025;
scene.add(sun);
const warmLight = new THREE.PointLight(0xffa95c, 15, 9, 2);
warmLight.position.set(-0.15, 2.2, -1.2);
scene.add(warmLight);
const blueLight = new THREE.PointLight(0x73b8ff, 9, 12, 2);
blueLight.position.set(5, 2.6, 3);
scene.add(blueLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({
color: 0x16252c,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.025;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
40,
40,
0x36505b,
0x223740
);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const path = new THREE.Mesh(
new THREE.BoxGeometry(12.3, 0.12, 1.48),
new THREE.MeshStandardMaterial({
color: 0x2a3b40,
roughness: 0.9,
metalness: 0.03,
flatShading: true
})
);
path.position.set(-0.1, 0.06, 0);
path.receiveShadow = true;
scene.add(path);
const railMaterial = new THREE.MeshStandardMaterial({
color: 0x3c535b,
roughness: 0.72,
metalness: 0.12,
flatShading: true
});
for (const z of [-0.79, 0.79]) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(12.3, 0.09, 0.09),
railMaterial
);
rail.position.set(-0.1, 0.145, z);
rail.castShadow = true;
rail.receiveShadow = true;
scene.add(rail);
}
const BASE_Y = 0.14;
const TARGET = new THREE.Vector3(-0.125, BASE_Y, 0);
const DOCK = new THREE.Vector3(-2.45, BASE_Y, 2.08);
const FALL_ANGLE = -Math.PI / 2;
const FALL_DURATION = 0.43;
const gapStrip = new THREE.Mesh(
new THREE.BoxGeometry(1.67, 0.035, 1.28),
new THREE.MeshStandardMaterial({
color: 0x6d292b,
emissive: 0x4c1115,
emissiveIntensity: 0.5,
roughness: 0.85,
transparent: true,
opacity: 0.9
})
);
gapStrip.position.set(TARGET.x, 0.13, 0);
gapStrip.receiveShadow = true;
scene.add(gapStrip);
const targetGroup = new THREE.Group();
targetGroup.position.copy(TARGET);
scene.add(targetGroup);
const targetBase = new THREE.Mesh(
new THREE.CylinderGeometry(0.51, 0.51, 0.035, 24),
new THREE.MeshBasicMaterial({
color: 0xffa45b,
transparent: true,
opacity: 0.15,
depthWrite: false
})
);
targetBase.position.y = 0.018;
targetGroup.add(targetBase);
const targetMaterial = new THREE.MeshBasicMaterial({
color: 0xffa45b,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.46, 0.025, 4, 32),
targetMaterial
);
targetRing.rotation.x = Math.PI / 2;
targetRing.position.y = 0.045;
targetGroup.add(targetRing);
for (let i = 0; i < 4; i++) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.2, 3),
targetMaterial
);
const a = i * Math.PI / 2;
arrow.position.set(
Math.cos(a) * 0.64,
0.07,
Math.sin(a) * 0.64
);
arrow.rotation.y = -a + Math.PI / 2;
targetGroup.add(arrow);
}
const dock = new THREE.Mesh(
new THREE.CylinderGeometry(0.57, 0.65, 0.12, 8),
new THREE.MeshStandardMaterial({
color: 0x644321,
roughness: 0.8,
metalness: 0.04,
flatShading: true
})
);
dock.position.set(DOCK.x, 0.095, DOCK.z);
dock.castShadow = true;
dock.receiveShadow = true;
scene.add(dock);
const dockRing = new THREE.Mesh(
new THREE.TorusGeometry(0.49, 0.025, 4, 24),
new THREE.MeshBasicMaterial({
color: 0xffb45b,
transparent: true,
opacity: 0.8
})
);
dockRing.rotation.x = Math.PI / 2;
dockRing.position.set(DOCK.x, 0.16, DOCK.z);
scene.add(dockRing);
const pathArrowMaterial = new THREE.MeshBasicMaterial({
color: 0x587078,
transparent: true,
opacity: 0.72
});
for (const x of [-5.5, -4.6, -3.7, -2.8, -1.9, 1.45, 2.35, 3.25, 4.15, 5.05]) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.22, 3),
pathArrowMaterial
);
arrow.rotation.z = -Math.PI / 2;
arrow.position.set(x, 0.17, 0.55);
scene.add(arrow);
}
function standardMaterial(color, extras = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.62,
metalness: 0.04,
flatShading: true,
...extras
});
}
function makeDomino(x, color, section, pipIndex, movable = false) {
const pivot = new THREE.Group();
pivot.position.set(x, BASE_Y, 0);
scene.add(pivot);
const body = new THREE.Mesh(
new THREE.BoxGeometry(0.47, 1.36, 0.31),
standardMaterial(color)
);
body.position.y = 0.68;
body.castShadow = true;
body.receiveShadow = true;
pivot.add(body);
const stripe = new THREE.Mesh(
new THREE.BoxGeometry(0.485, 0.105, 0.325),
standardMaterial(movable ? 0xffe0a7 : 0xdffcf6)
);
stripe.position.y = 1.15;
stripe.castShadow = true;
pivot.add(stripe);
const pipMaterial = new THREE.MeshBasicMaterial({
color: movable ? 0x4c2c11 : 0xf4ffff
});
const pipLayouts = [
[[0, 0.68]],
[[0, 0.45], [0, 0.91]],
[[0, 0.42], [0, 0.68], [0, 0.94]]
];
for (const [px, py] of pipLayouts[pipIndex % pipLayouts.length]) {
const pip = new THREE.Mesh(
new THREE.CircleGeometry(0.047, 6),
pipMaterial
);
pip.position.set(px, py, 0.16);
pivot.add(pip);
}
const domino = {
group: pivot,
section,
movable,
startDelay: Infinity
};
pivot.userData.domino = domino;
return domino;
}
const fixedDominoes = [];
const leftPositions = [-5.2, -4.35, -3.5, -2.65, -1.8, -1.0];
const rightPositions = [0.75, 1.6, 2.45, 3.3, 4.15, 5.0];
leftPositions.forEach((x, i) => {
fixedDominoes.push(
makeDomino(
x,
i % 2 === 0 ? 0x52c9bb : 0x69d4c4,
"left",
i
)
);
});
rightPositions.forEach((x, i) => {
fixedDominoes.push(
makeDomino(
x,
i % 2 === 0 ? 0x77a7ff : 0x6698e8,
"right",
i + 1
)
);
});
const movable = makeDomino(
DOCK.x,
0xffad4d,
"movable",
2,
true
);
movable.group.position.copy(DOCK);
const allDominoes = [...fixedDominoes, movable];
// Low-poly scenery.
const rockGeometry = new THREE.IcosahedronGeometry(1, 0);
const rockColors = [0x2a4144, 0x304b4a, 0x263b40];
let seed = 17;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
for (let i = 0; i < 18; i++) {
const rock = new THREE.Mesh(
rockGeometry,
standardMaterial(rockColors[i % rockColors.length])
);
const side = i % 2 === 0 ? -1 : 1;
const size = 0.18 + random() * 0.35;
rock.scale.set(
size * (0.8 + random()),
size * (0.6 + random() * 0.8),
size * (0.8 + random())
);
rock.position.set(
-8 + random() * 16,
size * 0.35,
side * (2.7 + random() * 2.5)
);
rock.rotation.set(random(), random() * Math.PI, random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
for (const x of [-6.35, 5.95]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.08, 0.12, 0.65, 6),
standardMaterial(0xe6924f)
);
post.position.set(x, 0.44, -0.94);
post.castShadow = true;
scene.add(post);
const cap = new THREE.Mesh(
new THREE.OctahedronGeometry(0.13, 0),
standardMaterial(0xffc16f)
);
cap.position.set(x, 0.85, -0.94);
cap.castShadow = true;
scene.add(cap);
}
const state = {
placed: false,
running: false,
bridged: false,
complete: false,
runStart: 0,
maxDelay: 0,
dragging: false,
orbiting: false,
pointerId: null,
motion: null
};
function setStatus(message, success = false) {
statusEl.textContent = message;
statusDot.classList.toggle("success", success);
}
function resetDominoTransforms() {
for (const domino of allDominoes) {
domino.group.rotation.set(0, 0, 0);
}
}
function buildSchedule(bridged) {
for (const domino of allDominoes) {
domino.startDelay = Infinity;
}
let ordered;
if (bridged) {
ordered = [...allDominoes].sort(
(a, b) => a.group.position.x - b.group.position.x
);
} else {
ordered = fixedDominoes
.filter((domino) => domino.section === "left")
.sort((a, b) => a.group.position.x - b.group.position.x);
}
let delay = 0.46;
let previousX = null;
ordered.forEach((domino, index) => {
if (index > 0) {
delay += Math.abs(
domino.group.position.x - previousX
) * 0.31;
}
domino.startDelay = delay;
previousX = domino.group.position.x;
});
state.maxDelay = delay;
}
function startRun(bridged) {
resetDominoTransforms();
state.bridged = bridged;
state.running = true;
state.complete = false;
state.runStart = performance.now();
buildSchedule(bridged);
progressFill.style.transform = "scaleX(0)";
if (bridged) {
phaseLabel.textContent = "Bridged test";
setStatus("Every tile is upright again. Restarting the bridged chain…");
} else {
phaseLabel.textContent = "Gap test";
setStatus("The wave is moving toward the deliberate gap…");
}
}
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
function animateTileTo(destination, duration, onComplete) {
state.motion = {
from: movable.group.position.clone(),
to: destination.clone(),
start: performance.now(),
duration,
onComplete
};
}
function updateMotion(now) {
if (!state.motion) return;
const motion = state.motion;
const t = Math.min(
1,
(now - motion.start) / motion.duration
);
const eased = easeOutCubic(t);
movable.group.position.lerpVectors(
motion.from,
motion.to,
eased
);
if (t >= 1) {
state.motion = null;
motion.onComplete?.();
}
}
function resetScene() {
state.placed = false;
state.dragging = false;
state.orbiting = false;
state.motion = null;
movable.group.position.copy(DOCK);
movable.group.rotation.set(0, 0, 0);
targetGroup.visible = true;
targetMaterial.color.set(0xffa45b);
restartBtn.disabled = true;
startRun(false);
}
restartBtn.addEventListener("click", () => {
if (!state.placed) return;
startRun(true);
});
resetBtn.addEventListener("click", resetScene);
// Pointer interaction: drag the amber tile, or drag empty space to orbit.
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const dragPlane = new THREE.Plane(
new THREE.Vector3(0, 1, 0),
-BASE_Y
);
const planeHit = new THREE.Vector3();
const dragOffset = new THREE.Vector3();
const lastPointer = new THREE.Vector2();
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObject(movable.group, true);
const canDragTile =
hits.length > 0 &&
!state.placed &&
!state.motion;
if (event.button === 0 && canDragTile) {
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
state.dragging = true;
state.pointerId = event.pointerId;
dragOffset
.copy(movable.group.position)
.sub(planeHit);
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
tileLabel.textContent = "DROP IN THE RING";
}
} else {
state.orbiting = true;
state.pointerId = event.pointerId;
lastPointer.set(event.clientX, event.clientY);
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
event.preventDefault();
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (state.dragging) {
raycaster.setFromCamera(pointer, camera);
if (raycaster.ray.intersectPlane(dragPlane, planeHit)) {
movable.group.position.x = THREE.MathUtils.clamp(
planeHit.x + dragOffset.x,
-5.6,
5.6
);
movable.group.position.z = THREE.MathUtils.clamp(
planeHit.z + dragOffset.z,
-1.25,
3.2
);
}
const distance = Math.hypot(
movable.group.position.x - TARGET.x,
movable.group.position.z - TARGET.z
);
targetMaterial.color.set(
distance < 0.76 ? 0x70efb7 : 0xffa45b
);
} else if (state.orbiting) {
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.46,
1.28
);
lastPointer.set(event.clientX, event.clientY);
updateCamera();
}
});
function finishPointer(event) {
if (state.dragging) {
state.dragging = false;
const distance = Math.hypot(
movable.group.position.x - TARGET.x,
movable.group.position.z - TARGET.z
);
const shouldPlace = distance < 0.76;
animateTileTo(
shouldPlace ? TARGET : DOCK,
300,
() => {
if (shouldPlace) {
state.placed = true;
targetGroup.visible = false;
restartBtn.disabled = false;
phaseLabel.textContent = "Ready to restart";
setStatus(
"Gap bridged. Restart to reset every tile upright and replay the full chain."
);
} else {
state.placed = false;
targetMaterial.color.set(0xffa45b);
setStatus(
"Not quite. Drag the amber tile into the outlined socket over the gap."
);
}
}
);
}
if (state.orbiting) {
state.orbiting = false;
renderer.domElement.style.cursor = "grab";
}
if (
event.pointerId === state.pointerId &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
state.pointerId = null;
}
renderer.domElement.addEventListener(
"pointerup",
finishPointer
);
renderer.domElement.addEventListener(
"pointercancel",
finishPointer
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
8.5,
19
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && state.placed && !event.repeat) {
event.preventDefault();
startRun(true);
}
if (event.key.toLowerCase() === "r") {
resetScene();
}
});
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.15 &&
Math.abs(projected.y) < 1.15;
element.style.opacity = visible ? "1" : "0";
if (!visible) return;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const labelWorld = new THREE.Vector3();
function animate(now) {
requestAnimationFrame(animate);
updateMotion(now);
updateCamera();
if (state.running) {
const elapsed = (now - state.runStart) / 1000;
let allFinished = true;
for (const domino of allDominoes) {
if (!Number.isFinite(domino.startDelay)) {
domino.group.rotation.z = 0;
continue;
}
const localTime =
(elapsed - domino.startDelay) / FALL_DURATION;
if (localTime <= 0) {
domino.group.rotation.z = 0;
allFinished = false;
} else {
const amount = Math.min(1, localTime);
domino.group.rotation.z =
FALL_ANGLE * easeOutCubic(amount);
if (amount < 1) allFinished = false;
}
}
const total = state.maxDelay + FALL_DURATION;
const progress = THREE.MathUtils.clamp(
elapsed / total,
0,
1
);
progressFill.style.transform = `scaleX(${progress})`;
if (allFinished && elapsed >= total) {
state.running = false;
state.complete = true;
if (state.bridged) {
phaseLabel.textContent = "Chain complete";
setStatus(
"Success! The bridged chain carried the reaction through every tile.",
true
);
} else {
phaseLabel.textContent = "Gap stopped it";
setStatus(
"Stopped by the gap. Drag the amber tile into the outlined socket."
);
}
}
}
const pulse = 1 + Math.sin(now * 0.004) * 0.065;
targetRing.scale.setScalar(pulse);
labelWorld.set(TARGET.x, 1.73, TARGET.z);
positionLabel(gapLabel, labelWorld);
if (!state.placed) {
labelWorld.copy(movable.group.position);
labelWorld.y += 1.7;
positionLabel(tileLabel, labelWorld);
tileLabel.textContent = state.dragging
? "DROP IN THE RING"
: "DRAG ME";
} else {
tileLabel.style.opacity = "0";
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect =
window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
});
setTimeout(() => startRun(false), 500);
requestAnimationFrame(animate);
</script>
</body>
</html>
```