Prompt specification
Build or customize the Three.js 3D scene "Centered versus offset fastening" 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: Centered versus offset fastening
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/centered-versus-offset-fastening-x0290
Scene Description Prompt:
Compare voxel screws approaching centered and offset holes; only the aligned screw seats correctly. Create one HTML file with Three.js from cdnjs.
Working Three.js Starter (centered-versus-offset-fastening-x0290):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Screw Fit Test</title>
<style>
:root {
--bg: #08111f;
--panel: rgba(10, 20, 36, 0.82);
--line: rgba(151, 184, 222, 0.2);
--text: #edf5ff;
--muted: #91a6bf;
--green: #50f0bd;
--orange: #ff9568;
--cyan: #7bdfff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
z-index: 4;
top: 20px;
left: 20px;
width: min(430px, calc(100vw - 40px));
padding: 18px 20px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
}
h1 {
margin: 10px 0 8px;
font-size: clamp(24px, 3vw, 36px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.description {
margin: 0;
max-width: 370px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 14px;
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #bfd0e2;
font-size: 11px;
font-weight: 700;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--green);
box-shadow: 0 0 10px rgba(80, 240, 189, 0.7);
}
.legend i.orange {
background: var(--orange);
box-shadow: 0 0 10px rgba(255, 149, 104, 0.7);
}
.camera-hint {
margin-top: 11px;
color: #7189a3;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
letter-spacing: 0.03em;
}
.world-label {
--signal: var(--green);
position: fixed;
z-index: 3;
display: flex;
align-items: flex-start;
gap: 9px;
min-width: 175px;
padding: 9px 12px;
border: 1px solid rgba(142, 174, 211, 0.22);
border-radius: 10px;
background: rgba(7, 16, 29, 0.76);
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(9px);
transform: translate(-50%, 0);
pointer-events: none;
white-space: nowrap;
}
.world-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 20px;
background: linear-gradient(var(--signal), transparent);
opacity: 0.7;
}
.world-label .signal {
width: 8px;
height: 8px;
margin-top: 4px;
border-radius: 2px;
background: var(--signal);
box-shadow: 0 0 12px var(--signal);
}
.world-label b {
display: block;
color: #f3f8ff;
font-size: 10px;
letter-spacing: 0.13em;
}
.world-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.world-label[data-state="ok"] {
--signal: var(--green);
}
.world-label[data-state="ok"] small {
color: var(--green);
}
.world-label[data-state="bad"] {
--signal: var(--orange);
}
.world-label[data-state="bad"] small {
color: var(--orange);
}
.controls {
position: fixed;
z-index: 5;
left: 50%;
bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
width: min(760px, calc(100vw - 32px));
padding: 10px;
border: 1px solid var(--line);
border-radius: 15px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(14px);
transform: translateX(-50%);
}
button {
min-height: 40px;
padding: 0 15px;
border: 1px solid rgba(148, 181, 219, 0.25);
border-radius: 10px;
background: rgba(128, 163, 204, 0.1);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
background: rgba(128, 163, 204, 0.19);
border-color: rgba(148, 181, 219, 0.45);
}
button:active {
transform: translateY(1px);
}
button.primary {
min-width: 150px;
border-color: rgba(123, 223, 255, 0.55);
background: linear-gradient(135deg, #207a95, #245b78);
box-shadow: 0 0 22px rgba(57, 190, 229, 0.13);
}
.timeline {
display: flex;
flex: 1;
align-items: center;
gap: 9px;
min-width: 120px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
}
input[type="range"] {
flex: 1;
min-width: 70px;
height: 4px;
margin: 0;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
var(--cyan) 0%,
var(--cyan) var(--progress, 0%),
rgba(126, 161, 201, 0.2) var(--progress, 0%),
rgba(126, 161, 201, 0.2) 100%
);
appearance: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 14px;
height: 14px;
border: 2px solid #dff8ff;
border-radius: 3px;
background: #4ac7e8;
box-shadow: 0 0 12px rgba(74, 199, 232, 0.65);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 2px solid #dff8ff;
border-radius: 3px;
background: #4ac7e8;
box-shadow: 0 0 12px rgba(74, 199, 232, 0.65);
}
#loading {
position: fixed;
z-index: 10;
inset: 0;
display: grid;
place-items: center;
background: var(--bg);
color: var(--cyan);
font: 700 12px ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.hud {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 15px;
}
.description {
max-width: 310px;
font-size: 12px;
}
.camera-hint {
display: none;
}
.controls {
bottom: 12px;
width: calc(100vw - 24px);
flex-wrap: wrap;
}
.timeline {
order: 3;
flex-basis: 100%;
}
button {
flex: 1;
padding: 0 10px;
}
.world-label {
min-width: 145px;
padding: 7px 9px;
transform: translate(-50%, 0) scale(0.86);
transform-origin: top center;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel screw alignment comparison"></canvas>
<div id="loading">Building voxel test rig...</div>
<header class="hud">
<div class="eyebrow">
<span class="live-dot"></span>
Voxel mechanics / Test 01
</div>
<h1>Screw fit comparison</h1>
<p class="description">
A centered bore and a bore shifted by one voxel. Both screws descend,
but only the shared drive-and-hole axis seats the screw correctly.
</p>
<div class="legend">
<span><i></i>Coaxial hole</span>
<span><i class="orange"></i>Offset hole</span>
</div>
<div class="camera-hint">DRAG TO ORBIT · SCROLL / PINCH TO ZOOM · SPACE TO PAUSE</div>
</header>
<div id="leftLabel" class="world-label" data-state="active">
<span class="signal"></span>
<div>
<b>ALIGNED DRIVE</b>
<small id="leftState">Centered hole · ready</small>
</div>
</div>
<div id="rightLabel" class="world-label" data-state="active">
<span class="signal"></span>
<div>
<b>OFFSET DRIVE</b>
<small id="rightState">Hole shifted one voxel · ready</small>
</div>
</div>
<div class="controls">
<button id="playButton" class="primary" type="button">Pause test</button>
<label class="timeline">
<span>0.0s</span>
<input
id="timeline"
type="range"
min="0"
max="1000"
value="0"
aria-label="Animation timeline"
/>
<span>6.0s</span>
</label>
<button id="viewButton" type="button">Reset view</button>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const playButton = document.querySelector("#playButton");
const timeline = document.querySelector("#timeline");
const viewButton = document.querySelector("#viewButton");
const leftLabel = document.querySelector("#leftLabel");
const rightLabel = document.querySelector("#rightLabel");
const leftState = document.querySelector("#leftState");
const rightState = document.querySelector("#rightState");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
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(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 15, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
80
);
const clock = new THREE.Clock();
const DURATION = 6;
const CELL = 0.78;
const PLATE_TOP = 0.5;
const HEAD_BOTTOM = 0.78;
const START_HEIGHT = 2.85;
const ALIGNED_HEIGHT = PLATE_TOP + 0.015 - HEAD_BOTTOM;
const OFFSET_HEIGHT = PLATE_TOP + 0.025;
let progress = 0;
let playing = true;
let collisionTriggered = false;
let collisionAge = 0;
// Direct camera controls: no examples/addons are used.
const orbit = {
target: new THREE.Vector3(0, 0.55, 0),
yaw: 0.16,
pitch: 0.5,
distance: 13.8,
desiredYaw: 0.16,
desiredPitch: 0.5,
desiredDistance: 13.8
};
let dragging = false;
let pointerX = 0;
let pointerY = 0;
function isMobile() {
return window.innerWidth < 700;
}
function resetView() {
orbit.desiredYaw = 0.16;
orbit.desiredPitch = 0.5;
orbit.desiredDistance = isMobile() ? 25 : 13.8;
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.desiredYaw -= dx * 0.006;
orbit.desiredPitch = THREE.MathUtils.clamp(
orbit.desiredPitch + dy * 0.005,
0.16,
1.08
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredDistance = THREE.MathUtils.clamp(
orbit.desiredDistance * Math.exp(event.deltaY * 0.001),
7,
32
);
},
{ passive: false }
);
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 9);
orbit.yaw += (orbit.desiredYaw - orbit.yaw) * damping;
orbit.pitch += (orbit.desiredPitch - orbit.pitch) * damping;
orbit.distance += (orbit.desiredDistance - orbit.distance) * damping;
const horizontalDistance =
orbit.distance * Math.cos(orbit.pitch);
camera.position.set(
orbit.target.x + Math.sin(orbit.yaw) * horizontalDistance,
orbit.target.y + Math.sin(orbit.pitch) * orbit.distance,
orbit.target.z + Math.cos(orbit.yaw) * horizontalDistance
);
camera.lookAt(orbit.target);
}
// Lighting.
scene.add(new THREE.HemisphereLight(0x9ac8ff, 0x182033, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.1);
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 = 30;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x6baeff, 1.35);
rimLight.position.set(-7, 5, -5);
scene.add(rimLight);
const greenLight = new THREE.PointLight(0x50f0bd, 13, 7, 2);
greenLight.position.set(-3.2, 1.5, 1.5);
scene.add(greenLight);
const orangeLight = new THREE.PointLight(0xff9568, 13, 7, 2);
orangeLight.position.set(3.2, 1.5, 1.5);
scene.add(orangeLight);
// Ground and grid.
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0x0c1726,
roughness: 0.93,
metalness: 0.08
});
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
floorMaterial
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.08;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
32,
32,
0x2d4963,
0x172a40
);
grid.position.y = -1.065;
grid.material.transparent = true;
grid.material.opacity = 0.32;
scene.add(grid);
const sharedMaterials = {
steel: new THREE.MeshStandardMaterial({
color: 0xb8c8d7,
metalness: 0.78,
roughness: 0.27
}),
steelDark: new THREE.MeshStandardMaterial({
color: 0x536477,
metalness: 0.72,
roughness: 0.34
}),
socket: new THREE.MeshStandardMaterial({
color: 0x152131,
metalness: 0.45,
roughness: 0.5
}),
pedestal: new THREE.MeshStandardMaterial({
color: 0x17263a,
metalness: 0.22,
roughness: 0.74
}),
well: new THREE.MeshStandardMaterial({
color: 0x07101b,
metalness: 0.15,
roughness: 0.9
}),
plate: [
new THREE.MeshStandardMaterial({
color: 0x35465d,
metalness: 0.28,
roughness: 0.67
}),
new THREE.MeshStandardMaterial({
color: 0x3a4c64,
metalness: 0.3,
roughness: 0.64
}),
new THREE.MeshStandardMaterial({
color: 0x304158,
metalness: 0.26,
roughness: 0.7
})
]
};
function makeBox(width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function createHoleFrame(holeX, color) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.35,
metalness: 0.35,
roughness: 0.38
});
const half = CELL / 2;
const rail = 0.055;
group.add(
makeBox(
rail,
0.075,
CELL + 0.11,
material,
holeX - half - rail / 2,
PLATE_TOP + 0.035,
0
),
makeBox(
rail,
0.075,
CELL + 0.11,
material,
holeX + half + rail / 2,
PLATE_TOP + 0.035,
0
),
makeBox(
CELL + 0.11,
0.075,
rail,
material,
holeX,
PLATE_TOP + 0.035,
-half - rail / 2
),
makeBox(
CELL + 0.11,
0.075,
rail,
material,
holeX,
PLATE_TOP + 0.035,
half + rail / 2
)
);
return { group, material };
}
function createTargetMarker() {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color: 0x9ae8ff,
transparent: true,
opacity: 0.68,
depthWrite: false
});
const size = 0.1;
const offset = 0.23;
group.add(
makeBox(size, 0.025, size, material, -offset, PLATE_TOP + 0.025, -offset),
makeBox(size, 0.025, size, material, offset, PLATE_TOP + 0.025, -offset),
makeBox(size, 0.025, size, material, -offset, PLATE_TOP + 0.025, offset),
makeBox(size, 0.025, size, material, offset, PLATE_TOP + 0.025, offset)
);
return group;
}
function createTrajectory(color) {
const group = new THREE.Group();
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.24,
depthWrite: false
});
for (let i = 0; i < 5; i++) {
const marker = new THREE.Mesh(
new THREE.BoxGeometry(0.095, 0.095, 0.095),
material
);
marker.position.y = 0.95 + i * 0.42;
group.add(marker);
}
return group;
}
function createStation(x, accentColor, holeCellX, showTarget) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
const accent = new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.65,
metalness: 0.4,
roughness: 0.35
});
// Pedestal.
const pedestal = makeBox(
5.78,
0.55,
5.78,
sharedMaterials.pedestal,
0,
-0.78,
0
);
group.add(pedestal);
group.add(
makeBox(
1.65,
0.075,
0.035,
accent,
0,
-0.72,
2.905
),
makeBox(
0.28,
0.075,
0.035,
accent,
-1.02,
-0.72,
2.905
),
makeBox(
0.28,
0.075,
0.035,
accent,
1.02,
-0.72,
2.905
)
);
// The plate is built from individual voxel columns.
for (let ix = -3; ix <= 3; ix++) {
for (let iz = -3; iz <= 3; iz++) {
if (ix === holeCellX && iz === 0) continue;
const materialIndex =
Math.abs(ix * 7 + iz * 13 + 20) % sharedMaterials.plate.length;
const tile = makeBox(
CELL - 0.026,
1,
CELL - 0.026,
sharedMaterials.plate[materialIndex],
ix * CELL,
0,
iz * CELL
);
group.add(tile);
}
}
// Dark bottom of the open voxel bore.
const holeX = holeCellX * CELL;
group.add(
makeBox(
CELL - 0.035,
0.045,
CELL - 0.035,
sharedMaterials.well,
holeX,
-0.485,
0
)
);
const frame = createHoleFrame(holeX, accentColor);
group.add(frame.group);
if (showTarget) {
const target = createTargetMarker();
group.add(target);
}
const trajectory = createTrajectory(accentColor);
group.add(trajectory);
return {
group,
frameMaterial: frame.material
};
}
function createScrew(accentColor) {
const group = new THREE.Group();
const accent = new THREE.MeshStandardMaterial({
color: accentColor,
emissive: accentColor,
emissiveIntensity: 0.28,
metalness: 0.58,
roughness: 0.32
});
// Square voxel shank.
group.add(
makeBox(
0.43,
0.66,
0.43,
sharedMaterials.steelDark,
0,
0.33,
0
)
);
// Block-like thread collars.
for (let i = 0; i < 5; i++) {
group.add(
makeBox(
0.53,
0.07,
0.53,
sharedMaterials.steel,
0,
0.1 + i * 0.13,
0
)
);
}
// Pixelated 3 × 3 head.
const headCell = 0.27;
for (let ix = -1; ix <= 1; ix++) {
for (let iz = -1; iz <= 1; iz++) {
const isAccent = ix === 0 && iz === 1;
const isSocket = ix === 0 && iz === 0;
const headPiece = makeBox(
headCell - 0.012,
0.28,
headCell - 0.012,
isSocket
? sharedMaterials.socket
: isAccent
? accent
: sharedMaterials.steel,
ix * headCell,
0.92,
iz * headCell
);
group.add(headPiece);
}
}
// Dark square suggesting a driver socket.
group.add(
makeBox(
0.16,
0.025,
0.16,
sharedMaterials.socket,
0,
1.07,
0
)
);
return group;
}
const leftStation = createStation(
-3.2,
0x50f0bd,
0,
false
);
const rightStation = createStation(
3.2,
0xff9568,
1,
true
);
const leftScrew = createScrew(0x50f0bd);
const rightScrew = createScrew(0xff9568);
leftStation.group.add(leftScrew);
rightStation.group.add(rightScrew);
leftScrew.position.y = START_HEIGHT;
rightScrew.position.y = START_HEIGHT;
// Small collision voxels for the failed test.
const collisionParticles = [];
for (let i = 0; i < 14; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xff9568,
transparent: true,
opacity: 0
});
const particle = new THREE.Mesh(
new THREE.BoxGeometry(0.075, 0.075, 0.075),
material
);
particle.visible = false;
scene.add(particle);
collisionParticles.push({
mesh: particle,
velocity: new THREE.Vector3(),
spin: new THREE.Vector3()
});
}
function triggerCollision() {
collisionTriggered = true;
collisionAge = 0;
const origin = new THREE.Vector3(0, PLATE_TOP + 0.06, 0);
rightStation.group.localToWorld(origin);
collisionParticles.forEach((particle, index) => {
const angle = (index / collisionParticles.length) * Math.PI * 2;
const speed = 0.75 + (index % 4) * 0.16;
particle.mesh.visible = true;
particle.mesh.position.copy(origin);
particle.mesh.material.opacity = 1;
particle.velocity.set(
Math.cos(angle) * speed,
0.75 + (index % 5) * 0.12,
Math.sin(angle) * speed
);
particle.spin.set(
1.3 + index * 0.03,
1.8 + index * 0.02,
1.1 + index * 0.04
);
});
}
function resetCollision() {
collisionTriggered = false;
collisionAge = 0;
collisionParticles.forEach((particle) => {
particle.mesh.visible = false;
particle.mesh.material.opacity = 0;
});
}
function updateParticles(dt) {
if (!collisionTriggered) return;
collisionAge += dt;
collisionParticles.forEach((particle) => {
particle.velocity.y -= 2.3 * dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.mesh.rotation.x += particle.spin.x * dt;
particle.mesh.rotation.y += particle.spin.y * dt;
particle.mesh.rotation.z += particle.spin.z * dt;
particle.mesh.material.opacity = Math.max(
0,
1 - collisionAge / 1.1
);
});
if (collisionAge > 1.2) {
collisionParticles.forEach((particle) => {
particle.mesh.visible = false;
});
}
}
function smooth01(value) {
const x = THREE.MathUtils.clamp(value, 0, 1);
return x * x * (3 - 2 * x);
}
function updateScrews() {
const descend = smooth01((progress - 0.08) / 0.82);
leftScrew.position.y = THREE.MathUtils.lerp(
START_HEIGHT,
ALIGNED_HEIGHT,
descend
);
rightScrew.position.y = THREE.MathUtils.lerp(
START_HEIGHT,
OFFSET_HEIGHT,
descend
);
// A short vibration emphasizes the failed contact.
const vibration =
progress > 0.88
? Math.sin(performance.now() * 0.038) *
0.017 *
smooth01((progress - 0.88) / 0.08)
: 0;
rightScrew.position.x = vibration;
leftStation.frameMaterial.emissiveIntensity =
1.35 + smooth01((progress - 0.87) / 0.11) * 1.8;
rightStation.frameMaterial.emissiveIntensity =
1.35 + Math.max(0, 1 - collisionAge / 0.55) * 3.4;
if (progress > 0.89 && !collisionTriggered) {
triggerCollision();
}
}
function updateUI() {
const seconds = progress * DURATION;
timeline.value = String(Math.round(progress * 1000));
timeline.style.setProperty("--progress", `${progress * 100}%`);
if (playing) {
playButton.textContent = "Pause test";
} else if (progress >= 0.999) {
playButton.textContent = "Replay test";
} else {
playButton.textContent = "Resume test";
}
if (progress < 0.08) {
leftState.textContent = "Centered hole · ready";
rightState.textContent = "Hole shifted one voxel · ready";
leftLabel.dataset.state = "active";
rightLabel.dataset.state = "active";
} else if (progress < 0.89) {
leftState.textContent = "Descending on shared axis";
rightState.textContent = "Descending toward solid voxel";
leftLabel.dataset.state = "active";
rightLabel.dataset.state = "active";
} else {
leftState.textContent = "Seated flush ✓";
rightState.textContent = "No entry · head proud ×";
leftLabel.dataset.state = "ok";
rightLabel.dataset.state = "bad";
}
const labelTime = document.querySelector(".timeline span");
if (labelTime) labelTime.textContent = `${seconds.toFixed(1)}s`;
}
function togglePlayback() {
if (progress >= 0.999) {
progress = 0;
resetCollision();
playing = true;
} else {
playing = !playing;
}
updateUI();
}
playButton.addEventListener("click", togglePlayback);
timeline.addEventListener("input", () => {
playing = false;
progress = Number(timeline.value) / 1000;
if (progress < 0.84 && collisionTriggered) {
resetCollision();
}
updateUI();
});
viewButton.addEventListener("click", resetView);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
}
if (event.key.toLowerCase() === "r") {
resetView();
}
});
const leftAnchor = new THREE.Vector3(-3.2, -0.68, 2.9);
const rightAnchor = new THREE.Vector3(3.2, -0.68, 2.9);
const projected = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.fov = width < 700 ? 50 : 42;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
resetView();
orbit.yaw = orbit.desiredYaw;
orbit.pitch = orbit.desiredPitch;
orbit.distance = orbit.desiredDistance;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (playing) {
progress += dt / DURATION;
if (progress >= 1) {
progress = 1;
playing = false;
}
}
updateCamera(dt);
updateScrews();
updateParticles(dt);
updateUI();
positionLabel(leftLabel, leftAnchor);
positionLabel(rightLabel, rightAnchor);
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
loading.remove();
});
</script>
</body>
</html>
```