Prompt specification
Build or customize the Three.js 3D scene "Neighborhood Four-Way Intersection Diorama" 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: Neighborhood Four-Way Intersection Diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-four-way-intersection-diorama-x2107
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: neighborhood four-way intersection diorama. Build an original bounded street corner with sidewalks, crosswalks, and turning paths. Use simplified visible signal phases and keep vehicles on continuous collision-free routes.
Working Three.js Starter (neighborhood-four-way-intersection-diorama-x2107):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Crossrown — Voxel Intersection Diorama</title>
<style>
:root {
--ink: #17313b;
--paper: rgba(255, 252, 242, 0.9);
--line: rgba(23, 49, 59, 0.14);
--green: #42d58b;
--amber: #ffbd4a;
--red: #f05c5c;
--cyan: #80d9eb;
--pink: #ff78ca;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8e4e5;
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;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
padding: 15px 18px 14px;
min-width: 260px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 40px rgba(24, 45, 53, 0.12);
backdrop-filter: blur(12px);
}
.eyebrow {
margin: 0 0 5px;
color: #688087;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 24px;
line-height: 1;
letter-spacing: -0.04em;
}
.brand p:last-child {
margin: 8px 0 0;
color: #668087;
font-size: 12px;
}
.phase-panel {
position: absolute;
top: 24px;
right: 26px;
width: min(330px, calc(100vw - 52px));
padding: 17px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 16px 40px rgba(24, 45, 53, 0.12);
backdrop-filter: blur(12px);
}
.phase-heading {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
}
.phase-number {
color: #789097;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.phase-title {
margin-top: 4px;
font-size: 19px;
font-weight: 800;
letter-spacing: -0.025em;
}
.countdown {
min-width: 55px;
padding: 7px 9px;
border-radius: 10px;
background: #17313b;
color: white;
font-variant-numeric: tabular-nums;
font-size: 13px;
font-weight: 800;
text-align: center;
}
.phase-detail {
min-height: 42px;
margin: 11px 0 13px;
color: #60767e;
font-size: 12px;
line-height: 1.45;
}
.timeline {
height: 5px;
overflow: hidden;
border-radius: 5px;
background: rgba(23, 49, 59, 0.1);
}
.timeline > div {
width: 0;
height: 100%;
border-radius: inherit;
background: #17313b;
transition: width 0.08s linear;
}
.signal-row {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
margin-top: 14px;
}
.signal-chip {
padding: 8px 4px 7px;
border-radius: 10px;
background: rgba(23, 49, 59, 0.07);
text-align: center;
transition: background 0.2s, color 0.2s;
}
.signal-chip strong {
display: block;
font-size: 10px;
letter-spacing: 0.08em;
}
.signal-chip small {
display: block;
margin-top: 3px;
font-size: 9px;
font-weight: 800;
}
.signal-chip.go {
background: rgba(66, 213, 139, 0.18);
color: #187449;
}
.signal-chip.amber {
background: rgba(255, 189, 74, 0.2);
color: #94600c;
}
.signal-chip.stop {
background: rgba(240, 92, 92, 0.12);
color: #9a4242;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 10px 14px;
margin-top: 13px;
color: #6d8188;
font-size: 10px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 13px;
height: 3px;
border-radius: 4px;
background: var(--cyan);
}
.legend .left {
background: var(--pink);
}
.legend .right {
background: #ffc857;
}
.controls {
position: absolute;
bottom: 24px;
left: 50%;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--paper);
box-shadow: 0 14px 35px rgba(24, 45, 53, 0.13);
pointer-events: auto;
transform: translateX(-50%);
backdrop-filter: blur(12px);
}
button {
min-height: 36px;
padding: 0 13px;
border: 0;
border-radius: 10px;
background: rgba(23, 49, 59, 0.08);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 0.15s, background 0.15s;
}
button:hover {
background: rgba(23, 49, 59, 0.14);
transform: translateY(-1px);
}
button.primary {
background: #17313b;
color: white;
}
.hint {
position: absolute;
bottom: 29px;
left: 26px;
color: rgba(23, 49, 59, 0.68);
font-size: 11px;
font-weight: 650;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #d8e4e5;
color: var(--ink);
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 0.4s;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 14px;
left: 14px;
min-width: 0;
padding: 12px 14px;
}
.brand p:last-child {
display: none;
}
h1 {
font-size: 20px;
}
.phase-panel {
top: auto;
right: 14px;
bottom: 78px;
width: calc(100vw - 28px);
}
.phase-detail,
.legend {
display: none;
}
.controls {
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel four-way intersection"></canvas>
<div class="overlay">
<header class="brand">
<p class="eyebrow">Neighborhood Study 04</p>
<h1>Pine & Eighth</h1>
<p>Voxel intersection · protected signal cycle</p>
</header>
<section class="phase-panel" aria-live="polite">
<div class="phase-heading">
<div>
<div id="phaseNumber" class="phase-number">Phase 01 / 14</div>
<div id="phaseTitle" class="phase-title">North / South Through</div>
</div>
<div id="countdown" class="countdown">9s</div>
</div>
<p id="phaseDetail" class="phase-detail"></p>
<div class="timeline">
<div id="timelineFill"></div>
</div>
<div class="signal-row">
<div id="signalN" class="signal-chip stop">
<strong>N</strong><small>STOP</small>
</div>
<div id="signalS" class="signal-chip stop">
<strong>S</strong><small>STOP</small>
</div>
<div id="signalE" class="signal-chip stop">
<strong>E</strong><small>STOP</small>
</div>
<div id="signalW" class="signal-chip stop">
<strong>W</strong><small>STOP</small>
</div>
</div>
<div class="legend">
<span><i></i> Through route</span>
<span><i class="left"></i> Protected left</span>
<span><i class="right"></i> Protected right</span>
</div>
</section>
<div class="hint">Drag to orbit · Scroll to zoom</div>
<nav class="controls" aria-label="Scene controls">
<button id="pauseButton" class="primary">Pause</button>
<button id="topButton">Top view</button>
<button id="resetButton">Reset camera</button>
</nav>
</div>
<div id="loading">Building the neighborhood…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const loading = document.getElementById("loading");
const phaseNumber = document.getElementById("phaseNumber");
const phaseTitle = document.getElementById("phaseTitle");
const phaseDetail = document.getElementById("phaseDetail");
const countdown = document.getElementById("countdown");
const timelineFill = document.getElementById("timelineFill");
const pauseButton = document.getElementById("pauseButton");
const topButton = document.getElementById("topButton");
const resetButton = document.getElementById("resetButton");
const signalElements = {
N: document.getElementById("signalN"),
S: document.getElementById("signalS"),
E: document.getElementById("signalE"),
W: document.getElementById("signalW")
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8e4e5);
scene.fog = new THREE.Fog(0xd8e4e5, 82, 145);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
220
);
const cameraTarget = new THREE.Vector3(0, 0.6, 0);
let cameraRadius = 86;
let cameraTheta = 0.76;
let cameraPhi = 0.76;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xe9f5ff, 0x8d8069, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d5, 3.25);
sun.position.set(-28, 48, 34);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -55;
sun.shadow.camera.right = 55;
sun.shadow.camera.top = 55;
sun.shadow.camera.bottom = -55;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 120;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8fc7df, 0.65);
fill.position.set(35, 22, -30);
scene.add(fill);
const materials = {
earth: new THREE.MeshStandardMaterial({
color: 0x9f856a,
roughness: 1
}),
baseEdge: new THREE.MeshStandardMaterial({
color: 0x77664f,
roughness: 1
}),
asphalt: new THREE.MeshStandardMaterial({
color: 0x35434b,
roughness: 0.94
}),
asphaltPatch: new THREE.MeshStandardMaterial({
color: 0x3d4b52,
roughness: 1
}),
sidewalk: new THREE.MeshStandardMaterial({
color: 0xd8d2c7,
roughness: 0.96
}),
curb: new THREE.MeshStandardMaterial({
color: 0xf0ece2,
roughness: 0.9
}),
lane: new THREE.MeshStandardMaterial({
color: 0xfff4d5,
roughness: 0.8
}),
yellow: new THREE.MeshStandardMaterial({
color: 0xf3c64f,
roughness: 0.85
}),
grass: new THREE.MeshStandardMaterial({
color: 0x87a968,
roughness: 1
}),
path: new THREE.MeshStandardMaterial({
color: 0xc9bfa7,
roughness: 1
}),
darkMetal: new THREE.MeshStandardMaterial({
color: 0x25343a,
roughness: 0.65,
metalness: 0.28
}),
pole: new THREE.MeshStandardMaterial({
color: 0x43565b,
roughness: 0.55,
metalness: 0.4
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x75563c,
roughness: 1
}),
leafA: new THREE.MeshStandardMaterial({
color: 0x5f8f58,
roughness: 1,
flatShading: true
}),
leafB: new THREE.MeshStandardMaterial({
color: 0x79a563,
roughness: 1,
flatShading: true
}),
leafC: new THREE.MeshStandardMaterial({
color: 0x9eb76b,
roughness: 1,
flatShading: true
}),
windowWarm: new THREE.MeshStandardMaterial({
color: 0x344a50,
emissive: 0xffbd63,
emissiveIntensity: 0.28,
roughness: 0.28
}),
windowCool: new THREE.MeshStandardMaterial({
color: 0x39515a,
emissive: 0x8ac7d6,
emissiveIntensity: 0.12,
roughness: 0.3
}),
glass: new THREE.MeshStandardMaterial({
color: 0x1f343d,
roughness: 0.18,
metalness: 0.25
}),
tire: new THREE.MeshStandardMaterial({
color: 0x182126,
roughness: 0.95
}),
headlight: new THREE.MeshStandardMaterial({
color: 0xfff0c2,
emissive: 0xffe5a0,
emissiveIntensity: 1.2,
roughness: 0.35
}),
taillight: new THREE.MeshStandardMaterial({
color: 0x8e2525,
emissive: 0xff3a2f,
emissiveIntensity: 0.75,
roughness: 0.4
})
};
function box(w, h, d, material, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let seed = 8173;
function random() {
seed = (seed * 16807) % 2147483647;
return (seed - 1) / 2147483646;
}
// Bounded diorama base.
box(78, 1.55, 78, materials.baseEdge, 0, -1.02, 0);
box(77.4, 0.18, 77.4, materials.earth, 0, -0.22, 0);
// Continuous plus-shaped asphalt road.
const roadPolygon = [
[-8, -36], [8, -36], [8, -8], [36, -8],
[36, 8], [8, 8], [8, 36], [-8, 36],
[-8, 8], [-36, 8], [-36, -8], [-8, -8]
];
const roadShape = new THREE.Shape(
roadPolygon.map(([x, z]) => new THREE.Vector2(x, -z))
);
const roadGeometry = new THREE.ShapeGeometry(roadShape);
roadGeometry.rotateX(-Math.PI / 2);
const road = new THREE.Mesh(roadGeometry, materials.asphalt);
road.position.y = 0.015;
road.receiveShadow = true;
scene.add(road);
// Raised sidewalk corner blocks.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(26, 0.52, 26, materials.sidewalk, sx * 22, 0.15, sz * 22);
// L-shaped curb along the street-facing corner.
box(0.6, 0.58, 26, materials.curb, sx * 8.75, 0.16, sz * 22);
box(26, 0.58, 0.6, materials.curb, sx * 22, 0.16, sz * 8.75);
}
}
// Subtle asphalt patches.
for (let i = 0; i < 18; i++) {
const side = random() > 0.5 ? 1 : -1;
const along = -31 + random() * 62;
const across = side * (4.5 + random() * 2.1);
const patch = box(
1.5 + random() * 3,
0.012,
0.7 + random() * 1.8,
materials.asphaltPatch,
along,
0.035,
across
);
patch.castShadow = false;
}
// Yellow center dividers, interrupted around the intersection.
for (let p = -32; p <= 32; p += 6) {
if (Math.abs(p) > 15) {
box(0.24, 0.035, 2.4, materials.yellow, 0, 0.065, p);
box(2.4, 0.035, 0.24, materials.yellow, p, 0.065, 0);
}
}
// White road edges.
for (const side of [-1, 1]) {
for (let p = -33; p <= 33; p += 6) {
if (Math.abs(p) > 15) {
box(0.16, 0.028, 2.7, materials.lane, side * 7.15, 0.065, p);
box(2.7, 0.028, 0.16, materials.lane, p, 0.065, side * 7.15);
}
}
}
// Crosswalks.
for (const z of [-11.6, 11.6]) {
for (let i = -2; i <= 2; i++) {
const stripe = box(
0.58,
0.04,
2.8,
materials.lane,
i * 2.65,
0.085,
z
);
stripe.castShadow = false;
}
}
for (const x of [-11.6, 11.6]) {
for (let i = -2; i <= 2; i++) {
const stripe = box(
2.8,
0.04,
0.58,
materials.lane,
x,
0.085,
i * 2.65
);
stripe.castShadow = false;
}
}
// Stop lines.
box(3.6, 0.04, 0.62, materials.lane, -3, 0.085, -13.5);
box(3.6, 0.04, 0.62, materials.lane, 3, 0.085, 13.5);
box(0.62, 0.04, 3.6, materials.lane, 13.5, 0.085, -3);
box(0.62, 0.04, 3.6, materials.lane, -13.5, 0.085, 3);
// Tactile paving blocks.
const tactileMaterial = new THREE.MeshStandardMaterial({
color: 0xe6b94e,
roughness: 0.95
});
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(1.4, 0.045, 0.8, tactileMaterial, sx * 6.6, 0.1, sz * 10.3);
box(0.8, 0.045, 1.4, tactileMaterial, sx * 10.3, 0.1, sz * 6.6);
}
}
function addBuilding(x, z, w, d, h, bodyColor, roofColor, shopColor) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const bodyMaterial = new THREE.MeshStandardMaterial({
color: bodyColor,
roughness: 0.92
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: roofColor,
roughness: 0.9
});
box(w + 0.5, 0.38, d + 0.5, materials.curb, 0, 0.55, 0, group);
box(w, h, d, bodyMaterial, 0, 0.65 + h / 2, 0, group);
box(w + 0.65, 0.45, d + 0.65, roofMaterial, 0, 0.8 + h, 0, group);
const floors = Math.max(2, Math.floor(h / 2.5));
const columns = Math.max(2, Math.floor(w / 2.3));
for (let floor = 0; floor < floors; floor++) {
const wy = 1.75 + floor * 2.25;
for (let col = 0; col < columns; col++) {
const wx = -w / 2 + 1.45 + col * ((w - 2.6) / Math.max(1, columns - 1));
const windowMaterial = random() > 0.32
? materials.windowWarm
: materials.windowCool;
box(
0.95,
1.05,
0.12,
windowMaterial,
wx,
wy,
d / 2 + 0.07,
group
);
box(
0.95,
1.05,
0.12,
windowMaterial,
wx,
wy,
-d / 2 - 0.07,
group
);
}
const sideColumns = Math.max(2, Math.floor(d / 2.5));
for (let col = 0; col < sideColumns; col++) {
const wz = -d / 2 + 1.35 + col * ((d - 2.5) / Math.max(1, sideColumns - 1));
box(
0.12,
1.05,
0.95,
random() > 0.5 ? materials.windowWarm : materials.windowCool,
w / 2 + 0.07,
wy,
wz,
group
);
box(
0.12,
1.05,
0.95,
random() > 0.5 ? materials.windowWarm : materials.windowCool,
-w / 2 - 0.07,
wy,
wz,
group
);
}
}
box(1.35, 2.1, 0.16, materials.glass, 0, 1.6, d / 2 + 0.1, group);
const awningMaterial = new THREE.MeshStandardMaterial({
color: shopColor,
roughness: 0.85
});
box(w * 0.62, 0.28, 1.25, awningMaterial, 0, 3.1, d / 2 + 0.55, group);
const signMaterial = new THREE.MeshStandardMaterial({
color: 0x243b43,
emissive: shopColor,
emissiveIntensity: 0.18,
roughness: 0.45
});
box(w * 0.47, 0.62, 0.22, signMaterial, 0, 3.8, d / 2 + 0.12, group);
box(2.2, 1.2, 2.4, materials.darkMetal, w * 0.22, h + 1.55, -d * 0.15, group);
box(2.6, 0.22, 2.8, roofMaterial, w * 0.22, h + 2.22, -d * 0.15, group);
}
addBuilding(-23, -23, 11, 10, 10.5, 0xd69b78, 0x76594e, 0x4f9ca2);
addBuilding(23, -23, 13, 9, 7.2, 0x93b8af, 0x566b6d, 0xeaa65d);
addBuilding(-23, 23, 10, 12, 8.2, 0xc7b6d2, 0x685e72, 0xe16f78);
// Southeast corner park.
box(20.5, 0.13, 20.5, materials.grass, 22.5, 0.48, 22.5);
box(3.1, 0.06, 22, materials.path, 17.1, 0.565, 22.5);
box(22, 0.06, 3.1, materials.path, 22.5, 0.566, 22.5);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x63b8ca,
emissive: 0x1e7185,
emissiveIntensity: 0.25,
roughness: 0.22,
metalness: 0.05
});
box(5.2, 0.65, 5.2, materials.curb, 27, 0.78, 27);
box(4.1, 0.12, 4.1, waterMaterial, 27, 1.14, 27);
box(1.2, 1.7, 1.2, materials.curb, 27, 1.55, 27);
box(2.1, 0.25, 2.1, waterMaterial, 27, 2.45, 27);
function addTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0.45, z);
group.scale.setScalar(scale);
scene.add(group);
box(0.65, 3.1, 0.65, materials.trunk, 0, 1.55, 0, group);
box(2.7, 2.0, 2.7, materials.leafA, 0, 3.7, 0, group);
box(2.0, 1.65, 2.0, materials.leafB, -0.55, 4.9, 0.15, group);
box(1.75, 1.45, 1.75, materials.leafC, 0.7, 4.55, -0.35, group);
}
[
[-12, -17, 0.95], [-15.5, -32, 1.1], [-32, -12, 0.9],
[-32, -32, 1.15], [13, -17, 0.9], [31, -12, 1.1],
[31, -31, 0.85], [-12, 15, 0.9], [-31, 13, 1.1],
[-32, 32, 0.85], [14, 14, 0.8], [32, 15, 1.0],
[14, 31, 0.95], [30, 32, 1.15]
].forEach(([x, z, s]) => addTree(x, z, s));
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.45, z);
group.rotation.y = rotation;
scene.add(group);
const wood = new THREE.MeshStandardMaterial({
color: 0xa56e43,
roughness: 1
});
box(3.1, 0.25, 0.8, wood, 0, 0.75, 0, group);
box(3.1, 1.15, 0.22, wood, 0, 1.25, -0.42, group);
box(0.22, 0.75, 0.7, materials.darkMetal, -1.15, 0.35, 0, group);
box(0.22, 0.75, 0.7, materials.darkMetal, 1.15, 0.35, 0, group);
}
addBench(15.5, 25, Math.PI / 2);
addBench(20, 15.3, 0);
addBench(27.5, 17, -Math.PI / 2);
function addStreetlamp(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0.42, z);
group.rotation.y = rotation;
scene.add(group);
box(0.28, 5.7, 0.28, materials.pole, 0, 2.85, 0, group);
box(2.2, 0.22, 0.22, materials.pole, 0.9, 5.55, 0, group);
box(0.75, 0.32, 0.55, materials.darkMetal, 1.75, 5.38, 0, group);
box(0.6, 0.08, 0.4, materials.headlight, 1.75, 5.18, 0, group);
}
addStreetlamp(-10.4, -17, Math.PI);
addStreetlamp(10.4, 17, 0);
addStreetlamp(17, -10.4, -Math.PI / 2);
addStreetlamp(-17, 10.4, Math.PI / 2);
addStreetlamp(-10.4, 31, Math.PI);
addStreetlamp(10.4, -31, 0);
// Traffic signal factory.
const signalRed = 0xf05c5c;
const signalYellow = 0xffbd4a;
const signalGreen = 0x42d58b;
function makeLensMaterial(color) {
return new THREE.MeshStandardMaterial({
color: 0x172126,
emissive: color,
emissiveIntensity: 0.02,
roughness: 0.3
});
}
function setLens(material, on, color) {
material.color.setHex(on ? color : 0x172126);
material.emissive.setHex(color);
material.emissiveIntensity = on ? 2.4 : 0.02;
}
function createSignal(id, x, z, yaw) {
const group = new THREE.Group();
group.position.set(x, 0.42, z);
group.rotation.y = yaw;
scene.add(group);
box(0.28, 5.0, 0.28, materials.pole, 0, 2.5, 0, group);
box(1.05, 4.55, 0.62, materials.darkMetal, 0, 4.85, 0, group);
const red = makeLensMaterial(signalRed);
const amber = makeLensMaterial(signalYellow);
const green = makeLensMaterial(signalGreen);
const arrow = makeLensMaterial(signalGreen);
box(0.5, 0.5, 0.16, red, 0, 6.25, -0.37, group);
box(0.5, 0.5, 0.16, amber, 0, 5.35, -0.37, group);
box(0.5, 0.5, 0.16, green, 0, 4.45, -0.37, group);
box(0.5, 0.5, 0.16, arrow, 0, 3.55, -0.37, group);
box(0.68, 0.16, 0.45, materials.darkMetal, 0, 6.56, -0.43, group);
box(0.68, 0.16, 0.45, materials.darkMetal, 0, 5.66, -0.43, group);
box(0.68, 0.16, 0.45, materials.darkMetal, 0, 4.76, -0.43, group);
box(0.68, 0.16, 0.45, materials.darkMetal, 0, 3.86, -0.43, group);
return { group, red, amber, green, arrow };
}
const signals = {
N: createSignal("N", -7.1, -13.8, 0),
S: createSignal("S", 7.1, 13.8, Math.PI),
E: createSignal("E", 13.8, -7.1, -Math.PI / 2),
W: createSignal("W", -13.8, 7.1, Math.PI / 2)
};
// Road routes. Every route is one continuous Three.js CurvePath.
const y = 0.09;
const routeGuides = [];
const routeMap = {};
function makeRoute(approach, movement, start, turnStart, controls, turnEnd, final) {
const path = new THREE.CurvePath();
let turnCurve = null;
if (movement === "through") {
path.add(new THREE.LineCurve3(start, final));
} else {
path.add(new THREE.LineCurve3(start, turnStart));
turnCurve = new THREE.CubicBezierCurve3(
turnStart,
controls[0],
controls[1],
turnEnd
);
path.add(turnCurve);
path.add(new THREE.LineCurve3(turnEnd, final));
}
const route = {
approach,
movement,
path,
turnCurve,
length: path.getLength()
};
const color = movement === "through"
? 0x80d9eb
: movement === "left"
? 0xff78ca
: 0xffc857;
const points = path.getPoints(170).map(p => new THREE.Vector3(p.x, 0.12, p.z));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
transparent: true,
opacity: 0.78,
dashSize: 1.05,
gapSize: 0.65,
depthWrite: false
});
const guide = new THREE.Line(geometry, material);
guide.computeLineDistances();
guide.visible = false;
guide.renderOrder = 3;
scene.add(guide);
route.guide = guide;
routeGuides.push(route);
return route;
}
const routeDefinitions = {
N: {
start: new THREE.Vector3(-3, y, -35),
stop: new THREE.Vector3(-3, y, -13),
through: {
final: new THREE.Vector3(-3, y, 35)
},
left: {
turnStart: new THREE.Vector3(-3, y, -9),
controls: [
new THREE.Vector3(-3, y, -1),
new THREE.Vector3(7, y, 3)
],
turnEnd: new THREE.Vector3(13, y, 3),
final: new THREE.Vector3(35, y, 3)
},
right: {
turnStart: new THREE.Vector3(-3, y, -9),
controls: [
new THREE.Vector3(-3, y, -5),
new THREE.Vector3(-7, y, -3)
],
turnEnd: new THREE.Vector3(-13, y, -3),
final: new THREE.Vector3(-35, y, -3)
}
},
S: {
start: new THREE.Vector3(3, y, 35),
stop: new THREE.Vector3(3, y, 13),
through: {
final: new THREE.Vector3(3, y, -35)
},
left: {
turnStart: new THREE.Vector3(3, y, 9),
controls: [
new THREE.Vector3(3, y, 1),
new THREE.Vector3(-7, y, -3)
],
turnEnd: new THREE.Vector3(-13, y, -3),
final: new THREE.Vector3(-35, y, -3)
},
right: {
turnStart: new THREE.Vector3(3, y, 9),
controls: [
new THREE.Vector3(3, y, 5),
new THREE.Vector3(7, y, 3)
],
turnEnd: new THREE.Vector3(13, y, 3),
final: new THREE.Vector3(35, y, 3)
}
},
E: {
start: new THREE.Vector3(35, y, -3),
stop: new THREE.Vector3(13, y, -3),
through: {
final: new THREE.Vector3(-35, y, -3)
},
left: {
turnStart: new THREE.Vector3(9, y, -3),
controls: [
new THREE.Vector3(4, y, -3),
new THREE.Vector3(-3, y, 6)
],
turnEnd: new THREE.Vector3(-3, y, 13),
final: new THREE.Vector3(-3, y, 35)
},
right: {
turnStart: new THREE.Vector3(9, y, -3),
controls: [
new THREE.Vector3(4, y, -3),
new THREE.Vector3(3, y, -8)
],
turnEnd: new THREE.Vector3(3, y, -13),
final: new THREE.Vector3(3, y, -35)
}
},
W: {
start: new THREE.Vector3(-35, y, 3),
stop: new THREE.Vector3(-13, y, 3),
through: {
final: new THREE.Vector3(35, y, 3)
},
left: {
turnStart: new THREE.Vector3(-9, y, 3),
controls: [
new THREE.Vector3(-4, y, 3),
new THREE.Vector3(3, y, -8)
],
turnEnd: new THREE.Vector3(3, y, -13),
final: new THREE.Vector3(3, y, -35)
},
right: {
turnStart: new THREE.Vector3(-9, y, 3),
controls: [
new THREE.Vector3(-4, y, 3),
new THREE.Vector3(-3, y, 8)
],
turnEnd: new THREE.Vector3(-3, y, 13),
final: new THREE.Vector3(-3, y, 35)
}
}
};
for (const [id, definition] of Object.entries(routeDefinitions)) {
routeMap[id] = {
through: makeRoute(
id,
"through",
definition.start,
null,
null,
null,
definition.through.final
),
left: makeRoute(
id,
"left",
definition.start,
definition.left.turnStart,
definition.left.controls,
definition.left.turnEnd,
definition.left.final
),
right: makeRoute(
id,
"right",
definition.start,
definition.right.turnStart,
definition.right.controls,
definition.right.turnEnd,
definition.right.final
)
};
}
function createVehicle(color) {
const group = new THREE.Group();
const bodyMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.48,
metalness: 0.12
});
box(1.85, 0.65, 3.55, bodyMaterial, 0, 0.82, 0, group);
box(1.55, 0.65, 1.95, materials.glass, 0, 1.42, -0.22, group);
box(1.62, 0.16, 1.55, bodyMaterial, 0, 1.78, -0.25, group);
box(0.38, 0.55, 0.72, materials.tire, -0.96, 0.48, -1.1, group);
box(0.38, 0.55, 0.72, materials.tire, 0.96, 0.48, -1.1, group);
box(0.38, 0.55, 0.72, materials.tire, -0.96, 0.48, 1.1, group);
box(0.38, 0.55, 0.72, materials.tire, 0.96, 0.48, 1.1, group);
box(0.38, 0.22, 0.12, materials.headlight, -0.58, 0.91, 1.81, group);
box(0.38, 0.22, 0.12, materials.headlight, 0.58, 0.91, 1.81, group);
box(0.34, 0.18, 0.12, materials.taillight, -0.62, 0.91, -1.81, group);
box(0.34, 0.18, 0.12, materials.taillight, 0.62, 0.91, -1.81, group);
return group;
}
const vehicleColors = {
N: 0xe56f58,
S: 0xe8b94f,
E: 0x56b99a,
W: 0x598ec5
};
const approaches = {};
for (const id of ["N", "S", "E", "W"]) {
const definition = routeDefinitions[id];
const vehicle = createVehicle(vehicleColors[id]);
scene.add(vehicle);
approaches[id] = {
id,
vehicle,
route: routeMap[id].through,
progress: 0,
speed: 0,
entered: false,
inZone: false,
stopDistance: definition.start.distanceTo(definition.stop)
};
}
// Pedestrians use the marked crosswalks during protected all-red intervals.
const pedestrians = [];
const pedestrianColors = [0xd86f74, 0x4b8fc2, 0xe2a24a, 0x6aa985];
function createPedestrian(route, offset, colorIndex, reverse = false) {
const group = new THREE.Group();
scene.add(group);
const shirt = new THREE.MeshStandardMaterial({
color: pedestrianColors[colorIndex],
roughness: 0.95
});
const pants = new THREE.MeshStandardMaterial({
color: 0x344b58,
roughness: 1
});
const skin = new THREE.MeshStandardMaterial({
color: 0xd7a27b,
roughness: 1
});
box(0.55, 0.78, 0.4, shirt, 0, 1.2, 0, group);
box(0.42, 0.42, 0.42, skin, 0, 1.82, 0, group);
const leftLeg = box(0.2, 0.65, 0.23, pants, -0.16, 0.48, 0, group);
const rightLeg = box(0.2, 0.65, 0.23, pants, 0.16, 0.48, 0, group);
pedestrians.push({
group,
route,
offset,
reverse,
leftLeg,
rightLeg,
phase: random() * Math.PI * 2
});
}
createPedestrian("N", -1.1, 0, false);
createPedestrian("N", 1.15, 1, true);
createPedestrian("S", -1.1, 2, true);
createPedestrian("S", 1.15, 3, false);
createPedestrian("E", -1.1, 1, false);
createPedestrian("E", 1.15, 2, true);
createPedestrian("W", -1.1, 0, true);
createPedestrian("W", 1.15, 3, false);
const phases = [
{
title: "North / South Through",
detail: "Opposing southbound and northbound lanes move together. All crossing traffic remains stopped.",
duration: 9,
grants: { N: "through", S: "through" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "North Protected Left",
detail: "Only the north approach receives a protected left arrow. Every other approach stays red.",
duration: 8,
grants: { N: "left" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "South Protected Left",
detail: "Only the south approach receives a protected left arrow. Every other approach stays red.",
duration: 8,
grants: { S: "left" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "Protected Right Turns",
detail: "All four right turns use separate corner paths and proceed simultaneously without crossing.",
duration: 9,
grants: { N: "right", S: "right", E: "right", W: "right" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "East / West Through",
detail: "Opposing westbound and eastbound lanes move together. All crossing traffic remains stopped.",
duration: 9,
grants: { E: "through", W: "through" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "East Protected Left",
detail: "Only the east approach receives a protected left arrow. Every other approach stays red.",
duration: 8,
grants: { E: "left" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
},
{
title: "West Protected Left",
detail: "Only the west approach receives a protected left arrow. Every other approach stays red.",
duration: 8,
grants: { W: "left" }
},
{
title: "All Red · Pedestrian Scramble",
detail: "The intersection is cleared before pedestrians use all four marked crosswalks.",
duration: 4.2,
grants: {},
allRed: true
}
];
let phaseIndex = 0;
let phaseElapsed = 0;
let paused = false;
function applyPhase() {
const phase = phases[phaseIndex];
for (const route of routeGuides) {
route.guide.visible = phase.grants[route.approach] === route.movement;
}
}
applyPhase();
function moveToward(value, target, maxDelta) {
if (value < target) return Math.min(value + maxDelta, target);
return Math.max(value - maxDelta, target);
}
function updateVehicles(dt) {
const phase = phases[phaseIndex];
for (const approach of Object.values(approaches)) {
const desiredMovement = phase.grants[approach.id] || null;
// A waiting vehicle may select its newly granted movement.
// All variants share the same approach line, so switching is continuous.
if (
desiredMovement &&
!approach.entered &&
approach.progress <= approach.stopDistance + 0.35 &&
approach.speed < 0.8 &&
approach.route.movement !== desiredMovement
) {
approach.route = routeMap[approach.id][desiredMovement];
approach.speed = 0;
}
if (approach.progress > approach.stopDistance + 0.55) {
approach.entered = true;
}
const granted = desiredMovement === approach.route.movement;
const target = granted || approach.entered
? approach.route.length
: approach.stopDistance;
const distance = target - approach.progress;
const targetSpeed = distance > 0.08
? distance < 2.3 ? 2.4 : 6.35
: 0;
const acceleration = targetSpeed > approach.speed ? 4.1 : 9.5;
approach.speed = moveToward(approach.speed, targetSpeed, acceleration * dt);
approach.progress += approach.speed * dt;
if (approach.progress >= approach.route.length - 0.03) {
approach.progress = 0;
approach.speed = 0;
approach.entered = false;
}
const routePoint = approach.route.path.getPoint(
Math.min(1, approach.progress / approach.route.length)
);
const tangent = approach.route.path.getTangent(
Math.min(1, approach.progress / approach.route.length)
);
approach.vehicle.position.set(routePoint.x, 0.43, routePoint.z);
approach.vehicle.rotation.y = Math.atan2(tangent.x, tangent.z);
approach.inZone =
approach.entered &&
Math.abs(routePoint.x) < 14 &&
Math.abs(routePoint.z) < 14;
}
}
function updatePedestrians() {
const phase = phases[phaseIndex];
const crossing = phase.allRed
? THREE.MathUtils.clamp((phaseElapsed - 0.35) / 3.15, 0, 1)
: 0;
for (const pedestrian of pedestrians) {
pedestrian.group.visible = phase.allRed;
if (!phase.allRed) continue;
let t = crossing;
if (pedestrian.reverse) t = 1 - t;
const start = -7.1;
const end = 7.1;
const along = THREE.MathUtils.lerp(start, end, t);
if (pedestrian.route === "N" || pedestrian.route === "S") {
const z = pedestrian.route === "N" ? -11.6 : 11.6;
pedestrian.group.position.set(along, 0.43, z + pedestrian.offset);
pedestrian.group.rotation.y = pedestrian.reverse ? Math.PI / 2 : -Math.PI / 2;
} else {
const x = pedestrian.route === "E" ? 11.6 : -11.6;
pedestrian.group.position.set(x + pedestrian.offset, 0.43, along);
pedestrian.group.rotation.y = pedestrian.reverse ? Math.PI : 0;
}
const stride = Math.sin(phaseElapsed * 11 + pedestrian.phase) * 0.45;
pedestrian.leftLeg.rotation.x = stride;
pedestrian.rightLeg.rotation.x = -stride;
pedestrian.group.position.y += Math.abs(Math.cos(stride)) * 0.035;
}
}
function updateSignals() {
const phase = phases[phaseIndex];
const amberEnding = !phase.allRed && phaseElapsed > phase.duration - 1.2;
const amberClearance = phase.allRed && phaseElapsed < 0.65;
for (const id of ["N", "S", "E", "W"]) {
const signal = signals[id];
const movement = phase.grants[id] || null;
const greenOn = Boolean(movement) && !amberEnding;
const arrowOn = greenOn && (movement === "left" || movement === "right");
const amberOn = Boolean(movement)
? amberEnding
: amberClearance;
setLens(signal.red, !movement && !amberOn, signalRed);
setLens(signal.amber, amberOn, signalYellow);
setLens(signal.green, greenOn && movement === "through", signalGreen);
setLens(signal.arrow, arrowOn, signalGreen);
const element = signalElements[id];
element.className = "signal-chip ";
if (amberOn) {
element.classList.add("amber");
element.querySelector("small").textContent = "AMBER";
} else if (movement) {
element.classList.add("go");
element.querySelector("small").textContent =
movement === "through" ? "THRU" : movement === "left" ? "LEFT" : "RIGHT";
} else {
element.classList.add("stop");
element.querySelector("small").textContent = "STOP";
}
}
}
function updatePhaseUI() {
const phase = phases[phaseIndex];
const remaining = Math.max(0, phase.duration - phaseElapsed);
const clearing = !phase.allRed && remaining <= 0;
phaseNumber.textContent =
`Phase ${String(phaseIndex + 1).padStart(2, "0")} / ${String(phases.length).padStart(2, "0")}`;
phaseTitle.textContent = phase.title;
phaseDetail.textContent = phase.detail;
countdown.textContent = clearing ? "CLEAR" : `${Math.ceil(remaining)}s`;
timelineFill.style.width =
`${THREE.MathUtils.clamp(phaseElapsed / phase.duration, 0, 1) * 100}%`;
}
function advancePhaseIfSafe() {
const phase = phases[phaseIndex];
if (phaseElapsed < phase.duration) return;
if (!phase.allRed) {
// Do not start the clearance interval while any vehicle still occupies
// the marked intersection or crosswalk conflict zone.
const occupied = Object.values(approaches).some(a => a.inZone);
if (occupied) return;
}
phaseIndex = (phaseIndex + 1) % phases.length;
phaseElapsed = 0;
applyPhase();
}
// Direct camera controls, implemented without addons.
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.005, 0.22, 1.25);
updateCamera();
});
canvas.addEventListener("pointerup", event => {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
48,
112
);
updateCamera();
}, { passive: false });
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
topButton.addEventListener("click", () => {
cameraRadius = 94;
cameraTheta = 0;
cameraPhi = 0.06;
updateCamera();
});
resetButton.addEventListener("click", () => {
cameraRadius = 86;
cameraTheta = 0.76;
cameraPhi = 0.76;
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
phaseElapsed += dt;
updateVehicles(dt);
updatePedestrians();
updateSignals();
updatePhaseUI();
advancePhaseIfSafe();
}
renderer.render(scene, camera);
}
updateSignals();
updatePedestrians();
updatePhaseUI();
animate();
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 450);
});
</script>
</body>
</html>
```