Prompt specification
Build or customize the Three.js 3D scene "Four Pedestrians Yield at a Doorway" 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: Four Pedestrians Yield at a Doorway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/four-pedestrians-yield-at-a-doorway-x3714
Scene Description Prompt:
Four pedestrians yield at a doorway. Animate four original characters approaching a doorway using explicit yielding priorities. Prevent overlap and show waiting rather than forcing everyone through at once. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (four-pedestrians-yield-at-a-doorway-x3714):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Doorway — Priority Yield</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
overflow: hidden;
background: #080d19;
color: #edf4ff;
}
canvas {
display: block;
width: 100vw;
height: 100vh;
touch-action: none;
}
.panel {
position: fixed;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 18px;
border: 1px solid #34425d;
border-radius: 16px;
background: rgba(9, 15, 29, 0.91);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.34);
backdrop-filter: blur(14px);
user-select: none;
z-index: 10;
}
.eyebrow {
color: #73d9ff;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 7px;
font-size: 25px;
line-height: 1.08;
}
.intro {
margin: 0 0 14px;
color: #aebbd0;
font-size: 13px;
line-height: 1.45;
}
.rule {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 12px;
padding: 10px 11px;
border: 1px solid #293953;
border-radius: 10px;
background: #111b2d;
color: #dce8fb;
font-size: 12px;
line-height: 1.35;
}
.rule strong {
color: #ffd166;
}
.priority-list {
display: grid;
gap: 7px;
}
.priority-row {
display: grid;
grid-template-columns: 35px 1fr auto;
align-items: center;
gap: 9px;
min-height: 42px;
padding: 7px 9px;
border: 1px solid #27334a;
border-radius: 10px;
background: rgba(20, 29, 47, 0.84);
transition: border-color 160ms, background 160ms, transform 160ms;
}
.priority-row.active {
border-color: #42d99d;
background: rgba(25, 67, 58, 0.88);
transform: translateX(3px);
}
.priority-row.done {
opacity: 0.58;
}
.number {
display: grid;
place-items: center;
width: 30px;
height: 30px;
border-radius: 8px;
color: #07101d;
font-weight: 900;
}
.person-name {
font-size: 13px;
font-weight: 800;
letter-spacing: 0.03em;
}
.person-sub {
margin-top: 2px;
color: #8493aa;
font-size: 10px;
}
.state {
color: #9eacc0;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-align: right;
}
.active .state {
color: #73f0bd;
}
.door-readout {
margin-top: 13px;
padding: 11px 12px;
border-radius: 10px;
background: #0b1322;
border: 1px solid #26334b;
}
.door-readout small {
display: block;
margin-bottom: 4px;
color: #7f90aa;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#door-status {
color: #ffd166;
font-size: 14px;
font-weight: 800;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 12px;
}
button {
flex: 1;
padding: 9px 12px;
border: 1px solid #3b4b68;
border-radius: 9px;
background: #18243a;
color: #e7f0ff;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: #223250;
border-color: #6680aa;
}
.hint {
position: fixed;
right: 18px;
bottom: 16px;
padding: 9px 12px;
border: 1px solid #293751;
border-radius: 10px;
background: rgba(8, 14, 26, 0.78);
color: #8999b1;
font-size: 11px;
pointer-events: none;
z-index: 10;
}
@media (max-width: 650px) {
.panel {
top: 10px;
left: 10px;
width: min(325px, calc(100vw - 20px));
padding: 13px;
}
h1 {
font-size: 21px;
}
.intro,
.rule {
font-size: 11px;
}
.person-sub {
display: none;
}
.hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<section class="panel" aria-label="Doorway priority controls">
<div class="eyebrow">Voxel Traffic Study</div>
<h1>Yield at the doorway</h1>
<p class="intro">
Four pedestrians approach in parallel. At the holding line, the explicit
priority order decides who receives the single doorway token.
</p>
<div class="rule">
<strong>PRIORITY 1 → 2 → 3 → 4</strong>
<span>Lower number = right of way. Only one pedestrian may enter the door zone.</span>
</div>
<div id="priority-list" class="priority-list"></div>
<div class="door-readout">
<small>Doorway token</small>
<div id="door-status" aria-live="polite">Gathering at the holding line</div>
</div>
<div class="buttons">
<button id="replay" type="button">Replay</button>
<button id="camera-reset" type="button">Reset camera</button>
</div>
</section>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x080d19);
scene.fog = new THREE.Fog(0x080d19, 17, 35);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
80
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
document.body.prepend(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xc8e8ff, 0x172036, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.2);
sun.position.set(-7, 13, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x5f8fff, 1.15);
fill.position.set(8, 6, -9);
scene.add(fill);
// -------------------------------------------------------------------------
// Shared voxel construction helpers
// -------------------------------------------------------------------------
const materialCache = new Map();
function material(color, roughness = 0.8) {
const key = `${color}-${roughness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness,
metalness: 0.02
})
);
}
return materialCache.get(key);
}
function box(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
options.material || material(color, options.roughness)
);
mesh.position.copy(position);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function makeHoldingSpot(x, z, color) {
const group = new THREE.Group();
const y = 0.075;
const width = 0.76;
box(group, new THREE.Vector3(width, 0.025, 0.06),
new THREE.Vector3(x, y, z - width / 2), color);
box(group, new THREE.Vector3(width, 0.025, 0.06),
new THREE.Vector3(x, y, z + width / 2), color);
box(group, new THREE.Vector3(0.06, 0.025, width),
new THREE.Vector3(x - width / 2, y, z), color);
box(group, new THREE.Vector3(0.06, 0.025, width),
new THREE.Vector3(x + width / 2, y, z), color);
scene.add(group);
}
// -------------------------------------------------------------------------
// Voxel environment
// -------------------------------------------------------------------------
const environment = new THREE.Group();
scene.add(environment);
// Main floor slab.
box(environment, new THREE.Vector3(18.5, 0.25, 9.5),
new THREE.Vector3(0.8, -0.16, 0), 0x172238);
// Four approach lanes.
const laneColors = [0x30425f, 0x30425f, 0x30425f, 0x30425f];
for (let lane = 0; lane < 4; lane++) {
const z = [-2.55, -0.85, 0.85, 2.55][lane];
for (let x = -7.0; x < 5.6; x += 0.72) {
box(environment, new THREE.Vector3(0.66, 0.035, 0.74),
new THREE.Vector3(x, 0.015, z), laneColors[lane]);
}
for (let x = -6.4; x < 1.2; x += 0.8) {
box(environment, new THREE.Vector3(0.38, 0.025, 0.075),
new THREE.Vector3(x, 0.05, z), 0x546987, {
castShadow: false
});
}
}
// White/yellow segmented holding line.
for (let z = -4.05; z <= 4.05; z += 0.56) {
box(environment, new THREE.Vector3(0.12, 0.04, 0.37),
new THREE.Vector3(1.38, 0.06, z),
Math.round(z * 10) % 2 === 0 ? 0xffd166 : 0xe8eef7, {
castShadow: false
});
}
// Four separate waiting pads.
[-2.55, -0.85, 0.85, 2.55].forEach((z, i) => {
const colors = [0xff6b6b, 0xf4c95d, 0xc084fc, 0x38bdf8];
makeHoldingSpot(1.9, z, colors[i]);
});
// Voxel doorway wall.
const wallX = 6.3;
const wallRows = 5;
const rowHeight = 0.64;
for (let row = 0; row < wallRows; row++) {
for (let z = -4.2; z <= 4.2; z += 0.7) {
const isOpening = row < 4 && Math.abs(z) < 1.05;
if (!isOpening) {
const shade = (row + Math.round((z + 4.2) * 2)) % 3;
const wallColor = [0x31435d, 0x374a66, 0x2b3b54][shade];
box(environment,
new THREE.Vector3(0.42, 0.59, 0.65),
new THREE.Vector3(wallX, 0.31 + row * rowHeight, z),
wallColor);
}
}
}
// Bright block doorway frame.
for (const z of [-1.1, 1.1]) {
for (let i = 0; i < 8; i++) {
box(environment, new THREE.Vector3(0.2, 0.3, 0.25),
new THREE.Vector3(6.04, 0.16 + i * 0.32, z), 0xffc857);
}
}
for (let i = 0; i < 8; i++) {
box(environment, new THREE.Vector3(0.2, 0.25, 0.25),
new THREE.Vector3(6.04, 2.57, -0.98 + i * 0.28), 0xffc857);
}
// Interior room beyond the doorway.
box(environment, new THREE.Vector3(3.6, 0.08, 8.4),
new THREE.Vector3(8.15, 0.015, 0), 0x51483f);
for (let x = 6.7; x < 9.8; x += 0.7) {
for (let z = -3.8; z < 4.0; z += 0.72) {
box(environment, new THREE.Vector3(0.64, 0.025, 0.66),
new THREE.Vector3(x, 0.065, z),
(Math.round(x + z) % 2) ? 0x756451 : 0x675b4c, {
castShadow: false
});
}
}
// Back wall, mostly seen through the doorway.
for (let z = -4; z <= 4; z += 0.8) {
for (let row = 0; row < 4; row++) {
box(environment, new THREE.Vector3(0.35, 0.72, 0.74),
new THREE.Vector3(9.45, 0.36 + row * 0.72, z), 0x3d3b3d);
}
}
// Small voxel planter for scene depth.
box(environment, new THREE.Vector3(0.85, 0.55, 0.85),
new THREE.Vector3(4.9, 0.28, 3.75), 0x8b5c42);
box(environment, new THREE.Vector3(0.55, 0.75, 0.55),
new THREE.Vector3(4.9, 0.88, 3.75), 0x3e9367);
box(environment, new THREE.Vector3(0.32, 0.42, 0.32),
new THREE.Vector3(4.72, 1.42, 3.64), 0x63bd7d);
box(environment, new THREE.Vector3(0.3, 0.52, 0.3),
new THREE.Vector3(5.08, 1.48, 3.88), 0x4faa72);
// -------------------------------------------------------------------------
// Dynamic canvas labels
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createActorLabel(actor) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 160;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
})
);
sprite.position.set(0, 2.3, 0);
sprite.scale.set(2.65, 0.66, 1);
sprite.renderOrder = 20;
actor.root.add(sprite);
actor.labelCanvas = canvas;
actor.labelContext = canvas.getContext("2d");
actor.labelTexture = texture;
actor.labelSprite = sprite;
}
function paintActorLabel(actor) {
const ctx = actor.labelContext;
const canvas = actor.labelCanvas;
ctx.clearRect(0, 0, canvas.width, canvas.height);
let stateText = "HOLDING";
let stateColor = "#ffd166";
if (actor.state === "approach") {
stateText = "APPROACHING";
stateColor = "#83d9ff";
} else if (actor.state === "hold") {
stateText = "HOLDING";
stateColor = "#ffd166";
} else if (actor.state === "yield") {
stateText = "YIELDING";
stateColor = "#ff8e9e";
} else if (actor.state === "moving") {
stateText = `PRIORITY ${actor.priority} · GO`;
stateColor = "#65efb5";
} else if (actor.state === "cleared") {
stateText = "THROUGH";
stateColor = "#a8b6ca";
}
ctx.fillStyle = "rgba(7, 13, 25, 0.93)";
roundedRect(ctx, 5, 5, 630, 150, 22);
ctx.fill();
ctx.strokeStyle = actor.color;
ctx.lineWidth = 5;
roundedRect(ctx, 5, 5, 630, 150, 22);
ctx.stroke();
ctx.fillStyle = actor.color;
roundedRect(ctx, 18, 20, 105, 120, 15);
ctx.fill();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#08101d";
ctx.font = "900 58px system-ui";
ctx.fillText(String(actor.priority), 70, 80);
ctx.textAlign = "left";
ctx.fillStyle = "#f3f7ff";
ctx.font = "900 38px system-ui";
ctx.fillText(actor.name.toUpperCase(), 148, 59);
ctx.fillStyle = stateColor;
ctx.font = "800 25px system-ui";
ctx.fillText(stateText, 149, 108);
actor.labelTexture.needsUpdate = true;
}
function createDoorSign() {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 210;
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
fog: false
})
);
sprite.position.set(6.0, 3.72, 0);
sprite.scale.set(3.2, 1.05, 1);
sprite.renderOrder = 20;
scene.add(sprite);
return { canvas, context: canvas.getContext("2d"), texture, sprite };
}
const doorSign = createDoorSign();
function paintDoorSign(title, detail, color) {
const ctx = doorSign.context;
ctx.clearRect(0, 0, doorSign.canvas.width, doorSign.canvas.height);
ctx.fillStyle = "rgba(7, 13, 25, 0.94)";
roundedRect(ctx, 8, 8, 624, 194, 24);
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 6;
roundedRect(ctx, 8, 8, 624, 194, 24);
ctx.stroke();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#8fa0b9";
ctx.font = "800 25px system-ui";
ctx.fillText(title, 320, 56);
ctx.fillStyle = color;
ctx.font = "900 47px system-ui";
ctx.fillText(detail, 320, 120);
doorSign.texture.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Original voxel pedestrians
// -------------------------------------------------------------------------
function createCharacter(actor) {
const root = new THREE.Group();
root.position.set(actor.startX, 0, actor.laneZ);
root.scale.setScalar(actor.scale);
scene.add(root);
const model = new THREE.Group();
root.add(model);
actor.model = model;
const skin = material(actor.skin);
const cloth = material(actor.color);
const darkCloth = material(actor.secondary);
const shoeMaterial = material(0x18202d);
const eyeMaterial = material(0x090d15);
// Leg pivots.
const legPivots = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(0, 0.73, side * 0.16);
model.add(pivot);
box(pivot, new THREE.Vector3(0.25, 0.52, 0.21),
new THREE.Vector3(0, -0.25, 0), actor.secondary);
box(pivot, new THREE.Vector3(0.38, 0.15, 0.24),
new THREE.Vector3(0.07, -0.55, 0), 0x18202d);
legPivots.push(pivot);
}
actor.legPivots = legPivots;
// Torso and belt.
box(model, new THREE.Vector3(0.49, 0.57, 0.58),
new THREE.Vector3(0, 1.05, 0), actor.color);
box(model, new THREE.Vector3(0.52, 0.11, 0.61),
new THREE.Vector3(0, 0.78, 0), actor.secondary);
// Neck and head.
box(model, new THREE.Vector3(0.22, 0.13, 0.24),
new THREE.Vector3(0, 1.38, 0), actor.skin);
box(model, new THREE.Vector3(0.45, 0.44, 0.47),
new THREE.Vector3(0, 1.61, 0), actor.skin);
// Hair / headwear variants.
if (actor.hat === "beanie") {
box(model, new THREE.Vector3(0.49, 0.18, 0.51),
new THREE.Vector3(0, 1.87, 0), actor.secondary);
box(model, new THREE.Vector3(0.52, 0.08, 0.54),
new THREE.Vector3(0, 1.78, 0), actor.color);
} else if (actor.hat === "cap") {
box(model, new THREE.Vector3(0.48, 0.16, 0.5),
new THREE.Vector3(0, 1.86, 0), actor.secondary);
box(model, new THREE.Vector3(0.3, 0.08, 0.53),
new THREE.Vector3(0.18, 1.79, 0), actor.color);
} else if (actor.hat === "bob") {
box(model, new THREE.Vector3(0.49, 0.22, 0.51),
new THREE.Vector3(0, 1.84, 0), actor.hair);
box(model, new THREE.Vector3(0.22, 0.48, 0.12),
new THREE.Vector3(-0.07, 1.58, 0.27), actor.hair);
box(model, new THREE.Vector3(0.22, 0.48, 0.12),
new THREE.Vector3(-0.07, 1.58, -0.27), actor.hair);
} else {
box(model, new THREE.Vector3(0.49, 0.19, 0.51),
new THREE.Vector3(0, 1.86, 0), actor.hair);
box(model, new THREE.Vector3(0.18, 0.25, 0.5),
new THREE.Vector3(-0.18, 1.72, 0), actor.hair);
}
// Face: two small block eyes and a simple nose.
for (const z of [-0.105, 0.105]) {
box(model, new THREE.Vector3(0.035, 0.065, 0.065),
new THREE.Vector3(0.239, 1.64, z), eyeMaterial, {
castShadow: false
});
}
box(model, new THREE.Vector3(0.07, 0.07, 0.09),
new THREE.Vector3(0.255, 1.53, 0), actor.skin, {
castShadow: false
});
// Arms.
const armPivots = [];
for (const side of [-1, 1]) {
const pivot = new THREE.Group();
pivot.position.set(0, 1.24, side * 0.38);
model.add(pivot);
box(pivot, new THREE.Vector3(0.22, 0.42, 0.18),
new THREE.Vector3(0, -0.2, 0), actor.color);
box(pivot, new THREE.Vector3(0.21, 0.16, 0.18),
new THREE.Vector3(0, -0.47, 0), actor.skin);
armPivots.push(pivot);
}
actor.armPivots = armPivots;
// Character-specific accessory blocks.
if (actor.accessory === "backpack") {
box(model, new THREE.Vector3(0.22, 0.48, 0.43),
new THREE.Vector3(-0.34, 1.04, 0), actor.secondary);
} else if (actor.accessory === "satchel") {
box(model, new THREE.Vector3(0.12, 0.37, 0.43),
new THREE.Vector3(0.29, 0.9, 0.12), actor.secondary);
} else if (actor.accessory === "scarf") {
box(model, new THREE.Vector3(0.42, 0.12, 0.61),
new THREE.Vector3(0, 1.35, 0), actor.secondary);
box(model, new THREE.Vector3(0.12, 0.38, 0.16),
new THREE.Vector3(0.18, 1.13, 0.2), actor.secondary);
} else if (actor.accessory === "glasses") {
for (const z of [-0.11, 0.11]) {
box(model, new THREE.Vector3(0.04, 0.11, 0.13),
new THREE.Vector3(0.258, 1.65, z), 0x202a3b, {
castShadow: false
});
}
box(model, new THREE.Vector3(0.035, 0.035, 0.1),
new THREE.Vector3(0.26, 1.65, 0), 0x202a3b, {
castShadow: false
});
}
actor.root = root;
createActorLabel(actor);
}
const actors = [
{
priority: 1,
name: "Amari",
role: "first in queue",
color: "#ff6b6b",
numericColor: 0xff6b6b,
secondary: 0x6d2738,
skin: 0x7d4934,
hair: 0x241a18,
hat: "curly",
accessory: "backpack",
laneZ: -2.55,
startX: -6.45,
waitX: 1.9,
scale: 1.03,
radius: 0.44,
state: "approach"
},
{
priority: 2,
name: "Bo",
role: "second in queue",
color: "#f4c95d",
numericColor: 0xf4c95d,
secondary: 0x39766c,
skin: 0xe0a77c,
hair: 0x30231e,
hat: "beanie",
accessory: "satchel",
laneZ: 0.85,
startX: -6.45,
waitX: 1.9,
scale: 0.95,
radius: 0.42,
state: "approach"
},
{
priority: 3,
name: "Lina",
role: "third in queue",
color: "#c084fc",
numericColor: 0xc084fc,
secondary: 0x4c3674,
skin: 0xb87551,
hair: 0x382018,
hat: "bob",
accessory: "scarf",
laneZ: 2.55,
startX: -6.45,
waitX: 1.9,
scale: 1.0,
radius: 0.43,
state: "approach"
},
{
priority: 4,
name: "Theo",
role: "fourth in queue",
color: "#38bdf8",
numericColor: 0x38bdf8,
secondary: 0x255f91,
skin: 0x9b664d,
hair: 0x171a20,
hat: "cap",
accessory: "glasses",
laneZ: -0.85,
startX: -6.45,
waitX: 1.9,
scale: 1.06,
radius: 0.45,
state: "approach"
}
];
const priorityOrder = [...actors].sort((a, b) => a.priority - b.priority);
actors.forEach(createCharacter);
// -------------------------------------------------------------------------
// Door token and signal lamps
// -------------------------------------------------------------------------
const tokenMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0xffa600,
emissiveIntensity: 1.6,
roughness: 0.35
});
const token = new THREE.Mesh(
new THREE.BoxGeometry(0.27, 0.27, 0.27),
tokenMaterial
);
token.castShadow = true;
token.visible = false;
scene.add(token);
function makeSignal(color, y) {
const mat = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.08,
roughness: 0.45
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.22, 0.22, 0.22),
mat
);
mesh.position.set(6.02, y, -1.5);
mesh.castShadow = true;
scene.add(mesh);
return mesh;
}
const signalRed = makeSignal(0xff4967, 2.3);
const signalAmber = makeSignal(0xffc857, 1.98);
const signalGreen = makeSignal(0x42d99d, 1.66);
function setSignal(which, on) {
const mesh = { red: signalRed, amber: signalAmber, green: signalGreen }[which];
mesh.material.emissiveIntensity = on ? 2.1 : 0.06;
mesh.material.color.set(on
? ({ red: 0xff4967, amber: 0xffc857, green: 0x42d99d })[which]
: 0x263040);
}
// -------------------------------------------------------------------------
// Priority queue state machine
// -------------------------------------------------------------------------
const priorityList = document.querySelector("#priority-list");
const doorStatus = document.querySelector("#door-status");
for (const actor of priorityOrder) {
const row = document.createElement("div");
row.className = "priority-row";
row.innerHTML = `
<div class="number" style="background:${actor.color}">${actor.priority}</div>
<div>
<div class="person-name">${actor.name}</div>
<div class="person-sub">${actor.role}</div>
</div>
<div class="state">APPROACHING</div>
`;
priorityList.appendChild(row);
actor.row = row;
actor.rowState = row.querySelector(".state");
}
let elapsed = 0;
let queueReady = false;
let complete = false;
let activeActor = null;
let nextPriorityIndex = 0;
let releaseCountdown = 0;
let lastDoorMode = "";
function setActorState(actor, state) {
actor.state = state;
paintActorLabel(actor);
}
function positionIsClear(actor, x, z) {
for (const other of actors) {
if (other === actor || other.state === "cleared" || !other.root.visible) {
continue;
}
const dx = x - other.root.position.x;
const dz = z - other.root.position.z;
const minimum = (actor.radius + other.radius) * 0.98;
if (dx * dx + dz * dz < minimum * minimum) {
return false;
}
}
return true;
}
function beginPriorityTurn(actor) {
activeActor = actor;
actor.route = [
new THREE.Vector2(actor.waitX, actor.laneZ),
new THREE.Vector2(3.2, actor.laneZ),
new THREE.Vector2(4.55, 0),
new THREE.Vector2(7.85, 0)
];
actor.routeIndex = 1;
actor.blocked = false;
setActorState(actor, "moving");
lastDoorMode = "";
}
function finishPriorityTurn(actor) {
actor.root.visible = false;
setActorState(actor, "cleared");
activeActor = null;
nextPriorityIndex += 1;
releaseCountdown = 0.75;
if (nextPriorityIndex >= priorityOrder.length) {
complete = true;
}
lastDoorMode = "";
}
function moveActorToWaypoint(actor, dt) {
const target = actor.route[actor.routeIndex];
const dx = target.x - actor.root.position.x;
const dz = target.y - actor.root.position.z;
const distance = Math.hypot(dx, dz);
if (distance < 0.015) {
actor.routeIndex += 1;
if (actor.routeIndex >= actor.route.length) {
finishPriorityTurn(actor);
}
return;
}
const step = Math.min(distance, actor.speed * dt);
const nx = actor.root.position.x + (dx / distance) * step;
const nz = actor.root.position.z + (dz / distance) * step;
// Movement is only committed when the next position is physically clear.
if (positionIsClear(actor, nx, nz)) {
actor.root.position.x = nx;
actor.root.position.z = nz;
actor.walkPhase += step * 7.5;
// The character model faces +X by default.
const desiredYaw = -Math.atan2(dz, dx);
const delta = Math.atan2(
Math.sin(desiredYaw - actor.root.rotation.y),
Math.cos(desiredYaw - actor.root.rotation.y)
);
actor.root.rotation.y += delta * Math.min(1, dt * 11);
actor.blocked = false;
} else {
actor.blocked = true;
}
}
function resetSimulation() {
elapsed = 0;
queueReady = false;
complete = false;
activeActor = null;
nextPriorityIndex = 0;
releaseCountdown = 0;
lastDoorMode = "";
for (const actor of actors) {
actor.root.visible = true;
actor.root.position.set(actor.startX, 0, actor.laneZ);
actor.root.rotation.y = 0;
actor.walkPhase = actor.priority * 0.7;
actor.speed = 1.72 + (4 - actor.priority) * 0.04;
actor.blocked = false;
actor.model.position.y = 0;
setActorState(actor, "approach");
}
token.visible = false;
setSignal("red", false);
setSignal("amber", false);
setSignal("green", false);
}
function updateSimulation(dt) {
elapsed += dt;
// Approach phase: lanes are separated, so pedestrians can advance in parallel.
for (const actor of actors) {
if (actor.state === "approach") {
const nextX = Math.min(
actor.waitX,
actor.root.position.x + actor.speed * dt
);
if (positionIsClear(actor, nextX, actor.laneZ)) {
actor.root.position.x = nextX;
actor.walkPhase += actor.speed * dt * 7.5;
}
if (actor.root.position.x >= actor.waitX - 0.001) {
setActorState(actor, "hold");
}
} else if (actor.state === "moving") {
moveActorToWaypoint(actor, dt);
}
}
const everyoneAtLine = actors.every(actor =>
actor.state === "hold" || actor.state === "yield" ||
actor.state === "moving" || actor.state === "cleared"
);
if (!queueReady && everyoneAtLine) {
queueReady = true;
releaseCountdown = 1.25;
for (const actor of actors) {
if (actor.state === "hold") {
setActorState(actor, "yield");
}
}
}
// One token, one pedestrian in the door zone.
if (queueReady && !activeActor && !complete) {
releaseCountdown -= dt;
if (releaseCountdown <= 0) {
beginPriorityTurn(priorityOrder[nextPriorityIndex]);
}
}
// Limb motion and subtle idle movement.
for (const actor of actors) {
if (!actor.root.visible) continue;
const moving = actor.state === "approach" || actor.state === "moving";
const swing = moving ? Math.sin(actor.walkPhase) : 0;
const bob = moving
? Math.abs(Math.sin(actor.walkPhase)) * 0.035
: Math.sin(elapsed * 2.1 + actor.priority) * 0.006;
actor.model.position.y = bob;
actor.legPivots[0].rotation.z = swing * 0.55;
actor.legPivots[1].rotation.z = -swing * 0.55;
actor.armPivots[0].rotation.z = -swing * 0.42;
actor.armPivots[1].rotation.z = swing * 0.42;
}
// Floating voxel token marks the current right-of-way holder.
token.visible = !!activeActor;
if (activeActor) {
token.position.set(
activeActor.root.position.x,
2.76 + Math.sin(elapsed * 4.2) * 0.07,
activeActor.root.position.z
);
token.rotation.x = elapsed * 1.7;
token.rotation.y = elapsed * 2.2;
}
updateInterface();
}
function updateInterface() {
const stateInfo = {
approach: ["APPROACHING", "#83d9ff"],
hold: ["HOLDING", "#ffd166"],
yield: ["YIELDING", "#ff8e9e"],
moving: ["PRIORITY " + (activeActor?.priority ?? ""), "#65efb5"],
cleared: ["THROUGH", "#a8b6ca"]
};
for (const actor of actors) {
const [text, color] = stateInfo[actor.state];
actor.rowState.textContent = text;
actor.rowState.style.color = color;
actor.row.classList.toggle(
"active",
actor === activeActor
);
actor.row.classList.toggle(
"done",
actor.state === "cleared"
);
}
let mode;
if (complete) {
mode = "complete";
doorStatus.textContent = "Clear — all pedestrians have passed";
doorStatus.style.color = "#65efb5";
paintDoorSign("ONE AT A TIME", "DOOR CLEAR", "#65efb5");
setSignal("red", false);
setSignal("amber", false);
setSignal("green", true);
} else if (activeActor) {
mode = `active-${activeActor.priority}`;
doorStatus.textContent = `Reserved for Priority ${activeActor.priority} — ${activeActor.name}`;
doorStatus.style.color = "#65efb5";
paintDoorSign("DOOR TOKEN", `PRIORITY ${activeActor.priority}`, "#65efb5");
setSignal("red", false);
setSignal("amber", false);
setSignal("green", true);
} else if (queueReady) {
mode = "waiting";
const next = priorityOrder[nextPriorityIndex];
doorStatus.textContent = `Priority ${next.priority} is next — everyone else waits`;
doorStatus.style.color = "#ffd166";
paintDoorSign("ONE AT A TIME", "WAIT FOR TOKEN", "#ffd166");
setSignal("red", false);
setSignal("amber", true);
setSignal("green", false);
} else {
mode = "gathering";
doorStatus.textContent = "Gathering at the holding line";
doorStatus.style.color = "#83d9ff";
paintDoorSign("ONE AT A TIME", "GATHERING", "#83d9ff");
setSignal("red", false);
setSignal("amber", true);
setSignal("green", false);
}
// Repaint the sign only when its mode changes.
if (mode !== lastDoorMode) {
lastDoorMode = mode;
}
}
// -------------------------------------------------------------------------
// Direct camera controls: no addons
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.6, 0.85, 0);
const defaultCamera = {
theta: 2.25,
phi: 0.9,
radius: 18.2
};
const cameraState = { ...defaultCamera };
let dragging = false;
let pointerX = 0;
let pointerY = 0;
const activePointers = new Map();
let pinchDistance = 0;
function updateCamera() {
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi, 0.48, 1.22);
cameraState.radius = THREE.MathUtils.clamp(cameraState.radius, 11, 27);
camera.position.setFromSpherical(
new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
)
).add(cameraTarget);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", event => {
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 1) {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
} else if (activePointers.size === 2) {
const points = [...activePointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", event => {
if (!activePointers.has(event.pointerId)) return;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0) {
cameraState.radius *= pinchDistance / Math.max(distance, 1);
}
pinchDistance = distance;
updateCamera();
return;
}
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
cameraState.theta -= dx * 0.006;
cameraState.phi -= dy * 0.005;
pointerX = event.clientX;
pointerY = event.clientY;
updateCamera();
});
function endPointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size < 2) {
pinchDistance = 0;
}
if (activePointers.size === 0) {
dragging = false;
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener("lostpointercapture", endPointer);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius += event.deltaY * 0.012;
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
document.querySelector("#camera-reset").addEventListener("click", () => {
Object.assign(cameraState, defaultCamera);
updateCamera();
});
document.querySelector("#replay").addEventListener("click", resetSimulation);
// -------------------------------------------------------------------------
// Resize and main loop
// -------------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
resetSimulation();
updateCamera();
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.04);
updateSimulation(dt);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```