Prompt specification
Build or customize the Three.js 3D scene "Alfama lane and viewpoint fragment" 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: Alfama lane and viewpoint fragment
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/alfama-lane-and-viewpoint-fragment-x2839
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts Alfama's stair lanes in Lisbon. Build a bounded low-poly scene with referenced roof forms, winding stairs, and a small river-facing viewpoint. Preserve the terrain's shape rather than arranging buildings on a flat grid. Low-poly.
Working Three.js Starter (alfama-lane-and-viewpoint-fragment-x2839):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Alfama — Stair Lanes of Lisbon</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;
}
body {
background:
radial-gradient(circle at 72% 18%, rgba(255, 244, 210, 0.85), transparent 22%),
linear-gradient(180deg, #bcd6df 0%, #d8e4df 52%, #c9bfa8 100%);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
top: 22px;
left: 22px;
z-index: 10;
width: min(320px, calc(100vw - 44px));
padding: 18px 18px 16px;
color: #23383a;
background: rgba(248, 244, 231, 0.88);
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 4px;
box-shadow: 0 14px 42px rgba(32, 48, 47, 0.18);
backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin: 0 0 4px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
color: #aa5139;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: 34px;
font-weight: 500;
letter-spacing: -0.03em;
}
.subtitle {
margin: 5px 0 15px;
color: #526363;
font-size: 12px;
line-height: 1.45;
}
.views {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
button {
appearance: none;
border: 1px solid rgba(37, 65, 67, 0.2);
border-radius: 3px;
padding: 8px 10px;
color: #294446;
background: rgba(255, 255, 255, 0.58);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: #fffaf0;
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid #b65d42;
outline-offset: 2px;
}
.hint {
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(37, 65, 67, 0.13);
color: #687676;
font-size: 10px;
line-height: 1.55;
}
.hint strong {
color: #405758;
}
.credit {
position: fixed;
right: 18px;
bottom: 15px;
z-index: 10;
color: rgba(35, 57, 58, 0.68);
font-size: 9px;
letter-spacing: 0.13em;
text-transform: uppercase;
pointer-events: none;
}
@media (max-width: 650px) {
.panel {
top: 12px;
left: 12px;
width: min(280px, calc(100vw - 24px));
padding: 14px;
}
h1 {
font-size: 28px;
}
.subtitle {
margin-bottom: 11px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive low-poly model of Alfama, Lisbon"></main>
<section class="panel">
<p class="eyebrow">Lisboa · Portugal</p>
<h1>Alfama</h1>
<p class="subtitle">
Terracotta roofs, narrow stair lanes and a small terrace above the Tagus.
</p>
<nav class="views" aria-label="Camera views">
<button type="button" data-view="overview">Overview</button>
<button type="button" data-view="terrace">River terrace</button>
<button type="button" data-view="stairs">Stair lane</button>
</nav>
<div class="hint">
<strong>Drag</strong> to orbit · <strong>Wheel / pinch</strong> to zoom ·
<strong>Right-drag</strong> to pan<br>
Arrow keys orbit · <strong>+</strong> / <strong>−</strong> zoom · <strong>R</strong> resets
</div>
</section>
<div class="credit">A compact study of Lisbon’s stepped quarter</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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;
renderer.setClearColor(0x000000, 0);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xc8dadd, 38, 82);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
120
);
const hemi = new THREE.HemisphereLight(0xe9f5f5, 0x806044, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffe0ae, 3.1);
sun.position.set(-12, 22, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -22;
sun.shadow.camera.right = 22;
sun.shadow.camera.top = 22;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 60;
sun.shadow.bias = -0.00025;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9fc8d7, 0.55);
fill.position.set(15, 9, -14);
scene.add(fill);
// ------------------------------------------------------------
// Shared materials
// ------------------------------------------------------------
const wallMaterials = new Map();
const foundationMaterial = new THREE.MeshStandardMaterial({
color: 0x807460,
roughness: 1,
flatShading: true
});
const streetMaterial = new THREE.MeshStandardMaterial({
color: 0xb9ab8e,
roughness: 1,
flatShading: true
});
const stairMaterial = new THREE.MeshStandardMaterial({
color: 0xc4b89d,
roughness: 0.96,
flatShading: true
});
const stepEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0x9f947e,
roughness: 1,
flatShading: true
});
const ironMaterial = new THREE.MeshStandardMaterial({
color: 0x304044,
roughness: 0.62,
metalness: 0.38
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x243d45,
roughness: 0.35,
metalness: 0.08
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0xf1ead9,
roughness: 0.88
});
const woodMaterial = new THREE.MeshStandardMaterial({
color: 0x815138,
roughness: 0.92
});
const terracottaMaterial = new THREE.MeshStandardMaterial({
color: 0x9f5b3f,
roughness: 0.95,
flatShading: true
});
function wallMaterial(color) {
if (!wallMaterials.has(color)) {
wallMaterials.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.94,
flatShading: true
})
);
}
return wallMaterials.get(color);
}
// ------------------------------------------------------------
// Terrain: a sloping, irregular ground surface rather than a grid
// ------------------------------------------------------------
function terrainHeight(x, z) {
const slope = (5.65 - z) * 0.335;
const lowerDetailScale =
0.52 + 0.48 * THREE.MathUtils.clamp((5.65 - z) / 13.8, 0, 1);
const detail =
(
Math.sin(x * 0.52 + z * 0.17) * 0.19 +
Math.cos(x * 0.93 - z * 0.31) * 0.11 +
Math.sin(x * 1.7 + z * 0.73) * 0.055
) * lowerDetailScale;
return Math.max(0.18, 0.38 + slope + detail);
}
function makeTerrain() {
const nx = 34;
const nz = 26;
const positions = [];
const colors = [];
const indices = [];
const low = new THREE.Color(0xb6a27b);
const high = new THREE.Color(0x827e67);
const color = new THREE.Color();
for (let j = 0; j <= nz; j++) {
const v = j / nz;
for (let i = 0; i <= nx; i++) {
const u = i / nx;
const halfWidth =
10.9 -
1.15 * Math.sin(Math.PI * v) -
0.45 * v +
0.18 * Math.sin(v * 9);
const x =
(u * 2 - 1) * halfWidth +
0.18 * Math.sin(v * 4.2);
const z =
-8.15 +
v * 13.8 +
(
0.18 * Math.sin(u * Math.PI * 3 + 0.6) +
0.12 * Math.sin(u * Math.PI * 7)
) * Math.pow(1 - v, 2);
const y = terrainHeight(x, z);
positions.push(x, y, z);
const heightMix = THREE.MathUtils.clamp((y - 0.2) / 5.0, 0, 1);
color.copy(low).lerp(high, heightMix);
color.multiplyScalar(0.94 + 0.09 * Math.sin(x * 2.7 + z * 1.4));
colors.push(color.r, color.g, color.b);
}
}
for (let j = 0; j < nz; j++) {
for (let i = 0; i < nx; i++) {
const a = j * (nx + 1) + i;
const b = a + 1;
const c = a + nx + 1;
const d = c + 1;
if ((i + j) % 2 === 0) {
indices.push(a, c, b, b, c, d);
} else {
indices.push(a, c, d, a, d, b);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
});
const terrain = new THREE.Mesh(geometry, material);
terrain.receiveShadow = true;
scene.add(terrain);
// Side walls form the finite cut edge of the terrain.
const boundary = [];
for (let i = 0; i <= nx; i++) {
boundary.push([i, 0]);
}
for (let j = 1; j <= nz; j++) {
boundary.push([nx, j]);
}
for (let i = nx - 1; i >= 0; i--) {
boundary.push([i, nz]);
}
for (let j = nz - 1; j > 0; j--) {
boundary.push([0, j]);
}
const wallPositions = [];
for (let k = 0; k < boundary.length; k++) {
const a = boundary[k];
const b = boundary[(k + 1) % boundary.length];
const ax = positions[(a[1] * (nx + 1) + a[0]) * 3];
const ay = positions[(a[1] * (nx + 1) + a[0]) * 3 + 1];
const az = positions[(a[1] * (nx + 1) + a[0]) * 3 + 2];
const bx = positions[(b[1] * (nx + 1) + b[0]) * 3];
const by = positions[(b[1] * (nx + 1) + b[0]) * 3 + 1];
const bz = positions[(b[1] * (nx + 1) + b[0]) * 3 + 2];
wallPositions.push(
ax, ay, az, bx, by, bz, bx, -1.06, bz,
ax, ay, az, bx, -1.06, bz, ax, -1.06, az
);
}
const sideGeometry = new THREE.BufferGeometry();
sideGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(wallPositions, 3)
);
sideGeometry.computeVertexNormals();
const sides = new THREE.Mesh(
sideGeometry,
new THREE.MeshStandardMaterial({
color: 0x756b58,
roughness: 1,
flatShading: true
})
);
sides.castShadow = true;
sides.receiveShadow = true;
scene.add(sides);
}
makeTerrain();
// A dark plinth makes the scene read as a bounded tabletop model.
const base = new THREE.Mesh(
new THREE.BoxGeometry(23.4, 0.8, 21.2),
new THREE.MeshStandardMaterial({
color: 0x343b3d,
roughness: 0.92
})
);
base.position.set(0, -1.48, 2.2);
base.receiveShadow = true;
scene.add(base);
// ------------------------------------------------------------
// River
// ------------------------------------------------------------
function makeRiver() {
const riverSlab = new THREE.Mesh(
new THREE.BoxGeometry(23.2, 1.0, 7.1),
new THREE.MeshStandardMaterial({
color: 0x245f69,
roughness: 0.7
})
);
riverSlab.position.set(0, -0.58, 9.1);
scene.add(riverSlab);
const nx = 28;
const nz = 8;
const positions = [];
const colors = [];
const indices = [];
const shallow = new THREE.Color(0x4e9b9e);
const deep = new THREE.Color(0x246d7d);
for (let j = 0; j <= nz; j++) {
const z = 5.57 + (j / nz) * 7.05;
for (let i = 0; i <= nx; i++) {
const x = -11.5 + (i / nx) * 23;
const y =
-0.065 +
Math.sin(x * 0.9 + z * 1.7) * 0.018 +
Math.cos(x * 0.42 - z * 1.1) * 0.013;
positions.push(x, y, z);
const mix = THREE.MathUtils.clamp(
0.3 + 0.25 * Math.sin(x * 0.38 + z) +
0.15 * Math.cos(z * 0.9),
0,
1
);
const c = new THREE.Color().lerpColors(deep, shallow, mix);
colors.push(c.r, c.g, c.b);
}
}
for (let j = 0; j < nz; j++) {
for (let i = 0; i < nx; i++) {
const a = j * (nx + 1) + i;
const b = a + 1;
const c = a + nx + 1;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setIndex(indices);
const faceted = geometry.toNonIndexed();
faceted.computeVertexNormals();
const water = new THREE.Mesh(
faceted,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.36,
metalness: 0.12,
flatShading: true
})
);
water.receiveShadow = true;
scene.add(water);
const wavePositions = [];
for (let i = 0; i < 42; i++) {
const x = -10.8 + Math.random() * 21.6;
const z = 6.15 + Math.random() * 5.95;
const length = 0.35 + Math.random() * 1.05;
const bend = (Math.random() - 0.5) * 0.08;
wavePositions.push(
x, 0.008, z,
x + length, 0.008, z + bend
);
}
const waveGeometry = new THREE.BufferGeometry();
waveGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(wavePositions, 3)
);
const waves = new THREE.LineSegments(
waveGeometry,
new THREE.LineBasicMaterial({
color: 0xd8eff0,
transparent: true,
opacity: 0.46
})
);
scene.add(waves);
}
makeRiver();
// ------------------------------------------------------------
// Winding lanes and stair flights
// ------------------------------------------------------------
function makeLaneRibbon(controlPoints, width = 1.62) {
const curve = new THREE.CatmullRomCurve3(
controlPoints.map(([x, z]) => new THREE.Vector3(x, 0, z)),
false,
"catmullrom",
0.38
);
const count = 130;
const positions = [];
const indices = [];
for (let i = 0; i <= count; i++) {
const t = i / count;
const p = curve.getPoint(t);
const tangent = curve.getTangent(t);
const side = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const leftX = p.x + side.x * width * 0.5;
const leftZ = p.z + side.z * width * 0.5;
const rightX = p.x - side.x * width * 0.5;
const rightZ = p.z - side.z * width * 0.5;
positions.push(
leftX, terrainHeight(leftX, leftZ) + 0.035, leftZ,
rightX, terrainHeight(rightX, rightZ) + 0.035, rightZ
);
if (i < count) {
const a = i * 2;
indices.push(a, a + 2, a + 1, a + 1, a + 2, a + 3);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const lane = new THREE.Mesh(geometry, streetMaterial);
lane.receiveShadow = true;
scene.add(lane);
return curve;
}
function makeStairs(curve, stepCount = 58, width = 1.34) {
const points = curve.getSpacedPoints(stepCount);
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const angle = Math.atan2(dx, dz);
const top =
Math.max(
terrainHeight(a.x, a.z),
terrainHeight(b.x, b.z)
) + 0.15;
const geometry = new THREE.BoxGeometry(
width,
0.18,
length + 0.055
);
const step = new THREE.Mesh(geometry, stairMaterial);
step.position.set(
(a.x + b.x) * 0.5,
top - 0.09,
(a.z + b.z) * 0.5
);
step.rotation.y = angle;
step.castShadow = true;
step.receiveShadow = true;
scene.add(step);
const lip = new THREE.Mesh(
new THREE.BoxGeometry(width * 0.97, 0.025, 0.035),
stepEdgeMaterial
);
lip.position.set(
(a.x + b.x) * 0.5,
top + 0.004,
(a.z + b.z) * 0.5
);
lip.rotation.y = angle;
scene.add(lip);
}
}
function cylinderBetween(a, b, radius, material, radialSegments = 6) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments
),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
return mesh;
}
function addHandrail(curve, sideSign = 1) {
const railPoints = [];
const count = 38;
for (let i = 0; i <= count; i++) {
const t = i / count;
const p = curve.getPoint(t);
const tangent = curve.getTangent(t);
const side = new THREE.Vector3(-tangent.z, 0, tangent.x).normalize();
const x = p.x + side.x * sideSign * 0.82;
const z = p.z + side.z * sideSign * 0.82;
const ground = terrainHeight(x, z);
railPoints.push(new THREE.Vector3(x, ground + 0.92, z));
if (i % 5 === 0) {
const post = cylinderBetween(
new THREE.Vector3(x, ground + 0.02, z),
new THREE.Vector3(x, ground + 0.94, z),
0.022,
ironMaterial,
6
);
scene.add(post);
}
}
for (let i = 0; i < railPoints.length - 1; i++) {
scene.add(
cylinderBetween(
railPoints[i],
railPoints[i + 1],
0.025,
ironMaterial,
6
)
);
}
}
const mainStairCurve = makeLaneRibbon([
[-3.8, -6.8],
[-4.5, -5.2],
[-2.8, -4.2],
[-3.3, -2.6],
[-1.6, -1.5],
[-2.1, 0.1],
[-0.2, 1.3],
[1.1, 2.7],
[3.3, 4.1]
]);
makeStairs(mainStairCurve, 66, 1.36);
addHandrail(mainStairCurve, 1);
const eastStairCurve = makeLaneRibbon([
[7.0, -5.6],
[5.4, -4.2],
[5.7, -2.4],
[4.3, -0.8],
[4.8, 1.2],
[3.3, 4.1]
], 1.38);
makeStairs(eastStairCurve, 50, 1.20);
const westConnector = makeLaneRibbon([
[-8.2, -1.5],
[-6.5, -0.3],
[-4.2, 0.8],
[-2.1, 1.8],
[1.1, 2.7]
], 1.25);
makeStairs(westConnector, 40, 1.08);
// ------------------------------------------------------------
// Buildings: individually placed and adapted to local ground
// ------------------------------------------------------------
function localGroundHeight(x, z, rotation, localX, localZ) {
const c = Math.cos(rotation);
const s = Math.sin(rotation);
const wx = x + localX * c + localZ * s;
const wz = z - localX * s + localZ * c;
return terrainHeight(wx, wz);
}
function createFoundation(group, x, z, width, depth, rotation, level) {
const corners = [
[-width / 2, -depth / 2],
[ width / 2, -depth / 2],
[ width / 2, depth / 2],
[-width / 2, depth / 2]
];
const positions = [];
for (let i = 0; i < 4; i++) {
const a = corners[i];
const b = corners[(i + 1) % 4];
const ay = localGroundHeight(x, z, rotation, a[0], a[1]) - 0.06;
const by = localGroundHeight(x, z, rotation, b[0], b[1]) - 0.06;
positions.push(
a[0], ay, a[1],
b[0], by, b[1],
b[0], level, b[1],
a[0], ay, a[1],
b[0], level, b[1],
a[0], level, a[1]
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
const foundation = new THREE.Mesh(geometry, foundationMaterial);
foundation.castShadow = true;
foundation.receiveShadow = true;
group.add(foundation);
}
function addWindow(parent, x, y, z, rotationY, shutterColor = 0x486d69) {
const windowGroup = new THREE.Group();
windowGroup.position.set(x, y, z);
windowGroup.rotation.y = rotationY;
const frame = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 0.76, 0.065),
frameMaterial
);
windowGroup.add(frame);
const pane = new THREE.Mesh(
new THREE.BoxGeometry(0.45, 0.62, 0.085),
windowMaterial
);
pane.position.z = 0.035;
windowGroup.add(pane);
const shutterMaterial = new THREE.MeshStandardMaterial({
color: shutterColor,
roughness: 0.88
});
const shutterA = new THREE.Mesh(
new THREE.BoxGeometry(0.15, 0.65, 0.055),
shutterMaterial
);
shutterA.position.set(-0.36, 0, 0.045);
const shutterB = shutterA.clone();
shutterB.position.x = 0.36;
windowGroup.add(shutterA, shutterB);
parent.add(windowGroup);
}
function makeRoof(parent, level, width, depth, type, roofColor) {
const overhang = 0.22;
const rx = width / 2 + overhang;
const rz = depth / 2 + overhang;
const roofHeight = type === "hip"
? Math.max(0.88, Math.min(1.3, width * 0.42))
: Math.max(0.82, Math.min(1.45, width * 0.46));
const positions = [];
const colors = [];
const base = new THREE.Color(roofColor);
const c1 = base.clone().multiplyScalar(1.07);
const c2 = base.clone().multiplyScalar(0.88);
function triangle(a, b, c, color) {
positions.push(...a, ...b, ...c);
for (let i = 0; i < 3; i++) {
colors.push(color.r, color.g, color.b);
}
}
if (type === "hip") {
const ridgeY = level + roofHeight;
const frontZ = rz * 0.38;
const backZ = -rz * 0.38;
triangle(
[-rx, level, -rz], [-rx, level, rz], [0, ridgeY, frontZ], c1
);
triangle(
[-rx, level, -rz], [0, ridgeY, frontZ], [0, ridgeY, backZ], c2
);
triangle(
[rx, level, -rz], [0, ridgeY, frontZ], [rx, level, rz], c2
);
triangle(
[rx, level, -rz], [0, ridgeY, backZ], [0, ridgeY, frontZ], c1
);
triangle(
[-rx, level, rz], [0, ridgeY, frontZ], [rx, level, rz], c1
);
triangle(
[-rx, level, -rz], [rx, level, -rz], [0, ridgeY, backZ], c2
);
} else {
const ridgeY = level + roofHeight;
triangle(
[-rx, level, -rz], [0, ridgeY, -rz], [0, ridgeY, rz], c1
);
triangle(
[-rx, level, -rz], [0, ridgeY, rz], [-rx, level, rz], c2
);
triangle(
[rx, level, -rz], [0, ridgeY, rz], [0, ridgeY, -rz], c2
);
triangle(
[rx, level, -rz], [rx, level, rz], [0, ridgeY, rz], c1
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.computeVertexNormals();
const roofMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.96,
side: THREE.DoubleSide,
flatShading: true
});
const roof = new THREE.Mesh(geometry, roofMaterial);
roof.castShadow = true;
roof.receiveShadow = true;
parent.add(roof);
// Tile courses: thin raised strips running up the roof slopes.
const tilePositions = [];
if (type === "gable") {
const rows = Math.max(4, Math.floor(rz * 2.2));
for (const side of [-1, 1]) {
for (let row = 0; row <= rows; row++) {
const z = -rz + (row / rows) * rz * 2;
const z2 = z + 0.035;
tilePositions.push(
side * rx, level + 0.025, z,
0, level + roofHeight + 0.025, z,
0, level + roofHeight + 0.025, z2,
side * rx, level + 0.025, z,
0, level + roofHeight + 0.025, z2,
side * rx, level + 0.025, z2
);
}
}
}
const tileGeometry = new THREE.BufferGeometry();
tileGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(tilePositions, 3)
);
if (tilePositions.length) {
const tiles = new THREE.Mesh(
tileGeometry,
new THREE.MeshStandardMaterial({
color: 0x853f31,
roughness: 1,
side: THREE.DoubleSide
})
);
parent.add(tiles);
}
// Ridge cap.
const ridgeLength = type === "hip" ? rz * 0.76 : rz * 2;
const ridge = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.095, ridgeLength),
terracottaMaterial
);
ridge.position.set(
0,
level + roofHeight + 0.035,
type === "hip" ? 0 : 0
);
ridge.castShadow = true;
parent.add(ridge);
}
function addBuilding(config, index) {
const {
x,
z,
width,
depth,
height,
rotation,
color,
roofType
} = config;
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const ground = [
localGroundHeight(x, z, rotation, -width / 2, -depth / 2),
localGroundHeight(x, z, rotation, width / 2, -depth / 2),
localGroundHeight(x, z, rotation, width / 2, depth / 2),
localGroundHeight(x, z, rotation, -width / 2, depth / 2)
];
const level = Math.max(...ground) + 0.07;
createFoundation(
group, x, z, width, depth, rotation, level
);
const body = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
wallMaterial(color)
);
body.position.y = level + height / 2;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const roofColors = [
0xa64d35,
0xb5583b,
0xc16a45,
0x9f4934
];
makeRoof(
group,
level + height,
width,
depth,
roofType || (index % 5 === 2 ? "hip" : "gable"),
roofColors[index % roofColors.length]
);
const floorCount = Math.max(2, Math.floor(height / 1.42));
for (let floor = 0; floor < floorCount; floor++) {
const y = level + 1.13 + floor * 1.36;
if (y > level + height - 0.3) continue;
addWindow(
group,
-width * 0.23,
y,
depth / 2 + 0.045,
0,
index % 2 ? 0x466b68 : 0x526d82
);
addWindow(
group,
width * 0.23,
y,
depth / 2 + 0.045,
0,
index % 3 ? 0x7f6c4c : 0x466b68
);
if (index % 2 === 0 && floor > 0) {
addWindow(
group,
width / 2 + 0.045,
y,
0,
Math.PI / 2,
0x526d82
);
}
}
const door = new THREE.Mesh(
new THREE.BoxGeometry(0.58, 1.22, 0.075),
woodMaterial
);
door.position.set(
-width * 0.13,
level + 0.61,
depth / 2 + 0.047
);
group.add(door);
const lintel = new THREE.Mesh(
new THREE.BoxGeometry(0.75, 0.10, 0.11),
frameMaterial
);
lintel.position.set(
-width * 0.13,
level + 1.24,
depth / 2 + 0.065
);
group.add(lintel);
// Chimneys rise directly through the tiled roof.
const chimney = new THREE.Mesh(
new THREE.BoxGeometry(0.29, 0.85, 0.31),
wallMaterial(0xd6c5a5)
);
chimney.position.set(
width * (index % 2 ? 0.25 : -0.24),
level + height + 0.44,
-depth * 0.16
);
chimney.castShadow = true;
group.add(chimney);
const chimneyCap = new THREE.Mesh(
new THREE.BoxGeometry(0.38, 0.10, 0.40),
terracottaMaterial
);
chimneyCap.position.set(
chimney.position.x,
chimney.position.y + 0.47,
chimney.position.z
);
group.add(chimneyCap);
// Small balconies reinforce the dense, vertical street character.
if (index % 4 === 1 && height > 3.6) {
const balcony = new THREE.Mesh(
new THREE.BoxGeometry(width * 0.72, 0.09, 0.48),
ironMaterial
);
balcony.position.set(
0,
level + 2.44,
depth / 2 + 0.24
);
balcony.castShadow = true;
group.add(balcony);
for (let rail = -2; rail <= 2; rail++) {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(0.025, 0.52, 0.025),
ironMaterial
);
bar.position.set(
rail * width * 0.15,
level + 2.70,
depth / 2 + 0.44
);
group.add(bar);
}
const balconyRail = new THREE.Mesh(
new THREE.BoxGeometry(width * 0.72, 0.035, 0.035),
ironMaterial
);
balconyRail.position.set(
0,
level + 2.96,
depth / 2 + 0.44
);
group.add(balconyRail);
}
return group;
}
const buildingConfigs = [
{ x: -8.0, z: -5.5, width: 2.2, depth: 2.4, height: 4.2, rotation: 0.08, color: 0xe2d2b4 },
{ x: -5.7, z: -6.2, width: 1.9, depth: 2.0, height: 3.7, rotation: -0.08, color: 0xd8c29e },
{ x: 1.3, z: -6.2, width: 2.1, depth: 2.3, height: 4.4, rotation: -0.04, color: 0xd7d8c9 },
{ x: 4.3, z: -6.0, width: 2.5, depth: 2.4, height: 3.9, rotation: 0.10, color: 0xe0b9a5 },
{ x: 7.5, z: -5.0, width: 2.1, depth: 2.6, height: 4.5, rotation: -0.13, color: 0xc9b68f },
{ x: -8.2, z: -2.6, width: 2.4, depth: 2.6, height: 4.1, rotation: 0.14, color: 0xc4cfd0 },
{ x: -5.4, z: -2.6, width: 2.2, depth: 2.4, height: 4.6, rotation: -0.10, color: 0xe6cf91 },
{ x: -0.5, z: -3.4, width: 2.4, depth: 2.5, height: 5.2, rotation: 0.12, color: 0xe2d2b4 },
{ x: 2.0, z: -3.5, width: 2.3, depth: 2.5, height: 4.3, rotation: -0.08, color: 0xd8c29e },
{ x: 8.2, z: -2.2, width: 1.8, depth: 2.4, height: 4.1, rotation: -0.12, color: 0xd9d3bd },
{ x: -8.1, z: 0.0, width: 2.0, depth: 2.3, height: 3.8, rotation: 0.08, color: 0xe0b9a5 },
{ x: -5.4, z: 0.2, width: 2.3, depth: 2.4, height: 4.2, rotation: -0.08, color: 0xd7d8c9 },
{ x: -3.2, z: 0.5, width: 1.7, depth: 2.0, height: 3.4, rotation: 0.05, color: 0xc9b68f },
{ x: 0.3, z: -0.6, width: 2.0, depth: 2.2, height: 3.9, rotation: -0.10, color: 0xe6cf91 },
{ x: 2.6, z: 0.0, width: 2.0, depth: 2.3, height: 3.5, rotation: 0.12, color: 0xc4cfd0 },
{ x: 6.8, z: 0.8, width: 2.2, depth: 2.5, height: 3.7, rotation: -0.10, color: 0xe2d2b4 }
];
buildingConfigs.forEach(addBuilding);
// ------------------------------------------------------------
// Small chapel and bell tower
// ------------------------------------------------------------
function makeChapel() {
const x = -1.1;
const z = -6.1;
const width = 2.35;
const depth = 2.15;
const height = 2.85;
const rotation = 0.025;
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
const ground = [
localGroundHeight(x, z, rotation, -width / 2, -depth / 2),
localGroundHeight(x, z, rotation, width / 2, -depth / 2),
localGroundHeight(x, z, rotation, width / 2, depth / 2),
localGroundHeight(x, z, rotation, -width / 2, depth / 2)
];
const level = Math.max(...ground) + 0.08;
createFoundation(group, x, z, width, depth, rotation, level);
const chapel = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
wallMaterial(0xf0e7d0)
);
chapel.position.y = level + height / 2;
chapel.castShadow = true;
chapel.receiveShadow = true;
group.add(chapel);
makeRoof(
group,
level + height,
width,
depth,
"gable",
0xad5037
);
// Rounded doorway silhouette.
const shape = new THREE.Shape();
shape.moveTo(-0.43, 0);
shape.lineTo(-0.43, 0.92);
shape.absarc(0, 0.92, 0.43, Math.PI, 0, true);
shape.lineTo(0.43, 0);
shape.closePath();
const doorway = new THREE.Mesh(
new THREE.ShapeGeometry(shape, 12),
new THREE.MeshStandardMaterial({
color: 0x3b4c4b,
roughness: 0.9,
side: THREE.DoubleSide
})
);
doorway.position.set(0, level + 0.03, depth / 2 + 0.018);
group.add(doorway);
// Bell tower attached to the left side.
const towerHeight = 4.6;
const tower = new THREE.Mesh(
new THREE.BoxGeometry(0.84, towerHeight, 0.84),
wallMaterial(0xf0e7d0)
);
tower.position.set(-1.34, level + towerHeight / 2, -0.05);
tower.castShadow = true;
group.add(tower);
const bellOpening = new THREE.Mesh(
new THREE.BoxGeometry(0.33, 0.63, 0.06),
windowMaterial
);
bellOpening.position.set(-1.34, level + 3.78, 0.39);
group.add(bellOpening);
const bell = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.26, 7),
new THREE.MeshStandardMaterial({
color: 0xb88a3e,
roughness: 0.45,
metalness: 0.55
})
);
bell.position.set(-1.34, level + 3.71, 0.41);
bell.rotation.x = Math.PI;
group.add(bell);
const towerRoof = new THREE.Mesh(
new THREE.ConeGeometry(0.68, 0.9, 4),
terracottaMaterial
);
towerRoof.position.set(-1.34, level + towerHeight + 0.43, -0.05);
towerRoof.rotation.y = Math.PI / 4;
towerRoof.castShadow = true;
group.add(towerRoof);
}
makeChapel();
// ------------------------------------------------------------
// River-facing terrace
// ------------------------------------------------------------
function makeViewpoint() {
const x = 3.5;
const z = 4.28;
const width = 4.2;
const depth = 1.8;
const ground = [
terrainHeight(x - width / 2, z - depth / 2),
terrainHeight(x + width / 2, z - depth / 2),
terrainHeight(x + width / 2, z + depth / 2),
terrainHeight(x - width / 2, z + depth / 2)
];
const level = Math.max(...ground) + 0.08;
const platform = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.20, depth),
new THREE.MeshStandardMaterial({
color: 0xc5b99f,
roughness: 0.98,
flatShading: true
})
);
platform.position.set(x, level - 0.10, z);
platform.castShadow = true;
platform.receiveShadow = true;
scene.add(platform);
const railZ = z + 0.76;
const railLeft = x - width / 2 + 0.12;
const railRight = x + width / 2 - 0.12;
for (let px = railLeft; px <= railRight + 0.01; px += 0.82) {
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.027, 0.032, 0.88, 7),
ironMaterial
);
post.position.set(px, level + 0.44, railZ);
post.castShadow = true;
scene.add(post);
}
scene.add(
cylinderBetween(
new THREE.Vector3(railLeft, level + 0.88, railZ),
new THREE.Vector3(railRight, level + 0.88, railZ),
0.035,
ironMaterial,
8
)
);
scene.add(
cylinderBetween(
new THREE.Vector3(railLeft, level + 0.45, railZ),
new THREE.Vector3(railRight, level + 0.45, railZ),
0.022,
ironMaterial,
7
)
);
// Bench facing the river.
const bench = new THREE.Group();
bench.position.set(3.55, level, 3.98);
for (let plank = -1; plank <= 1; plank++) {
const seat = new THREE.Mesh(
new THREE.BoxGeometry(1.62, 0.085, 0.16),
woodMaterial
);
seat.position.set(0, 0.47, plank * 0.18);
seat.castShadow = true;
bench.add(seat);
}
const back = new THREE.Mesh(
new THREE.BoxGeometry(1.62, 0.28, 0.075),
woodMaterial
);
back.position.set(0, 0.78, -0.22);
back.rotation.x = -0.12;
back.castShadow = true;
bench.add(back);
for (const legX of [-0.62, 0.62]) {
const leg = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.48, 0.34),
ironMaterial
);
leg.position.set(legX, 0.24, 0);
leg.castShadow = true;
bench.add(leg);
}
scene.add(bench);
// Small ceramic-looking viewpoint sign.
const signCanvas = document.createElement("canvas");
signCanvas.width = 512;
signCanvas.height = 128;
const ctx = signCanvas.getContext("2d");
ctx.fillStyle = "#eee6d2";
ctx.fillRect(0, 0, 512, 128);
ctx.strokeStyle = "#2e7180";
ctx.lineWidth = 10;
ctx.strokeRect(8, 8, 496, 112);
ctx.fillStyle = "#2c5964";
ctx.font = "bold 43px Georgia";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("MIRADOURO", 256, 66);
const texture = new THREE.CanvasTexture(signCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const signPost = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.045, 1.45, 7),
ironMaterial
);
signPost.position.set(1.26, level + 0.72, 4.88);
signPost.castShadow = true;
scene.add(signPost);
const sign = new THREE.Mesh(
new THREE.PlaneGeometry(1.32, 0.33),
new THREE.MeshBasicMaterial({
map: texture,
side: THREE.DoubleSide
})
);
sign.position.set(1.26, level + 1.43, 4.90);
sign.rotation.y = -0.08;
scene.add(sign);
// Terracotta pots with faceted greenery.
const foliageMaterial = new THREE.MeshStandardMaterial({
color: 0x567251,
roughness: 0.95,
flatShading: true
});
for (const [px, pz] of [[5.25, 4.55], [1.38, 4.63]]) {
const pot = new THREE.Mesh(
new THREE.CylinderGeometry(0.25, 0.19, 0.42, 7),
terracottaMaterial
);
pot.position.set(px, level + 0.21, pz);
pot.castShadow = true;
scene.add(pot);
const plant = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.38, 0),
foliageMaterial
);
plant.position.set(px, level + 0.64, pz);
plant.scale.set(1, 1.2, 1);
plant.castShadow = true;
scene.add(plant);
}
}
makeViewpoint();
// ------------------------------------------------------------
// Street lamps
// ------------------------------------------------------------
const lampGlowMaterial = new THREE.MeshStandardMaterial({
color: 0xffd88c,
emissive: 0xe6a442,
emissiveIntensity: 0.8,
roughness: 0.35
});
function makeLamp(x, z) {
const ground = terrainHeight(x, z);
const group = new THREE.Group();
group.position.set(x, ground, z);
const pole = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.04, 1.48, 7),
ironMaterial
);
pole.position.y = 0.74;
pole.castShadow = true;
group.add(pole);
const lantern = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.23, 0.17),
lampGlowMaterial
);
lantern.position.y = 1.55;
group.add(lantern);
const cap = new THREE.Mesh(
new THREE.ConeGeometry(0.17, 0.12, 4),
ironMaterial
);
cap.position.y = 1.73;
cap.rotation.y = Math.PI / 4;
group.add(cap);
scene.add(group);
}
[
[-5.8, -4.8],
[-4.7, -2.1],
[-2.7, 0.8],
[0.9, 2.0],
[5.4, 1.7],
[6.4, -3.5],
[-7.0, -1.1]
].forEach(([x, z]) => makeLamp(x, z));
// ------------------------------------------------------------
// Custom camera controls — no addon modules
// ------------------------------------------------------------
const control = {
target: new THREE.Vector3(0, 1.35, -0.5),
targetGoal: new THREE.Vector3(0, 1.35, -0.5),
radius: 30,
radiusGoal: 30,
theta: 0.43,
thetaGoal: 0.43,
phi: 0.90,
phiGoal: 0.90
};
function setView(name) {
if (name === "terrace") {
control.targetGoal.set(3.3, 0.75, 8.0);
control.radiusGoal = 7.0;
control.thetaGoal = Math.PI;
control.phiGoal = 1.30;
} else if (name === "stairs") {
control.targetGoal.set(-2.2, 2.15, -1.6);
control.radiusGoal = 11.5;
control.thetaGoal = 0.26;
control.phiGoal = 1.08;
} else {
control.targetGoal.set(0, 1.35, -0.5);
control.radiusGoal = 30;
control.thetaGoal = 0.43;
control.phiGoal = 0.90;
}
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setView(button.dataset.view);
});
});
const pointers = new Map();
let activeMode = "orbit";
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: event.button
});
if (pointers.size === 1) {
activeMode = event.button === 2 ? "pan" : "orbit";
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = previous.x - event.clientX;
const dy = previous.y - event.clientY;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
button: previous.button
});
if (pointers.size >= 2) {
const [a, b] = [...pointers.values()];
const oldDistance = Math.hypot(a.x - b.x, a.y - b.y);
const newDistance = Math.hypot(
event.clientX - (pointers.size > 1 ? b.x : a.x),
event.clientY - (pointers.size > 1 ? b.y : a.y)
);
if (oldDistance > 0 && newDistance > 0) {
control.radiusGoal = THREE.MathUtils.clamp(
control.radiusGoal * oldDistance / newDistance,
5,
48
);
}
return;
}
if (activeMode === "pan" || event.shiftKey) {
const scale = control.radiusGoal * 0.00135;
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
right.y = 0;
right.normalize();
control.targetGoal.addScaledVector(right, dx * scale);
control.targetGoal.addScaledVector(up, -dy * scale);
control.targetGoal.x = THREE.MathUtils.clamp(control.targetGoal.x, -12, 12);
control.targetGoal.y = THREE.MathUtils.clamp(control.targetGoal.y, -0.5, 8);
control.targetGoal.z = THREE.MathUtils.clamp(control.targetGoal.z, -10, 11);
} else {
control.thetaGoal -= dx * 0.005;
control.phiGoal = THREE.MathUtils.clamp(
control.phiGoal - dy * 0.005,
0.20,
1.46
);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("lostpointercapture", endPointer);
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
control.radiusGoal = THREE.MathUtils.clamp(
control.radiusGoal * Math.exp(event.deltaY * 0.001),
5,
48
);
}, { passive: false });
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "ArrowLeft":
control.thetaGoal += 0.12;
break;
case "ArrowRight":
control.thetaGoal -= 0.12;
break;
case "ArrowUp":
control.phiGoal = Math.max(0.2, control.phiGoal - 0.08);
break;
case "ArrowDown":
control.phiGoal = Math.min(1.46, control.phiGoal + 0.08);
break;
case "+":
case "=":
control.radiusGoal = Math.max(5, control.radiusGoal * 0.9);
break;
case "-":
case "_":
control.radiusGoal = Math.min(48, control.radiusGoal * 1.1);
break;
case "r":
case "R":
setView("overview");
break;
}
});
function updateCamera() {
const smoothing = 0.12;
let thetaDifference =
control.thetaGoal - control.theta;
thetaDifference = Math.atan2(
Math.sin(thetaDifference),
Math.cos(thetaDifference)
);
control.theta += thetaDifference * smoothing;
control.phi += (control.phiGoal - control.phi) * smoothing;
control.radius += (control.radiusGoal - control.radius) * smoothing;
control.target.lerp(control.targetGoal, smoothing);
const sinPhi = Math.sin(control.phi);
camera.position.set(
control.target.x +
control.radius * sinPhi * Math.sin(control.theta),
control.target.y +
control.radius * Math.cos(control.phi),
control.target.z +
control.radius * sinPhi * Math.cos(control.theta)
);
camera.lookAt(control.target);
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
updateCamera();
renderer.setAnimationLoop(() => {
updateCamera();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```