Prompt specification
Build or customize the Three.js 3D scene "Pedestrian crossing into Sorocaba's central square" 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: Pedestrian crossing into Sorocaba's central square
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pedestrian-crossing-into-sorocaba-s-central-square-x2718
Scene Description Prompt:
A voxel pedestrian crosses into Praça Coronel Fernando Prestes in Sorocaba along a continuous clear route. Three.js from cdnjs, one HTML file.
Working Three.js Starter (pedestrian-crossing-into-sorocaba-s-central-square-x2718):
```html
<!doctype html>
<html lang="pt-BR">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#17343a">
<title>Praça Coronel Fernando Prestes — Sorocaba</title>
<style>
:root {
--ink: #17343a;
--paper: #f5f0e5;
--muted: #6b7773;
--gold: #e6ad58;
--green: #2f705d;
--line: rgba(23, 52, 58, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bdd3d3;
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%;
touch-action: none;
outline: none;
}
.ui {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: rgba(245, 240, 229, 0.9);
border: 1px solid rgba(255, 255, 255, 0.58);
box-shadow: 0 18px 50px rgba(30, 50, 49, 0.16);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 20px 22px 18px;
border-radius: 4px 22px 4px 4px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 11px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 2px;
background: var(--gold);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 39px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.title-card p {
max-width: 310px;
margin: 13px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.live-badge {
position: absolute;
top: 24px;
right: 24px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 14px;
border-radius: 999px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #3f9475;
box-shadow: 0 0 0 4px rgba(63, 148, 117, 0.14);
}
.route-card {
position: absolute;
right: 24px;
bottom: 24px;
width: 270px;
padding: 16px 17px 15px;
border-radius: 18px 4px 18px 18px;
}
.route-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 8px;
}
.route-head strong {
font-size: 10px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
#route-status {
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.06em;
text-transform: uppercase;
text-align: right;
}
.progress-track {
height: 5px;
overflow: hidden;
border-radius: 10px;
background: rgba(23, 52, 58, 0.1);
}
#progress {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--gold), var(--green));
transition: width 80ms linear;
}
.route-meta {
display: flex;
justify-content: space-between;
margin-top: 9px;
color: var(--muted);
font-size: 9px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.controls {
position: absolute;
bottom: 24px;
left: 50%;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 999px;
transform: translateX(-50%);
}
button {
min-height: 36px;
padding: 0 16px;
border: 0;
border-radius: 999px;
background: transparent;
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: background 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(23, 52, 58, 0.08);
}
button:active {
transform: scale(0.97);
}
button.primary {
min-width: 96px;
background: var(--ink);
color: var(--paper);
}
button.primary:hover {
background: #254a51;
}
.hint {
position: absolute;
left: 24px;
bottom: 24px;
max-width: 230px;
padding: 12px 15px;
border: 1px solid rgba(255, 255, 255, 0.45);
border-radius: 4px 14px 14px 14px;
background: rgba(23, 52, 58, 0.84);
color: rgba(255, 255, 255, 0.9);
font-size: 10px;
line-height: 1.5;
}
.hint strong {
color: #f3c77f;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.compass {
position: absolute;
top: 82px;
right: 30px;
width: 38px;
height: 38px;
border: 1px solid rgba(23, 52, 58, 0.25);
border-radius: 50%;
color: var(--ink);
font-size: 9px;
font-weight: 900;
text-align: center;
line-height: 38px;
}
.compass::after {
position: absolute;
top: 5px;
left: 17px;
width: 0;
height: 0;
border-right: 2px solid transparent;
border-bottom: 7px solid var(--gold);
border-left: 2px solid transparent;
content: "";
}
@media (max-width: 820px) {
.title-card {
top: 12px;
left: 12px;
width: min(330px, calc(100vw - 24px));
padding: 15px 17px;
}
.title-card p,
.hint,
.compass {
display: none;
}
.live-badge {
top: auto;
right: 12px;
bottom: 128px;
}
.route-card {
right: 12px;
bottom: 66px;
width: 238px;
padding: 12px 14px;
}
.controls {
bottom: 12px;
}
button {
padding: 0 12px;
}
}
</style>
</head>
<body>
<div class="ui">
<header class="title-card panel">
<div class="eyebrow">Sorocaba · São Paulo</div>
<h1>Praça Coronel<br>Fernando Prestes</h1>
<p>
A voxel pedestrian follows one continuous, unobstructed axis from the
avenue into the civic square.
</p>
</header>
<div class="live-badge panel">
<span class="live-dot"></span>
<span id="live-label">Route in motion</span>
</div>
<div class="compass" aria-hidden="true">N</div>
<aside class="route-card panel">
<div class="route-head">
<strong>Pedestrian axis</strong>
<span id="route-status" aria-live="polite">Crossing avenue</span>
</div>
<div class="progress-track"><div id="progress"></div></div>
<div class="route-meta">
<span>2.8 m clear route</span>
<span id="progress-label">0%</span>
</div>
</aside>
<nav class="controls panel" aria-label="Animation and camera controls">
<button id="play" class="primary" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<button id="camera-reset" type="button">Reset view</button>
</nav>
<div class="hint">
<strong>Camera</strong><br>
Drag to orbit · Shift/right-drag to pan · Scroll to zoom · Double-click to reset
</div>
</div>
<noscript>This scene requires JavaScript and WebGL.</noscript>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xbdd3d3);
scene.fog = new THREE.Fog(0xbdd3d3, 64, 125);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
180
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel view of Praça Coronel Fernando Prestes in Sorocaba"
);
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xe8f5f4, 0x686956, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffeed0, 3.1);
sun.position.set(-24, 38, 22);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -48;
sun.shadow.camera.right = 48;
sun.shadow.camera.top = 42;
sun.shadow.camera.bottom = -42;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9cc9d6, 0.55);
fill.position.set(30, 18, -28);
scene.add(fill);
const materialCache = new Map();
function standardMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0
})
);
}
return materialCache.get(color);
}
function addBox(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
options = {}
) {
const geometry = new THREE.BoxGeometry(width, height, depth);
const material = options.material || standardMaterial(color);
const mesh = new THREE.Mesh(geometry, material);
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;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Procedural pavement texture
// ---------------------------------------------------------------------
function createPavingTexture() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#ded8c7";
ctx.fillRect(0, 0, 256, 256);
for (let i = 0; i < 260; i++) {
const x = Math.random() * 256;
const y = Math.random() * 256;
ctx.fillStyle = `rgba(66, 61, 48, ${Math.random() * 0.035})`;
ctx.fillRect(x, y, 1.5, 1.5);
}
ctx.strokeStyle = "rgba(73, 72, 60, 0.22)";
ctx.lineWidth = 2;
for (let p = 0; p <= 256; p += 64) {
ctx.beginPath();
ctx.moveTo(p, 0);
ctx.lineTo(p, 256);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, p);
ctx.lineTo(256, p);
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(9, 9);
texture.anisotropy = Math.min(
8,
renderer.capabilities.getMaxAnisotropy()
);
return texture;
}
const pavingTexture = createPavingTexture();
// ---------------------------------------------------------------------
// Ground, streets and sidewalks
// ---------------------------------------------------------------------
addBox(
scene,
180,
0.3,
180,
0xaabcb3,
0,
-1.18,
0,
{ castShadow: false }
);
addBox(scene, 78, 1.1, 64, 0x9b9785, 0, -0.57, 1);
// Horizontal avenue.
addBox(scene, 76, 0.12, 12, 0x4d5959, 0, 0.02, 18);
addBox(scene, 76, 0.18, 6, 0xc8c4b6, 0, 0.08, 9);
addBox(scene, 76, 0.18, 5, 0xc8c4b6, 0, 0.08, 26.5);
// Vertical street and its sidewalks.
addBox(scene, 9, 0.12, 40, 0x4d5959, -27, 0.025, -9);
addBox(scene, 5, 0.18, 40, 0xc8c4b6, -34, 0.08, -9);
addBox(scene, 5, 0.18, 40, 0xc8c4b6, -20, 0.08, -9);
// Plaza slab.
addBox(scene, 28, 0.22, 29, 0xa9a694, 18, 0.08, -5.5);
const plazaSurface = new THREE.Mesh(
new THREE.PlaneGeometry(27, 28),
new THREE.MeshStandardMaterial({
map: pavingTexture,
color: 0xffffff,
roughness: 0.95
})
);
plazaSurface.rotation.x = -Math.PI / 2;
plazaSurface.position.set(18, 0.2, -5.5);
plazaSurface.receiveShadow = true;
scene.add(plazaSurface);
// Plaza edging.
addBox(scene, 28.4, 0.28, 0.45, 0xb5ad99, 18, 0.16, 8.72);
addBox(scene, 28.4, 0.28, 0.45, 0xb5ad99, 18, 0.16, -19.72);
addBox(scene, 0.45, 0.28, 29, 0xb5ad99, 3.8, 0.16, -5.5);
addBox(scene, 0.45, 0.28, 29, 0xb5ad99, 32.2, 0.16, -5.5);
// Avenue lane markings.
for (let x = -34; x <= 34; x += 6) {
if (x > -9 && x < -1) continue;
addBox(scene, 2.8, 0.025, 0.16, 0xe9e2ca, x, 0.1, 18, {
castShadow: false
});
}
// Zebra crossing, aligned with the pedestrian route.
for (let z = 12.8; z <= 23.6; z += 1.35) {
addBox(scene, 4.8, 0.025, 0.68, 0xf1edda, -5, 0.21, z, {
castShadow: false
});
}
// ---------------------------------------------------------------------
// Continuous pedestrian route
// ---------------------------------------------------------------------
const routePoints = [
new THREE.Vector3(-5, 0.235, 27),
new THREE.Vector3(-5, 0.235, 10.8),
new THREE.Vector3(-4, 0.235, 9.2),
new THREE.Vector3(-1, 0.235, 8.3),
new THREE.Vector3(5, 0.235, 7.2),
new THREE.Vector3(10, 0.235, 4.2),
new THREE.Vector3(14, 0.235, 0.4),
new THREE.Vector3(18, 0.235, -3)
];
const routeCurve = new THREE.CatmullRomCurve3(
routePoints,
false,
"catmullrom",
0.35
);
function createRouteRibbon(curve, width, height) {
const segments = 180;
const positions = [];
const uvs = [];
const indices = [];
const leftEdge = [];
const rightEdge = [];
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const point = curve.getPointAt(t);
const before = curve.getPointAt(Math.max(0, t - 0.002));
const after = curve.getPointAt(Math.min(1, t + 0.002));
const tangent = after.clone().sub(before).normalize();
const side = new THREE.Vector3(-tangent.z, 0, tangent.x);
const left = point.clone().addScaledVector(side, width / 2);
const right = point.clone().addScaledVector(side, -width / 2);
positions.push(left.x, height, left.z);
positions.push(right.x, height, right.z);
uvs.push(t, 0, t, 1);
leftEdge.push(new THREE.Vector3(left.x, height + 0.015, left.z));
rightEdge.push(new THREE.Vector3(right.x, height + 0.015, right.z));
if (i < segments) {
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.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
color: 0xdca554,
roughness: 0.9,
transparent: true,
opacity: 0.76,
side: THREE.DoubleSide,
depthWrite: false,
polygonOffset: true,
polygonOffsetFactor: -1
});
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x8a6335,
transparent: true,
opacity: 0.7
});
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(leftEdge),
edgeMaterial
)
);
scene.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(rightEdge),
edgeMaterial
)
);
}
createRouteRibbon(routeCurve, 2.8, 0.225);
// Dashed centerline makes the route legible from start to finish.
const routeLength = routeCurve.getLength();
const dashMaterial = new THREE.MeshStandardMaterial({
color: 0xfff1c7,
emissive: 0x8e6427,
emissiveIntensity: 0.22,
roughness: 0.75
});
for (let d = 0.35; d < routeLength; d += 1.05) {
const t = d / routeLength;
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
const dash = addBox(
scene,
0.15,
0.035,
0.52,
0xffffff,
point.x,
0.25,
point.z,
{
material: dashMaterial,
castShadow: false
}
);
dash.rotation.y = Math.atan2(tangent.x, tangent.z);
}
// Directional chevrons on the pedestrian route.
function addRouteChevron(distance) {
const t = distance / routeLength;
const point = routeCurve.getPointAt(t);
const tangent = routeCurve.getTangentAt(t);
const group = new THREE.Group();
group.position.set(point.x, 0.285, point.z);
group.rotation.y = Math.atan2(tangent.x, tangent.z);
addBox(group, 0.9, 0.035, 0.13, 0xfff0c3, -0.22, 0, 0, {
rotationY: -0.58,
castShadow: false
});
addBox(group, 0.9, 0.035, 0.13, 0xfff0c3, 0.22, 0, 0, {
rotationY: 0.58,
castShadow: false
});
scene.add(group);
}
[4.2, 12, 20, 28, 35].forEach(addRouteChevron);
// ---------------------------------------------------------------------
// Voxel environmental helpers
// ---------------------------------------------------------------------
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
addBox(group, 0.48, 2.6, 0.48, 0x755641, 0, 1.3, 0);
addBox(group, 2.6, 1.35, 2.35, 0x376c4d, 0, 2.85, 0);
addBox(group, 2.05, 1.15, 2.0, 0x477b53, -0.18, 3.65, 0.08);
addBox(group, 1.5, 0.9, 1.45, 0x5b8a59, 0.3, 4.35, -0.05);
scene.add(group);
return group;
}
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addBox(group, 2.5, 0.16, 0.62, 0x9c6d42, 0, 0.65, 0);
addBox(group, 2.5, 0.55, 0.14, 0x9c6d42, 0, 1.03, -0.29);
addBox(group, 0.16, 0.62, 0.16, 0x465250, -0.88, 0.31, 0);
addBox(group, 0.16, 0.62, 0.16, 0x465250, 0.88, 0.31, 0);
scene.add(group);
}
function addLamp(x, z) {
addBox(scene, 0.5, 0.18, 0.5, 0x777b72, x, 0.29, z);
addBox(scene, 0.12, 4.1, 0.12, 0x3f504f, x, 2.34, z);
addBox(scene, 0.78, 0.18, 0.38, 0x3f504f, x, 4.38, z);
addBox(
scene,
0.55,
0.1,
0.25,
0xffe9ae,
x,
4.27,
z,
{
material: new THREE.MeshStandardMaterial({
color: 0xffe9ae,
emissive: 0xffca67,
emissiveIntensity: 0.75,
roughness: 0.6
})
}
);
}
function addPlanter(x, z, width = 2.2, depth = 1.2) {
addBox(scene, width, 0.55, depth, 0xaaa693, x, 0.48, z);
addBox(scene, width - 0.3, 0.12, depth - 0.3, 0x4f7655, x, 0.8, z);
}
// ---------------------------------------------------------------------
// Praça landscape
// ---------------------------------------------------------------------
// Trees are kept outside the pedestrian axis.
[
[7.2, -3.4, 0.95],
[7.6, -12.7, 1.1],
[15.4, -16.5, 1.05],
[27.1, -16.3, 0.95],
[29.1, -6.2, 1.05],
[28.5, 3.7, 0.95],
[11.1, -10.7, 0.82],
[-13.5, 7.0, 0.95],
[-21.5, 7.0, 1.0],
[18.0, 7.4, 0.85],
[-31.5, -17.0, 0.9],
[-32.0, 4.0, 0.95]
].forEach(([x, z, s]) => addTree(x, z, s));
[
[11.8, -12.8, 0],
[21.0, -17.0, 0],
[29.0, -11.5, Math.PI / 2],
[29.1, 0.2, Math.PI / 2],
[7.0, -14.5, 0]
].forEach(([x, z, r]) => addBench(x, z, r));
[
[4.8, 4.1],
[30.1, 7.5],
[30.5, -17.8],
[7.5, -18.0],
[5.0, -8.0],
[-9.0, 7.2],
[-17.0, 7.2],
[5.0, 27.0],
[11.0, 27.0],
[25.0, 27.0]
].forEach(([x, z]) => addLamp(x, z));
// Monumental garden at the heart of the square.
addBox(scene, 9.4, 0.35, 8.4, 0xb9b19d, 23.2, 0.38, -10.2);
addBox(scene, 8.6, 0.15, 7.6, 0x517458, 23.2, 0.62, -10.2);
addBox(scene, 3.8, 0.65, 3.8, 0xc8bea9, 23.2, 1.0, -10.2);
addBox(scene, 3.1, 0.4, 3.1, 0xd4cab7, 23.2, 1.52, -10.2);
addBox(scene, 1.45, 4.8, 1.45, 0xb4aa94, 23.2, 4.1, -10.2);
addBox(scene, 1.72, 0.35, 1.72, 0xd9cfb9, 23.2, 6.38, -10.2);
addBox(scene, 0.82, 0.72, 0.1, 0x9c7043, 23.2, 2.3, -9.43);
// Small planting blocks.
addPlanter(18.0, -16.7, 2.6, 1.1);
addPlanter(28.0, -16.7, 2.6, 1.1);
addPlanter(28.2, -1.2, 1.1, 2.6);
// ---------------------------------------------------------------------
// Monument plaque
// ---------------------------------------------------------------------
function createPlaqueTexture() {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#89623e";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#e5c78f";
ctx.lineWidth = 8;
ctx.strokeRect(14, 14, 996, 228);
ctx.fillStyle = "#fff1cf";
ctx.textAlign = "center";
ctx.font = "700 45px system-ui, sans-serif";
ctx.fillText("CORONEL FERNANDO PRESTES", 512, 105);
ctx.font = "500 27px system-ui, sans-serif";
ctx.fillText("PRAÇA • SOROCABA", 512, 164);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const plaque = new THREE.Mesh(
new THREE.PlaneGeometry(3.1, 0.78),
new THREE.MeshStandardMaterial({
map: createPlaqueTexture(),
roughness: 0.75
})
);
plaque.position.set(23.2, 2.3, -9.47);
scene.add(plaque);
// Square entry marker, outside the clear route.
addBox(scene, 0.16, 2.7, 0.16, 0x4b5d59, 27.0, 1.55, 7.5);
addBox(scene, 4.1, 1.05, 0.16, 0x2f705d, 27.0, 2.72, 7.5);
const entryTextureCanvas = document.createElement("canvas");
entryTextureCanvas.width = 768;
entryTextureCanvas.height = 192;
const entryCtx = entryTextureCanvas.getContext("2d");
entryCtx.fillStyle = "#2f705d";
entryCtx.fillRect(0, 0, 768, 192);
entryCtx.fillStyle = "#fff3d1";
entryCtx.textAlign = "center";
entryCtx.font = "800 53px system-ui, sans-serif";
entryCtx.fillText("PRAÇA ABERTA", 384, 82);
entryCtx.font = "500 27px system-ui, sans-serif";
entryCtx.fillText("ACESSO PEDONAL", 384, 137);
const entryTexture = new THREE.CanvasTexture(entryTextureCanvas);
entryTexture.colorSpace = THREE.SRGBColorSpace;
const entryFace = new THREE.Mesh(
new THREE.PlaneGeometry(3.88, 0.88),
new THREE.MeshStandardMaterial({
map: entryTexture,
roughness: 0.8
})
);
entryFace.position.set(27.0, 2.72, 7.59);
scene.add(entryFace);
// ---------------------------------------------------------------------
// Surrounding voxel city
// ---------------------------------------------------------------------
function addBuilding(
x,
z,
width,
depth,
height,
color,
rotation = 0,
roofColor = 0x59635e
) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addBox(group, width, height, depth, color, 0, height / 2, 0);
addBox(
group,
width + 0.35,
0.42,
depth + 0.35,
roofColor,
0,
height + 0.12,
0
);
const floors = Math.max(2, Math.floor(height / 2.1));
const columns = Math.max(2, Math.floor(width / 2.4));
for (let floor = 0; floor < floors; floor++) {
const wy = 1.5 + floor * 2.05;
for (let column = 0; column < columns; column++) {
const wx =
-width / 2 + 1.25 + column * ((width - 2.5) / Math.max(1, columns - 1));
addBox(
group,
0.88,
1.08,
0.06,
floor % 2 ? 0x6f8f90 : 0x8ba8a2,
wx,
wy,
depth / 2 + 0.035,
{ castShadow: false }
);
}
}
// Side windows.
const sideFloors = Math.max(2, Math.floor(height / 2.4));
for (let floor = 0; floor < sideFloors; floor++) {
addBox(
group,
0.06,
0.9,
0.72,
0x78989a,
width / 2 + 0.035,
1.4 + floor * 2.35,
0,
{ castShadow: false }
);
}
scene.add(group);
return group;
}
[
[-24, -25.3, 8.0, 7.0, 10.0, 0xc9a27e],
[-14.5, -25.8, 7.0, 6.5, 13.5, 0x8da69b],
[-4.8, -25.5, 7.2, 7.0, 9.0, 0xd7c3a3],
[5.0, -25.8, 7.3, 6.7, 14.0, 0xb1a48b],
[14.0, -26.2, 8.0, 6.5, 10.5, 0xc7b39b],
[25.0, -26.0, 8.0, 6.5, 15.0, 0x91a6a4],
[-35.0, -8.0, 7.0, 9.0, 11.0, 0xb9b6a6, Math.PI / 2],
[-35.0, 3.0, 7.0, 8.0, 8.0, 0xc5a78f, Math.PI / 2],
[35.4, -10.0, 7.0, 9.0, 13.0, 0x91a9a4, Math.PI / 2],
[35.4, 1.0, 7.0, 8.0, 8.5, 0xc6b49d, Math.PI / 2],
[-24.0, 31.0, 8.0, 6.0, 9.0, 0x9fae9f],
[-13.5, 31.0, 7.0, 6.0, 12.0, 0xc59a7d],
[17.0, 31.0, 10.0, 6.0, 10.0, 0x8fa4a3],
[29.0, 31.0, 8.0, 6.0, 13.0, 0xb5aa91]
].forEach((args) => addBuilding(...args));
// ---------------------------------------------------------------------
// Voxel vehicles
// ---------------------------------------------------------------------
function addCar(x, z, color, rotation = 0, bus = false) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
if (bus) {
addBox(group, 5.7, 2.1, 2.0, color, 0, 1.3, 0);
addBox(group, 5.35, 0.75, 2.04, 0x8eb0b3, 0, 1.63, 0.02);
addBox(group, 0.14, 0.78, 1.62, 0x40585b, 2.87, 1.55, 0);
} else {
addBox(group, 3.35, 0.75, 1.55, color, 0, 0.62, 0);
addBox(group, 1.85, 0.72, 1.35, 0x9ab8b7, -0.15, 1.25, 0);
addBox(group, 0.12, 0.54, 1.22, 0x3e5658, 1.67, 1.19, 0);
}
addBox(group, 0.62, 0.5, 0.18, 0x253234, -1.05, 0.35, 0.78);
addBox(group, 0.62, 0.5, 0.18, 0x253234, 1.05, 0.35, 0.78);
addBox(group, 0.62, 0.5, 0.18, 0x253234, -1.05, 0.35, -0.78);
addBox(group, 0.62, 0.5, 0.18, 0x253234, 1.05, 0.35, -0.78);
scene.add(group);
}
addCar(11.5, 15.7, 0xb55f4e);
addCar(24.8, 20.2, 0xd5c28e);
addCar(-25.2, 15.7, 0x557e88);
addCar(-31.0, 20.3, 0x869c73);
addCar(19.0, 20.2, 0xd5d0bd, 0, true);
addCar(-27, -4, 0x6b8c95, Math.PI / 2);
// ---------------------------------------------------------------------
// Voxel pedestrian
// ---------------------------------------------------------------------
function createVoxelPerson(options = {}) {
const {
shirt = 0x315f72,
trousers = 0x273b45,
skin = 0xc98962,
hair = 0x3b2d28,
backpack = 0xd19345,
cap = 0xe8d7ae
} = options;
const root = new THREE.Group();
// Legs use pivots at hip height for visible articulation.
const leftLeg = new THREE.Group();
const rightLeg = new THREE.Group();
leftLeg.position.set(-0.16, 0.82, 0);
rightLeg.position.set(0.16, 0.82, 0);
root.add(leftLeg, rightLeg);
addBox(leftLeg, 0.23, 0.57, 0.25, trousers, 0, -0.27, 0);
addBox(rightLeg, 0.23, 0.57, 0.25, trousers, 0, -0.27, 0);
addBox(leftLeg, 0.27, 0.14, 0.39, 0x283033, 0, -0.59, 0.05);
addBox(rightLeg, 0.27, 0.14, 0.39, 0x283033, 0, -0.59, 0.05);
// Torso and neck.
addBox(root, 0.62, 0.68, 0.36, shirt, 0, 1.15, 0);
addBox(root, 0.22, 0.12, 0.22, skin, 0, 1.53, 0);
// Arms.
const leftArm = new THREE.Group();
const rightArm = new THREE.Group();
leftArm.position.set(-0.42, 1.42, 0);
rightArm.position.set(0.42, 1.42, 0);
root.add(leftArm, rightArm);
addBox(leftArm, 0.19, 0.53, 0.21, shirt, 0, -0.24, 0);
addBox(rightArm, 0.19, 0.53, 0.21, shirt, 0, -0.24, 0);
addBox(leftArm, 0.18, 0.18, 0.19, skin, 0, -0.55, 0);
addBox(rightArm, 0.18, 0.18, 0.19, skin, 0, -0.55, 0);
// Head, cap and face.
addBox(root, 0.47, 0.48, 0.44, skin, 0, 1.82, 0);
addBox(root, 0.5, 0.18, 0.47, hair, 0, 2.03, -0.015);
addBox(root, 0.54, 0.08, 0.5, cap, 0, 2.1, -0.01);
addBox(root, 0.49, 0.07, 0.19, cap, 0, 2.06, 0.29);
addBox(root, 0.055, 0.065, 0.025, 0x253033, -0.115, 1.84, 0.228);
addBox(root, 0.055, 0.065, 0.025, 0x253033, 0.115, 1.84, 0.228);
// Backpack.
addBox(root, 0.43, 0.53, 0.22, backpack, 0, 1.18, -0.28);
addBox(root, 0.27, 0.12, 0.06, 0xb87835, 0, 1.23, -0.41);
root.traverse((object) => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
scene.add(root);
return {
root,
leftLeg,
rightLeg,
leftArm,
rightArm
};
}
const walker = createVoxelPerson({
shirt: 0x285c70,
trousers: 0x273943,
skin: 0xb97853,
hair: 0x302724,
backpack: 0xd79743,
cap: 0xf0dfb3
});
// A few quiet figures, all away from the route.
const standingPeople = [
createVoxelPerson({
shirt: 0x6e8170,
trousers: 0x4a4d50,
skin: 0xd19b74,
backpack: 0x7f6c58,
cap: 0x574e45
}),
createVoxelPerson({
shirt: 0xa26051,
trousers: 0x3b4750,
skin: 0x8f5e42,
backpack: 0x4f7776,
cap: 0xc2a273
}),
createVoxelPerson({
shirt: 0x516b87,
trousers: 0x36434a,
skin: 0xd3a07d,
backpack: 0xbe8d4f,
cap: 0xe2d3b2
})
];
standingPeople[0].root.position.set(25.5, 0.22, -2.8);
standingPeople[0].root.rotation.y = -0.8;
standingPeople[0].leftArm.rotation.z = -0.12;
standingPeople[0].rightArm.rotation.z = 0.12;
standingPeople[1].root.position.set(14.0, 0.22, -11.5);
standingPeople[1].root.rotation.y = 1.7;
standingPeople[2].root.position.set(28.2, 0.22, -14.1);
standingPeople[2].root.rotation.y = -1.3;
// ---------------------------------------------------------------------
// Camera controls implemented without addons
// ---------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(4, 0.5, 3),
radius: 58,
theta: 0.73,
phi: 0.78
};
const defaultCamera = {
target: new THREE.Vector3(4, 0.5, 3),
radius: 58,
theta: 0.73,
phi: 0.78
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
const canvas = renderer.domElement;
const activePointers = new Map();
let dragMode = "orbit";
let lastPinchDistance = 0;
function pointerDistance() {
const points = [...activePointers.values()];
if (points.length < 2) return 0;
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
dragMode =
event.button === 2 || event.shiftKey || event.ctrlKey
? "pan"
: "orbit";
if (activePointers.size === 2) {
lastPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = activePointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const distance = pointerDistance();
if (lastPinchDistance > 0 && distance > 0) {
cameraState.radius *= lastPinchDistance / distance;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
28,
100
);
}
lastPinchDistance = distance;
return;
}
if (dragMode === "orbit") {
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.32,
1.36
);
} else {
const forward = cameraState.target
.clone()
.sub(camera.position)
.setY(0)
.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, new THREE.Vector3(0, 1, 0))
.normalize();
const scale = cameraState.radius * 0.0014;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(forward, dy * scale);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-22,
28
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-24,
28
);
}
});
function endPointer(event) {
activePointers.delete(event.pointerId);
lastPinchDistance = activePointers.size === 2
? pointerDistance()
: 0;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius *= Math.exp(event.deltaY * 0.001);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
28,
100
);
},
{ passive: false }
);
function resetCamera() {
cameraState.target.copy(defaultCamera.target);
cameraState.radius = defaultCamera.radius;
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
addEventListener("keydown", (event) => {
const arrow = 0.08;
if (event.code === "ArrowLeft") cameraState.theta += arrow;
if (event.code === "ArrowRight") cameraState.theta -= arrow;
if (event.code === "ArrowUp") {
cameraState.phi = Math.max(0.32, cameraState.phi - arrow);
}
if (event.code === "ArrowDown") {
cameraState.phi = Math.min(1.36, cameraState.phi + arrow);
}
if (event.code === "Equal" || event.code === "NumpadAdd") {
cameraState.radius = Math.max(28, cameraState.radius - 3);
}
if (event.code === "Minus" || event.code === "NumpadSubtract") {
cameraState.radius = Math.min(100, cameraState.radius + 3);
}
if (event.code === "KeyR") resetCamera();
});
// ---------------------------------------------------------------------
// Animation and interface
// ---------------------------------------------------------------------
const playButton = document.getElementById("play");
const replayButton = document.getElementById("replay");
const resetButton = document.getElementById("camera-reset");
const progressBar = document.getElementById("progress");
const progressLabel = document.getElementById("progress-label");
const routeStatus = document.getElementById("route-status");
const liveLabel = document.getElementById("live-label");
const routeDuration = 23;
let playing = true;
let walkElapsed = 0;
function togglePlayback() {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Resume";
liveLabel.textContent = playing ? "Route in motion" : "Animation paused";
}
function replayWalk() {
walkElapsed = 0;
playing = true;
playButton.textContent = "Pause";
liveLabel.textContent = "Route in motion";
}
playButton.addEventListener("click", togglePlayback);
replayButton.addEventListener("click", replayWalk);
resetButton.addEventListener("click", resetCamera);
addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
togglePlayback();
}
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
walkElapsed += delta;
// Hold briefly at the destination, then begin another pass.
if (walkElapsed > routeDuration + 2.5) {
walkElapsed = 0;
}
}
const rawProgress = THREE.MathUtils.clamp(
walkElapsed / routeDuration,
0,
1
);
const point = routeCurve.getPointAt(rawProgress);
const tangent = routeCurve.getTangentAt(rawProgress);
walker.root.position.set(point.x, point.y, point.z);
walker.root.rotation.y = Math.atan2(tangent.x, tangent.z);
const moving = playing && rawProgress < 1;
const phase = walkElapsed * 8.6;
const swing = moving ? Math.sin(phase) * 0.58 : 0;
const armSwing = moving ? Math.sin(phase) * 0.48 : 0;
walker.leftLeg.rotation.x = swing;
walker.rightLeg.rotation.x = -swing;
walker.leftArm.rotation.x = -armSwing;
walker.rightArm.rotation.x = armSwing;
const bob = moving ? Math.abs(Math.sin(phase)) * 0.035 : 0;
walker.root.position.y += bob;
const percent = Math.round(rawProgress * 100);
progressBar.style.width = `${percent}%`;
progressLabel.textContent = `${percent}%`;
if (rawProgress < 0.43) {
routeStatus.textContent = "Crossing avenue";
} else if (rawProgress < 0.68) {
routeStatus.textContent = "Entering square";
} else {
routeStatus.textContent = "Inside civic square";
}
updateCamera();
renderer.render(scene, camera);
}
animate();
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
</script>
</body>
</html>
```