Prompt specification
Build or customize the Three.js 3D scene "Central Asian caravanserai exchange corner" 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: Central Asian caravanserai exchange corner
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/central-asian-caravanserai-exchange-corner-x2618
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring caravan stages and exchange along the Silk Roads. Create a referenced caravanserai courtyard with unloading bay, water point, and sheltered storage. Show goods changing carriers between two short regional journey stages. A 3D cutaway view.
Working Three.js Starter (central-asian-caravanserai-exchange-corner-x2618):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Caravanserai Exchange — A Silk Roads Cutaway</title>
<style>
:root {
--ink: #2b211b;
--muted: #725f4e;
--paper: rgba(248, 238, 216, 0.94);
--line: rgba(75, 52, 33, 0.2);
--teal: #2c7773;
--gold: #c68c38;
--rust: #a85336;
--shadow: 0 12px 40px rgba(61, 39, 23, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dfc9a5;
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%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
.masthead {
position: fixed;
top: 26px;
left: 28px;
z-index: 10;
width: min(460px, calc(100vw - 56px));
pointer-events: none;
text-shadow: 0 1px 12px rgba(255, 246, 225, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 2px;
content: "";
background: var(--teal);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 4.4vw, 62px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 410px;
margin: 14px 0 0;
color: #604d3c;
font-size: 13px;
line-height: 1.6;
}
.route-card {
position: fixed;
top: 26px;
right: 26px;
z-index: 12;
width: 304px;
padding: 18px 18px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
.route-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.route-heading strong {
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#phase-label {
padding: 5px 8px;
border-radius: 999px;
background: #e8dbc1;
color: #69452f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.route-step {
position: relative;
display: grid;
grid-template-columns: 30px 1fr auto;
gap: 10px;
align-items: center;
min-height: 57px;
}
.route-step:not(:last-child)::after {
position: absolute;
top: 39px;
bottom: -16px;
left: 14px;
width: 1px;
content: "";
background: #cdbb9d;
}
.step-number {
display: grid;
width: 29px;
height: 29px;
place-items: center;
border: 1px solid #bca98c;
border-radius: 50%;
background: #f7eedc;
color: #715f4b;
font-family: Georgia, serif;
font-size: 13px;
transition: 0.3s ease;
}
.route-step.active .step-number {
border-color: var(--teal);
background: var(--teal);
color: white;
box-shadow: 0 0 0 4px rgba(44, 119, 115, 0.12);
}
.route-step.done .step-number {
border-color: var(--gold);
background: var(--gold);
color: white;
}
.step-title {
font-size: 12px;
font-weight: 750;
}
.step-detail {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.step-carrier {
color: #8b755d;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.exchange-status {
margin-top: 12px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.status-row {
display: flex;
justify-content: space-between;
gap: 10px;
color: var(--muted);
font-size: 10px;
}
#status-value {
color: var(--ink);
font-weight: 700;
text-align: right;
}
.cargo-list {
display: flex;
gap: 7px;
margin-top: 12px;
}
.cargo-chip {
flex: 1;
padding: 7px 6px;
border: 1px solid #d6c7ad;
border-radius: 8px;
background: rgba(255, 255, 255, 0.3);
color: #806d59;
font-size: 9px;
font-weight: 700;
text-align: center;
transition: 0.25s ease;
}
.cargo-chip.active {
border-color: var(--teal);
background: rgba(44, 119, 115, 0.12);
color: var(--teal);
}
.cargo-chip.done {
border-color: #d3a454;
background: rgba(198, 140, 56, 0.16);
color: #80591f;
}
.progress-track {
height: 3px;
margin-top: 13px;
overflow: hidden;
border-radius: 4px;
background: #dfd0b7;
}
#progress {
width: 0%;
height: 100%;
background: linear-gradient(90deg, var(--teal), var(--gold));
transition: width 0.15s linear;
}
.controls {
position: fixed;
right: 26px;
bottom: 25px;
z-index: 12;
display: flex;
gap: 8px;
}
.control-button {
display: inline-flex;
min-height: 38px;
align-items: center;
gap: 7px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 10px;
background: var(--paper);
color: var(--ink);
box-shadow: 0 5px 18px rgba(61, 39, 23, 0.12);
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.04em;
transition: transform 0.2s ease, background 0.2s ease;
}
.control-button:hover {
transform: translateY(-2px);
background: #fff6e4;
}
.control-button svg {
width: 13px;
height: 13px;
fill: currentColor;
}
.legend {
position: fixed;
bottom: 26px;
left: 28px;
z-index: 10;
display: flex;
flex-wrap: wrap;
gap: 15px;
max-width: calc(100vw - 380px);
padding: 10px 13px;
border: 1px solid rgba(91, 62, 37, 0.16);
border-radius: 10px;
background: rgba(248, 238, 216, 0.76);
color: #624e3c;
backdrop-filter: blur(10px);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
}
.hint {
position: fixed;
bottom: 29px;
left: 50%;
z-index: 9;
transform: translateX(-50%);
color: rgba(68, 47, 31, 0.68);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.06em;
pointer-events: none;
white-space: nowrap;
}
.hotspot {
position: fixed;
top: 0;
left: 0;
z-index: 8;
display: flex;
align-items: center;
gap: 8px;
pointer-events: none;
transform: translate(-50%, -50%);
will-change: transform;
}
.hotspot .pin {
position: relative;
width: 11px;
height: 11px;
border: 2px solid #fff7e6;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 2px 8px rgba(50, 35, 22, 0.4);
}
.hotspot .pin::after {
position: absolute;
top: 12px;
left: 3px;
width: 1px;
height: 22px;
content: "";
background: linear-gradient(rgba(61, 42, 27, 0.55), transparent);
}
.hotspot .label {
padding: 7px 9px;
border: 1px solid rgba(83, 57, 35, 0.17);
border-radius: 7px;
background: rgba(250, 241, 222, 0.88);
color: #49372a;
box-shadow: 0 4px 14px rgba(67, 42, 24, 0.12);
backdrop-filter: blur(8px);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.hotspot.rust .pin {
background: var(--rust);
}
.hotspot.gold .pin {
background: var(--gold);
}
.stage-marker {
position: fixed;
top: 0;
left: 0;
z-index: 7;
padding: 6px 9px;
border-radius: 6px;
background: rgba(43, 33, 27, 0.78);
color: #f6e9cf;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
}
.loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #e3cfae;
color: #5b4330;
font-family: Georgia, serif;
font-size: 22px;
transition: opacity 0.6s ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 850px) {
.masthead {
top: 18px;
left: 18px;
}
.subtitle {
display: none;
}
.route-card {
top: auto;
right: 14px;
bottom: 14px;
width: 255px;
padding: 13px;
}
.route-heading {
margin-bottom: 8px;
}
.route-step {
min-height: 45px;
}
.route-step:not(:last-child)::after {
top: 32px;
bottom: -11px;
}
.step-detail,
.cargo-list,
.hint,
.legend {
display: none;
}
.exchange-status {
margin-top: 7px;
padding-top: 7px;
}
.controls {
top: 19px;
right: 14px;
bottom: auto;
}
.control-button span {
display: none;
}
.hotspot .label {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive 3D cutaway of a Silk Roads caravanserai exchange"></div>
<header class="masthead">
<div class="eyebrow">Silk Roads · Exchange Study 04</div>
<h1>Caravanserai<br />Exchange</h1>
<p class="subtitle">
A cutaway courtyard on the regional relay network. A long-haul pack camel
reaches the caravanserai; its cargo changes to a local cart for the next stage.
</p>
</header>
<aside class="route-card" aria-label="Journey stages and exchange status">
<div class="route-heading">
<strong>Two-stage regional relay</strong>
<span id="phase-label">Stage 01 arrives</span>
</div>
<div class="route-step active" id="step-1">
<div class="step-number">1</div>
<div>
<div class="step-title">Oasis road</div>
<div class="step-detail">Desert waystation → caravanserai</div>
</div>
<div class="step-carrier">Pack camel</div>
</div>
<div class="route-step" id="step-2">
<div class="step-number">2</div>
<div>
<div class="step-title">Waystation road</div>
<div class="step-detail">Caravanserai → eastern relay</div>
</div>
<div class="step-carrier">Local cart</div>
</div>
<div class="exchange-status">
<div class="status-row">
<span>Current movement</span>
<span id="status-value">Incoming caravan approaching</span>
</div>
<div class="cargo-list">
<div class="cargo-chip" id="cargo-0">Silk bales</div>
<div class="cargo-chip" id="cargo-1">Spice sacks</div>
<div class="cargo-chip" id="cargo-2">Lacquer casket</div>
</div>
<div class="progress-track"><div id="progress"></div></div>
</div>
</aside>
<div class="legend" aria-label="Scene legend">
<span><i class="dot" style="background:#2c7773"></i>Exchange route</span>
<span><i class="dot" style="background:#a85336"></i>Unloading bay</span>
<span><i class="dot" style="background:#c68c38"></i>Sheltered stores</span>
<span><i class="dot" style="background:#568c9d"></i>Water point</span>
</div>
<div class="hint">DRAG TO ORBIT · WHEEL TO ZOOM · DOUBLE-CLICK TO RESET · SPACE TO PAUSE</div>
<div class="controls">
<button class="control-button" id="pause-button" type="button" aria-label="Pause exchange animation">
<svg viewBox="0 0 24 24"><path d="M6 4h4v16H6zm8 0h4v16h-4z"/></svg>
<span id="pause-text">Pause</span>
</button>
<button class="control-button" id="reset-view" type="button" aria-label="Reset camera view">
<svg viewBox="0 0 24 24"><path d="M12 5a7 7 0 1 1-6.7 9H2l3.5-4L9 14H6.4A5 5 0 1 0 12 7V5z"/></svg>
<span>Reset view</span>
</button>
</div>
<div class="hotspot" data-position="10.6,3.6,2.7">
<i class="pin"></i><span class="label">Unloading bay</span>
</div>
<div class="hotspot" data-position="-9.2,2.4,5.2">
<i class="pin"></i><span class="label">Water point</span>
</div>
<div class="hotspot gold" data-position="-7.2,3.9,-6.6">
<i class="pin"></i><span class="label">Sheltered storage</span>
</div>
<div class="hotspot rust" data-position="-1.2,2.5,0.7">
<i class="pin"></i><span class="label">Carrier exchange</span>
</div>
<div class="stage-marker" id="stage-marker-1">01 · OASIS ROAD</div>
<div class="stage-marker" id="stage-marker-2">02 · WAYSTATION ROAD</div>
<div class="loading" id="loading">Preparing the caravan court…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* ---------------------------------------------------------
Renderer and scene
--------------------------------------------------------- */
const container = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe5d3b2);
scene.fog = new THREE.Fog(0xe5d3b2, 52, 105);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
150
);
const hemi = new THREE.HemisphereLight(0xfff0d0, 0x856344, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe0a5, 3.1);
sun.position.set(-24, 35, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 38;
sun.shadow.camera.bottom = -38;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 90;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa8c7ca, 0.65);
fill.position.set(28, 14, -24);
scene.add(fill);
/* ---------------------------------------------------------
Materials and helpers
--------------------------------------------------------- */
const material = (color, roughness = 0.85, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const M = {
sand: material(0xcaa875),
sandLight: material(0xe0c18d),
sandDark: material(0xa88457),
slab: material(0xd9bd8c),
adobe: material(0xc9955f),
adobeLight: material(0xe0b47a),
adobeDark: material(0xa97146),
cutEdge: material(0xf0d39d),
wood: material(0x775033),
woodLight: material(0xa87843),
woodDark: material(0x493326),
teal: material(0x2c7773),
tealDark: material(0x1e5654),
red: material(0xa34e35),
gold: material(0xc98e36),
cream: material(0xf0dfb8),
charcoal: material(0x33291f),
water: new THREE.MeshStandardMaterial({
color: 0x4e91a3,
roughness: 0.2,
metalness: 0.1,
transparent: true,
opacity: 0.88
}),
leather: material(0x7b4b2e),
horse: material(0x805a3b),
skin: material(0xb98256),
white: material(0xe7ddc6)
};
function addBox(parent, w, h, d, mat, x = 0, y = 0, z = 0) {
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 addCylinder(
parent,
topRadius,
bottomRadius,
height,
mat,
x = 0,
y = 0,
z = 0,
segments = 12
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(topRadius, bottomRadius, height, segments),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addSphere(parent, radius, mat, x = 0, y = 0, z = 0) {
const mesh = new THREE.Mesh(new THREE.SphereGeometry(radius, 16, 10), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function setShadow(group) {
group.traverse((child) => {
if (child.isMesh) {
child.castShadow = true;
child.receiveShadow = true;
}
});
return group;
}
let randomSeed = 81721;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const clamp = THREE.MathUtils.clamp;
const lerp = THREE.MathUtils.lerp;
function smoothstep(value) {
const x = clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
/* ---------------------------------------------------------
Terrain, courtyard slab, and distant landscape
--------------------------------------------------------- */
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(100, 100),
M.sand
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.18;
ground.receiveShadow = true;
scene.add(ground);
const court = addBox(scene, 30, 0.34, 22, M.slab, 0, 0, 0);
// Thin courtyard border and exposed cutaway edge.
addBox(scene, 30.2, 0.12, 0.18, M.cream, 0, 0.22, 10.9);
addBox(scene, 0.18, 0.12, 21.8, M.cream, 14.9, 0.22, 0);
addBox(scene, 30.2, 0.12, 0.18, M.cream, 0, 0.22, -10.9);
addBox(scene, 0.18, 0.12, 21.8, M.cream, -14.9, 0.22, 0);
// Subtle paving strips.
for (let x = -12; x <= 12; x += 4) {
addBox(scene, 0.08, 0.025, 20.6, M.sandDark, x, 0.19, 0);
}
for (let z = -8; z <= 8; z += 4) {
addBox(scene, 28.8, 0.025, 0.08, M.sandDark, 0, 0.19, z);
}
// Desert patches.
const patchMaterial = material(0xb88e60);
for (let i = 0; i < 18; i++) {
const patch = new THREE.Mesh(
new THREE.CircleGeometry(2 + random() * 5, 24),
patchMaterial
);
patch.rotation.x = -Math.PI / 2;
patch.scale.y = 0.3 + random() * 0.6;
patch.position.set(
(random() - 0.5) * 85,
-0.165,
(random() - 0.5) * 75
);
patch.receiveShadow = true;
scene.add(patch);
}
// Low-poly mountains.
for (let i = 0; i < 13; i++) {
const height = 5 + random() * 11;
const mountain = new THREE.Mesh(
new THREE.ConeGeometry(5 + random() * 6, height, 5),
material(i % 2 ? 0xb48b63 : 0xc0966d)
);
mountain.position.set(
-48 + i * 8 + random() * 4,
height / 2 - 0.4,
-35 - random() * 15
);
mountain.rotation.y = random() * Math.PI;
mountain.receiveShadow = true;
scene.add(mountain);
}
// Scattered stones.
const rockGeometry = new THREE.IcosahedronGeometry(0.24, 0);
for (let i = 0; i < 110; i++) {
const rock = new THREE.Mesh(
rockGeometry,
i % 3 === 0 ? M.adobeDark : M.sandDark
);
const angle = random() * Math.PI * 2;
const radius = 18 + random() * 24;
rock.position.set(
Math.cos(angle) * radius,
0.02,
Math.sin(angle) * radius
);
rock.scale.set(
0.6 + random() * 1.4,
0.4 + random() * 0.7,
0.6 + random() * 1.4
);
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
rock.receiveShadow = true;
scene.add(rock);
}
/* ---------------------------------------------------------
Caravanserai architecture
--------------------------------------------------------- */
const architecture = new THREE.Group();
scene.add(architecture);
// Northern wall.
addBox(architecture, 30.2, 3.15, 0.85, M.adobe, 0, 1.58, -10.7);
addBox(architecture, 30.25, 0.2, 0.9, M.adobeLight, 0, 3.12, -10.7);
// Western wall, split around a central gate.
addBox(architecture, 0.85, 3.15, 8.8, M.adobe, -14.7, 1.58, -6.1);
addBox(architecture, 0.85, 3.15, 8.8, M.adobe, -14.7, 1.58, 6.1);
// Eastern and southern walls are deliberately cut down for the cutaway.
addBox(architecture, 0.72, 0.72, 21.4, M.adobeDark, 14.7, 0.37, 0);
addBox(architecture, 0.78, 0.12, 21.5, M.cutEdge, 14.7, 0.78, 0);
addBox(architecture, 29.5, 0.38, 0.58, M.adobeDark, 0, 0.2, 10.75);
addBox(architecture, 29.6, 0.1, 0.62, M.cutEdge, 0, 0.45, 10.75);
function createArch(x, z, rotation = 0, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
group.scale.setScalar(scale);
architecture.add(group);
// Recessed dark opening.
addBox(group, 2.4, 2.4, 0.12, M.charcoal, 0, 1.2, -0.34);
addBox(group, 0.36, 2.45, 0.55, M.adobeLight, -1.24, 1.23, 0);
addBox(group, 0.36, 2.45, 0.55, M.adobeLight, 1.24, 1.23, 0);
const arch = new THREE.Mesh(
new THREE.TorusGeometry(1.25, 0.18, 7, 28, Math.PI),
M.adobeLight
);
arch.position.set(0, 2.45, 0);
arch.castShadow = true;
arch.receiveShadow = true;
group.add(arch);
// Decorative keystone.
const key = addBox(group, 0.28, 0.48, 0.58, M.cream, 0, 3.65, 0);
key.rotation.z = 0.03;
return group;
}
[-10.2, -5.1, 0, 5.1, 10.2].forEach((x) => createArch(x, -10.18));
// West-side arcade, leaving the central gate open.
[-8, -4, 4, 8].forEach((z) =>
createArch(-14.18, z, Math.PI / 2)
);
createArch(-14.18, 0, Math.PI / 2, 1.14);
// Corner towers.
function tower(x, z, height, cut = false) {
addBox(architecture, 1.9, height, 1.9, M.adobeDark, x, height / 2, z);
addBox(architecture, 2.05, 0.17, 2.05, M.cutEdge, x, height + 0.08, z);
if (!cut) {
for (let i = 0; i < 4; i++) {
const a = (i * Math.PI) / 2;
addBox(
architecture,
0.45,
0.55,
0.45,
M.adobeLight,
x + Math.sin(a) * 0.67,
height + 0.42,
z + Math.cos(a) * 0.67
);
}
}
}
tower(-14.7, -10.7, 4.1);
tower(-14.7, 10.7, 3.3, true);
tower(14.7, -10.7, 4.1);
tower(14.7, 10.7, 0.85, true);
// Rear roof ledge.
addBox(architecture, 30.6, 0.25, 1.35, M.woodDark, 0, 3.38, -10.4);
addBox(architecture, 30.4, 0.16, 0.18, M.woodLight, 0, 3.53, -9.78);
// Hanging textiles.
const bannerMaterials = [M.teal, M.red, M.gold];
[-11.5, -7.8, 3.7, 7.5, 11.5].forEach((x, i) => {
const banner = new THREE.Mesh(
new THREE.PlaneGeometry(0.75, 1.55),
bannerMaterials[i % bannerMaterials.length]
);
banner.position.set(x, 2.05, -9.72);
banner.userData.baseRotation = banner.rotation.z;
banner.userData.phase = i * 0.8;
architecture.add(banner);
});
/* ---------------------------------------------------------
Sheltered storage
--------------------------------------------------------- */
const storage = new THREE.Group();
storage.position.set(-7.2, 0, -6.7);
scene.add(storage);
addBox(storage, 6.2, 0.18, 4.5, M.sandDark, 0, 0.25, 0);
[-2.75, 2.75].forEach((x) => {
[-1.8, 1.8].forEach((z) => {
addBox(storage, 0.18, 2.45, 0.18, M.wood, x, 1.43, z);
});
});
addBox(storage, 6.2, 0.16, 0.18, M.woodDark, 0, 2.62, -1.8);
addBox(storage, 6.2, 0.16, 0.18, M.woodDark, 0, 2.62, 1.8);
const roofLeft = addBox(storage, 3.45, 0.14, 4.8, M.tealDark, -1.45, 2.88, 0);
roofLeft.rotation.z = -0.16;
const roofRight = addBox(storage, 3.45, 0.14, 4.8, M.teal, 1.45, 2.88, 0);
roofRight.rotation.z = 0.16;
addBox(storage, 0.16, 0.18, 4.75, M.woodLight, 0, 3.15, 0);
function createCrate(parent, x, y, z, size = 0.9, mat = M.woodLight) {
const group = new THREE.Group();
group.position.set(x, y, z);
parent.add(group);
addBox(group, size, size * 0.78, size * 0.86, mat);
addBox(group, size * 1.04, size * 0.08, size * 0.9, M.woodDark, 0, size * 0.24, 0);
addBox(group, size * 1.04, size * 0.08, size * 0.9, M.woodDark, 0, -size * 0.24, 0);
addBox(group, size * 0.1, size * 0.82, size * 0.9, M.woodDark, 0, 0, 0);
return group;
}
createCrate(storage, -1.75, 0.72, -0.85, 0.9);
createCrate(storage, -0.75, 0.7, -0.85, 0.82);
createCrate(storage, 1.45, 0.73, -0.95, 1.0, M.wood);
createCrate(storage, 2.15, 0.58, 0.8, 0.65, M.red);
// Rolled carpets.
for (let i = 0; i < 3; i++) {
const roll = addCylinder(
storage,
0.22,
0.22,
2.15,
i % 2 ? M.red : M.gold,
0.1 + i * 0.18,
0.67 + i * 0.37,
0.92,
14
);
roll.rotation.z = Math.PI / 2;
}
// Storage sacks.
[
[2.2, 0.57, -0.3, M.cream],
[1.55, 0.55, -0.4, M.sandLight],
[-2.25, 0.58, 1.1, M.red]
].forEach(([x, y, z, mat]) => {
const sack = addSphere(storage, 0.48, mat, x, y, z);
sack.scale.set(0.85, 1.15, 0.85);
addCylinder(storage, 0.11, 0.16, 0.18, M.woodDark, x, y + 0.48, z, 8);
});
/* ---------------------------------------------------------
Water point
--------------------------------------------------------- */
const waterPoint = new THREE.Group();
waterPoint.position.set(-9.3, 0, 5.2);
scene.add(waterPoint);
addCylinder(waterPoint, 1.05, 1.12, 0.75, M.adobeLight, 0, 0.48, 0, 8);
addCylinder(waterPoint, 0.73, 0.73, 0.12, M.charcoal, 0, 0.83, 0, 16);
const wellWater = new THREE.Mesh(
new THREE.CircleGeometry(0.68, 24),
M.water
);
wellWater.rotation.x = -Math.PI / 2;
wellWater.position.y = 0.88;
waterPoint.add(wellWater);
// Winch posts and crossbar.
[-0.85, 0.85].forEach((x) => {
addBox(waterPoint, 0.13, 2.1, 0.13, M.wood, x, 1.5, 0);
});
addBox(waterPoint, 2.0, 0.14, 0.14, M.woodDark, 0, 2.52, 0);
addCylinder(waterPoint, 0.11, 0.11, 1.4, M.woodLight, 0, 2.28, 0, 10);
addCylinder(waterPoint, 0.035, 0.035, 1.0, M.charcoal, 0, 1.65, 0, 8);
// Trough.
addBox(waterPoint, 2.1, 0.48, 0.95, M.wood, 1.9, 0.42, 0.45);
addBox(waterPoint, 1.86, 0.12, 0.72, M.water, 1.9, 0.64, 0.45);
addBox(waterPoint, 2.22, 0.12, 1.04, M.woodLight, 1.9, 0.7, 0.45);
// Ceramic jars.
function createJar(parent, x, y, z, scale = 1, mat = M.red) {
const jar = new THREE.Group();
jar.position.set(x, y, z);
jar.scale.setScalar(scale);
parent.add(jar);
const body = addSphere(jar, 0.48, mat, 0, 0.42, 0);
body.scale.set(0.82, 1.05, 0.82);
addCylinder(jar, 0.2, 0.28, 0.28, mat, 0, 0.84, 0, 12);
addCylinder(jar, 0.25, 0.22, 0.07, M.woodDark, 0, 1.0, 0, 12);
return jar;
}
createJar(waterPoint, -1.8, 0.15, 1.1, 0.95, M.tealDark);
createJar(waterPoint, -1.4, 0.15, 1.75, 0.72, M.cream);
/* ---------------------------------------------------------
Unloading bay
--------------------------------------------------------- */
const unloadingBay = new THREE.Group();
unloadingBay.position.set(10.5, 0, 2.7);
scene.add(unloadingBay);
addBox(unloadingBay, 5.1, 0.08, 4.7, M.sandDark, 0, 0.24, 0);
// Painted bay border.
addBox(unloadingBay, 5.0, 0.03, 0.1, M.red, 0, 0.3, -2.27);
addBox(unloadingBay, 5.0, 0.03, 0.1, M.red, 0, 0.3, 2.27);
addBox(unloadingBay, 0.1, 0.03, 4.5, M.red, -2.45, 0.3, 0);
addBox(unloadingBay, 0.1, 0.03, 4.5, M.red, 2.45, 0.3, 0);
[-2.15, 2.15].forEach((x) => {
[-1.95, 1.95].forEach((z) => {
addBox(unloadingBay, 0.15, 2.35, 0.15, M.wood, x, 1.42, z);
});
});
const canopy = addBox(unloadingBay, 4.65, 0.12, 4.45, M.cream, 0, 2.84, 0);
canopy.rotation.z = -0.07;
for (let i = 0; i < 5; i++) {
addBox(unloadingBay, 0.82, 0.14, 4.48, i % 2 ? M.red : M.cream, -1.86 + i * 0.93, 2.88, 0);
}
addBox(unloadingBay, 4.8, 0.12, 0.15, M.woodDark, 0, 2.7, 2.15);
// Stacked supplies in the bay.
createCrate(unloadingBay, 1.65, 0.76, -1.2, 0.92);
createCrate(unloadingBay, 1.65, 1.47, -1.2, 0.72, M.red);
createCrate(unloadingBay, 0.55, 0.73, -1.25, 0.8, M.teal);
const sandbag = addSphere(unloadingBay, 0.52, M.sandLight, -1.3, 0.57, 1.25);
sandbag.scale.set(1.3, 0.6, 0.75);
const sandbag2 = addSphere(unloadingBay, 0.5, M.sandLight, -0.65, 0.56, 1.35);
sandbag2.scale.set(1.35, 0.58, 0.75);
/* ---------------------------------------------------------
Route paving and arrows
--------------------------------------------------------- */
function createGroundArrow(x, z, color = M.teal) {
const shape = new THREE.Shape();
shape.moveTo(-0.5, -0.2);
shape.lineTo(0.12, -0.2);
shape.lineTo(0.12, -0.48);
shape.lineTo(0.72, 0);
shape.lineTo(0.12, 0.48);
shape.lineTo(0.12, 0.2);
shape.lineTo(-0.5, 0.2);
shape.closePath();
const arrow = new THREE.Mesh(
new THREE.ShapeGeometry(shape),
color
);
arrow.rotation.x = -Math.PI / 2;
arrow.position.set(x, 0.22, z);
scene.add(arrow);
}
for (let x = -28; x < -14; x += 2.5) {
createGroundArrow(x, 0, M.teal);
}
for (let x = 15; x < 28; x += 2.5) {
createGroundArrow(x, 0, M.gold);
}
// Route stones.
for (let x = -29; x < 29; x += 1.2) {
if (x > -14 && x < 14) continue;
const marker = addBox(
scene,
0.65,
0.05,
0.65,
x < 0 ? M.sandLight : M.adobeLight,
x,
0.16,
-0.62
);
marker.rotation.y = random() * 0.25;
}
/* ---------------------------------------------------------
Bactrian camels
--------------------------------------------------------- */
function createBactrianCamel() {
const group = new THREE.Group();
const legs = [];
const body = addSphere(group, 1, M.woodDark, 0, 1.7, 0);
body.scale.set(1.28, 0.64, 0.58);
const humpA = addSphere(group, 0.56, M.woodDark, -0.42, 2.33, 0);
humpA.scale.set(0.85, 1, 0.85);
const humpB = addSphere(group, 0.55, M.woodDark, 0.38, 2.3, 0);
humpB.scale.set(0.85, 1, 0.85);
const neck = addCylinder(group, 0.25, 0.31, 1.38, M.woodDark, 1.0, 2.22, 0, 10);
neck.rotation.z = -0.37;
const head = addSphere(group, 0.38, M.woodDark, 1.37, 2.75, 0);
head.scale.set(1.25, 0.72, 0.72);
const snout = addSphere(group, 0.27, M.leather, 1.72, 2.57, 0);
snout.scale.set(1.15, 0.62, 0.65);
addSphere(group, 0.075, M.charcoal, 1.53, 2.83, 0.24);
addSphere(group, 0.075, M.charcoal, 1.53, 2.83, -0.24);
const earA = addSphere(group, 0.13, M.woodDark, 1.2, 3.0, 0.16);
earA.scale.set(0.55, 1.35, 0.55);
const earB = addSphere(group, 0.13, M.woodDark, 1.2, 3.0, -0.16);
earB.scale.set(0.55, 1.35, 0.55);
[-0.72, 0.72].forEach((x) => {
[-0.34, 0.34].forEach((z) => {
const pivot = new THREE.Group();
pivot.position.set(x, 1.28, z);
group.add(pivot);
addCylinder(pivot, 0.1, 0.13, 1.15, M.woodDark, 0, -0.57, 0, 8);
addBox(pivot, 0.27, 0.16, 0.26, M.charcoal, 0, -1.15, 0);
legs.push(pivot);
});
});
const tail = addCylinder(group, 0.055, 0.075, 0.72, M.leather, -1.18, 1.68, 0, 7);
tail.rotation.z = 0.4;
// Pack blanket and side panniers.
addBox(group, 1.7, 0.1, 1.18, M.teal, -0.08, 2.02, 0);
addBox(group, 0.66, 0.62, 0.24, M.red, -0.4, 2.2, 0.58);
addBox(group, 0.66, 0.62, 0.24, M.red, -0.4, 2.2, -0.58);
addBox(group, 0.1, 0.65, 1.2, M.gold, 0.35, 2.22, 0);
// Rope harness.
const rope = new THREE.Mesh(
new THREE.TorusGeometry(0.58, 0.035, 5, 22),
M.cream
);
rope.position.set(1.05, 2.04, 0);
rope.rotation.y = Math.PI / 2;
group.add(rope);
group.userData.legs = legs;
return setShadow(group);
}
const incomingCaravan = new THREE.Group();
scene.add(incomingCaravan);
const leadCamel = createBactrianCamel();
incomingCaravan.add(leadCamel);
const secondCamel = createBactrianCamel();
secondCamel.position.set(-4.8, 0, -0.75);
secondCamel.rotation.y = -0.06;
incomingCaravan.add(secondCamel);
/* ---------------------------------------------------------
Regional cart and horse
--------------------------------------------------------- */
function createHorse() {
const group = new THREE.Group();
const legs = [];
const body = addSphere(group, 0.6, M.horse, 0, 1.25, 0);
body.scale.set(1.25, 0.72, 0.58);
const neck = addCylinder(group, 0.24, 0.33, 1.15, M.horse, 0.72, 1.73, 0, 10);
neck.rotation.z = -0.28;
const head = addSphere(group, 0.37, M.horse, 1.08, 2.18, 0);
head.scale.set(1.3, 0.7, 0.7);
const muzzle = addSphere(group, 0.25, M.leather, 1.48, 2.02, 0);
muzzle.scale.set(1.1, 0.65, 0.72);
addSphere(group, 0.065, M.charcoal, 1.25, 2.26, 0.21);
addSphere(group, 0.065, M.charcoal, 1.25, 2.26, -0.21);
const earA = addSphere(group, 0.12, M.horse, 0.84, 2.52, 0.13);
earA.scale.set(0.55, 1.2, 0.55);
const earB = addSphere(group, 0.12, M.horse, 0.84, 2.52, -0.13);
earB.scale.set(0.55, 1.2, 0.55);
[-0.48, 0.5].forEach((x) => {
[-0.25, 0.25].forEach((z) => {
const pivot = new THREE.Group();
pivot.position.set(x, 0.98, z);
group.add(pivot);
addCylinder(pivot, 0.085, 0.105, 0.88, M.horse, 0, -0.44, 0, 8);
addBox(pivot, 0.22, 0.12, 0.22, M.charcoal, 0, -0.89, 0);
legs.push(pivot);
});
});
// Mane and tail.
addBox(group, 0.18, 0.78, 0.12, M.charcoal, 0.63, 2.02, 0);
const tail = addCylinder(group, 0.07, 0.16, 0.9, M.charcoal, -0.72, 1.15, 0, 7);
tail.rotation.z = -0.35;
group.userData.legs = legs;
return setShadow(group);
}
const cart = new THREE.Group();
scene.add(cart);
addBox(cart, 2.55, 0.2, 1.55, M.woodLight, 0, 0.8, 0);
addBox(cart, 2.55, 0.45, 0.12, M.wood, 0, 1.04, 0.75);
addBox(cart, 2.55, 0.45, 0.12, M.wood, 0, 1.04, -0.75);
addBox(cart, 0.12, 0.45, 1.5, M.wood, -1.2, 1.04, 0);
// Canvas canopy hoops and cover.
[-0.95, 0.95].forEach((x) => {
const hoop = new THREE.Mesh(
new THREE.TorusGeometry(0.82, 0.045, 6, 20, Math.PI),
M.woodDark
);
hoop.position.set(x, 1.28, 0);
hoop.rotation.y = Math.PI / 2;
cart.add(hoop);
});
const cartCover = new THREE.Mesh(
new THREE.CylinderGeometry(
0.84,
0.84,
1.9,
16,
1,
true,
0,
Math.PI
),
M.cream
);
cartCover.rotation.z = Math.PI / 2;
cartCover.rotation.y = Math.PI / 2;
cartCover.position.set(0, 1.28, 0);
cartCover.material.side = THREE.DoubleSide;
cartCover.castShadow = true;
cart.add(cartCover);
// Wheels with visible spokes.
const wheels = [];
function createWheel(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.62, z);
cart.add(group);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.52, 0.075, 7, 24),
M.woodDark
);
group.add(rim);
const hub = addCylinder(
group,
0.13,
0.13,
0.2,
M.wood,
0,
0,
0,
10
);
hub.rotation.x = Math.PI / 2;
for (let i = 0; i < 6; i++) {
const spoke = addBox(group, 0.06, 0.95, 0.06, M.woodLight, 0, 0, 0);
spoke.rotation.z = (i * Math.PI) / 3;
}
wheels.push(group);
return group;
}
createWheel(0, 0.88);
createWheel(0, -0.88);
// Shafts.
addBox(cart, 3.7, 0.12, 0.12, M.wood, 2.7, 0.72, 0.45);
addBox(cart, 3.7, 0.12, 0.12, M.wood, 2.7, 0.72, -0.45);
const horse = createHorse();
horse.position.set(4.35, 0, 0);
cart.add(horse);
setShadow(cart);
/* ---------------------------------------------------------
People
--------------------------------------------------------- */
function createPerson(robeMat = M.teal, scale = 1) {
const group = new THREE.Group();
const robe = addCylinder(group, 0.22, 0.36, 0.9, robeMat, 0, 0.62, 0, 9);
robe.rotation.y = 0.1;
addBox(group, 0.46, 0.5, 0.28, robeMat, 0, 1.12, 0);
addSphere(group, 0.22, M.skin, 0, 1.58, 0);
// Turban.
const turban = addSphere(group, 0.25, M.white, 0, 1.73, 0);
turban.scale.set(1.05, 0.65, 1.05);
// Face.
addSphere(group, 0.035, M.charcoal, 0.1, 1.6, 0.205);
addSphere(group, 0.035, M.charcoal, -0.1, 1.6, 0.205);
// Arms.
const leftArm = addCylinder(group, 0.075, 0.085, 0.7, robeMat, -0.29, 1.1, 0, 8);
const rightArm = addCylinder(group, 0.075, 0.085, 0.7, robeMat, 0.29, 1.1, 0, 8);
leftArm.rotation.z = -0.28;
rightArm.rotation.z = 0.28;
// Legs and sandals.
addCylinder(group, 0.09, 0.09, 0.5, M.leather, -0.14, 0.22, 0, 8);
addCylinder(group, 0.09, 0.09, 0.5, M.leather, 0.14, 0.22, 0, 8);
addBox(group, 0.17, 0.09, 0.3, M.charcoal, -0.14, 0.04, 0.07);
addBox(group, 0.17, 0.09, 0.3, M.charcoal, 0.14, 0.04, 0.07);
group.userData.arms = [leftArm, rightArm];
group.scale.setScalar(scale);
return setShadow(group);
}
const activePorter = createPerson(M.red, 1.04);
scene.add(activePorter);
const storageWorker = createPerson(M.teal, 0.98);
storageWorker.position.set(-4.6, 0.18, -5.8);
storageWorker.rotation.y = -1.2;
scene.add(storageWorker);
const bayWorker = createPerson(M.gold, 0.98);
bayWorker.position.set(7.2, 0.18, 3.0);
bayWorker.rotation.y = 0.65;
scene.add(bayWorker);
const waterWorker = createPerson(M.cream, 0.95);
waterWorker.position.set(-7.4, 0.18, 4.8);
waterWorker.rotation.y = -0.5;
scene.add(waterWorker);
const merchant = createPerson(M.tealDark, 1.02);
merchant.position.set(5.9, 0.18, -1.4);
merchant.rotation.y = 1.3;
scene.add(merchant);
/* ---------------------------------------------------------
Transferable goods
--------------------------------------------------------- */
function createTransferCargo(type) {
const group = new THREE.Group();
if (type === 0) {
// Silk bale wrapped in teal cloth.
addBox(group, 0.76, 0.58, 0.64, M.teal);
addBox(group, 0.09, 0.62, 0.68, M.gold, -0.18, 0, 0);
addBox(group, 0.09, 0.62, 0.68, M.gold, 0.18, 0, 0);
addBox(group, 0.79, 0.08, 0.67, M.gold, 0, 0.13, 0);
} else if (type === 1) {
// Spice sack.
const sack = addSphere(group, 0.43, M.sandLight, 0, 0, 0);
sack.scale.set(0.95, 1.18, 0.86);
addCylinder(group, 0.11, 0.18, 0.22, M.woodDark, 0, 0.42, 0, 8);
addBox(group, 0.33, 0.23, 0.25, M.red, 0, 0.51, 0);
} else {
// Lacquered casket.
addBox(group, 0.82, 0.42, 0.56, M.red);
addBox(group, 0.88, 0.11, 0.61, M.woodDark, 0, 0.22, 0);
addBox(group, 0.12, 0.46, 0.59, M.gold, 0, 0, 0);
addSphere(group, 0.065, M.gold, 0, 0.3, 0);
}
scene.add(group);
return setShadow(group);
}
const cargo = [createTransferCargo(0), createTransferCargo(1), createTransferCargo(2)];
const camelPackPositions = [
new THREE.Vector3(-0.4, 2.55, 0.62),
new THREE.Vector3(0.25, 2.48, -0.62),
new THREE.Vector3(0.78, 2.55, 0.2)
];
const cartCargoPositions = [
new THREE.Vector3(-0.48, 1.25, 0.32),
new THREE.Vector3(0.28, 1.18, -0.34),
new THREE.Vector3(0.74, 1.15, 0.16)
];
function placeCargoOnCamel(index) {
const local = camelPackPositions[index];
cargo[index].position.set(
incomingCaravan.position.x + local.x,
local.y,
incomingCaravan.position.z + local.z
);
cargo[index].rotation.set(0, 0, 0);
}
function placeCargoOnCart(index) {
const local = cartCargoPositions[index];
const world = cart.localToWorld(local.clone());
cargo[index].position.copy(world);
cargo[index].rotation.set(0, 0, 0);
}
// Fixed ground paths from the incoming camel to the cart.
const transferCurves = camelPackPositions.map((pack, index) => {
const side = pack.z > 0 ? 1 : -1;
return new THREE.CatmullRomCurve3([
new THREE.Vector3(-17.9 + pack.x, pack.y, pack.z),
new THREE.Vector3(-15.4, 1.95, side * 1.45),
new THREE.Vector3(-8.0, 1.5, side * 1.8),
new THREE.Vector3(1.5, 1.35, side * 1.25),
new THREE.Vector3(6.6, 1.25, side * 0.55),
new THREE.Vector3(
cart.position.x + cartCargoPositions[index].x,
cartCargoPositions[index].y,
cartCargoPositions[index].z
)
]);
});
/* ---------------------------------------------------------
Palm trees and small environmental details
--------------------------------------------------------- */
function createPalm(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
const trunk = addCylinder(group, 0.22, 0.38, 4.7, M.wood, 0, 2.3, 0, 9);
trunk.rotation.z = -0.12;
const crown = new THREE.Group();
crown.position.set(-0.3, 4.55, 0);
group.add(crown);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const leaf = addBox(
crown,
2.2,
0.08,
0.42,
i % 2 ? M.tealDark : M.teal,
Math.cos(angle) * 0.75,
0,
Math.sin(angle) * 0.75
);
leaf.rotation.y = -angle;
leaf.rotation.z = -0.12;
}
addSphere(group, 0.35, M.woodDark, -0.35, 4.47, 0);
return setShadow(group);
}
createPalm(-17.5, 7.2, 0.9);
createPalm(-20.2, 8.1, 0.72);
createPalm(18.5, -8.0, 0.82);
// Small storage jars near the gate.
createJar(scene, 12.3, 0.18, -7.9, 0.85, M.teal);
createJar(scene, 13.1, 0.18, -7.2, 0.65, M.cream);
createJar(scene, -13.0, 0.18, 7.2, 0.72, M.red);
// Dust motes.
const dustCount = 220;
const dustPositions = new Float32Array(dustCount * 3);
for (let i = 0; i < dustCount; i++) {
dustPositions[i * 3] = (random() - 0.5) * 65;
dustPositions[i * 3 + 1] = random() * 12 + 0.4;
dustPositions[i * 3 + 2] = (random() - 0.5) * 55;
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xf4dfb3,
size: 0.065,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
scene.add(dust);
/* ---------------------------------------------------------
Custom camera controls
--------------------------------------------------------- */
const cameraState = {
radius: 46,
theta: 0.78,
phi: 0.88,
targetRadius: 46,
targetTheta: 0.78,
targetPhi: 0.88,
target: new THREE.Vector3(0, 1.2, 0)
};
function resetCamera() {
cameraState.targetRadius = 46;
cameraState.targetTheta = 0.78;
cameraState.targetPhi = 0.88;
cameraState.target.set(0, 1.2, 0);
}
function updateCamera() {
cameraState.radius = lerp(
cameraState.radius,
cameraState.targetRadius,
0.1
);
cameraState.theta = lerp(
cameraState.theta,
cameraState.targetTheta,
0.1
);
cameraState.phi = lerp(
cameraState.phi,
cameraState.targetPhi,
0.1
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
const pointer = {
down: false,
lastX: 0,
lastY: 0
};
renderer.domElement.addEventListener("pointerdown", (event) => {
pointer.down = true;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointer.down) return;
const dx = event.clientX - pointer.lastX;
const dy = event.clientY - pointer.lastY;
pointer.lastX = event.clientX;
pointer.lastY = event.clientY;
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = clamp(
cameraState.targetPhi - dy * 0.005,
0.32,
1.32
);
});
function releasePointer(event) {
pointer.down = false;
renderer.domElement.classList.remove("dragging");
if (event.pointerId !== undefined) {
try {
renderer.domElement.releasePointerCapture(event.pointerId);
} catch (_) {}
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
25,
72
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
/* ---------------------------------------------------------
Animation and UI state
--------------------------------------------------------- */
const pauseButton = document.querySelector("#pause-button");
const pauseText = document.querySelector("#pause-text");
const phaseLabel = document.querySelector("#phase-label");
const statusValue = document.querySelector("#status-value");
const progress = document.querySelector("#progress");
const step1 = document.querySelector("#step-1");
const step2 = document.querySelector("#step-2");
const cargoChips = [
document.querySelector("#cargo-0"),
document.querySelector("#cargo-1"),
document.querySelector("#cargo-2")
];
let paused = false;
let elapsed = 0;
let previousCartX = 8.5;
const cycleDuration = 28;
const transferStarts = [5, 8, 11];
const transferDuration = 2.6;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseText.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute(
"aria-label",
paused ? "Resume exchange animation" : "Pause exchange animation"
);
});
document.querySelector("#reset-view").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
function updateUI(t) {
progress.style.width = `${(t / cycleDuration) * 100}%`;
const activeTransfers = transferStarts.filter(
(start) => t >= start && t < start + transferDuration
);
cargoChips.forEach((chip, i) => {
const start = transferStarts[i];
const end = start + transferDuration;
chip.classList.toggle("active", t >= start && t < end);
chip.classList.toggle("done", t >= end);
});
if (t < 4) {
phaseLabel.textContent = "Stage 01 arrives";
statusValue.textContent = "Incoming caravan approaching";
step1.classList.add("active");
step2.classList.remove("active");
} else if (t < 5) {
phaseLabel.textContent = "Unloading";
statusValue.textContent = "Caravan secured at the western gate";
step1.classList.add("done");
step1.classList.remove("active");
} else if (t < 14) {
phaseLabel.textContent = "Carrier exchange";
statusValue.textContent = activeTransfers.length
? "Cargo changes to the regional cart"
: "Awaiting next item";
step1.classList.remove("active");
step1.classList.add("done");
step2.classList.add("active");
} else if (t < 22) {
phaseLabel.textContent = "Stage 02 departs";
statusValue.textContent = "Loaded cart heading east";
step1.classList.remove("active");
step1.classList.add("done");
step2.classList.remove("active");
step2.classList.add("done");
} else {
phaseLabel.textContent = "Next relay";
statusValue.textContent = "Carriers preparing for another exchange";
step1.classList.remove("active");
step2.classList.remove("active");
}
}
function animateCamel(camel, moving, time, phase = 0) {
const legs = camel.userData.legs;
legs.forEach((leg, index) => {
const direction = index % 2 === 0 ? 1 : -1;
leg.rotation.z = moving
? Math.sin(time * 5.3 + phase + index * 0.8) * 0.24 * direction
: 0;
});
}
function animate() {
requestAnimationFrame(animate);
if (!paused) {
elapsed += Math.min(clock.getDelta(), 0.05);
}
const t = elapsed % cycleDuration;
// Incoming Bactrian caravan.
let incomingX;
let incomingMoving = false;
if (t < 4) {
incomingX = lerp(-29, -17.5, smoothstep(t / 4));
incomingMoving = true;
} else if (t < 22) {
incomingX = -17.5;
} else {
incomingX = lerp(-17.5, -31, smoothstep((t - 22) / 6));
incomingMoving = true;
}
incomingCaravan.position.x = incomingX;
// Regional cart approaches, waits for transfer, then departs.
let cartX;
let cartMoving = false;
if (t < 4) {
cartX = lerp(24, 8.5, smoothstep(t / 4));
cartMoving = true;
} else if (t < 14) {
cartX = 8.5;
} else if (t < 22) {
cartX = lerp(8.5, 24, smoothstep((t - 14) / 8));
cartMoving = true;
} else {
cartX = 24;
}
cart.position.x = cartX;
const cartDelta = cartX - previousCartX;
previousCartX = cartX;
wheels.forEach((wheel) => {
wheel.rotation.z -= cartDelta / 0.52;
});
animateCamel(leadCamel, incomingMoving, elapsed, 0);
animateCamel(secondCamel, incomingMoving, elapsed, 1.2);
const horseLegs = horse.userData.legs;
horseLegs.forEach((leg, index) => {
leg.rotation.z = cartMoving
? Math.sin(elapsed * 5 + index * 1.4) * 0.23 *
(index % 2 === 0 ? 1 : -1)
: 0;
});
// Place goods on their original carrier before transfer.
cargo.forEach((_, index) => placeCargoOnCamel(index));
// Transfer each item sequentially from camel to cart.
let porterVisible = false;
cargo.forEach((item, index) => {
const start = transferStarts[index];
const end = start + transferDuration;
if (t >= start && t < end) {
porterVisible = true;
const rawProgress = (t - start) / transferDuration;
const p = smoothstep(rawProgress);
const point = transferCurves[index].getPoint(p);
const tangent = transferCurves[index].getTangent(p);
item.position.copy(point);
item.rotation.y = Math.sin(p * Math.PI * 2) * 0.06;
activePorter.position.set(
point.x - tangent.x * 0.45,
0.18 + Math.abs(Math.sin(elapsed * 8)) * 0.025,
point.z + 0.38
);
activePorter.rotation.y = Math.atan2(tangent.x, tangent.z);
activePorter.userData.arms.forEach((arm, armIndex) => {
arm.rotation.x = armIndex === 0 ? -1.05 : -0.85;
});
} else if (t >= end) {
placeCargoOnCart(index);
}
});
activePorter.visible = porterVisible;
// Gentle body motion.
leadCamel.position.y = incomingMoving
? Math.abs(Math.sin(elapsed * 5.3)) * 0.035
: 0;
secondCamel.position.y = incomingMoving
? Math.abs(Math.sin(elapsed * 5.3 + 1.2)) * 0.035
: 0;
// Banner and dust movement.
architecture.children.forEach((child) => {
if (child.userData.phase !== undefined) {
child.rotation.z =
child.userData.baseRotation + Math.sin(elapsed * 1.8 + child.userData.phase) * 0.025;
}
});
dust.rotation.y = elapsed * 0.012;
dust.position.y = Math.sin(elapsed * 0.25) * 0.15;
updateUI(t);
updateCamera();
updateHotspots();
renderer.render(scene, camera);
}
/* ---------------------------------------------------------
Screen-space labels
--------------------------------------------------------- */
const hotspotElements = [...document.querySelectorAll(".hotspot")];
const hotspotPositions = hotspotElements.map((element) => {
const values = element.dataset.position.split(",").map(Number);
return new THREE.Vector3(...values);
});
const marker1 = document.querySelector("#stage-marker-1");
const marker2 = document.querySelector("#stage-marker-2");
function projectElement(element, worldPosition, offsetY = 0) {
const projected = worldPosition.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
element.style.opacity = visible ? "1" : "0";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
function updateHotspots() {
hotspotElements.forEach((element, index) => {
projectElement(element, hotspotPositions[index]);
});
projectElement(marker1, new THREE.Vector3(-23.8, 3.3, 0));
projectElement(marker2, new THREE.Vector3(22.8, 3.0, 0));
}
/* ---------------------------------------------------------
Resize and startup
--------------------------------------------------------- */
const clock = new THREE.Clock();
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
updateCamera();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 700);
});
</script>
</body>
</html>
```