Prompt specification
Build or customize the Three.js 3D scene "Thread pitch alignment model" 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: Thread pitch alignment model
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/thread-pitch-alignment-model-x0285
Scene Description Prompt:
Thread pitch alignment model. Show a short threaded rod and cutaway nut with matching helical grooves; align their axes and enlarge the spacing enough to inspect engagement clearly. Please build this with Three.js from cdnjs in one self-contained HTML file, using visible voxel blocks.
Working Three.js Starter (thread-pitch-alignment-model-x0285):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Thread Pitch Alignment Model</title>
<style>
:root {
color-scheme: dark;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 30% 20%, #183342 0, transparent 42%),
radial-gradient(circle at 78% 70%, #281d13 0, transparent 38%),
linear-gradient(145deg, #071017 0%, #0b151d 48%, #071016 100%);
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.title {
position: fixed;
top: 24px;
left: 26px;
z-index: 10;
max-width: 440px;
pointer-events: none;
text-shadow: 0 2px 18px #000;
}
.eyebrow {
margin-bottom: 8px;
color: #61ddeb;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #f4f8fa;
font-size: clamp(24px, 3vw, 40px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 0;
color: #9fb3bf;
font-size: 13px;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 20;
top: 24px;
right: 24px;
width: 292px;
padding: 18px;
border: 1px solid rgba(132, 205, 218, 0.18);
border-radius: 16px;
background: rgba(7, 17, 24, 0.82);
box-shadow:
0 20px 55px rgba(0, 0, 0, 0.35),
inset 0 1px rgba(255, 255, 255, 0.04);
backdrop-filter: blur(15px);
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-bottom: 18px;
}
.metric {
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
color: #708b99;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
}
.metric strong {
display: block;
margin-top: 4px;
color: #dce9ee;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 13px;
}
.control-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: #bdd0d8;
font-size: 12px;
font-weight: 700;
}
#travelValue {
color: #61ddeb;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
}
input[type="range"] {
width: 100%;
margin: 12px 0 4px;
accent-color: #55d9e9;
cursor: ew-resize;
}
.range-ends {
display: flex;
justify-content: space-between;
color: #617985;
font-size: 10px;
}
.note {
margin: 14px 0;
color: #8098a5;
font-size: 10px;
line-height: 1.55;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
padding: 9px 10px;
border: 1px solid rgba(101, 218, 232, 0.2);
border-radius: 9px;
color: #d9edf1;
background: rgba(82, 205, 220, 0.08);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
background 150ms ease,
border-color 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(101, 218, 232, 0.48);
background: rgba(82, 205, 220, 0.15);
}
button:active {
transform: translateY(1px);
}
.legend {
position: fixed;
z-index: 10;
left: 26px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
gap: 9px 17px;
max-width: 480px;
padding: 12px 14px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 12px;
color: #91a8b3;
background: rgba(6, 14, 20, 0.65);
backdrop-filter: blur(12px);
font-size: 10px;
pointer-events: none;
}
.legend span {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18);
}
.hint {
position: fixed;
right: 26px;
bottom: 24px;
z-index: 10;
color: #77909c;
font-size: 10px;
pointer-events: none;
}
@media (max-width: 760px) {
.title {
top: 17px;
left: 17px;
max-width: calc(100vw - 34px);
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: min(292px, calc(100vw - 24px));
}
.legend,
.hint {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="title">
<div class="eyebrow">Mechanical phase study</div>
<h1>Thread Pitch Alignment</h1>
<p class="subtitle">
A voxel threaded rod and quarter-section nut sharing one X-axis,
identical helix pitch, and a separated inspection view.
</p>
</header>
<aside class="panel">
<div class="metrics">
<div class="metric">
<span>THREAD PITCH</span>
<strong>1.50 units</strong>
</div>
<div class="metric">
<span>VOXEL SIZE</span>
<strong>0.22 units</strong>
</div>
<div class="metric">
<span>AXIS</span>
<strong>Shared X</strong>
</div>
<div class="metric">
<span>SECTION</span>
<strong>90° cutaway</strong>
</div>
</div>
<div class="control-title">
<label for="travel">Nut axial travel</label>
<output id="travelValue">Gap 3.00 u</output>
</div>
<input
id="travel"
type="range"
min="-4.5"
max="2"
step="0.05"
value="0"
aria-label="Nut axial travel"
>
<div class="range-ends">
<span>Engaged</span>
<span>Exploded</span>
</div>
<p class="note">
Travel rotates the nut by one complete turn per pitch, keeping the
internal groove registered to the external ridge.
</p>
<div class="buttons">
<button id="engageButton" type="button">Engage preview</button>
<button id="explodeButton" type="button">Exploded view</button>
<button id="resetButton" type="button" style="grid-column: 1 / -1">
Reset camera
</button>
</div>
</aside>
<div class="legend">
<span>
<i class="swatch" style="background:#ffad57"></i>
External thread ridge
</span>
<span>
<i class="swatch" style="background:#38d1df"></i>
Internal groove lining
</span>
<span>
<i class="swatch" style="background:#d9a744"></i>
Nut body
</span>
<span>
<i class="swatch" style="background:#70899c"></i>
Rod core
</span>
</div>
<div class="hint">Drag to orbit · Wheel to zoom</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 stage = document.querySelector("#stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x000000, 0);
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
target: new THREE.Vector3(2.65, 0, 0),
theta: 0.55,
phi: 1.08,
radius: window.innerWidth < 700 ? 20 : 15.8,
targetTheta: 0.55,
targetPhi: 1.08,
targetRadius: window.innerWidth < 700 ? 20 : 15.8
};
// ---------------------------------------------------------------------
// Lighting and ground
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xc5efff, 0x15212b, 2.0));
const keyLight = new THREE.DirectionalLight(0xfff2dc, 3.2);
keyLight.position.set(-3, 11, 8);
keyLight.target.position.set(2.5, 0, 0);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.00015;
scene.add(keyLight, keyLight.target);
const rimLight = new THREE.DirectionalLight(0xff9a4c, 1.35);
rimLight.position.set(10, 3, -9);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 22),
new THREE.MeshStandardMaterial({
color: 0x09131a,
roughness: 0.96,
metalness: 0.05,
transparent: true,
opacity: 0.72
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.set(2.5, -2.22, 0);
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(
30,
60,
0x2c7180,
0x173640
);
grid.position.set(2.5, -2.205, 0);
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// ---------------------------------------------------------------------
// Model constants
// ---------------------------------------------------------------------
const TWO_PI = Math.PI * 2;
const CELL = 0.22;
const PITCH = 1.5;
const THREAD_HALF_WIDTH = 0.29;
const CORE_RADIUS = 0.64;
const RIDGE_RADIUS = 1.0;
const ROD_START = -4.0;
const ROD_END = 3.2;
const NUT_LENGTH = 4.4;
// The exploded nut is exactly two pitches from the rod end.
// This makes the visible helix orientation easy to compare.
const REFERENCE_NUT_X = ROD_END + 2 * PITCH;
// Phase offset places useful thread sections in the camera-facing view.
const HELIX_OFFSET = 0.45;
const COLORS = {
rodCore: new THREE.Color(0x70899c),
rodRidge: new THREE.Color(0xffad57),
nutBody: new THREE.Color(0xd9a744),
nutInner: new THREE.Color(0x9b6d25),
cutFace: new THREE.Color(0xffd678),
groove: new THREE.Color(0x38d1df)
};
let randomSeed = 928371;
function random() {
randomSeed =
(randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function variedColor(base, amount = 0.12) {
return base.clone().multiplyScalar(
1 + (random() - 0.5) * amount
);
}
// Distance to the nearest equivalent helical phase.
function phaseDistance(value) {
const fraction = ((value / PITCH) % 1 + 1) % 1;
return (
(fraction < 0.5 ? fraction : fraction - 1) * PITCH
);
}
function insideHexNut(y, z, apothem = 1.82) {
// Three unique normals define a regular hexagonal cross-section.
const n0 = y;
const n1 = y * 0.5 + z * Math.sqrt(3) * 0.5;
const n2 = -y * 0.5 + z * Math.sqrt(3) * 0.5;
return Math.max(
Math.abs(n0),
Math.abs(n1),
Math.abs(n2)
) <= apothem;
}
function makeVoxelMesh(voxels, material) {
const geometry = new THREE.BoxGeometry(
CELL * 0.84,
CELL * 0.84,
CELL * 0.84
);
const mesh = new THREE.InstancedMesh(
geometry,
material,
voxels.length
);
const dummy = new THREE.Object3D();
voxels.forEach((voxel, index) => {
dummy.position.set(voxel.x, voxel.y, voxel.z);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, voxel.color);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) {
mesh.instanceColor.needsUpdate = true;
}
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.frustumCulled = false;
return mesh;
}
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.53,
metalness: 0.22
});
// ---------------------------------------------------------------------
// Threaded rod
// ---------------------------------------------------------------------
const rodVoxels = [];
const rodCellCount = Math.round(
(ROD_END - ROD_START) / CELL
);
for (let ix = 0; ix < rodCellCount; ix++) {
const x = ROD_START + (ix + 0.5) * CELL;
for (let iy = -5; iy <= 5; iy++) {
const y = (iy + 0.5) * CELL;
for (let iz = -5; iz <= 5; iz++) {
const z = (iz + 0.5) * CELL;
const radius = Math.hypot(y, z);
const theta = Math.atan2(z, y);
const phase = phaseDistance(
x - (PITCH * theta) / TWO_PI + HELIX_OFFSET
);
const isCore = radius <= CORE_RADIUS;
const isRidge =
radius > CORE_RADIUS &&
radius <= RIDGE_RADIUS &&
Math.abs(phase) <= THREAD_HALF_WIDTH;
if (!isCore && !isRidge) {
continue;
}
rodVoxels.push({
x,
y,
z,
color: variedColor(
isRidge ? COLORS.rodRidge : COLORS.rodCore,
isRidge ? 0.16 : 0.11
)
});
}
}
}
const rodMesh = makeVoxelMesh(rodVoxels, voxelMaterial);
scene.add(rodMesh);
// ---------------------------------------------------------------------
// Hexagonal nut with a quarter-section cutaway
// ---------------------------------------------------------------------
const nutVoxels = [];
const nutCellCount = Math.round(NUT_LENGTH / CELL);
for (let ix = 0; ix < nutCellCount; ix++) {
const localX = (ix + 0.5) * CELL;
for (let iy = -11; iy <= 11; iy++) {
const y = (iy + 0.5) * CELL;
for (let iz = -11; iz <= 11; iz++) {
const z = (iz + 0.5) * CELL;
const radius = Math.hypot(y, z);
const theta = Math.atan2(z, y);
if (radius < 1.03 || !insideHexNut(y, z)) {
continue;
}
// Remove the near upper quadrant to expose the bore.
if (y > 0 && z > 0) {
continue;
}
// The groove follows the same pitch and phase as the rod ridge.
const groovePhase = phaseDistance(
localX -
(PITCH * theta) / TWO_PI +
REFERENCE_NUT_X +
HELIX_OFFSET
);
// Remove a helical channel into the inner nut wall.
if (
Math.abs(groovePhase) <= THREAD_HALF_WIDTH &&
radius <= 1.62
) {
continue;
}
const adjacentToCut =
(
y > 0 &&
z < 0 &&
z + CELL > 0
) ||
(
z > 0 &&
y < 0 &&
y + CELL > 0
);
let color;
if (adjacentToCut) {
color = COLORS.cutFace;
} else if (
radius < 1.62 &&
Math.abs(groovePhase) <= 0.58
) {
color = COLORS.groove;
} else if (radius < 1.36) {
color = COLORS.nutInner;
} else {
color = COLORS.nutBody;
}
nutVoxels.push({
x: localX,
y,
z,
color: variedColor(color, 0.13)
});
}
}
}
const nutGroup = new THREE.Group();
nutGroup.position.x = REFERENCE_NUT_X;
const nutMesh = makeVoxelMesh(nutVoxels, voxelMaterial);
nutGroup.add(nutMesh);
scene.add(nutGroup);
// ---------------------------------------------------------------------
// Dashed shared axis
// ---------------------------------------------------------------------
const axisGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-4.8, 0, 0),
new THREE.Vector3(11.3, 0, 0)
]);
const axisLine = new THREE.Line(
axisGeometry,
new THREE.LineDashedMaterial({
color: 0x62e2ed,
dashSize: 0.22,
gapSize: 0.14,
transparent: true,
opacity: 0.75,
depthTest: false,
depthWrite: false
})
);
axisLine.computeLineDistances();
axisLine.renderOrder = 5;
scene.add(axisLine);
// ---------------------------------------------------------------------
// Voxel phase guide between the separated parts
// ---------------------------------------------------------------------
const guideCapacity = 90;
const guideMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.085, 0.085, 0.085),
new THREE.MeshBasicMaterial({
color: 0x69edf7,
transparent: true,
opacity: 0.68,
depthWrite: false
}),
guideCapacity
);
guideMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
guideMesh.count = 0;
guideMesh.frustumCulled = false;
guideMesh.renderOrder = 3;
scene.add(guideMesh);
const guideDummy = new THREE.Object3D();
function updatePhaseGuide() {
const nutLeft = REFERENCE_NUT_X + travel;
const gap = nutLeft - ROD_END;
if (gap <= 0.08) {
guideMesh.count = 0;
guideMesh.instanceMatrix.needsUpdate = true;
return;
}
const count = Math.min(
guideCapacity,
Math.floor(gap / 0.14)
);
for (let i = 0; i < count; i++) {
const x = ROD_END + 0.08 + i * 0.14;
const theta =
(TWO_PI * (x + HELIX_OFFSET)) / PITCH;
const radius = 1.075;
guideDummy.position.set(
x,
radius * Math.cos(theta),
radius * Math.sin(theta)
);
guideDummy.updateMatrix();
guideMesh.setMatrixAt(i, guideDummy.matrix);
}
guideMesh.count = count;
guideMesh.instanceMatrix.needsUpdate = true;
}
// ---------------------------------------------------------------------
// Labels and leader lines
// ---------------------------------------------------------------------
function makeLabel(text, accent) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 128;
const context = canvas.getContext("2d");
context.fillStyle = "rgba(6, 15, 21, 0.88)";
context.fillRect(0, 8, 640, 112);
context.strokeStyle = accent;
context.lineWidth = 4;
context.strokeRect(2, 10, 636, 108);
context.fillStyle = accent;
context.font =
"700 34px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 320, 65);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(3.65, 0.73, 1);
sprite.renderOrder = 10;
return sprite;
}
function makeLeader(points, color) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map(point => new THREE.Vector3(...point))
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.72,
depthTest: false,
depthWrite: false
})
);
line.renderOrder = 9;
return line;
}
const rodLabel = makeLabel("THREADED ROD", "#ffad57");
rodLabel.position.set(-2.55, 1.48, 0.2);
scene.add(rodLabel);
scene.add(
makeLeader(
[
[-2.55, 1.13, 0.2],
[-2.05, 0.82, 0.25]
],
0xffad57
)
);
const nutLabel = makeLabel("CUTAWAY NUT", "#55e2ed");
nutLabel.position.set(2.15, 2.72, 0.15);
nutGroup.add(nutLabel);
nutGroup.add(
makeLeader(
[
[2.15, 2.34, 0.15],
[1.9, 1.63, 0.25]
],
0x55e2ed
)
);
// ---------------------------------------------------------------------
// Travel control and phase-preserving nut rotation
// ---------------------------------------------------------------------
const travelInput = document.querySelector("#travel");
const travelValue = document.querySelector("#travelValue");
let travel = 0;
let travelAnimation = null;
function setTravel(value) {
travel = value;
nutGroup.position.x = REFERENCE_NUT_X + travel;
// Translating one pitch requires one revolution to retain the
// same helical phase relative to the fixed rod.
nutGroup.rotation.x = (TWO_PI * travel) / PITCH;
travelInput.value = travel.toFixed(2);
const gap =
nutGroup.position.x - ROD_END;
travelValue.textContent =
gap >= 0
? `Gap ${gap.toFixed(2)} u`
: `Overlap ${Math.abs(gap).toFixed(2)} u`;
updatePhaseGuide();
}
function animateTravel(destination, duration = 850) {
travelAnimation = {
from: travel,
to: destination,
start: performance.now(),
duration
};
}
travelInput.addEventListener("input", event => {
travelAnimation = null;
setTravel(Number(event.target.value));
});
document.querySelector("#engageButton").addEventListener(
"click",
() => animateTravel(-4.5)
);
document.querySelector("#explodeButton").addEventListener(
"click",
() => animateTravel(0)
);
// ---------------------------------------------------------------------
// Custom camera controls: no addons are used
// ---------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) {
return;
}
const deltaX = event.clientX - lastPointerX;
const deltaY = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.targetTheta -= deltaX * 0.006;
orbit.targetPhi = THREE.MathUtils.clamp(
orbit.targetPhi - deltaY * 0.006,
0.22,
Math.PI - 0.22
);
});
function stopDragging(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * Math.exp(event.deltaY * 0.001),
8,
30
);
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", event => {
event.preventDefault();
});
document.querySelector("#resetButton").addEventListener(
"click",
() => {
orbit.targetTheta = 0.55;
orbit.targetPhi = 1.08;
orbit.targetRadius =
window.innerWidth < 700 ? 20 : 15.8;
}
);
// ---------------------------------------------------------------------
// Resize and animation
// ---------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(
window.innerWidth,
window.innerHeight
);
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
});
function updateCamera() {
orbit.theta +=
(orbit.targetTheta - orbit.theta) * 0.12;
orbit.phi +=
(orbit.targetPhi - orbit.phi) * 0.12;
orbit.radius +=
(orbit.targetRadius - orbit.radius) * 0.12;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x +
orbit.radius * sinPhi * Math.cos(orbit.theta),
orbit.target.y +
orbit.radius * Math.cos(orbit.phi),
orbit.target.z +
orbit.radius * sinPhi * Math.sin(orbit.theta)
);
camera.lookAt(orbit.target);
}
function animate(now) {
if (travelAnimation) {
const progress = THREE.MathUtils.clamp(
(now - travelAnimation.start) /
travelAnimation.duration,
0,
1
);
const eased =
progress < 0.5
? 4 * progress * progress * progress
: 1 -
Math.pow(-2 * progress + 2, 3) / 2;
setTravel(
THREE.MathUtils.lerp(
travelAnimation.from,
travelAnimation.to,
eased
)
);
if (progress >= 1) {
travelAnimation = null;
}
}
updateCamera();
renderer.render(scene, camera);
}
setTravel(0);
updateCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```