Prompt specification
Build or customize the Three.js 3D scene "Workshop Motor Drives Two Tools" 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: Workshop Motor Drives Two Tools
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/workshop-motor-drives-two-tools-x2019
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a clutch between aligned rotating shafts. Build a toy workshop with one drive shaft and selectable couplings to two machines. Show which tool is powered and which remains disconnected under visible state rules. Voxel style.
Working Three.js Starter (workshop-motor-drives-two-tools-x2019):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Clutch Workshop</title>
<style>
:root {
--ink: #17353b;
--panel: rgba(248, 246, 235, 0.94);
--line: rgba(23, 53, 59, 0.16);
--green: #21b982;
--green-dark: #087d5b;
--red: #e65d4f;
--red-dark: #a73531;
--amber: #f2b84b;
--blue: #559dcc;
--shadow: 0 12px 35px rgba(32, 50, 54, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d9e4df;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
"Liberation Mono", monospace;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.panel {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
#intro {
position: fixed;
top: 18px;
left: 18px;
width: min(365px, calc(100vw - 36px));
padding: 17px 18px 15px;
border-radius: 18px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #4f6d70;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
background: var(--amber);
border: 2px solid var(--ink);
box-shadow: 3px 3px 0 var(--ink);
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1;
letter-spacing: -0.055em;
}
#intro p {
margin: 9px 0 12px;
color: #557074;
font-size: 12px;
line-height: 1.5;
}
.formula {
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.58);
font-size: 11px;
font-weight: 800;
line-height: 1.5;
}
.formula span {
color: var(--green-dark);
}
.rules {
display: grid;
gap: 5px;
margin-top: 10px;
color: #49666a;
font-size: 10px;
line-height: 1.35;
}
.rules b {
color: var(--ink);
}
.hint {
margin-top: 11px;
padding-top: 10px;
border-top: 1px dashed var(--line);
color: #597175;
font-size: 10px;
line-height: 1.5;
}
#telemetry {
position: fixed;
top: 18px;
right: 18px;
width: min(320px, calc(100vw - 36px));
display: grid;
gap: 9px;
z-index: 5;
pointer-events: none;
}
.status-card {
padding: 12px 13px;
border-radius: 14px;
border-left: 6px solid var(--red);
transition: border-color 180ms ease, background 180ms ease;
}
.status-card.powered {
border-left-color: var(--green);
}
.status-top {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
}
.status-dot {
width: 10px;
height: 10px;
border: 2px solid var(--ink);
background: var(--red);
box-shadow: 2px 2px 0 var(--ink);
}
.powered .status-dot {
background: var(--green);
}
.status-top strong {
flex: 1;
}
.status-state {
color: var(--red-dark);
font-weight: 900;
letter-spacing: 0.06em;
}
.powered .status-state {
color: var(--green-dark);
}
.status-card p {
margin: 7px 0 0 26px;
color: #5b7376;
font-size: 10px;
line-height: 1.4;
}
#console {
position: fixed;
left: 50%;
bottom: 16px;
transform: translateX(-50%);
width: min(1080px, calc(100vw - 28px));
padding: 12px;
border-radius: 19px;
display: grid;
grid-template-columns: 1fr 1.55fr 1fr 1fr 1.3fr;
align-items: stretch;
gap: 9px;
z-index: 10;
}
.control {
min-width: 0;
padding: 9px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.55);
}
.control-label {
display: block;
margin-bottom: 7px;
color: #657c7f;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.big-button,
.small-button,
.segmented button,
.preset button {
border: 1px solid rgba(23, 53, 59, 0.16);
cursor: pointer;
color: var(--ink);
font-weight: 850;
transition: transform 120ms ease, background 140ms ease,
color 140ms ease, box-shadow 140ms ease;
}
.big-button:hover,
.small-button:hover,
.segmented button:hover,
.preset button:hover {
transform: translateY(-1px);
}
.big-button:active,
.small-button:active,
.segmented button:active,
.preset button:active {
transform: translateY(1px);
}
.big-button {
width: 100%;
min-height: 39px;
border-radius: 9px;
background: var(--red);
color: white;
box-shadow: 0 4px 0 var(--red-dark);
font-size: 11px;
}
.big-button.on {
background: var(--green);
box-shadow: 0 4px 0 var(--green-dark);
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 4px;
}
.segmented button {
min-width: 0;
min-height: 39px;
padding: 5px 3px;
border-radius: 8px;
background: #e5e8df;
font-size: 10px;
}
.segmented button.active {
background: var(--amber);
box-shadow: inset 0 -3px 0 rgba(23, 53, 59, 0.18);
}
.small-button {
width: 100%;
min-height: 39px;
padding: 5px 6px;
border-radius: 9px;
background: #e8e7df;
font-size: 10px;
}
.small-button.engaged {
color: white;
background: var(--green);
box-shadow: 0 4px 0 var(--green-dark);
}
.small-button.open {
color: white;
background: var(--red);
box-shadow: 0 4px 0 var(--red-dark);
}
.preset {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
}
.preset button {
min-height: 19px;
padding: 3px 4px;
border-radius: 7px;
background: #e5e8df;
font-size: 9px;
}
.preset .wide {
grid-column: 1 / -1;
background: #d9e6e7;
}
#loading {
position: fixed;
inset: 0;
display: grid;
place-items: center;
z-index: 30;
background: #d9e4df;
color: var(--ink);
font-size: 12px;
font-weight: 900;
letter-spacing: 0.15em;
}
#loading .cube {
width: 26px;
height: 26px;
margin: 0 auto 12px;
background: var(--amber);
border: 3px solid var(--ink);
box-shadow: 7px 7px 0 var(--ink);
animation: bob 700ms infinite alternate ease-in-out;
}
@keyframes bob {
to {
transform: translateY(-7px);
}
}
@media (max-width: 880px) {
#intro {
width: 310px;
}
.rules,
#intro p {
display: none;
}
#telemetry {
width: 275px;
}
#console {
grid-template-columns: 1fr 1.5fr 1fr 1fr;
}
.preset {
display: none;
}
}
@media (max-width: 650px) {
#intro {
top: 10px;
left: 10px;
width: 215px;
padding: 12px;
}
#intro p,
.rules,
.hint {
display: none;
}
.formula {
margin-top: 9px;
padding: 6px 8px;
font-size: 9px;
}
#telemetry {
top: 10px;
right: 10px;
width: 205px;
}
.status-card {
padding: 9px;
}
.status-card p {
display: none;
}
#console {
bottom: 8px;
width: calc(100vw - 16px);
padding: 8px;
grid-template-columns: 1fr 1.4fr 1fr 1fr;
gap: 5px;
}
.control {
padding: 6px;
}
.control-label {
font-size: 7px;
letter-spacing: 0.07em;
}
.big-button,
.small-button,
.segmented button {
min-height: 34px;
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel workshop showing two clutch-controlled machines"></canvas>
<section id="intro" class="panel">
<div class="eyebrow">Toy workshop / power transfer bench</div>
<h1>Clutch Lab</h1>
<p>
Route one drive shaft to either machine, then engage each branch clutch
independently.
</p>
<div class="formula">
POWERED = <span>DRIVE ON</span> + ROUTE MATCH + CLUTCH ENGAGED
</div>
<div class="rules">
<div><b>1.</b> The master drive supplies the central selector.</div>
<div><b>2.</b> The selector can choose A, B, or BOTH branches.</div>
<div><b>3.</b> An open clutch always leaves its machine disconnected.</div>
</div>
<div class="hint">
Drag to orbit · Scroll to zoom · Click a coupling hub to toggle it
</div>
</section>
<section id="telemetry" aria-live="polite">
<article id="statusA" class="status-card panel">
<div class="status-top">
<span class="status-dot"></span>
<strong>MACHINE A / DRILL</strong>
<span id="stateA" class="status-state">DISCONNECTED</span>
</div>
<p id="reasonA">Clutch A is open.</p>
</article>
<article id="statusB" class="status-card panel">
<div class="status-top">
<span class="status-dot"></span>
<strong>MACHINE B / PRESS</strong>
<span id="stateB" class="status-state">DISCONNECTED</span>
</div>
<p id="reasonB">Clutch B is open.</p>
</article>
</section>
<section id="console" class="panel" aria-label="Workshop controls">
<div class="control">
<span class="control-label">01 / Master</span>
<button id="masterButton" class="big-button on" aria-pressed="true">
DRIVE ON
</button>
</div>
<div class="control">
<span class="control-label">02 / Power selector</span>
<div class="segmented" role="group" aria-label="Power route">
<button data-route="A" aria-pressed="false">A</button>
<button data-route="B" aria-pressed="false">B</button>
<button data-route="BOTH" class="active" aria-pressed="true">BOTH</button>
</div>
</div>
<div class="control">
<span class="control-label">03 / Clutch A</span>
<button id="clutchAButton" class="small-button open" aria-pressed="false">
A / OPEN
</button>
</div>
<div class="control">
<span class="control-label">04 / Clutch B</span>
<button id="clutchBButton" class="small-button engaged" aria-pressed="true">
B / ENGAGED
</button>
</div>
<div class="control">
<span class="control-label">05 / Scenarios</span>
<div class="preset">
<button data-preset="mixed">Mixed</button>
<button data-preset="both">Both on</button>
<button data-preset="safe" class="wide">Safe power-off</button>
</div>
</div>
</section>
<div id="loading">
<div>
<div class="cube"></div>
BUILDING WORKSHOP
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e4df);
scene.fog = new THREE.Fog(0xd9e4df, 18, 31);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
yaw: 0.57,
pitch: 0.49,
distance: 16.8,
target: new THREE.Vector3(-0.25, 1.25, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
camera.position.set(
target.x + distance * Math.cos(pitch) * Math.sin(yaw),
target.y + distance * Math.sin(pitch),
target.z + distance * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff8df, 0x637a7c, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff0d2, 3.1);
keyLight.position.set(-3, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8ec9df, 1.4);
rimLight.position.set(8, 5, -8);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Shared materials and voxel construction helpers
// -------------------------------------------------------------------------
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.05,
...options
});
}
const mats = {
floor: material(0xc9c8b9),
floorLight: material(0xe4dfcd),
floorDark: material(0xb7b9ad),
wall: material(0x9fb8b5),
wallTrim: material(0x31565b),
cream: material(0xf2e8cf),
white: material(0xf8f5e9),
steel: material(0x52676b, { metalness: 0.5, roughness: 0.48 }),
steelLight: material(0x91a6a6, { metalness: 0.45, roughness: 0.5 }),
dark: material(0x233b40),
blue: material(0x559dcc),
blueDark: material(0x39799e),
yellow: material(0xf2b84b),
orange: material(0xe68c42),
red: material(0xe65d4f),
green: material(0x36c991),
greenDark: material(0x168662),
wood: material(0xaa7951),
woodLight: material(0xc99a68),
black: material(0x253236),
peg: material(0x718f8d)
};
function addBox(
parent,
size,
position,
mat,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, w, h, r) {
const radius = Math.min(r, w / 2, h / 2);
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + w, y, x + w, y + h, radius);
ctx.arcTo(x + w, y + h, x, y + h, radius);
ctx.arcTo(x, y + h, x, y, radius);
ctx.arcTo(x, y, x + w, y, radius);
ctx.closePath();
}
function makeWallSign(parent, title, subtitle, width, height) {
const c = document.createElement("canvas");
c.width = 1024;
c.height = 256;
const ctx = c.getContext("2d");
ctx.fillStyle = "#244047";
roundedRect(ctx, 8, 8, 1008, 240, 26);
ctx.fill();
ctx.strokeStyle = "#f2b84b";
ctx.lineWidth = 10;
roundedRect(ctx, 24, 24, 976, 208, 18);
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = "#f8f3df";
ctx.font = "900 78px monospace";
ctx.fillText(title, 512, 119);
ctx.fillStyle = "#a9d6c8";
ctx.font = "700 31px monospace";
ctx.fillText(subtitle, 512, 179);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
new THREE.MeshBasicMaterial({ map: texture })
);
parent.add(mesh);
return mesh;
}
function makeStatusSprite(machineName, subtitle) {
const c = document.createElement("canvas");
c.width = 640;
c.height = 160;
const ctx = c.getContext("2d");
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(2.7, 0.675, 1);
sprite.userData = {
canvas: c,
context: ctx,
texture,
machineName,
subtitle
};
drawStatusSprite(sprite, false, "Waiting");
return sprite;
}
function drawStatusSprite(sprite, powered, reason) {
const { context: ctx, canvas: c, texture, machineName, subtitle } =
sprite.userData;
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = powered ? "#147d5a" : "#7d3c37";
roundedRect(ctx, 8, 8, 624, 144, 24);
ctx.fill();
ctx.fillStyle = powered ? "#42e4a7" : "#ff8b7d";
roundedRect(ctx, 22, 22, 596, 19, 9);
ctx.fill();
ctx.textAlign = "left";
ctx.fillStyle = "#ffffff";
ctx.font = "900 38px monospace";
ctx.fillText(machineName, 35, 83);
ctx.textAlign = "right";
ctx.font = "900 33px monospace";
ctx.fillText(powered ? "POWERED" : "DISCONNECTED", 603, 83);
ctx.textAlign = "left";
ctx.fillStyle = powered ? "#c5ffe9" : "#ffd2cd";
ctx.font = "700 22px monospace";
ctx.fillText(subtitle, 36, 124);
texture.needsUpdate = true;
}
function makeTextSprite(text, bg = "#244047", fg = "#f8f3df", scale = 1.7) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 128;
const ctx = c.getContext("2d");
ctx.fillStyle = bg;
roundedRect(ctx, 8, 8, 496, 112, 18);
ctx.fill();
ctx.fillStyle = fg;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "900 46px monospace";
ctx.fillText(text, 256, 65);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
sprite.scale.set(scale, scale * 0.25, 1);
return sprite;
}
// -------------------------------------------------------------------------
// Workshop room
// -------------------------------------------------------------------------
addBox(scene, [18.2, 0.36, 10.5], [0, -0.22, 0], mats.floor);
addBox(scene, [18.4, 0.16, 10.7], [0, -0.43, 0], mats.dark);
for (let x = -8; x <= 8; x += 2) {
for (let z = -4; z <= 4; z += 2) {
const tileMat =
(x + z) % 4 === 0 ? mats.floorLight : mats.floor;
addBox(scene, [1.86, 0.045, 1.86], [x, 0.015, z], tileMat, false);
}
}
addBox(scene, [18, 4.6, 0.28], [0, 2.05, -5.2], mats.wall);
addBox(scene, [0.28, 4.6, 10.4], [-8.9, 2.05, 0], mats.wall);
addBox(scene, [18.1, 0.24, 0.38], [0, 4.27, -5.05], mats.wallTrim);
addBox(scene, [0.38, 0.24, 10.5], [-8.76, 4.27, 0], mats.wallTrim);
// Pegboard and tool silhouettes.
addBox(scene, [6.4, 2.25, 0.12], [3.45, 2.35, -5.0], mats.wood);
for (let x = 0.9; x <= 6.0; x += 0.52) {
for (let y = 1.42; y <= 3.28; y += 0.43) {
addBox(
scene,
[0.085, 0.085, 0.035],
[x, y, -4.915],
mats.peg,
false
);
}
}
// Toy hammer, wrench, and screwdriver on the pegboard.
addBox(scene, [0.13, 0.9, 0.11], [1.65, 2.72, -4.78], mats.woodLight);
addBox(scene, [0.56, 0.24, 0.13], [1.65, 3.19, -4.78], mats.steelLight);
addBox(scene, [0.18, 1.03, 0.1], [2.7, 2.74, -4.78], mats.steelLight);
addBox(scene, [0.38, 0.22, 0.1], [2.7, 3.28, -4.78], mats.steelLight);
addBox(scene, [0.42, 0.18, 0.1], [2.7, 2.22, -4.78], mats.steelLight);
addBox(scene, [0.12, 0.85, 0.1], [3.8, 2.76, -4.78], mats.yellow);
addBox(scene, [0.18, 0.42, 0.1], [3.8, 2.2, -4.78], mats.red);
// Shelf and parts bins.
addBox(scene, [4.5, 0.16, 0.72], [4.8, 3.65, -4.72], mats.woodLight);
addBox(scene, [1.1, 0.65, 0.65], [3.25, 3.28, -4.68], mats.blue);
addBox(scene, [0.85, 0.52, 0.62], [4.72, 3.42, -4.68], mats.yellow);
addBox(scene, [1.05, 0.58, 0.62], [6.0, 3.38, -4.68], mats.orange);
// Workbench along the left wall.
addBox(scene, [2.0, 0.18, 1.0], [-7.55, 1.4, -1.3], mats.woodLight);
for (const z of [-1.65, -0.95]) {
addBox(scene, [0.16, 1.35, 0.16], [-8.15, 0.65, z], mats.dark);
addBox(scene, [0.16, 1.35, 0.16], [-6.95, 0.65, z], mats.dark);
}
addBox(scene, [0.8, 0.55, 0.75], [-7.7, 1.77, -1.25], mats.blue);
addBox(scene, [0.55, 0.35, 0.55], [-6.85, 1.67, -1.35], mats.yellow);
// Window on the back wall.
addBox(scene, [3.0, 1.55, 0.12], [-5.3, 2.55, -5.01], mats.cream);
addBox(scene, [2.72, 1.27, 0.08], [-5.3, 2.55, -4.92], material(0x8bc5d8));
addBox(scene, [0.1, 1.3, 0.1], [-5.3, 2.55, -4.84], mats.cream);
addBox(scene, [2.75, 0.1, 0.1], [-5.3, 2.55, -4.84], mats.cream);
const wallSign = makeWallSign(
scene,
"CLUTCH LAB",
"ONE DRIVE / TWO MACHINES",
5.4,
1.35
);
wallSign.position.set(-3.3, 3.72, -4.86);
// Floor safety blocks and arrows.
for (let i = 0; i < 5; i++) {
addBox(
scene,
[0.25, 0.06, 0.5],
[-7.9 + i * 0.48, 0.075, 3.7],
i % 2 === 0 ? mats.yellow : mats.dark,
false
);
}
// -------------------------------------------------------------------------
// Drive motor and rotating main shaft
// -------------------------------------------------------------------------
addBox(scene, [1.85, 0.46, 1.8], [-6.45, 0.24, 0], mats.dark);
addBox(scene, [1.35, 1.25, 1.35], [-6.45, 1.08, 0], mats.blue);
addBox(scene, [1.0, 0.22, 1.5], [-6.45, 1.82, 0], mats.blueDark);
addBox(scene, [0.32, 0.66, 1.48], [-5.85, 1.12, 0], mats.steelLight);
for (let z = -0.48; z <= 0.49; z += 0.24) {
addBox(scene, [0.06, 0.62, 0.07], [-5.64, 1.12, z], mats.dark);
}
const sourceBeaconMaterial = material(0x36c991, {
emissive: 0x1a9d6d,
emissiveIntensity: 0.8
});
addBox(scene, [0.28, 0.28, 0.28], [-6.45, 2.18, 0], sourceBeaconMaterial);
addBox(scene, [0.4, 0.1, 0.4], [-6.45, 2.34, 0], mats.dark);
const mainShaft = new THREE.Group();
mainShaft.position.set(-3.73, 1.3, 0);
scene.add(mainShaft);
addBox(mainShaft, [4.05, 0.27, 0.27], [0, 0, 0], mats.steelLight);
addBox(mainShaft, [3.8, 0.035, 0.075], [0, 0.153, 0], mats.white);
addBox(mainShaft, [0.22, 0.38, 0.38], [-1.65, 0, 0], mats.yellow);
addBox(scene, [0.2, 0.48, 0.48], [-5.55, 1.3, 0], mats.dark);
addBox(scene, [0.22, 0.5, 0.5], [-1.72, 1.3, 0], mats.dark);
// Central selector housing.
addBox(scene, [1.4, 0.35, 3.85], [-1.12, 0.2, 0], mats.dark);
addBox(scene, [1.08, 1.45, 3.45], [-1.12, 1.02, 0], mats.blueDark);
addBox(scene, [1.23, 0.18, 3.62], [-1.12, 1.84, 0], mats.steel);
const routeLampMaterials = {
A: material(0x879493),
B: material(0x879493),
BOTH: material(0xf2b84b, {
emissive: 0x9d5b08,
emissiveIntensity: 0.65
})
};
addBox(scene, [0.3, 0.22, 0.3], [-1.12, 2.05, 1.75], routeLampMaterials.A);
addBox(scene, [0.3, 0.22, 0.3], [-1.12, 2.05, 0], routeLampMaterials.BOTH);
addBox(scene, [0.3, 0.22, 0.3], [-1.12, 2.05, -1.75], routeLampMaterials.B);
const selectorLabel = makeTextSprite("SELECT", "#244047", "#f8f3df", 1.25);
selectorLabel.position.set(-1.1, 2.48, 0);
scene.add(selectorLabel);
// -------------------------------------------------------------------------
// Clutch hubs
// -------------------------------------------------------------------------
const pickables = [];
function makeHub(baseColor, isInput) {
const group = new THREE.Group();
const bodyMaterial = material(baseColor, {
emissive: 0x000000,
emissiveIntensity: 0
});
const body = addBox(group, [0.48, 0.64, 0.64], [0, 0, 0], bodyMaterial);
const stripeMaterial = material(0xf8f4e8);
addBox(group, [0.18, 0.045, 0.14], [0, 0.335, 0], stripeMaterial);
const toothMaterial = material(isInput ? 0xf4c258 : baseColor, {
metalness: 0.35,
roughness: 0.52
});
const toothX = isInput ? 0.29 : -0.29;
for (let i = 0; i < 4; i++) {
const angle = (i / 4) * Math.PI * 2 + Math.PI / 4;
const tooth = addBox(
group,
[0.16, 0.13, 0.13],
[
toothX,
Math.cos(angle) * 0.27,
Math.sin(angle) * 0.27
],
toothMaterial
);
tooth.rotation.x = angle;
}
group.userData.bodyMaterial = bodyMaterial;
return group;
}
function makeBranch(id, z, machineName) {
const branch = {
id,
z,
inputAngle: 0,
outputAngle: 0,
inputHub: null,
outputHub: null,
inputShaft: null,
outputShaft: null,
outputHubMaterial: null,
lampMaterial: null,
routeMaterial: null
};
const inputShaft = new THREE.Group();
inputShaft.position.set(-0.34, 1.3, z);
scene.add(inputShaft);
addBox(inputShaft, [1.48, 0.25, 0.25], [0, 0, 0], mats.steelLight);
addBox(inputShaft, [1.36, 0.03, 0.07], [0, 0.14, 0], mats.yellow);
branch.inputShaft = inputShaft;
const outputShaft = new THREE.Group();
outputShaft.position.set(2.27, 1.3, z);
scene.add(outputShaft);
addBox(outputShaft, [2.86, 0.25, 0.25], [0, 0, 0], mats.steelLight);
addBox(outputShaft, [2.68, 0.035, 0.07], [0, 0.14, 0], mats.blue);
branch.outputShaft = outputShaft;
const coupler = new THREE.Group();
coupler.position.set(0.55, 1.3, z);
scene.add(coupler);
const inputHub = makeHub(0xf2b84b, true);
inputHub.position.x = -0.2;
inputHub.userData.action = `toggle:${id}`;
coupler.add(inputHub);
branch.inputHub = inputHub;
const outputHub = makeHub(0x36c991, false);
outputHub.position.x = 0.2;
outputHub.userData.action = `toggle:${id}`;
coupler.add(outputHub);
branch.outputHub = outputHub;
branch.outputHubMaterial = outputHub.userData.bodyMaterial;
pickables.push(inputHub, outputHub);
const lampMaterial = material(0xe65d4f, {
emissive: 0x8d1f1b,
emissiveIntensity: 0.6
});
addBox(scene, [0.22, 0.22, 0.22], [0.55, 2.05, z], lampMaterial);
branch.lampMaterial = lampMaterial;
const routeMaterial = material(0xf2b84b, {
emissive: 0x8a4a06,
emissiveIntensity: 0.7,
metalness: 0.18
});
addBox(scene, [1.4, 0.09, 0.12], [-0.25, 0.54, z], routeMaterial);
branch.routeMaterial = routeMaterial;
const clutchLabel = makeTextSprite(
`${id} CLUTCH`,
"#244047",
"#f8f3df",
1.45
);
clutchLabel.position.set(0.55, 2.43, z);
scene.add(clutchLabel);
const statusSprite = makeStatusSprite(
`MACHINE ${id}`,
id === "A" ? "DRILL" : "PRESS"
);
statusSprite.position.set(4.8, 3.43, z);
scene.add(statusSprite);
branch.statusSprite = statusSprite;
return branch;
}
const branchA = makeBranch("A", 1.75, "DRILL");
const branchB = makeBranch("B", -1.75, "PRESS");
const branches = { A: branchA, B: branchB };
// -------------------------------------------------------------------------
// Voxel machines
// -------------------------------------------------------------------------
// Machine A: drill.
const drillMachine = new THREE.Group();
drillMachine.position.set(4.8, 0, 1.75);
scene.add(drillMachine);
addBox(drillMachine, [2.05, 0.44, 1.85], [0, 0.24, 0], mats.dark);
addBox(drillMachine, [1.65, 0.88, 1.48], [0.1, 0.9, 0], mats.blue);
addBox(drillMachine, [0.48, 1.75, 0.62], [0.48, 1.72, 0], mats.blueDark);
addBox(drillMachine, [1.12, 0.26, 0.86], [0.18, 2.62, 0], mats.yellow);
addBox(drillMachine, [0.35, 0.45, 0.45], [-0.82, 1.18, 0], mats.steel);
const drillRotor = new THREE.Group();
drillRotor.position.set(-0.05, 2.06, 0);
drillMachine.add(drillRotor);
addBox(drillRotor, [0.25, 0.73, 0.25], [0, 0, 0], mats.steelLight);
addBox(drillRotor, [0.09, 0.55, 0.09], [0, -0.55, 0], mats.yellow);
addBox(drillRotor, [0.31, 0.08, 0.08], [0, 0.15, 0], mats.white);
// Machine B: press.
const pressMachine = new THREE.Group();
pressMachine.position.set(4.8, 0, -1.75);
scene.add(pressMachine);
addBox(pressMachine, [2.05, 0.44, 1.85], [0, 0.24, 0], mats.dark);
addBox(pressMachine, [0.48, 2.28, 1.55], [0.65, 1.47, 0], mats.orange);
addBox(pressMachine, [1.65, 0.3, 1.55], [0.05, 2.63, 0], mats.yellow);
addBox(pressMachine, [0.62, 0.45, 0.5], [-0.72, 1.08, 0], mats.steel);
const pressRam = new THREE.Group();
pressRam.position.set(-0.02, 1.74, 0);
pressMachine.add(pressRam);
addBox(pressRam, [0.38, 0.86, 0.38], [0, 0, 0], mats.steelLight);
addBox(pressRam, [0.85, 0.18, 0.85], [0, -0.48, 0], mats.dark);
const pressWheel = new THREE.Group();
pressWheel.position.set(-0.9, 1.22, 0);
pressMachine.add(pressWheel);
addBox(pressWheel, [0.14, 0.52, 0.52], [0, 0, 0], mats.blue);
addBox(pressWheel, [0.16, 0.12, 0.12], [0, 0.2, 0], mats.white);
// -------------------------------------------------------------------------
// State model and visible power rules
// -------------------------------------------------------------------------
const state = {
drive: true,
route: "BOTH",
clutchA: false,
clutchB: true
};
function isRouted(id) {
return state.route === "BOTH" || state.route === id;
}
function isPowered(id) {
return (
state.drive &&
isRouted(id) &&
state["clutch" + id]
);
}
function getReason(id) {
if (!state.drive) {
return "Master drive is off.";
}
if (!isRouted(id)) {
return state.route === "A"
? "The selector is routed to A only."
: "The selector is routed to B only.";
}
if (!state["clutch" + id]) {
return `Clutch ${id} is open, so the branch is disconnected.`;
}
return "Drive, route, and clutch are all engaged.";
}
const masterButton = document.querySelector("#masterButton");
const clutchAButton = document.querySelector("#clutchAButton");
const clutchBButton = document.querySelector("#clutchBButton");
const routeButtons = [...document.querySelectorAll("[data-route]")];
const presetButtons = [...document.querySelectorAll("[data-preset]")];
function updateUI() {
masterButton.classList.toggle("on", state.drive);
masterButton.setAttribute("aria-pressed", String(state.drive));
masterButton.textContent = state.drive ? "DRIVE ON" : "DRIVE OFF";
routeButtons.forEach((button) => {
const active = button.dataset.route === state.route;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
for (const id of ["A", "B"]) {
const button = id === "A" ? clutchAButton : clutchBButton;
const engaged = state["clutch" + id];
button.classList.toggle("engaged", engaged);
button.classList.toggle("open", !engaged);
button.setAttribute("aria-pressed", String(engaged));
button.textContent = `${id} / ${engaged ? "ENGAGED" : "OPEN"}`;
const card = document.querySelector(`#status${id}`);
const stateLabel = document.querySelector(`#state${id}`);
const reasonLabel = document.querySelector(`#reason${id}`);
const powered = isPowered(id);
card.classList.toggle("powered", powered);
stateLabel.textContent = powered ? "POWERED" : "DISCONNECTED";
reasonLabel.textContent = getReason(id);
const branch = branches[id];
drawStatusSprite(branch.statusSprite, powered, getReason(id));
}
}
function setState(next) {
Object.assign(state, next);
updateUI();
}
function toggleClutch(id) {
state["clutch" + id] = !state["clutch" + id];
updateUI();
}
masterButton.addEventListener("click", () => {
state.drive = !state.drive;
updateUI();
});
clutchAButton.addEventListener("click", () => toggleClutch("A"));
clutchBButton.addEventListener("click", () => toggleClutch("B"));
routeButtons.forEach((button) => {
button.addEventListener("click", () => {
state.route = button.dataset.route;
updateUI();
});
});
presetButtons.forEach((button) => {
button.addEventListener("click", () => {
const preset = button.dataset.preset;
if (preset === "mixed") {
setState({
drive: true,
route: "BOTH",
clutchA: false,
clutchB: true
});
} else if (preset === "both") {
setState({
drive: true,
route: "BOTH",
clutchA: true,
clutchB: true
});
} else if (preset === "safe") {
setState({
drive: false,
route: "BOTH",
clutchA: false,
clutchB: false
});
}
});
});
// -------------------------------------------------------------------------
// Custom orbit controls and coupling picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerDown = new THREE.Vector2();
let pointerIsDown = false;
let pointerMoved = false;
let hoverAction = null;
function getPointerNDC(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findAction(object) {
let current = object;
while (current) {
if (current.userData && current.userData.action) {
return current.userData.action;
}
current = current.parent;
}
return null;
}
function pickAction(event) {
getPointerNDC(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(pickables, true);
for (const hit of hits) {
const action = findAction(hit.object);
if (action) return action;
}
return null;
}
function setHover(action) {
if (hoverAction === action) return;
hoverAction = action;
for (const branch of Object.values(branches)) {
for (const hub of [branch.inputHub, branch.outputHub]) {
const mat = hub.userData.bodyMaterial;
mat.emissive.setHex(action && hub.userData.action === action ? 0xffffff : 0x000000);
mat.emissiveIntensity = action && hub.userData.action === action ? 0.34 : 0;
}
}
canvas.style.cursor = action ? "pointer" : "grab";
}
canvas.addEventListener("pointerdown", (event) => {
pointerIsDown = true;
pointerMoved = false;
pointerDown.set(event.clientX, event.clientY);
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (pointerIsDown) {
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
if (Math.abs(dx) + Math.abs(dy) > 3) {
pointerMoved = true;
}
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.18,
1.12
);
pointerDown.set(event.clientX, event.clientY);
updateCamera();
} else {
setHover(pickAction(event));
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerMoved) {
const action = pickAction(event);
if (action && action.startsWith("toggle:")) {
toggleClutch(action.split(":")[1]);
}
}
pointerIsDown = false;
canvas.style.cursor = hoverAction ? "pointer" : "grab";
});
canvas.addEventListener("pointercancel", () => {
pointerIsDown = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
10.5,
24
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.key.toLowerCase() === "a") toggleClutch("A");
if (event.key.toLowerCase() === "b") toggleClutch("B");
if (event.key.toLowerCase() === "d") {
state.drive = !state.drive;
updateUI();
}
if (event.key === "1") {
state.route = "A";
updateUI();
}
if (event.key === "2") {
state.route = "B";
updateUI();
}
if (event.key === "3") {
state.route = "BOTH";
updateUI();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let sourceAngle = 0;
function updateBranch(branch, dt, elapsed) {
const routed = isRouted(branch.id);
const powered = isPowered(branch.id);
const inputSpeed = state.drive && routed ? 3.0 : 0;
const outputSpeed = powered ? 3.0 : 0;
branch.inputAngle += inputSpeed * dt;
branch.outputAngle += outputSpeed * dt;
branch.inputShaft.rotation.x = branch.inputAngle;
branch.inputHub.rotation.x = branch.inputAngle;
branch.outputShaft.rotation.x = branch.outputAngle;
branch.outputHub.rotation.x = branch.outputAngle;
// The output dog slides away when its clutch is open.
const targetHubX = state["clutch" + branch.id] ? 0.2 : 0.52;
branch.outputHub.position.x = THREE.MathUtils.lerp(
branch.outputHub.position.x,
targetHubX,
1 - Math.exp(-10 * dt)
);
const outputColor = powered
? 0x36c991
: routed && state.drive
? 0xf2b84b
: 0xe65d4f;
branch.outputHubMaterial.color.setHex(outputColor);
branch.outputHubMaterial.emissive.setHex(
hoverAction === `toggle:${branch.id}` ? 0xffffff : outputColor
);
branch.outputHubMaterial.emissiveIntensity =
hoverAction === `toggle:${branch.id}`
? 0.34
: powered
? 0.14
: 0.05;
branch.lampMaterial.color.setHex(outputColor);
branch.lampMaterial.emissive.setHex(outputColor);
branch.lampMaterial.emissiveIntensity = powered ? 0.95 : 0.4;
const routeSelected = routed;
branch.routeMaterial.color.setHex(
routeSelected ? 0xf2b84b : 0x788b8b
);
branch.routeMaterial.emissive.setHex(
routeSelected ? 0x9a5707 : 0x000000
);
branch.routeMaterial.emissiveIntensity = routeSelected
? state.drive
? 0.75
: 0.12
: 0;
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
sourceAngle += (state.drive ? 3.0 : 0) * dt;
mainShaft.rotation.x = sourceAngle;
sourceBeaconMaterial.color.setHex(state.drive ? 0x36c991 : 0xe65d4f);
sourceBeaconMaterial.emissive.setHex(state.drive ? 0x1a9d6d : 0x8d1f1b);
sourceBeaconMaterial.emissiveIntensity = state.drive ? 0.95 : 0.45;
for (const route of ["A", "B", "BOTH"]) {
const selected = state.route === route;
routeLampMaterials[route].color.setHex(
selected ? 0xf2b84b : 0x738987
);
routeLampMaterials[route].emissive.setHex(
selected ? 0x9d5b08 : 0x000000
);
routeLampMaterials[route].emissiveIntensity = selected ? 0.75 : 0;
}
updateBranch(branchA, dt, elapsed);
updateBranch(branchB, dt, elapsed);
drillRotor.rotation.y = branchA.outputAngle * 1.8;
pressWheel.rotation.x = branchB.outputAngle * 1.4;
const pressTarget = isPowered("B")
? 1.55 + Math.sin(elapsed * 5.2) * 0.28
: 1.55;
pressRam.position.y = THREE.MathUtils.lerp(
pressRam.position.y,
pressTarget,
1 - Math.exp(-9 * dt)
);
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, false);
});
updateUI();
updateBranch(branchA, 0.016, 0);
updateBranch(branchB, 0.016, 0);
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").style.display = "none";
});
</script>
</body>
</html>
```