Prompt specification
Build or customize the Three.js 3D scene "Doorstop contact study" 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: Doorstop contact study
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/doorstop-contact-study-x0010
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing a rubber door wedge. Place the wedge beneath a hinged door and animate insertion along the floor; stop door rotation when its bottom edge contacts the wedge's rising surface. Voxel style.
Working Three.js Starter (doorstop-contact-study-x0010):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Door Wedge</title>
<style>
:root {
--ink: #17312f;
--muted: #647672;
--paper: rgba(248, 247, 238, 0.91);
--line: rgba(23, 49, 47, 0.15);
--orange: #e76535;
--orange-dark: #b9401e;
--teal: #2f8f87;
--yellow: #f3c84b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dce3df;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(
circle at 50% 42%,
transparent 48%,
rgba(33, 55, 51, 0.11) 100%
);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
pointer-events: auto;
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.74);
box-shadow:
0 18px 45px rgba(30, 50, 46, 0.13),
0 2px 8px rgba(30, 50, 46, 0.07);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
.title-panel {
position: absolute;
top: 24px;
left: 24px;
width: min(370px, calc(100vw - 48px));
padding: 19px 20px 17px;
border-radius: 5px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 3px;
content: "";
background: var(--teal);
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
font-weight: 850;
line-height: 0.98;
letter-spacing: -0.055em;
text-transform: uppercase;
}
.subtitle {
margin: 11px 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.status {
display: inline-flex;
align-items: center;
gap: 8px;
min-width: 0;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.status .dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: #7b8b87;
box-shadow: 0 0 0 4px rgba(123, 139, 135, 0.13);
}
.status.inserting .dot,
.status.closing .dot {
background: var(--orange);
box-shadow: 0 0 0 4px rgba(231, 101, 53, 0.14);
animation: pulse 1s ease-in-out infinite;
}
.status.locked .dot {
background: var(--yellow);
box-shadow: 0 0 0 4px rgba(243, 200, 75, 0.18);
}
@keyframes pulse {
50% {
transform: scale(0.68);
opacity: 0.72;
}
}
.mini-readout {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.08em;
text-align: right;
text-transform: uppercase;
white-space: nowrap;
}
.mini-readout strong {
display: block;
margin-top: 2px;
color: var(--ink);
font-size: 14px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.02em;
}
.readout-panel {
position: absolute;
top: 24px;
right: 24px;
display: flex;
gap: 1px;
overflow: hidden;
border-radius: 5px;
}
.readout {
min-width: 104px;
padding: 14px 16px;
text-align: right;
}
.readout span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.readout strong {
display: block;
margin-top: 3px;
font-size: 22px;
font-weight: 850;
font-variant-numeric: tabular-nums;
letter-spacing: -0.05em;
}
.bottom-panel {
position: absolute;
right: 24px;
bottom: 24px;
left: 24px;
display: grid;
grid-template-columns: minmax(0, 1fr) auto;
align-items: center;
gap: 20px;
padding: 14px 16px 14px 18px;
border-radius: 5px;
}
.timeline {
position: relative;
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 9px;
min-width: 0;
}
.timeline::before {
position: absolute;
top: 15px;
right: 12%;
left: 12%;
height: 2px;
content: "";
background: var(--line);
}
.step {
position: relative;
z-index: 1;
display: flex;
align-items: center;
gap: 9px;
color: #8a9794;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.step:nth-child(2) {
justify-content: center;
}
.step:last-child {
justify-content: flex-end;
}
.step b {
display: grid;
width: 30px;
height: 30px;
place-items: center;
border: 2px solid #cbd3cf;
border-radius: 50%;
background: var(--paper);
color: #899692;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.step.active {
color: var(--orange-dark);
}
.step.active b {
border-color: var(--orange);
background: var(--orange);
color: white;
}
.step.done {
color: var(--teal);
}
.step.done b {
border-color: var(--teal);
color: var(--teal);
}
.progress-track {
position: absolute;
right: 12%;
bottom: 7px;
left: 12%;
height: 2px;
overflow: hidden;
background: rgba(23, 49, 47, 0.08);
}
.progress-fill {
width: 0;
height: 100%;
background: var(--orange);
transition: width 80ms linear;
}
.controls {
display: flex;
align-items: center;
gap: 8px;
}
.control-button {
min-height: 38px;
padding: 0 14px;
border: 1px solid var(--line);
border-radius: 3px;
background: transparent;
color: var(--ink);
cursor: pointer;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
transition:
background 160ms ease,
transform 160ms ease;
}
.control-button:hover {
background: white;
transform: translateY(-1px);
}
.control-button.primary {
border-color: var(--ink);
background: var(--ink);
color: white;
}
.control-button.primary:hover {
background: #244743;
}
.instructions {
position: absolute;
right: 26px;
bottom: 100px;
padding: 7px 10px;
border-radius: 3px;
background: rgba(248, 247, 238, 0.74);
color: #647672;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#contact-tag {
position: fixed;
z-index: 4;
display: none;
padding: 8px 10px;
border: 1px solid rgba(23, 49, 47, 0.18);
border-radius: 3px;
background: var(--yellow);
color: var(--ink);
box-shadow: 0 8px 20px rgba(23, 49, 47, 0.16);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.11em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -125%);
}
@media (max-width: 760px) {
.title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.readout-panel {
top: auto;
right: 12px;
bottom: 108px;
}
.readout {
min-width: 82px;
padding: 10px 12px;
}
.readout strong {
font-size: 18px;
}
.bottom-panel {
right: 12px;
bottom: 12px;
left: 12px;
grid-template-columns: 1fr;
gap: 12px;
}
.controls {
justify-content: flex-end;
}
.instructions {
display: none;
}
.step {
gap: 5px;
font-size: 8px;
}
.step b {
width: 26px;
height: 26px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Animated voxel door and rubber wedge"></canvas>
<div class="vignette"></div>
<main class="hud">
<section class="title-panel panel">
<div class="eyebrow">Mechanism 04 · Collision Study</div>
<h1>Rubber Door<br>Wedge</h1>
<p class="subtitle">
A voxel door closes until its lower edge meets the rising face of an
inserted rubber wedge.
</p>
<div class="status-row">
<div id="status" class="status ready" aria-live="polite">
<span class="dot"></span>
<span id="status-text">Ready</span>
</div>
<div class="mini-readout">
Wedge travel
<strong id="travel-readout">0.00 m</strong>
</div>
</div>
</section>
<section class="readout-panel panel">
<div class="readout">
<span>Door angle</span>
<strong id="angle-readout">58.0°</strong>
</div>
<div class="readout">
<span>State</span>
<strong id="state-readout">Open</strong>
</div>
</section>
<div class="instructions">Drag to orbit · Wheel to zoom · Double-click to reset</div>
<section class="bottom-panel panel">
<div class="timeline">
<div class="step active" data-step="ready">
<b>01</b><span>Position</span>
</div>
<div class="step" data-step="insert">
<b>02</b><span>Insert</span>
</div>
<div class="step" data-step="close">
<b>03</b><span>Close / Lock</span>
</div>
<div class="progress-track">
<div id="progress-fill" class="progress-fill"></div>
</div>
</div>
<div class="controls">
<button id="pause-button" class="control-button">Pause</button>
<button id="replay-button" class="control-button primary">↻ Replay</button>
</div>
</section>
</main>
<div id="contact-tag">Rising face contact</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Configuration
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const statusElement = document.querySelector("#status");
const statusText = document.querySelector("#status-text");
const angleReadout = document.querySelector("#angle-readout");
const stateReadout = document.querySelector("#state-readout");
const travelReadout = document.querySelector("#travel-readout");
const progressFill = document.querySelector("#progress-fill");
const contactTag = document.querySelector("#contact-tag");
const pauseButton = document.querySelector("#pause-button");
const replayButton = document.querySelector("#replay-button");
const stepElements = [
...document.querySelectorAll(".step")
];
const DEG = Math.PI / 180;
const HINGE_X = -3;
const DOOR_LENGTH = 4.2;
const DOOR_HEIGHT = 3.6;
const DOOR_CELL = 0.3;
const DOOR_BOTTOM = 0.035;
const DOOR_THICKNESS = 0.28;
const WEDGE_CELL = 0.18;
const WEDGE_COLUMNS = 6;
const WEDGE_DEPTH_COLUMNS = 6;
const WEDGE_WIDTH = WEDGE_COLUMNS * WEDGE_CELL;
const WEDGE_DEPTH = WEDGE_DEPTH_COLUMNS * WEDGE_CELL;
const HALF_DEPTH = WEDGE_DEPTH / 2;
// High on the hinge-facing/left side, descending toward the right.
const WEDGE_LEVELS = [4, 3, 2, 2, 1, 1];
const WEDGE_START_X = 2.85;
const WEDGE_END_X = 0.55;
const START_ANGLE = 58 * DEG;
const HOLD_END = 0.8;
const INSERT_END = 3.8;
const CLOSE_SPEED = 7 * DEG;
const EXPECTED_END =
INSERT_END + (START_ANGLE - Math.atan(HALF_DEPTH / 3.55)) / CLOSE_SPEED;
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce3df);
scene.fog = new THREE.Fog(0xdce3df, 15, 31);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(
0xf5fbf4,
0x7f9186,
2.05
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(
0xfff4d7,
4.4
);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.bias = -0.00045;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(
0x8ccbc7,
1.8
);
rimLight.position.set(-7, 5, -6);
scene.add(rimLight);
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.88,
metalness: 0,
flatShading: true,
...options
});
const floorBaseMaterial = material(0x758780);
const frameMaterial = material(0x284a46);
const hingeMaterial = material(0x45635f, {
metalness: 0.2,
roughness: 0.55
});
const brassMaterial = material(0xe9bd54, {
roughness: 0.42,
metalness: 0.2
});
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
mat
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel floor
// -------------------------------------------------------------------------
addBox(
scene,
new THREE.Vector3(16, 0.24, 13),
new THREE.Vector3(-0.3, -0.17, 0.9),
floorBaseMaterial,
false
);
const tileGeometry = new THREE.BoxGeometry(0.68, 0.08, 0.68);
const tileMaterial = material(0xffffff);
const tileCountX = 18;
const tileCountZ = 16;
const tiles = new THREE.InstancedMesh(
tileGeometry,
tileMaterial,
tileCountX * tileCountZ
);
tiles.receiveShadow = true;
const tileMatrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < tileCountX; ix++) {
for (let iz = 0; iz < tileCountZ; iz++) {
const x = -6.26 + ix * 0.72;
const z = -4.2 + iz * 0.72;
tileMatrix.makeTranslation(x, -0.04, z);
tiles.setMatrixAt(tileIndex, tileMatrix);
const variation = ((ix * 17 + iz * 11) % 7) * 0.008;
tileColor.setHSL(
0.43,
0.075,
0.71 + variation - Math.abs(ix - 9) * 0.002
);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.instanceMatrix.needsUpdate = true;
tiles.instanceColor.needsUpdate = true;
scene.add(tiles);
// -------------------------------------------------------------------------
// Voxel frame and hinge hardware
// -------------------------------------------------------------------------
addBox(
scene,
new THREE.Vector3(0.3, 3.9, 0.5),
new THREE.Vector3(HINGE_X - 0.18, 1.95, 0),
frameMaterial
);
addBox(
scene,
new THREE.Vector3(0.3, 3.9, 0.5),
new THREE.Vector3(1.4, 1.95, 0),
frameMaterial
);
addBox(
scene,
new THREE.Vector3(4.88, 0.28, 0.5),
new THREE.Vector3(-0.89, 3.9, 0),
frameMaterial
);
for (const y of [0.65, 1.8, 2.95]) {
addBox(
scene,
new THREE.Vector3(0.28, 0.3, 0.64),
new THREE.Vector3(HINGE_X - 0.05, y, 0),
hingeMaterial
);
}
// -------------------------------------------------------------------------
// Voxel door
// -------------------------------------------------------------------------
const doorGroup = new THREE.Group();
doorGroup.position.set(HINGE_X, 0, 0);
scene.add(doorGroup);
const doorColumns = Math.round(DOOR_LENGTH / DOOR_CELL);
const doorRows = Math.round(DOOR_HEIGHT / DOOR_CELL);
const doorGeometry = new THREE.BoxGeometry(
DOOR_CELL - 0.018,
DOOR_CELL - 0.018,
DOOR_THICKNESS
);
const doorMaterial = material(0xffffff);
const doorBlocks = new THREE.InstancedMesh(
doorGeometry,
doorMaterial,
doorColumns * doorRows
);
doorBlocks.castShadow = true;
doorBlocks.receiveShadow = true;
const doorMatrix = new THREE.Matrix4();
const doorColor = new THREE.Color();
let doorIndex = 0;
for (let row = 0; row < doorRows; row++) {
for (let column = 0; column < doorColumns; column++) {
const x = (column + 0.5) * DOOR_CELL;
const y = DOOR_BOTTOM + (row + 0.5) * DOOR_CELL;
doorMatrix.makeTranslation(x, y, 0);
doorBlocks.setMatrixAt(doorIndex, doorMatrix);
const isFrame =
column === 0 ||
column === doorColumns - 1 ||
row === 0 ||
row === doorRows - 1;
if (isFrame) {
doorColor.setHSL(0.48, 0.22, 0.31);
} else {
const checker = (column + row) % 2;
doorColor.setHSL(
0.47 + checker * 0.008,
0.18,
0.48 + ((column * 3 + row * 5) % 4) * 0.013
);
}
doorBlocks.setColorAt(doorIndex, doorColor);
doorIndex++;
}
}
doorBlocks.instanceMatrix.needsUpdate = true;
doorBlocks.instanceColor.needsUpdate = true;
doorGroup.add(doorBlocks);
// Blocky handle on both sides of the door.
const handleGeometry = new THREE.BoxGeometry(0.12, 0.12, 0.12);
for (const side of [-1, 1]) {
const handle = new THREE.Group();
handle.position.set(DOOR_LENGTH - 0.4, 1.68, side * 0.205);
for (const [x, y] of [
[0, 0.18],
[0, 0],
[0, -0.18]
]) {
const piece = new THREE.Mesh(handleGeometry, brassMaterial);
piece.position.set(x, y, 0);
piece.castShadow = true;
handle.add(piece);
}
doorGroup.add(handle);
}
// -------------------------------------------------------------------------
// Voxel rubber wedge
// -------------------------------------------------------------------------
const wedgeGroup = new THREE.Group();
wedgeGroup.position.x = WEDGE_START_X;
scene.add(wedgeGroup);
let wedgeBlockCount = 0;
for (const levels of WEDGE_LEVELS) {
wedgeBlockCount += levels * WEDGE_DEPTH_COLUMNS;
}
const wedgeGeometry = new THREE.BoxGeometry(
WEDGE_CELL - 0.006,
WEDGE_CELL - 0.006,
WEDGE_CELL - 0.006
);
const wedgeMaterial = material(0xffffff, {
roughness: 0.96
});
const wedgeBlocks = new THREE.InstancedMesh(
wedgeGeometry,
wedgeMaterial,
wedgeBlockCount
);
wedgeBlocks.castShadow = true;
wedgeBlocks.receiveShadow = true;
const wedgeMatrix = new THREE.Matrix4();
const wedgeColor = new THREE.Color();
let wedgeIndex = 0;
for (let column = 0; column < WEDGE_COLUMNS; column++) {
const levels = WEDGE_LEVELS[column];
for (let level = 0; level < levels; level++) {
for (let depth = 0; depth < WEDGE_DEPTH_COLUMNS; depth++) {
const x = (column + 0.5) * WEDGE_CELL;
const y = (level + 0.5) * WEDGE_CELL;
const z =
(depth - (WEDGE_DEPTH_COLUMNS - 1) / 2) * WEDGE_CELL;
wedgeMatrix.makeTranslation(x, y, z);
wedgeBlocks.setMatrixAt(wedgeIndex, wedgeMatrix);
const topHighlight = level === levels - 1 ? 0.035 : 0;
const checker = (column + depth + level) % 2;
wedgeColor.setHSL(
0.055 + checker * 0.006,
0.73,
0.49 + topHighlight - level * 0.012
);
wedgeBlocks.setColorAt(wedgeIndex, wedgeColor);
wedgeIndex++;
}
}
}
wedgeBlocks.instanceMatrix.needsUpdate = true;
wedgeBlocks.instanceColor.needsUpdate = true;
wedgeGroup.add(wedgeBlocks);
// -------------------------------------------------------------------------
// Floor insertion guide
// -------------------------------------------------------------------------
const guideMaterial = material(0x65b8ad, {
emissive: 0x1a4e48,
emissiveIntensity: 0.22
});
for (let i = 0; i < 10; i++) {
addBox(
scene,
new THREE.Vector3(0.075, 0.025, 0.18),
new THREE.Vector3(2.95 - i * 0.25, 0.015, 0.82),
guideMaterial,
false
);
}
// Small voxel arrow pointing in the insertion direction.
for (let i = 0; i < 4; i++) {
addBox(
scene,
new THREE.Vector3(0.13, 0.03, 0.13),
new THREE.Vector3(0.43 - i * 0.11, 0.018, 0.82),
guideMaterial,
false
);
}
// -------------------------------------------------------------------------
// Contact marker
// -------------------------------------------------------------------------
const contactMarker = new THREE.Group();
contactMarker.visible = false;
const contactMaterial = material(0xffdc55, {
emissive: 0xf3a900,
emissiveIntensity: 0.75,
roughness: 0.5
});
const contactGeometry = new THREE.BoxGeometry(0.055, 0.055, 0.055);
const contactOffsets = [
[0, 0],
[0.075, 0],
[-0.075, 0],
[0, 0.075],
[0, -0.075]
];
for (const [x, y] of contactOffsets) {
const cube = new THREE.Mesh(contactGeometry, contactMaterial);
cube.position.set(x, y, 0);
cube.castShadow = true;
contactMarker.add(cube);
}
scene.add(contactMarker);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(-0.25, 1.2, 0.85),
yaw: 0.72,
pitch: 0.48,
radius: 13.7
};
function resetCamera() {
cameraState.target.set(-0.25, 1.2, 0.85);
cameraState.yaw = 0.72;
cameraState.pitch = 0.48;
cameraState.radius = 13.7;
}
function updateCamera() {
const { target, yaw, pitch, radius } = cameraState;
camera.position.set(
target.x + radius * Math.cos(pitch) * Math.sin(yaw),
target.y + radius * Math.sin(pitch),
target.z + radius * Math.cos(pitch) * Math.cos(yaw)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
let pointerDown = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.16,
1.18
);
previousX = event.clientX;
previousY = event.clientY;
});
function endPointer(event) {
pointerDown = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7,
22
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLButtonElement) return;
if (event.code === "KeyR") {
replay();
}
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
});
// -------------------------------------------------------------------------
// Collision model
//
// The lower door edge is a radial line in the floor plane. Its leading
// sample crosses the front plane of the wedge. The wedge's voxel columns
// provide the actual rising height field used for the contact test.
// -------------------------------------------------------------------------
function wedgeHeightAt(worldX, wedgeX) {
const localX = worldX - wedgeX;
if (localX < 0 || localX >= WEDGE_WIDTH) {
return null;
}
const column = Math.min(
WEDGE_COLUMNS - 1,
Math.floor(localX / WEDGE_CELL)
);
return WEDGE_LEVELS[column] * WEDGE_CELL;
}
function detectLowerEdgeContact(angle, wedgeX) {
if (angle <= 0.001) return null;
// The centerline of the lower edge intersects z = +HALF_DEPTH here.
const contactX =
HINGE_X + HALF_DEPTH / Math.tan(angle);
if (
contactX < wedgeX ||
contactX > wedgeX + WEDGE_WIDTH
) {
return null;
}
const height = wedgeHeightAt(contactX, wedgeX);
if (height === null || height <= DOOR_BOTTOM + 0.015) {
return null;
}
return {
x: contactX,
height,
contactAngle: Math.atan(
HALF_DEPTH / (wedgeX - HINGE_X)
)
};
}
// -------------------------------------------------------------------------
// Timeline and UI
// -------------------------------------------------------------------------
let elapsed = 0;
let paused = false;
let hasContacted = false;
let contactAngle = 0;
let wedgeX = WEDGE_START_X;
let doorAngle = START_ANGLE;
const clock = new THREE.Clock();
function smoothstep(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function setStepState(activeName, completedNames) {
for (const step of stepElements) {
const name = step.dataset.step;
step.classList.toggle("active", name === activeName);
step.classList.toggle("done", completedNames.includes(name));
}
}
function updateUI() {
angleReadout.textContent = `${(doorAngle / DEG).toFixed(1)}°`;
travelReadout.textContent = `${(WEDGE_START_X - wedgeX).toFixed(2)} m`;
if (hasContacted) {
statusElement.className = "status locked";
statusText.textContent = "Contact — rotation locked";
stateReadout.textContent = "Locked";
contactTag.style.display = "block";
setStepState("close", ["ready", "insert", "close"]);
} else if (paused) {
statusElement.className = "status closing";
statusText.textContent = "Paused";
stateReadout.textContent = "Paused";
} else if (elapsed < HOLD_END) {
statusElement.className = "status ready";
statusText.textContent = "Ready";
stateReadout.textContent = "Open";
setStepState("ready", []);
} else if (elapsed < INSERT_END) {
statusElement.className = "status inserting";
statusText.textContent = "Inserting wedge";
stateReadout.textContent = "Insert";
setStepState("insert", ["ready"]);
} else {
statusElement.className = "status closing";
statusText.textContent = "Door closing";
stateReadout.textContent = "Closing";
setStepState("close", ["ready", "insert"]);
}
const progress = hasContacted
? 100
: THREE.MathUtils.clamp((elapsed / EXPECTED_END) * 100, 0, 100);
progressFill.style.width = `${progress}%`;
}
function updateContactTag() {
if (!hasContacted) return;
const worldPoint = new THREE.Vector3(
WEDGE_END_X + 0.02,
0.14,
HALF_DEPTH + 0.03
);
const projected = worldPoint.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
contactTag.style.left = `${x}px`;
contactTag.style.top = `${y}px`;
contactTag.textContent =
`Rising face contact · ${(contactAngle / DEG).toFixed(1)}°`;
}
function togglePause() {
if (hasContacted) return;
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
}
function replay() {
elapsed = 0;
paused = false;
hasContacted = false;
contactAngle = 0;
wedgeX = WEDGE_START_X;
doorAngle = START_ANGLE;
wedgeGroup.position.x = wedgeX;
doorGroup.rotation.y = -doorAngle;
contactMarker.visible = false;
contactTag.style.display = "none";
pauseButton.textContent = "Pause";
clock.getDelta();
updateUI();
}
pauseButton.addEventListener("click", togglePause);
replayButton.addEventListener("click", replay);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused && !hasContacted) {
elapsed += delta;
}
if (elapsed < HOLD_END) {
wedgeX = WEDGE_START_X;
doorAngle = START_ANGLE;
} else if (elapsed < INSERT_END) {
const insertionT =
(elapsed - HOLD_END) / (INSERT_END - HOLD_END);
const eased = smoothstep(insertionT);
wedgeX = THREE.MathUtils.lerp(
WEDGE_START_X,
WEDGE_END_X,
eased
);
// The door remains fully open until insertion is complete.
doorAngle = START_ANGLE;
} else {
wedgeX = WEDGE_END_X;
const desiredAngle =
START_ANGLE - (elapsed - INSERT_END) * CLOSE_SPEED;
const contact = detectLowerEdgeContact(
Math.max(desiredAngle, 0),
wedgeX
);
if (contact) {
hasContacted = true;
contactAngle = contact.contactAngle;
doorAngle = contactAngle;
contactMarker.visible = true;
contactMarker.position.set(
WEDGE_END_X + 0.025,
0.13,
HALF_DEPTH + 0.035
);
} else {
doorAngle = Math.max(desiredAngle, 0);
}
}
wedgeGroup.position.x = wedgeX;
doorGroup.rotation.y = -doorAngle;
const pulse = 1 + Math.sin(performance.now() * 0.009) * 0.08;
contactMarker.scale.setScalar(pulse);
updateCamera();
updateUI();
updateContactTag();
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);
});
resetCamera();
updateCamera();
updateUI();
animate();
</script>
</body>
</html>
```