Prompt specification
Build or customize the Three.js 3D scene "Eixample chamfered corner pavement tile" 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 chamfered corner pavement tile
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/eixample-chamfered-corner-pavement-tile-x2809
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows the chamfered intersections of Barcelona's Eixample. Build one referenced Barcelona chamfered corner with curb, crossing, and angled building edge. Show the diagonal cut compared with a faint square-corner guide. Voxel style.
Working Three.js Starter (eixample-chamfered-corner-pavement-tile-x2809):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Barcelona Eixample — Chamfered Corner</title>
<style>
:root {
--ink: #26383c;
--muted: #68777a;
--paper: #eee9de;
--accent: #e66a3c;
--guide: #7296ad;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title-panel {
position: absolute;
top: 28px;
left: 30px;
width: min(390px, calc(100vw - 60px));
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 10px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 30px;
height: 3px;
content: "";
background: var(--accent);
}
h1 {
margin: 0;
font-size: clamp(30px, 4.4vw, 58px);
font-weight: 780;
line-height: 0.94;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
max-width: 330px;
margin: 15px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.reference {
display: inline-flex;
align-items: center;
gap: 8px;
margin-top: 15px;
padding: 8px 10px;
border: 1px solid rgba(38, 56, 60, 0.15);
border-radius: 3px;
background: rgba(238, 233, 222, 0.78);
backdrop-filter: blur(8px);
color: var(--ink);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.reference span {
color: var(--accent);
font-size: 15px;
}
.legend {
position: absolute;
top: 28px;
right: 30px;
width: 220px;
padding: 15px 16px;
border: 1px solid rgba(38, 56, 60, 0.14);
border-radius: 4px;
background: rgba(238, 233, 222, 0.82);
box-shadow: 0 12px 35px rgba(49, 54, 49, 0.08);
backdrop-filter: blur(10px);
}
.legend-title {
margin-bottom: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 10px;
margin: 9px 0;
font-size: 11px;
font-weight: 650;
}
.swatch {
width: 24px;
height: 3px;
flex: 0 0 auto;
background: var(--accent);
}
.swatch.ghost {
height: 0;
border-top: 2px dashed var(--guide);
background: transparent;
}
.swatch.crossing {
width: 14px;
height: 14px;
background:
repeating-linear-gradient(
90deg,
#f8f4e9 0 3px,
transparent 3px 6px
);
border: 1px solid rgba(38, 56, 60, 0.15);
}
.pin {
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
transform: translate(-50%, -50%);
border: 1px solid rgba(38, 56, 60, 0.14);
border-radius: 3px;
background: rgba(246, 242, 233, 0.92);
box-shadow: 0 7px 24px rgba(41, 48, 46, 0.11);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
backdrop-filter: blur(7px);
}
.pin::before {
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
content: "";
background: var(--accent);
box-shadow: 0 0 0 4px rgba(230, 106, 60, 0.13);
}
.pin.ghost::before {
background: var(--guide);
box-shadow: 0 0 0 4px rgba(114, 150, 173, 0.13);
}
.pin.crossing::before {
background: #f8f4e9;
border: 1px solid var(--ink);
box-shadow: 0 0 0 4px rgba(38, 56, 60, 0.09);
}
.scale {
position: absolute;
right: 32px;
bottom: 30px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-align: right;
text-transform: uppercase;
}
.scale-line {
position: relative;
width: 100px;
height: 7px;
margin: 8px 0 5px auto;
border-right: 1px solid var(--ink);
border-bottom: 1px solid var(--ink);
border-left: 1px solid var(--ink);
}
.controls {
position: absolute;
bottom: 27px;
left: 30px;
color: rgba(38, 56, 60, 0.58);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.north {
position: absolute;
right: 37px;
bottom: 95px;
display: grid;
width: 40px;
height: 40px;
place-items: center;
border: 1px solid rgba(38, 56, 60, 0.2);
border-radius: 50%;
color: var(--muted);
font-size: 9px;
font-weight: 850;
}
.north::before {
position: absolute;
top: 5px;
width: 0;
height: 0;
content: "";
border-right: 4px solid transparent;
border-bottom: 8px solid var(--accent);
border-left: 4px solid transparent;
}
@media (max-width: 760px) {
.title-panel {
top: 20px;
left: 20px;
}
.subtitle {
display: none;
}
.reference {
margin-top: 12px;
font-size: 8px;
}
.legend {
top: auto;
right: 18px;
bottom: 65px;
width: 175px;
padding: 12px;
}
.controls {
bottom: 19px;
left: 20px;
font-size: 8px;
}
.scale,
.north {
display: none;
}
.pin {
padding: 6px 8px;
font-size: 8px;
}
}
</style>
</head>
<body>
<div class="hud">
<section class="title-panel">
<div class="eyebrow">Urban morphology / 01</div>
<h1>The 45°<br />Corner</h1>
<p class="subtitle">
Barcelona’s Eixample replaces a rigid square block corner with a
diagonal building edge, enlarged curb and generous civic crossing.
</p>
<div class="reference">
<span>◈</span> Barcelona · Eixample · Reference corner
</div>
</section>
<aside class="legend">
<div class="legend-title">Comparison key</div>
<div class="legend-row"><i class="swatch"></i> Diagonal chamfer</div>
<div class="legend-row"><i class="swatch ghost"></i> Square-corner guide</div>
<div class="legend-row"><i class="swatch crossing"></i> Pedestrian crossing</div>
</aside>
<div id="label-chamfer" class="pin">45° angled edge</div>
<div id="label-ghost" class="pin ghost">Square corner ghost</div>
<div id="label-crossing" class="pin crossing">Curb + crossing</div>
<div class="north">N</div>
<div class="scale">
Schematic street geometry
<div class="scale-line"></div>
Not to scale
</div>
<div class="controls">Drag to orbit · Scroll to zoom · Double-click to reset</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, scene and orthographic camera
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xeee9de);
scene.fog = new THREE.Fog(0xeee9de, 105, 175);
const camera = new THREE.OrthographicCamera(-50, 50, 30, -30, 0.1, 250);
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;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xfff6df, 0x73818a, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffefd2, 3.15);
sun.position.set(-36, 62, -28);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -70;
sun.shadow.camera.right = 70;
sun.shadow.camera.top = 70;
sun.shadow.camera.bottom = -70;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 150;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d5df, 0.7);
fill.position.set(45, 28, 40);
scene.add(fill);
// -------------------------------------------------------------------------
// Materials
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...options,
});
const M = {
ground: material(0xddd7c9),
road: material(0x45585a),
roadAlt: material(0x4b5d5f),
sidewalk: material(0xd9cdb8),
plaza: material(0xe0d3bd),
curb: material(0xf0e8d8),
curbSide: material(0xb9ad99),
building: material(0xd6b58e),
buildingDark: material(0xc69b76),
buildingLight: material(0xe2c6a2),
roof: material(0xb88766),
window: material(0x304b52, { roughness: 0.42 }),
windowLight: material(0x638387, { roughness: 0.4 }),
frame: material(0xefe3cf),
accent: material(0xe66a3c),
dark: material(0x28383c),
rubber: material(0x20282a),
white: material(0xf8f3e8),
yellow: material(0xe8bc55),
context: material(0xc9c0af),
contextRoof: material(0xaaa18f),
tree: material(0x718d64),
treeLight: material(0x8ca275),
trunk: material(0x7b604b),
skin: material(0xd4a077),
red: material(0xb84e3e),
blueCar: material(0x607e88),
};
// -------------------------------------------------------------------------
// Geometry helpers
// -------------------------------------------------------------------------
function addBox(
width,
height,
depth,
x,
y,
z,
mat,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
if (options.rotationY) mesh.rotation.y = options.rotationY;
if (options.rotationX) mesh.rotation.x = options.rotationX;
if (options.rotationZ) mesh.rotation.z = options.rotationZ;
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.parent) options.parent.add(mesh);
else scene.add(mesh);
return mesh;
}
function addLine(points, color, opacity = 1, dashed = false) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((p) => new THREE.Vector3(...p))
);
const lineMaterial = dashed
? new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize: 0.7,
gapSize: 0.45,
depthWrite: false,
})
: new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity,
depthWrite: false,
});
const line = new THREE.Line(geometry, lineMaterial);
if (dashed) line.computeLineDistances();
scene.add(line);
return line;
}
function prism(points, height, mat, y = 0, outline = false) {
const shape = new THREE.Shape();
points.forEach(([x, z], index) => {
if (index === 0) shape.moveTo(x, -z);
else shape.lineTo(x, -z);
});
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
bevelEnabled: false,
curveSegments: 1,
});
geometry.rotateX(-Math.PI / 2);
const mesh = new THREE.Mesh(geometry, mat);
mesh.position.y = y;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
if (outline) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry, 25),
new THREE.LineBasicMaterial({
color: 0x775e4b,
transparent: true,
opacity: 0.2,
})
);
edge.position.copy(mesh.position);
scene.add(edge);
}
return mesh;
}
// -------------------------------------------------------------------------
// Plan geometry
// -------------------------------------------------------------------------
const ROAD_HALF = 10.5;
const BLOCK_END = 41;
const CHAMFER = 10.5;
// Square corner and its two diagonal replacement endpoints.
const S = new THREE.Vector2(ROAD_HALF, ROAD_HALF);
const A = new THREE.Vector2(ROAD_HALF + CHAMFER, ROAD_HALF);
const B = new THREE.Vector2(ROAD_HALF, ROAD_HALF + CHAMFER);
const blockOutline = [
[A.x, A.y],
[BLOCK_END, A.y],
[BLOCK_END, BLOCK_END],
[B.x, BLOCK_END],
[B.x, B.y],
];
const plazaTriangle = [
[S.x, S.y],
[A.x, A.y],
[B.x, B.y],
];
// Ground and orthogonal Eixample road grid.
addBox(
112,
0.35,
112,
0,
-0.27,
0,
M.ground,
{ castShadow: false }
);
addBox(
108,
0.1,
ROAD_HALF * 2,
0,
-0.035,
0,
M.road,
{ castShadow: false }
);
addBox(
ROAD_HALF * 2,
0.1,
108,
0,
-0.025,
0,
M.road,
{ castShadow: false }
);
// Main block pavement and building mass.
prism(blockOutline, 0.17, M.sidewalk, 0.025, false);
prism(plazaTriangle, 0.18, M.plaza, 0.025, false);
prism(blockOutline, 24, M.building, 0.19, true);
// A slightly raised roof plate.
prism(blockOutline, 0.48, M.roof, 24.19, false);
// Faint overlay identifying the volume removed by the chamfer.
const removedGeometry = new THREE.BufferGeometry();
removedGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
S.x, 0.195, S.y,
A.x, 0.195, A.y,
B.x, 0.195, B.y,
],
3
)
);
removedGeometry.computeVertexNormals();
const removedOverlay = new THREE.Mesh(
removedGeometry,
new THREE.MeshBasicMaterial({
color: 0xe66a3c,
transparent: true,
opacity: 0.11,
side: THREE.DoubleSide,
depthWrite: false,
})
);
scene.add(removedOverlay);
// -------------------------------------------------------------------------
// Faint square-corner comparison guide
// -------------------------------------------------------------------------
addLine(
[
[A.x, 0.24, A.y],
[S.x, 0.24, S.y],
[B.x, 0.24, B.y],
],
0x7296ad,
0.38,
true
);
addLine(
[
[S.x, 0.24, S.y],
[S.x, 24.67, S.y],
],
0x7296ad,
0.28,
true
);
addLine(
[
[A.x, 24.67, A.y],
[S.x, 24.67, S.y],
[B.x, 24.67, B.y],
],
0x7296ad,
0.2,
true
);
// -------------------------------------------------------------------------
// Curbs
// -------------------------------------------------------------------------
// Diagonal curb: repeated short voxel-like blocks.
const diagonalLength = A.distanceTo(B);
const curbBlockLength = 1.23;
const curbCount = Math.floor(diagonalLength / curbBlockLength);
for (let i = 0; i < curbCount; i++) {
const t = (i + 0.5) / curbCount;
const x = THREE.MathUtils.lerp(A.x, B.x, t);
const z = THREE.MathUtils.lerp(A.y, B.y, t);
addBox(
curbBlockLength,
0.42,
0.72,
x,
0.39,
z,
M.curb,
{ rotationY: -Math.PI / 4 }
);
}
// Straight curbs continuing from the chamfer endpoints.
for (let x = A.x + 0.55; x < BLOCK_END - 0.4; x += 1.24) {
addBox(1.15, 0.42, 0.72, x, 0.39, ROAD_HALF, M.curb);
}
for (let z = B.y + 0.55; z < BLOCK_END - 0.4; z += 1.24) {
addBox(0.72, 0.42, 1.15, ROAD_HALF, 0.39, z, M.curb);
}
// Outer edge curbs, establishing the complete block.
for (let x = A.x + 0.6; x < BLOCK_END; x += 1.35) {
addBox(1.22, 0.42, 0.65, x, 0.39, BLOCK_END, M.curb);
}
for (let z = B.y + 0.6; z < BLOCK_END; z += 1.35) {
addBox(0.65, 0.42, 1.22, BLOCK_END, 0.39, z, M.curb);
}
// Bright accent confirms the diagonal replacement edge.
addBox(
diagonalLength,
0.13,
0.18,
(A.x + B.x) / 2,
0.66,
(A.y + B.y) / 2,
M.accent,
{ rotationY: -Math.PI / 4, castShadow: false }
);
// -------------------------------------------------------------------------
// Facades and voxel windows
// -------------------------------------------------------------------------
function addWindow(group, x, y, width = 1.35, height = 2.05) {
addBox(
width + 0.22,
height + 0.22,
0.13,
x,
y,
0.12,
M.frame,
{ parent: group }
);
addBox(
width,
height,
0.16,
x,
y,
0.22,
(x + y) % 2 > 0.5 ? M.window : M.windowLight,
{ parent: group }
);
addBox(
0.1,
height,
0.19,
x,
y,
0.32,
M.frame,
{ parent: group }
);
addBox(
width,
0.1,
0.19,
x,
y + 0.1,
0.32,
M.frame,
{ parent: group }
);
}
function addBalcony(group, x, y, width = 1.35) {
addBox(
width + 0.75,
0.2,
1.05,
x,
y - 1.2,
0.55,
M.buildingLight,
{ parent: group }
);
addBox(
width + 0.75,
0.12,
0.12,
x,
y - 0.48,
1.0,
M.dark,
{ parent: group }
);
for (const dx of [-0.62, 0.62]) {
addBox(
0.1,
0.75,
0.1,
x + dx,
y - 0.86,
1.0,
M.dark,
{ parent: group }
);
}
}
function addFacadeDetails(group, length, columns, floors, balcony = false) {
const floorYs = [3.35, 7.35, 11.35, 15.35, 19.35];
floorYs.slice(0, floors).forEach((fy, floorIndex) => {
for (let col = 0; col < columns; col++) {
const x =
-length / 2 +
((col + 0.5) * length) / columns;
addWindow(group, x, fy);
if (balcony) addBalcony(group, x, fy);
}
addBox(
length,
0.15,
0.3,
0,
fy + 1.65,
0.18,
M.buildingLight,
{ parent: group }
);
});
}
// South facade.
const southFacade = new THREE.Group();
southFacade.position.set(30.5, 0.19, ROAD_HALF);
southFacade.rotation.y = Math.PI;
scene.add(southFacade);
addFacadeDetails(southFacade, 18.8, 6, 5, true);
// West facade.
const westFacade = new THREE.Group();
westFacade.position.set(ROAD_HALF, 0.19, 30.5);
westFacade.rotation.y = -Math.PI / 2;
scene.add(westFacade);
addFacadeDetails(westFacade, 18.8, 6, 5, true);
// Angled Eixample facade.
const chamferFacade = new THREE.Group();
chamferFacade.position.set(
(A.x + B.x) / 2,
0.19,
(A.y + B.y) / 2
);
chamferFacade.rotation.y = -3 * Math.PI / 4;
scene.add(chamferFacade);
addFacadeDetails(chamferFacade, diagonalLength - 0.65, 5, 5, true);
// Ground-floor shops on the angled edge.
for (let i = 0; i < 4; i++) {
const x = -5.1 + i * 3.4;
addBox(
2.55,
2.3,
0.16,
x,
1.35,
0.18,
M.window,
{ parent: chamferFacade }
);
addBox(
2.72,
0.16,
0.21,
x,
1.35,
0.3,
M.frame,
{ parent: chamferFacade }
);
addBox(
2.9,
0.22,
1.55,
x,
2.65,
0.68,
i % 2 ? M.accent : M.buildingDark,
{ parent: chamferFacade }
);
}
// Projecting cornice on the chamfer.
addBox(
diagonalLength + 0.35,
0.65,
0.95,
0,
23.25,
0.25,
M.buildingLight,
{ parent: chamferFacade }
);
// Straight cornices.
addBox(
19.25,
0.55,
0.72,
30.5,
23.35,
10.28,
M.buildingLight
);
addBox(
0.72,
0.55,
19.25,
10.28,
23.35,
30.5,
M.buildingLight
);
// -------------------------------------------------------------------------
// Rooftop voxel details
// -------------------------------------------------------------------------
addBox(5.6, 1.45, 4.8, 31.7, 25.15, 33.6, M.buildingDark);
addBox(3.4, 0.45, 2.8, 23.5, 24.7, 18.7, M.buildingLight);
addBox(2.1, 1.1, 2.1, 36.4, 24.95, 25.2, M.roof);
addBox(1.25, 2.2, 1.25, 27.2, 25.55, 35.7, M.buildingLight);
// Small rooftop garden planters.
for (const [x, z] of [
[17.2, 34.5],
[21.1, 35.2],
[24.6, 34.1],
]) {
addBox(2.2, 0.55, 1.8, x, 24.75, z, M.contextRoof);
addBox(1.8, 0.9, 1.45, x, 25.45, z, M.tree);
}
// -------------------------------------------------------------------------
// Crossings
// -------------------------------------------------------------------------
// Crossing over the north-south avenue.
for (let x = -9.35; x <= 9.4; x += 1.55) {
addBox(
0.95,
0.055,
7.0,
x,
0.065,
16.1,
M.white,
{ castShadow: false }
);
}
// Crossing over the east-west avenue.
for (let z = -9.35; z <= 9.4; z += 1.55) {
addBox(
7.0,
0.055,
0.95,
16.1,
0.07,
z,
M.white,
{ castShadow: false }
);
}
// Low curb ramps represented with shallow stepped voxels.
addBox(1.3, 0.16, 2.1, 10.3, 0.17, 16.1, M.curb);
addBox(1.3, 0.16, 2.1, 16.1, 0.17, 10.3, M.curb);
// -------------------------------------------------------------------------
// Road markings
// -------------------------------------------------------------------------
for (let z = -46; z <= 46; z += 4.8) {
if (Math.abs(z) > 11 && Math.abs(z - 16.1) > 4.4) {
addBox(
0.16,
0.035,
2.15,
0,
0.065,
z,
M.yellow,
{ castShadow: false }
);
}
}
for (let x = -46; x <= 46; x += 4.8) {
if (Math.abs(x) > 11 && Math.abs(x - 16.1) > 4.4) {
addBox(
2.15,
0.035,
0.16,
x,
0.065,
0,
M.yellow,
{ castShadow: false }
);
}
}
// -------------------------------------------------------------------------
// Context blocks, intentionally quiet and low-detail
// -------------------------------------------------------------------------
addBox(
29.5,
0.18,
29.5,
-25.75,
0.1,
25.75,
M.sidewalk,
{ castShadow: false }
);
addBox(
29.5,
0.18,
29.5,
25.75,
0.1,
-25.75,
M.sidewalk,
{ castShadow: false }
);
addBox(
29.2,
8.6,
29.2,
-25.75,
4.45,
25.75,
M.context
);
addBox(
29.2,
8.6,
29.2,
25.75,
4.45,
-25.75,
M.context
);
addBox(
29.5,
0.45,
29.5,
-25.75,
8.95,
25.75,
M.contextRoof
);
addBox(
29.5,
0.45,
29.5,
25.75,
8.95,
-25.75,
M.contextRoof
);
// -------------------------------------------------------------------------
// Voxel street furniture
// -------------------------------------------------------------------------
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.18, z);
group.scale.setScalar(scale);
scene.add(group);
addBox(0.46, 2.5, 0.46, 0, 1.25, 0, M.trunk, { parent: group });
addBox(2.5, 2.2, 2.5, 0, 3.2, 0, M.tree, { parent: group });
addBox(1.8, 1.8, 1.8, -0.55, 4.25, 0.25, M.treeLight, {
parent: group,
});
addBox(1.4, 1.4, 1.4, 0.75, 4.15, -0.45, M.tree, {
parent: group,
});
addBox(2.7, 0.35, 2.7, 0, 0.1, 0, M.curbSide, { parent: group });
}
function addLamp(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.18, z);
group.rotation.y = rotation;
scene.add(group);
addBox(0.16, 5.2, 0.16, 0, 2.6, 0, M.dark, { parent: group });
addBox(1.5, 0.14, 0.14, 0.7, 5.1, 0, M.dark, { parent: group });
addBox(0.72, 0.2, 0.42, 1.25, 4.98, 0, M.windowLight, {
parent: group,
});
}
function addPerson(x, z, color, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.18, z);
group.rotation.y = rotation;
scene.add(group);
const clothing = material(color);
addBox(0.45, 1.05, 0.35, 0, 1.03, 0, clothing, { parent: group });
addBox(0.34, 0.34, 0.34, 0, 1.75, 0, M.skin, { parent: group });
addBox(0.17, 0.55, 0.19, -0.13, 0.3, 0, M.dark, {
parent: group,
});
addBox(0.17, 0.55, 0.19, 0.13, 0.3, 0, M.dark, {
parent: group,
});
addBox(0.14, 0.75, 0.16, -0.31, 1.0, 0, clothing, {
parent: group,
});
addBox(0.14, 0.75, 0.16, 0.31, 1.0, 0, clothing, {
parent: group,
});
}
function addCar(x, z, color, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.08, z);
group.rotation.y = rotation;
scene.add(group);
const bodyMaterial = material(color);
addBox(2.1, 0.75, 4.4, 0, 0.65, 0, bodyMaterial, { parent: group });
addBox(1.7, 0.65, 2.2, 0, 1.25, -0.15, M.windowLight, {
parent: group,
});
addBox(2.15, 0.22, 4.45, 0, 0.35, 0, M.dark, { parent: group });
for (const sx of [-1.06, 1.06]) {
for (const sz of [-1.45, 1.45]) {
addBox(0.34, 0.62, 0.72, sx, 0.35, sz, M.rubber, {
parent: group,
});
}
}
addBox(0.65, 0.22, 0.08, -0.62, 0.72, -2.23, M.white, {
parent: group,
});
addBox(0.65, 0.22, 0.08, 0.62, 0.72, -2.23, M.white, {
parent: group,
});
}
addTree(-12.1, 25.5, 0.95);
addTree(-12.1, 31.0, 1.05);
addTree(25.2, -12.1, 1.0);
addTree(31.5, -12.1, 0.9);
addLamp(-11.8, 18.7, Math.PI / 2);
addLamp(18.7, -11.8, Math.PI);
addCar(-4.6, -24, M.red, 0);
addCar(4.7, 29, M.blueCar, Math.PI);
addCar(-23, 3.8, M.buildingLight, Math.PI / 2);
addCar(24, -4.3, M.white, -Math.PI / 2);
addPerson(9.3, 14.8, M.red, -0.6);
addPerson(11.1, 15.6, M.blueCar, 0.5);
addPerson(14.7, 13.8, M.buildingDark, -1.0);
addPerson(15.7, 10.8, M.tree, 0.3);
addPerson(-9.9, 16.0, M.windowLight, 1.2);
// -------------------------------------------------------------------------
// Camera controls implemented directly without add-ons
// -------------------------------------------------------------------------
const target = new THREE.Vector3(13.5, 7.2, 13.5);
const cameraState = {
theta: Math.PI * 1.25,
phi: THREE.MathUtils.degToRad(58),
radius: 76,
goalTheta: Math.PI * 1.25,
goalPhi: THREE.MathUtils.degToRad(58),
goalRadius: 76,
};
const initialCamera = { ...cameraState };
function updateCamera() {
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraState.goalTheta,
0.1
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraState.goalPhi,
0.1
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraState.goalRadius,
0.1
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
target.x + cameraState.radius * sinPhi * Math.cos(cameraState.theta),
target.y + cameraState.radius * Math.cos(cameraState.phi),
target.z + cameraState.radius * sinPhi * Math.sin(cameraState.theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
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 dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.goalTheta -= dx * 0.006;
cameraState.goalPhi = THREE.MathUtils.clamp(
cameraState.goalPhi + dy * 0.005,
THREE.MathUtils.degToRad(28),
THREE.MathUtils.degToRad(78)
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.goalRadius = THREE.MathUtils.clamp(
cameraState.goalRadius * Math.exp(event.deltaY * 0.001),
35,
115
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.goalTheta = initialCamera.goalTheta;
cameraState.goalPhi = initialCamera.goalPhi;
cameraState.goalRadius = initialCamera.goalRadius;
});
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") cameraState.goalTheta -= 0.12;
if (event.key === "ArrowRight") cameraState.goalTheta += 0.12;
if (event.key === "ArrowUp") cameraState.goalPhi -= 0.06;
if (event.key === "ArrowDown") cameraState.goalPhi += 0.06;
if (event.key === "+" || event.key === "=") {
cameraState.goalRadius = Math.max(35, cameraState.goalRadius - 3);
}
if (event.key === "-") {
cameraState.goalRadius = Math.min(115, cameraState.goalRadius + 3);
}
});
// -------------------------------------------------------------------------
// Responsive projection and annotation projection
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = aspect < 1 ? 76 : 58;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
const labels = [
{
element: document.getElementById("label-chamfer"),
position: new THREE.Vector3(15.75, 14.2, 15.75),
},
{
element: document.getElementById("label-ghost"),
position: new THREE.Vector3(7.7, 10.5, 7.7),
},
{
element: document.getElementById("label-crossing"),
position: new THREE.Vector3(0.7, 1.0, 16.1),
},
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.2 &&
Math.abs(projected.y) < 1.2;
label.element.style.display = visible ? "flex" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```