Prompt specification
Build or customize the Three.js 3D scene "Before and After Gate States" 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: Before and After Gate States
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/before-and-after-gate-states-x3425
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring temporal order and successive gate states. Place a closed gate, passing cart, and open gate on three time-marked bases. Preserve object identity so ordering depends on states rather than unrelated decorations. Low-poly.
Working Three.js Starter (before-and-after-gate-states-x3425):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Through the Gate — Temporal Sequence</title>
<style>
:root {
--ink: #243139;
--muted: #68757b;
--paper: #ece9e2;
--panel: rgba(250, 249, 245, 0.88);
--closed: #d45f45;
--passing: #d6a23c;
--open: #4f9a7c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hero {
position: fixed;
top: 30px;
left: 32px;
z-index: 5;
max-width: 410px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
background: var(--ink);
content: "";
}
h1 {
margin: 0;
font-size: clamp(34px, 4.2vw, 62px);
font-weight: 760;
letter-spacing: -0.055em;
line-height: 0.94;
}
.hero p {
max-width: 350px;
margin: 17px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.meta {
position: fixed;
top: 31px;
right: 32px;
z-index: 5;
text-align: right;
pointer-events: none;
}
.meta-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.meta-copy {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
}
.hint {
position: fixed;
right: 32px;
bottom: 139px;
z-index: 5;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.05em;
pointer-events: none;
}
.timeline-shell {
position: fixed;
left: 50%;
bottom: 25px;
z-index: 10;
width: min(780px, calc(100% - 32px));
padding: 12px;
border: 1px solid rgba(36, 49, 57, 0.13);
border-radius: 19px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(41, 49, 50, 0.14),
inset 0 1px 0 rgba(255, 255, 255, 0.75);
backdrop-filter: blur(16px);
transform: translateX(-50%);
}
.timeline-row {
display: flex;
align-items: stretch;
gap: 10px;
}
#playButton {
display: grid;
flex: 0 0 48px;
width: 48px;
min-height: 64px;
padding: 0;
place-items: center;
border: 0;
border-radius: 13px;
background: var(--ink);
color: white;
cursor: pointer;
transition: transform 160ms ease, background 160ms ease;
}
#playButton:hover {
background: #35464f;
transform: translateY(-1px);
}
#playButton:active {
transform: translateY(0);
}
#playGlyph {
font-size: 16px;
line-height: 1;
}
.phases {
display: grid;
flex: 1;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.phase {
position: relative;
display: grid;
min-width: 0;
padding: 10px 12px 9px 15px;
overflow: hidden;
border: 1px solid transparent;
border-radius: 12px;
background: transparent;
color: var(--muted);
text-align: left;
cursor: pointer;
transition:
color 160ms ease,
background 160ms ease,
border-color 160ms ease;
}
.phase::before {
position: absolute;
top: 10px;
bottom: 10px;
left: 5px;
width: 3px;
border-radius: 3px;
background: var(--state);
content: "";
opacity: 0.35;
transition: opacity 160ms ease;
}
.phase:hover {
background: rgba(255, 255, 255, 0.54);
color: var(--ink);
}
.phase.active {
border-color: rgba(36, 49, 57, 0.11);
background: rgba(255, 255, 255, 0.78);
color: var(--ink);
}
.phase.active::before {
opacity: 1;
}
.phase-time {
margin-bottom: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
}
.phase-name {
overflow: hidden;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.04em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.progress-track {
position: absolute;
right: 15px;
bottom: 5px;
left: 69px;
height: 2px;
overflow: hidden;
border-radius: 2px;
background: rgba(36, 49, 57, 0.09);
}
#progressFill {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--ink);
transition: width 80ms linear;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 700px) {
.hero {
top: 18px;
left: 18px;
max-width: 280px;
}
.hero p {
max-width: 250px;
margin-top: 11px;
font-size: 12px;
}
.meta {
display: none;
}
.hint {
right: 18px;
bottom: 124px;
font-size: 10px;
}
.timeline-shell {
bottom: 12px;
padding: 9px;
border-radius: 16px;
}
.timeline-row {
gap: 7px;
}
#playButton {
flex-basis: 42px;
width: 42px;
min-height: 58px;
}
.phases {
gap: 4px;
}
.phase {
padding: 9px 7px 8px 11px;
}
.phase-time {
font-size: 10px;
}
.phase-name {
font-size: 10px;
}
}
@media (prefers-reduced-motion: reduce) {
*,
*::before,
*::after {
scroll-behavior: auto !important;
transition-duration: 0.01ms !important;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Three-dimensional temporal sequence with a closed gate, passing cart, and open gate"></main>
<header class="hero">
<div class="eyebrow">Temporal study · 03 moments</div>
<h1>Through<br>the gate</h1>
<p>
Three fixed moments arranged in time: a closed barrier, a passing cart,
and the same passage opened.
</p>
</header>
<aside class="meta">
<div class="meta-title">Low-poly sequence</div>
<div class="meta-copy">00:00 — 00:08 · Stable scene entities</div>
</aside>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<section class="timeline-shell" aria-label="Timeline controls">
<div class="timeline-row">
<button id="playButton" type="button" aria-label="Play sequence" aria-pressed="false">
<span id="playGlyph" aria-hidden="true">▶</span>
</button>
<div class="phases" role="list">
<button
class="phase active"
type="button"
role="listitem"
data-phase="0"
style="--state: var(--closed)"
aria-pressed="true"
>
<span class="phase-time">T0 · 00:00</span>
<span class="phase-name">Closed gate</span>
</button>
<button
class="phase"
type="button"
role="listitem"
data-phase="1"
style="--state: var(--passing)"
aria-pressed="false"
>
<span class="phase-time">T1 · 00:04</span>
<span class="phase-name">Passing cart</span>
</button>
<button
class="phase"
type="button"
role="listitem"
data-phase="2"
style="--state: var(--open)"
aria-pressed="false"
>
<span class="phase-time">T2 · 00:08</span>
<span class="phase-name">Open gate</span>
</button>
</div>
</div>
<div class="progress-track" aria-hidden="true">
<span id="progressFill"></span>
</div>
<span id="sequenceStatus" class="sr-only" aria-live="polite">
T0, 00:00, closed gate
</span>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const palette = {
background: 0xece9e2,
ink: 0x29363d,
muted: 0x7e898c,
base: 0xd9d7cf,
baseTop: 0xf0eee8,
cream: 0xf5f1e5,
closed: 0xd45f45,
passing: 0xd6a23c,
open: 0x4f9a7c,
frame: 0x435158,
rail: 0x596469
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(palette.background);
scene.fog = new THREE.Fog(palette.background, 25, 55);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive low-poly scene showing three stages of a gate passage"
);
sceneHost.appendChild(renderer.domElement);
// Lighting
const hemisphere = new THREE.HemisphereLight(
0xfffbf2,
0x7d898b,
2.25
);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff7e8, 3.2);
sun.position.set(-7, 13, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -13;
sun.shadow.camera.right = 13;
sun.shadow.camera.top = 11;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 38;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xd8e7ec, 1.1);
fill.position.set(9, 7, -8);
scene.add(fill);
const floor = new THREE.Mesh(
new THREE.CircleGeometry(27, 16),
new THREE.MeshStandardMaterial({
color: palette.background,
roughness: 1,
flatShading: true
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.31;
floor.receiveShadow = true;
scene.add(floor);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.04,
flatShading: true,
...options
});
}
const sharedMaterials = {
frame: standardMaterial(palette.frame, { roughness: 0.64 }),
cream: standardMaterial(palette.cream),
rail: standardMaterial(palette.rail, { roughness: 0.58, metalness: 0.18 }),
sleeper: standardMaterial(0x8c918e),
cartBody: standardMaterial(palette.passing),
cartDark: standardMaterial(0x334249),
cargo: standardMaterial(0xb98035),
cargoLight: standardMaterial(0xe0bd72),
wheel: standardMaterial(0x29343a),
hub: standardMaterial(0xb9b6aa)
};
const baseSurface = 0.34;
const basePositions = [-6, 0, 6];
const stateColors = [palette.closed, palette.passing, palette.open];
const stateNames = ["closed gate", "passing cart", "open gate"];
const stateTimes = ["00:00", "00:04", "00:08"];
const baseRings = [];
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function makePlaque(index) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
roundedRect(ctx, 0, 0, 1024, 256, 26);
ctx.fillStyle = "#29363d";
ctx.fill();
ctx.fillStyle = `#${stateColors[index].toString(16).padStart(6, "0")}`;
ctx.fillRect(0, 0, 24, 256);
ctx.fillStyle = "#f6f1e6";
ctx.font = "800 77px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.fillText(stateTimes[index], 58, 112);
ctx.fillStyle = "#bdc6c6";
ctx.font = "700 35px system-ui, sans-serif";
ctx.letterSpacing = "2px";
ctx.fillText(`T${index} / ${stateNames[index].toUpperCase()}`, 60, 180);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(4.25, 1.0625),
new THREE.MeshBasicMaterial({ map: texture })
);
plaque.position.set(
basePositions[index],
0.83,
2.18
);
plaque.rotation.x = -0.2;
const support = new THREE.Mesh(
new THREE.BoxGeometry(1.15, 0.36, 0.12),
sharedMaterials.frame
);
support.position.set(basePositions[index], 0.48, 2.17);
support.castShadow = true;
scene.add(plaque, support);
}
function makeBase(index) {
const group = new THREE.Group();
group.position.x = basePositions[index];
const body = new THREE.Mesh(
new THREE.CylinderGeometry(3.0, 3.18, 0.48, 6),
standardMaterial(palette.base)
);
body.rotation.y = Math.PI / 6;
body.castShadow = true;
body.receiveShadow = true;
const top = new THREE.Mesh(
new THREE.CylinderGeometry(2.96, 2.96, 0.1, 6),
standardMaterial(palette.baseTop)
);
top.position.y = 0.28;
top.rotation.y = Math.PI / 6;
top.receiveShadow = true;
const ring = new THREE.Mesh(
new THREE.RingGeometry(2.74, 2.91, 6),
new THREE.MeshBasicMaterial({
color: stateColors[index],
transparent: true,
opacity: index === 0 ? 1 : 0.28,
side: THREE.DoubleSide,
depthWrite: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.rotation.z = Math.PI / 6;
ring.position.y = 0.345;
group.add(body, top, ring);
scene.add(group);
baseRings.push(ring);
}
for (let i = 0; i < 3; i++) {
makeBase(i);
makePlaque(i);
}
function makeGate(barrierColor, open) {
const gate = new THREE.Group();
gate.position.y = baseSurface;
for (const x of [-1.45, 1.45]) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.115, 0.13, 2.65, 8),
sharedMaterials.frame
);
post.position.set(x, 1.325, 0);
post.castShadow = true;
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.5, 0.12, 0.5),
sharedMaterials.frame
);
foot.position.set(x, 0.06, 0);
foot.castShadow = true;
gate.add(post, foot);
}
const hinge = new THREE.Group();
hinge.position.set(-1.45, 2.45, 0);
const barrierMaterial = standardMaterial(barrierColor, {
roughness: 0.7
});
const barrier = new THREE.Mesh(
new THREE.BoxGeometry(2.9, 0.34, 0.18),
barrierMaterial
);
barrier.position.x = 1.45;
barrier.castShadow = true;
hinge.add(barrier);
// Physical diagonal marks make the state readable without relying on color.
for (const x of [0.55, 1.45, 2.35]) {
const stripe = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.36, 0.025),
sharedMaterials.cream
);
stripe.position.set(x, 0, 0.105);
stripe.rotation.z = -0.38;
hinge.add(stripe);
}
const hingeCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.18, 0.25, 8),
sharedMaterials.hub
);
hingeCap.rotation.x = Math.PI / 2;
hinge.add(hingeCap);
if (open) {
hinge.rotation.z = THREE.MathUtils.degToRad(72);
}
gate.add(hinge);
gate.userData.hinge = hinge;
gate.userData.state = open ? "open" : "closed";
return gate;
}
function makeCart() {
const cart = new THREE.Group();
cart.position.y = baseSurface;
const chassis = new THREE.Mesh(
new THREE.BoxGeometry(2.5, 0.18, 1.58),
sharedMaterials.cartDark
);
chassis.position.y = 0.58;
chassis.castShadow = true;
const body = new THREE.Mesh(
new THREE.BoxGeometry(2.28, 0.56, 1.4),
sharedMaterials.cartBody
);
body.position.y = 0.86;
body.castShadow = true;
const front = new THREE.Mesh(
new THREE.BoxGeometry(2.3, 0.42, 0.12),
sharedMaterials.cartDark
);
front.position.set(0, 0.88, 0.74);
front.castShadow = true;
const cargoA = new THREE.Mesh(
new THREE.BoxGeometry(0.78, 0.64, 0.82),
sharedMaterials.cargo
);
cargoA.position.set(-0.58, 1.48, -0.04);
cargoA.rotation.y = 0.08;
cargoA.castShadow = true;
const cargoB = new THREE.Mesh(
new THREE.BoxGeometry(0.72, 0.54, 0.76),
sharedMaterials.cargoLight
);
cargoB.position.set(0.46, 1.43, 0.02);
cargoB.rotation.y = -0.1;
cargoB.castShadow = true;
const cargoC = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.42, 0.58),
sharedMaterials.cargo
);
cargoC.position.set(0.37, 1.91, 0);
cargoC.rotation.y = 0.12;
cargoC.castShadow = true;
const wheels = [];
for (const x of [-1.16, 1.16]) {
for (const z of [-0.56, 0.56]) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.34, z);
const tire = new THREE.Mesh(
new THREE.CylinderGeometry(0.33, 0.33, 0.2, 10),
sharedMaterials.wheel
);
tire.rotation.z = Math.PI / 2;
tire.castShadow = true;
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.225, 8),
sharedMaterials.hub
);
hub.rotation.z = Math.PI / 2;
const spoke = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.48, 0.055),
sharedMaterials.hub
);
spoke.position.x = Math.sign(x) * 0.115;
pivot.add(tire, hub, spoke);
cart.add(pivot);
wheels.push(pivot);
}
}
cart.add(chassis, body, front, cargoA, cargoB, cargoC);
cart.userData.wheels = wheels;
return cart;
}
function makeTrack() {
const track = new THREE.Group();
for (const x of [-1.16, 1.16]) {
const rail = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.07, 4.1),
sharedMaterials.rail
);
rail.position.set(x, 0.385, 0);
rail.castShadow = true;
rail.receiveShadow = true;
track.add(rail);
}
for (let i = -4; i <= 4; i++) {
const sleeper = new THREE.Mesh(
new THREE.BoxGeometry(2.78, 0.045, 0.12),
sharedMaterials.sleeper
);
sleeper.position.set(0, 0.35, i * 0.48);
sleeper.receiveShadow = true;
track.add(sleeper);
}
scene.add(track);
}
function makeArrow(x) {
const shape = new THREE.Shape();
shape.moveTo(0.42, 0);
shape.lineTo(0, 0.28);
shape.lineTo(0, 0.11);
shape.lineTo(-0.42, 0.11);
shape.lineTo(-0.42, -0.11);
shape.lineTo(0, -0.11);
shape.lineTo(0, -0.28);
shape.closePath();
const arrow = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
new THREE.MeshStandardMaterial({
color: 0x9ca39f,
roughness: 1,
side: THREE.DoubleSide
})
);
arrow.rotation.x = -Math.PI / 2;
arrow.position.set(x, -0.285, 0.72);
scene.add(arrow);
}
makeTrack();
makeArrow(-3);
makeArrow(3);
// Stable identities are created once and retained for the entire scene.
// The timeline changes states and transforms; it never replaces these objects.
const entities = Object.freeze({
closedGate: makeGate(palette.closed, false),
cart: makeCart(),
openGate: makeGate(palette.open, true)
});
entities.closedGate.position.x = basePositions[0];
entities.cart.position.x = basePositions[1];
entities.openGate.position.x = basePositions[2];
scene.add(
entities.closedGate,
entities.cart,
entities.openGate
);
// Direct camera controls: no add-ons or external control modules.
class DirectOrbitControls {
constructor(camera, element) {
this.camera = camera;
this.element = element;
this.target = new THREE.Vector3(0, 1.45, 0);
this.yaw = 0.12;
this.pitch = 0.35;
this.distance = 22;
this.desiredYaw = this.yaw;
this.desiredPitch = this.pitch;
this.desiredDistance = this.distance;
this.baseDistance = 22;
this.dragging = false;
this.lastX = 0;
this.lastY = 0;
element.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
this.dragging = true;
this.lastX = event.clientX;
this.lastY = event.clientY;
element.setPointerCapture(event.pointerId);
});
element.addEventListener("pointermove", (event) => {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.lastX = event.clientX;
this.lastY = event.clientY;
this.desiredYaw -= dx * 0.006;
this.desiredPitch = THREE.MathUtils.clamp(
this.desiredPitch + dy * 0.005,
0.16,
0.9
);
});
const stopDragging = (event) => {
this.dragging = false;
if (element.hasPointerCapture(event.pointerId)) {
element.releasePointerCapture(event.pointerId);
}
};
element.addEventListener("pointerup", stopDragging);
element.addEventListener("pointercancel", stopDragging);
element.addEventListener(
"wheel",
(event) => {
event.preventDefault();
this.desiredDistance = THREE.MathUtils.clamp(
this.desiredDistance * Math.exp(event.deltaY * 0.001),
10,
82
);
},
{ passive: false }
);
element.addEventListener("dblclick", () => this.reset());
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
this.desiredYaw -= 0.08;
} else if (event.key === "ArrowRight") {
this.desiredYaw += 0.08;
} else if (event.key === "ArrowUp") {
this.desiredPitch = THREE.MathUtils.clamp(
this.desiredPitch + 0.06,
0.16,
0.9
);
} else if (event.key === "ArrowDown") {
this.desiredPitch = THREE.MathUtils.clamp(
this.desiredPitch - 0.06,
0.16,
0.9
);
}
});
}
fitToViewport() {
const verticalRadians = THREE.MathUtils.degToRad(
this.camera.fov * 0.5
);
this.baseDistance = Math.max(
21,
20 / (2 * Math.tan(verticalRadians) * this.camera.aspect)
);
this.desiredDistance = this.baseDistance;
this.distance = this.baseDistance;
}
reset() {
this.desiredYaw = 0.12;
this.desiredPitch = 0.35;
this.desiredDistance = this.baseDistance;
}
update(delta) {
const smoothing = 1 - Math.exp(-delta * 10);
this.yaw += (this.desiredYaw - this.yaw) * smoothing;
this.pitch += (this.desiredPitch - this.pitch) * smoothing;
this.distance += (this.desiredDistance - this.distance) * smoothing;
const horizontal = Math.cos(this.pitch) * this.distance;
this.camera.position.set(
this.target.x + Math.sin(this.yaw) * horizontal,
this.target.y + Math.sin(this.pitch) * this.distance,
this.target.z + Math.cos(this.yaw) * horizontal
);
this.camera.lookAt(this.target);
}
}
const controls = new DirectOrbitControls(
camera,
renderer.domElement
);
// Timeline UI
const phaseButtons = [...document.querySelectorAll(".phase")];
const playButton = document.getElementById("playButton");
const playGlyph = document.getElementById("playGlyph");
const progressFill = document.getElementById("progressFill");
const sequenceStatus = document.getElementById("sequenceStatus");
const phaseDurations = [1800, 2800, 1800];
const totalDuration = phaseDurations.reduce(
(sum, duration) => sum + duration,
0
);
let activePhase = 0;
let phaseElapsed = 0;
let sequencePlaying = false;
function updatePlayButton() {
playGlyph.textContent = sequencePlaying ? "Ⅱ" : "▶";
playButton.setAttribute("aria-pressed", String(sequencePlaying));
playButton.setAttribute(
"aria-label",
sequencePlaying ? "Pause sequence" : "Play sequence"
);
}
function setActivePhase(index) {
activePhase = index;
phaseButtons.forEach((button, buttonIndex) => {
const active = buttonIndex === index;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
baseRings.forEach((ring, ringIndex) => {
ring.material.opacity = ringIndex === index ? 1 : 0.28;
});
sequenceStatus.textContent =
`T${index}, ${stateTimes[index]}, ${stateNames[index]}`;
}
function updateProgress() {
let elapsedBefore = 0;
for (let i = 0; i < activePhase; i++) {
elapsedBefore += phaseDurations[i];
}
const progress = THREE.MathUtils.clamp(
(elapsedBefore + phaseElapsed) / totalDuration,
0,
1
);
progressFill.style.width = `${progress * 100}%`;
}
phaseButtons.forEach((button) => {
button.addEventListener("click", () => {
const index = Number(button.dataset.phase);
phaseElapsed = 0;
setActivePhase(index);
updateProgress();
});
});
playButton.addEventListener("click", () => {
if (!sequencePlaying && activePhase === 2) {
activePhase = 0;
phaseElapsed = 0;
setActivePhase(0);
}
sequencePlaying = !sequencePlaying;
updatePlayButton();
});
setActivePhase(0);
updatePlayButton();
let cartTravel = 0;
let previousCartZ = 0;
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
controls.fitToViewport();
}
window.addEventListener("resize", resize);
resize();
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (sequencePlaying) {
phaseElapsed += delta * 1000;
const duration = phaseDurations[activePhase];
if (phaseElapsed >= duration) {
if (activePhase < phaseDurations.length - 1) {
phaseElapsed -= duration;
setActivePhase(activePhase + 1);
} else {
phaseElapsed = duration;
sequencePlaying = false;
updatePlayButton();
}
}
updateProgress();
}
// The cart is the same object throughout. Only its transform and wheel
// rotations change, preserving identity through the timeline.
const cartSpeed = activePhase === 1 ? 1.35 : 0.68;
if (!reducedMotion) {
cartTravel += delta * cartSpeed;
}
const cartZ = reducedMotion ? 0.72 : Math.sin(cartTravel) * 1.25;
const cartDelta = cartZ - previousCartZ;
previousCartZ = cartZ;
entities.cart.position.z = cartZ;
for (const wheel of entities.cart.userData.wheels) {
wheel.rotation.x -= cartDelta / 0.33;
}
baseRings.forEach((ring, index) => {
const pulse =
index === activePhase && !reducedMotion
? 1 + Math.sin(now * 0.003) * 0.009
: 1;
ring.scale.setScalar(pulse);
});
controls.update(delta);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```