Prompt specification
Build or customize the Three.js 3D scene "Recife Antigo square and waterfront block" 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: Recife Antigo square and waterfront block
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/recife-antigo-square-and-waterfront-block-x2695
Scene Description Prompt:
Model Recife Antigo's Marco Zero in voxels, with historic facades, open plaza, and clear waterfront sightlines. Create standalone HTML with Three.js from cdnjs.
Working Three.js Starter (recife-antigo-square-and-waterfront-block-x2695):
```html
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Marco Zero — Recife Antigo</title>
<style>
:root {
--ink: #17313a;
--paper: rgba(255, 249, 232, 0.94);
--line: rgba(23, 49, 58, 0.16);
--accent: #c8523f;
--water: #277d8b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #c7e4eb;
color: var(--ink);
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;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 20px 20px 16px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--paper);
box-shadow: 0 16px 45px rgba(23, 49, 58, 0.15);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #597078;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
content: "";
background: var(--accent);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 48px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.04em;
}
.subtitle {
margin: 11px 0 17px;
color: #52676e;
font-size: 12px;
line-height: 1.55;
}
.views {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
pointer-events: auto;
}
.views button {
min-height: 37px;
padding: 8px 6px;
border: 1px solid rgba(23, 49, 58, 0.18);
border-radius: 3px;
background: rgba(255, 255, 255, 0.45);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
transition:
background 160ms ease,
color 160ms ease,
transform 160ms ease;
}
.views button:hover {
transform: translateY(-1px);
background: white;
}
.views button.active {
border-color: var(--ink);
background: var(--ink);
color: #fff9e8;
}
.legend {
position: fixed;
z-index: 8;
right: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: min(430px, calc(100vw - 44px));
padding: 10px 12px;
border: 1px solid var(--line);
border-radius: 4px;
background: rgba(255, 249, 232, 0.88);
box-shadow: 0 10px 28px rgba(23, 49, 58, 0.12);
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #52676e;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 1px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.help {
position: fixed;
z-index: 8;
left: 22px;
bottom: 22px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 4px;
background: rgba(255, 249, 232, 0.86);
color: #52676e;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.035em;
backdrop-filter: blur(10px);
pointer-events: none;
}
.map-label {
position: fixed;
z-index: 4;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 2px;
background: rgba(23, 49, 58, 0.82);
box-shadow: 0 5px 16px rgba(23, 49, 58, 0.18);
color: #fff9e8;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
}
.map-label::after {
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 17px;
content: "";
background: rgba(23, 49, 58, 0.7);
}
.map-label::before {
width: 5px;
height: 5px;
border-radius: 50%;
content: "";
background: #f3b83f;
box-shadow: 0 0 0 3px rgba(243, 184, 63, 0.2);
}
.north {
position: fixed;
z-index: 8;
top: 24px;
right: 25px;
width: 42px;
height: 42px;
border: 1px solid rgba(23, 49, 58, 0.2);
border-radius: 50%;
background: rgba(255, 249, 232, 0.84);
color: var(--ink);
font-size: 9px;
font-weight: 850;
pointer-events: none;
backdrop-filter: blur(8px);
}
.north span {
position: absolute;
top: 5px;
left: 50%;
transform: translateX(-50%);
}
.north i {
position: absolute;
top: 15px;
left: 50%;
width: 0;
height: 0;
border-right: 5px solid transparent;
border-bottom: 13px solid var(--accent);
border-left: 5px solid transparent;
transform: translateX(-50%);
}
.north b {
position: absolute;
right: 0;
bottom: 5px;
left: 0;
text-align: center;
color: #71858b;
font-size: 7px;
letter-spacing: 0.12em;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
h1 {
font-size: 34px;
}
.subtitle {
margin-bottom: 12px;
}
.help {
display: none;
}
.legend {
right: 12px;
bottom: 12px;
left: 12px;
justify-content: center;
max-width: none;
}
.north {
top: auto;
right: 14px;
bottom: 86px;
}
}
</style>
</head>
<body>
<div class="panel">
<div class="eyebrow">Recife Antigo · Pernambuco</div>
<h1>Marco Zero</h1>
<p class="subtitle">
Um interpretação voxel da praça histórica, entre as fachadas coloniais
de Recife e aMargem aberta do rio Capibaribe.
</p>
<nav class="views" aria-label="Vistas da cena">
<button class="active" data-view="overview">Panorâmica</button>
<button data-view="plaza">Praça</button>
<button data-view="river">Rio</button>
</nav>
</div>
<div class="north" aria-hidden="true">
<span>N</span>
<i></i>
<b>CAP</b>
</div>
<div class="map-label" id="label-square">Praça Marco Zero</div>
<div class="map-label" id="label-river">Rio Capibaribe</div>
<div class="map-label" id="label-oldtown">Recife Antigo</div>
<div class="help">
Arraste para orbitar · role para zoom · Shift + arraste para mover · setas para navegar
</div>
<div class="legend" aria-label="Legenda">
<div class="legend-item">
<span class="swatch" style="background:#d6a05b"></span>
Fachadas históricas
</div>
<div class="legend-item">
<span class="swatch" style="background:#d8c39b"></span>
Praça aberta
</div>
<div class="legend-item">
<span class="swatch" style="background:#277d8b"></span>
Capibaribe
</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, camera, and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color("#c6e3ea");
scene.fog = new THREE.Fog("#c6e3ea", 105, 220);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
350
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
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 hemisphere = new THREE.HemisphereLight("#e9f8ff", "#856a4f", 1.75);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight("#fff0cf", 2.65);
sun.position.set(-42, 68, 38);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -78;
sun.shadow.camera.right = 78;
sun.shadow.camera.top = 78;
sun.shadow.camera.bottom = -78;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 180;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
scene.add(sun.target);
const fill = new THREE.DirectionalLight("#9ed9ea", 0.45);
fill.position.set(45, 22, -30);
scene.add(fill);
// -------------------------------------------------------------------------
// Deterministic random generator
// -------------------------------------------------------------------------
let seed = 8127;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function shade(color, factor) {
return new THREE.Color(color).multiplyScalar(factor).getHex();
}
// -------------------------------------------------------------------------
// Efficient static voxel batching
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const yAxis = new THREE.Vector3(0, 1, 0);
class BoxBatch {
constructor() {
this.items = [];
}
add(x, y, z, width, height, depth, color, yaw = 0) {
if (
width <= 0 ||
height <= 0 ||
depth <= 0
) {
return;
}
const quaternion = new THREE.Quaternion().setFromAxisAngle(yAxis, yaw);
const matrix = new THREE.Matrix4().compose(
new THREE.Vector3(x, y, z),
quaternion,
new THREE.Vector3(width, height, depth)
);
this.items.push({
matrix,
color: new THREE.Color(color)
});
}
build() {
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.84,
metalness: 0.015
});
const mesh = new THREE.InstancedMesh(
unitBox,
material,
this.items.length
);
this.items.forEach((item, index) => {
mesh.setMatrixAt(index, item.matrix);
mesh.setColorAt(index, item.color);
});
mesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.computeBoundingBox();
mesh.computeBoundingSphere();
scene.add(mesh);
return mesh;
}
}
const batch = new BoxBatch();
function localBox(
originX,
originZ,
yaw,
x,
y,
z,
width,
height,
depth,
color,
localYaw = 0
) {
const c = Math.cos(yaw);
const s = Math.sin(yaw);
const worldX = originX + x * c + z * s;
const worldZ = originZ - x * s + z * c;
batch.add(
worldX,
y,
worldZ,
width,
height,
depth,
color,
yaw + localYaw
);
}
// -------------------------------------------------------------------------
// Ground, plaza paving, and river
// -------------------------------------------------------------------------
// Raised old-town ground.
batch.add(0, -0.76, -3, 86, 1.22, 58, "#a18e70");
// Plaza paving, with a radial compass-like pattern.
for (let ix = -20; ix <= 20; ix++) {
for (let iz = -11; iz <= 9; iz++) {
const x = ix * 1.3;
const z = iz * 1.3;
const distance = Math.hypot(x, z);
let color = (ix + iz) % 2 === 0 ? "#d4bf96" : "#cbb48b";
const border = Math.abs(ix) >= 19 || iz <= -10 || iz >= 8;
if (border) {
color = "#b9a27e";
}
if (distance < 2.5) {
color = "#eee0bc";
} else if (distance > 4.7 && distance < 6.2) {
color = "#bd7159";
} else if (
distance < 9 &&
(Math.abs(x) < 0.72 || Math.abs(z) < 0.72)
) {
color = "#536f73";
}
batch.add(x, -0.01, z, 1.23, 0.14, 1.23, color);
}
}
// Broad waterfront promenade.
for (let ix = -24; ix <= 24; ix++) {
for (let iz = 0; iz < 6; iz++) {
const x = ix * 1.48;
const z = 14.3 + iz * 1.55;
const edge = iz === 5;
const color = edge
? "#a89476"
: (ix + iz) % 3 === 0
? "#d2bd96"
: "#c9b28a";
batch.add(x, -0.025, z, 1.4, 0.13, 1.47, color);
}
}
// Quay wall and evenly spaced bollards.
for (let x = -39; x <= 39; x += 3) {
batch.add(x, -0.22, 24.15, 2.92, 0.72, 0.72, "#8f816c");
batch.add(x, 0.22, 24.15, 2.94, 0.12, 0.84, "#c5b28d");
}
for (let x = -35; x <= 35; x += 5) {
batch.add(x, 0.38, 23.55, 0.38, 0.75, 0.38, "#394a4b");
batch.add(x, 0.76, 23.55, 0.58, 0.16, 0.58, "#26383b");
}
// River plane.
const waterMaterial = new THREE.MeshStandardMaterial({
color: "#277d8b",
roughness: 0.28,
metalness: 0.12
});
const water = new THREE.Mesh(
new THREE.PlaneGeometry(190, 100),
waterMaterial
);
water.rotation.x = -Math.PI / 2;
water.position.set(0, -0.43, 75);
water.receiveShadow = true;
scene.add(water);
// Far bank, kept subdued by fog.
batch.add(0, -0.32, 123, 190, 1.2, 13, "#7e9b96");
for (let x = -88; x < 90; x += 8) {
const width = 5.5 + random() * 3.2;
const height = 5 + random() * 12;
const depth = 5 + random() * 3;
const color = random() > 0.5 ? "#89a8ad" : "#78979f";
batch.add(
x + random() * 2,
height / 2,
122 + random() * 2,
width,
height,
depth,
color
);
}
// -------------------------------------------------------------------------
// Historic façades
// -------------------------------------------------------------------------
function buildFacade({
x,
z,
width,
height,
depth = 6,
yaw = 0,
color,
roof = "#b85f43",
style = "standard"
}) {
const trim = "#f2dfba";
const darkTrim = shade(color, 0.72);
const glass = "#244957";
const roofColors = [
roof,
shade(roof, 1.13),
shade(roof, 0.87)
];
// Main volume and masonry bands.
localBox(x, z, yaw, 0, height / 2, 0, width, height, depth, color);
localBox(
x,
z,
yaw,
0,
0.3,
0,
width + 0.15,
0.6,
depth + 0.12,
"#9d896d"
);
localBox(
x,
z,
yaw,
0,
3.18,
depth / 2 + 0.05,
width + 0.18,
0.22,
0.25,
trim
);
localBox(
x,
z,
yaw,
0,
height - 0.32,
depth / 2 + 0.05,
width + 0.45,
0.42,
0.42,
trim
);
localBox(
x,
z,
yaw,
0,
height - 0.07,
depth / 2 + 0.06,
width + 0.65,
0.18,
0.56,
darkTrim
);
// Ground-floor doors and shop openings.
const doorCount = width > 9 ? 3 : 2;
const doorSpacing = width / (doorCount + 1);
for (let i = 0; i < doorCount; i++) {
const dx = -width / 2 + doorSpacing * (i + 1);
localBox(
x,
z,
yaw,
dx,
1.22,
depth / 2 + 0.09,
1.34,
2.44,
0.18,
glass
);
localBox(
x,
z,
yaw,
dx,
2.56,
depth / 2 + 0.19,
1.7,
0.2,
0.32,
trim
);
localBox(
x,
z,
yaw,
dx - 0.82,
1.28,
depth / 2 + 0.18,
0.22,
2.55,
0.32,
trim
);
localBox(
x,
z,
yaw,
dx + 0.82,
1.28,
depth / 2 + 0.18,
0.22,
2.55,
0.32,
trim
);
}
// Upper windows, shutters, frames, and sills.
for (let floorY = 4.58; floorY < height - 0.7; floorY += 3.08) {
const bayCount = Math.max(2, Math.floor(width / 2.25));
const spacing = width / (bayCount + 1);
for (let bay = 0; bay < bayCount; bay++) {
const wx = -width / 2 + spacing * (bay + 1);
localBox(
x,
z,
yaw,
wx,
floorY,
depth / 2 + 0.09,
1.48,
1.72,
0.16,
trim
);
localBox(
x,
z,
yaw,
wx,
floorY,
depth / 2 + 0.19,
1.02,
1.29,
0.1,
glass
);
localBox(
x,
z,
yaw,
wx - 1.03,
floorY,
depth / 2 + 0.18,
0.31,
1.56,
0.13,
darkTrim
);
localBox(
x,
z,
yaw,
wx + 1.03,
floorY,
depth / 2 + 0.18,
0.31,
1.56,
0.13,
darkTrim
);
localBox(
x,
z,
yaw,
wx,
floorY - 0.94,
depth / 2 + 0.22,
1.66,
0.15,
0.42,
trim
);
}
}
// Edge pilasters emphasize the narrow colonial proportions.
for (const side of [-1, 1]) {
localBox(
x,
z,
yaw,
side * (width / 2 - 0.17),
height / 2,
depth / 2 + 0.1,
0.22,
height - 0.55,
0.22,
trim
);
}
// A striped market awning.
if (style === "market") {
const stripeCount = Math.floor(width / 0.85);
for (let i = 0; i < stripeCount; i++) {
const sx = -width / 2 + 0.43 + i * 0.85;
localBox(
x,
z,
yaw,
sx,
2.91,
depth / 2 + 0.61,
0.82,
0.18,
1.25,
i % 2 === 0 ? trim : shade(roof, 1.05)
);
}
}
// Selected façades receive iron balconies.
if (style === "balcony") {
localBox(
x,
z,
yaw,
0,
3.43,
depth / 2 + 0.58,
width - 1.15,
0.2,
1.18,
"#536267"
);
localBox(
x,
z,
yaw,
0,
4.04,
depth / 2 + 1.08,
width - 1.15,
0.1,
0.12,
"#34484d"
);
const railCount = Math.floor((width - 1.2) / 0.45);
for (let i = 0; i <= railCount; i++) {
const rx = -(width - 1.2) / 2 + i * ((width - 1.2) / railCount);
localBox(
x,
z,
yaw,
rx,
3.75,
depth / 2 + 1.08,
0.08,
1.08,
0.08,
"#34484d"
);
}
}
// Stepped, tiled roof.
for (let tier = 0; tier < 4; tier++) {
const tierDepth = depth + 0.8 - tier * 1.35;
const roofWidth = width + 0.65;
const tileCount = Math.ceil(roofWidth / 0.88);
const tileWidth = roofWidth / tileCount;
for (let tile = 0; tile < tileCount; tile++) {
const tx = -roofWidth / 2 + tileWidth * (tile + 0.5);
localBox(
x,
z,
yaw,
tx,
height + 0.36 + tier * 0.38,
-0.26 * tier,
tileWidth - 0.035,
0.4,
tierDepth,
roofColors[(tile + tier) % roofColors.length]
);
}
}
// Central civic façade with a stepped pediment.
if (style === "civic") {
for (let step = 0; step < 5; step++) {
localBox(
x,
z,
yaw,
0,
height + 0.55 + step * 0.37,
depth / 2 + 0.16,
4.7 - step * 0.88,
0.38,
0.34,
trim
);
}
for (let i = 0; i < 3; i++) {
localBox(
x,
z,
yaw,
0,
0.12 + i * 0.14,
depth / 2 + 0.5 + i * 0.25,
4.2 + i * 0.7,
0.15,
0.65,
"#b7a486"
);
}
// Small flagpoles, kept as geometric voxels.
for (const side of [-1, 1]) {
localBox(
x,
z,
yaw,
side * 2.2,
height + 2.6,
depth / 2 + 0.15,
0.09,
2.25,
0.09,
"#e8dfc8"
);
localBox(
x,
z,
yaw,
side * 2.2 + 0.46,
height + 3.35,
depth / 2 + 0.15,
0.88,
0.5,
0.08,
side < 0 ? "#286b4d" : "#d5a934"
);
}
}
// Chimneys.
if (style !== "civic") {
localBox(
x,
z,
yaw,
width * 0.28,
height + 1.7,
-depth * 0.12,
0.65,
1.55,
0.72,
shade(roof, 0.8)
);
localBox(
x,
z,
yaw,
width * 0.28,
height + 2.48,
-depth * 0.12,
0.88,
0.18,
0.94,
trim
);
}
}
// Northern façade wall.
buildFacade({
x: -28,
z: -20,
width: 8,
height: 8.4,
color: "#c86f57",
roof: "#a84e3d",
style: "market"
});
buildFacade({
x: -20,
z: -20,
width: 8,
height: 10.2,
color: "#e4c69c",
roof: "#b85c42",
style: "balcony"
});
buildFacade({
x: -12,
z: -20,
width: 8,
height: 8.8,
color: "#6c9b91",
roof: "#9a493b",
style: "market"
});
buildFacade({
x: -4,
z: -20,
width: 8,
height: 11.3,
depth: 7,
color: "#d5a253",
roof: "#a74b38",
style: "civic"
});
buildFacade({
x: 4.5,
z: -20,
width: 9,
height: 9.2,
color: "#d99486",
roof: "#b65343",
style: "balcony"
});
buildFacade({
x: 13,
z: -20,
width: 8,
height: 10.6,
color: "#86aeb7",
roof: "#9e503f",
style: "standard"
});
buildFacade({
x: 21.5,
z: -20,
width: 8,
height: 8.2,
color: "#e1d1a4",
roof: "#ad523b",
style: "market"
});
buildFacade({
x: 29.5,
z: -20,
width: 7,
height: 9.7,
color: "#c47b63",
roof: "#9a493b",
style: "balcony"
});
// Side façades face inward toward the plaza.
buildFacade({
x: -30,
z: -12.5,
width: 6.5,
height: 9.1,
yaw: Math.PI / 2,
color: "#e0b96f",
roof: "#aa503b",
style: "market"
});
buildFacade({
x: -30,
z: -5.2,
width: 7,
height: 10.4,
yaw: Math.PI / 2,
color: "#72a7a0",
roof: "#b4523e",
style: "balcony"
});
buildFacade({
x: -30,
z: 2.0,
width: 6.5,
height: 8.3,
yaw: Math.PI / 2,
color: "#d58b73",
roof: "#a84a39",
style: "standard"
});
buildFacade({
x: 30,
z: -10,
width: 7.5,
height: 10.1,
yaw: -Math.PI / 2,
color: "#d9b18a",
roof: "#a94f3b",
style: "balcony"
});
buildFacade({
x: 30,
z: -2,
width: 8,
height: 8.7,
yaw: -Math.PI / 2,
color: "#7b9faa",
roof: "#b45742",
style: "market"
});
buildFacade({
x: 30,
z: 5.7,
width: 7,
height: 9.6,
yaw: -Math.PI / 2,
color: "#d27f69",
roof: "#9c4b3c",
style: "balcony"
});
// -------------------------------------------------------------------------
// Trees, benches, lamps, and small human figures
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1) {
batch.add(
x,
1.45 * scale,
z,
0.48 * scale,
2.9 * scale,
0.48 * scale,
"#70513b"
);
batch.add(
x,
3.45 * scale,
z,
3.4 * scale,
2.1 * scale,
3.25 * scale,
"#527b4f"
);
batch.add(
x - 0.45 * scale,
4.45 * scale,
z + 0.2 * scale,
2.45 * scale,
1.3 * scale,
2.45 * scale,
"#688f54"
);
batch.add(
x + 1.15 * scale,
3.65 * scale,
z - 0.3 * scale,
1.8 * scale,
1.35 * scale,
2.1 * scale,
"#426d47"
);
batch.add(
x - 1.2 * scale,
3.25 * scale,
z - 0.55 * scale,
1.7 * scale,
1.25 * scale,
1.8 * scale,
"#769958"
);
}
[
[-23, -13, 1],
[23, -13, 1.05],
[-23, -4, 0.92],
[23, -4, 0.95],
[-23, 7, 1.04],
[23, 7, 0.9],
[-33, 14, 1.1],
[33, 14, 1.05]
].forEach(([x, z, scale]) => addTree(x, z, scale));
function addBench(x, z, yaw = 0) {
localBox(x, z, yaw, 0, 0.82, 0, 2.9, 0.22, 0.78, "#8c6546");
localBox(x, z, yaw, 0, 1.35, -0.33, 2.9, 0.58, 0.14, "#956d4b");
for (const side of [-1, 1]) {
localBox(
x,
z,
yaw,
side * 1.05,
0.38,
0,
0.18,
0.76,
0.65,
"#3f4d4e"
);
}
}
addBench(-19.8, -6.5, Math.PI / 2);
addBench(19.8, -6.5, -Math.PI / 2);
addBench(-16.5, 10.3, 0);
addBench(16.5, 10.3, 0);
function addLamp(x, z) {
batch.add(x, 0.25, z, 0.72, 0.5, 0.72, "#64706c");
batch.add(x, 2.25, z, 0.19, 4.1, 0.19, "#34494c");
batch.add(x, 4.34, z, 0.66, 0.65, 0.66, "#f2cf78");
batch.add(x, 4.73, z, 0.9, 0.16, 0.9, "#3b4c4c");
}
for (const x of [-22, 22]) {
addLamp(x, -15);
addLamp(x, 0);
addLamp(x, 12);
}
function addPerson(x, z, shirt, yaw = 0, scale = 1) {
const skin = "#b87952";
const trousers = "#334a55";
localBox(
x,
z,
yaw,
0,
1.06 * scale,
0,
0.58 * scale,
0.72 * scale,
0.34 * scale,
shirt
);
localBox(
x,
z,
yaw,
0,
1.65 * scale,
0,
0.42 * scale,
0.42 * scale,
0.4 * scale,
skin
);
for (const side of [-1, 1]) {
localBox(
x,
z,
yaw,
side * 0.16 * scale,
0.48 * scale,
0,
0.2 * scale,
0.78 * scale,
0.24 * scale,
trousers
);
}
}
[
[-13.5, 5.3, "#d4a941", 0.2],
[10.4, -7.1, "#b95749", -0.8],
[17.5, 4.0, "#4f7f9a", 1.1],
[-20.8, -8.4, "#e3d8bd", 0.4],
[20.3, 9.2, "#617e55", -0.2],
[-7.8, -10.2, "#8f5d85", 0.7],
[6.5, 11.0, "#c4774f", -0.5],
[-29.5, 11.0, "#507b91", 0.4],
[29.0, 13.0, "#d4a341", -0.7]
].forEach(([x, z, color, yaw]) => addPerson(x, z, color, yaw));
// -------------------------------------------------------------------------
// Small boats in the river
// -------------------------------------------------------------------------
function addBoat(x, z, yaw = 0, color = "#d9d1b6") {
localBox(x, z, yaw, 0, 0.03, 0, 6.4, 0.62, 2.25, "#315563");
localBox(x, z, yaw, 0, 0.38, 0, 5.9, 0.42, 2.0, color);
localBox(x, z, yaw, -0.65, 0.92, 0, 2.15, 1.0, 1.65, "#e3dcc6");
localBox(x, z, yaw, -0.65, 1.46, 0, 2.5, 0.18, 1.9, "#aa5445");
localBox(x, z, yaw, 1.9, 1.3, 0, 0.13, 2.6, 0.13, "#65543f");
}
addBoat(13, 33, -0.16, "#d8c79c");
addBoat(-29, 43, 0.22, "#d9d7c6");
addBoat(39, 57, -0.11, "#c8d1c4");
// Build the complete static scene in one instanced draw call.
batch.build();
// -------------------------------------------------------------------------
// Animated river highlights
// -------------------------------------------------------------------------
const waveCount = 42;
const waveMesh = new THREE.InstancedMesh(
unitBox,
new THREE.MeshBasicMaterial({
color: "#c3eef0",
transparent: true,
opacity: 0.38,
depthWrite: false
}),
waveCount
);
waveMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
waveMesh.frustumCulled = false;
waveMesh.renderOrder = 2;
scene.add(waveMesh);
const waves = [];
for (let i = 0; i < waveCount; i++) {
waves.push({
x: -85 + random() * 170,
z: random() * 96,
length: 1.3 + random() * 4.8,
speed: 0.25 + random() * 0.65,
phase: random() * Math.PI * 2
});
}
const waveMatrix = new THREE.Matrix4();
const wavePosition = new THREE.Vector3();
const waveScale = new THREE.Vector3();
const waveQuaternion = new THREE.Quaternion();
function updateWaves(time) {
waves.forEach((wave, index) => {
const z = 26 + ((wave.z + time * wave.speed) % 96);
const x = wave.x + Math.sin(time * 0.55 + wave.phase) * 0.45;
wavePosition.set(
x,
-0.365 + Math.sin(time * 0.8 + wave.phase) * 0.018,
z
);
waveScale.set(wave.length, 0.035, 0.12);
waveMatrix.compose(wavePosition, waveQuaternion, waveScale);
waveMesh.setMatrixAt(index, waveMatrix);
});
waveMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Local camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
const views = {
overview: {
target: new THREE.Vector3(0, 1.5, -2),
radius: 88,
theta: 0.62,
phi: 0.76
},
plaza: {
target: new THREE.Vector3(0, 0.6, 0),
radius: 59,
theta: 0.88,
phi: 0.48
},
river: {
target: new THREE.Vector3(0, 1.7, 13),
radius: 63,
theta: 0,
phi: 1.16
}
};
const goal = {
target: views.overview.target.clone(),
radius: views.overview.radius,
theta: views.overview.theta,
phi: views.overview.phi
};
const current = {
target: views.overview.target.clone(),
radius: views.overview.radius,
theta: views.overview.theta,
phi: views.overview.phi
};
const canvas = renderer.domElement;
let dragging = false;
let panning = false;
let pointerX = 0;
let pointerY = 0;
function setView(name) {
const view = views[name];
goal.target.copy(view.target);
goal.radius = view.radius;
goal.theta = view.theta;
goal.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));
});
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
dragging = true;
panning = event.shiftKey || event.button === 2;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
if (panning) {
const scale = goal.radius * 0.0012;
const right = new THREE.Vector3(
Math.cos(goal.theta),
0,
-Math.sin(goal.theta)
);
const forward = new THREE.Vector3(
Math.sin(goal.theta),
0,
Math.cos(goal.theta)
);
goal.target.addScaledVector(right, -dx * scale);
goal.target.addScaledVector(forward, dy * scale);
goal.target.x = THREE.MathUtils.clamp(goal.target.x, -34, 34);
goal.target.z = THREE.MathUtils.clamp(goal.target.z, -28, 30);
goal.target.y = THREE.MathUtils.clamp(goal.target.y, 0, 8);
} else {
goal.theta -= dx * 0.005;
goal.phi = THREE.MathUtils.clamp(
goal.phi - dy * 0.004,
0.18,
1.43
);
}
});
function endDrag(event) {
dragging = false;
panning = false;
canvas.classList.remove("dragging");
if (event && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
goal.radius = THREE.MathUtils.clamp(
goal.radius * Math.exp(event.deltaY * 0.001),
24,
145
);
},
{ passive: false }
);
addEventListener("keydown", (event) => {
const key = event.key.toLowerCase();
if (key === "arrowleft") goal.theta += 0.08;
if (key === "arrowright") goal.theta -= 0.08;
if (key === "arrowup") goal.phi = Math.max(0.18, goal.phi - 0.06);
if (key === "arrowdown") goal.phi = Math.min(1.43, goal.phi + 0.06);
if (key === "+" || key === "=") goal.radius = Math.max(24, goal.radius - 4);
if (key === "-") goal.radius = Math.min(145, goal.radius + 4);
if (key === "r") setView("overview");
});
function updateCamera(delta) {
const ease = 1 - Math.exp(-delta * 8);
current.target.lerp(goal.target, ease);
current.radius = THREE.MathUtils.lerp(current.radius, goal.radius, ease);
current.theta = THREE.MathUtils.lerp(current.theta, goal.theta, ease);
current.phi = THREE.MathUtils.lerp(current.phi, goal.phi, ease);
const horizontal = Math.sin(current.phi) * current.radius;
camera.position.set(
current.target.x + horizontal * Math.sin(current.theta),
current.target.y + Math.cos(current.phi) * current.radius,
current.target.z + horizontal * Math.cos(current.theta)
);
camera.lookAt(current.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// Projected map labels
// -------------------------------------------------------------------------
const labels = [
{
element: document.getElementById("label-square"),
position: new THREE.Vector3(0, 0.35, 0)
},
{
element: document.getElementById("label-river"),
position: new THREE.Vector3(0, 0.25, 26)
},
{
element: document.getElementById("label-oldtown"),
position: new THREE.Vector3(-11, 12.6, -16)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const rect = canvas.getBoundingClientRect();
for (const label of labels) {
projected.copy(label.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
label.element.style.opacity = visible ? "1" : "0";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * rect.width + rect.left;
const y = (-projected.y * 0.5 + 0.5) * rect.height + rect.top;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.75));
});
let previousTime = performance.now();
function animate(time) {
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
updateCamera(delta);
updateWaves(time / 1000);
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateCamera(1);
updateWaves(0);
requestAnimationFrame(animate);
</script>
</body>
</html>
```