Prompt specification
Build or customize the Three.js 3D scene "Balat hillside street toward the Golden Horn" 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: Balat hillside street toward the Golden Horn
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/balat-hillside-street-toward-the-golden-horn-x2923
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing Balat's hillside streets in Istanbul. Build a bounded low-poly scene with referenced facade forms, climbing lanes, and a simplified waterfront direction. Avoid compressing unrelated Istanbul landmarks into the residential block. Low-poly.
Working Three.js Starter (balat-hillside-street-toward-the-golden-horn-x2923):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Balat — Istanbul Hillside Block</title>
<style>
:root {
color-scheme: light;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d3d5;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: #26363a;
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(360px, calc(100vw - 44px));
padding: 17px 19px 16px;
border: 1px solid rgba(255, 255, 255, 0.56);
border-radius: 14px;
background: rgba(247, 242, 227, 0.88);
box-shadow: 0 14px 38px rgba(35, 51, 53, 0.15);
backdrop-filter: blur(10px);
}
.eyebrow {
margin-bottom: 5px;
color: #8b4e43;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(27px, 4vw, 39px);
font-weight: 500;
line-height: 1;
}
.subtitle {
margin: 9px 0 0;
color: #536164;
font-size: 12px;
line-height: 1.5;
}
.controls {
position: absolute;
bottom: 22px;
left: 22px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.48);
border-radius: 10px;
background: rgba(247, 242, 227, 0.84);
box-shadow: 0 8px 24px rgba(35, 51, 53, 0.12);
color: #526064;
font-size: 11px;
backdrop-filter: blur(8px);
}
.controls strong {
color: #26363a;
}
.directions {
position: absolute;
right: 22px;
top: 22px;
display: grid;
gap: 9px;
justify-items: end;
}
.direction {
display: flex;
align-items: center;
gap: 10px;
min-width: 146px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 10px;
background: rgba(247, 242, 227, 0.84);
box-shadow: 0 8px 22px rgba(35, 51, 53, 0.12);
backdrop-filter: blur(8px);
}
.direction .arrow {
width: 25px;
color: #8b4e43;
font-family: Georgia, serif;
font-size: 29px;
line-height: 0.8;
text-align: center;
}
.direction.water .arrow {
color: #287783;
}
.direction b,
.direction small {
display: block;
}
.direction b {
color: #344649;
font-size: 10px;
letter-spacing: 0.13em;
}
.direction small {
margin-top: 2px;
color: #718084;
font-size: 9px;
}
button {
pointer-events: auto;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 9px;
padding: 9px 12px;
background: rgba(247, 242, 227, 0.9);
color: #405054;
font: 700 10px/1 inherit;
letter-spacing: 0.1em;
text-transform: uppercase;
cursor: pointer;
box-shadow: 0 6px 18px rgba(35, 51, 53, 0.12);
}
button:hover {
background: #fffaf0;
}
.note {
position: absolute;
right: 22px;
bottom: 22px;
max-width: 255px;
padding: 9px 12px;
border-radius: 9px;
background: rgba(38, 54, 58, 0.72);
color: rgba(255, 255, 255, 0.86);
font-size: 10px;
line-height: 1.45;
backdrop-filter: blur(8px);
}
@media (max-width: 650px) {
.title-card {
top: 14px;
left: 14px;
}
.subtitle {
display: none;
}
.directions {
top: 14px;
right: 14px;
}
.direction {
min-width: 42px;
padding: 8px 10px;
}
.direction div {
display: none;
}
.controls {
left: 14px;
bottom: 14px;
}
.note {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly model of Balat's hillside streets"></canvas>
<div class="hud">
<section class="title-card">
<div class="eyebrow">Istanbul · Residential Fabric</div>
<h1>Balat Hillside</h1>
<p class="subtitle">
A compact low-poly neighborhood of steep lanes, narrow stairways,
tiled roofs, and pastel waterfront houses.
</p>
</section>
<div class="controls">
<strong>Drag</strong> orbit · <strong>Wheel</strong> zoom ·
<strong>Shift-drag</strong> pan · <strong>R</strong> reset
</div>
<div class="directions">
<div class="direction">
<span class="arrow">↑</span>
<div><b>RIDGE</b><small>climbing streets</small></div>
</div>
<div class="direction water">
<span class="arrow">↓</span>
<div><b>WATERFRONT</b><small>simplified direction</small></div>
</div>
<button id="reset" type="button">Reset view</button>
</div>
<div class="note">
A bounded residential block rather than a montage of unrelated
Istanbul landmarks.
</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 and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
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.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d3d5);
scene.fog = new THREE.Fog(0xb9d3d5, 42, 82);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
140
);
const hemisphere = new THREE.HemisphereLight(0xfff2d7, 0x61736c, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffdfb0, 3.0);
sun.position.set(-14, 25, 17);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9e1ec, 0.65);
fill.position.set(18, 10, -16);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared geometry, materials and helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const unitFoliage = new THREE.IcosahedronGeometry(1, 0);
const materials = {
base: new THREE.MeshStandardMaterial({
color: 0x394b50,
roughness: 0.96,
flatShading: true
}),
stone: new THREE.MeshStandardMaterial({
color: 0x8b8173,
roughness: 1,
flatShading: true
}),
stoneDark: new THREE.MeshStandardMaterial({
color: 0x716b63,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
}),
stoneLight: new THREE.MeshStandardMaterial({
color: 0xb5aa97,
roughness: 0.95,
flatShading: true
}),
road: new THREE.MeshStandardMaterial({
color: 0x887b69,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
}),
trim: new THREE.MeshStandardMaterial({
color: 0xeee3cb,
roughness: 0.9,
flatShading: true
}),
glass: new THREE.MeshStandardMaterial({
color: 0x263d47,
roughness: 0.38,
metalness: 0.05,
flatShading: true
}),
wood: new THREE.MeshStandardMaterial({
color: 0x684a39,
roughness: 1,
flatShading: true
}),
darkWood: new THREE.MeshStandardMaterial({
color: 0x45382f,
roughness: 1,
flatShading: true
}),
metal: new THREE.MeshStandardMaterial({
color: 0x385454,
roughness: 0.7,
flatShading: true
}),
foliage: new THREE.MeshStandardMaterial({
color: 0x657d5d,
roughness: 1,
flatShading: true
}),
foliageLight: new THREE.MeshStandardMaterial({
color: 0x82916a,
roughness: 1,
flatShading: true
}),
lamp: new THREE.MeshStandardMaterial({
color: 0xffd58a,
emissive: 0xd68b36,
emissiveIntensity: 0.55,
roughness: 0.7,
flatShading: true
}),
water: new THREE.MeshStandardMaterial({
color: 0x4b9ca4,
roughness: 0.42,
metalness: 0.08,
transparent: true,
opacity: 0.94,
flatShading: true
}),
waterLine: new THREE.MeshBasicMaterial({
color: 0xa4d3ce,
transparent: true,
opacity: 0.48
})
};
const shutterMaterials = [
new THREE.MeshStandardMaterial({
color: 0x3d716b,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x365b73,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x8d4e42,
roughness: 0.95,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xc08b4a,
roughness: 0.95,
flatShading: true
})
];
const roofMaterials = [
new THREE.MeshStandardMaterial({
color: 0x9b4938,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0x8d4034,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
}),
new THREE.MeshStandardMaterial({
color: 0xa9563e,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
})
];
function box(parent, w, h, d, x, y, z, material, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let randomSeed = 924713;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomBetween(min, max) {
return min + random() * (max - min);
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function terrainHeight(x, z) {
const rise = clamp((14.5 - z) / 29, 0, 1);
const crossHill =
Math.sin((x + 3) * 0.31) * 0.38 * rise +
Math.cos(z * 0.43) * 0.20 * rise;
const sideLift =
0.28 * Math.pow(Math.abs(x) / 17, 2) * rise;
return 0.16 + rise * 6.72 + crossHill + sideLift;
}
function cylinderBetween(parent, a, b, radius, material, segments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
segments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Bounded diorama base and low-poly hillside terrain
// -------------------------------------------------------------------------
box(scene, 36, 0.8, 36, 0, -0.85, 1.5, materials.base);
const terrainPositions = [];
const terrainColors = [];
const terrainLow = new THREE.Color(0x778b69);
const terrainMid = new THREE.Color(0x9b9b72);
const terrainHigh = new THREE.Color(0xafa078);
function pushTerrainTriangle(a, b, c) {
const averageHeight =
(terrainHeight(a[0], a[1]) +
terrainHeight(b[0], b[1]) +
terrainHeight(c[0], c[1])) /
3;
const color = new THREE.Color();
const t = clamp(averageHeight / 7.1, 0, 1);
if (t < 0.5) {
color.copy(terrainLow).lerp(terrainMid, t * 2);
} else {
color.copy(terrainMid).lerp(terrainHigh, (t - 0.5) * 2);
}
color.offsetHSL(
randomBetween(-0.012, 0.012),
randomBetween(-0.035, 0.035),
randomBetween(-0.055, 0.055)
);
for (const vertex of [a, b, c]) {
terrainPositions.push(
vertex[0],
terrainHeight(vertex[0], vertex[1]),
vertex[1]
);
terrainColors.push(color.r, color.g, color.b);
}
}
const gridX = 20;
const gridZ = 20;
const minX = -17;
const maxX = 17;
const minZ = -15;
const maxZ = 14.5;
for (let iz = 0; iz < gridZ; iz++) {
const z0 = THREE.MathUtils.lerp(minZ, maxZ, iz / gridZ);
const z1 = THREE.MathUtils.lerp(minZ, maxZ, (iz + 1) / gridZ);
for (let ix = 0; ix < gridX; ix++) {
const x0 = THREE.MathUtils.lerp(minX, maxX, ix / gridX);
const x1 = THREE.MathUtils.lerp(minX, maxX, (ix + 1) / gridX);
const a = [x0, z0];
const b = [x1, z0];
const c = [x1, z1];
const d = [x0, z1];
if ((ix + iz) % 2 === 0) {
pushTerrainTriangle(a, d, b);
pushTerrainTriangle(b, d, c);
} else {
pushTerrainTriangle(a, d, c);
pushTerrainTriangle(a, c, b);
}
}
}
const terrainGeometry = new THREE.BufferGeometry();
terrainGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(terrainPositions, 3)
);
terrainGeometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
})
);
terrain.receiveShadow = true;
scene.add(terrain);
function addRetainingFace(vertices) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, materials.stoneDark);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
// Left and right cut faces of the hill.
for (const side of [-1, 1]) {
const vertices = [];
const x = side * 17;
for (let i = 0; i < 26; i++) {
const z0 = THREE.MathUtils.lerp(minZ, maxZ, i / 26);
const z1 = THREE.MathUtils.lerp(minZ, maxZ, (i + 1) / 26);
const y0 = terrainHeight(x, z0);
const y1 = terrainHeight(x, z1);
vertices.push(
x, y0, z0,
x, -0.45, z0,
x, y1, z1,
x, y1, z1,
x, -0.45, z0,
x, -0.45, z1
);
}
addRetainingFace(vertices);
}
// Back cut face.
{
const vertices = [];
for (let i = 0; i < 26; i++) {
const x0 = THREE.MathUtils.lerp(minX, maxX, i / 26);
const x1 = THREE.MathUtils.lerp(minX, maxX, (i + 1) / 26);
const y0 = terrainHeight(x0, minZ);
const y1 = terrainHeight(x1, minZ);
vertices.push(
x0, y0, minZ,
x0, -0.45, minZ,
x1, y1, minZ,
x1, y1, minZ,
x0, -0.45, minZ,
x1, -0.45, minZ
);
}
addRetainingFace(vertices);
}
// -------------------------------------------------------------------------
// Winding streets and cobbled climbing lanes
// -------------------------------------------------------------------------
const mainCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(4.5, 0, 14.1),
new THREE.Vector3(2.7, 0, 10.0),
new THREE.Vector3(-1.5, 0, 6.0),
new THREE.Vector3(1.5, 0, 2.0),
new THREE.Vector3(-1.0, 0, -2.0),
new THREE.Vector3(2.5, 0, -6.0),
new THREE.Vector3(0.4, 0, -10.0),
new THREE.Vector3(0.8, 0, -13.7)
],
false,
"centripetal"
);
const westCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-14.1, 0, 12.2),
new THREE.Vector3(-11.1, 0, 8.1),
new THREE.Vector3(-8.0, 0, 4.5),
new THREE.Vector3(-9.0, 0, 0.0),
new THREE.Vector3(-6.0, 0, -4.0),
new THREE.Vector3(-8.0, 0, -8.0),
new THREE.Vector3(-4.0, 0, -11.0)
],
false,
"centripetal"
);
const eastCurve = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(9.0, 0, 12.2),
new THREE.Vector3(8.0, 0, 8.0),
new THREE.Vector3(11.0, 0, 4.0),
new THREE.Vector3(9.0, 0, 0.0),
new THREE.Vector3(12.0, 0, -4.0),
new THREE.Vector3(10.0, 0, -8.0)
],
false,
"centripetal"
);
function createRoad(curve, width, material, samples = 90) {
const positions = [];
const indices = [];
for (let i = 0; i <= samples; i++) {
const t = i / samples;
const point = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const perpendicular = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const left = point.clone().addScaledVector(perpendicular, width * 0.5);
const right = point.clone().addScaledVector(perpendicular, -width * 0.5);
positions.push(
left.x, terrainHeight(left.x, left.z) + 0.075, left.z,
right.x, terrainHeight(right.x, right.z) + 0.075, right.z
);
if (i < samples) {
const a = i * 2;
const b = a + 1;
const c = a + 2;
const d = a + 3;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
createRoad(mainCurve, 2.7, materials.road, 120);
createRoad(westCurve, 2.05, materials.road, 90);
createRoad(eastCurve, 1.95, materials.road, 80);
// Small individual paving stones give the main lane a cobbled texture.
{
const stoneCount = 100;
const lanes = [-0.78, 0, 0.78];
const cobbleGeometry = new THREE.BoxGeometry(0.5, 0.075, 0.29);
const cobbleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
flatShading: true
});
const cobbles = new THREE.InstancedMesh(
cobbleGeometry,
cobbleMaterial,
stoneCount * lanes.length
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
let instance = 0;
for (let i = 0; i < stoneCount; i++) {
const t = (i + 0.5) / stoneCount;
const point = mainCurve.getPointAt(t);
const tangent = mainCurve.getTangentAt(t).normalize();
const perpendicular = new THREE.Vector3(-tangent.z, 0, tangent.x);
const angle = Math.atan2(tangent.x, tangent.z);
for (const lane of lanes) {
const lateral = randomBetween(-0.12, 0.12);
const p = point
.clone()
.addScaledVector(perpendicular, lane + lateral);
dummy.position.set(
p.x,
terrainHeight(p.x, p.z) + 0.115,
p.z
);
dummy.rotation.set(
0,
angle + randomBetween(-0.08, 0.08),
0
);
dummy.scale.set(
randomBetween(0.82, 1.08),
randomBetween(0.8, 1.08),
randomBetween(0.86, 1.12)
);
dummy.updateMatrix();
cobbles.setMatrixAt(instance, dummy.matrix);
color.setHSL(
randomBetween(0.075, 0.105),
randomBetween(0.17, 0.28),
randomBetween(0.48, 0.64)
);
cobbles.setColorAt(instance, color);
instance++;
}
}
cobbles.receiveShadow = true;
scene.add(cobbles);
}
function createStairFlight(startT, endT, count) {
const stepGeometry = new THREE.BoxGeometry(2.55, 0.26, 0.48);
const railPoints = [];
for (let i = 0; i < count; i++) {
const t = THREE.MathUtils.lerp(startT, endT, i / (count - 1));
const point = mainCurve.getPointAt(t);
const tangent = mainCurve.getTangentAt(t).normalize();
const angle = Math.atan2(tangent.x, tangent.z);
const y = terrainHeight(point.x, point.z);
const step = new THREE.Mesh(stepGeometry, materials.stoneLight);
step.position.set(point.x, y + 0.09, point.z);
step.rotation.y = angle;
step.castShadow = true;
step.receiveShadow = true;
scene.add(step);
if (i % 4 === 0 || i === count - 1) {
const perpendicular = new THREE.Vector3(-tangent.z, 0, tangent.x);
const railPoint = point
.clone()
.addScaledVector(perpendicular, 1.27);
railPoint.y = y + 1.02;
railPoints.push(railPoint);
cylinderBetween(
scene,
new THREE.Vector3(railPoint.x, y, railPoint.z),
railPoint,
0.045,
materials.metal
);
}
}
for (let i = 1; i < railPoints.length; i++) {
cylinderBetween(
scene,
railPoints[i - 1],
railPoints[i],
0.038,
materials.metal
);
}
}
createStairFlight(0.235, 0.375, 16);
createStairFlight(0.59, 0.715, 14);
// -------------------------------------------------------------------------
// Balat-like facade forms
// -------------------------------------------------------------------------
function makeRoof(width, depth, roofHeight, material) {
const halfWidth = width * 0.5;
const halfDepth = depth * 0.5;
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
-halfWidth, 0, halfDepth,
halfWidth, 0, halfDepth,
0, roofHeight, halfDepth,
-halfWidth, 0, -halfDepth,
halfWidth, 0, -halfDepth,
0, roofHeight, -halfDepth
],
3
)
);
geometry.setIndex([
0, 3, 5, 0, 5, 2,
1, 2, 5, 1, 5, 4,
0, 1, 4, 0, 4, 3,
0, 2, 1,
3, 4, 5
]);
geometry.computeVertexNormals();
const roof = new THREE.Mesh(geometry, material);
roof.castShadow = true;
roof.receiveShadow = true;
return roof;
}
function addFrontWindow(parent, x, y, z, shutterMaterial, hasShutters = true) {
box(parent, 0.96, 1.18, 0.08, x, y, z, materials.trim);
box(parent, 0.76, 0.96, 0.095, x, y, z + 0.055, materials.glass);
box(parent, 0.055, 0.96, 0.11, x, y, z + 0.095, materials.trim);
if (hasShutters) {
box(
parent,
0.23,
1.05,
0.09,
x - 0.61,
y,
z + 0.05,
shutterMaterial
);
box(
parent,
0.23,
1.05,
0.09,
x + 0.61,
y,
z + 0.05,
shutterMaterial
);
}
}
function addSideWindow(parent, x, y, z) {
box(parent, 0.08, 1.12, 0.83, x, y, z, materials.trim);
box(parent, 0.095, 0.91, 0.64, x + 0.055, y, z, materials.glass);
}
function addBuilding(options) {
const {
x,
z,
w,
d,
floors,
rotation,
color,
balcony = false,
bay = false
} = options;
const floorHeight = 2.38;
const bodyHeight = floors * floorHeight;
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const cos = Math.cos(rotation);
const sin = Math.sin(rotation);
const localTerrain = (lx, lz) =>
terrainHeight(
x + lx * cos + lz * sin,
z - lx * sin + lz * cos
);
const cornerHeights = [
localTerrain(-w * 0.5, -d * 0.5),
localTerrain(w * 0.5, -d * 0.5),
localTerrain(-w * 0.5, d * 0.5),
localTerrain(w * 0.5, d * 0.5)
];
const minimumHeight = Math.min(...cornerHeights);
const maximumHeight = Math.max(...cornerHeights);
const floorY = maximumHeight + 0.08;
const foundationBottom = minimumHeight - 0.4;
const foundationHeight = floorY - foundationBottom;
const wallMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
flatShading: true
});
box(
group,
w + 0.16,
foundationHeight,
d + 0.16,
0,
foundationBottom + foundationHeight * 0.5,
0,
materials.stone
);
box(
group,
w,
bodyHeight,
d,
0,
floorY + bodyHeight * 0.5,
0,
wallMaterial
);
const frontZ = d * 0.5 + 0.035;
const shutterMaterial =
shutterMaterials[Math.floor(random() * shutterMaterials.length)];
// Entrance and door surround.
box(group, 1.08, 1.93, 0.1, 0, floorY + 0.965, frontZ, materials.trim);
box(
group,
0.84,
1.72,
0.12,
0,
floorY + 0.86,
frontZ + 0.065,
random() > 0.5 ? shutterMaterial : materials.metal
);
// Small steps compensate for the slope.
const frontGround = localTerrain(0, d * 0.5 + 0.35);
const stepRise = Math.max(0, floorY - frontGround);
if (stepRise > 0.12) {
for (let i = 0; i < 3; i++) {
const h = stepRise / 3 + 0.035;
box(
group,
1.12,
h,
0.34,
0,
frontGround + h * (i + 0.5),
d * 0.5 + 0.65 - i * 0.23,
materials.stoneLight
);
}
}
for (let floor = 0; floor < floors; floor++) {
const windowY = floorY + floor * floorHeight + 1.24;
if (floor === 0) {
addFrontWindow(
group,
-w * 0.29,
windowY,
frontZ,
shutterMaterial,
random() > 0.25
);
} else {
addFrontWindow(
group,
-w * 0.235,
windowY,
frontZ,
shutterMaterial,
random() > 0.2
);
addFrontWindow(
group,
w * 0.235,
windowY,
frontZ,
shutterMaterial,
random() > 0.35
);
}
addSideWindow(group, w * 0.5 + 0.025, windowY, -d * 0.08);
if (floor > 0) {
box(
group,
w + 0.04,
0.075,
0.085,
0,
floorY + floor * floorHeight,
frontZ + 0.018,
materials.trim
);
}
}
// Pale corner trim gives the low-poly facades a hand-built character.
for (const corner of [-1, 1]) {
box(
group,
0.13,
bodyHeight,
0.09,
corner * (w * 0.5 - 0.065),
floorY + bodyHeight * 0.5,
frontZ + 0.025,
materials.trim
);
}
if (balcony && floors > 1) {
const balconyY = floorY + floorHeight;
const balconyWidth = w * 0.64;
box(
group,
balconyWidth,
0.13,
0.78,
0,
balconyY,
d * 0.5 + 0.34,
materials.wood
);
for (let i = 0; i < 8; i++) {
const px = THREE.MathUtils.lerp(
-balconyWidth * 0.46,
balconyWidth * 0.46,
i / 7
);
box(
group,
0.05,
0.66,
0.05,
px,
balconyY + 0.38,
d * 0.5 + 0.68,
materials.darkWood
);
}
for (const railY of [balconyY + 0.31, balconyY + 0.69]) {
box(
group,
balconyWidth,
0.055,
0.055,
0,
railY,
d * 0.5 + 0.68,
materials.darkWood
);
}
}
if (bay && floors > 1) {
const bayY = floorY + floorHeight + 1.15;
box(
group,
1.48,
1.46,
0.58,
0,
bayY,
d * 0.5 + 0.22,
materials.wood
);
box(
group,
1.13,
1.04,
0.075,
0,
bayY + 0.02,
d * 0.5 + 0.54,
materials.glass
);
box(
group,
0.06,
1.04,
0.085,
0,
bayY + 0.02,
d * 0.5 + 0.59,
materials.trim
);
box(
group,
1.62,
0.12,
0.73,
0,
bayY + 0.77,
d * 0.5 + 0.22,
materials.trim
);
}
const roof = makeRoof(
w + 0.42,
d + 0.44,
1.05 + randomBetween(-0.1, 0.16),
roofMaterials[Math.floor(random() * roofMaterials.length)]
);
roof.position.y = floorY + bodyHeight;
group.add(roof);
box(
group,
0.43,
0.95,
0.48,
w * 0.25,
floorY + bodyHeight + 0.72,
-d * 0.08,
wallMaterial
);
box(
group,
0.51,
0.12,
0.56,
w * 0.25,
floorY + bodyHeight + 1.22,
-d * 0.08,
materials.trim
);
}
const housePalette = [
0xe0c99f,
0xc88968,
0x94ad91,
0x91aeba,
0xc98d83,
0xd6b37c,
0xb3b78c,
0xd7c6a6,
0xb8735f
];
const buildings = [
{ x: 7.8, z: 11.2, w: 4.4, d: 4.8, floors: 3, rotation: -Math.PI / 2, color: 0xe0c99f },
{ x: -4.2, z: 11.0, w: 4.2, d: 4.6, floors: 2, rotation: Math.PI / 2, color: 0xc88968 },
{ x: 4.8, z: 6.8, w: 4.3, d: 4.5, floors: 3, rotation: -Math.PI / 2, color: 0x94ad91, balcony: true },
{ x: -5.8, z: 7.0, w: 4.6, d: 5.0, floors: 3, rotation: Math.PI / 2, color: 0xc98d83 },
{ x: 6.8, z: 1.8, w: 4.3, d: 4.7, floors: 2, rotation: -Math.PI / 2, color: 0x91aeba },
{ x: -4.8, z: 1.7, w: 4.5, d: 4.8, floors: 3, rotation: Math.PI / 2, color: 0xd6b37c, balcony: true },
{ x: 5.6, z: -3.2, w: 4.2, d: 4.5, floors: 3, rotation: -Math.PI / 2, color: 0xb3b78c },
{ x: -5.8, z: -3.0, w: 4.3, d: 4.6, floors: 2, rotation: Math.PI / 2, color: 0x91aeba },
{ x: 7.4, z: -7.2, w: 4.2, d: 4.4, floors: 2, rotation: -Math.PI / 2, color: 0xd7c6a6 },
{ x: -3.4, z: -8.0, w: 3.6, d: 4.2, floors: 3, rotation: Math.PI / 2, color: 0xc98d83, bay: true },
{ x: 6.0, z: -11.0, w: 4.1, d: 4.5, floors: 2, rotation: -Math.PI / 2, color: 0x94ad91 },
{ x: -5.3, z: -11.0, w: 4.3, d: 4.4, floors: 2, rotation: Math.PI / 2, color: 0xc88968 },
{ x: -14.0, z: 10.0, w: 3.6, d: 4.2, floors: 2, rotation: Math.PI / 2, color: 0x91aeba },
{ x: -12.4, z: 1.0, w: 3.8, d: 4.2, floors: 2, rotation: Math.PI / 2, color: 0xd7c6a6, balcony: true },
{ x: -11.4, z: -7.5, w: 3.6, d: 4.0, floors: 2, rotation: Math.PI / 2, color: 0xb3b78c },
{ x: -8.1, z: -12.7, w: 3.6, d: 4.1, floors: 2, rotation: 0, color: 0xe0c99f },
{ x: 13.5, z: 10.0, w: 3.7, d: 4.2, floors: 3, rotation: -Math.PI / 2, color: 0xc98d83, balcony: true },
{ x: 14.6, z: 4.0, w: 3.5, d: 4.2, floors: 2, rotation: -Math.PI / 2, color: 0xd6b37c },
{ x: 13.0, z: -1.8, w: 3.7, d: 4.3, floors: 2, rotation: -Math.PI / 2, color: 0x94ad91 },
{ x: 15.0, z: -4.7, w: 3.2, d: 3.9, floors: 2, rotation: -Math.PI / 2, color: 0xe0c99f },
{ x: 8.0, z: -12.6, w: 3.5, d: 4.0, floors: 2, rotation: 0, color: 0xc88968 }
];
buildings.forEach((building, index) => {
addBuilding({
...building,
color: index % 5 === 0 ? housePalette[(index + 2) % housePalette.length] : building.color
});
});
// -------------------------------------------------------------------------
// Street lamps, trees and small neighborhood details
// -------------------------------------------------------------------------
function addLamp(x, z) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(x, z), z);
scene.add(group);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.065, 2.25, 6),
materials.metal
);
pole.position.y = 1.125;
pole.castShadow = true;
group.add(pole);
box(group, 0.32, 0.35, 0.32, 0, 2.28, 0, materials.lamp);
box(group, 0.42, 0.08, 0.42, 0, 2.5, 0, materials.metal);
}
[
[3.95, 11.8],
[-2.75, 5.2],
[3.35, 0.6],
[-3.25, -5.1],
[3.7, -9.0],
[-10.5, 5.7]
].forEach(([x, z]) => addLamp(x, z));
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, terrainHeight(x, z), z);
group.scale.setScalar(scale);
scene.add(group);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.12, 0.2, 1.65, 5),
materials.wood
);
trunk.position.y = 0.825;
trunk.castShadow = true;
group.add(trunk);
const crown = new THREE.Mesh(unitFoliage, materials.foliage);
crown.position.set(0, 2.0, 0);
crown.scale.set(0.95, 1.25, 0.88);
crown.rotation.set(0.15, 0.3, -0.08);
crown.castShadow = true;
group.add(crown);
const crown2 = new THREE.Mesh(unitFoliage, materials.foliageLight);
crown2.position.set(0.32, 2.42, -0.08);
crown2.scale.set(0.65, 0.78, 0.62);
crown2.rotation.set(-0.1, 0.8, 0.1);
crown2.castShadow = true;
group.add(crown2);
}
addTree(-15.2, -11.2, 1.0);
addTree(14.9, -10.3, 0.9);
addTree(-15.0, 12.4, 0.82);
addTree(12.8, -13.0, 0.75);
// -------------------------------------------------------------------------
// Simplified waterfront: quay, water, pier and two small boats
// -------------------------------------------------------------------------
box(scene, 34.6, 0.82, 0.48, 0, 0.02, 14.62, materials.stone);
box(scene, 34.9, 0.14, 0.68, 0, 0.46, 14.64, materials.stoneLight);
box(scene, 34, 0.12, 4.25, 0, -0.13, 16.72, materials.water, false);
// Stylized water strokes.
for (let i = 0; i < 13; i++) {
const line = box(
scene,
randomBetween(1.2, 3.6),
0.018,
0.045,
randomBetween(-14.5, 14.5),
-0.055,
randomBetween(15.2, 18.1),
materials.waterLine,
false
);
line.rotation.y = randomBetween(-0.3, 0.3);
}
// Quay bollards.
for (let x = -14; x <= 14; x += 4) {
const bollard = new THREE.Mesh(
new THREE.CylinderGeometry(0.09, 0.12, 0.45, 6),
materials.metal
);
bollard.position.set(x, 0.69, 15.02);
bollard.castShadow = true;
scene.add(bollard);
box(
scene,
0.25,
0.07,
0.25,
x,
0.94,
15.02,
materials.metal
);
}
// Narrow wooden pier.
for (let i = 0; i < 8; i++) {
box(
scene,
1.55,
0.13,
0.42,
10.7,
0.04,
15.0 + i * 0.46,
i % 2 === 0 ? materials.wood : materials.darkWood
);
}
for (const x of [10.05, 11.35]) {
for (const z of [15.15, 18.25]) {
cylinderBetween(
scene,
new THREE.Vector3(x, -0.1, z),
new THREE.Vector3(x, 0.55, z),
0.055,
materials.darkWood
);
}
}
const boats = [];
function addBoat(x, z, rotation, hullColor, phase) {
const group = new THREE.Group();
group.position.set(x, -0.1, z);
group.rotation.y = rotation;
scene.add(group);
const hullMaterial = new THREE.MeshStandardMaterial({
color: hullColor,
roughness: 0.9,
flatShading: true
});
box(group, 2.45, 0.36, 0.88, 0, 0.06, 0, hullMaterial);
const bow = new THREE.Mesh(
new THREE.ConeGeometry(0.55, 0.95, 4),
hullMaterial
);
bow.rotation.z = -Math.PI / 2;
bow.position.set(1.45, 0.07, 0);
bow.scale.z = 0.78;
bow.castShadow = true;
group.add(bow);
box(group, 2.12, 0.08, 0.7, -0.08, 0.29, 0, materials.wood);
box(group, 0.76, 0.62, 0.62, -0.38, 0.61, 0, materials.trim);
box(group, 0.54, 0.3, 0.65, -0.38, 0.68, 0, materials.glass);
boats.push({ group, phase });
}
addBoat(6.5, 16.45, -0.16, 0xb85d46, 0.2);
addBoat(-8.6, 16.75, 0.13, 0xe4d2ae, 2.1);
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, pan and reset
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.78,
phi: 1.02,
radius: 40,
target: new THREE.Vector3(0, 2.4, -0.4)
};
const desired = {
theta: cameraState.theta,
phi: cameraState.phi,
radius: cameraState.radius,
target: cameraState.target.clone()
};
function updateCamera(immediate = false) {
const smoothing = immediate ? 1 : 0.13;
cameraState.theta +=
(desired.theta - cameraState.theta) * smoothing;
cameraState.phi +=
(desired.phi - cameraState.phi) * smoothing;
cameraState.radius +=
(desired.radius - cameraState.radius) * smoothing;
cameraState.target.lerp(desired.target, smoothing);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetView() {
desired.theta = 0.78;
desired.phi = 1.02;
desired.radius = 40;
desired.target.set(0, 2.4, -0.4);
}
const pointer = {
active: false,
id: null,
x: 0,
y: 0,
shift: false
};
canvas.addEventListener("pointerdown", (event) => {
pointer.active = true;
pointer.id = event.pointerId;
pointer.x = event.clientX;
pointer.y = event.clientY;
pointer.shift = event.shiftKey;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointer.active || event.pointerId !== pointer.id) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (event.shiftKey || pointer.shift) {
const scale = cameraState.radius * 0.0012;
const right = new THREE.Vector3(
Math.cos(cameraState.theta),
0,
-Math.sin(cameraState.theta)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.theta),
0,
-Math.cos(cameraState.theta)
);
desired.target.addScaledVector(right, -dx * scale);
desired.target.addScaledVector(forward, -dy * scale);
desired.target.x = clamp(desired.target.x, -11, 11);
desired.target.z = clamp(desired.target.z, -13, 10);
} else {
desired.theta -= dx * 0.0052;
desired.phi = clamp(desired.phi - dy * 0.0046, 0.38, 1.43);
}
});
function endPointer(event) {
if (event.pointerId !== pointer.id) return;
pointer.active = false;
pointer.id = null;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
18,
62
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetView();
if (event.key === "ArrowLeft") desired.theta += 0.08;
if (event.key === "ArrowRight") desired.theta -= 0.08;
if (event.key === "ArrowUp") desired.phi = clamp(desired.phi - 0.06, 0.38, 1.43);
if (event.key === "ArrowDown") desired.phi = clamp(desired.phi + 0.06, 0.38, 1.43);
if (event.key === "+" || event.key === "=") {
desired.radius = clamp(desired.radius - 2, 18, 62);
}
if (event.key === "-") {
desired.radius = clamp(desired.radius + 2, 18, 62);
}
});
document.querySelector("#reset").addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateCamera(true);
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
boats.forEach((boat) => {
boat.group.position.y =
-0.1 + Math.sin(elapsed * 0.85 + boat.phase) * 0.025;
boat.group.rotation.z =
Math.sin(elapsed * 0.55 + boat.phase) * 0.012;
});
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```