Prompt specification
Build or customize the Three.js 3D scene "Warehouse Height-Control Lift" 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: Warehouse Height-Control Lift
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/warehouse-height-control-lift-x2031
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a feedback-controlled position actuator. Build a toy lift with position sensing, target floors, and bounded motor commands. Visualize error correction and overshoot only according to the explicit simplified dynamics. Voxel style.
Working Three.js Starter (warehouse-height-control-lift-x2031):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Feedback Actuator</title>
<style>
:root {
--ink: #102a35;
--panel: rgba(13, 36, 46, 0.91);
--panel-soft: rgba(20, 49, 60, 0.84);
--line: rgba(171, 225, 220, 0.23);
--text: #effbf8;
--muted: #9db9b7;
--cyan: #51e5df;
--green: #8ee66f;
--yellow: #ffd166;
--orange: #ff7b54;
--red: #f45b69;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce9e7;
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport,
#viewport canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#viewport canvas {
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
background: var(--panel);
box-shadow: 0 12px 38px rgba(12, 35, 42, 0.2);
backdrop-filter: blur(12px);
border-radius: 12px;
}
.eyebrow {
color: var(--cyan);
font: 700 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.topbar {
position: fixed;
z-index: 11;
top: 18px;
left: 20px;
pointer-events: none;
color: var(--ink);
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
}
.topbar h1 {
margin: 4px 0 3px;
font-size: clamp(21px, 2.4vw, 34px);
line-height: 1;
letter-spacing: -0.04em;
}
.topbar p {
margin: 0;
max-width: 470px;
color: #47636a;
font: 600 11px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#controlPanel {
top: 18px;
left: 20px;
width: 276px;
padding: 16px;
transform: translateY(98px);
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.floor-buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 7px;
}
.floor-buttons button {
position: relative;
height: 47px;
border: 1px solid rgba(140, 205, 199, 0.24);
border-radius: 8px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
cursor: pointer;
transition: 140ms ease;
}
.floor-buttons button:hover {
border-color: var(--cyan);
background: rgba(81, 229, 223, 0.09);
}
.floor-buttons button.active {
border-color: var(--green);
background: rgba(142, 230, 111, 0.15);
box-shadow: inset 0 -3px 0 var(--green);
}
.floor-buttons strong {
display: block;
font-size: 19px;
line-height: 1;
}
.floor-buttons small {
display: block;
margin-top: 5px;
color: var(--muted);
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.control-row {
margin-top: 15px;
}
.control-row label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--text);
font: 700 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.control-row output {
color: var(--cyan);
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 17px;
}
.action {
min-height: 37px;
border: 1px solid rgba(140, 205, 199, 0.3);
border-radius: 7px;
background: rgba(255, 255, 255, 0.055);
color: var(--text);
cursor: pointer;
font: 800 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.action:hover {
background: rgba(81, 229, 223, 0.12);
border-color: var(--cyan);
}
.action.primary {
border-color: var(--cyan);
background: var(--cyan);
color: #10333a;
}
.equations {
margin-top: 17px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.equation {
margin: 0 0 8px;
color: #d4e8e5;
font: 500 10px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
white-space: nowrap;
}
.equation b {
color: var(--yellow);
font-weight: 700;
}
.model-note {
margin: 10px 0 0;
color: var(--muted);
font: 500 9px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#telemetryPanel {
top: 18px;
right: 20px;
width: 278px;
padding: 16px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(81, 229, 223, 0.28);
border-radius: 999px;
background: rgba(81, 229, 223, 0.08);
color: var(--cyan);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
}
.status::before {
width: 6px;
height: 6px;
border-radius: 1px;
background: currentColor;
content: "";
}
.status.saturated {
color: var(--yellow);
border-color: rgba(255, 209, 102, 0.3);
background: rgba(255, 209, 102, 0.09);
}
.status.overshoot {
color: var(--orange);
border-color: rgba(255, 123, 84, 0.38);
background: rgba(255, 123, 84, 0.1);
}
.status.hold {
color: var(--green);
border-color: rgba(142, 230, 111, 0.3);
background: rgba(142, 230, 111, 0.09);
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.readout {
min-height: 57px;
padding: 9px 10px;
border: 1px solid rgba(140, 205, 199, 0.14);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
}
.readout span {
display: block;
margin-bottom: 6px;
color: var(--muted);
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.readout b {
font: 750 16px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.meter-block {
margin-top: 14px;
}
.meter-title {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
text-transform: uppercase;
}
.command-track,
.error-track {
position: relative;
height: 12px;
overflow: hidden;
border: 1px solid rgba(140, 205, 199, 0.18);
border-radius: 4px;
background:
linear-gradient(90deg,
rgba(244, 91, 105, 0.16) 0 50%,
rgba(81, 229, 223, 0.08) 50% 100%);
}
.command-track::after,
.error-track::after {
position: absolute;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
background: rgba(239, 251, 248, 0.38);
content: "";
}
#commandNeedle,
#errorNeedle {
position: absolute;
top: 1px;
bottom: 1px;
left: 50%;
width: 4px;
border-radius: 1px;
background: var(--cyan);
transform: translateX(-50%);
box-shadow: 0 0 10px currentColor;
}
.scale-labels {
display: flex;
justify-content: space-between;
margin-top: 5px;
color: #759592;
font: 500 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.overshoot-readout {
display: flex;
justify-content: space-between;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font: 600 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
#overshootValue {
color: var(--orange);
}
#chartPanel {
right: 20px;
bottom: 18px;
left: 316px;
height: 190px;
padding: 13px 15px 10px;
}
.chart-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 5px;
color: var(--muted);
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.legend {
display: flex;
gap: 13px;
letter-spacing: 0;
text-transform: none;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 15px;
height: 3px;
background: var(--cyan);
}
.legend .target {
background: var(--green);
}
#plot {
display: block;
width: 100%;
height: 140px;
}
#worldLabels {
position: fixed;
inset: 0;
z-index: 5;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 5px 7px;
border: 1px solid currentColor;
border-radius: 4px;
background: rgba(13, 36, 46, 0.88);
box-shadow: 0 4px 12px rgba(12, 35, 42, 0.16);
font: 800 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.05em;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#targetLabel {
color: var(--green);
}
#sensorLabel {
color: var(--cyan);
}
#hint {
position: fixed;
z-index: 8;
right: 22px;
bottom: 218px;
padding: 7px 9px;
border: 1px solid rgba(32, 69, 74, 0.15);
border-radius: 5px;
background: rgba(235, 245, 242, 0.76);
color: #506b70;
font: 700 8px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.05em;
pointer-events: none;
}
@media (max-width: 850px) {
#controlPanel {
top: auto;
bottom: 16px;
left: 12px;
width: 235px;
transform: none;
}
#telemetryPanel {
top: 12px;
right: 12px;
width: 235px;
}
#chartPanel {
right: 12px;
bottom: 16px;
left: 258px;
}
.equations {
display: none;
}
.topbar {
top: 12px;
left: 12px;
}
.topbar p {
display: none;
}
}
@media (max-width: 620px) {
#controlPanel {
display: none;
}
#telemetryPanel {
width: 210px;
padding: 12px;
}
#chartPanel {
right: 8px;
bottom: 8px;
left: 8px;
height: 160px;
}
#plot {
height: 110px;
}
#hint {
display: none;
}
.readouts {
gap: 5px;
}
.readout {
padding: 7px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="topbar">
<div class="eyebrow">Toy actuator / closed-loop digital twin</div>
<h1>BLOCK LIFT</h1>
<p>Position feedback, a bounded motor command, and the resulting overshoot.</p>
</header>
<section id="controlPanel" class="panel">
<div class="section-title">
<span>Target floor</span>
<span>Reference r</span>
</div>
<div class="floor-buttons" id="floorButtons">
<button data-floor="0">
<strong>F0</strong>
<small>BASE</small>
</button>
<button data-floor="1" class="active">
<strong>F1</strong>
<small>LOW</small>
</button>
<button data-floor="2">
<strong>F2</strong>
<small>MID</small>
</button>
<button data-floor="3">
<strong>F3</strong>
<small>TOP</small>
</button>
</div>
<div class="control-row">
<label for="kpInput">
Position gain Kp
<output id="kpOutput">2.40</output>
</label>
<input id="kpInput" type="range" min="0.4" max="4" step="0.05" value="2.4" />
</div>
<div class="control-row">
<label for="kdInput">
Velocity feedback Kd
<output id="kdOutput">0.45</output>
</label>
<input id="kdInput" type="range" min="0" max="1.5" step="0.05" value="0.45" />
</div>
<div class="button-row">
<button id="pauseButton" class="action primary">Pause</button>
<button id="resetButton" class="action">Reset rig</button>
</div>
<div class="equations">
<div class="section-title">
<span>Explicit plant + controller</span>
<span>PD</span>
</div>
<p class="equation">e = r − x</p>
<p class="equation">u = sat(Kp·e − Kd·v, <b>±2</b>)</p>
<p class="equation">v ← v + (u − <b>0.55</b>v)Δt</p>
<p class="equation">x ← x + vΔt</p>
<p class="model-note">
Semi-implicit Euler · Δt = 1/120 s · mass = 1 · no hidden noise,
disturbance, feed-forward, or overshoot animation.
</p>
</div>
</section>
<section id="telemetryPanel" class="panel">
<div class="section-title">
<span>Live telemetry</span>
<span id="status" class="status">Correcting</span>
</div>
<div class="readouts">
<div class="readout">
<span>Position x</span>
<b id="xValue">0.000</b>
</div>
<div class="readout">
<span>Target r</span>
<b id="targetValue">2.000</b>
</div>
<div class="readout">
<span>Error e</span>
<b id="errorValue">+2.000</b>
</div>
<div class="readout">
<span>Velocity v</span>
<b id="velocityValue">0.000</b>
</div>
</div>
<div class="meter-block">
<div class="meter-title">
<span>Applied motor command u</span>
<span id="commandValue">0.00</span>
</div>
<div class="command-track">
<div id="commandNeedle"></div>
</div>
<div class="scale-labels">
<span>−2.00</span>
<span>0</span>
<span>+2.00</span>
</div>
</div>
<div class="meter-block">
<div class="meter-title">
<span>Position error</span>
<span id="rawCommandValue">raw 0.00</span>
</div>
<div class="error-track">
<div id="errorNeedle"></div>
</div>
<div class="scale-labels">
<span>below target</span>
<span>zero</span>
<span>above target</span>
</div>
</div>
<div class="overshoot-readout">
<span>Peak target crossing</span>
<span id="overshootValue">0.000 floors</span>
</div>
</section>
<section id="chartPanel" class="panel">
<div class="chart-head">
<span>Position trace · last 10 seconds</span>
<div class="legend">
<span><i></i>position</span>
<span><i class="target"></i>target</span>
</div>
</div>
<canvas id="plot"></canvas>
</section>
<div id="worldLabels">
<div id="targetLabel" class="world-label">TARGET · F2</div>
<div id="sensorLabel" class="world-label">SENSOR · 0.000</div>
</div>
<div id="hint">DRAG TO ORBIT · WHEEL TO ZOOM · DOUBLE-CLICK TO RESET VIEW</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model parameters. The visible dynamics are exactly those shown in the UI.
// -------------------------------------------------------------------------
const MODEL = Object.freeze({
dt: 1 / 120,
mass: 1,
drag: 0.55,
commandLimit: 2,
floorHeight: 1.45
});
const state = {
x: 0,
v: 0,
target: 2,
u: 0,
rawU: 0,
error: 2,
saturated: false,
time: 0,
running: true,
overshootPeak: 0,
overshootUntil: -1,
kp: 2.4,
kd: 0.45
};
const history = [];
const trailPoints = [];
let nextHistoryTime = 0;
let nextTrailTime = 0;
let accumulator = 0;
// -------------------------------------------------------------------------
// Renderer, scene, camera.
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e7);
scene.fog = new THREE.Fog(0xdce9e7, 15, 31);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
viewport.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
theta: 0.72,
phi: 1.02,
radius: 13.2,
target: new THREE.Vector3(0, 2.05, 0)
};
function updateCamera() {
const s = Math.sin(cameraState.phi);
const c = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * c,
cameraState.target.z +
cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
// Direct camera controls: no addons or OrbitControls.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.008;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.006,
0.42,
1.38
);
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
7,
21
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraState.theta = 0.72;
cameraState.phi = 1.02;
cameraState.radius = 13.2;
});
// -------------------------------------------------------------------------
// Lighting and voxel construction helpers.
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xf5ffff, 0x8aa39d, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d4, 3.4);
sun.position.set(-7, 12, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -7;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x8be8e0, 1.1);
rimLight.position.set(8, 5, -7);
scene.add(rimLight);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.83,
metalness: 0.04,
flatShading: true,
...options
});
}
const materials = {
ground: standardMaterial(0xcbdad6),
slabA: standardMaterial(0xf1eee2),
slabB: standardMaterial(0xe1e6dd),
edgeA: standardMaterial(0x6b8582),
edgeB: standardMaterial(0x57716f),
frame: standardMaterial(0x38545a),
rail: standardMaterial(0x607c80, { metalness: 0.42, roughness: 0.43 }),
car: standardMaterial(0xf3bd43),
carDark: standardMaterial(0x243f47),
carTop: standardMaterial(0xffd76b),
glass: standardMaterial(0x82d9dc, {
metalness: 0.2,
roughness: 0.24,
transparent: true,
opacity: 0.72
}),
cyan: standardMaterial(0x51e5df, {
emissive: 0x123c3b,
emissiveIntensity: 0.75
}),
green: standardMaterial(0x8ee66f, {
emissive: 0x183a13,
emissiveIntensity: 0.75
}),
orange: standardMaterial(0xff7b54, {
emissive: 0x4a160b,
emissiveIntensity: 0.65
}),
motor: standardMaterial(0x334c54),
motorCap: standardMaterial(0xffd166)
};
function addBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, foreground = "#effbf8", background = "#17343d") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 80;
const context = canvas.getContext("2d");
context.fillStyle = background;
context.fillRect(0, 0, canvas.width, canvas.height);
context.strokeStyle = foreground;
context.lineWidth = 5;
context.strokeRect(3, 3, canvas.width - 6, canvas.height - 6);
context.fillStyle = foreground;
context.font = "bold 34px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.scale.set(0.58, 0.181, 1);
return sprite;
}
// -------------------------------------------------------------------------
// Toy lift environment.
// -------------------------------------------------------------------------
addBox(22, 0.35, 18, materials.ground, 0, -0.55, 0);
const towerHeight = MODEL.floorHeight * 3 + 0.55;
for (let floor = 0; floor <= 3; floor++) {
const y = floor * MODEL.floorHeight;
addBox(
3.9,
0.18,
4.4,
floor % 2 === 0 ? materials.slabA : materials.slabB,
-1.35,
y - 0.09,
0
);
addBox(
3.9,
0.075,
0.09,
floor % 2 === 0 ? materials.edgeA : materials.edgeB,
-1.35,
y - 0.025,
2.2
);
const label = makeTextSprite(`F${floor}`, floor === 2 ? "#8ee66f" : "#effbf8");
label.position.set(-2.78, y + 0.08, 2.27);
scene.add(label);
// Small voxel cargo stacks on the open landing.
if (floor < 3) {
addBox(
0.48,
0.48,
0.48,
floor % 2 === 0 ? materials.cyan : materials.motor,
-2.35,
y + 0.24,
-0.85
);
addBox(
0.34,
0.34,
0.34,
materials.motorCap,
-2.35,
y + 0.65,
-0.85
);
}
}
// Structural columns.
const columnPositions = [
[-3.15, -2.05],
[-3.15, 2.05],
[0.55, -2.05]
];
for (const [x, z] of columnPositions) {
addBox(0.23, towerHeight, 0.23, materials.frame, x, towerHeight / 2 - 0.13, z);
}
// Back rail pair.
addBox(0.1, towerHeight, 0.1, materials.rail, 0.62, towerHeight / 2 - 0.1, -1.63);
addBox(0.1, towerHeight, 0.1, materials.rail, 2.08, towerHeight / 2 - 0.1, -1.63);
for (let floor = 0; floor <= 3; floor++) {
addBox(
1.62,
0.09,
0.1,
materials.rail,
1.35,
floor * MODEL.floorHeight,
-1.63
);
}
// Position-sensing mast.
addBox(0.09, 5.9, 0.09, materials.frame, 2.55, 2.73, 0.75);
for (let floor = 0; floor <= 3; floor++) {
addBox(
0.38,
0.06,
0.12,
materials.cyan,
2.55,
floor * MODEL.floorHeight,
0.75
);
}
// Target beacon.
const targetBeaconBase = addBox(
0.25,
0.11,
0.25,
materials.green,
0.62,
state.target * MODEL.floorHeight + 0.07,
1.82
);
const targetBeaconTop = addBox(
0.13,
0.18,
0.13,
materials.green,
0.62,
state.target * MODEL.floorHeight + 0.21,
1.82
);
// -------------------------------------------------------------------------
// Lift car.
// -------------------------------------------------------------------------
const car = new THREE.Group();
car.position.set(1.35, 0, 0.15);
scene.add(car);
addBox(1.42, 0.16, 1.62, materials.carDark, 0, 0.08, 0, car);
addBox(1.28, 0.77, 1.46, materials.car, 0, 0.535, 0, car);
addBox(1.48, 0.12, 1.66, materials.carTop, 0, 0.98, 0, car);
addBox(0.39, 0.3, 0.045, materials.glass, -0.27, 0.6, 0.748, car);
addBox(0.39, 0.3, 0.045, materials.glass, 0.27, 0.6, 0.748, car);
addBox(0.11, 0.3, 0.045, materials.carDark, 0, 0.6, 0.75, car);
// Sensor and roof cargo.
addBox(0.18, 0.17, 0.18, materials.cyan, 0.56, 1.13, -0.53, car);
addBox(0.48, 0.35, 0.48, materials.motor, -0.18, 1.22, 0.04, car);
addBox(0.12, 0.14, 0.5, materials.motorCap, -0.18, 1.46, 0.04, car);
// Target ghost car.
const ghost = new THREE.Mesh(
new THREE.BoxGeometry(1.52, 1.12, 1.7),
new THREE.MeshBasicMaterial({
color: 0x8ee66f,
wireframe: true,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
ghost.position.set(1.35, state.target * MODEL.floorHeight + 0.53, 0.15);
scene.add(ghost);
// Exact sensed-position and target markers.
const sensorMarker = addBox(
0.2,
0.2,
0.2,
materials.cyan,
2.55,
0.08,
0.79
);
const targetMarker = addBox(
0.23,
0.23,
0.23,
materials.green,
2.55,
state.target * MODEL.floorHeight + 0.08,
0.79
);
// Dynamic error connection.
const errorGeometry = new THREE.BufferGeometry();
errorGeometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const errorLine = new THREE.Line(
errorGeometry,
new THREE.LineBasicMaterial({ color: 0x51e5df })
);
scene.add(errorLine);
// Overshoot span, shown only after the model crosses the target.
const overshootSpan = addBox(
0.075,
1,
0.075,
materials.orange,
2.78,
0,
0.79,
scene,
false
);
overshootSpan.visible = false;
// -------------------------------------------------------------------------
// Voxel trajectory.
// -------------------------------------------------------------------------
const maxTrailPoints = 220;
const trailGeometry = new THREE.BoxGeometry(0.105, 0.105, 0.105);
const trailMaterial = standardMaterial(0x51e5df, {
emissive: 0x164c49,
emissiveIntensity: 0.55
});
const trailMesh = new THREE.InstancedMesh(
trailGeometry,
trailMaterial,
maxTrailPoints
);
trailMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
trailMesh.castShadow = true;
scene.add(trailMesh);
const dummy = new THREE.Object3D();
function clearTrail() {
for (let i = 0; i < maxTrailPoints; i++) {
dummy.position.set(0, -100, 0);
dummy.scale.setScalar(0);
dummy.updateMatrix();
trailMesh.setMatrixAt(i, dummy.matrix);
}
trailMesh.instanceMatrix.needsUpdate = true;
trailPoints.length = 0;
}
function updateTrailMeshes() {
for (let i = 0; i < maxTrailPoints; i++) {
const point = trailPoints[i];
if (point) {
dummy.position.set(
1.35,
point.x * MODEL.floorHeight + 0.1,
0.15
);
dummy.scale.setScalar(1);
} else {
dummy.position.set(0, -100, 0);
dummy.scale.setScalar(0);
}
dummy.updateMatrix();
trailMesh.setMatrixAt(i, dummy.matrix);
}
trailMesh.instanceMatrix.needsUpdate = true;
}
clearTrail();
// -------------------------------------------------------------------------
// Bounded motor command gauge.
// -------------------------------------------------------------------------
addBox(0.38, 2.6, 0.28, materials.motor, 3.23, 1.33, -0.68);
addBox(0.12, 0.11, 0.13, materials.motorCap, 3.23, 0.14, -0.51);
addBox(0.12, 0.11, 0.13, materials.motorCap, 3.23, 2.52, -0.51);
const commandBar = addBox(
0.17,
1,
0.17,
materials.cyan,
3.23,
1.33,
-0.5
);
// A voxel rotor visualizes the sign and magnitude of the applied command.
const motorRotor = new THREE.Group();
motorRotor.position.set(3.23, 0.43, -1.18);
scene.add(motorRotor);
addBox(0.58, 0.24, 0.58, materials.motor, 0, 0, 0, motorRotor);
addBox(0.66, 0.08, 0.14, materials.motorCap, 0, 0.08, 0, motorRotor);
addBox(0.14, 0.08, 0.66, materials.motorCap, 0, 0.08, 0, motorRotor);
// -------------------------------------------------------------------------
// Simulation.
// -------------------------------------------------------------------------
function clampCommand(value) {
return THREE.MathUtils.clamp(
value,
-MODEL.commandLimit,
MODEL.commandLimit
);
}
function recordOvershoot(previousError, newError, previousTarget) {
// A target change is not a physical crossing.
if (state.target !== previousTarget) return;
const crossedUp = previousError > 0 && newError < 0 && state.v > 0;
const crossedDown = previousError < 0 && newError > 0 && state.v < 0;
if (crossedUp || crossedDown) {
const amount = Math.abs(state.x - state.target);
state.overshootPeak = Math.max(state.overshootPeak, amount);
state.overshootUntil = state.time + 1.8;
}
}
function simulateStep() {
const previousError = state.error;
const previousTarget = state.target;
const error = state.target - state.x;
const rawU = state.kp * error - state.kd * state.v;
const u = clampCommand(rawU);
const acceleration = (u - MODEL.drag * state.v) / MODEL.mass;
state.v += acceleration * MODEL.dt;
state.x += state.v * MODEL.dt;
state.time += MODEL.dt;
state.error = state.target - state.x;
state.rawU = rawU;
state.u = u;
state.saturated = Math.abs(rawU) > MODEL.commandLimit + 1e-9;
recordOvershoot(previousError, state.error, previousTarget);
if (state.time >= nextHistoryTime) {
history.push({
t: state.time,
x: state.x,
r: state.target,
u: state.u,
v: state.v
});
if (history.length > 600) history.shift();
nextHistoryTime = state.time + 0.04;
}
if (state.time >= nextTrailTime) {
trailPoints.push({ x: state.x });
if (trailPoints.length > maxTrailPoints) trailPoints.shift();
updateTrailMeshes();
nextTrailTime = state.time + 0.06;
}
}
// -------------------------------------------------------------------------
// UI state.
// -------------------------------------------------------------------------
const floorButtons = [...document.querySelectorAll("#floorButtons button")];
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
const kpInput = document.getElementById("kpInput");
const kdInput = document.getElementById("kdInput");
const kpOutput = document.getElementById("kpOutput");
const kdOutput = document.getElementById("kdOutput");
const xValue = document.getElementById("xValue");
const targetValue = document.getElementById("targetValue");
const errorValue = document.getElementById("errorValue");
const velocityValue = document.getElementById("velocityValue");
const commandValue = document.getElementById("commandValue");
const rawCommandValue = document.getElementById("rawCommandValue");
const statusElement = document.getElementById("status");
const overshootValue = document.getElementById("overshootValue");
const commandNeedle = document.getElementById("commandNeedle");
const errorNeedle = document.getElementById("errorNeedle");
const targetLabel = document.getElementById("targetLabel");
const sensorLabel = document.getElementById("sensorLabel");
function setTarget(floor) {
state.target = floor;
floorButtons.forEach((button) => {
button.classList.toggle("active", Number(button.dataset.floor) === floor);
});
targetLabel.textContent = `TARGET · F${floor}`;
}
floorButtons.forEach((button) => {
button.addEventListener("click", () => {
setTarget(Number(button.dataset.floor));
});
});
kpInput.addEventListener("input", () => {
state.kp = Number(kpInput.value);
kpOutput.textContent = state.kp.toFixed(2);
});
kdInput.addEventListener("input", () => {
state.kd = Number(kdInput.value);
kdOutput.textContent = state.kd.toFixed(2);
});
pauseButton.addEventListener("click", () => {
state.running = !state.running;
pauseButton.textContent = state.running ? "Pause" : "Run";
pauseButton.classList.toggle("primary", state.running);
});
resetButton.addEventListener("click", () => {
state.x = 0;
state.v = 0;
state.u = 0;
state.rawU = 0;
state.error = 0;
state.time = 0;
state.saturated = false;
state.overshootPeak = 0;
state.overshootUntil = -1;
accumulator = 0;
nextHistoryTime = 0;
nextTrailTime = 0;
history.length = 0;
clearTrail();
setTarget(0);
});
setTarget(2);
// -------------------------------------------------------------------------
// Telemetry and 3D feedback visualization.
// -------------------------------------------------------------------------
function signed(value, digits = 3) {
return `${value >= 0 ? "+" : ""}${value.toFixed(digits)}`;
}
function updateTelemetry() {
xValue.textContent = state.x.toFixed(3);
targetValue.textContent = state.target.toFixed(3);
errorValue.textContent = signed(state.error);
velocityValue.textContent = signed(state.v);
commandValue.textContent = signed(state.u, 2);
rawCommandValue.textContent = `raw ${signed(state.rawU, 2)}`;
overshootValue.textContent = `${state.overshootPeak.toFixed(3)} floors`;
sensorLabel.textContent = `SENSOR · ${state.x.toFixed(3)}`;
const commandPercent = 50 + (state.u / MODEL.commandLimit) * 50;
commandNeedle.style.left = `${commandPercent}%`;
commandNeedle.style.background = state.saturated
? "var(--yellow)"
: state.u < 0
? "var(--red)"
: "var(--cyan)";
const errorPercent = 50 + THREE.MathUtils.clamp(state.error / 3.5, -1, 1) * 50;
errorNeedle.style.left = `${errorPercent}%`;
errorNeedle.style.background =
state.error < 0 ? "var(--orange)" : "var(--cyan)";
const overshootActive = state.time < state.overshootUntil;
statusElement.className = "status";
if (overshootActive) {
statusElement.textContent = "Overshoot";
statusElement.classList.add("overshoot");
} else if (Math.abs(state.error) < 0.015) {
statusElement.textContent = "At target";
statusElement.classList.add("hold");
} else if (state.saturated) {
statusElement.textContent = "Saturated";
statusElement.classList.add("saturated");
} else if (state.error * state.v > 0) {
statusElement.textContent = "Correcting";
} else {
statusElement.textContent = "Recovering";
}
}
function updateScene(delta) {
const currentY = state.x * MODEL.floorHeight;
const targetY = state.target * MODEL.floorHeight;
const overshootActive = state.time < state.overshootUntil;
car.position.y = currentY;
ghost.position.y = targetY + 0.53;
const ghostPulse = 1 + Math.sin(state.time * 3.4) * 0.012;
ghost.scale.set(ghostPulse, ghostPulse, ghostPulse);
sensorMarker.position.y = currentY + 0.08;
targetMarker.position.y = targetY + 0.08;
targetBeaconBase.position.y = targetY + 0.07;
targetBeaconTop.position.y = targetY + 0.21;
const linePositions = errorGeometry.attributes.position.array;
linePositions[0] = 2.55;
linePositions[1] = currentY + 0.08;
linePositions[2] = 0.79;
linePositions[3] = 2.55;
linePositions[4] = targetY + 0.08;
linePositions[5] = 0.79;
errorGeometry.attributes.position.needsUpdate = true;
errorLine.material.color.setHex(
overshootActive || state.error < -0.015
? 0xff7b54
: 0x51e5df
);
const absoluteError = Math.abs(state.x - state.target);
overshootSpan.visible = overshootActive && absoluteError > 0.01;
if (overshootSpan.visible) {
overshootSpan.scale.y = Math.max(0.01, absoluteError * MODEL.floorHeight);
overshootSpan.position.y =
(currentY + targetY) * 0.5;
}
const commandRatio = state.u / MODEL.commandLimit;
const halfGaugeHeight = 1.08;
const barLength = Math.max(0.025, Math.abs(commandRatio) * halfGaugeHeight * 2);
const direction = Math.sign(commandRatio) || 1;
commandBar.scale.y = barLength;
commandBar.position.y = 1.33 + direction * barLength * 0.5;
commandBar.material = state.saturated
? materials.motorCap
: state.u < 0
? materials.orange
: materials.cyan;
motorRotor.rotation.y += state.u * delta * 2.2;
motorRotor.rotation.z = state.v * 0.22;
}
// -------------------------------------------------------------------------
// Position chart.
// -------------------------------------------------------------------------
const plot = document.getElementById("plot");
const plotContext = plot.getContext("2d");
function resizePlot() {
const rect = plot.getBoundingClientRect();
const dpr = Math.min(window.devicePixelRatio, 2);
plot.width = Math.max(1, Math.floor(rect.width * dpr));
plot.height = Math.max(1, Math.floor(rect.height * dpr));
plotContext.setTransform(dpr, 0, 0, dpr, 0, 0);
}
function drawPlot() {
const rect = plot.getBoundingClientRect();
const width = rect.width;
const height = rect.height;
plotContext.clearRect(0, 0, width, height);
const padding = {
left: 36,
right: 10,
top: 8,
bottom: 23
};
const chartWidth = width - padding.left - padding.right;
const chartHeight = height - padding.top - padding.bottom;
const xMinimum = Math.max(0, state.time - 10);
const xMaximum = Math.max(10, state.time);
let yMaximum = 3.25;
for (const sample of history) {
yMaximum = Math.max(yMaximum, sample.x + 0.18, sample.r + 0.18);
}
yMaximum = Math.ceil(yMaximum * 2) / 2;
const yMinimum = -0.25;
const mapX = (time) =>
padding.left + ((time - xMinimum) / (xMaximum - xMinimum)) * chartWidth;
const mapY = (position) =>
padding.top +
chartHeight -
((position - yMinimum) / (yMaximum - yMinimum)) * chartHeight;
// Grid.
plotContext.strokeStyle = "rgba(171, 225, 220, 0.11)";
plotContext.lineWidth = 1;
for (let floor = 0; floor <= Math.floor(yMaximum); floor++) {
const y = mapY(floor);
plotContext.beginPath();
plotContext.moveTo(padding.left, y);
plotContext.lineTo(width - padding.right, y);
plotContext.stroke();
plotContext.fillStyle = "rgba(157, 185, 183, 0.8)";
plotContext.font = "9px monospace";
plotContext.textAlign = "right";
plotContext.textBaseline = "middle";
plotContext.fillText(String(floor), padding.left - 7, y);
}
for (let second = Math.ceil(xMinimum); second <= xMaximum; second += 2) {
const x = mapX(second);
plotContext.beginPath();
plotContext.moveTo(x, padding.top);
plotContext.lineTo(x, height - padding.bottom);
plotContext.stroke();
plotContext.fillStyle = "rgba(157, 185, 183, 0.75)";
plotContext.font = "9px monospace";
plotContext.textAlign = "center";
plotContext.textBaseline = "top";
plotContext.fillText(`${second}s`, x, height - padding.bottom + 6);
}
function drawSeries(key, color, dashed = false) {
if (history.length < 2) return;
plotContext.save();
plotContext.beginPath();
plotContext.rect(
padding.left,
padding.top,
chartWidth,
chartHeight
);
plotContext.clip();
plotContext.beginPath();
history.forEach((sample, index) => {
const x = mapX(sample.t);
const y = mapY(sample[key]);
if (index === 0) plotContext.moveTo(x, y);
else plotContext.lineTo(x, y);
});
plotContext.strokeStyle = color;
plotContext.lineWidth = 2;
plotContext.setLineDash(dashed ? [6, 5] : []);
plotContext.stroke();
plotContext.restore();
}
drawSeries("r", "#8ee66f", true);
drawSeries("x", "#51e5df", false);
// Current-position dot.
const currentX = mapX(state.time);
const currentY = mapY(state.x);
plotContext.fillStyle = "#51e5df";
plotContext.beginPath();
plotContext.arc(currentX, currentY, 3.2, 0, Math.PI * 2);
plotContext.fill();
}
// -------------------------------------------------------------------------
// Projected HTML labels.
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeWorldLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
const targetLabelPosition = new THREE.Vector3();
const sensorLabelPosition = new THREE.Vector3();
function updateWorldLabels() {
targetLabelPosition.set(
2.78,
state.target * MODEL.floorHeight + 0.37,
0.86
);
sensorLabelPosition.set(
2.78,
state.x * MODEL.floorHeight + 0.03,
0.86
);
placeWorldLabel(targetLabel, targetLabelPosition);
placeWorldLabel(sensorLabel, sensorLabelPosition);
}
// -------------------------------------------------------------------------
// Animation loop.
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (state.running) {
accumulator += delta;
let steps = 0;
while (accumulator >= MODEL.dt && steps < 12) {
simulateStep();
accumulator -= MODEL.dt;
steps++;
}
}
updateCamera();
updateScene(delta);
updateTelemetry();
updateWorldLabels();
drawPlot();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
resizePlot();
});
requestAnimationFrame(resizePlot);
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```