Prompt specification
Build or customize the Three.js 3D scene "Reversible cabinet assembly lesson" 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: Reversible cabinet assembly lesson
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reversible-cabinet-assembly-lesson-x0276
Scene Description Prompt:
Please use Three.js from cdnjs for a low-poly scene based on this idea: reversible cabinet assembly lesson. Create an exploded cabinet whose door can be mounted on either side; users relocate hinge parts and preserve consistent axes, clearances, and opening direction. Return one self-contained HTML file.
Working Three.js Starter (reversible-cabinet-assembly-lesson-x0276):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Assembly Lab — Reversible Cabinet</title>
<style>
:root {
--ink: #183b35;
--muted: #667b75;
--green: #2d665a;
--green-soft: #e1ece6;
--orange: #e46f4d;
--yellow: #e8ba55;
--paper: rgba(255, 255, 255, 0.9);
--line: rgba(28, 69, 61, 0.14);
--shadow: 0 18px 50px rgba(32, 64, 55, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8ede7;
color: var(--ink);
font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
#app,
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene {
z-index: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
cursor: grab;
touch-action: none;
}
#scene canvas.dragging {
cursor: grabbing;
}
#labels {
z-index: 2;
pointer-events: none;
overflow: hidden;
}
.topbar {
position: fixed;
z-index: 10;
top: 20px;
left: 24px;
right: 24px;
display: flex;
align-items: center;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
pointer-events: auto;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
place-items: center;
border-radius: 12px;
background: var(--ink);
box-shadow: 0 8px 22px rgba(24, 59, 53, 0.2);
}
.brand-mark svg {
width: 23px;
height: 23px;
}
.brand-copy {
line-height: 1.08;
}
.brand-copy strong {
display: block;
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.brand-copy span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.top-chip {
display: flex;
align-items: center;
gap: 8px;
padding: 9px 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(255, 255, 255, 0.76);
backdrop-filter: blur(12px);
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.top-chip::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: #4c9a75;
box-shadow: 0 0 0 4px rgba(76, 154, 117, 0.13);
}
.panel {
position: fixed;
z-index: 8;
top: 78px;
left: 24px;
width: 342px;
max-height: calc(100vh - 100px);
overflow: auto;
padding: 22px;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 24px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: rgba(45, 102, 90, 0.25) transparent;
}
.eyebrow {
margin: 0 0 9px;
color: var(--orange);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 290px;
font-size: clamp(25px, 2.2vw, 34px);
line-height: 1.03;
letter-spacing: -0.045em;
}
.intro {
margin: 12px 0 20px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.section {
padding-top: 17px;
margin-top: 17px;
border-top: 1px solid var(--line);
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 12px;
}
.section-head label,
.section-title {
font-size: 11px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
output {
min-width: 42px;
padding: 4px 7px;
border-radius: 7px;
background: var(--green-soft);
color: var(--green);
font-size: 11px;
font-weight: 800;
text-align: center;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
padding: 5px;
border: 1px solid var(--line);
border-radius: 13px;
background: #edf1ed;
}
.segmented button {
padding: 10px;
border: 0;
border-radius: 9px;
background: transparent;
color: var(--muted);
cursor: pointer;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: 160ms ease;
}
.segmented button:hover {
color: var(--ink);
}
.segmented button.active {
background: #fff;
color: var(--green);
box-shadow: 0 3px 12px rgba(31, 65, 56, 0.1);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 10px;
}
.button {
min-height: 39px;
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 11px;
background: #fff;
cursor: pointer;
font-size: 11px;
font-weight: 800;
transition: transform 140ms ease, background 140ms ease, opacity 140ms ease;
}
.button:hover {
transform: translateY(-1px);
background: #f5f8f5;
}
.button:active {
transform: translateY(0);
}
.button.primary {
border-color: var(--green);
background: var(--green);
color: #fff;
}
.button.primary:hover {
background: #24564c;
}
.button:disabled {
cursor: wait;
opacity: 0.48;
transform: none;
}
.range-wrap {
margin-top: 13px;
}
.range-meta {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
color: var(--muted);
font-size: 11px;
}
.range-meta strong {
color: var(--ink);
font-size: 11px;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 999px;
appearance: none;
background: linear-gradient(
90deg,
var(--green) 0%,
var(--green) var(--progress, 0%),
#dce4df var(--progress, 0%),
#dce4df 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #fff;
border-radius: 50%;
appearance: none;
background: var(--green);
box-shadow: 0 2px 7px rgba(30, 67, 58, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #fff;
border-radius: 50%;
background: var(--green);
box-shadow: 0 2px 7px rgba(30, 67, 58, 0.28);
}
.lock-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
padding: 11px 12px;
border: 1px solid rgba(76, 154, 117, 0.18);
border-radius: 12px;
background: rgba(225, 236, 230, 0.65);
}
.lock-copy strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
}
.lock-copy span {
color: var(--muted);
font-size: 10px;
}
.lock-pill {
padding: 5px 8px;
border-radius: 999px;
background: #fff;
color: #3a8062;
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.right-panel {
position: fixed;
z-index: 7;
top: 78px;
right: 24px;
width: 268px;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 21px;
background: rgba(255, 255, 255, 0.84);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.right-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 11px;
}
.right-title span:first-child {
font-size: 11px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.mini-chip {
padding: 4px 7px;
border-radius: 999px;
background: var(--green-soft);
color: var(--green);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.check {
display: grid;
grid-template-columns: 28px 1fr auto;
align-items: center;
gap: 9px;
padding: 10px 0;
border-top: 1px solid var(--line);
}
.check-icon {
width: 24px;
height: 24px;
display: grid;
place-items: center;
border-radius: 8px;
background: #e4f0e8;
color: #388060;
font-size: 12px;
font-weight: 900;
}
.check-icon.warn {
background: #fff0dc;
color: #b06b29;
}
.check strong {
display: block;
margin-bottom: 2px;
font-size: 11px;
}
.check small {
color: var(--muted);
font-size: 9px;
}
.check-value {
color: var(--green);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.steps {
margin-top: 16px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.step {
width: 100%;
display: grid;
grid-template-columns: 28px 1fr;
align-items: center;
gap: 9px;
padding: 8px 0;
border: 0;
background: transparent;
cursor: pointer;
text-align: left;
}
.step-number {
width: 27px;
height: 27px;
display: grid;
place-items: center;
border: 1px solid var(--line);
border-radius: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 900;
}
.step.active .step-number {
border-color: var(--green);
background: var(--green);
color: #fff;
}
.step strong {
display: block;
font-size: 10px;
}
.step small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.camera-help {
margin-top: 12px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
line-height: 1.5;
}
.camera-help button {
float: right;
padding: 4px 8px;
border: 1px solid var(--line);
border-radius: 7px;
background: #fff;
cursor: pointer;
font-size: 9px;
font-weight: 800;
}
.scene-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border: 1px solid rgba(255, 255, 255, 0.82);
border-radius: 9px;
background: rgba(255, 255, 255, 0.88);
box-shadow: 0 6px 18px rgba(31, 63, 55, 0.11);
color: var(--ink);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.07em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
transition: opacity 120ms ease;
}
.scene-label::before {
content: "";
width: 7px;
height: 7px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 0 3px rgba(228, 111, 77, 0.14);
}
.scene-label.axis::before {
background: var(--yellow);
box-shadow: 0 0 0 3px rgba(232, 186, 85, 0.18);
}
.scene-label.arc::before {
background: #4c9a75;
box-shadow: 0 0 0 3px rgba(76, 154, 117, 0.15);
}
.toast {
position: fixed;
z-index: 12;
left: 50%;
bottom: 22px;
display: flex;
align-items: center;
gap: 11px;
max-width: min(440px, calc(100vw - 32px));
padding: 11px 15px;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 14px;
background: rgba(24, 59, 53, 0.92);
box-shadow: 0 12px 30px rgba(24, 59, 53, 0.22);
color: #fff;
opacity: 0;
pointer-events: none;
transform: translate(-50%, 15px);
transition: 250ms ease;
}
.toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
.toast-mark {
width: 25px;
height: 25px;
display: grid;
place-items: center;
flex: 0 0 auto;
border-radius: 8px;
background: rgba(255, 255, 255, 0.12);
color: #f0c66c;
font-size: 12px;
}
.toast strong {
display: block;
font-size: 10px;
}
.toast span {
display: block;
margin-top: 2px;
color: rgba(255, 255, 255, 0.68);
font-size: 9px;
}
@media (max-width: 900px) {
.topbar {
top: 12px;
left: 14px;
right: 14px;
}
.top-chip {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 46vh;
padding: 17px;
border-radius: 19px;
}
.right-panel {
display: none;
}
h1 {
max-width: none;
font-size: 24px;
}
.intro {
margin-bottom: 14px;
}
.scene-label {
font-size: 8px;
}
.scene-label.clearance {
display: none;
}
.toast {
bottom: calc(46vh + 24px);
}
}
@media (max-height: 720px) and (min-width: 901px) {
.panel {
padding: 17px;
}
h1 {
font-size: 27px;
}
.intro {
margin: 9px 0 14px;
}
.section {
padding-top: 12px;
margin-top: 12px;
}
}
</style>
</head>
<body>
<div id="app">
<div id="scene" aria-label="Interactive 3D reversible cabinet assembly"></div>
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<svg viewBox="0 0 24 24" fill="none">
<path d="M4 5.5 12 2l8 3.5v13L12 22l-8-3.5v-13Z"
stroke="#F3C66B" stroke-width="1.6" stroke-linejoin="round"/>
<path d="m4 5.5 8 3.7 8-3.7M12 9.2V22"
stroke="#F3C66B" stroke-width="1.6" stroke-linejoin="round"/>
</svg>
</div>
<div class="brand-copy">
<strong>Assembly Lab</strong>
<span>Module 04 · Joinery</span>
</div>
</div>
<div class="top-chip">Interactive training model</div>
</header>
<aside class="panel" aria-label="Assembly controls">
<p class="eyebrow">Reversible cabinet system</p>
<h1>Build once.<br>Hang it either way.</h1>
<p class="intro">
Relocate the hinge kit to either stile. The vertical pivot, mounting
offset, and room-side swing remain constrained.
</p>
<section class="section">
<div class="section-head">
<span class="section-title">01 · Hinge side</span>
<output id="mountOutput">Left</output>
</div>
<div class="segmented" role="group" aria-label="Choose hinge side">
<button id="leftButton" class="active" type="button">Left stile</button>
<button id="rightButton" type="button">Right stile</button>
</div>
<div class="button-row">
<button id="relocateButton" class="button primary" type="button">
Relocate hinge kit
</button>
<button id="openButton" class="button" type="button">Open to 100°</button>
</div>
</section>
<section class="section">
<div class="section-head">
<label for="angleSlider">Door opening</label>
<output id="angleOutput">0°</output>
</div>
<div class="range-wrap">
<div class="range-meta">
<span>Closed</span>
<strong id="swingDirection">Room-side swing</strong>
<span>100°</span>
</div>
<input
id="angleSlider"
type="range"
min="0"
max="100"
value="0"
aria-label="Door opening angle"
>
</div>
<div class="section-head" style="margin-top:18px; margin-bottom:10px">
<label for="explodeSlider">Exploded view</label>
<output id="explodeOutput">0%</output>
</div>
<div class="range-wrap" style="margin-top:0">
<div class="range-meta">
<span>Assembled</span>
<strong>Component spacing</strong>
<span>Exploded</span>
</div>
<input
id="explodeSlider"
type="range"
min="0"
max="100"
value="0"
aria-label="Exploded view amount"
>
</div>
</section>
<section class="section">
<div class="lock-row">
<div class="lock-copy">
<strong>Assembly constraints</strong>
<span>Axis, clearance, and swing direction</span>
</div>
<span class="lock-pill">Locked on</span>
</div>
</section>
</aside>
<aside class="right-panel" aria-label="Live checks and lesson steps">
<div class="right-title">
<span>Live checks</span>
<span class="mini-chip" id="stateChip">Ready</span>
</div>
<div class="check">
<div class="check-icon" id="axisIcon">✓</div>
<div>
<strong>Pivot axis</strong>
<small>Shared vertical datum</small>
</div>
<div class="check-value" id="axisValue">Y · locked</div>
</div>
<div class="check">
<div class="check-icon">✓</div>
<div>
<strong>Hinge stand-off</strong>
<small>Axis outside the stile face</small>
</div>
<div class="check-value">6 mm</div>
</div>
<div class="check">
<div class="check-icon" id="swingIcon">✓</div>
<div>
<strong>Swing clearance</strong>
<small>Door edge clears the carcass</small>
</div>
<div class="check-value" id="swingValue">100° clear</div>
</div>
<div class="steps">
<button class="step active" type="button" data-step="side">
<span class="step-number">01</span>
<span>
<strong>Relocate the hinge</strong>
<small>Choose either mounting stile</small>
</span>
</button>
<button class="step" type="button" data-step="axis">
<span class="step-number">02</span>
<span>
<strong>Inspect the axis</strong>
<small>Confirm the vertical Y datum</small>
</span>
</button>
<button class="step" type="button" data-step="swing">
<span class="step-number">03</span>
<span>
<strong>Test the swing</strong>
<small>Open through the full 100° arc</small>
</span>
</button>
</div>
<div class="camera-help">
Drag to orbit · scroll to zoom · click the hinge to relocate
<button id="resetCamera" type="button">Reset view</button>
</div>
</aside>
<div id="toast" class="toast" role="status" aria-live="polite">
<div class="toast-mark">✓</div>
<div>
<strong id="toastTitle">Assembly constraints enabled</strong>
<span id="toastMessage">The hinge axis and room-side swing are locked.</span>
</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene and renderer
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const labelHost = document.getElementById("labels");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute("aria-label", "3D cabinet model");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8ede7);
scene.fog = new THREE.Fog(0xe8ede7, 11, 24);
const camera = new THREE.PerspectiveCamera(
34,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 0, 0);
const desiredTarget = new THREE.Vector3(0, 0, 0);
let cameraRadius = 8.6;
let desiredRadius = 8.6;
let cameraTheta = 0.34;
let cameraPhi = 1.24;
const hemisphere = new THREE.HemisphereLight(0xf8fff8, 0x8b9e91, 2.15);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.3);
keyLight.position.set(-4.5, 7, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 20;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcce8e5, 1.15);
fillLight.position.set(5, 2, 3);
scene.add(fillLight);
const rimLight = new THREE.DirectionalLight(0xffcfaa, 1.1);
rimLight.position.set(0, 3, -6);
scene.add(rimLight);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
const materials = {
carcass: new THREE.MeshStandardMaterial({
color: 0x315b52,
roughness: 0.74,
metalness: 0.02,
flatShading: true
}),
carcassDark: new THREE.MeshStandardMaterial({
color: 0x244a43,
roughness: 0.8,
metalness: 0.02,
flatShading: true
}),
interior: new THREE.MeshStandardMaterial({
color: 0xc9c1a9,
roughness: 0.9,
metalness: 0,
flatShading: true
}),
door: new THREE.MeshStandardMaterial({
color: 0x4d7d70,
roughness: 0.68,
metalness: 0.02,
flatShading: true
}),
doorFace: new THREE.MeshStandardMaterial({
color: 0x5b8c7e,
roughness: 0.72,
metalness: 0.01,
flatShading: true
}),
doorBack: new THREE.MeshStandardMaterial({
color: 0x3d675e,
roughness: 0.82,
flatShading: true
}),
hinge: new THREE.MeshStandardMaterial({
color: 0xe46f4d,
roughness: 0.48,
metalness: 0.18,
flatShading: true
}),
brass: new THREE.MeshStandardMaterial({
color: 0xe4b650,
roughness: 0.4,
metalness: 0.48,
flatShading: true
}),
screw: new THREE.MeshStandardMaterial({
color: 0x314d47,
roughness: 0.35,
metalness: 0.65,
flatShading: true
}),
ground: new THREE.MeshStandardMaterial({
color: 0xe2e8e1,
roughness: 1,
metalness: 0
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x183b35,
transparent: true,
opacity: 0.22
});
function makeBox(parent, size, position, material, withEdges = true) {
const geometry = new THREE.BoxGeometry(...size);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
if (withEdges) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(geometry),
edgeMaterial
);
mesh.add(edges);
}
parent.add(mesh);
return mesh;
}
function makeCylinder(
parent,
radius,
height,
position,
material,
radialSegments = 8
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, radialSegments),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Cabinet dimensions. One scene unit represents approximately 100 mm.
// -------------------------------------------------------------------------
const W = 2.8;
const H = 3.25;
const D = 1.32;
const T = 0.13;
const DOOR_W = 2.68;
const DOOR_H = 3.06;
const DOOR_T = 0.12;
const DOOR_GAP = 0.06;
const AXIS_X = W / 2 + DOOR_GAP;
const DOOR_LOCAL_CENTER = DOOR_GAP + DOOR_W / 2;
const PIVOT_Z = D / 2 + 0.10;
const MAX_ANGLE = THREE.MathUtils.degToRad(100);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
materials.ground
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.73;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 48, 0xa7b5aa, 0xc4cec5);
grid.position.y = -1.725;
grid.material.transparent = true;
grid.material.opacity = 0.22;
scene.add(grid);
// -------------------------------------------------------------------------
// Exploded cabinet carcass
// -------------------------------------------------------------------------
const carcass = new THREE.Group();
scene.add(carcass);
const panelSpecs = [];
function addPanel(name, size, base, offset, material) {
const mesh = makeBox(carcass, size, base, material);
mesh.name = name;
panelSpecs.push({ mesh, base, offset });
return mesh;
}
const leftPanel = addPanel(
"Left stile",
[T, H, D],
[-W / 2 + T / 2, 0, 0],
[-0.86, 0, 0],
materials.carcass
);
const rightPanel = addPanel(
"Right stile",
[T, H, D],
[W / 2 - T / 2, 0, 0],
[0.86, 0, 0],
materials.carcass
);
const topPanel = addPanel(
"Top panel",
[W - T * 2, T, D],
[0, H / 2 - T / 2, 0],
[0, 0.78, 0],
materials.carcassDark
);
const bottomPanel = addPanel(
"Bottom panel",
[W - T * 2, T, D],
[0, -H / 2 + T / 2, 0],
[0, -0.78, 0],
materials.carcassDark
);
const backPanel = addPanel(
"Back panel",
[W - T * 2, H - T * 2, T],
[0, 0, -D / 2 + T / 2],
[0, 0, -0.95],
materials.interior
);
const shelfPanel = addPanel(
"Shelf",
[W - T * 2 - 0.04, T, D - 0.14],
[0, 0.22, 0.02],
[0.58, 0.18, 0.18],
materials.interior
);
// Recessed mounting receivers remain with their respective stiles.
function addReceiver(parent, xSign) {
for (const y of [-0.96, 0.96]) {
makeBox(
parent,
[0.055, 0.34, 0.28],
[xSign * 0.087, y, 0.48],
materials.brass
);
for (const dy of [-0.105, 0.105]) {
const screw = makeCylinder(
parent,
0.025,
0.018,
[xSign * 0.122, y + dy, 0.48],
materials.screw,
8
);
screw.rotation.z = Math.PI / 2;
}
}
}
addReceiver(leftPanel, 1);
addReceiver(rightPanel, -1);
// -------------------------------------------------------------------------
// Door, moving hinge leaf, and fixed hinge hardware
// -------------------------------------------------------------------------
const pickables = [];
const doorPivot = new THREE.Group();
scene.add(doorPivot);
const doorLeaf = new THREE.Group();
doorPivot.add(doorLeaf);
const doorPanel = makeBox(
doorLeaf,
[DOOR_W, DOOR_H, DOOR_T],
[0, 0, 0],
materials.door
);
doorPanel.userData.action = "door";
pickables.push(doorPanel);
makeBox(
doorLeaf,
[DOOR_W - 0.25, DOOR_H - 0.25, 0.025],
[0, 0, 0.073],
materials.doorFace
);
makeBox(
doorLeaf,
[DOOR_W - 0.25, DOOR_H - 0.25, 0.018],
[0, 0, -0.069],
materials.doorBack
);
const handle = new THREE.Group();
doorLeaf.add(handle);
makeBox(
handle,
[0.065, 0.46, 0.075],
[0, -0.05, 0.145],
materials.brass
);
makeBox(
handle,
[0.08, 0.075, 0.12],
[0, 0.15, 0.105],
materials.brass
);
makeBox(
handle,
[0.08, 0.075, 0.12],
[0, -0.25, 0.105],
materials.brass
);
const movingLeaf = new THREE.Group();
doorPivot.add(movingLeaf);
const movingLeafPlate = makeBox(
movingLeaf,
[0.22, 0.34, 0.042],
[0, 0, 0.025],
materials.hinge
);
const movingScrewMaterial = materials.screw;
for (const y of [-0.96, 0.96]) {
for (const dy of [-0.105, 0.105]) {
const screw = makeCylinder(
movingLeaf,
0.022,
0.016,
[0, y + dy, 0.052],
movingScrewMaterial,
8
);
screw.rotation.x = Math.PI / 2;
}
}
const hingeHardware = new THREE.Group();
scene.add(hingeHardware);
const fixedLeaf = new THREE.Group();
hingeHardware.add(fixedLeaf);
for (const y of [-0.96, 0.96]) {
makeBox(
fixedLeaf,
[0.22, 0.34, 0.045],
[0, y, 0.02],
materials.hinge
);
const barrel = makeCylinder(
hingeHardware,
0.063,
0.34,
[0, y, 0],
materials.brass,
8
);
barrel.userData.action = "hinge";
pickables.push(barrel);
const invisibleHit = new THREE.Mesh(
new THREE.SphereGeometry(0.16, 10, 8),
new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
colorWrite: false
})
);
invisibleHit.position.set(0, y, 0);
invisibleHit.userData.action = "hinge";
hingeHardware.add(invisibleHit);
pickables.push(invisibleHit);
}
// Shared vertical hinge datum.
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -1.61, 0),
new THREE.Vector3(0, 1.67, 0)
]);
const axisMaterial = new THREE.LineDashedMaterial({
color: 0xe4b650,
dashSize: 0.09,
gapSize: 0.055,
transparent: true,
opacity: 0.9,
depthTest: false
});
const axisLine = new THREE.Line(axisGeometry, axisMaterial);
axisLine.computeLineDistances();
axisLine.renderOrder = 5;
hingeHardware.add(axisLine);
const axisTop = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075),
materials.brass
);
axisTop.position.y = 1.69;
hingeHardware.add(axisTop);
const axisBottom = new THREE.Mesh(
new THREE.OctahedronGeometry(0.06),
materials.brass
);
axisBottom.position.y = -1.61;
hingeHardware.add(axisBottom);
// -------------------------------------------------------------------------
// Room-side swing envelope
// -------------------------------------------------------------------------
const ARC_SEGMENTS = 42;
const arcPositions = new Float32Array((ARC_SEGMENTS + 1) * 3);
const arcGeometry = new THREE.BufferGeometry();
arcGeometry.setAttribute(
"position",
new THREE.BufferAttribute(arcPositions, 3)
);
const arcMaterial = new THREE.LineBasicMaterial({
color: 0x4c9a75,
transparent: true,
opacity: 0.72,
depthTest: false
});
const arcLine = new THREE.Line(arcGeometry, arcMaterial);
arcLine.renderOrder = 3;
scene.add(arcLine);
const sectorPositions = new Float32Array(ARC_SEGMENTS * 9);
const sectorGeometry = new THREE.BufferGeometry();
sectorGeometry.setAttribute(
"position",
new THREE.BufferAttribute(sectorPositions, 3)
);
const sectorMaterial = new THREE.MeshBasicMaterial({
color: 0x6aaf8f,
transparent: true,
opacity: 0.08,
side: THREE.DoubleSide,
depthWrite: false
});
const swingSector = new THREE.Mesh(sectorGeometry, sectorMaterial);
swingSector.renderOrder = 2;
scene.add(swingSector);
// -------------------------------------------------------------------------
// Projected HTML labels
// -------------------------------------------------------------------------
const labels = [];
function makeLabel(text, className, anchor) {
const element = document.createElement("div");
element.className = `scene-label ${className}`;
element.textContent = text;
labelHost.appendChild(element);
const label = {
element,
anchor,
visible: true
};
labels.push(label);
return label;
}
const doorLabelAnchor = new THREE.Object3D();
doorLabelAnchor.position.set(0, 1.12, 0.1);
doorLeaf.add(doorLabelAnchor);
const axisLabelAnchor = new THREE.Object3D();
axisLabelAnchor.position.set(0, 1.78, 0);
hingeHardware.add(axisLabelAnchor);
const clearanceLabelAnchor = new THREE.Object3D();
clearanceLabelAnchor.position.set(0, 0.05, 0.13);
hingeHardware.add(clearanceLabelAnchor);
const arcLabelAnchor = new THREE.Object3D();
scene.add(arcLabelAnchor);
makeLabel("Reversible door", "", doorLabelAnchor);
makeLabel("Shared hinge axis · Y", "axis", axisLabelAnchor);
makeLabel("6 mm axis offset", "clearance", clearanceLabelAnchor);
makeLabel("100° clear swing", "arc", arcLabelAnchor);
// -------------------------------------------------------------------------
// State and UI
// -------------------------------------------------------------------------
let currentSide = -1; // -1 = left, +1 = right
let targetSide = -1;
let explodedTarget = 0;
let explodedCurrent = 0;
let angleTarget = 0;
let angleCurrent = 0;
const relocation = {
active: false,
from: -1,
to: -1,
start: 0,
duration: 0.9
};
const leftButton = document.getElementById("leftButton");
const rightButton = document.getElementById("rightButton");
const relocateButton = document.getElementById("relocateButton");
const openButton = document.getElementById("openButton");
const angleSlider = document.getElementById("angleSlider");
const angleOutput = document.getElementById("angleOutput");
const explodeSlider = document.getElementById("explodeSlider");
const explodeOutput = document.getElementById("explodeOutput");
const mountOutput = document.getElementById("mountOutput");
const stateChip = document.getElementById("stateChip");
const axisIcon = document.getElementById("axisIcon");
const axisValue = document.getElementById("axisValue");
const swingIcon = document.getElementById("swingIcon");
const swingValue = document.getElementById("swingValue");
const toast = document.getElementById("toast");
const toastTitle = document.getElementById("toastTitle");
const toastMessage = document.getElementById("toastMessage");
let toastTimer;
function showToast(title, message) {
toastTitle.textContent = title;
toastMessage.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 3000);
}
function updateRangeFill(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--progress", `${percent}%`);
}
function updateSideUI() {
const sideName = targetSide < 0 ? "Left" : "Right";
mountOutput.textContent = sideName;
leftButton.classList.toggle("active", targetSide < 0);
rightButton.classList.toggle("active", targetSide > 0);
relocateButton.textContent =
targetSide < 0 ? "Relocate to right stile" : "Relocate to left stile";
}
function updateChecks() {
if (relocation.active) {
stateChip.textContent = "Moving";
axisIcon.textContent = "…";
axisIcon.classList.add("warn");
axisValue.textContent = "Preserved";
swingIcon.textContent = "…";
swingIcon.classList.add("warn");
swingValue.textContent = "Verifying";
return;
}
stateChip.textContent = "Pass";
axisIcon.textContent = "✓";
axisIcon.classList.remove("warn");
axisValue.textContent = "Y · locked";
swingIcon.textContent = "✓";
swingIcon.classList.remove("warn");
swingValue.textContent =
angleCurrent > 0.03 ? "100° clear" : "Ready";
}
function switchSide(side) {
if (relocation.active || side === targetSide) return;
relocation.active = true;
relocation.from = currentSide;
relocation.to = side;
relocation.start = performance.now() / 1000;
targetSide = side;
angleTarget = 0;
angleSlider.value = "0";
angleOutput.textContent = "0°";
openButton.textContent = "Open to 100°";
updateSideUI();
updateChecks();
showToast(
"Relocating hinge kit",
"The door is detached while the hinge moves along the shared vertical datum."
);
}
function setDoorAngle(degrees) {
angleTarget = THREE.MathUtils.degToRad(
THREE.MathUtils.clamp(degrees, 0, 100)
);
angleSlider.value = String(Math.round(degrees));
angleOutput.textContent = `${Math.round(degrees)}°`;
openButton.textContent = degrees > 2 ? "Close door" : "Open to 100°";
updateRangeFill(angleSlider);
}
function toggleDoor() {
setDoorAngle(angleTarget > 0.03 ? 0 : 100);
}
leftButton.addEventListener("click", () => switchSide(-1));
rightButton.addEventListener("click", () => switchSide(1));
relocateButton.addEventListener("click", () => {
switchSide(targetSide < 0 ? 1 : -1);
});
openButton.addEventListener("click", toggleDoor);
angleSlider.addEventListener("input", () => {
setDoorAngle(Number(angleSlider.value));
});
explodeSlider.addEventListener("input", () => {
explodedTarget = Number(explodeSlider.value) / 100;
explodeOutput.textContent = `${explodeSlider.value}%`;
updateRangeFill(explodeSlider);
});
document.querySelectorAll(".step").forEach((button) => {
button.addEventListener("click", () => {
const step = button.dataset.step;
if (step === "side") {
switchSide(targetSide < 0 ? 1 : -1);
} else if (step === "axis") {
desiredTarget.set(currentSide * 1.08, 0.02, 0.4);
desiredRadius = 5.8;
showToast(
"Inspect the vertical datum",
"Both hinge barrels share one continuous Y-axis through the cabinet height."
);
} else if (step === "swing") {
toggleDoor();
}
});
});
updateSideUI();
updateRangeFill(angleSlider);
updateRangeFill(explodeSlider);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, reset, and click picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const canvas = renderer.domElement;
let pointerDown = false;
let pointerMoved = false;
let pointerStartX = 0;
let pointerStartY = 0;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = false;
pointerStartX = event.clientX;
pointerStartY = event.clientY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (
Math.hypot(
event.clientX - pointerStartX,
event.clientY - pointerStartY
) > 4
) {
pointerMoved = true;
}
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.55,
1.48
);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
canvas.classList.remove("dragging");
if (pointerMoved) return;
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
if (intersections.length) {
const action = intersections[0].object.userData.action;
if (action === "hinge") {
switchSide(targetSide < 0 ? 1 : -1);
} else if (action === "door") {
toggleDoor();
}
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
canvas.classList.remove("dragging");
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desiredRadius = THREE.MathUtils.clamp(
desiredRadius + event.deltaY * 0.008,
5.1,
13.5
);
},
{ passive: false }
);
document.getElementById("resetCamera").addEventListener("click", () => {
desiredTarget.set(0, 0, 0);
desiredRadius = 8.6;
cameraTheta = 0.34;
cameraPhi = 1.24;
});
// -------------------------------------------------------------------------
// Animation helpers
// -------------------------------------------------------------------------
function smoothstep(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function updateSwingEnvelope(pivotX, pivotZ, side) {
const y = -1.14;
for (let i = 0; i <= ARC_SEGMENTS; i++) {
const fraction = i / ARC_SEGMENTS;
const angle = fraction * MAX_ANGLE;
const localX = -side * (DOOR_GAP + DOOR_W);
const x = pivotX + localX * Math.cos(angle);
const z = pivotZ - localX * Math.sin(side * angle);
arcPositions[i * 3] = x;
arcPositions[i * 3 + 1] = y;
arcPositions[i * 3 + 2] = z;
}
arcGeometry.attributes.position.needsUpdate = true;
arcGeometry.computeBoundingSphere();
for (let i = 0; i < ARC_SEGMENTS; i++) {
const a = i * 3;
const b = (i + 1) * 3;
const offset = i * 9;
sectorPositions[offset] = pivotX;
sectorPositions[offset + 1] = y;
sectorPositions[offset + 2] = pivotZ;
sectorPositions[offset + 3] = arcPositions[a];
sectorPositions[offset + 4] = y;
sectorPositions[offset + 5] = arcPositions[a + 2];
sectorPositions[offset + 6] = arcPositions[b];
sectorPositions[offset + 7] = y;
sectorPositions[offset + 8] = arcPositions[b + 2];
}
sectorGeometry.attributes.position.needsUpdate = true;
sectorGeometry.computeBoundingSphere();
const labelAngle = MAX_ANGLE * 0.57;
const labelLocalX = -side * (DOOR_GAP + DOOR_W);
arcLabelAnchor.position.set(
pivotX + labelLocalX * Math.cos(labelAngle),
y + 0.03,
pivotZ - labelLocalX * Math.sin(side * labelAngle)
);
}
function updateCamera() {
cameraTarget.lerp(desiredTarget, 0.08);
cameraRadius = THREE.MathUtils.lerp(cameraRadius, desiredRadius, 0.1);
const horizontalRadius =
cameraRadius * Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + horizontalRadius * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
label.anchor.getWorldPosition(projected);
projected.project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.08 &&
projected.x < 1.08 &&
projected.y > -1.08 &&
projected.y < 1.08;
label.element.style.opacity = onScreen ? "1" : "0";
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// -------------------------------------------------------------------------
// Main animation loop
// -------------------------------------------------------------------------
let previousTime = performance.now() / 1000;
let checkAccumulator = 0;
function animate() {
const now = performance.now() / 1000;
const dt = Math.min(now - previousTime, 0.05);
previousTime = now;
const smoothing = 1 - Math.exp(-dt * 7.5);
explodedCurrent = THREE.MathUtils.lerp(
explodedCurrent,
explodedTarget,
smoothing
);
angleCurrent = THREE.MathUtils.lerp(
angleCurrent,
angleTarget,
smoothing
);
// Relocate the hinge and reattach the detached door.
let pivotX = currentSide * AXIS_X;
let detachedAmount = 0;
if (relocation.active) {
const progress = THREE.MathUtils.clamp(
(now - relocation.start) / relocation.duration,
0,
1
);
const eased = smoothstep(progress);
pivotX = THREE.MathUtils.lerp(
relocation.from * AXIS_X,
relocation.to * AXIS_X,
eased
);
detachedAmount = Math.sin(progress * Math.PI) * 0.55;
const oldWorldCenter =
-relocation.from * (AXIS_X + DOOR_LOCAL_CENTER);
const newWorldCenter =
-relocation.to * (AXIS_X + DOOR_LOCAL_CENTER);
let doorWorldCenter;
if (progress < 0.55) {
doorWorldCenter = THREE.MathUtils.lerp(
oldWorldCenter,
0,
smoothstep(progress / 0.55)
);
} else {
doorWorldCenter = THREE.MathUtils.lerp(
0,
newWorldCenter,
smoothstep((progress - 0.55) / 0.45)
);
}
doorLeaf.position.x = doorWorldCenter - pivotX;
if (progress >= 1) {
relocation.active = false;
currentSide = relocation.to;
targetSide = relocation.to;
pivotX = currentSide * AXIS_X;
doorLeaf.position.x = -currentSide * DOOR_LOCAL_CENTER;
updateSideUI();
updateChecks();
showToast(
`Hinge kit seated on the ${currentSide < 0 ? "left" : "right"} stile`,
"The shared Y-axis, 6 mm stand-off, and room-side opening direction are unchanged."
);
}
} else {
currentSide = targetSide;
pivotX = currentSide * AXIS_X;
doorLeaf.position.x = -currentSide * DOOR_LOCAL_CENTER;
}
const pivotZ =
PIVOT_Z + explodedCurrent * 1.04 + detachedAmount;
doorPivot.position.set(pivotX, 0, pivotZ);
hingeHardware.position.set(pivotX, 0, pivotZ);
doorPivot.rotation.y = relocation.active
? 0
: currentSide * angleCurrent;
// Hardware direction follows the selected mounting side.
fixedLeaf.position.x = -currentSide * 0.105;
movingLeaf.position.x = -currentSide * 0.105;
handle.position.x = -currentSide * (DOOR_W / 2 - 0.18);
// Explode the carcass while keeping every panel aligned to its datum.
for (const panel of panelSpecs) {
panel.mesh.position.set(
panel.base[0] + panel.offset[0] * explodedCurrent,
panel.base[1] + panel.offset[1] * explodedCurrent,
panel.base[2] + panel.offset[2] * explodedCurrent
);
}
updateSwingEnvelope(pivotX, pivotZ, currentSide);
const envelopeVisibility = THREE.MathUtils.clamp(
0.35 + explodedCurrent * 0.65 + angleCurrent * 2.2,
0.35,
1
);
arcMaterial.opacity = 0.72 * envelopeVisibility;
sectorMaterial.opacity = 0.08 * envelopeVisibility;
updateCamera();
updateLabels();
checkAccumulator += dt;
if (checkAccumulator > 0.12) {
updateChecks();
checkAccumulator = 0;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
showToast(
"Assembly constraints enabled",
"Choose a hinge side, inspect the vertical axis, then test the full swing."
);
</script>
</body>
</html>
```