Prompt specification
Build or customize the Three.js 3D scene "Mombasa Old Town lane toward the harbor" 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: Mombasa Old Town lane toward the harbor
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/mombasa-old-town-lane-toward-the-harbor-x2943
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring Mombasa Old Town's lanes and harbor approaches. Build a bounded low-poly scene with referenced facade forms, narrow shaded streets, and a partial harbor view. Preserve local architectural distinctions instead of blending unrelated coastal traditions. Low-poly.
Working Three.js Starter (mombasa-old-town-lane-toward-the-harbor-x2943):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Mombasa Old Town — Lanes & Harbour Approaches</title>
<style>
:root {
color-scheme: dark;
--ink: #f7f0df;
--muted: #c8c1b0;
--panel: rgba(24, 31, 30, 0.84);
--line: rgba(255, 255, 255, 0.15);
--accent: #e5ad66;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8cece;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
color: var(--ink);
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
#title {
top: 20px;
left: 20px;
width: min(430px, calc(100vw - 40px));
padding: 17px 19px 16px;
border-radius: 4px;
}
.eyebrow {
margin-bottom: 5px;
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(23px, 3vw, 35px);
font-weight: 500;
line-height: 1.05;
letter-spacing: -0.025em;
}
#title p {
margin: 9px 0 0;
max-width: 360px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
#views {
top: 20px;
right: 20px;
display: flex;
gap: 6px;
padding: 6px;
border-radius: 4px;
}
button {
appearance: none;
border: 1px solid transparent;
border-radius: 3px;
padding: 9px 11px;
color: #ded9cb;
background: transparent;
font: inherit;
font-size: 11px;
font-weight: 650;
cursor: pointer;
transition: background 140ms ease, color 140ms ease;
}
button:hover,
button.active {
color: #fff7e7;
background: rgba(229, 173, 102, 0.18);
border-color: rgba(229, 173, 102, 0.35);
}
#legend {
bottom: 20px;
left: 20px;
width: min(330px, calc(100vw - 40px));
padding: 15px 17px;
border-radius: 4px;
}
#legend .heading {
margin-bottom: 11px;
color: var(--accent);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 13px 1fr;
gap: 10px;
align-items: start;
margin: 9px 0;
color: #ddd7c9;
font-size: 11px;
line-height: 1.35;
}
.swatch {
width: 11px;
height: 11px;
margin-top: 2px;
border: 1px solid rgba(255, 255, 255, 0.25);
background: var(--swatch);
}
.legend-row strong {
display: block;
color: #fff8e9;
font-size: 11px;
font-weight: 650;
}
.legend-row span {
color: #aaa99f;
}
#help {
right: 20px;
bottom: 20px;
padding: 10px 13px;
border-radius: 4px;
color: #c6c4ba;
font-size: 10px;
letter-spacing: 0.03em;
}
#help b { color: #f5ead4; }
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
color: #f5ead4;
background: #7d9999;
font-size: 12px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
#title {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#title p { display: none; }
#views {
top: 112px;
right: 12px;
}
button { padding: 8px 9px; font-size: 10px; }
#legend {
left: 12px;
bottom: 12px;
width: 245px;
padding: 12px;
}
#legend .legend-row:nth-of-type(n+4) { display: none; }
#help { display: none; }
}
</style>
</head>
<body>
<div id="loading">Composing the old town…</div>
<section id="title" class="panel">
<div class="eyebrow">Kenya · Swahili Coast</div>
<h1>Mombasa Old Town</h1>
<p>
An interpretive low-poly study of coral-stone lanes, shaded merchant
façades, Fort Jesus, and the working harbour approaches.
</p>
</section>
<nav id="views" class="panel" aria-label="Camera views">
<button data-view="town">Old Town</button>
<button data-view="fort">Fort Jesus</button>
<button data-view="harbor">Harbour</button>
<button data-view="aerial">Aerial</button>
</nav>
<aside id="legend" class="panel">
<div class="heading">Referenced coastal forms</div>
<div class="legend-row">
<i class="swatch" style="--swatch:#c88b68"></i>
<div><strong>Swahili coral house</strong><span>Thick masonry, small openings, parapets and timber screens.</span></div>
</div>
<div class="legend-row">
<i class="swatch" style="--swatch:#c99d5d"></i>
<div><strong>Indian Ocean merchant front</strong><span>Tall narrow bays, projecting mashrabiya and shaded shopfronts.</span></div>
</div>
<div class="legend-row">
<i class="swatch" style="--swatch:#d9d3bf"></i>
<div><strong>Portuguese colonial fabric</strong><span>Arcaded stores and the four-bastioned Fort Jesus enceinte.</span></div>
</div>
<div class="legend-row">
<i class="swatch" style="--swatch:#6f8d89"></i>
<div><strong>Harbour approaches</strong><span>Quays, dhows, modern cargo traffic and a breakwater.</span></div>
</div>
</aside>
<div id="help" class="panel">
<b>Drag</b> orbit · <b>Wheel / pinch</b> zoom · <b>Shift-drag / right-drag</b> pan · <b>R</b> reset
</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
// -------------------------------------------------------------------------
// Renderer, camera and deterministic scene palette
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8cece);
scene.fog = new THREE.Fog(0xb8cece, 105, 190);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
260
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.appendChild(renderer.domElement);
const clock = new THREE.Clock();
const seeded = (() => {
let state = 481516;
return () => {
state |= 0;
state = (state + 0x6D2B79F5) | 0;
let t = Math.imul(state ^ (state >>> 15), 1 | state);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
})();
const random = (a, b) => a + seeded() * (b - a);
const choose = array => array[Math.floor(seeded() * array.length)];
const standard = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.9,
metalness: 0,
flatShading: true,
...options
});
const materials = {
base: standard(0x55504a),
land: standard(0xc2a981),
townGround: standard(0xb99e78),
road: standard(0x746f64),
curb: standard(0x9e8b70),
coral: [
standard(0xc88966),
standard(0xd29d77),
standard(0xb97559),
standard(0xd8aa80)
],
lime: [
standard(0xe4d3ad),
standard(0xd6c29a),
standard(0xeadbb8)
],
ochre: [
standard(0xc59a5d),
standard(0xb7844f),
standard(0xd0aa6c)
],
merchant: [
standard(0xc49c60),
standard(0xb66c50),
standard(0xd2b076)
],
warehouse: [
standard(0xa96b50),
standard(0xb77a57),
standard(0x98604b)
],
colonial: standard(0xd8d2bd),
stone: standard(0xbd9873),
fort: standard(0xb88e69),
fortDark: standard(0x9e7355),
wood: standard(0x593b2b),
darkWood: standard(0x382b24),
window: standard(0x23383a, { roughness: 0.48 }),
shutter: standard(0x315848),
metal: standard(0x414c4a, { roughness: 0.58, metalness: 0.35 }),
iron: standard(0x2d3938),
roofRed: standard(0x8e4d3c),
roofGreen: standard(0x59645c),
canvasCream: standard(0xe8d9b7),
canvasRed: standard(0xa84f3d),
canvasGreen: standard(0x567568),
canvasBlue: standard(0x4d6f7a),
yellow: standard(0xd7a24c),
leaf: [
standard(0x3d6848),
standard(0x52784c),
standard(0x6e8450)
],
trunk: standard(0x65503a),
skin: standard(0x8b6045),
white: standard(0xe8e4d7),
red: standard(0xa54032),
blue: standard(0x365e79),
ochreCloth: standard(0xc9933f)
};
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(rt, rb, h, segments, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(rt, rb, h, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(a, b, width, material, parent = scene) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, direction.length(), width),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function archGeometry(width, height) {
const radius = width * 0.5;
const spring = height - radius;
const shape = new THREE.Shape();
shape.moveTo(-radius, 0);
shape.lineTo(radius, 0);
shape.lineTo(radius, spring);
shape.absarc(0, spring, radius, 0, Math.PI, false);
shape.lineTo(-radius, 0);
return new THREE.ShapeGeometry(shape);
}
function arch(width, height, material, x, y, z, parent) {
const mesh = new THREE.Mesh(archGeometry(width, height), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Lighting and the bounded diorama base
// -------------------------------------------------------------------------
const hemi = new THREE.HemisphereLight(0xd9edf0, 0x756049, 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe2b0, 3.25);
sun.position.set(-48, 78, 38);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -72;
sun.shadow.camera.right = 72;
sun.shadow.camera.top = 62;
sun.shadow.camera.bottom = -62;
sun.shadow.camera.near = 10;
sun.shadow.camera.far = 180;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
box(110, 2.1, 92, materials.base, 0, -1.35, 0);
box(76, 1.1, 90, materials.land, -16, 0.25, 0);
box(69, 0.12, 65, materials.townGround, -15.5, 0.77, 8);
// Low-poly harbour water.
const waterGeometry = new THREE.PlaneGeometry(30, 90, 20, 40);
waterGeometry.rotateX(-Math.PI / 2);
const waterPosition = waterGeometry.attributes.position;
const waterColors = [];
const waterColor = new THREE.Color();
for (let i = 0; i < waterPosition.count; i++) {
const x = waterPosition.getX(i);
const z = waterPosition.getZ(i);
const variation = Math.sin(x * 0.7 + z * 0.13) * 0.5 + 0.5;
waterColor.setHSL(0.52, 0.27, 0.35 + variation * 0.055);
waterColors.push(waterColor.r, waterColor.g, waterColor.b);
waterPosition.setY(i, 0);
}
waterGeometry.setAttribute(
'color',
new THREE.Float32BufferAttribute(waterColors, 3)
);
const water = new THREE.Mesh(
waterGeometry,
new THREE.MeshPhongMaterial({
vertexColors: true,
shininess: 65,
flatShading: true
})
);
water.position.set(38, 0.43, 0);
water.receiveShadow = true;
scene.add(water);
// Stone quay and retaining wall.
box(4.1, 0.65, 90, materials.curb, 20.55, 0.85, 0);
box(1.0, 1.65, 90, materials.stone, 22.7, 0.48, 0);
box(0.24, 0.22, 90, materials.wood, 22.05, 1.27, 0);
// -------------------------------------------------------------------------
// Narrow street network
// -------------------------------------------------------------------------
const roads = [
{ w: 3.2, d: 82, x: -36, z: -2 },
{ w: 4.4, d: 82, x: -15, z: -2 },
{ w: 3.4, d: 82, x: 2, z: -2 },
{ w: 2.4, d: 64, x: 19.1, z: 7 },
{ w: 69, d: 3.8, x: -15.5, z: -23 },
{ w: 69, d: 4.0, x: -15.5, z: 4 },
{ w: 69, d: 4.0, x: -15.5, z: 27 }
];
roads.forEach(r => {
box(r.w, 0.07, r.d, materials.road, r.x, 0.84, r.z);
});
// Drainage lines and irregular paving bands.
for (const z of [-23, 4, 27]) {
box(68, 0.055, 0.16, materials.curb, -15.5, 0.9, z + 1.35);
}
for (let z = -38; z < 40; z += 5.8) {
box(0.12, 0.055, 1.2, materials.curb, -16.75, 0.9, z);
box(0.12, 0.055, 1.0, materials.curb, -13.25, 0.9, z + 1.1);
}
// -------------------------------------------------------------------------
// Shop signs, windows and local façade types
// -------------------------------------------------------------------------
const shopNames = [
'S. A. MOHAMMED & CO.',
'MOMBASA CURIOS',
'HIDES & TRADE',
'THE OLD HARBOR',
'EAST AFRICA GOODS',
'S. J. PATEL',
'CARGO & GENERAL'
];
function signTexture(text, background = '#34433f', foreground = '#f3e3bd') {
const canvas = document.createElement('canvas');
canvas.width = 512;
canvas.height = 96;
const ctx = canvas.getContext('2d');
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = 'rgba(232,196,129,.55)';
ctx.lineWidth = 4;
ctx.strokeRect(5, 5, canvas.width - 10, canvas.height - 10);
ctx.fillStyle = foreground;
ctx.font = '600 29px Georgia, serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 1, canvas.width - 34);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function addWindow(group, x, y, front, width = 0.92, height = 1.22, style) {
box(width + 0.24, height + 0.24, 0.10, materials.wood, x, y, front + 0.035, group);
box(width, height, 0.07, materials.window, x, y, front + 0.095, group);
box(width + 0.38, 0.13, 0.13, materials.stone, x, y + height / 2 + 0.13, front + 0.07, group);
if (style === 'swahili' || style === 'merchant') {
for (let i = -1; i <= 1; i++) {
box(0.035, height * 0.86, 0.04, materials.wood,
x + i * width * 0.26, y, front + 0.145, group);
}
}
if (style === 'colonial') {
box(width * 0.55, height * 0.92, 0.055, materials.shutter,
x - width * 0.72, y, front + 0.11, group);
box(width * 0.55, height * 0.92, 0.055, materials.shutter,
x + width * 0.72, y, front + 0.11, group);
}
}
function addAwning(group, width, front, color) {
const stripeWidth = width / 8;
for (let i = 0; i < 8; i++) {
const strip = box(stripeWidth, 0.08, 1.15,
i % 2 ? materials.canvasCream : color,
-width / 2 + stripeWidth * (i + 0.5),
2.78,
front + 0.52,
group
);
strip.rotation.x = -0.11;
}
box(width, 0.27, 0.08, color, 0, 2.57, front + 1.08, group);
}
function addSwahiliBalcony(group, width, front) {
const balconyWidth = Math.min(width * 0.72, 4.3);
box(balconyWidth, 0.16, 0.92, materials.wood, 0, 3.22, front + 0.39, group);
box(balconyWidth, 0.10, 0.10, materials.darkWood, 0, 4.12, front + 0.82, group);
box(balconyWidth, 0.10, 0.10, materials.darkWood, 0, 3.58, front + 0.82, group);
const slats = Math.max(5, Math.floor(balconyWidth / 0.36));
for (let i = 0; i <= slats; i++) {
const x = -balconyWidth / 2 + (balconyWidth / slats) * i;
box(0.055, 0.80, 0.055, materials.wood, x, 3.69, front + 0.82, group);
}
beam(
[-balconyWidth * 0.42, 2.65, front + 0.06],
[-balconyWidth * 0.31, 3.17, front + 0.73],
0.11, materials.wood, group
);
beam(
[balconyWidth * 0.42, 2.65, front + 0.06],
[balconyWidth * 0.31, 3.17, front + 0.73],
0.11, materials.wood, group
);
}
function addMerchantBay(group, width, front, height) {
const bayWidth = Math.min(width * 0.74, 4.1);
const bayHeight = 1.55;
const bayY = Math.min(4.65, height - 2.25);
box(bayWidth, bayHeight, 0.82, materials.wood, 0, bayY, front + 0.34, group);
box(bayWidth - 0.22, bayHeight - 0.22, 0.06, materials.window,
0, bayY, front + 0.78, group);
const bars = Math.max(6, Math.floor(bayWidth / 0.25));
for (let i = 0; i <= bars; i++) {
const x = -bayWidth / 2 + 0.13 + ((bayWidth - 0.26) / bars) * i;
box(0.045, bayHeight - 0.12, 0.06, materials.wood,
x, bayY, front + 0.84, group);
}
box(bayWidth + 0.35, 0.14, 1.02, materials.darkWood,
0, bayY + bayHeight / 2 + 0.12, front + 0.38, group);
}
function addColonialVeranda(group, width, front) {
const verandaWidth = Math.min(width * 0.83, 5.4);
box(verandaWidth, 0.16, 1.12, materials.stone, 0, 0.20, front + 0.48, group);
box(verandaWidth + 0.3, 0.14, 1.3, materials.white, 0, 2.88, front + 0.56, group);
const columns = Math.max(3, Math.round(verandaWidth / 1.4));
for (let i = 0; i <= columns; i++) {
const x = -verandaWidth / 2 + (verandaWidth / columns) * i;
cylinder(0.075, 0.095, 2.6, 6, materials.white, x, 1.5, front + 0.92, group);
}
}
function addFlatRoof(group, width, depth, height, style) {
box(width, 0.13, depth, materials.roofGreen, 0, height + 0.06, 0, group);
box(width, 0.52, 0.18, materials.stone, 0, height + 0.28, depth / 2, group);
box(width, 0.52, 0.18, materials.stone, 0, height + 0.28, -depth / 2, group);
box(0.18, 0.52, depth, materials.stone, width / 2, height + 0.28, 0, group);
box(0.18, 0.52, depth, materials.stone, -width / 2, height + 0.28, 0, group);
if (style === 'swahili' && seeded() > 0.48) {
box(1.1, 0.75, 0.85, materials.metal,
random(-width * 0.25, width * 0.25), height + 0.48, random(-depth * 0.2, depth * 0.2), group);
}
}
function addPitchedRoof(group, width, depth, height) {
const rise = 1.75;
const half = width / 2 + 0.28;
const slopeLength = Math.hypot(half, rise);
const angle = Math.atan2(rise, half);
const left = box(slopeLength, 0.18, depth + 0.55, materials.roofRed,
-width / 4, height + rise / 2, 0, group);
left.rotation.z = angle;
const right = box(slopeLength, 0.18, depth + 0.55, materials.roofRed,
width / 4, height + rise / 2, 0, group);
right.rotation.z = -angle;
box(0.17, 0.17, depth + 0.62, materials.metal,
0, height + rise, 0, group);
for (let i = -2; i <= 2; i++) {
const ridge = box(0.045, 0.055, depth + 0.38, materials.roofGreen,
i * width * 0.16, height + rise - Math.abs(i) * 0.37, 0, group);
ridge.rotation.z = i < 0 ? angle : -angle;
}
}
function createBuilding(width, depth, height, style, x, z, rotation) {
const group = new THREE.Group();
group.position.set(x, 0.88, z);
group.rotation.y = rotation;
scene.add(group);
let bodyMaterial;
if (style === 'swahili') bodyMaterial = choose(materials.coral);
else if (style === 'merchant') bodyMaterial = choose(materials.merchant);
else if (style === 'warehouse') bodyMaterial = choose(materials.warehouse);
else if (style === 'colonial') bodyMaterial = materials.colonial;
else bodyMaterial = choose(materials.lime);
box(width, height, depth, bodyMaterial, 0, height / 2, 0, group);
box(width + 0.13, 0.42, depth + 0.13, materials.stone, 0, 0.21, 0, group);
if (style === 'swahili') {
box(width + 0.04, 0.12, depth + 0.04, materials.stone, 0, 2.74, 0, group);
if (height > 8) {
box(width + 0.04, 0.10, depth + 0.04, materials.stone, 0, height * 0.56, 0, group);
}
}
const front = depth / 2;
const doorX = random(-width * 0.12, width * 0.12);
arch(1.08, 2.18, materials.wood, doorX, 0.43, front + 0.035, group);
box(1.37, 0.15, 0.18, materials.stone, doorX, 2.48, front + 0.07, group);
const sideWindowOffset = Math.max(1.35, width * 0.29);
if (width > 3.3) {
addWindow(group, doorX - sideWindowOffset, 1.52, front, 0.82, 1.10, style);
addWindow(group, doorX + sideWindowOffset, 1.52, front, 0.82, 1.10, style);
}
const upperLevels = [];
for (let level = 3.85; level < height - 0.65; level += 2.65) {
upperLevels.push(level);
}
upperLevels.forEach((level, levelIndex) => {
let count = width > 5.0 ? 3 : 2;
if (style === 'merchant' && levelIndex === upperLevels.length - 1) count = 1;
for (let i = 0; i < count; i++) {
const x = count === 1 ? 0 : -width * 0.30 + (width * 0.60 / (count - 1)) * i;
addWindow(group, x, level, front, 0.88, 1.22, style);
}
});
if (style === 'swahili' && height > 7) {
addSwahiliBalcony(group, width, front);
}
if (style === 'merchant') {
addMerchantBay(group, width, front, height);
}
if (style === 'colonial') {
addColonialVeranda(group, width, front);
}
if (style === 'warehouse') {
arch(1.8, 2.45, materials.darkWood, 0, 0.44, front + 0.05, group);
box(2.15, 0.16, 0.18, materials.stone, 0, 2.84, front + 0.07, group);
}
if (style !== 'colonial' && seeded() > 0.30) {
const awningColor = choose([
materials.canvasRed,
materials.canvasGreen,
materials.canvasBlue
]);
addAwning(group, Math.min(width * 0.78, 4.6), front, awningColor);
}
if (seeded() > 0.59) {
const signWidth = Math.min(width * 0.73, 4.2);
const texture = signTexture(choose(shopNames));
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(signWidth, signWidth * 0.1875),
new THREE.MeshBasicMaterial({ map: texture })
);
sign.position.set(0, 3.16, front + 0.12);
group.add(sign);
}
if (style === 'warehouse') {
addPitchedRoof(group, width, depth, height);
} else {
addFlatRoof(group, width, depth, height, style);
}
return group;
}
// -------------------------------------------------------------------------
// Perimeter blocks: narrow shaded lanes and small internal courts
// -------------------------------------------------------------------------
function splitRange(start, end, minimum = 4.2, maximum = 6.6) {
const result = [];
let cursor = start;
while (end - cursor > minimum + 0.5) {
let piece = random(minimum, maximum);
if (end - cursor - piece < minimum) piece = end - cursor;
result.push([cursor, cursor + piece]);
cursor += piece + random(0.16, 0.32);
}
return result;
}
function randomStyle() {
const r = seeded();
if (r < 0.40) return 'swahili';
if (r < 0.66) return 'merchant';
if (r < 0.83) return 'warehouse';
return 'colonial';
}
function populateBlock(x1, x2, z1, z2, index) {
const depth = 4.45;
// Buildings facing the north–south lanes.
splitRange(z1 + 0.6, z2 - 0.6).forEach(([a, b], i) => {
const w = b - a;
const h = random(6.3, 11.8);
createBuilding(w, depth, h, randomStyle(),
x1 + depth / 2, (a + b) / 2, -Math.PI / 2);
createBuilding(w, depth, h + random(-0.6, 1.2), randomStyle(),
x2 - depth / 2, (a + b) / 2, Math.PI / 2);
});
// Wider blocks receive one deeper frontage on each cross lane.
if (x2 - x1 > 14.4) {
const start = x1 + depth + 0.35;
const end = x2 - depth - 0.35;
splitRange(start, end, 4.6, 6.1).forEach(([a, b]) => {
const w = b - a;
createBuilding(w, depth, random(7, 12.5), randomStyle(),
(a + b) / 2, z1 + depth / 2, Math.PI);
createBuilding(w, depth, random(7, 12.5), randomStyle(),
(a + b) / 2, z2 - depth / 2, 0);
});
}
}
const blocks = [
[-49, -38, -20, 2],
[-34, -18, -20, 2],
[-12, 0, -20, 2],
[4, 18, -20, 2],
[-49, -38, 8, 25],
[-34, -18, 8, 25],
[-12, 0, 8, 25],
[4, 18, 8, 25],
[-49, -38, 31, 39],
[-34, -18, 31, 39],
[-12, 0, 31, 39],
[4, 18, 31, 39]
];
blocks.forEach((b, i) => populateBlock(...b, i));
// -------------------------------------------------------------------------
// Fort Jesus: Portuguese bastioned enceinte near the harbour
// -------------------------------------------------------------------------
const fort = new THREE.Group();
fort.position.set(3, 0.88, -35);
scene.add(fort);
const fortW = 26;
const fortD = 18;
const wallHeight = 4.1;
// North wall leaves a central entrance.
box(10.8, wallHeight, 0.95, materials.fort, -7.55, wallHeight / 2, fortD / 2, fort);
box(10.8, wallHeight, 0.95, materials.fort, 7.55, wallHeight / 2, fortD / 2, fort);
box(0.95, wallHeight, fortD, materials.fort, -fortW / 2, wallHeight / 2, 0, fort);
box(0.95, wallHeight, fortD, materials.fort, fortW / 2, wallHeight / 2, 0, fort);
box(fortW, wallHeight, 0.95, materials.fort, 0, wallHeight / 2, -fortD / 2, fort);
for (const x of [-13, 13]) {
for (const z of [-9, 9]) {
const bastion = cylinder(3.35, 4.05, 4.8, 8, materials.fort,
x, 2.4, z, fort);
bastion.rotation.y = Math.PI / 8;
const cap = cylinder(3.55, 3.35, 0.42, 8, materials.fortDark,
x, 4.95, z, fort);
cap.rotation.y = Math.PI / 8;
}
}
for (let x = -11.5; x <= 11.5; x += 2.1) {
if (Math.abs(x) > 2.6) {
box(0.85, 0.7, 0.95, materials.fort, x, wallHeight + 0.3, fortD / 2, fort);
box(0.85, 0.7, 0.95, materials.fort, x, wallHeight + 0.3, -fortD / 2, fort);
}
}
for (let z = -7.2; z <= 7.2; z += 2.0) {
box(0.95, 0.7, 0.85, materials.fort, -fortW / 2, wallHeight + 0.3, z, fort);
box(0.95, 0.7, 0.85, materials.fort, fortW / 2, wallHeight + 0.3, z, fort);
}
arch(3.3, 3.6, materials.darkWood, 0, 0.05, fortD / 2 + 0.50, fort);
box(6.9, 4.6, 5.7, materials.fort, 0, 2.3, -1.6, fort);
box(7.4, 0.35, 6.2, materials.stone, 0, 4.75, -1.6, fort);
box(3.0, 2.25, 0.12, materials.darkWood, 0, 1.35, 1.29, fort);
// Small cannon on the southern battery.
const cannon = cylinder(0.16, 0.24, 1.55, 8, materials.iron,
4.8, 2.1, -7.8, fort);
cannon.rotation.x = Math.PI / 2;
// Fort flag.
cylinder(0.035, 0.055, 5.4, 6, materials.metal, 0, 7.4, -1.6, fort);
const flag = box(1.4, 0.75, 0.035, materials.red, 0.72, 9.55, -1.6, fort);
// -------------------------------------------------------------------------
// Trees, people, street furniture and overhead wires
// -------------------------------------------------------------------------
function tree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.9, z);
group.scale.setScalar(scale);
scene.add(group);
cylinder(0.18, 0.32, 3.4, 6, materials.trunk, 0, 1.7, 0, group);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.8, 1),
choose(materials.leaf)
);
crown.position.y = 3.6;
crown.scale.set(1.15, 0.82, 1.0);
crown.castShadow = true;
group.add(crown);
const crown2 = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.15, 0),
choose(materials.leaf)
);
crown2.position.set(0.85, 3.1, 0.2);
crown2.castShadow = true;
group.add(crown2);
}
tree(-27.7, -11.2, 0.82);
tree(-26.9, 16.0, 0.78);
tree(-5.5, 15.9, 0.72);
tree(-7.4, -34.8, 0.88);
tree(17.7, 33.0, 0.83);
function person(x, z, rotation = 0, cloth = materials.blue) {
const group = new THREE.Group();
group.position.set(x, 0.92, z);
group.rotation.y = rotation;
scene.add(group);
cylinder(0.17, 0.24, 0.78, 6, cloth, 0, 1.05, 0, group);
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.18, 6, 4),
materials.skin
);
head.position.y = 1.62;
head.castShadow = true;
group.add(head);
box(0.11, 0.55, 0.13, materials.darkWood, -0.10, 0.30, 0, group);
box(0.11, 0.55, 0.13, materials.darkWood, 0.10, 0.30, 0, group);
}
const people = [
[-15.8, -15, 0.5, materials.blue],
[-14.2, -8.3, -1.2, materials.ochreCloth],
[-16.0, 7.7, 2.4, materials.canvasRed],
[-13.9, 21.3, 0.4, materials.white],
[-36.9, 11.0, 1.1, materials.blue],
[1.1, -18.0, 2.0, materials.ochreCloth],
[3.0, 12.8, -0.8, materials.white],
[20.2, 15.3, 1.6, materials.canvasGreen],
[20.4, 28.8, -1.0, materials.blue],
[19.6, -8.7, 2.7, materials.white]
];
people.forEach(p => person(...p));
function streetLamp(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.9, z);
group.rotation.y = rotation;
scene.add(group);
cylinder(0.055, 0.09, 3.7, 6, materials.iron, 0, 1.85, 0, group);
box(0.48, 0.55, 0.48, materials.yellow, 0, 3.8, 0, group);
box(0.58, 0.10, 0.58, materials.iron, 0, 4.11, 0, group);
}
streetLamp(-16.7, -12, 0);
streetLamp(-13.3, 17, 0);
streetLamp(0.3, 5.5, 0);
streetLamp(-34.4, 26, 0);
streetLamp(18.0, 22, 0);
function overheadWire(x1, x2, y, z) {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(x1, y, z),
new THREE.Vector3((x1 + x2) / 2, y - 0.75, z),
new THREE.Vector3(x2, y, z)
]);
const wire = new THREE.Mesh(
new THREE.TubeGeometry(curve, 10, 0.025, 4, false),
materials.iron
);
scene.add(wire);
}
overheadWire(-17.2, -12.8, 8.2, -10);
overheadWire(-17.2, -12.8, 8.6, 5.5);
overheadWire(-17.2, -12.8, 8.4, 21.5);
// -------------------------------------------------------------------------
// Harbour: dhow, cargo ship, quay cranes, containers and breakwater
// -------------------------------------------------------------------------
const hullShape = new THREE.Shape();
hullShape.moveTo(-3.3, -1.0);
hullShape.lineTo(2.8, -0.85);
hullShape.lineTo(4.0, 0);
hullShape.lineTo(2.8, 0.85);
hullShape.lineTo(-3.3, 1.0);
hullShape.lineTo(-3.9, 0.55);
hullShape.lineTo(-3.9, -0.55);
hullShape.closePath();
const dhow = new THREE.Group();
dhow.position.set(29.5, 0.55, 20.5);
dhow.rotation.y = -0.28;
scene.add(dhow);
const hullGeo = new THREE.ExtrudeGeometry(hullShape, {
depth: 0.85,
bevelEnabled: false,
curveSegments: 1
});
hullGeo.rotateX(Math.PI / 2);
const hull = new THREE.Mesh(hullGeo, materials.wood);
hull.position.y = 0.75;
hull.castShadow = true;
dhow.add(hull);
box(5.6, 0.16, 1.55, materials.roofRed, -0.15, 1.05, 0, dhow);
cylinder(0.055, 0.075, 6.1, 6, materials.wood, 0.6, 3.65, 0, dhow);
const sailShape = new THREE.Shape();
sailShape.moveTo(-2.9, 0);
sailShape.lineTo(0.25, 4.9);
sailShape.lineTo(1.15, 0.45);
sailShape.closePath();
const sail = new THREE.Mesh(
new THREE.ShapeGeometry(sailShape),
new THREE.MeshStandardMaterial({
color: 0xd9c299,
roughness: 1,
side: THREE.DoubleSide,
flatShading: true
})
);
sail.position.set(0.52, 1.55, 0.03);
sail.castShadow = true;
dhow.add(sail);
beam([-3.5, 1.1, 0], [0.65, 6.25, 0], 0.025, materials.iron, dhow);
beam([0.65, 6.25, 0], [3.2, 1.1, 0], 0.025, materials.iron, dhow);
const cargoShip = new THREE.Group();
cargoShip.position.set(39.0, 0.7, -7.0);
cargoShip.rotation.y = -0.06;
scene.add(cargoShip);
box(7.0, 2.6, 22.0, materials.iron, 0, 1.3, 0, cargoShip);
box(6.2, 0.35, 20.4, materials.red, 0, 2.75, 0, cargoShip);
const bowShape = new THREE.Shape();
bowShape.moveTo(-3.5, 0);
bowShape.lineTo(3.5, 0);
bowShape.lineTo(0, 5.8);
bowShape.closePath();
const bowGeo = new THREE.ExtrudeGeometry(bowShape, { depth: 2.6, bevelEnabled: false });
bowGeo.rotateX(Math.PI / 2);
bowGeo.translate(0, 1.3, -11);
const bow = new THREE.Mesh(bowGeo, materials.iron);
bow.castShadow = true;
cargoShip.add(bow);
const containerColors = [
standard(0x9d503d),
standard(0x496f70),
standard(0xb28a4e),
standard(0x59635b),
standard(0x754a45)
];
for (let row = 0; row < 5; row++) {
for (let level = 0; level < (row < 3 ? 2 : 1); level++) {
for (const x of [-1.7, 1.7]) {
box(2.8, 1.45, 3.55, choose(containerColors),
x, 3.65 + level * 1.5, -7.0 + row * 3.7, cargoShip);
}
}
}
box(5.7, 4.4, 4.6, materials.white, 0, 5.0, 9.2, cargoShip);
box(5.1, 1.15, 3.8, materials.window, 0, 5.8, 6.85, cargoShip);
box(5.9, 0.28, 4.9, materials.white, 0, 7.3, 9.2, cargoShip);
cylinder(0.18, 0.25, 3.6, 8, materials.red, 0, 10.2, 9.4, cargoShip);
function crane(z, scale = 1) {
const group = new THREE.Group();
group.position.set(20.4, 1.13, z);
group.scale.setScalar(scale);
scene.add(group);
box(3.4, 0.45, 3.4, materials.metal, 0, 0.2, 0, group);
for (const x of [-1.1, 1.1]) {
for (const zz of [-1.1, 1.1]) {
beam([x, 0.4, zz], [x * 0.62, 10.0, zz * 0.62], 0.17, materials.yellow, group);
}
}
for (let y = 1; y < 10; y += 2.2) {
beam([-1.1, y, -1.1], [1.1, y + 1.8, -1.1], 0.09, materials.yellow, group);
beam([1.1, y, 1.1], [-1.1, y + 1.8, 1.1], 0.09, materials.yellow, group);
}
beam([0, 10, 0], [14.5, 14.3, 0], 0.34, materials.yellow, group);
beam([0, 10, 0], [-4.4, 13.1, 0], 0.30, materials.yellow, group);
beam([0, 10.3, 0], [5.0, 14.8, 0], 0.11, materials.metal, group);
beam([0, 10.3, 0], [-2.1, 13.5, 0], 0.11, materials.metal, group);
beam([13.6, 14.15, 0], [13.6, 6.2, 0], 0.055, materials.iron, group);
box(0.8, 0.45, 0.8, materials.iron, 13.6, 5.9, 0, group);
}
crane(9, 1.0);
crane(30, 0.88);
for (let i = 0; i < 7; i++) {
box(2.4, 1.35, 4.3, choose(containerColors),
19.7, 1.72, -4.0 + i * 5.0);
}
// Breakwater and small navigation light.
box(1.8, 1.8, 72, materials.stone, 52.0, 0.65, -5);
cylinder(0.85, 1.15, 4.2, 8, materials.white, 50.5, 2.65, 33.5);
cylinder(0.88, 0.88, 0.9, 8, materials.red, 50.5, 5.15, 33.5);
cylinder(0.3, 0.4, 1.6, 8, materials.metal, 50.5, 6.15, 33.5);
// Small harbour boat.
const skiff = new THREE.Group();
skiff.position.set(28.5, 0.55, -28.0);
skiff.rotation.y = 0.7;
scene.add(skiff);
box(3.5, 0.55, 1.35, materials.canvasBlue, 0, 0.35, 0, skiff);
box(2.5, 0.18, 0.95, materials.wood, -0.2, 0.7, 0, skiff);
cylinder(0.035, 0.045, 3.8, 6, materials.wood, 0, 2.25, 0, skiff);
// Quay bollards.
for (let z = -37; z <= 37; z += 7) {
cylinder(0.16, 0.23, 0.7, 8, materials.iron, 21.8, 1.48, z);
}
// -------------------------------------------------------------------------
// World-space labels
// -------------------------------------------------------------------------
function labelSprite(text, subtitle, x, y, z, width = 13) {
const canvas = document.createElement('canvas');
canvas.width = 768;
canvas.height = 150;
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgba(30,40,38,.84)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = '#e4ae67';
ctx.fillRect(0, 0, 8, canvas.height);
ctx.fillStyle = '#fff3d8';
ctx.font = '600 37px Georgia, serif';
ctx.textAlign = 'center';
ctx.fillText(text, canvas.width / 2, 59);
ctx.fillStyle = '#c9c1ae';
ctx.font = '500 24px sans-serif';
ctx.fillText(subtitle, canvas.width / 2, 102);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.set(x, y, z);
sprite.scale.set(width, width * 150 / 768, 1);
sprite.renderOrder = 20;
scene.add(sprite);
return sprite;
}
labelSprite('FORT JESUS', 'Portuguese bastioned fort · 1593', 3, 10.2, -35, 16);
labelSprite('OLD TOWN LANES', 'Coral masonry · shaded merchant fronts', -16, 13, 4, 17);
labelSprite('HARBOUR APPROACHES', 'Dhow and deep-water trade', 34, 13, 9, 17);
// -------------------------------------------------------------------------
// Custom camera controls: orbit, pan, wheel and pinch zoom
// -------------------------------------------------------------------------
const desired = {
target: new THREE.Vector3(-4, 3, 3),
radius: 92,
theta: 0.84,
phi: 0.82
};
const current = {
target: desired.target.clone(),
radius: desired.radius,
theta: desired.theta,
phi: desired.phi
};
const views = {
town: { target: [-16, 3.2, 5], radius: 49, theta: 0.22, phi: 1.02 },
fort: { target: [3, 3.1, -31], radius: 44, theta: 0.18, phi: 1.05 },
harbor: { target: [31, 2.7, 9], radius: 57, theta: 1.10, phi: 1.00 },
aerial: { target: [-4, 2.5, 1], radius: 99, theta: 0.83, phi: 0.77 }
};
function setView(name) {
const view = views[name] || views.aerial;
desired.target.set(...view.target);
desired.radius = view.radius;
desired.theta = view.theta;
desired.phi = view.phi;
document.querySelectorAll('[data-view]').forEach(button => {
button.classList.toggle('active', button.dataset.view === name);
});
}
document.querySelectorAll('[data-view]').forEach(button => {
button.addEventListener('click', () => setView(button.dataset.view));
});
const pointers = new Map();
let lastPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener('contextmenu', event => event.preventDefault());
renderer.domElement.addEventListener('pointerdown', event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
lastPinchDistance = pointerDistance();
});
renderer.domElement.addEventListener('pointermove', event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size >= 2) {
const distance = pointerDistance();
if (lastPinchDistance > 0) {
desired.radius = THREE.MathUtils.clamp(
desired.radius * (lastPinchDistance / distance),
18, 150
);
}
lastPinchDistance = distance;
return;
}
const panning = event.shiftKey || event.buttons === 2;
if (panning) {
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
right.y = 0;
right.normalize();
const forward = camera.getWorldDirection(new THREE.Vector3());
forward.y = 0;
forward.normalize();
const scale = desired.radius * 0.00125;
desired.target
.addScaledVector(right, -dx * scale)
.addScaledVector(forward, dy * scale);
desired.target.x = THREE.MathUtils.clamp(desired.target.x, -42, 42);
desired.target.z = THREE.MathUtils.clamp(desired.target.z, -36, 36);
} else {
desired.theta -= dx * 0.005;
desired.phi = THREE.MathUtils.clamp(desired.phi + dy * 0.004, 0.30, 1.35);
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
lastPinchDistance = pointerDistance();
}
renderer.domElement.addEventListener('pointerup', removePointer);
renderer.domElement.addEventListener('pointercancel', removePointer);
renderer.domElement.addEventListener('wheel', event => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
18, 150
);
}, { passive: false });
renderer.domElement.addEventListener('dblclick', () => setView('aerial'));
addEventListener('keydown', event => {
if (event.key.toLowerCase() === 'r') setView('aerial');
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let frame = 0;
const originalWaterPositions = new Float32Array(waterPosition.array);
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
frame++;
const damping = 1 - Math.exp(-dt * 6.5);
current.target.lerp(desired.target, damping);
current.radius = THREE.MathUtils.lerp(current.radius, desired.radius, damping);
current.theta = THREE.MathUtils.lerp(current.theta, desired.theta, damping);
current.phi = THREE.MathUtils.lerp(current.phi, desired.phi, damping);
const sinPhi = Math.sin(current.phi);
camera.position.set(
current.target.x + current.radius * sinPhi * Math.sin(current.theta),
current.target.y + current.radius * Math.cos(current.phi),
current.target.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(current.target);
if (frame % 2 === 0) {
for (let i = 0; i < waterPosition.count; i++) {
const x = originalWaterPositions[i * 3];
const z = originalWaterPositions[i * 3 + 2];
const wave =
Math.sin(x * 0.63 + frame * 0.022) * 0.055 +
Math.cos(z * 0.34 - frame * 0.017) * 0.045 +
Math.sin((x + z) * 0.48 + frame * 0.012) * 0.025;
waterPosition.setY(i, wave);
}
waterPosition.needsUpdate = true;
if (frame % 6 === 0) waterGeometry.computeVertexNormals();
}
dhow.position.y = 0.55 + Math.sin(frame * 0.032) * 0.055;
dhow.rotation.z = Math.sin(frame * 0.025) * 0.012;
cargoShip.position.y = 0.70 + Math.sin(frame * 0.018 + 1.4) * 0.045;
cargoShip.rotation.z = Math.sin(frame * 0.016) * 0.006;
flag.rotation.y = Math.sin(frame * 0.06) * 0.13;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setView('aerial');
animate();
requestAnimationFrame(() => {
document.getElementById('loading').classList.add('hidden');
});
addEventListener('resize', () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
});
</script>
</body>
</html>
```