Prompt specification
Build or customize the Three.js 3D scene "Original Historic Street Tram 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: Original Historic Street Tram Scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/original-historic-street-tram-scene-x2147
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: original historic street tram scene. Build an original compact street with tram rails, sidewalks, and building corners. Preserve swept clearance and use a small deterministic traffic set. Return one self-contained HTML file.
Working Three.js Starter (original-historic-street-tram-scene-x2147):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Heritage Line — Historic Tram Street</title>
<style>
:root {
color-scheme: dark;
--ink: #f4ecdc;
--muted: #c6bba8;
--panel: rgba(25, 29, 31, 0.82);
--line: rgba(244, 236, 220, 0.18);
--amber: #e5b75d;
--green: #9ec8a1;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: #aebfc4;
font-family: Georgia, "Times New Roman", serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.hud {
position: fixed;
z-index: 10;
color: var(--ink);
pointer-events: none;
text-shadow: 0 2px 12px rgba(0, 0, 0, 0.38);
}
.title {
top: 24px;
left: 26px;
max-width: 360px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--amber);
font: 700 11px/1.2 system-ui, sans-serif;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 48px);
font-weight: 400;
line-height: 0.96;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 11px;
color: var(--muted);
font: 13px/1.45 system-ui, sans-serif;
letter-spacing: 0.04em;
}
.status {
left: 26px;
bottom: 25px;
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.chip {
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--panel);
backdrop-filter: blur(10px);
font: 700 10px/1 system-ui, sans-serif;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.chip.clear {
color: var(--amber);
border-color: rgba(229, 183, 93, 0.42);
}
.chip.running {
color: var(--green);
}
.controls {
right: 24px;
bottom: 24px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 8px;
padding: 10px 13px;
background: var(--panel);
color: var(--ink);
backdrop-filter: blur(10px);
font: 700 10px/1 system-ui, sans-serif;
letter-spacing: 0.09em;
text-transform: uppercase;
cursor: pointer;
transition: background 160ms ease, border-color 160ms ease, transform 160ms ease;
}
button:hover {
background: rgba(50, 55, 55, 0.94);
border-color: rgba(244, 236, 220, 0.38);
transform: translateY(-1px);
}
button.active {
color: #242627;
background: var(--amber);
border-color: var(--amber);
}
.hint {
position: fixed;
z-index: 10;
left: 50%;
bottom: 25px;
transform: translateX(-50%);
padding: 9px 14px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(25, 29, 31, 0.62);
color: rgba(244, 236, 220, 0.72);
backdrop-filter: blur(8px);
pointer-events: none;
font: 11px/1 system-ui, sans-serif;
letter-spacing: 0.04em;
white-space: nowrap;
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
color: #f4ecdc;
background: #293033;
font: 12px/1.4 system-ui, sans-serif;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 450ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.title { top: 17px; left: 18px; }
.subtitle { max-width: 250px; }
.status { left: 18px; bottom: 18px; }
.chip:nth-child(3) { display: none; }
.controls { right: 15px; bottom: 72px; }
.hint { display: none; }
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="hud title">
<div class="eyebrow">Heritage District · 1908</div>
<h1>Tram &<br>Stone Corner</h1>
<div class="subtitle">A compact deterministic streetscape with vintage rolling stock.</div>
</header>
<div class="hud status">
<div class="chip running" id="trafficStatus">Traffic running · 03</div>
<div class="chip clear">Tram sweep clear</div>
<div class="chip">Drag orbit · Wheel zoom</div>
</div>
<nav class="hud controls" aria-label="Scene controls">
<button data-view="overview" class="active">Overview</button>
<button data-view="street">Street</button>
<button data-view="track">Track</button>
<button id="pause">Pause</button>
</nav>
<div class="hint">Drag to orbit · Right-drag to pan · Scroll to zoom</div>
<div id="loading">Preparing the heritage district…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const loading = document.getElementById("loading");
const pauseButton = document.getElementById("pause");
const trafficStatus = document.getElementById("trafficStatus");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xaebfc4);
scene.fog = new THREE.Fog(0xaebfc4, 43, 92);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 150);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xdce9ed, 0x675849, 2.05);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffe1b0, 3.1);
sun.position.set(-15, 26, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 27;
sun.shadow.camera.bottom = -27;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.00035;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d6e7, 0.65);
fill.position.set(18, 10, -18);
scene.add(fill);
const material = (color, roughness = 0.78, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const mats = {
base: material(0x4a4037),
ground: material(0x8c9998),
road: material(0x393d3d),
sidewalk: material(0xaaa391),
seam: material(0x77766f),
curb: material(0xc5beaa),
rail: material(0x7c7770, 0.32, 0.78),
sleeper: material(0x3e3026),
brick: material(0x874b3d),
brickDark: material(0x653a34),
plaster: material(0xc9b995),
cream: material(0xe2d8bd),
trim: material(0xd6c6a4),
roof: material(0x4a4b49),
glass: material(0x263f49, 0.22, 0.12),
windowFrame: material(0xede3c8),
darkMetal: material(0x303536, 0.4, 0.62),
tramGreen: material(0x285a4d, 0.44, 0.08),
tramCream: material(0xe0d3ad, 0.58),
tramRed: material(0x8e342f, 0.55),
rubber: material(0x202324),
brass: material(0xc39a4e, 0.3, 0.7),
lamp: new THREE.MeshStandardMaterial({
color: 0xffd98a,
emissive: 0xffb84d,
emissiveIntensity: 1.6,
roughness: 0.4
}),
wood: material(0x5b3f2d),
leaves: material(0x4f6b4b),
leavesLight: material(0x6f8057),
burgundy: material(0x6f2d32),
amberLine: new THREE.MeshBasicMaterial({
color: 0xe3b65b,
transparent: true,
opacity: 0.5,
depthWrite: false
})
};
function box(parent, w, h, d, mat, x = 0, y = 0, z = 0, shadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat);
mesh.position.set(x, y, z);
mesh.castShadow = shadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(parent, rt, rb, h, mat, x = 0, y = 0, z = 0, segments = 12) {
const mesh = new THREE.Mesh(new THREE.CylinderGeometry(rt, rb, h, segments), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function sphere(parent, radius, mat, x = 0, y = 0, z = 0, detail = 10) {
const mesh = new THREE.Mesh(new THREE.SphereGeometry(radius, detail, 8), mat);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function rod(parent, a, b, radius, mat, segments = 8) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function textPanel(text, width, height, bg = "#243331", fg = "#f1dfb5") {
const canvas = document.createElement("canvas");
canvas.width = 768;
canvas.height = 192;
const ctx = canvas.getContext("2d");
ctx.fillStyle = bg;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = fg;
ctx.globalAlpha = 0.65;
ctx.lineWidth = 5;
ctx.strokeRect(12, 12, canvas.width - 24, canvas.height - 24);
ctx.globalAlpha = 1;
ctx.fillStyle = fg;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "600 65px Georgia, serif";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 3, canvas.width - 55);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshStandardMaterial({
map: texture,
roughness: 0.72,
side: THREE.DoubleSide
})
);
}
// Diorama foundation and street surfaces.
const ground = new THREE.Mesh(new THREE.PlaneGeometry(150, 150), mats.ground);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.72;
ground.receiveShadow = true;
scene.add(ground);
box(scene, 26, 0.62, 38, mats.base, 0, -0.38, 0);
box(scene, 10, 0.12, 38, mats.road, 0, -0.035, 0, false);
box(scene, 26, 0.11, 7, mats.road, 0, -0.04, 0, false);
// Deterministic cobblestones. No random calls are used.
const stoneGeo = new THREE.BoxGeometry(0.54, 0.065, 0.54);
const cobbles = new THREE.InstancedMesh(stoneGeo, material(0xffffff), 2300);
cobbles.receiveShadow = true;
const dummy = new THREE.Object3D();
const stoneColor = new THREE.Color();
let stoneIndex = 0;
for (let x = -11.7; x <= 11.7; x += 0.6) {
for (let z = -17.7; z <= 17.7; z += 0.6) {
const isNorthSouth = Math.abs(x) <= 4.8;
const isCrossStreet = Math.abs(z) <= 3.0;
if (!isNorthSouth && !isCrossStreet) continue;
const hash = Math.sin(x * 91.731 + z * 47.117) * 43758.5453;
const n = hash - Math.floor(hash);
const shade = 0.115 + n * 0.055;
stoneColor.setRGB(shade * 1.08, shade * 1.04, shade * 0.94);
dummy.position.set(x, 0.025, z);
dummy.rotation.y = (n - 0.5) * 0.045;
dummy.scale.set(0.93 + n * 0.08, 1, 0.93 + (1 - n) * 0.08);
dummy.updateMatrix();
cobbles.setMatrixAt(stoneIndex, dummy.matrix);
cobbles.setColorAt(stoneIndex, stoneColor);
stoneIndex++;
}
}
cobbles.count = stoneIndex;
cobbles.instanceMatrix.needsUpdate = true;
if (cobbles.instanceColor) cobbles.instanceColor.needsUpdate = true;
scene.add(cobbles);
// Four raised sidewalk blocks leave a clear north-south tram alignment.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(scene, 8, 0.27, 15.4, mats.sidewalk, sx * 9, 0.115, sz * 11.2, false);
for (let x = 5.3; x <= 12.7; x += 1.2) {
box(scene, 0.025, 0.012, 15.1, mats.seam, sx * x, 0.256, sz * 11.2, false);
}
for (let z = 4.0; z <= 18.6; z += 1.2) {
box(scene, 7.8, 0.012, 0.025, mats.seam, sx * 9, 0.257, sz * z, false);
}
}
}
// Segmented curbs.
for (let z = -17.5; z < 17.6; z += 1.05) {
if (Math.abs(z) < 3.9) continue;
for (const x of [-5.08, 5.08]) {
box(scene, 0.3, 0.29, 0.98, mats.curb, x, 0.16, z);
}
}
for (let x = -12.5; x < 12.6; x += 1.05) {
if (Math.abs(x) < 5.35) continue;
for (const z of [-3.56, 3.56]) {
box(scene, 0.98, 0.29, 0.3, mats.curb, x, 0.16, z);
}
}
// Crosswalk paint.
const paint = material(0xd5cdb9, 0.92);
for (const z of [-4.05, 4.05]) {
for (let x = -4.35; x <= 4.36; x += 0.82) {
box(scene, 0.44, 0.012, 0.62, paint, x, 0.076, z, false);
}
}
// Single heritage tram track, with a 1.4 m gauge.
const trackCenter = -1.65;
for (let z = -18.2; z <= 18.2; z += 0.64) {
if (Math.abs(z) < 3.5) continue;
box(scene, 1.92, 0.075, 0.16, mats.sleeper, trackCenter, 0.105, z, false);
}
for (const x of [trackCenter - 0.7, trackCenter + 0.7]) {
box(scene, 0.11, 0.09, 38, mats.rail, x, 0.18, 0, false);
box(scene, 0.055, 0.025, 38, mats.darkMetal, x, 0.225, 0, false);
}
// A subtle swept-envelope guide. Traffic remains outside this corridor.
const sweep = new THREE.Mesh(
new THREE.PlaneGeometry(2.05, 37.5),
new THREE.MeshBasicMaterial({
color: 0xe3b65b,
transparent: true,
opacity: 0.065,
depthWrite: false,
side: THREE.DoubleSide
})
);
sweep.rotation.x = -Math.PI / 2;
sweep.position.set(trackCenter, 0.094, 0);
scene.add(sweep);
for (const x of [trackCenter - 1.04, trackCenter + 1.04]) {
for (let z = -18; z < 18; z += 1.15) {
box(scene, 0.055, 0.012, 0.57, mats.amberLine, x, 0.102, z, false);
}
}
function addBuilding({
x, z, w, d, h, bodyMat, trimMat,
signText, signColor = "#283533",
turret = false
}) {
const group = new THREE.Group();
group.position.set(x, 0.25, z);
scene.add(group);
box(group, w, h, d, bodyMat, 0, h / 2, 0);
box(group, w + 0.28, 0.25, d + 0.28, trimMat, 0, h + 0.02, 0);
box(group, w + 0.12, 0.18, d + 0.12, mats.roof, 0, h + 0.2, 0);
// Horizontal masonry bands.
const bands = Math.max(1, Math.floor(h / 2.6));
for (let i = 1; i <= bands; i++) {
const y = (h / (bands + 1)) * i;
box(group, w + 0.035, 0.09, d + 0.035, trimMat, 0, y, 0);
}
const rows = Math.max(2, Math.floor((h - 0.8) / 2.0));
const countX = Math.max(2, Math.floor(d / 1.45));
const countZ = Math.max(2, Math.floor(w / 1.45));
for (const side of [-1, 1]) {
for (let row = 0; row < rows; row++) {
const y = 1.65 + row * 2.02;
if (y + 0.56 > h - 0.15) continue;
for (let i = 0; i < countX; i++) {
const zz = -d / 2 + 0.85 + i * ((d - 1.7) / Math.max(1, countX - 1));
box(group, 0.085, 1.12, 0.78, mats.windowFrame, side * (w / 2 + 0.045), y, zz);
box(group, 0.095, 0.88, 0.56, mats.glass, side * (w / 2 + 0.095), y, zz);
box(group, 0.105, 0.88, 0.035, trimMat, side * (w / 2 + 0.11), y, zz);
}
for (let i = 0; i < countZ; i++) {
const xx = -w / 2 + 0.85 + i * ((w - 1.7) / Math.max(1, countZ - 1));
box(group, 0.78, 1.12, 0.085, mats.windowFrame, xx, y, side * (d / 2 + 0.045));
box(group, 0.56, 0.88, 0.095, mats.glass, xx, y, side * (d / 2 + 0.095));
box(group, 0.035, 0.88, 0.105, trimMat, xx, y, side * (d / 2 + 0.11));
}
}
}
// Road-facing shopfront and door.
const roadZ = -Math.sign(z || 1);
const roadX = -Math.sign(x || 1);
box(group, 1.65, 1.55, 0.1, mats.glass, 0, 0.98, roadZ * (d / 2 + 0.06));
box(group, 0.84, 1.75, 0.12, mats.wood, w * 0.29, 1.02, roadZ * (d / 2 + 0.08));
box(group, 1.85, 0.13, 0.17, trimMat, 0, 1.88, roadZ * (d / 2 + 0.08));
const sign = textPanel(signText, Math.min(w * 0.73, 3.8), 0.62, signColor);
sign.position.set(0, 2.55, roadZ * (d / 2 + 0.16));
sign.rotation.y = roadZ > 0 ? 0 : Math.PI;
group.add(sign);
// Corner quoins.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
for (let y = 0.45; y < h; y += 0.75) {
box(
group, 0.38, 0.26, 0.38, trimMat,
sx * (w / 2 - 0.12), y, sz * (d / 2 - 0.12)
);
}
}
}
// Chimney.
box(group, 0.65, 1.15, 0.7, mats.brickDark, w * 0.22, h + 0.82, -d * 0.18);
box(group, 0.79, 0.15, 0.83, trimMat, w * 0.22, h + 1.42, -d * 0.18);
if (turret) {
const towerHeight = h + 0.75;
cylinder(group, 0.92, 0.98, towerHeight, bodyMat, w / 2 - 0.82, towerHeight / 2, d / 2 - 0.82, 8);
const roof = new THREE.Mesh(new THREE.ConeGeometry(1.28, 1.55, 8), mats.roof);
roof.position.set(w / 2 - 0.82, towerHeight + 0.72, d / 2 - 0.82);
roof.castShadow = true;
group.add(roof);
for (const y of [2.2, 4.2, 6.2]) {
box(group, 0.34, 0.65, 0.34, mats.glass, w / 2 - 0.82, y, d / 2 - 1.75);
}
}
return group;
}
addBuilding({
x: -8.75, z: -8.25, w: 5.9, d: 6.2, h: 7.6,
bodyMat: mats.brick, trimMat: mats.cream,
signText: "MERIDIAN HOTEL", signColor: "#5a2f2a", turret: true
});
addBuilding({
x: 8.65, z: -8.4, w: 5.6, d: 6.0, h: 6.25,
bodyMat: mats.plaster, trimMat: mats.cream,
signText: "PRINTERS & CO.", signColor: "#334c45"
});
addBuilding({
x: -8.9, z: 8.45, w: 6.0, d: 5.7, h: 4.75,
bodyMat: mats.brickDark, trimMat: mats.trim,
signText: "TRAM OFFICE", signColor: "#244f46"
});
addBuilding({
x: 8.8, z: 8.55, w: 5.55, d: 5.6, h: 5.15,
bodyMat: mats.cream, trimMat: mats.trim,
signText: "CAFÉ LUNA", signColor: "#713038"
});
// Small awning over the cafe frontage.
const cafe = scene.children.find(o => o.position.x === 8.8 && o.position.z === 8.55);
if (cafe) {
const awning = box(cafe, 3.5, 0.13, 1.05, mats.burgundy, 0, 2.06, -3.0);
awning.rotation.x = -0.16;
for (let i = -1; i <= 1; i++) {
box(cafe, 0.09, 0.52, 0.09, mats.darkMetal, i * 1.2, 1.82, -3.48);
}
}
function addLamp(x, z) {
const group = new THREE.Group();
group.position.set(x, 0.25, z);
scene.add(group);
cylinder(group, 0.18, 0.24, 0.22, mats.darkMetal, 0, 0.1, 0);
cylinder(group, 0.055, 0.075, 3.35, mats.darkMetal, 0, 1.77, 0);
box(group, 0.34, 0.48, 0.34, mats.lamp, 0, 3.62, 0);
box(group, 0.47, 0.09, 0.47, mats.darkMetal, 0, 3.9, 0);
const roof = new THREE.Mesh(new THREE.ConeGeometry(0.34, 0.25, 4), mats.darkMetal);
roof.position.y = 4.05;
roof.rotation.y = Math.PI / 4;
group.add(roof);
}
addLamp(-5.65, -4.45);
addLamp(5.65, -4.45);
addLamp(-5.65, 4.45);
addLamp(5.65, 4.45);
addLamp(-5.7, -13.6);
addLamp(5.7, 13.6);
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.25, z);
group.scale.setScalar(scale);
scene.add(group);
cylinder(group, 0.15, 0.22, 2.35, mats.wood, 0, 1.15, 0);
const crown = new THREE.Mesh(new THREE.IcosahedronGeometry(1.0, 1), mats.leaves);
crown.position.set(0, 2.7, 0);
crown.scale.set(0.9, 1.15, 0.9);
crown.castShadow = true;
group.add(crown);
const crown2 = new THREE.Mesh(new THREE.IcosahedronGeometry(0.72, 1), mats.leavesLight);
crown2.position.set(0.35, 3.25, -0.12);
crown2.castShadow = true;
group.add(crown2);
cylinder(group, 0.95, 0.95, 0.15, mats.wood, 0, 0.06, 0);
}
addTree(-6.55, -14.2, 0.95);
addTree(6.65, 14.2, 0.9);
addTree(-6.75, 13.7, 0.82);
addTree(6.55, -14.4, 0.86);
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.25, z);
group.rotation.y = rotation;
scene.add(group);
box(group, 1.45, 0.1, 0.43, mats.wood, 0, 0.48, 0);
box(group, 1.45, 0.42, 0.09, mats.wood, 0, 0.76, -0.2);
for (const xx of [-0.55, 0.55]) {
box(group, 0.09, 0.5, 0.36, mats.darkMetal, xx, 0.25, 0);
}
}
addBench(5.75, -10.4, Math.PI / 2);
addBench(-5.75, 10.5, -Math.PI / 2);
// Overhead tram wire and support poles.
rod(scene, [trackCenter, 4.65, -19], [trackCenter, 4.65, 19], 0.018, mats.darkMetal);
for (const z of [-16, -8, 0, 8, 16]) {
cylinder(scene, 0.07, 0.105, 4.4, mats.darkMetal, -5.55, 2.45, z);
rod(scene, [-5.55, 4.35, z], [trackCenter, 4.65, z], 0.035, mats.darkMetal);
}
// Tram-stop marker.
cylinder(scene, 0.055, 0.075, 2.35, mats.darkMetal, -4.15, 1.4, -10.3);
const stopSign = textPanel("TRAM STOP", 1.15, 0.38, "#315348", "#f0dca8");
stopSign.position.set(-4.15, 2.48, -10.28);
stopSign.rotation.y = Math.PI;
scene.add(stopSign);
// Vintage tram, front facing +Z.
function makeTram() {
const group = new THREE.Group();
box(group, 1.58, 0.64, 4.55, mats.tramGreen, 0, 0.93, 0);
box(group, 1.5, 1.14, 4.28, mats.tramCream, 0, 1.74, 0);
box(group, 1.72, 0.18, 4.58, mats.roof, 0, 2.39, 0);
box(group, 1.62, 0.12, 4.58, mats.tramRed, 0, 1.22, 0);
box(group, 1.58, 0.09, 4.56, mats.brass, 0, 1.31, 0);
for (const side of [-1, 1]) {
for (const z of [-1.43, 0, 1.43]) {
box(group, 0.075, 0.72, 0.94, mats.glass, side * 0.765, 1.79, z);
box(group, 0.09, 0.82, 0.07, mats.tramCream, side * 0.79, 1.79, z - 0.5);
box(group, 0.09, 0.82, 0.07, mats.tramCream, side * 0.79, 1.79, z + 0.5);
}
for (const z of [-1.5, 1.5]) {
const wheel = cylinder(group, 0.31, 0.31, 0.16, mats.rubber, side * 0.76, 0.52, z, 18);
wheel.rotation.z = Math.PI / 2;
const hub = cylinder(group, 0.11, 0.11, 0.18, mats.brass, side * 0.79, 0.52, z, 12);
hub.rotation.z = Math.PI / 2;
}
}
for (const end of [-1, 1]) {
box(group, 1.18, 0.72, 0.08, mats.glass, 0, 1.79, end * 2.16);
box(group, 0.06, 0.82, 0.1, mats.tramCream, -0.62, 1.79, end * 2.18);
box(group, 0.06, 0.82, 0.1, mats.tramCream, 0.62, 1.79, end * 2.18);
sphere(group, 0.105, mats.lamp, -0.48, 1.22, end * 2.27);
sphere(group, 0.105, mats.lamp, 0.48, 1.22, end * 2.27);
}
const destination = textPanel("HERITAGE LINE", 1.13, 0.25, "#263d36", "#efd99f");
destination.position.set(0, 2.12, 2.205);
group.add(destination);
rod(group, [0, 2.48, 0.25], [0, 4.65, 1.65], 0.025, mats.darkMetal);
rod(group, [0, 4.65, 1.65], [0, 4.48, 2.05], 0.035, mats.darkMetal);
group.position.x = trackCenter;
scene.add(group);
return group;
}
// Deterministic period motor vehicles, both outside the swept tram zone.
function makeVehicle(color, van = false) {
const group = new THREE.Group();
const bodyMat = material(color, 0.48, 0.06);
const width = van ? 1.25 : 1.42;
const length = van ? 3.55 : 2.95;
if (van) {
box(group, width, 1.25, length - 0.35, bodyMat, 0, 1.0, -0.25);
box(group, width * 0.96, 0.73, 1.0, bodyMat, 0, 1.35, length * 0.36);
box(group, width * 0.83, 0.48, 0.07, mats.glass, 0, 1.45, length * 0.36 + 0.52);
box(group, width * 0.97, 0.13, 0.15, mats.brass, 0, 1.62, length * 0.36 + 0.54);
} else {
box(group, width, 0.56, length, bodyMat, 0, 0.68, 0);
box(group, width * 0.84, 0.58, 1.45, bodyMat, 0, 1.18, -0.18);
box(group, width * 0.73, 0.4, 0.07, mats.glass, 0, 1.2, 0.56);
for (const side of [-1, 1]) {
box(group, 0.055, 0.39, 1.02, mats.glass, side * width * 0.43, 1.2, -0.15);
}
box(group, width + 0.05, 0.08, 0.13, mats.brass, 0, 0.83, length / 2 + 0.02);
box(group, width + 0.05, 0.08, 0.13, mats.brass, 0, 0.83, -length / 2 - 0.02);
}
for (const side of [-1, 1]) {
for (const z of [-length * 0.31, length * 0.31]) {
const wheel = cylinder(group, 0.28, 0.28, 0.15, mats.rubber, side * width * 0.5, 0.34, z, 16);
wheel.rotation.z = Math.PI / 2;
const hub = cylinder(group, 0.11, 0.11, 0.17, mats.brass, side * width * 0.51, 0.34, z, 12);
hub.rotation.z = Math.PI / 2;
}
}
sphere(group, 0.09, mats.lamp, -width * 0.29, 0.86, length / 2 + 0.02);
sphere(group, 0.09, mats.lamp, width * 0.29, 0.86, length / 2 + 0.02);
return group;
}
const tram = makeTram();
const touringCar = makeVehicle(0x253e3c);
const deliveryVan = makeVehicle(0xa66f35, true);
touringCar.position.x = 1.95;
deliveryVan.position.x = 3.5;
scene.add(touringCar, deliveryVan);
// A few pedestrians use fixed loops and fixed speeds.
function makePerson(coatColor, hatColor) {
const group = new THREE.Group();
const coat = material(coatColor);
const skin = material(0xc68f6a);
const hat = material(hatColor);
box(group, 0.32, 0.66, 0.25, coat, 0, 1.0, 0);
sphere(group, 0.16, skin, 0, 1.48, 0);
cylinder(group, 0.19, 0.19, 0.08, hat, 0, 1.63, 0);
cylinder(group, 0.12, 0.15, 0.16, hat, 0, 1.7, 0);
const leftLeg = box(group, 0.11, 0.58, 0.12, mats.darkMetal, -0.09, 0.42, 0);
const rightLeg = box(group, 0.11, 0.58, 0.12, mats.darkMetal, 0.09, 0.42, 0);
group.userData.leftLeg = leftLeg;
group.userData.rightLeg = rightLeg;
return group;
}
const pedestrians = [
{ object: makePerson(0x46516c, 0x282b2c), x: -5.75, z: -13.5, speed: 0.42, range: 21, offset: 0 },
{ object: makePerson(0x793f38, 0x6d573e), x: 5.72, z: 13.7, speed: 0.36, range: 22, offset: 3 },
{ object: makePerson(0x6f7652, 0x343936), x: -7.4, z: 5.7, speed: 0.28, range: 8, offset: 2 },
{ object: makePerson(0x384f5c, 0x463b34), x: 7.45, z: -6.1, speed: 0.3, range: 8, offset: 5 }
];
for (const person of pedestrians) scene.add(person.object);
// Manual camera controls implemented without Three.js addons.
const cameraState = {
target: new THREE.Vector3(0, 1.45, 0),
radius: 33,
theta: 0.66,
phi: 0.96
};
const presets = {
overview: { target: [0, 1.45, 0], radius: 33, theta: 0.66, phi: 0.96 },
street: { target: [0, 1.45, -2.2], radius: 22.5, theta: 0.16, phi: 1.2 },
track: { target: [-1.4, 1.65, -0.4], radius: 19, theta: 0.5, phi: 1.08 }
};
let cameraTween = null;
function applyCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function setPreset(name) {
const p = presets[name];
cameraTween = {
start: performance.now(),
duration: 850,
from: {
target: cameraState.target.clone(),
radius: cameraState.radius,
theta: cameraState.theta,
phi: cameraState.phi
},
to: {
target: new THREE.Vector3(...p.target),
radius: p.radius,
theta: p.theta,
phi: p.phi
}
};
document.querySelectorAll("[data-view]").forEach(button => {
button.classList.toggle("active", button.dataset.view === name);
});
}
document.querySelectorAll("[data-view]").forEach(button => {
button.addEventListener("click", () => setPreset(button.dataset.view));
});
const pointers = new Map();
let gestureMode = "rotate";
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
gestureMode = event.button === 2 || event.shiftKey ? "pan" : "rotate";
cameraTween = null;
});
renderer.domElement.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
if (gestureMode === "pan") {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrixWorld, 0);
const scale = cameraState.radius * 0.0015;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.y += dy * scale;
} else {
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - dy * 0.005, 0.42, 1.42);
}
} else if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(values[0].x - values[1].x, values[0].y - values[1].y);
const midpoint = {
x: (values[0].x + values[1].x) * 0.5,
y: (values[0].y + values[1].y) * 0.5
};
if (cameraTween) cameraTween = null;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (camera.lastPinchDistance || distance) / Math.max(distance, 1),
12,
58
);
camera.lastPinchDistance = distance;
cameraState.target.y += (midpoint.y - (camera.lastPinchY || midpoint.y)) * 0.012;
camera.lastPinchY = midpoint.y;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
camera.lastPinchDistance = 0;
camera.lastPinchY = 0;
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraTween = null;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
58
);
}, { passive: false });
// Fixed-step simulation ensures a repeatable traffic timeline.
let paused = false;
let simulationTime = 0;
let previousTime = performance.now();
let accumulator = 0;
const fixedStep = 1 / 60;
function wrap(value, range) {
return ((value % range) + range) % range;
}
function updateTraffic() {
tram.position.z = -19 + wrap(simulationTime * 2.2, 38);
touringCar.position.z = 17 - wrap(simulationTime * 3.1, 34);
touringCar.rotation.y = Math.PI;
deliveryVan.position.z = -17 + wrap(simulationTime * 2.05, 34);
deliveryVan.rotation.y = 0;
for (const person of pedestrians) {
const progress = wrap(simulationTime * person.speed + person.offset, person.range);
person.object.position.z = person.z + progress;
person.object.rotation.y = person.x > 0 ? Math.PI / 2 : -Math.PI / 2;
const stride = Math.sin(simulationTime * 4.5 + person.offset) * 0.23;
person.object.userData.leftLeg.rotation.x = stride;
person.object.userData.rightLeg.rotation.x = -stride;
}
}
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.classList.toggle("active", paused);
trafficStatus.textContent = paused ? "Traffic paused · 03" : "Traffic running · 03";
trafficStatus.style.color = paused ? "#e5b75d" : "#9ec8a1";
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
function animate(now) {
requestAnimationFrame(animate);
const frameDelta = Math.min((now - previousTime) / 1000, 0.1);
previousTime = now;
if (!paused) {
accumulator += frameDelta;
while (accumulator >= fixedStep) {
simulationTime += fixedStep;
accumulator -= fixedStep;
}
}
updateTraffic();
if (cameraTween) {
const raw = Math.min((now - cameraTween.start) / cameraTween.duration, 1);
const t = raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
cameraState.target.lerpVectors(cameraTween.from.target, cameraTween.to.target, t);
cameraState.radius = THREE.MathUtils.lerp(cameraTween.from.radius, cameraTween.to.radius, t);
cameraState.theta = THREE.MathUtils.lerp(cameraTween.from.theta, cameraTween.to.theta, t);
cameraState.phi = THREE.MathUtils.lerp(cameraTween.from.phi, cameraTween.to.phi, t);
if (raw >= 1) cameraTween = null;
}
applyCamera();
renderer.render(scene, camera);
}
applyCamera();
updateTraffic();
renderer.render(scene, camera);
requestAnimationFrame(animate);
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 500);
</script>
</body>
</html>
```