Prompt specification
Build or customize the Three.js 3D scene "Directions as Words and Arrows" 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: Directions as Words and Arrows
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/directions-as-words-and-arrows-x3430
Scene Description Prompt:
A stylized cart follows equivalent written directions and arrows along the same grid path. Make one HTML file with Three.js from cdnjs.
Working Three.js Starter (directions-as-words-and-arrows-x3430):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Grid Route — Written Directions & Arrows</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 25, 43, 0.82);
--line: rgba(154, 210, 255, 0.16);
--text: #eef7ff;
--muted: #8da9c2;
--cyan: #50d8ff;
--yellow: #ffd166;
--pink: #ff6da8;
--green: #71e6b1;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
body {
user-select: none;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 24px;
left: 26px;
z-index: 5;
pointer-events: none;
max-width: 430px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 10px 0 6px;
font-size: clamp(25px, 3vw, 40px);
line-height: 1.05;
letter-spacing: -0.045em;
font-weight: 760;
}
.header p {
margin: 0;
color: var(--muted);
font-size: 14px;
}
.panel {
position: fixed;
z-index: 6;
left: 24px;
top: 150px;
width: 344px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 20px;
background:
linear-gradient(145deg, rgba(18, 42, 68, 0.86), rgba(7, 19, 33, 0.86));
box-shadow:
0 22px 60px rgba(0, 0, 0, 0.32),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.panel-heading strong {
font-size: 14px;
letter-spacing: 0.02em;
}
.step-count {
padding: 5px 9px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
font-size: 11px;
font-weight: 700;
}
#commandList {
display: grid;
gap: 7px;
}
.command {
display: grid;
grid-template-columns: 32px 42px 1fr auto;
align-items: center;
min-height: 52px;
padding: 8px 10px;
border: 1px solid transparent;
border-radius: 13px;
background: rgba(255, 255, 255, 0.025);
transition:
border-color 180ms ease,
background 180ms ease,
transform 180ms ease,
opacity 180ms ease;
}
.command.active {
border-color: rgba(80, 216, 255, 0.46);
background: rgba(80, 216, 255, 0.085);
transform: translateX(3px);
}
.command.done {
opacity: 0.52;
}
.command-number {
color: #66839d;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
}
.command-icon {
display: grid;
place-items: center;
width: 32px;
height: 32px;
border-radius: 10px;
color: var(--yellow);
background: rgba(255, 209, 102, 0.1);
font-size: 23px;
font-weight: 800;
line-height: 1;
}
.command.turn .command-icon {
color: var(--pink);
background: rgba(255, 109, 168, 0.1);
}
.command.active .command-icon {
box-shadow: 0 0 22px rgba(255, 209, 102, 0.13);
}
.command-title {
display: block;
font-size: 13px;
font-weight: 730;
}
.command-detail {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.command-state {
color: #57738c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.command.active .command-state {
color: var(--cyan);
}
.command.done .command-state {
color: var(--green);
}
.progress-wrap {
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid var(--line);
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.progress-track {
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.07);
}
#progressBar {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), var(--green));
box-shadow: 0 0 16px rgba(80, 216, 255, 0.5);
transition: width 80ms linear;
}
.current {
display: flex;
align-items: center;
gap: 12px;
min-height: 64px;
margin-top: 13px;
padding: 11px;
border-radius: 14px;
background: rgba(3, 12, 22, 0.44);
}
#currentIcon {
display: grid;
place-items: center;
flex: 0 0 38px;
width: 38px;
height: 38px;
border: 1px solid rgba(80, 216, 255, 0.24);
border-radius: 11px;
color: var(--cyan);
background: rgba(80, 216, 255, 0.08);
font-size: 24px;
font-weight: 800;
}
#currentTitle {
display: block;
font-size: 12px;
}
#currentSub {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.legend {
position: fixed;
z-index: 5;
top: 25px;
right: 25px;
width: 206px;
padding: 14px 15px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(8, 23, 40, 0.68);
backdrop-filter: blur(14px);
pointer-events: none;
}
.legend-title {
margin-bottom: 10px;
color: #d8e9f8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin-top: 8px;
color: var(--muted);
font-size: 11px;
}
.swatch {
width: 19px;
height: 7px;
border-radius: 99px;
background: var(--cyan);
}
.swatch.arrow {
width: 8px;
height: 8px;
margin-left: 5px;
border-radius: 2px;
background: var(--yellow);
transform: rotate(45deg);
}
.swatch.turn {
width: 8px;
height: 8px;
margin-left: 5px;
border-radius: 50%;
background: var(--pink);
}
.controls {
position: fixed;
z-index: 7;
right: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(8, 23, 40, 0.82);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
button {
border: 0;
color: #06121e;
background: var(--cyan);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
}
#playButton {
min-width: 105px;
padding: 10px 15px;
border-radius: 10px;
transition: filter 150ms ease, transform 150ms ease;
}
#playButton:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
#resetButton {
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 10px;
color: var(--text);
background: rgba(255, 255, 255, 0.055);
}
.speed-control {
display: grid;
gap: 4px;
min-width: 100px;
padding: 0 6px;
}
.speed-label {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
accent-color: var(--cyan);
cursor: pointer;
}
.hint {
position: fixed;
z-index: 5;
left: 25px;
bottom: 25px;
color: #7793ad;
font-size: 11px;
pointer-events: none;
}
.hint kbd {
padding: 3px 6px;
border: 1px solid var(--line);
border-radius: 5px;
color: #b9cee0;
background: rgba(255, 255, 255, 0.04);
font: inherit;
font-size: 10px;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
background: var(--bg);
font-size: 12px;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.header {
top: 17px;
left: 17px;
}
.header p {
display: none;
}
.panel {
top: auto;
right: 14px;
bottom: 83px;
left: 14px;
width: auto;
padding: 13px;
border-radius: 16px;
}
.command {
grid-template-columns: 24px 36px 1fr auto;
min-height: 43px;
padding: 5px 7px;
}
.command-detail,
.current {
display: none;
}
.legend,
.hint {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
left: 14px;
justify-content: center;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="header">
<div class="eyebrow">
<span class="live-dot"></span>
Motion Study 01
</div>
<h1>Follow the written route.</h1>
<p>Each instruction matches an arrow on the same grid path.</p>
</header>
<aside class="panel">
<div class="panel-heading">
<strong>Written directions</strong>
<span class="step-count" id="stepCount">0 / 5</span>
</div>
<div id="commandList"></div>
<div class="progress-wrap">
<div class="progress-label">
<span>Route progress</span>
<span id="progressText">0%</span>
</div>
<div class="progress-track">
<div id="progressBar"></div>
</div>
</div>
<div class="current">
<div id="currentIcon">→</div>
<div>
<b id="currentTitle">Ready to follow</b>
<small id="currentSub">The cart will begin at the start marker.</small>
</div>
</div>
</aside>
<div class="legend">
<div class="legend-title">Matched signals</div>
<div class="legend-row"><span class="swatch"></span> Highlighted grid path</div>
<div class="legend-row"><span class="swatch arrow"></span> Direction arrows</div>
<div class="legend-row"><span class="swatch turn"></span> Turn markers</div>
</div>
<div class="controls">
<button id="playButton" type="button">Start route</button>
<button id="resetButton" type="button">Reset</button>
<label class="speed-control">
<span class="speed-label">
<span>Speed</span>
<span id="speedText">1.0×</span>
</span>
<input
id="speedInput"
type="range"
min="0.5"
max="2"
value="1"
step="0.1"
aria-label="Playback speed"
/>
</label>
</div>
<div class="hint">
<kbd>Drag</kbd> orbit <kbd>Shift + Drag</kbd> pan
<kbd>Scroll</kbd> zoom <kbd>Double-click</kbd> reset view
</div>
<div id="loading">Building the grid route…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const stage = $("stage");
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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.15;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 20, 40);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Custom camera state. No external controls addon is used.
const cameraState = {
target: new THREE.Vector3(-0.6, 0, 0),
theta: 0.68,
phi: 0.86,
radius: 19
};
function updateCamera() {
const { target, theta, phi, radius } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xa9dcff, 0x07101b, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffffff, 3.1);
sun.position.set(-6, 13, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rimLight = new THREE.PointLight(0x36c9ff, 18, 17, 2);
rimLight.position.set(4, 5, -4);
scene.add(rimLight);
// ---------------------------------------------------------------------
// Route data: written directions and their physical equivalents
// ---------------------------------------------------------------------
const commands = [
{
kind: "move",
cells: 4,
title: "Forward 4 cells",
detail: "Travel east",
icon: "→"
},
{
kind: "turn",
delta: -Math.PI / 2,
title: "Turn right",
detail: "Face south",
icon: "↱"
},
{
kind: "move",
cells: 3,
title: "Forward 3 cells",
detail: "Travel south",
icon: "↓"
},
{
kind: "turn",
delta: Math.PI / 2,
title: "Turn left",
detail: "Face east",
icon: "↰"
},
{
kind: "move",
cells: 2,
title: "Forward 2 cells",
detail: "Travel east",
icon: "→"
}
];
let cursor = { x: -6, z: -3 };
let heading = Math.PI / 2;
for (const command of commands) {
command.fromHeading = heading;
command.start = { ...cursor };
if (command.kind === "move") {
command.direction = {
x: Math.sin(heading),
z: Math.cos(heading)
};
command.end = {
x: cursor.x + command.direction.x * command.cells,
z: cursor.z + command.direction.z * command.cells
};
cursor = { ...command.end };
} else {
command.end = { ...cursor };
heading += command.delta;
}
}
const routeStart = commands[0].start;
const routeEnd = commands[commands.length - 1].end;
// ---------------------------------------------------------------------
// Board and grid
// ---------------------------------------------------------------------
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0x10263b,
roughness: 0.87,
metalness: 0.08
});
const board = new THREE.Mesh(
new THREE.BoxGeometry(16, 0.24, 12),
boardMaterial
);
board.position.y = -0.12;
board.receiveShadow = true;
scene.add(board);
const boardEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x183b55,
roughness: 0.72,
metalness: 0.18
});
const edgeGeometryHorizontal = new THREE.BoxGeometry(16.12, 0.12, 0.1);
const edgeGeometryVertical = new THREE.BoxGeometry(0.1, 0.12, 12.12);
for (const z of [-6.02, 6.02]) {
const edge = new THREE.Mesh(edgeGeometryHorizontal, boardEdgeMaterial);
edge.position.set(0, -0.02, z);
scene.add(edge);
}
for (const x of [-8.02, 8.02]) {
const edge = new THREE.Mesh(edgeGeometryVertical, boardEdgeMaterial);
edge.position.set(x, -0.02, 0);
scene.add(edge);
}
const gridPositions = [];
for (let x = -8; x <= 8; x++) {
gridPositions.push(x, 0.008, -6, x, 0.008, 6);
}
for (let z = -6; z <= 6; z++) {
gridPositions.push(-8, 0.008, z, 8, 0.008, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridPositions, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8bc8e8,
transparent: true,
opacity: 0.17
})
);
scene.add(grid);
// ---------------------------------------------------------------------
// Path tiles
// ---------------------------------------------------------------------
const tileGeometry = new THREE.BoxGeometry(0.94, 0.055, 0.94);
const tileMaterials = [
new THREE.MeshStandardMaterial({
color: 0x155b72,
roughness: 0.58,
metalness: 0.12,
emissive: 0x073342,
emissiveIntensity: 0.42
}),
new THREE.MeshStandardMaterial({
color: 0x1b526d,
roughness: 0.6,
metalness: 0.1,
emissive: 0x062c3b,
emissiveIntensity: 0.34
})
];
const tileKeys = new Set();
function addPathTile(x, z) {
const key = `${x},${z}`;
if (tileKeys.has(key)) return;
tileKeys.add(key);
const tile = new THREE.Mesh(
tileGeometry,
tileMaterials[tileKeys.size % tileMaterials.length]
);
tile.position.set(x, 0.03, z);
tile.receiveShadow = true;
scene.add(tile);
}
for (const command of commands) {
if (command.kind !== "move") continue;
for (let i = 0; i <= command.cells; i++) {
addPathTile(
command.start.x + command.direction.x * i,
command.start.z + command.direction.z * i
);
}
}
// ---------------------------------------------------------------------
// Direction arrows and turn markers
// ---------------------------------------------------------------------
const forwardArrowMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x744600,
emissiveIntensity: 0.65,
roughness: 0.42
});
const turnArrowMaterial = new THREE.MeshStandardMaterial({
color: 0xff6da8,
emissive: 0x70143b,
emissiveIntensity: 0.68,
roughness: 0.42
});
function createArrow(material) {
const group = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.025, 0.34),
material
);
shaft.position.set(0, 0.01, -0.08);
group.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.18, 0.28, 3),
material
);
head.rotation.x = Math.PI / 2;
head.position.set(0, 0.01, 0.2);
group.add(head);
return group;
}
const animatedArrows = [];
for (const command of commands) {
if (command.kind === "move") {
for (let i = 0; i < command.cells; i++) {
const arrow = createArrow(forwardArrowMaterial);
arrow.position.set(
command.start.x + command.direction.x * (i + 0.5),
0.095,
command.start.z + command.direction.z * (i + 0.5)
);
arrow.rotation.y = Math.atan2(
command.direction.x,
command.direction.z
);
scene.add(arrow);
animatedArrows.push(arrow);
}
} else {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.36, 0.035, 8, 32),
turnArrowMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(command.start.x, 0.095, command.start.z);
scene.add(ring);
const outgoingHeading = command.fromHeading + command.delta;
const outgoingDirection = {
x: Math.sin(outgoingHeading),
z: Math.cos(outgoingHeading)
};
const arrow = createArrow(turnArrowMaterial);
arrow.position.set(command.start.x, 0.11, command.start.z);
arrow.rotation.y = Math.atan2(
outgoingDirection.x,
outgoingDirection.z
);
scene.add(arrow);
animatedArrows.push(arrow);
}
}
// ---------------------------------------------------------------------
// Text labels made entirely with canvas textures
// ---------------------------------------------------------------------
function createLabel(text, color = "#dff6ff") {
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(5, 18, 31, 0.78)";
context.beginPath();
context.roundRect(8, 20, 496, 88, 22);
context.fill();
context.strokeStyle = "rgba(140, 215, 255, 0.32)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = color;
context.font = "700 38px system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 256, 64);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.3, 0.575, 1);
return sprite;
}
const startLabel = createLabel("START", "#71e6b1");
startLabel.position.set(routeStart.x, 0.82, routeStart.z - 0.3);
scene.add(startLabel);
const finishLabel = createLabel("FINISH", "#ffd166");
finishLabel.position.set(routeEnd.x, 0.95, routeEnd.z);
scene.add(finishLabel);
for (const command of commands) {
if (command.kind !== "turn") continue;
const label = createLabel(
command.title.toUpperCase(),
"#ff9dc5"
);
label.position.set(command.start.x, 0.7, command.start.z);
label.scale.set(1.75, 0.44, 1);
scene.add(label);
}
// Start marker.
const startRing = new THREE.Mesh(
new THREE.TorusGeometry(0.36, 0.045, 8, 32),
new THREE.MeshStandardMaterial({
color: 0x71e6b1,
emissive: 0x15583e,
emissiveIntensity: 0.7
})
);
startRing.rotation.x = Math.PI / 2;
startRing.position.set(routeStart.x, 0.095, routeStart.z);
scene.add(startRing);
// Finish flag.
const flagGroup = new THREE.Group();
flagGroup.position.set(routeEnd.x, 0, routeEnd.z);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 1.12, 12),
new THREE.MeshStandardMaterial({
color: 0xd9edf8,
metalness: 0.55,
roughness: 0.35
})
);
pole.position.y = 0.56;
pole.castShadow = true;
flagGroup.add(pole);
const flagShape = new THREE.Shape();
flagShape.moveTo(0, 0);
flagShape.lineTo(0.58, -0.15);
flagShape.lineTo(0, -0.36);
flagShape.closePath();
const flag = new THREE.Mesh(
new THREE.ShapeGeometry(flagShape),
new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x654000,
emissiveIntensity: 0.55,
side: THREE.DoubleSide,
roughness: 0.45
})
);
flag.position.set(0.02, 1.08, 0);
flag.castShadow = true;
flagGroup.add(flag);
scene.add(flagGroup);
// ---------------------------------------------------------------------
// Decorative low-poly blocks, kept away from the route
// ---------------------------------------------------------------------
const blockPositions = [
[4.2, -4.1, 0.55],
[5.6, -4.0, 0.85],
[6.9, -3.0, 0.65],
[4.2, -1.2, 0.72],
[5.6, -0.5, 1.08],
[6.9, 0.4, 0.75],
[4.8, 2.3, 0.48],
[6.4, 2.7, 0.92]
];
for (let i = 0; i < blockPositions.length; i++) {
const [x, z, height] = blockPositions[i];
const block = new THREE.Mesh(
new THREE.BoxGeometry(0.72, height, 0.72),
new THREE.MeshStandardMaterial({
color: i % 2 ? 0x193d57 : 0x153249,
roughness: 0.72,
metalness: 0.14
})
);
block.position.set(x, height / 2 + 0.015, z);
block.castShadow = true;
block.receiveShadow = true;
scene.add(block);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.77, 0.045, 0.77),
new THREE.MeshStandardMaterial({
color: 0x2c6580,
emissive: 0x0b2938,
emissiveIntensity: 0.5
})
);
cap.position.set(x, height + 0.03, z);
scene.add(cap);
}
// ---------------------------------------------------------------------
// Stylized cart
// ---------------------------------------------------------------------
const cart = new THREE.Group();
scene.add(cart);
const cartOrange = new THREE.MeshStandardMaterial({
color: 0xff7b4d,
roughness: 0.44,
metalness: 0.12
});
const cartDark = new THREE.MeshStandardMaterial({
color: 0x132536,
roughness: 0.62,
metalness: 0.24
});
const cartCream = new THREE.MeshStandardMaterial({
color: 0xffe4b5,
roughness: 0.62
});
const cartTeal = new THREE.MeshStandardMaterial({
color: 0x4bd5c4,
roughness: 0.46,
metalness: 0.06
});
function addCartMesh(geometry, material, position) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
cart.add(mesh);
return mesh;
}
addCartMesh(
new THREE.BoxGeometry(0.72, 0.13, 0.9),
cartDark,
new THREE.Vector3(0, 0.21, 0)
);
addCartMesh(
new THREE.BoxGeometry(0.72, 0.16, 0.88),
cartOrange,
new THREE.Vector3(0, 0.34, 0)
);
addCartMesh(
new THREE.BoxGeometry(0.09, 0.3, 0.78),
cartOrange,
new THREE.Vector3(-0.325, 0.5, -0.02)
);
addCartMesh(
new THREE.BoxGeometry(0.09, 0.3, 0.78),
cartOrange,
new THREE.Vector3(0.325, 0.5, -0.02)
);
addCartMesh(
new THREE.BoxGeometry(0.66, 0.3, 0.09),
cartOrange,
new THREE.Vector3(0, 0.5, 0.39)
);
addCartMesh(
new THREE.BoxGeometry(0.5, 0.28, 0.48),
cartCream,
new THREE.Vector3(0, 0.54, -0.17)
);
addCartMesh(
new THREE.BoxGeometry(0.42, 0.08, 0.39),
cartTeal,
new THREE.Vector3(0, 0.72, -0.17)
);
addCartMesh(
new THREE.BoxGeometry(0.13, 0.055, 0.17),
cartCream,
new THREE.Vector3(0, 0.46, 0.47)
);
const wheelMeshes = [];
function createWheel(x, z) {
const pivot = new THREE.Group();
pivot.position.set(x, 0.18, z);
pivot.rotation.z = Math.PI / 2;
const wheel = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 0.11, 18),
cartDark
);
wheel.castShadow = true;
const hub = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 0.115, 14),
cartCream
);
pivot.add(wheel, hub);
cart.add(pivot);
wheelMeshes.push(wheel);
}
createWheel(-0.39, 0.31);
createWheel(0.39, 0.31);
createWheel(-0.39, -0.31);
createWheel(0.39, -0.31);
// A small rear handle makes the cart silhouette more distinct.
const handle = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.025, 8, 20, Math.PI),
cartDark
);
handle.rotation.y = Math.PI / 2;
handle.position.set(0, 0.6, -0.57);
handle.castShadow = true;
cart.add(handle);
// ---------------------------------------------------------------------
// Written direction UI
// ---------------------------------------------------------------------
const commandRows = [];
commands.forEach((command, index) => {
const row = document.createElement("div");
row.className = `command${command.kind === "turn" ? " turn" : ""}`;
row.innerHTML = `
<span class="command-number">${String(index + 1).padStart(2, "0")}</span>
<span class="command-icon">${command.icon}</span>
<span>
<span class="command-title">${command.title}</span>
<span class="command-detail">${command.detail}</span>
</span>
<span class="command-state">Waiting</span>
`;
$("commandList").appendChild(row);
commandRows.push(row);
});
// ---------------------------------------------------------------------
// Animation state
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let commandIndex = 0;
let commandElapsed = 0;
let running = false;
let started = false;
let completed = false;
let speed = 1;
let autoStartTimer = null;
const totalMovementCells = commands.reduce(
(sum, command) =>
sum + (command.kind === "move" ? command.cells : 0),
0
);
function updateButton() {
if (completed) {
$("playButton").textContent = "Replay";
} else if (running) {
$("playButton").textContent = "Pause";
} else if (started) {
$("playButton").textContent = "Resume";
} else {
$("playButton").textContent = "Start route";
}
}
function refreshCommandUI() {
commandRows.forEach((row, index) => {
const isDone = completed || index < commandIndex;
const isActive = !completed && index === commandIndex;
row.classList.toggle("done", isDone);
row.classList.toggle("active", isActive);
const state = row.querySelector(".command-state");
state.textContent = isDone ? "Done" : isActive ? "Now" : "Waiting";
});
$("stepCount").textContent = completed
? `${commands.length} / ${commands.length}`
: `${commandIndex} / ${commands.length}`;
if (completed) {
$("currentIcon").textContent = "✓";
$("currentTitle").textContent = "Route complete";
$("currentSub").textContent = "The cart followed all written directions.";
} else {
const command = commands[commandIndex];
$("currentIcon").textContent = command.icon;
$("currentTitle").textContent = command.title;
$("currentSub").textContent = command.detail;
}
}
function updateProgress() {
let movedCells = 0;
for (let i = 0; i < commandIndex; i++) {
if (commands[i].kind === "move") {
movedCells += commands[i].cells;
}
}
if (!completed && commands[commandIndex]?.kind === "move") {
movedCells += Math.min(commandElapsed, commands[commandIndex].cells);
}
if (completed) movedCells = totalMovementCells;
const progress = Math.min(movedCells / totalMovementCells, 1);
$("progressBar").style.width = `${progress * 100}%`;
$("progressText").textContent = `${Math.round(progress * 100)}%`;
}
function resetTour(scheduleAutoStart = false) {
clearTimeout(autoStartTimer);
commandIndex = 0;
commandElapsed = 0;
running = false;
started = false;
completed = false;
heading = commands[0].fromHeading;
cart.position.set(routeStart.x, 0.055, routeStart.z);
cart.rotation.set(0, heading, 0);
for (const wheel of wheelMeshes) {
wheel.rotation.y = 0;
}
refreshCommandUI();
updateProgress();
updateButton();
if (scheduleAutoStart) {
autoStartTimer = setTimeout(() => {
if (!started && !completed) {
started = true;
running = true;
updateButton();
}
}, 650);
}
}
function beginCommand(index) {
commandIndex = index;
commandElapsed = 0;
refreshCommandUI();
}
function finishRoute() {
completed = true;
running = false;
started = true;
commandIndex = commands.length;
refreshCommandUI();
updateProgress();
updateButton();
}
function animateRoute(dt) {
if (!running || completed) return;
const command = commands[commandIndex];
if (command.kind === "move") {
commandElapsed += dt * speed;
const progress = Math.min(commandElapsed / command.cells, 1);
const x = THREE.MathUtils.lerp(
command.start.x,
command.end.x,
progress
);
const z = THREE.MathUtils.lerp(
command.start.z,
command.end.z,
progress
);
const traveledThisFrame =
dt * speed * Math.hypot(command.direction.x, command.direction.z);
cart.position.x = x;
cart.position.z = z;
heading = command.fromHeading;
cart.rotation.y = heading;
for (const wheel of wheelMeshes) {
wheel.rotation.y -= traveledThisFrame / 0.16;
}
if (progress >= 1) {
if (commandIndex + 1 < commands.length) {
beginCommand(commandIndex + 1);
} else {
finishRoute();
}
}
} else {
commandElapsed += dt * speed;
const progress = Math.min(commandElapsed / 0.72, 1);
const eased = progress * progress * (3 - 2 * progress);
heading =
command.fromHeading + command.delta * eased;
cart.rotation.y = heading;
if (progress >= 1) {
if (commandIndex + 1 < commands.length) {
beginCommand(commandIndex + 1);
} else {
finishRoute();
}
}
}
updateProgress();
}
// ---------------------------------------------------------------------
// Playback controls
// ---------------------------------------------------------------------
$("playButton").addEventListener("click", () => {
clearTimeout(autoStartTimer);
if (completed) {
resetTour(false);
started = true;
running = true;
} else if (running) {
running = false;
} else {
started = true;
running = true;
}
updateButton();
});
$("resetButton").addEventListener("click", () => {
resetTour(false);
});
$("speedInput").addEventListener("input", (event) => {
speed = Number(event.target.value);
$("speedText").textContent = `${speed.toFixed(1)}×`;
});
// ---------------------------------------------------------------------
// Custom camera controls: drag orbit, Shift-drag pan, wheel zoom
// ---------------------------------------------------------------------
const pointer = {
active: false,
id: null,
lastX: 0,
lastY: 0,
mode: "orbit"
};
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
pointer.active = true;
pointer.id = event.pointerId;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
pointer.mode = event.shiftKey ? "pan" : "orbit";
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
if (pointer.mode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.35,
1.28
);
} else {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const panScale = cameraState.radius * 0.0012;
cameraState.target.addScaledVector(right, -dx * panScale);
cameraState.target.addScaledVector(forward, dy * panScale);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-6,
6
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-5,
5
);
}
updateCamera();
});
function endPointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
10,
32
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.target.set(-0.6, 0, 0);
cameraState.theta = 0.68;
cameraState.phi = 0.86;
cameraState.radius = 19;
updateCamera();
});
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
resetTour(true);
$("loading").classList.add("hidden");
function render() {
requestAnimationFrame(render);
const dt = Math.min(clock.getDelta(), 0.05);
animateRoute(dt);
const pulse = 1 + Math.sin(clock.elapsedTime * 3.1) * 0.045;
for (let i = 0; i < animatedArrows.length; i++) {
animatedArrows[i].scale.setScalar(
pulse + Math.sin(clock.elapsedTime * 2.4 + i * 0.45) * 0.025
);
}
startRing.rotation.z = clock.elapsedTime * 0.35;
flagGroup.rotation.y = Math.sin(clock.elapsedTime * 1.6) * 0.035;
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>
```