Prompt specification
Build or customize the Three.js 3D scene "Lead Screw With One Pitch Marked" 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: Lead Screw With One Pitch Marked
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/lead-screw-with-one-pitch-marked-x2069
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a single-start helical lead screw and matching nut with an axial pitch marker. Show that one full relative turn advances the nut by one pitch. A 3D cutaway view.
Working Three.js Starter (lead-screw-with-one-pitch-marked-x2069):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Single-Start Lead Screw — 3D Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #06111c;
--panel: rgba(8, 20, 32, 0.88);
--border: rgba(132, 208, 232, 0.22);
--text: #eaf7ff;
--muted: #91adbd;
--cyan: #5ee7ff;
--gold: #ffb234;
--green: #67f2ad;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 72% 30%, #123149 0%, transparent 36%),
linear-gradient(145deg, #071522, #030a12 72%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
#panel {
position: fixed;
top: 20px;
left: 20px;
z-index: 10;
width: min(360px, calc(100vw - 40px));
padding: 20px;
border: 1px solid var(--border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 20px 60px rgba(0, 0, 0, 0.34),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
user-select: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 13px var(--cyan);
}
h1 {
margin: 0;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 16px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.badges {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-bottom: 16px;
}
.badge {
padding: 6px 9px;
border: 1px solid rgba(255, 178, 52, 0.3);
border-radius: 999px;
background: rgba(255, 178, 52, 0.08);
color: #ffd38c;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badge.cut {
border-color: rgba(94, 231, 255, 0.3);
background: rgba(94, 231, 255, 0.08);
color: #a9f3ff;
}
.equation {
display: flex;
align-items: baseline;
justify-content: center;
gap: 12px;
margin: 4px 0 16px;
padding: 14px 12px;
border: 1px solid rgba(94, 231, 255, 0.18);
border-radius: 13px;
background: linear-gradient(
110deg,
rgba(94, 231, 255, 0.08),
rgba(255, 178, 52, 0.06)
);
font-variant-numeric: tabular-nums;
}
#turnReadout {
min-width: 82px;
color: var(--cyan);
font-size: 30px;
font-weight: 750;
}
.equation b {
color: var(--muted);
font-size: 19px;
font-weight: 500;
}
#pitchReadout {
min-width: 90px;
color: var(--gold);
font-size: 22px;
font-weight: 750;
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 15px;
}
.stat {
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.stat span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.075em;
text-transform: uppercase;
}
.stat strong {
font-size: 15px;
font-variant-numeric: tabular-nums;
}
#travelValue {
color: var(--gold);
}
input[type="range"] {
--p: 0%;
width: 100%;
height: 5px;
margin: 4px 0 15px;
border-radius: 999px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--p),
rgba(255, 255, 255, 0.12) var(--p),
rgba(255, 255, 255, 0.12) 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #07131e;
border-radius: 50%;
appearance: none;
background: var(--cyan);
box-shadow: 0 0 12px rgba(94, 231, 255, 0.8);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 13px;
height: 13px;
border: 3px solid #07131e;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px rgba(94, 231, 255, 0.8);
cursor: pointer;
}
.buttons {
display: flex;
gap: 8px;
}
button {
flex: 1;
min-height: 39px;
border: 1px solid rgba(94, 231, 255, 0.24);
border-radius: 10px;
background: rgba(94, 231, 255, 0.1);
color: #d9faff;
font: inherit;
font-size: 13px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
transform 150ms ease;
}
button:hover {
background: rgba(94, 231, 255, 0.18);
}
button:active {
transform: translateY(1px);
}
button.secondary {
border-color: rgba(255, 255, 255, 0.12);
background: rgba(255, 255, 255, 0.045);
color: var(--muted);
}
.note {
margin: 14px 0 0;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.08);
color: var(--muted);
font-size: 11px;
line-height: 1.5;
}
.note strong {
color: #d9edf7;
}
#completeMessage {
display: none;
margin-top: 12px;
padding: 10px 12px;
border: 1px solid rgba(103, 242, 173, 0.3);
border-radius: 10px;
background: rgba(103, 242, 173, 0.08);
color: #a4ffd5;
font-size: 12px;
font-weight: 700;
text-align: center;
}
#panel.complete #completeMessage {
display: block;
}
#hint {
position: fixed;
right: 18px;
bottom: 16px;
z-index: 5;
padding: 8px 11px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(3, 10, 17, 0.55);
color: rgba(210, 232, 241, 0.64);
font-size: 11px;
pointer-events: none;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #06111c;
color: var(--cyan);
font-size: 13px;
letter-spacing: 0.08em;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 700px) {
#panel {
top: 10px;
left: 10px;
width: min(330px, calc(100vw - 20px));
padding: 15px;
}
h1 {
font-size: 21px;
}
.subtitle {
margin-bottom: 12px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section id="panel" aria-label="Lead screw animation controls">
<div class="eyebrow">Kinematic cutaway</div>
<h1>Single-Start Lead Screw</h1>
<p class="subtitle">
The screw rotates while the non-rotating nut travels axially.
One complete relative turn advances the nut by exactly one pitch.
</p>
<div class="badges">
<span class="badge">1 Start</span>
<span class="badge">Lead = Pitch</span>
<span class="badge cut">3D Cutaway</span>
</div>
<div class="equation" aria-live="polite">
<span id="turnReadout">0°</span>
<b>=</b>
<span id="pitchReadout">0.00 × P</span>
</div>
<div class="stats">
<div class="stat">
<span>Screw rotation</span>
<strong id="angleValue">0°</strong>
</div>
<div class="stat">
<span>Nut orientation</span>
<strong>0° fixed</strong>
</div>
<div class="stat">
<span>Axial travel</span>
<strong id="travelValue">0.00 units</strong>
</div>
<div class="stat">
<span>Relative turns</span>
<strong id="turnValue">0.00 rev</strong>
</div>
</div>
<input
id="progress"
type="range"
min="0"
max="1"
step="0.001"
value="0"
aria-label="Animation progress"
>
<div class="buttons">
<button id="playButton" type="button">Play turn</button>
<button id="resetButton" class="secondary" type="button">Reset</button>
</div>
<div id="completeMessage">
One full turn completed: the nut advanced exactly one pitch.
</div>
<p class="note">
<strong>Geometry:</strong> one continuous external helix and one matching
internal thread. For a single-start screw,
<strong>lead = axial distance traveled in one revolution</strong>.
</p>
</section>
<div id="hint">Drag to orbit · Wheel to zoom · Shift/right-drag to pan</div>
<div id="loading">LOADING THREE.JS SCENE…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and renderer
// -------------------------------------------------------------------------
const PITCH = 1.20;
const ANIMATION_SECONDS = 6.5;
const TAU = Math.PI * 2;
const sceneElement = document.getElementById("scene");
const loadingElement = document.getElementById("loading");
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneElement.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06111c);
scene.fog = new THREE.Fog(0x06111c, 18, 34);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
theta: 0.56,
phi: 1.18,
radius: 13.6,
target: new THREE.Vector3(0, 0, -0.05)
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x +
controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z +
controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting and floor
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xbceaff, 0x142032, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.2);
keyLight.position.set(4, 8, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3bcfff, 2.0);
rimLight.position.set(-7, 3, -7);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9d32, 35, 15, 2);
warmLight.position.set(2, 1.5, 3);
scene.add(warmLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x081522,
metalness: 0.05,
roughness: 0.94
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.5;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(24, 48, 0x285f72, 0x123443);
grid.position.y = -2.485;
grid.material.transparent = true;
grid.material.opacity = 0.48;
scene.add(grid);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const materials = {
core: new THREE.MeshStandardMaterial({
color: 0x91a3af,
metalness: 0.88,
roughness: 0.22
}),
externalThread: new THREE.MeshStandardMaterial({
color: 0xffa52b,
metalness: 0.78,
roughness: 0.2
}),
journal: new THREE.MeshStandardMaterial({
color: 0x607582,
metalness: 0.9,
roughness: 0.25
}),
nutOuter: new THREE.MeshStandardMaterial({
color: 0x167f94,
metalness: 0.62,
roughness: 0.3
}),
nutInner: new THREE.MeshStandardMaterial({
color: 0x0a2934,
metalness: 0.5,
roughness: 0.4
}),
nutEnd: new THREE.MeshStandardMaterial({
color: 0x238fa1,
metalness: 0.65,
roughness: 0.28
}),
cutFace: new THREE.MeshStandardMaterial({
color: 0xffb43b,
emissive: 0x3a1700,
emissiveIntensity: 0.28,
metalness: 0.58,
roughness: 0.34
}),
internalThread: new THREE.MeshStandardMaterial({
color: 0xff713d,
metalness: 0.8,
roughness: 0.2
}),
index: new THREE.MeshStandardMaterial({
color: 0xff4d72,
emissive: 0x5b071c,
emissiveIntensity: 0.75,
metalness: 0.45,
roughness: 0.22
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x5ee7ff,
emissive: 0x063843,
emissiveIntensity: 0.6,
metalness: 0.4,
roughness: 0.25
})
};
function cylinderX(radius, length, material, x = 0, radialSegments = 64) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
),
material
);
mesh.rotation.z = Math.PI / 2;
mesh.position.x = x;
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function cylinderBetween(a, b, radius, material, radialSegments = 12) {
const direction = new THREE.Vector3().subVectors(b, a);
const midpoint = new THREE.Vector3()
.addVectors(a, b)
.multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
direction.length(),
radialSegments
),
material
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
return mesh;
}
function coneAt(position, direction, radius, height, material) {
const mesh = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, 20),
material
);
mesh.position.copy(position);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
return mesh;
}
// Exact helix: x = c + pitch * theta / (2π).
class HelixCurve extends THREE.Curve {
constructor(constant, pitch, radius, thetaStart, thetaEnd) {
super();
this.constant = constant;
this.pitch = pitch;
this.radius = radius;
this.thetaStart = thetaStart;
this.thetaEnd = thetaEnd;
}
getPoint(t, target = new THREE.Vector3()) {
const theta = THREE.MathUtils.lerp(
this.thetaStart,
this.thetaEnd,
t
);
target.set(
this.constant + this.pitch * theta / TAU,
this.radius * Math.cos(theta),
this.radius * Math.sin(theta)
);
return target;
}
}
// -------------------------------------------------------------------------
// Lead screw
// -------------------------------------------------------------------------
const screwGroup = new THREE.Group();
scene.add(screwGroup);
const screwCore = cylinderX(0.72, 8.8, materials.core, 0, 96);
screwGroup.add(screwCore);
for (const side of [-1, 1]) {
screwGroup.add(
cylinderX(0.78, 0.18, materials.journal, side * 4.28, 72)
);
screwGroup.add(
cylinderX(0.61, 0.48, materials.journal, side * 4.58, 72)
);
screwGroup.add(
cylinderX(0.47, 0.18, materials.core, side * 4.91, 72)
);
}
const threadConstant = -4.2;
const externalTurns = 7;
const externalCurve = new HelixCurve(
threadConstant,
PITCH,
0.855,
0,
TAU * externalTurns
);
const externalThread = new THREE.Mesh(
new THREE.TubeGeometry(externalCurve, 700, 0.135, 12, false),
materials.externalThread
);
externalThread.castShadow = true;
externalThread.receiveShadow = true;
screwGroup.add(externalThread);
// Bright radial index makes screw rotation easy to see.
const indexMarker = new THREE.Mesh(
new THREE.BoxGeometry(0.23, 0.48, 0.12),
materials.index
);
indexMarker.position.set(4.58, 0.67, 0);
indexMarker.castShadow = true;
screwGroup.add(indexMarker);
// Static curved arrow around the screw axis.
const arrowPoints = [];
const arrowStart = -0.35;
const arrowEnd = 5.55;
for (let i = 0; i <= 110; i++) {
const t = i / 110;
const theta = THREE.MathUtils.lerp(arrowStart, arrowEnd, t);
arrowPoints.push(
new THREE.Vector3(
4.58,
1.14 * Math.cos(theta),
1.14 * Math.sin(theta)
)
);
}
const arrowCurve = new THREE.CatmullRomCurve3(arrowPoints);
const rotationArrow = new THREE.Mesh(
new THREE.TubeGeometry(arrowCurve, 120, 0.026, 8, false),
materials.cyan
);
scene.add(rotationArrow);
const arrowEndTheta = arrowEnd;
const arrowTangent = new THREE.Vector3(
0,
-Math.sin(arrowEndTheta),
Math.cos(arrowEndTheta)
).normalize();
scene.add(
coneAt(
arrowPoints[arrowPoints.length - 1],
arrowTangent,
0.075,
0.2,
materials.cyan
)
);
// -------------------------------------------------------------------------
// Cutaway nut: partial annular sleeve with colored cut faces
// -------------------------------------------------------------------------
function quadGeometry(quads) {
const positions = [];
const indices = [];
for (const quad of quads) {
const base = positions.length / 3;
for (const vertex of quad) {
positions.push(vertex.x, vertex.y, vertex.z);
}
indices.push(
base, base + 1, base + 2,
base, base + 2, base + 3
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function makeAnnularSector(
length,
outerRadius,
innerRadius,
thetaStart,
thetaEnd,
segments = 72
) {
const half = length / 2;
const outerQuads = [];
const innerQuads = [];
const plusEndQuads = [];
const minusEndQuads = [];
const startCutQuads = [];
const endCutQuads = [];
for (let i = 0; i < segments; i++) {
const t0 = THREE.MathUtils.lerp(
thetaStart,
thetaEnd,
i / segments
);
const t1 = THREE.MathUtils.lerp(
thetaStart,
thetaEnd,
(i + 1) / segments
);
const oc0 = Math.cos(t0);
const os0 = Math.sin(t0);
const oc1 = Math.cos(t1);
const os1 = Math.sin(t1);
outerQuads.push([
new THREE.Vector3(-half, outerRadius * oc0, outerRadius * os0),
new THREE.Vector3(-half, outerRadius * oc1, outerRadius * os1),
new THREE.Vector3(half, outerRadius * oc1, outerRadius * os1),
new THREE.Vector3(half, outerRadius * oc0, outerRadius * os0)
]);
innerQuads.push([
new THREE.Vector3(-half, innerRadius * oc0, innerRadius * os0),
new THREE.Vector3(half, innerRadius * oc0, innerRadius * os0),
new THREE.Vector3(half, innerRadius * oc1, innerRadius * os1),
new THREE.Vector3(-half, innerRadius * oc1, innerRadius * os1)
]);
plusEndQuads.push([
new THREE.Vector3(half, innerRadius * oc0, innerRadius * os0),
new THREE.Vector3(half, outerRadius * oc0, outerRadius * os0),
new THREE.Vector3(half, outerRadius * oc1, outerRadius * os1),
new THREE.Vector3(half, innerRadius * oc1, innerRadius * os1)
]);
minusEndQuads.push([
new THREE.Vector3(-half, innerRadius * oc0, innerRadius * os0),
new THREE.Vector3(-half, innerRadius * oc1, innerRadius * os1),
new THREE.Vector3(-half, outerRadius * oc1, outerRadius * os1),
new THREE.Vector3(-half, outerRadius * oc0, outerRadius * os0)
]);
}
const innerAt = (x, radius, theta) =>
new THREE.Vector3(
x,
radius * Math.cos(theta),
radius * Math.sin(theta)
);
startCutQuads.push([
innerAt(-half, innerRadius, thetaStart),
innerAt(-half, outerRadius, thetaStart),
innerAt(half, outerRadius, thetaStart),
innerAt(half, innerRadius, thetaStart)
]);
endCutQuads.push([
innerAt(-half, innerRadius, thetaEnd),
innerAt(half, innerRadius, thetaEnd),
innerAt(half, outerRadius, thetaEnd),
innerAt(-half, outerRadius, thetaEnd)
]);
return {
outer: quadGeometry(outerQuads),
inner: quadGeometry(innerQuads),
plusEnd: quadGeometry(plusEndQuads),
minusEnd: quadGeometry(minusEndQuads),
startCut: quadGeometry(startCutQuads),
endCut: quadGeometry(endCutQuads)
};
}
const nutGroup = new THREE.Group();
scene.add(nutGroup);
const NUT_LENGTH = 1.90;
const NUT_OUTER_RADIUS = 1.43;
const NUT_INNER_RADIUS = 1.16;
// The removed sector faces the default camera.
const cutCenter = Math.atan2(10.5, 4.8);
const cutHalfAngle = 0.85;
const thetaStart = cutCenter + cutHalfAngle;
const thetaEnd = cutCenter + TAU - cutHalfAngle;
const nutSector = makeAnnularSector(
NUT_LENGTH,
NUT_OUTER_RADIUS,
NUT_INNER_RADIUS,
thetaStart,
thetaEnd
);
function addNutMesh(geometry, material) {
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
nutGroup.add(mesh);
return mesh;
}
addNutMesh(nutSector.outer, materials.nutOuter);
addNutMesh(nutSector.inner, materials.nutInner);
addNutMesh(nutSector.plusEnd, materials.nutEnd);
addNutMesh(nutSector.minusEnd, materials.nutEnd);
addNutMesh(nutSector.startCut, materials.cutFace);
addNutMesh(nutSector.endCut, materials.cutFace);
// A thin cutaway flange at the left end of the nut.
const flangeSector = makeAnnularSector(
0.18,
1.61,
1.16,
thetaStart,
thetaEnd
);
const flange = addNutMesh(flangeSector.outer, materials.nutOuter);
flange.position.x = -0.86;
const flangeInner = addNutMesh(
flangeSector.inner,
materials.nutInner
);
flangeInner.position.x = -0.86;
const flangeEnd = addNutMesh(
flangeSector.minusEnd,
materials.nutEnd
);
flangeEnd.position.x = -0.86;
const flangeCut = addNutMesh(
flangeSector.startCut,
materials.cutFace
);
flangeCut.position.x = -0.86;
const flangeCut2 = addNutMesh(
flangeSector.endCut,
materials.cutFace
);
flangeCut2.position.x = -0.86;
// Matching internal thread. It is axially offset by half a pitch so that
// it interleaves with the external thread.
const internalStartX = -NUT_LENGTH / 2;
const internalEndX = NUT_LENGTH / 2;
const halfPitchOffset = PITCH / 2;
const internalThetaStart =
TAU *
((internalStartX - threadConstant - halfPitchOffset) / PITCH);
const internalThetaEnd =
TAU *
((internalEndX - threadConstant - halfPitchOffset) / PITCH);
const internalCurve = new HelixCurve(
threadConstant,
PITCH,
1.03,
internalThetaStart,
internalThetaEnd
);
const internalThread = new THREE.Mesh(
new THREE.TubeGeometry(internalCurve, 190, 0.105, 10, false),
materials.internalThread
);
internalThread.castShadow = true;
internalThread.receiveShadow = true;
nutGroup.add(internalThread);
// Bright rails along the two cut boundaries.
for (const theta of [thetaStart, thetaEnd]) {
for (const radius of [NUT_INNER_RADIUS, NUT_OUTER_RADIUS]) {
const y = radius * Math.cos(theta);
const z = radius * Math.sin(theta);
nutGroup.add(
cylinderBetween(
new THREE.Vector3(-NUT_LENGTH / 2, y, z),
new THREE.Vector3(NUT_LENGTH / 2, y, z),
0.025,
materials.cutFace,
8
)
);
}
}
// Moving cyan witness pointer attached to the nut.
nutGroup.add(
cylinderBetween(
new THREE.Vector3(0, -1.36, 1.45),
new THREE.Vector3(0, -1.78, 1.45),
0.026,
materials.cyan,
8
)
);
nutGroup.add(
coneAt(
new THREE.Vector3(0, -1.86, 1.45),
new THREE.Vector3(0, -1, 0),
0.09,
0.22,
materials.cyan
)
);
// -------------------------------------------------------------------------
// Ghost outline of the nut at its initial axial position
// -------------------------------------------------------------------------
const ghostGroup = new THREE.Group();
scene.add(ghostGroup);
const ghostMaterial = new THREE.LineBasicMaterial({
color: 0x65dcff,
transparent: true,
opacity: 0.22,
depthWrite: false
});
function addCircleOutline(radius, x) {
const points = [];
for (let i = 0; i < 96; i++) {
const theta = (i / 96) * TAU;
points.push(
new THREE.Vector3(
x,
radius * Math.cos(theta),
radius * Math.sin(theta)
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
ghostGroup.add(new THREE.LineLoop(geometry, ghostMaterial));
}
addCircleOutline(NUT_OUTER_RADIUS, -NUT_LENGTH / 2);
addCircleOutline(NUT_OUTER_RADIUS, NUT_LENGTH / 2);
addCircleOutline(NUT_INNER_RADIUS, -NUT_LENGTH / 2);
addCircleOutline(NUT_INNER_RADIUS, NUT_LENGTH / 2);
for (const theta of [0, Math.PI / 2, Math.PI, Math.PI * 1.5]) {
const points = [
new THREE.Vector3(
-NUT_LENGTH / 2,
NUT_OUTER_RADIUS * Math.cos(theta),
NUT_OUTER_RADIUS * Math.sin(theta)
),
new THREE.Vector3(
NUT_LENGTH / 2,
NUT_OUTER_RADIUS * Math.cos(theta),
NUT_OUTER_RADIUS * Math.sin(theta)
)
];
ghostGroup.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
ghostMaterial
)
);
}
// -------------------------------------------------------------------------
// Axial pitch marker: initial position to one-pitch final position
// -------------------------------------------------------------------------
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xffc34d,
emissive: 0x4b2700,
emissiveIntensity: 0.35,
metalness: 0.45,
roughness: 0.3
});
const markerY = -1.98;
const markerZ = 1.45;
scene.add(
cylinderBetween(
new THREE.Vector3(-PITCH, markerY, markerZ),
new THREE.Vector3(0, markerY, markerZ),
0.022,
markerMaterial,
10
)
);
for (const x of [-PITCH, 0]) {
scene.add(
cylinderBetween(
new THREE.Vector3(x, -1.69, markerZ),
new THREE.Vector3(x, -2.04, markerZ),
0.022,
markerMaterial,
10
)
);
}
scene.add(
coneAt(
new THREE.Vector3(-PITCH, markerY, markerZ),
new THREE.Vector3(-1, 0, 0),
0.075,
0.2,
markerMaterial
)
);
scene.add(
coneAt(
new THREE.Vector3(0, markerY, markerZ),
new THREE.Vector3(1, 0, 0),
0.075,
0.2,
markerMaterial
)
);
// -------------------------------------------------------------------------
// Labels and leader lines
// -------------------------------------------------------------------------
function makeTextSprite(text, width = 2.7) {
const canvas = document.createElement("canvas");
canvas.width = 720;
canvas.height = 144;
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
context.beginPath();
if (context.roundRect) {
context.roundRect(3, 3, 714, 138, 22);
} else {
context.rect(3, 3, 714, 138);
}
context.fillStyle = "rgba(5, 18, 29, 0.90)";
context.fill();
context.strokeStyle = "rgba(120, 205, 235, 0.55)";
context.lineWidth = 3;
context.stroke();
context.fillStyle = "#eaf8ff";
context.font = "700 35px Inter, system-ui, sans-serif";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, width * 0.2, 1);
sprite.renderOrder = 20;
return sprite;
}
function addCallout(text, labelPosition, targetPosition, width = 2.7) {
const label = makeTextSprite(text, width);
label.position.copy(labelPosition);
scene.add(label);
const lineGeometry = new THREE.BufferGeometry().setFromPoints([
labelPosition,
targetPosition
]);
const line = new THREE.Line(
lineGeometry,
new THREE.LineBasicMaterial({
color: 0x6da7ba,
transparent: true,
opacity: 0.55,
depthTest: false
})
);
line.renderOrder = 19;
scene.add(line);
}
addCallout(
"SINGLE 1× EXTERNAL HELIX",
new THREE.Vector3(2.75, 1.55, 0.55),
new THREE.Vector3(2.25, 0.72, 0.3),
3.05
);
addCallout(
"CUTAWAY NUT",
new THREE.Vector3(0.55, 2.02, 0.65),
new THREE.Vector3(0.25, 1.05, 0.2),
2.35
);
addCallout(
"MATCHING 1× INTERNAL THREAD",
new THREE.Vector3(-0.55, -1.25, 1.8),
new THREE.Vector3(-0.15, -0.88, 0.95),
3.25
);
addCallout(
"360° RELATIVE TURN",
new THREE.Vector3(4.62, 1.72, 0.45),
new THREE.Vector3(4.58, 1.13, 0.25),
2.85
);
const pitchLabel = makeTextSprite(
"AXIAL TRAVEL = 1 PITCH",
3.15
);
pitchLabel.position.set(-PITCH / 2, -2.25, markerZ);
scene.add(pitchLabel);
const startLabel = makeTextSprite("0", 0.75);
startLabel.position.set(0, -1.58, markerZ);
scene.add(startLabel);
const onePitchLabel = makeTextSprite("1P", 0.9);
onePitchLabel.position.set(-PITCH, -1.58, markerZ);
scene.add(onePitchLabel);
// -------------------------------------------------------------------------
// Animation and UI
// -------------------------------------------------------------------------
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const progressInput = document.getElementById("progress");
const panel = document.getElementById("panel");
const turnReadout = document.getElementById("turnReadout");
const pitchReadout = document.getElementById("pitchReadout");
const angleValue = document.getElementById("angleValue");
const travelValue = document.getElementById("travelValue");
const turnValue = document.getElementById("turnValue");
let progress = 0;
let playing = false;
let userInteracted = false;
function updateMechanism() {
const angle = progress * TAU;
screwGroup.rotation.x = angle;
// For positive screw rotation, the nut advances in negative X.
// After 360°, nutGroup.position.x is exactly -PITCH.
nutGroup.position.x = -PITCH * progress;
const degrees = progress * 360;
const travel = PITCH * progress;
turnReadout.textContent = `${degrees.toFixed(0)}°`;
pitchReadout.textContent = `${progress.toFixed(2)} × P`;
angleValue.textContent = `${degrees.toFixed(0)}°`;
travelValue.textContent = `${travel.toFixed(2)} units`;
turnValue.textContent = `${progress.toFixed(2)} rev`;
progressInput.value = progress.toFixed(3);
progressInput.style.setProperty("--p", `${progress * 100}%`);
progressInput.setAttribute(
"aria-valuetext",
`${degrees.toFixed(0)} degrees, ${progress.toFixed(2)} pitch`
);
panel.classList.toggle("complete", progress >= 0.9999);
}
function setPlaying(value) {
playing = value;
if (playing && progress >= 0.9999) {
progress = 0;
}
playButton.textContent = playing
? "Pause"
: progress >= 0.9999
? "Replay turn"
: "Play turn";
}
playButton.addEventListener("click", () => {
userInteracted = true;
setPlaying(!playing);
});
resetButton.addEventListener("click", () => {
userInteracted = true;
progress = 0;
setPlaying(false);
updateMechanism();
});
progressInput.addEventListener("input", () => {
userInteracted = true;
progress = Number(progressInput.value);
setPlaying(false);
updateMechanism();
});
window.addEventListener("keydown", (event) => {
if (
event.target instanceof HTMLInputElement ||
event.target instanceof HTMLButtonElement
) {
return;
}
if (event.code === "Space") {
event.preventDefault();
userInteracted = true;
setPlaying(!playing);
}
if (event.key.toLowerCase() === "r") {
userInteracted = true;
progress = 0;
setPlaying(false);
updateMechanism();
}
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, wheel/pinch zoom
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = null;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
userInteracted = true;
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
pan: event.button === 2 || event.shiftKey
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
canvas.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
const dx = event.clientX - pointer.x;
const dy = event.clientY - pointer.y;
pointer.x = event.clientX;
pointer.y = event.clientY;
if (pointers.size === 1) {
if (pointer.pan) {
const right = new THREE.Vector3(1, 0, 0).applyQuaternion(
camera.quaternion
);
const up = new THREE.Vector3(0, 1, 0).applyQuaternion(
camera.quaternion
);
const scale = controls.radius * 0.0015;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(up, dy * scale);
} else {
controls.theta -= dx * 0.006;
controls.phi -= dy * 0.006;
controls.phi = THREE.MathUtils.clamp(
controls.phi,
0.2,
Math.PI - 0.2
);
}
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance && distance > 0) {
controls.radius *= previousPinchDistance / distance;
controls.radius = THREE.MathUtils.clamp(
controls.radius,
7,
23
);
}
previousPinchDistance = distance;
}
});
function removePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = null;
}
canvas.addEventListener("pointerup", removePointer);
canvas.addEventListener("pointercancel", removePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
userInteracted = true;
controls.radius *= Math.exp(event.deltaY * 0.001);
controls.radius = THREE.MathUtils.clamp(
controls.radius,
7,
23
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
progress += delta / ANIMATION_SECONDS;
if (progress >= 1) {
progress = 1;
setPlaying(false);
}
}
updateMechanism();
updateCamera();
renderer.render(scene, camera);
}
updateMechanism();
animate();
loadingElement.classList.add("hidden");
setTimeout(() => loadingElement.remove(), 450);
// Autoplay unless the user requested reduced motion.
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (!reducedMotion) {
setTimeout(() => {
if (!userInteracted) {
setPlaying(true);
}
}, 900);
}
</script>
</body>
</html>
```