Prompt specification
Build or customize the Three.js 3D scene "Matched and mismatched tenon profiles" 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: Matched and mismatched tenon profiles
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/matched-and-mismatched-tenon-profiles-x2201
Scene Description Prompt:
Compare stylized matching and oversized timber tenons against one mortise, aligning cross-sections to reveal clearance and interference. One HTML file using Three.js from cdnjs.
Working Three.js Starter (matched-and-mismatched-tenon-profiles-x2201):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Timber Tenon Fit Study</title>
<style>
:root {
--ink: #edf2ed;
--muted: #93a19c;
--panel: rgba(20, 27, 27, 0.86);
--line: rgba(255, 255, 255, 0.11);
--good: #71d7ae;
--bad: #ff745d;
--gold: #e5b56d;
--state: var(--good);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 42% 32%, #263233 0, #151c1d 43%, #0c1112 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.intro {
position: fixed;
z-index: 4;
top: 30px;
left: 34px;
width: min(430px, 42vw);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--gold);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 28px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(36px, 4.2vw, 68px);
font-weight: 560;
letter-spacing: -0.055em;
line-height: 0.94;
}
.intro p {
max-width: 370px;
margin: 19px 0 0;
color: #aab5b0;
font-size: 13px;
line-height: 1.65;
}
.panel {
position: fixed;
z-index: 6;
top: 20px;
right: 20px;
width: 354px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 18px;
border: 1px solid var(--line);
border-radius: 18px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(20px);
scrollbar-width: thin;
scrollbar-color: #48524f transparent;
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 14px;
}
.panel-title {
font-size: 11px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.live-dot {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.live-dot::before {
width: 6px;
height: 6px;
content: "";
border-radius: 50%;
background: var(--state);
box-shadow: 0 0 13px var(--state);
}
.status {
position: relative;
overflow: hidden;
padding: 15px 16px;
border: 1px solid color-mix(in srgb, var(--state) 30%, transparent);
border-radius: 13px;
background:
linear-gradient(110deg, color-mix(in srgb, var(--state) 11%, transparent), transparent 68%),
rgba(255, 255, 255, 0.025);
}
.status::after {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 3px;
content: "";
background: var(--state);
}
.status-code {
color: var(--state);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
}
.status h2 {
margin: 5px 0 5px;
font-size: 21px;
font-weight: 620;
letter-spacing: -0.025em;
}
.status p {
margin: 0;
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin: 12px 0;
}
.metric {
min-width: 0;
padding: 11px 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric-label {
display: block;
min-height: 24px;
color: var(--muted);
font-size: 9px;
line-height: 1.3;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.metric-value {
display: block;
margin-top: 5px;
color: var(--good);
font-size: 17px;
font-weight: 650;
white-space: nowrap;
}
.metric-value.bad {
color: var(--bad);
}
.section-card {
overflow: hidden;
margin: 12px 0;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(6, 10, 11, 0.28);
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 10px 12px;
border-bottom: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.section-head strong {
color: var(--state);
font-weight: 750;
}
#sectionSvg {
display: block;
width: 100%;
height: auto;
}
.specimens {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.specimen {
position: relative;
min-height: 78px;
padding: 12px;
overflow: hidden;
color: var(--ink);
text-align: left;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
cursor: pointer;
transition: border-color 180ms ease, background 180ms ease, transform 180ms ease;
}
.specimen:hover {
transform: translateY(-1px);
border-color: rgba(255, 255, 255, 0.23);
}
.specimen.active[data-mode="match"] {
border-color: rgba(113, 215, 174, 0.55);
background: rgba(113, 215, 174, 0.09);
}
.specimen.active[data-mode="over"] {
border-color: rgba(255, 116, 93, 0.58);
background: rgba(255, 116, 93, 0.09);
}
.specimen-letter {
display: inline-grid;
width: 21px;
height: 21px;
margin-bottom: 7px;
place-items: center;
border: 1px solid currentColor;
border-radius: 50%;
color: var(--good);
font-size: 10px;
font-weight: 800;
}
.specimen[data-mode="over"] .specimen-letter {
color: var(--bad);
}
.specimen strong {
display: block;
font-size: 12px;
font-weight: 650;
}
.specimen small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
line-height: 1.35;
}
.compare-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 9px;
}
.action {
padding: 9px 10px;
color: #dce4df;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
font-size: 10px;
font-weight: 650;
cursor: pointer;
}
.action:hover {
border-color: rgba(255, 255, 255, 0.25);
background: rgba(255, 255, 255, 0.07);
}
.switches {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin-top: 9px;
}
.switch {
position: relative;
display: flex;
align-items: center;
justify-content: center;
min-height: 34px;
color: var(--muted);
border: 1px solid var(--line);
border-radius: 9px;
font-size: 9px;
cursor: pointer;
user-select: none;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch::before {
width: 7px;
height: 7px;
margin-right: 7px;
content: "";
border-radius: 50%;
background: #4a5451;
}
.switch:has(input:checked) {
color: var(--ink);
border-color: rgba(229, 181, 109, 0.35);
background: rgba(229, 181, 109, 0.07);
}
.switch:has(input:checked)::before {
background: var(--gold);
box-shadow: 0 0 8px rgba(229, 181, 109, 0.65);
}
.view-hint {
position: fixed;
z-index: 4;
bottom: 19px;
left: 50%;
padding: 8px 13px;
color: #9daaa5;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(11, 15, 16, 0.48);
font-size: 10px;
letter-spacing: 0.05em;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(10px);
}
#labels {
position: fixed;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 6px 9px;
color: #dce4df;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 7px;
background: rgba(12, 17, 18, 0.68);
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.18);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
line-height: 1.35;
text-transform: uppercase;
transform: translate(-50%, -50%);
white-space: nowrap;
backdrop-filter: blur(8px);
}
.world-label small {
display: block;
margin-top: 2px;
color: #8f9c97;
font-size: 8px;
font-weight: 550;
letter-spacing: 0.04em;
}
.world-label.good {
color: var(--good);
border-color: rgba(113, 215, 174, 0.28);
}
.world-label.bad {
color: var(--bad);
border-color: rgba(255, 116, 93, 0.3);
}
.world-label.gold {
color: var(--gold);
border-color: rgba(229, 181, 109, 0.28);
}
@media (max-width: 900px) {
.intro {
top: 20px;
left: 20px;
width: 42vw;
}
.intro p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: 350px;
max-height: 55vh;
}
.view-hint {
display: none;
}
}
@media (max-width: 620px) {
.intro {
top: 15px;
left: 16px;
width: calc(100% - 32px);
}
h1 {
font-size: 36px;
}
.eyebrow {
margin-bottom: 8px;
}
.panel {
right: 10px;
bottom: 10px;
width: calc(100% - 20px);
max-height: 53vh;
}
.world-label {
display: none !important;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="intro">
<div class="eyebrow">Timber joinery · Fit study 01</div>
<h1>One mortise.<br />Two outcomes.</h1>
<p>
Register a correctly sized tenon and an oversized tenon to the same
mortise. Compare their entrance sections to expose clearance versus
solid interference.
</p>
</header>
<aside class="panel">
<div class="panel-header">
<div class="panel-title">Registered fit</div>
<div class="live-dot">Live section</div>
</div>
<section class="status">
<div id="statusCode" class="status-code">SPECIMEN A · MATCHING</div>
<h2 id="statusTitle">Clearance fit</h2>
<p id="statusCopy">
The tenon remains inside the mortise boundary with an even perimeter gap.
</p>
</section>
<div class="metrics">
<div class="metric">
<span class="metric-label">Side<br />clearance</span>
<strong id="sideMetric" class="metric-value">8.0 mm</strong>
</div>
<div class="metric">
<span class="metric-label">Top / bottom<br />clearance</span>
<strong id="heightMetric" class="metric-value">8.0 mm</strong>
</div>
<div class="metric">
<span class="metric-label">Back<br />clearance</span>
<strong id="depthMetric" class="metric-value">17.0 mm</strong>
</div>
</div>
<section class="section-card">
<div class="section-head">
<span>Entrance section · aligned</span>
<strong id="sectionState">Clearance</strong>
</div>
<svg id="sectionSvg" viewBox="0 0 260 175" role="img"
aria-label="Aligned entrance section of the mortise and selected tenon">
<defs>
<pattern id="interferenceHatch" width="8" height="8"
patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
<rect width="8" height="8" fill="rgba(255,116,93,.09)"></rect>
<line x1="0" y1="0" x2="0" y2="8"
stroke="rgba(255,116,93,.75)" stroke-width="2"></line>
</pattern>
</defs>
<rect x="45" y="35" width="170" height="115" rx="2"
fill="rgba(229,181,109,.045)"
stroke="#d9ae68" stroke-width="2"></rect>
<g id="svgGaps" opacity="1">
<rect x="45" y="35" width="8" height="115" fill="rgba(113,215,174,.25)"></rect>
<rect x="207" y="35" width="8" height="115" fill="rgba(113,215,174,.25)"></rect>
<rect x="45" y="35" width="170" height="8" fill="rgba(113,215,174,.25)"></rect>
<rect x="45" y="142" width="170" height="8" fill="rgba(113,215,174,.25)"></rect>
</g>
<g id="svgInterference" opacity="0">
<rect x="39" y="30" width="6" height="125" fill="url(#interferenceHatch)"></rect>
<rect x="215" y="30" width="6" height="125" fill="url(#interferenceHatch)"></rect>
<rect x="45" y="30" width="170" height="5" fill="url(#interferenceHatch)"></rect>
<rect x="45" y="145" width="170" height="5" fill="url(#interferenceHatch)"></rect>
</g>
<rect id="svgCandidate" x="53" y="43" width="154" height="99" rx="1"
fill="rgba(113,215,174,.12)"
stroke="#71d7ae" stroke-width="2"></rect>
<line x1="130" y1="18" x2="130" y2="157"
stroke="rgba(255,255,255,.15)" stroke-dasharray="3 4"></line>
<line x1="22" y1="92" x2="238" y2="92"
stroke="rgba(255,255,255,.15)" stroke-dasharray="3 4"></line>
<text x="130" y="166" text-anchor="middle"
fill="#7f8c87" font-size="8" letter-spacing="1.2">
MORTISE DATUM / 1 UNIT = 1 MM
</text>
</svg>
</section>
<div class="specimens">
<button class="specimen active" data-mode="match" type="button">
<span class="specimen-letter">A</span>
<strong>Matching tenon</strong>
<small>154 × 99 × 128 mm</small>
</button>
<button class="specimen" data-mode="over" type="button">
<span class="specimen-letter">B</span>
<strong>Oversized tenon</strong>
<small>182 × 125 × 148 mm</small>
</button>
</div>
<div class="compare-row">
<button id="compareButton" class="action" type="button">▶ Play A/B</button>
<button id="resetButton" class="action" type="button">Reset view</button>
</div>
<div class="switches">
<label class="switch">
<input id="xrayToggle" type="checkbox" />
X-ray
</label>
<label class="switch">
<input id="sectionToggle" type="checkbox" checked />
Section
</label>
<label class="switch">
<input id="labelsToggle" type="checkbox" checked />
Labels
</label>
</div>
</aside>
<div id="labels">
<div id="labelMortise" class="world-label gold">
One mortise
<small>170 × 115 × 145 mm</small>
</div>
<div id="labelMatch" class="world-label good">
A · Matching
<small>8 mm perimeter</small>
</div>
<div id="labelOver" class="world-label bad">
B · Oversized
<small>solid interference</small>
</div>
<div id="labelSection" class="world-label">
Registered entrance
<small>aligned cross-section</small>
</div>
<div id="labelAxis" class="world-label">
Insertion axis
<small>shared datum</small>
</div>
</div>
<div class="view-hint">Drag to orbit · Scroll to zoom · Double-click to reset</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 labels = document.getElementById("labels");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0x111719, 12, 27);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(0, 1.62, 0.55);
const desired = {
theta: 0.58,
phi: 1.13,
radius: 11.4
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius
};
function updateCamera(dt) {
const f = 1 - Math.exp(-8 * dt);
current.theta = THREE.MathUtils.lerp(current.theta, desired.theta, f);
current.phi = THREE.MathUtils.lerp(current.phi, desired.phi, f);
current.radius = THREE.MathUtils.lerp(current.radius, desired.radius, f);
const sinPhi = Math.sin(current.phi);
camera.position.set(
cameraTarget.x + current.radius * sinPhi * Math.sin(current.theta),
cameraTarget.y + current.radius * Math.cos(current.phi),
cameraTarget.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(cameraTarget);
}
updateCamera(1);
const hemi = new THREE.HemisphereLight(0xc9e2dc, 0x4b3522, 1.65);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffe4bc, 3.5);
key.position.set(5.5, 9, 7.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -9;
key.shadow.camera.right = 9;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 1;
key.shadow.camera.far = 25;
key.shadow.bias = -0.00025;
scene.add(key);
const rim = new THREE.DirectionalLight(0x87b8c5, 1.8);
rim.position.set(-6, 5, -6);
scene.add(rim);
const cavityLight = new THREE.PointLight(0xffc77e, 7, 4.2, 2);
cavityLight.position.set(0, 1.9, 0.75);
scene.add(cavityLight);
let randomSeed = 73129;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function makeWoodTexture() {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 512;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#c98b4e";
ctx.fillRect(0, 0, 512, 512);
const wash = ctx.createLinearGradient(0, 0, 512, 0);
wash.addColorStop(0, "rgba(255,229,176,.18)");
wash.addColorStop(0.5, "rgba(95,45,18,.06)");
wash.addColorStop(1, "rgba(255,223,163,.12)");
ctx.fillStyle = wash;
ctx.fillRect(0, 0, 512, 512);
for (let i = 0; i < 260; i++) {
const y = random() * 512;
const amplitude = 1 + random() * 8;
const frequency = 0.006 + random() * 0.018;
const phase = random() * Math.PI * 2;
ctx.beginPath();
for (let x = -10; x <= 522; x += 8) {
const grainY =
y +
Math.sin(x * frequency + phase) * amplitude +
Math.sin(x * 0.045 + phase) * 0.8;
if (x === -10) ctx.moveTo(x, grainY);
else ctx.lineTo(x, grainY);
}
const dark = random() > 0.28;
ctx.strokeStyle = dark
? `rgba(77,38,13,${0.05 + random() * 0.17})`
: `rgba(255,220,157,${0.04 + random() * 0.13})`;
ctx.lineWidth = 0.5 + random() * 2.1;
ctx.stroke();
}
const knotX = 330;
const knotY = 184;
for (let i = 0; i < 8; i++) {
ctx.beginPath();
ctx.ellipse(
knotX,
knotY,
8 + i * 6,
3 + i * 2.8,
-0.15,
0,
Math.PI * 2
);
ctx.strokeStyle = `rgba(74,34,13,${0.18 - i * 0.016})`;
ctx.lineWidth = 1.2;
ctx.stroke();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(2.2, 1.35);
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 = "#d49a58";
ctx.fillRect(0, 0, 256, 256);
for (let i = 0; i < 18; i++) {
ctx.beginPath();
ctx.ellipse(
130 + Math.sin(i * 1.7) * 2,
126 + Math.cos(i * 1.3) * 2,
12 + i * 7.4,
9 + i * 5.8,
0.08,
0,
Math.PI * 2
);
ctx.strokeStyle = `rgba(93,48,19,${0.23 - i * 0.006})`;
ctx.lineWidth = 1.4;
ctx.stroke();
}
for (let i = 0; i < 70; i++) {
const x = random() * 256;
const y = random() * 256;
ctx.fillStyle = `rgba(73,37,15,${random() * 0.12})`;
ctx.fillRect(x, y, 1 + random() * 8, 0.7);
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = makeWoodTexture();
const endGrainTexture = makeEndGrainTexture();
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(36, 36),
new THREE.MeshStandardMaterial({
color: 0x141b1c,
roughness: 0.96,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.035;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(30, 30, 0x53605c, 0x2a3332);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
const BEAM_W = 6.4;
const BEAM_H = 3.4;
const FRONT_Z = 1.2;
const BACK_Z = -0.25;
const MORTISE = {
width: 1.7,
height: 1.15,
depth: 1.45,
centerY: 1.85
};
const beamMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: 0xf2c991,
roughness: 0.8,
metalness: 0
});
const cavityMaterial = new THREE.MeshStandardMaterial({
map: endGrainTexture,
color: 0xd69a58,
roughness: 0.9,
side: THREE.DoubleSide
});
const beam = new THREE.Group();
scene.add(beam);
function addBeamBox(width, height, depth, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
beamMaterial
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
beam.add(mesh);
return mesh;
}
// Solid rear portion of the timber member.
addBeamBox(
BEAM_W,
BEAM_H,
FRONT_Z - BACK_Z + 0.95,
0,
BEAM_H / 2,
BACK_Z - 0.475
);
// Front frame built around one true rectangular mortise.
const frontDepth = FRONT_Z - BACK_Z;
const frontZ = (FRONT_Z + BACK_Z) / 2;
const sideWidth = (BEAM_W - MORTISE.width) / 2;
addBeamBox(
sideWidth,
BEAM_H,
frontDepth,
-(MORTISE.width / 2 + sideWidth / 2),
BEAM_H / 2,
frontZ
);
addBeamBox(
sideWidth,
BEAM_H,
frontDepth,
MORTISE.width / 2 + sideWidth / 2,
BEAM_H / 2,
frontZ
);
const lowerHeight = MORTISE.centerY - MORTISE.height / 2;
const upperHeight = BEAM_H - (MORTISE.centerY + MORTISE.height / 2);
addBeamBox(
MORTISE.width,
lowerHeight,
frontDepth,
0,
lowerHeight / 2,
frontZ
);
addBeamBox(
MORTISE.width,
upperHeight,
frontDepth,
0,
BEAM_H - upperHeight / 2,
frontZ
);
function addCavityPlane(width, height, position, rotation) {
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
cavityMaterial
);
plane.position.copy(position);
plane.rotation.set(rotation.x, rotation.y, rotation.z);
plane.receiveShadow = true;
beam.add(plane);
}
addCavityPlane(
MORTISE.width,
MORTISE.height,
new THREE.Vector3(0, MORTISE.centerY, BACK_Z + 0.008),
new THREE.Euler(0, 0, 0)
);
for (const side of [-1, 1]) {
addCavityPlane(
MORTISE.depth,
MORTISE.height,
new THREE.Vector3(
side * MORTISE.width / 2,
MORTISE.centerY,
frontZ
),
new THREE.Euler(0, Math.PI / 2, 0)
);
}
addCavityPlane(
MORTISE.width,
MORTISE.depth,
new THREE.Vector3(
0,
MORTISE.centerY + MORTISE.height / 2,
frontZ
),
new THREE.Euler(Math.PI / 2, 0, 0)
);
addCavityPlane(
MORTISE.width,
MORTISE.depth,
new THREE.Vector3(
0,
MORTISE.centerY - MORTISE.height / 2,
frontZ
),
new THREE.Euler(Math.PI / 2, 0, 0)
);
const beamOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(
new THREE.BoxGeometry(BEAM_W, BEAM_H, FRONT_Z - BACK_Z + 0.95)
),
new THREE.LineBasicMaterial({
color: 0x6f4c2c,
transparent: true,
opacity: 0.48
})
);
beamOutline.position.set(0, BEAM_H / 2, BACK_Z - 0.475);
beam.add(beamOutline);
const rimMaterial = new THREE.LineBasicMaterial({
color: 0xf0c77f,
transparent: true,
opacity: 0.82,
depthTest: false
});
function rectanglePoints(width, height, z, centerY = MORTISE.centerY) {
const x = width / 2;
const y = height / 2;
return [
new THREE.Vector3(-x, centerY - y, z),
new THREE.Vector3(x, centerY - y, z),
new THREE.Vector3(x, centerY + y, z),
new THREE.Vector3(-x, centerY + y, z),
new THREE.Vector3(-x, centerY - y, z)
];
}
const cavityRim = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
rectanglePoints(MORTISE.width, MORTISE.height, FRONT_Z + 0.009)
),
rimMaterial
);
cavityRim.renderOrder = 12;
beam.add(cavityRim);
function makeBadge(letter, background, foreground) {
const canvas = document.createElement("canvas");
canvas.width = canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(64, 64, 49, 0, Math.PI * 2);
ctx.fillStyle = background;
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = foreground;
ctx.stroke();
ctx.fillStyle = foreground;
ctx.font = "700 54px sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(letter, 64, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
return new THREE.Mesh(
new THREE.PlaneGeometry(0.38, 0.38),
new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
depthWrite: false
})
);
}
function makeTenon({
id,
width,
height,
depth,
sideColor,
endColor,
edgeColor,
badgeLetter,
badgeBackground,
badgeForeground,
parkPosition,
parkRotation
}) {
const group = new THREE.Group();
const sideMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
color: sideColor,
roughness: 0.78,
metalness: 0
});
const endMaterial = new THREE.MeshStandardMaterial({
map: endGrainTexture,
color: endColor,
roughness: 0.9
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
[
sideMaterial,
sideMaterial,
sideMaterial,
sideMaterial,
endMaterial,
endMaterial
]
);
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color: edgeColor,
transparent: true,
opacity: 0.9
})
);
edges.renderOrder = 5;
group.add(edges);
const badge = makeBadge(
badgeLetter,
badgeBackground,
badgeForeground
);
badge.position.set(0, 0, depth / 2 + 0.008);
badge.renderOrder = 8;
group.add(badge);
group.position.copy(parkPosition);
group.rotation.set(...parkRotation);
scene.add(group);
return {
id,
group,
width,
height,
depth,
parkPosition,
parkRotation,
targetPosition: parkPosition.clone(),
targetRotation: new THREE.Euler(...parkRotation)
};
}
const matchingTenon = makeTenon({
id: "match",
width: 1.54,
height: 0.99,
depth: 1.28,
sideColor: 0xffd58f,
endColor: 0xf2bd72,
edgeColor: 0x71d7ae,
badgeLetter: "A",
badgeBackground: "rgba(14,32,27,.92)",
badgeForeground: "#71d7ae",
parkPosition: new THREE.Vector3(-4.15, MORTISE.centerY, 3.05),
parkRotation: new THREE.Euler(0.02, -0.18, 0.04)
});
const oversizedTenon = makeTenon({
id: "over",
width: 1.82,
height: 1.25,
depth: 1.48,
sideColor: 0xe98770,
endColor: 0xd9785f,
edgeColor: 0xff745d,
badgeLetter: "B",
badgeBackground: "rgba(44,16,13,.94)",
badgeForeground: "#ff745d",
parkPosition: new THREE.Vector3(4.15, MORTISE.centerY, 3.05),
parkRotation: new THREE.Euler(-0.02, 0.18, -0.04)
});
const sectionGroup = new THREE.Group();
scene.add(sectionGroup);
const sectionCavityLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
rectanglePoints(MORTISE.width, MORTISE.height, FRONT_Z + 0.028)
),
new THREE.LineBasicMaterial({
color: 0xf0c77f,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
sectionCavityLine.renderOrder = 20;
sectionGroup.add(sectionCavityLine);
const matchingSectionLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
rectanglePoints(
matchingTenon.width,
matchingTenon.height,
FRONT_Z + 0.035
)
),
new THREE.LineBasicMaterial({
color: 0x71d7ae,
transparent: true,
opacity: 1,
depthTest: false
})
);
matchingSectionLine.renderOrder = 21;
sectionGroup.add(matchingSectionLine);
const oversizedSectionLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(
rectanglePoints(
oversizedTenon.width,
oversizedTenon.height,
FRONT_Z + 0.036
)
),
new THREE.LineBasicMaterial({
color: 0xff745d,
transparent: true,
opacity: 1,
depthTest: false
})
);
oversizedSectionLine.renderOrder = 21;
sectionGroup.add(oversizedSectionLine);
const sectionPlane = new THREE.Mesh(
new THREE.PlaneGeometry(2.35, 1.78),
new THREE.MeshBasicMaterial({
color: 0xf1d39d,
transparent: true,
opacity: 0.025,
depthWrite: false,
side: THREE.DoubleSide
})
);
sectionPlane.position.set(0, MORTISE.centerY, FRONT_Z + 0.022);
sectionPlane.renderOrder = 18;
sectionGroup.add(sectionPlane);
const gapGroup = new THREE.Group();
sectionGroup.add(gapGroup);
function makeMarker(width, height, x, y, color, opacity) {
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
depthTest: false
});
const marker = new THREE.Mesh(
new THREE.BoxGeometry(width, height, 0.018),
material
);
marker.position.set(x, y, FRONT_Z + 0.04);
marker.renderOrder = 22;
return marker;
}
const cavityHalfW = MORTISE.width / 2;
const cavityHalfH = MORTISE.height / 2;
const matchHalfW = matchingTenon.width / 2;
const matchHalfH = matchingTenon.height / 2;
const overHalfW = oversizedTenon.width / 2;
const overHalfH = oversizedTenon.height / 2;
gapGroup.add(
makeMarker(
cavityHalfW - matchHalfW,
matchingTenon.height,
-(cavityHalfW + matchHalfW) / 2,
MORTISE.centerY,
0x71d7ae,
0.45
),
makeMarker(
cavityHalfW - matchHalfW,
matchingTenon.height,
(cavityHalfW + matchHalfW) / 2,
MORTISE.centerY,
0x71d7ae,
0.45
),
makeMarker(
matchingTenon.width,
cavityHalfH - matchHalfH,
0,
MORTISE.centerY - (cavityHalfH + matchHalfH) / 2,
0x71d7ae,
0.45
),
makeMarker(
matchingTenon.width,
cavityHalfH - matchHalfH,
0,
MORTISE.centerY + (cavityHalfH + matchHalfH) / 2,
0x71d7ae,
0.45
)
);
const interferenceGroup = new THREE.Group();
sectionGroup.add(interferenceGroup);
interferenceGroup.add(
makeMarker(
overHalfW - cavityHalfW,
oversizedTenon.height,
-(cavityHalfW + overHalfW) / 2,
MORTISE.centerY,
0xff745d,
0.72
),
makeMarker(
overHalfW - cavityHalfW,
oversizedTenon.height,
(cavityHalfW + overHalfW) / 2,
MORTISE.centerY,
0xff745d,
0.72
),
makeMarker(
oversizedTenon.width,
overHalfH - cavityHalfH,
0,
MORTISE.centerY - (cavityHalfH + overHalfH) / 2,
0xff745d,
0.72
),
makeMarker(
oversizedTenon.width,
overHalfH - cavityHalfH,
0,
MORTISE.centerY + (cavityHalfH + overHalfH) / 2,
0xff745d,
0.72
)
);
const insertionArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, -1),
new THREE.Vector3(-1.3, 0.73, 2.25),
1.25,
0x71d7ae,
0.18,
0.11
);
scene.add(insertionArrow);
const statusCode = document.getElementById("statusCode");
const statusTitle = document.getElementById("statusTitle");
const statusCopy = document.getElementById("statusCopy");
const sideMetric = document.getElementById("sideMetric");
const heightMetric = document.getElementById("heightMetric");
const depthMetric = document.getElementById("depthMetric");
const sectionState = document.getElementById("sectionState");
const svgCandidate = document.getElementById("svgCandidate");
const svgGaps = document.getElementById("svgGaps");
const svgInterference = document.getElementById("svgInterference");
const specimenButtons = [...document.querySelectorAll(".specimen")];
let currentMode = "match";
let compareTimer = null;
function setMetric(element, value, negative) {
element.textContent = value;
element.classList.toggle("bad", negative);
}
function setMode(mode) {
currentMode = mode;
const isMatching = mode === "match";
const stateColor = isMatching ? "#71d7ae" : "#ff745d";
document.documentElement.style.setProperty("--state", stateColor);
matchingTenon.targetPosition.copy(
isMatching
? new THREE.Vector3(0, MORTISE.centerY, 0.56)
: matchingTenon.parkPosition
);
matchingTenon.targetRotation.set(
...(isMatching ? [0, 0, 0] : matchingTenon.parkRotation)
);
oversizedTenon.targetPosition.copy(
isMatching
? oversizedTenon.parkPosition
: new THREE.Vector3(0, MORTISE.centerY, 0.52)
);
oversizedTenon.targetRotation.set(
...(isMatching ? oversizedTenon.parkRotation : [0, 0, 0])
);
matchingSectionLine.visible = isMatching;
oversizedSectionLine.visible = !isMatching;
gapGroup.visible = isMatching;
interferenceGroup.visible = !isMatching;
insertionArrow.setColor(new THREE.Color(stateColor));
rimMaterial.color.set(stateColor);
if (isMatching) {
statusCode.textContent = "SPECIMEN A · MATCHING";
statusTitle.textContent = "Clearance fit";
statusCopy.textContent =
"The tenon remains inside the mortise boundary with an even perimeter gap.";
sectionState.textContent = "Clearance";
setMetric(sideMetric, "8.0 mm", false);
setMetric(heightMetric, "8.0 mm", false);
setMetric(depthMetric, "17.0 mm", false);
svgCandidate.setAttribute("x", "53");
svgCandidate.setAttribute("y", "43");
svgCandidate.setAttribute("width", "154");
svgCandidate.setAttribute("height", "99");
svgCandidate.setAttribute("fill", "rgba(113,215,174,.12)");
svgCandidate.setAttribute("stroke", "#71d7ae");
svgGaps.setAttribute("opacity", "1");
svgInterference.setAttribute("opacity", "0");
} else {
statusCode.textContent = "SPECIMEN B · OVERSIZED";
statusTitle.textContent = "Interference";
statusCopy.textContent =
"The tenon crosses the mortise boundary and occupies timber that cannot be removed.";
sectionState.textContent = "Interference";
setMetric(sideMetric, "−6.0 mm", true);
setMetric(heightMetric, "−5.0 mm", true);
setMetric(depthMetric, "−3.0 mm", true);
svgCandidate.setAttribute("x", "39");
svgCandidate.setAttribute("y", "30");
svgCandidate.setAttribute("width", "182");
svgCandidate.setAttribute("height", "125");
svgCandidate.setAttribute("fill", "rgba(255,116,93,.14)");
svgCandidate.setAttribute("stroke", "#ff745d");
svgGaps.setAttribute("opacity", "0");
svgInterference.setAttribute("opacity", "1");
}
specimenButtons.forEach((button) => {
button.classList.toggle("active", button.dataset.mode === mode);
});
document.getElementById("labelMatch").classList.toggle("active", isMatching);
document.getElementById("labelOver").classList.toggle("active", !isMatching);
}
function stopCompare() {
if (compareTimer !== null) {
clearInterval(compareTimer);
compareTimer = null;
}
document.getElementById("compareButton").textContent = "▶ Play A/B";
}
specimenButtons.forEach((button) => {
button.addEventListener("click", () => {
stopCompare();
setMode(button.dataset.mode);
});
});
document.getElementById("compareButton").addEventListener("click", () => {
if (compareTimer !== null) {
stopCompare();
} else {
compareTimer = setInterval(() => {
setMode(currentMode === "match" ? "over" : "match");
}, 3200);
document.getElementById("compareButton").textContent = "Ⅱ Pause A/B";
}
});
document.getElementById("resetButton").addEventListener("click", () => {
desired.theta = 0.58;
desired.phi = 1.13;
desired.radius = 11.4;
});
document.getElementById("xrayToggle").addEventListener("change", (event) => {
const enabled = event.target.checked;
beamMaterial.transparent = enabled;
beamMaterial.opacity = enabled ? 0.2 : 1;
beamMaterial.depthWrite = !enabled;
beamMaterial.needsUpdate = true;
cavityMaterial.transparent = enabled;
cavityMaterial.opacity = enabled ? 0.28 : 1;
cavityMaterial.depthWrite = !enabled;
cavityMaterial.needsUpdate = true;
});
document.getElementById("sectionToggle").addEventListener("change", (event) => {
sectionGroup.visible = event.target.checked;
});
document.getElementById("labelsToggle").addEventListener("change", (event) => {
labels.style.display = event.target.checked ? "block" : "none";
});
// Custom orbit controls implemented directly, without addons.
let dragging = false;
let lastX = 0;
let lastY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.48,
1.46
);
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
7.2,
16.5
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
desired.theta = 0.58;
desired.phi = 1.13;
desired.radius = 11.4;
});
const projected = new THREE.Vector3();
function placeLabel(element, worldPosition, visible = true) {
if (!visible) {
element.style.opacity = "0";
return;
}
projected.copy(worldPosition).project(camera);
if (projected.z < -1 || projected.z > 1) {
element.style.opacity = "0";
return;
}
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`;
element.style.opacity = "1";
}
const labelMortise = document.getElementById("labelMortise");
const labelMatch = document.getElementById("labelMatch");
const labelOver = document.getElementById("labelOver");
const labelSection = document.getElementById("labelSection");
const labelAxis = document.getElementById("labelAxis");
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
updateCamera(dt);
const f = 1 - Math.exp(-6.2 * dt);
for (const tenon of [matchingTenon, oversizedTenon]) {
tenon.group.position.lerp(tenon.targetPosition, f);
tenon.group.rotation.x = THREE.MathUtils.lerp(
tenon.group.rotation.x,
tenon.targetRotation.x,
f
);
tenon.group.rotation.y = THREE.MathUtils.lerp(
tenon.group.rotation.y,
tenon.targetRotation.y,
f
);
tenon.group.rotation.z = THREE.MathUtils.lerp(
tenon.group.rotation.z,
tenon.targetRotation.z,
f
);
}
const pulse = 0.72 + Math.sin(elapsed * 4.2) * 0.22;
interferenceGroup.children.forEach((marker) => {
marker.material.opacity = pulse;
});
const gapPulse = 0.34 + Math.sin(elapsed * 3.1) * 0.08;
gapGroup.children.forEach((marker) => {
marker.material.opacity = gapPulse;
});
placeLabel(
labelMortise,
new THREE.Vector3(-1.75, 3.22, FRONT_Z + 0.08)
);
placeLabel(
labelMatch,
matchingTenon.group.position.clone().add(
new THREE.Vector3(0, 0.83, 0)
),
currentMode === "match"
);
placeLabel(
labelOver,
oversizedTenon.group.position.clone().add(
new THREE.Vector3(0, 0.9, 0)
),
currentMode === "over"
);
placeLabel(
labelSection,
new THREE.Vector3(1.35, 0.86, FRONT_Z + 0.05)
);
placeLabel(
labelAxis,
new THREE.Vector3(-1.3, 0.48, 2.25)
);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
setMode("match");
animate();
</script>
</body>
</html>
```