Prompt specification
Build or customize the Three.js 3D scene "Peg and Hole With Visible Clearance" 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: Peg and Hole With Visible Clearance
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/peg-and-hole-with-visible-clearance-x2033
Scene Description Prompt:
A stylized peg and cutaway hole with labeled diameters, distinguishing clearance from interference before insertion. Three.js from cdnjs, one HTML file.
Working Three.js Starter (peg-and-hole-with-visible-clearance-x2033):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FitLab — Diameter Fit Before Insertion</title>
<style>
:root {
--bg: #071116;
--panel: rgba(9, 24, 30, 0.84);
--line: rgba(145, 216, 220, 0.16);
--text: #e8f6f4;
--muted: #8fa8aa;
--cyan: #72e1df;
--accent: #70e0b7;
--accent-soft: rgba(112, 224, 183, 0.12);
--warning: #ff7272;
--warning-soft: rgba(255, 114, 114, 0.12);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.topbar {
position: fixed;
z-index: 5;
top: 24px;
left: 30px;
right: 30px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
font-size: 13px;
font-weight: 800;
letter-spacing: 0.16em;
}
.brand-mark {
position: relative;
width: 28px;
height: 28px;
border: 1px solid rgba(114, 225, 223, 0.55);
border-radius: 50%;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
background: var(--cyan);
}
.brand-mark::before {
width: 12px;
height: 1px;
left: 7px;
top: 13px;
}
.brand-mark::after {
width: 1px;
height: 12px;
left: 13px;
top: 7px;
}
.brand small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 8px;
font-weight: 650;
letter-spacing: 0.18em;
}
.live {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 17, 22, 0.54);
color: #a9c1c2;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.15em;
backdrop-filter: blur(12px);
}
.live i {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
animation: pulse 2.2s ease-in-out infinite;
}
@keyframes pulse {
50% {
opacity: 0.35;
}
}
.intro {
position: fixed;
z-index: 3;
top: 104px;
left: 40px;
width: min(360px, 32vw);
pointer-events: none;
}
.kicker {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 15px;
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.18em;
}
.kicker::before {
content: "";
width: 24px;
height: 1px;
background: var(--cyan);
}
h1 {
margin: 0;
font-size: clamp(35px, 4vw, 61px);
line-height: 0.98;
font-weight: 640;
letter-spacing: -0.055em;
}
h1 em {
color: var(--cyan);
font-family: Georgia, "Times New Roman", serif;
font-weight: 400;
}
.intro p {
max-width: 330px;
margin: 20px 0 0;
color: #9bb0b1;
font-size: 12px;
line-height: 1.65;
}
.panel {
position: fixed;
z-index: 6;
top: 82px;
right: 26px;
width: 326px;
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
background:
linear-gradient(145deg, rgba(24, 52, 59, 0.25), transparent 45%),
var(--panel);
box-shadow:
0 24px 80px rgba(0, 0, 0, 0.3),
inset 0 1px rgba(255, 255, 255, 0.025);
backdrop-filter: blur(20px) saturate(125%);
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
color: #96afb0;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
}
.code {
padding: 5px 7px;
border: 1px solid var(--line);
border-radius: 5px;
color: var(--cyan);
letter-spacing: 0.1em;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border: 1px solid rgba(150, 200, 200, 0.08);
border-radius: 11px;
background: rgba(0, 0, 0, 0.18);
}
.mode {
position: relative;
min-height: 42px;
border: 0;
border-radius: 8px;
background: transparent;
color: #839b9c;
cursor: pointer;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
transition: 180ms ease;
}
.mode:hover {
color: #d6eeee;
background: rgba(255, 255, 255, 0.025);
}
.mode.active {
color: #071116;
background: var(--accent);
box-shadow: 0 5px 18px var(--accent-soft);
}
.mode[data-mode="interference"].active {
background: var(--warning);
box-shadow: 0 5px 18px var(--warning-soft);
}
.outcome {
position: relative;
overflow: hidden;
margin-top: 15px;
padding: 16px;
border: 1px solid rgba(112, 224, 183, 0.22);
border-radius: 12px;
background: var(--accent-soft);
}
.outcome::after {
content: "";
position: absolute;
right: -28px;
top: -40px;
width: 110px;
height: 110px;
border: 1px solid var(--accent);
border-radius: 50%;
opacity: 0.12;
}
.outcome-label {
margin-bottom: 8px;
color: var(--accent);
font-size: 10px;
font-weight: 900;
letter-spacing: 0.17em;
}
.outcome strong {
display: block;
margin-bottom: 5px;
font-size: 21px;
letter-spacing: -0.03em;
}
.outcome span:last-child {
color: #9eb3b4;
font-size: 10px;
line-height: 1.45;
}
.metrics {
margin: 15px 0 0;
}
.metric {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 39px;
border-bottom: 1px solid rgba(140, 190, 190, 0.09);
color: #8fa6a7;
font-size: 10px;
}
.metric span:last-child {
color: #e3f1ef;
font-variant-numeric: tabular-nums;
font-weight: 720;
}
.metric.accent span:last-child {
color: var(--accent);
}
.buttons {
display: grid;
grid-template-columns: 1fr 38px;
gap: 8px;
margin-top: 17px;
}
.run {
display: flex;
align-items: center;
justify-content: space-between;
min-height: 44px;
padding: 0 14px;
border: 0;
border-radius: 10px;
background: var(--accent);
color: #071116;
cursor: pointer;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.1em;
transition:
transform 150ms ease,
filter 150ms ease;
}
.run:hover {
transform: translateY(-1px);
filter: brightness(1.08);
}
.run:active {
transform: translateY(0);
}
.run svg {
width: 17px;
height: 17px;
}
.reset {
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
color: #9bb0b1;
cursor: pointer;
font-size: 17px;
transition: 150ms ease;
}
.reset:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.06);
}
.progress {
height: 3px;
margin-top: 11px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.06);
}
.progress-fill {
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--accent);
transform: scaleX(0);
transform-origin: left;
}
.note {
min-height: 43px;
margin: 13px 1px 0;
color: #82999a;
font-size: 10px;
line-height: 1.55;
}
.legend {
position: fixed;
z-index: 3;
left: 40px;
bottom: 33px;
display: flex;
gap: 18px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #81999a;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.swatch {
width: 16px;
height: 3px;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 9px rgba(114, 225, 223, 0.25);
}
.swatch.peg {
background: var(--accent);
}
.hint {
position: fixed;
z-index: 3;
right: 28px;
bottom: 26px;
color: rgba(151, 175, 176, 0.58);
font-size: 9px;
letter-spacing: 0.05em;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
transition: opacity 500ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
.loader {
width: 42px;
height: 42px;
border: 1px solid rgba(114, 225, 223, 0.2);
border-top-color: var(--cyan);
border-radius: 50%;
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 900px) {
.intro {
left: 26px;
width: 300px;
}
.intro p {
display: none;
}
.panel {
right: 18px;
width: 306px;
}
.legend {
left: 26px;
}
.hint {
display: none;
}
}
@media (max-width: 680px) {
.topbar {
top: 17px;
left: 18px;
right: 18px;
}
.live {
padding: 7px 9px;
font-size: 8px;
}
.intro {
top: 70px;
left: 20px;
width: calc(100% - 40px);
}
.kicker {
margin-bottom: 10px;
}
h1 {
max-width: 270px;
font-size: 36px;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 15px;
border-radius: 15px;
}
.panel-head,
.metrics,
.note,
.progress {
display: none;
}
.segmented {
margin-bottom: 10px;
}
.outcome {
margin: 0;
padding: 10px 12px;
}
.outcome-label {
margin-bottom: 4px;
font-size: 8px;
}
.outcome strong {
margin: 0;
font-size: 16px;
}
.outcome span:last-child {
display: none;
}
.buttons {
margin-top: 9px;
}
.run {
min-height: 38px;
}
.reset {
min-height: 38px;
}
.legend {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D cutaway showing peg and hole fit"></canvas>
<header class="topbar">
<div class="brand">
<span class="brand-mark"></span>
<div>
FITLAB
<small>ENGINEERING VISUALS</small>
</div>
</div>
<div class="live">
<i></i>
LIVE CUTAWAY
</div>
</header>
<section class="intro">
<div class="kicker">ASSEMBLY STUDY / 01</div>
<h1>Diameter fit<br /><em>before insertion.</em></h1>
<p>
A sectioned guide reveals whether the peg can pass—or binds before it
reaches the bore.
</p>
</section>
<aside class="panel" id="panel">
<div class="panel-head">
<span>FIT CONDITION</span>
<span class="code">Ø50 BORE</span>
</div>
<div class="segmented" role="group" aria-label="Fit condition">
<button
class="mode active"
data-mode="clearance"
aria-pressed="true"
>
CLEARANCE
</button>
<button
class="mode"
data-mode="interference"
aria-pressed="false"
>
INTERFERENCE
</button>
</div>
<div class="outcome">
<div class="outcome-label" id="outcomeLabel">CLEARANCE FIT</div>
<strong id="outcomeTitle">Room to pass</strong>
<span id="outcomeCopy">
The peg remains smaller than the guide on every side.
</span>
</div>
<div class="metrics">
<div class="metric">
<span>GUIDE BORE</span>
<span>Ø 50.00 mm</span>
</div>
<div class="metric accent">
<span>TEST PEG</span>
<span id="pegValue">Ø 48.00 mm</span>
</div>
<div class="metric">
<span>DIAMETER Δ</span>
<span id="differenceValue">+2.00 mm</span>
</div>
<div class="metric accent">
<span id="fitTypeLabel">RADIAL CLEARANCE</span>
<span id="radialValue">+1.00 mm</span>
</div>
</div>
<div class="buttons">
<button class="run" id="runButton">
<span>RUN FIT TEST</span>
<svg viewBox="0 0 24 24" fill="none" aria-hidden="true">
<path
d="M5 12h13m-5-5 5 5-5 5"
stroke="currentColor"
stroke-width="1.8"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</button>
<button class="reset" id="resetButton" title="Reset fit test" aria-label="Reset fit test">
↺
</button>
</div>
<div class="progress">
<div class="progress-fill" id="progressFill"></div>
</div>
<p class="note" id="note">
A 1.00 mm radial gap exists on every side. The cylindrical shoulder
can travel through the guide.
</p>
</aside>
<div class="legend">
<div class="legend-item">
<span class="swatch"></span>
Sectioned guide
</div>
<div class="legend-item">
<span class="swatch peg"></span>
Test peg
</div>
</div>
<div class="hint">
DRAG TO ORBIT · WHEEL TO ZOOM · R TO RESET VIEW
</div>
<div id="loading"><div class="loader"></div></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const panel = document.querySelector("#panel");
const modeButtons = [...document.querySelectorAll(".mode")];
const runButton = document.querySelector("#runButton");
const runButtonText = runButton.querySelector("span");
const resetButton = document.querySelector("#resetButton");
const progressFill = document.querySelector("#progressFill");
const outcomeLabel = document.querySelector("#outcomeLabel");
const outcomeTitle = document.querySelector("#outcomeTitle");
const outcomeCopy = document.querySelector("#outcomeCopy");
const pegValue = document.querySelector("#pegValue");
const differenceValue = document.querySelector("#differenceValue");
const radialValue = document.querySelector("#radialValue");
const fitTypeLabel = document.querySelector("#fitTypeLabel");
const note = document.querySelector("#note");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.Fog(0x071116, 11, 25);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
// Custom camera controls; no addon modules are used.
const cameraTarget = new THREE.Vector3(-0.25, 0.65, 0);
const defaultCamera = {
yaw: 0.69,
pitch: 0.68,
radius: 9.6
};
const cameraState = { ...defaultCamera };
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const { yaw, pitch, radius } = cameraState;
camera.position.set(
cameraTarget.x + radius * Math.sin(pitch) * Math.sin(yaw),
cameraTarget.y + radius * Math.cos(pitch),
cameraTarget.z + radius * Math.sin(pitch) * Math.cos(yaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
Object.assign(cameraState, defaultCamera);
updateCamera();
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.25,
1.28
);
updateCamera();
});
const stopDragging = () => {
dragging = false;
};
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener("lostpointercapture", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.008,
6.5,
14
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
// Lighting.
scene.add(new THREE.HemisphereLight(0x9ee9ef, 0x071014, 2.25));
const keyLight = new THREE.DirectionalLight(0xf2ffff, 3.2);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 20;
keyLight.shadow.bias = -0.0002;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x4be5df, 28, 12, 2);
cyanLight.position.set(-3.8, 2.4, 3.2);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffb66f, 18, 10, 2);
warmLight.position.set(4, 1.5, -2.8);
scene.add(warmLight);
const boreLight = new THREE.PointLight(0x45d9dd, 10, 6, 2);
boreLight.position.set(0, -0.8, 0);
scene.add(boreLight);
// Materials.
const topMaterial = new THREE.MeshStandardMaterial({
color: 0x1a4651,
metalness: 0.58,
roughness: 0.48
});
const sideMaterial = new THREE.MeshStandardMaterial({
color: 0x0c2832,
metalness: 0.7,
roughness: 0.43
});
const edgeMaterial = new THREE.MeshBasicMaterial({
color: 0x72e1df,
transparent: true,
opacity: 0.86
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x14252a,
metalness: 0.8,
roughness: 0.32
});
const pegMaterial = new THREE.MeshPhysicalMaterial({
color: 0x70e0b7,
emissive: 0x0b3c31,
emissiveIntensity: 0.22,
metalness: 0.64,
roughness: 0.24,
clearcoat: 0.38,
clearcoatRoughness: 0.22
});
const bandMaterial = new THREE.MeshStandardMaterial({
color: 0xe8fff7,
emissive: 0x1f8a70,
emissiveIntensity: 0.55,
metalness: 0.48,
roughness: 0.22
});
// Floor and grid.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(34, 34),
new THREE.MeshStandardMaterial({
color: 0x071217,
metalness: 0.12,
roughness: 0.92
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.47;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(22, 44, 0x24535d, 0x102c33);
grid.position.y = -1.445;
grid.material.transparent = true;
grid.material.opacity = 0.33;
scene.add(grid);
// Sectioned annular housing.
const holeRadius = 1.25; // 25 mm radius = 50 mm diameter
const outerRadius = 2.72;
const housingHeight = 1.4;
const startAngle = THREE.MathUtils.degToRad(80);
const endAngle = THREE.MathUtils.degToRad(370);
const housingShape = new THREE.Shape();
housingShape.moveTo(
Math.cos(startAngle) * outerRadius,
Math.sin(startAngle) * outerRadius
);
housingShape.absarc(
0,
0,
outerRadius,
startAngle,
endAngle,
false
);
housingShape.lineTo(
Math.cos(endAngle) * holeRadius,
Math.sin(endAngle) * holeRadius
);
housingShape.absarc(
0,
0,
holeRadius,
endAngle,
startAngle,
true
);
housingShape.closePath();
const housingGeometry = new THREE.ExtrudeGeometry(housingShape, {
depth: housingHeight,
steps: 1,
curveSegments: 128,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: 0.035,
bevelThickness: 0.035
});
housingGeometry.rotateX(Math.PI / 2);
housingGeometry.computeVertexNormals();
const housing = new THREE.Mesh(
housingGeometry,
[topMaterial, sideMaterial]
);
housing.castShadow = true;
housing.receiveShadow = true;
scene.add(housing);
// Technical hatching for the two exposed section faces.
const hatchCanvas = document.createElement("canvas");
hatchCanvas.width = hatchCanvas.height = 128;
const hatchContext = hatchCanvas.getContext("2d");
hatchContext.fillStyle = "#8fd7d0";
hatchContext.fillRect(0, 0, 128, 128);
hatchContext.strokeStyle = "#356d70";
hatchContext.lineWidth = 5;
for (let i = -128; i < 256; i += 22) {
hatchContext.beginPath();
hatchContext.moveTo(i, 0);
hatchContext.lineTo(i + 128, 128);
hatchContext.stroke();
}
const hatchTexture = new THREE.CanvasTexture(hatchCanvas);
hatchTexture.wrapS = THREE.RepeatWrapping;
hatchTexture.wrapT = THREE.RepeatWrapping;
hatchTexture.repeat.set(3, 2);
hatchTexture.colorSpace = THREE.SRGBColorSpace;
const sectionMaterial = new THREE.MeshStandardMaterial({
map: hatchTexture,
color: 0xffffff,
metalness: 0.12,
roughness: 0.72,
side: THREE.DoubleSide
});
function makeSectionFace(angle) {
const c = Math.cos(angle);
const s = Math.sin(angle);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(
[
holeRadius * c, -housingHeight, holeRadius * s,
outerRadius * c, -housingHeight, outerRadius * s,
holeRadius * c, 0.025, holeRadius * s,
outerRadius * c, 0.025, outerRadius * s
],
3
)
);
geometry.setAttribute(
"uv",
new THREE.Float32BufferAttribute([0, 0, 1, 0, 0, 1, 1, 1], 2)
);
geometry.setIndex([0, 1, 2, 2, 1, 3]);
geometry.computeVertexNormals();
const face = new THREE.Mesh(geometry, sectionMaterial);
face.castShadow = true;
face.receiveShadow = true;
return face;
}
scene.add(makeSectionFace(startAngle));
scene.add(makeSectionFace(endAngle));
function makeArcLine(radius, y, color, opacity = 0.85) {
const points = [];
for (let i = 0; i <= 160; i++) {
const angle = THREE.MathUtils.lerp(startAngle, endAngle, i / 160);
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
y,
Math.sin(angle) * radius
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
});
return new THREE.Line(geometry, material);
}
scene.add(makeArcLine(holeRadius, 0.055, 0x82fff0, 0.95));
scene.add(makeArcLine(outerRadius, 0.055, 0x4f9ba1, 0.65));
// Small fasteners around the guide.
const fastenerGeometry = new THREE.CylinderGeometry(0.095, 0.095, 0.085, 24);
const fastenerSlotGeometry = new THREE.BoxGeometry(0.12, 0.012, 0.024);
[125, 160, 200, 235, 270, 305, 340].forEach((degrees) => {
const angle = THREE.MathUtils.degToRad(degrees);
const x = Math.cos(angle) * 2.08;
const z = Math.sin(angle) * 2.08;
const fastener = new THREE.Mesh(fastenerGeometry, darkMetal);
fastener.position.set(x, 0.09, z);
fastener.castShadow = true;
scene.add(fastener);
const slot = new THREE.Mesh(fastenerSlotGeometry, edgeMaterial);
slot.position.set(x, 0.137, z);
slot.rotation.y = angle;
scene.add(slot);
});
// Center axis.
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -2.25, 0),
new THREE.Vector3(0, 4.65, 0)
]);
const axis = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x91b7bb,
dashSize: 0.08,
gapSize: 0.07,
transparent: true,
opacity: 0.32,
depthTest: false
})
);
axis.computeLineDistances();
axis.renderOrder = 4;
scene.add(axis);
// Canvas-backed dimensional labels.
function roundedRect(context, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + width, y, x + width, y + height, r);
context.arcTo(x + width, y + height, x, y + height, r);
context.arcTo(x, y + height, x, y, r);
context.arcTo(x, y, x + width, y, r);
context.closePath();
}
function makeLabel(primary, secondary, accent = "#72e1df") {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 640;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(2.8, 0.7, 1);
sprite.renderOrder = 30;
function update(nextPrimary, nextSecondary, nextAccent = accent) {
context.clearRect(0, 0, 640, 160);
context.shadowColor = "rgba(0, 0, 0, 0.35)";
context.shadowBlur = 18;
context.shadowOffsetY = 6;
roundedRect(context, 10, 12, 620, 136, 24);
context.fillStyle = "rgba(6, 19, 24, 0.93)";
context.fill();
context.shadowColor = "transparent";
context.lineWidth = 3;
context.strokeStyle = nextAccent;
context.globalAlpha = 0.72;
context.stroke();
context.globalAlpha = 1;
context.fillStyle = nextAccent;
context.fillRect(30, 34, 5, 91);
context.textAlign = "left";
context.textBaseline = "middle";
context.fillStyle = "#f0fbf9";
context.font = "700 46px Inter, Arial, sans-serif";
context.fillText(nextPrimary, 55, 62);
context.fillStyle = "#93abad";
context.font = "700 20px Inter, Arial, sans-serif";
context.fillText(nextSecondary, 56, 113);
texture.needsUpdate = true;
}
update(primary, secondary, accent);
return { sprite, update };
}
function createDimension(color) {
const group = new THREE.Group();
const lineMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthTest: false
});
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-1, 0, 0),
new THREE.Vector3(1, 0, 0)
]);
const line = new THREE.Line(lineGeometry, lineMaterial);
line.scale.x = 1;
line.renderOrder = 25;
group.add(line);
const arrowMaterial = new THREE.MeshBasicMaterial({
color,
depthTest: false
});
const arrowGeometry = new THREE.ConeGeometry(0.075, 0.2, 16);
const leftArrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
const rightArrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
leftArrow.rotation.z = Math.PI / 2;
rightArrow.rotation.z = -Math.PI / 2;
leftArrow.renderOrder = 25;
rightArrow.renderOrder = 25;
group.add(leftArrow, rightArrow);
function setRadius(radius) {
line.scale.x = radius;
leftArrow.position.x = -radius + 0.1;
rightArrow.position.x = radius - 0.1;
}
function setColor(nextColor) {
lineMaterial.color.set(nextColor);
arrowMaterial.color.set(nextColor);
}
return { group, setRadius, setColor };
}
const holeDimension = createDimension(0x72e1df);
holeDimension.setRadius(holeRadius);
holeDimension.group.position.set(0, 0.15, 0);
scene.add(holeDimension.group);
const holeLabel = makeLabel(
"Ø 50.00 mm",
"SECTIONED GUIDE BORE",
"#72e1df"
);
holeLabel.sprite.position.set(0, 0.42, 1.95);
scene.add(holeLabel.sprite);
// Peg assembly.
const pegRoot = new THREE.Group();
const pegVisual = new THREE.Group();
pegRoot.add(pegVisual);
scene.add(pegRoot);
const pegBody = new THREE.Mesh(
new THREE.CylinderGeometry(1, 1, 3, 64),
pegMaterial
);
pegBody.position.y = 1.95;
pegBody.castShadow = true;
pegBody.receiveShadow = true;
pegVisual.add(pegBody);
const pegNose = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 1, 0.45, 64),
pegMaterial
);
pegNose.position.y = 0.225;
pegNose.castShadow = true;
pegNose.receiveShadow = true;
pegVisual.add(pegNose);
const pegDome = new THREE.Mesh(
new THREE.SphereGeometry(1, 64, 32, 0, Math.PI * 2, 0, Math.PI / 2),
pegMaterial
);
pegDome.position.y = 3.45;
pegDome.castShadow = true;
pegVisual.add(pegDome);
const bandGeometry = new THREE.TorusGeometry(1, 0.065, 12, 64);
bandGeometry.rotateX(Math.PI / 2);
[1.2, 2.8].forEach((height) => {
const band = new THREE.Mesh(bandGeometry, bandMaterial);
band.position.y = height;
band.castShadow = true;
pegVisual.add(band);
});
const pegDimension = createDimension(0x70e0b7);
pegDimension.group.position.set(0, 2.08, 1.28);
pegRoot.add(pegDimension.group);
const pegLabel = makeLabel(
"Ø 48.00 mm",
"TEST PEG",
"#70e0b7"
);
pegLabel.sprite.position.set(0, 0.37, 0.22);
pegDimension.group.add(pegLabel.sprite);
// Peg footprint projected onto the guide surface.
const fitRingMaterial = new THREE.MeshBasicMaterial({
color: 0x70e0b7,
transparent: true,
opacity: 0.92,
depthWrite: false
});
const fitRingGeometry = new THREE.TorusGeometry(1, 0.026, 10, 96);
fitRingGeometry.rotateX(Math.PI / 2);
const fitRing = new THREE.Mesh(fitRingGeometry, fitRingMaterial);
fitRing.position.y = 0.095;
scene.add(fitRing);
// Per-side fit callout.
const fitBadge = makeLabel(
"1.00 mm / side",
"RADIAL CLEARANCE",
"#70e0b7"
);
fitBadge.sprite.position.set(2.05, 0.62, 1.62);
scene.add(fitBadge.sprite);
const fitLeaderMaterial = new THREE.LineBasicMaterial({
color: 0x70e0b7,
transparent: true,
opacity: 0.6,
depthTest: false
});
const fitLeader = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0.88, 0.14, 0.88),
new THREE.Vector3(2.05, 0.62, 1.62)
]),
fitLeaderMaterial
);
fitLeader.renderOrder = 20;
scene.add(fitLeader);
// Fit simulation state.
const FIT = {
clearance: {
radius: 1.2,
pegDiameter: "48.00",
accent: "#70e0b7",
rgb: 0x70e0b7,
targetBottom: -2.25
},
interference: {
radius: 1.3,
pegDiameter: "52.00",
accent: "#ff7272",
rgb: 0xff7272,
targetBottom: -0.45
}
};
let currentMode = "clearance";
let targetPegRadius = FIT.clearance.radius;
let insertionProgress = 0;
let autoTesting = false;
const targetPegColor = new THREE.Color(FIT.clearance.rgb);
const targetBandColor = new THREE.Color(0xe8fff7);
function updatePanel() {
const clearance = currentMode === "clearance";
const accent = clearance ? "#70e0b7" : "#ff7272";
document.documentElement.style.setProperty("--accent", accent);
document.documentElement.style.setProperty(
"--accent-soft",
clearance ? "rgba(112, 224, 183, 0.12)" : "rgba(255, 114, 114, 0.12)"
);
panel.style.setProperty("--accent", accent);
panel.style.setProperty(
"--accent-soft",
clearance ? "rgba(112, 224, 183, 0.12)" : "rgba(255, 114, 114, 0.12)"
);
modeButtons.forEach((button) => {
const active = button.dataset.mode === currentMode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
pegValue.textContent = `Ø ${FIT[currentMode].pegDiameter} mm`;
differenceValue.textContent = clearance ? "+2.00 mm" : "−2.00 mm";
if (clearance) {
outcomeLabel.textContent = "CLEARANCE FIT";
outcomeTitle.textContent = "Room to pass";
outcomeCopy.textContent =
"The peg remains smaller than the guide on every side.";
fitTypeLabel.textContent = "RADIAL CLEARANCE";
radialValue.textContent = "+1.00 mm";
note.textContent =
"A 1.00 mm radial gap exists on every side. The cylindrical shoulder can travel through the guide.";
} else {
outcomeLabel.textContent = "INTERFERENCE FIT";
outcomeTitle.textContent = "Binding before travel";
outcomeCopy.textContent =
"The peg overlaps the guide on every side.";
fitTypeLabel.textContent = "RADIAL INTERFERENCE";
radialValue.textContent = "−1.00 mm";
note.textContent =
"The peg overlaps the guide by 1.00 mm per side. The cylindrical shoulder contacts the face and cannot pass.";
}
}
function setMode(mode) {
currentMode = mode;
const fit = FIT[mode];
targetPegRadius = fit.radius;
targetPegColor.setHex(fit.rgb);
pegVisual.scale.set(targetPegRadius, 1, targetPegRadius);
pegDimension.setRadius(fit.radius);
pegDimension.setColor(fit.rgb);
pegDimension.group.position.z = fit.radius + 0.08;
pegLabel.update(
`Ø ${fit.pegDiameter} mm`,
"TEST PEG",
fit.accent
);
fitRing.scale.set(fit.radius, 1, fit.radius);
fitRingMaterial.color.setHex(fit.rgb);
fitLeaderMaterial.color.setHex(fit.rgb);
fitBadge.update(
"1.00 mm / side",
mode === "clearance" ? "RADIAL CLEARANCE" : "RADIAL OVERLAP",
fit.accent
);
pegRoot.position.y = 0.7;
insertionProgress = 0;
autoTesting = false;
progressFill.style.transform = "scaleX(0)";
runButtonText.textContent = "RUN FIT TEST";
updatePanel();
}
function runTest() {
insertionProgress = 0;
autoTesting = true;
runButtonText.textContent = "TESTING…";
outcomeLabel.textContent = "TEST IN PROGRESS";
outcomeTitle.textContent = "Lowering the peg";
outcomeCopy.textContent =
"The guide is checking the cylindrical shoulder against the bore.";
progressFill.style.transform = "scaleX(0)";
}
function resetTest() {
insertionProgress = 0;
autoTesting = false;
pegRoot.position.y = 0.7;
progressFill.style.transform = "scaleX(0)";
runButtonText.textContent = "RUN FIT TEST";
updatePanel();
}
modeButtons.forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
runButton.addEventListener("click", runTest);
resetButton.addEventListener("click", resetTest);
window.addEventListener("resize", () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Smoothly morph the peg radius and color when switching modes.
const radiusEase = 1 - Math.exp(-dt * 8);
pegVisual.scale.x = THREE.MathUtils.lerp(
pegVisual.scale.x,
targetPegRadius,
radiusEase
);
pegVisual.scale.z = pegVisual.scale.x;
pegMaterial.color.lerp(targetPegColor, radiusEase);
pegMaterial.emissive.lerp(targetPegColor, radiusEase);
bandMaterial.color.lerp(targetBandColor, radiusEase);
if (autoTesting) {
insertionProgress = Math.min(1, insertionProgress + dt * 0.43);
const eased = 1 - Math.pow(1 - insertionProgress, 3);
const startBottom = 0.7;
const endBottom = FIT[currentMode].targetBottom;
pegRoot.position.y = THREE.MathUtils.lerp(
startBottom,
endBottom,
eased
);
progressFill.style.transform = `scaleX(${insertionProgress})`;
if (insertionProgress >= 1) {
autoTesting = false;
runButtonText.textContent = "REPLAY FIT TEST";
if (currentMode === "clearance") {
outcomeLabel.textContent = "CLEARANCE CONFIRMED";
outcomeTitle.textContent = "Full travel achieved";
outcomeCopy.textContent =
"The 48 mm peg passes through the 50 mm guide without contact.";
} else {
outcomeLabel.textContent = "INTERFERENCE CONFIRMED";
outcomeTitle.textContent = "Stopped at the shoulder";
outcomeCopy.textContent =
"The 52 mm peg overlaps the bore and binds at the guide face.";
}
}
}
// A very subtle inspection-light pulse.
cyanLight.intensity = 27 + Math.sin(elapsed * 1.35) * 2.2;
boreLight.intensity = 9.5 + Math.sin(elapsed * 1.1 + 1.3) * 1.4;
updateCamera();
renderer.render(scene, camera);
}
setMode("clearance");
updateCamera();
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```