Prompt specification
Build or customize the Three.js 3D scene "Earthworm soil tunnel 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: Earthworm soil tunnel section
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/earthworm-soil-tunnel-section-x0673
Scene Description Prompt:
Model a soil block with one curved tunnel and segmented earthworm, showing roots and leaf litter above; keep the tunnel continuous between two visible openings. Use Three.js from cdnjs in a single HTML file, with stylized solid shapes.
Working Three.js Starter (earthworm-soil-tunnel-section-x0673):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Soil Tunnel Cutaway</title>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d8d1bf;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#title {
position: fixed;
top: 22px;
left: 24px;
z-index: 10;
padding: 12px 15px;
color: #4a3024;
background: rgba(255, 252, 240, 0.82);
border: 1px solid rgba(74, 48, 36, 0.18);
border-radius: 9px;
box-shadow: 0 6px 24px rgba(74, 48, 36, 0.12);
pointer-events: none;
backdrop-filter: blur(5px);
}
#title strong {
display: block;
font-size: 14px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#title span {
display: block;
margin-top: 4px;
color: #796252;
font-size: 12px;
}
#hint {
position: fixed;
right: 20px;
bottom: 18px;
z-index: 10;
color: #665143;
font-size: 12px;
letter-spacing: 0.04em;
padding: 8px 11px;
border-radius: 7px;
background: rgba(255, 252, 240, 0.72);
pointer-events: none;
}
</style>
</head>
<body>
<div id="title">
<strong>Soil Tunnel Cutaway</strong>
<span>Roots, leaf litter, and a continuous earthworm tunnel</span>
</div>
<div id="hint">Drag to orbit · Scroll 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";
// ------------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd8d1bf);
scene.fog = new THREE.Fog(0xd8d1bf, 26, 60);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, 0.45, 0);
const desired = {
theta: 0.46,
phi: 1.12,
radius: window.innerWidth < 700 ? 23 : 17.8
};
const current = { ...desired };
const hemi = new THREE.HemisphereLight(0xfff4dc, 0x604737, 1.8);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d5, 2.5);
sun.position.set(-7, 11, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -14;
sun.shadow.camera.right = 14;
sun.shadow.camera.top = 14;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 40;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaac4cf, 0.65);
fill.position.set(9, 3, -8);
scene.add(fill);
// ------------------------------------------------------------------
// Materials
// ------------------------------------------------------------------
const soilBackMat = new THREE.MeshStandardMaterial({
color: 0x68432f,
roughness: 1,
flatShading: true
});
const soilFaceMat = new THREE.MeshStandardMaterial({
color: 0x895a3c,
roughness: 1,
flatShading: true
});
const topSoilMat = new THREE.MeshStandardMaterial({
color: 0x59623c,
roughness: 1,
flatShading: true
});
const strataMat = new THREE.MeshStandardMaterial({
color: 0x9b6947,
roughness: 1
});
const tunnelRimMat = new THREE.MeshStandardMaterial({
color: 0x76503a,
roughness: 1
});
const tunnelVoidMat = new THREE.MeshStandardMaterial({
color: 0x251610,
roughness: 1,
side: THREE.DoubleSide
});
const rootMat = new THREE.MeshStandardMaterial({
color: 0xc4a06a,
roughness: 1,
flatShading: true
});
const rootTipMat = new THREE.MeshStandardMaterial({
color: 0xd0b27f,
roughness: 1,
flatShading: true
});
const stemMat = new THREE.MeshStandardMaterial({
color: 0x647044,
roughness: 1,
flatShading: true
});
const leafMaterials = [
new THREE.MeshStandardMaterial({
color: 0xb76d36,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xd39a48,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x7d8449,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x984c32,
roughness: 1,
flatShading: true
})
];
const wormMaterials = [
new THREE.MeshStandardMaterial({
color: 0xd8897b,
roughness: 0.86,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xc9786d,
roughness: 0.88,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xe09b8c,
roughness: 0.85,
flatShading: true
})
];
const wormRingMat = new THREE.MeshStandardMaterial({
color: 0x914d49,
roughness: 0.9
});
// ------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------
let seed = 732451;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(min, max) {
return min + random() * (max - min);
}
function setShadow(mesh, cast = true, receive = true) {
mesh.castShadow = cast;
mesh.receiveShadow = receive;
return mesh;
}
const yAxis = new THREE.Vector3(0, 1, 0);
const xAxis = new THREE.Vector3(1, 0, 0);
const zAxis = new THREE.Vector3(0, 0, 1);
function cylinderBetween(a, b, radius, material, radialSegments = 7) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius * 0.78,
radius,
length,
radialSegments
);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
return setShadow(mesh);
}
function makeTube(points, radius, material, segments = 24) {
const curve = new THREE.CatmullRomCurve3(points);
const geometry = new THREE.TubeGeometry(curve, segments, radius, 7, false);
return setShadow(new THREE.Mesh(geometry, material));
}
function makeLeafGeometry() {
const shape = new THREE.Shape();
shape.moveTo(0, -0.46);
shape.bezierCurveTo(0.32, -0.29, 0.35, 0.18, 0, 0.54);
shape.bezierCurveTo(-0.32, 0.22, -0.31, -0.25, 0, -0.46);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.055,
bevelEnabled: true,
bevelThickness: 0.018,
bevelSize: 0.025,
bevelSegments: 1,
curveSegments: 8
});
geometry.translate(0, 0, -0.0275);
geometry.rotateX(-Math.PI / 2);
geometry.computeVertexNormals();
return geometry;
}
const leafGeometry = makeLeafGeometry();
// ------------------------------------------------------------------
// Soil block
// ------------------------------------------------------------------
const soilBlock = new THREE.Group();
scene.add(soilBlock);
// Main soil mass behind the exposed section.
const soilBody = setShadow(
new THREE.Mesh(new THREE.BoxGeometry(12, 6, 3.8), soilBackMat)
);
soilBody.position.set(0, 0, -1.5);
soilBlock.add(soilBody);
// Front cutaway slab with two actual circular holes.
const leftOpening = new THREE.Vector2(-4.85, 1.55);
const rightOpening = new THREE.Vector2(4.85, 0.45);
const faceShape = new THREE.Shape();
faceShape.moveTo(-6, -3);
faceShape.lineTo(6, -3);
faceShape.lineTo(6, 3);
faceShape.lineTo(-6, 3);
faceShape.closePath();
for (const opening of [leftOpening, rightOpening]) {
const hole = new THREE.Path();
hole.absarc(opening.x, opening.y, 0.6, 0, Math.PI * 2, true);
faceShape.holes.push(hole);
}
const faceGeometry = new THREE.ExtrudeGeometry(faceShape, {
depth: 0.45,
bevelEnabled: false,
curveSegments: 40
});
const soilFace = setShadow(
new THREE.Mesh(faceGeometry, soilFaceMat)
);
soilFace.position.z = 0.4;
soilBlock.add(soilFace);
// Thin grassy/topsoil layer.
const topLayer = setShadow(
new THREE.Mesh(new THREE.BoxGeometry(12.25, 0.36, 4.65), topSoilMat)
);
topLayer.position.set(0, 3.12, -0.95);
soilBlock.add(topLayer);
// Visible horizontal strata on the right side.
for (const y of [-1.85, -0.35, 1.42]) {
const stripe = setShadow(
new THREE.Mesh(new THREE.BoxGeometry(0.035, 0.13, 3.55), strataMat)
);
stripe.position.set(6.012, y, -1.5);
soilBlock.add(stripe);
}
// Stylized soil aggregates on the exposed face.
const aggregateGeometry = new THREE.DodecahedronGeometry(1, 0);
const aggregateMaterials = [
new THREE.MeshStandardMaterial({
color: 0x946346,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0x795039,
roughness: 1,
flatShading: true
}),
new THREE.MeshStandardMaterial({
color: 0xa06f4d,
roughness: 1,
flatShading: true
})
];
for (let i = 0; i < 52; i++) {
const x = range(-5.75, 5.75);
const y = range(-2.75, 2.8);
const nearLeftOpening =
Math.hypot(x - leftOpening.x, y - leftOpening.y) < 0.85;
const nearRightOpening =
Math.hypot(x - rightOpening.x, y - rightOpening.y) < 0.85;
if (nearLeftOpening || nearRightOpening) continue;
const size = range(0.07, 0.2);
const aggregate = new THREE.Mesh(
aggregateGeometry,
aggregateMaterials[i % aggregateMaterials.length]
);
aggregate.position.set(x, y, 0.865);
aggregate.scale.set(size, size * range(0.65, 1.1), size * 0.36);
aggregate.rotation.set(random(), random(), random());
setShadow(aggregate, true, true);
soilBlock.add(aggregate);
}
// ------------------------------------------------------------------
// Recessed tunnel openings
// ------------------------------------------------------------------
function addOpening(position) {
const darkDisk = new THREE.Mesh(
new THREE.CircleGeometry(0.585, 32),
new THREE.MeshBasicMaterial({ color: 0x1d100c })
);
darkDisk.position.set(position.x, position.y, 0.405);
soilBlock.add(darkDisk);
const recessGeometry = new THREE.CylinderGeometry(
0.585,
0.585,
0.42,
32,
1,
true
);
recessGeometry.rotateX(Math.PI / 2);
const recess = new THREE.Mesh(recessGeometry, tunnelVoidMat);
recess.position.set(position.x, position.y, 0.62);
soilBlock.add(recess);
const rim = setShadow(
new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.085, 8, 36),
tunnelRimMat
)
);
rim.position.set(position.x, position.y, 0.87);
soilBlock.add(rim);
}
addOpening(leftOpening);
addOpening(rightOpening);
// ------------------------------------------------------------------
// One continuous curved tunnel
// ------------------------------------------------------------------
const tunnelPoints = [
new THREE.Vector3(-4.85, 1.55, 0.89),
new THREE.Vector3(-4.48, 1.12, 1.24),
new THREE.Vector3(-3.78, 0.66, 1.62),
new THREE.Vector3(-2.60, 0.62, 1.94),
new THREE.Vector3(-1.44, 1.20, 2.08),
new THREE.Vector3(-0.24, 1.42, 1.96),
new THREE.Vector3(0.76, 0.76, 1.80),
new THREE.Vector3(2.00, 0.16, 1.58),
new THREE.Vector3(3.10, 0.56, 1.30),
new THREE.Vector3(4.02, 0.76, 1.05),
new THREE.Vector3(4.85, 0.45, 0.89)
];
const tunnelCurve = new THREE.CatmullRomCurve3(
tunnelPoints,
false,
"centripetal",
0.45
);
// Build a flat, solid ribbon that reads as an open channel in the cut face.
function makeRibbon(curve, width, zOffset) {
const count = 120;
const positions = [];
const indices = [];
const side = new THREE.Vector3();
for (let i = 0; i < count; i++) {
const t = i / (count - 1);
const point = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
side.set(-tangent.y, tangent.x, 0);
if (side.lengthSq() < 0.0001) side.set(1, 0, 0);
side.normalize();
const left = point.clone().addScaledVector(side, width * 0.5);
const right = point.clone().addScaledVector(side, -width * 0.5);
left.z += zOffset;
right.z += zOffset;
positions.push(left.x, left.y, left.z);
positions.push(right.x, right.y, right.z);
if (i < count - 1) {
const a = i * 2;
indices.push(a, a + 1, a + 2);
indices.push(a + 1, a + 3, a + 2);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
const tunnelBorder = new THREE.Mesh(
makeRibbon(tunnelCurve, 1.18, -0.025),
new THREE.MeshStandardMaterial({
color: 0x744b35,
roughness: 1,
side: THREE.DoubleSide
})
);
setShadow(tunnelBorder, true, true);
soilBlock.add(tunnelBorder);
const tunnelInterior = new THREE.Mesh(
makeRibbon(tunnelCurve, 0.84, 0.018),
new THREE.MeshStandardMaterial({
color: 0x281710,
roughness: 1,
side: THREE.DoubleSide
})
);
setShadow(tunnelInterior, true, true);
soilBlock.add(tunnelInterior);
// ------------------------------------------------------------------
// Segmented earthworm following the tunnel
// ------------------------------------------------------------------
const wormGroup = new THREE.Group();
soilBlock.add(wormGroup);
const wormPoints = tunnelPoints.map((point) =>
new THREE.Vector3(point.x, point.y, point.z + 0.16)
);
const wormCurve = new THREE.CatmullRomCurve3(
wormPoints,
false,
"centripetal",
0.45
);
const segmentGeometry = new THREE.SphereGeometry(1, 16, 10);
const ringGeometry = new THREE.TorusGeometry(1, 0.11, 6, 16);
const segmentCount = 34;
for (let i = 0; i < segmentCount; i++) {
const t = 0.035 + (i / (segmentCount - 1)) * 0.93;
const point = wormCurve.getPointAt(t);
const tangent = wormCurve.getTangentAt(t).normalize();
const taper = Math.sin(Math.PI * t);
const radius = 0.165 + taper * 0.035;
const halfLength = 0.225 + taper * 0.055;
const segment = new THREE.Mesh(
segmentGeometry,
wormMaterials[i % wormMaterials.length]
);
segment.position.copy(point);
segment.quaternion.setFromUnitVectors(xAxis, tangent);
segment.scale.set(halfLength, radius, radius);
setShadow(segment, true, true);
wormGroup.add(segment);
const ring = new THREE.Mesh(ringGeometry, wormRingMat);
ring.position.copy(point).addScaledVector(tangent, -halfLength * 0.42);
ring.quaternion.setFromUnitVectors(zAxis, tangent);
ring.scale.setScalar(radius * 0.97);
setShadow(ring, true, true);
wormGroup.add(ring);
}
// Rounded head at the right-hand opening.
const headT = 0.975;
const headPoint = wormCurve.getPointAt(headT);
const headTangent = wormCurve.getTangentAt(headT).normalize();
const head = new THREE.Mesh(segmentGeometry, wormMaterials[2]);
head.position.copy(headPoint);
head.quaternion.setFromUnitVectors(xAxis, headTangent);
head.scale.set(0.29, 0.235, 0.205);
setShadow(head, true, true);
wormGroup.add(head);
const headSide = new THREE.Vector3(
-headTangent.y,
headTangent.x,
0
).normalize();
const eyeGeometry = new THREE.SphereGeometry(0.037, 10, 8);
const eyeMaterial = new THREE.MeshStandardMaterial({
color: 0x241b18,
roughness: 0.55
});
for (const sign of [-1, 1]) {
const eye = new THREE.Mesh(eyeGeometry, eyeMaterial);
eye.position
.copy(headPoint)
.addScaledVector(headSide, sign * 0.085)
.addScaledVector(headTangent, 0.09);
eye.position.z += 0.16;
wormGroup.add(eye);
}
// Slightly enlarged clitellum band.
const clitellumPoint = wormCurve.getPointAt(0.76);
const clitellumTangent = wormCurve.getTangentAt(0.76).normalize();
const clitellum = new THREE.Mesh(segmentGeometry, wormMaterials[1]);
clitellum.position.copy(clitellumPoint);
clitellum.quaternion.setFromUnitVectors(xAxis, clitellumTangent);
clitellum.scale.set(0.13, 0.235, 0.215);
setShadow(clitellum, true, true);
wormGroup.add(clitellum);
// ------------------------------------------------------------------
// Roots and small plants
// ------------------------------------------------------------------
function addRootBranch(origin, delta, radius, material) {
const middle = origin.clone().addScaledVector(delta, 0.52);
middle.x += Math.sign(delta.x || 1) * 0.12;
middle.y += 0.08;
const end = origin.clone().add(delta);
return makeTube(
[origin.clone(), middle, end],
radius,
material,
14
);
}
function addRootSystem(x, lean, scale) {
const points = [
new THREE.Vector3(x, 3.48, 0.96),
new THREE.Vector3(x + 0.06, 3.02, 0.92),
new THREE.Vector3(x + lean * 0.35, 2.45, 0.89),
new THREE.Vector3(x + lean * 0.72, 1.78, 0.88),
new THREE.Vector3(x + lean, 1.05, 0.88),
new THREE.Vector3(x + lean * 1.12, 0.18, 0.88)
];
const mainCurve = new THREE.CatmullRomCurve3(points);
const mainRoot = setShadow(
new THREE.Mesh(
new THREE.TubeGeometry(mainCurve, 34, 0.105 * scale, 7, false),
rootMat
)
);
soilBlock.add(mainRoot);
const branchData = [
[0.34, -0.72, -1.0],
[0.54, 0.62, -1.12],
[0.73, -0.48, -0.86],
[0.86, 0.38, -0.68]
];
for (const [t, dx, dy] of branchData) {
const origin = mainCurve.getPointAt(t);
const branch = addRootBranch(
origin,
new THREE.Vector3(dx * scale, dy * scale, -0.015),
0.052 * scale,
rootMat
);
soilBlock.add(branch);
}
// Exposed root curl above the soil surface.
const surfaceRoot = makeTube(
[
new THREE.Vector3(x, 3.33, 0.97),
new THREE.Vector3(x + 0.18, 3.48, 0.78),
new THREE.Vector3(x + 0.42, 3.39, 0.56),
new THREE.Vector3(x + 0.58, 3.32, 0.43)
],
0.065 * scale,
rootTipMat,
18
);
soilBlock.add(surfaceRoot);
}
addRootSystem(-3.35, 0.34, 1.0);
addRootSystem(-0.85, -0.28, 0.9);
addRootSystem(2.45, 0.30, 1.08);
addRootSystem(4.95, -0.22, 0.76);
function addPlant(x, z, height, leafCount = 5) {
const base = new THREE.Vector3(x, 3.31, z);
const top = new THREE.Vector3(
x + range(-0.12, 0.12),
3.31 + height,
z + range(-0.08, 0.08)
);
const stem = cylinderBetween(base, top, 0.045, stemMat, 6);
soilBlock.add(stem);
for (let i = 0; i < leafCount; i++) {
const t = 0.28 + (i / leafCount) * 0.68;
const attachment = base.clone().lerp(top, t);
const angle = i * 2.35 + range(-0.3, 0.3);
const leaf = new THREE.Mesh(
leafGeometry,
leafMaterials[i % leafMaterials.length]
);
leaf.position.copy(attachment);
leaf.position.x += Math.cos(angle) * 0.28;
leaf.position.z += Math.sin(angle) * 0.28;
leaf.rotation.y = -angle;
leaf.rotation.z = range(-0.45, 0.45);
leaf.scale.setScalar(range(0.55, 0.8));
setShadow(leaf, true, true);
soilBlock.add(leaf);
}
}
addPlant(-3.35, 0.78, 1.08, 5);
addPlant(-0.85, 0.84, 0.86, 4);
addPlant(2.45, 0.72, 1.20, 5);
// ------------------------------------------------------------------
// Leaf litter and twigs on top
// ------------------------------------------------------------------
for (let i = 0; i < 30; i++) {
const leaf = new THREE.Mesh(
leafGeometry,
leafMaterials[i % leafMaterials.length]
);
leaf.position.set(
range(-5.55, 5.55),
3.34 + range(-0.01, 0.025),
range(-2.9, 1.05)
);
leaf.rotation.y = range(0, Math.PI * 2);
leaf.rotation.z = range(-0.16, 0.16);
leaf.scale.set(
range(0.48, 0.84),
range(0.48, 0.84),
range(0.48, 0.84)
);
setShadow(leaf, true, true);
soilBlock.add(leaf);
}
for (let i = 0; i < 7; i++) {
const x = range(-5.2, 5.2);
const z = range(-2.6, 0.85);
const angle = range(0, Math.PI * 2);
const length = range(0.55, 1.15);
const a = new THREE.Vector3(x, 3.37, z);
const b = new THREE.Vector3(
x + Math.cos(angle) * length,
3.39,
z + Math.sin(angle) * length
);
const twig = cylinderBetween(a, b, range(0.025, 0.045), rootMat, 6);
soilBlock.add(twig);
}
// A few surface stones.
for (let i = 0; i < 12; i++) {
const stone = new THREE.Mesh(
new THREE.DodecahedronGeometry(1, 0),
aggregateMaterials[i % aggregateMaterials.length]
);
const size = range(0.06, 0.15);
stone.position.set(
range(-5.7, 5.7),
3.35,
range(-2.9, 1.0)
);
stone.scale.set(size, size * 0.55, size * 0.8);
stone.rotation.set(random(), random(), random());
setShadow(stone, true, true);
soilBlock.add(stone);
}
// ------------------------------------------------------------------
// Ground and presentation
// ------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(200, 200),
new THREE.MeshStandardMaterial({
color: 0xcfc7b3,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -3.28;
ground.receiveShadow = true;
scene.add(ground);
// ------------------------------------------------------------------
// Direct camera controls: orbit and zoom without addons
// ------------------------------------------------------------------
const canvas = renderer.domElement;
let dragging = false;
let lastX = 0;
let lastY = 0;
function resetCamera() {
desired.theta = 0.46;
desired.phi = 1.12;
desired.radius = window.innerWidth < 700 ? 23 : 17.8;
}
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
lastX = event.clientX;
lastY = event.clientY;
desired.theta -= dx * 0.006;
desired.phi = THREE.MathUtils.clamp(
desired.phi - dy * 0.005,
0.38,
1.45
);
});
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();
desired.radius = THREE.MathUtils.clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
10,
30
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", 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));
});
// ------------------------------------------------------------------
// Animation loop
// ------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const delta = clock.getDelta();
const smoothing = 1 - Math.exp(-10 * delta);
current.theta += (desired.theta - current.theta) * smoothing;
current.phi += (desired.phi - current.phi) * smoothing;
current.radius += (desired.radius - current.radius) * smoothing;
const sinPhi = Math.sin(current.phi);
camera.position.set(
target.x + current.radius * sinPhi * Math.sin(current.theta),
target.y + current.radius * Math.cos(current.phi),
target.z + current.radius * sinPhi * Math.cos(current.theta)
);
camera.lookAt(target);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```