Prompt specification
Build or customize the Three.js 3D scene "Gamla Stan square-to-waterfront street slice" 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: Gamla Stan square-to-waterfront street slice
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/gamla-stan-square-to-waterfront-street-slice-x2871
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a bounded voxel slice of Stockholm's Gamla Stan, from an enclosed square toward a waterfront edge. Base the facade proportions and roof silhouettes on references, keeping the scene a selective interpretation of the place. Arrange closely spaced building fronts around the square, then let a narrow connecting lane open toward a small strip of quay and water. A view from the square should initially reveal only part of the waterfront; moving along the lane gradually exposes the wider view. Give the paving, walls, roofs, and water separate restrained color groups.
Working Three.js Starter (gamla-stan-square-to-waterfront-street-slice-x2871):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover" />
<title>Gamla Stan — A Voxel Waterfront</title>
<style>
:root {
color-scheme: dark;
--ink: #f1eee4;
--muted: rgba(241, 238, 228, 0.66);
--glass: rgba(24, 30, 30, 0.68);
--line: rgba(255, 255, 255, 0.16);
--ochre: #c49a69;
--roof: #444747;
--stone: #918b7d;
--water: #52777b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #aab8b6;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(180deg, rgba(18, 23, 23, 0.2), transparent 24%, transparent 73%, rgba(18, 23, 23, 0.28)),
radial-gradient(circle at center, transparent 58%, rgba(20, 25, 25, 0.18) 100%);
z-index: 2;
}
.panel {
position: fixed;
z-index: 4;
color: var(--ink);
background: var(--glass);
border: 1px solid var(--line);
box-shadow: 0 12px 36px rgba(0, 0, 0, 0.18);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-card {
top: 22px;
left: 22px;
width: min(350px, calc(100vw - 44px));
padding: 17px 18px 15px;
border-radius: 3px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 18px;
height: 1px;
background: var(--ochre);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 4vw, 39px);
font-weight: 400;
line-height: 0.98;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 0;
max-width: 290px;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.legend {
left: 22px;
bottom: 22px;
padding: 12px 14px;
border-radius: 3px;
display: grid;
grid-template-columns: repeat(2, auto);
gap: 8px 16px;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
}
.swatch {
width: 8px;
height: 8px;
border-radius: 1px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}
.controls {
right: 22px;
bottom: 22px;
min-width: 260px;
padding: 12px;
border-radius: 3px;
}
.controls p {
margin: 0 0 10px;
color: var(--muted);
font-size: 9px;
line-height: 1.6;
letter-spacing: 0.05em;
text-align: center;
text-transform: uppercase;
}
.controls kbd {
color: var(--ink);
font: inherit;
font-weight: 750;
}
button {
width: 100%;
border: 1px solid rgba(255, 255, 255, 0.22);
border-radius: 2px;
padding: 10px 12px;
color: var(--ink);
background: rgba(255, 255, 255, 0.07);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.13);
border-color: rgba(255, 255, 255, 0.38);
}
button:active {
transform: translateY(1px);
}
.progress {
position: fixed;
z-index: 4;
left: 50%;
bottom: 24px;
width: min(280px, 34vw);
transform: translateX(-50%);
color: var(--ink);
pointer-events: none;
text-align: center;
}
.progress-label {
margin-bottom: 8px;
color: rgba(255, 255, 255, 0.78);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
text-shadow: 0 1px 8px rgba(0, 0, 0, 0.6);
}
.track {
height: 2px;
overflow: hidden;
background: rgba(255, 255, 255, 0.23);
box-shadow: 0 1px 8px rgba(0, 0, 0, 0.35);
}
.fill {
width: 0;
height: 100%;
background: var(--ochre);
transition: width 100ms linear;
}
.crosshair {
position: fixed;
z-index: 3;
top: 50%;
left: 50%;
width: 5px;
height: 5px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 50%;
transform: translate(-50%, -50%);
pointer-events: none;
opacity: 0.65;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}
.loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #ede8dc;
background: #27302f;
transition: opacity 650ms ease, visibility 650ms ease;
}
.loading.done {
opacity: 0;
visibility: hidden;
}
.loading div {
text-align: center;
}
.loading strong {
display: block;
margin-bottom: 10px;
font-family: Georgia, "Times New Roman", serif;
font-size: 28px;
font-weight: 400;
}
.loading span {
color: rgba(255, 255, 255, 0.55);
font-size: 9px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
padding: 13px 14px;
}
.subtitle {
display: none;
}
.legend {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
min-width: 210px;
}
.progress {
left: 12px;
bottom: 88px;
width: calc(100vw - 250px);
transform: none;
}
}
</style>
</head>
<body>
<div class="vignette"></div>
<div class="crosshair"></div>
<section class="panel title-card">
<div class="eyebrow">Stockholm · Selective voxel study</div>
<h1>Gamla Stan</h1>
<p class="subtitle">
An enclosed square, a narrow passage, and the first reveal of the harbor.
The architecture is an interpretation rather than a literal reconstruction.
</p>
</section>
<section class="panel legend" aria-label="Scene color groups">
<span><i class="swatch" style="background:#b58a5c"></i>Walls</span>
<span><i class="swatch" style="background:#3f4444"></i>Roofs</span>
<span><i class="swatch" style="background:#918b7d"></i>Paving</span>
<span><i class="swatch" style="background:#52777b"></i>Water</span>
</section>
<section class="panel controls">
<p>
<kbd>W A S D</kbd> / arrows walk ·
drag to look · <kbd>R</kbd> reset
</p>
<button id="walkButton" type="button">Walk to the quay</button>
</section>
<div class="progress">
<div class="progress-label" id="progressLabel">Enclosed square</div>
<div class="track"><div class="fill" id="progressFill"></div></div>
</div>
<div class="loading" id="loading">
<div>
<strong>Gamla Stan</strong>
<span>Building the voxel waterfront</span>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
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;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaebbb8);
scene.fog = new THREE.Fog(0xaebbb8, 48, 105);
const camera = new THREE.PerspectiveCamera(
55,
window.innerWidth / window.innerHeight,
0.08,
150
);
camera.rotation.order = "YXZ";
const hemisphere = new THREE.HemisphereLight(0xdce8e5, 0x695b4d, 1.75);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe0ad, 2.25);
sun.position.set(-26, 42, 22);
sun.target.position.set(0, 0, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -38;
sun.shadow.camera.right = 38;
sun.shadow.camera.top = 58;
sun.shadow.camera.bottom = -48;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 125;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.035;
scene.add(sun, sun.target);
const fill = new THREE.DirectionalLight(0x9fb8bd, 0.5);
fill.position.set(22, 18, -25);
scene.add(fill);
// ---------------------------------------------------------------------
// Shared geometry and restrained material groups
// ---------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const standard = (color, roughness = 0.9, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const wallMaterials = [
standard(0xb4875d),
standard(0xc3a16d),
standard(0xa86d58),
standard(0xbdb08d),
standard(0x96988a),
standard(0xb38b73),
standard(0x8e9187),
standard(0xc9ae78)
];
const roofMaterials = [
standard(0x3d4343),
standard(0x484846),
standard(0x493a38),
standard(0x394442),
standard(0x51423d)
];
const pavingMaterials = [
standard(0x918b7d),
standard(0x878276),
standard(0x9b9383),
standard(0x7f7d73)
];
const trimMaterial = standard(0xb3a78e);
const darkTrimMaterial = standard(0x30383a);
const windowMaterial = standard(0x24373d, 0.46, 0.08);
const doorMaterial = standard(0x323638);
const shutterRustMaterial = standard(0x754d43);
const shutterGreenMaterial = standard(0x536159);
const awningMaterial = standard(0x81574a);
const stoneMaterial = standard(0x77776f);
const lightStoneMaterial = standard(0xa39b89);
const baseMaterial = standard(0x55564e);
const bronzeMaterial = standard(0x465049, 0.65, 0.12);
const timberMaterial = standard(0x625043);
const lanternMaterial = new THREE.MeshStandardMaterial({
color: 0xf0d59c,
emissive: 0xd7a34d,
emissiveIntensity: 0.65,
roughness: 0.5
});
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x486f73,
roughness: 0.28,
metalness: 0.08
});
// ---------------------------------------------------------------------
// Small instancing system for paving, windows, shutters, and trim
// ---------------------------------------------------------------------
const batches = new Map();
const batchMaterials = new Map();
function registerBatch(key, material) {
batches.set(key, []);
batchMaterials.set(key, material);
}
registerBatch("paving0", pavingMaterials[0]);
registerBatch("paving1", pavingMaterials[1]);
registerBatch("paving2", pavingMaterials[2]);
registerBatch("paving3", pavingMaterials[3]);
registerBatch("windowFrame", darkTrimMaterial);
registerBatch("windowPane", windowMaterial);
registerBatch("mullion", darkTrimMaterial);
registerBatch("sill", trimMaterial);
registerBatch("door", doorMaterial);
registerBatch("shutterRust", shutterRustMaterial);
registerBatch("shutterGreen", shutterGreenMaterial);
registerBatch("awning", awningMaterial);
const tempPosition = new THREE.Vector3();
const tempScale = new THREE.Vector3();
const tempQuaternion = new THREE.Quaternion();
const tempMatrix = new THREE.Matrix4();
const tempWorldMatrix = new THREE.Matrix4();
function addInstance(key, position, scale, parentMatrix = null, rotation = null) {
tempPosition.set(position[0], position[1], position[2]);
tempScale.set(scale[0], scale[1], scale[2]);
if (rotation) {
tempQuaternion.setFromEuler(rotation);
} else {
tempQuaternion.identity();
}
tempMatrix.compose(tempPosition, tempQuaternion, tempScale);
if (parentMatrix) {
tempWorldMatrix.copy(parentMatrix).multiply(tempMatrix);
} else {
tempWorldMatrix.copy(tempMatrix);
}
if (!batches.has(key)) batches.set(key, []);
batches.get(key).push(tempWorldMatrix.clone());
}
function finalizeInstances() {
for (const [key, matrices] of batches) {
if (!matrices.length) continue;
const mesh = new THREE.InstancedMesh(
unitBox,
batchMaterials.get(key),
matrices.length
);
matrices.forEach((matrix, index) => mesh.setMatrixAt(index, matrix));
mesh.instanceMatrix.needsUpdate = true;
mesh.computeBoundingSphere();
mesh.receiveShadow = key.startsWith("paving");
mesh.castShadow = false;
scene.add(mesh);
}
}
function box(parent, material, x, y, z, w, h, d, cast = true, receive = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.position.set(x, y, z);
mesh.scale.set(w, h, d);
mesh.castShadow = cast;
mesh.receiveShadow = receive;
parent.add(mesh);
return mesh;
}
function worldBox(material, x, y, z, w, h, d, cast = true, receive = true) {
return box(scene, material, x, y, z, w, h, d, cast, receive);
}
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value += 0x6d2b79f5;
let t = value;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(731994);
// ---------------------------------------------------------------------
// Voxel architecture
//
// The narrow plots, repeated window bays, shallow upper floors, steep
// gables, and dark pitched roofs reference the proportions and layered
// roof silhouettes associated with Gamla Stan, without claiming a
// surveyed reconstruction.
// ---------------------------------------------------------------------
function createGableGeometry(width, depth, rise) {
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
-width / 2, 0, -depth / 2,
0, rise, -depth / 2,
width / 2, 0, -depth / 2,
-width / 2, 0, depth / 2,
0, rise, depth / 2,
width / 2, 0, depth / 2
]);
const indices = [
0, 2, 1,
3, 4, 5,
0, 1, 4, 0, 4, 3,
1, 2, 5, 1, 5, 4,
0, 3, 5, 0, 5, 2
];
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addBuilding(spec) {
const group = new THREE.Group();
group.position.set(spec.x, 0, spec.z);
group.rotation.y = spec.rotation;
scene.add(group);
const wall = spec.wall;
const roof = spec.roof;
box(group, wall, 0, spec.h / 2, 0, spec.w, spec.h, spec.d);
box(group, stoneMaterial, 0, 0.38, 0, spec.w + 0.12, 0.76, spec.d + 0.12);
box(group, trimMaterial, 0, spec.h - 0.12, 0, spec.w + 0.16, 0.24, spec.d + 0.16);
const rise = 2.35 + random() * 1.45;
if (spec.roofStyle === "stepped") {
for (let i = 0; i < 4; i++) {
const width = spec.w + 0.7 - i * 0.28;
const depth = spec.d + 0.7 - i * 0.17;
box(
group,
roof,
0,
spec.h + 0.35 + i * 0.48,
0,
width,
0.52,
depth
);
}
} else {
const roofGeometry = createGableGeometry(
spec.w + 0.7,
spec.d + 0.7,
rise
);
const roofMesh = new THREE.Mesh(roofGeometry, roof);
roofMesh.position.y = spec.h + 0.04;
roofMesh.castShadow = true;
roofMesh.receiveShadow = true;
group.add(roofMesh);
box(
group,
roof,
0,
spec.h + rise + 0.08,
0,
0.2,
0.18,
spec.d + 0.9
);
}
// Chimneys are deliberately simple, square volumes.
const chimneyX = spec.w * 0.27;
const chimneyHeight = spec.roofStyle === "stepped" ? 1.35 : 1.65;
const chimneyBase = spec.roofStyle === "stepped"
? spec.h + 1.55
: spec.h + rise * (1 - Math.abs(chimneyX) / (spec.w / 2));
box(
group,
roof,
chimneyX,
chimneyBase + chimneyHeight / 2,
-spec.d * 0.13,
0.62,
chimneyHeight,
0.68
);
box(
group,
stoneMaterial,
chimneyX,
chimneyBase + chimneyHeight + 0.08,
-spec.d * 0.13,
0.78,
0.16,
0.82
);
group.updateMatrixWorld(true);
// Façade bays. The group matrix turns the local +Z front toward
// the square or lane.
const front = spec.d / 2 + 0.035;
const floors = Math.max(3, Math.min(5, Math.round(spec.h / 3.05)));
const spacing = 1.72;
const count = Math.max(2, Math.floor((spec.w - 0.9) / spacing));
const startX = -((count - 1) * spacing) / 2;
for (let floor = 0; floor < floors - 1; floor++) {
const y = 3.82 + floor * 2.66;
for (let i = 0; i < count; i++) {
const x = startX + i * spacing;
addInstance(
"windowFrame",
[x, y, front],
[1.04, 1.55, 0.11],
group.matrixWorld
);
addInstance(
"windowPane",
[x, y, front + 0.075],
[0.78, 1.27, 0.065],
group.matrixWorld
);
addInstance(
"mullion",
[x, y, front + 0.12],
[0.055, 1.29, 0.045],
group.matrixWorld
);
addInstance(
"mullion",
[x, y, front + 0.12],
[0.8, 0.055, 0.045],
group.matrixWorld
);
addInstance(
"sill",
[x, y - 0.83, front + 0.07],
[1.24, 0.13, 0.22],
group.matrixWorld
);
if (random() > 0.57) {
const shutterKey = random() > 0.5 ? "shutterRust" : "shutterGreen";
addInstance(
shutterKey,
[x - 0.73, y, front + 0.08],
[0.34, 1.53, 0.08],
group.matrixWorld
);
addInstance(
shutterKey,
[x + 0.73, y, front + 0.08],
[0.34, 1.53, 0.08],
group.matrixWorld
);
}
}
}
// Ground-floor entrance and occasional shop bay.
addInstance(
"door",
[0, 1.18, front + 0.055],
[1.14, 2.36, 0.13],
group.matrixWorld
);
addInstance(
"sill",
[0, 2.42, front + 0.11],
[1.45, 0.15, 0.24],
group.matrixWorld
);
if (spec.w > 6.8) {
for (const x of [-spec.w * 0.29, spec.w * 0.29]) {
addInstance(
"windowFrame",
[x, 1.38, front],
[1.22, 1.38, 0.11],
group.matrixWorld
);
addInstance(
"windowPane",
[x, 1.38, front + 0.075],
[0.94, 1.12, 0.065],
group.matrixWorld
);
}
if (random() > 0.35) {
const awning = box(
group,
awningMaterial,
-spec.w * 0.26,
2.28,
front + 0.42,
1.85,
0.13,
0.82
);
awning.rotation.x = -0.12;
}
}
return group;
}
const buildingSpecs = [];
// South side of the enclosed square.
[-20, -12, -4, 4, 12, 20].forEach((x, index) => {
buildingSpecs.push({
x,
z: -30.5,
w: 7.65,
d: 11,
h: 11.2 + (index % 3) * 1.6,
rotation: 0,
wall: wallMaterials[(index + 1) % wallMaterials.length],
roof: roofMaterials[index % 4],
roofStyle: index % 5 === 0 ? "stepped" : "gable"
});
});
// West and east fronts surrounding the square.
[-19, -9.5, 0].forEach((z, index) => {
buildingSpecs.push({
x: -18.5,
z,
w: 9.15,
d: 13,
h: 12.1 + index * 1.45,
rotation: Math.PI / 2,
wall: wallMaterials[(index + 3) % wallMaterials.length],
roof: roofMaterials[(index + 1) % roofMaterials.length],
roofStyle: index === 1 ? "stepped" : "gable"
});
buildingSpecs.push({
x: 18.5,
z,
w: 9.15,
d: 13,
h: 11.7 + index * 1.55,
rotation: -Math.PI / 2,
wall: wallMaterials[(index + 5) % wallMaterials.length],
roof: roofMaterials[(index + 2) % roofMaterials.length],
roofStyle: "gable"
});
});
// The far side is broken by the narrow lane.
[
[-20, 9.2],
[-10.2, 10.9],
[10.2, 10.9],
[20, 9.2]
].forEach(([x, w], index) => {
buildingSpecs.push({
x,
z: 9,
w,
d: 8,
h: 12 + (index % 3) * 1.45,
rotation: Math.PI,
wall: wallMaterials[(index + 2) % wallMaterials.length],
roof: roofMaterials[(index + 3) % roofMaterials.length],
roofStyle: index === 1 ? "stepped" : "gable"
});
});
// Closely packed lane walls. Their inner faces form an eight-metre gap.
[
[17.5, 9],
[26.5, 9],
[35.5, 9],
[42.2, 5.6]
].forEach(([z, w], index) => {
buildingSpecs.push({
x: -9,
z,
w,
d: 10,
h: 10.7 + (index % 3) * 1.55,
rotation: Math.PI / 2,
wall: wallMaterials[(index + 4) % wallMaterials.length],
roof: roofMaterials[index % roofMaterials.length],
roofStyle: index === 2 ? "stepped" : "gable"
});
buildingSpecs.push({
x: 9,
z,
w,
d: 10,
h: 11.2 + (index % 3) * 1.45,
rotation: -Math.PI / 2,
wall: wallMaterials[(index + 6) % wallMaterials.length],
roof: roofMaterials[(index + 1) % roofMaterials.length],
roofStyle: "gable"
});
});
buildingSpecs.forEach(addBuilding);
// ---------------------------------------------------------------------
// A simplified church tower and spire, used as a skyline reference.
// ---------------------------------------------------------------------
const tower = new THREE.Group();
tower.position.set(-16, 0, 3.2);
scene.add(tower);
box(tower, wallMaterials[3], 0, 7.2, 0, 4.6, 14.4, 4.6);
box(tower, stoneMaterial, 0, 0.55, 0, 5.2, 1.1, 5.2);
box(tower, darkTrimMaterial, 0, 14.7, 0, 5.2, 2.5, 5.2);
for (let i = 0; i < 6; i++) {
const width = 5.3 - i * 0.72;
const height = 0.9;
box(
tower,
roofMaterials[3],
0,
16.05 + i * 0.78,
0,
width,
height,
width
);
}
box(tower, roofMaterials[3], 0, 21.05, 0, 0.16, 1.3, 0.16);
box(tower, roofMaterials[3], 0, 21.45, 0, 0.7, 0.14, 0.14);
// ---------------------------------------------------------------------
// Bounded ground, paving, curbs, and quay
// ---------------------------------------------------------------------
worldBox(baseMaterial, 0, -0.78, 20, 62, 1.2, 118);
function paveRegion(x0, x1, z0, z1, size, variation = 4) {
for (let x = x0 + size / 2; x < x1; x += size) {
for (let z = z0 + size / 2; z < z1; z += size) {
const width = Math.min(size * 0.96, x1 - x);
const depth = Math.min(size * 0.96, z1 - z);
const key = `paving${Math.floor(random() * variation)}`;
addInstance(key, [x, 0.035, z], [width, 0.12, depth]);
}
}
}
paveRegion(-12, 12, -25, 5, 1.48);
paveRegion(-4, 4, 5, 45, 1.22);
paveRegion(-24, 24, 45, 53, 1.95);
// Square perimeter stones, interrupted at the lane.
worldBox(lightStoneMaterial, -12.05, 0.13, -10, 0.35, 0.3, 30);
worldBox(lightStoneMaterial, 12.05, 0.13, -10, 0.35, 0.3, 30);
worldBox(lightStoneMaterial, -8, 0.13, -25.05, 8, 0.3, 0.35);
worldBox(lightStoneMaterial, 8, 0.13, -25.05, 8, 0.3, 0.35);
worldBox(lightStoneMaterial, -8, 0.13, 5.05, 8, 0.3, 0.35);
worldBox(lightStoneMaterial, 8, 0.13, 5.05, 8, 0.3, 0.35);
// Lane edging.
worldBox(lightStoneMaterial, -4.05, 0.12, 29, 0.24, 0.28, 40);
worldBox(lightStoneMaterial, 4.05, 0.12, 29, 0.24, 0.28, 40);
// Quay face and individual edge blocks.
worldBox(stoneMaterial, 0, 0.12, 53.05, 48, 0.52, 0.85);
for (let x = -23; x <= 23; x += 2) {
if (x > 5 && x < 11) continue;
worldBox(lightStoneMaterial, x, 0.4, 53.5, 1.82, 0.65, 0.5);
}
// Small stair down to the water.
for (let i = 0; i < 3; i++) {
worldBox(
lightStoneMaterial,
8,
0.34 - i * 0.18,
53.45 + i * 0.5,
4.8,
0.32,
0.55
);
}
// ---------------------------------------------------------------------
// Water and animated voxel wave marks
// ---------------------------------------------------------------------
const water = worldBox(
waterMaterial,
0,
-0.12,
68,
62,
0.16,
29,
false,
true
);
const waveCount = 65;
const waveGeometry = new THREE.BoxGeometry(1, 1, 1);
const waveMaterial = new THREE.MeshBasicMaterial({
color: 0x91aaab,
transparent: true,
opacity: 0.32,
depthWrite: false
});
const waveMesh = new THREE.InstancedMesh(waveGeometry, waveMaterial, waveCount);
waveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waveMesh.frustumCulled = false;
scene.add(waveMesh);
const waveData = [];
for (let i = 0; i < waveCount; i++) {
waveData.push({
x: -30 + random() * 60,
z: 55 + random() * 25,
length: 0.65 + random() * 2.1,
phase: random() * Math.PI * 2
});
}
function updateWaves(time) {
waveData.forEach((wave, index) => {
const x = wave.x + Math.sin(time * 0.34 + wave.phase) * 0.45;
const z = wave.z + Math.cos(time * 0.27 + wave.phase) * 0.12;
const matrix = new THREE.Matrix4().compose(
new THREE.Vector3(x, -0.015, z),
new THREE.Quaternion(),
new THREE.Vector3(wave.length, 0.025, 0.075)
);
waveMesh.setMatrixAt(index, matrix);
});
waveMesh.instanceMatrix.needsUpdate = true;
}
// ---------------------------------------------------------------------
// Small street details: statue, lamps, benches, bollards, and boat
// ---------------------------------------------------------------------
const statue = new THREE.Group();
statue.position.set(-6.5, 0, -7.5);
scene.add(statue);
box(statue, lightStoneMaterial, 0, 0.32, 0, 2.1, 0.64, 2.1);
box(statue, stoneMaterial, 0, 0.93, 0, 1.25, 0.62, 1.25);
box(statue, bronzeMaterial, 0, 1.75, 0, 0.65, 1.2, 0.48);
box(statue, bronzeMaterial, 0, 2.55, 0, 0.44, 0.44, 0.44);
box(statue, bronzeMaterial, 0, 1.95, 0.02, 1.15, 0.22, 0.32);
function addLamp(x, z) {
const lamp = new THREE.Group();
lamp.position.set(x, 0, z);
scene.add(lamp);
box(lamp, darkTrimMaterial, 0, 0.14, 0, 0.48, 0.28, 0.48);
box(lamp, darkTrimMaterial, 0, 1.55, 0, 0.14, 2.8, 0.14);
box(lamp, darkTrimMaterial, 0, 2.98, 0, 0.52, 0.12, 0.52);
box(lamp, lanternMaterial, 0, 3.22, 0, 0.32, 0.42, 0.32, false, false);
box(lamp, darkTrimMaterial, 0, 3.47, 0, 0.48, 0.12, 0.48);
}
addLamp(-10.7, -17);
addLamp(10.7, -11);
addLamp(-10.7, 1);
addLamp(10.7, 1);
addLamp(-3.55, 13);
addLamp(3.55, 24);
addLamp(-3.55, 36);
function addBench(x, z, rotation = 0) {
const bench = new THREE.Group();
bench.position.set(x, 0, z);
bench.rotation.y = rotation;
scene.add(bench);
box(bench, timberMaterial, 0, 0.65, 0, 2.6, 0.18, 0.66);
box(bench, timberMaterial, 0, 1.12, -0.27, 2.6, 0.58, 0.14);
box(bench, darkTrimMaterial, -0.95, 0.32, 0, 0.16, 0.64, 0.56);
box(bench, darkTrimMaterial, 0.95, 0.32, 0, 0.16, 0.64, 0.56);
}
addBench(7.5, -13.5, -Math.PI / 2);
addBench(-8, 0.5, Math.PI / 2);
for (let x = -21; x <= 21; x += 5) {
if (x > 5 && x < 11) continue;
worldBox(darkTrimMaterial, x, 0.55, 52.65, 0.34, 0.9, 0.34);
worldBox(darkTrimMaterial, x, 0.99, 52.65, 0.48, 0.16, 0.48);
}
const boat = new THREE.Group();
boat.position.set(-9, 0, 61.5);
boat.rotation.y = -0.22;
scene.add(boat);
box(boat, shutterRustMaterial, 0, 0.14, 0, 5.3, 0.62, 1.65);
box(boat, shutterRustMaterial, -2.8, 0.29, 0, 1.1, 0.42, 1.05);
box(boat, timberMaterial, 0, 0.49, 0, 4.65, 0.14, 1.38);
box(boat, wallMaterials[3], 0.35, 1.02, 0, 1.65, 1.0, 1.12);
box(boat, darkTrimMaterial, 0.35, 1.04, 0.58, 1.2, 0.48, 0.08);
box(boat, timberMaterial, 0.35, 1.58, 0, 1.85, 0.12, 1.28);
box(boat, timberMaterial, -1.1, 1.45, 0, 0.12, 2.1, 0.12);
finalizeInstances();
// ---------------------------------------------------------------------
// Custom first-person controls. No camera-control addons are used.
// ---------------------------------------------------------------------
const startPosition = new THREE.Vector3(0, 3.25, -21.5);
let heading = 0;
let pitch = -0.015;
let walkPhase = 0;
let guide = null;
const keys = new Set();
const canvas = renderer.domElement;
function isWalkable(x, z) {
const margin = 0.42;
const inSquare =
x >= -12 + margin &&
x <= 12 - margin &&
z >= -25 + margin &&
z <= 4.75;
const inLane =
x >= -4 + margin &&
x <= 4 - margin &&
z >= 4.55 &&
z <= 45.2;
const onQuay =
x >= -23.5 &&
x <= 23.5 &&
z >= 44.8 &&
z <= 52.2;
return inSquare || inLane || onQuay;
}
function cancelGuide() {
if (!guide) return;
guide = null;
updateWalkButton();
}
function updateCameraOrientation() {
camera.rotation.order = "YXZ";
camera.rotation.x = pitch;
camera.rotation.y = heading + Math.PI;
camera.rotation.z = 0;
}
function resetView() {
cancelGuide();
camera.position.copy(startPosition);
heading = 0;
pitch = -0.015;
updateCameraOrientation();
}
function addGuideSegment(a, b) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.01) return;
guide.segments.push({
a,
b,
dx,
dz,
length,
distance: guide.total
});
guide.total += length;
}
function startGuide(direction) {
const p = { x: camera.position.x, z: camera.position.z };
guide = { direction, segments: [], total: 0, travelled: 0 };
const waypoints = [p];
if (direction === "water") {
waypoints.push({ x: 0, z: p.z });
if (p.z < 5) waypoints.push({ x: 0, z: 5 });
if (p.z < 45) waypoints.push({ x: 0, z: 45 });
waypoints.push({ x: 0, z: 49 });
} else {
waypoints.push({ x: 0, z: p.z });
if (p.z > 45) waypoints.push({ x: 0, z: 45 });
if (p.z > 5) waypoints.push({ x: 0, z: 5 });
waypoints.push({ x: 0, z: -21.5 });
}
for (let i = 0; i < waypoints.length - 1; i++) {
addGuideSegment(waypoints[i], waypoints[i + 1]);
}
updateWalkButton();
}
function updateGuide(dt) {
if (!guide || !guide.segments.length) return;
const speed = 2.75;
let remaining = speed * dt;
let active = guide.segments[0];
while (remaining > 0 && guide.travelled < guide.total) {
active = guide.segments.find(
segment => guide.travelled < segment.distance + segment.length
);
if (!active) break;
const localDistance = guide.travelled - active.distance;
const available = active.length - localDistance;
const step = Math.min(remaining, available);
const nx = active.a.x + active.dx * ((localDistance + step) / active.length);
const nz = active.a.z + active.dz * ((localDistance + step) / active.length);
camera.position.x = nx;
camera.position.z = nz;
guide.travelled += step;
remaining -= step;
}
const targetHeading = Math.atan2(active.dx, active.dz);
heading = lerpAngle(heading, targetHeading, 1 - Math.exp(-dt * 4));
pitch = THREE.MathUtils.lerp(pitch, -0.01, 1 - Math.exp(-dt * 3));
if (guide.travelled >= guide.total - 0.01) {
if (guide.direction === "square") {
heading = 0;
}
guide = null;
updateWalkButton();
}
}
function lerpAngle(a, b, amount) {
let difference = ((b - a + Math.PI) % (Math.PI * 2)) - Math.PI;
if (difference < -Math.PI) difference += Math.PI * 2;
return a + difference * amount;
}
function updateWalkButton() {
const button = document.getElementById("walkButton");
if (guide) {
button.textContent = guide.direction === "water"
? "Pause the walk"
: "Pause the return";
} else {
button.textContent = camera.position.z > 20
? "Return to the square"
: "Walk to the quay";
}
}
document.getElementById("walkButton").addEventListener("click", () => {
if (guide) {
cancelGuide();
return;
}
const direction = camera.position.z > 20 ? "square" : "water";
startGuide(direction);
});
window.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (["arrowup", "arrowdown", "arrowleft", "arrowright", " "].includes(key)) {
event.preventDefault();
}
if (key === "r") {
resetView();
return;
}
if (["w", "a", "s", "d", "arrowup", "arrowdown", "arrowleft", "arrowright"].includes(key)) {
keys.add(key);
cancelGuide();
}
});
window.addEventListener("keyup", event => {
keys.delete(event.key.toLowerCase());
});
window.addEventListener("blur", () => keys.clear());
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
cancelGuide();
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
heading -= dx * 0.004;
pitch -= dy * 0.003;
pitch = THREE.MathUtils.clamp(pitch, -0.72, 0.62);
});
canvas.addEventListener("pointerup", event => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
dragging = false;
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
camera.fov = THREE.MathUtils.clamp(
camera.fov + event.deltaY * 0.025,
42,
70
);
camera.updateProjectionMatrix();
}, { passive: false });
// ---------------------------------------------------------------------
// Movement, progress, and animation
// ---------------------------------------------------------------------
const progressFill = document.getElementById("progressFill");
const progressLabel = document.getElementById("progressLabel");
function updateManualMovement(dt) {
if (guide) return;
let forward = 0;
let strafe = 0;
if (keys.has("w") || keys.has("arrowup")) forward += 1;
if (keys.has("s") || keys.has("arrowdown")) forward -= 1;
if (keys.has("d") || keys.has("arrowright")) strafe += 1;
if (keys.has("a") || keys.has("arrowleft")) strafe -= 1;
if (!forward && !strafe) return;
const length = Math.hypot(forward, strafe);
forward /= length;
strafe /= length;
const speed = keys.has("shift") ? 7.2 : 4.25;
const dx =
(Math.sin(heading) * forward - Math.cos(heading) * strafe) * speed * dt;
const dz =
(Math.cos(heading) * forward + Math.sin(heading) * strafe) * speed * dt;
const nextX = camera.position.x + dx;
if (isWalkable(nextX, camera.position.z)) {
camera.position.x = nextX;
}
const nextZ = camera.position.z + dz;
if (isWalkable(camera.position.x, nextZ)) {
camera.position.z = nextZ;
}
walkPhase += dt * speed * 1.8;
}
function updateProgress() {
const progress = THREE.MathUtils.clamp(
(camera.position.z + 21.5) / 70.5,
0,
1
);
progressFill.style.width = `${progress * 100}%`;
if (camera.position.z < 5) {
progressLabel.textContent = "Enclosed square";
} else if (camera.position.z < 45) {
progressLabel.textContent = "Narrow lane";
} else {
progressLabel.textContent = "Waterfront edge";
}
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.75));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
resetView();
updateWalkButton();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateGuide(dt);
updateManualMovement(dt);
const moving = guide || keys.size > 0;
const bob = moving
? Math.sin(walkPhase) * 0.025
: Math.sin(time * 0.75) * 0.008;
camera.position.y = 3.25 + bob;
updateCameraOrientation();
updateProgress();
updateWaves(time);
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.getElementById("loading").classList.add("done");
});
</script>
</body>
</html>
```