Prompt specification
Build or customize the Three.js 3D scene "Bucket-wheel toy section" 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: Bucket-wheel toy section
Category: Games & Play · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bucket-wheel-toy-section-x0393
Scene Description Prompt:
Please use Three.js from cdnjs for a stylized scene based on this idea: bucket-wheel toy section. Show a small wheel with eight shallow buckets under a spout and above a collection tray; the bucket openings must face the incoming water path. Return one self-contained HTML file.
Working Three.js Starter (bucket-wheel-toy-section-x0393):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Little Loop Lab — Bucket Wheel</title>
<style>
:root {
--ink: #173d46;
--muted: #547078;
--paper: rgba(255, 252, 244, 0.9);
--accent: #ef6258;
--water: #2ab7ca;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #f3eee4;
color: var(--ink);
font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
outline: none;
}
canvas.grabbing {
cursor: grabbing;
}
.title-card {
position: fixed;
top: 24px;
left: 24px;
width: min(330px, calc(100vw - 48px));
padding: 18px 20px 17px;
border: 1px solid rgba(23, 61, 70, 0.1);
border-radius: 20px;
background: var(--paper);
box-shadow: 0 16px 50px rgba(36, 68, 70, 0.12);
backdrop-filter: blur(12px);
user-select: none;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(239, 98, 88, 0.13);
}
h1 {
margin: 0;
font-size: clamp(24px, 4vw, 34px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 10px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.features {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 14px;
}
.feature {
padding: 6px 9px;
border-radius: 999px;
background: rgba(42, 183, 202, 0.1);
color: #197987;
font-size: 11px;
font-weight: 700;
}
.controls {
position: fixed;
right: 22px;
bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 12px;
user-select: none;
}
.hint {
padding: 9px 12px;
border: 1px solid rgba(23, 61, 70, 0.09);
border-radius: 999px;
background: rgba(255, 252, 244, 0.78);
box-shadow: 0 8px 24px rgba(36, 68, 70, 0.08);
backdrop-filter: blur(10px);
}
#reset {
border: 0;
border-radius: 50%;
width: 42px;
height: 42px;
background: var(--ink);
color: white;
font-size: 19px;
cursor: pointer;
box-shadow: 0 8px 22px rgba(23, 61, 70, 0.2);
transition: transform 160ms ease, background 160ms ease;
}
#reset:hover {
transform: rotate(-20deg) scale(1.04);
background: #245c66;
}
.badge {
position: fixed;
left: 24px;
bottom: 24px;
display: flex;
align-items: center;
gap: 9px;
padding: 10px 13px;
border-radius: 14px;
background: rgba(255, 252, 244, 0.82);
border: 1px solid rgba(23, 61, 70, 0.08);
color: var(--muted);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.05em;
text-transform: uppercase;
backdrop-filter: blur(10px);
user-select: none;
pointer-events: none;
}
.badge span {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--water);
box-shadow: 0 0 0 4px rgba(42, 183, 202, 0.13);
}
@media (max-width: 650px) {
.title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
border-radius: 17px;
}
h1 {
font-size: 25px;
}
.subtitle {
font-size: 12px;
}
.features,
.badge {
display: none;
}
.controls {
right: 14px;
bottom: 14px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<main id="scene" aria-label="Interactive 3D bucket wheel toy"></main>
<section class="title-card">
<div class="eyebrow">Cutaway Toy · Section 01</div>
<h1>Little Loop Lab</h1>
<p class="subtitle">
A top-fed bucket wheel with eight open scoops, a playful coral spout,
and a mint collection tray.
</p>
<div class="features">
<div class="feature">8 open buckets</div>
<div class="feature">Top-fed stream</div>
<div class="feature">Catch tray</div>
</div>
</section>
<div class="badge"><span></span> Water-flow model active</div>
<div class="controls">
<div class="hint">Drag to orbit · Scroll to zoom</div>
<button id="reset" type="button" title="Reset camera" aria-label="Reset camera">↻</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3eee4);
scene.fog = new THREE.Fog(0xf3eee4, 13, 24);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
50
);
const cameraState = {
target: new THREE.Vector3(0, 2.08, 0),
radius: 9.15,
theta: 0.56,
phi: 1.28
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.radius = 9.15;
cameraState.theta = 0.56;
cameraState.phi = 1.28;
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xfff8e8, 0x6f9694, 2.25);
scene.add(hemi);
const keyLight = new THREE.DirectionalLight(0xfff4dc, 3.4);
keyLight.position.set(4.5, 8.5, 6.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8edee3, 1.15);
fillLight.position.set(-5, 4, -4);
scene.add(fillLight);
const warmLight = new THREE.PointLight(0xff7e68, 12, 9, 2);
warmLight.position.set(-2.4, 4.6, 2.2);
scene.add(warmLight);
const palette = {
ink: 0x173d46,
teal: 0x187781,
tealLight: 0x4ca7a6,
coral: 0xef6258,
coralDark: 0xc94948,
yellow: 0xf5c75a,
cream: 0xfff2ce,
water: 0x2ab7ca,
waterLight: 0x81e5e7,
base: 0xe7d8bd,
tray: 0x64c6b6,
trayDark: 0x2f8f8b
};
function standard(color, roughness = 0.58, metalness = 0.0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
ink: standard(palette.ink, 0.5),
teal: standard(palette.teal, 0.48),
tealLight: standard(palette.tealLight, 0.52),
coral: standard(palette.coral, 0.48),
coralDark: standard(palette.coralDark, 0.6),
yellow: standard(palette.yellow, 0.5),
cream: standard(palette.cream, 0.62),
base: standard(palette.base, 0.78),
tray: standard(palette.tray, 0.48),
trayDark: standard(palette.trayDark, 0.62),
water: new THREE.MeshStandardMaterial({
color: palette.water,
roughness: 0.22,
transparent: true,
opacity: 0.72,
depthWrite: false
}),
waterLight: new THREE.MeshStandardMaterial({
color: palette.waterLight,
roughness: 0.18,
transparent: true,
opacity: 0.9,
depthWrite: false
})
};
function roundedBoxGeometry(width, height, depth, radius = 0.08) {
const r = Math.min(radius, width * 0.45, height * 0.45);
const shape = new THREE.Shape();
const x = -width / 2;
const y = -height / 2;
shape.moveTo(x + r, y);
shape.lineTo(x + width - r, y);
shape.quadraticCurveTo(x + width, y, x + width, y + r);
shape.lineTo(x + width, y + height - r);
shape.quadraticCurveTo(x + width, y + height, x + width - r, y + height);
shape.lineTo(x + r, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - r);
shape.lineTo(x, y + r);
shape.quadraticCurveTo(x, y, x + r, y);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
steps: 1,
bevelEnabled: true,
bevelSegments: 3,
bevelSize: Math.min(r * 0.2, depth * 0.18),
bevelThickness: Math.min(r * 0.18, depth * 0.18),
curveSegments: 8
});
geometry.translate(0, 0, -depth / 2);
geometry.computeVertexNormals();
return geometry;
}
function horizontalRoundedBox(width, depth, thickness, radius = 0.12) {
const geometry = roundedBoxGeometry(width, depth, thickness, radius);
geometry.rotateX(-Math.PI / 2);
return geometry;
}
function addMesh(geometry, material, position, parent = scene) {
const mesh = new THREE.Mesh(geometry, material);
if (position) mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, material, parent = scene, topRadius = radius) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
topRadius,
radius,
length,
14,
1,
false
);
const mesh = addMesh(geometry, material, start.clone().add(end).multiplyScalar(0.5), parent);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
return mesh;
}
// Soft ground and toy display platform.
const ground = addMesh(
new THREE.PlaneGeometry(40, 40),
standard(0xf3eee4, 0.95),
new THREE.Vector3(0, -0.49, 0)
);
ground.rotation.x = -Math.PI / 2;
ground.castShadow = false;
addMesh(
horizontalRoundedBox(7.25, 4.95, 0.24, 0.3),
materials.base,
new THREE.Vector3(0, -0.36, 0.08)
);
const platformInset = addMesh(
horizontalRoundedBox(6.65, 4.35, 0.025, 0.25),
standard(0xefdfc4, 0.88),
new THREE.Vector3(0, -0.225, 0.08)
);
platformInset.castShadow = false;
const screwMaterial = standard(0x9a886d, 0.4, 0.25);
[
[-3.15, -2.05],
[3.15, -2.05],
[-3.15, 2.15],
[3.15, 2.15]
].forEach(([x, z]) => {
const screw = addMesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.025, 20),
screwMaterial,
new THREE.Vector3(x, -0.195, z)
);
screw.castShadow = false;
});
// Rear support for the wheel axle.
const support = new THREE.Group();
scene.add(support);
addMesh(
roundedBoxGeometry(2.35, 0.18, 0.36, 0.08),
materials.ink,
new THREE.Vector3(0, -0.08, -1.02),
support
);
addMesh(
roundedBoxGeometry(0.2, 2.35, 0.24, 0.08),
materials.teal,
new THREE.Vector3(0, 1.02, -1.02),
support
);
cylinderBetween(
new THREE.Vector3(-1.02, -0.02, -1.02),
new THREE.Vector3(-0.06, 2.14, -1.02),
0.085,
materials.tealLight,
support
);
cylinderBetween(
new THREE.Vector3(1.02, -0.02, -1.02),
new THREE.Vector3(0.06, 2.14, -1.02),
0.085,
materials.tealLight,
support
);
// Open collection tray.
const tray = new THREE.Group();
scene.add(tray);
addMesh(
horizontalRoundedBox(3.28, 1.82, 0.16, 0.2),
materials.trayDark,
new THREE.Vector3(0, -0.12, 0.08),
tray
);
addMesh(
roundedBoxGeometry(3.28, 0.58, 0.13, 0.09),
materials.tray,
new THREE.Vector3(0, 0.13, 0.94),
tray
);
addMesh(
roundedBoxGeometry(3.28, 0.58, 0.13, 0.09),
materials.tray,
new THREE.Vector3(0, 0.13, -0.78),
tray
);
addMesh(
roundedBoxGeometry(0.13, 0.58, 1.82, 0.09),
materials.tray,
new THREE.Vector3(-1.575, 0.13, 0.08),
tray
);
addMesh(
roundedBoxGeometry(0.13, 0.58, 1.82, 0.09),
materials.tray,
new THREE.Vector3(1.575, 0.13, 0.08),
tray
);
const pool = addMesh(
horizontalRoundedBox(2.98, 1.48, 0.035, 0.16),
materials.water,
new THREE.Vector3(0, 0.025, 0.08),
tray
);
pool.castShadow = false;
const poolHighlights = [];
for (let i = 0; i < 3; i++) {
const ripple = addMesh(
new THREE.TorusGeometry(0.18 + i * 0.12, 0.012, 8, 40),
materials.waterLight,
new THREE.Vector3(-0.75 + i * 0.68, 0.048, 0.02 + i * 0.12),
tray
);
ripple.rotation.x = Math.PI / 2;
ripple.castShadow = false;
poolHighlights.push(ripple);
}
// Wheel assembly.
const wheel = new THREE.Group();
wheel.position.set(0, 2.25, 0);
scene.add(wheel);
addMesh(
new THREE.TorusGeometry(1.31, 0.13, 18, 84),
materials.yellow,
new THREE.Vector3(),
wheel
);
addMesh(
new THREE.TorusGeometry(0.68, 0.075, 14, 64),
materials.coral,
new THREE.Vector3(),
wheel
);
const spokeGeometry = new THREE.BoxGeometry(0.11, 1.03, 0.13);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2 + Math.PI / 2;
const spoke = addMesh(
spokeGeometry,
materials.coral,
new THREE.Vector3(
Math.cos(angle) * 0.64,
Math.sin(angle) * 0.64,
0
),
wheel
);
spoke.rotation.z = angle - Math.PI / 2;
}
const hub = addMesh(
new THREE.CylinderGeometry(0.31, 0.31, 0.9, 36),
materials.yellow,
new THREE.Vector3(),
wheel
);
hub.rotation.x = Math.PI / 2;
const hubCap = addMesh(
new THREE.CylinderGeometry(0.23, 0.23, 0.11, 32),
materials.cream,
new THREE.Vector3(0, 0, 0.49),
wheel
);
hubCap.rotation.x = Math.PI / 2;
// Eight shallow, open-top buckets. Local +Y is the opening direction.
const bucketRadius = 1.48;
const bucketDepth = 0.38;
const bucketWidth = 0.64;
const bucketZDepth = 0.57;
const buckets = [];
for (let i = 0; i < 8; i++) {
const angle = Math.PI / 2 + (i / 8) * Math.PI * 2;
const bucket = new THREE.Group();
bucket.position.set(
Math.cos(angle) * bucketRadius,
Math.sin(angle) * bucketRadius,
0
);
bucket.rotation.z = angle - Math.PI / 2;
wheel.add(bucket);
const bucketMaterial = i % 2 === 0 ? materials.coral : materials.coralDark;
addMesh(
new THREE.BoxGeometry(bucketWidth, 0.085, bucketZDepth),
bucketMaterial,
new THREE.Vector3(0, -bucketDepth, 0),
bucket
);
addMesh(
new THREE.BoxGeometry(0.075, bucketDepth, bucketZDepth),
bucketMaterial,
new THREE.Vector3(-bucketWidth / 2 + 0.0375, -bucketDepth / 2, 0),
bucket
);
addMesh(
new THREE.BoxGeometry(0.075, bucketDepth, bucketZDepth),
bucketMaterial,
new THREE.Vector3(bucketWidth / 2 - 0.0375, -bucketDepth / 2, 0),
bucket
);
// Cream lip pieces make the open mouth easy to read.
addMesh(
new THREE.BoxGeometry(0.09, 0.035, bucketZDepth + 0.025),
materials.cream,
new THREE.Vector3(-bucketWidth / 2 + 0.0375, 0.012, 0),
bucket
);
addMesh(
new THREE.BoxGeometry(0.09, 0.035, bucketZDepth + 0.025),
materials.cream,
new THREE.Vector3(bucketWidth / 2 - 0.0375, 0.012, 0),
bucket
);
buckets.push(bucket);
}
// A small pool of water visible in the upper receiving bucket.
const upperBucketWater = addMesh(
new THREE.BoxGeometry(0.49, 0.025, 0.45),
materials.waterLight,
new THREE.Vector3(0, -0.31, 0),
buckets[0]
);
upperBucketWater.castShadow = false;
// Front crank handle.
cylinderBetween(
new THREE.Vector3(0, 0, 0.83),
new THREE.Vector3(0.48, -0.28, 0.83),
0.075,
materials.ink,
wheel
);
const crankKnob = addMesh(
new THREE.CylinderGeometry(0.14, 0.14, 0.29, 24),
materials.coral,
new THREE.Vector3(0.48, -0.28, 0.96),
wheel
);
crankKnob.rotation.x = Math.PI / 2;
// Spout stand.
addMesh(
horizontalRoundedBox(0.92, 0.82, 0.15, 0.14),
materials.ink,
new THREE.Vector3(-2.35, -0.09, -0.45)
);
addMesh(
roundedBoxGeometry(0.22, 4.2, 0.22, 0.09),
materials.teal,
new THREE.Vector3(-2.35, 2.04, -0.45)
);
const spoutCurve = new THREE.CatmullRomCurve3([
new THREE.Vector3(-2.35, 3.93, -0.45),
new THREE.Vector3(-2.35, 4.48, -0.36),
new THREE.Vector3(-1.75, 4.78, -0.12),
new THREE.Vector3(-0.86, 4.72, 0),
new THREE.Vector3(-0.24, 4.48, 0),
new THREE.Vector3(0, 4.24, 0)
]);
addMesh(
new THREE.TubeGeometry(spoutCurve, 80, 0.145, 14, false),
materials.coral,
new THREE.Vector3(),
scene
);
const spoutCollar = addMesh(
new THREE.CylinderGeometry(0.23, 0.18, 0.24, 28),
materials.coralDark,
new THREE.Vector3(0, 4.11, 0)
);
spoutCollar.castShadow = true;
const mouth = addMesh(
new THREE.CylinderGeometry(0.125, 0.125, 0.018, 28),
materials.ink,
new THREE.Vector3(0, 3.985, 0)
);
mouth.castShadow = false;
// Main falling stream, aligned with the upper bucket opening.
const stream = addMesh(
new THREE.CylinderGeometry(0.075, 0.09, 0.27, 20),
materials.waterLight,
new THREE.Vector3(0, 3.855, 0)
);
stream.castShadow = false;
const streamDroplets = [];
for (let i = 0; i < 6; i++) {
const drop = addMesh(
new THREE.SphereGeometry(0.026, 12, 10),
materials.waterLight,
new THREE.Vector3(),
scene
);
drop.castShadow = false;
streamDroplets.push(drop);
}
// Splash around the upward-facing bucket mouth.
const splashDrops = [];
for (let i = 0; i < 9; i++) {
const drop = addMesh(
new THREE.SphereGeometry(0.025, 12, 10),
materials.waterLight,
new THREE.Vector3(),
scene
);
drop.castShadow = false;
splashDrops.push(drop);
}
// Drops leaving the lowest inverted bucket into the tray.
const trayDrops = [];
for (let i = 0; i < 5; i++) {
const drop = addMesh(
new THREE.SphereGeometry(0.032, 12, 10),
materials.water,
new THREE.Vector3(),
scene
);
drop.castShadow = false;
trayDrops.push(drop);
}
// Decorative toy blocks around the platform edge.
const blockA = addMesh(
roundedBoxGeometry(0.38, 0.38, 0.38, 0.09),
materials.yellow,
new THREE.Vector3(2.65, -0.015, 1.55)
);
blockA.rotation.y = 0.22;
const blockB = addMesh(
roundedBoxGeometry(0.32, 0.54, 0.32, 0.08),
materials.coral,
new THREE.Vector3(2.62, 0.26, 1.56)
);
blockB.rotation.y = -0.12;
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
// A restrained rocking motion keeps the receiving mouth aligned
// with the incoming vertical stream.
const desiredRotation = Math.sin(elapsed * 0.8) * 0.028;
wheel.rotation.z = THREE.MathUtils.lerp(
wheel.rotation.z,
desiredRotation,
0.055
);
stream.material.opacity = 0.72 + Math.sin(elapsed * 5) * 0.08;
streamDroplets.forEach((drop, i) => {
const phase = (elapsed * 1.45 + i / streamDroplets.length) % 1;
drop.position.set(
0.018 * Math.sin(i * 2.4 + elapsed * 3),
3.98 - phase * 0.25,
0.014 * Math.cos(i * 1.7 + elapsed * 2.6)
);
drop.scale.set(0.8, 1.25, 0.8);
});
splashDrops.forEach((drop, i) => {
const phase = (elapsed * 1.25 + i / splashDrops.length) % 1;
const angle = (i / splashDrops.length) * Math.PI * 2 + elapsed * 0.34;
const radius = 0.025 + phase * 0.13;
drop.position.set(
Math.cos(angle) * radius,
3.73 + Math.sin(phase * Math.PI) * 0.13 - phase * 0.035,
Math.sin(angle) * radius * 0.7
);
drop.scale.setScalar(0.75 + Math.sin(phase * Math.PI) * 0.45);
});
trayDrops.forEach((drop, i) => {
const phase = (elapsed * 0.72 + i / trayDrops.length) % 1;
drop.position.set(
0.035 * Math.sin(i * 2.1 + elapsed * 2),
0.76 - phase * 0.39,
0.028 * Math.cos(i * 1.4)
);
drop.scale.set(0.8, 1.2, 0.8);
});
poolHighlights.forEach((ripple, i) => {
const pulse = (elapsed * 0.38 + i * 0.31) % 1;
ripple.scale.setScalar(0.75 + pulse * 0.55);
ripple.material.opacity = 0.75 * (1 - pulse);
});
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
// Direct camera controls implemented without external controls modules.
const pointers = new Map();
let dragging = false;
let lastX = 0;
let lastY = 0;
let lastPinchDistance = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.classList.add("grabbing");
}
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
lastPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1 && dragging) {
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.72,
1.49
);
}
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (lastPinchDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (lastPinchDistance / distance),
6.1,
13.8
);
}
lastPinchDistance = distance;
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
if (pointers.size === 0) {
dragging = false;
canvas.classList.remove("grabbing");
} else if (pointers.size === 1) {
const remaining = [...pointers.values()][0];
lastX = remaining.x;
lastY = remaining.y;
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.006,
6.1,
13.8
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```