Prompt specification
Build or customize the Three.js 3D scene "Eixample four-corner intersection scene" 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: Eixample four-corner intersection scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/eixample-four-corner-intersection-scene-x2811
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring the chamfered intersections of Barcelona's Eixample. Build a bounded voxel intersection with four referenced chamfered blocks, courtyards hinted behind facades, and street trees. Keep traffic simple enough to preserve the geometry's readability. Voxel style.
Working Three.js Starter (eixample-four-corner-intersection-scene-x2811):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Eixample — Voxel Intersection</title>
<style>
:root {
color-scheme: light;
--ink: #18333a;
--paper: rgba(248, 239, 218, 0.88);
--line: rgba(24, 51, 58, 0.18);
--accent: #b75f42;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d6dc;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--ink);
}
.card {
position: absolute;
top: 22px;
left: 22px;
width: min(340px, calc(100vw - 44px));
padding: 18px 19px 16px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--paper);
box-shadow: 0 14px 38px rgba(43, 66, 69, 0.16);
backdrop-filter: blur(12px);
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
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(25px, 3vw, 36px);
font-weight: 500;
line-height: 1.03;
letter-spacing: -0.025em;
}
.description {
margin: 12px 0 15px;
max-width: 29em;
color: rgba(24, 51, 58, 0.73);
font-size: 12px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px 12px;
margin-bottom: 15px;
color: rgba(24, 51, 58, 0.7);
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 1px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.buttons {
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid rgba(24, 51, 58, 0.22);
border-radius: 2px;
padding: 8px 11px;
background: rgba(255, 255, 255, 0.42);
color: var(--ink);
font: inherit;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.78);
transform: translateY(-1px);
}
.badge {
position: absolute;
top: 22px;
right: 22px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 3px;
background: rgba(248, 239, 218, 0.68);
color: rgba(24, 51, 58, 0.73);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
backdrop-filter: blur(8px);
}
.hint {
position: absolute;
right: 22px;
bottom: 20px;
padding: 8px 10px;
border-radius: 3px;
background: rgba(24, 51, 58, 0.72);
color: #f8efd9;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
@media (max-width: 620px) {
.card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
.description {
display: none;
}
.legend {
margin-bottom: 10px;
}
.badge {
display: none;
}
.hint {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="card">
<p class="eyebrow">Barcelona · Eixample</p>
<h1>Chamfered<br />voxel junction</h1>
<p class="description">
Four corner blocks meet at a compact civic intersection. Stepped
silhouettes, recessed courtyards and restrained traffic preserve the
characteristic 45-degree geometry of the Eixample.
</p>
<div class="legend">
<span><i class="swatch" style="background:#c98f63"></i>Block 01</span>
<span><i class="swatch" style="background:#d2a36f"></i>Block 02</span>
<span><i class="swatch" style="background:#c98270"></i>Block 03</span>
<span><i class="swatch" style="background:#d3bd91"></i>Block 04</span>
</div>
<div class="buttons">
<button id="reset-view" type="button">Reset view</button>
<button id="toggle-traffic" type="button">Pause traffic</button>
</div>
</section>
<div class="badge">4 blocks · 4 hidden courtyards</div>
<div class="hint">Drag to orbit · Wheel to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera and lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d6dc);
scene.fog = new THREE.Fog(0xb9d6dc, 118, 225);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel model of a chamfered Barcelona Eixample intersection"
);
document.body.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-70, 70, 55, -55, 0.1, 350);
const controls = {
target: new THREE.Vector3(0, 3, 0),
theta: Math.PI * 0.25,
phi: 0.72,
zoom: 1
};
function updateCamera() {
const radius = 120;
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + radius * sinPhi * Math.sin(controls.theta),
controls.target.y + radius * Math.cos(controls.phi),
controls.target.z + radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const aspect = window.innerWidth / window.innerHeight;
const frustumHeight = Math.max(108, 150 / aspect);
camera.left = (-frustumHeight * aspect) / 2;
camera.right = (frustumHeight * aspect) / 2;
camera.top = frustumHeight / 2;
camera.bottom = -frustumHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xeaf8ff, 0x8d7158, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc9, 3.2);
sun.position.set(-48, 82, -36);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -76;
sun.shadow.camera.right = 76;
sun.shadow.camera.top = 76;
sun.shadow.camera.bottom = -76;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 180;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc5e8ff, 0.55);
fill.position.set(55, 35, 50);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared geometry and materials
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const up = new THREE.Vector3(0, 1, 0);
const identityQuaternion = new THREE.Quaternion();
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
...options
});
}
const materials = {
base: standardMaterial(0xb8a88f),
ground: standardMaterial(0xd7c6a9),
sidewalk: standardMaterial(0xd9cbb3),
curb: standardMaterial(0xeee1c9),
asphalt: standardMaterial(0x394b50, { roughness: 0.96 }),
lane: standardMaterial(0xf1dfb2),
crosswalk: standardMaterial(0xf4ead3),
roof: standardMaterial(0x9a5843),
roofEdge: standardMaterial(0xe2c9a5),
roofEquipment: standardMaterial(0x785f50),
planter: standardMaterial(0x9b7455),
courtyard: standardMaterial(0xc7b492),
trunk: standardMaterial(0x76513b),
foliage: standardMaterial(0xffffff),
lamp: standardMaterial(0xffffff, {
emissive: 0xffd37a,
emissiveIntensity: 0.65
}),
water: standardMaterial(0x63b9c2, {
roughness: 0.24,
metalness: 0.08
}),
signal: standardMaterial(0x273c40),
signalRed: standardMaterial(0xd34d3f, {
emissive: 0x7b130c,
emissiveIntensity: 0.45
}),
signalGreen: standardMaterial(0x5aaa69, {
emissive: 0x174c23,
emissiveIntensity: 0.35
})
};
const palettes = [
{
facade: standardMaterial(0xc98f63),
trim: standardMaterial(0xe2c39c),
shutter: 0x486f70
},
{
facade: standardMaterial(0xd2a36f),
trim: standardMaterial(0xf0d8ad),
shutter: 0x775748
},
{
facade: standardMaterial(0xc98270),
trim: standardMaterial(0xe5c2a4),
shutter: 0x41646c
},
{
facade: standardMaterial(0xd3bd91),
trim: standardMaterial(0xf0dfbd),
shutter: 0x80594b
}
];
function makeBox(width, height, depth, x, y, z, material, cast = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.castShadow = cast;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function matrixFrom(position, quaternion, scale) {
return new THREE.Matrix4().compose(
position,
quaternion,
new THREE.Vector3(scale.x, scale.y, scale.z)
);
}
function quaternionFromBasis(xAxis, yAxis, zAxis) {
const matrix = new THREE.Matrix4().makeBasis(xAxis, yAxis, zAxis);
return new THREE.Quaternion().setFromRotationMatrix(matrix);
}
// -------------------------------------------------------------------------
// Base, streets, sidewalks and road markings
// -------------------------------------------------------------------------
const ROAD_HALF = 8;
const QUADRANT_EDGE = 52;
const GROUND_LEVEL = 0.36;
makeBox(116, 1.6, 116, 0, -0.8, 0, materials.base);
makeBox(115, 0.2, 115, 0, 0.02, 0, materials.ground, false);
const roadArms = [
[16, 44, 0, 30],
[16, 44, 0, -30],
[44, 16, 30, 0],
[44, 16, -30, 0],
[16, 16, 0, 0]
];
for (const [w, d, x, z] of roadArms) {
makeBox(w, 0.12, d, x, 0.12, z, materials.asphalt, false);
}
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
makeBox(
QUADRANT_EDGE - ROAD_HALF,
0.32,
QUADRANT_EDGE - ROAD_HALF,
sx * (ROAD_HALF + QUADRANT_EDGE) / 2,
0.18,
sz * (ROAD_HALF + QUADRANT_EDGE) / 2,
materials.sidewalk,
false
);
}
}
// Fine central lane dashes, interrupted at the intersection.
for (let p = -49; p <= 49; p += 6) {
if (Math.abs(p) > 13) {
makeBox(2.7, 0.025, 0.12, p, 0.195, 0, materials.lane, false);
makeBox(0.12, 0.025, 2.7, 0, 0.195, p, materials.lane, false);
}
}
// Zebra crossings on all four approaches.
for (const side of [-1, 1]) {
for (let x = -6; x <= 6; x += 1.5) {
makeBox(
0.68,
0.03,
2.7,
x,
0.205,
side * 10.2,
materials.crosswalk,
false
);
}
for (let z = -6; z <= 6; z += 1.5) {
makeBox(
2.7,
0.03,
0.68,
side * 10.2,
0.205,
z,
materials.crosswalk,
false
);
}
}
// -------------------------------------------------------------------------
// Instanced facade details
// -------------------------------------------------------------------------
const frameInstances = [];
const glassInstances = [];
const shutterInstances = [];
const sillInstances = [];
const balconySlabInstances = [];
const balconyRailInstances = [];
const treeTrunkInstances = [];
const treeLeafInstances = [];
const lampPostInstances = [];
const lampHeadInstances = [];
function addInstance(bucket, position, quaternion, scale, color) {
bucket.push({
matrix: matrixFrom(position, quaternion, scale),
color: color instanceof THREE.Color ? color : new THREE.Color(color)
});
}
function addWindow(surface, normal, shutterColor, balcony = false, lit = false) {
const normalDirection = normal.clone().normalize();
const tangent = new THREE.Vector3()
.crossVectors(up, normalDirection)
.normalize();
const quaternion = quaternionFromBasis(tangent, up, normalDirection);
const width = 1.04;
const height = 1.42;
addInstance(
frameInstances,
surface.clone().addScaledVector(normalDirection, 0.065),
quaternion,
new THREE.Vector3(width + 0.34, height + 0.34, 0.14),
0xead7b8
);
addInstance(
glassInstances,
surface.clone().addScaledVector(normalDirection, 0.155),
quaternion,
new THREE.Vector3(width, height, 0.08),
lit ? 0xe4b968 : 0x294955
);
addInstance(
sillInstances,
surface.clone()
.addScaledVector(normalDirection, 0.12)
.add(new THREE.Vector3(0, -height / 2 - 0.12, 0)),
quaternion,
new THREE.Vector3(width + 0.52, 0.13, 0.36),
0xead7b8
);
for (const direction of [-1, 1]) {
addInstance(
shutterInstances,
surface
.clone()
.addScaledVector(normalDirection, 0.15)
.addScaledVector(tangent, direction * (width / 2 + 0.23)),
quaternion,
new THREE.Vector3(0.27, height, 0.09),
shutterColor
);
}
if (balcony) {
const bottom = surface.y - height / 2;
addInstance(
balconySlabInstances,
surface
.clone()
.addScaledVector(normalDirection, 0.34)
.add(new THREE.Vector3(0, bottom - surface.y - 0.17, 0)),
quaternion,
new THREE.Vector3(width + 0.82, 0.14, 0.76),
0xe7d4b3
);
addInstance(
balconyRailInstances,
surface
.clone()
.addScaledVector(normalDirection, 0.68)
.add(new THREE.Vector3(0, bottom - surface.y + 0.31, 0)),
quaternion,
new THREE.Vector3(width + 0.82, 0.65, 0.08),
0x3d565a
);
}
}
function addFacadeWindows(
axis,
fixed,
normalLocal,
start,
end,
height,
shutterColor,
outerFace
) {
const floors = Math.max(2, Math.floor((height - 0.35) / 2.55));
const spacing = 2.65;
const usable = end - start - 1.2;
const count = Math.max(1, Math.floor(usable / spacing));
for (let floor = 0; floor < floors; floor++) {
const y = GROUND_LEVEL + 1.75 + floor * 2.55;
for (let index = 0; index < count; index++) {
const along = start + 1.25 + index * spacing;
let localA;
let localB;
let normalWorld;
if (axis === "a") {
localA = along;
localB = fixed;
normalWorld = new THREE.Vector3(
0,
0,
normalLocal[1] * Math.sign(localB)
);
} else {
localA = fixed;
localB = along;
normalWorld = new THREE.Vector3(
normalLocal[0] * Math.sign(localA),
0,
0
);
}
const surface = new THREE.Vector3(
Math.sign(localA) * (12 + Math.abs(localA)),
y,
Math.sign(localB) * (12 + Math.abs(localB))
);
const lit = (Math.round(along * 3 + floor * 5) % 13) === 0;
const balcony = outerFace && floor > 0 && index % 3 === 0;
addWindow(
surface,
normalWorld,
shutterColor,
balcony,
lit
);
}
}
}
// -------------------------------------------------------------------------
// Four chamfered perimeter blocks
// -------------------------------------------------------------------------
const BLOCK_SIZE = 36;
const CHAMFER = 4;
const BUILDING_DEPTH = 7.5;
const blockHeights = [
[8.7, 10.7, 11.5, 9.8],
[10.2, 8.8, 11.8, 10.5],
[9.1, 11.7, 10.4, 12.0],
[11.2, 10.1, 8.9, 10.8]
];
function makeBlock(sx, sz, palette, heights, blockIndex) {
const point = (a, b, y = 0) =>
new THREE.Vector3(sx * (12 + a), y, sz * (12 + b));
const facade = palette.facade;
const trim = palette.trim;
const wings = [
{
axis: "a",
start: CHAMFER,
end: BLOCK_SIZE - CHAMFER,
centerFixed: BUILDING_DEPTH / 2,
outerFixed: 0,
innerFixed: BUILDING_DEPTH,
outward: [0, -1],
height: heights[0]
},
{
axis: "b",
start: CHAMFER,
end: BLOCK_SIZE - CHAMFER,
centerFixed: BUILDING_DEPTH / 2,
outerFixed: 0,
innerFixed: BUILDING_DEPTH,
outward: [-1, 0],
height: heights[1]
},
{
axis: "a",
start: CHAMFER,
end: BLOCK_SIZE - CHAMFER,
centerFixed: BLOCK_SIZE - BUILDING_DEPTH / 2,
outerFixed: BLOCK_SIZE,
innerFixed: BLOCK_SIZE - BUILDING_DEPTH,
outward: [0, 1],
height: heights[2]
},
{
axis: "b",
start: CHAMFER,
end: BLOCK_SIZE - CHAMFER,
centerFixed: BLOCK_SIZE - BUILDING_DEPTH / 2,
outerFixed: BLOCK_SIZE,
innerFixed: BLOCK_SIZE - BUILDING_DEPTH,
outward: [1, 0],
height: heights[3]
}
];
for (let wingIndex = 0; wingIndex < wings.length; wingIndex++) {
const wing = wings[wingIndex];
const length = wing.end - wing.start;
const height = wing.height;
let centerA;
let centerB;
let width;
let depth;
if (wing.axis === "a") {
centerA = (wing.start + wing.end) / 2;
centerB = wing.centerFixed;
width = length;
depth = BUILDING_DEPTH;
} else {
centerA = wing.centerFixed;
centerB = (wing.start + wing.end) / 2;
width = BUILDING_DEPTH;
depth = length;
}
const center = point(centerA, centerB);
makeBox(
width,
height,
depth,
center.x,
GROUND_LEVEL + height / 2,
center.z,
facade
);
makeBox(
width + 0.16,
0.82,
depth + 0.16,
center.x,
GROUND_LEVEL + 0.41,
center.z,
trim
);
makeBox(
width + 0.32,
0.22,
depth + 0.32,
center.x,
GROUND_LEVEL + height + 0.11,
center.z,
trim
);
makeBox(
width + 0.16,
0.18,
depth + 0.16,
center.x,
GROUND_LEVEL + height + 0.3,
center.z,
materials.roof
);
addFacadeWindows(
wing.axis,
wing.outerFixed,
wing.outward,
wing.start,
wing.end,
height,
palette.shutter,
true
);
addFacadeWindows(
wing.axis,
wing.innerFixed,
[-wing.outward[0], -wing.outward[1]],
wing.start,
wing.end,
height,
palette.shutter,
false
);
// Small roof volumes, chimneys and a few planted roof cubes.
const featureA = wing.start + 3.4;
const featureB = wing.end - 3.2;
const featurePosition = point(
wing.axis === "a" ? featureA : centerA,
wing.axis === "a" ? centerB : featureB
);
const equipmentWidth = wing.axis === "a" ? 2.5 : 1.5;
const equipmentDepth = wing.axis === "a" ? 1.5 : 2.5;
makeBox(
equipmentWidth,
0.85,
equipmentDepth,
featurePosition.x,
GROUND_LEVEL + height + 0.82,
featurePosition.z,
materials.roofEquipment
);
const chimneyPosition = point(
wing.axis === "a" ? featureB : centerA,
wing.axis === "a" ? centerB : featureA
);
makeBox(
0.7,
1.55,
0.7,
chimneyPosition.x,
GROUND_LEVEL + height + 1.13,
chimneyPosition.z,
trim
);
makeBox(
0.88,
0.18,
0.88,
chimneyPosition.x,
GROUND_LEVEL + height + 1.95,
chimneyPosition.z,
materials.roof
);
if (wingIndex >= 2) {
const gardenPosition = point(
wing.axis === "a" ? wing.start + 6 : centerA,
wing.axis === "a" ? centerB : wing.start + 6
);
makeBox(
wing.axis === "a" ? 2.4 : 1.2,
0.7,
wing.axis === "a" ? 1.2 : 2.4,
gardenPosition.x,
GROUND_LEVEL + height + 0.74,
gardenPosition.z,
standardMaterial(0x63805b)
);
}
}
// Four true diagonal corners. The central one forms the most legible
// octagonal junction; the other three complete the Eixample block.
const corners = [
{
p1: [CHAMFER, 0],
p2: [0, CHAMFER],
inward: [1, 1]
},
{
p1: [BLOCK_SIZE, CHAMFER],
p2: [BLOCK_SIZE - CHAMFER, 0],
inward: [-1, 1]
},
{
p1: [BLOCK_SIZE - CHAMFER, BLOCK_SIZE],
p2: [BLOCK_SIZE, BLOCK_SIZE - CHAMFER],
inward: [-1, -1]
},
{
p1: [0, BLOCK_SIZE - CHAMFER],
p2: [CHAMFER, BLOCK_SIZE],
inward: [1, -1]
}
];
corners.forEach((corner, cornerIndex) => {
const p1 = point(corner.p1[0], corner.p1[1]);
const p2 = point(corner.p2[0], corner.p2[1]);
const tangent = p2.clone().sub(p1).normalize();
const inward = new THREE.Vector3(
sx * corner.inward[0],
0,
sz * corner.inward[1]
).normalize();
const quaternion = quaternionFromBasis(tangent, up, inward);
const midpoint = p1.clone().add(p2).multiplyScalar(0.5);
const center = midpoint.clone().addScaledVector(inward, BUILDING_DEPTH / 2);
const length = p1.distanceTo(p2);
const height = (heights[cornerIndex] + heights[(cornerIndex + 1) % 4]) / 2;
makeBox(
length,
height,
BUILDING_DEPTH,
center.x,
GROUND_LEVEL + height / 2,
center.z,
facade
);
makeBox(
length + 0.12,
0.82,
BUILDING_DEPTH + 0.12,
center.x,
GROUND_LEVEL + 0.41,
center.z,
trim
);
makeBox(
length + 0.25,
0.22,
BUILDING_DEPTH + 0.25,
center.x,
GROUND_LEVEL + height + 0.11,
center.z,
trim
);
makeBox(
length + 0.08,
0.18,
BUILDING_DEPTH + 0.08,
center.x,
GROUND_LEVEL + height + 0.32,
center.z,
materials.roof
);
const outward = inward.clone().negate();
const floors = Math.max(2, Math.floor((height - 0.4) / 2.55));
for (let floor = 0; floor < floors; floor++) {
const y = GROUND_LEVEL + 1.75 + floor * 2.55;
for (const t of [0.27, 0.73]) {
const surface = p1
.clone()
.lerp(p2, t)
.addScaledVector(outward, 0.05);
surface.y = y;
addWindow(
surface,
outward,
palette.shutter,
floor > 0 && t > 0.5,
(floor + cornerIndex) % 4 === 0
);
}
}
});
// Courtyard paving, fountain and small tree court behind the facades.
const courtyardCenter = point(BLOCK_SIZE / 2, BLOCK_SIZE / 2);
const courtyardSize = BLOCK_SIZE - BUILDING_DEPTH * 2 + 0.6;
makeBox(
courtyardSize,
0.1,
courtyardSize,
courtyardCenter.x,
0.39,
courtyardCenter.z,
materials.courtyard,
false
);
const fountain = point(BLOCK_SIZE / 2, BLOCK_SIZE / 2);
makeBox(
3.5,
0.3,
3.5,
fountain.x,
0.53,
fountain.z,
trim
);
makeBox(
2.85,
0.09,
2.85,
fountain.x,
0.72,
fountain.z,
materials.water,
false
);
makeBox(
0.55,
1.05,
0.55,
fountain.x,
1.03,
fountain.z,
trim
);
const courtyardTrees = [
[13.5, 13.5],
[22.4, 14.1],
[14.2, 22.5]
];
for (const [a, b] of courtyardTrees) {
const treePoint = point(a, b, 0.44);
makeBox(
2.5,
0.32,
2.5,
treePoint.x,
0.58,
treePoint.z,
materials.planter
);
addTree(treePoint.x, treePoint.z, 0.72, 0.74);
}
// Street trees and lamps occupy the setback between road and facade.
for (let along = 6; along <= BLOCK_SIZE - 5; along += 8) {
const nearTree = point(along, -2.25, GROUND_LEVEL);
const sideTree = point(-2.25, along, GROUND_LEVEL);
addTree(nearTree.x, nearTree.z, 0.84, GROUND_LEVEL);
addTree(sideTree.x, sideTree.z, 0.84, GROUND_LEVEL);
}
for (const along of [10, 27]) {
const nearLamp = point(along, -3.35, GROUND_LEVEL);
const sideLamp = point(-3.35, along, GROUND_LEVEL);
addLamp(nearLamp.x, nearLamp.z);
addLamp(sideLamp.x, sideLamp.z);
}
// Four small traffic signals at the chamfered corners.
const signalPosition = point(-2.25, -2.25, GROUND_LEVEL);
makeBox(
0.22,
3.15,
0.22,
signalPosition.x,
GROUND_LEVEL + 1.58,
signalPosition.z,
materials.signal
);
makeBox(
0.62,
1.2,
0.5,
signalPosition.x,
GROUND_LEVEL + 3.05,
signalPosition.z,
materials.signal
);
makeBox(
0.25,
0.25,
0.1,
signalPosition.x,
GROUND_LEVEL + 3.35,
signalPosition.z + 0.27,
blockIndex % 2 === 0 ? materials.signalRed : materials.signalGreen
);
}
// -------------------------------------------------------------------------
// Voxel trees and lamps
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1, baseY = GROUND_LEVEL) {
const trunkPosition = new THREE.Vector3(
x,
baseY + 1.1 * scale,
z
);
addInstance(
treeTrunkInstances,
trunkPosition,
identityQuaternion,
new THREE.Vector3(0.43 * scale, 2.2 * scale, 0.43 * scale),
0x76513b
);
const clusters = [
{
offset: [0, 2.55, 0],
scale: [2.25, 1.8, 2.25],
color: 0x52784c
},
{
offset: [-0.72, 3.08, 0.2],
scale: [1.45, 1.45, 1.55],
color: 0x668b54
},
{
offset: [0.72, 3.12, -0.18],
scale: [1.5, 1.55, 1.45],
color: 0x769a5d
},
{
offset: [0.1, 3.66, 0.35],
scale: [1.45, 1.25, 1.45],
color: 0x89aa65
},
{
offset: [-0.18, 2.7, -0.78],
scale: [1.3, 1.35, 1.35],
color: 0x466f49
}
];
clusters.forEach((cluster, index) => {
addInstance(
treeLeafInstances,
new THREE.Vector3(
x + cluster.offset[0] * scale,
baseY + cluster.offset[1] * scale,
z + cluster.offset[2] * scale
),
identityQuaternion,
new THREE.Vector3(
cluster.scale[0] * scale,
cluster.scale[1] * scale,
cluster.scale[2] * scale
),
cluster.color
);
});
}
function addLamp(x, z) {
addInstance(
lampPostInstances,
new THREE.Vector3(x, GROUND_LEVEL + 1.65, z),
identityQuaternion,
new THREE.Vector3(0.18, 3.3, 0.18),
0x3b4c4f
);
addInstance(
lampHeadInstances,
new THREE.Vector3(x, GROUND_LEVEL + 3.38, z),
identityQuaternion,
new THREE.Vector3(0.58, 0.22, 0.58),
0xffdda1
);
}
makeBlock(1, 1, palettes[0], blockHeights[0], 0);
makeBlock(-1, 1, palettes[1], blockHeights[1], 1);
makeBlock(-1, -1, palettes[2], blockHeights[2], 2);
makeBlock(1, -1, palettes[3], blockHeights[3], 3);
// -------------------------------------------------------------------------
// Convert repeated details into instanced meshes
// -------------------------------------------------------------------------
function finalizeInstances(bucket, material, castShadow = false) {
if (!bucket.length) return;
const mesh = new THREE.InstancedMesh(unitBox, material, bucket.length);
bucket.forEach((item, index) => {
mesh.setMatrixAt(index, item.matrix);
mesh.setColorAt(index, item.color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
finalizeInstances(
frameInstances,
standardMaterial(0xffffff, { roughness: 0.8 })
);
finalizeInstances(
glassInstances,
standardMaterial(0xffffff, {
roughness: 0.24,
metalness: 0.12
})
);
finalizeInstances(
shutterInstances,
standardMaterial(0xffffff, { roughness: 0.86 })
);
finalizeInstances(
sillInstances,
standardMaterial(0xffffff, { roughness: 0.85 })
);
finalizeInstances(
balconySlabInstances,
standardMaterial(0xffffff, { roughness: 0.82 })
);
finalizeInstances(
balconyRailInstances,
standardMaterial(0xffffff, {
roughness: 0.5,
metalness: 0.18
})
);
finalizeInstances(
treeTrunkInstances,
standardMaterial(0xffffff),
true
);
finalizeInstances(
treeLeafInstances,
standardMaterial(0xffffff),
true
);
finalizeInstances(
lampPostInstances,
standardMaterial(0xffffff, {
roughness: 0.5,
metalness: 0.25
}),
true
);
finalizeInstances(
lampHeadInstances,
materials.lamp,
false
);
// -------------------------------------------------------------------------
// Restrained traffic: vehicles remain on the four approaches
// -------------------------------------------------------------------------
const cars = [];
function createCar(axis, lane, direction, phase, speed, color) {
const group = new THREE.Group();
const bodyMaterial = standardMaterial(color, {
roughness: 0.45,
metalness: 0.12
});
const glassMaterial = standardMaterial(0x28444d, {
roughness: 0.22,
metalness: 0.18
});
const wheelMaterial = standardMaterial(0x242a2b);
const alongX = axis === "x";
const body = new THREE.Mesh(unitBox, bodyMaterial);
body.scale.set(
alongX ? 3.6 : 1.55,
0.78,
alongX ? 1.55 : 3.6
);
body.position.y = 0.68;
body.castShadow = true;
body.receiveShadow = true;
group.add(body);
const cabin = new THREE.Mesh(unitBox, glassMaterial);
cabin.scale.set(
alongX ? 1.8 : 1.34,
0.72,
alongX ? 1.34 : 1.8
);
cabin.position.set(
alongX ? -0.18 : 0,
1.35,
alongX ? 0 : -0.18
);
cabin.castShadow = true;
group.add(cabin);
const roof = new THREE.Mesh(unitBox, bodyMaterial);
roof.scale.set(
alongX ? 1.5 : 1.22,
0.12,
alongX ? 1.22 : 1.5
);
roof.position.set(
alongX ? -0.18 : 0,
1.75,
alongX ? 0 : -0.18
);
roof.castShadow = true;
group.add(roof);
for (const longitudinal of [-1.12, 1.12]) {
for (const lateral of [-0.79, 0.79]) {
const wheel = new THREE.Mesh(unitBox, wheelMaterial);
wheel.scale.set(
alongX ? 0.68 : 0.32,
0.5,
alongX ? 0.32 : 0.68
);
wheel.position.set(
alongX ? longitudinal : lateral,
0.4,
alongX ? lateral : longitudinal
);
wheel.castShadow = true;
group.add(wheel);
}
}
const headlightMaterial = standardMaterial(0xffe4a8, {
emissive: 0x8d6926,
emissiveIntensity: 0.35
});
for (const lateral of [-0.47, 0.47]) {
const light = new THREE.Mesh(unitBox, headlightMaterial);
light.scale.set(0.16, 0.18, 0.12);
light.position.set(
alongX ? 1.82 : lateral,
0.72,
alongX ? lateral : 1.82
);
group.add(light);
}
if (axis === "x" && direction < 0) group.rotation.y = Math.PI;
if (axis === "z" && direction > 0) group.rotation.y = -Math.PI / 2;
if (axis === "z" && direction < 0) group.rotation.y = Math.PI / 2;
scene.add(group);
cars.push({
group,
axis,
lane,
direction,
phase,
speed,
min: 12,
max: 51
});
}
createCar("x", -2.45, 1, 3, 5.2, 0xd95d45);
createCar("x", 2.45, -1, 15, 4.6, 0xe2d4a9);
createCar("z", -2.45, 1, 8, 4.4, 0x557e83);
createCar("z", 2.45, -1, 22, 5.0, 0xc89a43);
createCar("x", -2.45, 1, 30, 4.8, 0xe7e0cf);
createCar("z", 2.45, -1, 12, 4.3, 0x6f8d72);
let trafficEnabled = true;
function updateTraffic(delta) {
for (const car of cars) {
car.phase = (car.phase + delta * car.speed) % (car.max - car.min);
const position =
car.direction > 0
? car.min + car.phase
: car.max - car.phase;
if (car.axis === "x") {
car.group.position.set(position, 0.2, car.lane);
} else {
car.group.position.set(car.lane, 0.2, position);
}
}
}
// -------------------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - pointerX;
const deltaY = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
controls.theta -= deltaX * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi - deltaY * 0.005,
0.34,
1.34
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
2.15
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") controls.theta -= 0.08;
if (event.key === "ArrowRight") controls.theta += 0.08;
if (event.key === "ArrowUp") controls.phi -= 0.06;
if (event.key === "ArrowDown") controls.phi += 0.06;
if (event.key === "+" || event.key === "=") controls.zoom *= 1.08;
if (event.key === "-") controls.zoom /= 1.08;
if (event.key.toLowerCase() === "r") {
controls.theta = Math.PI * 0.25;
controls.phi = 0.72;
controls.zoom = 1;
}
controls.phi = THREE.MathUtils.clamp(controls.phi, 0.34, 1.34);
controls.zoom = THREE.MathUtils.clamp(controls.zoom, 0.68, 2.15);
updateCamera();
});
document.getElementById("reset-view").addEventListener("click", () => {
controls.theta = Math.PI * 0.25;
controls.phi = 0.72;
controls.zoom = 1;
updateCamera();
});
const trafficButton = document.getElementById("toggle-traffic");
trafficButton.addEventListener("click", () => {
trafficEnabled = !trafficEnabled;
for (const car of cars) {
car.group.visible = trafficEnabled;
}
trafficButton.textContent = trafficEnabled
? "Pause traffic"
: "Resume traffic";
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (trafficEnabled) updateTraffic(delta);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```