Prompt specification
Build or customize the Three.js 3D scene "Trace deliveries through a gate and branching lanes" 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: Trace deliveries through a gate and branching lanes
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/trace-deliveries-through-a-gate-and-branching-lanes-x2912
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Bab Bou Jeloud and adjacent lanes in Fez. Let users guide a fictional handcart to two shops through the schematic network. Show turning constraints and shared pedestrian space without assigning actual local access rules. Low-poly.
Working Three.js Starter (trace-deliveries-through-a-gate-and-branching-lanes-x2912):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover" />
<title>Bab Bou Jeloud — Fictional Handcart Study</title>
<style>
:root {
--ink: #263238;
--muted: #6e6254;
--paper: rgba(255, 249, 235, 0.92);
--line: rgba(73, 58, 43, 0.18);
--blue: #257a91;
--gold: #d99332;
--green: #56845f;
--rose: #a8564e;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9c8a7;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
background: var(--paper);
border: 1px solid var(--line);
box-shadow: 0 12px 35px rgba(61, 42, 24, 0.14);
backdrop-filter: blur(12px);
}
header {
position: absolute;
top: 18px;
left: 18px;
width: min(410px, calc(100vw - 36px));
padding: 16px 18px 14px;
border-radius: 16px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
color: var(--blue);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 0 4px rgba(217, 147, 50, 0.14);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 36px);
font-weight: 500;
letter-spacing: -0.025em;
line-height: 1;
}
.subtitle {
margin-top: 8px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.objective {
position: absolute;
top: 18px;
right: 18px;
width: min(310px, calc(100vw - 36px));
padding: 15px;
border-radius: 16px;
}
.objective-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#objectiveTitle {
margin-top: 5px;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
line-height: 1.15;
}
#objectiveHint {
min-height: 36px;
margin-top: 7px;
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
.steps {
display: flex;
gap: 7px;
margin-top: 12px;
}
.step {
display: flex;
flex: 1;
align-items: center;
gap: 7px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
}
.step b {
display: grid;
width: 19px;
height: 19px;
flex: 0 0 19px;
place-items: center;
border-radius: 50%;
background: #e5dcc9;
color: #6c604e;
font-size: 9px;
}
.step.active {
border-color: rgba(37, 122, 145, 0.45);
color: var(--blue);
}
.step.active b { background: var(--blue); color: white; }
.step.done { color: var(--green); }
.step.done b { background: var(--green); color: white; }
.map-panel {
position: absolute;
right: 18px;
bottom: 18px;
width: 224px;
padding: 11px;
border-radius: 15px;
}
.map-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#minimap {
display: block;
width: 100%;
height: 178px;
border-radius: 9px;
background: #eadfca;
}
.map-legend {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: var(--muted);
font-size: 9px;
}
.dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 3px;
border-radius: 50%;
vertical-align: 0;
}
.dot.blue { background: var(--blue); }
.dot.rose { background: var(--rose); }
.controls {
position: absolute;
bottom: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 12px 14px;
border-radius: 15px;
}
.control-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.keys {
color: var(--muted);
font-size: 10px;
line-height: 1.7;
}
kbd {
display: inline-block;
min-width: 20px;
margin: 0 1px;
padding: 2px 5px;
border: 1px solid rgba(65, 49, 35, 0.2);
border-bottom-width: 2px;
border-radius: 5px;
background: #fffaf0;
color: var(--ink);
font: 700 9px ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
.buttons {
display: flex;
gap: 6px;
pointer-events: auto;
}
button {
border: 1px solid rgba(67, 52, 39, 0.2);
border-radius: 9px;
background: #fffaf0;
color: var(--ink);
padding: 8px 10px;
cursor: pointer;
font: 700 10px inherit;
box-shadow: 0 2px 0 rgba(67, 52, 39, 0.09);
}
button:hover { background: #f1e5ce; }
button:active { transform: translateY(1px); box-shadow: none; }
button.primary { background: var(--blue); color: white; border-color: var(--blue); }
#turnCue {
min-height: 18px;
margin-top: 8px;
padding-top: 8px;
border-top: 1px solid var(--line);
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.dpad {
display: none;
grid-template-columns: repeat(3, 44px);
grid-template-rows: repeat(2, 42px);
gap: 4px;
pointer-events: auto;
}
.dpad button {
padding: 0;
border-radius: 10px;
font-size: 17px;
touch-action: none;
user-select: none;
}
.dpad .up { grid-column: 2; }
.dpad .left { grid-column: 1; grid-row: 2; }
.dpad .down { grid-column: 2; grid-row: 2; }
.dpad .right { grid-column: 3; grid-row: 2; }
.disclaimer {
position: absolute;
left: 50%;
bottom: 20px;
width: min(480px, calc(100vw - 420px));
transform: translateX(-50%);
color: rgba(49, 43, 35, 0.72);
font-size: 9px;
line-height: 1.4;
text-align: center;
text-shadow: 0 1px 2px rgba(255,255,255,.7);
}
#toast {
position: absolute;
left: 50%;
top: 17%;
max-width: min(500px, calc(100vw - 30px));
padding: 11px 15px;
transform: translate(-50%, -8px);
border-radius: 12px;
background: rgba(38, 50, 56, 0.92);
color: #fffaf0;
box-shadow: 0 10px 30px rgba(30, 24, 18, 0.22);
font-size: 12px;
line-height: 1.35;
opacity: 0;
transition: opacity .2s, transform .2s;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
header {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 12px 14px;
}
.subtitle { display: none; }
.objective {
top: 98px;
right: 10px;
width: 220px;
padding: 11px;
}
#objectiveHint { display: none; }
.steps { margin-top: 8px; }
.step { padding: 5px; font-size: 8px; }
.map-panel {
right: 10px;
bottom: 116px;
width: 154px;
padding: 8px;
}
#minimap { height: 120px; }
.map-legend { display: none; }
.controls {
left: 10px;
bottom: 10px;
width: calc(100vw - 184px);
min-width: 158px;
padding: 10px;
}
.keys { display: none; }
.control-row { justify-content: center; }
.buttons { flex-wrap: wrap; justify-content: center; }
.buttons button { padding: 7px 8px; }
.disclaimer { display: none; }
.dpad {
position: absolute;
right: 10px;
bottom: 10px;
display: grid;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly schematic of Bab Bou Jeloud with a fictional handcart"></canvas>
<div class="hud">
<header class="panel">
<div class="eyebrow">Fictional route study · Fez</div>
<h1>Bab Bou Jeloud</h1>
<div class="subtitle">
Guide a handcart through a schematic lane network and make two fictional deliveries.
The streets, shops, and turn envelopes are illustrative rather than survey-accurate.
</div>
</header>
<section class="objective panel" aria-live="polite">
<div class="objective-label">Current objective</div>
<div id="objectiveTitle">Reach Blue Pottery</div>
<div id="objectiveHint">Continue north through the gate toward the pottery lane.</div>
<div class="steps">
<div class="step active" id="stepA"><b>1</b><span>Pottery</span></div>
<div class="step" id="stepB"><b>2</b><span>Textile</span></div>
</div>
</section>
<section class="controls panel">
<div class="control-row">
<div class="keys">
<kbd>W</kbd>/<kbd>↑</kbd> drive <kbd>S</kbd>/<kbd>↓</kbd> reverse<br />
<kbd>A</kbd>/<kbd>←</kbd> queue left <kbd>D</kbd>/<kbd>→</kbd> queue right
</div>
<div class="buttons">
<button id="followButton" class="primary">Follow cart</button>
<button id="resetButton">Reset</button>
</div>
</div>
<div id="turnCue">Turn cue: none</div>
</section>
<section class="map-panel panel">
<div class="map-head"><span>Schematic lanes</span><span>N ↑</span></div>
<canvas id="minimap" width="404" height="356"></canvas>
<div class="map-legend">
<span><i class="dot blue"></i>shop</span>
<span><i class="dot rose"></i>cart</span>
</div>
</section>
<div class="dpad" aria-label="Touch controls">
<button class="up" data-key="ArrowUp" aria-label="Drive forward">↑</button>
<button class="left" data-key="ArrowLeft" aria-label="Queue left turn">←</button>
<button class="down" data-key="ArrowDown" aria-label="Reverse">↓</button>
<button class="right" data-key="ArrowRight" aria-label="Queue right turn">→</button>
</div>
<div class="disclaimer">
Imagined geometry and fictional cart movements only. This scene does not state or encode
actual vehicle access, pedestrian priority, delivery, or local traffic rules.
</div>
<div id="toast" role="status"></div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9c8a7);
scene.fog = new THREE.Fog(0xd9c8a7, 58, 110);
const camera = new THREE.PerspectiveCamera(44, innerWidth / innerHeight, 0.1, 180);
const hemi = new THREE.HemisphereLight(0xfff2d8, 0x756e59, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe4b0, 3.2);
sun.position.set(-24, 38, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 48;
sun.shadow.camera.bottom = -48;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const material = (color, extra = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...extra
});
const M = {
ground: material(0xc9b894),
road: material(0xd7c39c),
roadEdge: material(0xe6d6b8),
marking: material(0xf2e7cf),
wall: material(0xe5d4b6),
wall2: material(0xd4b895),
wall3: material(0xc99d7b),
terracotta: material(0xa85f45),
wood: material(0x805a36),
woodLight: material(0xb1814d),
metal: material(0x3e484b, { metalness: 0.25 }),
blue: material(0x28778b),
blueDark: material(0x174d5e),
green: material(0x567854),
greenLight: material(0x789060),
gold: material(0xd89a35),
cream: material(0xf5e9ce),
rose: material(0xa8554e),
dark: material(0x32383a)
};
function box(w, h, d, x, y, z, mat, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, x, y, z, mat, parent = scene, segments = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(radius, x, y, z, mat, parent = scene, segments = 8) {
const mesh = new THREE.Mesh(new THREE.SphereGeometry(radius, segments, 6), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabel(text, {
width = 6,
height = 1.35,
color = "#fff8e8",
background = "rgba(38,50,56,.91)",
accent = "#d99a37",
fontSize = 44
} = {}) {
const c = document.createElement("canvas");
c.width = 768;
c.height = 168;
const ctx = c.getContext("2d");
ctx.fillStyle = background;
ctx.beginPath();
ctx.roundRect(4, 4, 760, 160, 22);
ctx.fill();
ctx.fillStyle = accent;
ctx.fillRect(20, 28, 9, 112);
ctx.fillStyle = color;
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.font = `600 ${fontSize}px system-ui, sans-serif`;
const lines = text.split("\n");
const lineHeight = 52;
const startY = lines.length === 1 ? 84 : 60;
lines.forEach((line, i) => ctx.fillText(line, 52, startY + i * lineHeight));
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
// -------------------------------------------------------------------------
// Schematic graph
// -------------------------------------------------------------------------
const nodes = [
{ id: "gate", x: 0, z: 0 },
{ id: "n1", x: 0, z: -10 },
{ id: "pottery", x: 0, z: -20 },
{ id: "e1", x: 10, z: 0 },
{ id: "east", x: 20, z: 0 },
{ id: "w1", x: -10, z: 0 },
{ id: "w2", x: -10, z: 10 },
{ id: "textile", x: -10, z: 20 }
];
const edges = [
{ a: 0, b: 1 },
{ a: 1, b: 2 },
{ a: 0, b: 3 },
{ a: 3, b: 4 },
{ a: 0, b: 5 },
{ a: 5, b: 6 },
{ a: 6, b: 7 }
];
const edgeLength = e => Math.hypot(
nodes[e.b].x - nodes[e.a].x,
nodes[e.b].z - nodes[e.a].z
);
function edgePoint(e, t, out = new THREE.Vector3()) {
const a = nodes[e.a];
const b = nodes[e.b];
return out.set(
THREE.MathUtils.lerp(a.x, b.x, t),
0.17,
THREE.MathUtils.lerp(a.z, b.z, t)
);
}
// -------------------------------------------------------------------------
// Ground, lanes, markings
// -------------------------------------------------------------------------
box(100, 0.35, 92, 0, -0.28, -2, M.ground);
edges.forEach((e, index) => {
const a = nodes[e.a];
const b = nodes[e.b];
const length = edgeLength(e);
const horizontal = a.z === b.z;
box(
horizontal ? length : 6.2,
0.16,
horizontal ? 6.2 : length,
(a.x + b.x) / 2,
0.02,
(a.z + b.z) / 2,
M.road
);
const stripeCount = Math.floor(length / 3.2);
for (let i = 1; i < stripeCount; i++) {
const t = i / stripeCount;
box(
horizontal ? 1.15 : 0.16,
0.035,
horizontal ? 0.16 : 1.15,
THREE.MathUtils.lerp(a.x, b.x, t),
0.125,
THREE.MathUtils.lerp(a.z, b.z, t),
M.marking
);
}
// Low pale borders suggest an open shared surface rather than a protected lane.
const offset = 3.18;
if (horizontal) {
box(length, 0.08, 0.18, (a.x + b.x) / 2, 0.13, (a.z + b.z) / 2 - offset, M.roadEdge);
box(length, 0.08, 0.18, (a.x + b.x) / 2, 0.13, (a.z + b.z) / 2 + offset, M.roadEdge);
} else {
box(0.18, 0.08, length, (a.x + b.x) / 2 - offset, 0.13, (a.z + b.z) / 2, M.roadEdge);
box(0.18, 0.08, length, (a.x + b.x) / 2 + offset, 0.13, (a.z + b.z) / 2, M.roadEdge);
}
});
nodes.forEach(n => {
cylinder(3.12, 3.12, 0.16, n.x, 0.025, n.z, M.road, scene, 16);
});
// -------------------------------------------------------------------------
// Bab Bou Jeloud-inspired low-poly gate
// -------------------------------------------------------------------------
const gate = new THREE.Group();
gate.position.set(0, 0, 0);
scene.add(gate);
box(2.15, 5.8, 2.7, -4.35, 2.9, 0, M.wall2, gate);
box(2.15, 5.8, 2.7, 4.35, 2.9, 0, M.wall2, gate);
box(1.5, 1.2, 3.0, -4.35, 6.25, 0, M.wall3, gate);
box(1.5, 1.2, 3.0, 4.35, 6.25, 0, M.wall3, gate);
// Facade bands
box(2.35, 0.35, 2.9, -4.35, 5.55, 0, M.cream, gate);
box(2.35, 0.35, 2.9, 4.35, 5.55, 0, M.cream, gate);
box(2.4, 0.42, 3.1, -4.35, 0.25, 0, M.wall3, gate);
box(2.4, 0.42, 3.1, 4.35, 0.25, 0, M.wall3, gate);
// Faceted voussoirs
const archRadius = 3.45;
for (let i = 0; i < 11; i++) {
const theta = (i + 0.5) * Math.PI / 11;
const stone = box(
0.83,
0.94,
2.85,
Math.cos(theta) * archRadius,
3.15 + Math.sin(theta) * archRadius,
0,
i % 2 ? M.wall : M.wall2,
gate
);
stone.rotation.z = -theta + Math.PI / 2;
}
// Decorative upper wall and merlons
box(8.8, 0.65, 2.8, 0, 7.15, 0, M.wall2, gate);
for (let i = 0; i < 7; i++) {
box(0.68, 0.72, 2.95, -3.6 + i * 1.2, 7.8, 0, M.wall3, gate);
}
// Gate details
box(0.25, 2.7, 0.12, -3.45, 1.5, 1.43, M.blueDark, gate);
box(0.25, 2.7, 0.12, 3.45, 1.5, 1.43, M.blueDark, gate);
sphere(0.12, -3.45, 1.45, 1.55, M.gold, gate);
sphere(0.12, 3.45, 1.45, 1.55, M.gold, gate);
const gateLabel = makeLabel("BAB BOU JELOUD\nschematic gateway", {
width: 7.2,
height: 1.65,
accent: "#d99a37"
});
gateLabel.position.set(0, 9.15, 0);
scene.add(gateLabel);
// -------------------------------------------------------------------------
// Buildings and shops
// -------------------------------------------------------------------------
function makeBuilding({
x, z, rotation = 0, width = 6, depth = 5, height = 5.4,
wall = M.wall, roof = M.terracotta, shop = false
}) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = rotation;
scene.add(g);
box(width, height, depth, 0, height / 2, 0, wall, g);
box(width + 0.35, 0.34, depth + 0.35, 0, height + 0.08, 0, roof, g);
box(width + 0.1, 0.2, depth + 0.1, 0, height + 0.31, 0, M.cream, g);
// Front door
box(1.05, 2.25, 0.14, 0, 1.13, depth / 2 + 0.08, M.blueDark, g);
box(0.08, 0.08, 0.12, 0.36, 1.08, depth / 2 + 0.2, M.gold, g);
// Recessed-looking windows
const windows = width > 7 ? [-2.35, 0, 2.35] : [-1.75, 1.75];
windows.forEach(wx => {
box(0.88, 1.05, 0.11, wx, height * 0.63, depth / 2 + 0.09, M.blue, g);
box(1.03, 0.12, 0.18, wx, height * 0.63 + 0.58, depth / 2 + 0.12, M.cream, g);
box(0.09, 1.1, 0.16, wx, height * 0.63, depth / 2 + 0.13, M.cream, g);
});
// Side window
box(0.1, 0.85, 0.8, width / 2 + 0.07, height * 0.61, 0.35, M.blueDark, g);
if (shop) {
// Striped awning
const awningW = width * 0.68;
for (let i = 0; i < 6; i++) {
const stripe = box(
awningW / 6,
0.16,
1.45,
-awningW / 2 + (i + 0.5) * awningW / 6,
2.75,
depth / 2 + 0.55,
i % 2 ? M.cream : M.blue,
g
);
stripe.rotation.x = -0.12;
}
box(awningW, 0.11, 0.12, 0, 2.59, depth / 2 + 1.15, M.wood, g);
}
return g;
}
makeBuilding({ x: -5.7, z: -11.8, rotation: Math.PI / 2, width: 6.8, depth: 5, wall: M.wall2 });
makeBuilding({ x: 6.0, z: -15.8, rotation: -Math.PI / 2, width: 6.3, depth: 5.2, wall: M.wall3 });
makeBuilding({ x: -6.2, z: -18.0, rotation: Math.PI / 2, width: 5.7, depth: 5.2, wall: M.wall });
makeBuilding({ x: 6.5, z: -5.9, rotation: -Math.PI / 2, width: 6.8, depth: 5.4, wall: M.wall2 });
makeBuilding({ x: 14.4, z: -5.8, rotation: -Math.PI / 2, width: 7.4, depth: 5.2, wall: M.wall });
makeBuilding({ x: 19.8, z: -5.3, rotation: -Math.PI / 2, width: 6.5, depth: 4.8, wall: M.wall3 });
makeBuilding({ x: 8.4, z: 5.9, rotation: Math.PI / 2, width: 6.7, depth: 5.1, wall: M.wall });
makeBuilding({ x: 16.0, z: 5.9, rotation: Math.PI / 2, width: 7.3, depth: 5.4, wall: M.wall2 });
makeBuilding({ x: -15.3, z: 6.3, rotation: -Math.PI / 2, width: 6.4, depth: 5.2, wall: M.wall2 });
makeBuilding({ x: -15.6, z: 14.7, rotation: -Math.PI / 2, width: 7.0, depth: 5.0, wall: M.wall3 });
makeBuilding({ x: -4.7, z: 8.1, rotation: Math.PI / 2, width: 6.0, depth: 5.2, wall: M.wall });
makeBuilding({ x: 4.8, z: 12.2, rotation: -Math.PI / 2, width: 7.0, depth: 5.1, wall: M.wall2 });
const potteryBuilding = makeBuilding({
x: 4.8, z: -21.5, rotation: -Math.PI / 2,
width: 7.6, depth: 5.6, height: 5.7,
wall: M.wall3, shop: true
});
const textileBuilding = makeBuilding({
x: -14.8, z: 20.2, rotation: Math.PI / 2,
width: 7.6, depth: 5.6, height: 5.9,
wall: M.wall, shop: true
});
const potteryLabel = makeLabel("BLUE POTTERY\nfictional delivery 01", {
width: 6.5, height: 1.5, accent: "#2b8198"
});
potteryLabel.position.set(4.7, 6.7, -21.4);
scene.add(potteryLabel);
const textileLabel = makeLabel("TEXTILE HOUSE\nfictional delivery 02", {
width: 6.5, height: 1.5, accent: "#b36056"
});
textileLabel.position.set(-14.7, 7.1, 20.2);
scene.add(textileLabel);
// Shop merchandise
for (let i = 0; i < 7; i++) {
const pot = cylinder(
0.18 + (i % 3) * 0.04,
0.28 + (i % 3) * 0.05,
0.58,
2.1 + i * 0.48,
0.42,
-19.9,
i % 2 ? M.blue : M.blueDark,
scene,
8
);
pot.rotation.y = i * 0.4;
}
for (let i = 0; i < 5; i++) {
const roll = cylinder(0.23, 0.23, 1.3, -12.25, 0.72, 18.3 + i * 0.68, i % 2 ? M.rose : M.gold, scene, 8);
roll.rotation.z = Math.PI / 2;
}
// Delivery rings
function deliveryRing(x, z, color) {
const mat = new THREE.MeshStandardMaterial({
color, emissive: color, emissiveIntensity: 0.28, roughness: 0.65
});
const ring = new THREE.Mesh(new THREE.TorusGeometry(1.25, 0.09, 6, 32), mat);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.24, z);
scene.add(ring);
return ring;
}
const potteryRing = deliveryRing(nodes[2].x, nodes[2].z, 0x2b8198);
const textileRing = deliveryRing(nodes[7].x, nodes[7].z, 0xb36056);
// -------------------------------------------------------------------------
// Turn envelopes and shared-space markers
// -------------------------------------------------------------------------
function dashedCircle(x, z, radius, color = 0xd99435) {
const points = [];
for (let i = 0; i <= 72; i++) {
const a = i / 72 * Math.PI * 2;
points.push(new THREE.Vector3(x + Math.cos(a) * radius, 0.24, z + Math.sin(a) * radius));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, new THREE.LineDashedMaterial({
color, dashSize: 0.55, gapSize: 0.35, transparent: true, opacity: 0.75
}));
line.computeLineDistances();
scene.add(line);
}
dashedCircle(nodes[0].x, nodes[0].z, 4.25);
dashedCircle(nodes[5].x, nodes[5].z, 4.25);
function planter(x, z) {
cylinder(0.78, 0.95, 0.55, x, 0.31, z, M.wall3, scene, 8);
cylinder(0.68, 0.68, 0.18, x, 0.66, z, M.wood, scene, 8);
sphere(0.72, x, 1.15, z, M.green, scene, 7);
}
planter(4.45, 4.25);
planter(-6.35, 4.2);
planter(4.45, -4.25);
const turnLabel = makeLabel("TURN ENVELOPE\ncart geometry only", {
width: 5.4, height: 1.35, accent: "#d99435", fontSize: 39
});
turnLabel.position.set(-5.3, 2.9, 4.5);
scene.add(turnLabel);
const sharedLabel = makeLabel("SHARED SURFACE\ncart + pedestrians", {
width: 5.4, height: 1.35, accent: "#257a91", fontSize: 39
});
sharedLabel.position.set(5.1, 2.5, 4.2);
scene.add(sharedLabel);
// -------------------------------------------------------------------------
// Trees, stalls, lanterns, decorative props
// -------------------------------------------------------------------------
function tree(x, z, scale = 1) {
cylinder(0.13, 0.2, 2.2 * scale, x, 1.1 * scale, z, M.wood, scene, 7);
const crown = sphere(1.05 * scale, x, 2.6 * scale, z, M.green, scene, 7);
crown.scale.y = 1.15;
const crown2 = sphere(0.72 * scale, x + 0.38 * scale, 3.15 * scale, z, M.greenLight, scene, 7);
}
[
[-4.3, 3.8, .9], [4.4, -4.1, .85], [7.8, 3.9, 1.05],
[-6.3, -7.2, .8], [4.4, 7.2, .85], [12.8, 4, .9],
[-4.5, 13.5, 1.0], [4.4, 17.3, .9], [-16.8, 3.7, .9],
[-4.7, -11.5, .75], [17.5, 4.0, .8]
].forEach(p => tree(...p));
function stall(x, z, rotation, color) {
const g = new THREE.Group();
g.position.set(x, 0, z);
g.rotation.y = rotation;
scene.add(g);
box(2.8, 0.18, 1.6, 0, 1.05, 0, M.woodLight, g);
for (const sx of [-1.2, 1.2]) {
for (const sz of [-0.55, 0.55]) {
box(0.09, 1.05, 0.09, sx, 0.53, sz, M.wood, g);
}
}
for (let i = 0; i < 5; i++) {
box(0.55, 0.12, 1.75, -1.1 + i * 0.55, 2.05, 0, i % 2 ? M.cream : color, g);
}
}
stall(6.7, 3.7, 0, M.rose);
stall(-13.0, -3.8, Math.PI / 2, M.blue);
stall(4.2, 15.8, -Math.PI / 2, M.gold);
function lantern(x, y, z, color) {
const glow = material(color, { emissive: color, emissiveIntensity: 0.7 });
const lamp = sphere(0.17, x, y, z, glow, scene, 7);
box(0.05, 0.32, 0.05, x, y + 0.23, z, M.metal);
return lamp;
}
for (let i = 0; i < 9; i++) {
lantern(-3.7 + i * 0.92, 4.1 - Math.abs(i - 4) * 0.12, 1.55, i % 2 ? M.gold : M.rose);
}
// -------------------------------------------------------------------------
// Handcart
// -------------------------------------------------------------------------
const cart = new THREE.Group();
scene.add(cart);
box(1.55, 0.22, 2.0, 0, 0.82, 0, M.woodLight, cart);
box(1.38, 0.48, 0.13, 0, 1.12, -0.92, M.wood, cart);
box(0.13, 0.48, 1.8, -0.7, 1.12, 0, M.wood, cart);
box(0.13, 0.48, 1.8, 0.7, 1.12, 0, M.wood, cart);
// Handles point along local +Z.
for (const x of [-0.52, 0.52]) {
const handle = box(0.11, 0.11, 1.85, x, 0.83, 1.62, M.wood, cart);
handle.rotation.x = -0.08;
}
const wheelMeshes = [];
for (const x of [-0.86, 0.86]) {
const wheel = new THREE.Mesh(new THREE.CylinderGeometry(0.56, 0.56, 0.18, 12), M.metal);
wheel.rotation.z = Math.PI / 2;
wheel.position.set(x, 0.55, -0.35);
wheel.castShadow = true;
cart.add(wheel);
wheelMeshes.push(wheel);
const hub = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.15, 0.21, 8), M.gold);
hub.rotation.z = Math.PI / 2;
hub.position.copy(wheel.position);
cart.add(hub);
}
const cargoGroup = new THREE.Group();
cart.add(cargoGroup);
box(0.72, 0.58, 0.7, -0.33, 1.25, -0.25, M.blue, cargoGroup);
box(0.72, 0.58, 0.7, 0.33, 1.25, -0.25, M.rose, cargoGroup);
box(0.8, 0.48, 0.65, 0, 1.2, 0.5, M.gold, cargoGroup);
box(1.55, 0.09, 1.95, 0, 0.98, 0, M.wood, cargoGroup);
const cartState = {
edge: 0,
t: 0.16,
sign: 1,
yaw: Math.PI,
deliveredA: false,
deliveredB: false,
attemptedB: false
};
let queuedTurn = 0;
const pressed = new Set();
const cartPosition = new THREE.Vector3();
const cartHeading = new THREE.Vector3();
function currentHeading(out = new THREE.Vector3()) {
const e = edges[cartState.edge];
const a = nodes[e.a];
const b = nodes[e.b];
out.set(b.x - a.x, 0, b.z - a.z).normalize().multiplyScalar(cartState.sign);
return out;
}
function updateCartTransform() {
const e = edges[cartState.edge];
edgePoint(e, cartState.t, cartPosition);
cart.position.copy(cartPosition);
currentHeading(cartHeading);
const targetYaw = Math.atan2(cartHeading.x, cartHeading.z);
let delta = targetYaw - cartState.yaw;
delta = Math.atan2(Math.sin(delta), Math.cos(delta));
cartState.yaw += delta * Math.min(1, 10 * clock.getDelta());
cart.rotation.y = cartState.yaw;
}
// -------------------------------------------------------------------------
// Pedestrians: deliberately share the same schematic surface
// -------------------------------------------------------------------------
const pedestrians = [];
const pedMaterials = [M.blue, M.rose, M.green, M.gold, M.cream];
for (let i = 0; i < 24; i++) {
const g = new THREE.Group();
const bodyMat = pedMaterials[i % pedMaterials.length];
cylinder(0.18, 0.26, 0.72, 0, 0.91, 0, bodyMat, g, 6);
sphere(0.19, 0, 1.46, 0, M.wall2, g, 7);
box(0.13, 0.63, 0.13, -0.11, 0.35, 0, M.dark, g);
box(0.13, 0.63, 0.13, 0.11, 0.35, 0, M.dark, g);
box(0.12, 0.55, 0.12, -0.31, 0.96, 0, bodyMat, g);
box(0.12, 0.55, 0.12, 0.31, 0.96, 0, bodyMat, g);
scene.add(g);
const e = edges[Math.floor(Math.random() * edges.length)];
pedestrians.push({
group: g,
edge: e,
t: Math.random(),
speed: 0.28 + Math.random() * 0.42,
direction: Math.random() > 0.5 ? 1 : -1,
offset: (Math.random() - 0.5) * 4.2,
phase: Math.random() * 10
});
}
const pedPosition = new THREE.Vector3();
const pedHeading = new THREE.Vector3();
function updatePedestrians(dt) {
pedestrians.forEach(p => {
const length = edgeLength(p.edge);
p.t += p.direction * p.speed * dt / length;
if (p.t > 1) p.t -= 1;
if (p.t < 0) p.t += 1;
edgePoint(p.edge, p.t, pedPosition);
const a = nodes[p.edge.a];
const b = nodes[p.edge.b];
pedHeading.set(b.x - a.x, 0, b.z - a.z).normalize();
const sideX = -pedHeading.z;
const sideZ = pedHeading.x;
pedPosition.x += sideX * p.offset;
pedPosition.z += sideZ * p.offset;
p.group.position.copy(pedPosition);
p.group.rotation.y = Math.atan2(pedHeading.x * p.direction, pedHeading.z * p.direction);
p.group.position.y += Math.sin(clock.elapsedTime * 5 + p.phase) * 0.025;
});
}
// -------------------------------------------------------------------------
// Guidance and graph navigation
// -------------------------------------------------------------------------
const objectiveTitle = document.querySelector("#objectiveTitle");
const objectiveHint = document.querySelector("#objectiveHint");
const stepA = document.querySelector("#stepA");
const stepB = document.querySelector("#stepB");
const turnCue = document.querySelector("#turnCue");
const toastElement = document.querySelector("#toast");
let toastTimer;
function toast(message, duration = 2800) {
toastElement.textContent = message;
toastElement.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toastElement.classList.remove("show"), duration);
}
function updateObjective() {
if (!cartState.deliveredA) {
objectiveTitle.textContent = "Reach Blue Pottery";
objectiveHint.textContent = "Continue north through the gate toward the pottery lane.";
stepA.className = "step active";
stepB.className = "step";
} else if (!cartState.deliveredB) {
objectiveTitle.textContent = "Reach Textile House";
objectiveHint.textContent =
"Reverse south. At the gate queue RIGHT for the west lane, then LEFT toward the textile lane.";
stepA.className = "step done";
stepB.className = "step active";
} else {
objectiveTitle.textContent = "Both fictional deliveries reached";
objectiveHint.textContent = "Explore the shared streets, or reset to try another route.";
stepA.className = "step done";
stepB.className = "step done";
}
}
function setTurnCue(side) {
queuedTurn = side;
turnCue.textContent = side === -1
? "Turn cue: LEFT armed"
: side === 1
? "Turn cue: RIGHT armed"
: "Turn cue: none";
}
function signedAngle(heading, outgoing) {
const cross = heading.x * outgoing.z - heading.z * outgoing.x;
const dot = heading.x * outgoing.x + heading.z * outgoing.z;
return Math.atan2(cross, dot);
}
function chooseNextEdge(nodeIndex, previousNodeIndex, heading) {
const candidates = [];
edges.forEach((e, index) => {
if (e.a === nodeIndex && e.b !== previousNodeIndex) {
const other = nodes[e.b];
candidates.push({
index,
outgoing: new THREE.Vector3(other.x - nodes[nodeIndex].x, 0, other.z - nodes[nodeIndex].z).normalize()
});
} else if (e.b === nodeIndex && e.a !== previousNodeIndex) {
const other = nodes[e.a];
candidates.push({
index,
outgoing: new THREE.Vector3(other.x - nodes[nodeIndex].x, 0, other.z - nodes[nodeIndex].z).normalize()
});
}
});
if (!candidates.length) return null;
for (const c of candidates) c.angle = signedAngle(heading, c.outgoing);
const straight = candidates.find(c => Math.abs(c.angle) < 0.2);
if (straight && queuedTurn === 0) return straight.index;
if (queuedTurn !== 0) {
const matching = candidates
.filter(c => queuedTurn < 0 ? c.angle < -0.2 : c.angle > 0.2)
.sort((a, b) => queuedTurn < 0 ? a.angle - b.angle : b.angle - a.angle);
if (matching.length) return matching[0].index;
return null;
}
// A single non-straight branch still requires an explicit left/right cue.
return null;
}
function attemptTransition() {
const e = edges[cartState.edge];
let nodeIndex;
let previousNodeIndex;
if (cartState.sign > 0 && cartState.t >= 0.9999) {
nodeIndex = e.b;
previousNodeIndex = e.a;
} else if (cartState.sign < 0 && cartState.t <= 0.0001) {
nodeIndex = e.a;
previousNodeIndex = e.b;
} else {
return true;
}
currentHeading(cartHeading);
const nextIndex = chooseNextEdge(nodeIndex, previousNodeIndex, cartHeading);
if (nextIndex === null) {
if (!window.__turnNoticeShown) {
toast("This schematic junction needs a turn cue: A / ← for left, D / → for right.");
window.__turnNoticeShown = true;
setTimeout(() => { window.__turnNoticeShown = false; }, 2200);
}
return false;
}
const next = edges[nextIndex];
if (next.a === nodeIndex) {
cartState.edge = nextIndex;
cartState.t = 0;
cartState.sign = 1;
} else {
cartState.edge = nextIndex;
cartState.t = 1;
cartState.sign = -1;
}
queuedTurn = 0;
turnCue.textContent = "Turn cue: none";
return true;
}
function checkDelivery() {
if (!cartState.deliveredA) {
const d = Math.hypot(
cartPosition.x - nodes[2].x,
cartPosition.z - nodes[2].z
);
if (d < 2.3) {
cartState.deliveredA = true;
cargoGroup.children[0].visible = false;
toast("Blue Pottery reached. The fictional blue parcel has been unloaded.");
updateObjective();
}
}
if (!cartState.deliveredB) {
const d = Math.hypot(
cartPosition.x - nodes[7].x,
cartPosition.z - nodes[7].z
);
if (d < 2.3) {
if (cartState.deliveredA) {
cartState.deliveredB = true;
cargoGroup.children[1].visible = false;
cargoGroup.children[2].visible = false;
toast("Both fictional shops reached. The textile delivery is complete.");
updateObjective();
} else if (!cartState.attemptedB) {
cartState.attemptedB = true;
toast("The textile parcel is loaded for the second stop. Reach Blue Pottery first.");
}
}
}
}
function updateCart(dt) {
let delta = 0;
const forward = pressed.has("KeyW") || pressed.has("ArrowUp");
const reverse = pressed.has("KeyS") || pressed.has("ArrowDown");
if (forward) cartState.sign = 1;
else if (reverse) cartState.sign = -1;
if (forward || reverse) {
const boost = pressed.has("ShiftLeft") || pressed.has("ShiftRight");
const speed = boost ? 10.2 : 7.1;
const length = edgeLength(edges[cartState.edge]);
delta = speed * dt * cartState.sign;
cartState.t += delta / length;
if (cartState.t >= 1) cartState.t = 1;
if (cartState.t <= 0) cartState.t = 0;
if (!attemptTransition()) {
cartState.t = Math.max(0, Math.min(1, cartState.t));
}
}
edgePoint(edges[cartState.edge], cartState.t, cartPosition);
checkDelivery();
const e = edges[cartState.edge];
const a = nodes[e.a];
const b = nodes[e.b];
cartHeading.set(b.x - a.x, 0, b.z - a.z).normalize().multiplyScalar(cartState.sign);
const targetYaw = Math.atan2(cartHeading.x, cartHeading.z);
let yawDelta = targetYaw - cartState.yaw;
yawDelta = Math.atan2(Math.sin(yawDelta), Math.cos(yawDelta));
cartState.yaw += yawDelta * Math.min(1, dt * 10);
cart.position.copy(cartPosition);
cart.rotation.y = cartState.yaw;
wheelMeshes.forEach(w => {
w.rotation.x -= delta / 0.56;
});
}
const handledKeys = new Set([
"KeyW", "KeyA", "KeyS", "KeyD",
"ArrowUp", "ArrowLeft", "ArrowDown", "ArrowRight",
"ShiftLeft", "ShiftRight"
]);
addEventListener("keydown", event => {
if (!handledKeys.has(event.code)) return;
event.preventDefault();
pressed.add(event.code);
if (!event.repeat && (event.code === "KeyA" || event.code === "ArrowLeft")) {
setTurnCue(-1);
toast("Left turn cue armed for the next junction.");
}
if (!event.repeat && (event.code === "KeyD" || event.code === "ArrowRight")) {
setTurnCue(1);
toast("Right turn cue armed for the next junction.");
}
});
addEventListener("keyup", event => {
pressed.delete(event.code);
});
addEventListener("blur", () => pressed.clear());
// Touch / on-screen controls
document.querySelectorAll("[data-key]").forEach(button => {
const key = button.dataset.key;
button.addEventListener("pointerdown", event => {
event.preventDefault();
pressed.add(key);
if (key === "ArrowLeft") {
setTurnCue(-1);
toast("Left turn cue armed for the next junction.");
}
if (key === "ArrowRight") {
setTurnCue(1);
toast("Right turn cue armed for the next junction.");
}
});
const release = event => {
event.preventDefault();
pressed.delete(key);
};
button.addEventListener("pointerup", release);
button.addEventListener("pointercancel", release);
button.addEventListener("pointerleave", release);
});
// -------------------------------------------------------------------------
// Camera controls implemented directly without add-ons
// -------------------------------------------------------------------------
let followCart = false;
let cameraYaw = 0.66;
let cameraPitch = 0.82;
let cameraRadius = 63;
let dragging = false;
let pointerX = 0;
let pointerY = 0;
const cameraTarget = new THREE.Vector3(0, 0, -2);
const desiredTarget = new THREE.Vector3(0, 0, -2);
function positionCamera() {
const horizontal = cameraRadius * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraRadius,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
cameraYaw -= (event.clientX - pointerX) * 0.006;
cameraPitch = Math.max(0.32, Math.min(1.28, cameraPitch + (event.clientY - pointerY) * 0.004));
pointerX = event.clientX;
pointerY = event.clientY;
});
canvas.addEventListener("pointerup", event => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) canvas.releasePointerCapture(event.pointerId);
});
canvas.addEventListener("pointercancel", () => { dragging = false; });
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = Math.max(18, Math.min(90, cameraRadius + event.deltaY * 0.035));
}, { passive: false });
const followButton = document.querySelector("#followButton");
function setFollow(value) {
followCart = value;
cameraRadius = value ? 29 : 63;
followButton.textContent = value ? "District view" : "Follow cart";
followButton.classList.toggle("primary", !value);
}
followButton.addEventListener("click", () => setFollow(!followCart));
document.querySelector("#resetButton").addEventListener("click", () => {
cartState.edge = 0;
cartState.t = 0.16;
cartState.sign = 1;
cartState.yaw = Math.PI;
cartState.deliveredA = false;
cartState.deliveredB = false;
cartState.attemptedB = false;
queuedTurn = 0;
pressed.clear();
cargoGroup.children[0].visible = true;
cargoGroup.children[1].visible = true;
cargoGroup.children[2].visible = true;
setTurnCue(0);
updateObjective();
toast("Handcart reset at the gate.");
});
// -------------------------------------------------------------------------
// Minimap
// -------------------------------------------------------------------------
const mapCanvas = document.querySelector("#minimap");
const mapCtx = mapCanvas.getContext("2d");
function drawMinimap() {
const w = mapCanvas.width;
const h = mapCanvas.height;
mapCtx.clearRect(0, 0, w, h);
const bounds = { minX: -25, maxX: 25, minZ: -27, maxZ: 27 };
const pad = 24;
const sx = (w - pad * 2) / (bounds.maxX - bounds.minX);
const sz = (h - pad * 2) / (bounds.maxZ - bounds.minZ);
const scale = Math.min(sx, sz);
const ox = w / 2;
const oz = h / 2;
const mapX = x => ox + x * scale;
const mapZ = z => oz + z * scale;
mapCtx.fillStyle = "#eadfca";
mapCtx.fillRect(0, 0, w, h);
// Gate symbol
mapCtx.strokeStyle = "#8c765d";
mapCtx.lineWidth = 5;
mapCtx.beginPath();
mapCtx.arc(mapX(0), mapZ(0), 8, Math.PI, 0);
mapCtx.stroke();
mapCtx.lineCap = "round";
mapCtx.lineJoin = "round";
edges.forEach(e => {
const a = nodes[e.a];
const b = nodes[e.b];
mapCtx.strokeStyle = "#c7ad7d";
mapCtx.lineWidth = 16;
mapCtx.beginPath();
mapCtx.moveTo(mapX(a.x), mapZ(a.z));
mapCtx.lineTo(mapX(b.x), mapZ(b.z));
mapCtx.stroke();
mapCtx.strokeStyle = "rgba(255,248,225,.55)";
mapCtx.lineWidth = 2;
mapCtx.setLineDash([7, 8]);
mapCtx.stroke();
mapCtx.setLineDash([]);
});
function shop(x, z, color) {
mapCtx.fillStyle = color;
mapCtx.beginPath();
mapCtx.arc(mapX(x), mapZ(z), 9, 0, Math.PI * 2);
mapCtx.fill();
mapCtx.strokeStyle = "#fff8e8";
mapCtx.lineWidth = 3;
mapCtx.stroke();
}
shop(nodes[2].x, nodes[2].z, cartState.deliveredA ? "#56845f" : "#257a91");
shop(nodes[7].x, nodes[7].z, cartState.deliveredB ? "#56845f" : "#a8564e");
// Cart arrow
const px = mapX(cartPosition.x);
const pz = mapZ(cartPosition.z);
const angle = Math.atan2(cartHeading.x, cartHeading.z);
mapCtx.save();
mapCtx.translate(px, pz);
mapCtx.rotate(-angle);
mapCtx.fillStyle = "#a8564e";
mapCtx.strokeStyle = "#fff8e8";
mapCtx.lineWidth = 3;
mapCtx.beginPath();
mapCtx.moveTo(0, -11);
mapCtx.lineTo(8, 9);
mapCtx.lineTo(0, 5);
mapCtx.lineTo(-8, 9);
mapCtx.closePath();
mapCtx.fill();
mapCtx.stroke();
mapCtx.restore();
mapCtx.fillStyle = "rgba(73,58,43,.52)";
mapCtx.font = "600 16px system-ui";
mapCtx.textAlign = "center";
mapCtx.fillText("N", w / 2, 18);
}
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.04);
updateCart(dt);
updatePedestrians(dt);
if (followCart) {
desiredTarget.set(cartPosition.x, 1.1, cartPosition.z);
} else {
desiredTarget.set(0, 0, -2);
}
cameraTarget.lerp(desiredTarget, 1 - Math.exp(-dt * 3.5));
positionCamera();
const pulse = 1 + Math.sin(clock.elapsedTime * 2.7) * 0.08;
potteryRing.scale.setScalar(pulse);
textileRing.scale.setScalar(1 + Math.sin(clock.elapsedTime * 2.7 + 1.4) * 0.08);
drawMinimap();
renderer.render(scene, camera);
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
});
updateObjective();
positionCamera();
animate();
</script>
</body>
</html>
```