Prompt specification
Build or customize the Three.js 3D scene "Hanoi Old Quarter intersection 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: Hanoi Old Quarter intersection slice
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hanoi-old-quarter-intersection-slice-x2979
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows Hanoi's Old Quarter. Build a bounded low-poly scene with referenced narrow facades, intersecting streets, and mixed everyday uses. Use a small number of controlled moving vehicles so the spatial arrangement remains readable. Low-poly.
Working Three.js Starter (hanoi-old-quarter-intersection-slice-x2979):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hanoi's Old Quarter — Low-Poly Scene</title>
<style>
:root {
--ink: #272923;
--paper: rgba(248, 241, 220, 0.92);
--accent: #b65038;
--line: rgba(54, 53, 44, 0.2);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #cbd0c8;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.title-card {
position: fixed;
top: 20px;
left: 20px;
width: min(360px, calc(100vw - 40px));
padding: 17px 19px 16px;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.5);
border-top: 4px solid var(--accent);
box-shadow: 0 12px 35px rgba(52, 48, 37, 0.16);
backdrop-filter: blur(10px);
pointer-events: none;
z-index: 5;
}
.eyebrow {
margin: 0 0 4px;
color: #8c4937;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 39px);
font-weight: 600;
line-height: 1;
letter-spacing: -0.035em;
}
.subtitle {
margin: 8px 0 0;
max-width: 300px;
color: #66675f;
font-size: 12px;
line-height: 1.45;
}
.controls {
position: fixed;
top: 20px;
right: 20px;
display: flex;
gap: 8px;
z-index: 6;
}
button {
border: 1px solid rgba(54, 53, 44, 0.16);
border-radius: 7px;
padding: 10px 13px;
background: var(--paper);
color: var(--ink);
box-shadow: 0 5px 18px rgba(52, 48, 37, 0.12);
font: 700 11px/1 ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.04em;
cursor: pointer;
backdrop-filter: blur(8px);
}
button:hover {
transform: translateY(-1px);
background: #fff8e8;
}
button:active { transform: translateY(0); }
.legend {
position: fixed;
right: 20px;
bottom: 20px;
padding: 11px 13px;
background: rgba(248, 241, 220, 0.84);
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 7px;
color: #5e6058;
font-size: 10px;
line-height: 1.55;
box-shadow: 0 6px 22px rgba(52, 48, 37, 0.1);
pointer-events: none;
z-index: 5;
}
.legend strong {
color: #35372f;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: #d8d2be;
z-index: 20;
transition: opacity 0.45s ease;
font: 700 12px/1.4 ui-sans-serif, system-ui, sans-serif;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
.title-card { top: 12px; left: 12px; padding: 13px 15px; }
.subtitle { display: none; }
.controls { top: 12px; right: 12px; }
button { padding: 9px 10px; font-size: 10px; }
.legend { right: 12px; bottom: 12px; }
}
</style>
</head>
<body>
<div id="loading">Constructing the old quarter…</div>
<section class="title-card">
<p class="eyebrow">Vietnam · 36 × 30 m study</p>
<h1>Hanoi's Old Quarter</h1>
<p class="subtitle">
Narrow tube-house facades, shaded shopfronts, cafés, markets, rooftop water
tanks, overhead wires, and slow traffic through intersecting lanes.
</p>
</section>
<div class="controls">
<button id="trafficButton" type="button">Pause traffic</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="legend">
<strong>Camera</strong><br>
Drag: orbit · Shift/right-drag: pan · Wheel/pinch: zoom
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xcbd2cc);
scene.fog = new THREE.Fog(0xcbd2cc, 72, 135);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
220
);
const hemi = new THREE.HemisphereLight(0xf5ead0, 0x63736c, 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe0a5, 3.3);
sun.position.set(-28, 48, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -45;
sun.shadow.camera.right = 45;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 110;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ab8c4, 0.55);
fill.position.set(35, 20, -30);
scene.add(fill);
let seed = 1842;
function random() {
seed |= 0;
seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
const rand = (a, b) => a + (b - a) * random();
const mat = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0,
flatShading: true,
...options
});
const M = {
base: mat(0x827966),
ground: mat(0xb9aa8e),
sidewalk: mat(0xc8bba1),
curb: mat(0xd8ccb4),
road: mat(0x555750),
roadEdge: mat(0x74736a),
dark: mat(0x252a29),
window: mat(0x334746, {
roughness: 0.35,
metalness: 0.18,
emissive: 0x182322,
emissiveIntensity: 0.35
}),
shutter: mat(0x8c493a),
shutterGreen: mat(0x496c5d),
shutterBlue: mat(0x4e6874),
wood: mat(0x684a36),
trim: mat(0xe5d8bc),
awningRed: mat(0xb74e39),
awningCream: mat(0xeee0bd),
awningGreen: mat(0x4f7768),
metal: mat(0x66706c, { metalness: 0.48, roughness: 0.52 }),
rubber: mat(0x202322),
lamp: mat(0xffd18b, {
emissive: 0xffb84d,
emissiveIntensity: 1.35
}),
foliage: mat(0x4e7352),
foliageLight: mat(0x71885c),
trunk: mat(0x68503a),
produce: [
mat(0xd45636),
mat(0xe2a434),
mat(0x658848),
mat(0xb8833a)
]
};
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 ico(radius, material, x, y, z, parent = scene, detail = 0) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, detail),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function line(points, color = 0x343633, parent = scene, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(p => new THREE.Vector3(...p))
);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity
});
const object = new THREE.Line(geometry, material);
parent.add(object);
return object;
}
// ---------------------------------------------------------------------
// Base and street network
// ---------------------------------------------------------------------
box(60, 1.25, 48, M.base, 0, -0.7, 0);
const ground = box(59.5, 0.12, 47.5, M.ground, 0, -0.02, 0);
ground.castShadow = false;
box(60.3, 0.24, 0.24, M.curb, 0, -0.25, 24.1);
box(60.3, 0.24, 0.24, M.curb, 0, -0.25, -24.1);
box(0.24, 0.24, 48.3, M.curb, 30.1, -0.25, 0);
box(0.24, 0.24, 48.3, M.curb, -30.1, -0.25, 0);
function road(w, d, x, z, y = 0.055) {
const mesh = box(w, 0.07, d, M.road, x, y, z);
mesh.castShadow = false;
return mesh;
}
// Main east-west street and its narrow sidewalks.
road(58.5, 6, 0, 0, 0.06);
box(58.5, 0.11, 1.05, M.sidewalk, 0, 0.055, -3.52);
box(58.5, 0.11, 1.05, M.sidewalk, 0, 0.055, 3.52);
// Two older north-south streets.
road(4.4, 44, -14.5, 0, 0.075);
box(0.8, 0.11, 44, M.sidewalk, -17.05, 0.06, 0);
box(0.8, 0.11, 44, M.sidewalk, -11.95, 0.06, 0);
road(4.8, 44, 8, 0, 0.08);
box(0.8, 0.11, 44, M.sidewalk, 5.25, 0.065, 0);
box(0.8, 0.11, 44, M.sidewalk, 10.75, 0.065, 0);
// Rear and front lanes.
road(58.5, 3.1, 0, -15, 0.055);
box(58.5, 0.1, 0.7, M.sidewalk, 0, 0.05, -16.9);
box(58.5, 0.1, 0.7, M.sidewalk, 0, 0.05, -13.15);
road(58.5, 3, 0, 13, 0.055);
box(58.5, 0.1, 0.7, M.sidewalk, 0, 0.05, 11.15);
box(58.5, 0.1, 0.7, M.sidewalk, 0, 0.05, 14.85);
// Narrow perimeter connectors used by the bicycle route.
road(2.15, 13.6, -25, 6.5, 0.065);
road(2.15, 13.6, 25.5, 6.5, 0.065);
// Drain lines and worn central patches.
for (let x = -27; x <= 27; x += 5.5) {
box(2.6, 0.018, 0.055, M.roadEdge, x, 0.105, 0);
}
// Zebra crossings at the main intersections.
function crossing(x, horizontal = true) {
for (let i = 0; i < 6; i++) {
const offset = -2 + i * 0.8;
if (horizontal) {
box(0.38, 0.02, 2.1, M.trim, x + offset, 0.115, 0);
} else {
box(2.1, 0.02, 0.38, M.trim, x, 0.115, offset);
}
}
}
crossing(-18.3);
crossing(-10.7);
crossing(5.0);
crossing(11.0);
// ---------------------------------------------------------------------
// Procedural signage
// ---------------------------------------------------------------------
function signTexture(text, background = "#a94331", foreground = "#fff0c9") {
const vertical = text.includes("|");
const canvas = document.createElement("canvas");
canvas.width = vertical ? 128 : 512;
canvas.height = vertical ? 512 : 96;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = foreground;
ctx.lineWidth = vertical ? 6 : 4;
ctx.strokeRect(7, 7, canvas.width - 14, canvas.height - 14);
ctx.fillStyle = foreground;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
const lines = vertical ? text.split("|") : [text];
const maxWidth = canvas.width - 28;
let fontSize = vertical ? 48 : 47;
do {
ctx.font = `700 ${fontSize}px Arial, sans-serif`;
const fits = lines.every(line => ctx.measureText(line).width <= maxWidth);
if (fits) break;
fontSize -= 2;
} while (fontSize > 18);
const lineHeight = fontSize * 1.15;
const startY = canvas.height / 2 - (lines.length - 1) * lineHeight / 2;
lines.forEach((line, index) => {
ctx.fillText(line, canvas.width / 2, startY + index * lineHeight);
});
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(4, renderer.capabilities.getMaxAnisotropy());
return texture;
}
function signMesh(text, w, h, x, y, z, parent = scene, vertical = false) {
const material = new THREE.MeshBasicMaterial({
map: signTexture(text, vertical ? "#a44332" : "#325f55"),
side: THREE.DoubleSide,
toneMapped: false
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(w, h), material);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Tube-house and shophouse generator
// ---------------------------------------------------------------------
const facadePalette = [
0xd2a24a, 0xe0c489, 0xb9674d, 0x6e8b7a,
0x6d8490, 0xc98b68, 0xb9a06f, 0xd6b47a,
0x9d715d, 0x78917d
];
const shopNames = [
"PHỞ BÒ", "CÀ PHÊ", "TẠP HÓA", "BÚN CHẢ",
"BIA HƠI", "BÁNH MÌ", "MỸ PHẨM", "HOA QUẢ",
"CƠM TẤM", "NƯỚC MÍA"
];
function makeBuilding({
x, z, w, d, h, color = facadePalette[0], rotation = 0,
name = "PHỞ", commercial = true, balcony = true
}) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const bodyMaterial = mat(color);
const trimMaterial = mat(new THREE.Color(color).multiplyScalar(1.13));
box(w, h, d, bodyMaterial, 0, h / 2, 0, group);
box(w + 0.12, 0.24, d + 0.12, trimMaterial, 0, h - 0.08, 0, group);
box(w + 0.08, 0.42, d + 0.08, M.curb, 0, 0.22, 0, group);
// Corner pilasters emphasize the narrow plots.
box(0.15, h - 0.3, 0.14, trimMaterial, -w / 2 + 0.08, h / 2, d / 2 + 0.02, group);
box(0.15, h - 0.3, 0.14, trimMaterial, w / 2 - 0.08, h / 2, d / 2 + 0.02, group);
const front = d / 2 + 0.035;
// Ground-floor shopfront or residential doorway.
box(w - 0.34, 1.95, 0.075, M.dark, 0, 1.28, front, group);
const doorWidth = commercial ? 0.72 : 0.88;
const doorMaterial = commercial ? M.window : M.wood;
box(doorWidth, 1.65, 0.09, doorMaterial, -w * 0.16, 1.18, front + 0.055, group);
box(w * 0.52, 1.65, 0.09, M.window, w * 0.18, 1.18, front + 0.055, group);
box(0.045, 0.08, 0.06, M.trim, w * 0.06, 1.18, front + 0.12, group);
if (commercial) {
const stripeCount = Math.max(4, Math.round(w / 0.65));
const stripeWidth = (w - 0.2) / stripeCount;
for (let i = 0; i < stripeCount; i++) {
const material = i % 2 === 0 ? M.awningRed : M.awningCream;
box(
stripeWidth + 0.01, 0.08, 1.05,
material,
-w / 2 + 0.1 + stripeWidth * (i + 0.5),
2.53,
front + 0.49,
group
);
}
box(w - 0.28, 0.06, 0.06, M.wood, 0, 2.47, front + 1.0, group);
signMesh(name, w * 0.82, 0.58, 0, 3.02, front + 0.11, group);
} else {
box(w * 0.48, 0.18, 0.11, trimMaterial, 0, 2.52, front + 0.06, group);
}
const upperFloors = Math.max(1, Math.floor((h - 3.6) / 2.9));
const shutterMaterials = [M.shutter, M.shutterGreen, M.shutterBlue];
for (let floor = 0; floor < upperFloors; floor++) {
const y = 4.25 + floor * 2.9;
const windowCount = w > 3.65 ? 2 : 1;
for (let j = 0; j < windowCount; j++) {
const wx = windowCount === 1
? 0
: -w * 0.23 + j * w * 0.46;
box(0.78, 1.16, 0.075, M.window, wx, y, front + 0.035, group);
box(0.9, 0.08, 0.13, trimMaterial, wx, y + 0.61, front + 0.07, group);
box(0.9, 0.08, 0.13, trimMaterial, wx, y - 0.61, front + 0.07, group);
const shutter = shutterMaterials[(floor + j) % shutterMaterials.length];
box(0.24, 1.16, 0.07, shutter, wx - 0.51, y, front + 0.08, group);
box(0.24, 1.16, 0.07, shutter, wx + 0.51, y, front + 0.08, group);
if (balcony && floor > 0 && floor % 2 === 0) {
box(w - 0.28, 0.1, 0.72, M.trim, 0, y - 0.72, front + 0.31, group);
box(w - 0.34, 0.06, 0.06, M.metal, 0, y - 0.24, front + 0.65, group);
for (let b = 0; b < 5; b++) {
box(
0.045, 0.48, 0.045,
M.metal,
-w * 0.4 + b * w * 0.2,
y - 0.47,
front + 0.65,
group
);
}
}
}
// A few side windows make the side elevations readable.
box(0.055, 1.02, 0.72, M.window, w / 2 + 0.025, y, -d * 0.12, group);
box(0.055, 1.02, 0.72, M.window, -w / 2 - 0.025, y, d * 0.18, group);
}
if (commercial && random() > 0.28) {
signMesh(name.replace(" ", "|"), 0.64, 3.65, w / 2 - 0.29, 5.15, front + 0.16, group, true);
}
// Rooftop water tank and small service structure.
if (random() > 0.22) {
cylinder(0.45, 0.45, 0.72, 10, M.metal, -w * 0.18, h + 0.62, -d * 0.1, group);
cylinder(0.48, 0.48, 0.08, 10, M.dark, -w * 0.18, h + 1.01, -d * 0.1, group);
for (const sx of [-0.31, 0.31]) {
box(0.08, 0.35, 0.08, M.metal, -w * 0.18 + sx, h + 0.2, -d * 0.1, group);
}
}
if (random() > 0.52) {
box(w * 0.43, 0.75, d * 0.32, trimMaterial, w * 0.13, h + 0.38, d * 0.12, group);
}
// Aerial and occasional rooftop laundry rail.
line([
[w * 0.27, h, -d * 0.25],
[w * 0.27, h + 1.1, -d * 0.25]
], 0x4b4b45, group);
if (balcony && random() > 0.45) {
line([
[-w * 0.34, 6.8, front + 0.64],
[w * 0.34, 6.55, front + 0.64]
], 0x4b4b45, group);
const clothes = [M.awningCream, M.shutterBlue, M.awningGreen];
for (let i = 0; i < 3; i++) {
box(
0.3, 0.48, 0.025,
clothes[i % clothes.length],
-w * 0.22 + i * w * 0.22,
6.37 + (i % 2) * 0.08,
front + 0.66,
group
);
}
}
return group;
}
let buildingIndex = 0;
function building(x, z, w, d, h, rotation = 0, commercial = true) {
const name = shopNames[buildingIndex % shopNames.length];
const color = facadePalette[(buildingIndex * 3 + 1) % facadePalette.length];
buildingIndex++;
return makeBuilding({
x, z, w, d, h, rotation, color, name, commercial,
balcony: !commercial || buildingIndex % 3 !== 0
});
}
// North side of the main street.
building(-27, -7.45, 3.2, 7.4, 11.3);
building(-23.5, -7.25, 3.25, 7.2, 13.5);
building(-19.9, -7.8, 3.1, 8.1, 10.2);
building(-10.6, -8.0, 3.1, 8.6, 12.8);
building(-6.8, -7.7, 3.25, 8.0, 10.7);
building(-2.9, -8.05, 3.3, 8.8, 14.0);
building(1.0, -7.5, 3.2, 7.8, 11.7);
building(4.5, -8.0, 3.1, 8.6, 9.8);
building(12.0, -7.7, 3.2, 8.0, 12.4);
building(16.0, -7.5, 3.25, 7.8, 10.3);
building(20.0, -7.8, 3.2, 8.2, 13.6);
building(24.0, -7.45, 3.25, 7.6, 11.0);
building(27.3, -7.7, 2.8, 8.0, 9.6);
// South side of the main street.
building(-27, 7.2, 3.2, 6.8, 10.4, Math.PI);
building(-23.4, 7.5, 3.25, 7.0, 12.9, Math.PI);
building(-19.8, 7.2, 3.1, 6.8, 9.8, Math.PI);
building(-10.5, 7.4, 3.1, 7.2, 11.6, Math.PI);
building(-6.7, 7.1, 3.25, 6.8, 13.1, Math.PI);
building(-2.8, 7.5, 3.3, 7.3, 10.6, Math.PI);
building(1.0, 7.3, 3.2, 7.0, 12.2, Math.PI);
building(4.5, 7.7, 3.1, 7.6, 9.7, Math.PI);
building(12.0, 7.5, 3.2, 7.2, 11.0, Math.PI);
building(16.0, 7.2, 3.25, 6.8, 13.5, Math.PI);
building(20.0, 7.6, 3.2, 7.4, 10.4, Math.PI);
building(24.0, 7.3, 3.25, 6.9, 12.0, Math.PI);
building(27.3, 7.6, 2.8, 7.2, 9.5, Math.PI);
// Smaller facades turned toward the intersecting lanes.
building(-10.55, -9.7, 3.25, 3.5, 10.5, -Math.PI / 2);
building(-10.55, -6.3, 3.2, 3.5, 12.0, -Math.PI / 2);
building(3.8, -9.7, 3.2, 3.5, 11.4, Math.PI / 2);
building(3.8, -6.3, 3.2, 3.5, 9.8, Math.PI / 2);
building(12.15, -9.7, 3.2, 3.5, 12.0, -Math.PI / 2);
building(12.15, -6.3, 3.2, 3.5, 10.3, -Math.PI / 2);
building(-18.45, 6.0, 3.2, 3.4, 10.6, Math.PI / 2);
building(-18.45, 9.4, 3.2, 3.4, 12.5, Math.PI / 2);
building(3.8, 6.0, 3.2, 3.4, 11.8, Math.PI / 2);
building(3.8, 9.4, 3.2, 3.4, 9.6, Math.PI / 2);
building(12.15, 6.0, 3.2, 3.4, 12.1, -Math.PI / 2);
building(12.15, 9.4, 3.2, 3.4, 10.1, -Math.PI / 2);
// Rear and front perimeter blocks.
const northXs = [-27, -23.5, -20, -10.5, -6.6, -2.8, 1.0, 4.5, 12, 16, 20, 24, 27.2];
for (const x of northXs) {
building(x, -19.25, rand(2.9, 3.35), 4.8, rand(8.7, 12.8), 0, random() > 0.4);
}
const southXs = [-27, -23.4, -19.8, -10.5, -6.7, -2.8, 1.0, 4.5, 12, 16, 20, 24, 27.2];
for (const x of southXs) {
building(x, 17.75, rand(2.9, 3.35), 5.5, rand(8.8, 12.5), Math.PI, random() > 0.4);
}
// ---------------------------------------------------------------------
// Street furniture and everyday uses
// ---------------------------------------------------------------------
function streetLamp(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
cylinder(0.065, 0.095, 4.2, 8, M.dark, 0, 2.1, 0, group);
box(0.58, 0.08, 0.08, M.dark, 0.23, 4.12, 0, group);
box(0.28, 0.15, 0.28, M.lamp, 0.48, 4.02, 0, group);
box(0.33, 0.08, 0.33, M.dark, 0.48, 4.12, 0, group);
}
streetLamp(-11.8, 3.35, 0);
streetLamp(4.9, -3.35, Math.PI);
streetLamp(10.9, 7.0, Math.PI / 2);
streetLamp(-12.2, -8.0, -Math.PI / 2);
streetLamp(-22.0, 11.45, 0);
streetLamp(22.5, 14.55, Math.PI);
streetLamp(-8.5, -16.55, 0);
streetLamp(18.0, -13.3, Math.PI);
function bollard(x, z) {
cylinder(0.07, 0.1, 0.62, 7, M.dark, x, 0.31, z);
ico(0.1, M.trim, x, 0.65, z);
}
for (let x = -27; x <= 27; x += 3.2) {
bollard(x, 3.28);
if (Math.round(x) % 2 === 0) bollard(x, -3.28);
}
function tree(x, z, scale = 1) {
cylinder(0.13, 0.2, 2.2, 7, M.trunk, x, 1.1, z);
const crown = ico(1.05, M.foliage, x, 2.7, z);
crown.scale.set(scale, rand(0.85, 1.15) * scale, scale);
crown.rotation.set(rand(-0.2, 0.2), rand(0, Math.PI), rand(-0.2, 0.2));
ico(0.7, M.foliageLight, x + 0.45, 2.5, z + 0.2).scale.setScalar(scale);
}
tree(-28, -21.8, 1.1);
tree(-22.8, -21.6, 0.9);
tree(28.2, -21.5, 1.05);
tree(27.9, 21.5, 1.1);
tree(-28.3, 21.6, 0.95);
tree(-12.0, 21.4, 0.8);
tree(28.0, 4.0, 0.85);
// Café tables and low chairs.
function cafeSet(x, z) {
cylinder(0.48, 0.48, 0.09, 10, M.wood, x, 0.82, z);
cylinder(0.055, 0.07, 0.78, 7, M.metal, x, 0.41, z);
box(0.4, 0.07, 0.4, M.dark, x, 0.06, z);
box(0.36, 0.38, 0.08, M.shutter, x, 0.31, z + 0.36);
box(0.36, 0.07, 0.36, M.wood, x, 0.17, z + 0.58);
cylinder(0.11, 0.1, 0.19, 8, M.awningCream, x, 1.0, z);
}
cafeSet(-4.0, -3.13);
cafeSet(-1.9, -3.2);
cafeSet(-3.0, -3.25);
// Plastic stools outside a bia hoi shop.
function stool(x, z, color) {
cylinder(0.2, 0.18, 0.08, 8, color, x, 0.48, z);
for (let i = 0; i < 3; i++) {
const angle = i * Math.PI * 2 / 3;
cylinder(0.025, 0.035, 0.45, 6, color,
x + Math.cos(angle) * 0.12, 0.24,
z + Math.sin(angle) * 0.12
);
}
}
stool(18.0, -3.15, M.shutter);
stool(18.55, -3.25, M.shutterGreen);
stool(19.15, -3.1, M.shutterBlue);
stool(18.3, -2.65, M.shutter);
// Fruit and vegetable market.
function marketStall(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = Math.PI;
scene.add(group);
for (const px of [-1.25, 1.25]) {
for (const pz of [-0.55, 0.55]) {
cylinder(0.045, 0.055, 2.2, 7, M.metal, px, 1.1, pz, group);
}
}
box(2.9, 0.12, 1.5, M.wood, 0, 0.82, 0, group);
for (let i = 0; i < 6; i++) {
box(0.49, 0.08, 1.55, i % 2 ? M.awningCream : M.awningGreen,
-1.225 + i * 0.49, 2.18, 0, group);
}
for (let row = 0; row < 2; row++) {
for (let col = 0; col < 5; col++) {
const crate = box(
0.43, 0.22, 0.5,
M.wood,
-0.88 + col * 0.44,
1.0 + row * 0.25,
-0.18 + row * 0.35,
group
);
crate.castShadow = true;
for (let k = 0; k < 3; k++) {
ico(
0.11,
M.produce[(row + col + k) % M.produce.length],
crate.position.x + rand(-0.12, 0.12),
crate.position.y + 0.17,
crate.position.z + rand(-0.12, 0.12),
group
);
}
}
}
}
marketStall(2.6, 3.75);
// Workshop crates and stacked containers.
function crate(x, z, size = 0.62) {
box(size, size * 0.62, size * 0.8, M.wood, x, size * 0.31, z);
box(size + 0.03, 0.055, size * 0.83, M.trim, x, size * 0.45, z);
}
crate(-27.0, 3.35, 0.7);
crate(-26.25, 3.45, 0.55);
crate(-26.8, 3.4, 0.48);
crate(22.5, 3.5, 0.68);
crate(23.3, 3.55, 0.58);
crate(-5.3, -16.95, 0.62);
crate(-4.55, -16.95, 0.5);
// Small neighborhood shrine at a lane junction.
function shrine() {
const group = new THREE.Group();
group.position.set(-17.25, 0, 3.8);
scene.add(group);
box(1.55, 0.18, 1.15, M.curb, 0, 0.09, 0, group);
box(1.12, 1.7, 0.75, M.shutter, 0, 0.98, 0, group);
box(1.42, 0.18, 0.95, M.awningGreen, 0, 1.91, 0, group);
for (const x of [-0.48, 0.48]) {
cylinder(0.07, 0.08, 1.65, 8, M.awningRed, x, 0.95, 0.28, group);
}
box(1.2, 0.1, 0.9, M.awningRed, 0, 2.02, 0, group);
box(0.46, 0.55, 0.05, M.lamp, 0, 1.1, 0.41, group);
}
shrine();
// Utility wires sag over the main street.
function utilityWire(x1, x2, z, y, sag = 1.0) {
const points = [];
for (let i = 0; i <= 12; i++) {
const t = i / 12;
points.push([
THREE.MathUtils.lerp(x1, x2, t),
y - Math.sin(t * Math.PI) * sag,
z
]);
}
line(points, 0x3e403b);
}
utilityWire(-10.7, 5.2, -3.6, 9.1, 0.85);
utilityWire(-10.5, 5.3, -3.35, 8.55, 0.65);
utilityWire(-10.8, 5.1, 3.55, 9.5, 0.8);
utilityWire(-17.1, -11.9, -7.5, 8.8, 0.7);
utilityWire(10.8, 5.3, 6.8, 9.0, 0.7);
// ---------------------------------------------------------------------
// People
// ---------------------------------------------------------------------
function person(x, z, shirtColor, rotation = 0, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
group.scale.setScalar(scale);
scene.add(group);
const shirt = mat(shirtColor);
const trousers = mat(0x3b4241);
const skin = mat(0xc99068);
cylinder(0.21, 0.25, 0.66, 7, shirt, 0, 1.08, 0, group);
ico(0.18, skin, 0, 1.57, 0, group);
cylinder(0.075, 0.075, 0.62, 6, trousers, -0.11, 0.42, 0, group);
cylinder(0.075, 0.075, 0.62, 6, trousers, 0.11, 0.42, 0, group);
const leftArm = box(0.1, 0.53, 0.12, shirt, -0.28, 1.05, 0, group);
const rightArm = box(0.1, 0.53, 0.12, shirt, 0.28, 1.05, 0, group);
leftArm.rotation.z = -0.08;
rightArm.rotation.z = 0.08;
}
person(-8.2, -3.25, 0xb75b42, 0.4);
person(-6.9, -3.3, 0x527a70, -0.5);
person(4.4, 3.3, 0xd6b071, Math.PI);
person(9.8, 3.4, 0x647f8b, Math.PI - 0.4);
person(-14.9, 5.2, 0x9d5747, 1.2);
person(7.4, -10.2, 0x587a6d, -0.8);
person(-23.2, 11.7, 0xd3c2a0, Math.PI);
person(23.0, 11.7, 0x9b5d4c, Math.PI);
person(-28.0, 5.3, 0x537b82, 0.2);
person(27.8, -8.2, 0xc98e48, -1.0);
// ---------------------------------------------------------------------
// Vehicles
// ---------------------------------------------------------------------
function wheel(radius, width, material, x, y, z, parent) {
const mesh = cylinder(radius, radius, width, 12, material, x, y, z, parent);
mesh.rotation.z = Math.PI / 2;
return mesh;
}
function motorbike() {
const group = new THREE.Group();
const bodyMaterial = mat(0xb64e38);
const wheels = [];
wheels.push(wheel(0.31, 0.16, M.rubber, 0, 0.34, 0.62, group));
wheels.push(wheel(0.31, 0.16, M.rubber, 0, 0.34, -0.62, group));
box(0.28, 0.3, 1.12, bodyMaterial, 0, 0.56, 0.05, group);
box(0.42, 0.13, 0.64, M.dark, 0, 0.83, -0.12, group);
box(0.17, 0.62, 0.15, M.metal, 0, 0.72, 0.57, group);
box(0.67, 0.07, 0.08, M.metal, 0, 1.05, 0.52, group);
box(0.24, 0.18, 0.1, M.lamp, 0, 0.89, 0.68, group);
// Rider.
cylinder(0.2, 0.23, 0.58, 7, mat(0x496c75), 0, 1.17, -0.08, group);
ico(0.17, mat(0xc38e65), 0, 1.62, 0.03, group);
box(0.11, 0.48, 0.12, mat(0x496c75), -0.25, 1.13, 0.15, group);
box(0.11, 0.48, 0.12, mat(0x496c75), 0.25, 1.13, 0.15, group);
box(0.14, 0.5, 0.14, M.dark, -0.15, 0.61, 0.08, group);
box(0.14, 0.5, 0.14, M.dark, 0.15, 0.61, 0.08, group);
group.userData.wheels = wheels;
return group;
}
function bicycle() {
const group = new THREE.Group();
const frame = mat(0x49776d);
const wheels = [];
wheels.push(wheel(0.41, 0.12, M.rubber, 0, 0.44, 0.72, group));
wheels.push(wheel(0.41, 0.12, M.rubber, 0, 0.44, -0.72, group));
const bar1 = box(0.055, 0.055, 1.05, frame, 0, 0.66, 0.12, group);
const bar2 = box(0.055, 0.7, 0.055, frame, 0, 0.75, -0.05, group);
const bar3 = box(0.055, 0.055, 0.95, frame, 0, 0.75, -0.36, group);
bar2.rotation.x = -0.35;
box(0.31, 0.08, 0.35, M.dark, 0, 1.05, -0.24, group);
box(0.58, 0.055, 0.055, M.metal, 0, 1.13, 0.51, group);
box(0.3, 0.06, 0.28, M.metal, 0, 0.96, -0.51, group);
group.userData.wheels = wheels;
return group;
}
const motorbikeMesh = motorbike();
const bicycleMesh = bicycle();
scene.add(motorbikeMesh, bicycleMesh);
const motorRoute = new THREE.CatmullRomCurve3([
new THREE.Vector3(-13.1, 0.08, 0.82),
new THREE.Vector3(6.4, 0.08, 0.82),
new THREE.Vector3(7.55, 0.08, -0.5),
new THREE.Vector3(7.55, 0.08, -13.2),
new THREE.Vector3(6.6, 0.08, -14.55),
new THREE.Vector3(-12.8, 0.08, -14.55),
new THREE.Vector3(-14.05, 0.08, -13.2),
new THREE.Vector3(-14.05, 0.08, -1.0),
new THREE.Vector3(-13.1, 0.08, 0.82)
], true, "centripetal");
const bicycleRoute = new THREE.CatmullRomCurve3([
new THREE.Vector3(-24.55, 0.08, 12.55),
new THREE.Vector3(6.4, 0.08, 12.55),
new THREE.Vector3(7.55, 0.08, 11.35),
new THREE.Vector3(7.55, 0.08, 1.2),
new THREE.Vector3(6.4, 0.08, 0.45),
new THREE.Vector3(-24.55, 0.08, 0.45),
new THREE.Vector3(-24.55, 0.08, 12.55)
], true, "centripetal");
// A parked delivery van adds another everyday use without adding traffic.
function parkedVan() {
const group = new THREE.Group();
group.position.set(-8.1, 0, 3.95);
group.rotation.y = Math.PI;
scene.add(group);
const body = mat(0xd6c5a2);
box(1.65, 1.55, 2.95, body, 0, 1.12, 0, group);
box(1.68, 0.78, 1.12, mat(0x65817e), 0, 1.55, 1.05, group);
box(1.5, 0.5, 0.06, M.window, 0, 1.58, 1.64, group);
box(1.82, 0.13, 3.04, M.trim, 0, 1.93, 0, group);
box(0.08, 0.76, 0.1, M.metal, 0.72, 1.6, 1.22, group);
box(0.08, 0.76, 0.1, M.metal, -0.72, 1.6, 1.22, group);
const wheels = [];
for (const x of [-0.83, 0.83]) {
for (const z of [-0.95, 1.0]) {
wheels.push(wheel(0.34, 0.18, M.rubber, x, 0.35, z, group));
}
}
signMesh("GIAO HÀNG", 1.2, 0.38, 0, 1.05, -1.49, group);
group.userData.wheels = wheels;
}
parkedVan();
// ---------------------------------------------------------------------
// Custom orbit / pan / zoom camera controls
// ---------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 3.3, 0),
theta: 0.73,
phi: 0.73,
radius: 68,
dragging: false,
panning: false,
lastX: 0,
lastY: 0
};
function updateCamera() {
orbit.phi = THREE.MathUtils.clamp(orbit.phi, 0.25, 1.36);
orbit.radius = THREE.MathUtils.clamp(orbit.radius, 27, 105);
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", event => {
orbit.dragging = true;
orbit.panning = event.button === 2 || event.shiftKey;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!orbit.dragging) return;
const dx = event.clientX - orbit.lastX;
const dy = event.clientY - orbit.lastY;
orbit.lastX = event.clientX;
orbit.lastY = event.clientY;
if (orbit.panning) {
const right = new THREE.Vector3(
Math.cos(orbit.theta),
0,
-Math.sin(orbit.theta)
);
const forward = new THREE.Vector3(
-Math.sin(orbit.theta),
0,
-Math.cos(orbit.theta)
);
const scale = orbit.radius * 0.0014;
orbit.target.addScaledVector(right, -dx * scale);
orbit.target.addScaledVector(forward, -dy * scale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -18, 18);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -16, 16);
} else {
orbit.theta -= dx * 0.005;
orbit.phi -= dy * 0.0045;
}
updateCamera();
});
function endDrag(event) {
orbit.dragging = false;
orbit.panning = false;
if (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();
orbit.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
});
// Basic pinch zoom.
const touches = new Map();
let pinchDistance = 0;
canvas.addEventListener("touchstart", event => {
for (const touch of event.changedTouches) {
touches.set(touch.identifier, {
x: touch.clientX,
y: touch.clientY
});
}
if (touches.size === 2) {
const [a, b] = [...touches.values()];
pinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
}, { passive: true });
canvas.addEventListener("touchmove", event => {
if (event.touches.length === 2) {
event.preventDefault();
const a = event.touches[0];
const b = event.touches[1];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (pinchDistance > 0) {
orbit.radius *= pinchDistance / distance;
updateCamera();
}
pinchDistance = distance;
}
}, { passive: false });
canvas.addEventListener("touchend", event => {
for (const touch of event.changedTouches) touches.delete(touch.identifier);
if (touches.size < 2) pinchDistance = 0;
});
document.getElementById("resetButton").addEventListener("click", () => {
orbit.target.set(0, 3.3, 0);
orbit.theta = 0.73;
orbit.phi = 0.73;
orbit.radius = 68;
updateCamera();
});
let trafficPaused = false;
const trafficButton = document.getElementById("trafficButton");
trafficButton.addEventListener("click", () => {
trafficPaused = !trafficPaused;
trafficButton.textContent = trafficPaused ? "Resume traffic" : "Pause traffic";
});
updateCamera();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let trafficTime = 0;
function placeVehicle(mesh, curve, speed, offset, distanceMoved) {
const u = (trafficTime * speed / curve.getLength() + offset) % 1;
const point = curve.getPointAt(u);
const tangent = curve.getTangentAt(u);
mesh.position.copy(point);
mesh.rotation.y = Math.atan2(tangent.x, tangent.z);
for (const wheelMesh of mesh.userData.wheels || []) {
wheelMesh.rotation.x += distanceMoved / 0.34;
}
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!trafficPaused) trafficTime += delta;
placeVehicle(motorbikeMesh, motorRoute, 3.1, 0.08, delta * 3.1);
placeVehicle(bicycleMesh, bicycleRoute, 2.45, 0.56, delta * 2.45);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
});
renderer.render(scene, camera);
document.getElementById("loading").classList.add("hidden");
animate();
</script>
</body>
</html>
```