Prompt specification
Build or customize the Three.js 3D scene "Reuse parts library kiosk" 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: Reuse parts library kiosk
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reuse-parts-library-kiosk-x0272
Scene Description Prompt:
Build a low-poly repair kiosk that matches replacement handles, knobs, and hinges by geometric fit. Three.js from cdnjs, one HTML file.
Working Three.js Starter (reuse-parts-library-kiosk-x0272):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FORM / FIX — Geometric Repair Kiosk</title>
<style>
:root {
--ink: #17211d;
--muted: #6c756f;
--paper: #f4f2e9;
--panel: #ebe9df;
--line: #d5d5ca;
--green: #b8ef57;
--green-dark: #51742d;
--orange: #ff7048;
--red: #d84e3f;
--blue: #4c83a8;
--shadow: 0 14px 35px rgba(31, 44, 37, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
button:focus-visible {
outline: 3px solid rgba(76, 131, 168, 0.45);
outline-offset: 2px;
}
#app {
height: 100dvh;
display: grid;
grid-template-rows: 68px minmax(0, 1fr);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 22px;
background: var(--ink);
color: #f8f7ee;
border-bottom: 1px solid rgba(255, 255, 255, 0.09);
z-index: 20;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 10px;
background: var(--green);
color: var(--ink);
font-size: 22px;
font-weight: 900;
transform: rotate(-5deg);
}
.brand-title {
font-size: 15px;
font-weight: 850;
letter-spacing: 0.14em;
white-space: nowrap;
}
.brand-title span {
color: var(--green);
}
.brand-subtitle {
margin-top: 2px;
color: #aeb8b1;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-right {
display: flex;
align-items: center;
gap: 18px;
color: #c7cec9;
font-size: 12px;
}
.local-status {
display: flex;
align-items: center;
gap: 8px;
text-transform: uppercase;
letter-spacing: 0.1em;
font-size: 10px;
font-weight: 750;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 0 5px rgba(184, 239, 87, 0.1);
}
.header-tag {
border: 1px solid #435048;
border-radius: 999px;
padding: 7px 10px;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 292px minmax(360px, 1fr) 304px;
gap: 1px;
background: var(--line);
}
aside {
min-height: 0;
overflow-y: auto;
background: var(--paper);
scrollbar-width: thin;
scrollbar-color: #bbc0b7 transparent;
}
.inventory {
display: flex;
flex-direction: column;
}
.panel-heading {
padding: 22px 20px 16px;
}
.eyebrow {
margin: 0 0 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1,
h2,
h3,
p {
margin-top: 0;
}
h1 {
margin-bottom: 7px;
font-size: 22px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.panel-copy {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.suggest-button {
margin-top: 14px;
width: 100%;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: #fffef8;
color: var(--ink);
cursor: pointer;
font-size: 11px;
font-weight: 750;
text-align: left;
display: flex;
justify-content: space-between;
align-items: center;
}
.suggest-button:hover {
border-color: #aeb5aa;
background: #fff;
}
.suggest-button span:last-child {
color: var(--green-dark);
}
.category-tabs {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 0 14px 14px;
}
.category-tab {
min-width: 0;
padding: 11px 4px 9px;
border: 1px solid var(--line);
border-radius: 10px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.03em;
}
.category-tab .tab-icon {
display: block;
margin-bottom: 5px;
font-size: 19px;
line-height: 1;
color: var(--ink);
}
.category-tab.active {
background: var(--ink);
border-color: var(--ink);
color: #f7f6ed;
}
.category-tab.active .tab-icon {
color: var(--green);
}
.category-tab:disabled {
cursor: wait;
opacity: 0.65;
}
.stock-heading {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 20px 9px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.stock-count {
padding: 3px 7px;
border-radius: 999px;
background: #dedfd5;
color: var(--ink);
letter-spacing: 0;
}
#part-list {
padding: 0 12px 20px;
display: grid;
gap: 9px;
}
.part-card {
position: relative;
width: 100%;
padding: 13px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.55);
color: var(--ink);
cursor: pointer;
text-align: left;
transition: transform 150ms ease, border-color 150ms ease, background 150ms ease;
}
.part-card:hover {
transform: translateY(-1px);
border-color: #9fa89e;
background: #fffef8;
}
.part-card.selected {
border-color: var(--ink);
background: #fffef8;
box-shadow: inset 3px 0 0 var(--green);
}
.part-card:disabled {
cursor: wait;
opacity: 0.62;
}
.part-top {
display: flex;
align-items: flex-start;
gap: 10px;
}
.part-glyph {
flex: 0 0 36px;
height: 36px;
display: grid;
place-items: center;
border-radius: 9px;
background: #e3e4dc;
font-size: 21px;
}
.part-card.selected .part-glyph {
background: var(--green);
}
.part-name {
font-size: 13px;
font-weight: 800;
line-height: 1.2;
}
.part-code {
margin-top: 4px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
letter-spacing: 0.05em;
}
.part-dimensions {
margin-top: 11px;
padding-top: 9px;
border-top: 1px solid #e1e1d8;
color: #4f5a53;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
line-height: 1.45;
}
.part-finish {
margin-left: auto;
flex-shrink: 0;
color: var(--muted);
font-size: 9px;
font-weight: 750;
}
.stage {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background: #d9ded7;
}
#three-canvas {
width: 100%;
height: 100%;
display: block;
outline: none;
cursor: grab;
touch-action: none;
}
#three-canvas.dragging {
cursor: grabbing;
}
.stage-top {
position: absolute;
top: 16px;
left: 16px;
right: 16px;
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
pointer-events: none;
}
.stage-badge {
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 9px;
background: rgba(248, 248, 240, 0.77);
box-shadow: 0 5px 20px rgba(44, 54, 48, 0.08);
backdrop-filter: blur(8px);
}
.stage-badge strong {
display: block;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stage-badge span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.stage-actions {
display: flex;
gap: 7px;
pointer-events: auto;
}
.icon-button {
width: 36px;
height: 36px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 9px;
background: rgba(248, 248, 240, 0.82);
color: var(--ink);
cursor: pointer;
box-shadow: 0 5px 20px rgba(44, 54, 48, 0.08);
font-weight: 800;
}
.icon-button:hover {
background: white;
}
.stage-bottom {
position: absolute;
bottom: 16px;
left: 16px;
right: 16px;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 16px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
max-width: 440px;
padding: 10px 12px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 9px;
background: rgba(248, 248, 240, 0.75);
color: #56615a;
font-size: 9px;
backdrop-filter: blur(8px);
}
.legend span {
display: flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--green);
}
.legend i.blue {
background: var(--blue);
}
.legend i.orange {
background: var(--orange);
}
.orbit-hint {
color: #56615a;
font-size: 9px;
text-align: right;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.stage-flash {
position: absolute;
inset: 0;
pointer-events: none;
opacity: 0;
box-shadow: inset 0 0 0 0 rgba(216, 78, 63, 0);
}
.stage-flash.reject {
animation: rejectFlash 700ms ease-out;
}
.stage-flash.success {
animation: successFlash 800ms ease-out;
}
@keyframes rejectFlash {
0% { opacity: 1; box-shadow: inset 0 0 0 8px rgba(216, 78, 63, 0.75); }
100% { opacity: 0; box-shadow: inset 0 0 0 20px rgba(216, 78, 63, 0); }
}
@keyframes successFlash {
0% { opacity: 1; box-shadow: inset 0 0 0 8px rgba(184, 239, 87, 0.9); }
100% { opacity: 0; box-shadow: inset 0 0 0 20px rgba(184, 239, 87, 0); }
}
.inspector {
background: var(--paper);
padding-bottom: 24px;
}
.inspector-title {
padding: 22px 20px 18px;
border-bottom: 1px solid var(--line);
}
.inspector-title h2 {
margin-bottom: 5px;
font-size: 19px;
letter-spacing: -0.025em;
}
.inspector-title p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.steps {
padding: 16px 20px 8px;
}
.step {
position: relative;
display: grid;
grid-template-columns: 25px 1fr;
gap: 9px;
padding-bottom: 13px;
color: #929990;
font-size: 10px;
font-weight: 750;
}
.step:not(:last-child)::after {
content: "";
position: absolute;
top: 24px;
bottom: 1px;
left: 11px;
width: 1px;
background: #d5d8cf;
}
.step-number {
width: 23px;
height: 23px;
display: grid;
place-items: center;
border: 1px solid #cdd1c8;
border-radius: 50%;
background: var(--paper);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 9px;
}
.step.active {
color: var(--ink);
}
.step.active .step-number {
border-color: var(--ink);
background: var(--ink);
color: var(--green);
}
.step.complete {
color: var(--green-dark);
}
.step.complete .step-number {
border-color: var(--green);
background: var(--green);
color: var(--ink);
}
.measurement {
margin: 5px 16px 16px;
padding: 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(255, 255, 255, 0.42);
}
.measurement-heading {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
}
.measurement-heading strong {
font-size: 11px;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.measurement-status {
padding: 4px 6px;
border-radius: 5px;
background: #e2e3da;
color: #707970;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 8px;
text-transform: uppercase;
}
.measurement-status.ready {
background: #dff1b7;
color: #466422;
}
.spec-list {
display: grid;
gap: 10px;
}
.spec-row {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
padding-bottom: 9px;
border-bottom: 1px dashed #d7d8cf;
}
.spec-row:last-child {
padding-bottom: 0;
border-bottom: 0;
}
.spec-name {
color: var(--muted);
font-size: 10px;
}
.spec-value {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
font-weight: 800;
text-align: right;
}
.spec-value.hidden-value {
color: #a4aaa1;
letter-spacing: 0.2em;
}
.primary-button,
.secondary-button {
width: calc(100% - 32px);
margin: 0 16px 10px;
min-height: 43px;
border-radius: 10px;
cursor: pointer;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.04em;
}
.primary-button {
border: 1px solid var(--ink);
background: var(--green);
color: var(--ink);
box-shadow: 0 5px 0 #8aab45;
}
.primary-button:hover:not(:disabled) {
transform: translateY(-1px);
box-shadow: 0 6px 0 #8aab45;
}
.primary-button:active:not(:disabled) {
transform: translateY(3px);
box-shadow: 0 2px 0 #8aab45;
}
.secondary-button {
border: 1px solid var(--line);
background: transparent;
color: var(--ink);
}
.secondary-button:hover:not(:disabled) {
background: #fffef8;
}
.primary-button:disabled,
.secondary-button:disabled {
cursor: not-allowed;
opacity: 0.42;
box-shadow: none;
}
.result {
margin: 8px 16px 0;
padding: 14px;
min-height: 92px;
border: 1px solid var(--line);
border-radius: 12px;
background: #efeee6;
}
.result.success {
border-color: #9fbe6a;
background: #edf6d8;
}
.result.reject {
border-color: #d9a398;
background: #f8e7e2;
}
.result-title {
margin-bottom: 6px;
font-size: 11px;
font-weight: 850;
}
.result.success .result-title {
color: var(--green-dark);
}
.result.reject .result-title {
color: var(--red);
}
.result-text {
margin: 0;
color: #69716a;
font-size: 10px;
line-height: 1.5;
}
.result ul {
margin: 6px 0 0;
padding-left: 15px;
color: #79564f;
font-size: 9px;
line-height: 1.55;
}
.fine-print {
margin: 13px 20px 0;
color: #8b928a;
font-size: 9px;
line-height: 1.5;
}
#toast {
position: absolute;
top: 76px;
left: 50%;
z-index: 12;
padding: 10px 14px;
border-radius: 9px;
background: var(--ink);
color: white;
box-shadow: var(--shadow);
font-size: 10px;
font-weight: 750;
transform: translate(-50%, -12px);
opacity: 0;
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: absolute;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #d9ded7;
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 250ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 1100px) {
.workspace {
grid-template-columns: 270px minmax(320px, 1fr) 280px;
}
.part-finish {
display: none;
}
}
@media (max-width: 860px) {
body {
overflow: auto;
}
#app {
height: auto;
min-height: 100dvh;
grid-template-rows: 68px auto;
}
.workspace {
display: flex;
flex-direction: column;
}
.inventory {
max-height: 430px;
}
.stage {
height: 62dvh;
min-height: 460px;
order: -1;
}
.inspector {
overflow: visible;
}
.header-tag {
display: none;
}
}
@media (max-width: 520px) {
header {
padding: 0 14px;
}
.brand-subtitle,
.local-status {
display: none;
}
.stage-bottom {
align-items: flex-start;
}
.orbit-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true">↗</div>
<div>
<div class="brand-title">FORM <span>/</span> FIX</div>
<div class="brand-subtitle">Geometric repair kiosk</div>
</div>
</div>
<div class="header-right">
<div class="local-status">
<span class="status-dot"></span>
Local geometry engine
</div>
<div class="header-tag">BAY 03 · REPAIR</div>
</div>
</header>
<main class="workspace">
<aside class="inventory" aria-label="Replacement inventory">
<div class="panel-heading">
<p class="eyebrow">Replacement stock</p>
<h1>Match by geometry.</h1>
<p class="panel-copy">
Scan the mounting point, choose a replacement, then verify the fit
against measured dimensions.
</p>
<button class="suggest-button" id="suggest-button" type="button">
<span>Suggest closest part</span>
<span>↗</span>
</button>
</div>
<div class="category-tabs" role="tablist" aria-label="Part category">
<button class="category-tab active" data-category="handles" type="button">
<span class="tab-icon">↔</span>
Handles
</button>
<button class="category-tab" data-category="knobs" type="button">
<span class="tab-icon">◉</span>
Knobs
</button>
<button class="category-tab" data-category="hinges" type="button">
<span class="tab-icon">⌁</span>
Hinges
</button>
</div>
<div class="stock-heading">
<span>Available parts</span>
<span class="stock-count" id="stock-count">3 SKUs</span>
</div>
<div id="part-list"></div>
</aside>
<section class="stage" id="stage" aria-label="Interactive 3D repair kiosk">
<canvas id="three-canvas" tabindex="0"></canvas>
<div class="stage-top">
<div class="stage-badge">
<strong id="station-title">Mount station 01</strong>
<span>Low-poly service kiosk · metric units</span>
</div>
<div class="stage-actions">
<button class="icon-button" id="reset-camera" type="button" title="Reset camera" aria-label="Reset camera">
⟲
</button>
</div>
</div>
<div class="stage-bottom">
<div class="legend">
<span><i></i> Active mount</span>
<span><i class="blue"></i> Measured geometry</span>
<span><i class="orange"></i> Replacement part</span>
</div>
<div class="orbit-hint">Drag to orbit · Scroll to zoom</div>
</div>
<div class="stage-flash" id="stage-flash"></div>
<div id="toast" role="status" aria-live="polite"></div>
<div id="loading">Loading repair kiosk…</div>
</section>
<aside class="inspector" aria-label="Fit inspector">
<div class="inspector-title">
<p class="eyebrow">Fit inspector</p>
<h2 id="inspector-title">Pull handle mount</h2>
<p>Physical compatibility is checked from measured dimensions only.</p>
</div>
<div class="steps">
<div class="step active" id="step-1">
<span class="step-number">1</span>
<span>Measure active mount</span>
</div>
<div class="step" id="step-2">
<span class="step-number">2</span>
<span>Select replacement</span>
</div>
<div class="step" id="step-3">
<span class="step-number">3</span>
<span>Run fit test</span>
</div>
</div>
<div class="measurement">
<div class="measurement-heading">
<strong>Mount geometry</strong>
<span class="measurement-status" id="measurement-status">Not scanned</span>
</div>
<div class="spec-list" id="spec-list"></div>
</div>
<button class="secondary-button" id="scan-button" type="button">
Scan mount geometry
</button>
<button class="primary-button" id="test-button" type="button" disabled>
Test geometric fit
</button>
<div class="result" id="result" aria-live="polite">
<div class="result-title" id="result-title">Awaiting mount scan</div>
<p class="result-text" id="result-text">
Measure the active station before selecting a replacement.
</p>
</div>
<p class="fine-print">
Fit tolerances: ±2 mm handle pitch, ±0.5 mm shaft bore, and ±1 mm
hinge pitch. All dimensions are illustrative.
</p>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (selector) => document.querySelector(selector);
const $$ = (selector) => [...document.querySelectorAll(selector)];
const catalog = {
handles: {
label: "Pull handle mount",
station: "Mount station 01",
icon: "↔",
target: {
holeSpacing: 128,
holeDia: 5,
clearance: 32
},
specs: [
["Hole pitch", "holeSpacing", "mm", "±2 mm"],
["Hole diameter", "holeDia", "mm", "±0.5 mm"],
["Grip clearance", "clearance", "mm", "clearance"]
],
parts: [
{
id: "H-128",
name: "Bow pull",
finish: "Steel",
dims: { holeSpacing: 128, holeDia: 5, clearance: 32 }
},
{
id: "H-096",
name: "Slim pull",
finish: "Black",
dims: { holeSpacing: 96, holeDia: 5, clearance: 28 }
},
{
id: "H-152",
name: "Bridge pull",
finish: "Brass",
dims: { holeSpacing: 152, holeDia: 6, clearance: 35 }
}
]
},
knobs: {
label: "Control knob mount",
station: "Mount station 02",
icon: "◉",
target: {
bore: 12,
collarDia: 28,
shaftDepth: 18
},
specs: [
["Shaft bore", "bore", "mm", "±0.5 mm"],
["Base collar", "collarDia", "mm", "±2 mm"],
["Shaft depth", "shaftDepth", "mm", "±1 mm"]
],
parts: [
{
id: "K-12",
name: "Low-profile knob",
finish: "Steel",
dims: { bore: 12, collarDia: 28, shaftDepth: 18 }
},
{
id: "K-08",
name: "Compact knob",
finish: "Black",
dims: { bore: 8, collarDia: 25, shaftDepth: 16 }
},
{
id: "K-14",
name: "Grip knob",
finish: "Brass",
dims: { bore: 14, collarDia: 32, shaftDepth: 20 }
}
]
},
hinges: {
label: "Cabinet hinge mount",
station: "Mount station 03",
icon: "⌁",
target: {
holePitch: 72,
barrelDia: 18,
leafWidth: 42
},
specs: [
["Hole pitch", "holePitch", "mm", "±1 mm"],
["Barrel diameter", "barrelDia", "mm", "±1 mm"],
["Leaf width", "leafWidth", "mm", "±2 mm"]
],
parts: [
{
id: "HG-72",
name: "Standard butt hinge",
finish: "Steel",
dims: { holePitch: 72, barrelDia: 18, leafWidth: 42 }
},
{
id: "HG-64",
name: "Compact hinge",
finish: "Black",
dims: { holePitch: 64, barrelDia: 16, leafWidth: 40 }
},
{
id: "HG-80",
name: "Wide hinge",
finish: "Brass",
dims: { holePitch: 80, barrelDia: 20, leafWidth: 44 }
}
]
}
};
const state = {
category: "handles",
selectedPart: null,
scanned: false,
scanning: false,
scanStart: 0,
testing: false,
fitResult: null,
test: null
};
const canvas = $("#three-canvas");
const stage = $("#stage");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9ded7);
scene.fog = new THREE.Fog(0xd9ded7, 11, 24);
const camera = new THREE.PerspectiveCamera(39, 1, 0.1, 50);
const cameraTarget = new THREE.Vector3(0, 1.95, 0);
const orbit = {
theta: 0.57,
phi: 1.15,
radius: 9.3,
target: cameraTarget.clone()
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.theta = 0.57;
orbit.phi = 1.15;
orbit.radius = 9.3;
orbit.target.copy(cameraTarget);
updateCamera();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf9fff4, 0x657067, 2.1);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff7df, 3.2);
keyLight.position.set(4.5, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.bias = -0.0007;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xaac9e1, 1.25);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xd9ded7,
roughness: 1,
metalness: 0
}),
cabinet: new THREE.MeshStandardMaterial({
color: 0xd9d4c3,
roughness: 0.88,
metalness: 0.02,
flatShading: true
}),
cabinetDark: new THREE.MeshStandardMaterial({
color: 0x35413a,
roughness: 0.78,
metalness: 0.08,
flatShading: true
}),
bench: new THREE.MeshStandardMaterial({
color: 0x8b9389,
roughness: 0.72,
metalness: 0.12,
flatShading: true
}),
board: new THREE.MeshStandardMaterial({
color: 0xb8c1b8,
roughness: 0.9,
metalness: 0.02,
flatShading: true
}),
panel: new THREE.MeshStandardMaterial({
color: 0xcdd0c6,
roughness: 0.84,
metalness: 0.03,
flatShading: true
}),
dark: new THREE.MeshStandardMaterial({
color: 0x202a25,
roughness: 0.67,
metalness: 0.12,
flatShading: true
}),
steel: new THREE.MeshStandardMaterial({
color: 0xb9c3bf,
roughness: 0.34,
metalness: 0.72,
flatShading: true
}),
black: new THREE.MeshStandardMaterial({
color: 0x303633,
roughness: 0.5,
metalness: 0.45,
flatShading: true
}),
brass: new THREE.MeshStandardMaterial({
color: 0xb88d49,
roughness: 0.38,
metalness: 0.7,
flatShading: true
}),
green: new THREE.MeshStandardMaterial({
color: 0xb8ef57,
roughness: 0.6,
metalness: 0.08,
flatShading: true
}),
orange: new THREE.MeshStandardMaterial({
color: 0xff7048,
roughness: 0.55,
metalness: 0.1,
flatShading: true
})
};
const finishMaterials = {
Steel: materials.steel,
Black: materials.black,
Brass: materials.brass
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderZ(parent, radius, length, position, material, segments = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.rotation.x = Math.PI / 2;
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderY(parent, radius, length, position, material, segments = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinderX(parent, radius, length, position, material, segments = 10) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, segments),
material
);
mesh.rotation.z = Math.PI / 2;
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextPlane(text, width, height, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = options.canvasWidth || 768;
canvas.height = options.canvasHeight || 160;
const ctx = canvas.getContext("2d");
if (options.background) {
ctx.fillStyle = options.background;
ctx.fillRect(0, 0, canvas.width, canvas.height);
} else {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
if (options.border) {
ctx.strokeStyle = options.border;
ctx.lineWidth = 8;
ctx.strokeRect(8, 8, canvas.width - 16, canvas.height - 16);
}
ctx.fillStyle = options.color || "#17211d";
ctx.font = `${options.weight || 800} ${options.fontSize || 54}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 3);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
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 makeMeasureSprite(text) {
const canvas = document.createElement("canvas");
canvas.width = 384;
canvas.height = 112;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(20, 36, 45, 0.92)";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#d9f2ff";
ctx.font = "800 42px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(0.72, 0.21, 1);
sprite.renderOrder = 10;
return sprite;
}
// Ground and subtle grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
materials.floor
);
floor.rotation.x = -Math.PI / 2;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x839087, 0xb7c0b7);
grid.position.y = 0.008;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
const kiosk = new THREE.Group();
scene.add(kiosk);
// Cabinet and workbench.
addBox(kiosk, [4.75, 1.08, 1.6], [0, 0.64, 0], materials.cabinet);
addBox(kiosk, [4.42, 0.12, 1.38], [0, 0.1, 0], materials.dark);
addBox(kiosk, [5.25, 0.18, 2.12], [0, 1.22, 0], materials.bench);
for (let i = -1; i <= 1; i++) {
addBox(
kiosk,
[1.43, 0.86, 0.06],
[i * 1.49, 0.69, 0.83],
materials.cabinet
);
addBox(
kiosk,
[0.34, 0.055, 0.065],
[i * 1.49, 0.84, 0.89],
materials.dark
);
}
// Vertical board and support structure.
addBox(kiosk, [5.48, 2.68, 0.18], [0, 2.63, -0.85], materials.board);
addBox(kiosk, [0.2, 2.78, 0.68], [-2.6, 2.57, -0.86], materials.cabinetDark);
addBox(kiosk, [0.2, 2.78, 0.68], [2.6, 2.57, -0.86], materials.cabinetDark);
addBox(kiosk, [5.5, 0.4, 0.54], [0, 4.1, -0.69], materials.cabinetDark);
const headerText = makeTextPlane(
"FORM / FIX · GEOMETRIC PARTS",
3.9,
0.28,
{
canvasWidth: 1024,
canvasHeight: 128,
color: "#eef3e8",
fontSize: 46,
weight: 850
}
);
headerText.position.set(0, 4.1, -0.405);
kiosk.add(headerText);
const statusLamp = addCylinderZ(
kiosk,
0.065,
0.04,
[2.43, 4.1, -0.39],
materials.green,
12
);
// Pegboard holes using an instanced mesh.
const pegGeometry = new THREE.CylinderGeometry(0.022, 0.022, 0.018, 6);
pegGeometry.rotateX(Math.PI / 2);
const pegMaterial = new THREE.MeshStandardMaterial({
color: 0x4b574f,
roughness: 1
});
const pegs = new THREE.InstancedMesh(pegGeometry, pegMaterial, 72);
const dummy = new THREE.Object3D();
let pegIndex = 0;
for (let row = 0; row < 6; row++) {
for (let col = 0; col < 12; col++) {
dummy.position.set(
-2.42 + col * 0.44,
1.52 + row * 0.41,
-0.748
);
dummy.updateMatrix();
pegs.setMatrixAt(pegIndex++, dummy.matrix);
}
}
pegs.receiveShadow = true;
kiosk.add(pegs);
// Work tray for the selected replacement.
addBox(
kiosk,
[1.42, 0.045, 0.72],
[0, 1.335, 0.57],
materials.dark
);
addBox(
kiosk,
[1.22, 0.012, 0.52],
[0, 1.362, 0.57],
materials.cabinetDark,
false
);
const stations = [];
const stationX = [-1.72, 0, 1.72];
function buildPartModel(category, dims, mainMaterial, accentMaterial, ghost = false) {
const group = new THREE.Group();
if (category === "handles") {
const spacing = dims.holeSpacing / 100;
const clearance = dims.clearance / 100;
const holeRadius = dims.holeDia / 200;
for (const sign of [-1, 1]) {
addCylinderZ(
group,
0.045,
clearance,
[sign * spacing / 2, 0, clearance / 2],
mainMaterial
);
addCylinderZ(
group,
0.082,
0.055,
[sign * spacing / 2, 0, 0.03],
accentMaterial
);
addCylinderZ(
group,
holeRadius,
0.065,
[sign * spacing / 2, 0, 0.02],
accentMaterial,
8
);
}
addCylinderX(
group,
0.067,
spacing,
[0, 0, clearance],
mainMaterial,
10
);
}
if (category === "knobs") {
const depth = dims.shaftDepth / 100;
const collarRadius = dims.collarDia / 200;
addCylinderZ(
group,
dims.bore / 200,
0.22,
[0, 0, 0.055],
accentMaterial,
12
);
addCylinderZ(
group,
collarRadius,
0.055,
[0, 0, 0.045],
mainMaterial,
12
);
addCylinderZ(
group,
0.17,
depth,
[0, 0, depth / 2 + 0.055],
mainMaterial,
12
);
// Faceted grip notches.
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const notch = new THREE.Mesh(
new THREE.BoxGeometry(0.035, 0.018, depth * 0.72),
accentMaterial
);
notch.position.set(
Math.cos(angle) * 0.165,
Math.sin(angle) * 0.165,
depth / 2 + 0.055
);
notch.rotation.z = angle;
notch.castShadow = true;
group.add(notch);
}
}
if (category === "hinges") {
const leafWidth = dims.leafWidth / 100;
const pitch = dims.holePitch / 100;
const barrelRadius = dims.barrelDia / 200;
const leafHeight = 0.78;
addBox(
group,
[leafWidth / 2 - 0.025, leafHeight, 0.055],
[-leafWidth / 4, 0, 0.035],
mainMaterial
);
addBox(
group,
[leafWidth / 2 - 0.025, leafHeight, 0.055],
[leafWidth / 4, 0, 0.055],
mainMaterial
);
addCylinderY(
group,
barrelRadius,
leafHeight + 0.04,
[0, 0, 0.09],
mainMaterial,
10
);
for (const x of [-leafWidth / 4, leafWidth / 4]) {
for (const y of [-pitch / 2, pitch / 2]) {
addCylinderZ(
group,
0.025,
0.065,
[x, y, 0.085],
accentMaterial,
8
);
}
}
}
if (ghost) {
group.traverse((object) => {
if (object.isMesh) {
object.castShadow = false;
object.receiveShadow = false;
object.renderOrder = 1;
}
});
}
return group;
}
function buildStation(category, x, index) {
const data = catalog[category];
const group = new THREE.Group();
group.position.set(x, 2.59, -0.715);
group.userData.category = category;
const plateMaterial = materials.panel.clone();
const plate = addBox(
group,
[1.38, 1.58, 0.075],
[0, 0, 0],
plateMaterial
);
const edgeGeometry = new THREE.EdgesGeometry(plate.geometry);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x7d897f,
transparent: true,
opacity: 0.5
});
const outline = new THREE.LineSegments(edgeGeometry, edgeMaterial);
outline.position.copy(plate.position);
group.add(outline);
const mount = addBox(
group,
[0.97, 0.91, 0.045],
[0, -0.06, 0.06],
materials.cabinet
);
const label = makeTextPlane(
`${String(index + 1).padStart(2, "0")} ${data.label.toUpperCase()}`,
1.12,
0.19,
{
canvasWidth: 900,
canvasHeight: 120,
color: "#314039",
fontSize: 37
}
);
label.position.set(0, 0.61, 0.052);
group.add(label);
const ghostMaterial = new THREE.MeshStandardMaterial({
color: 0x6d8fa0,
roughness: 0.65,
metalness: 0.08,
transparent: true,
opacity: 0.17,
depthWrite: false,
flatShading: true
});
const ghost = buildPartModel(
category,
data.target,
ghostMaterial,
ghostMaterial,
true
);
ghost.position.set(0, -0.06, 0.11);
group.add(ghost);
// Mounting-hole indications.
if (category === "handles") {
for (const sign of [-1, 1]) {
addCylinderZ(
group,
0.027,
0.025,
[sign * data.target.holeSpacing / 200, -0.06, 0.09],
materials.dark,
8
);
}
}
if (category === "knobs") {
addCylinderZ(
group,
data.target.bore / 200,
0.026,
[0, -0.06, 0.09],
materials.dark,
16
);
addCylinderZ(
group,
data.target.collarDia / 200,
0.022,
[0, -0.06, 0.085],
ghostMaterial,
20
);
}
if (category === "hinges") {
for (const xHole of [-data.target.leafWidth / 400, data.target.leafWidth / 400]) {
for (const yHole of [-data.target.holePitch / 200, data.target.holePitch / 200]) {
addCylinderZ(
group,
0.025,
0.025,
[xHole, -0.06 + yHole, 0.09],
materials.dark,
8
);
}
}
}
kiosk.add(group);
const station = {
category,
group,
plate,
plateMaterial,
outline,
edgeMaterial,
ghost,
ghostMaterial
};
stations.push(station);
return station;
}
buildStation("handles", stationX[0], 0);
buildStation("knobs", stationX[1], 1);
buildStation("hinges", stationX[2], 2);
// Small low-poly floor marker.
const marker = new THREE.Mesh(
new THREE.CylinderGeometry(1.55, 1.7, 0.035, 12),
new THREE.MeshStandardMaterial({
color: 0xc8cdc5,
roughness: 1
})
);
marker.position.set(0, 0.018, 0.05);
marker.receiveShadow = true;
scene.add(marker);
// Scanner beam.
const scannerMaterial = new THREE.MeshBasicMaterial({
color: 0x9ce8ff,
transparent: true,
opacity: 0.7,
depthWrite: false
});
const scannerBeam = new THREE.Mesh(
new THREE.BoxGeometry(0.045, 2.62, 0.04),
scannerMaterial
);
scannerBeam.position.set(-2.7, 2.63, -0.55);
scannerBeam.visible = false;
scene.add(scannerBeam);
// Measurement overlay.
let measurementGroup = null;
let preview = null;
function clearMeasurement() {
if (!measurementGroup) return;
scene.remove(measurementGroup);
measurementGroup.traverse((object) => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
if (object.material.map) object.material.map.dispose();
object.material.dispose();
}
});
measurementGroup = null;
}
function addDimensionLine(points, labelText, labelPosition) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const material = new THREE.LineBasicMaterial({
color: 0x2f789f,
transparent: true,
opacity: 0.95,
depthTest: false
});
const line = new THREE.Line(geometry, material);
line.renderOrder = 9;
measurementGroup.add(line);
const sprite = makeMeasureSprite(labelText);
sprite.position.set(...labelPosition);
measurementGroup.add(sprite);
}
function buildMeasurementOverlay() {
clearMeasurement();
measurementGroup = new THREE.Group();
const station = stations.find((item) => item.category === state.category);
const target = catalog[state.category].target;
const sx = station.group.position.x;
const sy = station.group.position.y;
const sz = station.group.position.z + 0.34;
if (state.category === "handles") {
const half = target.holeSpacing / 200;
const y = sy - 0.61;
addDimensionLine(
[
[sx - half, y, sz],
[sx + half, y, sz]
],
`${target.holeSpacing} mm`,
[sx, y - 0.16, sz]
);
addDimensionLine(
[
[sx - half, y - 0.06, sz],
[sx - half, y + 0.06, sz]
],
"",
[sx, y - 1, sz]
);
addDimensionLine(
[
[sx + half, y - 0.06, sz],
[sx + half, y + 0.06, sz]
],
"",
[sx, y - 1, sz]
);
}
if (state.category === "knobs") {
const half = target.collarDia / 200;
const y = sy - 0.52;
addDimensionLine(
[
[sx - half, y, sz],
[sx + half, y, sz]
],
`Ø${target.collarDia} mm`,
[sx, y - 0.16, sz]
);
}
if (state.category === "hinges") {
const half = target.holePitch / 200;
const x = sx + 0.58;
addDimensionLine(
[
[x, sy - half, sz],
[x, sy + half, sz]
],
`${target.holePitch} mm`,
[x + 0.08, sy, sz]
);
}
scene.add(measurementGroup);
}
function updateStationAppearance() {
for (const station of stations) {
const selected = station.category === state.category;
station.plateMaterial.color.setHex(selected ? 0xf0eee0 : 0xb8c0b7);
station.edgeMaterial.color.setHex(selected ? 0x5e8b31 : 0x78847b);
station.edgeMaterial.opacity = selected ? 0.95 : 0.3;
station.ghostMaterial.opacity = selected ? 0.32 : 0.11;
station.ghost.visible = true;
}
}
function disposeObject(object) {
object.traverse((child) => {
if (child.geometry) child.geometry.dispose();
});
object.removeFromParent();
}
function clearPreview() {
if (!preview) return;
disposeObject(preview);
preview = null;
}
function createPreview() {
clearPreview();
const part = catalog[state.category].parts.find(
(item) => item.id === state.selectedPart
);
if (!part) return;
preview = buildPartModel(
state.category,
part.dims,
finishMaterials[part.finish] || materials.steel,
materials.dark
);
preview.position.set(0, 1.73, 0.58);
preview.rotation.y = -0.25;
scene.add(preview);
}
function updateSteps() {
const step1 = $("#step-1");
const step2 = $("#step-2");
const step3 = $("#step-3");
step1.className = state.scanned ? "step complete" : "step active";
step2.className = state.selectedPart ? "step complete" : state.scanned ? "step active" : "step";
step3.className = state.testing ? "step active" : state.fitResult ? "step complete" : "step";
}
function renderParts() {
const list = $("#part-list");
const data = catalog[state.category];
$("#stock-count").textContent = `${data.parts.length} SKUs`;
list.innerHTML = data.parts.map((part) => {
let dimensions = "";
if (state.category === "handles") {
dimensions = `${part.dims.holeSpacing} mm pitch<br>Ø${part.dims.holeDia} mm holes`;
} else if (state.category === "knobs") {
dimensions = `Ø${part.dims.bore} mm bore<br>${part.dims.collarDia} mm collar`;
} else {
dimensions = `${part.dims.holePitch} mm pitch<br>Ø${part.dims.barrelDia} mm barrel`;
}
const selected = part.id === state.selectedPart;
return `
<button
class="part-card ${selected ? "selected" : ""}"
data-part="${part.id}"
type="button"
${state.testing ? "disabled" : ""}
aria-pressed="${selected}"
>
<div class="part-top">
<div class="part-glyph">${data.icon}</div>
<div>
<div class="part-name">${part.name}</div>
<div class="part-code">${part.id}</div>
</div>
<div class="part-finish">${part.finish}</div>
</div>
<div class="part-dimensions">${dimensions}</div>
</button>
`;
}).join("");
$$(".part-card").forEach((button) => {
button.addEventListener("click", () => {
selectPart(button.dataset.part);
});
});
}
function renderTarget() {
const data = catalog[state.category];
$("#inspector-title").textContent = data.label;
$("#station-title").textContent = data.station;
$("#spec-list").innerHTML = data.specs.map(([name, key, unit, tolerance]) => {
const value = state.scanned ? `${data.target[key]} ${unit}` : "—";
const hiddenClass = state.scanned ? "" : "hidden-value";
return `
<div class="spec-row">
<span class="spec-name">${name}</span>
<span>
<span class="spec-value ${hiddenClass}">${value}</span>
${state.scanned ? `<div class="spec-name" style="margin-top:3px;font-size:8px">${tolerance}</div>` : ""}
</span>
</div>
`;
}).join("");
const status = $("#measurement-status");
status.textContent = state.scanning
? "Scanning"
: state.scanned
? "Mapped"
: "Not scanned";
status.classList.toggle("ready", state.scanned);
$("#scan-button").textContent = state.scanning
? "Scanning mount…"
: state.scanned
? "Re-scan mount geometry"
: "Scan mount geometry";
$("#scan-button").disabled = state.scanning || state.testing;
$("#test-button").disabled = !state.scanned || !state.selectedPart || state.testing;
$("#test-button").textContent = state.testing
? "Testing fit…"
: "Test geometric fit";
updateSteps();
}
function renderResult() {
const result = $("#result");
const title = $("#result-title");
const text = $("#result-text");
result.className = "result";
if (state.fitResult?.ok) {
result.classList.add("success");
title.textContent = "Geometric fit confirmed";
text.innerHTML = `
${state.fitResult.partName} is compatible with the measured
${catalog[state.category].label.toLowerCase()}. The part has been
seated on the mount.
`;
} else if (state.fitResult && !state.fitResult.ok) {
result.classList.add("reject");
title.textContent = "Fit rejected — geometry differs";
text.innerHTML = `
The replacement cannot seat correctly.
<ul>${state.fitResult.reasons.map((reason) => `<li>${reason}</li>`).join("")}</ul>
`;
} else if (state.scanning) {
title.textContent = "Reading mount geometry";
text.textContent = "The scanner is measuring hole positions, bore size, and mounting depth.";
} else if (state.scanned) {
title.textContent = "Mount mapped";
text.textContent = state.selectedPart
? "A replacement is selected. Run the fit test to verify every mating dimension."
: "Choose a replacement from the inventory, then run the fit test.";
} else {
title.textContent = "Awaiting mount scan";
text.textContent = "Measure the active station before selecting a replacement.";
}
}
function selectCategory(category) {
if (state.testing || state.scanning) return;
if (category === state.category) return;
state.category = category;
state.selectedPart = null;
state.scanned = false;
state.fitResult = null;
state.test = null;
clearPreview();
clearMeasurement();
$$(".category-tab").forEach((button) => {
button.classList.toggle("active", button.dataset.category === category);
});
updateStationAppearance();
renderParts();
renderTarget();
renderResult();
}
function selectPart(partId) {
if (state.testing || state.scanning) return;
state.selectedPart = partId;
state.fitResult = null;
createPreview();
renderParts();
renderTarget();
renderResult();
}
function evaluateFit(category, part) {
const target = catalog[category].target;
const dims = part.dims;
const reasons = [];
if (category === "handles") {
if (Math.abs(dims.holeSpacing - target.holeSpacing) > 2) {
reasons.push(
`Hole pitch is ${dims.holeSpacing} mm; mount requires ${target.holeSpacing} mm (±2 mm).`
);
}
if (Math.abs(dims.holeDia - target.holeDia) > 0.5) {
reasons.push(
`Hole diameter is Ø${dims.holeDia} mm; mount accepts Ø${target.holeDia} mm.`
);
}
}
if (category === "knobs") {
if (Math.abs(dims.bore - target.bore) > 0.5) {
reasons.push(
`Bore is Ø${dims.bore} mm; shaft requires Ø${target.bore} mm (±0.5 mm).`
);
}
if (Math.abs(dims.collarDia - target.collarDia) > 2) {
reasons.push(
`Base collar is Ø${dims.collarDia} mm; mount is Ø${target.collarDia} mm.`
);
}
if (Math.abs(dims.shaftDepth - target.shaftDepth) > 1) {
reasons.push(
`Shaft depth is ${dims.shaftDepth} mm; mount accepts ${target.shaftDepth} mm.`
);
}
}
if (category === "hinges") {
if (Math.abs(dims.holePitch - target.holePitch) > 1) {
reasons.push(
`Hole pitch is ${dims.holePitch} mm; mount requires ${target.holePitch} mm (±1 mm).`
);
}
if (Math.abs(dims.barrelDia - target.barrelDia) > 1) {
reasons.push(
`Barrel is Ø${dims.barrelDia} mm; mount accepts Ø${target.barrelDia} mm.`
);
}
if (Math.abs(dims.leafWidth - target.leafWidth) > 2) {
reasons.push(
`Leaf width is ${dims.leafWidth} mm; mount accepts ${target.leafWidth} mm.`
);
}
}
return {
ok: reasons.length === 0,
reasons,
partName: part.name
};
}
function showToast(message) {
const toast = $("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(showToast.timeout);
showToast.timeout = setTimeout(() => toast.classList.remove("show"), 2200);
}
function flashResult(ok) {
const flash = $("#stage-flash");
flash.className = "stage-flash";
void flash.offsetWidth;
flash.classList.add(ok ? "success" : "reject");
}
function startScan() {
if (state.scanning || state.testing) return;
state.scanning = true;
state.scanStart = performance.now();
scannerBeam.visible = true;
renderTarget();
renderResult();
updateSteps();
}
function startFitTest() {
if (!state.scanned || !state.selectedPart || state.testing || !preview) return;
const part = catalog[state.category].parts.find(
(item) => item.id === state.selectedPart
);
const evaluation = evaluateFit(state.category, part);
const station = stations.find((item) => item.category === state.category);
state.testing = true;
state.fitResult = evaluation;
state.test = {
phase: "approach",
start: performance.now(),
from: preview.position.clone(),
fromRotation: preview.rotation.clone(),
destination: station.group.position.clone().add(new THREE.Vector3(0, -0.06, 0.11)),
evaluation
};
renderParts();
renderTarget();
renderResult();
updateSteps();
}
function closestPart() {
const data = catalog[state.category];
const target = data.target;
return [...data.parts].sort((a, b) => {
const score = (part) => {
let total = 0;
for (const key of Object.keys(target)) {
const tolerance = key === "bore" || key === "holeDia"
? 0.5
: key === "holeSpacing" || key === "holePitch"
? 1.5
: 1;
total += Math.abs(part.dims[key] - target[key]) / tolerance;
}
return total;
};
return score(a) - score(b);
})[0];
}
// UI events.
$$(".category-tab").forEach((button) => {
button.addEventListener("click", () => selectCategory(button.dataset.category));
});
$("#scan-button").addEventListener("click", startScan);
$("#test-button").addEventListener("click", startFitTest);
$("#reset-camera").addEventListener("click", resetCamera);
$("#suggest-button").addEventListener("click", () => {
if (state.testing || state.scanning) return;
const part = closestPart();
selectPart(part.id);
showToast(`${part.id} selected as the closest geometric match`);
});
// Direct camera controls implemented without Three.js addons.
const pointer = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
let drag = 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;
}
function categoryAtPointer(event) {
setPointer(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(stations.map((station) => station.group), true);
if (!hits.length) return null;
let object = hits[0].object;
while (object && !object.userData.category) {
object = object.parent;
}
return object?.userData?.category || null;
}
canvas.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
canvas.setPointerCapture(event.pointerId);
drag = {
x: event.clientX,
y: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false
};
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (drag) {
const dx = event.clientX - drag.lastX;
const dy = event.clientY - drag.lastY;
if (Math.abs(event.clientX - drag.x) + Math.abs(event.clientY - drag.y) > 4) {
drag.moved = true;
}
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.005, 0.5, 1.45);
drag.lastX = event.clientX;
drag.lastY = event.clientY;
updateCamera();
} else {
const category = categoryAtPointer(event);
canvas.style.cursor = category ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
const wasDrag = drag?.moved;
drag = null;
canvas.classList.remove("dragging");
if (!wasDrag) {
const category = categoryAtPointer(event);
if (category) selectCategory(category);
}
});
canvas.addEventListener("pointercancel", () => {
drag = null;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius + event.deltaY * 0.008,
5.2,
13
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
let handled = true;
switch (event.key) {
case "ArrowLeft":
orbit.theta -= 0.1;
break;
case "ArrowRight":
orbit.theta += 0.1;
break;
case "ArrowUp":
orbit.phi = THREE.MathUtils.clamp(orbit.phi - 0.08, 0.5, 1.45);
break;
case "ArrowDown":
orbit.phi = THREE.MathUtils.clamp(orbit.phi + 0.08, 0.5, 1.45);
break;
case "+":
case "=":
orbit.radius = Math.max(5.2, orbit.radius - 0.4);
break;
case "-":
orbit.radius = Math.min(13, orbit.radius + 0.4);
break;
case "r":
case "R":
resetCamera();
break;
default:
handled = false;
}
if (handled) {
event.preventDefault();
updateCamera();
}
});
// Responsive renderer sizing.
const resizeObserver = new ResizeObserver(() => {
const width = stage.clientWidth;
const height = stage.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
resizeObserver.observe(stage);
// Animation and state transitions.
function easeOutCubic(value) {
return 1 - Math.pow(1 - value, 3);
}
function updateScanner(now) {
if (!state.scanning) return;
const progress = THREE.MathUtils.clamp((now - state.scanStart) / 1250, 0, 1);
scannerBeam.position.x = -2.7 + progress * 5.4;
scannerMaterial.opacity = 0.45 + Math.sin(progress * Math.PI) * 0.45;
if (progress >= 1) {
state.scanning = false;
state.scanned = true;
scannerBeam.visible = false;
buildMeasurementOverlay();
renderTarget();
renderResult();
showToast("Mount geometry mapped");
}
}
function updateFitTest(now) {
if (!state.test || !preview) return;
const test = state.test;
if (test.phase === "approach") {
const progress = THREE.MathUtils.clamp((now - test.start) / 720, 0, 1);
const eased = easeOutCubic(progress);
preview.position.lerpVectors(test.from, test.destination, eased);
preview.position.y += Math.sin(progress * Math.PI) * 0.46;
preview.rotation.y = THREE.MathUtils.lerp(
test.fromRotation.y,
0,
eased
);
if (progress >= 1) {
if (test.evaluation.ok) {
state.test = null;
state.testing = false;
state.fitResult = test.evaluation;
preview.position.copy(test.destination);
preview.rotation.set(0, 0, 0);
flashResult(true);
renderParts();
renderTarget();
renderResult();
showToast("Fit confirmed — geometry compatible");
} else {
test.phase = "reject";
test.start = now;
test.rejectFrom = preview.position.clone();
flashResult(false);
}
}
}
if (test.phase === "reject") {
const progress = THREE.MathUtils.clamp((now - test.start) / 700, 0, 1);
const eased = easeOutCubic(progress);
preview.position.lerpVectors(test.rejectFrom, test.from, eased);
preview.rotation.z = Math.sin(progress * 13) * 0.09 * (1 - progress);
preview.rotation.y = THREE.MathUtils.lerp(
test.fromRotation.y,
-0.45,
eased
);
if (progress >= 1) {
preview.position.copy(test.from);
preview.rotation.copy(test.fromRotation);
state.test = null;
state.testing = false;
state.fitResult = test.evaluation;
renderParts();
renderTarget();
renderResult();
showToast("Replacement rejected — check measured geometry");
}
}
}
function animate(now) {
requestAnimationFrame(animate);
updateScanner(now);
updateFitTest(now);
renderer.render(scene, camera);
}
// Initial UI.
updateStationAppearance();
renderParts();
renderTarget();
renderResult();
requestAnimationFrame(animate);
requestAnimationFrame(() => {
$("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```