Prompt specification
Build or customize the Three.js 3D scene "Pegboard hook spacing tile" 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: Pegboard hook spacing tile
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pegboard-hook-spacing-tile-x0209
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a stylized pegboard hook-spacing study. A perforated rectangular board stands upright on a small base. Place three hook types beside it: a short straight hook, a curved tool hook, and a wider bracket. Each has mounting pins aligned to the same grid interval on the board. Let the viewer move a selected hook to a few valid positions, keeping both pins aligned with holes before attachment. Include one offset outline that makes an incompatible placement obvious. Use light wood or pale painted board, darker hooks, and a close three-quarter view that reveals the gap behind the mounted hardware.
Working Three.js Starter (pegboard-hook-spacing-tile-x0209):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hook Spacing Study</title>
<style>
:root {
color-scheme: light;
--ink: #26302f;
--muted: #68716e;
--paper: rgba(250, 248, 242, 0.92);
--line: rgba(38, 48, 47, 0.14);
--amber: #c88b32;
--red: #b64e42;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5dc;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
left: 22px;
bottom: 22px;
width: min(390px, calc(100vw - 44px));
padding: 19px 20px 17px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 18px;
background: var(--paper);
box-shadow:
0 18px 45px rgba(53, 45, 35, 0.13),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--amber);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 25px;
font-weight: 720;
letter-spacing: -0.035em;
}
.intro {
margin: 8px 0 15px;
color: var(--muted);
font-size: 12.5px;
line-height: 1.48;
}
.label {
display: flex;
align-items: center;
gap: 8px;
margin: 13px 0 7px;
color: #59615f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.label::after {
content: "";
height: 1px;
flex: 1;
background: var(--line);
}
.buttons {
display: flex;
gap: 7px;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 10px;
background: rgba(255, 255, 255, 0.58);
color: var(--ink);
font: inherit;
font-size: 11.5px;
font-weight: 650;
cursor: pointer;
transition:
transform 120ms ease,
border-color 120ms ease,
background 120ms ease,
box-shadow 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(200, 139, 50, 0.45);
background: #fffdf8;
}
button:active {
transform: translateY(0);
}
button.active {
border-color: rgba(200, 139, 50, 0.72);
background: #f5e7cf;
box-shadow: inset 0 0 0 1px rgba(200, 139, 50, 0.12);
}
.type-button {
flex: 1;
padding-left: 8px;
padding-right: 8px;
}
.mount-button {
min-width: 54px;
flex: 1;
}
.mount-button span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 550;
}
.status {
min-height: 35px;
margin-top: 14px;
padding: 9px 11px;
border-radius: 10px;
background: rgba(40, 49, 48, 0.055);
color: #5b6461;
font-size: 11px;
line-height: 1.4;
}
.status.good {
background: rgba(200, 139, 50, 0.11);
color: #80571f;
}
.legend {
position: fixed;
top: 20px;
right: 20px;
padding: 11px 13px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 12px;
background: rgba(250, 248, 242, 0.82);
box-shadow: 0 10px 30px rgba(53, 45, 35, 0.09);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
pointer-events: none;
font-size: 10.5px;
}
.legend div {
display: flex;
align-items: center;
gap: 8px;
margin: 4px 0;
color: #5b6461;
}
.swatch {
width: 13px;
height: 13px;
border-radius: 50%;
border: 2px solid var(--amber);
background: rgba(200, 139, 50, 0.1);
}
.swatch.invalid {
border-color: var(--red);
background: transparent;
border-style: dashed;
}
.hint {
position: fixed;
right: 20px;
bottom: 20px;
max-width: 310px;
padding: 9px 12px;
border-radius: 10px;
background: rgba(250, 248, 242, 0.78);
color: #6b716e;
box-shadow: 0 8px 24px rgba(53, 45, 35, 0.07);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
pointer-events: none;
text-align: right;
font-size: 10px;
line-height: 1.45;
}
@media (max-width: 760px) {
.panel {
left: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
h1 {
font-size: 21px;
}
.intro {
margin-bottom: 10px;
}
.legend {
top: 12px;
right: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="panel">
<div class="eyebrow">Interactive hardware study</div>
<h1>Hook spacing study</h1>
<p class="intro">
Select a hook type, then choose an amber mount or drag the piece onto
the board. Both pins must share the same two-unit hole interval.
</p>
<div class="label">Hook type</div>
<div class="buttons">
<button class="type-button" data-type="straight">Short straight</button>
<button class="type-button" data-type="curved">Curved tool</button>
<button class="type-button active" data-type="wide">Wide bracket</button>
</div>
<div class="label">Approved mounts</div>
<div class="buttons">
<button class="mount-button" data-target="A">A<span>lower left</span></button>
<button class="mount-button" data-target="B">B<span>upper center</span></button>
<button class="mount-button" data-target="C">C<span>lower right</span></button>
</div>
<div class="status" id="status" aria-live="polite">
Wide bracket selected. Choose mount A, B, or C.
</div>
</div>
<div class="legend">
<div><span class="swatch"></span> Aligned pin pair</div>
<div><span class="swatch invalid"></span> Half-grid offset / invalid</div>
</div>
<div class="hint">
Drag empty space to orbit · wheel to zoom<br />
Drag a selected hook toward an amber mount · double-click to reset view
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.createElement("canvas");
document.body.insertBefore(canvas, document.querySelector(".panel"));
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5dc);
scene.fog = new THREE.Fog(0xe9e5dc, 14, 28);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraState = {
target: new THREE.Vector3(0.05, 2.15, 0.4),
radius: 9.5,
theta: -0.62,
phi: 1.28
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xfff8e9, 0x81766b, 2.05));
const keyLight = new THREE.DirectionalLight(0xfff3d9, 3.5);
keyLight.position.set(-4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xd9e6ec, 1.35);
fillLight.position.set(6, 4, 2);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffd7a2, 1.05);
rimLight.position.set(1, 5, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and procedural textures
// -------------------------------------------------------------------------
function makeWoodTexture() {
const c = document.createElement("canvas");
c.width = 512;
c.height = 512;
const ctx = c.getContext("2d");
const gradient = ctx.createLinearGradient(0, 0, 512, 0);
gradient.addColorStop(0, "#d8c29e");
gradient.addColorStop(0.46, "#e2cfae");
gradient.addColorStop(1, "#cfb68f");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 512, 512);
for (let i = 0; i < 95; i++) {
const y = Math.random() * 512;
const amplitude = 1 + Math.random() * 7;
const frequency = 0.006 + Math.random() * 0.012;
ctx.beginPath();
for (let x = 0; x <= 512; x += 8) {
const yy =
y +
Math.sin(x * frequency + i * 1.71) * amplitude +
Math.sin(x * frequency * 0.37 + i) * amplitude * 0.45;
if (x === 0) ctx.moveTo(x, yy);
else ctx.lineTo(x, yy);
}
ctx.strokeStyle = `rgba(116, 83, 48, ${0.018 + Math.random() * 0.035})`;
ctx.lineWidth = 0.5 + Math.random() * 1.4;
ctx.stroke();
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2.4, 1.4);
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = makeWoodTexture();
const boardMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xfff9ed,
roughness: 0.77,
metalness: 0.02
});
const baseMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xe4d2b5,
roughness: 0.72
});
const darkWoodMaterial = new THREE.MeshStandardMaterial({
color: 0x6f5b45,
roughness: 0.72
});
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xdcd7cc,
roughness: 0.94,
metalness: 0
});
const holeMaterial = new THREE.MeshBasicMaterial({
color: 0x40352c
});
const holeRimMaterial = new THREE.MeshBasicMaterial({
color: 0xb89b75,
transparent: true,
opacity: 0.44,
depthWrite: false
});
const amberMaterial = new THREE.MeshStandardMaterial({
color: 0xd49a3b,
emissive: 0x7b4810,
emissiveIntensity: 0.25,
roughness: 0.38,
metalness: 0.62
});
const amberBasic = new THREE.MeshBasicMaterial({
color: 0xd39a39,
transparent: true,
opacity: 0.92
});
const redBasic = new THREE.MeshBasicMaterial({
color: 0xb64e42,
transparent: true,
opacity: 0.92
});
// -------------------------------------------------------------------------
// Board, base, and hole grid
// -------------------------------------------------------------------------
const BOARD_X = -0.75;
const BOARD_Y = 2.5;
const BOARD_WIDTH = 5.4;
const BOARD_HEIGHT = 4.4;
const GRID = 0.5;
const FRONT = 0.145;
const boardGroup = new THREE.Group();
boardGroup.position.set(BOARD_X, BOARD_Y, 0);
scene.add(boardGroup);
function roundedRectangleShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
return shape;
}
const boardShape = roundedRectangleShape(BOARD_WIDTH, BOARD_HEIGHT, 0.15);
const boardGeometry = new THREE.ExtrudeGeometry(boardShape, {
depth: 0.22,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.035,
bevelThickness: 0.025,
curveSegments: 8,
steps: 1
});
boardGeometry.translate(0, 0, -0.11);
const board = new THREE.Mesh(boardGeometry, boardMaterial);
board.castShadow = true;
board.receiveShadow = true;
boardGroup.add(board);
const base = new THREE.Mesh(
new THREE.BoxGeometry(5.9, 0.24, 1.28),
baseMaterial
);
base.position.set(BOARD_X, 0.12, -0.18);
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const boardRiser = new THREE.Mesh(
new THREE.BoxGeometry(4.9, 0.14, 0.38),
darkWoodMaterial
);
boardRiser.position.set(BOARD_X, 0.29, -0.03);
boardRiser.castShadow = true;
boardRiser.receiveShadow = true;
scene.add(boardRiser);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.005;
floor.receiveShadow = true;
scene.add(floor);
const holePositions = [];
const holeSet = new Set();
for (let x = -2; x <= 2.001; x += GRID) {
for (let y = -2; y <= 2.001; y += GRID) {
holePositions.push(new THREE.Vector3(x, y, 0));
holeSet.add(`${x.toFixed(2)},${y.toFixed(2)}`);
}
}
const holeRims = new THREE.InstancedMesh(
new THREE.CircleGeometry(0.052, 16),
holeRimMaterial,
holePositions.length
);
const holes = new THREE.InstancedMesh(
new THREE.CircleGeometry(0.034, 16),
holeMaterial,
holePositions.length
);
const dummy = new THREE.Object3D();
holePositions.forEach((position, index) => {
dummy.position.set(position.x, position.y, 0.141);
dummy.updateMatrix();
holeRims.setMatrixAt(index, dummy.matrix);
dummy.position.set(position.x, position.y, 0.146);
dummy.updateMatrix();
holes.setMatrixAt(index, dummy.matrix);
});
holeRims.renderOrder = 1;
holes.renderOrder = 2;
boardGroup.add(holeRims, holes);
function isHole(x, y) {
return holeSet.has(`${x.toFixed(2)},${y.toFixed(2)}`);
}
// -------------------------------------------------------------------------
// Text and badge helpers
// -------------------------------------------------------------------------
function makeTextPlane(lines, width, height, options = {}) {
const c = document.createElement("canvas");
c.width = 1024;
c.height = 256;
const ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
if (options.background) {
ctx.fillStyle = options.background;
ctx.fillRect(0, 0, c.width, c.height);
}
if (options.border) {
ctx.strokeStyle = options.border;
ctx.lineWidth = 5;
ctx.strokeRect(8, 8, c.width - 16, c.height - 16);
}
const primary = options.primary || "#34403e";
const secondary = options.secondary || "#7b7b70";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = primary;
ctx.font = `700 ${lines[0].size || 54}px system-ui, sans-serif`;
ctx.fillText(lines[0].text, 512, lines[0].y || 98);
if (lines[1]) {
ctx.fillStyle = secondary;
ctx.font = `600 ${lines[1].size || 27}px system-ui, sans-serif`;
ctx.fillText(lines[1].text, 512, lines[1].y || 166);
}
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
material
);
return mesh;
}
function makeBadgeSprite(text, options = {}) {
const c = document.createElement("canvas");
c.width = 512;
c.height = 160;
const ctx = c.getContext("2d");
const radius = 28;
ctx.beginPath();
ctx.moveTo(radius, 8);
ctx.lineTo(c.width - radius, 8);
ctx.quadraticCurveTo(c.width - 8, 8, c.width - 8, radius);
ctx.lineTo(c.width - 8, c.height - radius);
ctx.quadraticCurveTo(
c.width - 8,
c.height - 8,
c.width - radius,
c.height - 8
);
ctx.lineTo(radius, c.height - 8);
ctx.quadraticCurveTo(8, c.height - 8, 8, c.height - radius);
ctx.lineTo(8, radius);
ctx.quadraticCurveTo(8, 8, radius, 8);
ctx.closePath();
ctx.fillStyle = options.background || "rgba(248,245,236,0.94)";
ctx.fill();
if (options.border) {
ctx.strokeStyle = options.border;
ctx.lineWidth = 5;
ctx.stroke();
}
ctx.fillStyle = options.color || "#46504d";
ctx.font = `750 ${options.fontSize || 42}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, c.width / 2, c.height / 2 + 1);
const texture = new THREE.CanvasTexture(c);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(options.width || 1.35, options.height || 0.42, 1);
return sprite;
}
const boardTitle = makeTextPlane(
[
{
text: "HOOK SPACING STUDY",
size: 54,
y: 96
},
{
text: "50 mm GRID / TWO-UNIT PIN PITCH",
size: 27,
y: 165
}
],
3.4,
0.56
);
boardTitle.position.set(0, 2.04, 0.162);
boardGroup.add(boardTitle);
// -------------------------------------------------------------------------
// Valid mounts
// -------------------------------------------------------------------------
const targetDefinitions = [
{ id: "A", local: new THREE.Vector2(-1.5, -1.5) },
{ id: "B", local: new THREE.Vector2(0, 0.5) },
{ id: "C", local: new THREE.Vector2(1.5, -1.5) }
];
const targets = [];
const targetMeshes = [];
const invisibleHitMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
});
targetDefinitions.forEach((definition) => {
const target = {
id: definition.id,
local: definition.local,
rings: [],
world: new THREE.Vector3()
};
const ringGeometry = new THREE.TorusGeometry(0.075, 0.012, 8, 28);
[-0.5, 0.5].forEach((pinOffset) => {
const ring = new THREE.Mesh(ringGeometry, amberBasic);
ring.position.set(
definition.local.x + pinOffset,
definition.local.y,
0.158
);
boardGroup.add(ring);
target.rings.push(ring);
});
const halo = new THREE.Mesh(
new THREE.CircleGeometry(0.18, 32),
new THREE.MeshBasicMaterial({
color: 0xd39a39,
transparent: true,
opacity: 0.07,
depthWrite: false
})
);
halo.position.set(definition.local.x, definition.local.y, 0.151);
boardGroup.add(halo);
const hit = new THREE.Mesh(
new THREE.CircleGeometry(0.27, 24),
invisibleHitMaterial
);
hit.position.set(definition.local.x, definition.local.y, 0.162);
hit.userData.targetId = definition.id;
boardGroup.add(hit);
targetMeshes.push(hit);
targets.push(target);
});
boardGroup.updateMatrixWorld(true);
targets.forEach((target) => {
target.world.set(
target.local.x,
target.local.y,
FRONT + 0.003
);
boardGroup.localToWorld(target.world);
});
function targetById(id) {
return targets.find((target) => target.id === id);
}
targets.forEach((target) => {
const label = makeBadgeSprite(target.id, {
width: 0.34,
height: 0.34,
fontSize: 66,
background: "rgba(216, 160, 67, 0.96)",
color: "#fffaf0",
border: "rgba(255,255,255,0.65)"
});
label.position.set(
target.local.x,
target.local.y + 0.54,
0.205
);
boardGroup.add(label);
});
// -------------------------------------------------------------------------
// Obvious invalid, half-grid-offset outline
// -------------------------------------------------------------------------
const invalidGroup = new THREE.Group();
invalidGroup.position.set(0.25, -1.0, 0.165);
boardGroup.add(invalidGroup);
const invalidCurvePoints = [
new THREE.Vector3(-0.62, 0, 0.4),
new THREE.Vector3(-0.72, -0.08, 0.66),
new THREE.Vector3(-0.58, -0.22, 0.87),
new THREE.Vector3(0, -0.32, 0.97),
new THREE.Vector3(0.58, -0.22, 0.87),
new THREE.Vector3(0.72, -0.08, 0.66),
new THREE.Vector3(0.62, 0, 0.4)
];
const invalidCurve = new THREE.CatmullRomCurve3(invalidCurvePoints);
const invalidGeometry = new THREE.BufferGeometry().setFromPoints(
invalidCurve.getPoints(72)
);
const invalidLine = new THREE.Line(
invalidGeometry,
new THREE.LineDashedMaterial({
color: 0xb64e42,
dashSize: 0.08,
gapSize: 0.055,
transparent: true,
opacity: 0.92,
depthWrite: false
})
);
invalidLine.computeLineDistances();
invalidGroup.add(invalidLine);
const invalidBar = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-0.65, 0, 0.4),
new THREE.Vector3(0.65, 0, 0.4)
]),
new THREE.LineDashedMaterial({
color: 0xb64e42,
dashSize: 0.07,
gapSize: 0.05,
transparent: true,
opacity: 0.9
})
);
invalidBar.computeLineDistances();
invalidGroup.add(invalidBar);
[-0.5, 0.5].forEach((x) => {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.079, 0.012, 8, 24),
redBasic
);
ring.position.set(x, 0, 0.02);
invalidGroup.add(ring);
const crossGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(x - 0.045, -0.045, 0.025),
new THREE.Vector3(x + 0.045, 0.045, 0.025),
new THREE.Vector3(x - 0.045, 0.045, 0.025),
new THREE.Vector3(x + 0.045, -0.045, 0.025)
]);
const cross = new THREE.LineSegments(
crossGeometry,
new THREE.LineBasicMaterial({
color: 0xb64e42,
transparent: true,
opacity: 0.95
})
);
invalidGroup.add(cross);
});
const invalidLabel = makeBadgeSprite("OFFSET 0.25 — INVALID", {
width: 1.75,
height: 0.35,
fontSize: 33,
background: "rgba(248,235,229,0.95)",
border: "rgba(182,78,66,0.65)",
color: "#9e4037"
});
invalidLabel.position.set(0.25, -0.42, 0.23);
boardGroup.add(invalidLabel);
// -------------------------------------------------------------------------
// Hook geometry
// -------------------------------------------------------------------------
function horizontalBar(length, radius, material, z = 0.43) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 16),
material
);
mesh.rotation.z = Math.PI / 2;
mesh.position.z = z;
mesh.castShadow = true;
return mesh;
}
function makeHook(type, interactive = true) {
const group = new THREE.Group();
const colors = {
straight: 0x343c3d,
curved: 0x414846,
wide: 0x252c2d
};
const bodyMaterial = new THREE.MeshStandardMaterial({
color: colors[type],
roughness: 0.36,
metalness: 0.68,
emissive: 0x000000,
emissiveIntensity: 0.3
});
const pinMaterial = new THREE.MeshStandardMaterial({
color: 0x737b7b,
roughness: 0.28,
metalness: 0.82
});
const pinGeometry = new THREE.CylinderGeometry(
0.043,
0.043,
0.34,
14
);
const washerGeometry = new THREE.CylinderGeometry(
0.075,
0.075,
0.035,
18
);
[-0.5, 0.5].forEach((x) => {
const pin = new THREE.Mesh(pinGeometry, pinMaterial);
pin.rotation.x = Math.PI / 2;
pin.position.set(x, 0, 0.17);
pin.castShadow = true;
group.add(pin);
const washer = new THREE.Mesh(washerGeometry, pinMaterial);
washer.rotation.x = Math.PI / 2;
washer.position.set(x, 0, 0.017);
washer.castShadow = true;
group.add(washer);
const connector = new THREE.Mesh(
new THREE.CylinderGeometry(0.059, 0.059, 0.12, 14),
bodyMaterial
);
connector.rotation.x = Math.PI / 2;
connector.position.set(x, 0, 0.39);
connector.castShadow = true;
group.add(connector);
});
const bar = horizontalBar(1.3, 0.066, bodyMaterial);
group.add(bar);
if (type === "straight") {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0.43),
new THREE.Vector3(0, 0, 0.69),
new THREE.Vector3(0, 0.07, 0.94),
new THREE.Vector3(0, 0.2, 1.02)
]);
const hook = new THREE.Mesh(
new THREE.TubeGeometry(curve, 32, 0.07, 12, false),
bodyMaterial
);
hook.castShadow = true;
group.add(hook);
const tip = new THREE.Mesh(
new THREE.SphereGeometry(0.071, 16, 12),
bodyMaterial
);
tip.position.set(0, 0.2, 1.02);
tip.castShadow = true;
group.add(tip);
}
if (type === "curved") {
const curve = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0.43),
new THREE.Vector3(0, -0.08, 0.64),
new THREE.Vector3(0, -0.28, 0.79),
new THREE.Vector3(0, -0.39, 0.98),
new THREE.Vector3(0, -0.22, 1.12),
new THREE.Vector3(0, 0.02, 1.06),
new THREE.Vector3(0, 0.08, 0.87)
]);
const hook = new THREE.Mesh(
new THREE.TubeGeometry(curve, 52, 0.068, 12, false),
bodyMaterial
);
hook.castShadow = true;
group.add(hook);
const tip = new THREE.Mesh(
new THREE.SphereGeometry(0.069, 16, 12),
bodyMaterial
);
tip.position.set(0, 0.08, 0.87);
tip.castShadow = true;
group.add(tip);
}
if (type === "wide") {
const bracketCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-0.65, 0, 0.43),
new THREE.Vector3(-0.78, -0.05, 0.68),
new THREE.Vector3(-0.7, -0.16, 0.92),
new THREE.Vector3(0, -0.2, 0.99),
new THREE.Vector3(0.7, -0.16, 0.92),
new THREE.Vector3(0.78, -0.05, 0.68),
new THREE.Vector3(0.65, 0, 0.43)
]);
const cradle = new THREE.Mesh(
new THREE.TubeGeometry(bracketCurve, 64, 0.072, 12, false),
bodyMaterial
);
cradle.castShadow = true;
group.add(cradle);
const frontLip = horizontalBar(1.24, 0.066, bodyMaterial, 0.98);
frontLip.position.y = -0.19;
group.add(frontLip);
}
const selectionRing = new THREE.Mesh(
new THREE.TorusGeometry(0.7, 0.011, 8, 64),
new THREE.MeshBasicMaterial({
color: 0xd59b3a,
transparent: true,
opacity: 0.64,
depthWrite: false
})
);
selectionRing.position.z = 0.13;
selectionRing.visible = false;
group.add(selectionRing);
if (interactive) {
group.userData.hookType = type;
}
return {
group,
bodyMaterial,
pinMaterial,
selectionRing
};
}
// -------------------------------------------------------------------------
// Type library beside the board
// -------------------------------------------------------------------------
const rackX = 3.32;
const rackLevels = {
straight: 3.48,
curved: 2.28,
wide: 1.08
};
const rackBase = new THREE.Mesh(
new THREE.BoxGeometry(1.65, 0.2, 1.15),
baseMaterial
);
rackBase.position.set(rackX, 0.1, -0.05);
rackBase.castShadow = true;
rackBase.receiveShadow = true;
scene.add(rackBase);
const rackPost = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 3.9, 0.16),
darkWoodMaterial
);
rackPost.position.set(rackX, 2.08, -0.16);
rackPost.castShadow = true;
rackPost.receiveShadow = true;
scene.add(rackPost);
const typeNames = {
straight: "01 SHORT STRAIGHT",
curved: "02 CURVED TOOL",
wide: "03 WIDE BRACKET"
};
const hooks = {};
const homePositions = {};
const hookTargets = {
straight: null,
curved: null,
wide: null
};
Object.keys(rackLevels).forEach((type) => {
const level = rackLevels[type];
const hook = makeHook(type, true);
hook.group.position.set(rackX, level, 0.18);
hook.group.rotation.y = 0.1;
hook.group.scale.setScalar(0.82);
scene.add(hook.group);
hooks[type] = hook;
homePositions[type] = hook.group.position.clone();
const label = makeBadgeSprite(typeNames[type], {
width: 1.45,
height: 0.35,
fontSize: 31,
background: "rgba(245,241,231,0.96)",
border: "rgba(97,87,72,0.18)",
color: "#525b57"
});
label.position.set(rackX, level - 0.52, 0.43);
scene.add(label);
const shelf = new THREE.Mesh(
new THREE.BoxGeometry(1.35, 0.1, 0.42),
baseMaterial
);
shelf.position.set(rackX, level - 0.38, -0.02);
shelf.castShadow = true;
shelf.receiveShadow = true;
scene.add(shelf);
});
const rackHeader = makeBadgeSprite("TYPE LIBRARY", {
width: 1.45,
height: 0.38,
fontSize: 38,
background: "rgba(64,73,69,0.95)",
color: "#f8f4e9"
});
rackHeader.position.set(rackX, 4.22, 0.25);
scene.add(rackHeader);
// Installed reference: a wide bracket with a clearly visible stand-off gap.
const referenceHook = makeHook("wide", false);
referenceHook.group.position.copy(
boardGroup.localToWorld(new THREE.Vector3(1.5, 1.5, FRONT))
);
referenceHook.group.rotation.y = 0;
scene.add(referenceHook.group);
const standOffLabel = makeBadgeSprite("8 mm STAND-OFF", {
width: 1.15,
height: 0.29,
fontSize: 28,
background: "rgba(245,241,231,0.88)",
border: "rgba(67,74,70,0.16)",
color: "#666d69"
});
standOffLabel.position.set(
BOARD_X + 1.5,
BOARD_Y + 0.9,
FRONT + 0.18
);
scene.add(standOffLabel);
// -------------------------------------------------------------------------
// Selection and movement
// -------------------------------------------------------------------------
let selectedType = "wide";
const statusElement = document.getElementById("status");
const typeButtons = [...document.querySelectorAll(".type-button")];
const targetButtons = [...document.querySelectorAll(".mount-button")];
function setStatus(message, good = false) {
statusElement.textContent = message;
statusElement.classList.toggle("good", good);
}
function selectHook(type, announce = true) {
selectedType = type;
Object.entries(hooks).forEach(([key, hook]) => {
const selected = key === type;
hook.selectionRing.visible = selected;
hook.bodyMaterial.emissive.setHex(selected ? 0x684116 : 0x000000);
hook.bodyMaterial.emissiveIntensity = selected ? 0.38 : 0;
});
typeButtons.forEach((button) => {
button.classList.toggle("active", button.dataset.type === type);
});
if (announce) {
setStatus(
`${typeNames[type].replace(/^\d+\s+/, "")} selected. Choose mount A, B, or C.`
);
}
}
typeButtons.forEach((button) => {
button.addEventListener("click", () => {
selectHook(button.dataset.type);
});
});
const tweens = [];
function tweenMove(
group,
destination,
endScale = 1,
endRotationY = 0,
duration = 650,
onComplete = null
) {
tweens.push({
group,
from: group.position.clone(),
to: destination.clone(),
fromScale: group.scale.x,
toScale: endScale,
fromRotation: group.rotation.y,
toRotation: endRotationY,
start: performance.now(),
duration,
onComplete
});
}
function updateTweens(now) {
for (let i = tweens.length - 1; i >= 0; i--) {
const tween = tweens[i];
const t = Math.min(1, (now - tween.start) / tween.duration);
const eased =
t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
tween.group.position.lerpVectors(tween.from, tween.to, eased);
const scale = THREE.MathUtils.lerp(
tween.fromScale,
tween.toScale,
eased
);
tween.group.scale.setScalar(scale);
tween.group.rotation.y = THREE.MathUtils.lerp(
tween.fromRotation,
tween.toRotation,
eased
);
if (t >= 1) {
tweens.splice(i, 1);
if (tween.onComplete) tween.onComplete();
}
}
}
function freeTarget(targetId, exceptType) {
Object.entries(hookTargets).forEach(([type, occupiedId]) => {
if (
occupiedId === targetId &&
type !== exceptType &&
occupiedId !== null
) {
hookTargets[type] = null;
const hook = hooks[type];
tweenMove(
hook.group,
homePositions[type],
0.82,
0.1,
520
);
setStatus(
`${typeNames[type].replace(/^\d+\s+/, "")} returned to the type library.`,
false
);
}
});
}
function moveToTarget(type, targetId) {
const target = targetById(targetId);
if (!target) return;
freeTarget(targetId, type);
const hook = hooks[type];
hookTargets[type] = targetId;
tweenMove(
hook.group,
target.world,
1,
0,
700,
() => {
setStatus(
`${typeNames[type].replace(/^\d+\s+/, "")} attached at ${targetId}. Both pins align with holes.`,
true
);
}
);
targetButtons.forEach((button) => {
button.classList.toggle(
"active",
button.dataset.target === targetId &&
hookTargets[selectedType] === targetId
);
});
}
targetButtons.forEach((button) => {
button.addEventListener("click", () => {
moveToTarget(selectedType, button.dataset.target);
});
});
selectHook(selectedType, false);
// -------------------------------------------------------------------------
// Pointer picking, dragging, and custom orbit controls
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const boardPlane = new THREE.Plane(
new THREE.Vector3(0, 0, 1),
-(FRONT + 0.015)
);
const planeIntersection = new THREE.Vector3();
let dragState = null;
let orbitState = null;
function setPointer(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;
raycaster.setFromCamera(pointer, camera);
}
function findHookType(object) {
let current = object;
while (current) {
if (current.userData.hookType) {
return current.userData.hookType;
}
current = current.parent;
}
return null;
}
function pickHook(event) {
setPointer(event);
const intersections = raycaster.intersectObjects(
Object.values(hooks).map((hook) => hook.group),
true
);
for (const intersection of intersections) {
const type = findHookType(intersection.object);
if (type) return type;
}
return null;
}
function pickTarget(event) {
setPointer(event);
const intersections = raycaster.intersectObjects(targetMeshes, false);
return intersections.length ? intersections[0].object.userData.targetId : null;
}
function pointerOnBoard(event) {
setPointer(event);
const hit = raycaster.ray.intersectPlane(boardPlane, planeIntersection);
if (!hit) return null;
boardGroup.updateMatrixWorld(true);
return boardGroup.worldToLocal(hit.clone());
}
function nearestTarget(localPoint) {
let nearest = null;
let nearestDistance = Infinity;
targets.forEach((target) => {
const distance = localPoint.distanceTo(target.local);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = target;
}
});
return nearestDistance <= 0.55 ? nearest : null;
}
function targetHasAlignedPins(target) {
return (
isHole(target.local.x - 0.5, target.local.y) &&
isHole(target.local.x + 0.5, target.local.y)
);
}
function targetOccupiedByAnother(targetId, type) {
return Object.entries(hookTargets).some(
([otherType, occupiedId]) =>
otherType !== type && occupiedId === targetId
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
const type = pickHook(event);
const targetId = pickTarget(event);
if (targetId) {
moveToTarget(selectedType, targetId);
return;
}
if (type) {
selectHook(type);
const hook = hooks[type];
dragState = {
type,
pointerId: event.pointerId,
origin: hook.group.position.clone(),
originScale: hook.group.scale.x,
originRotation: hook.group.rotation.y,
candidate: null
};
canvas.style.cursor = "grabbing";
return;
}
orbitState = {
pointerId: event.pointerId,
x: event.clientX,
y: event.clientY,
theta: cameraState.theta,
phi: cameraState.phi
};
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (orbitState && event.pointerId === orbitState.pointerId) {
const dx = event.clientX - orbitState.x;
const dy = event.clientY - orbitState.y;
cameraState.theta = orbitState.theta - dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
orbitState.phi - dy * 0.005,
0.82,
1.49
);
updateCamera();
return;
}
if (dragState && event.pointerId === dragState.pointerId) {
const localPoint = pointerOnBoard(event);
if (!localPoint) return;
const candidate = nearestTarget(localPoint);
const hook = hooks[dragState.type];
if (
candidate &&
targetHasAlignedPins(candidate) &&
!targetOccupiedByAnother(candidate.id, dragState.type)
) {
dragState.candidate = candidate;
hook.group.position.copy(candidate.world);
hook.group.scale.setScalar(1);
hook.group.rotation.y = 0;
setStatus(
`Pins aligned at ${candidate.id}. Release to attach.`,
true
);
} else {
dragState.candidate = null;
if (targetOccupiedByAnother(candidate?.id, dragState.type)) {
setStatus(`Mount ${candidate.id} is occupied.`, false);
} else {
setStatus(
"Move closer to an amber mount. The hook only attaches when both pins align.",
false
);
}
}
return;
}
const type = pickHook(event);
const targetId = pickTarget(event);
canvas.style.cursor = type || targetId ? "pointer" : "grab";
});
function finishPointer(event) {
if (dragState && event.pointerId === dragState.pointerId) {
const hook = hooks[dragState.type];
if (dragState.candidate) {
const candidate = dragState.candidate;
hookTargets[dragState.type] = candidate.id;
setStatus(
`${typeNames[dragState.type].replace(/^\d+\s+/, "")} attached at ${candidate.id}. Both pins align with holes.`,
true
);
targetButtons.forEach((button) => {
button.classList.toggle(
"active",
button.dataset.target === candidate.id &&
hookTargets[selectedType] === candidate.id
);
});
} else {
tweenMove(
hook.group,
dragState.origin,
dragState.originScale,
dragState.originRotation,
420
);
}
dragState = null;
}
if (orbitState && event.pointerId === orbitState.pointerId) {
orbitState = null;
}
canvas.style.cursor = "grab";
}
canvas.addEventListener("pointerup", finishPointer);
canvas.addEventListener("pointercancel", finishPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.006,
6.2,
13.5
);
updateCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.radius = 9.5;
cameraState.theta = -0.62;
cameraState.phi = 1.28;
cameraState.target.set(0.05, 2.15, 0.4);
updateCamera();
}
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "1") selectHook("straight");
if (event.key === "2") selectHook("curved");
if (event.key === "3") selectHook("wide");
if (event.key.toLowerCase() === "a") moveToTarget(selectedType, "A");
if (event.key.toLowerCase() === "b") moveToTarget(selectedType, "B");
if (event.key.toLowerCase() === "c") moveToTarget(selectedType, "C");
});
// -------------------------------------------------------------------------
// Responsive rendering and animation
// -------------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
function animate(now) {
requestAnimationFrame(animate);
updateTweens(now);
const pulse = 1 + Math.sin(now * 0.0028) * 0.045;
targets.forEach((target) => {
target.rings.forEach((ring) => {
ring.scale.setScalar(pulse);
});
});
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```