Prompt specification
Build or customize the Three.js 3D scene "Khan el-Khalili passage and courtyard junction" 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: Khan el-Khalili passage and courtyard junction
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/khan-el-khalili-passage-and-courtyard-junction-x2899
Scene Description Prompt:
Build a voxel Khan el-Khalili junction in Cairo, connecting a covered shopping lane with an open courtyard. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (khan-el-khalili-passage-and-courtyard-junction-x2899):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Khan el-Khalili — Voxel Junction</title>
<style>
:root {
color-scheme: dark;
--ink: #f5e8cf;
--muted: #c9b99b;
--panel: rgba(26, 22, 19, 0.82);
--line: rgba(245, 232, 207, 0.2);
--accent: #df9b4b;
--teal: #56a6a0;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #171a24;
}
body {
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.title-card {
position: fixed;
z-index: 10;
top: 24px;
left: 24px;
width: min(370px, calc(100vw - 48px));
padding: 19px 21px 18px;
border: 1px solid var(--line);
border-radius: 3px;
background:
linear-gradient(135deg, rgba(223, 155, 75, 0.09), transparent 55%),
var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 9px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 39px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 12px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.subtitle strong {
color: var(--ink);
font-weight: 650;
}
.view-controls {
position: fixed;
z-index: 20;
right: 22px;
top: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 7px;
max-width: min(480px, calc(100vw - 44px));
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 2px;
padding: 10px 12px;
background: rgba(24, 22, 20, 0.82);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover,
button.active {
border-color: rgba(223, 155, 75, 0.8);
background: rgba(91, 62, 34, 0.9);
}
button:active {
transform: translateY(1px);
}
.bottom-bar {
position: fixed;
z-index: 20;
left: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 12px;
color: rgba(245, 232, 207, 0.78);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
text-shadow: 0 2px 12px rgba(0, 0, 0, 0.8);
}
.mouse-icon {
position: relative;
width: 17px;
height: 24px;
border: 1px solid currentColor;
border-radius: 9px;
}
.mouse-icon::after {
content: "";
position: absolute;
top: 4px;
left: 7px;
width: 2px;
height: 5px;
border-radius: 2px;
background: currentColor;
}
.pin {
position: fixed;
z-index: 8;
display: none;
transform: translate(-50%, -100%);
padding: 7px 9px;
border: 1px solid rgba(245, 232, 207, 0.35);
background: rgba(22, 20, 18, 0.82);
color: var(--ink);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(8px);
}
.pin::after {
content: "";
position: absolute;
left: 50%;
bottom: -5px;
width: 8px;
height: 8px;
border-right: 1px solid rgba(245, 232, 207, 0.35);
border-bottom: 1px solid rgba(245, 232, 207, 0.35);
background: rgba(22, 20, 18, 0.82);
transform: translateX(-50%) rotate(45deg);
}
.pin span {
margin-right: 7px;
color: var(--accent);
}
.loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #171a24;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 15px 17px;
}
.title-card .subtitle {
max-width: 300px;
}
.view-controls {
top: auto;
right: 14px;
bottom: 14px;
left: 14px;
justify-content: center;
}
button {
padding: 9px 10px;
font-size: 9px;
}
.bottom-bar {
display: none;
}
.pin {
display: none !important;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<section class="title-card">
<div class="eyebrow">Cairo · After Dark</div>
<h1>Khan el-Khalili</h1>
<p class="subtitle">
A voxel junction where the <strong>covered shopping lane</strong>
opens into a lantern-lit <strong>courtyard</strong>.
</p>
</section>
<nav class="view-controls" aria-label="Scene controls">
<button data-view="junction" class="active">Junction</button>
<button data-view="lane">Shopping Lane</button>
<button data-view="courtyard">Courtyard</button>
<button data-view="overview">Overview</button>
<button id="orbitButton" aria-pressed="false">Auto Orbit</button>
<button id="moodButton">Light · Night</button>
</nav>
<div class="bottom-bar">
<span class="mouse-icon"></span>
<span>Drag to orbit · Wheel to zoom · Shift-drag to pan</span>
</div>
<div class="pin" data-pin="lane"><span>01</span>Covered Lane</div>
<div class="pin" data-pin="arch"><span>02</span>Junction Arch</div>
<div class="pin" data-pin="fountain"><span>03</span>Voxel Fountain</div>
<div class="pin" data-pin="court"><span>04</span>Open Courtyard</div>
<div class="loading" id="loading">Constructing the old city…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Scene setup
// ------------------------------------------------------------
const stage = document.querySelector("#stage");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
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.08;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x171a24);
scene.fog = new THREE.Fog(0x171a24, 34, 78);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
150
);
const world = new THREE.Group();
scene.add(world);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
let randomSeed = 82473;
function random() {
randomSeed |= 0;
randomSeed = (randomSeed + 0x6d2b79f5) | 0;
let t = Math.imul(randomSeed ^ (randomSeed >>> 15), 1 | randomSeed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function standard(color, roughness = 0.86, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
sand: standard(0xc79055),
sandLight: standard(0xe0ad70),
sandPale: standard(0xedc990),
sandDark: standard(0x98613e),
trim: standard(0x724936),
plaster: standard(0xd7ae78),
wood: standard(0x593624),
darkWood: standard(0x30231d),
charcoal: standard(0x201d1b),
black: standard(0x121416),
brass: standard(0xb88337, 0.45, 0.55),
copper: standard(0x9d5938, 0.52, 0.42),
teal: standard(0x367e7b),
turquoise: standard(0x58aaa1),
red: standard(0x8f3f32),
rug: standard(0x6f302b),
cream: standard(0xe7d5ae),
leaf: standard(0x4d6e43),
leafLight: standard(0x73804c),
soil: standard(0x453528),
water: new THREE.MeshStandardMaterial({
color: 0x397f94,
roughness: 0.24,
metalness: 0.12,
transparent: true,
opacity: 0.82
}),
lantern: new THREE.MeshStandardMaterial({
color: 0xffd17a,
emissive: 0xff9b37,
emissiveIntensity: 2.3,
roughness: 0.45
})
};
function box(w, h, d, x, y, z, material, options = {}) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(w, h, d);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.rotation) {
mesh.rotation.set(
options.rotation.x || 0,
options.rotation.y || 0,
options.rotation.z || 0
);
}
(options.parent || world).add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xa8c8dc, 0x68452f, 0.78);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffd39c, 2.1);
sun.position.set(-15, 28, 18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -34;
sun.shadow.camera.right = 34;
sun.shadow.camera.top = 30;
sun.shadow.camera.bottom = -30;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 85;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x7397ba, 0.65);
fill.position.set(18, 15, -20);
scene.add(fill);
const pointLights = [];
function pointLight(color, intensity, distance, x, y, z) {
const light = new THREE.PointLight(color, intensity, distance, 2);
light.position.set(x, y, z);
scene.add(light);
pointLights.push(light);
return light;
}
pointLight(0xffa649, 48, 15, 8.5, 3.7, 1);
pointLight(0xffa649, 38, 12, 0.15, 4.2, 0);
pointLight(0xffbd70, 28, 10, -4.2, 4.4, -3);
// ------------------------------------------------------------
// Ground and paving
// ------------------------------------------------------------
box(35, 0.7, 32, 2.5, -0.42, 0, materials.sandDark);
box(31, 0.25, 29, 2.5, -0.13, 0, materials.sand);
function makePaving(startX, startZ, columns, rows, cell, palette) {
const count = columns * rows;
const mesh = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.95
}),
count
);
const matrix = new THREE.Matrix4();
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
const color = new THREE.Color();
let index = 0;
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
position.set(
startX + column * cell,
-0.005,
startZ + row * cell
);
scale.set(cell - 0.045, 0.095, cell - 0.045);
matrix.compose(position, quaternion, scale);
color.setHex(palette[Math.floor(random() * palette.length)]);
color.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.06,
(random() - 0.5) * 0.08
);
mesh.setMatrixAt(index, matrix);
mesh.setColorAt(index, color);
index++;
}
}
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = false;
mesh.receiveShadow = true;
world.add(mesh);
}
makePaving(-8.7, -13.5, 8, 23, 1.15, [
0x9e7049,
0xaa7b50,
0xb48657,
0x936746
]);
makePaving(3.3, -11.3, 11, 20, 1.16, [
0xc99761,
0xd0a06a,
0xbe8b57,
0xd8aa72
]);
// Lane carpet.
box(3.25, 0.035, 26.6, -4.15, 0.07, 0, materials.rug);
box(0.13, 0.05, 26.6, -5.65, 0.085, 0, materials.brass);
box(0.13, 0.05, 26.6, -2.65, 0.085, 0, materials.brass);
for (let z = -12; z <= 12; z += 4) {
box(2.65, 0.045, 0.12, -4.15, 0.095, z, materials.red);
box(0.09, 0.055, 1.2, -4.15, 0.1, z + 0.6, materials.turquoise);
box(0.09, 0.055, 1.2, -4.15, 0.1, z - 0.6, materials.turquoise);
}
// ------------------------------------------------------------
// Masonry and stepped voxel arches
// ------------------------------------------------------------
const masonryMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.93
});
function buildWall(
axis,
fixed,
start,
end,
height,
openings = [],
thickness = 0.58,
baseY = 0
) {
const cell = 0.8;
const rows = Math.round(height / cell);
const columns = Math.ceil((end - start) / cell);
const maximum = rows * columns;
const instances = [];
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
const along = start + (column + 0.5) * cell;
let hidden = false;
for (const opening of openings) {
const spring = opening.spring ?? 3;
const steps = Math.max(1, rows - 1 - spring);
const taper = opening.width / 2 / steps;
const halfWidth =
row <= spring
? opening.width / 2
: Math.max(0, opening.width / 2 - (row - spring) * taper);
if (Math.abs(along - opening.center) < halfWidth) {
hidden = true;
break;
}
}
if (!hidden) {
instances.push({ row, column, along });
}
}
}
const mesh = new THREE.InstancedMesh(
unitBox,
masonryMaterial,
instances.length
);
const matrix = new THREE.Matrix4();
const position = new THREE.Vector3();
const quaternion = new THREE.Quaternion();
const scale = new THREE.Vector3();
const color = new THREE.Color();
const stoneColors = [0xc38d56, 0xd09b63, 0xb77b4e, 0xd6a36c];
instances.forEach((instance, index) => {
const y = baseY + (instance.row + 0.5) * cell;
if (axis === "z") {
position.set(fixed, y, instance.along);
scale.set(thickness * 0.95, cell * 0.94, cell * 0.94);
} else {
position.set(instance.along, y, fixed);
scale.set(cell * 0.94, cell * 0.94, thickness * 0.95);
}
matrix.compose(position, quaternion, scale);
mesh.setMatrixAt(index, matrix);
color.setHex(
stoneColors[
(Math.floor(random() * stoneColors.length) +
instance.row +
instance.column) %
stoneColors.length
]
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.055);
mesh.setColorAt(index, color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
world.add(mesh);
}
function trimOpening(
axis,
fixed,
center,
width,
height,
spring,
material = materials.trim
) {
const cell = 0.8;
const rows = Math.round(height / cell);
const steps = Math.max(1, rows - 1 - spring);
const taper = width / 2 / steps;
function trimBlock(along, y, alongSize = 0.16) {
if (axis === "z") {
box(thicknessOf(fixed) + 0.11, cell * 0.94, alongSize, fixed, y, along, material);
} else {
box(alongSize, cell * 0.94, thicknessOf(fixed) + 0.11, along, y, fixed, material);
}
}
function thicknessOf() {
return 0.58;
}
for (let row = 0; row < spring; row++) {
const y = (row + 0.5) * cell;
trimBlock(center - width / 2, y);
trimBlock(center + width / 2, y);
}
for (let row = spring; row < rows; row++) {
const halfWidth = Math.max(
0,
width / 2 - (row - spring) * taper
);
const y = (row + 0.5) * cell;
if (halfWidth < 0.04) {
trimBlock(center, y, 0.52);
} else {
trimBlock(center - halfWidth, y);
trimBlock(center + halfWidth, y);
}
}
}
const leftOpenings = [
{ center: -11.5, width: 2.4, spring: 3 },
{ center: -6.5, width: 2.4, spring: 3 },
{ center: -1.5, width: 2.4, spring: 3 },
{ center: 3.5, width: 2.4, spring: 3 },
{ center: 8.5, width: 2.4, spring: 3 },
{ center: 12, width: 2.4, spring: 3 }
];
const rightOpenings = [
{ center: -11, width: 2.4, spring: 3 },
{ center: -6.5, width: 2.4, spring: 3 },
{ center: 0, width: 5.2, spring: 4 },
{ center: 6.5, width: 2.4, spring: 3 },
{ center: 11, width: 2.4, spring: 3 }
];
buildWall("z", -9, -13.6, 13.6, 6.4, leftOpenings);
buildWall("z", 1, -13.6, 13.6, 7.2, rightOpenings, 0.66);
leftOpenings.forEach(opening => {
trimOpening("z", -9, opening.center, opening.width, 6.4, opening.spring);
});
rightOpenings.forEach(opening => {
trimOpening(
"z",
1,
opening.center,
opening.width,
7.2,
opening.spring,
opening.width > 4 ? materials.sandLight : materials.trim
);
});
// Main junction keystone and lanterns.
box(0.86, 0.72, 0.88, 0.92, 6.0, 0, materials.sandPale);
box(0.91, 0.14, 0.98, 0.91, 6.38, 0, materials.turquoise);
// ------------------------------------------------------------
// Shop fronts and generated signage
// ------------------------------------------------------------
function signTexture(text, accent = "#df9b4b") {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = "#241d18";
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = accent;
context.lineWidth = 5;
context.strokeRect(10, 10, canvas.width - 20, canvas.height - 20);
context.fillStyle = "#f0d9ad";
context.font = "700 47px Georgia";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text.toUpperCase(), canvas.width / 2, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(
renderer.capabilities.getMaxAnisotropy(),
8
);
return texture;
}
function addSign(text, x, y, z, facing, accent) {
const texture = signTexture(text, accent);
const material = new THREE.MeshBasicMaterial({ map: texture });
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(2.12, 0.53),
material
);
sign.position.set(x, y, z);
sign.rotation.y = facing === "east" ? Math.PI / 2 : -Math.PI / 2;
world.add(sign);
return sign;
}
function addLantern(x, y, z, withLight = false) {
box(0.34, 0.46, 0.34, x, y, z, materials.lantern, {
castShadow: false
});
box(0.43, 0.07, 0.43, x, y + 0.27, z, materials.brass);
box(0.43, 0.07, 0.43, x, y - 0.27, z, materials.brass);
box(0.045, 0.51, 0.045, x - 0.2, y, z - 0.2, materials.brass);
box(0.045, 0.51, 0.045, x + 0.2, y, z - 0.2, materials.brass);
box(0.045, 0.51, 0.045, x - 0.2, y, z + 0.2, materials.brass);
box(0.045, 0.51, 0.045, x + 0.2, y, z + 0.2, materials.brass);
box(0.035, 0.5, 0.035, x, y + 0.54, z, materials.darkWood);
if (withLight) {
pointLight(0xffa649, 22, 8, x, y, z);
}
}
function addShop(fixed, center, side, name, accent, accentMaterial) {
const width = 2.48;
const backX = fixed + side * 1.52;
// Recessed interior.
box(0.2, 3.7, width, backX, 1.85, center, materials.charcoal);
box(0.7, 0.16, width - 0.38, fixed + side * 1.15, 0.22, center, materials.wood);
box(0.14, 2.5, 0.8, fixed + side * 1.25, 1.45, center, materials.darkWood);
// Shelves and small colorful goods.
for (let shelf = 0; shelf < 3; shelf++) {
const y = 0.8 + shelf * 0.78;
box(0.72, 0.12, width - 0.62, fixed + side * 1.22, y, center, materials.wood);
for (let item = -1; item <= 1; item++) {
const itemMaterial = [materials.red, materials.teal, accentMaterial][
(item + shelf + 10) % 3
];
box(
0.32,
0.26 + (item + shelf + 10) % 2 * 0.1,
0.3,
fixed + side * 1.19,
y + 0.19,
center + item * 0.53,
itemMaterial
);
}
}
// Stepped fabric awning, facing into the lane.
for (let stripe = 0; stripe < 5; stripe++) {
const awningX = fixed - side * (0.18 + stripe * 0.34);
const awningY = 3.58 - stripe * 0.075;
const awningMaterial =
stripe % 2 === 0 ? materials.cream : accentMaterial;
box(
0.38,
0.14,
width + 0.38,
awningX,
awningY,
center,
awningMaterial
);
}
addSign(
name,
fixed - side * 0.39,
4.35,
center,
side < 0 ? "east" : "west",
accent
);
addLantern(fixed - side * 0.8, 3.03, center + width * 0.34);
}
const leftShops = [
["Spices", "#df9b4b", materials.red],
["Textiles", "#58aaa1", materials.teal],
["Brass", "#e1bd72", materials.brass],
["Lanterns", "#df9b4b", materials.copper],
["Coffee", "#b56d48", materials.sandDark],
["Papyrus", "#58aaa1", materials.turquoise]
];
leftOpenings.forEach((opening, index) => {
const shop = leftShops[index];
addShop(-9, opening.center, -1, shop[0], shop[1], shop[2]);
});
const rightShops = [
["Copper", "#df9b4b", materials.copper],
["Perfume", "#d89979", materials.red],
["Carpets", "#58aaa1", materials.teal],
["Books", "#df9b4b", materials.sandDark]
];
[-11, -6.5, 6.5, 11].forEach((center, index) => {
const shop = rightShops[index];
addShop(1, center, 1, shop[0], shop[1], shop[2]);
});
// ------------------------------------------------------------
// Covered lane roof: beams, reed lattice, and cloth edges
// ------------------------------------------------------------
for (let z = -13.2; z <= 13.2; z += 1.65) {
box(9.9, 0.19, 0.2, -4, 5.35, z, materials.darkWood);
}
box(0.24, 0.26, 27.4, -8.75, 5.55, 0, materials.wood);
box(0.24, 0.26, 27.4, 0.75, 5.55, 0, materials.wood);
for (const x of [-7.4, -5.8, -4.2, -2.6, -1]) {
box(0.11, 0.12, 27.1, x, 5.48, 0, materials.sandDark);
}
box(1.85, 0.13, 27.2, -8.05, 5.64, 0, materials.cream);
box(1.55, 0.13, 27.2, 0.05, 5.64, 0, materials.sandLight);
for (const z of [-8, 0, 8]) {
box(0.055, 0.58, 0.055, -4.15, 5.0, z, materials.darkWood);
addLantern(-4.15, 4.52, z, z === 0);
}
// ------------------------------------------------------------
// Courtyard perimeter and arcade
// ------------------------------------------------------------
const farOpenings = [
{ center: -8.2, width: 3.2, spring: 4 },
{ center: -2.8, width: 3.2, spring: 4 },
{ center: 2.8, width: 3.2, spring: 4 },
{ center: 8.2, width: 3.2, spring: 4 }
];
buildWall("z", 16.6, -12.3, 12.3, 6.4, farOpenings);
farOpenings.forEach(opening => {
trimOpening("z", 16.6, opening.center, opening.width, 6.4, 4);
box(0.16, 4.8, opening.width - 0.28, 17.18, 2.4, opening.center, materials.charcoal);
});
const backOpenings = [
{ center: 7, width: 3.4, spring: 4 },
{ center: 12, width: 3.4, spring: 4 }
];
buildWall("x", -12.3, 3.2, 16.6, 6.4, backOpenings);
backOpenings.forEach(opening => {
trimOpening("x", -12.3, opening.center, opening.width, 6.4, 4);
box(opening.width - 0.3, 4.8, 0.16, opening.center, 2.4, -12.88, materials.charcoal);
});
buildWall("x", 12.3, 3.2, 16.6, 1.6);
// A full entrance portal at the far end of the lane.
buildWall(
"x",
-14,
-9,
1,
6.4,
[{ center: -4, width: 4.8, spring: 3 }]
);
trimOpening("x", -14, -4, 4.8, 6.4, 3);
box(4.4, 0.22, 0.76, -4, 5.1, -13.82, materials.sandLight);
// ------------------------------------------------------------
// Fountain and geometric tile mosaic
// ------------------------------------------------------------
box(3.9, 0.32, 3.9, 8.5, 0.12, 1, materials.sandPale);
box(3.35, 0.52, 3.35, 8.5, 0.48, 1, materials.sandLight);
box(2.7, 0.12, 2.7, 8.5, 0.79, 1, materials.water, {
castShadow: false
});
box(3.35, 0.24, 0.24, 8.5, 0.83, -0.57, materials.trim);
box(3.35, 0.24, 0.24, 8.5, 0.83, 2.57, materials.trim);
box(0.24, 0.24, 3.35, 6.97, 0.83, 1, materials.trim);
box(0.24, 0.24, 3.35, 10.03, 0.83, 1, materials.trim);
box(0.72, 1.45, 0.72, 8.5, 1.48, 1, materials.sandPale);
box(1.85, 0.24, 1.85, 8.5, 2.15, 1, materials.sandLight);
box(1.25, 0.22, 1.25, 8.5, 2.34, 1, materials.water, {
castShadow: false
});
box(0.35, 0.78, 0.35, 8.5, 2.75, 1, materials.sandPale);
box(0.68, 0.18, 0.68, 8.5, 3.18, 1, materials.brass);
const mosaicMaterials = [
materials.teal,
materials.cream,
materials.red,
materials.sandDark
];
for (let x = 0; x < 9; x++) {
for (let z = 0; z < 9; z++) {
const px = 6.62 + x * 0.47;
const pz = -0.97 + z * 0.47;
if (Math.abs(px - 8.5) < 1.9 && Math.abs(pz - 1) < 1.9) {
continue;
}
const material =
(x + z) % 2 === 0
? mosaicMaterials[(x + z) % mosaicMaterials.length]
: materials.sandPale;
box(0.4, 0.055, 0.4, px, 0.09, pz, material, {
castShadow: false
});
}
}
// ------------------------------------------------------------
// Voxel palms, planters, benches, and market props
// ------------------------------------------------------------
function addPalm(x, z, height = 5.2) {
const levels = Math.floor(height / 0.43);
for (let level = 0; level < levels; level++) {
const offset = level % 2 === 0 ? -0.08 : 0.08;
box(
0.4,
0.44,
0.4,
x + offset,
0.25 + level * 0.42,
z,
level % 3 === 0 ? materials.sandDark : materials.wood
);
}
const crownY = height + 0.1;
box(0.78, 0.62, 0.78, x, crownY, z, materials.leaf);
box(3.0, 0.22, 0.65, x, crownY + 0.04, z, materials.leaf);
box(0.65, 0.22, 3.0, x, crownY + 0.04, z, materials.leafLight);
box(2.0, 0.2, 0.5, x + 1.0, crownY + 0.34, z + 0.8, materials.leaf);
box(2.0, 0.2, 0.5, x - 1.0, crownY + 0.34, z - 0.8, materials.leafLight);
box(0.5, 0.2, 2.0, x + 0.8, crownY + 0.34, z + 1.0, materials.leafLight);
box(0.5, 0.2, 2.0, x - 0.8, crownY + 0.34, z - 1.0, materials.leaf);
box(1.35, 0.65, 1.35, x, 0.23, z, materials.sandDark);
box(1.12, 0.12, 1.12, x, 0.58, z, materials.soil);
}
addPalm(14.1, -8.5, 5.7);
addPalm(4.9, 8.6, 5.0);
addPalm(14.2, 7.6, 5.35);
function planter(x, z, width = 2.2) {
box(width, 0.7, 1.3, x, 0.36, z, materials.sandLight);
box(width - 0.25, 0.12, 1.05, x, 0.74, z, materials.soil);
box(width - 0.45, 0.42, 0.95, x, 0.96, z, materials.leaf);
box(width - 0.75, 0.35, 0.65, x, 1.22, z, materials.leafLight);
}
planter(4.2, -9.3);
planter(12.0, 9.5, 2.8);
function bench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
world.add(group);
const seat = box(2.7, 0.22, 0.65, 0, 0.78, 0, materials.wood, {
parent: group
});
seat.receiveShadow = true;
box(0.24, 0.72, 0.5, -1.03, 0.39, 0, materials.darkWood, {
parent: group
});
box(0.24, 0.72, 0.5, 1.03, 0.39, 0, materials.darkWood, {
parent: group
});
}
bench(5.0, -5.8, Math.PI / 2);
bench(12.4, 4.8, Math.PI / 2);
bench(10.7, -9.5);
// Crates and stacked goods along the lane.
function crate(x, z, size = 0.8) {
box(size, size * 0.7, size, x, size * 0.36, z, materials.wood);
box(size * 0.72, size * 0.54, size * 0.72, x, size * 0.36, z, materials.sandDark);
box(size + 0.04, 0.08, size + 0.04, x, size * 0.73, z, materials.darkWood);
}
crate(-1.8, -10.8, 0.78);
crate(-1.1, -10.9, 0.66);
crate(-1.6, -9.8, 0.62);
crate(-6.4, 9.7, 0.8);
crate(3.2, -5.7, 0.74);
// ------------------------------------------------------------
// Corner tower, stepped roof, and distant city blocks
// ------------------------------------------------------------
const towerX = 15.15;
const towerZ = -10.9;
box(2.45, 5.7, 2.45, towerX, 2.85, towerZ, materials.sand);
box(2.75, 0.28, 2.75, towerX, 5.74, towerZ, materials.sandLight);
box(1.95, 1.8, 1.95, towerX, 6.78, towerZ, materials.plaster);
box(2.28, 0.24, 2.28, towerX, 7.79, towerZ, materials.trim);
for (const dx of [-0.82, 0, 0.82]) {
for (const dz of [-0.82, 0.82]) {
box(0.4, 0.48, 0.4, towerX + dx, 8.14, towerZ + dz, materials.sandLight);
}
}
addLantern(towerX, 8.75, towerZ, false);
// Stepped voxel roof behind the arcade.
for (let level = 0; level < 6; level++) {
const width = 4.3 - level * 0.58;
box(
width,
0.34,
width,
12.1,
6.55 + level * 0.31,
-10.95,
level % 2 ? materials.teal : materials.sandLight
);
}
box(0.24, 0.9, 0.24, 12.1, 8.5, -10.95, materials.brass);
box(0.5, 0.25, 0.5, 12.1, 8.98, -10.95, materials.brass);
const distantMaterial = standard(0x6f665d);
const distantRoof = standard(0x4f514f);
for (let i = 0; i < 13; i++) {
const x = 19 + random() * 8;
const z = -18 + random() * 36;
const height = 3.5 + random() * 6;
const width = 1.6 + random() * 2.2;
const depth = 1.6 + random() * 2.2;
box(width, height, depth, x, height / 2 - 0.1, z, distantMaterial);
box(width + 0.2, 0.25, depth + 0.2, x, height, z, distantRoof);
box(width * 0.42, 0.55, depth * 0.4, x, height + 0.35, z, distantRoof);
}
// ------------------------------------------------------------
// Courtyard string lights
// ------------------------------------------------------------
const lightPoles = [
[4.1, -10.7],
[12.7, -10.7],
[15.3, 3.6],
[4.1, 9.8]
];
lightPoles.forEach(([x, z]) => {
box(0.18, 5.7, 0.18, x, 2.85, z, materials.darkWood);
box(0.55, 0.12, 0.55, x, 5.65, z, materials.brass);
});
box(8.65, 0.065, 0.065, 8.4, 5.43, -10.7, materials.darkWood);
box(0.065, 0.065, 14.35, 15.3, 5.43, -3.55, materials.darkWood);
box(11.25, 0.065, 0.065, 9.7, 5.43, 9.8, materials.darkWood);
for (let z = -9.8; z <= 9.5; z += 2.3) {
addLantern(4.1 + (z + 10.7) / 19.4 * 8.65, 5.05, z);
}
for (let x = 5; x <= 15; x += 2.4) {
addLantern(x, 5.05, -10.7);
}
// ------------------------------------------------------------
// Camera and direct controls
// ------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(2.0, 2.1, 0),
radius: 35,
theta: 0.48,
phi: 0.82
};
const views = {
junction: {
target: [1.7, 2.15, 0],
radius: 29,
theta: 0.48,
phi: 0.92
},
lane: {
target: [-4.2, 2.1, -4.4],
radius: 18,
theta: 3.0,
phi: 1.18
},
courtyard: {
target: [9.1, 1.7, 1.0],
radius: 23,
theta: 0.78,
phi: 0.94
},
overview: {
target: [2.2, 0.8, 0],
radius: 45,
theta: 0.46,
phi: 0.57
}
};
let cameraTween = null;
let autoOrbit = false;
function shortestAngle(from, to) {
let delta = (to - from + Math.PI) % (Math.PI * 2);
if (delta < 0) delta += Math.PI * 2;
return delta - Math.PI;
}
function flyTo(name, instant = false) {
const view = views[name];
if (!view) return;
document.querySelectorAll("[data-view]").forEach(button => {
button.classList.toggle("active", button.dataset.view === name);
});
const destination = {
target: new THREE.Vector3(...view.target),
radius: view.radius,
theta: cameraState.theta + shortestAngle(cameraState.theta, view.theta),
phi: view.phi
};
if (instant) {
cameraState.target.copy(destination.target);
cameraState.radius = destination.radius;
cameraState.theta = destination.theta;
cameraState.phi = destination.phi;
cameraTween = null;
return;
}
cameraTween = {
elapsed: 0,
duration: 1.2,
from: {
target: cameraState.target.clone(),
radius: cameraState.radius,
theta: cameraState.theta,
phi: cameraState.phi
},
to: destination
};
}
document.querySelectorAll("[data-view]").forEach(button => {
button.addEventListener("click", () => flyTo(button.dataset.view));
});
const orbitButton = document.querySelector("#orbitButton");
orbitButton.addEventListener("click", () => {
autoOrbit = !autoOrbit;
orbitButton.classList.toggle("active", autoOrbit);
orbitButton.setAttribute("aria-pressed", String(autoOrbit));
if (autoOrbit) cameraTween = null;
});
let dragging = null;
renderer.domElement.addEventListener("pointerdown", event => {
cameraTween = null;
dragging = {
id: event.pointerId,
x: event.clientX,
y: event.clientY,
pan: event.button === 2 || event.shiftKey
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging || dragging.id !== event.pointerId) return;
const dx = event.clientX - dragging.x;
const dy = event.clientY - dragging.y;
dragging.x = event.clientX;
dragging.y = event.clientY;
if (dragging.pan) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const scale = cameraState.radius * 0.00135;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(camera.up, dy * scale);
} else {
cameraState.theta -= dx * 0.0052;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0043,
0.3,
1.46
);
}
});
function stopDragging(event) {
if (dragging && dragging.id === event.pointerId) {
dragging = null;
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraTween = null;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
11,
62
);
},
{ passive: false }
);
// Simple two-finger pinch support.
const activePointers = new Map();
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (pinchDistance / distance),
11,
62
);
}
pinchDistance = distance;
}
});
renderer.domElement.addEventListener("pointerup", event => {
activePointers.delete(event.pointerId);
pinchDistance = 0;
});
// ------------------------------------------------------------
// Day/night lighting
// ------------------------------------------------------------
let dayMode = false;
const moodButton = document.querySelector("#moodButton");
function setMood(day) {
dayMode = day;
if (day) {
scene.background.setHex(0xa8c5d0);
scene.fog.color.setHex(0xa8c5d0);
scene.fog.near = 42;
scene.fog.far = 95;
hemisphere.intensity = 1.35;
hemisphere.color.setHex(0xd9edf2);
hemisphere.groundColor.setHex(0x8e6545);
sun.intensity = 3.2;
fill.intensity = 0.8;
renderer.toneMappingExposure = 1.02;
materials.lantern.emissiveIntensity = 0.65;
pointLights.forEach(light => light.intensity *= 0.3);
} else {
scene.background.setHex(0x171a24);
scene.fog.color.setHex(0x171a24);
scene.fog.near = 34;
scene.fog.far = 78;
hemisphere.intensity = 0.78;
hemisphere.color.setHex(0xa8c8dc);
hemisphere.groundColor.setHex(0x68452f);
sun.intensity = 2.1;
fill.intensity = 0.65;
renderer.toneMappingExposure = 1.08;
materials.lantern.emissiveIntensity = 2.3;
pointLights.forEach((light, index) => {
light.intensity = [48, 38, 28, 22, 22][index % 5];
});
}
moodButton.textContent = `Light · ${day ? "Day" : "Night"}`;
}
moodButton.addEventListener("click", () => setMood(!dayMode));
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const pinAnchors = {
lane: new THREE.Vector3(-4.3, 3.8, -6.5),
arch: new THREE.Vector3(0.65, 5.2, 0),
fountain: new THREE.Vector3(8.5, 3.5, 1),
court: new THREE.Vector3(12.4, 1.1, 5.4)
};
const pins = [...document.querySelectorAll(".pin")];
const projected = new THREE.Vector3();
function updatePins() {
const width = stage.clientWidth;
const height = stage.clientHeight;
pins.forEach(pin => {
const anchor = pinAnchors[pin.dataset.pin];
projected.copy(anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.08 &&
Math.abs(projected.y) < 1.08;
pin.style.display = visible ? "block" : "none";
if (visible) {
pin.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
pin.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
});
}
// ------------------------------------------------------------
// Render loop
// ------------------------------------------------------------
const clock = new THREE.Clock();
function updateCamera() {
if (cameraTween) {
cameraTween.elapsed += clock.getDelta();
const progress = Math.min(
cameraTween.elapsed / cameraTween.duration,
1
);
const eased =
progress < 0.5
? 4 * progress * progress * progress
: 1 - Math.pow(-2 * progress + 2, 3) / 2;
cameraState.target.lerpVectors(
cameraTween.from.target,
cameraTween.to.target,
eased
);
cameraState.radius = THREE.MathUtils.lerp(
cameraTween.from.radius,
cameraTween.to.radius,
eased
);
cameraState.theta = THREE.MathUtils.lerp(
cameraTween.from.theta,
cameraTween.to.theta,
eased
);
cameraState.phi = THREE.MathUtils.lerp(
cameraTween.from.phi,
cameraTween.to.phi,
eased
);
if (progress >= 1) cameraTween = null;
} else if (autoOrbit) {
cameraState.theta += 0.075 * clock.getDelta();
}
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);
}
function animate() {
requestAnimationFrame(animate);
updateCamera();
updatePins();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
});
flyTo("junction", true);
updateCamera();
renderer.render(scene, camera);
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 600);
});
</script>
</body>
</html>
```