Prompt specification
Build or customize the Three.js 3D scene "Adjustable lamp reach sequence" 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: Adjustable lamp reach sequence
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/adjustable-lamp-reach-sequence-x0194
Scene Description Prompt:
Animate a voxel desk lamp bending from shelf to page, with connected arms and a following light spot. Three.js from cdnjs; standalone HTML.
Working Three.js Starter (adjustable-lamp-reach-sequence-x0194):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Desk Lamp</title>
<style>
:root {
--ink: #e9f6f2;
--muted: #91aaa8;
--accent: #ffb44b;
--panel: rgba(7, 20, 25, 0.76);
--line: rgba(172, 222, 211, 0.18);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071419;
color: var(--ink);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.title {
position: absolute;
top: 24px;
left: 26px;
max-width: min(430px, calc(100vw - 52px));
}
.eyebrow {
margin-bottom: 9px;
color: var(--accent);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 4vw, 43px);
font-weight: 500;
line-height: 0.98;
letter-spacing: -0.055em;
}
.subtitle {
margin-top: 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.6;
letter-spacing: 0.05em;
}
.controls {
position: absolute;
right: 22px;
bottom: 22px;
width: min(310px, calc(100vw - 44px));
padding: 14px;
border: 1px solid var(--line);
border-radius: 3px;
background: var(--panel);
backdrop-filter: blur(12px);
pointer-events: auto;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 11px;
font-size: 10px;
letter-spacing: 0.09em;
}
.status-label {
color: var(--muted);
}
#status {
color: var(--accent);
text-align: right;
}
.track {
position: relative;
height: 3px;
overflow: hidden;
background: rgba(255, 255, 255, 0.09);
}
#progress {
position: absolute;
inset: 0 auto 0 0;
width: 0%;
background: var(--accent);
box-shadow: 0 0 12px rgba(255, 180, 75, 0.75);
}
.buttons {
display: flex;
gap: 8px;
margin-top: 13px;
}
button {
flex: 1;
border: 1px solid var(--line);
border-radius: 2px;
padding: 9px 10px;
background: rgba(255, 255, 255, 0.035);
color: var(--ink);
font: inherit;
font-size: 10px;
letter-spacing: 0.1em;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
color 160ms ease;
}
button:hover {
background: rgba(255, 180, 75, 0.1);
border-color: rgba(255, 180, 75, 0.55);
color: #ffd18a;
}
.help {
position: absolute;
bottom: 25px;
left: 26px;
color: var(--muted);
font-size: 9px;
line-height: 1.7;
letter-spacing: 0.12em;
}
.help b {
color: var(--ink);
font-weight: 500;
}
#loading {
position: fixed;
inset: 0;
z-index: 5;
display: grid;
place-items: center;
background: #071419;
color: var(--accent);
font-size: 11px;
letter-spacing: 0.18em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.title {
top: 18px;
left: 18px;
}
.subtitle {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
width: calc(100vw - 28px);
}
.help {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" tabindex="0" aria-label="Animated voxel desk lamp scene"></canvas>
<div class="hud">
<header class="title">
<div class="eyebrow">Articulated light study / 01</div>
<h1>Shelf to<br>page.</h1>
<div class="subtitle">
A two-bone voxel lamp unfolds across the desk while its
warm spotlight tracks the reading surface.
</div>
</header>
<div class="controls">
<div class="status-row">
<span class="status-label">LAMP STATE</span>
<span id="status" aria-live="polite">PARKED // SHELF</span>
</div>
<div class="track"><div id="progress"></div></div>
<div class="buttons">
<button id="pause" type="button">PAUSE</button>
<button id="replay" type="button">REPLAY</button>
</div>
</div>
<div class="help">
<b>DRAG</b> TO ORBIT · <b>WHEEL / PINCH</b> TO ZOOM<br>
<b>DOUBLE-CLICK</b> TO RESET CAMERA
</div>
</div>
<div id="loading">Building the desk…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Setup
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const statusElement = document.querySelector("#status");
const progressElement = document.querySelector("#progress");
const pauseButton = document.querySelector("#pause");
const replayButton = document.querySelector("#replay");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: 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;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071419);
scene.fog = new THREE.FogExp2(0x071419, 0.025);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
// -------------------------------------------------------------------------
// Materials and voxel construction helpers
// -------------------------------------------------------------------------
const material = (color, roughness = 0.78, metalness = 0.0) =>
new THREE.MeshStandardMaterial({
color,
roughness,
metalness,
flatShading: true
});
const M = {
wall: material(0x10252b),
wallInset: material(0x0b1c22),
desk: material(0x24454a),
deskEdge: material(0x173238),
line: material(0x31575a),
shelf: material(0x496c69),
shelfEdge: material(0x29494b),
cream: material(0xf0dfb7),
paper: material(0xfff0cf),
paperShade: material(0xd9c7a5),
ink: material(0x7b8278),
orange: material(0xffad42, 0.62),
orangeLight: material(0xffce70, 0.58),
orangeDark: material(0xb85c31),
navy: material(0x152d36, 0.64),
teal: material(0x55b7a7, 0.65),
red: material(0xe65e50),
blue: material(0x4e8dba),
dark: material(0x09171c),
bulb: new THREE.MeshStandardMaterial({
color: 0xffe3a0,
emissive: 0xffb23f,
emissiveIntensity: 4,
roughness: 0.25
})
};
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x092029,
transparent: true,
opacity: 0.38
});
function block(
width,
height,
depth,
mat,
parent = scene,
x = 0,
y = 0,
z = 0,
outlined = false
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
if (outlined) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
edgeMaterial
);
mesh.add(edges);
}
return mesh;
}
// -------------------------------------------------------------------------
// Environment
// -------------------------------------------------------------------------
block(12, 6.5, 0.3, M.wall, scene, 0, 2.4, -3.7);
block(11.7, 0.055, 0.06, M.wallInset, scene, 0, 0.35, -3.52);
block(11.7, 0.055, 0.06, M.wallInset, scene, 0, 2.65, -3.52);
for (let x = -5.4; x <= 5.4; x += 0.9) {
block(0.025, 5.8, 0.025, M.wallInset, scene, x, 2.5, -3.53);
}
// Desk.
block(11.7, 0.38, 7.1, M.desk, scene, 0, -0.22, 0, true);
block(11.75, 0.42, 0.18, M.deskEdge, scene, 0, -0.23, 3.49);
for (let x = -5.2; x <= 5.2; x += 0.8) {
block(0.018, 0.012, 6.95, M.line, scene, x, -0.024, 0);
}
// Shelf.
block(3.95, 0.22, 1.12, M.shelf, scene, -2.85, 1.5, -2.91, true);
block(3.95, 0.13, 0.13, M.shelfEdge, scene, -2.85, 1.34, -2.39);
block(0.18, 0.65, 0.7, M.shelfEdge, scene, -4.35, 1.12, -3.03);
block(0.18, 0.65, 0.7, M.shelfEdge, scene, -1.35, 1.12, -3.03);
// A few voxel books on the shelf.
block(0.28, 0.92, 0.68, M.red, scene, -4.25, 2.07, -2.92, true);
block(0.32, 0.75, 0.68, M.blue, scene, -3.93, 1.985, -2.92, true);
block(0.22, 1.05, 0.68, M.teal, scene, -3.58, 2.135, -2.92, true);
block(0.68, 0.17, 0.69, M.orangeDark, scene, -4.18, 2.61, -2.92, true);
// Open book/page on the desk.
const book = new THREE.Group();
book.position.set(1.25, 0, 0.65);
book.rotation.y = -0.11;
scene.add(book);
block(2.35, 0.09, 1.58, M.navy, book, 0, 0.035, 0, true);
block(1.07, 0.105, 1.43, M.paper, book, -0.555, 0.125, 0, true);
block(1.07, 0.105, 1.43, M.paper, book, 0.555, 0.125, 0, true);
block(0.055, 0.125, 1.43, M.paperShade, book, 0, 0.13, 0);
for (let i = 0; i < 7; i++) {
const width = i === 0 ? 0.72 : 0.76 - (i % 3) * 0.09;
block(width, 0.012, 0.025, M.ink, book, -0.56, 0.183, -0.48 + i * 0.15);
block(width * 0.86, 0.012, 0.025, M.ink, book, 0.56, 0.183, -0.48 + i * 0.15);
}
block(0.14, 0.025, 0.75, M.red, book, 0.93, 0.17, 0.47);
// Desk accessories.
const mug = new THREE.Group();
mug.position.set(-0.45, 0, 1.28);
scene.add(mug);
block(0.55, 0.62, 0.55, M.teal, mug, 0, 0.31, 0, true);
block(0.42, 0.025, 0.42, M.dark, mug, 0, 0.625, 0);
block(0.13, 0.38, 0.14, M.teal, mug, 0.36, 0.34, 0);
block(0.13, 0.13, 0.14, M.teal, mug, 0.43, 0.51, 0);
block(0.13, 0.13, 0.14, M.teal, mug, 0.43, 0.18, 0);
block(0.7, 0.52, 0.65, M.navy, scene, 3.25, 0.26, 0.25, true);
block(0.22, 0.9, 0.22, M.orange, scene, 3.12, 0.87, 0.25);
block(0.2, 0.82, 0.2, M.red, scene, 3.36, 0.83, 0.25);
block(0.18, 1.0, 0.18, M.cream, scene, 3.48, 0.91, 0.25);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0x9ec8cf, 0x17262a, 1.35));
const keyLight = new THREE.DirectionalLight(0xb8e3df, 2.4);
keyLight.position.set(4, 8, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0xffa34b, 1.0);
rimLight.position.set(-6, 4, -2);
scene.add(rimLight);
const spotTarget = new THREE.Object3D();
scene.add(spotTarget);
const spotLight = new THREE.SpotLight(
0xffcc75,
90,
15,
Math.PI / 7.4,
0.58,
1.35
);
spotLight.target = spotTarget;
spotLight.castShadow = true;
spotLight.shadow.mapSize.set(1024, 1024);
spotLight.shadow.camera.near = 0.12;
spotLight.shadow.camera.far = 15;
spotLight.shadow.bias = -0.0005;
scene.add(spotLight);
// -------------------------------------------------------------------------
// Voxel lamp base
// -------------------------------------------------------------------------
const SHOULDER = new THREE.Vector3(-3.0, 2.12, -2.62);
const lampBase = new THREE.Group();
lampBase.position.set(SHOULDER.x, 0, SHOULDER.z);
scene.add(lampBase);
block(0.95, 0.18, 0.78, M.navy, lampBase, 0, 1.69, 0, true);
block(0.72, 0.18, 0.61, M.orangeDark, lampBase, 0, 1.84, 0, true);
block(0.42, 0.39, 0.42, M.orange, lampBase, 0, 2.0, 0, true);
block(0.56, 0.22, 0.52, M.navy, lampBase, 0, 2.12, 0, true);
// -------------------------------------------------------------------------
// Connected voxel arms
// -------------------------------------------------------------------------
const L1 = 3.15;
const L2 = 3.25;
function makeArm(length, mainMaterial, accentMaterial) {
const group = new THREE.Group();
block(0.25, length, 0.27, mainMaterial, group, 0, 0, 0, true);
block(0.09, length * 0.87, 0.30, M.navy, group, 0, 0, 0);
for (let y = -length / 2 + 0.23; y < length / 2; y += 0.46) {
block(
0.37,
0.14,
0.37,
accentMaterial,
group,
0,
y,
0,
true
);
}
block(0.43, 0.24, 0.43, M.navy, group, 0, -length / 2, 0, true);
block(0.43, 0.24, 0.43, M.navy, group, 0, length / 2, 0, true);
return group;
}
const lowerArm = makeArm(L1, M.orange, M.orangeLight);
const upperArm = makeArm(L2, M.orangeDark, M.orange);
scene.add(lowerArm, upperArm);
const elbowJoint = block(0.61, 0.61, 0.61, M.navy, scene, 0, 0, 0, true);
const elbowCap = block(0.29, 0.66, 0.66, M.teal, scene, 0, 0, 0, true);
const wristJoint = block(0.49, 0.49, 0.49, M.navy, scene, 0, 0, 0, true);
// -------------------------------------------------------------------------
// Voxel lamp head. Local +Z points toward the light target.
// -------------------------------------------------------------------------
const lampHead = new THREE.Group();
scene.add(lampHead);
block(0.82, 0.69, 0.17, M.navy, lampHead, 0, 0, -0.23, true);
block(0.86, 0.16, 0.61, M.orange, lampHead, 0, 0.40, 0.01, true);
block(0.15, 0.66, 0.61, M.orange, lampHead, -0.355, 0.02, 0.01, true);
block(0.15, 0.66, 0.61, M.orange, lampHead, 0.355, 0.02, 0.01, true);
block(0.86, 0.12, 0.61, M.orangeLight, lampHead, 0, -0.34, 0.01, true);
block(0.48, 0.17, 0.035, M.bulb, lampHead, 0, -0.10, 0.325);
for (let x = -0.18; x <= 0.18; x += 0.18) {
block(0.035, 0.31, 0.045, M.navy, lampHead, x, -0.08, 0.35);
}
// -------------------------------------------------------------------------
// Light cone, traveling glow and projected pools
// -------------------------------------------------------------------------
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0xffc86c,
transparent: true,
opacity: 0.035,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
fog: false
});
const beam = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 1, 1, 24, 1, true),
beamMaterial
);
scene.add(beam);
function makeGlowTexture() {
const glowCanvas = document.createElement("canvas");
glowCanvas.width = 128;
glowCanvas.height = 128;
const context = glowCanvas.getContext("2d");
const gradient = context.createRadialGradient(
64,
64,
0,
64,
64,
64
);
gradient.addColorStop(0, "rgba(255,255,235,1)");
gradient.addColorStop(0.18, "rgba(255,220,130,0.9)");
gradient.addColorStop(0.5, "rgba(255,174,60,0.38)");
gradient.addColorStop(1, "rgba(255,150,40,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(glowCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const glowTexture = makeGlowTexture();
const travelGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: glowTexture,
color: 0xffd47e,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending,
fog: false
})
);
travelGlow.scale.set(0.7, 0.7, 0.7);
scene.add(travelGlow);
function makePool(position, size) {
const pool = new THREE.Mesh(
new THREE.CircleGeometry(1, 8),
new THREE.MeshBasicMaterial({
map: glowTexture,
color: 0xffc861,
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
fog: false
})
);
pool.rotation.x = -Math.PI / 2;
pool.position.copy(position);
pool.scale.set(size, size, size);
scene.add(pool);
return pool;
}
const START_LANDING = new THREE.Vector3(-1.95, 1.622, -2.53);
const END_LANDING = new THREE.Vector3(1.25, 0.184, 0.65);
const shelfPool = makePool(START_LANDING, 0.78);
const pagePool = makePool(END_LANDING, 0.88);
// -------------------------------------------------------------------------
// Two-bone IK
// -------------------------------------------------------------------------
const yAxis = new THREE.Vector3(0, 1, 0);
const zAxis = new THREE.Vector3(0, 0, 1);
function smooth01(value) {
const t = THREE.MathUtils.clamp(value, 0, 1);
return t * t * (3 - 2 * t);
}
function solveIK(root, end, lengthA, lengthB, bendPreference) {
const raw = end.clone().sub(root);
const distance = THREE.MathUtils.clamp(
raw.length(),
0.001,
lengthA + lengthB - 0.001
);
const axis = raw.normalize();
const along =
(lengthA * lengthA - lengthB * lengthB + distance * distance) /
(2 * distance);
const height = Math.sqrt(
Math.max(0, lengthA * lengthA - along * along)
);
const bend = bendPreference.clone();
bend.addScaledVector(axis, -bend.dot(axis));
if (bend.lengthSq() < 0.0001) {
bend.set(0, 0, 1);
bend.addScaledVector(axis, -bend.dot(axis));
}
bend.normalize();
const elbow = root
.clone()
.addScaledVector(axis, along)
.addScaledVector(bend, height);
return elbow;
}
function positionArm(arm, start, end) {
const direction = end.clone().sub(start);
const length = direction.length();
arm.position.copy(start).add(end).multiplyScalar(0.5);
arm.quaternion.setFromUnitVectors(yAxis, direction.normalize());
// The arm geometry is built at its exact IK length.
arm.scale.y = length / arm.userData.baseLength;
}
lowerArm.userData.baseLength = L1;
upperArm.userData.baseLength = L2;
// -------------------------------------------------------------------------
// Animation timeline
// -------------------------------------------------------------------------
const START_HEAD = new THREE.Vector3(-2.35, 3.15, -2.30);
const END_HEAD = new THREE.Vector3(1.25, 1.9, 0.65);
const CYCLE_DURATION = 9;
let elapsed = 0;
let paused = false;
function movementAt(cycleTime) {
if (cycleTime < 1.0) return 0;
if (cycleTime < 5.2) {
return smooth01((cycleTime - 1.0) / 4.2);
}
if (cycleTime < 6.8) return 1;
return 1 - smooth01((cycleTime - 6.8) / 2.2);
}
const headPosition = new THREE.Vector3();
const landingPosition = new THREE.Vector3();
const lightPosition = new THREE.Vector3();
const lightDirection = new THREE.Vector3();
const beamDirection = new THREE.Vector3();
function updateLamp(cycleTime) {
const u = movementAt(cycleTime);
// Head travels from a folded shelf position toward the book.
headPosition.lerpVectors(START_HEAD, END_HEAD, u);
headPosition.y += Math.sin(Math.PI * u) * 0.75;
headPosition.z += Math.sin(Math.PI * u) * 0.35;
// The spotlight endpoint follows the same motion, then settles on the page.
landingPosition.lerpVectors(START_LANDING, END_LANDING, u);
landingPosition.y += Math.sin(Math.PI * u) * 0.65;
landingPosition.z += Math.sin(Math.PI * u) * 0.20;
const bendPreference = new THREE.Vector3(-0.2, 1, 0.35).lerp(
new THREE.Vector3(0, 1, 0),
smooth01(u)
);
const elbow = solveIK(
SHOULDER,
headPosition,
L1,
L2,
bendPreference
);
positionArm(lowerArm, SHOULDER, elbow);
positionArm(upperArm, elbow, headPosition);
elbowJoint.position.copy(elbow);
elbowCap.position.copy(elbow);
elbowCap.rotation.z = Math.PI / 2;
wristJoint.position.copy(headPosition);
// Aim the head's local +Z axis at the moving light landing point.
lightDirection.copy(landingPosition).sub(headPosition).normalize();
lampHead.position.copy(headPosition);
lampHead.quaternion.setFromUnitVectors(zAxis, lightDirection);
// The actual point light sits just inside the shade opening.
lightPosition
.copy(headPosition)
.addScaledVector(lightDirection, 0.38);
spotLight.position.copy(lightPosition);
spotTarget.position.copy(landingPosition);
spotLight.intensity = 55 + u * 80;
// Update the visible translucent beam.
beamDirection.copy(lightPosition).sub(landingPosition);
const beamLength = beamDirection.length();
beam.position.copy(lightPosition).add(landingPosition).multiplyScalar(0.5);
beam.quaternion.setFromUnitVectors(yAxis, beamDirection.normalize());
beam.scale.set(0.68, beamLength, 0.68);
beamMaterial.opacity = 0.023 + u * 0.018;
// The small glow travels between surfaces during the bend.
travelGlow.position.copy(landingPosition);
travelGlow.material.opacity = Math.sin(Math.PI * u) * 0.95;
// Pools fade in on the actual shelf and page surfaces.
shelfPool.material.opacity = 0.9 * (1 - smooth01(u / 0.24));
pagePool.material.opacity = 0.95 * smooth01((u - 0.72) / 0.28);
if (cycleTime < 1.0) {
statusElement.textContent = "PARKED // SHELF";
} else if (cycleTime < 5.2) {
statusElement.textContent = "BENDING // SHELF TO PAGE";
} else if (cycleTime < 6.8) {
statusElement.textContent = "LOCKED // PAGE";
} else {
statusElement.textContent = "RETRACTING // TO SHELF";
}
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, wheel zoom and touch pinch
// -------------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(-0.15, 1.55, -0.35);
const desired = {
theta: 0.64,
phi: 1.10,
radius: 11.25
};
const current = {
theta: desired.theta,
phi: desired.phi,
radius: desired.radius
};
function resetCamera() {
desired.theta = 0.64;
desired.phi = 1.10;
desired.radius = 11.25;
}
function updateCamera(dt) {
const damping = 1 - Math.exp(-dt * 8);
current.theta += (desired.theta - current.theta) * damping;
current.phi += (desired.phi - current.phi) * damping;
current.radius += (desired.radius - current.radius) * damping;
const sinPhi = Math.sin(current.phi);
camera.position.set(
cameraTarget.x + current.radius * sinPhi * Math.sin(current.theta),
cameraTarget.y + current.radius * Math.cos(current.phi),
cameraTarget.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(cameraTarget);
}
const pointers = new Map();
let pinchDistance = 0;
function getPinchDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
pinchDistance = getPinchDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.38,
1.43
);
} else if (pointers.size === 2) {
const nextDistance = getPinchDistance();
if (pinchDistance > 0 && nextDistance > 0) {
desired.radius = THREE.MathUtils.clamp(
desired.radius * (pinchDistance / nextDistance),
6.8,
17
);
}
pinchDistance = nextDistance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
pinchDistance = getPinchDistance();
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
6.8,
17
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "PLAY" : "PAUSE";
});
replayButton.addEventListener("click", () => {
elapsed = 0;
paused = false;
pauseButton.textContent = "PAUSE";
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
let previousTime = performance.now();
function animate(now) {
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += dt;
}
const cycleTime = elapsed % CYCLE_DURATION;
updateLamp(cycleTime);
updateCamera(dt);
progressElement.style.width = `${(cycleTime / CYCLE_DURATION) * 100}%`;
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resize();
updateLamp(0);
updateCamera(1);
renderer.render(scene, camera);
document.querySelector("#loading")?.remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```