Prompt specification
Build or customize the Three.js 3D scene "Intersection conflict visualization" 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: Intersection conflict visualization
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/intersection-conflict-visualization-x0424
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring pedestrian crossing signals. Create a simplified junction where users arrange signal phases; overlapping permitted paths are highlighted as conflicts, and the scene clearly labels itself an educational timing model. Low-poly.
Working Three.js Starter (intersection-conflict-visualization-x0424):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pedestrian Signal Lab — Educational Timing Model</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(15, 30, 36, 0.92);
--panel-border: rgba(255, 255, 255, 0.12);
--text: #edf7f4;
--muted: #9eb3b0;
--teal: #55e0bc;
--violet: #9f8cff;
--amber: #ffc857;
--danger: #ff5b55;
--safe: #55e0bc;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dbe7e5;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input,
select {
font: inherit;
}
button,
select,
input[type="range"] {
cursor: pointer;
}
button {
color: var(--text);
border: 1px solid rgba(255, 255, 255, 0.14);
background: rgba(255, 255, 255, 0.07);
border-radius: 8px;
padding: 7px 10px;
transition:
background 140ms ease,
border-color 140ms ease,
transform 140ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.13);
border-color: rgba(255, 255, 255, 0.28);
}
button:active {
transform: translateY(1px);
}
button.primary {
color: #06231d;
border-color: transparent;
background: var(--teal);
font-weight: 800;
}
button.danger {
color: #ffd7d5;
border-color: rgba(255, 91, 85, 0.3);
background: rgba(255, 91, 85, 0.12);
}
button.small {
padding: 5px 8px;
font-size: 12px;
}
select {
color: var(--text);
background: #20353b;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 7px;
padding: 5px 7px;
}
input[type="range"] {
width: 100%;
accent-color: var(--teal);
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--panel-border);
border-radius: 15px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(6, 20, 25, 0.25),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(14px);
}
#phasePanel {
top: 72px;
left: 16px;
width: 350px;
max-height: calc(100vh - 250px);
overflow: auto;
padding: 15px;
scrollbar-width: thin;
}
#reportPanel {
top: 16px;
right: 16px;
width: 302px;
padding: 15px;
}
#timelinePanel {
right: 334px;
bottom: 16px;
left: 382px;
min-height: 154px;
padding: 13px 15px 15px;
}
#topBadge {
position: fixed;
z-index: 12;
top: 17px;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
gap: 10px;
max-width: 470px;
padding: 10px 14px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 999px;
color: #eafff8;
background: rgba(15, 40, 44, 0.88);
box-shadow: 0 9px 30px rgba(8, 30, 35, 0.18);
backdrop-filter: blur(12px);
pointer-events: none;
text-align: center;
}
#topBadge .badge-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 16px var(--teal);
}
#topBadge strong {
display: block;
font-size: 12px;
letter-spacing: 0.12em;
}
#topBadge span {
display: block;
margin-top: 2px;
color: #b9ceca;
font-size: 11px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--teal);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1,
h2,
h3,
p {
margin-top: 0;
}
h1 {
margin-bottom: 5px;
font-size: 23px;
line-height: 1.05;
}
h2 {
margin-bottom: 8px;
font-size: 15px;
}
.subtle {
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.section-rule {
height: 1px;
margin: 13px 0;
background: rgba(255, 255, 255, 0.1);
}
.preset-row,
.add-row,
.toolbar,
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
}
.preset-row button {
flex: 1;
font-size: 11px;
}
#phaseList {
display: grid;
gap: 9px;
margin: 12px 0;
}
.phase-card {
position: relative;
padding: 11px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-left: 4px solid var(--teal);
border-radius: 11px;
background: rgba(255, 255, 255, 0.055);
transition:
border-color 140ms ease,
background 140ms ease,
opacity 140ms ease;
}
.phase-card.type-ns {
border-left-color: var(--violet);
}
.phase-card.type-all {
border-left-color: var(--amber);
}
.phase-card.drag-over {
border-color: var(--teal);
background: rgba(85, 224, 188, 0.1);
}
.phase-card.dragging {
opacity: 0.42;
}
.phase-head {
display: flex;
align-items: center;
gap: 8px;
}
.drag-handle {
color: #7f9694;
cursor: grab;
font-size: 17px;
letter-spacing: -3px;
user-select: none;
}
.drag-handle:active {
cursor: grabbing;
}
.phase-index {
display: grid;
width: 23px;
height: 23px;
flex: 0 0 auto;
place-items: center;
border-radius: 50%;
color: #09241f;
background: var(--teal);
font-size: 11px;
font-weight: 900;
}
.type-ns .phase-index {
color: #171027;
background: var(--violet);
}
.type-all .phase-index {
color: #302408;
background: var(--amber);
}
.phase-type {
min-width: 0;
flex: 1;
font-size: 12px;
font-weight: 750;
}
.phase-actions {
display: flex;
gap: 4px;
}
.phase-actions button {
width: 27px;
padding: 4px 0;
font-size: 12px;
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 9px;
color: #b9c9c7;
font-size: 11px;
}
.control-label output {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.duration-row {
display: grid;
grid-template-columns: 1fr 58px;
gap: 8px;
align-items: center;
margin-top: 5px;
}
.duration-row input[type="number"] {
width: 100%;
padding: 5px 6px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 7px;
color: var(--text);
background: #20353b;
}
.overlap-note {
margin-top: 7px;
padding: 6px 8px;
border-radius: 7px;
color: #bdd0cd;
background: rgba(255, 255, 255, 0.045);
font-size: 10px;
line-height: 1.35;
}
.overlap-note.active {
color: #ffd1cf;
background: rgba(255, 91, 85, 0.12);
}
.add-row button {
flex: 1;
padding: 8px 5px;
font-size: 11px;
}
.report-status {
padding: 12px;
border: 1px solid rgba(85, 224, 188, 0.2);
border-radius: 10px;
background: rgba(85, 224, 188, 0.08);
}
.report-status.danger {
border-color: rgba(255, 91, 85, 0.35);
background: rgba(255, 91, 85, 0.11);
}
.report-status strong {
display: block;
margin-bottom: 4px;
font-size: 14px;
}
.report-status span {
color: #c3d3d0;
font-size: 11px;
line-height: 1.4;
}
.conflict-list {
display: grid;
gap: 5px;
margin-top: 9px;
}
.conflict-item {
display: flex;
justify-content: space-between;
padding: 7px 8px;
border-radius: 7px;
color: #ffd5d3;
background: rgba(255, 91, 85, 0.09);
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.legend {
display: grid;
gap: 7px;
margin-top: 10px;
}
.legend-row {
display: flex;
align-items: center;
gap: 8px;
color: #c7d7d4;
font-size: 11px;
}
.swatch {
width: 18px;
height: 6px;
border-radius: 999px;
}
.swatch.ew {
background: var(--teal);
}
.swatch.ns {
background: var(--violet);
}
.swatch.conflict {
background: var(--danger);
box-shadow: 0 0 9px rgba(255, 91, 85, 0.65);
}
.timeline-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.timeline-top h2 {
margin: 0;
white-space: nowrap;
}
.timeline-controls {
display: flex;
align-items: center;
justify-content: flex-end;
gap: 8px;
flex: 1;
}
.state-pill {
max-width: 240px;
overflow: hidden;
padding: 6px 9px;
border: 1px solid rgba(85, 224, 188, 0.25);
border-radius: 999px;
color: #bff9e9;
background: rgba(85, 224, 188, 0.08);
font-size: 10px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.state-pill.conflict {
color: #ffe0de;
border-color: rgba(255, 91, 85, 0.4);
background: rgba(255, 91, 85, 0.14);
}
#clock {
min-width: 66px;
color: var(--text);
font-size: 13px;
font-variant-numeric: tabular-nums;
text-align: right;
}
.timeline-track {
position: relative;
height: 75px;
margin-top: 11px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 9px;
background:
linear-gradient(rgba(255, 255, 255, 0.025) 1px, transparent 1px) 0 0 / 100% 16px,
rgba(4, 18, 22, 0.45);
cursor: crosshair;
}
.phase-bar {
position: absolute;
display: flex;
align-items: center;
overflow: hidden;
height: 14px;
min-width: 2px;
padding: 0 5px;
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 4px;
color: #08251e;
background: var(--teal);
font-size: 9px;
font-weight: 900;
white-space: nowrap;
}
.phase-bar.ns {
color: #1c1234;
background: var(--violet);
}
.phase-bar.all {
color: #332505;
background: var(--amber);
}
.phase-bar.extension {
padding: 0;
border-style: dashed;
border-color: rgba(255, 255, 255, 0.32);
background:
repeating-linear-gradient(
135deg,
rgba(255, 91, 85, 0.85) 0 4px,
rgba(255, 91, 85, 0.35) 4px 8px
);
}
.conflict-strip {
position: absolute;
right: 0;
bottom: 2px;
left: 0;
height: 5px;
border-radius: 999px;
background: rgba(255, 255, 255, 0.05);
}
.conflict-segment {
position: absolute;
top: 0;
bottom: 0;
min-width: 2px;
border-radius: 999px;
background: var(--danger);
box-shadow: 0 0 9px rgba(255, 91, 85, 0.75);
}
#playhead {
position: absolute;
z-index: 5;
top: 0;
bottom: 0;
width: 2px;
transform: translateX(-1px);
background: white;
box-shadow: 0 0 8px rgba(255, 255, 255, 0.8);
pointer-events: none;
}
#scrubber {
margin: 8px 0 0;
}
.timeline-footer {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 5px;
color: #9fb2af;
font-size: 10px;
}
#cameraHint {
position: fixed;
z-index: 8;
right: 334px;
bottom: 179px;
padding: 6px 9px;
border-radius: 8px;
color: rgba(239, 250, 247, 0.9);
background: rgba(15, 30, 36, 0.72);
font-size: 10px;
pointer-events: none;
}
.note-box {
margin-top: 10px;
padding: 9px 10px;
border-left: 3px solid var(--amber);
border-radius: 0 8px 8px 0;
color: #c5d5d2;
background: rgba(255, 200, 87, 0.07);
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 1050px) {
#topBadge {
right: 334px;
left: 382px;
width: auto;
max-width: none;
transform: none;
}
#timelinePanel {
right: 16px;
left: 382px;
}
#cameraHint {
right: 16px;
}
}
@media (max-width: 760px) {
#topBadge {
top: 8px;
right: 8px;
left: 8px;
justify-content: center;
}
#topBadge span {
display: none;
}
#phasePanel {
top: 54px;
left: 8px;
width: min(330px, calc(100vw - 16px));
max-height: 46vh;
}
#reportPanel {
top: auto;
right: 8px;
bottom: 174px;
width: min(290px, calc(100vw - 16px));
}
#timelinePanel {
right: 8px;
bottom: 8px;
left: 8px;
min-height: 150px;
}
#cameraHint {
display: none;
}
.timeline-top {
align-items: flex-start;
flex-direction: column;
gap: 7px;
}
.timeline-controls {
width: 100%;
justify-content: flex-start;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="topBadge">
<div class="badge-dot"></div>
<div>
<strong>EDUCATIONAL TIMING MODEL</strong>
<span>Simplified pedestrian phase scheduler — not for traffic-engineering design</span>
</div>
</div>
<aside id="phasePanel" class="panel">
<p class="eyebrow">Phase builder</p>
<h1>Pedestrian Signal Lab</h1>
<p class="subtle">
Arrange pedestrian phases, extend a phase into the next, and inspect
simultaneous permitted paths.
</p>
<div class="preset-row">
<button id="safePreset">Safe plan</button>
<button id="conflictPreset">Conflict example</button>
</div>
<div class="section-rule"></div>
<div id="phaseList"></div>
<div class="add-row">
<button data-add="ew">+ East–West walk</button>
<button data-add="ns">+ North–South walk</button>
<button data-add="all">+ All-red</button>
</div>
<div class="note-box">
<strong>How overlap works:</strong> “Overlap next” extends a phase beyond
its nominal end. If East–West and North–South walks overlap, their
abstract permitted paths turn red and a conflict is reported.
All-red takes priority.
</div>
</aside>
<aside id="reportPanel" class="panel">
<p class="eyebrow">Conflict inspector</p>
<h2>Timing analysis</h2>
<div id="reportStatus" class="report-status">
<strong id="reportTitle">Checking model…</strong>
<span id="reportMessage"></span>
</div>
<div id="conflictList" class="conflict-list"></div>
<div class="legend">
<div class="legend-row">
<span class="swatch ew"></span>
East–West permitted path
</div>
<div class="legend-row">
<span class="swatch ns"></span>
North–South permitted path
</div>
<div class="legend-row">
<span class="swatch conflict"></span>
Overlapping permitted paths
</div>
</div>
<div class="section-rule"></div>
<h2>Camera</h2>
<div class="toolbar">
<button id="resetCamera" class="small">Reset view</button>
<button id="zoomIn" class="small">Zoom +</button>
<button id="zoomOut" class="small">Zoom −</button>
</div>
<div class="note-box">
This scene abstracts pedestrian routes to two crossing bands so that
timing conflicts remain easy to see. It does not model vehicle phases,
demand, crossing distance, or code-required clearance.
</div>
</aside>
<section id="timelinePanel" class="panel">
<div class="timeline-top">
<h2>Cycle timeline</h2>
<div class="timeline-controls">
<button id="playButton" class="primary small">▶ Play</button>
<select id="speedSelect" aria-label="Playback speed">
<option value="0.5">0.5×</option>
<option value="1" selected>1×</option>
<option value="2">2×</option>
<option value="4">4×</option>
</select>
<div id="statePill" class="state-pill">Cycle boundary</div>
<div id="clock">00:00.0</div>
</div>
</div>
<div id="timelineTrack" class="timeline-track">
<div id="phaseBars"></div>
<div id="conflictBar" class="conflict-strip"></div>
<div id="playhead"></div>
</div>
<input
id="scrubber"
type="range"
min="0"
max="60"
step="0.1"
value="0"
aria-label="Timeline position"
/>
<div class="timeline-footer">
<span>Click the timeline or drag the scrubber to inspect a moment.</span>
<span id="cycleLength">Cycle: 40.0 s</span>
</div>
</section>
<div id="cameraHint">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom · Double-click to reset
</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 phaseList = document.getElementById("phaseList");
const phaseBars = document.getElementById("phaseBars");
const conflictBar = document.getElementById("conflictBar");
const playhead = document.getElementById("playhead");
const scrubber = document.getElementById("scrubber");
const timelineTrack = document.getElementById("timelineTrack");
const playButton = document.getElementById("playButton");
const speedSelect = document.getElementById("speedSelect");
const clock = document.getElementById("clock");
const statePill = document.getElementById("statePill");
const cycleLength = document.getElementById("cycleLength");
const reportStatus = document.getElementById("reportStatus");
const reportTitle = document.getElementById("reportTitle");
const reportMessage = document.getElementById("reportMessage");
const conflictList = document.getElementById("conflictList");
const typeMeta = {
ew: {
label: "East–West Walk",
short: "EW walk",
className: "ew"
},
ns: {
label: "North–South Walk",
short: "NS walk",
className: "ns"
},
all: {
label: "All-Red Clearance",
short: "All-red",
className: "all"
}
};
let phaseId = 4;
let phases = [
{ id: 1, type: "ew", duration: 18, overlap: 4 },
{ id: 2, type: "ns", duration: 18, overlap: 0 },
{ id: 3, type: "all", duration: 4, overlap: 0 }
];
let schedule = [];
let conflictIntervals = [];
let totalTime = 1;
let currentTime = 20;
let playing = false;
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdbe7e5);
scene.fog = new THREE.Fog(0xdbe7e5, 90, 190);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
300
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xe9ffff, 0x66766b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.2);
sun.position.set(-28, 48, 24);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -65;
sun.shadow.camera.right = 65;
sun.shadow.camera.top = 65;
sun.shadow.camera.bottom = -65;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 125;
sun.shadow.bias = -0.0004;
scene.add(sun);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0,
flatShading: true,
...options
});
}
function addBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
radiusTop,
radiusBottom,
height,
material,
x = 0,
y = 0,
z = 0,
segments = 8,
parent = scene
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
segments
),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Simplified low-poly junction
// -------------------------------------------------------------------------
const groundMaterial = standardMaterial(0x91a98d);
const grassMaterial = standardMaterial(0x8fae83);
const roadMaterial = standardMaterial(0x39474b);
const curbMaterial = standardMaterial(0xc4ccc7);
const sidewalkMaterial = standardMaterial(0xaebbb5);
const stripeMaterial = standardMaterial(0xf3f4e9);
const yellowMaterial = standardMaterial(0xe4bb52);
const trunkMaterial = standardMaterial(0x76533d);
const leafMaterial = standardMaterial(0x4f8062);
const leafMaterial2 = standardMaterial(0x6d976a);
addBox(180, 0.3, 180, grassMaterial, 0, -0.55, 0);
// Two perpendicular roads.
addBox(180, 0.22, 18, roadMaterial, 0, -0.13, 0);
addBox(18, 0.22, 180, roadMaterial, 0, -0.125, 0);
// Four raised sidewalk corner blocks.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(
40,
0.55,
40,
sidewalkMaterial,
sx * 29,
0.02,
sz * 29
);
// A curb edge around each block.
addBox(40, 0.28, 0.5, curbMaterial, sx * 29, 0.12, sz * 9.35);
addBox(0.5, 0.28, 40, curbMaterial, sx * 9.35, 0.12, sz * 29);
}
}
// Lane center dashes, leaving the junction clear.
for (let x = -76; x <= 76; x += 8) {
if (Math.abs(x) > 13) {
addBox(3.8, 0.025, 0.17, yellowMaterial, x, 0.01, 0);
}
}
for (let z = -76; z <= 76; z += 8) {
if (Math.abs(z) > 13) {
addBox(0.17, 0.025, 3.8, yellowMaterial, 0, 0.012, z);
}
}
// Zebra crossings on all four sides.
for (const z of [-8.5, 8.5]) {
for (let x = -6.7; x <= 6.8; x += 1.2) {
addBox(0.62, 0.035, 2.8, stripeMaterial, x, 0.02, z);
}
}
for (const x of [-8.5, 8.5]) {
for (let z = -6.7; z <= 6.8; z += 1.2) {
addBox(2.8, 0.035, 0.62, stripeMaterial, x, 0.023, z);
}
}
// Stop lines.
addBox(6.8, 0.03, 0.24, stripeMaterial, 11.5, 0.025, -6.4);
addBox(6.8, 0.03, 0.24, stripeMaterial, -11.5, 0.025, 6.4);
addBox(0.24, 0.03, 6.8, stripeMaterial, -6.4, 0.026, 11.5);
addBox(0.24, 0.03, 6.8, stripeMaterial, 6.4, 0.027, -11.5);
let randomSeed = 19;
function random() {
randomSeed = (randomSeed * 16807) % 2147483647;
return (randomSeed - 1) / 2147483646;
}
function addBuilding(x, z, width, depth, height, color) {
const material = standardMaterial(color);
const windowMaterial = standardMaterial(0x9bc6c4, {
emissive: 0x224b4a,
emissiveIntensity: 0.25
});
addBox(width, height, depth, material, x, height / 2 + 0.35, z);
addBox(
width + 0.5,
0.35,
depth + 0.5,
standardMaterial(0x536a69),
x,
height + 0.5,
z
);
const levels = Math.max(2, Math.floor(height / 3.2));
const columns = Math.max(2, Math.floor(width / 3));
for (let level = 0; level < levels; level++) {
for (let col = 0; col < columns; col++) {
const wx = x - width / 2 + 1.6 + col * ((width - 3.2) / Math.max(1, columns - 1));
const wy = 2 + level * 2.6;
addBox(1.05, 1.15, 0.08, windowMaterial, wx, wy, z + depth / 2 + 0.045);
addBox(0.08, 1.15, 1.05, windowMaterial, x + width / 2 + 0.045, wy, wx);
}
}
}
addBuilding(-28, -29, 13, 12, 11, 0xc98970);
addBuilding(29, -29, 14, 11, 14, 0x7899a0);
addBuilding(-29, 29, 12, 14, 15, 0xb6a373);
addBuilding(29, 29, 15, 13, 10, 0x809878);
function addTree(x, z, scale = 1) {
addCylinder(
0.28 * scale,
0.38 * scale,
3.1 * scale,
trunkMaterial,
x,
1.65 * scale,
z,
7
);
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(2.15 * scale, 0),
random() > 0.5 ? leafMaterial : leafMaterial2
);
crown.position.set(x, 4.0 * scale, z);
crown.scale.y = 1.2;
crown.castShadow = true;
scene.add(crown);
}
[
[-18, -42, 1.1],
[-40, -17, 0.9],
[18, -42, 1.0],
[42, -18, 1.15],
[-18, 42, 1.05],
[-42, 18, 1.0],
[18, 42, 0.9],
[42, 18, 1.15]
].forEach(([x, z, scale]) => addTree(x, z, scale));
function addBench(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
addBox(3.5, 0.18, 0.8, standardMaterial(0x8d6747), 0, 0.75, 0, group);
addBox(3.5, 0.75, 0.16, standardMaterial(0x8d6747), 0, 1.25, -0.32, group);
addBox(0.16, 0.7, 0.7, standardMaterial(0x39484b), -1.3, 0.35, 0, group);
addBox(0.16, 0.7, 0.7, standardMaterial(0x39484b), 1.3, 0.35, 0, group);
scene.add(group);
}
addBench(-17, 14, Math.PI);
addBench(17, -14, 0);
addBench(14, 17, Math.PI / 2);
addBench(-14, -17, -Math.PI / 2);
function addCar(x, z, color, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
const bodyMaterial = standardMaterial(color);
const glassMaterial = standardMaterial(0x48656b, {
roughness: 0.35,
metalness: 0.15
});
addBox(4.1, 0.75, 1.75, bodyMaterial, 0, 0.65, 0, group);
addBox(2.15, 0.7, 1.5, glassMaterial, -0.15, 1.25, 0, group);
for (const wx of [-1.25, 1.25]) {
for (const wz of [-0.9, 0.9]) {
const wheel = addCylinder(
0.38,
0.38,
0.22,
standardMaterial(0x202729),
wx,
0.38,
wz,
10,
group
);
wheel.rotation.x = Math.PI / 2;
}
}
scene.add(group);
}
addCar(34, -4.5, 0xe6b458);
addCar(-38, 4.5, 0x7ca6c3, Math.PI);
addCar(4.5, 38, 0xcc7a70, Math.PI / 2);
addCar(-4.5, -36, 0x83a783, -Math.PI / 2);
// -------------------------------------------------------------------------
// Abstract permitted-path overlays
// -------------------------------------------------------------------------
const ewPathMaterial = new THREE.MeshBasicMaterial({
color: 0x55e0bc,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const nsPathMaterial = new THREE.MeshBasicMaterial({
color: 0x9f8cff,
transparent: true,
opacity: 0.1,
depthWrite: false
});
const ewPath = new THREE.Mesh(
new THREE.BoxGeometry(34, 0.035, 2.25),
ewPathMaterial
);
ewPath.position.y = 0.075;
ewPath.renderOrder = 2;
scene.add(ewPath);
const nsPath = new THREE.Mesh(
new THREE.BoxGeometry(2.25, 0.035, 34),
nsPathMaterial
);
nsPath.position.y = 0.078;
nsPath.renderOrder = 2;
scene.add(nsPath);
function addPathArrow(material, x, z, axis) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.62, 1.65, 3),
material
);
arrow.position.set(x, 0.13, z);
if (axis === "ew") {
arrow.rotation.z = -Math.PI / 2;
} else {
arrow.rotation.x = Math.PI / 2;
}
arrow.renderOrder = 3;
scene.add(arrow);
}
for (const x of [-13, -7, 7, 13]) {
addPathArrow(ewPathMaterial, x, 0, "ew");
}
for (const z of [-13, -7, 7, 13]) {
addPathArrow(nsPathMaterial, 0, z, "ns");
}
const conflictRing = new THREE.Mesh(
new THREE.TorusGeometry(4.1, 0.2, 6, 36),
new THREE.MeshBasicMaterial({
color: 0xff4d47,
transparent: true,
opacity: 0.95,
depthWrite: false
})
);
conflictRing.rotation.x = Math.PI / 2;
conflictRing.position.y = 0.18;
conflictRing.renderOrder = 5;
conflictRing.visible = false;
scene.add(conflictRing);
// -------------------------------------------------------------------------
// Low-poly pedestrian signal heads
// -------------------------------------------------------------------------
const signals = [];
const signalPostMaterial = standardMaterial(0x27383c, {
roughness: 0.6
});
const signalCaseMaterial = standardMaterial(0x1b282c, {
roughness: 0.5
});
const redPersonMaterial = standardMaterial(0xff514b, {
emissive: 0x7a100b,
emissiveIntensity: 0.55
});
const greenPersonMaterial = standardMaterial(0x62ff9c, {
emissive: 0x159a49,
emissiveIntensity: 0.8
});
function makeStandingPerson() {
const group = new THREE.Group();
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.2, 7, 5),
redPersonMaterial
);
head.position.y = 0.75;
group.add(head);
addBox(0.34, 0.55, 0.15, redPersonMaterial, 0, 0.34, 0, group);
addBox(0.13, 0.55, 0.15, redPersonMaterial, -0.11, -0.18, 0, group);
addBox(0.13, 0.55, 0.15, redPersonMaterial, 0.11, -0.18, 0, group);
return group;
}
function makeWalkingPerson() {
const group = new THREE.Group();
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.2, 7, 5),
greenPersonMaterial
);
head.position.set(0.08, 0.77, 0);
group.add(head);
const torso = addBox(
0.34,
0.55,
0.15,
greenPersonMaterial,
0,
0.35,
0,
group
);
torso.rotation.z = -0.12;
const armA = addBox(
0.12,
0.5,
0.15,
greenPersonMaterial,
-0.26,
0.36,
0,
group
);
armA.rotation.z = -0.58;
const armB = addBox(
0.12,
0.5,
0.15,
greenPersonMaterial,
0.25,
0.42,
0,
group
);
armB.rotation.z = 0.7;
const legA = addBox(
0.13,
0.58,
0.15,
greenPersonMaterial,
-0.18,
-0.2,
0,
group
);
legA.rotation.z = 0.55;
const legB = addBox(
0.13,
0.58,
0.15,
greenPersonMaterial,
0.2,
-0.2,
0,
group
);
legB.rotation.z = -0.45;
return group;
}
function createSignal(x, z, axis) {
const group = new THREE.Group();
group.position.set(x, 0, z);
// Turn the local +Z face toward the junction.
group.rotation.y = Math.atan2(-x, -z);
addCylinder(
0.12,
0.16,
3.5,
signalPostMaterial,
0,
1.75,
0,
8,
group
);
addBox(1.45, 2.35, 0.68, signalCaseMaterial, 0, 4.25, 0, group);
addBox(1.7, 0.18, 0.85, signalCaseMaterial, 0, 5.5, 0, group);
const red = makeStandingPerson();
red.position.set(0, 4.77, 0.39);
group.add(red);
const green = makeWalkingPerson();
green.position.set(0, 3.7, 0.39);
group.add(green);
green.visible = false;
scene.add(group);
signals.push({ axis, red, green });
}
createSignal(-10.3, -10.3, "ew");
createSignal(-10.3, 10.3, "ew");
createSignal(10.3, -10.3, "ns");
createSignal(10.3, 10.3, "ns");
// -------------------------------------------------------------------------
// Canvas-based world labels
// -------------------------------------------------------------------------
function makeTextSprite(text, background = "#153238", foreground = "#effff9") {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 256;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = "rgba(255,255,255,0.22)";
context.lineWidth = 8;
context.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
const lines = text.split("\n");
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = foreground;
if (lines.length === 1) {
context.font = "800 52px system-ui, sans-serif";
context.fillText(lines[0], canvas.width / 2, canvas.height / 2);
} else {
context.font = "900 42px system-ui, sans-serif";
context.fillText(lines[0], canvas.width / 2, 94);
context.font = "600 30px system-ui, sans-serif";
context.fillText(lines[1], canvas.width / 2, 168);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
const educationSign = makeTextSprite(
"EDUCATIONAL TIMING MODEL\nAbstract pedestrian paths — not for design",
"#153238",
"#eafff7"
);
educationSign.position.set(-27, 15.5, 24);
educationSign.scale.set(24, 6, 1);
scene.add(educationSign);
const conflictSign = makeTextSprite(
"CONCURRENT WALK CONFLICT",
"#721f1d",
"#fff1ef"
);
conflictSign.position.set(0, 6.4, 0);
conflictSign.scale.set(13.5, 3.4, 1);
conflictSign.visible = false;
scene.add(conflictSign);
// -------------------------------------------------------------------------
// Direct camera controls, implemented without Three.js add-ons
// -------------------------------------------------------------------------
class CameraController {
constructor(camera, element) {
this.camera = camera;
this.element = element;
this.target = new THREE.Vector3(0, 0.8, 0);
this.radius = 66;
this.theta = 0.76;
this.phi = 0.88;
this.minRadius = 28;
this.maxRadius = 110;
this.minPhi = 0.22;
this.maxPhi = 1.36;
this.dragging = false;
this.mode = "orbit";
this.lastX = 0;
this.lastY = 0;
this.right = new THREE.Vector3();
this.up = new THREE.Vector3();
this.onPointerDown = this.onPointerDown.bind(this);
this.onPointerMove = this.onPointerMove.bind(this);
this.onPointerUp = this.onPointerUp.bind(this);
this.onWheel = this.onWheel.bind(this);
element.addEventListener("pointerdown", this.onPointerDown);
element.addEventListener("pointermove", this.onPointerMove);
element.addEventListener("pointerup", this.onPointerUp);
element.addEventListener("pointercancel", this.onPointerUp);
element.addEventListener("wheel", this.onWheel, { passive: false });
element.addEventListener("contextmenu", (event) => event.preventDefault());
element.addEventListener("dblclick", () => this.reset());
this.update();
}
onPointerDown(event) {
if (event.button !== 0 && event.button !== 2) return;
this.dragging = true;
this.mode = event.button === 2 || event.shiftKey ? "pan" : "orbit";
this.lastX = event.clientX;
this.lastY = event.clientY;
this.element.setPointerCapture(event.pointerId);
}
onPointerMove(event) {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.lastX = event.clientX;
this.lastY = event.clientY;
if (this.mode === "orbit") {
this.theta -= dx * 0.006;
this.phi = THREE.MathUtils.clamp(
this.phi + dy * 0.005,
this.minPhi,
this.maxPhi
);
} else {
this.camera.updateMatrixWorld();
this.right.setFromMatrixColumn(this.camera.matrix, 0);
this.up.setFromMatrixColumn(this.camera.matrix, 1);
const scale = this.radius * 0.0015;
this.target.addScaledVector(this.right, -dx * scale);
this.target.addScaledVector(this.up, dy * scale);
this.target.x = THREE.MathUtils.clamp(this.target.x, -45, 45);
this.target.z = THREE.MathUtils.clamp(this.target.z, -45, 45);
}
this.update();
}
onPointerUp(event) {
this.dragging = false;
if (this.element.hasPointerCapture(event.pointerId)) {
this.element.releasePointerCapture(event.pointerId);
}
}
onWheel(event) {
event.preventDefault();
this.radius = THREE.MathUtils.clamp(
this.radius * Math.exp(event.deltaY * 0.001),
this.minRadius,
this.maxRadius
);
this.update();
}
zoom(factor) {
this.radius = THREE.MathUtils.clamp(
this.radius * factor,
this.minRadius,
this.maxRadius
);
this.update();
}
reset() {
this.target.set(0, 0.8, 0);
this.radius = 66;
this.theta = 0.76;
this.phi = 0.88;
this.update();
}
update() {
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.target.x + this.radius * sinPhi * Math.sin(this.theta),
this.target.y + this.radius * Math.cos(this.phi),
this.target.z + this.radius * sinPhi * Math.cos(this.theta)
);
this.camera.lookAt(this.target);
}
}
const controls = new CameraController(camera, renderer.domElement);
// -------------------------------------------------------------------------
// Timing model
// -------------------------------------------------------------------------
function computeSchedule() {
let cursor = 0;
return phases.map((phase, index) => {
const start = cursor;
const nominalEnd = start + phase.duration;
const next = phases[index + 1];
// An overlap may extend only into the immediately following phase.
const overlap = next
? Math.min(phase.overlap, next.duration)
: 0;
cursor = nominalEnd;
return {
id: phase.id,
type: phase.type,
start,
nominalEnd,
end: nominalEnd + overlap,
duration: phase.duration,
overlap
};
});
}
function stateAt(time) {
const active = schedule.filter(
(item) => time >= item.start && time < item.end
);
const allRedActive = active.some((item) => item.type === "all");
const ewActive = active.some((item) => item.type === "ew");
const nsActive = active.some((item) => item.type === "ns");
// All-red overrides all other permissions.
const ew = !allRedActive && ewActive;
const ns = !allRedActive && nsActive;
return {
ew,
ns,
allRed: allRedActive,
conflict: ew && ns,
active
};
}
function computeConflictIntervals() {
const boundaries = new Set([0, totalTime]);
for (const item of schedule) {
boundaries.add(item.start);
boundaries.add(item.nominalEnd);
boundaries.add(item.end);
}
const sorted = [...boundaries]
.filter((value) => value >= 0 && value <= totalTime)
.sort((a, b) => a - b);
const intervals = [];
for (let index = 0; index < sorted.length - 1; index++) {
const start = sorted[index];
const end = sorted[index + 1];
if (end - start < 0.0001) continue;
const midpoint = (start + end) / 2;
if (!stateAt(midpoint).conflict) continue;
const previous = intervals[intervals.length - 1];
if (previous && Math.abs(previous.end - start) < 0.0001) {
previous.end = end;
} else {
intervals.push({ start, end });
}
}
return intervals;
}
function totalConflictDuration() {
return conflictIntervals.reduce(
(sum, interval) => sum + interval.end - interval.start,
0
);
}
function formatClock(time) {
const minutes = Math.floor(time / 60);
const seconds = time - minutes * 60;
return `${String(minutes).padStart(2, "0")}:${seconds
.toFixed(1)
.padStart(4, "0")}`;
}
function rebuildModel(options = {}) {
const { cards = true } = options;
schedule = computeSchedule();
totalTime = schedule.length
? schedule[schedule.length - 1].nominalEnd
: 1;
if (currentTime > totalTime) {
currentTime %= totalTime;
}
conflictIntervals = computeConflictIntervals();
if (cards) renderPhaseCards();
renderTimeline();
updateReport();
scrubber.max = totalTime;
scrubber.value = currentTime;
cycleLength.textContent = `Cycle: ${totalTime.toFixed(1)} s`;
}
// -------------------------------------------------------------------------
// UI rendering
// -------------------------------------------------------------------------
function renderPhaseCards() {
phaseList.innerHTML = "";
phases.forEach((phase, index) => {
const card = document.createElement("div");
card.className = `phase-card type-${phase.type}`;
card.dataset.id = phase.id;
const isLast = index === phases.length - 1;
card.innerHTML = `
<div class="phase-head">
<div class="drag-handle" draggable="true" title="Drag to reorder">⋮⋮</div>
<div class="phase-index">${index + 1}</div>
<select class="phase-type" aria-label="Phase type">
<option value="ew" ${phase.type === "ew" ? "selected" : ""}>
East–West Walk
</option>
<option value="ns" ${phase.type === "ns" ? "selected" : ""}>
North–South Walk
</option>
<option value="all" ${phase.type === "all" ? "selected" : ""}>
All-Red Clearance
</option>
</select>
<div class="phase-actions">
<button data-action="up" title="Move up" ${index === 0 ? "disabled" : ""}>↑</button>
<button data-action="down" title="Move down" ${isLast ? "disabled" : ""}>↓</button>
<button data-action="duplicate" title="Duplicate">⧉</button>
<button data-action="delete" title="Delete" ${phases.length <= 1 ? "disabled" : ""}>×</button>
</div>
</div>
<div class="control-label">
<span>Phase duration</span>
<output class="duration-output">${phase.duration.toFixed(0)} s</output>
</div>
<div class="duration-row">
<input
class="duration-range"
type="range"
min="2"
max="40"
step="1"
value="${phase.duration}"
aria-label="Phase duration"
/>
<input
class="duration-number"
type="number"
min="2"
max="40"
step="1"
value="${phase.duration}"
aria-label="Phase duration in seconds"
/>
</div>
<div class="control-label">
<span>Overlap next</span>
<output class="overlap-output">${phase.overlap.toFixed(0)} s</output>
</div>
<input
class="overlap-range"
type="range"
min="0"
max="12"
step="1"
value="${phase.overlap}"
${isLast ? "disabled" : ""}
aria-label="Overlap into the next phase"
/>
<div class="overlap-note ${phase.overlap > 0 && !isLast ? "active" : ""}">
${
isLast
? "The final phase cannot overlap a following phase."
: phase.overlap > 0
? `This phase extends ${phase.overlap.toFixed(
0
)} s into the next phase.`
: "This phase ends before the next phase begins."
}
</div>
`;
phaseList.appendChild(card);
const typeSelect = card.querySelector(".phase-type");
const durationRange = card.querySelector(".duration-range");
const durationNumber = card.querySelector(".duration-number");
const durationOutput = card.querySelector(".duration-output");
const overlapRange = card.querySelector(".overlap-range");
const overlapOutput = card.querySelector(".overlap-output");
const overlapNote = card.querySelector(".overlap-note");
const dragHandle = card.querySelector(".drag-handle");
typeSelect.addEventListener("change", () => {
phase.type = typeSelect.value;
rebuildModel();
});
durationRange.addEventListener("input", () => {
phase.duration = Number(durationRange.value);
durationNumber.value = phase.duration;
durationOutput.textContent = `${phase.duration.toFixed(0)} s`;
rebuildModel({ cards: false });
});
durationNumber.addEventListener("change", () => {
phase.duration = THREE.MathUtils.clamp(
Number(durationNumber.value) || 2,
2,
40
);
durationNumber.value = phase.duration;
durationOutput.textContent = `${phase.duration.toFixed(0)} s`;
durationRange.value = phase.duration;
rebuildModel({ cards: false });
});
overlapRange.addEventListener("input", () => {
phase.overlap = Number(overlapRange.value);
overlapOutput.textContent = `${phase.overlap.toFixed(0)} s`;
overlapNote.textContent = `This phase extends ${phase.overlap.toFixed(
0
)} s into the next phase.`;
overlapNote.classList.toggle("active", phase.overlap > 0);
rebuildModel({ cards: false });
});
card.querySelectorAll("[data-action]").forEach((button) => {
button.addEventListener("click", () => {
const action = button.dataset.action;
if (action === "up" && index > 0) {
[phases[index - 1], phases[index]] = [
phases[index],
phases[index - 1]
];
}
if (action === "down" && index < phases.length - 1) {
[phases[index + 1], phases[index]] = [
phases[index],
phases[index + 1]
];
}
if (action === "duplicate") {
phases.splice(index + 1, 0, {
...phase,
id: phaseId++
});
}
if (action === "delete" && phases.length > 1) {
phases.splice(index, 1);
}
rebuildModel();
});
});
dragHandle.addEventListener("dragstart", (event) => {
event.dataTransfer.setData("text/plain", String(phase.id));
event.dataTransfer.effectAllowed = "move";
card.classList.add("dragging");
});
dragHandle.addEventListener("dragend", () => {
card.classList.remove("dragging");
phaseList.querySelectorAll(".drag-over").forEach((element) => {
element.classList.remove("drag-over");
});
});
card.addEventListener("dragover", (event) => {
event.preventDefault();
card.classList.add("drag-over");
});
card.addEventListener("dragleave", () => {
card.classList.remove("drag-over");
});
card.addEventListener("drop", (event) => {
event.preventDefault();
card.classList.remove("drag-over");
const sourceId = Number(event.dataTransfer.getData("text/plain"));
const sourceIndex = phases.findIndex((item) => item.id === sourceId);
const destinationIndex = phases.findIndex((item) => item.id === phase.id);
if (
sourceIndex < 0 ||
destinationIndex < 0 ||
sourceIndex === destinationIndex
) {
return;
}
const [moved] = phases.splice(sourceIndex, 1);
phases.splice(destinationIndex, 0, moved);
rebuildModel();
});
});
}
function renderTimeline() {
const rowTops = {
ew: 5,
ns: 24,
all: 43
};
phaseBars.innerHTML = "";
for (const item of schedule) {
const left = (item.start / totalTime) * 100;
const width = (item.duration / totalTime) * 100;
const top = rowTops[item.type];
const bar = document.createElement("div");
bar.className = `phase-bar ${item.type}`;
bar.style.left = `${left}%`;
bar.style.top = `${top}px`;
bar.style.width = `${width}%`;
bar.textContent = item.duration >= 5 ? typeMeta[item.type].short : "";
bar.title = `${typeMeta[item.type].label}: ${item.start.toFixed(
1
)}–${item.nominalEnd.toFixed(1)} s`;
phaseBars.appendChild(bar);
if (item.overlap > 0) {
const extension = document.createElement("div");
extension.className = "phase-bar extension";
extension.style.left = `${(item.nominalEnd / totalTime) * 100}%`;
extension.style.top = `${top}px`;
extension.style.width = `${(item.overlap / totalTime) * 100}%`;
extension.title = `Overlap extension: ${item.overlap.toFixed(1)} s`;
phaseBars.appendChild(extension);
}
}
conflictBar.innerHTML = "";
for (const interval of conflictIntervals) {
const segment = document.createElement("div");
segment.className = "conflict-segment";
segment.style.left = `${(interval.start / totalTime) * 100}%`;
segment.style.width = `${
((interval.end - interval.start) / totalTime) * 100
}%`;
conflictBar.appendChild(segment);
}
}
function updateReport() {
const conflictDuration = totalConflictDuration();
reportStatus.classList.toggle("danger", conflictDuration > 0);
conflictList.innerHTML = "";
if (conflictDuration > 0) {
reportTitle.textContent = `${conflictDuration.toFixed(1)} s of conflict`;
reportMessage.textContent =
"East–West and North–South walks are simultaneously permitted.";
for (const interval of conflictIntervals) {
const item = document.createElement("div");
item.className = "conflict-item";
item.innerHTML = `
<span>${interval.start.toFixed(1)}–${interval.end.toFixed(1)} s</span>
<strong>${(interval.end - interval.start).toFixed(1)} s</strong>
`;
conflictList.appendChild(item);
}
} else {
reportTitle.textContent = "No timing conflict";
reportMessage.textContent =
"The permitted crossing paths never overlap in this cycle.";
}
}
function updateSceneState() {
const state = stateAt(currentTime);
const ewColor = state.conflict
? 0xff4d47
: state.ew
? 0x55e0bc
: 0x7c9394;
const nsColor = state.conflict
? 0xff4d47
: state.ns
? 0x9f8cff
: 0x7c9394;
ewPathMaterial.color.setHex(ewColor);
nsPathMaterial.color.setHex(nsColor);
ewPathMaterial.opacity = state.conflict
? 0.68
: state.ew
? 0.43
: 0.075;
nsPathMaterial.opacity = state.conflict
? 0.68
: state.ns
? 0.43
: 0.075;
for (const signal of signals) {
const walking = signal.axis === "ew" ? state.ew : state.ns;
signal.red.visible = !walking;
signal.green.visible = walking;
}
conflictRing.visible = state.conflict;
conflictSign.visible = state.conflict;
statePill.classList.toggle("conflict", state.conflict);
if (state.conflict) {
statePill.textContent = "CONFLICT — EW + NS walk";
} else if (state.allRed) {
statePill.textContent = "All-red clearance";
} else if (state.ew) {
statePill.textContent = "East–West walk";
} else if (state.ns) {
statePill.textContent = "North–South walk";
} else {
statePill.textContent = "Cycle boundary";
}
clock.textContent = formatClock(currentTime);
scrubber.value = currentTime;
playhead.style.left = `${(currentTime / totalTime) * 100}%`;
}
// -------------------------------------------------------------------------
// Controls and presets
// -------------------------------------------------------------------------
document.querySelectorAll("[data-add]").forEach((button) => {
button.addEventListener("click", () => {
const type = button.dataset.add;
phases.push({
id: phaseId++,
type,
duration: type === "all" ? 4 : 18,
overlap: 0
});
rebuildModel();
});
});
document.getElementById("safePreset").addEventListener("click", () => {
phases = [
{ id: phaseId++, type: "ew", duration: 20, overlap: 0 },
{ id: phaseId++, type: "ns", duration: 20, overlap: 0 },
{ id: phaseId++, type: "all", duration: 4, overlap: 0 }
];
currentTime = 0;
playing = false;
playButton.textContent = "▶ Play";
rebuildModel();
});
document.getElementById("conflictPreset").addEventListener("click", () => {
phases = [
{ id: phaseId++, type: "ew", duration: 18, overlap: 4 },
{ id: phaseId++, type: "ns", duration: 18, overlap: 0 },
{ id: phaseId++, type: "all", duration: 4, overlap: 0 }
];
currentTime = 20;
playing = false;
playButton.textContent = "▶ Play";
rebuildModel();
});
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "❚❚ Pause" : "▶ Play";
});
scrubber.addEventListener("input", () => {
currentTime = Number(scrubber.value);
updateSceneState();
});
timelineTrack.addEventListener("pointerdown", (event) => {
const rect = timelineTrack.getBoundingClientRect();
const fraction = THREE.MathUtils.clamp(
(event.clientX - rect.left) / rect.width,
0,
1
);
currentTime = fraction * totalTime;
updateSceneState();
});
document.getElementById("resetCamera").addEventListener("click", () => {
controls.reset();
});
document.getElementById("zoomIn").addEventListener("click", () => {
controls.zoom(0.86);
});
document.getElementById("zoomOut").addEventListener("click", () => {
controls.zoom(1.16);
});
window.addEventListener("keydown", (event) => {
const tag = event.target.tagName;
if (["INPUT", "SELECT", "BUTTON"].includes(tag)) return;
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Animation loop
// -------------------------------------------------------------------------
const clock3D = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock3D.getDelta(), 0.1);
if (playing && totalTime > 0) {
currentTime += delta * Number(speedSelect.value);
if (currentTime >= totalTime) {
currentTime %= totalTime;
}
}
updateSceneState();
if (conflictRing.visible) {
const pulse = 1 + Math.sin(performance.now() * 0.009) * 0.12;
conflictRing.scale.setScalar(pulse);
conflictRing.material.opacity = 0.75 + Math.sin(performance.now() * 0.009) * 0.2;
}
renderer.render(scene, camera);
}
rebuildModel();
updateSceneState();
animate();
</script>
</body>
</html>
```