Prompt specification
Build or customize the Three.js 3D scene "Compact Town Roundabout 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: Compact Town Roundabout Scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/compact-town-roundabout-scene-x2135
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: compact town roundabout scene. Build an original small roundabout with four approaches and pedestrian islands. Use a bounded vehicle count and explicitly simplified gap acceptance rather than claiming realistic driver behavior.
Working Three.js Starter (compact-town-roundabout-scene-x2135):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bramblewick Roundabout</title>
<style>
:root {
color-scheme: light;
--ink: #18343b;
--panel: rgba(249, 247, 235, 0.92);
--line: rgba(24, 52, 59, 0.16);
--accent: #e66b4f;
--green: #39766c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8d9dc;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 12px 35px rgba(26, 52, 57, 0.13);
backdrop-filter: blur(10px);
}
.title-card {
top: 20px;
left: 20px;
width: min(340px, calc(100vw - 40px));
padding: 17px 19px;
border-radius: 16px;
}
.eyebrow {
margin: 0 0 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
font-size: 13px;
line-height: 1.45;
color: rgba(24, 52, 59, 0.72);
}
.model-card {
top: 20px;
right: 20px;
width: 285px;
padding: 15px 17px;
border-radius: 16px;
}
.model-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
font-size: 12px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.count {
padding: 4px 8px;
border-radius: 999px;
background: #dce9df;
color: #275d52;
font-size: 11px;
letter-spacing: 0.04em;
white-space: nowrap;
}
.model-row {
display: flex;
justify-content: space-between;
gap: 14px;
padding: 6px 0;
border-top: 1px solid var(--line);
font-size: 12px;
}
.model-row span:first-child {
color: rgba(24, 52, 59, 0.64);
}
.model-row strong {
text-align: right;
font-weight: 750;
}
.disclaimer {
margin: 9px 0 0;
padding-top: 9px;
border-top: 1px solid var(--line);
color: rgba(24, 52, 59, 0.7);
font-size: 11px;
line-height: 1.45;
}
.status {
margin-top: 10px;
min-height: 32px;
padding: 8px 10px;
border-radius: 9px;
background: rgba(57, 118, 108, 0.1);
color: #315f58;
font-size: 11px;
line-height: 1.35;
}
.legend {
left: 20px;
bottom: 20px;
display: flex;
flex-wrap: wrap;
gap: 13px;
max-width: calc(100vw - 40px);
padding: 11px 14px;
border-radius: 13px;
font-size: 11px;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
white-space: nowrap;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.controls {
right: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 7px;
padding: 9px;
border-radius: 13px;
}
button {
border: 0;
border-radius: 9px;
padding: 9px 11px;
background: #e8eee6;
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
transition:
background 120ms ease,
transform 120ms ease;
}
button:hover {
background: #dce8dc;
transform: translateY(-1px);
}
button.primary {
background: var(--green);
color: #fffdf2;
}
button.primary:hover {
background: #2f665e;
}
.hint {
padding: 0 4px;
color: rgba(24, 52, 59, 0.58);
font-size: 10px;
white-space: nowrap;
}
@media (max-width: 760px) {
.title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
.model-card {
top: auto;
right: 12px;
bottom: 72px;
width: 238px;
padding: 12px 13px;
}
.legend {
display: none;
}
.controls {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<section class="panel title-card">
<p class="eyebrow">Voxel Town Study 04</p>
<h1>Bramblewick Roundabout</h1>
<p class="subtitle">
A compact four-arm civic roundabout with a clock-tower park,
pedestrian splitter islands, and a deliberately tiny traffic system.
</p>
</section>
<section class="panel model-card">
<div class="model-title">
<span>Traffic model</span>
<span class="count" id="vehicleCount">10 / 10</span>
</div>
<div class="model-row">
<span>Vehicle pool</span>
<strong>10 max</strong>
</div>
<div class="model-row">
<span>Yield clearance</span>
<strong>Fixed 2.2 seconds</strong>
</div>
<div class="model-row">
<span>Ring movement</span>
<strong>Constant speed + spacing</strong>
</div>
<div class="model-row">
<span>Waiting queue</span>
<strong id="queueCount">6 vehicles</strong>
</div>
<p class="disclaimer">
Simplified gap acceptance only—not a realistic driver-behavior model.
Pedestrians are decorative scenery and do not affect traffic.
</p>
<div class="status" id="status">
Waiting for the first fixed-clearance gap…
</div>
</section>
<section class="panel legend">
<span class="legend-item">
<i class="swatch" style="background:#3c4e58"></i> roundabout
</span>
<span class="legend-item">
<i class="swatch" style="background:#71a969"></i> pedestrian island
</span>
<span class="legend-item">
<i class="swatch" style="background:#d8b979"></i> town paving
</span>
<span class="legend-item">
<i class="swatch" style="background:#f3edcf"></i> crossing
</span>
</section>
<nav class="panel controls" aria-label="Scene controls">
<button id="pauseButton" class="primary">Pause</button>
<button data-view="iso">Isometric</button>
<button data-view="top">Top view</button>
<span class="hint">Drag to orbit · wheel to zoom</span>
</nav>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Core setup
// ------------------------------------------------------------
const TAU = Math.PI * 2;
const MAX_VEHICLES = 10;
const NOMINAL_SPEED = 3.0;
const GAP_SECONDS = 2.2;
const CLEARANCE = NOMINAL_SPEED * GAP_SECONDS;
const RING_RADIUS = 4.9;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8d9dc);
scene.fog = new THREE.Fog(0xb8d9dc, 42, 88);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
150
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xeaf8f4, 0x65705c, 2.15);
scene.add(hemisphere);
const sunlight = new THREE.DirectionalLight(0xfff1cf, 3.2);
sunlight.position.set(-18, 32, 15);
sunlight.castShadow = true;
sunlight.shadow.mapSize.set(2048, 2048);
sunlight.shadow.camera.left = -30;
sunlight.shadow.camera.right = 30;
sunlight.shadow.camera.top = 30;
sunlight.shadow.camera.bottom = -30;
sunlight.shadow.camera.near = 1;
sunlight.shadow.camera.far = 80;
sunlight.shadow.bias = -0.00035;
scene.add(sunlight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function material(color) {
if (color && color.isMaterial) return color;
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.86,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function box(
parent,
width,
height,
depth,
color,
x = 0,
y = 0,
z = 0,
rotationY = 0,
castShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material(color));
mesh.scale.set(width, height, depth);
mesh.position.set(x, y, z);
mesh.rotation.y = rotationY;
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function seededRandom(seed) {
return function () {
seed |= 0;
seed = (seed + 0x6d2b79f5) | 0;
let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = seededRandom(4281);
const COLORS = {
grass: 0x79a96a,
grassLight: 0x91bd72,
grassDark: 0x4f8054,
road: 0x3c4e58,
roadEdge: 0x2e414b,
paving: 0xd8b979,
pavingLight: 0xe4ca91,
cream: 0xf3edcf,
white: 0xf8f4df,
stone: 0xb9b19a,
trunk: 0x70533c,
leaves: 0x4f8d55,
leavesLight: 0x6ca85c,
leavesDark: 0x397149,
glass: 0x355b68,
roof: 0x9f5147,
roofDark: 0x70443f,
skin: 0xe3b18d,
black: 0x263039
};
const lampMaterial = new THREE.MeshStandardMaterial({
color: 0xffe4a1,
emissive: 0xffbd57,
emissiveIntensity: 0.8,
roughness: 0.55
});
// ------------------------------------------------------------
// Road layout and town
// ------------------------------------------------------------
box(scene, 40, 0.7, 40, COLORS.grass, 0, -0.45, 0, 0, false);
// Cross-shaped approach roads.
box(scene, 36, 0.14, 6.2, COLORS.road, 0, -0.03, 0, 0, false);
box(scene, 6.2, 0.14, 36, COLORS.road, 0, -0.025, 0, 0, false);
// Four corner pedestrian plazas.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(
scene,
15,
0.18,
15,
COLORS.paving,
sx * 10.5,
0,
sz * 10.5,
0,
false
);
box(
scene,
14.65,
0.035,
14.65,
COLORS.pavingLight,
sx * 10.5,
0.105,
sz * 10.5,
0,
false
);
}
}
// Voxel roundabout ring.
const ringSegments = 64;
for (let i = 0; i < ringSegments; i++) {
const theta = (i / ringSegments) * TAU;
const radius = 5.05;
box(
scene,
0.62,
0.14,
3.9,
COLORS.road,
Math.cos(theta) * radius,
-0.025,
Math.sin(theta) * radius,
Math.PI / 2 - theta,
false
);
}
// Pixelated central park.
for (let ix = -3; ix <= 3; ix++) {
for (let iz = -3; iz <= 3; iz++) {
if (Math.hypot(ix, iz) <= 3.02) {
box(
scene,
0.98,
0.28,
0.98,
(ix + iz) % 2 === 0
? COLORS.grassLight
: COLORS.grass,
ix,
0.075,
iz,
0,
false
);
}
}
}
// Curbed central edge.
for (let i = 0; i < ringSegments; i++) {
const theta = (i / ringSegments) * TAU;
const radius = 3.12;
box(
scene,
0.68,
0.3,
0.3,
COLORS.pavingLight,
Math.cos(theta) * radius,
0.13,
Math.sin(theta) * radius,
Math.PI / 2 - theta,
false
);
}
// Dashed lane-edge markers.
for (let i = 0; i < ringSegments; i += 4) {
for (const radius of [3.43, 6.42]) {
const theta = (i / ringSegments) * TAU;
box(
scene,
0.42,
0.035,
0.1,
COLORS.white,
Math.cos(theta) * radius,
0.067,
Math.sin(theta) * radius,
Math.PI / 2 - theta,
false
);
}
}
function addPerson(x, z, shirtColor, baseY = 0.12) {
box(scene, 0.1, 0.24, 0.13, COLORS.black, x - 0.065, baseY + 0.15, z);
box(scene, 0.1, 0.24, 0.13, COLORS.black, x + 0.065, baseY + 0.15, z);
box(scene, 0.28, 0.42, 0.2, shirtColor, x, baseY + 0.43, z);
box(scene, 0.23, 0.23, 0.23, COLORS.skin, x, baseY + 0.76, z);
}
function addTree(x, z, scale = 1, baseY = 0.12) {
box(
scene,
0.28 * scale,
1.15 * scale,
0.28 * scale,
COLORS.trunk,
x,
baseY + 0.575 * scale,
z
);
box(
scene,
1.05 * scale,
0.78 * scale,
1.05 * scale,
COLORS.leaves,
x,
baseY + 1.35 * scale,
z
);
box(
scene,
0.76 * scale,
0.7 * scale,
0.76 * scale,
COLORS.leavesLight,
x - 0.1 * scale,
baseY + 1.83 * scale,
z + 0.04 * scale
);
box(
scene,
0.58 * scale,
0.48 * scale,
0.58 * scale,
COLORS.leavesDark,
x + 0.12 * scale,
baseY + 1.55 * scale,
z - 0.16 * scale
);
}
function addStreetLamp(x, z) {
box(scene, 0.12, 2.5, 0.12, COLORS.black, x, 1.3, z);
box(scene, 0.68, 0.12, 0.12, COLORS.black, x, 2.5, z);
box(scene, 0.35, 0.18, 0.35, lampMaterial, x + 0.28, 2.42, z);
}
function buildBuilding(x, z, width, depth, height, bodyColor, roofColor) {
box(
scene,
width + 0.35,
0.28,
depth + 0.35,
COLORS.stone,
x,
0.25,
z
);
box(
scene,
width,
height,
depth,
bodyColor,
x,
0.35 + height / 2,
z
);
box(
scene,
width + 0.3,
0.28,
depth + 0.3,
roofColor,
x,
height + 0.47,
z
);
box(
scene,
width * 0.7,
0.32,
depth * 0.7,
roofColor,
x,
height + 0.76,
z
);
box(
scene,
width * 0.34,
0.3,
depth * 0.34,
COLORS.roofDark,
x,
height + 1.02,
z
);
const levels = Math.max(1, Math.floor(height / 1.55));
for (let level = 0; level < levels; level++) {
const windowY = 1.12 + level * 1.42;
for (const side of [-1, 1]) {
for (const offset of [-0.23, 0.23]) {
box(
scene,
0.08,
0.55,
0.62,
COLORS.glass,
x + side * (width / 2 + 0.035),
windowY,
z + depth * offset
);
box(
scene,
0.62,
0.55,
0.08,
COLORS.glass,
x + width * offset,
windowY,
z + side * (depth / 2 + 0.035)
);
}
}
}
const awningColors = [0xe66b4f, 0x4c8e86, 0xd9aa45, 0x8c6c9c];
const awning = awningColors[Math.floor(random() * awningColors.length)];
const facingX = x < 0 ? 1 : -1;
const facingZ = z < 0 ? 1 : -1;
box(
scene,
0.36,
0.18,
depth * 0.72,
awning,
x + facingX * (width / 2 + 0.14),
0.98,
z
);
box(
scene,
width * 0.72,
0.18,
0.36,
awning,
x,
0.98,
z + facingZ * (depth / 2 + 0.14)
);
}
const buildingColors = [
0xe5c894,
0xc97961,
0x8eb5a5,
0xd5a850,
0xb8a2bb,
0x7898a7
];
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
buildBuilding(
sx * 10.1,
sz * 10.1,
4.5,
4.8,
3.4 + random() * 2.1,
buildingColors[Math.floor(random() * buildingColors.length)],
random() > 0.45 ? COLORS.roof : 0x5c6870
);
buildBuilding(
sx * 14.3,
sz * 6.7,
3.1,
3.8,
2.8 + random() * 1.7,
buildingColors[Math.floor(random() * buildingColors.length)],
random() > 0.5 ? COLORS.roof : 0x59656b
);
addTree(sx * 6.9, sz * 14.2, 0.9);
addTree(sx * 16.4, sz * 4.7, 0.78);
addTree(sx * 4.4, sz * 16.5, 0.83);
addStreetLamp(sx * 4.0, sz * 13.5);
addStreetLamp(sx * 15.8, sz * 3.9);
addPerson(sx * 8.0, sz * 11.4, 0x557e9d);
addPerson(sx * 12.0, sz * 13.7, 0xd36f55);
}
}
// Central clock tower.
box(scene, 2.05, 0.38, 2.05, COLORS.stone, 0, 0.4, 0);
box(scene, 1.25, 2.6, 1.25, 0xe1d4ad, 0, 1.88, 0);
box(scene, 1.55, 0.25, 1.55, COLORS.roofDark, 0, 3.3, 0);
box(scene, 1.12, 0.36, 1.12, COLORS.roof, 0, 3.58, 0);
box(scene, 0.62, 0.34, 0.62, COLORS.roofDark, 0, 3.9, 0);
for (const side of [-1, 1]) {
box(scene, 0.08, 0.62, 0.62, COLORS.glass, side * 0.645, 2.3, 0);
box(scene, 0.62, 0.62, 0.08, COLORS.glass, 0, 2.3, side * 0.645);
box(scene, 0.1, 0.06, 0.3, COLORS.cream, side * 0.7, 2.36, 0.06);
box(scene, 0.3, 0.06, 0.1, COLORS.cream, side * 0.7, 2.23, 0.06);
box(scene, 0.3, 0.06, 0.1, COLORS.cream, 0.06, 2.36, side * 0.7);
box(scene, 0.1, 0.06, 0.3, COLORS.cream, 0.06, 2.23, side * 0.7);
}
addTree(-1.8, -1.8, 0.84, 0.23);
addTree(1.8, 1.8, 0.84, 0.23);
addTree(-1.9, 1.65, 0.68, 0.23);
addTree(1.65, -1.65, 0.68, 0.23);
// ------------------------------------------------------------
// Four approaches, crossings, and pedestrian splitter islands
// ------------------------------------------------------------
const APPROACHES = [
{
name: "East",
out: new THREE.Vector3(1, 0, 0),
theta: 0
},
{
name: "North",
out: new THREE.Vector3(0, 0, 1),
theta: Math.PI / 2
},
{
name: "West",
out: new THREE.Vector3(-1, 0, 0),
theta: Math.PI
},
{
name: "South",
out: new THREE.Vector3(0, 0, -1),
theta: Math.PI * 1.5
}
];
function rightOf(direction) {
return new THREE.Vector3(
direction.z,
0,
-direction.x
);
}
function headingFromDirection(direction) {
return Math.atan2(-direction.z, direction.x);
}
for (const approach of APPROACHES) {
const { out } = approach;
const horizontal = Math.abs(out.x) > 0.5;
// Zebra crossing before each splitter island.
for (let stripe = 0; stripe < 5; stripe++) {
const radius = 7.35 + stripe * 0.42;
if (horizontal) {
box(
scene,
0.32,
0.035,
5.0,
COLORS.white,
out.x * radius,
0.066,
0,
0,
false
);
} else {
box(
scene,
5.0,
0.035,
0.32,
COLORS.white,
0,
0.066,
out.z * radius,
0,
false
);
}
}
const islandX = out.x * 11.2;
const islandZ = out.z * 11.2;
box(
scene,
horizontal ? 3.4 : 0.9,
0.28,
horizontal ? 0.9 : 3.4,
COLORS.pavingLight,
islandX,
0.13,
islandZ,
0,
false
);
box(
scene,
horizontal ? 3.08 : 0.58,
0.08,
horizontal ? 0.58 : 3.08,
COLORS.grassLight,
islandX,
0.3,
islandZ,
0,
false
);
const tangent = new THREE.Vector3(-out.z, 0, out.x);
addPerson(
islandX + tangent.x * 0.85,
islandZ + tangent.z * 0.85,
0xcf6f52,
0.34
);
addPerson(
islandX - tangent.x * 0.75,
islandZ - tangent.z * 0.75,
0x477b91,
0.34
);
// Voxel yield sign.
const signX = out.x * 9.55;
const signZ = out.z * 9.55;
box(scene, 0.12, 1.15, 0.12, COLORS.black, signX, 0.7, signZ);
box(scene, 0.62, 0.44, 0.18, 0xd95142, signX, 1.3, signZ);
box(scene, 0.34, 0.18, 0.2, COLORS.white, signX, 1.3, signZ);
// Simple directional arrow in the incoming lane.
const arrow = new THREE.Group();
const inward = out.clone().multiplyScalar(-1);
const right = rightOf(inward);
const arrowRadius = 14.6;
arrow.position.set(
out.x * arrowRadius + right.x * 1.45,
0.075,
out.z * arrowRadius + right.z * 1.45
);
arrow.rotation.y = headingFromDirection(inward);
box(arrow, 1.05, 0.035, 0.18, COLORS.white, -0.05, 0, 0, 0, false);
box(arrow, 0.42, 0.035, 0.18, COLORS.white, 0.42, 0, 0.2, 0, false);
box(arrow, 0.42, 0.035, 0.18, COLORS.white, 0.42, 0, -0.2, 0, false);
scene.add(arrow);
}
// ------------------------------------------------------------
// Voxel vehicles
// ------------------------------------------------------------
const vehicleColors = [
0xe66b4f,
0x4d8d92,
0xe4b74f,
0xf1ead7,
0x7a5e91,
0x78a66b,
0xd8895b,
0x526a83
];
function createVehicle(color) {
const group = new THREE.Group();
box(group, 1.55, 0.36, 0.78, color, 0, 0.38, 0);
box(group, 0.82, 0.34, 0.67, COLORS.glass, -0.16, 0.69, 0);
box(group, 0.86, 0.08, 0.69, color, -0.16, 0.88, 0);
box(group, 0.55, 0.12, 0.73, color, 0.52, 0.59, 0);
box(group, 0.13, 0.14, 0.19, 0xffe5a1, 0.79, 0.4, 0.23);
box(group, 0.13, 0.14, 0.19, 0xffe5a1, 0.79, 0.4, -0.23);
box(group, 0.1, 0.12, 0.18, 0xc8443c, -0.79, 0.39, 0.23);
box(group, 0.1, 0.12, 0.18, 0xc8443c, -0.79, 0.39, -0.23);
for (const x of [-0.51, 0.51]) {
for (const z of [-0.42, 0.42]) {
box(group, 0.31, 0.3, 0.15, COLORS.black, x, 0.19, z);
}
}
group.position.y = 0.06;
scene.add(group);
return group;
}
const vehicles = [];
const queues = [[], [], [], []];
const approachCooldown = [0, 0, 0, 0];
function queuePosition(approachIndex, queueIndex) {
const approach = APPROACHES[approachIndex];
const inward = approach.out.clone().multiplyScalar(-1);
const right = rightOf(inward);
const radius = 9.8 + queueIndex * 2.2;
return approach.out
.clone()
.multiplyScalar(radius)
.add(right.multiplyScalar(1.45));
}
function placeVehicle(car, x, z, heading) {
car.group.position.set(x, 0.06, z);
car.group.rotation.y = heading;
}
function addVehicle(color, properties) {
const car = {
group: createVehicle(color),
state: "wait",
approach: 0,
theta: 0,
speed: NOMINAL_SPEED,
exitApp: 0,
mergeElapsed: 0,
mergeDuration: 1.25,
mergePath: null,
exitElapsed: 0,
exitDuration: 2.55,
exitPath: null,
offstageTimer: 0,
nextEntry: 0,
...properties
};
vehicles.push(car);
return car;
}
// Four circulating agents, initially spaced around the ring.
const initialCircular = [
{ theta: 0.52, exitApp: 3 },
{ theta: 1.92, exitApp: 2 },
{ theta: 3.28, exitApp: 1 },
{ theta: 5.02, exitApp: 0 }
];
for (const spec of initialCircular) {
const car = addVehicle(
vehicleColors[vehicles.length % vehicleColors.length],
{
state: "circle",
theta: spec.theta,
exitApp: spec.exitApp
}
);
const x = Math.cos(spec.theta) * RING_RADIUS;
const z = Math.sin(spec.theta) * RING_RADIUS;
const tangent = new THREE.Vector3(
Math.sin(spec.theta),
0,
-Math.cos(spec.theta)
);
placeVehicle(car, x, z, headingFromDirection(tangent));
}
// Six queued agents: two on the east arm, two on north, one each
// on west and south. The complete pool never exceeds ten agents.
const queueSetup = [
[0, 0],
[1, 0],
[2, 0],
[3, 0],
[0, 1],
[1, 1]
];
for (const [approachIndex] of queueSetup) {
const car = addVehicle(
vehicleColors[vehicles.length % vehicleColors.length],
{
state: "wait",
approach: approachIndex
}
);
queues[approachIndex].push(car);
}
function repositionQueues() {
for (let app = 0; app < queues.length; app++) {
const inward = APPROACHES[app].out.clone().multiplyScalar(-1);
queues[app].forEach((car, index) => {
car.state = "wait";
car.approach = app;
car.group.visible = true;
const position = queuePosition(app, index);
placeVehicle(
car,
position.x,
position.z,
headingFromDirection(inward)
);
});
}
}
repositionQueues();
// ------------------------------------------------------------
// Deliberately simplified traffic logic
//
// This is not a driver model. A waiting vehicle is accepted only
// when no circulating vehicle is within a fixed arc corresponding
// to 2.2 seconds at the nominal speed. Ring vehicles use a basic
// minimum-spacing rule and otherwise travel at constant speed.
// ------------------------------------------------------------
function modulo(value, divisor) {
return ((value % divisor) + divisor) % divisor;
}
function circularArcDistance(a, b) {
const difference = Math.abs(modulo(a - b + Math.PI, TAU) - Math.PI);
return Math.min(difference, TAU - difference) * RING_RADIUS;
}
function cubicPoint(path, t) {
const u = 1 - t;
const a = u * u * u;
const b = 3 * u * u * t;
const c = 3 * u * t * t;
const d = t * t * t;
return new THREE.Vector3()
.addScaledVector(path.p0, a)
.addScaledVector(path.p1, b)
.addScaledVector(path.p2, c)
.addScaledVector(path.p3, d);
}
function cubicDirection(path, t) {
const u = 1 - t;
return new THREE.Vector3()
.addScaledVector(
path.p1.clone().sub(path.p0),
3 * u * u
)
.addScaledVector(
path.p2.clone().sub(path.p1),
6 * u * t
)
.addScaledVector(
path.p3.clone().sub(path.p2),
3 * t * t
)
.normalize();
}
function makeMergePath(approachIndex) {
const approach = APPROACHES[approachIndex];
const inward = approach.out.clone().multiplyScalar(-1);
const right = rightOf(inward);
const p0 = approach.out
.clone()
.multiplyScalar(9.8)
.add(right.multiplyScalar(1.45));
const p3 = approach.out.clone().multiplyScalar(RING_RADIUS);
const tangent = new THREE.Vector3(
Math.sin(approach.theta),
0,
-Math.cos(approach.theta)
);
return {
p0,
p1: p0.clone().addScaledVector(inward, 1.65),
p2: p3.clone().addScaledVector(tangent, -1.65),
p3
};
}
function makeExitPath(approachIndex) {
const approach = APPROACHES[approachIndex];
const tangent = new THREE.Vector3(
Math.sin(approach.theta),
0,
-Math.cos(approach.theta)
);
const p0 = approach.out.clone().multiplyScalar(RING_RADIUS);
const right = rightOf(approach.out);
const p3 = approach.out
.clone()
.multiplyScalar(17.2)
.add(right.multiplyScalar(1.45));
return {
p0,
p1: p0.clone().addScaledVector(tangent, 1.6),
p2: p3.clone().addScaledVector(approach.out, -1.6),
p3
};
}
const statusElement = document.getElementById("status");
const vehicleCountElement = document.getElementById("vehicleCount");
const queueCountElement = document.getElementById("queueCount");
function setStatus(message) {
statusElement.textContent = message;
}
function beginMerge(car, approachIndex) {
car.state = "merge";
car.approach = approachIndex;
car.mergeElapsed = 0;
car.mergePath = makeMergePath(approachIndex);
car.group.visible = true;
}
function attemptGrants() {
for (let app = 0; app < APPROACHES.length; app++) {
if (approachCooldown[app] > 0 || queues[app].length === 0) {
continue;
}
const approach = APPROACHES[app];
// Conservative fixed-clearance test around the yield point.
const conflict = vehicles.some((other) => {
if (other.state !== "circle") return false;
return (
circularArcDistance(other.theta, approach.theta) <
CLEARANCE
);
});
const alreadyMerging = vehicles.some(
(other) =>
other.state === "merge" && other.approach === app
);
if (conflict || alreadyMerging) continue;
const car = queues[app].shift();
beginMerge(car, app);
approachCooldown[app] = 1.35;
setStatus(
`${approach.name} arm accepted after the fixed 2.2 s clearance test.`
);
repositionQueues();
}
}
function desiredRingSpeed(car) {
let nearestGap = Infinity;
for (const other of vehicles) {
if (other === car || other.state !== "circle") continue;
// Vehicles circulate clockwise, so the next car has a smaller angle.
const angularGap = modulo(car.theta - other.theta, TAU);
if (
angularGap > 0.001 &&
angularGap < nearestGap
) {
nearestGap = angularGap;
}
}
if (!Number.isFinite(nearestGap)) {
return NOMINAL_SPEED;
}
const arcGap = nearestGap * RING_RADIUS;
const spacingSpeed = Math.max(0, (arcGap - 2.5) * 1.45);
return Math.min(NOMINAL_SPEED, spacingSpeed);
}
function updateTraffic(delta) {
for (let i = 0; i < approachCooldown.length; i++) {
approachCooldown[i] = Math.max(
0,
approachCooldown[i] - delta
);
}
repositionQueues();
for (const car of vehicles) {
if (car.state === "merge") {
car.mergeElapsed += delta;
const t = Math.min(1, car.mergeElapsed / car.mergeDuration);
const point = cubicPoint(car.mergePath, t);
const direction = cubicDirection(car.mergePath, t);
placeVehicle(
car,
point.x,
point.z,
headingFromDirection(direction)
);
if (t >= 1) {
car.state = "circle";
car.theta = APPROACHES[car.approach].theta;
car.speed = NOMINAL_SPEED;
const point2 = car.mergePath.p3;
const tangent = new THREE.Vector3(
Math.sin(car.theta),
0,
-Math.cos(car.theta)
);
placeVehicle(
car,
point2.x,
point2.z,
headingFromDirection(tangent)
);
}
} else if (car.state === "circle") {
car.speed = desiredRingSpeed(car);
const angularStep = (car.speed * delta) / RING_RADIUS;
const oldTheta = car.theta;
car.theta = modulo(oldTheta - angularStep, TAU);
const targetTheta = APPROACHES[car.exitApp].theta;
const distanceToExit =
modulo(oldTheta - targetTheta, TAU) * RING_RADIUS;
if (distanceToExit <= car.speed * delta + 0.05) {
car.theta = targetTheta;
car.state = "exit";
car.exitElapsed = 0;
car.exitPath = makeExitPath(car.exitApp);
const point = car.exitPath.p0;
const tangent = new THREE.Vector3(
Math.sin(targetTheta),
0,
-Math.cos(targetTheta)
);
placeVehicle(
car,
point.x,
point.z,
headingFromDirection(tangent)
);
} else {
const x = Math.cos(car.theta) * RING_RADIUS;
const z = Math.sin(car.theta) * RING_RADIUS;
const tangent = new THREE.Vector3(
Math.sin(car.theta),
0,
-Math.cos(car.theta)
);
placeVehicle(
car,
x,
z,
headingFromDirection(tangent)
);
}
} else if (car.state === "exit") {
car.exitElapsed += delta;
const t = Math.min(1, car.exitElapsed / car.exitDuration);
const point = cubicPoint(car.exitPath, t);
const direction = cubicDirection(car.exitPath, t);
placeVehicle(
car,
point.x,
point.z,
headingFromDirection(direction)
);
if (t >= 1) {
// Recycle the same bounded agent from the opposite arm.
car.state = "offstage";
car.group.visible = false;
car.offstageTimer = 1.5;
car.nextEntry = (car.exitApp + 2) % APPROACHES.length;
}
} else if (car.state === "offstage") {
car.offstageTimer -= delta;
if (
car.offstageTimer <= 0 &&
queues[car.nextEntry].length < 3
) {
car.state = "wait";
car.approach = car.nextEntry;
car.group.visible = true;
queues[car.nextEntry].push(car);
}
}
}
attemptGrants();
const visibleVehicles = vehicles.filter(
(car) => car.state !== "offstage"
).length;
const queuedVehicles = queues.reduce(
(total, queue) => total + queue.length,
0
);
vehicleCountElement.textContent =
`${visibleVehicles} / ${MAX_VEHICLES}`;
queueCountElement.textContent =
`${queuedVehicles} vehicle${queuedVehicles === 1 ? "" : "s"}`;
}
// ------------------------------------------------------------
// Direct camera controls, implemented without Three.js addons
// ------------------------------------------------------------
let desiredYaw = 0.76;
let desiredPitch = 0.72;
let desiredRadius = 49;
let currentYaw = desiredYaw;
let currentPitch = desiredPitch;
let currentRadius = desiredRadius;
const cameraTarget = new THREE.Vector3(0, 0.4, 0);
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateCamera(delta) {
const smoothing = 1 - Math.exp(-9 * delta);
currentYaw += (desiredYaw - currentYaw) * smoothing;
currentPitch += (desiredPitch - currentPitch) * smoothing;
currentRadius += (desiredRadius - currentRadius) * smoothing;
const horizontalRadius =
currentRadius * Math.cos(currentPitch);
camera.position.set(
cameraTarget.x +
Math.sin(currentYaw) * horizontalRadius,
cameraTarget.y +
Math.sin(currentPitch) * currentRadius,
cameraTarget.z +
Math.cos(currentYaw) * horizontalRadius
);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
desiredYaw -= dx * 0.006;
desiredPitch = THREE.MathUtils.clamp(
desiredPitch + dy * 0.005,
0.28,
1.49
);
});
function stopDragging(event) {
dragging = false;
if (
renderer.domElement.hasPointerCapture &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius + event.deltaY * 0.035,
25,
72
);
},
{ passive: false }
);
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
if (button.dataset.view === "top") {
desiredYaw = 0;
desiredPitch = 1.48;
desiredRadius = 47;
} else {
desiredYaw = 0.76;
desiredPitch = 0.72;
desiredRadius = 49;
}
});
});
let paused = false;
const pauseButton = document.getElementById("pauseButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// ------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(time) {
const delta = Math.min((time - previousTime) / 1000, 0.05);
previousTime = time;
if (!paused) {
updateTraffic(delta);
}
updateCamera(delta);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```