Prompt specification
Build or customize the Three.js 3D scene "State-machine counterexample tester" 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: State-machine counterexample tester
Category: Algorithms & Systems · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/state-machine-counterexample-tester-x1880
Scene Description Prompt:
An interactive low-poly elevator controller that rejects movement while its doors are open. Build one HTML file with Three.js from cdnjs.
Working Three.js Starter (state-machine-counterexample-tester-x1880):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Elevator Controller</title>
<style>
:root {
--ink: #173039;
--panel: rgba(19, 42, 49, 0.92);
--panel-soft: rgba(255, 255, 255, 0.08);
--mint: #72e1c1;
--amber: #ffc76a;
--coral: #ff776d;
--white: #f4f6ef;
--muted: #9eb2b4;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce6e2;
color: var(--white);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.brand {
position: fixed;
top: 24px;
left: 24px;
z-index: 5;
display: flex;
align-items: center;
gap: 12px;
padding: 12px 16px 12px 12px;
border: 1px solid rgba(23, 48, 57, 0.12);
border-radius: 16px;
background: rgba(244, 246, 239, 0.84);
color: var(--ink);
box-shadow: 0 12px 35px rgba(23, 48, 57, 0.12);
backdrop-filter: blur(14px);
pointer-events: none;
}
.brand-mark {
position: relative;
width: 38px;
height: 38px;
border: 3px solid var(--ink);
border-radius: 8px;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
top: 3px;
bottom: 3px;
width: 12px;
border: 2px solid var(--ink);
}
.brand-mark::before {
left: 3px;
border-right: 0;
}
.brand-mark::after {
right: 3px;
border-left: 0;
}
.brand strong {
display: block;
font-size: 14px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand span {
display: block;
margin-top: 2px;
color: #577078;
font-size: 11px;
letter-spacing: 0.04em;
}
.controller {
position: fixed;
z-index: 10;
top: 24px;
right: 24px;
width: min(318px, calc(100vw - 48px));
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 24px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(23, 48, 57, 0.27);
backdrop-filter: blur(18px);
}
.eyebrow {
margin: 0 0 7px;
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.readout {
display: grid;
grid-template-columns: 1fr auto;
align-items: end;
gap: 12px;
padding-bottom: 15px;
border-bottom: 1px solid rgba(255, 255, 255, 0.09);
}
.floor-number {
display: flex;
align-items: baseline;
gap: 9px;
}
#floorValue {
font-size: 58px;
font-weight: 800;
line-height: 0.9;
letter-spacing: -0.08em;
}
#floorName {
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.target {
text-align: right;
}
.target small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
#targetValue {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
.interlock {
display: flex;
align-items: center;
gap: 9px;
margin: 14px 0;
padding: 10px 12px;
border-radius: 12px;
background: rgba(255, 119, 109, 0.11);
color: #ffaaa3;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
transition: 180ms ease;
}
.interlock::before {
content: "";
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
.interlock.ready {
background: rgba(114, 225, 193, 0.11);
color: var(--mint);
}
.interlock.moving {
background: rgba(255, 199, 106, 0.12);
color: var(--amber);
}
.section-label {
display: flex;
justify-content: space-between;
margin: 16px 0 8px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.section-label span:last-child {
color: #668086;
letter-spacing: 0.04em;
}
.floor-grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 8px;
}
.control-button {
position: relative;
min-height: 47px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 12px;
background: rgba(255, 255, 255, 0.07);
color: var(--white);
cursor: pointer;
transition:
transform 120ms ease,
background 160ms ease,
border-color 160ms ease,
box-shadow 160ms ease;
}
.control-button:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.25);
background: rgba(255, 255, 255, 0.12);
}
.control-button:active {
transform: translateY(1px) scale(0.98);
}
.control-button:focus-visible {
outline: 2px solid var(--mint);
outline-offset: 2px;
}
.floor-grid .control-button {
font-size: 16px;
font-weight: 800;
}
.floor-grid .control-button.active {
border-color: var(--mint);
background: rgba(114, 225, 193, 0.16);
color: var(--mint);
box-shadow: inset 0 0 0 1px rgba(114, 225, 193, 0.14);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.control-button small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.movement .control-button {
color: #a7d9ff;
}
.movement .control-button.locked {
border-color: rgba(255, 119, 109, 0.2);
color: #ffaaa3;
}
.movement .control-button.locked::after {
content: "LOCK";
position: absolute;
top: 4px;
right: 5px;
font-size: 6px;
font-weight: 800;
letter-spacing: 0.1em;
}
.doors .control-button.open {
border-color: var(--mint);
background: rgba(114, 225, 193, 0.16);
color: var(--mint);
}
.doors .control-button.close {
border-color: var(--amber);
background: rgba(255, 199, 106, 0.12);
color: var(--amber);
}
.door-track {
height: 4px;
margin-top: 9px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#doorProgress {
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--mint);
transform-origin: left;
transition: background 180ms ease;
}
.status {
display: flex;
align-items: flex-start;
gap: 9px;
min-height: 52px;
margin-top: 14px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
background: rgba(0, 0, 0, 0.12);
color: #c7d3d2;
font-size: 11px;
line-height: 1.45;
}
.status-dot {
width: 7px;
height: 7px;
flex: 0 0 auto;
margin-top: 4px;
border-radius: 50%;
background: var(--muted);
}
.status.danger {
border-color: rgba(255, 119, 109, 0.22);
color: #ffc0ba;
}
.status.danger .status-dot {
background: var(--coral);
box-shadow: 0 0 10px var(--coral);
}
.status.success .status-dot {
background: var(--mint);
box-shadow: 0 0 10px var(--mint);
}
.status.busy .status-dot {
background: var(--amber);
box-shadow: 0 0 10px var(--amber);
}
.toast {
position: fixed;
z-index: 20;
top: 25px;
left: 50%;
max-width: min(430px, calc(100vw - 40px));
padding: 13px 18px;
border: 1px solid rgba(255, 119, 109, 0.45);
border-radius: 12px;
background: #7e2f2b;
color: #fff4ef;
box-shadow: 0 14px 40px rgba(70, 26, 24, 0.25);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-align: center;
text-transform: uppercase;
opacity: 0;
pointer-events: none;
transform: translate(-50%, -14px);
transition: 200ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.help {
position: fixed;
z-index: 5;
bottom: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid rgba(23, 48, 57, 0.12);
border-radius: 12px;
background: rgba(244, 246, 239, 0.82);
color: #526c72;
box-shadow: 0 10px 30px rgba(23, 48, 57, 0.1);
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
}
.help kbd {
padding: 3px 5px;
border: 1px solid rgba(23, 48, 57, 0.18);
border-bottom-width: 2px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
font: 9px ui-monospace, monospace;
}
#resetCamera {
border: 1px solid rgba(23, 48, 57, 0.18);
border-radius: 8px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
cursor: pointer;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 720px) {
.brand {
top: 12px;
left: 12px;
padding: 9px 12px 9px 9px;
}
.brand span {
display: none;
}
.controller {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
border-radius: 20px;
}
.readout {
padding-bottom: 10px;
}
#floorValue {
font-size: 44px;
}
.interlock {
margin: 10px 0;
}
.section-label {
margin-top: 11px;
}
.control-button {
min-height: 42px;
}
.status {
min-height: 40px;
margin-top: 10px;
}
.help {
display: none;
}
.toast {
top: 74px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive low-poly elevator scene"></canvas>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<strong>Vertical 04</strong>
<span>Low-poly elevator interlock</span>
</div>
</div>
<section class="controller" aria-label="Elevator controller">
<p class="eyebrow">Elevator Controller</p>
<div class="readout">
<div class="floor-number">
<span id="floorValue">G</span>
<span id="floorName">Lobby</span>
</div>
<div class="target">
<small>Target</small>
<span id="targetValue">G</span>
</div>
</div>
<div class="interlock" id="interlock">
Locked — doors open
</div>
<div class="section-label">
<span>Select floor</span>
<span>Keys 0–3</span>
</div>
<div class="floor-grid" id="floorButtons">
<button class="control-button active" data-floor="0" aria-label="Go to lobby">G</button>
<button class="control-button" data-floor="1" aria-label="Go to floor 1">1</button>
<button class="control-button" data-floor="2" aria-label="Go to floor 2">2</button>
<button class="control-button" data-floor="3" aria-label="Go to floor 3">3</button>
</div>
<div class="section-label">
<span>Travel</span>
<span>Arrow keys</span>
</div>
<div class="button-row movement">
<button class="control-button locked" id="moveUp">
▲ UP
<small>One floor</small>
</button>
<button class="control-button locked" id="moveDown">
▼ DOWN
<small>One floor</small>
</button>
</div>
<div class="section-label">
<span>Door interlock</span>
<span>O / C</span>
</div>
<div class="button-row doors">
<button class="control-button open" id="openDoors">OPEN</button>
<button class="control-button close" id="closeDoors">CLOSE</button>
</div>
<div class="door-track" aria-hidden="true">
<div id="doorProgress"></div>
</div>
<div class="status" id="statusBox" role="status" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Doors are open. Close them before moving.</span>
</div>
</section>
<div class="toast" id="toast" role="alert"></div>
<div class="help">
<span>Drag to orbit</span>
<span>•</span>
<span>Scroll to zoom</span>
<span>•</span>
<kbd>R</kbd>
<button id="resetCamera">Reset view</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// DOM and application state
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const floorValue = document.querySelector("#floorValue");
const floorName = document.querySelector("#floorName");
const targetValue = document.querySelector("#targetValue");
const interlock = document.querySelector("#interlock");
const statusBox = document.querySelector("#statusBox");
const statusText = document.querySelector("#statusText");
const toast = document.querySelector("#toast");
const doorProgressBar = document.querySelector("#doorProgress");
const floorButtons = [...document.querySelectorAll("[data-floor]")];
const moveUpButton = document.querySelector("#moveUp");
const moveDownButton = document.querySelector("#moveDown");
const openDoorsButton = document.querySelector("#openDoors");
const closeDoorsButton = document.querySelector("#closeDoors");
const FLOORS = [
{ name: "G", label: "Lobby", y: 0 },
{ name: "1", label: "Studio", y: 4.2 },
{ name: "2", label: "Gallery", y: 8.4 },
{ name: "3", label: "Sky", y: 12.6 }
];
const state = {
currentFloor: 0,
targetFloor: 0,
moving: false,
moveElapsed: 0,
moveDuration: 0,
moveFromY: 0,
moveToY: 0,
doorTarget: 1,
doorProgress: 1,
shakeTime: 0,
rejectFlash: 0,
toastTimer: null
};
// ------------------------------------------------------------
// Renderer, camera, and lights
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce6e2);
scene.fog = new THREE.Fog(0xdce6e2, 22, 42);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(0.3, 6.25, 0);
const cameraState = {
yaw: 0.52,
pitch: 0.17,
radius: 17.5
};
function updateCamera() {
const { yaw, pitch, radius } = cameraState;
const horizontal = Math.cos(pitch) * radius;
camera.position.set(
cameraTarget.x + Math.sin(yaw) * horizontal,
cameraTarget.y + Math.sin(pitch) * radius,
cameraTarget.z + Math.cos(yaw) * horizontal
);
camera.lookAt(cameraTarget);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf6fff8, 0x536a68, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d4, 3.2);
keyLight.position.set(7, 15, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0008;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x9edbff, 1.25);
rimLight.position.set(-8, 8, -8);
scene.add(rimLight);
// ------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.05,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0xb8c8c0),
grid: new THREE.LineBasicMaterial({
color: 0x8da49d,
transparent: true,
opacity: 0.24
}),
shaft: standardMaterial(0x31464c, { metalness: 0.38, roughness: 0.58 }),
shaftLight: standardMaterial(0x526b70, { metalness: 0.32 }),
shaftDark: standardMaterial(0x1d3036),
platform: standardMaterial(0x8da39e),
cabin: standardMaterial(0xe4aa49),
cabinDark: standardMaterial(0x7e5b31),
cabinLight: standardMaterial(0xffd78b),
door: standardMaterial(0xe8a447),
doorEdge: standardMaterial(0x9b672d),
window: standardMaterial(0x8fd0d0, {
transparent: true,
opacity: 0.68,
metalness: 0.12,
roughness: 0.24
}),
panel: standardMaterial(0x183039),
panelFace: standardMaterial(0x24434b),
mint: standardMaterial(0x66d9b8, {
emissive: 0x174f42,
emissiveIntensity: 0.45
}),
amber: standardMaterial(0xffbd58, {
emissive: 0x5b3210,
emissiveIntensity: 0.35
}),
coral: standardMaterial(0xff776d, {
emissive: 0x521813,
emissiveIntensity: 0.35
}),
blue: standardMaterial(0x8ac9ee, {
emissive: 0x173d55,
emissiveIntensity: 0.35
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x172a30,
transparent: true,
opacity: 0.34
});
function addBox(parent, width, height, depth, material, x = 0, y = 0, z = 0, outlined = false) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
edgeMaterial
);
mesh.add(edges);
}
return mesh;
}
function addCylinder(parent, radius, height, material, x, y, z, segments = 12) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, segments),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createLabelTexture(text, options = {}) {
const {
width = 512,
height = 128,
background = "transparent",
foreground = "#ffffff",
fontSize = 58,
font = "700 58px Arial",
align = "center"
} = options;
const labelCanvas = document.createElement("canvas");
labelCanvas.width = width;
labelCanvas.height = height;
const context = labelCanvas.getContext("2d");
if (background !== "transparent") {
context.fillStyle = background;
context.fillRect(0, 0, width, height);
}
context.fillStyle = foreground;
context.font = font;
context.textAlign = align;
context.textBaseline = "middle";
const x = align === "center" ? width / 2 : 12;
context.fillText(text, x, height / 2, width - 24);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
return texture;
}
function addLabel(
parent,
text,
width,
height,
x,
y,
z,
options = {}
) {
const texture = createLabelTexture(text, options);
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const label = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
label.position.set(x, y, z);
parent.add(label);
return label;
}
// ------------------------------------------------------------
// Ground and shaft
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.24;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 20, 0x8da49d, 0x9aafa7);
grid.position.y = -0.225;
grid.material.transparent = true;
grid.material.opacity = 0.24;
scene.add(grid);
const shaft = new THREE.Group();
scene.add(shaft);
const shaftTop = FLOORS[FLOORS.length - 1].y + 3.6;
const shaftHeight = shaftTop + 0.3;
for (const x of [-2.25, 2.25]) {
for (const z of [-1.55, 1.55]) {
addBox(
shaft,
0.18,
shaftHeight,
0.18,
materials.shaft,
x,
shaftHeight / 2 - 0.1,
z,
true
);
}
}
for (let i = 0; i < FLOORS.length; i++) {
const floor = FLOORS[i];
addBox(
shaft,
4.55,
0.16,
0.18,
materials.shaftLight,
0,
floor.y + 3.38,
1.55,
true
);
addBox(
shaft,
4.55,
0.16,
0.18,
materials.shaftLight,
0,
floor.y + 3.38,
-1.55,
true
);
for (const x of [-2.25, 2.25]) {
addBox(
shaft,
0.18,
0.16,
3.15,
materials.shaftLight,
x,
floor.y + 3.38,
0,
true
);
}
addBox(
shaft,
2.2,
0.18,
2.8,
materials.platform,
-3.22,
floor.y - 0.05,
0,
true
);
addBox(
shaft,
0.12,
1.15,
0.12,
materials.shaft,
-2.48,
floor.y + 0.58,
1.58,
true
);
addLabel(
shaft,
`${floor.name} · ${floor.label.toUpperCase()}`,
1.75,
0.42,
-2.48,
floor.y + 1.28,
1.66,
{
foreground: "#eff7ef",
fontSize: 45,
font: "800 45px Arial"
}
);
}
for (let i = 0; i < FLOORS.length - 1; i++) {
const floor = FLOORS[i];
const next = FLOORS[i + 1];
addBox(
shaft,
4.25,
next.y - floor.y - 0.14,
0.12,
i % 2 === 0 ? materials.shaftDark : materials.shaft,
0,
(floor.y + next.y) / 2,
-1.55
);
}
addBox(
shaft,
4.5,
0.22,
3.3,
materials.shaftLight,
0,
shaftTop,
0,
true
);
// ------------------------------------------------------------
// Elevator car and sliding doors
// ------------------------------------------------------------
const car = new THREE.Group();
car.position.y = FLOORS[0].y;
scene.add(car);
addBox(car, 3.6, 0.24, 3.08, materials.cabinDark, 0, 0.02, 0, true);
addBox(car, 3.38, 0.08, 2.84, materials.cabinLight, 0, 0.17, 0);
addBox(car, 3.82, 0.24, 3.25, materials.cabin, 0, 3.23, 0, true);
addBox(
car,
3.55,
3.0,
0.12,
materials.cabin,
0,
1.66,
-1.45,
true
);
const sideWallMaterial = standardMaterial(0xe9b85c, {
transparent: true,
opacity: 0.3,
depthWrite: false
});
addBox(car, 0.12, 3.0, 2.88, sideWallMaterial, -1.76, 1.66, 0);
addBox(car, 0.12, 3.0, 2.88, sideWallMaterial, 1.76, 1.66, 0);
addBox(car, 0.16, 3.2, 0.18, materials.cabinDark, -1.72, 1.65, 1.5, true);
addBox(car, 0.16, 3.2, 0.18, materials.cabinDark, 1.72, 1.65, 1.5, true);
addBox(car, 3.62, 0.34, 0.2, materials.cabinDark, 0, 3.16, 1.5, true);
addBox(car, 1.75, 0.05, 0.1, materials.cabinLight, 0, 3.06, 0.55);
addBox(car, 1.75, 0.05, 0.1, materials.cabinLight, 0, 3.06, -0.45);
const railMaterial = standardMaterial(0x73542d, {
metalness: 0.5,
roughness: 0.4
});
addBox(car, 2.6, 0.08, 0.08, railMaterial, 0, 1.0, -1.31, true);
for (const x of [-1.25, 0, 1.25]) {
addBox(car, 0.06, 0.7, 0.06, railMaterial, x, 1.35, -1.32);
}
const doorGroups = [];
for (const side of [-1, 1]) {
const doorGroup = new THREE.Group();
doorGroup.position.set(side * 0.82, 0, 1.56);
car.add(doorGroup);
addBox(
doorGroup,
1.62,
2.95,
0.13,
materials.door,
0,
1.58,
0,
true
);
addBox(
doorGroup,
0.6,
1.08,
0.045,
materials.window,
0,
1.93,
0.085,
true
);
addBox(
doorGroup,
0.035,
2.7,
0.025,
materials.doorEdge,
-side * 0.76,
1.58,
0.08
);
doorGroups.push({ group: doorGroup, side });
}
// ------------------------------------------------------------
// Freestanding 3D controller panel
// ------------------------------------------------------------
const panel = new THREE.Group();
panel.position.set(3.55, 0, 1.0);
scene.add(panel);
const panelMaterial = standardMaterial(0x183039, {
emissive: 0x000000,
emissiveIntensity: 0
});
addBox(panel, 1.86, 5.3, 0.42, panelMaterial, 0, 3.05, 0, true);
addBox(panel, 1.56, 0.78, 0.07, materials.shaftDark, 0, 4.74, 0.24, true);
addLabel(
panel,
"VERTICAL / 04",
1.25,
0.25,
0,
5.35,
0.225,
{
foreground: "#bcecdf",
fontSize: 35,
font: "800 35px Arial"
}
);
const screenCanvas = document.createElement("canvas");
screenCanvas.width = 512;
screenCanvas.height = 256;
const screenContext = screenCanvas.getContext("2d");
const screenTexture = new THREE.CanvasTexture(screenCanvas);
screenTexture.colorSpace = THREE.SRGBColorSpace;
const screenMaterial = new THREE.MeshBasicMaterial({
map: screenTexture,
toneMapped: false
});
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(1.42, 0.66),
screenMaterial
);
screen.position.set(0, 4.74, 0.285);
panel.add(screen);
const interactive3D = [];
const floor3DButtons = [];
function addPanelButton({
x,
y,
label,
material,
action,
color = "#ffffff"
}) {
const button = addCylinder(
panel,
0.205,
0.09,
material,
x,
y,
0.29,
12
);
button.rotation.x = Math.PI / 2;
button.userData.action = action;
interactive3D.push(button);
const buttonLabel = addLabel(
panel,
label,
0.31,
0.16,
x,
y,
0.342,
{
foreground: color,
fontSize: 65,
font: "800 65px Arial"
}
);
buttonLabel.userData.action = action;
return { mesh: button, label: buttonLabel, material };
}
floor3DButtons.push(
addPanelButton({
x: -0.4,
y: 3.82,
label: "G",
material: materials.mint,
action: { type: "floor", index: 0 },
color: "#102e2a"
})
);
floor3DButtons.push(
addPanelButton({
x: 0.4,
y: 3.82,
label: "1",
material: materials.blue,
action: { type: "floor", index: 1 },
color: "#102c3a"
})
);
floor3DButtons.push(
addPanelButton({
x: -0.4,
y: 3.18,
label: "2",
material: materials.blue,
action: { type: "floor", index: 2 },
color: "#102c3a"
})
);
floor3DButtons.push(
addPanelButton({
x: 0.4,
y: 3.18,
label: "3",
material: materials.blue,
action: { type: "floor", index: 3 },
color: "#102c3a"
})
);
const up3D = addPanelButton({
x: -0.4,
y: 2.48,
label: "▲",
material: materials.blue,
action: { type: "up" },
color: "#102c3a"
});
const down3D = addPanelButton({
x: 0.4,
y: 2.48,
label: "▼",
material: materials.blue,
action: { type: "down" },
color: "#102c3a"
});
const open3D = addPanelButton({
x: -0.4,
y: 1.73,
label: "O",
material: materials.mint,
action: { type: "open" },
color: "#102e2a"
});
const close3D = addPanelButton({
x: 0.4,
y: 1.73,
label: "C",
material: materials.amber,
action: { type: "close" },
color: "#3d250c"
});
addLabel(
panel,
"DOOR CONTROL",
1.15,
0.23,
0,
1.13,
0.225,
{
foreground: "#88a4a8",
fontSize: 31,
font: "700 31px Arial"
}
);
// ------------------------------------------------------------
// UI and controller logic
// ------------------------------------------------------------
function setStatus(message, type = "neutral") {
statusText.textContent = message;
statusBox.className = `status ${type}`;
}
function showToast(message) {
clearTimeout(state.toastTimer);
toast.textContent = message;
toast.classList.add("show");
state.toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2200);
}
let audioContext = null;
function rejectionSound() {
try {
const AudioContextClass =
window.AudioContext || window.webkitAudioContext;
if (!AudioContextClass) return;
if (!audioContext) {
audioContext = new AudioContextClass();
}
if (audioContext.state === "suspended") {
audioContext.resume();
}
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
const now = audioContext.currentTime;
oscillator.type = "square";
oscillator.frequency.setValueAtTime(150, now);
oscillator.frequency.exponentialRampToValueAtTime(95, now + 0.18);
gain.gain.setValueAtTime(0.0001, now);
gain.gain.exponentialRampToValueAtTime(0.055, now + 0.015);
gain.gain.exponentialRampToValueAtTime(0.0001, now + 0.22);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start(now);
oscillator.stop(now + 0.23);
} catch {
// Audio is optional.
}
}
function rejectMovement(reason) {
const message = `Movement rejected — ${reason}`;
setStatus(message, "danger");
showToast(message);
state.shakeTime = 0.45;
state.rejectFlash = 1;
rejectionSound();
updateUI();
}
function doorsAreOpen() {
return state.doorProgress > 0.015 || state.doorTarget > 0;
}
function requestFloor(index) {
if (state.moving) {
rejectMovement("the elevator is already moving");
return;
}
if (index === state.currentFloor) {
setStatus(`Already at ${FLOORS[index].label}.`, "success");
return;
}
if (doorsAreOpen()) {
rejectMovement("close the doors first");
return;
}
state.targetFloor = index;
state.moving = true;
state.moveElapsed = 0;
state.moveFromY = car.position.y;
state.moveToY = FLOORS[index].y;
state.moveDuration =
1.35 + Math.abs(index - state.currentFloor) * 0.75;
setStatus(
`Moving to ${FLOORS[index].label}…`,
"busy"
);
updateUI();
}
function requestAdjacent(direction) {
if (state.moving) {
rejectMovement("the elevator is already moving");
return;
}
const nextFloor = state.currentFloor + direction;
if (nextFloor < 0 || nextFloor >= FLOORS.length) {
rejectMovement(
direction > 0 ? "this is the top floor" : "this is the bottom floor"
);
return;
}
requestFloor(nextFloor);
}
function openDoors() {
if (state.moving) {
rejectMovement("doors cannot open while moving");
return;
}
if (state.doorTarget === 1 && state.doorProgress > 0.99) {
setStatus("Doors are already open.", "success");
return;
}
state.doorTarget = 1;
setStatus("Opening doors…", "busy");
updateUI();
}
function closeDoors() {
if (state.moving) {
rejectMovement("doors cannot close while moving");
return;
}
if (state.doorTarget === 0 && state.doorProgress < 0.01) {
setStatus("Doors are already closed.", "success");
return;
}
state.doorTarget = 0;
setStatus("Closing doors…", "busy");
updateUI();
}
function dispatchAction(action) {
if (!action) return;
if (action.type === "floor") {
requestFloor(action.index);
} else if (action.type === "up") {
requestAdjacent(1);
} else if (action.type === "down") {
requestAdjacent(-1);
} else if (action.type === "open") {
openDoors();
} else if (action.type === "close") {
closeDoors();
}
}
function updateScreen() {
const width = screenCanvas.width;
const height = screenCanvas.height;
const context = screenContext;
context.fillStyle = "#10292f";
context.fillRect(0, 0, width, height);
context.fillStyle = "#68dfbd";
context.fillRect(0, 0, width, 7);
context.font = "700 23px Arial";
context.fillStyle = "#83a9aa";
context.textAlign = "left";
context.textBaseline = "alphabetic";
context.fillText("CURRENT FLOOR", 28, 48);
context.font = "800 112px Arial";
context.fillStyle = state.moving ? "#ffd071" : "#f0f7ed";
context.fillText(
state.moving
? FLOORS[state.targetFloor].name
: FLOORS[state.currentFloor].name,
24,
158
);
context.font = "700 21px Arial";
context.fillStyle = "#8fb0b0";
context.textAlign = "right";
context.fillText(
state.moving ? "IN TRANSIT" : "INTERLOCK READY",
484,
48
);
context.font = "600 18px Arial";
context.fillStyle = "#d4e1da";
context.textAlign = "left";
const currentStatus = statusText.textContent;
const shortStatus =
currentStatus.length > 43
? `${currentStatus.slice(0, 40)}…`
: currentStatus;
context.fillText(shortStatus, 28, 220);
screenTexture.needsUpdate = true;
}
function updateUI() {
const current = FLOORS[state.currentFloor];
const target = FLOORS[state.targetFloor];
floorValue.textContent = current.name;
floorName.textContent = current.label;
targetValue.textContent =
state.moving && state.targetFloor !== state.currentFloor
? `${target.name} →`
: current.name;
floorButtons.forEach((button, index) => {
const active =
index === state.currentFloor ||
(state.moving && index === state.targetFloor);
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
const locked = doorsAreOpen() && !state.moving;
moveUpButton.classList.toggle("locked", locked);
moveDownButton.classList.toggle("locked", locked);
openDoorsButton.classList.toggle("open", state.doorTarget === 1);
closeDoorsButton.classList.toggle("close", state.doorTarget === 0);
openDoorsButton.disabled = state.moving;
closeDoorsButton.disabled = state.moving;
if (state.moving) {
interlock.textContent = "In motion — travel enabled";
interlock.className = "interlock moving";
} else if (locked) {
interlock.textContent = "Locked — doors open";
interlock.className = "interlock";
} else {
interlock.textContent = "Ready — doors closed";
interlock.className = "interlock ready";
}
doorProgressBar.style.transform = `scaleX(${state.doorProgress})`;
doorProgressBar.style.background =
state.doorTarget === 0 ? "#ffc76a" : "#72e1c1";
for (let i = 0; i < floor3DButtons.length; i++) {
const button = floor3DButtons[i];
const selected =
i === state.currentFloor ||
(state.moving && i === state.targetFloor);
button.material.emissive.setHex(
selected ? 0x286b5b : 0x17323b
);
button.material.emissiveIntensity = selected ? 0.8 : 0.25;
}
up3D.material.emissive.setHex(locked ? 0x591c18 : 0x173d55);
down3D.material.emissive.setHex(locked ? 0x591c18 : 0x173d55);
open3D.material.emissive.setHex(
state.doorTarget === 1 ? 0x286b5b : 0x174f42
);
close3D.material.emissive.setHex(
state.doorTarget === 0 ? 0x6a4014 : 0x5b3210
);
updateScreen();
}
floorButtons.forEach((button) => {
button.addEventListener("click", () => {
requestFloor(Number(button.dataset.floor));
});
});
moveUpButton.addEventListener("click", () => requestAdjacent(1));
moveDownButton.addEventListener("click", () => requestAdjacent(-1));
openDoorsButton.addEventListener("click", openDoors);
closeDoorsButton.addEventListener("click", closeDoors);
// ------------------------------------------------------------
// Custom pointer camera controls and 3D button raycasting
// ------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const drag = {
active: false,
pointerId: null,
startX: 0,
startY: 0,
lastX: 0,
lastY: 0,
moved: false
};
function setPointerFromEvent(event) {
const bounds = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - bounds.left) / bounds.width) * 2 - 1;
pointer.y =
-((event.clientY - bounds.top) / bounds.height) * 2 + 1;
}
function get3DButton(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hit = raycaster.intersectObjects(interactive3D, false)[0];
return hit ? hit.object.userData.action : null;
}
canvas.addEventListener("pointerdown", (event) => {
drag.active = true;
drag.pointerId = event.pointerId;
drag.startX = event.clientX;
drag.startY = event.clientY;
drag.lastX = event.clientX;
drag.lastY = event.clientY;
drag.moved = false;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (drag.active && drag.pointerId === event.pointerId) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
if (
Math.abs(event.clientX - drag.startX) +
Math.abs(event.clientY - drag.startY) >
5
) {
drag.moved = true;
}
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
-0.08,
0.68
);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
updateCamera();
} else {
canvas.style.cursor = get3DButton(event) ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (drag.pointerId === event.pointerId) {
if (!drag.moved) {
dispatchAction(get3DButton(event));
}
drag.active = false;
drag.pointerId = null;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
});
canvas.addEventListener("pointercancel", () => {
drag.active = false;
drag.pointerId = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.012,
9,
27
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.yaw = 0.52;
cameraState.pitch = 0.17;
cameraState.radius = 17.5;
updateCamera();
}
document.querySelector("#resetCamera").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.repeat) return;
const key = event.key.toLowerCase();
if (["0", "1", "2", "3"].includes(key)) {
requestFloor(Number(key));
} else if (key === "arrowup") {
event.preventDefault();
requestAdjacent(1);
} else if (key === "arrowdown") {
event.preventDefault();
requestAdjacent(-1);
} else if (key === "o") {
openDoors();
} else if (key === "c") {
closeDoors();
} else if (key === "r") {
resetCamera();
}
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
// Door animation.
const doorSpeed = dt * 1.65;
const doorDifference = state.doorTarget - state.doorProgress;
if (Math.abs(doorDifference) <= doorSpeed) {
state.doorProgress = state.doorTarget;
if (state.doorTarget === 1) {
setStatus("Doors open. Close them before moving.", "success");
} else {
setStatus("Doors closed. Choose a floor.", "success");
}
updateUI();
} else {
state.doorProgress += Math.sign(doorDifference) * doorSpeed;
}
for (const door of doorGroups) {
door.group.position.x =
door.side * (0.82 + state.doorProgress * 1.47);
}
// Elevator movement.
if (state.moving) {
state.moveElapsed += dt;
const progress = THREE.MathUtils.clamp(
state.moveElapsed / state.moveDuration,
0,
1
);
const eased = progress * progress * (3 - 2 * progress);
car.position.y = THREE.MathUtils.lerp(
state.moveFromY,
state.moveToY,
eased
);
if (progress >= 1) {
state.currentFloor = state.targetFloor;
state.moving = false;
car.position.y = FLOORS[state.currentFloor].y;
setStatus(
`Arrived at ${FLOORS[state.currentFloor].label}. Open doors when ready.`,
"success"
);
updateUI();
}
}
// Rejection shake.
if (state.shakeTime > 0) {
state.shakeTime = Math.max(0, state.shakeTime - dt);
const intensity = state.shakeTime / 0.45;
car.position.x = Math.sin(state.shakeTime * 75) * 0.035 * intensity;
car.rotation.z = Math.sin(state.shakeTime * 58) * 0.006 * intensity;
} else {
car.position.x = 0;
car.rotation.z = 0;
}
// Red flash on the physical controller.
state.rejectFlash = Math.max(0, state.rejectFlash - dt * 1.8);
panelMaterial.emissive.setRGB(
state.rejectFlash * 0.35,
state.rejectFlash * 0.035,
state.rejectFlash * 0.025
);
panelMaterial.emissiveIntensity = state.rejectFlash;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
setStatus(
"Doors are open. Close them before moving.",
"success"
);
updateUI();
animate();
</script>
</body>
</html>
```