Prompt specification
Build or customize the Three.js 3D scene "Flexible art classroom carts" 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: Flexible art classroom carts
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/flexible-art-classroom-carts-x0199
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows locking caster wheels. Compose easels, supply carts, tables, and drying shelves; move carts into working positions, lock their wheels, and display the remaining open circulation area. Voxel style.
Working Three.js Starter (flexible-art-classroom-carts-x0199):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Studio — Locking Caster Planner</title>
<style>
:root {
--ink: #20302f;
--muted: #71807d;
--paper: #f5f5ef;
--panel: #fbfbf7;
--line: #dfe3dc;
--green: #44a982;
--green-dark: #23745a;
--orange: #ef8054;
--blue: #568fc3;
--shadow: 0 12px 35px rgba(28, 45, 42, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce1dd;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
cursor: pointer;
}
button:disabled {
cursor: default;
}
#app {
display: grid;
grid-template-columns: minmax(0, 1fr) 370px;
width: 100%;
height: 100%;
}
#stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #dce1dd;
}
#stage > canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.topbar {
position: absolute;
top: 24px;
left: 26px;
z-index: 4;
display: flex;
align-items: flex-start;
gap: 18px;
pointer-events: none;
}
.eyebrow {
margin: 0 0 7px;
color: #667672;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 430px;
font-size: clamp(23px, 3vw, 38px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.live-badge {
display: inline-flex;
align-items: center;
gap: 7px;
margin-top: 2px;
padding: 8px 10px;
border: 1px solid rgba(45, 69, 64, 0.14);
border-radius: 8px;
background: rgba(249, 250, 245, 0.78);
color: #45615a;
backdrop-filter: blur(8px);
font: 700 9px/1 ui-monospace, monospace;
letter-spacing: 0.1em;
}
.live-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 4px rgba(68, 169, 130, 0.13);
content: "";
}
.scene-legend {
position: absolute;
left: 26px;
bottom: 24px;
z-index: 4;
display: flex;
flex-wrap: wrap;
gap: 8px;
max-width: 490px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
padding: 8px 10px;
border: 1px solid rgba(49, 68, 64, 0.12);
border-radius: 7px;
background: rgba(249, 250, 245, 0.82);
box-shadow: 0 4px 16px rgba(32, 48, 47, 0.05);
backdrop-filter: blur(8px);
color: #4e615d;
font-size: 11px;
font-weight: 650;
}
.legend-swatch {
width: 11px;
height: 11px;
border-radius: 2px;
}
.legend-swatch.clear {
border: 1px solid rgba(41, 131, 99, 0.35);
background: rgba(83, 193, 151, 0.25);
}
.legend-swatch.locked {
background: var(--green);
}
.legend-swatch.unlocked {
background: var(--orange);
}
.camera-hint {
position: absolute;
right: 22px;
bottom: 23px;
z-index: 4;
padding: 8px 10px;
border-radius: 7px;
background: rgba(32, 48, 47, 0.76);
color: #f5f7f2;
pointer-events: none;
font: 600 9px/1 ui-monospace, monospace;
letter-spacing: 0.04em;
}
#toast {
position: absolute;
left: 50%;
bottom: 78px;
z-index: 8;
max-width: min(470px, calc(100% - 40px));
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-radius: 9px;
background: rgba(31, 48, 46, 0.91);
box-shadow: var(--shadow);
color: white;
font-size: 12px;
font-weight: 650;
text-align: center;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 12px);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
aside {
position: relative;
z-index: 6;
display: flex;
flex-direction: column;
min-height: 0;
overflow: auto;
border-left: 1px solid #d9ded7;
background: var(--panel);
box-shadow: -8px 0 30px rgba(31, 47, 44, 0.06);
}
.side-header {
padding: 26px 24px 20px;
border-bottom: 1px solid var(--line);
}
.side-header-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.side-header h2 {
margin: 0;
font-size: 20px;
letter-spacing: -0.035em;
}
.version {
color: #8a9692;
font: 700 9px/1 ui-monospace, monospace;
letter-spacing: 0.08em;
}
.side-header p {
margin: 10px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.side-body {
padding: 20px 24px 26px;
}
.primary-button {
width: 100%;
padding: 12px 14px;
border: 0;
border-radius: 8px;
background: var(--ink);
box-shadow: 0 7px 18px rgba(32, 48, 47, 0.15);
color: white;
font-size: 12px;
font-weight: 750;
transition: transform 120ms ease, background 120ms ease;
}
.primary-button:hover:not(:disabled) {
background: #2d4541;
transform: translateY(-1px);
}
.primary-button:disabled {
background: #c7cfca;
box-shadow: none;
}
.progress-block {
margin-top: 14px;
}
.progress-label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: #62716e;
font: 700 9px/1 ui-monospace, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.progress-track {
height: 6px;
overflow: hidden;
border-radius: 999px;
background: #e4e8e2;
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #53b58c, #2e8c69);
transition: width 250ms ease;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 24px 0 10px;
color: #6a7875;
font: 700 9px/1 ui-monospace, monospace;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.section-title span:last-child {
color: #a1aaa6;
font-size: 8px;
}
.cart-card {
margin-bottom: 11px;
padding: 14px;
border: 1px solid var(--line);
border-radius: 10px;
background: #fff;
transition: border-color 150ms ease, box-shadow 150ms ease;
cursor: pointer;
}
.cart-card:hover {
border-color: #c3cec7;
}
.cart-card.selected {
border-color: #73aa93;
box-shadow: 0 0 0 3px rgba(68, 169, 130, 0.09);
}
.cart-head {
display: flex;
align-items: center;
gap: 10px;
}
.cart-id {
display: grid;
width: 34px;
height: 34px;
place-items: center;
flex: 0 0 auto;
border-radius: 7px;
color: white;
font: 800 12px/1 ui-monospace, monospace;
}
.cart-id.a {
background: var(--orange);
}
.cart-id.b {
background: var(--blue);
}
.cart-name {
min-width: 0;
flex: 1;
}
.cart-name strong {
display: block;
font-size: 12px;
letter-spacing: -0.01em;
}
.cart-name small {
display: block;
margin-top: 3px;
color: #87938f;
font: 700 8px/1 ui-monospace, monospace;
letter-spacing: 0.05em;
}
.caster-dots {
display: flex;
gap: 4px;
}
.caster-dot {
width: 7px;
height: 12px;
border-radius: 2px;
background: #e7e9e4;
box-shadow: inset 0 0 0 1px #d9ded8;
transition: background 150ms ease, box-shadow 150ms ease;
}
.caster-dot.locked {
background: var(--green);
box-shadow: inset 0 0 0 1px #318664;
}
.cart-status {
margin: 12px 0 10px;
color: #65746f;
font: 700 9px/1 ui-monospace, monospace;
letter-spacing: 0.05em;
}
.cart-status.secure {
color: var(--green-dark);
}
.cart-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.small-button {
min-height: 34px;
padding: 8px 9px;
border: 1px solid #dce1dc;
border-radius: 7px;
background: #f5f6f2;
color: #52625e;
font-size: 10px;
font-weight: 750;
transition: background 120ms ease, color 120ms ease;
}
.small-button:hover:not(:disabled) {
background: #eaf1ec;
color: #2d7157;
}
.small-button.lock {
border-color: #cfe1d7;
background: #eef7f1;
color: #347c5f;
}
.small-button:disabled {
opacity: 0.52;
}
.detail-panel {
margin-top: 12px;
overflow: hidden;
border: 1px solid var(--line);
border-radius: 10px;
background: #f4f5ef;
}
.detail-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 12px;
border-bottom: 1px solid #dde1da;
color: #687772;
font: 700 8px/1 ui-monospace, monospace;
letter-spacing: 0.11em;
}
#detailState {
color: #d66c4f;
}
#detailState.locked {
color: var(--green-dark);
}
#detailCanvas {
position: relative;
width: 100%;
height: 145px;
overflow: hidden;
}
#detailCanvas canvas {
display: block;
width: 100%;
height: 100%;
}
.detail-note {
margin: 0;
padding: 9px 12px 11px;
border-top: 1px solid #dde1da;
color: #71807b;
font-size: 10px;
line-height: 1.45;
}
.area-card {
padding: 15px;
border: 1px solid #dce3dc;
border-radius: 10px;
background: linear-gradient(145deg, #f1f8f3, #f9faf6);
}
.area-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.area-top small {
display: block;
margin-bottom: 5px;
color: #708079;
font: 700 8px/1 ui-monospace, monospace;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.area-value {
font-size: 27px;
font-weight: 800;
letter-spacing: -0.05em;
}
.area-value span {
margin-left: 3px;
color: #71817b;
font-size: 11px;
font-weight: 700;
letter-spacing: 0;
}
.area-percent {
padding: 6px 8px;
border-radius: 6px;
background: #dcefe3;
color: #337759;
font: 800 10px/1 ui-monospace, monospace;
}
.area-meta {
margin: 7px 0 12px;
color: #6f8079;
font-size: 10px;
line-height: 1.45;
}
.toggle-row {
display: flex;
align-items: center;
justify-content: space-between;
padding-top: 10px;
border-top: 1px solid #dce5dd;
color: #52655e;
font-size: 10px;
font-weight: 700;
}
.toggle-row input {
width: 32px;
height: 17px;
accent-color: var(--green);
cursor: pointer;
}
.reset-button {
width: 100%;
margin-top: 19px;
padding: 10px;
border: 1px solid #d9dfd9;
border-radius: 8px;
background: transparent;
color: #6f7b77;
font-size: 10px;
font-weight: 750;
}
.reset-button:hover {
border-color: #bdc9c1;
background: #f2f4ef;
color: #40514c;
}
.instructions {
margin: 18px 0 0;
padding-left: 16px;
color: #7b8884;
font-size: 10px;
line-height: 1.6;
}
.instructions li::marker {
color: #5ca989;
}
@media (max-width: 880px) {
#app {
grid-template-columns: 1fr;
grid-template-rows: 56vh 44vh;
}
aside {
border-top: 1px solid #d9ded7;
border-left: 0;
}
.topbar {
top: 17px;
left: 18px;
}
.scene-legend {
left: 18px;
bottom: 17px;
}
.camera-hint {
display: none;
}
.side-header,
.side-body {
padding-left: 18px;
padding-right: 18px;
}
}
@media (max-width: 520px) {
.live-badge {
display: none;
}
.scene-legend {
max-width: 270px;
}
.legend-item {
padding: 7px 8px;
font-size: 9px;
}
}
</style>
</head>
<body>
<main id="app">
<section id="stage" aria-label="Interactive voxel art studio">
<div class="topbar">
<div>
<p class="eyebrow">Studio logistics / live plan</p>
<h1>Caster<br>lock planner</h1>
</div>
<div class="live-badge">LIVE WORKFLOW</div>
</div>
<div class="scene-legend">
<div class="legend-item">
<span class="legend-swatch clear"></span>
Open circulation
</div>
<div class="legend-item">
<span class="legend-swatch locked"></span>
Locked caster
</div>
<div class="legend-item">
<span class="legend-swatch unlocked"></span>
Unlocked caster
</div>
</div>
<div class="camera-hint">DRAG TO ORBIT · SHIFT/DRAG TO PAN · SCROLL TO ZOOM</div>
<div id="toast" role="status" aria-live="polite"></div>
</section>
<aside>
<header class="side-header">
<div class="side-header-row">
<h2>Working positions</h2>
<span class="version">VOXEL / 01</span>
</div>
<p>
Move each supply cart into its marked bay, then engage all four
locking casters. The teal floor shows connected circulation space.
</p>
</header>
<div class="side-body">
<button id="autoButton" class="primary-button">Run complete setup</button>
<div class="progress-block">
<div class="progress-label">
<span>Workflow</span>
<span id="progressText">0 / 2 placed</span>
</div>
<div class="progress-track">
<div id="progressFill" class="progress-fill"></div>
</div>
</div>
<div class="section-title">
<span>Supply carts</span>
<span>SELECT A CART</span>
</div>
<article class="cart-card selected" data-card="a" tabindex="0">
<div class="cart-head">
<div class="cart-id a">A1</div>
<div class="cart-name">
<strong>Paint supply cart</strong>
<small>4 LOCKING CASTERS</small>
</div>
<div class="caster-dots" data-dots="a">
<span class="caster-dot"></span>
<span class="caster-dot"></span>
<span class="caster-dot"></span>
<span class="caster-dot"></span>
</div>
</div>
<div class="cart-status" data-status="a">IN STAGING</div>
<div class="cart-actions">
<button class="small-button" data-move="a">Move to bay</button>
<button class="small-button lock" data-lock="a" disabled>Lock casters</button>
</div>
</article>
<article class="cart-card" data-card="b" tabindex="0">
<div class="cart-head">
<div class="cart-id b">B2</div>
<div class="cart-name">
<strong>Material supply cart</strong>
<small>4 LOCKING CASTERS</small>
</div>
<div class="caster-dots" data-dots="b">
<span class="caster-dot"></span>
<span class="caster-dot"></span>
<span class="caster-dot"></span>
<span class="caster-dot"></span>
</div>
</div>
<div class="cart-status" data-status="b">IN STAGING</div>
<div class="cart-actions">
<button class="small-button" data-move="b">Move to bay</button>
<button class="small-button lock" data-lock="b" disabled>Lock casters</button>
</div>
</article>
<div class="detail-panel">
<div class="detail-head">
<span>LIVE CASTER MECHANISM</span>
<b id="detailState">UNLOCKED</b>
</div>
<div id="detailCanvas"></div>
<p class="detail-note">
Click an individual wheel on a placed cart to toggle its individual brake.
</p>
</div>
<div class="section-title">
<span>Circulation</span>
<span>CONNECTED FLOOR</span>
</div>
<div class="area-card">
<div class="area-top">
<div>
<small>Remaining open area</small>
<div class="area-value"><b id="areaValue">0</b><span>m²</span></div>
</div>
<div id="areaPercent" class="area-percent">0%</div>
</div>
<p id="areaMeta" class="area-meta">Calculating connected circulation space…</p>
<div class="toggle-row">
<label for="circulationToggle">Show circulation overlay</label>
<input id="circulationToggle" type="checkbox" checked>
</div>
</div>
<button id="resetButton" class="reset-button">Reset all positions</button>
<ol class="instructions">
<li>Select a cart, then move it to its dashed working bay.</li>
<li>Lock all casters or click individual wheels in the scene.</li>
<li>Keep the teal connected route clear for safe movement.</li>
</ol>
</div>
</aside>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.querySelector("#stage");
const toast = document.querySelector("#toast");
const autoButton = document.querySelector("#autoButton");
const resetButton = document.querySelector("#resetButton");
const circulationToggle = document.querySelector("#circulationToggle");
const detailState = document.querySelector("#detailState");
const scene = new THREE.Scene();
scene.background = new THREE.Color("#dce1dd");
scene.fog = new THREE.Fog("#dce1dd", 38, 72);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
stage.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-18, 18, 13, -13, 0.1, 120);
const cameraTarget = new THREE.Vector3(0, 0.7, 0);
const orbit = {
yaw: 0.72,
pitch: 0.67,
distance: 32,
zoom: 1
};
const hemi = new THREE.HemisphereLight("#f7fbff", "#76807a", 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight("#fff4dc", 3.15);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight("#b8d9ee", 1.15);
fill.position.set(13, 9, -10);
scene.add(fill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function material(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0
})
);
}
return materialCache.get(color);
}
function box(parent, size, position, color, options = {}) {
const mesh = new THREE.Mesh(
unitBox,
options.material || material(color)
);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
if (options.rotation) {
mesh.rotation.set(...options.rotation);
}
if (options.userData) {
Object.assign(mesh.userData, options.userData);
}
parent.add(mesh);
return mesh;
}
function labelSprite(parent, text, position, width = 2.2, background = "#263b37", foreground = "#f6f4ea") {
const canvas = document.createElement("canvas");
canvas.width = 256;
canvas.height = 96;
const ctx = canvas.getContext("2d");
ctx.fillStyle = background;
ctx.fillRect(0, 0, 256, 96);
ctx.fillStyle = foreground;
ctx.font = "800 34px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 128, 49);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.position.set(position[0], position[1], position[2]);
sprite.scale.set(width, width * 96 / 256, 1);
parent.add(sprite);
return sprite;
}
function cornerFrame(parent, width, depth, color, opacity = 0.9) {
const group = new THREE.Group();
const frameMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
const corners = [-1, 1];
for (const sx of corners) {
for (const sz of corners) {
const x = sx * (width / 2 - 0.28);
const z = sz * (depth / 2);
box(group, [0.58, 0.045, 0.09], [x, 0.055, z], color, {
material: frameMaterial,
castShadow: false,
receiveShadow: false,
userData: { noPick: true }
});
box(group, [0.09, 0.045, 0.58], [sx * width / 2, 0.055, sz * (depth / 2 - 0.28)], color, {
material: frameMaterial,
castShadow: false,
receiveShadow: false,
userData: { noPick: true }
});
}
}
parent.add(group);
return { group, material: frameMaterial };
}
// ---------- Floor and architecture ----------
const COLS = 28;
const ROWS = 20;
const floor = new THREE.Group();
scene.add(floor);
box(floor, [29, 0.52, 21], [0, -0.29, 0], "#bdc5bf", {
castShadow: false
});
const tileCount = COLS * ROWS;
const floorTiles = new THREE.InstancedMesh(
unitBox,
new THREE.MeshStandardMaterial({
color: "#ffffff",
roughness: 0.95
}),
tileCount
);
floorTiles.receiveShadow = true;
floorTiles.castShadow = false;
const dummy = new THREE.Object3D();
const floorColors = ["#e7e4da", "#e2dfd4", "#ebe8de", "#dfddd2"];
let tileIndex = 0;
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
dummy.position.set(-COLS / 2 + col + 0.5, -0.02, -ROWS / 2 + row + 0.5);
dummy.scale.set(0.975, 0.08, 0.975);
dummy.updateMatrix();
floorTiles.setMatrixAt(tileIndex, dummy.matrix);
floorTiles.setColorAt(tileIndex, new THREE.Color(floorColors[(row * 3 + col) % floorColors.length]));
tileIndex++;
}
}
floorTiles.instanceMatrix.needsUpdate = true;
floorTiles.instanceColor.needsUpdate = true;
floor.add(floorTiles);
box(scene, [29.3, 5.1, 0.32], [0, 2.5, -10.25], "#cfd6d1");
box(scene, [0.32, 5.1, 20.6], [-14.25, 2.5, 0], "#c8d0cb");
// Voxel windows.
for (const x of [-5.2, 2.4, 9.6]) {
box(scene, [4.2, 2.25, 0.12], [x, 3.0, -10.02], "#91b8ca", {
castShadow: false
});
box(scene, [0.16, 2.45, 0.18], [x, 3.0, -9.91], "#eef0e8");
box(scene, [4.4, 0.16, 0.18], [x, 3.0, -9.91], "#eef0e8");
box(scene, [4.45, 0.16, 0.28], [x, 1.85, -9.87], "#eef0e8");
}
labelSprite(scene, "MAKER STUDIO", [0, 4.45, -9.87], 4.4, "#304640", "#e9eee6");
// Entry arrow.
const entry = new THREE.Group();
entry.position.set(7, 0, 9.15);
box(entry, [1.5, 0.06, 0.18], [0, 0.075, 0], "#5cb48f", {
castShadow: false
});
box(entry, [0.55, 0.06, 0.18], [0.34, 0.075, -0.34], "#5cb48f", {
castShadow: false,
rotation: [0, Math.PI / 4, 0]
});
box(entry, [0.55, 0.06, 0.18], [0.34, 0.075, 0.34], "#5cb48f", {
castShadow: false,
rotation: [0, -Math.PI / 4, 0]
});
scene.add(entry);
// ---------- Fixed studio equipment ----------
function makeEasel(x, z, accent) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const wood = "#8b6749";
box(group, [0.16, 3.4, 0.16], [-0.58, 1.72, 0.2], wood);
box(group, [0.16, 3.4, 0.16], [0.58, 1.72, 0.2], wood);
box(group, [0.16, 3.15, 0.16], [-0.42, 1.6, -0.55], wood, {
rotation: [0.12, 0, 0]
});
box(group, [0.16, 3.15, 0.16], [0.42, 1.6, -0.55], wood, {
rotation: [0.12, 0, 0]
});
box(group, [1.55, 0.14, 0.42], [0, 1.02, 0.27], "#9d7653");
box(group, [1.45, 1.5, 0.13], [0, 2.15, 0.02], "#eee9da");
box(group, [0.72, 0.52, 0.06], [-0.17, 2.38, 0.11], accent);
box(group, [0.38, 0.86, 0.06], [0.38, 2.29, 0.11], "#e3a85f");
box(group, [0.28, 0.28, 0.06], [0.24, 1.91, 0.11], "#6e9e8d");
box(group, [0.25, 0.3, 0.25], [-0.5, 1.23, 0.29], "#e4b04e");
box(group, [0.21, 0.25, 0.21], [0.4, 1.2, 0.27], "#7298bd");
labelSprite(group, "EASEL", [0, 3.55, 0], 1.55, "#394d47", "#f5f2e9");
}
function makeTable(x, z, width = 5, depth = 2) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
box(group, [width, 0.22, depth], [0, 1.68, 0], "#b98c62");
box(group, [width - 0.2, 0.32, depth - 0.2], [0, 1.38, 0], "#6c7770");
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(group, [0.22, 1.4, 0.22], [
sx * (width / 2 - 0.25),
0.72,
sz * (depth / 2 - 0.23)
], "#596863");
}
}
box(group, [1.45, 0.035, 0.95], [-0.85, 1.82, 0.05], "#f0eee4");
box(group, [0.55, 0.32, 0.42], [0.75, 1.95, -0.1], "#d77a58");
box(group, [0.35, 0.28, 0.35], [1.35, 1.93, 0.3], "#6b9f8b");
box(group, [0.28, 0.28, 0.28], [-1.35, 1.96, -0.38], "#e2b254");
labelSprite(group, "WORK TABLE", [0, 2.28, 0], 2.4, "#42534e", "#f5f1e7");
}
function makeDryingShelf(x, z, width = 4.4) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const frame = "#527c73";
const depth = 1.6;
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(group, [0.15, 4.25, 0.15], [
sx * (width / 2 - 0.1),
2.15,
sz * (depth / 2 - 0.1)
], frame);
}
}
for (let i = 0; i < 4; i++) {
const shelfY = 0.42 + i * 1.02;
box(group, [width, 0.11, depth], [0, shelfY, 0], "#91aaa0");
box(group, [width - 0.18, 0.08, depth - 0.18], [0, shelfY + 0.1, 0], "#718d83");
}
const palette = ["#e09264", "#5d90ad", "#d3b15d", "#79a17d"];
for (let i = 0; i < 8; i++) {
const level = i % 4;
const side = i < 4 ? -1 : 1;
const shelfY = 0.58 + level * 1.02;
const px = side * width * 0.23;
box(group, [1.35, 0.09, 0.72], [px, shelfY, 0.04], "#e8e3d4");
box(group, [0.56, 0.055, 0.48], [px, shelfY + 0.08, 0.08], palette[i % palette.length]);
}
labelSprite(group, "DRY / 04", [0, 4.72, 0], 2.1, "#3d5d54", "#f1f4ea");
}
function makeSupplyShelf(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
const width = 3.1;
const depth = 1.7;
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(group, [0.14, 3.35, 0.14], [
sx * (width / 2 - 0.08),
1.7,
sz * (depth / 2 - 0.08)
], "#606e67");
}
}
for (let i = 0; i < 3; i++) {
const y = 0.45 + i * 1.03;
box(group, [width, 0.12, depth], [0, y, 0], "#87968d");
box(group, [0.72, 0.55, 0.8], [-0.85, y + 0.34, 0], i % 2 ? "#d8825f" : "#7098ae");
box(group, [0.64, 0.45, 0.7], [0.75, y + 0.29, 0], i % 2 ? "#b5a15e" : "#7fa188");
}
labelSprite(group, "SUPPLIES", [0, 3.75, 0], 2.35, "#45564f", "#f3f0e7");
}
function makeBench(x, z) {
const group = new THREE.Group();
group.position.set(x, 0, z);
scene.add(group);
box(group, [3.2, 0.24, 1.5], [0, 1.3, 0], "#8b745a");
box(group, [0.24, 1.2, 1.2], [-1.25, 0.62, 0], "#52625b");
box(group, [0.24, 1.2, 1.2], [1.25, 0.62, 0], "#52625b");
box(group, [0.65, 0.42, 0.65], [-0.65, 1.63, 0], "#d58460");
box(group, [0.55, 0.35, 0.55], [0.55, 1.6, 0.15], "#6d91a6");
}
makeEasel(-9.2, -6.1, "#cf765b");
makeTable(0.6, -6.2, 5.4, 2.1);
makeDryingShelf(8.3, -6.2, 4.6);
makeSupplyShelf(10.4, 3.5);
makeBench(-10.2, 1.6);
// ---------- Locking caster construction ----------
function createCaster(parent, x, z, detailed = false) {
const group = new THREE.Group();
group.position.set(x, 0, z);
parent.add(group);
const tire = "#283431";
const metal = detailed ? "#b5c4bd" : "#8d9c95";
const hub = "#d3d8cf";
box(group, [0.18, 0.37, 0.18], [0, 0.85, 0], metal);
box(group, [0.48, 0.12, 0.48], [0, 0.58, 0], metal);
box(group, [0.12, 0.42, 0.44], [-0.18, 0.43, 0], metal);
box(group, [0.12, 0.42, 0.44], [0.18, 0.43, 0], metal);
const wheel = box(group, [0.38, 0.5, 0.58], [0, 0.29, 0], tire);
const wheelHub = box(group, [0.42, 0.18, 0.19], [0, 0.29, 0], hub);
wheelHub.rotation.z = Math.PI / 2;
const brakeMaterial = new THREE.MeshStandardMaterial({
color: "#ef7358",
roughness: 0.65,
metalness: 0.08
});
const brake = box(group, [0.13, 0.38, 0.17], [0.29, 0.57, 0.02], "#ef7358", {
material: brakeMaterial
});
brake.rotation.z = -0.78;
const indicator = box(group, [0.13, 0.08, 0.13], [0.29, 0.79, 0.02], "#ef7358", {
material: new THREE.MeshStandardMaterial({
color: "#ef7358",
roughness: 0.6
})
});
const caster = {
group,
wheel,
hub: wheelHub,
brake,
indicator,
locked: false
};
function setLocked(locked) {
caster.locked = locked;
const color = locked ? "#43b884" : "#ef7358";
brakeMaterial.color.set(color);
indicator.material.color.set(color);
brake.rotation.z = locked ? 0 : -0.78;
}
caster.setLocked = setLocked;
return caster;
}
function makeCart(id, start, target, color) {
const group = new THREE.Group();
group.position.set(start[0], 0, start[1]);
scene.add(group);
const dark = "#334540";
const metal = "#7f9189";
box(group, [2.55, 0.17, 1.58], [0, 0.98, 0], metal);
box(group, [2.45, 0.13, 1.48], [0, 1.52, 0], color);
box(group, [2.45, 0.13, 1.48], [0, 2.18, 0], color);
box(group, [2.45, 0.13, 1.48], [0, 2.78, 0], color);
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
box(group, [0.12, 1.78, 0.12], [
sx * 1.13,
1.89,
sz * 0.66
], dark);
}
}
// Front bins and top supplies.
box(group, [0.78, 0.47, 0.68], [-0.65, 1.84, 0.08], "#ede5d0");
box(group, [0.65, 0.43, 0.64], [0.55, 1.82, 0.04], "#9cae9f");
box(group, [0.52, 0.43, 0.52], [-0.38, 2.47, -0.08], "#e2b154");
box(group, [0.45, 0.37, 0.45], [0.55, 2.44, 0.1], "#77a3ae");
// Handle.
box(group, [1.72, 0.11, 0.11], [0, 2.98, 0.65], dark);
box(group, [0.11, 0.11, 0.64], [-0.8, 2.98, 0.35], dark);
box(group, [0.11, 0.11, 0.64], [0.8, 2.98, 0.35], dark);
labelSprite(group, id, [0, 3.47, 0], 1.28, color, "#ffffff");
const casters = [];
const positions = [
[-1.02, -0.6],
[1.02, -0.6],
[-1.02, 0.6],
[1.02, 0.6]
];
for (const [x, z] of positions) {
casters.push(createCaster(group, x, z));
}
const selection = cornerFrame(group, 2.85, 1.9, "#43ae83", 0.95);
selection.group.visible = false;
const cart = {
id,
group,
casters,
selection,
start: new THREE.Vector3(start[0], 0, start[1]),
target: new THREE.Vector3(target[0], 0, target[1]),
startPosition: group.position.clone(),
moving: false,
placed: false,
moveStarted: 0,
color
};
for (const caster of casters) {
caster.cart = cart;
caster.wheel.userData.caster = caster;
caster.brake.userData.caster = caster;
}
group.traverse((object) => {
if (
object.isMesh &&
!object.userData.caster &&
!object.userData.noPick
) {
object.userData.cart = cart;
}
});
return cart;
}
const carts = [
makeCart("A1", [-7, 7], [-5, 0], "#ef8054"),
makeCart("B2", [1, 8], [5, 0.25], "#568fc3")
];
const targetZones = carts.map((cart) => {
const zone = cornerFrame(
scene,
3.05,
2.05,
"#b49a58",
0.88
);
zone.group.position.set(cart.target.x, 0, cart.target.z);
return zone;
});
const stagingZones = carts.map((cart) => {
const zone = cornerFrame(
scene,
3.05,
2.05,
"#b47966",
0.58
);
zone.group.position.set(cart.start.x, 0, cart.start.z);
return zone;
});
// ---------- Circulation calculation and overlay ----------
const circulationMaterial = new THREE.MeshBasicMaterial({
color: "#56b891",
transparent: true,
opacity: 0.16,
depthWrite: false
});
const circulationMesh = new THREE.InstancedMesh(
unitBox,
circulationMaterial,
COLS * ROWS
);
circulationMesh.count = 0;
circulationMesh.renderOrder = 2;
circulationMesh.frustumCulled = false;
scene.add(circulationMesh);
const fixedObstacles = [
{ x: -9.2, z: -6.1, width: 2.1, depth: 1.8 },
{ x: 0.6, z: -6.2, width: 5.4, depth: 2.1 },
{ x: 8.3, z: -6.2, width: 4.6, depth: 1.8 },
{ x: 10.4, z: 3.5, width: 3.1, depth: 1.8 },
{ x: -10.2, z: 1.6, width: 3.2, depth: 1.5 }
];
function addFootprint(occupied, x, z, width, depth) {
const minCol = Math.max(0, Math.floor(x - width / 2 + COLS / 2));
const maxCol = Math.min(COLS - 1, Math.ceil(x + width / 2 + COLS / 2) - 1);
const minRow = Math.max(0, Math.floor(z - depth / 2 + ROWS / 2));
const maxRow = Math.min(ROWS - 1, Math.ceil(z + depth / 2 + ROWS / 2) - 1);
for (let row = minRow; row <= maxRow; row++) {
for (let col = minCol; col <= maxCol; col++) {
occupied[row * COLS + col] = 1;
}
}
}
function updateCirculation() {
const occupied = new Uint8Array(COLS * ROWS);
for (const obstacle of fixedObstacles) {
addFootprint(
occupied,
obstacle.x,
obstacle.z,
obstacle.width,
obstacle.depth
);
}
for (const cart of carts) {
addFootprint(
occupied,
cart.group.position.x,
cart.group.position.z,
2.75,
1.85
);
}
let emptyCount = 0;
for (const value of occupied) {
if (!value) emptyCount++;
}
// Entry is near the bottom-right side of the studio.
const preferredCol = 21;
const preferredRow = ROWS - 1;
let start = preferredRow * COLS + preferredCol;
if (occupied[start]) {
let bestDistance = Infinity;
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
const index = row * COLS + col;
if (occupied[index]) continue;
const distance = Math.hypot(col - preferredCol, row - preferredRow);
if (distance < bestDistance) {
bestDistance = distance;
start = index;
}
}
}
}
const visited = new Uint8Array(COLS * ROWS);
const queue = [start];
visited[start] = 1;
let queueIndex = 0;
while (queueIndex < queue.length) {
const index = queue[queueIndex++];
const col = index % COLS;
const row = Math.floor(index / COLS);
const neighbors = [];
if (col > 0) neighbors.push(index - 1);
if (col < COLS - 1) neighbors.push(index + 1);
if (row > 0) neighbors.push(index - COLS);
if (row < ROWS - 1) neighbors.push(index + COLS);
for (const neighbor of neighbors) {
if (!visited[neighbor] && !occupied[neighbor]) {
visited[neighbor] = 1;
queue.push(neighbor);
}
}
}
let instance = 0;
for (let index = 0; index < visited.length; index++) {
if (!visited[index]) continue;
const col = index % COLS;
const row = Math.floor(index / COLS);
dummy.position.set(
-COLS / 2 + col + 0.5,
0.035,
-ROWS / 2 + row + 0.5
);
dummy.scale.set(0.87, 0.026, 0.87);
dummy.updateMatrix();
circulationMesh.setMatrixAt(instance, dummy.matrix);
instance++;
}
circulationMesh.count = instance;
circulationMesh.instanceMatrix.needsUpdate = true;
circulationMesh.visible = circulationToggle.checked;
const percentage = emptyCount ? Math.round((queue.length / emptyCount) * 100) : 0;
document.querySelector("#areaValue").textContent = queue.length;
document.querySelector("#areaPercent").textContent = `${percentage}%`;
document.querySelector("#areaMeta").textContent =
`${queue.length} of ${emptyCount} usable floor tiles remain connected to the entry.`;
}
// ---------- Live caster detail viewport ----------
const detailScene = new THREE.Scene();
detailScene.background = new THREE.Color("#eef0e9");
const detailRenderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false
});
detailRenderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
detailRenderer.outputColorSpace = THREE.SRGBColorSpace;
detailRenderer.toneMapping = THREE.ACESFilmicToneMapping;
detailRenderer.toneMappingExposure = 1.1;
document.querySelector("#detailCanvas").appendChild(detailRenderer.domElement);
const detailCamera = new THREE.OrthographicCamera(-1.6, 1.6, 1, -1, 0.1, 30);
detailCamera.position.set(2.6, 2.0, 3.2);
detailCamera.lookAt(0, 0.72, 0);
detailScene.add(new THREE.HemisphereLight("#ffffff", "#718078", 2.4));
const detailLight = new THREE.DirectionalLight("#fff1d9", 3);
detailLight.position.set(-3, 5, 4);
detailScene.add(detailLight);
const detailGroup = new THREE.Group();
detailGroup.scale.setScalar(1.6);
detailScene.add(detailGroup);
box(detailGroup, [2.4, 0.12, 1.75], [0, 0.03, 0], "#d4dbd3");
const detailCaster = createCaster(detailGroup, 0, 0, true);
// ---------- Interaction state ----------
let selectedCart = carts[0];
let autoRunning = false;
let workflowToken = 0;
let toastTimer;
const pickables = [];
for (const cart of carts) {
cart.group.traverse((object) => {
if (
object.isMesh &&
!object.userData.noPick &&
(object.userData.cart || object.userData.caster)
) {
pickables.push(object);
}
});
}
function showToast(message) {
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.classList.remove("visible");
}, 2600);
}
function selectCart(cart) {
selectedCart = cart;
for (const candidate of carts) {
candidate.selection.group.visible = candidate === cart;
}
updateDetail();
updateUI();
}
function lockedCount(cart) {
return cart.casters.filter((caster) => caster.locked).length;
}
function setCasterLocked(caster, locked) {
caster.setLocked(locked);
}
function toggleCaster(caster) {
const cart = caster.cart;
if (!cart) return;
selectCart(cart);
if (!cart.placed) {
showToast("Move this cart into its working bay before locking the casters.");
return;
}
if (cart.moving) return;
setCasterLocked(caster, !caster.locked);
updateUI();
updateCirculation();
if (lockedCount(cart) === 4) {
showToast(`${cart.id} is secured with all four locking casters engaged.`);
}
}
function lockAll(cart) {
if (!cart.placed || cart.moving) return false;
for (const caster of cart.casters) {
setCasterLocked(caster, true);
}
updateUI();
updateCirculation();
return true;
}
function moveCart(cart, callback = null) {
if (cart.moving) return;
if (cart.placed) {
if (callback) callback();
return;
}
for (const caster of cart.casters) {
setCasterLocked(caster, false);
}
cart.moving = true;
cart.moveStarted = performance.now();
cart.startPosition.copy(cart.group.position);
selectCart(cart);
updateCirculation();
updateUI();
if (callback) cart.afterMove = callback;
}
function updateZoneColors() {
carts.forEach((cart, index) => {
const count = lockedCount(cart);
const zone = targetZones[index];
const color = count === 4
? "#42ad7e"
: cart.placed
? "#d5a449"
: "#9d8c62";
zone.material.color.set(color);
zone.material.opacity = cart.placed ? 0.95 : 0.58;
stagingZones[index].material.opacity = cart.placed ? 0.18 : 0.58;
});
}
function updateDetail() {
if (!selectedCart) return;
const count = lockedCount(selectedCart);
if (count === 4) {
detailState.textContent = "LOCKED";
detailState.classList.add("locked");
} else if (count > 0) {
detailState.textContent = `${count}/4 LOCKED`;
detailState.classList.remove("locked");
} else {
detailState.textContent = "UNLOCKED";
detailState.classList.remove("locked");
}
detailCaster.setLocked(selectedCart.casters[0].locked);
}
function updateUI() {
const placed = carts.filter((cart) => cart.placed).length;
const locked = carts.reduce((sum, cart) => sum + lockedCount(cart), 0);
document.querySelector("#progressText").textContent =
`${placed} / 2 placed · ${locked} / 8 locked`;
document.querySelector("#progressFill").style.width =
`${((placed / 2) * 60 + (locked / 8) * 40)}%`;
autoButton.disabled = autoRunning;
autoButton.textContent = autoRunning
? "Running setup…"
: placed === 2 && locked === 8
? "Setup complete"
: "Run complete setup";
for (const cart of carts) {
const card = document.querySelector(`[data-card="${cart.id[0].toLowerCase()}"]`);
const status = document.querySelector(`[data-status="${cart.id[0].toLowerCase()}"]`);
const moveButton = document.querySelector(`[data-move="${cart.id[0].toLowerCase()}"]`);
const lockButton = document.querySelector(`[data-lock="${cart.id[0].toLowerCase()}"]`);
const dots = document.querySelector(`[data-dots="${cart.id[0].toLowerCase()}"]`);
const count = lockedCount(cart);
card.classList.toggle("selected", selectedCart === cart);
status.classList.remove("secure");
if (cart.moving) {
status.textContent = "MOVING TO WORKING BAY";
} else if (count === 4) {
status.textContent = "IN POSITION · ALL CASTERS LOCKED";
status.classList.add("secure");
} else if (cart.placed) {
status.textContent = count
? `IN POSITION · ${count}/4 CASTERS LOCKED`
: "IN POSITION · CASTERS UNLOCKED";
} else {
status.textContent = "IN STAGING · CASTERS UNLOCKED";
}
moveButton.disabled = cart.placed || cart.moving;
moveButton.textContent = cart.placed
? "✓ In position"
: cart.moving
? "Moving…"
: "Move to bay";
lockButton.disabled = !cart.placed || cart.moving || count === 4;
lockButton.textContent = count === 4
? "4/4 locked"
: `Lock casters (${count}/4)`;
[...dots.children].forEach((dot, index) => {
dot.classList.toggle("locked", index < count);
});
}
updateZoneColors();
updateDetail();
}
// ---------- UI events ----------
for (const cart of carts) {
const key = cart.id[0].toLowerCase();
const card = document.querySelector(`[data-card="${key}"]`);
card.addEventListener("click", () => selectCart(cart));
card.addEventListener("keydown", (event) => {
if (event.key === "Enter" || event.key === " ") {
event.preventDefault();
selectCart(cart);
}
});
document.querySelector(`[data-move="${key}"]`).addEventListener("click", (event) => {
event.stopPropagation();
moveCart(cart);
});
document.querySelector(`[data-lock="${key}"]`).addEventListener("click", (event) => {
event.stopPropagation();
selectCart(cart);
if (lockAll(cart)) {
showToast(`${cart.id} is secured with all four locking casters engaged.`);
} else {
showToast("Move the cart into its working bay first.");
}
});
}
autoButton.addEventListener("click", () => {
if (autoRunning) return;
autoRunning = true;
const token = ++workflowToken;
updateUI();
const startSecond = () => {
if (token !== workflowToken) return;
moveCart(carts[1], () => {
if (token !== workflowToken) return;
lockAll(carts[1]);
autoRunning = false;
updateUI();
updateCirculation();
showToast("Setup complete: both carts are positioned, locked, and circulation is clear.");
});
};
moveCart(carts[0], () => {
if (token !== workflowToken) return;
lockAll(carts[0]);
setTimeout(startSecond, 450);
});
});
circulationToggle.addEventListener("change", () => {
circulationMesh.visible = circulationToggle.checked;
});
resetButton.addEventListener("click", () => {
workflowToken++;
autoRunning = false;
for (const cart of carts) {
cart.group.position.copy(cart.start);
cart.group.position.y = 0;
cart.moving = false;
cart.placed = false;
cart.afterMove = null;
for (const caster of cart.casters) {
setCasterLocked(caster, false);
}
}
selectCart(carts[0]);
updateCirculation();
updateUI();
showToast("Scene reset. Both carts are back in staging.");
});
// ---------- Direct camera controls ----------
function updateCamera() {
const cosPitch = Math.cos(orbit.pitch);
camera.position.set(
cameraTarget.x + Math.sin(orbit.yaw) * cosPitch * orbit.distance,
cameraTarget.y + Math.sin(orbit.pitch) * orbit.distance,
cameraTarget.z + Math.cos(orbit.yaw) * cosPitch * orbit.distance
);
camera.lookAt(cameraTarget);
camera.zoom = orbit.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
const aspect = width / Math.max(height, 1);
const verticalSize = Math.max(24, 36 / aspect);
renderer.setSize(width, height, false);
camera.left = -verticalSize * aspect / 2;
camera.right = verticalSize * aspect / 2;
camera.top = verticalSize / 2;
camera.bottom = -verticalSize / 2;
camera.updateProjectionMatrix();
updateCamera();
const detailContainer = document.querySelector("#detailCanvas");
detailRenderer.setSize(
detailContainer.clientWidth,
detailContainer.clientHeight,
false
);
const detailAspect = detailContainer.clientWidth / Math.max(detailContainer.clientHeight, 1);
detailCamera.left = -1.6 * detailAspect;
detailCamera.right = 1.6 * detailAspect;
detailCamera.top = 1.6;
detailCamera.bottom = -1.6;
detailCamera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(stage);
window.addEventListener("resize", resize);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = null;
let dragged = false;
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pick(event) {
updatePointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, false);
return hits.length ? hits[0].object : null;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointerDown = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
button: event.button,
pan: event.button === 2 || event.shiftKey
};
dragged = false;
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (pointerDown) {
const totalDistance = Math.hypot(
event.clientX - pointerDown.x,
event.clientY - pointerDown.y
);
if (totalDistance > 4) dragged = true;
const dx = event.clientX - pointerDown.lastX;
const dy = event.clientY - pointerDown.lastY;
if (pointerDown.pan) {
const rect = renderer.domElement.getBoundingClientRect();
const worldPerPixel =
(camera.top - camera.bottom) / camera.zoom / Math.max(rect.height, 1);
cameraTarget.x -= dx * worldPerPixel * Math.cos(orbit.yaw);
cameraTarget.z += dx * worldPerPixel * Math.sin(orbit.yaw);
cameraTarget.z += dy * worldPerPixel * 0.45;
} else if (dragged) {
orbit.yaw -= dx * 0.006;
orbit.pitch = THREE.MathUtils.clamp(
orbit.pitch + dy * 0.005,
0.35,
1.15
);
}
pointerDown.lastX = event.clientX;
pointerDown.lastY = event.clientY;
updateCamera();
} else {
const object = pick(event);
renderer.domElement.style.cursor = object ? "pointer" : "grab";
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!dragged && pointerDown && pointerDown.button === 0) {
const object = pick(event);
if (object) {
if (object.userData.caster) {
toggleCaster(object.userData.caster);
} else if (object.userData.cart) {
selectCart(object.userData.cart);
}
}
}
pointerDown = null;
dragged = false;
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
dragged = false;
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
orbit.zoom = THREE.MathUtils.clamp(
orbit.zoom * (event.deltaY > 0 ? 0.9 : 1.1),
0.68,
2.3
);
updateCamera();
}, { passive: false });
// ---------- Animation ----------
function animate(now) {
for (const cart of carts) {
if (cart.moving) {
const duration = 1500;
const t = Math.min(1, (now - cart.moveStarted) / duration);
const eased = 1 - Math.pow(1 - t, 3);
cart.group.position.x = THREE.MathUtils.lerp(
cart.startPosition.x,
cart.target.x,
eased
);
cart.group.position.z = THREE.MathUtils.lerp(
cart.startPosition.z,
cart.target.z,
eased
);
cart.group.position.y = Math.sin(t * Math.PI) * 0.055;
for (const caster of cart.casters) {
caster.wheel.rotation.x = -now * 0.009;
caster.hub.rotation.y = now * 0.009;
}
if (t >= 1) {
cart.moving = false;
cart.placed = true;
cart.group.position.copy(cart.target);
cart.group.position.y = 0;
for (const caster of cart.casters) {
caster.wheel.rotation.x = 0;
caster.hub.rotation.y = 0;
}
updateUI();
updateCirculation();
showToast(`${cart.id} reached its working position. Lock its casters to secure it.`);
if (cart.afterMove) {
const callback = cart.afterMove;
cart.afterMove = null;
callback();
}
}
}
}
circulationMaterial.opacity = 0.145 + Math.sin(now * 0.0015) * 0.018;
detailGroup.rotation.y = Math.sin(now * 0.0008) * 0.16;
renderer.render(scene, camera);
detailRenderer.render(detailScene, detailCamera);
}
// Initial state.
selectCart(carts[0]);
updateCirculation();
updateUI();
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```