Prompt specification
Build or customize the Three.js 3D scene "Traffic-light state machine sculpture" 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: Traffic-light state machine sculpture
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/traffic-light-state-machine-sculpture-x1877
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a finite-state machine with event-driven transitions. Build a three-state signal model with explicit transition arrows and timing labels; allow only permitted transitions and distinguish state from elapsed time within a state. Low-poly.
Working Three.js Starter (traffic-light-state-machine-sculpture-x1877):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Signal Relay — Event-Driven Finite-State Machine</title>
<style>
:root {
color-scheme: dark;
--bg: #06101b;
--panel: #0b1725;
--panel-2: #0f1d2d;
--line: rgba(151, 190, 220, 0.16);
--text: #eaf4ff;
--muted: #8298ad;
--idle: #51c7ff;
--armed: #ffc857;
--active: #ff5d8f;
--state-color: var(--idle);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
min-height: 100%;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
overflow: hidden;
}
button {
font: inherit;
}
.shell {
display: flex;
flex-direction: column;
width: 100%;
height: 100vh;
min-height: 680px;
}
.topbar {
z-index: 10;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
min-height: 78px;
padding: 14px 22px;
border-bottom: 1px solid var(--line);
background: rgba(6, 16, 27, 0.94);
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
position: relative;
width: 39px;
height: 39px;
flex: 0 0 auto;
border: 1px solid rgba(81, 199, 255, 0.45);
background: rgba(81, 199, 255, 0.08);
clip-path: polygon(50% 0, 100% 27%, 100% 73%, 50% 100%, 0 73%, 0 27%);
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
border-radius: 50%;
}
.brand-mark::before {
width: 8px;
height: 8px;
left: 8px;
top: 15px;
background: var(--idle);
box-shadow: 12px 0 0 var(--armed), 24px 0 0 var(--active);
}
.brand-mark::after {
width: 31px;
height: 1px;
left: 4px;
top: 19px;
background: rgba(234, 244, 255, 0.65);
transform: rotate(-9deg);
}
.eyebrow {
margin-bottom: 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(17px, 2vw, 23px);
line-height: 1.1;
letter-spacing: -0.02em;
}
.top-meta {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #6ee7a8;
box-shadow: 0 0 12px rgba(110, 231, 168, 0.75);
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 340px;
flex: 1;
min-height: 0;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 48%, rgba(31, 72, 105, 0.13), transparent 47%),
#07111d;
}
#scene {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
outline: none;
}
#scene.dragging {
cursor: grabbing;
}
.readouts {
position: absolute;
z-index: 4;
top: 16px;
left: 16px;
right: 16px;
display: grid;
grid-template-columns: 1.05fr 0.9fr 1.15fr;
gap: 9px;
pointer-events: none;
}
.readout {
min-width: 0;
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(8, 21, 34, 0.78);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.14);
backdrop-filter: blur(12px);
}
.readout-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.readout strong {
display: block;
overflow: hidden;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 20px;
line-height: 1.1;
text-overflow: ellipsis;
white-space: nowrap;
}
.readout small {
display: block;
margin-top: 6px;
overflow: hidden;
color: var(--muted);
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.state-readout strong {
color: var(--state-color);
text-shadow: 0 0 18px color-mix(in srgb, var(--state-color) 35%, transparent);
}
.timer-track {
height: 3px;
margin-top: 9px;
overflow: hidden;
border-radius: 5px;
background: rgba(255, 255, 255, 0.08);
}
.timer-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: var(--state-color);
transition: width 80ms linear;
}
.state-chips {
display: flex;
flex-wrap: wrap;
gap: 5px;
margin-top: 9px;
}
.state-chip {
padding: 3px 6px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 4px;
color: #62778c;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.08em;
}
.state-chip.active {
border-color: var(--state-color);
color: var(--state-color);
background: rgba(255, 255, 255, 0.035);
}
#labels {
position: absolute;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
will-change: left, top;
}
.state-label {
min-width: 128px;
padding: 9px 12px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-top: 2px solid var(--tone);
border-radius: 7px;
background: rgba(5, 15, 25, 0.86);
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.24);
text-align: center;
backdrop-filter: blur(8px);
}
.state-label.current {
border-color: var(--tone);
box-shadow:
0 0 0 1px rgba(255, 255, 255, 0.025),
0 0 24px rgba(0, 0, 0, 0.25);
}
.state-label strong {
display: block;
color: var(--tone);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 14px;
letter-spacing: 0.08em;
}
.state-label small {
display: block;
margin-top: 3px;
color: #93a8bb;
font-size: 8px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.transition-label {
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 6px;
background: rgba(5, 15, 25, 0.9);
box-shadow: 0 7px 18px rgba(0, 0, 0, 0.2);
text-align: center;
}
.transition-label b {
display: block;
color: #e9f5ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
letter-spacing: 0.08em;
}
.transition-label span {
display: block;
margin-top: 3px;
color: #8fa5b8;
font-size: 8px;
white-space: nowrap;
}
.transition-label.ready {
border-color: rgba(110, 231, 168, 0.55);
}
.transition-label.waiting {
border-color: rgba(255, 200, 87, 0.48);
}
.transition-label.flash {
animation: label-flash 450ms ease-out;
}
@keyframes label-flash {
0% {
transform: translate(-50%, -50%) scale(1);
box-shadow: 0 0 0 0 rgba(110, 231, 168, 0.65);
}
100% {
transform: translate(-50%, -50%) scale(1.12);
box-shadow: 0 0 0 15px rgba(110, 231, 168, 0);
}
}
.stage-footer {
position: absolute;
z-index: 4;
right: 15px;
bottom: 14px;
left: 15px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
pointer-events: none;
}
.camera-hint {
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 6px;
color: #778da2;
background: rgba(6, 16, 27, 0.72);
font-size: 10px;
backdrop-filter: blur(8px);
}
.view-button {
pointer-events: auto;
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 6px;
color: #b8cad9;
background: rgba(10, 25, 39, 0.85);
cursor: pointer;
font-size: 10px;
}
.view-button:hover {
border-color: rgba(81, 199, 255, 0.45);
color: white;
}
aside {
min-height: 0;
overflow-y: auto;
border-left: 1px solid var(--line);
background: linear-gradient(180deg, #091522, #07111c);
}
.side-section {
padding: 18px;
border-bottom: 1px solid var(--line);
}
.section-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.section-heading h2 {
margin: 0;
font-size: 11px;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-heading span {
color: var(--muted);
font-size: 9px;
}
.contract-row {
display: grid;
grid-template-columns: 54px 1fr 14px 54px;
align-items: center;
gap: 7px;
padding: 10px 0;
border-top: 1px solid rgba(255, 255, 255, 0.055);
}
.contract-row:first-of-type {
border-top: 0;
}
.state-token {
padding: 5px 3px;
border: 1px solid currentColor;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 8px;
font-weight: 900;
letter-spacing: 0.06em;
text-align: center;
}
.token-idle {
color: var(--idle);
}
.token-armed {
color: var(--armed);
}
.token-active {
color: var(--active);
}
.contract-event {
min-width: 0;
text-align: center;
}
.contract-event b {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
letter-spacing: 0.05em;
}
.contract-event small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
}
.contract-arrow {
color: #536a7e;
text-align: center;
}
.semantic-note {
margin: 12px 0 0;
padding: 10px 11px;
border-left: 2px solid var(--armed);
border-radius: 0 5px 5px 0;
color: #aabccb;
background: rgba(255, 200, 87, 0.055);
font-size: 10px;
line-height: 1.55;
}
.semantic-note b {
color: #f0f7fd;
}
.clock-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-bottom: 12px;
}
.clock-status {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
.clock-button {
padding: 6px 8px;
border: 1px solid var(--line);
border-radius: 5px;
color: #a9bdcd;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
font-size: 9px;
}
.event-button {
position: relative;
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 9px;
width: 100%;
margin-top: 8px;
padding: 10px;
border: 1px solid var(--line);
border-radius: 8px;
color: var(--text);
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
text-align: left;
transition:
border-color 150ms ease,
background 150ms ease,
transform 100ms ease;
}
.event-button:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.28);
background: rgba(255, 255, 255, 0.045);
}
.event-button.ready {
border-color: rgba(110, 231, 168, 0.5);
background: rgba(110, 231, 168, 0.055);
}
.event-button.waiting {
border-color: rgba(255, 200, 87, 0.35);
}
.event-button.wrong {
opacity: 0.58;
}
.shortcut {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 5px;
color: #a8bdce;
background: rgba(0, 0, 0, 0.17);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
font-weight: 800;
}
.event-copy strong {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
letter-spacing: 0.08em;
}
.event-copy small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
}
.event-status {
color: #8399aa;
font-size: 8px;
text-align: right;
}
.event-button.ready .event-status {
color: #6ee7a8;
}
.event-button.waiting .event-status {
color: var(--armed);
}
#eventLog {
display: grid;
gap: 7px;
min-height: 115px;
max-height: 210px;
overflow-y: auto;
}
.log-row {
display: grid;
grid-template-columns: 43px 1fr;
gap: 8px;
padding: 7px 8px;
border-left: 2px solid #4b6072;
border-radius: 0 5px 5px 0;
background: rgba(255, 255, 255, 0.025);
font-size: 9px;
line-height: 1.4;
}
.log-row.accepted {
border-left-color: #6ee7a8;
}
.log-row.rejected {
border-left-color: #ff6b81;
}
.log-row.info {
border-left-color: #51c7ff;
}
.log-time {
color: #62788c;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.log-message b {
color: #edf7ff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 9px;
}
.footer {
display: flex;
justify-content: space-between;
gap: 20px;
padding: 9px 18px;
border-top: 1px solid var(--line);
color: #61768a;
background: #06101b;
font-size: 9px;
letter-spacing: 0.02em;
}
.footer b {
color: #91a8ba;
}
@media (max-width: 900px) {
body {
overflow: auto;
}
.shell {
height: auto;
min-height: 100vh;
}
.layout {
grid-template-columns: 1fr;
}
.stage {
height: 70vh;
min-height: 560px;
}
aside {
overflow: visible;
border-top: 1px solid var(--line);
border-left: 0;
}
}
@media (max-width: 600px) {
.topbar {
min-height: 68px;
padding: 12px 14px;
}
.top-meta {
display: none;
}
.stage {
height: 72vh;
min-height: 590px;
}
.readouts {
top: 10px;
right: 10px;
left: 10px;
grid-template-columns: 1fr 1fr;
}
.readout {
padding: 10px;
}
.readout:last-child {
grid-column: 1 / -1;
}
.readout strong {
font-size: 17px;
}
.world-label.state-label {
min-width: 105px;
padding: 7px 8px;
}
.world-label.state-label strong {
font-size: 11px;
}
.transition-label {
padding: 5px 6px;
}
.transition-label b {
font-size: 8px;
}
.transition-label span {
font-size: 7px;
}
.camera-hint {
display: none;
}
.stage-footer {
justify-content: flex-end;
}
.footer {
flex-direction: column;
gap: 4px;
}
}
</style>
</head>
<body>
<div class="shell">
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<div class="eyebrow">Finite-state machine / signal relay</div>
<h1>Event-Driven Signal Model</h1>
</div>
</div>
<div class="top-meta">
<span class="live-dot"></span>
<span>Three states</span>
<span>•</span>
<span>Three permitted transitions</span>
<span>•</span>
<span>No automatic state changes</span>
</div>
</header>
<main class="layout">
<section class="stage" id="stage" aria-label="Interactive 3D finite-state machine">
<canvas id="scene" aria-label="Low-poly three-state signal model"></canvas>
<div class="readouts" aria-label="Machine status">
<div class="readout state-readout">
<span class="readout-label">Current state</span>
<strong id="currentState">IDLE</strong>
<div class="state-chips">
<span class="state-chip active" data-chip="IDLE">IDLE</span>
<span class="state-chip" data-chip="ARMED">ARMED</span>
<span class="state-chip" data-chip="ACTIVE">ACTIVE</span>
</div>
</div>
<div class="readout">
<span class="readout-label">Elapsed in current state</span>
<strong id="elapsed">0.00 s</strong>
<div class="timer-track"><div class="timer-fill" id="timerFill"></div></div>
</div>
<div class="readout">
<span class="readout-label">Next transition</span>
<strong id="nextEvent">ENABLE</strong>
<small id="nextDetail">Guard opens in 0.80 s</small>
</div>
</div>
<div id="labels"></div>
<div class="stage-footer">
<div class="camera-hint">Drag to orbit • Wheel to zoom • Double-click to reset view</div>
<button class="view-button" id="resetView" type="button">Reset camera</button>
</div>
</section>
<aside>
<section class="side-section">
<div class="section-heading">
<h2>Transition contract</h2>
<span>complete relation</span>
</div>
<div class="contract-row">
<span class="state-token token-idle">IDLE</span>
<div class="contract-event">
<b>ENABLE</b>
<small>dwell ≥ 0.80 s</small>
</div>
<span class="contract-arrow">→</span>
<span class="state-token token-armed">ARMED</span>
</div>
<div class="contract-row">
<span class="state-token token-armed">ARMED</span>
<div class="contract-event">
<b>TRIGGER</b>
<small>dwell ≥ 1.20 s</small>
</div>
<span class="contract-arrow">→</span>
<span class="state-token token-active">ACTIVE</span>
</div>
<div class="contract-row">
<span class="state-token token-active">ACTIVE</span>
<div class="contract-event">
<b>RESET</b>
<small>immediate</small>
</div>
<span class="contract-arrow">→</span>
<span class="state-token token-idle">IDLE</span>
</div>
<p class="semantic-note">
<b>State and time are separate.</b>
The state is <em>IDLE</em>, <em>ARMED</em>, or <em>ACTIVE</em>.
The elapsed timer is only dwell time inside that state.
A clock tick never performs a transition.
</p>
</section>
<section class="side-section">
<div class="section-heading">
<h2>Event console</h2>
<span>manual input only</span>
</div>
<div class="clock-row">
<span class="clock-status" id="clockStatus">Dwell clock running</span>
<button class="clock-button" id="pauseClock" type="button" aria-pressed="false">
Ⅱ Pause dwell clock
</button>
</div>
<button class="event-button" data-event="ENABLE" type="button">
<span class="shortcut">E</span>
<span class="event-copy">
<strong>ENABLE</strong>
<small>IDLE → ARMED · guard 0.80 s</small>
</span>
<span class="event-status">waiting</span>
</button>
<button class="event-button" data-event="TRIGGER" type="button">
<span class="shortcut">T</span>
<span class="event-copy">
<strong>TRIGGER</strong>
<small>ARMED → ACTIVE · guard 1.20 s</small>
</span>
<span class="event-status">waiting</span>
</button>
<button class="event-button" data-event="RESET" type="button">
<span class="shortcut">R</span>
<span class="event-copy">
<strong>RESET</strong>
<small>ACTIVE → IDLE · immediate</small>
</span>
<span class="event-status">waiting</span>
</button>
</section>
<section class="side-section">
<div class="section-heading">
<h2>Event log</h2>
<span>newest first</span>
</div>
<div id="eventLog" role="log" aria-live="polite"></div>
</section>
</aside>
</main>
<footer class="footer">
<span><b>Only the three drawn arrows are legal.</b> All other event/state combinations are rejected.</span>
<span>Keyboard: <b>E</b> enable · <b>T</b> trigger · <b>R</b> reset</span>
</footer>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => [...document.querySelectorAll(selector)];
const stage = $("#stage");
const canvas = $("#scene");
const labelLayer = $("#labels");
const stateDefinitions = {
IDLE: {
name: "IDLE",
color: 0x51c7ff,
x: -4,
z: 1,
threshold: 0.8,
event: "ENABLE"
},
ARMED: {
name: "ARMED",
color: 0xffc857,
x: 0,
z: -2.2,
threshold: 1.2,
event: "TRIGGER"
},
ACTIVE: {
name: "ACTIVE",
color: 0xff5d8f,
x: 4,
z: 1,
threshold: 0,
event: "RESET"
}
};
const transitions = [
{
event: "ENABLE",
from: "IDLE",
to: "ARMED",
threshold: 0.8,
color: 0xffc857,
start: new THREE.Vector3(-3.35, 0.78, 0.32),
control: new THREE.Vector3(-1.75, 1.65, -1.45),
end: new THREE.Vector3(0, 0.78, -1.35)
},
{
event: "TRIGGER",
from: "ARMED",
to: "ACTIVE",
threshold: 1.2,
color: 0xff5d8f,
start: new THREE.Vector3(0.62, 0.78, -1.55),
control: new THREE.Vector3(1.75, 1.65, -0.4),
end: new THREE.Vector3(3.34, 0.78, 0.43)
},
{
event: "RESET",
from: "ACTIVE",
to: "IDLE",
threshold: 0,
color: 0x51c7ff,
start: new THREE.Vector3(3.78, 0.72, 1.86),
control: new THREE.Vector3(0, 0.48, 3.65),
end: new THREE.Vector3(-3.78, 0.72, 1.86)
}
];
let currentState = "IDLE";
let stateElapsed = 0;
let simulationTime = 0;
let paused = false;
let lastFrame = performance.now();
let lastHudUpdate = 0;
let width = 1;
let height = 1;
let hasSized = false;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.FogExp2(0x07111d, 0.027);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 0.7, 0);
let cameraTheta = 0.12;
let cameraPhi = 0.96;
let cameraRadius = 15.5;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraTheta = 0.12;
cameraPhi = 0.96;
cameraRadius = width < 650 ? 18.2 : 15.5;
updateCamera();
}
// Lighting.
scene.add(new THREE.HemisphereLight(0xb9dcff, 0x07101a, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0006;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ea8ff, 1.25);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
// Low-poly ground.
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(32, 22),
new THREE.MeshStandardMaterial({
color: 0x091725,
roughness: 0.94,
metalness: 0.02
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 16, 0x24455e, 0x173147);
grid.position.y = 0.006;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const nodeObjects = new Map();
function lowPolyMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.62,
metalness: 0.12,
flatShading: true,
...options
});
}
function createStateNode(key, definition, index) {
const group = new THREE.Group();
group.position.set(definition.x, 0, definition.z);
const darkBaseMaterial = lowPolyMaterial(0x14283b);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(1.06, 1.23, 0.38, 6),
darkBaseMaterial
);
base.position.y = 0.19;
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const upperMaterial = lowPolyMaterial(definition.color, {
roughness: 0.5,
metalness: 0.2
});
const upper = new THREE.Mesh(
new THREE.CylinderGeometry(0.86, 0.96, 0.18, 6),
upperMaterial
);
upper.position.y = 0.46;
upper.castShadow = true;
upper.receiveShadow = true;
group.add(upper);
const ringMaterial = lowPolyMaterial(definition.color, {
emissive: definition.color,
emissiveIntensity: 0.08
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.82, 0.045, 4, 18),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.y = 0.58;
group.add(ring);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.16, 1.25, 5),
lowPolyMaterial(0x365166)
);
post.position.y = 1.08;
post.castShadow = true;
group.add(post);
let symbolGeometry;
if (key === "IDLE") {
symbolGeometry = new THREE.BoxGeometry(0.7, 0.7, 0.7);
} else if (key === "ARMED") {
symbolGeometry = new THREE.ConeGeometry(0.52, 0.9, 5);
} else {
symbolGeometry = new THREE.OctahedronGeometry(0.58, 0);
}
const symbolMaterial = lowPolyMaterial(definition.color, {
emissive: definition.color,
emissiveIntensity: 0.08,
roughness: 0.4,
metalness: 0.18
});
const symbol = new THREE.Mesh(symbolGeometry, symbolMaterial);
symbol.position.y = 1.96;
symbol.castShadow = true;
group.add(symbol);
const light = new THREE.PointLight(definition.color, 0.05, 4.5, 2);
light.position.y = 2.05;
group.add(light);
scene.add(group);
const label = document.createElement("div");
label.className = "world-label state-label";
label.style.setProperty(
"--tone",
`#${new THREE.Color(definition.color).getHexString()}`
);
label.innerHTML = `
<strong>${definition.name}</strong>
<small class="state-time">STATE</small>
`;
labelLayer.appendChild(label);
nodeObjects.set(key, {
group,
symbol,
symbolMaterial,
ring,
ringMaterial,
light,
label,
labelPosition: new THREE.Vector3(definition.x, 3.12, definition.z),
baseSymbolY: symbol.position.y,
index
});
}
Object.entries(stateDefinitions).forEach(([key, definition], index) => {
createStateNode(key, definition, index);
});
const up = new THREE.Vector3(0, 1, 0);
const white = new THREE.Color(0xffffff);
function createArrow(transition) {
const curve = new THREE.QuadraticBezierCurve3(
transition.start,
transition.control,
transition.end
);
const material = lowPolyMaterial(transition.color, {
emissive: transition.color,
emissiveIntensity: 0.04,
roughness: 0.52,
transparent: true,
opacity: 0.72
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 32, 0.075, 6, false),
material
);
tube.castShadow = true;
scene.add(tube);
const headMaterial = material.clone();
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.22, 0.54, 6),
headMaterial
);
const headPosition = curve.getPointAt(0.982);
const tangent = curve.getTangentAt(0.982).normalize();
head.position.copy(headPosition);
head.quaternion.setFromUnitVectors(up, tangent);
head.castShadow = true;
scene.add(head);
const label = document.createElement("div");
label.className = "world-label transition-label";
label.innerHTML = `
<b>${transition.event}</b>
<span>${
transition.threshold > 0
? `dwell ≥ ${transition.threshold.toFixed(2)} s`
: "immediate · event required"
}</span>
`;
labelLayer.appendChild(label);
const labelPosition = curve.getPointAt(0.52).add(
new THREE.Vector3(0, transition.event === "RESET" ? 0.16 : 0.34, 0)
);
transition.curve = curve;
transition.tube = tube;
transition.material = material;
transition.headMaterial = headMaterial;
transition.label = label;
transition.labelPosition = labelPosition;
transition.pulse = 0;
}
transitions.forEach(createArrow);
function projectLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (visible) {
element.style.left = `${((projected.x + 1) / 2) * width}px`;
element.style.top = `${((1 - projected.y) / 2) * height}px`;
}
}
function hexCss(value) {
return `#${value.toString(16).padStart(6, "0")}`;
}
function addLog(kind, event, message) {
const row = document.createElement("div");
row.className = `log-row ${kind}`;
row.innerHTML = `
<span class="log-time">${simulationTime.toFixed(2)}s</span>
<span class="log-message"><b>${event}</b> — ${message}</span>
`;
$("#eventLog").prepend(row);
while ($("#eventLog").children.length > 8) {
$("#eventLog").lastElementChild.remove();
}
}
function sendEvent(eventName) {
const transition = transitions.find((item) => item.event === eventName);
if (!transition) {
return;
}
if (currentState !== transition.from) {
addLog(
"rejected",
eventName,
`not permitted in ${currentState}`
);
updateHud();
return;
}
if (stateElapsed + 1e-6 < transition.threshold) {
addLog(
"rejected",
eventName,
`guard not met: ${stateElapsed.toFixed(2)} / ${transition.threshold.toFixed(2)} s`
);
updateHud();
return;
}
const previousState = currentState;
currentState = transition.to;
stateElapsed = 0;
transition.pulse = 1;
transition.label.classList.remove("flash");
void transition.label.offsetWidth;
transition.label.classList.add("flash");
addLog(
"accepted",
eventName,
`${previousState} → ${currentState}`
);
updateHud();
}
function updateHud() {
const definition = stateDefinitions[currentState];
const color = hexCss(definition.color);
document.documentElement.style.setProperty("--state-color", color);
$("#currentState").textContent = definition.name;
$("#elapsed").textContent = `${stateElapsed.toFixed(2)} s`;
$("#nextEvent").textContent = definition.event;
let nextDetail;
if (definition.threshold === 0) {
nextDetail = "Immediate · event required";
} else if (stateElapsed + 1e-6 >= definition.threshold) {
nextDetail = "Guard satisfied · waiting for event";
} else {
nextDetail = `Guard opens in ${(
definition.threshold - stateElapsed
).toFixed(2)} s`;
}
$("#nextDetail").textContent = nextDetail;
const progress =
definition.threshold > 0
? Math.min(100, (stateElapsed / definition.threshold) * 100)
: 100;
$("#timerFill").style.width = `${progress}%`;
$$(".state-chip").forEach((chip) => {
chip.classList.toggle("active", chip.dataset.chip === currentState);
});
$$(".event-button").forEach((button) => {
const transition = transitions.find(
(item) => item.event === button.dataset.event
);
const status = button.querySelector(".event-status");
button.classList.remove("ready", "waiting", "wrong");
if (currentState !== transition.from) {
button.classList.add("wrong");
status.textContent = "wrong state";
} else if (stateElapsed + 1e-6 < transition.threshold) {
button.classList.add("waiting");
status.textContent = `${stateElapsed.toFixed(
2
)}/${transition.threshold.toFixed(2)} s`;
} else {
button.classList.add("ready");
status.textContent = "ready";
}
});
nodeObjects.forEach((node, key) => {
const isCurrent = key === currentState;
node.label.classList.toggle("current", isCurrent);
node.label.querySelector(".state-time").textContent = isCurrent
? `CURRENT · t+ ${stateElapsed.toFixed(2)} s`
: "STATE";
});
}
$$(".event-button").forEach((button) => {
button.addEventListener("click", () => {
sendEvent(button.dataset.event);
});
});
$("#pauseClock").addEventListener("click", () => {
paused = !paused;
$("#pauseClock").textContent = paused
? "▶ Resume dwell clock"
: "Ⅱ Pause dwell clock";
$("#pauseClock").setAttribute("aria-pressed", String(paused));
$("#clockStatus").textContent = paused
? "Dwell clock paused"
: "Dwell clock running";
});
window.addEventListener("keydown", (event) => {
if (event.repeat || event.ctrlKey || event.metaKey || event.altKey) {
return;
}
const key = event.key.toLowerCase();
if (key === "e") {
sendEvent("ENABLE");
} else if (key === "t") {
sendEvent("TRIGGER");
} else if (key === "r") {
sendEvent("RESET");
}
});
// Custom orbit and zoom controls; no Three.js examples/addons are used.
let dragging = false;
let dragX = 0;
let dragY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragX = event.clientX;
dragY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) {
return;
}
const deltaX = event.clientX - dragX;
const deltaY = event.clientY - dragY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.55,
1.35
);
dragX = event.clientX;
dragY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
10,
24
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
$("#resetView").addEventListener("click", resetCamera);
function resize() {
const rect = stage.getBoundingClientRect();
width = Math.max(1, rect.width);
height = Math.max(1, rect.height);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
if (!hasSized) {
cameraRadius = width < 650 ? 18.2 : 15.5;
hasSized = true;
}
updateCamera();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(stage);
resize();
updateHud();
addLog("info", "FSM", "Initialized in IDLE; transitions require explicit events");
function animate(now) {
const realDelta = Math.min((now - lastFrame) / 1000, 0.05);
lastFrame = now;
if (!paused) {
simulationTime += realDelta;
stateElapsed += realDelta;
}
nodeObjects.forEach((node, key) => {
const isCurrent = key === currentState;
const phase = now * 0.002 + node.index * 1.7;
node.symbol.rotation.y += realDelta * (isCurrent ? 0.72 : 0.08);
node.symbol.rotation.x = isCurrent
? Math.sin(now * 0.00085 + node.index) * 0.09
: 0;
node.symbol.position.y =
node.baseSymbolY + Math.sin(phase) * (isCurrent ? 0.055 : 0.018);
node.symbolMaterial.emissiveIntensity = isCurrent
? 0.45 + Math.sin(phase) * 0.12
: 0.045;
node.ringMaterial.emissiveIntensity = isCurrent ? 1.15 : 0.07;
node.light.intensity = isCurrent ? 2.15 : 0.04;
const ringScale = isCurrent ? 1 + Math.sin(phase) * 0.018 : 1;
node.ring.scale.setScalar(ringScale);
});
transitions.forEach((transition) => {
const isCurrentRoute = transition.from === currentState;
const guardReady =
isCurrentRoute && stateElapsed + 1e-6 >= transition.threshold;
transition.pulse = Math.max(0, transition.pulse - realDelta * 2.2);
transition.material.color.set(transition.color);
if (guardReady) {
transition.material.color.lerp(white, 0.2);
transition.material.opacity = 1;
transition.material.emissiveIntensity = 0.22 + transition.pulse * 0.8;
transition.label.classList.add("ready");
transition.label.classList.remove("waiting");
} else if (isCurrentRoute) {
transition.material.opacity = 0.9;
transition.material.emissiveIntensity = 0.08 + transition.pulse * 0.8;
transition.label.classList.add("waiting");
transition.label.classList.remove("ready");
} else {
transition.material.opacity = 0.34;
transition.material.emissiveIntensity = 0.015;
transition.label.classList.remove("ready", "waiting");
}
transition.headMaterial.color.copy(transition.material.color);
transition.headMaterial.opacity = transition.material.opacity;
transition.headMaterial.emissive.copy(transition.material.emissive);
transition.headMaterial.emissiveIntensity =
transition.material.emissiveIntensity;
});
if (now - lastHudUpdate > 50) {
updateHud();
lastHudUpdate = now;
}
nodeObjects.forEach((node) => {
projectLabel(node.label, node.labelPosition);
});
transitions.forEach((transition) => {
projectLabel(transition.label, transition.labelPosition);
});
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```