Prompt specification
Build or customize the Three.js 3D scene "Rotating Screw Moves a Captive Nut" 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: Rotating Screw Moves a Captive Nut
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/rotating-screw-moves-a-captive-nut-x2070
Scene Description Prompt:
Rotating screw moves a captive nut. Animate a nut constrained against rotation while a single-start screw turns. Couple axial translation to turn count multiplied by pitch, avoiding independent decorative motion. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (rotating-screw-moves-a-captive-nut-x2070):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Screw and Captive Nut</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(8, 18, 28, 0.84);
--border: rgba(120, 220, 255, 0.22);
--cyan: #63f4dd;
--orange: #ffad42;
--muted: #91a8b7;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071019;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 16px 17px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
user-select: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0 0 12px;
font-size: 20px;
line-height: 1.12;
}
.formula {
margin: 10px 0 12px;
padding: 10px 11px;
border: 1px solid rgba(99, 244, 221, 0.16);
border-radius: 9px;
background: rgba(99, 244, 221, 0.055);
color: #d9fff8;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.55;
}
.readout {
display: grid;
grid-template-columns: 1fr auto;
gap: 6px 14px;
font-size: 12px;
}
.readout span:nth-child(odd) {
color: var(--muted);
}
.readout span:nth-child(even) {
color: #f3fbff;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-weight: 700;
text-align: right;
}
.locked {
color: var(--orange) !important;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 14px;
}
button {
flex: 1;
padding: 8px 9px;
border: 1px solid rgba(140, 210, 235, 0.22);
border-radius: 8px;
background: rgba(255, 255, 255, 0.055);
color: #e9f7ff;
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
background 120ms ease,
border-color 120ms ease,
transform 120ms ease;
}
button:hover {
background: rgba(99, 244, 221, 0.12);
border-color: rgba(99, 244, 221, 0.45);
}
button:active {
transform: translateY(1px);
}
.hint {
position: fixed;
z-index: 10;
right: 18px;
bottom: 16px;
max-width: calc(100vw - 36px);
padding: 8px 11px;
border: 1px solid rgba(120, 220, 255, 0.15);
border-radius: 8px;
background: rgba(8, 18, 28, 0.68);
color: #a9bdca;
font-size: 11px;
text-align: right;
backdrop-filter: blur(10px);
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font-size: 10px;
}
.legend i {
display: inline-block;
width: 8px;
height: 8px;
margin-right: 5px;
border-radius: 2px;
vertical-align: 0;
}
.loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #071019;
color: var(--cyan);
font-size: 13px;
letter-spacing: 0.08em;
text-transform: uppercase;
transition: opacity 300ms ease;
pointer-events: none;
}
.loading.hidden {
opacity: 0;
}
@media (max-width: 600px) {
.panel {
top: 10px;
left: 10px;
width: calc(100vw - 20px);
padding: 13px 14px;
}
h1 {
font-size: 17px;
}
.hint {
right: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0"></canvas>
<section class="panel">
<div class="eyebrow">Single-start voxel mechanism</div>
<h1>Rotating Screw / Captive Nut</h1>
<div class="formula">
θ = 2πN<br>
Δy = N × p
</div>
<div class="readout">
<span>Turn count N</span><span id="turns">+0.00</span>
<span>Axial travel</span><span id="travel">+0.00</span>
<span>Pitch p</span><span>0.36 units/rev</span>
<span>Nut orientation</span><span class="locked">0° — locked</span>
</div>
<div class="buttons">
<button id="pause" type="button">PAUSE</button>
<button id="reverse" type="button">REVERSE</button>
<button id="reset" type="button">RESET</button>
</div>
<div class="legend">
<span><i style="background:#55ead5"></i>Single-start screw</span>
<span><i style="background:#ff9e35"></i>Captive nut</span>
<span><i style="background:#71869a"></i>Anti-rotation rails</span>
</div>
</section>
<div class="hint">
Drag: orbit · Shift/right-drag: pan · Wheel: zoom · Double-click: reset view
</div>
<div id="loading" class="loading">Loading Three.js…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Mechanical constants
// -------------------------------------------------------------------------
const TAU = Math.PI * 2;
const PITCH = 0.36; // Axial distance per complete revolution.
const INITIAL_NUT_Y = -2.20;
const CONTACT_ANGLE = Math.PI / 2;
const canvas = document.querySelector("#scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.FogExp2(0x071019, 0.026);
const camera = new THREE.PerspectiveCamera(
46,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9bdcff, 0x162331, 1.45));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.0);
keyLight.position.set(6, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0003;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x31e8ff, 28, 20, 2);
rimLight.position.set(-4, 1.5, -4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8a32, 16, 12, 2);
warmLight.position.set(4, -2.5, 4);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Shared voxel geometry and instancing helper
// -------------------------------------------------------------------------
const boxGeometry = new THREE.BoxGeometry(1, 1, 1);
function createVoxelInstances(
entries,
parent,
{
roughness = 0.58,
metalness = 0.12,
castShadow = true,
receiveShadow = true
} = {}
) {
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness,
metalness,
flatShading: true
});
const mesh = new THREE.InstancedMesh(
boxGeometry,
material,
entries.length
);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
entries.forEach((entry, index) => {
dummy.position.set(
entry.x ?? 0,
entry.y ?? 0,
entry.z ?? 0
);
dummy.rotation.set(
entry.rx ?? 0,
entry.ry ?? 0,
entry.rz ?? 0
);
dummy.scale.set(
entry.sx ?? 1,
entry.sy ?? 1,
entry.sz ?? 1
);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (entry.color !== undefined) {
color.set(entry.color);
mesh.setColorAt(index, color);
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
mesh.computeBoundingSphere();
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x0b1720,
roughness: 0.93,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -5.72;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x244657, 0x132733);
grid.position.y = -5.705;
grid.material.transparent = true;
grid.material.opacity = 0.45;
scene.add(grid);
// -------------------------------------------------------------------------
// Static supports, guide rails, and ruler
// -------------------------------------------------------------------------
const staticVoxels = [];
// Lower bearing / motor housing, built as a square voxel ring.
for (const y of [-5.47, -5.25]) {
for (let ix = -3; ix <= 3; ix++) {
for (let iz = -3; iz <= 3; iz++) {
const m = Math.max(Math.abs(ix), Math.abs(iz));
if (m >= 2) {
staticVoxels.push({
x: ix * 0.22,
y,
z: iz * 0.22,
sx: 0.205,
sy: 0.205,
sz: 0.205,
color:
(ix + iz) % 2 === 0
? 0x2c4353
: 0x213644
});
}
}
}
}
// Upper bearing ring.
for (let ix = -3; ix <= 3; ix++) {
for (let iz = -3; iz <= 3; iz++) {
const m = Math.max(Math.abs(ix), Math.abs(iz));
if (m >= 2) {
staticVoxels.push({
x: ix * 0.21,
y: 6.20,
z: iz * 0.21,
sx: 0.198,
sy: 0.24,
sz: 0.198,
color: (ix + iz) % 2 === 0 ? 0x314b5c : 0x243b4a
});
}
}
}
// Two long, square-section anti-rotation rails.
for (const x of [-1.08, 1.08]) {
for (let y = -5.0; y <= 6.05; y += 0.18) {
staticVoxels.push({
x,
y,
z: 0,
sx: 0.14,
sy: 0.164,
sz: 0.14,
color: Math.round(y * 10) % 3 === 0 ? 0x8299aa : 0x6f8799
});
}
}
// Vertical voxel ruler.
for (let y = -4.95; y <= 4.85; y += 0.18) {
staticVoxels.push({
x: -1.91,
y,
z: 0.20,
sx: 0.075,
sy: 0.165,
sz: 0.075,
color: 0x486171
});
}
// One tick per screw pitch.
for (let k = -7; k <= 18; k++) {
const y = INITIAL_NUT_Y + k * PITCH;
const isZero = k === 0;
staticVoxels.push({
x: -1.71,
y,
z: 0.20,
sx: isZero ? 0.43 : 0.31,
sy: isZero ? 0.055 : 0.035,
sz: 0.085,
color: isZero ? 0xf2fbff : 0x5d7687
});
}
createVoxelInstances(staticVoxels, scene, {
roughness: 0.64,
metalness: 0.24
});
// -------------------------------------------------------------------------
// Rotating screw assembly
// -------------------------------------------------------------------------
const screwGroup = new THREE.Group();
scene.add(screwGroup);
const screwMinY = -5.58;
const screwMaxY = 6.48;
// Square voxel shaft core.
const coreVoxels = [];
const coreCell = 0.07;
for (let y = screwMinY; y <= screwMaxY; y += coreCell) {
for (let ix = -2; ix <= 2; ix++) {
for (let iz = -2; iz <= 2; iz++) {
coreVoxels.push({
x: ix * coreCell,
y,
z: iz * coreCell,
sx: 0.066,
sy: 0.066,
sz: 0.066,
color:
(ix + iz) % 2 === 0
? 0x176a78
: 0x125b6b
});
}
}
}
createVoxelInstances(coreVoxels, screwGroup, {
roughness: 0.42,
metalness: 0.46
});
/*
One continuous single-start thread.
The helix is left-handed with respect to the chosen positive rotation:
localAngle = contactAngle - 2π(y - initialY) / pitch
After rotating the screw by θ = 2πN, a ridge is at the fixed nut
contact angle when:
y = initialY + N * pitch
Thus the nut translation is not animated independently.
*/
const threadVoxels = [];
const threadRadius = 0.235;
const threadSize = 0.085;
const threadStep = PITCH / 30;
for (
let y = screwMinY;
y <= screwMaxY;
y += threadStep
) {
const centerAngle =
CONTACT_ANGLE -
TAU * (y - INITIAL_NUT_Y) / PITCH;
for (const angularOffset of [-0.13, 0, 0.13]) {
const angle = centerAngle + angularOffset;
threadVoxels.push({
x: threadRadius * Math.cos(angle),
y,
z: threadRadius * Math.sin(angle),
sx: threadSize,
sy: threadSize,
sz: threadSize,
color:
Math.abs(angularOffset) > 0.01
? 0x45d8ca
: 0x67f2df
});
}
}
createVoxelInstances(threadVoxels, screwGroup, {
roughness: 0.34,
metalness: 0.38
});
// A bright marker and partial arrow, rigidly attached to the screw.
const screwMarkerVoxels = [
{
x: 0.39,
y: -4.75,
z: 0,
sx: 0.17,
sy: 0.15,
sz: 0.17,
color: 0xffd35a
},
{
x: 0.51,
y: -4.75,
z: 0,
sx: 0.12,
sy: 0.15,
sz: 0.12,
color: 0xffd35a
}
];
for (let i = 0; i < 8; i++) {
const angle = -0.72 + i * 0.18;
screwMarkerVoxels.push({
x: 0.54 * Math.cos(angle),
y: -4.44,
z: 0.54 * Math.sin(angle),
sx: 0.105,
sy: 0.09,
sz: 0.105,
color: 0xffc13e
});
}
createVoxelInstances(screwMarkerVoxels, screwGroup, {
roughness: 0.48,
metalness: 0.18
});
// -------------------------------------------------------------------------
// Captive, non-rotating nut
// -------------------------------------------------------------------------
const nutGroup = new THREE.Group();
nutGroup.position.y = INITIAL_NUT_Y;
scene.add(nutGroup);
const nutVoxels = [];
const nutCell = 0.135;
// Two voxel layers forming a square nut with a visible central bore.
for (const y of [-0.085, 0.085]) {
for (let ix = -5; ix <= 5; ix++) {
for (let iz = -5; iz <= 5; iz++) {
const m = Math.max(Math.abs(ix), Math.abs(iz));
if (m >= 3 && m <= 5) {
const checker = (ix + iz) % 2 === 0;
nutVoxels.push({
x: ix * nutCell,
y,
z: iz * nutCell,
sx: 0.125,
sy: 0.145,
sz: 0.125,
color:
m === 3
? checker
? 0xd96e22
: 0xf1842c
: checker
? 0xff9d32
: 0xf58b25
});
}
}
}
}
// Connectors from the nut to both rails.
for (const side of [-1, 1]) {
nutVoxels.push({
x: side * 0.86,
y: 0,
z: 0,
sx: 0.38,
sy: 0.22,
sz: 0.22,
color: 0xe67d25
});
// Four corner blocks form a sliding sleeve around each square rail.
for (const dx of [-0.12, 0.12]) {
for (const dz of [-0.12, 0.12]) {
nutVoxels.push({
x: side * 1.08 + dx,
y: 0,
z: dz,
sx: 0.07,
sy: 0.38,
sz: 0.07,
color: 0x3bc9d5
});
}
}
}
// The visible internal tooth remains at a fixed angular position.
nutVoxels.push({
x: 0,
y: 0,
z: 0.245,
sx: 0.105,
sy: 0.11,
sz: 0.10,
color: 0xffc15a
});
// Asymmetric vertical witness marks make the locked nut orientation obvious.
for (const y of [-0.12, 0, 0.12]) {
nutVoxels.push({
x: 0.51,
y,
z: 0.755,
sx: 0.10,
sy: 0.095,
sz: 0.075,
color: 0xfff0b0
});
}
// Pointer attached to the nut; it follows the nut's exact axial position.
nutVoxels.push({
x: -1.47,
y: 0,
z: 0.20,
sx: 0.17,
sy: 0.085,
sz: 0.22,
color: 0xffad42
});
createVoxelInstances(nutVoxels, nutGroup, {
roughness: 0.51,
metalness: 0.16
});
// -------------------------------------------------------------------------
// Camera controls implemented directly, without camera-control addons
// -------------------------------------------------------------------------
const cameraState = {
target: new THREE.Vector3(0, -0.2, 0.12),
azimuth: 0.58,
polar: 1.38,
radius: 15.2
};
const defaultCamera = {
target: cameraState.target.clone(),
azimuth: cameraState.azimuth,
polar: cameraState.polar,
radius: cameraState.radius
};
function updateCamera() {
const sinPolar = Math.sin(cameraState.polar);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPolar * Math.sin(cameraState.azimuth),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.polar),
cameraState.target.z +
cameraState.radius * sinPolar * Math.cos(cameraState.azimuth)
);
camera.lookAt(cameraState.target);
}
let dragging = false;
let dragMode = "orbit";
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
dragMode =
event.button === 2 || event.shiftKey ? "pan" : "orbit";
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
if (dragMode === "orbit") {
cameraState.azimuth -= dx * 0.006;
cameraState.polar = THREE.MathUtils.clamp(
cameraState.polar - dy * 0.005,
0.28,
Math.PI - 0.28
);
} else {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const up = new THREE.Vector3()
.crossVectors(right, forward)
.normalize();
const scale = cameraState.radius * 0.00145;
cameraState.target.addScaledVector(right, -dx * scale);
cameraState.target.addScaledVector(up, dy * scale);
}
updateCamera();
});
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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
5.5,
25
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
cameraState.target.copy(defaultCamera.target);
cameraState.azimuth = defaultCamera.azimuth;
cameraState.polar = defaultCamera.polar;
cameraState.radius = defaultCamera.radius;
updateCamera();
});
updateCamera();
// -------------------------------------------------------------------------
// Mechanical state and controls
// -------------------------------------------------------------------------
const mechanical = {
turns: 0,
turnsPerSecond: 0.32,
paused: false
};
const turnsElement = document.querySelector("#turns");
const travelElement = document.querySelector("#travel");
const pauseButton = document.querySelector("#pause");
const reverseButton = document.querySelector("#reverse");
const resetButton = document.querySelector("#reset");
function signed(value, digits = 2) {
const prefix = value >= 0 ? "+" : "";
return prefix + value.toFixed(digits);
}
function togglePause() {
mechanical.paused = !mechanical.paused;
pauseButton.textContent = mechanical.paused ? "RESUME" : "PAUSE";
}
function reverseDrive() {
mechanical.turnsPerSecond *= -1;
reverseButton.textContent =
mechanical.turnsPerSecond < 0 ? "REVERSE ✓" : "REVERSE";
}
function resetMechanism() {
mechanical.turns = 0;
mechanical.turnsPerSecond = 0.32;
mechanical.paused = false;
pauseButton.textContent = "PAUSE";
reverseButton.textContent = "REVERSE";
}
pauseButton.addEventListener("click", togglePause);
reverseButton.addEventListener("click", reverseDrive);
resetButton.addEventListener("click", resetMechanism);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "v") {
reverseDrive();
}
});
// -------------------------------------------------------------------------
// Animation: one source of truth for screw rotation and nut translation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!mechanical.paused) {
mechanical.turns += mechanical.turnsPerSecond * dt;
}
const turns = mechanical.turns;
const travel = turns * PITCH;
// Screw angle and nut translation both derive from the same turn count.
screwGroup.rotation.y = turns * TAU;
nutGroup.position.y = INITIAL_NUT_Y + travel;
// The nut is mechanically constrained and must never acquire yaw.
nutGroup.rotation.set(0, 0, 0);
turnsElement.textContent = signed(turns);
travelElement.textContent = signed(travel);
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
});
animate();
document.querySelector("#loading").classList.add("hidden");
</script>
</body>
</html>
```