Prompt specification
Build or customize the Three.js 3D scene "Pitch Changes Travel Per Turn" 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: Pitch Changes Travel Per Turn
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pitch-changes-travel-per-turn-x2072
Scene Description Prompt:
Compare low-poly single-start screw lifts with different pitches under equal rotation, showing travel and ideal force tradeoffs. Three.js from cdnjs, one HTML file.
Working Three.js Starter (pitch-changes-travel-per-turn-x2072):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Single-Start Screw Lift: Pitch vs Travel and Force</title>
<style>
:root {
color-scheme: light;
--ink: #172231;
--muted: #637083;
--panel: rgba(255, 255, 255, 0.91);
--line: rgba(23, 34, 49, 0.12);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #edf1f5;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
.panel {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
box-shadow: 0 12px 35px rgba(23, 34, 49, 0.10);
backdrop-filter: blur(12px);
}
header {
position: fixed;
top: 18px;
left: 18px;
width: min(470px, calc(100vw - 390px));
padding: 17px 19px;
pointer-events: none;
}
header h1 {
margin: 0 0 6px;
font-size: clamp(19px, 2vw, 28px);
line-height: 1.08;
letter-spacing: -0.035em;
}
header p {
margin: 0;
max-width: 430px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
position: fixed;
top: 18px;
right: 18px;
width: 330px;
padding: 16px;
}
.control {
margin-bottom: 13px;
}
.control:last-of-type { margin-bottom: 8px; }
.control label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
margin-bottom: 7px;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.01em;
}
output {
color: var(--muted);
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: #2878d0;
cursor: pointer;
}
.buttons {
display: flex;
gap: 8px;
margin: 14px 0;
}
button {
flex: 1;
border: 1px solid rgba(23, 34, 49, 0.15);
border-radius: 9px;
padding: 9px 11px;
background: #fff;
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
button:hover { background: #f3f6f9; }
button.primary { background: #2878d0; border-color: #2878d0; color: white; }
button.primary:hover { background: #2169b9; }
.formula {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.formula span {
padding: 8px 9px;
border-radius: 9px;
background: #eef3f8;
color: #30445b;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 11px;
text-align: center;
}
.note {
margin: 10px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.legend {
display: flex;
gap: 13px;
margin-top: 9px;
color: var(--muted);
font-size: 10px;
}
.legend i {
display: inline-block;
width: 9px;
height: 9px;
margin-right: 4px;
border-radius: 50%;
vertical-align: -1px;
}
.stats {
position: fixed;
left: 18px;
right: 18px;
bottom: 18px;
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 12px;
pointer-events: none;
}
.stat-card {
position: relative;
overflow: hidden;
padding: 13px 15px 12px;
}
.stat-card::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--accent);
}
.stat-head {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
margin-bottom: 9px;
}
.stat-head h2 {
margin: 0;
font-size: 14px;
letter-spacing: -0.015em;
}
.stat-head span {
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.08em;
}
.metric {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
margin-top: 7px;
font-size: 11px;
color: var(--muted);
}
.metric strong {
color: var(--ink);
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.track {
height: 4px;
margin-top: 5px;
overflow: hidden;
border-radius: 999px;
background: #e6ebf0;
}
.track i {
display: block;
height: 100%;
width: 0;
border-radius: inherit;
transition: width 80ms linear;
}
.travel-bar { background: var(--accent); }
.force-bar { background: #b74c4c; }
.hint {
position: fixed;
left: 50%;
bottom: 174px;
transform: translateX(-50%);
padding: 6px 10px;
border-radius: 999px;
background: rgba(23, 34, 49, 0.72);
color: white;
font-size: 10px;
pointer-events: none;
opacity: 0.88;
}
@media (max-width: 820px) {
header {
width: calc(100vw - 36px);
right: 18px;
}
.controls {
top: 142px;
left: 18px;
right: 18px;
width: auto;
}
.stats {
gap: 7px;
}
.stat-card {
padding: 10px 10px 9px;
}
.stat-head {
display: block;
margin-bottom: 5px;
}
.stat-head span { display: none; }
.metric { font-size: 9px; }
.metric strong { font-size: 11px; }
.hint { display: none; }
}
@media (max-width: 480px) {
header { padding: 13px 14px; }
header p { font-size: 11px; }
.controls { top: 128px; padding: 12px; }
.formula { display: none; }
.note { margin-top: 7px; }
.stats { left: 10px; right: 10px; bottom: 10px; gap: 5px; }
.stat-card { padding: 8px 7px 7px; }
.stat-head h2 { font-size: 11px; }
.metric { display: block; }
.metric strong { display: block; margin-top: 2px; }
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="panel">
<h1>Single-start screw lift</h1>
<p>
All three screws receive the same shaft rotation and drive torque.
A larger pitch produces more travel but lower ideal axial force.
</p>
</header>
<section class="panel controls" aria-label="Simulation controls">
<div class="control">
<label for="rotation">
Equal shaft rotation
<output id="rotationOutput">360° · 1.00 rev</output>
</label>
<input id="rotation" type="range" min="0" max="2" step="0.01" value="1">
</div>
<div class="control">
<label for="torque">
Available drive torque
<output id="torqueOutput">20 N·m</output>
</label>
<input id="torque" type="range" min="5" max="50" step="1" value="20">
</div>
<div class="buttons">
<button id="play" class="primary" type="button">Animate</button>
<button id="resetCamera" type="button">Reset view</button>
</div>
<div class="formula">
<span>travel = pitch × turns</span>
<span>F = 2πτ / pitch</span>
</div>
<p class="note">
Ideal, frictionless model. For a single-start screw, lead = pitch.
With equal torque and rotation, travel × force is the same for every pitch.
</p>
<div class="legend">
<span><i style="background:#2878d0"></i>travel arrow</span>
<span><i style="background:#b74c4c"></i>axial force</span>
</div>
</section>
<div id="stats" class="stats"></div>
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------- DOM ----------
const sceneHost = document.getElementById("scene");
const statsHost = document.getElementById("stats");
const rotationInput = document.getElementById("rotation");
const torqueInput = document.getElementById("torque");
const rotationOutput = document.getElementById("rotationOutput");
const torqueOutput = document.getElementById("torqueOutput");
const playButton = document.getElementById("play");
const resetCameraButton = document.getElementById("resetCamera");
const PITCHES = [
{ mm: 4, color: "#2f80ed", name: "Fine" },
{ mm: 8, color: "#ed8a32", name: "Balanced" },
{ mm: 12, color: "#8b5cc7", name: "Fast" }
];
const MM_TO_WORLD = 0.12;
const PLATFORM_BASE_Y = 2.23;
const MAX_TURNS = 2;
const state = {
turns: 1,
torque: 20,
playing: false
};
statsHost.innerHTML = PITCHES.map((p, i) => `
<article class="panel stat-card" style="--accent:${p.color}">
<div class="stat-head">
<h2>${p.mm} mm/rev</h2>
<span>${p.name}</span>
</div>
<div class="metric">
<span>Travel</span>
<strong id="travel${i}">0.0 mm</strong>
</div>
<div class="track"><i id="travelBar${i}" class="travel-bar"></i></div>
<div class="metric">
<span>Ideal force</span>
<strong id="force${i}">0.0 kN</strong>
</div>
<div class="track"><i id="forceBar${i}" class="force-bar"></i></div>
</article>
`).join("");
// ---------- Renderer and scene ----------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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.domElement.setAttribute("aria-label", "Interactive 3D comparison of screw lift pitches");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xedf1f5);
scene.fog = new THREE.Fog(0xedf1f5, 24, 48);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
yaw: 0.43,
pitch: 0.34,
distance: 16.8,
target: new THREE.Vector3(0, 3.15, 0)
};
scene.add(new THREE.HemisphereLight(0xffffff, 0x748294, 2.1));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.7);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8ddff, 0.7);
fillLight.position.set(-8, 6, -7);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(50, 50),
new THREE.MeshStandardMaterial({ color: 0xe4e9ee, roughness: 0.95 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(34, 34, 0x9eabb8, 0xc3ccd5);
grid.position.y = 0.002;
grid.material.transparent = true;
grid.material.opacity = 0.35;
scene.add(grid);
// ---------- Shared materials ----------
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x273544, roughness: 0.72, metalness: 0.18, flatShading: true
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x4b5968, roughness: 0.56, metalness: 0.55, flatShading: true
});
const nutMaterial = new THREE.MeshStandardMaterial({
color: 0x566575, roughness: 0.48, metalness: 0.65, flatShading: true
});
const shaftMaterial = new THREE.MeshStandardMaterial({
color: 0x9ba8b5, roughness: 0.33, metalness: 0.78, flatShading: true
});
const loadMaterial = new THREE.MeshStandardMaterial({
color: 0x445260, roughness: 0.68, metalness: 0.12, flatShading: true
});
// ---------- Geometry helpers ----------
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(parent, radiusTop, radiusBottom, height, position, material, segments = 8) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, segments, 1),
material
);
mesh.position.set(...position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeTextSprite(text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "rgba(20, 29, 41, 0.94)";
ctx.beginPath();
if (ctx.roundRect) {
ctx.roundRect(4, 4, 504, 120, 24);
} else {
ctx.rect(4, 4, 504, 120);
}
ctx.fill();
ctx.strokeStyle = accent;
ctx.lineWidth = 6;
ctx.stroke();
ctx.fillStyle = "#ffffff";
ctx.font = "700 48px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 66);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(2.55, 0.64, 1);
sprite.renderOrder = 10;
return sprite;
}
// A faceted rectangular helical ribbon around the shaft.
function createThreadGeometry(length, worldPitch) {
const turns = length / worldPitch;
const segments = Math.max(36, Math.ceil(turns * 24));
const startY = 0.36;
const innerRadius = 0.13;
const outerRadius = 0.235;
const ribbonHeight = 0.085;
const positions = [];
const indices = [];
for (let s = 0; s <= segments; s++) {
const t = s / segments;
const angle = t * turns * Math.PI * 2;
const y = startY + t * length;
const c = Math.cos(angle);
const sn = Math.sin(angle);
positions.push(
outerRadius * c, y - ribbonHeight / 2, outerRadius * sn,
outerRadius * c, y + ribbonHeight / 2, outerRadius * sn,
innerRadius * c, y + ribbonHeight / 2, innerRadius * sn,
innerRadius * c, y - ribbonHeight / 2, innerRadius * sn
);
}
for (let s = 0; s < segments; s++) {
const a = s * 4;
const b = (s + 1) * 4;
for (let j = 0; j < 4; j++) {
const j1 = (j + 1) % 4;
indices.push(
a + j, a + j1, b + j1,
a + j, b + j1, b + j
);
}
}
// End caps.
indices.push(0, 2, 1, 0, 3, 2);
const end = segments * 4;
indices.push(end, end + 1, end + 2, end, end + 2, end + 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addHexNut(parent) {
const count = 6;
const outerRadius = 0.86;
const innerRadius = 0.44;
const height = 1.12;
const outerApothem = outerRadius * Math.cos(Math.PI / 6);
const innerApothem = innerRadius * Math.cos(Math.PI / 6);
const wall = outerApothem - innerApothem;
const radial = (outerApothem + innerApothem) / 2;
const sideLength = 2 * outerRadius * Math.sin(Math.PI / 6);
for (let i = 0; i < count; i++) {
const theta = i * Math.PI / 3 + Math.PI / 6;
const segment = addBox(
parent,
[sideLength + 0.035, height, wall],
[radial * Math.cos(theta), 1.22, radial * Math.sin(theta)],
nutMaterial
);
segment.rotation.y = Math.PI / 2 - theta;
}
}
function createRig(pitch, accent, xPosition) {
const rig = new THREE.Group();
rig.position.x = xPosition;
scene.add(rig);
const accentMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(accent),
roughness: 0.48,
metalness: 0.3,
flatShading: true
});
const threadMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color(accent),
roughness: 0.38,
metalness: 0.58,
flatShading: true,
side: THREE.DoubleSide
});
// Base and rear guide columns.
addBox(rig, [2.42, 0.34, 1.92], [0, 0.17, 0], baseMaterial);
addBox(rig, [1.85, 0.12, 1.38], [0, 0.40, 0], frameMaterial);
for (const x of [-0.88, 0.88]) {
addCylinder(
rig, 0.065, 0.065, 6.05,
[x, 3.43, -0.72],
frameMaterial, 6
);
}
// Fixed nut.
addHexNut(rig);
// Nut support blocks leave the screw path open.
addBox(rig, [0.18, 0.35, 0.52], [-0.66, 0.52, 0], baseMaterial);
addBox(rig, [0.18, 0.35, 0.52], [0.66, 0.52, 0], baseMaterial);
// Rotating screw.
const shaft = new THREE.Group();
rig.add(shaft);
addCylinder(
shaft,
0.145, 0.145, 6.12,
[0, 3.38, 0],
shaftMaterial, 10
);
const worldPitch = pitch * MM_TO_WORLD;
const thread = new THREE.Mesh(
createThreadGeometry(5.93, worldPitch),
threadMaterial
);
thread.castShadow = true;
thread.receiveShadow = true;
shaft.add(thread);
// A visible rotation marker on top of the shaft.
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0xe55252, roughness: 0.45, metalness: 0.15, flatShading: true
});
addBox(shaft, [0.39, 0.085, 0.105], [0.25, 6.40, 0], markerMaterial);
// Moving platform, built as a frame around the central screw.
const platform = new THREE.Group();
platform.position.y = PLATFORM_BASE_Y;
rig.add(platform);
addBox(platform, [0.39, 0.22, 1.45], [-0.81, 0, 0], accentMaterial);
addBox(platform, [0.39, 0.22, 1.45], [0.81, 0, 0], accentMaterial);
addBox(platform, [1.23, 0.22, 0.39], [0, 0, -0.53], accentMaterial);
addBox(platform, [1.23, 0.22, 0.39], [0, 0, 0.53], accentMaterial);
const load = addBox(
platform,
[1.07, 0.72, 0.92],
[0, 0.48, 0],
loadMaterial
);
addBox(
platform,
[0.78, 0.055, 0.64],
[0, 0.86, 0],
accentMaterial
);
// Zero-travel reference line.
const referenceGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-1.23, PLATFORM_BASE_Y - 0.15, 0),
new THREE.Vector3(1.23, PLATFORM_BASE_Y - 0.15, 0)
]);
const reference = new THREE.Line(
referenceGeometry,
new THREE.LineDashedMaterial({
color: 0x8190a0,
dashSize: 0.08,
gapSize: 0.055,
transparent: true,
opacity: 0.65
})
);
reference.computeLineDistances();
rig.add(reference);
// Upward travel arrow.
const travelArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(1.18, PLATFORM_BASE_Y + 0.12, 0),
0.1,
new THREE.Color(accent),
0.18,
0.11
);
rig.add(travelArrow);
// Downward ideal axial-force arrow.
const forceArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(-1.18, PLATFORM_BASE_Y + 1.55, 0),
0.8,
0xb74c4c,
0.18,
0.12
);
rig.add(forceArrow);
const label = makeTextSprite(`${pitch} mm/rev`, accent);
label.position.set(0, 6.98, 0);
rig.add(label);
return { rig, shaft, platform, travelArrow, forceArrow };
}
const rigs = PITCHES.map((p, i) =>
createRig(p.mm, p.color, (i - 1) * 5.05)
);
// ---------- Numerical model ----------
function updateVisuals() {
const angle = state.turns * Math.PI * 2;
const maximumTravel = MAX_TURNS * 12;
const maximumForce = state.torque * 2 * Math.PI / 0.004;
PITCHES.forEach((p, i) => {
const travelMM = state.turns * p.mm;
const travelWorld = travelMM * MM_TO_WORLD;
const forceN = state.torque * 2 * Math.PI / (p.mm / 1000);
const item = rigs[i];
item.shaft.rotation.y = -angle;
item.platform.position.y = PLATFORM_BASE_Y + travelWorld;
item.travelArrow.visible = travelWorld > 0.01;
item.travelArrow.setLength(
Math.max(travelWorld, 0.01),
Math.min(0.22, 0.08 + travelWorld * 0.08),
Math.min(0.13, 0.06 + travelWorld * 0.04)
);
const forceLength = 0.40 + 1.02 * (forceN / maximumForce);
item.forceArrow.position.set(
-1.18,
PLATFORM_BASE_Y + travelWorld + 1.55,
0
);
item.forceArrow.setLength(forceLength, 0.18, 0.12);
document.getElementById(`travel${i}`).textContent = `${travelMM.toFixed(1)} mm`;
document.getElementById(`force${i}`).textContent = `${(forceN / 1000).toFixed(1)} kN`;
document.getElementById(`travelBar${i}`).style.width =
`${100 * travelMM / maximumTravel}%`;
document.getElementById(`forceBar${i}`).style.width =
`${100 * forceN / maximumForce}%`;
});
const degrees = angle * 180 / Math.PI;
rotationOutput.textContent = `${degrees.toFixed(0)}° · ${state.turns.toFixed(2)} rev`;
torqueOutput.textContent = `${state.torque.toFixed(0)} N·m`;
}
// ---------- Controls ----------
function syncPlayButton() {
playButton.textContent = state.playing ? "Pause" : "Animate";
}
rotationInput.addEventListener("input", event => {
state.turns = Number(event.target.value);
state.playing = false;
syncPlayButton();
updateVisuals();
});
torqueInput.addEventListener("input", event => {
state.torque = Number(event.target.value);
updateVisuals();
});
playButton.addEventListener("click", () => {
if (state.turns >= MAX_TURNS - 0.001) state.turns = 0;
state.playing = !state.playing;
syncPlayButton();
updateVisuals();
});
// ---------- Camera controls implemented directly ----------
function resetCamera() {
cameraState.yaw = 0.43;
cameraState.pitch = 0.34;
cameraState.distance = 16.8;
cameraState.target.set(0, 3.15, 0);
}
resetCameraButton.addEventListener("click", resetCamera);
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.08,
1.12
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
9.5,
26
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const fitFactor = Math.max(1, 1.08 / aspect);
const distance = cameraState.distance * fitFactor;
const offset = new THREE.Vector3(
Math.sin(cameraState.yaw) * Math.cos(cameraState.pitch),
Math.sin(cameraState.pitch),
Math.cos(cameraState.yaw) * Math.cos(cameraState.pitch)
).multiplyScalar(distance);
camera.position.copy(cameraState.target).add(offset);
camera.lookAt(cameraState.target);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
// ---------- Render loop ----------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (state.playing) {
state.turns = Math.min(MAX_TURNS, state.turns + delta * 0.22);
rotationInput.value = state.turns.toFixed(2);
if (state.turns >= MAX_TURNS) {
state.playing = false;
syncPlayButton();
}
updateVisuals();
}
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resize();
updateVisuals();
syncPlayButton();
animate();
</script>
</body>
</html>
```