Prompt specification
Build or customize the Three.js 3D scene "Flat-pack assembly workbench" 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: Flat-pack assembly workbench
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/flat-pack-assembly-workbench-x0303
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts screwheads and matching driver bits. Arrange panels, screws, drivers, and illustrated part trays; selecting a fastener highlights its matching driver and the receiving holes in a small furniture assembly. Voxel style.
Working Three.js Starter (flat-pack-assembly-workbench-x0303):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Fastener Match — Three.js</title>
<style>
:root {
--ink: #252824;
--paper: #f4eddd;
--panel: rgba(250, 246, 235, 0.9);
--line: rgba(37, 40, 36, 0.17);
--shadow: 0 12px 35px rgba(45, 38, 28, 0.13);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7dfd0;
color: var(--ink);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
canvas {
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.card {
background: var(--panel);
border: 1px solid rgba(255, 255, 255, 0.7);
box-shadow: var(--shadow);
backdrop-filter: blur(9px);
}
.title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(330px, calc(100vw - 44px));
padding: 17px 18px 16px;
border-radius: 4px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 9px;
color: #68695f;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
background: #f2b84b;
box-shadow: 4px -4px 0 #e46755, 4px 4px 0 #42ad9b;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 31px);
line-height: 1;
letter-spacing: -0.06em;
text-transform: uppercase;
}
.intro {
margin: 10px 0 14px;
color: #64665e;
font-size: 11px;
line-height: 1.55;
}
.match-card {
display: grid;
grid-template-columns: 12px 1fr;
gap: 11px;
align-items: stretch;
padding: 11px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.48);
}
.match-swatch {
width: 12px;
min-height: 42px;
background: #f2b84b;
transition: background 180ms ease;
}
.match-title {
font-size: 12px;
font-weight: 900;
letter-spacing: 0.04em;
}
.match-desc {
margin-top: 5px;
color: #71736b;
font-size: 9px;
line-height: 1.45;
}
.chips {
position: absolute;
left: 22px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 7px;
pointer-events: auto;
}
.type-chip {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.76);
border-radius: 3px;
background: var(--panel);
box-shadow: 0 6px 18px rgba(45, 38, 28, 0.1);
color: var(--ink);
font: 800 10px ui-monospace, SFMono-Regular, Menlo, monospace;
cursor: pointer;
transition: transform 140ms ease, background 140ms ease;
}
.type-chip:hover,
.type-chip.active {
transform: translateY(-2px);
background: #fffaf0;
}
.type-chip.active {
outline: 2px solid var(--chip-color);
outline-offset: -3px;
}
.chip-dot {
width: 10px;
height: 10px;
background: var(--chip-color);
box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.13);
}
.help {
position: absolute;
right: 22px;
bottom: 22px;
padding: 10px 12px;
border-radius: 3px;
background: rgba(37, 40, 36, 0.82);
color: #f7f0df;
font-size: 9px;
line-height: 1.55;
letter-spacing: 0.04em;
text-align: right;
}
.tooltip {
position: fixed;
z-index: 10;
max-width: 210px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 3px;
background: rgba(31, 33, 30, 0.92);
color: #fff8e9;
font-size: 10px;
line-height: 1.4;
pointer-events: none;
opacity: 0;
transform: translate(12px, 12px);
transition: opacity 100ms ease;
}
.tooltip.visible {
opacity: 1;
}
.loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: #e7dfd0;
color: #575a52;
font-size: 12px;
font-weight: 900;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
.title-card {
top: 12px;
left: 12px;
width: min(290px, calc(100vw - 24px));
padding: 13px;
}
.intro {
display: none;
}
.chips {
left: 12px;
bottom: 12px;
right: 12px;
}
.type-chip {
padding: 8px;
font-size: 9px;
}
.help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel fastener matching workshop"></canvas>
<div class="hud">
<section class="title-card card">
<div class="eyebrow">Assembly Lab / 03</div>
<h1>Fastener Match</h1>
<p class="intro">
Select a screwhead, driver bit, tray label, or receiving socket.
The matching tool and cabinet holes illuminate automatically.
</p>
<div class="match-card" aria-live="polite">
<div class="match-swatch" id="matchSwatch"></div>
<div>
<div class="match-title" id="matchTitle">#2 PHILLIPS</div>
<div class="match-desc" id="matchDesc">P2 fastener → P2 driver → receiving holes</div>
</div>
</div>
</section>
<nav class="chips" aria-label="Fastener types">
<button class="type-chip active" data-type="phillips" style="--chip-color:#f2b84b">
<span class="chip-dot"></span>P2 PHILLIPS
</button>
<button class="type-chip" data-type="torx" style="--chip-color:#e46755">
<span class="chip-dot"></span>T20 TORX
</button>
<button class="type-chip" data-type="hex" style="--chip-color:#42ad9b">
<span class="chip-dot"></span>H5 HEX
</button>
</nav>
<div class="help">
DRAG — ORBIT WHEEL — ZOOM<br>
R — RESET 1 / 2 / 3 — SELECT
</div>
</div>
<div class="tooltip" id="tooltip"></div>
<div class="loading" id="loading">Building voxel workshop…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Core setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const tooltip = document.querySelector("#tooltip");
const matchSwatch = document.querySelector("#matchSwatch");
const matchTitle = document.querySelector("#matchTitle");
const matchDesc = document.querySelector("#matchDesc");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7dfd0);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.OrthographicCamera(-10, 10, 6, -6, 0.1, 80);
const cameraTarget = new THREE.Vector3(0, 1.25, 0);
const controls = {
yaw: 0.52,
pitch: 0.53,
zoom: 1,
dragging: false,
moved: false,
lastX: 0,
lastY: 0
};
const sceneRoot = new THREE.Group();
scene.add(sceneRoot);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pickables = [];
const fasteners = [];
const types = {
phillips: {
key: "phillips",
label: "#2 PHILLIPS",
short: "P2",
driverShort: "P2",
driverLabel: "P2 Phillips driver",
css: "#f2b84b",
color: 0xf2b84b,
subtitle: "CROSS HEAD · 6.3 MM"
},
torx: {
key: "torx",
label: "T20 TORX",
short: "T20",
driverShort: "T20",
driverLabel: "T20 Torx driver",
css: "#e46755",
color: 0xe46755,
subtitle: "STAR HEAD · SECURITY TIP"
},
hex: {
key: "hex",
label: "5 MM HEX",
short: "H5",
driverShort: "H5",
driverLabel: "5 mm hex driver",
css: "#42ad9b",
color: 0x42ad9b,
subtitle: "HEX SOCKET · FLAT FLAT"
}
};
// -------------------------------------------------------------------------
// Materials and voxel helpers
// -------------------------------------------------------------------------
function standard(color, roughness = 0.78, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
});
}
const materials = {
wall: standard(0xe5ddcc),
pegboard: standard(0xcbbd9f),
pegHole: standard(0x6e675b),
bench: standard(0xa96f3d),
benchEdge: standard(0x704729),
benchGrain: standard(0x8a552e),
floor: standard(0xb9ad97),
woodA: standard(0xc98d52),
woodB: standard(0xd8a66b),
woodDark: standard(0x9e6237),
woodLine: standard(0xb47743),
pocket: standard(0x5a4635),
metal: standard(0xaeb4b2, 0.3, 0.72),
metalDark: standard(0x606866, 0.4, 0.65),
screwShaft: standard(0x929a98, 0.28, 0.78),
tray: standard(0x737c79, 0.66, 0.12),
trayInside: standard(0x555e5a),
dark: standard(0x262a27),
labelDark: standard(0x343833),
white: standard(0xf7f0df)
};
function box(parent, w, h, d, x, y, z, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeLabelTexture(title, subtitle, color, icon = "screw") {
const c = document.createElement("canvas");
c.width = 512;
c.height = 144;
const ctx = c.getContext("2d");
ctx.fillStyle = "#f3ecdc";
ctx.fillRect(0, 0, c.width, c.height);
ctx.fillStyle = color;
ctx.fillRect(0, 0, 18, c.height);
ctx.strokeStyle = "#343832";
ctx.lineWidth = 8;
ctx.strokeRect(4, 4, c.width - 8, c.height - 8);
// Pixel-art fastener illustration.
ctx.fillStyle = color;
if (icon === "driver") {
ctx.fillRect(58, 20, 38, 14);
ctx.fillRect(52, 34, 50, 28);
ctx.fillRect(60, 62, 34, 20);
ctx.fillRect(73, 82, 8, 25);
ctx.fillRect(66, 107, 22, 8);
} else if (icon === "panel") {
ctx.fillRect(40, 25, 60, 86);
ctx.fillStyle = "#9b6439";
ctx.fillRect(47, 38, 46, 6);
ctx.fillRect(47, 60, 46, 6);
ctx.fillRect(47, 82, 46, 6);
} else {
ctx.fillRect(43, 40, 64, 18);
ctx.fillRect(49, 58, 52, 14);
ctx.fillRect(66, 72, 18, 36);
ctx.fillRect(58, 100, 34, 8);
}
ctx.fillStyle = "#252824";
ctx.font = "900 37px monospace";
ctx.textBaseline = "middle";
ctx.fillText(title, 126, 52);
ctx.fillStyle = "#696b63";
ctx.font = "700 18px monospace";
ctx.fillText(subtitle, 127, 99);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.magFilter = THREE.NearestFilter;
texture.minFilter = THREE.LinearFilter;
return texture;
}
function makeLabelPlane(parent, title, subtitle, color, icon, w, h, x, y, z) {
const texture = makeLabelTexture(title, subtitle, color, icon);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(w, h),
new THREE.MeshBasicMaterial({ map: texture })
);
mesh.position.set(x, y, z);
parent.add(mesh);
return mesh;
}
function addTopGlyph(parent, typeKey, material, y = 0, scale = 1) {
const group = new THREE.Group();
group.position.y = y;
parent.add(group);
const s = scale;
if (typeKey === "phillips") {
box(group, 0.28 * s, 0.025, 0.075 * s, 0, 0, 0, material, false);
box(group, 0.075 * s, 0.025, 0.28 * s, 0, 0, 0, material, false);
} else if (typeKey === "torx") {
box(group, 0.12 * s, 0.025, 0.12 * s, 0, 0, 0, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, 0.09 * s, 0, 0.09 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, -0.09 * s, 0, 0.09 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, 0.09 * s, 0, -0.09 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, -0.09 * s, 0, -0.09 * s, material, false);
} else {
box(group, 0.075 * s, 0.025, 0.075 * s, 0, 0, 0.12 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, 0.105 * s, 0, 0.06 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, 0.105 * s, 0, -0.06 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, 0, 0, -0.12 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, -0.105 * s, 0, -0.06 * s, material, false);
box(group, 0.075 * s, 0.025, 0.075 * s, -0.105 * s, 0, 0.06 * s, material, false);
}
return group;
}
function addFrontGlyph(parent, typeKey, material, z = 0, scale = 1) {
const group = new THREE.Group();
group.position.z = z;
parent.add(group);
const s = scale;
if (typeKey === "phillips") {
box(group, 0.16 * s, 0.045 * s, 0.03, 0, 0, 0, material, false);
box(group, 0.045 * s, 0.16 * s, 0.03, 0, 0, 0, material, false);
} else if (typeKey === "torx") {
box(group, 0.08 * s, 0.08 * s, 0.03, 0, 0, 0, material, false);
box(group, 0.055 * s, 0.055 * s, 0.03, 0.07 * s, 0.07 * s, 0, material, false);
box(group, 0.055 * s, 0.055 * s, 0.03, -0.07 * s, 0.07 * s, 0, material, false);
box(group, 0.055 * s, 0.055 * s, 0.03, 0.07 * s, -0.07 * s, 0, material, false);
box(group, 0.055 * s, 0.055 * s, 0.03, -0.07 * s, -0.07 * s, 0, material, false);
} else {
for (let i = 0; i < 6; i++) {
const a = i * Math.PI / 3;
box(
group,
0.052 * s,
0.052 * s,
0.03,
Math.cos(a) * 0.065 * s,
Math.sin(a) * 0.065 * s,
0,
material,
false
);
}
}
return group;
}
// -------------------------------------------------------------------------
// Lighting and workshop environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfff7e6, 0x655744, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d2, 3.1);
sun.position.set(-6, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.00025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xbcd5d2, 0.9);
fill.position.set(8, 6, -1);
scene.add(fill);
box(sceneRoot, 40, 0.25, 30, 0, -2.05, 0, materials.floor);
box(sceneRoot, 16, 8, 0.28, 0, 2.3, -5.35, materials.wall);
const pegboard = new THREE.Group();
pegboard.position.set(0, 2.55, -5.14);
sceneRoot.add(pegboard);
box(pegboard, 14.7, 5.15, 0.18, 0, 0, 0, materials.pegboard);
for (let row = 0; row < 6; row++) {
for (let col = 0; col < 15; col++) {
box(
pegboard,
0.075,
0.075,
0.035,
-6.75 + col * 0.96,
-2.05 + row * 0.82,
0.115,
materials.pegHole,
false
);
}
}
box(pegboard, 15.2, 0.16, 0.48, 0, -2.66, 0.12, materials.benchEdge);
box(pegboard, 15.2, 0.16, 0.48, 0, 2.66, 0.12, materials.benchEdge);
// Small voxel storage blocks on the rear shelf.
const storage = new THREE.Group();
storage.position.set(0, 4.75, -4.88);
sceneRoot.add(storage);
box(storage, 1.1, 0.65, 0.75, -5.9, 0, 0, materials.woodB);
box(storage, 0.85, 0.48, 0.65, -4.85, 0, 0.03, materials.woodA);
box(storage, 0.55, 0.82, 0.55, 5.8, 0, 0, materials.trayInside);
box(storage, 0.8, 0.36, 0.62, 4.95, 0, 0.02, materials.woodDark);
// Workbench.
const bench = new THREE.Group();
sceneRoot.add(bench);
box(bench, 16.2, 0.46, 10.1, 0, -0.23, 0, materials.bench);
box(bench, 16.35, 0.18, 0.35, 0, -0.1, 4.95, materials.benchEdge);
box(bench, 16.35, 0.18, 0.35, 0, -0.1, -4.95, materials.benchEdge);
for (let i = 0; i < 17; i++) {
const x = -7.3 + i * 0.91;
const z = -3.9 + ((i * 7) % 8) * 0.86;
box(bench, 0.025, 0.012, 5.9, x, 0.009, z, materials.benchGrain, false);
}
for (const x of [-6.8, 6.8]) {
for (const z of [-3.7, 3.7]) {
box(bench, 0.55, 1.8, 0.55, x, -1.35, z, materials.benchEdge);
}
}
// -------------------------------------------------------------------------
// Voxel panel stock
// -------------------------------------------------------------------------
const panelDisplay = new THREE.Group();
panelDisplay.position.set(-4.95, 0, -1.1);
sceneRoot.add(panelDisplay);
box(panelDisplay, 3.2, 3.8, 0.22, -0.4, 1.96, 0, materials.woodA);
box(panelDisplay, 3.26, 0.12, 0.28, -0.4, 3.8, 0, materials.woodDark);
box(panelDisplay, 3.26, 0.12, 0.28, -0.4, 0.12, 0, materials.woodDark);
for (let i = 0; i < 6; i++) {
box(
panelDisplay,
2.62 - (i % 2) * 0.35,
0.025,
0.018,
-0.45,
0.58 + i * 0.52,
0.12,
materials.woodLine,
false
);
}
box(panelDisplay, 2.6, 3.4, 0.22, 1.35, 1.76, 0.48, materials.woodB);
box(panelDisplay, 2.66, 0.11, 0.28, 1.35, 3.41, 0.48, materials.woodDark);
for (const y of [0.72, 1.42, 2.12, 2.82]) {
for (const x of [0.55, 2.15]) {
box(panelDisplay, 0.105, 0.105, 0.035, x, y, 0.61, materials.pocket, false);
}
}
for (let i = 0; i < 4; i++) {
box(
panelDisplay,
1.8 - i * 0.2,
0.022,
0.018,
1.35,
0.6 + i * 0.68,
0.61,
materials.woodLine,
false
);
}
makeLabelPlane(
panelDisplay,
"PANEL STOCK",
"SOLID OAK / PRE-DRILL",
"#d8a66b",
"panel",
2.55,
0.64,
-0.4,
3.16,
0.135
);
box(panelDisplay, 3.3, 0.16, 0.8, -0.4, 0.03, 0.18, materials.benchEdge);
// -------------------------------------------------------------------------
// Fasteners, drivers, and illustrated part trays
// -------------------------------------------------------------------------
function createFastener(parent, typeKey, x, z, index) {
const type = types[typeKey];
const group = new THREE.Group();
group.position.set(x, 0.34, z);
parent.add(group);
const headMaterial = standard(0xb8bdb9, 0.25, 0.75);
box(group, 0.5, 0.16, 0.5, 0, 0, 0, headMaterial);
box(group, 0.26, 0.11, 0.24, 0, 0, 0.3, materials.screwShaft);
box(group, 0.13, 0.1, 0.66, 0, -0.005, 0.48, materials.screwShaft);
box(group, 0.08, 0.075, 0.11, 0, -0.005, 0.85, materials.metalDark);
const markerMaterial = standard(type.color, 0.48, 0.2);
addTopGlyph(group, typeKey, markerMaterial, 0.092, 1);
const padMaterial = new THREE.MeshBasicMaterial({
color: 0xffd35a,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const pad = box(
group,
0.68,
0.026,
0.68,
0,
-0.115,
0,
padMaterial,
false
);
pad.visible = false;
group.userData = {
pickKind: "fastener",
type: typeKey,
label: `${type.label} screw`
};
const fastener = {
group,
type: typeKey,
headMaterial,
pad,
index
};
fasteners.push(fastener);
pickables.push(group);
return fastener;
}
function createDriver(parent, typeKey, x, z) {
const type = types[typeKey];
const group = new THREE.Group();
group.position.set(x, 0.39, z);
parent.add(group);
const bodyMaterial = standard(type.color, 0.5, 0.15);
const accentMaterial = standard(type.color, 0.38, 0.32);
box(group, 0.54, 0.34, 0.66, 0, 0, -0.58, bodyMaterial);
box(group, 0.59, 0.39, 0.12, 0, 0, -0.3, accentMaterial);
box(group, 0.58, 0.4, 0.12, 0, 0, -0.84, accentMaterial);
for (let i = 0; i < 4; i++) {
box(
group,
0.59,
0.42,
0.035,
0,
0,
-0.72 + i * 0.1,
materials.metalDark,
false
);
}
box(group, 0.16, 0.16, 0.88, 0, 0, 0.2, materials.metal);
box(group, 0.23, 0.22, 0.17, 0, 0, 0.64, materials.metalDark);
const tipGlyphMaterial = new THREE.MeshStandardMaterial({
color: type.color,
emissive: type.color,
emissiveIntensity: 0.08,
roughness: 0.45,
metalness: 0.25
});
addFrontGlyph(group, typeKey, tipGlyphMaterial, 0.745, 0.72);
const padMaterial = new THREE.MeshBasicMaterial({
color: type.color,
transparent: true,
opacity: 0.25,
depthWrite: false
});
const pad = box(
group,
0.9,
0.025,
1.68,
0,
-0.235,
0,
padMaterial,
false
);
pad.visible = false;
group.userData = {
pickKind: "driver",
type: typeKey,
label: type.driverLabel
};
type.driverBodyMaterial = bodyMaterial;
type.driverAccentMaterial = accentMaterial;
type.driverPad = pad;
type.driverGroup = group;
pickables.push(group);
return group;
}
function createTray(typeKey, x) {
const type = types[typeKey];
const tray = new THREE.Group();
tray.position.set(x, 0, 2.65);
sceneRoot.add(tray);
const width = 2.25;
const depth = 1.95;
box(tray, width, 0.14, depth, 0, 0.1, 0, materials.trayInside);
box(tray, 0.11, 0.34, depth, -width / 2, 0.25, 0, materials.tray);
box(tray, 0.11, 0.34, depth, width / 2, 0.25, 0, materials.tray);
box(tray, width, 0.34, 0.11, 0, 0.25, -depth / 2, materials.tray);
box(tray, width, 0.34, 0.11, 0, 0.25, depth / 2, materials.tray);
const stripMaterial = standard(type.color, 0.55, 0.12);
type.trayStripMaterial = stripMaterial;
box(
tray,
width - 0.12,
0.055,
0.035,
0,
0.435,
depth / 2 + 0.073,
stripMaterial,
false
);
makeLabelPlane(
tray,
type.label,
type.subtitle,
type.css,
"screw",
1.87,
0.49,
0,
0.29,
depth / 2 + 0.066
);
createFastener(tray, typeKey, -0.7, -0.52, 0);
createFastener(tray, typeKey, -0.2, -0.52, 1);
createDriver(tray, typeKey, 0.58, -0.02);
return tray;
}
createTray("phillips", -2.6);
createTray("torx", 0);
createTray("hex", 2.6);
// -------------------------------------------------------------------------
// Small furniture assembly with receiving holes
// -------------------------------------------------------------------------
const cabinet = new THREE.Group();
cabinet.position.set(4.55, 0, -0.65);
sceneRoot.add(cabinet);
for (const x of [-1.1, 1.1]) {
for (const z of [-0.78, 0.78]) {
box(cabinet, 0.28, 0.76, 0.28, x, 0.38, z, materials.woodDark);
}
}
box(cabinet, 2.7, 0.24, 2.08, 0, 0.84, 0, materials.woodA);
box(cabinet, 2.7, 0.24, 2.08, 0, 3.16, 0, materials.woodB);
box(cabinet, 0.22, 2.25, 2.08, -1.24, 1.99, 0, materials.woodA);
box(cabinet, 0.22, 2.25, 2.08, 1.24, 1.99, 0, materials.woodA);
box(cabinet, 2.46, 2.18, 0.17, 0, 1.98, -0.95, materials.woodDark);
box(cabinet, 2.42, 0.16, 1.92, 0, 1.92, 0, materials.woodB);
box(cabinet, 2.54, 0.24, 0.18, 0, 1.08, 1.08, materials.woodDark);
box(cabinet, 2.54, 0.24, 0.18, 0, 2.88, 1.08, materials.woodDark);
box(cabinet, 0.1, 0.3, 0.13, -1.18, 2.98, 1.12, materials.metal);
box(cabinet, 0.1, 0.3, 0.13, 1.18, 2.98, 1.12, materials.metal);
makeLabelPlane(
cabinet,
"OPEN FRAME",
"NIGHTSTAND / 15 SOCKETS",
"#8c6645",
"panel",
1.8,
0.46,
0,
0.88,
1.19
);
function createReceivingHole(x, y, z, typeKey) {
const type = types[typeKey];
const group = new THREE.Group();
group.position.set(x, y, z);
cabinet.add(group);
if (!type.holeSocketMaterial) {
type.holeSocketMaterial = standard(0x262a27, 0.7, 0.08);
type.holeRimMaterial = standard(0x777065, 0.6, 0.1);
type.holeMotifMaterial = new THREE.MeshStandardMaterial({
color: type.color,
emissive: type.color,
emissiveIntensity: 0,
roughness: 0.55,
metalness: 0.1,
transparent: true,
opacity: 0.25,
depthWrite: false
});
}
box(group, 0.205, 0.205, 0.035, 0, 0, 0, type.holeRimMaterial, false);
box(group, 0.135, 0.135, 0.04, 0, 0, 0.025, type.holeSocketMaterial, false);
addFrontGlyph(group, typeKey, type.holeMotifMaterial, 0.052, 0.66);
group.userData = {
pickKind: "hole",
type: typeKey,
label: `${type.label} receiving socket`
};
type.holeCount = (type.holeCount || 0) + 1;
pickables.push(group);
return group;
}
const topOrder = [
"phillips", "phillips", "phillips",
"torx", "torx", "torx",
"hex", "hex", "hex"
];
for (let i = 0; i < 9; i++) {
createReceivingHole(
-1 + i * 0.25,
2.88,
1.205,
topOrder[i]
);
}
const lowerOrder = [
"phillips", "phillips",
"torx", "torx",
"hex", "hex"
];
for (let i = 0; i < 6; i++) {
createReceivingHole(
-0.9 + i * 0.36,
1.08,
1.205,
lowerOrder[i]
);
}
for (const [typeKey, centerX] of [
["phillips", -0.75],
["torx", 0],
["hex", 0.75]
]) {
const type = types[typeKey];
const material = standard(type.color, 0.55, 0.15);
const bar = box(
cabinet,
0.68,
0.055,
0.055,
centerX,
2.69,
1.23,
material,
false
);
type.holeBarMaterial = material;
type.holeBar = bar;
}
// -------------------------------------------------------------------------
// Voxel match trail from the driver toward the cabinet sockets
// -------------------------------------------------------------------------
const routeMaterial = new THREE.MeshBasicMaterial({
color: types.phillips.color,
transparent: true,
opacity: 0.72,
depthWrite: false
});
const route = new THREE.Group();
sceneRoot.add(route);
for (let i = 0; i < 17; i++) {
box(route, 0.085, 0.026, 0.085, 0, 0, 0, routeMaterial, false);
}
function updateRoute(typeKey) {
const type = types[typeKey];
const start = new THREE.Vector3();
const end = new THREE.Vector3();
type.driverGroup.getWorldPosition(start);
type.holeBar.getWorldPosition(end);
route.children.forEach((cube, index) => {
const t = (index + 1) / (route.children.length + 1);
cube.position.set(
THREE.MathUtils.lerp(start.x, end.x, t),
0.035,
THREE.MathUtils.lerp(start.z, end.z, t)
);
});
routeMaterial.color.set(type.color);
}
// -------------------------------------------------------------------------
// Selection and highlighting
// -------------------------------------------------------------------------
let selectedFastener = null;
let selectedType = "phillips";
function selectFastener(fastener) {
selectedFastener = fastener;
selectedType = fastener.type;
updateSelection();
}
function selectType(typeKey) {
const fastener = fasteners.find(item => item.type === typeKey);
if (fastener) {
selectFastener(fastener);
}
}
function updateSelection() {
const type = types[selectedType];
fasteners.forEach(fastener => {
fastener.pad.visible = false;
fastener.headMaterial.emissive.set(0x000000);
fastener.headMaterial.emissiveIntensity = 0;
});
Object.values(types).forEach(item => {
const active = item.key === selectedType;
item.driverPad.visible = active;
item.driverBodyMaterial.emissive.set(active ? item.color : 0x000000);
item.driverBodyMaterial.emissiveIntensity = active ? 0.42 : 0;
item.driverAccentMaterial.emissive.set(active ? item.color : 0x000000);
item.driverAccentMaterial.emissiveIntensity = active ? 0.22 : 0;
item.trayStripMaterial.emissive.set(active ? item.color : 0x000000);
item.trayStripMaterial.emissiveIntensity = active ? 0.32 : 0;
item.holeSocketMaterial.color.set(active ? 0x24423b : 0x262a27);
item.holeSocketMaterial.emissive.set(item.color);
item.holeSocketMaterial.emissiveIntensity = active ? 0.4 : 0;
item.holeRimMaterial.color.set(active ? item.color : 0x777065);
item.holeRimMaterial.emissive.set(active ? item.color : 0x000000);
item.holeRimMaterial.emissiveIntensity = active ? 0.25 : 0;
item.holeMotifMaterial.opacity = active ? 1 : 0.25;
item.holeMotifMaterial.emissiveIntensity = active ? 0.95 : 0;
item.holeBarMaterial.emissive.set(active ? item.color : 0x000000);
item.holeBarMaterial.emissiveIntensity = active ? 0.6 : 0;
});
if (selectedFastener) {
selectedFastener.pad.visible = true;
selectedFastener.headMaterial.emissive.set(0xffd35a);
selectedFastener.headMaterial.emissiveIntensity = 0.48;
}
matchSwatch.style.background = type.css;
matchTitle.textContent = type.label;
matchDesc.textContent =
`${type.short} fastener → ${type.driverShort} driver → ${type.holeCount} receiving holes`;
document.querySelectorAll(".type-chip").forEach(button => {
button.classList.toggle("active", button.dataset.type === selectedType);
});
scene.updateMatrixWorld(true);
updateRoute(selectedType);
}
// -------------------------------------------------------------------------
// Camera controls implemented directly in this file
// -------------------------------------------------------------------------
function updateCamera() {
const radius = 20;
camera.position.set(
cameraTarget.x + Math.sin(controls.yaw) * Math.cos(controls.pitch) * radius,
cameraTarget.y + Math.sin(controls.pitch) * radius,
cameraTarget.z + Math.cos(controls.yaw) * Math.cos(controls.pitch) * radius
);
camera.lookAt(cameraTarget);
camera.zoom = controls.zoom;
camera.updateProjectionMatrix();
}
function resetCamera() {
controls.yaw = 0.52;
controls.pitch = 0.53;
controls.zoom = 1;
updateCamera();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setSize(width, height, false);
const viewHeight = Math.max(10.8, 18 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
controls.dragging = true;
controls.moved = false;
controls.lastX = event.clientX;
controls.lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", event => {
if (controls.dragging) {
const dx = event.clientX - controls.lastX;
const dy = event.clientY - controls.lastY;
if (Math.abs(dx) + Math.abs(dy) > 5) {
controls.moved = true;
}
controls.lastX = event.clientX;
controls.lastY = event.clientY;
controls.yaw = THREE.MathUtils.clamp(
controls.yaw - dx * 0.0055,
-0.95,
0.98
);
controls.pitch = THREE.MathUtils.clamp(
controls.pitch + dy * 0.0045,
0.32,
0.95
);
updateCamera();
hideTooltip();
return;
}
updateHover(event.clientX, event.clientY);
});
canvas.addEventListener("pointerup", event => {
controls.dragging = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
if (!controls.moved) {
const hit = pickAt(event.clientX, event.clientY);
if (hit) {
if (hit.kind === "fastener") {
const fastener = fasteners.find(item => item.group === hit.object);
if (fastener) selectFastener(fastener);
} else {
selectType(hit.type);
}
}
}
});
canvas.addEventListener("pointercancel", () => {
controls.dragging = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointerleave", () => {
if (!controls.dragging) {
canvas.style.cursor = "grab";
hideTooltip();
}
});
canvas.addEventListener(
"wheel",
event => {
event.preventDefault();
const factor = Math.exp(-event.deltaY * 0.001);
controls.zoom = THREE.MathUtils.clamp(
controls.zoom * factor,
0.72,
2.35
);
updateCamera();
},
{ passive: false }
);
window.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (key === "r") {
resetCamera();
} else if (key === "1") {
selectType("phillips");
} else if (key === "2") {
selectType("torx");
} else if (key === "3") {
selectType("hex");
}
});
document.querySelectorAll(".type-chip").forEach(button => {
button.addEventListener("click", () => {
selectType(button.dataset.type);
});
});
// -------------------------------------------------------------------------
// Picking and tooltip
// -------------------------------------------------------------------------
function pickAt(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, true);
for (const intersection of intersections) {
let object = intersection.object;
while (object && !object.userData.pickKind) {
object = object.parent;
}
if (object && object.userData.pickKind) {
return {
kind: object.userData.pickKind,
type: object.userData.type,
label: object.userData.label,
object
};
}
}
return null;
}
function updateHover(clientX, clientY) {
const hit = pickAt(clientX, clientY);
if (!hit) {
canvas.style.cursor = "grab";
hideTooltip();
return;
}
canvas.style.cursor = "pointer";
tooltip.textContent = hit.label;
tooltip.style.left = `${clientX}px`;
tooltip.style.top = `${clientY}px`;
tooltip.classList.add("visible");
}
function hideTooltip() {
tooltip.classList.remove("visible");
}
// -------------------------------------------------------------------------
// Start scene
// -------------------------------------------------------------------------
resize();
resetCamera();
selectType("phillips");
canvas.style.cursor = "grab";
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
const pulse = 0.5 + Math.sin(elapsed * 3.2) * 0.5;
if (selectedFastener) {
const scale = 1 + pulse * 0.045;
selectedFastener.pad.scale.set(scale, 1, scale);
selectedFastener.pad.material.opacity = 0.3 + pulse * 0.18;
}
const activeType = types[selectedType];
activeType.driverPad.material.opacity = 0.18 + pulse * 0.18;
activeType.holeMotifMaterial.emissiveIntensity = 0.65 + pulse * 0.55;
routeMaterial.opacity = 0.5 + pulse * 0.25;
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
});
</script>
</body>
</html>
```