Prompt specification
Build or customize the Three.js 3D scene "Adjust clearance and test assembly" 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: Adjust clearance and test assembly
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/adjust-clearance-and-test-assembly-x2204
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts wood joints and assembly tolerances. Let users vary a simple joint's clearance before inserting it. Highlight collision, valid fit, or excessive looseness using geometric checks, without claiming to model material deformation. Low-poly.
Working Three.js Starter (adjust-clearance-and-test-assembly-x2204):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wood Joint Tolerance Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #10161c;
--panel: rgba(18, 25, 31, 0.88);
--line: rgba(255, 255, 255, 0.11);
--muted: #91a0aa;
--text: #f4f0e8;
--wood: #c9894c;
--range-color: #55d69e;
}
* {
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;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.intro {
position: fixed;
top: 28px;
left: 30px;
z-index: 2;
pointer-events: none;
max-width: min(520px, calc(100vw - 430px));
text-shadow: 0 2px 20px rgba(0, 0, 0, 0.4);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: #d7ad7c;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 25px;
height: 2px;
background: #d7ad7c;
}
h1 {
margin: 9px 0 4px;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(38px, 5.2vw, 76px);
font-weight: 500;
line-height: 0.95;
letter-spacing: -0.045em;
}
.intro p {
margin: 12px 0 0;
max-width: 390px;
color: #bdc6cc;
font-size: 14px;
line-height: 1.55;
}
.panel {
position: fixed;
top: 24px;
right: 24px;
z-index: 3;
width: 354px;
max-height: calc(100vh - 48px);
overflow: auto;
padding: 20px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #52616b transparent;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 17px;
}
.panel-head strong {
font-size: 11px;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.icon-button {
width: 30px;
height: 30px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.04);
color: #c8d0d5;
cursor: pointer;
font-size: 17px;
}
.icon-button:hover {
background: rgba(255, 255, 255, 0.09);
color: white;
}
.section-label {
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.value-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
}
#clearanceValue {
font-variant-numeric: tabular-nums;
font-size: 29px;
font-weight: 750;
letter-spacing: -0.04em;
}
.unit-note {
color: var(--muted);
font-size: 12px;
text-align: right;
}
.range-wrap {
position: relative;
margin-top: 15px;
padding: 8px 0 0;
}
.range-zones {
position: absolute;
top: 9px;
left: 9px;
right: 9px;
height: 6px;
overflow: hidden;
border-radius: 99px;
background:
linear-gradient(
to right,
#f06459 0% 27.27%,
#55d69e 27.27% 34.55%,
#eeb75c 34.55% 100%
);
}
input[type="range"] {
position: relative;
display: block;
width: 100%;
height: 24px;
margin: 0;
appearance: none;
-webkit-appearance: none;
background: transparent;
cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
height: 6px;
background: transparent;
}
input[type="range"]::-moz-range-track {
height: 6px;
background: transparent;
}
input[type="range"]::-webkit-slider-thumb {
width: 19px;
height: 19px;
margin-top: -6px;
appearance: none;
-webkit-appearance: none;
border: 3px solid #10161c;
border-radius: 50%;
background: var(--range-color);
box-shadow: 0 0 0 1px var(--range-color), 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type="range"]::-moz-range-thumb {
width: 15px;
height: 15px;
border: 3px solid #10161c;
border-radius: 50%;
background: var(--range-color);
box-shadow: 0 0 0 1px var(--range-color), 0 2px 8px rgba(0, 0, 0, 0.5);
}
input[type="range"]:disabled {
cursor: not-allowed;
opacity: 0.42;
}
.range-labels {
display: flex;
justify-content: space-between;
margin-top: 4px;
color: #7f8c95;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.range-labels span:nth-child(1) { color: #ed776d; }
.range-labels span:nth-child(2) { color: #69d8a7; }
.range-labels span:nth-child(3) { color: #e7b76e; }
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 13px;
}
.preset {
padding: 8px 4px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: #b7c0c6;
font: inherit;
font-size: 10px;
cursor: pointer;
}
.preset:hover,
.preset.active {
border-color: rgba(255, 255, 255, 0.27);
background: rgba(255, 255, 255, 0.08);
color: white;
}
.divider {
height: 1px;
margin: 19px 0;
background: var(--line);
}
.status-card {
--state: #55d69e;
position: relative;
display: grid;
grid-template-columns: 38px 1fr;
gap: 11px;
padding: 13px;
overflow: hidden;
border: 1px solid color-mix(in srgb, var(--state) 42%, transparent);
border-radius: 12px;
background: color-mix(in srgb, var(--state) 9%, transparent);
}
.status-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--state);
}
.status-icon {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border: 1px solid color-mix(in srgb, var(--state) 50%, transparent);
border-radius: 50%;
color: var(--state);
font-size: 18px;
font-weight: 900;
}
.status-title {
margin: 1px 0 4px;
color: var(--state);
font-size: 13px;
font-weight: 800;
}
.status-copy {
margin: 0;
color: #aeb9c0;
font-size: 11px;
line-height: 1.48;
}
.status-card.pulse {
animation: status-pulse 0.55s ease;
}
@keyframes status-pulse {
50% {
transform: translateX(3px);
box-shadow: 0 0 24px color-mix(in srgb, var(--state) 25%, transparent);
}
}
.facts {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 11px;
}
.fact {
min-width: 0;
padding: 9px 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
}
.fact span {
display: block;
overflow: hidden;
color: #7f8c95;
font-size: 8px;
font-weight: 750;
letter-spacing: 0.07em;
text-overflow: ellipsis;
text-transform: uppercase;
white-space: nowrap;
}
.fact strong {
display: block;
margin-top: 5px;
color: #dfe5e8;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.depth-row {
display: flex;
justify-content: space-between;
margin: 15px 0 7px;
color: #aeb8be;
font-size: 10px;
}
.depth-row strong {
color: #eef2f4;
font-variant-numeric: tabular-nums;
}
.depth-track {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#depthFill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #b77c48, #e1ad74);
transition: width 0.08s linear;
}
.primary-button {
width: 100%;
margin-top: 17px;
padding: 12px 14px;
border: 0;
border-radius: 10px;
background: #d29559;
color: #20160e;
font: inherit;
font-size: 12px;
font-weight: 850;
cursor: pointer;
box-shadow: 0 7px 20px rgba(177, 113, 52, 0.18);
}
.primary-button:hover {
background: #e0a66d;
}
.primary-button:disabled {
cursor: wait;
opacity: 0.55;
}
.primary-button.secondary {
background: rgba(255, 255, 255, 0.08);
color: #e7ecef;
box-shadow: none;
}
.dimensions {
display: grid;
gap: 7px;
margin-top: 15px;
color: #91a0aa;
font-size: 10px;
}
.dimensions div {
display: flex;
justify-content: space-between;
gap: 15px;
}
.dimensions strong {
color: #d8dfe3;
font-variant-numeric: tabular-nums;
font-weight: 650;
}
.limit-note {
margin: 15px 0 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #77858e;
font-size: 9px;
line-height: 1.5;
}
.limit-note strong {
color: #aab5bb;
}
.camera-hint {
position: fixed;
left: 30px;
bottom: 23px;
z-index: 2;
display: flex;
align-items: center;
gap: 9px;
color: #84929b;
font-size: 10px;
letter-spacing: 0.04em;
pointer-events: none;
}
.camera-hint::before {
content: "";
width: 7px;
height: 7px;
border: 1px solid #9aa6ad;
border-radius: 50%;
}
.scene-tag {
position: fixed;
z-index: 1;
padding: 5px 7px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 6px;
background: rgba(12, 18, 23, 0.7);
color: #c7d0d5;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.13em;
pointer-events: none;
text-transform: uppercase;
transform: translate(-50%, -100%);
white-space: nowrap;
}
@media (max-width: 760px) {
.intro {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
.intro h1 {
font-size: 37px;
}
.intro p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 48vh;
padding: 15px;
border-radius: 14px;
}
.camera-hint,
.scene-tag {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="intro">
<div class="eyebrow">Assembly tolerance study</div>
<h1>Mortise & Tenon</h1>
<p>
Set the opening before insertion. The joint is checked against simple
rectangular envelopes—no material deformation is simulated.
</p>
</header>
<aside class="panel" aria-label="Joint tolerance controls">
<div class="panel-head">
<strong>Fit setup</strong>
<button
id="resetView"
class="icon-button"
type="button"
title="Reset camera"
aria-label="Reset camera"
>↺</button>
</div>
<div class="section-label">Clearance / interference</div>
<div class="value-row">
<div id="clearanceValue">+0.4 mm</div>
<div class="unit-note">per side<br>on both axes</div>
</div>
<div class="range-wrap">
<div class="range-zones"></div>
<input
id="clearance"
type="range"
min="-3"
max="8"
step="0.1"
value="0.4"
aria-label="Clearance or interference per side in millimeters"
>
<div class="range-labels">
<span>Collision</span>
<span>Fit</span>
<span>Loose</span>
</div>
</div>
<div class="presets">
<button class="preset" type="button" data-value="-2">−2 mm</button>
<button class="preset active" type="button" data-value="0.4">Nominal</button>
<button class="preset" type="button" data-value="3">+3 mm</button>
</div>
<div class="divider"></div>
<div id="statusCard" class="status-card">
<div id="statusIcon" class="status-icon">✓</div>
<div>
<div id="statusTitle" class="status-title">Valid geometric fit</div>
<p id="statusCopy" class="status-copy"></p>
</div>
</div>
<div class="facts">
<div class="fact">
<span>Width</span>
<strong id="widthFact">0.4 mm/side</strong>
</div>
<div class="fact">
<span>Height</span>
<strong id="heightFact">0.4 mm/side</strong>
</div>
<div class="fact">
<span>Depth</span>
<strong id="depthFact">4.0 mm left</strong>
</div>
</div>
<div class="depth-row">
<span>Tenon insertion depth</span>
<strong id="depthValue">0 / 88 mm</strong>
</div>
<div class="depth-track">
<div id="depthFill"></div>
</div>
<button id="insertButton" class="primary-button" type="button">
Insert tenon
</button>
<div class="dimensions">
<div>
<span>Tenon envelope</span>
<strong>64 × 48 × 88 mm</strong>
</div>
<div>
<span>Mortise opening</span>
<strong id="openingMetric">64.8 × 48.8 mm</strong>
</div>
<div>
<span>Pocket depth</span>
<strong>92 mm</strong>
</div>
</div>
<p class="limit-note">
<strong>Geometric model only.</strong>
Width, height, and depth are compared as axis-aligned rectangular
envelopes. This does not simulate wood deformation, grain, moisture,
adhesive, fasteners, or structural strength. The 0.8 mm loose-fit
threshold is an illustrative scenario, not a universal woodworking limit.
</p>
</aside>
<div class="camera-hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div id="mortiseTag" class="scene-tag">Mortise</div>
<div id="tenonTag" class="scene-tag">Tenon</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const stage = document.getElementById("stage");
const clearanceInput = document.getElementById("clearance");
const clearanceValue = document.getElementById("clearanceValue");
const statusCard = document.getElementById("statusCard");
const statusIcon = document.getElementById("statusIcon");
const statusTitle = document.getElementById("statusTitle");
const statusCopy = document.getElementById("statusCopy");
const widthFact = document.getElementById("widthFact");
const heightFact = document.getElementById("heightFact");
const depthFact = document.getElementById("depthFact");
const openingMetric = document.getElementById("openingMetric");
const depthValue = document.getElementById("depthValue");
const depthFill = document.getElementById("depthFill");
const insertButton = document.getElementById("insertButton");
const resetViewButton = document.getElementById("resetView");
const presetButtons = [...document.querySelectorAll(".preset")];
const mortiseTag = document.getElementById("mortiseTag");
const tenonTag = document.getElementById("tenonTag");
// One scene unit equals 100 mm.
const MM = 0.01;
const TENON_WIDTH = 64 * MM;
const TENON_HEIGHT = 48 * MM;
const TENON_LENGTH = 88 * MM;
const POCKET_DEPTH = 92 * MM;
const BEAM_WIDTH = 420 * MM;
const BEAM_HEIGHT = 155 * MM;
const BEAM_DEPTH = 125 * MM;
const HOLE_X = 35 * MM;
const HOLE_Y = 2 * MM;
const FRONT_Z = BEAM_DEPTH / 2;
const BACK_Z = -BEAM_DEPTH / 2;
const POCKET_BACK_Z = FRONT_Z - POCKET_DEPTH;
const LOOSE_LIMIT = 0.8; // mm per side, illustrative scenario threshold
const START_Z = FRONT_Z + 105 * MM;
const state = {
clearance: 0.4,
kind: "valid",
holeWidth: TENON_WIDTH,
holeHeight: TENON_HEIGHT,
widthGap: 0.4,
heightGap: 0.4,
depthMargin: 4
};
// ---------------------------------------------------------------------
// Renderer and scene
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x10161c);
scene.fog = new THREE.Fog(0x10161c, 9, 19);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.05,
50
);
// Direct, dependency-free orbit controls.
class OrbitControlsLite {
constructor(camera, element) {
this.camera = camera;
this.element = element;
this.target = new THREE.Vector3(0.12, 0.03, 0.34);
this.theta = 0.57;
this.phi = 1.13;
this.radius = window.innerWidth < 700 ? 8.8 : 7.4;
this.dragging = false;
this.lastX = 0;
this.lastY = 0;
element.addEventListener("pointerdown", this.onPointerDown);
element.addEventListener("pointermove", this.onPointerMove);
element.addEventListener("pointerup", this.onPointerUp);
element.addEventListener("pointercancel", this.onPointerUp);
element.addEventListener("wheel", this.onWheel, { passive: false });
element.addEventListener("dblclick", () => this.reset());
element.addEventListener("contextmenu", event => event.preventDefault());
this.update();
}
reset() {
this.theta = 0.57;
this.phi = 1.13;
this.radius = window.innerWidth < 700 ? 8.8 : 7.4;
this.target.set(0.12, 0.03, 0.34);
this.update();
}
onPointerDown = event => {
if (event.button !== 0) return;
this.dragging = true;
this.lastX = event.clientX;
this.lastY = event.clientY;
this.element.setPointerCapture(event.pointerId);
};
onPointerMove = event => {
if (!this.dragging) return;
const dx = event.clientX - this.lastX;
const dy = event.clientY - this.lastY;
this.lastX = event.clientX;
this.lastY = event.clientY;
this.theta -= dx * 0.006;
this.phi = THREE.MathUtils.clamp(
this.phi - dy * 0.005,
0.28,
1.43
);
this.update();
};
onPointerUp = event => {
this.dragging = false;
if (this.element.hasPointerCapture(event.pointerId)) {
this.element.releasePointerCapture(event.pointerId);
}
};
onWheel = event => {
event.preventDefault();
this.radius = THREE.MathUtils.clamp(
this.radius * Math.exp(event.deltaY * 0.001),
4.8,
11.5
);
this.update();
};
update() {
const spherical = new THREE.Spherical(
this.radius,
this.phi,
this.theta
);
this.camera.position.setFromSpherical(spherical).add(this.target);
this.camera.lookAt(this.target);
this.camera.updateMatrixWorld();
}
}
const controls = new OrbitControlsLite(camera, renderer.domElement);
// ---------------------------------------------------------------------
// Procedural low-poly wood textures
// ---------------------------------------------------------------------
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value = (1664525 * value + 1013904223) >>> 0;
return value / 4294967296;
};
}
function makeWoodTexture() {
const random = seededRandom(9182);
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#b87943";
ctx.fillRect(0, 0, 256, 256);
for (let i = 0; i < 54; i++) {
const y = random() * 256;
const amplitude = 1 + random() * 7;
const frequency = 0.015 + random() * 0.035;
const phase = random() * Math.PI * 2;
ctx.beginPath();
for (let x = 0; x <= 256; x += 8) {
const wave = Math.sin(x * frequency + phase) * amplitude;
const drift = Math.sin(x * 0.006 + phase) * 5;
const py = y + wave + drift;
if (x === 0) ctx.moveTo(x, py);
else ctx.lineTo(x, py);
}
ctx.strokeStyle = `rgba(75, 38, 14, ${0.05 + random() * 0.17})`;
ctx.lineWidth = 0.5 + random() * 1.8;
ctx.stroke();
}
for (let i = 0; i < 7; i++) {
const x = random() * 256;
const y = random() * 256;
const radius = 5 + random() * 13;
ctx.beginPath();
ctx.ellipse(x, y, radius * 1.8, radius * 0.55, 0, 0, Math.PI * 2);
ctx.strokeStyle = "rgba(78, 38, 14, 0.14)";
ctx.lineWidth = 1;
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function makeEndGrainTexture() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 256;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#c68a51";
ctx.fillRect(0, 0, 256, 256);
for (let i = 0; i < 13; i++) {
ctx.beginPath();
ctx.ellipse(
128,
128,
8 + i * 9,
5 + i * 7,
0.18,
0,
Math.PI * 2
);
ctx.strokeStyle = `rgba(91, 47, 18, ${0.15 - i * 0.006})`;
ctx.lineWidth = 2;
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = makeWoodTexture();
const endTexture = makeEndGrainTexture();
const beamMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xffffff,
roughness: 0.82,
metalness: 0,
flatShading: true
});
const tenonSideMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xf2c18f,
roughness: 0.76,
metalness: 0,
flatShading: true
});
const tenonEndMaterial = new THREE.MeshStandardMaterial({
map: endTexture,
color: 0xffffff,
roughness: 0.8,
metalness: 0,
flatShading: true
});
const benchMaterial = new THREE.MeshStandardMaterial({
color: 0x33261d,
roughness: 0.96,
flatShading: true
});
// ---------------------------------------------------------------------
// Lighting and environment
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xdbeafe, 0x49301c, 1.8));
const keyLight = new THREE.DirectionalLight(0xffe3bd, 3.1);
keyLight.position.set(-3.5, 6, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 5;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8eb8dc, 1.1);
fillLight.position.set(5, 2, -4);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x151d23,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.91;
ground.receiveShadow = true;
scene.add(ground);
const bench = new THREE.Mesh(
new THREE.BoxGeometry(6.8, 0.14, 3.1),
benchMaterial
);
bench.position.set(0, -0.84, 0.05);
bench.castShadow = true;
bench.receiveShadow = true;
scene.add(bench);
const grid = new THREE.GridHelper(14, 28, 0x34434e, 0x25313a);
grid.position.y = -0.902;
grid.material.transparent = true;
grid.material.opacity = 0.16;
scene.add(grid);
// ---------------------------------------------------------------------
// Low-poly geometry helpers
// ---------------------------------------------------------------------
function chamferedRectangle(width, height, chamfer) {
const shape = new THREE.Shape();
const w = width / 2;
const h = height / 2;
const c = chamfer;
shape.moveTo(-w + c, -h);
shape.lineTo(w - c, -h);
shape.lineTo(w, -h + c);
shape.lineTo(w, h - c);
shape.lineTo(w - c, h);
shape.lineTo(-w + c, h);
shape.lineTo(-w, h - c);
shape.lineTo(-w, -h + c);
shape.closePath();
return shape;
}
function addRectangularHole(shape, x, y, width, height) {
const hole = new THREE.Path();
const left = x - width / 2;
const right = x + width / 2;
const bottom = y - height / 2;
const top = y + height / 2;
// Clockwise winding, opposite to the outer shape.
hole.moveTo(left, bottom);
hole.lineTo(left, top);
hole.lineTo(right, top);
hole.lineTo(right, bottom);
hole.closePath();
shape.holes.push(hole);
}
const beamBack = new THREE.Mesh(
new THREE.ExtrudeGeometry(
chamferedRectangle(BEAM_WIDTH, BEAM_HEIGHT, 8 * MM),
{
depth: POCKET_BACK_Z - BACK_Z,
bevelEnabled: false,
steps: 1,
curveSegments: 1
}
),
[beamMaterial, beamMaterial]
);
beamBack.geometry.translate(0, 0, BACK_Z);
beamBack.castShadow = true;
beamBack.receiveShadow = true;
scene.add(beamBack);
let beamFront = null;
const pocketFloor = new THREE.Mesh(
new THREE.PlaneGeometry(1, 1),
new THREE.MeshStandardMaterial({
color: 0x70411f,
roughness: 0.94,
side: THREE.DoubleSide
})
);
pocketFloor.position.set(HOLE_X, HOLE_Y, POCKET_BACK_Z + 0.002);
scene.add(pocketFloor);
function rebuildPocket() {
if (beamFront) {
scene.remove(beamFront);
beamFront.geometry.dispose();
}
const shape = chamferedRectangle(BEAM_WIDTH, BEAM_HEIGHT, 8 * MM);
addRectangularHole(
shape,
HOLE_X,
HOLE_Y,
state.holeWidth,
state.holeHeight
);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: POCKET_DEPTH,
bevelEnabled: false,
steps: 1,
curveSegments: 1
});
geometry.translate(0, 0, POCKET_BACK_Z);
beamFront = new THREE.Mesh(geometry, [beamMaterial, beamMaterial]);
beamFront.castShadow = true;
beamFront.receiveShadow = true;
scene.add(beamFront);
pocketFloor.scale.set(state.holeWidth, state.holeHeight, 1);
}
// Colored rim around the pocket opening.
const rimMaterial = new THREE.MeshBasicMaterial({
color: 0x55d69e,
transparent: true,
opacity: 0.85,
depthWrite: false
});
const rimBars = Array.from({ length: 4 }, () => {
const bar = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
rimMaterial
);
scene.add(bar);
return bar;
});
function updateRim() {
const width = state.holeWidth;
const height = state.holeHeight;
const thickness = 3.2 * MM;
rimBars[0].scale.set(width + 9 * MM, thickness, thickness);
rimBars[0].position.set(
HOLE_X,
HOLE_Y + height / 2 + 2.5 * MM,
FRONT_Z + 0.013
);
rimBars[1].scale.set(width + 9 * MM, thickness, thickness);
rimBars[1].position.set(
HOLE_X,
HOLE_Y - height / 2 - 2.5 * MM,
FRONT_Z + 0.013
);
rimBars[2].scale.set(thickness, height, thickness);
rimBars[2].position.set(
HOLE_X - width / 2 - 2.5 * MM,
HOLE_Y,
FRONT_Z + 0.013
);
rimBars[3].scale.set(thickness, height, thickness);
rimBars[3].position.set(
HOLE_X + width / 2 + 2.5 * MM,
HOLE_Y,
FRONT_Z + 0.013
);
}
// ---------------------------------------------------------------------
// Tenon and alignment guide
// ---------------------------------------------------------------------
const assembly = new THREE.Group();
assembly.position.z = START_Z;
scene.add(assembly);
const tenonGeometry = new THREE.ExtrudeGeometry(
chamferedRectangle(TENON_WIDTH, TENON_HEIGHT, 5 * MM),
{
depth: TENON_LENGTH,
bevelEnabled: false,
steps: 1,
curveSegments: 1
}
);
// Local z = 0 is the trailing end; the tip points toward negative z.
tenonGeometry.translate(0, 0, -TENON_LENGTH);
const tenon = new THREE.Mesh(
tenonGeometry,
[tenonEndMaterial, tenonSideMaterial]
);
tenon.position.set(HOLE_X, HOLE_Y, 0);
tenon.castShadow = true;
tenon.receiveShadow = true;
assembly.add(tenon);
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x55d69e,
transparent: true,
opacity: 0.78
});
const tenonEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(tenonGeometry, 20),
edgeMaterial
);
tenonEdges.scale.setScalar(1.004);
tenon.add(tenonEdges);
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(HOLE_X, HOLE_Y, POCKET_BACK_Z - 0.02),
new THREE.Vector3(HOLE_X, HOLE_Y, FRONT_Z + 1.35)
]);
const guide = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0xc4d0d7,
dashSize: 5 * MM,
gapSize: 4 * MM,
transparent: true,
opacity: 0.28
})
);
guide.computeLineDistances();
scene.add(guide);
// ---------------------------------------------------------------------
// Geometric fit checks
// ---------------------------------------------------------------------
const statusColors = {
collision: 0xf06459,
valid: 0x55d69e,
loose: 0xeeb75c
};
function classifyFit() {
// Actual dimensional comparisons, in millimeters.
const widthGap = (state.holeWidth - TENON_WIDTH) / (2 * MM);
const heightGap = (state.holeHeight - TENON_HEIGHT) / (2 * MM);
const depthMargin = (POCKET_DEPTH - TENON_LENGTH) / MM;
state.widthGap = widthGap;
state.heightGap = heightGap;
state.depthMargin = depthMargin;
if (
widthGap < -0.0001 ||
heightGap < -0.0001 ||
depthMargin < -0.0001
) {
state.kind = "collision";
} else if (
widthGap > LOOSE_LIMIT + 0.0001 ||
heightGap > LOOSE_LIMIT + 0.0001
) {
state.kind = "loose";
} else {
state.kind = "valid";
}
}
function signed(value) {
return `${value >= 0 ? "+" : ""}${value.toFixed(1)}`;
}
function updateClearance() {
state.clearance = Number(clearanceInput.value);
state.holeWidth =
TENON_WIDTH + 2 * state.clearance * MM;
state.holeHeight =
TENON_HEIGHT + 2 * state.clearance * MM;
classifyFit();
rebuildPocket();
updateRim();
const color = statusColors[state.kind];
const cssColor = `#${color.toString(16).padStart(6, "0")}`;
rimMaterial.color.setHex(color);
edgeMaterial.color.setHex(color);
document.documentElement.style.setProperty("--range-color", cssColor);
statusCard.style.setProperty("--state", cssColor);
clearanceValue.textContent = `${signed(state.clearance)} mm`;
clearanceInput.setAttribute(
"aria-valuetext",
`${signed(state.clearance)} millimeters per side`
);
openingMetric.textContent =
`${(state.holeWidth / MM).toFixed(1)} × ` +
`${(state.holeHeight / MM).toFixed(1)} mm`;
if (state.kind === "collision") {
statusIcon.textContent = "×";
statusTitle.textContent = "Geometric collision";
const widthOverlap = Math.max(0, -state.widthGap);
const heightOverlap = Math.max(0, -state.heightGap);
statusCopy.textContent =
`The pocket is ${widthOverlap.toFixed(1)} mm narrower and ` +
`${heightOverlap.toFixed(1)} mm shorter per side than the tenon. ` +
`Insertion is blocked.`;
widthFact.textContent = `${widthOverlap.toFixed(1)} mm overlap`;
heightFact.textContent = `${heightOverlap.toFixed(1)} mm overlap`;
} else if (state.kind === "loose") {
statusIcon.textContent = "!";
statusTitle.textContent = "Excessive looseness";
statusCopy.textContent =
`The opening exceeds the tenon by ${state.widthGap.toFixed(1)} mm ` +
`per side, above the ${LOOSE_LIMIT.toFixed(1)} mm loose-fit threshold ` +
`used in this scenario.`;
widthFact.textContent = `${state.widthGap.toFixed(1)} mm/side`;
heightFact.textContent = `${state.heightGap.toFixed(1)} mm/side`;
} else {
statusIcon.textContent = "✓";
statusTitle.textContent = "Valid geometric fit";
statusCopy.textContent =
`The tenon clears both pocket axes and retains ` +
`${state.depthMargin.toFixed(1)} mm of axial depth margin.`;
widthFact.textContent = `${state.widthGap.toFixed(1)} mm/side`;
heightFact.textContent = `${state.heightGap.toFixed(1)} mm/side`;
}
depthFact.textContent = `${state.depthMargin.toFixed(1)} mm left`;
presetButtons.forEach(button => {
const active =
Math.abs(Number(button.dataset.value) - state.clearance) < 0.01;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
}
// ---------------------------------------------------------------------
// Insertion animation
// ---------------------------------------------------------------------
let animation = null;
let inserted = false;
let busy = false;
let blockedFlash = 0;
function animateAssembly(targetZ, duration, onComplete) {
animation = {
from: assembly.position.z,
to: targetZ,
start: performance.now(),
duration,
onComplete
};
busy = true;
insertButton.disabled = true;
}
function updateDepthReadout() {
const penetration = THREE.MathUtils.clamp(
FRONT_Z + TENON_LENGTH - assembly.position.z,
0,
TENON_LENGTH
);
const millimeters = penetration / MM;
depthValue.textContent =
`${millimeters.toFixed(0)} / ${(TENON_LENGTH / MM).toFixed(0)} mm`;
depthFill.style.width = `${(penetration / TENON_LENGTH) * 100}%`;
}
insertButton.addEventListener("click", () => {
if (busy) return;
if (inserted) {
animateAssembly(START_Z, 1200, () => {
inserted = false;
busy = false;
clearanceInput.disabled = false;
insertButton.disabled = false;
insertButton.textContent = "Insert tenon";
insertButton.classList.remove("secondary");
});
return;
}
if (state.kind === "collision") {
blockedFlash = 1;
statusCard.classList.remove("pulse");
void statusCard.offsetWidth;
statusCard.classList.add("pulse");
insertButton.textContent = "Entry blocked";
window.setTimeout(() => {
if (!busy && !inserted) {
insertButton.textContent = "Insert tenon";
}
}, 1300);
return;
}
clearanceInput.disabled = true;
animateAssembly(FRONT_Z, 1350, () => {
inserted = true;
busy = false;
insertButton.disabled = false;
insertButton.textContent = "Withdraw tenon";
insertButton.classList.add("secondary");
});
});
clearanceInput.addEventListener("input", () => {
if (!inserted && !busy) updateClearance();
});
presetButtons.forEach(button => {
button.addEventListener("click", () => {
if (inserted || busy) return;
clearanceInput.value = button.dataset.value;
updateClearance();
});
});
resetViewButton.addEventListener("click", () => controls.reset());
// ---------------------------------------------------------------------
// Projected labels
// ---------------------------------------------------------------------
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(
mortiseTag,
new THREE.Vector3(
HOLE_X + state.holeWidth / 2 + 5 * MM,
HOLE_Y + state.holeHeight / 2 + 10 * MM,
FRONT_Z + 0.04
)
);
placeLabel(
tenonTag,
new THREE.Vector3(
HOLE_X + TENON_WIDTH / 2 + 5 * MM,
HOLE_Y + TENON_HEIGHT / 2 + 10 * MM,
assembly.position.z
)
);
}
// ---------------------------------------------------------------------
// Resize and render loop
// ---------------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
updateClearance();
updateDepthReadout();
function animate(now) {
requestAnimationFrame(animate);
if (animation) {
const raw = THREE.MathUtils.clamp(
(now - animation.start) / animation.duration,
0,
1
);
// Smooth ease-in/ease-out without external animation libraries.
const eased =
raw < 0.5
? 4 * raw * raw * raw
: 1 - Math.pow(-2 * raw + 2, 3) / 2;
assembly.position.z = THREE.MathUtils.lerp(
animation.from,
animation.to,
eased
);
if (raw >= 1) {
const callback = animation.onComplete;
animation = null;
callback?.();
}
}
blockedFlash = Math.max(0, blockedFlash - 0.018);
rimMaterial.opacity =
0.76 +
blockedFlash * 0.24 *
(0.5 + 0.5 * Math.sin(now * 0.025));
updateDepthReadout();
controls.update();
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```