Prompt specification
Build or customize the Three.js 3D scene "Cupboard door adjustment corner" 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: Cupboard door adjustment corner
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/cupboard-door-adjustment-corner-x0275
Scene Description Prompt:
Build a cabinet with an uneven door gap and exposed hinges; small alignment controls restore parallel edges and allow the door to close without intersecting the frame. Build this with Three.js from cdnjs as a voxel scene in one self-contained HTML file.
Working Three.js Starter (cupboard-door-adjustment-corner-x0275):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Cabinet — Hinge Alignment Lab</title>
<style>
:root {
--ink: #18312f;
--muted: #657a76;
--paper: rgba(247, 248, 242, 0.92);
--line: rgba(24, 49, 47, 0.14);
--teal: #287a70;
--mint: #68b7a8;
--amber: #d99538;
--red: #c65349;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #fbfaf4 0%, #e8e7de 58%, #d9ddd7 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.brand {
position: absolute;
top: 22px;
left: 24px;
display: flex;
align-items: center;
gap: 13px;
}
.brand-mark {
width: 38px;
height: 38px;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 3px;
transform: rotate(-2deg);
}
.brand-mark i {
display: block;
background: var(--teal);
border-radius: 2px;
}
.brand-mark i:nth-child(2),
.brand-mark i:nth-child(4),
.brand-mark i:nth-child(8) {
background: var(--mint);
}
.brand-mark i:nth-child(5),
.brand-mark i:nth-child(6) {
background: #d7e6df;
}
.eyebrow {
margin: 0 0 3px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: 18px;
line-height: 1;
letter-spacing: -0.025em;
}
.status {
position: absolute;
top: 24px;
right: 24px;
display: inline-flex;
align-items: center;
gap: 9px;
min-height: 34px;
padding: 0 13px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--paper);
box-shadow: 0 8px 30px rgba(42, 55, 49, 0.08);
backdrop-filter: blur(12px);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status::before {
content: "";
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--mint);
box-shadow: 0 0 0 4px rgba(104, 183, 168, 0.16);
}
.status.warning::before {
background: var(--amber);
box-shadow: 0 0 0 4px rgba(217, 149, 56, 0.15);
}
.status.danger::before {
background: var(--red);
box-shadow: 0 0 0 4px rgba(198, 83, 73, 0.15);
}
.diagnostics {
position: absolute;
top: 76px;
right: 24px;
width: 224px;
padding: 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 16px 50px rgba(42, 55, 49, 0.09);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.diagnostics h2 {
margin: 0 0 4px;
font-size: 12px;
letter-spacing: -0.01em;
}
.diagnostics p {
margin: 0 0 14px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.metric {
display: grid;
grid-template-columns: 1fr auto;
gap: 5px 10px;
margin-top: 11px;
font-size: 10px;
font-weight: 750;
}
.metric span:nth-child(2) {
font-variant-numeric: tabular-nums;
}
.meter {
grid-column: 1 / -1;
height: 4px;
overflow: hidden;
border-radius: 99px;
background: rgba(24, 49, 47, 0.08);
}
.meter i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
background: var(--mint);
transition: width 0.12s linear, background 0.2s ease;
}
.legend {
display: flex;
gap: 13px;
margin-top: 15px;
padding-top: 12px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend span {
display: flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 13px;
height: 2px;
background: #efb45e;
}
.legend span:last-child i {
background: #37b8a6;
}
.controls {
position: absolute;
left: 50%;
bottom: 22px;
width: min(920px, calc(100vw - 40px));
padding: 15px;
transform: translateX(-50%);
border: 1px solid var(--line);
border-radius: 20px;
background: rgba(248, 249, 244, 0.94);
box-shadow: 0 20px 65px rgba(42, 55, 49, 0.16);
backdrop-filter: blur(18px);
pointer-events: auto;
}
.control-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
margin-bottom: 12px;
}
.control-copy strong {
display: block;
font-size: 12px;
}
.control-copy span {
color: var(--muted);
font-size: 10px;
}
.actions {
display: flex;
gap: 7px;
flex-wrap: wrap;
justify-content: flex-end;
}
button {
min-height: 34px;
padding: 0 13px;
border: 1px solid rgba(24, 49, 47, 0.15);
border-radius: 9px;
background: rgba(255, 255, 255, 0.76);
color: var(--ink);
cursor: pointer;
font: inherit;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
transition: transform 0.16s ease, background 0.16s ease, border-color 0.16s ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(40, 122, 112, 0.45);
background: white;
}
button:active {
transform: translateY(0);
}
button.primary {
border-color: var(--teal);
background: var(--teal);
color: white;
}
button.primary:hover {
background: #216c63;
}
.sliders {
display: grid;
grid-template-columns: 1.35fr 1fr 1fr 1fr;
gap: 15px;
}
.slider {
min-width: 0;
}
.slider-label {
display: flex;
justify-content: space-between;
gap: 8px;
margin-bottom: 7px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.075em;
text-transform: uppercase;
}
.slider-label output {
color: var(--teal);
font-variant-numeric: tabular-nums;
letter-spacing: 0;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 5px 0;
appearance: none;
border-radius: 99px;
outline: none;
background:
linear-gradient(
to right,
var(--teal) 0 var(--progress, 50%),
rgba(24, 49, 47, 0.12) var(--progress, 50%) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 1px 6px rgba(22, 60, 54, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 11px;
height: 11px;
border: 3px solid white;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 1px 6px rgba(22, 60, 54, 0.28);
}
.toggle {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--muted);
cursor: pointer;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
}
.toggle input {
accent-color: var(--teal);
}
.camera-hint {
position: absolute;
right: 25px;
bottom: 23px;
color: rgba(24, 49, 47, 0.52);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.shortcut {
position: absolute;
left: 25px;
bottom: 25px;
color: rgba(24, 49, 47, 0.45);
font-size: 9px;
line-height: 1.65;
}
kbd {
display: inline-block;
min-width: 18px;
padding: 1px 4px;
border: 1px solid rgba(24, 49, 47, 0.16);
border-radius: 4px;
background: rgba(255, 255, 255, 0.44);
color: var(--ink);
text-align: center;
font: inherit;
}
@media (max-width: 800px) {
.diagnostics,
.camera-hint,
.shortcut {
display: none;
}
.status {
top: 77px;
right: 16px;
}
.brand {
top: 16px;
left: 16px;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
.sliders {
grid-template-columns: 1fr 1fr;
gap: 11px 16px;
}
.control-top {
align-items: flex-start;
flex-direction: column;
}
.actions {
width: 100%;
justify-content: flex-start;
}
}
@media (max-width: 470px) {
.actions button {
flex: 1;
padding: 0 7px;
}
.control-copy span {
display: none;
}
}
</style>
</head>
<body>
<div class="hud">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i><i></i>
</div>
<div>
<p class="eyebrow">Cabinet / 04</p>
<h1>Hinge Alignment Lab</h1>
</div>
</div>
<div id="status" class="status warning" aria-live="polite">Uneven gap</div>
<aside class="diagnostics">
<h2>Closed-plane clearance</h2>
<p>Live projection of the door perimeter against the frame opening.</p>
<div class="metric">
<span>TOP</span><span id="topValue">—</span>
<div class="meter"><i id="topBar"></i></div>
</div>
<div class="metric">
<span>BOTTOM</span><span id="bottomValue">—</span>
<div class="meter"><i id="bottomBar"></i></div>
</div>
<div class="metric">
<span>HINGE SIDE</span><span id="hingeValue">—</span>
<div class="meter"><i id="hingeBar"></i></div>
</div>
<div class="metric">
<span>LATCH SIDE</span><span id="latchValue">—</span>
<div class="meter"><i id="latchBar"></i></div>
</div>
<div class="legend">
<span><i></i> Frame</span>
<span><i></i> Door</span>
</div>
</aside>
<section class="controls">
<div class="control-top">
<div class="control-copy">
<strong>Fine adjustment</strong>
<span>Close safely aligns the hinge before the door reaches the frame.</span>
</div>
<div class="actions">
<label class="toggle">
<input id="gapToggle" type="checkbox" checked />
Gap map
</label>
<button id="alignBtn">Align hinges</button>
<button id="openBtn">Open</button>
<button id="closeBtn" class="primary">Align + close</button>
</div>
</div>
<div class="sliders">
<div class="slider">
<div class="slider-label">
<label for="angle">Door swing</label>
<output id="angleOut">9°</output>
</div>
<input id="angle" type="range" min="0" max="72" step="1" value="9" />
</div>
<div class="slider">
<div class="slider-label">
<label for="tilt">Hinge tilt</label>
<output id="tiltOut">+0.32°</output>
</div>
<input id="tilt" type="range" min="-0.9" max="0.9" step="0.01" value="0.32" />
</div>
<div class="slider">
<div class="slider-label">
<label for="lift">Vertical trim</label>
<output id="liftOut">−7.5 mm</output>
</div>
<input id="lift" type="range" min="-12" max="12" step="0.5" value="-7.5" />
</div>
<div class="slider">
<div class="slider-label">
<label for="depth">Depth seat</label>
<output id="depthOut">+1.5 mm</output>
</div>
<input id="depth" type="range" min="-12" max="12" step="0.5" value="1.5" />
</div>
</div>
</section>
<div class="shortcut">
<kbd>A</kbd> align <kbd>C</kbd> close <kbd>O</kbd> open
</div>
<div class="camera-hint">Drag to orbit · scroll to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
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.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.13;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive voxel cabinet scene");
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-5, 5, 5, -5, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, 3.6, 0.25);
let cameraAzimuth = 0.69;
let cameraPolar = 1.14;
let cameraZoom = 1;
function updateCamera() {
const radius = 14;
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + radius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + radius * Math.cos(cameraPolar),
cameraTarget.z + radius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Keep the full cabinet visible on narrow displays.
const halfHeight = Math.max(4.85 / cameraZoom, 4.35 / aspect);
const halfWidth = halfHeight * aspect;
camera.left = -halfWidth;
camera.right = halfWidth;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
updateCamera();
resize();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf8f5e9, 0x78908a, 2.15));
const keyLight = new THREE.DirectionalLight(0xfff3d9, 4.0);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x8dd8ca, 1.35);
rimLight.position.set(8, 6, -6);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb66d, 14, 16, 2);
warmLight.position.set(-5, 4, 5);
scene.add(warmLight);
// ------------------------------------------------------------
// Instanced voxel builder
// ------------------------------------------------------------
const unitCube = new THREE.BoxGeometry(1, 1, 1);
class VoxelBatch {
constructor() {
this.entries = [];
}
add(x, y, z, sx, sy, sz, color) {
this.entries.push({ x, y, z, sx, sy, sz, color });
}
build(materialOptions = {}) {
if (!this.entries.length) return null;
const material = new THREE.MeshStandardMaterial({
roughness: 0.79,
metalness: 0.02,
vertexColors: true,
...materialOptions
});
const mesh = new THREE.InstancedMesh(
unitCube,
material,
this.entries.length
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
this.entries.forEach((entry, index) => {
dummy.position.set(entry.x, entry.y, entry.z);
dummy.scale.set(entry.sx, entry.sy, entry.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, color.set(entry.color));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
}
function addGrid(
batch,
originX,
originY,
originZ,
columns,
rows,
layers,
cell,
palette,
gap = 0.026
) {
for (let layer = 0; layer < layers; layer++) {
for (let row = 0; row < rows; row++) {
for (let column = 0; column < columns; column++) {
let color;
if (typeof palette === "function") {
color = palette(column, row, layer);
} else if (Array.isArray(palette)) {
const hash =
column * 17 + row * 31 + layer * 47;
color = palette[hash % palette.length];
} else {
color = palette;
}
batch.add(
originX + (column + 0.5) * cell,
originY + (row + 0.5) * cell,
originZ + (layer + 0.5) * cell,
cell - gap,
cell - gap,
cell - gap,
color
);
}
}
}
}
function addBox(
parent,
x,
y,
z,
sx,
sy,
sz,
color,
materialOptions = {}
) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.58,
metalness: 0.16,
...materialOptions
});
const mesh = new THREE.Mesh(unitCube, material);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Dimensions
// ------------------------------------------------------------
const CELL = 0.3;
const FRAME_LEFT = -2.55;
const FRAME_RIGHT = 2.55;
const FRAME_BOTTOM = 0.9;
const FRAME_TOP = 6.6;
const FRAME_FRONT_Z = 1.2;
const DOOR_WIDTH = 4.9;
const DOOR_HEIGHT = 5.1;
const DOOR_CENTER_Y = 3.75;
const HINGE_X = 2.5;
const HINGE_Z = 1.42;
const MM_PER_UNIT = 75;
// ------------------------------------------------------------
// Voxel floor
// ------------------------------------------------------------
const floorBatch = new VoxelBatch();
const floorColors = ["#d8ddd6", "#dfe2dc", "#d2d8d1", "#e5e5de"];
addGrid(
floorBatch,
-9.1,
-0.1,
-8.45,
28,
28,
1,
0.65,
(column, row) =>
floorColors[(column * 3 + row * 5) % floorColors.length],
0.055
);
const floor = floorBatch.build({ roughness: 0.95 });
floor.receiveShadow = true;
scene.add(floor);
// ------------------------------------------------------------
// Cabinet carcass
// ------------------------------------------------------------
const shellBatch = new VoxelBatch();
const innerBatch = new VoxelBatch();
const frameBatch = new VoxelBatch();
const walnut = ["#694332", "#754c37", "#80533c", "#704735"];
const frameWood = ["#b2784f", "#bf8358", "#aa6c47", "#c58e62"];
const interior = ["#315451", "#385e59", "#2d4d4b", "#416863"];
// Back wall.
addGrid(
innerBatch,
-3.15,
0.3,
-0.9,
21,
23,
1,
CELL,
interior,
0.035
);
// Outer shell.
addGrid(
shellBatch,
-3.15,
0.3,
-0.9,
2,
23,
7,
CELL,
walnut
);
addGrid(
shellBatch,
2.55,
0.3,
-0.9,
2,
23,
7,
CELL,
walnut
);
addGrid(
shellBatch,
-3.15,
0.3,
-0.9,
21,
2,
7,
CELL,
walnut
);
addGrid(
shellBatch,
-3.15,
6.6,
-0.9,
21,
2,
7,
CELL,
walnut
);
// Front frame, defining the exact opening.
addGrid(
frameBatch,
-3.15,
0.9,
1.02,
2,
19,
1,
CELL,
frameWood
);
addGrid(
frameBatch,
2.55,
0.9,
1.02,
2,
19,
1,
CELL,
frameWood
);
addGrid(
frameBatch,
-3.15,
0.9,
1.02,
21,
2,
1,
CELL,
frameWood
);
addGrid(
frameBatch,
-3.15,
6.6,
1.02,
21,
2,
1,
CELL,
frameWood
);
// Interior shelves.
addGrid(
innerBatch,
-2.55,
2.4,
-0.65,
17,
1,
3,
CELL,
["#456c64", "#3d625c", "#4d776e"]
);
addGrid(
innerBatch,
-2.55,
4.25,
-0.65,
17,
1,
3,
CELL,
["#456c64", "#3d625c", "#4d776e"]
);
scene.add(shellBatch.build());
scene.add(innerBatch.build());
scene.add(frameBatch.build());
// Voxel feet.
const feetBatch = new VoxelBatch();
for (const x of [-2.55, 2.55]) {
for (const z of [-0.65, 0.65]) {
feetBatch.add(x, 0.15, z, 0.34, 0.3, 0.34, "#49352d");
}
}
scene.add(feetBatch.build({ roughness: 0.9 }));
// Small voxel objects visible through the opening.
const propsBatch = new VoxelBatch();
// Books on the upper shelf.
propsBatch.add(-1.65, 4.72, -0.34, 0.22, 0.34, 0.28, "#c9694d");
propsBatch.add(-1.38, 4.72, -0.34, 0.22, 0.34, 0.28, "#dfb65e");
propsBatch.add(-1.11, 4.72, -0.34, 0.22, 0.34, 0.28, "#6f8fba");
// Stacked storage blocks on the lower shelf.
propsBatch.add(1.08, 2.87, -0.3, 0.46, 0.32, 0.42, "#c58e62");
propsBatch.add(1.08, 3.17, -0.3, 0.34, 0.28, 0.34, "#739e91");
scene.add(propsBatch.build({ roughness: 0.86 }));
// ------------------------------------------------------------
// Door and hinge rig
// ------------------------------------------------------------
const hingePivot = new THREE.Group();
hingePivot.position.set(HINGE_X, 0, HINGE_Z);
scene.add(hingePivot);
const doorAssembly = new THREE.Group();
doorAssembly.position.set(-DOOR_WIDTH / 2, DOOR_CENTER_Y, 0);
hingePivot.add(doorAssembly);
// Dark backing makes the individual door voxels read as a panel.
addBox(
doorAssembly,
0,
0,
-0.105,
DOOR_WIDTH - 0.035,
DOOR_HEIGHT - 0.035,
0.07,
"#294d49",
{ roughness: 0.9 }
);
const doorBatch = new VoxelBatch();
const doorColors = [
"#4f8178",
"#568a7f",
"#47766f",
"#5c9185",
"#527f77"
];
const doorColumns = 17;
const doorRows = 17;
const doorCellX = 0.288;
const doorCellY = 0.3;
for (let row = 0; row < doorRows; row++) {
for (let column = 0; column < doorColumns; column++) {
const edge =
column === 0 ||
column === doorColumns - 1 ||
row === 0 ||
row === doorRows - 1;
const hash = column * 13 + row * 19;
const color = edge
? (hash % 3 === 0 ? "#8bb6a8" : "#7eaa9c")
: doorColors[hash % doorColors.length];
doorBatch.add(
(column - 8) * doorCellX,
(row - 8) * doorCellY,
0,
doorCellX - 0.012,
doorCellY - 0.012,
0.16,
color
);
}
}
const doorVoxels = doorBatch.build({ roughness: 0.72 });
doorAssembly.add(doorVoxels);
// Brass voxel handle.
const handleBatch = new VoxelBatch();
handleBatch.add(-1.88, 0.18, 0.2, 0.13, 0.32, 0.13, "#d5a452");
handleBatch.add(-1.88, 0.02, 0.28, 0.18, 0.13, 0.24, "#e0b45e");
handleBatch.add(-1.88, 0.34, 0.28, 0.18, 0.13, 0.24, "#e0b45e");
handleBatch.add(-1.88, 0.18, 0.4, 0.13, 0.13, 0.13, "#efc871");
const movingHardware = handleBatch.build({
roughness: 0.42,
metalness: 0.52
});
doorAssembly.add(movingHardware);
// Moving hinge leaves are part of the tilted door.
const movingHingeBatch = new VoxelBatch();
for (const hingeY of [1.45, 6.05]) {
movingHingeBatch.add(
DOOR_WIDTH / 2 - 0.18,
hingeY - DOOR_CENTER_Y,
0.035,
0.36,
0.18,
0.1,
"#aebbb5"
);
movingHingeBatch.add(
DOOR_WIDTH / 2 - 0.06,
hingeY - DOOR_CENTER_Y,
0.035,
0.12,
0.18,
0.1,
"#7e8f8b"
);
}
const movingHinges = movingHingeBatch.build({
roughness: 0.38,
metalness: 0.68
});
doorAssembly.add(movingHinges);
// Exposed fixed hardware.
const fixedHardware = new THREE.Group();
scene.add(fixedHardware);
for (const hingeY of [1.45, 6.05]) {
// Leaf attached to the cabinet frame.
addBox(
fixedHardware,
2.69,
hingeY,
1.28,
0.34,
0.18,
0.11,
"#aebbb5",
{ roughness: 0.38, metalness: 0.68 }
);
// Small bridge toward the pivot.
addBox(
fixedHardware,
2.55,
hingeY,
1.36,
0.15,
0.16,
0.15,
"#7e8f8b",
{ roughness: 0.38, metalness: 0.68 }
);
// Stacked square knuckles.
for (let i = 0; i < 6; i++) {
addBox(
fixedHardware,
HINGE_X,
hingeY - 0.275 + i * 0.11,
HINGE_Z,
0.14,
0.095,
0.14,
i % 2 === 0 ? "#d3d9d2" : "#778b86",
{ roughness: 0.34, metalness: 0.72 }
);
}
// Vertical pin.
addBox(
fixedHardware,
HINGE_X,
hingeY,
HINGE_Z,
0.065,
0.75,
0.065,
"#657b77",
{ roughness: 0.3, metalness: 0.8 }
);
}
// ------------------------------------------------------------
// Gap map: frame outline and transformed door outline
// ------------------------------------------------------------
const gapPositions = new Float32Array(16 * 3);
const gapColors = new Float32Array(16 * 3);
const gapGeometry = new THREE.BufferGeometry();
gapGeometry.setAttribute(
"position",
new THREE.BufferAttribute(gapPositions, 3)
);
gapGeometry.setAttribute(
"color",
new THREE.BufferAttribute(gapColors, 3)
);
const gapMaterial = new THREE.LineBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.92,
depthTest: false
});
const gapLines = new THREE.LineSegments(gapGeometry, gapMaterial);
gapLines.renderOrder = 20;
scene.add(gapLines);
// ------------------------------------------------------------
// Adjustment state
// ------------------------------------------------------------
const state = {
angle: THREE.MathUtils.degToRad(9),
tilt: THREE.MathUtils.degToRad(0.32),
lift: -0.1,
depth: 0.02
};
const target = { ...state };
let pendingClose = false;
const ui = {
angle: document.querySelector("#angle"),
tilt: document.querySelector("#tilt"),
lift: document.querySelector("#lift"),
depth: document.querySelector("#depth"),
angleOut: document.querySelector("#angleOut"),
tiltOut: document.querySelector("#tiltOut"),
liftOut: document.querySelector("#liftOut"),
depthOut: document.querySelector("#depthOut"),
status: document.querySelector("#status"),
gapToggle: document.querySelector("#gapToggle"),
alignBtn: document.querySelector("#alignBtn"),
openBtn: document.querySelector("#openBtn"),
closeBtn: document.querySelector("#closeBtn")
};
function signed(value, decimals = 1) {
const rounded = Number(value).toFixed(decimals);
return Number(rounded) > 0 ? `+${rounded}` : rounded;
}
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const progress = ((value - min) / (max - min)) * 100;
input.style.setProperty("--progress", `${progress}%`);
}
function updateOutputs() {
ui.angleOut.textContent = `${ui.angle.value}°`;
ui.tiltOut.textContent = `${signed(Number(ui.tilt.value), 2)}°`;
ui.liftOut.textContent = `${signed(Number(ui.lift.value), 1)} mm`;
ui.depthOut.textContent = `${signed(Number(ui.depth.value), 1)} mm`;
Object.values(ui)
.filter((element) => element instanceof HTMLInputElement)
.forEach(updateRangeFill);
}
function setAlignmentControls(tilt, liftMm, depthMm) {
ui.tilt.value = tilt;
ui.lift.value = liftMm;
ui.depth.value = depthMm;
updateOutputs();
}
function computeClearance(values = state) {
const halfWidth = DOOR_WIDTH / 2;
const halfHeight = DOOR_HEIGHT / 2;
const cos = Math.cos(values.tilt);
const sin = Math.sin(values.tilt);
const centerX = HINGE_X - DOOR_WIDTH / 2;
const centerY = DOOR_CENTER_Y + values.lift;
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;
for (const x of [-halfWidth, halfWidth]) {
for (const y of [-halfHeight, halfHeight]) {
const rotatedX = x * cos - y * sin;
const rotatedY = x * sin + y * cos;
const worldX = centerX + rotatedX;
const worldY = centerY + rotatedY;
minX = Math.min(minX, worldX);
maxX = Math.max(maxX, worldX);
minY = Math.min(minY, worldY);
maxY = Math.max(maxY, worldY);
}
}
const top = (FRAME_TOP - maxY) * MM_PER_UNIT;
const bottom = (minY - FRAME_BOTTOM) * MM_PER_UNIT;
const hinge = (FRAME_RIGHT - maxX) * MM_PER_UNIT;
const latch = (minX - FRAME_LEFT) * MM_PER_UNIT;
const doorBackZ = HINGE_Z + values.depth - 0.085;
const depthConflict = doorBackZ < FRAME_FRONT_Z - 0.002;
const collision =
top < 0 ||
bottom < 0 ||
hinge < 0 ||
latch < 0 ||
depthConflict;
return { top, bottom, hinge, latch, collision, depthConflict };
}
function updateMetric(name, value) {
const valueElement = document.querySelector(`#${name}Value`);
const barElement = document.querySelector(`#${name}Bar`);
valueElement.textContent = `${value.toFixed(1)} mm`;
barElement.style.width = `${Math.max(0, Math.min(100, (value / 32) * 100))}%`;
const color =
value < 0
? "var(--red)"
: value < 4
? "var(--amber)"
: "var(--mint)";
barElement.style.background = color;
}
function updateStatus() {
const clearance = computeClearance(state);
const uneven =
Math.abs(clearance.top - clearance.bottom) > 3.2 ||
Math.abs(state.tilt) > THREE.MathUtils.degToRad(0.025);
ui.status.className = "status";
if (clearance.collision) {
ui.status.classList.add("danger");
ui.status.textContent = "Frame conflict";
} else if (uneven) {
ui.status.classList.add("warning");
ui.status.textContent = "Uneven gap";
} else if (target.angle < THREE.MathUtils.degToRad(1)) {
ui.status.textContent = "Closed · parallel";
} else {
ui.status.textContent = pendingClose ? "Aligning hinge" : "Clearance OK";
}
updateMetric("top", clearance.top);
updateMetric("bottom", clearance.bottom);
updateMetric("hinge", clearance.hinge);
updateMetric("latch", clearance.latch);
}
ui.angle.addEventListener("input", () => {
pendingClose = false;
const requestedAngle = THREE.MathUtils.degToRad(Number(ui.angle.value));
// A badly seated door is prevented from latching against the frame.
if (requestedAngle < THREE.MathUtils.degToRad(8)) {
const projected = {
...target,
angle: requestedAngle,
tilt: THREE.MathUtils.degToRad(Number(ui.tilt.value)),
lift: Number(ui.lift.value) / MM_PER_UNIT,
depth: Number(ui.depth.value) / MM_PER_UNIT
};
if (computeClearance(projected).collision) {
ui.angle.value = 8;
ui.status.className = "status danger";
ui.status.textContent = "Align depth before closing";
}
}
target.angle = THREE.MathUtils.degToRad(Number(ui.angle.value));
updateOutputs();
});
ui.tilt.addEventListener("input", () => {
pendingClose = false;
target.tilt = THREE.MathUtils.degToRad(Number(ui.tilt.value));
updateOutputs();
});
ui.lift.addEventListener("input", () => {
pendingClose = false;
target.lift = Number(ui.lift.value) / MM_PER_UNIT;
updateOutputs();
});
ui.depth.addEventListener("input", () => {
pendingClose = false;
target.depth = Number(ui.depth.value) / MM_PER_UNIT;
updateOutputs();
});
function alignHinges() {
pendingClose = false;
target.tilt = 0;
target.lift = 0;
target.depth = 0;
setAlignmentControls(0, 0, 0);
}
function openDoor() {
pendingClose = false;
target.angle = THREE.MathUtils.degToRad(48);
ui.angle.value = 48;
updateOutputs();
}
function closeSafely() {
pendingClose = true;
target.tilt = 0;
target.lift = 0;
target.depth = 0;
setAlignmentControls(0, 0, 0);
}
ui.alignBtn.addEventListener("click", alignHinges);
ui.openBtn.addEventListener("click", openDoor);
ui.closeBtn.addEventListener("click", closeSafely);
ui.gapToggle.addEventListener("change", () => {
gapLines.visible = ui.gapToggle.checked;
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
const key = event.key.toLowerCase();
if (key === "a") alignHinges();
if (key === "o") openDoor();
if (key === "c") closeSafely();
});
// ------------------------------------------------------------
// Custom camera controls — no Three.js addons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
let dragDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
dragDistance = 0;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
dragDistance += Math.abs(deltaX) + Math.abs(deltaY);
cameraAzimuth -= deltaX * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - deltaY * 0.005,
0.72,
1.43
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
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();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * (event.deltaY > 0 ? 0.92 : 1.08),
0.72,
1.48
);
resize();
},
{ passive: false }
);
// ------------------------------------------------------------
// Gap overlay update
// ------------------------------------------------------------
const frameOutline = [
new THREE.Vector3(FRAME_LEFT, FRAME_BOTTOM, 1.58),
new THREE.Vector3(FRAME_RIGHT, FRAME_BOTTOM, 1.58),
new THREE.Vector3(FRAME_RIGHT, FRAME_TOP, 1.58),
new THREE.Vector3(FRAME_LEFT, FRAME_TOP, 1.58)
];
const doorLocalOutline = [
new THREE.Vector3(-DOOR_WIDTH / 2, -DOOR_HEIGHT / 2, 0.1),
new THREE.Vector3(DOOR_WIDTH / 2, -DOOR_HEIGHT / 2, 0.1),
new THREE.Vector3(DOOR_WIDTH / 2, DOOR_HEIGHT / 2, 0.1),
new THREE.Vector3(-DOOR_WIDTH / 2, DOOR_HEIGHT / 2, 0.1)
];
const outlinePairs = [
[0, 1],
[1, 2],
[2, 3],
[3, 0]
];
function updateGapOverlay() {
doorAssembly.updateWorldMatrix(true, false);
const doorOutline = doorLocalOutline.map((point) =>
doorAssembly.localToWorld(point.clone())
);
let vertex = 0;
const writeVertex = (point, color) => {
gapPositions[vertex * 3] = point.x;
gapPositions[vertex * 3 + 1] = point.y;
gapPositions[vertex * 3 + 2] = point.z;
gapColors[vertex * 3] = color.r;
gapColors[vertex * 3 + 1] = color.g;
gapColors[vertex * 3 + 2] = color.b;
vertex++;
};
const clearance = computeClearance(state);
const frameColors = [
new THREE.Color(clearance.bottom < 4 ? "#d97055" : "#e4ad55"),
new THREE.Color(clearance.hinge < 4 ? "#d97055" : "#e4ad55"),
new THREE.Color(clearance.top < 4 ? "#d97055" : "#e4ad55"),
new THREE.Color(clearance.latch < 4 ? "#d97055" : "#e4ad55")
];
const doorColor = new THREE.Color("#39bba8");
outlinePairs.forEach(([a, b], index) => {
writeVertex(frameOutline[a], frameColors[index]);
writeVertex(frameOutline[b], frameColors[index]);
});
outlinePairs.forEach(([a, b]) => {
writeVertex(doorOutline[a], doorColor);
writeVertex(doorOutline[b], doorColor);
});
gapGeometry.attributes.position.needsUpdate = true;
gapGeometry.attributes.color.needsUpdate = true;
gapGeometry.computeBoundingSphere();
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 8.5);
state.angle = THREE.MathUtils.lerp(state.angle, target.angle, smoothing);
state.tilt = THREE.MathUtils.lerp(state.tilt, target.tilt, smoothing);
state.lift = THREE.MathUtils.lerp(state.lift, target.lift, smoothing);
state.depth = THREE.MathUtils.lerp(state.depth, target.depth, smoothing);
// The safe-close sequence first corrects all three hinge offsets,
// then moves the door into the latched position.
if (
pendingClose &&
Math.abs(state.tilt - target.tilt) < 0.0015 &&
Math.abs(state.lift - target.lift) < 0.0015 &&
Math.abs(state.depth - target.depth) < 0.0015
) {
target.angle = 0;
ui.angle.value = 0;
updateOutputs();
pendingClose = false;
}
hingePivot.rotation.y = state.angle;
doorAssembly.rotation.z = state.tilt;
doorAssembly.position.y = DOOR_CENTER_Y + state.lift;
doorAssembly.position.z = state.depth;
updateGapOverlay();
updateStatus();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateOutputs();
updateGapOverlay();
updateStatus();
animate();
</script>
</body>
</html>
```