Prompt specification
Build or customize the Three.js 3D scene "Hook placement compatibility" 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: Hook placement compatibility
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/hook-placement-compatibility-x0210
Scene Description Prompt:
Hook placement compatibility. Let users move hooks among highlighted pegboard holes; valid placements snap into alignment, while incompatible spacing remains visibly offset from the receiving holes. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (hook-placement-compatibility-x0210):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hook Placement Lab</title>
<style>
:root {
--ink: #18312c;
--muted: #667873;
--paper: rgba(248, 250, 246, 0.92);
--line: rgba(24, 49, 44, 0.13);
--green: #238c76;
--red: #e85d52;
--amber: #ffd166;
--violet: #a78bfa;
--coral: #ff8a65;
--teal: #64e6d2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce2de;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
body {
user-select: none;
}
#stage {
position: fixed;
inset: 0;
}
#stage::after {
content: "";
position: absolute;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 46%,
transparent 38%,
rgba(27, 48, 42, 0.08) 100%
);
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 10;
top: 22px;
left: 24px;
right: 24px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 42px;
height: 42px;
display: grid;
place-items: center;
border: 1px solid rgba(24, 49, 44, 0.16);
border-radius: 13px;
background: rgba(248, 250, 246, 0.85);
box-shadow: 0 8px 28px rgba(37, 57, 50, 0.08);
backdrop-filter: blur(12px);
}
.brand-mark svg {
width: 24px;
height: 24px;
}
.eyebrow {
margin-bottom: 3px;
color: var(--green);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.4vw, 34px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 7px 0 0;
color: var(--muted);
font-size: 13px;
}
.toolbar {
display: flex;
gap: 9px;
pointer-events: auto;
}
button {
color: inherit;
font: inherit;
}
.tool-button {
height: 42px;
padding: 0 16px;
display: inline-flex;
align-items: center;
gap: 9px;
border: 1px solid rgba(24, 49, 44, 0.16);
border-radius: 12px;
background: rgba(248, 250, 246, 0.88);
box-shadow: 0 8px 24px rgba(37, 57, 50, 0.08);
backdrop-filter: blur(12px);
cursor: pointer;
transition:
transform 160ms ease,
background 160ms ease;
}
.tool-button:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.98);
}
.tool-button:active {
transform: translateY(1px);
}
.tool-button svg {
width: 16px;
height: 16px;
}
.panel {
position: fixed;
z-index: 10;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--paper);
box-shadow: 0 18px 60px rgba(37, 57, 50, 0.11);
backdrop-filter: blur(16px);
}
.intro-panel {
left: 24px;
bottom: 24px;
width: 285px;
padding: 18px;
}
.panel-kicker {
margin-bottom: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.step {
display: grid;
grid-template-columns: 27px 1fr;
gap: 10px;
margin-top: 12px;
}
.step-number {
width: 25px;
height: 25px;
display: grid;
place-items: center;
border-radius: 8px;
background: #e5eee9;
color: var(--green);
font-size: 11px;
font-weight: 800;
}
.step strong {
display: block;
margin-bottom: 3px;
font-size: 13px;
}
.step span:last-child {
display: block;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.compat-note {
margin-top: 16px;
padding-top: 14px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.compat-note strong {
color: var(--ink);
}
.spec-panel {
top: 104px;
right: 24px;
width: 264px;
padding: 16px;
}
.hook-list {
display: grid;
gap: 7px;
}
.hook-card {
width: 100%;
padding: 10px;
display: grid;
grid-template-columns: 30px 1fr auto;
align-items: center;
gap: 10px;
border: 1px solid transparent;
border-radius: 12px;
background: rgba(232, 238, 233, 0.65);
text-align: left;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease,
transform 160ms ease;
}
.hook-card:hover {
transform: translateX(-2px);
background: rgba(240, 245, 240, 0.95);
}
.hook-card.active {
border-color: rgba(35, 140, 118, 0.38);
background: rgba(255, 255, 255, 0.96);
}
.hook-swatch {
width: 26px;
height: 26px;
display: grid;
place-items: center;
border-radius: 8px;
color: #17332d;
font-size: 10px;
font-weight: 900;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}
.hook-name {
display: block;
font-size: 12px;
font-weight: 750;
}
.hook-meta {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
}
.pitch-badge {
padding: 4px 6px;
border-radius: 6px;
background: rgba(24, 49, 44, 0.07);
color: var(--muted);
font-size: 9px;
font-weight: 800;
}
.status {
position: fixed;
z-index: 12;
left: 50%;
bottom: 24px;
width: min(460px, calc(100vw - 650px));
min-width: 310px;
padding: 12px 16px;
display: grid;
grid-template-columns: 11px 1fr;
align-items: center;
gap: 11px;
transform: translateX(-50%);
border: 1px solid rgba(24, 49, 44, 0.13);
border-radius: 14px;
background: rgba(248, 250, 246, 0.93);
box-shadow: 0 12px 40px rgba(31, 51, 44, 0.11);
backdrop-filter: blur(14px);
pointer-events: none;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: #82908b;
box-shadow: 0 0 0 4px rgba(130, 144, 139, 0.12);
}
.status[data-tone="valid"] .status-dot {
background: #36ad83;
box-shadow: 0 0 0 4px rgba(54, 173, 131, 0.14);
}
.status[data-tone="invalid"] .status-dot {
background: var(--red);
box-shadow: 0 0 0 4px rgba(232, 93, 82, 0.14);
}
.status-title {
font-size: 12px;
font-weight: 800;
}
.status-detail {
margin-top: 2px;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
@media (max-width: 1050px) {
.intro-panel {
width: 245px;
}
.spec-panel {
width: 235px;
}
.status {
left: 285px;
right: 285px;
width: auto;
min-width: 0;
transform: none;
}
}
@media (max-width: 760px) {
.topbar {
top: 14px;
left: 14px;
right: 14px;
}
.subtitle,
.intro-panel,
.spec-panel {
display: none;
}
.brand-mark {
width: 38px;
height: 38px;
}
h1 {
font-size: 23px;
}
.tool-button {
height: 38px;
padding: 0 12px;
}
.tool-button span {
display: none;
}
.status {
left: 12px;
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive pegboard hook placement demo"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path d="M6 3v8.2a5 5 0 0 0 10 0V8" stroke="#238c76" stroke-width="2.2" stroke-linecap="round"/>
<path d="M6 6h7" stroke="#18312c" stroke-width="2.2" stroke-linecap="round"/>
<circle cx="16" cy="17" r="2" fill="#64e6d2"/>
</svg>
</div>
<div>
<div class="eyebrow">Workshop / Fit Test</div>
<h1>Hook placement lab</h1>
<p class="subtitle">Match each hook’s pitch to the illuminated receiver holes.</p>
</div>
</div>
<div class="toolbar">
<button class="tool-button" id="resetButton" type="button" title="Reset layout">
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path d="M4 9a8 8 0 1 1-.4 5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<path d="M4 4v5h5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>
<span>Reset layout</span>
</button>
</div>
</header>
<aside class="panel intro-panel">
<div class="panel-kicker">How to test a fit</div>
<div class="step">
<div class="step-number">01</div>
<div>
<strong>Pick up a hook</strong>
<span>Drag any low-poly hook directly on the pegboard.</span>
</div>
</div>
<div class="step">
<div class="step-number">02</div>
<div>
<strong>Aim for a lit hole</strong>
<span>Colored receiver holes show the available hole families.</span>
</div>
</div>
<div class="step">
<div class="step-number">03</div>
<div>
<strong>Check the alignment</strong>
<span>Matching pitch snaps exactly; mismatched pitch stays offset.</span>
</div>
</div>
<div class="compat-note">
<strong>Compatibility rule:</strong>
a hook fits only when its mounting-hole spacing matches the receiving pair.
A one-hole hook uses the teal sockets.
</div>
</aside>
<aside class="panel spec-panel">
<div class="panel-kicker">Hook specifications</div>
<div class="hook-list" id="hookList"></div>
</aside>
<div class="status" id="status" data-tone="neutral" aria-live="polite">
<div class="status-dot"></div>
<div>
<div class="status-title" id="statusTitle">Ready for a test fit</div>
<div class="status-detail" id="statusDetail">Drag a hook over one of the illuminated receiver holes.</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const stage = document.querySelector("#stage");
const statusElement = document.querySelector("#status");
const statusTitle = document.querySelector("#statusTitle");
const statusDetail = document.querySelector("#statusDetail");
const hookListElement = document.querySelector("#hookList");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce2de);
scene.fog = new THREE.Fog(0xdce2de, 28, 60);
const camera = new THREE.PerspectiveCamera(35, 1, 0.1, 100);
camera.position.set(0, 0, 18);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
stage.appendChild(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x65736c, 2.1);
scene.add(hemiLight);
const keyLight = new THREE.DirectionalLight(0xfff1d2, 3.0);
keyLight.position.set(-6, 8, 13);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xccecff, 1.15);
fillLight.position.set(7, -2, 9);
scene.add(fillLight);
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(100, 70),
new THREE.MeshStandardMaterial({
color: 0xd4dcd7,
roughness: 1,
metalness: 0
})
);
wall.position.z = -0.72;
wall.receiveShadow = true;
scene.add(wall);
// -------------------------------------------------------------------------
// Pegboard and grid
// -------------------------------------------------------------------------
const GRID = 1;
const COLS = 13;
const ROWS = 8;
const BOARD_W = 13.85;
const BOARD_H = 8.65;
const boardGroup = new THREE.Group();
scene.add(boardGroup);
const boardMaterial = new THREE.MeshStandardMaterial({
color: 0xb9aa8d,
roughness: 0.88,
metalness: 0.04
});
const board = new THREE.Mesh(
new THREE.BoxGeometry(BOARD_W, BOARD_H, 0.42),
boardMaterial
);
board.position.z = -0.23;
board.castShadow = true;
board.receiveShadow = true;
boardGroup.add(board);
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x263c36,
roughness: 0.58,
metalness: 0.18
});
const frameDepth = 0.24;
const frameThickness = 0.18;
const framePieces = [
[BOARD_W + 0.35, frameThickness, 0, BOARD_H / 2 + 0.08],
[BOARD_W + 0.35, frameThickness, 0, -BOARD_H / 2 - 0.08],
[frameThickness, BOARD_H + 0.35, BOARD_W / 2 + 0.08, 0],
[frameThickness, BOARD_H + 0.35, -BOARD_W / 2 - 0.08, 0]
];
for (const [width, height, x, y] of framePieces) {
const piece = new THREE.Mesh(
new THREE.BoxGeometry(width, height, frameDepth),
frameMaterial
);
piece.position.set(x, y, 0.035);
piece.castShadow = true;
piece.receiveShadow = true;
boardGroup.add(piece);
}
const holeGeometry = new THREE.CylinderGeometry(0.068, 0.075, 0.08, 8);
holeGeometry.rotateX(Math.PI / 2);
const holeMaterial = new THREE.MeshStandardMaterial({
color: 0x514d42,
roughness: 1,
metalness: 0
});
function gridPosition(column, row) {
return new THREE.Vector3(
(column - (COLS - 1) / 2) * GRID,
(row - (ROWS - 1) / 2) * GRID,
0
);
}
for (let row = 0; row < ROWS; row++) {
for (let column = 0; column < COLS; column++) {
const position = gridPosition(column, row);
const hole = new THREE.Mesh(holeGeometry, holeMaterial);
hole.position.set(position.x, position.y, -0.005);
hole.receiveShadow = true;
boardGroup.add(hole);
}
}
const boltGeometry = new THREE.CylinderGeometry(0.12, 0.12, 0.07, 8);
boltGeometry.rotateX(Math.PI / 2);
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0x87958f,
metalness: 0.72,
roughness: 0.3
});
for (const x of [-BOARD_W / 2, BOARD_W / 2]) {
for (const y of [-BOARD_H / 2, BOARD_H / 2]) {
const bolt = new THREE.Mesh(boltGeometry, boltMaterial);
bolt.position.set(x, y, 0.16);
boardGroup.add(bolt);
}
}
// -------------------------------------------------------------------------
// Hook families and receiver slots
// -------------------------------------------------------------------------
const HOOK_TYPES = {
single: {
name: "Key clip",
description: "Single-hole clip",
pitch: 1,
color: 0x64e6d2,
css: "#64e6d2"
},
double: {
name: "Towel peg",
description: "Paired utility peg",
pitch: 2,
color: 0xffd166,
css: "#ffd166"
},
rail: {
name: "Rail hook",
description: "Three-hole rail",
pitch: 3,
color: 0xa78bfa,
css: "#a78bfa"
},
wide: {
name: "Shelf arm",
description: "Four-hole shelf bracket",
pitch: 4,
color: 0xff8a65,
css: "#ff8a65"
}
};
/*
Each highlighted hole belongs to a slot. A hook is compatible only
when its mounting pitch and receiver slot type agree.
*/
const slotDefinitions = [
{ type: "single", anchors: [[2, 4]] },
{ type: "single", anchors: [[6, 4]] },
{ type: "single", anchors: [[10, 4]] },
{ type: "double", anchors: [[1, 5], [3, 5]] },
{ type: "double", anchors: [[6, 5], [8, 5]] },
{ type: "double", anchors: [[10, 5], [12, 5]] },
{ type: "rail", anchors: [[1, 3], [4, 3]] },
{ type: "rail", anchors: [[8, 3], [11, 3]] },
{ type: "wide", anchors: [[1, 6], [5, 6]] },
{ type: "wide", anchors: [[7, 6], [11, 6]] }
];
const slots = slotDefinitions.map((definition, index) => ({
id: index,
type: definition.type,
anchors: definition.anchors.map(([column, row]) => ({
column,
row,
...gridPosition(column, row)
}))
}));
const targets = [];
const targetByKey = new Map();
function targetKey(column, row) {
return `${column}:${row}`;
}
for (const slot of slots) {
const type = HOOK_TYPES[slot.type];
slot.anchors.forEach((anchor, anchorIndex) => {
const group = new THREE.Group();
group.position.set(anchor.x, anchor.y, 0.052);
const fillMaterial = new THREE.MeshBasicMaterial({
color: type.color,
transparent: true,
opacity: 0.16,
depthWrite: false
});
const fill = new THREE.Mesh(
new THREE.CircleGeometry(0.12, 12),
fillMaterial
);
fill.position.z = 0;
group.add(fill);
const ringMaterial = new THREE.MeshBasicMaterial({
color: type.color,
transparent: true,
opacity: 0.78,
depthWrite: false,
depthTest: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.145, 0.031, 5, 16),
ringMaterial
);
ring.renderOrder = 5;
group.add(ring);
boardGroup.add(group);
const target = {
key: targetKey(anchor.column, anchor.row),
column: anchor.column,
row: anchor.row,
x: anchor.x,
y: anchor.y,
type: slot.type,
slotIndex: slot.id,
anchorIndex,
group,
ring,
fill,
phase: targets.length * 0.71
};
targets.push(target);
targetByKey.set(target.key, target);
});
}
// -------------------------------------------------------------------------
// Low-poly hook construction
// -------------------------------------------------------------------------
function createChamferedShape(width, height, chamfer = 0.11) {
const shape = new THREE.Shape();
const left = -width / 2;
const right = width / 2;
const bottom = -height / 2;
const top = height / 2;
shape.moveTo(left + chamfer, bottom);
shape.lineTo(right - chamfer, bottom);
shape.lineTo(right, bottom + chamfer);
shape.lineTo(right, top - chamfer);
shape.lineTo(right - chamfer, top);
shape.lineTo(left + chamfer, top);
shape.lineTo(left, top - chamfer);
shape.lineTo(left, bottom + chamfer);
shape.closePath();
return shape;
}
function addCylinderBetween(
parent,
start,
end,
radius,
material,
radialSegments = 6
) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius * 0.88,
length,
radialSegments,
1,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function createHook(id, typeKey, initialSlotIndex) {
const definition = HOOK_TYPES[typeKey];
const group = new THREE.Group();
group.userData.hookId = id;
const mountLocals =
definition.pitch === 1
? [new THREE.Vector3(0, 0, 0)]
: [
new THREE.Vector3(-definition.pitch / 2, 0, 0),
new THREE.Vector3(definition.pitch / 2, 0, 0)
];
const plateWidth =
definition.pitch === 1 ? 1.08 : definition.pitch + 0.58;
const plateHeight = 0.62;
const plateShape = createChamferedShape(plateWidth, plateHeight);
for (const mount of mountLocals) {
const holePath = new THREE.Path();
holePath.absarc(mount.x, mount.y, 0.115, 0, Math.PI * 2, true);
plateShape.holes.push(holePath);
}
const plateGeometry = new THREE.ExtrudeGeometry(plateShape, {
depth: 0.14,
bevelEnabled: false,
curveSegments: 8
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x31433e,
metalness: 0.52,
roughness: 0.38,
flatShading: true
});
const accentMaterial = new THREE.MeshStandardMaterial({
color: definition.color,
emissive: definition.color,
emissiveIntensity: 0.08,
metalness: 0.38,
roughness: 0.33,
flatShading: true
});
const plate = new THREE.Mesh(plateGeometry, baseMaterial);
plate.castShadow = true;
plate.receiveShadow = true;
group.add(plate);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(plateWidth * 0.28, 0.055, 0.045),
accentMaterial
);
cap.position.set(0, plateHeight / 2 + 0.018, 0.15);
cap.castShadow = true;
group.add(cap);
const mountRingMaterials = [];
for (const mount of mountLocals) {
const ringMaterial = new THREE.MeshBasicMaterial({
color: definition.color,
transparent: true,
opacity: 0.95,
depthTest: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.127, 0.022, 5, 14),
ringMaterial
);
ring.position.set(mount.x, mount.y, 0.148);
ring.renderOrder = 6;
group.add(ring);
mountRingMaterials.push(ringMaterial);
}
if (typeKey === "single") {
addCylinderBetween(
group,
new THREE.Vector3(0, 0.11, 0.22),
new THREE.Vector3(0, 0.72, 0.22),
0.095,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0, 0.72, 0.22),
new THREE.Vector3(0.29, 0.72, 0.22),
0.095,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0.29, 0.72, 0.22),
new THREE.Vector3(0.29, 0.84, 0.22),
0.095,
accentMaterial
);
} else if (typeKey === "double") {
addCylinderBetween(
group,
new THREE.Vector3(0, 0.1, 0.22),
new THREE.Vector3(0, 0.73, 0.22),
0.12,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0, 0.73, 0.22),
new THREE.Vector3(0.43, 0.73, 0.22),
0.12,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0.43, 0.73, 0.22),
new THREE.Vector3(0.43, 0.89, 0.22),
0.12,
accentMaterial
);
} else if (typeKey === "rail") {
addCylinderBetween(
group,
new THREE.Vector3(0, 0.09, 0.22),
new THREE.Vector3(0, 0.96, 0.22),
0.13,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0, 0.96, 0.22),
new THREE.Vector3(0.78, 0.96, 0.22),
0.13,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0.78, 0.96, 0.22),
new THREE.Vector3(0.78, 0.76, 0.22),
0.13,
accentMaterial
);
} else {
addCylinderBetween(
group,
new THREE.Vector3(0, 0.08, 0.21),
new THREE.Vector3(0, 0.65, 0.21),
0.13,
accentMaterial
);
addCylinderBetween(
group,
new THREE.Vector3(0, 0.15, 0.21),
new THREE.Vector3(0.72, 0.62, 0.34),
0.105,
accentMaterial
);
const shelf = new THREE.Mesh(
new THREE.BoxGeometry(1.18, 0.12, 0.52),
accentMaterial
);
shelf.position.set(0.35, 0.66, 0.36);
shelf.castShadow = true;
group.add(shelf);
}
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(),
new THREE.Vector3()
]);
const guideMaterial = new THREE.LineDashedMaterial({
color: 0xff5e54,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.95,
depthTest: false
});
const guide = new THREE.Line(guideGeometry, guideMaterial);
guide.visible = false;
guide.renderOrder = 8;
group.add(guide);
scene.add(group);
return {
id,
type: typeKey,
definition,
group,
mountLocals,
mountRingMaterials,
accentMaterial,
guide,
slotId: null,
initialSlotIndex,
invalidTarget: null,
invalidGuideLocal: null
};
}
const hooks = [
createHook("key", "single", 1),
createHook("towel", "double", 4),
createHook("rail", "rail", 6),
createHook("shelf", "wide", 9)
];
const hookById = new Map(hooks.map((hook) => [hook.id, hook]));
const occupancy = new Map();
function placeHookOnSlot(hook, slotIndex) {
const slot = slots[slotIndex];
const anchor = slot.anchors[0];
const mount = hook.mountLocals[0];
hook.group.position.set(
anchor.x - mount.x,
anchor.y - mount.y,
0.035
);
hook.group.rotation.set(0, 0, 0);
hook.slotId = slotIndex;
}
for (const hook of hooks) {
placeHookOnSlot(hook, hook.initialSlotIndex);
occupancy.set(hook.slotId, hook.id);
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
let selectedHookId = "key";
for (const hook of hooks) {
const card = document.createElement("button");
card.type = "button";
card.className = "hook-card";
card.dataset.hookId = hook.id;
card.innerHTML = `
<span
class="hook-swatch"
style="background:${hook.definition.css}"
>${hook.definition.pitch}H</span>
<span>
<span class="hook-name">${hook.definition.name}</span>
<span class="hook-meta">${hook.definition.description}</span>
</span>
<span class="pitch-badge">${hook.definition.pitch} holes</span>
`;
card.addEventListener("click", () => {
selectHook(hook.id);
});
hookListElement.appendChild(card);
}
function selectHook(id) {
selectedHookId = id;
const hook = hookById.get(id);
for (const card of hookListElement.querySelectorAll(".hook-card")) {
card.classList.toggle("active", card.dataset.hookId === id);
}
for (const item of hooks) {
item.accentMaterial.emissiveIntensity =
item.id === id ? 0.27 : 0.08;
}
if (!dragState) {
setStatus(
"neutral",
`${hook.definition.name} selected`,
`This hook requires ${hook.definition.pitch} hole spacing. Drag it onto a matching receiver.`
);
}
}
function setStatus(tone, title, detail) {
statusElement.dataset.tone = tone;
statusTitle.textContent = title;
statusDetail.textContent = detail;
}
let dragState = null;
selectHook(selectedHookId);
// -------------------------------------------------------------------------
// Pointer interaction
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerWorld = new THREE.Vector3();
const dragPlane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -0.05);
let activeTarget = null;
let activeSlotValid = false;
let invalidTarget = null;
let flashTarget = null;
let flashUntil = 0;
const INVALID_OFFSET = new THREE.Vector2(0.24, 0.16);
function updatePointer(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
raycaster.ray.intersectPlane(dragPlane, pointerWorld);
}
function hookFromIntersection(intersection) {
let object = intersection.object;
while (object && !object.userData.hookId) {
object = object.parent;
}
return object ? hookById.get(object.userData.hookId) : null;
}
function nearestTarget(position, maximumDistance = 0.46) {
let nearest = null;
let nearestDistance = maximumDistance;
for (const target of targets) {
const distance = Math.hypot(
target.x - position.x,
target.y - position.y
);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = target;
}
}
return nearest;
}
function slotOccupiedByOther(slotIndex, hook) {
const occupant = occupancy.get(slotIndex);
return occupant && occupant !== hook.id ? hookById.get(occupant) : null;
}
function setMountRingColor(hook, color) {
for (const material of hook.mountRingMaterials) {
material.color.setHex(color);
}
}
function clearInvalidGuide(hook) {
hook.guide.visible = false;
hook.invalidTarget = null;
hook.invalidGuideLocal = null;
setMountRingColor(hook, hook.definition.color);
}
function updateInvalidGuide(hook, target, localMount) {
hook.group.updateMatrixWorld(true);
const receiver = new THREE.Vector3(target.x, target.y, 0.42);
const actual = hook.group.localToWorld(
new THREE.Vector3(localMount.x, localMount.y, 0.16)
);
actual.z = 0.42;
hook.guide.geometry.setFromPoints([receiver, actual]);
hook.guide.computeLineDistances();
hook.guide.visible = true;
hook.invalidTarget = target;
hook.invalidGuideLocal = localMount;
setMountRingColor(hook, 0xff5e54);
}
function previewValid(hook, target) {
const localMount = hook.mountLocals[target.anchorIndex];
hook.group.position.set(
target.x - localMount.x,
target.y - localMount.y,
0.035
);
hook.group.rotation.set(0, 0, 0);
clearInvalidGuide(hook);
}
function previewInvalid(hook, target) {
/*
Put one mounting hole close to the receiving hole, but deliberately
offset it. The red dashed guide makes the failed alignment explicit.
*/
const localMount =
hook.mountLocals.length === 1
? hook.mountLocals[0]
: target.x < 0
? hook.mountLocals[0]
: hook.mountLocals[hook.mountLocals.length - 1];
hook.group.position.set(
target.x + INVALID_OFFSET.x - localMount.x,
target.y + INVALID_OFFSET.y - localMount.y,
0.045
);
hook.group.rotation.set(0, 0, 0.065);
updateInvalidGuide(hook, target, localMount);
}
function beginDrag(hook, event) {
selectHook(hook.id);
clearInvalidGuide(hook);
invalidTarget = null;
dragState = {
hook,
pointerId: event.pointerId,
startPosition: hook.group.position.clone(),
startRotation: hook.group.rotation.clone(),
startSlotId: hook.slotId,
grabOffset: new THREE.Vector3(
pointerWorld.x - hook.group.position.x,
pointerWorld.y - hook.group.position.y
)
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = "grabbing";
}
function updateDrag() {
if (!dragState) return;
const { hook } = dragState;
hook.group.position.set(
pointerWorld.x - dragState.grabOffset.x,
pointerWorld.y - dragState.grabOffset.y,
0.045
);
hook.group.rotation.set(0, 0, 0.035);
activeTarget = nearestTarget(pointerWorld);
if (!activeTarget) {
activeSlotValid = false;
setStatus(
"neutral",
"Find an illuminated receiver",
"Move the hook over one of the colored target holes."
);
return;
}
const slot = slots[activeTarget.slotIndex];
const spacingMatches = slot.type === hook.type;
const occupied = slotOccupiedByOther(slot.id, hook);
activeSlotValid = spacingMatches && !occupied;
if (activeSlotValid) {
previewValid(hook, activeTarget);
setStatus(
"valid",
"Spacing matches — snap aligned",
`${hook.definition.pitch}H hook → ${hook.definition.pitch}H receiver. The mounting holes line up exactly.`
);
} else {
previewInvalid(hook, activeTarget);
const reason = !spacingMatches
? `This receiver requires ${HOOK_TYPES[slot.type].pitch}H spacing.`
: "That receiver is already occupied.";
setStatus(
"invalid",
"Incompatible placement",
`${reason} The mounting hole remains visibly offset.`
);
}
}
function finishDrag(cancelled = false) {
if (!dragState) return;
const { hook, pointerId, startPosition, startRotation, startSlotId } =
dragState;
if (renderer.domElement.hasPointerCapture(pointerId)) {
renderer.domElement.releasePointerCapture(pointerId);
}
if (cancelled) {
hook.group.position.copy(startPosition);
hook.group.rotation.copy(startRotation);
hook.slotId = startSlotId;
clearInvalidGuide(hook);
setStatus(
"neutral",
"Placement cancelled",
"The hook was returned to its previous position."
);
} else if (activeTarget && activeSlotValid) {
const slot = slots[activeTarget.slotIndex];
if (hook.slotId !== null && hook.slotId !== slot.id) {
occupancy.delete(hook.slotId);
}
occupancy.set(slot.id, hook.id);
hook.slotId = slot.id;
previewValid(hook, activeTarget);
flashTarget = activeTarget;
flashUntil = performance.now() + 1100;
setStatus(
"valid",
"Compatible placement confirmed",
`${hook.definition.name} snapped to the ${hook.definition.pitch}H receiver.`
);
} else if (activeTarget) {
if (hook.slotId !== null) {
occupancy.delete(hook.slotId);
}
hook.slotId = null;
invalidTarget = activeTarget;
const slot = slots[activeTarget.slotIndex];
const reason = slot.type !== hook.type
? `Expected ${hook.definition.pitch}H spacing; this receiver is ${HOOK_TYPES[slot.type].pitch}H.`
: "The matching receiver is already occupied.";
setStatus(
"invalid",
"Placement held in misalignment",
`${reason} Move the hook again to correct the fit.`
);
} else {
hook.group.position.copy(startPosition);
hook.group.rotation.copy(startRotation);
hook.slotId = startSlotId;
clearInvalidGuide(hook);
setStatus(
"neutral",
"No receiver selected",
"Drop the hook directly over an illuminated hole."
);
}
dragState = null;
activeTarget = null;
activeSlotValid = false;
renderer.domElement.style.cursor = "grab";
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
updatePointer(event);
const intersections = raycaster.intersectObjects(
hooks.map((hook) => hook.group),
true
);
if (!intersections.length) return;
const hook = hookFromIntersection(intersections[0]);
if (!hook) return;
event.preventDefault();
beginDrag(hook, event);
});
renderer.domElement.addEventListener("pointermove", (event) => {
updatePointer(event);
if (dragState) {
updateDrag();
return;
}
const intersections = raycaster.intersectObjects(
hooks.map((hook) => hook.group),
true
);
renderer.domElement.style.cursor = intersections.length ? "grab" : "default";
});
renderer.domElement.addEventListener("pointerup", () => finishDrag(false));
renderer.domElement.addEventListener("pointercancel", () => finishDrag(true));
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragState) renderer.domElement.style.cursor = "default";
});
window.addEventListener("keydown", (event) => {
if (event.key === "Escape" && dragState) {
finishDrag(true);
}
});
// -------------------------------------------------------------------------
// Reset
// -------------------------------------------------------------------------
document.querySelector("#resetButton").addEventListener("click", () => {
if (dragState) finishDrag(true);
occupancy.clear();
invalidTarget = null;
flashTarget = null;
for (const hook of hooks) {
clearInvalidGuide(hook);
placeHookOnSlot(hook, hook.initialSlotIndex);
occupancy.set(hook.slotId, hook.id);
}
selectHook("key");
setStatus(
"neutral",
"Board reset",
"All hooks are back in their original compatible positions."
);
});
// -------------------------------------------------------------------------
// Rendering and responsive camera framing
// -------------------------------------------------------------------------
function resize() {
const width = stage.clientWidth;
const height = stage.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
const verticalField = THREE.MathUtils.degToRad(camera.fov);
const horizontalDistance =
(BOARD_W / 2 + 0.8) /
(Math.tan(verticalField / 2) * camera.aspect);
const verticalDistance = (BOARD_H / 2 + 1.1) / Math.tan(verticalField / 2);
camera.position.set(0, 0, Math.max(17.5, horizontalDistance, verticalDistance));
camera.lookAt(0, 0, 0);
}
window.addEventListener("resize", resize);
resize();
function animateTargets(time) {
const seconds = time / 1000;
for (const target of targets) {
const baseColor = HOOK_TYPES[target.type].color;
let color = baseColor;
let opacity = 0.56;
let emphasis = 0;
if (dragState) {
if (target === activeTarget) {
color = activeSlotValid ? 0x66e6a3 : 0xff6258;
opacity = 1;
emphasis = 0.18;
} else if (target.type === dragState.hook.type) {
opacity = 0.92;
emphasis = 0.05;
} else {
opacity = 0.14;
}
} else if (target === invalidTarget) {
color = 0xff6258;
opacity = 1;
emphasis = 0.13;
} else if (target === flashTarget && time < flashUntil) {
color = 0x66e6a3;
opacity = 1;
emphasis = 0.13;
}
const pulse = 1 + Math.sin(seconds * 3.1 + target.phase) * 0.045;
target.ring.material.color.setHex(color);
target.ring.material.opacity = opacity;
target.ring.scale.setScalar(pulse + emphasis);
target.fill.material.color.setHex(color);
target.fill.material.opacity = opacity * 0.16;
target.fill.scale.setScalar(pulse + emphasis * 0.4);
}
}
function animate(time) {
animateTargets(time);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```