Prompt specification
Build or customize the Three.js 3D scene "Pilot hole and screw alignment" 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: Pilot hole and screw alignment
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pilot-hole-and-screw-alignment-x0289
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: pilot hole and screw alignment. Show a short wood block with a narrow centered pilot hole and a screw suspended above it; expose a side cutaway to reveal their shared axis.
Working Three.js Starter (pilot-hole-and-screw-alignment-x0289):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pilot Hole & Screw Alignment</title>
<style>
:root {
color-scheme: dark;
--bg: #07111d;
--panel: rgba(13, 28, 42, 0.76);
--border: rgba(165, 218, 226, 0.18);
--text: #edf8f7;
--muted: #91a9ad;
--accent: #58e2cf;
--accent-dark: #123d3b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#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% 45%, transparent 35%, rgba(2, 8, 15, 0.34) 100%),
linear-gradient(180deg, rgba(3, 10, 18, 0.1), rgba(3, 10, 18, 0.38));
z-index: 1;
}
header {
position: fixed;
top: 28px;
left: 30px;
z-index: 3;
max-width: 410px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 10px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 25px;
height: 1px;
content: "";
background: var(--accent);
box-shadow: 0 0 10px rgba(88, 226, 207, 0.7);
}
h1 {
margin: 0;
font-size: clamp(28px, 3.2vw, 45px);
font-weight: 650;
line-height: 1.02;
letter-spacing: -0.045em;
text-wrap: balance;
}
header p {
max-width: 350px;
margin: 13px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status {
position: fixed;
top: 30px;
right: 30px;
z-index: 3;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 999px;
background: rgba(8, 21, 32, 0.62);
backdrop-filter: blur(14px);
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
pointer-events: none;
}
.status strong {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(88, 226, 207, 0.1),
0 0 13px rgba(88, 226, 207, 0.75);
}
.callout {
position: fixed;
z-index: 3;
display: flex;
gap: 9px;
align-items: flex-start;
padding: 9px 11px;
border: 1px solid rgba(136, 216, 216, 0.2);
border-radius: 9px;
background: rgba(8, 22, 33, 0.72);
backdrop-filter: blur(10px);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.18);
transform: translate(-50%, -50%);
pointer-events: none;
transition: opacity 180ms ease;
}
.callout .index {
display: grid;
flex: 0 0 21px;
width: 21px;
height: 21px;
place-items: center;
border: 1px solid rgba(88, 226, 207, 0.42);
border-radius: 5px;
color: var(--accent);
font-size: 9px;
font-weight: 800;
}
.callout b {
display: block;
margin-top: 1px;
color: #e8f7f5;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.callout small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.controls {
position: fixed;
right: 28px;
bottom: 26px;
z-index: 4;
display: flex;
gap: 9px;
padding: 9px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(18px);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.3);
}
button {
display: flex;
height: 38px;
align-items: center;
gap: 9px;
padding: 0 13px;
border: 1px solid transparent;
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #d8e6e6;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
color 160ms ease,
transform 160ms ease;
}
button:hover {
border-color: rgba(120, 221, 213, 0.25);
background: rgba(255, 255, 255, 0.08);
color: white;
}
button:active {
transform: translateY(1px);
}
button:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
button.active {
border-color: rgba(88, 226, 207, 0.3);
background: rgba(88, 226, 207, 0.1);
color: #bff8ef;
}
.switch {
position: relative;
width: 26px;
height: 15px;
border-radius: 999px;
background: #3d4d54;
transition: background 180ms ease;
}
.switch::after {
position: absolute;
top: 3px;
left: 3px;
width: 9px;
height: 9px;
border-radius: 50%;
content: "";
background: #a4b5b8;
transition:
left 180ms ease,
background 180ms ease;
}
button.active .switch {
background: rgba(88, 226, 207, 0.38);
}
button.active .switch::after {
left: 14px;
background: var(--accent);
box-shadow: 0 0 8px rgba(88, 226, 207, 0.6);
}
.reset {
width: 39px;
justify-content: center;
padding: 0;
}
.reset svg {
width: 16px;
height: 16px;
}
.hint {
position: fixed;
bottom: 30px;
left: 30px;
z-index: 3;
display: flex;
align-items: center;
gap: 9px;
color: rgba(188, 210, 211, 0.65);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
.hint svg {
width: 16px;
height: 16px;
color: var(--accent);
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
background: var(--bg);
transition: opacity 450ms ease, visibility 450ms ease;
}
#loading.hidden {
visibility: hidden;
opacity: 0;
}
.loader {
display: flex;
align-items: center;
gap: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.loader::before {
width: 16px;
height: 16px;
border: 2px solid rgba(88, 226, 207, 0.2);
border-top-color: var(--accent);
border-radius: 50%;
content: "";
animation: spin 0.8s linear infinite;
}
@keyframes spin {
to {
transform: rotate(360deg);
}
}
@media (max-width: 760px) {
header {
top: 20px;
left: 20px;
max-width: calc(100vw - 40px);
}
header p {
display: none;
}
.status {
top: auto;
right: 20px;
bottom: 84px;
}
.controls {
right: 20px;
bottom: 20px;
}
.hint,
.callout {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D pilot hole and screw alignment scene"></canvas>
<div class="vignette"></div>
<header>
<div class="eyebrow">Assembly study / 01</div>
<h1>Pilot hole ×<br />screw alignment</h1>
<p>
A sectional view of a centered pilot bore and a suspended fastener sharing
one continuous vertical centerline.
</p>
</header>
<div class="status">
<span class="status-dot"></span>
<span id="stateLabel">Side section</span>
<strong>0.0°</strong>
</div>
<div id="pilotLabel" class="callout">
<span class="index">01</span>
<div>
<b>Centered pilot</b>
<small>Ø 3 mm · true center</small>
</div>
</div>
<div id="axisLabel" class="callout">
<span class="index">02</span>
<div>
<b>Shared axis</b>
<small>screw ↕ pilot</small>
</div>
</div>
<div class="hint">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.6">
<path d="M9 11V5a2 2 0 0 1 4 0v6" />
<path d="M13 8a2 2 0 0 1 4 0v6a6 6 0 0 1-6 6h-1a6 6 0 0 1-5.2-3L3 14.5A2 2 0 0 1 6.5 12L9 15" />
</svg>
Drag to orbit · Scroll to zoom
</div>
<div class="controls">
<button id="cutawayButton" class="active" type="button" aria-pressed="true">
<span class="switch" aria-hidden="true"></span>
Side cutaway
</button>
<button class="reset" id="resetButton" type="button" aria-label="Reset camera">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7">
<path d="M4 4v6h6" />
<path d="M5.6 15.5A8 8 0 1 0 6 7.3L4 10" />
</svg>
</button>
</div>
<div id="loading">
<div class="loader">Building assembly</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 canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.localClippingEnabled = true;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.FogExp2(0x07111d, 0.028);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 1.34, 0);
// ------------------------------------------------------------
// Procedural wood texture
// ------------------------------------------------------------
function makeWoodTexture() {
const size = 512;
const woodCanvas = document.createElement("canvas");
woodCanvas.width = size;
woodCanvas.height = size;
const ctx = woodCanvas.getContext("2d");
let seed = 9137;
const random = () => {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
};
const gradient = ctx.createLinearGradient(0, 0, 0, size);
gradient.addColorStop(0, "#c58a4d");
gradient.addColorStop(0.48, "#ad6f39");
gradient.addColorStop(1, "#c68b50");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, size, size);
for (let line = 0; line < 150; line++) {
const baseY = random() * size;
const amplitude = 1.5 + random() * 6;
const frequency = 0.008 + random() * 0.015;
const phase = random() * Math.PI * 2;
ctx.beginPath();
for (let x = -10; x <= size + 10; x += 6) {
const y =
baseY +
Math.sin(x * frequency + phase) * amplitude +
Math.sin(x * 0.0035 + phase * 0.7) * 4;
if (x === -10) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
}
const alpha = 0.035 + random() * 0.11;
ctx.strokeStyle = random() > 0.3
? `rgba(69, 34, 15, ${alpha})`
: `rgba(255, 218, 157, ${alpha * 0.7})`;
ctx.lineWidth = 0.45 + random() * 1.5;
ctx.stroke();
}
for (let i = 0; i < 18000; i++) {
const alpha = random() * 0.055;
ctx.fillStyle = random() > 0.48
? `rgba(255, 227, 178, ${alpha})`
: `rgba(45, 22, 10, ${alpha})`;
ctx.fillRect(
random() * size,
random() * size,
1 + random() * 2,
1
);
}
const texture = new THREE.CanvasTexture(woodCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
const woodTexture = makeWoodTexture();
const woodBump = woodTexture.clone();
woodBump.colorSpace = THREE.NoColorSpace;
woodBump.needsUpdate = true;
// ------------------------------------------------------------
// Shared dimensions and clipping plane
// ------------------------------------------------------------
const BLOCK_W = 3.0;
const BLOCK_H = 1.25;
const BLOCK_D = 1.35;
const BLOCK_Y = BLOCK_H / 2;
const HOLE_R = 0.095;
// Removes the +Z half of the wood as the cutaway opens.
const clipPlane = new THREE.Plane(
new THREE.Vector3(0, 0, -1),
BLOCK_D / 2 + 0.02
);
const woodFaceMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
bumpMap: woodBump,
bumpScale: 0.035,
color: 0xffffff,
roughness: 0.78,
metalness: 0,
clippingPlanes: [clipPlane],
clipShadows: true
});
const woodEdgeMaterial = new THREE.MeshStandardMaterial({
map: woodTexture,
bumpMap: woodBump,
bumpScale: 0.025,
color: 0xc17b40,
roughness: 0.84,
metalness: 0,
clippingPlanes: [clipPlane],
clipShadows: true
});
// ------------------------------------------------------------
// Wood block with a real cylindrical opening
// ------------------------------------------------------------
const blockShape = new THREE.Shape();
blockShape.moveTo(-BLOCK_W / 2, -BLOCK_H / 2);
blockShape.lineTo(BLOCK_W / 2, -BLOCK_H / 2);
blockShape.lineTo(BLOCK_W / 2, BLOCK_H / 2);
blockShape.lineTo(-BLOCK_W / 2, BLOCK_H / 2);
blockShape.closePath();
const holePath = new THREE.Path();
holePath.absarc(0, 0, HOLE_R, 0, Math.PI * 2, true);
blockShape.holes.push(holePath);
const blockGeometry = new THREE.ExtrudeGeometry(blockShape, {
depth: BLOCK_D,
steps: 1,
bevelEnabled: false,
curveSegments: 64
});
blockGeometry.translate(0, 0, -BLOCK_D / 2);
const woodBlock = new THREE.Mesh(blockGeometry, [
woodFaceMaterial,
woodEdgeMaterial
]);
woodBlock.position.y = BLOCK_Y;
woodBlock.castShadow = true;
woodBlock.receiveShadow = true;
scene.add(woodBlock);
const blockEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(blockGeometry, 28),
new THREE.LineBasicMaterial({
color: 0x6f3f22,
transparent: true,
opacity: 0.38,
clippingPlanes: [clipPlane]
})
);
blockEdges.position.y = BLOCK_Y;
scene.add(blockEdges);
// Dark inner bore makes the narrow pilot opening easy to read.
const boreMaterial = new THREE.MeshStandardMaterial({
color: 0x492719,
roughness: 0.92,
metalness: 0,
side: THREE.DoubleSide,
clippingPlanes: [clipPlane]
});
const bore = new THREE.Mesh(
new THREE.CylinderGeometry(
HOLE_R * 0.97,
HOLE_R * 0.97,
BLOCK_H + 0.015,
64,
1,
true
),
boreMaterial
);
bore.position.y = BLOCK_Y;
scene.add(bore);
// ------------------------------------------------------------
// Cap the exposed section and show the removed half as a ghost
// ------------------------------------------------------------
const sectionMaterial = new THREE.MeshStandardMaterial({
color: 0xd49b5e,
roughness: 0.9,
metalness: 0,
transparent: true,
opacity: 0
});
const sectionEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x6a3a20,
transparent: true,
opacity: 0
});
const sectionGroup = new THREE.Group();
scene.add(sectionGroup);
const sectionWidth = BLOCK_W / 2 - HOLE_R;
const sectionDepth = 0.025;
const leftSectionGeometry = new THREE.BoxGeometry(
sectionWidth,
BLOCK_H,
sectionDepth
);
const rightSectionGeometry = leftSectionGeometry.clone();
const leftSection = new THREE.Mesh(
leftSectionGeometry,
sectionMaterial
);
leftSection.position.set(
-(BLOCK_W / 2 + HOLE_R) / 2,
BLOCK_Y,
0.006
);
leftSection.castShadow = true;
leftSection.receiveShadow = true;
sectionGroup.add(leftSection);
const rightSection = new THREE.Mesh(
rightSectionGeometry,
sectionMaterial
);
rightSection.position.set(
(BLOCK_W / 2 + HOLE_R) / 2,
BLOCK_Y,
0.006
);
rightSection.castShadow = true;
rightSection.receiveShadow = true;
sectionGroup.add(rightSection);
const leftSectionEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(leftSectionGeometry),
sectionEdgeMaterial
);
leftSectionEdges.position.copy(leftSection.position);
sectionGroup.add(leftSectionEdges);
const rightSectionEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(rightSectionGeometry),
sectionEdgeMaterial
);
rightSectionEdges.position.copy(rightSection.position);
sectionGroup.add(rightSectionEdges);
// Fine section-edge accents around the narrow bore.
const boreEdgeGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-HOLE_R, 0.008, 0.02),
new THREE.Vector3(-HOLE_R, BLOCK_H + 0.008, 0.02),
new THREE.Vector3(HOLE_R, 0.008, 0.02),
new THREE.Vector3(HOLE_R, BLOCK_H + 0.008, 0.02)
]);
const boreEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x173d3a,
transparent: true,
opacity: 0
});
const boreEdges = new THREE.LineSegments(
boreEdgeGeometry,
boreEdgeMaterial
);
sectionGroup.add(boreEdges);
const ghostMaterial = new THREE.LineBasicMaterial({
color: 0x68d9cf,
transparent: true,
opacity: 0,
depthWrite: false
});
const ghostGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(BLOCK_W, BLOCK_H, BLOCK_D / 2)
);
const ghost = new THREE.LineSegments(ghostGeometry, ghostMaterial);
ghost.position.set(0, BLOCK_Y, BLOCK_D / 4);
scene.add(ghost);
// ------------------------------------------------------------
// Shared centerline and target ring
// ------------------------------------------------------------
const axisMaterial = new THREE.LineDashedMaterial({
color: 0x58e2cf,
dashSize: 0.085,
gapSize: 0.055,
transparent: true,
opacity: 0.9,
depthTest: false,
depthWrite: false
});
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, -0.08, 0),
new THREE.Vector3(0, 3.42, 0)
]);
const axisLine = new THREE.Line(axisGeometry, axisMaterial);
axisLine.computeLineDistances();
axisLine.renderOrder = 10;
scene.add(axisLine);
const targetMaterial = new THREE.MeshBasicMaterial({
color: 0x64f2de,
transparent: true,
opacity: 0.72,
depthTest: false,
depthWrite: false
});
const targetRing = new THREE.Mesh(
new THREE.TorusGeometry(0.16, 0.009, 8, 72),
targetMaterial
);
targetRing.rotation.x = Math.PI / 2;
targetRing.position.set(0, BLOCK_H + 0.018, 0);
targetRing.renderOrder = 9;
scene.add(targetRing);
// ------------------------------------------------------------
// Screw: point, shaft, helical thread, and slotted head
// ------------------------------------------------------------
const screw = new THREE.Group();
scene.add(screw);
const screwMetal = new THREE.MeshStandardMaterial({
color: 0x9eb4b9,
metalness: 0.86,
roughness: 0.24
});
const screwHighlight = new THREE.MeshStandardMaterial({
color: 0xc1d1d2,
metalness: 0.82,
roughness: 0.2
});
const slotMaterial = new THREE.MeshStandardMaterial({
color: 0x17252a,
metalness: 0.55,
roughness: 0.4
});
const TIP_Y = 1.48;
const coneHeight = 0.3;
const tip = new THREE.Mesh(
new THREE.ConeGeometry(0.125, coneHeight, 40),
screwMetal
);
tip.rotation.z = Math.PI;
tip.position.y = TIP_Y + coneHeight / 2;
tip.castShadow = true;
screw.add(tip);
const shaftHeight = 1.13;
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.074, 0.074, shaftHeight, 40),
screwMetal
);
shaft.position.y = TIP_Y + coneHeight + shaftHeight / 2;
shaft.castShadow = true;
screw.add(shaft);
const threadPoints = [];
const threadTurns = 7;
const threadSamples = 150;
for (let i = 0; i <= threadSamples; i++) {
const t = i / threadSamples;
const angle = t * Math.PI * 2 * threadTurns;
threadPoints.push(
new THREE.Vector3(
Math.cos(angle) * 0.092,
1.86 + t * 0.98,
Math.sin(angle) * 0.092
)
);
}
const threadCurve = new THREE.CatmullRomCurve3(
threadPoints,
false,
"centripetal"
);
const thread = new THREE.Mesh(
new THREE.TubeGeometry(threadCurve, 230, 0.019, 6, false),
screwHighlight
);
thread.castShadow = true;
screw.add(thread);
const head = new THREE.Mesh(
new THREE.CylinderGeometry(0.3, 0.3, 0.2, 48),
screwHighlight
);
head.position.y = 3.01;
head.castShadow = true;
screw.add(head);
const slot = new THREE.Mesh(
new THREE.BoxGeometry(0.37, 0.018, 0.056),
slotMaterial
);
slot.position.y = 3.113;
slot.rotation.y = -0.26;
screw.add(slot);
// ------------------------------------------------------------
// Ground, grid, and lighting
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(24, 24),
new THREE.MeshStandardMaterial({
color: 0x0a1722,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.035;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(14, 28, 0x27505b, 0x17333f);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
scene.add(
new THREE.HemisphereLight(0xc9efff, 0x3b271b, 1.75)
);
const keyLight = new THREE.DirectionalLight(0xffe5c3, 3.4);
keyLight.position.set(4.5, 7, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -5;
keyLight.shadow.camera.right = 5;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 18;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5ce7d7, 1.9);
rimLight.position.set(-4, 4, -3.5);
scene.add(rimLight);
const fillLight = new THREE.PointLight(0x8fc7ff, 16, 10, 2);
fillLight.position.set(2.5, 2.8, 3.8);
scene.add(fillLight);
// ------------------------------------------------------------
// Custom orbit controls — no Three.js addons
// ------------------------------------------------------------
const mobileLayout = window.innerWidth < 760;
let azimuthTarget = 0.68;
let polarTarget = 1.16;
let radiusTarget = mobileLayout ? 8.2 : 7.1;
let azimuth = azimuthTarget;
let polar = polarTarget;
let radius = radiusTarget;
const pointers = new Map();
let pinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
pinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
azimuthTarget -= dx * 0.007;
polarTarget = THREE.MathUtils.clamp(
polarTarget + dy * 0.006,
0.42,
1.48
);
} else if (pointers.size === 2) {
const nextDistance = pointerDistance();
if (pinchDistance > 0 && nextDistance > 0) {
radiusTarget = THREE.MathUtils.clamp(
radiusTarget * (pinchDistance / nextDistance),
4.7,
11.5
);
}
pinchDistance = nextDistance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
radiusTarget = THREE.MathUtils.clamp(
radiusTarget * Math.exp(event.deltaY * 0.001),
4.7,
11.5
);
},
{ passive: false }
);
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const cutawayButton = document.querySelector("#cutawayButton");
const resetButton = document.querySelector("#resetButton");
const stateLabel = document.querySelector("#stateLabel");
let cutTarget = 1;
let cutAmount = 1;
cutawayButton.addEventListener("click", () => {
cutTarget = cutTarget > 0.5 ? 0 : 1;
cutawayButton.classList.toggle("active", cutTarget === 1);
cutawayButton.setAttribute("aria-pressed", String(cutTarget === 1));
stateLabel.textContent = cutTarget ? "Side section" : "Full block";
});
resetButton.addEventListener("click", () => {
azimuthTarget = 0.68;
polarTarget = 1.16;
radiusTarget = window.innerWidth < 760 ? 8.2 : 7.1;
});
// ------------------------------------------------------------
// Screen-space labels
// ------------------------------------------------------------
const pilotLabel = document.querySelector("#pilotLabel");
const axisLabel = document.querySelector("#axisLabel");
const projected = new THREE.Vector3();
function placeLabel(element, anchor, offsetX, offsetY, opacity = 1) {
projected.copy(anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x + offsetX}px`;
element.style.top = `${y + offsetY}px`;
element.style.opacity = String(opacity);
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
}
function updateLabels() {
placeLabel(
pilotLabel,
new THREE.Vector3(-0.12, BLOCK_H + 0.03, 0),
-118,
-28
);
placeLabel(
axisLabel,
new THREE.Vector3(0.02, 1.56, 0.02),
112,
18,
cutAmount
);
}
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
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 < 760 ? 48 : 42;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
const damping = 1 - Math.exp(-delta * 9);
azimuth = THREE.MathUtils.lerp(azimuth, azimuthTarget, damping);
polar = THREE.MathUtils.lerp(polar, polarTarget, damping);
radius = THREE.MathUtils.lerp(radius, radiusTarget, damping);
const horizontalRadius = Math.sin(polar) * radius;
camera.position.set(
cameraTarget.x + horizontalRadius * Math.sin(azimuth),
cameraTarget.y + Math.cos(polar) * radius,
cameraTarget.z + horizontalRadius * Math.cos(azimuth)
);
camera.lookAt(cameraTarget);
cutAmount = THREE.MathUtils.lerp(
cutAmount,
cutTarget,
1 - Math.exp(-delta * 7.5)
);
// A value of 0 keeps the block intact; 0 exposes the +Z half.
clipPlane.constant = THREE.MathUtils.lerp(
BLOCK_D / 2 + 0.02,
0,
cutAmount
);
const sectionOpacity = THREE.MathUtils.smoothstep(
cutAmount,
0.82,
1
);
sectionGroup.visible = cutAmount > 0.8;
sectionMaterial.opacity = sectionOpacity;
sectionEdgeMaterial.opacity = sectionOpacity * 0.6;
boreEdgeMaterial.opacity = sectionOpacity * 0.85;
ghost.visible = cutAmount > 0.02;
ghostMaterial.opacity = cutAmount * 0.22;
axisLine.visible = cutAmount > 0.025;
axisMaterial.opacity = cutAmount * 0.9;
targetMaterial.opacity =
0.5 + Math.sin(elapsed * 2.1) * 0.12 + cutAmount * 0.16;
targetRing.scale.setScalar(1 + Math.sin(elapsed * 2.1) * 0.035);
// The screw remains exactly on x=0, z=0; only its height breathes.
screw.position.y = Math.sin(elapsed * 1.25) * 0.022;
screw.rotation.y = Math.sin(elapsed * 0.42) * 0.09;
renderer.render(scene, camera);
updateLabels();
if (firstFrame) {
firstFrame = false;
document.querySelector("#loading").classList.add("hidden");
}
}
animate();
</script>
</body>
</html>
```