Prompt specification
Build or customize the Three.js 3D scene "Root cap and gravity direction" 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: Root cap and gravity direction
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/root-cap-and-gravity-direction-x0769
Scene Description Prompt:
Make an enlarged stylized root tip with a visible root cap, soil grains, and downward gravity arrow. One HTML file using Three.js from cdnjs.
Working Three.js Starter (root-cap-and-gravity-direction-x0769):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Enlarged Root Tip</title>
<style>
:root {
color-scheme: dark;
--cream: #f0dfb5;
--orange: #f0a33b;
--teal: #55e6d0;
--muted: #b9b6aa;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 43%, #493625 0%, #30291f 30%, #1b211d 70%, #111714 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
max-width: 440px;
pointer-events: none;
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.55);
}
.eyebrow {
margin-bottom: 9px;
color: var(--teal);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.23em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: #fff6dc;
font-size: clamp(28px, 4vw, 52px);
font-weight: 750;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 13px 0 0;
max-width: 390px;
color: #d0cbbc;
font-size: 14px;
line-height: 1.55;
}
.legend {
position: fixed;
right: 28px;
top: 28px;
z-index: 5;
display: grid;
gap: 9px;
padding: 14px 16px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 14px;
background: rgba(16, 20, 17, 0.55);
box-shadow: 0 14px 40px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
color: #d8d5ca;
font-size: 11px;
letter-spacing: 0.04em;
}
.swatch {
width: 10px;
height: 10px;
border-radius: 50%;
box-shadow: 0 0 12px currentColor;
}
.swatch.cap {
color: var(--orange);
background: var(--orange);
}
.swatch.root {
color: var(--cream);
background: var(--cream);
}
.swatch.gravity {
color: var(--teal);
background: var(--teal);
}
.tag {
position: fixed;
z-index: 4;
min-width: 110px;
padding: 8px 11px 8px 13px;
border-left: 3px solid var(--tag-color);
border-radius: 5px;
background: rgba(14, 18, 15, 0.72);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(8px);
transform: translate(-50%, -50%);
pointer-events: none;
transition: opacity 120ms ease;
white-space: nowrap;
}
.tag::before {
content: "";
position: absolute;
top: 50%;
right: 100%;
width: 24px;
height: 1px;
background: var(--tag-color);
opacity: 0.75;
}
.tag strong {
display: block;
color: var(--tag-color);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
}
.tag small {
display: block;
margin-top: 3px;
color: #c9c7bf;
font-size: 10px;
}
#cap-tag {
--tag-color: var(--orange);
}
#root-tag {
--tag-color: var(--cream);
}
#gravity-tag {
--tag-color: var(--teal);
}
.help {
position: fixed;
left: 50%;
bottom: 22px;
z-index: 5;
padding: 9px 14px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 999px;
background: rgba(15, 19, 16, 0.56);
color: #c8c7bd;
font-size: 11px;
letter-spacing: 0.03em;
transform: translateX(-50%);
backdrop-filter: blur(10px);
pointer-events: none;
white-space: nowrap;
}
@media (max-width: 700px) {
.header {
top: 20px;
left: 20px;
right: 20px;
}
.subtitle {
max-width: 270px;
font-size: 12px;
}
.legend {
top: auto;
right: 16px;
bottom: 62px;
padding: 10px 12px;
}
.legend-row {
font-size: 9px;
}
.tag {
min-width: auto;
padding: 6px 8px;
}
.tag small {
display: none;
}
.help {
bottom: 14px;
font-size: 10px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header class="header">
<div class="eyebrow">Botanical Cutaway · Enlarged View</div>
<h1>Root Tip</h1>
<p class="subtitle">
A stylized root grows downward through granular soil. The amber root cap
protects the delicate growing region as gravity guides the root.
</p>
</header>
<aside class="legend" aria-label="Scene legend">
<div class="legend-row">
<span class="swatch cap"></span>
Protective root cap
</div>
<div class="legend-row">
<span class="swatch root"></span>
Root body
</div>
<div class="legend-row">
<span class="swatch gravity"></span>
Downward gravity
</div>
</aside>
<div id="cap-tag" class="tag">
<strong>ROOT CAP</strong>
<small>protective cell layer</small>
</div>
<div id="root-tag" class="tag">
<strong>ROOT BODY</strong>
<small>growing tissue</small>
</div>
<div id="gravity-tag" class="tag">
<strong>GRAVITY</strong>
<small>downward direction</small>
</div>
<div class="help">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 viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x181d18, 0.033);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ---------------------------------------------------------------------
// Lighting
// ---------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xdde9df, 0x49301d, 2.1));
const keyLight = new THREE.DirectionalLight(0xffe0a3, 3.2);
keyLight.position.set(-4, 7, 6);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 22;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x9ccfd0, 1.55);
fillLight.position.set(6, 1, 4);
scene.add(fillLight);
const rimLight = new THREE.PointLight(0xff8b42, 15, 9, 2);
rimLight.position.set(-1.7, -2.2, 2.5);
scene.add(rimLight);
const capLight = new THREE.PointLight(0xffbd6b, 7, 5, 2);
capLight.position.set(0.4, -2.2, 2);
scene.add(capLight);
// ---------------------------------------------------------------------
// Reproducible random helper
// ---------------------------------------------------------------------
let randomSeed = 84173;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function randomBetween(min, max) {
return min + (max - min) * random();
}
// ---------------------------------------------------------------------
// Soil floor
// ---------------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x302219,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.16;
floor.receiveShadow = true;
scene.add(floor);
// Large, dim soil masses provide depth behind the cutaway.
const backdropRockGeometry = new THREE.IcosahedronGeometry(1, 1);
for (let i = 0; i < 13; i++) {
const rockMaterial = new THREE.MeshStandardMaterial({
color: new THREE.Color().setHSL(
randomBetween(0.065, 0.095),
randomBetween(0.25, 0.42),
randomBetween(0.12, 0.21)
),
roughness: 1,
flatShading: true
});
const rock = new THREE.Mesh(backdropRockGeometry, rockMaterial);
const size = randomBetween(0.42, 1.05);
rock.position.set(
randomBetween(-6.3, 6.3),
randomBetween(-3.0, 4.1),
randomBetween(-4.5, -3.0)
);
rock.scale.set(
size * randomBetween(0.75, 1.35),
size * randomBetween(0.6, 1.1),
size * randomBetween(0.55, 0.9)
);
rock.rotation.set(
randomBetween(0, Math.PI),
randomBetween(0, Math.PI),
randomBetween(0, Math.PI)
);
scene.add(rock);
}
// ---------------------------------------------------------------------
// Instanced soil grains
// ---------------------------------------------------------------------
const grainCount = 155;
const grainGeometry = new THREE.IcosahedronGeometry(1, 1);
const grainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
metalness: 0,
flatShading: true
});
const grains = new THREE.InstancedMesh(
grainGeometry,
grainMaterial,
grainCount
);
grains.castShadow = true;
grains.receiveShadow = true;
const dummy = new THREE.Object3D();
const grainColor = new THREE.Color();
for (let i = 0; i < grainCount; i++) {
let x = randomBetween(-4.8, 4.8);
const y = randomBetween(-3.05, 4.35);
const z = randomBetween(-2.7, 1.45);
// Keep the central root readable while retaining grains in the soil.
if (Math.abs(x) < 1.12 && y < 4.5) {
x = (x < 0 ? -1 : 1) * randomBetween(1.18, 2.4);
}
const size = randomBetween(0.045, 0.19);
dummy.position.set(x, y, z);
dummy.rotation.set(
randomBetween(0, Math.PI),
randomBetween(0, Math.PI),
randomBetween(0, Math.PI)
);
dummy.scale.set(
size * randomBetween(0.7, 1.35),
size * randomBetween(0.55, 1.05),
size * randomBetween(0.6, 1.25)
);
dummy.updateMatrix();
grains.setMatrixAt(i, dummy.matrix);
grainColor.setHSL(
randomBetween(0.065, 0.105),
randomBetween(0.32, 0.58),
randomBetween(0.19, 0.38)
);
grains.setColorAt(i, grainColor);
}
grains.instanceMatrix.needsUpdate = true;
grains.instanceColor.needsUpdate = true;
grains.computeBoundingSphere();
scene.add(grains);
// Fine suspended soil dust.
const dustCount = 520;
const dustPositions = new Float32Array(dustCount * 3);
for (let i = 0; i < dustCount; i++) {
dustPositions[i * 3] = randomBetween(-5.2, 5.2);
dustPositions[i * 3 + 1] = randomBetween(-3.1, 4.5);
dustPositions[i * 3 + 2] = randomBetween(-2.6, 1.6);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xc9aa79,
size: 0.022,
transparent: true,
opacity: 0.38,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(dust);
// ---------------------------------------------------------------------
// Root body
// ---------------------------------------------------------------------
const rootGroup = new THREE.Group();
scene.add(rootGroup);
// Profile is ordered from bottom to top for the LatheGeometry.
const rootProfile = [
new THREE.Vector2(0.28, -1.66),
new THREE.Vector2(0.31, -1.25),
new THREE.Vector2(0.38, -0.60),
new THREE.Vector2(0.45, 0.20),
new THREE.Vector2(0.53, 1.00),
new THREE.Vector2(0.62, 1.90),
new THREE.Vector2(0.72, 2.80),
new THREE.Vector2(0.80, 3.70),
new THREE.Vector2(0.87, 4.65)
];
const rootGeometry = new THREE.LatheGeometry(rootProfile, 72);
// Add subtle color variation to the root tissue.
const rootPositions = rootGeometry.attributes.position;
const rootColors = new Float32Array(rootPositions.count * 3);
const lowerRootColor = new THREE.Color(0xb99458);
const upperRootColor = new THREE.Color(0xf0dfb5);
const sampledColor = new THREE.Color();
for (let i = 0; i < rootPositions.count; i++) {
const y = rootPositions.getY(i);
const t = THREE.MathUtils.clamp((y + 1.66) / 6.31, 0, 1);
sampledColor.lerpColors(lowerRootColor, upperRootColor, t);
sampledColor.multiplyScalar(randomBetween(0.91, 1.06));
rootColors[i * 3] = sampledColor.r;
rootColors[i * 3 + 1] = sampledColor.g;
rootColors[i * 3 + 2] = sampledColor.b;
}
rootGeometry.setAttribute(
"color",
new THREE.BufferAttribute(rootColors, 3)
);
rootGeometry.computeVertexNormals();
const rootOutline = new THREE.Mesh(
rootGeometry,
new THREE.MeshBasicMaterial({
color: 0x46321c,
side: THREE.BackSide,
transparent: true,
opacity: 0.55
})
);
rootOutline.scale.set(1.024, 1.004, 1.024);
rootGroup.add(rootOutline);
const rootMesh = new THREE.Mesh(
rootGeometry,
new THREE.MeshStandardMaterial({
color: 0xffffff,
vertexColors: true,
roughness: 0.73,
metalness: 0
})
);
rootMesh.castShadow = true;
rootMesh.receiveShadow = true;
rootGroup.add(rootMesh);
// Radius lookup for placing grooves and cell details.
function rootRadiusAt(y) {
const radii = [
[-1.66, 0.28],
[-1.25, 0.31],
[-0.60, 0.38],
[0.20, 0.45],
[1.00, 0.53],
[1.90, 0.62],
[2.80, 0.72],
[3.70, 0.80],
[4.65, 0.87]
];
if (y <= radii[0][0]) return radii[0][1];
if (y >= radii[radii.length - 1][0]) {
return radii[radii.length - 1][1];
}
for (let i = 0; i < radii.length - 1; i++) {
const [y0, r0] = radii[i];
const [y1, r1] = radii[i + 1];
if (y >= y0 && y <= y1) {
const t = (y - y0) / (y1 - y0);
return THREE.MathUtils.lerp(r0, r1, t);
}
}
return 0.5;
}
// Longitudinal grooves emphasize the root's longitudinal tissue.
const grooveMaterial = new THREE.MeshStandardMaterial({
color: 0x8b6237,
roughness: 0.9,
transparent: true,
opacity: 0.58,
depthWrite: false
});
const grooveYs = [-1.52, -1.12, -0.45, 0.35, 1.2, 2.1, 3.0, 3.9, 4.5];
for (let g = 0; g < 14; g++) {
const angle = (g / 14) * Math.PI * 2;
const points = grooveYs.map((y) => {
const radius = rootRadiusAt(y) + 0.008;
return new THREE.Vector3(
Math.sin(angle) * radius,
y,
Math.cos(angle) * radius
);
});
const curve = new THREE.CatmullRomCurve3(points);
const groove = new THREE.Mesh(
new THREE.TubeGeometry(curve, 52, 0.009, 5, false),
grooveMaterial
);
rootGroup.add(groove);
}
// Soft oval cell markings across the root surface.
const cellCount = 62;
const cellMesh = new THREE.InstancedMesh(
new THREE.SphereGeometry(1, 12, 8),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88
}),
cellCount
);
for (let i = 0; i < cellCount; i++) {
const y = randomBetween(-1.45, 3.85);
const angle = randomBetween(0, Math.PI * 2);
const radius = rootRadiusAt(y) + 0.006;
const size = randomBetween(0.035, 0.062);
dummy.position.set(
Math.sin(angle) * radius,
y,
Math.cos(angle) * radius
);
dummy.rotation.set(0, angle, 0);
dummy.scale.set(size, size * randomBetween(1.35, 1.9), size * 0.28);
dummy.updateMatrix();
cellMesh.setMatrixAt(i, dummy.matrix);
sampledColor.setHSL(
randomBetween(0.105, 0.14),
randomBetween(0.25, 0.42),
randomBetween(0.43, 0.63)
);
cellMesh.setColorAt(i, sampledColor);
}
cellMesh.instanceMatrix.needsUpdate = true;
cellMesh.instanceColor.needsUpdate = true;
cellMesh.computeBoundingSphere();
rootGroup.add(cellMesh);
// ---------------------------------------------------------------------
// Distinct amber root cap
// ---------------------------------------------------------------------
const capGroup = new THREE.Group();
rootGroup.add(capGroup);
const capProfile = [
new THREE.Vector2(0.00, -2.88),
new THREE.Vector2(0.08, -2.82),
new THREE.Vector2(0.20, -2.64),
new THREE.Vector2(0.31, -2.38),
new THREE.Vector2(0.38, -2.08),
new THREE.Vector2(0.40, -1.82),
new THREE.Vector2(0.36, -1.58),
new THREE.Vector2(0.30, -1.38)
];
const capGeometry = new THREE.LatheGeometry(capProfile, 72);
const capOutline = new THREE.Mesh(
capGeometry,
new THREE.MeshBasicMaterial({
color: 0x5a2d12,
side: THREE.BackSide,
transparent: true,
opacity: 0.72
})
);
capOutline.scale.set(1.035, 1.004, 1.035);
capGroup.add(capOutline);
const capMesh = new THREE.Mesh(
capGeometry,
new THREE.MeshStandardMaterial({
color: 0xd77924,
emissive: 0x431604,
emissiveIntensity: 0.24,
roughness: 0.48,
metalness: 0
})
);
capMesh.castShadow = true;
capMesh.receiveShadow = true;
capGroup.add(capMesh);
// A darker seam clearly separates the cap from the root body.
const capSeam = new THREE.Mesh(
new THREE.TorusGeometry(0.325, 0.025, 8, 56),
new THREE.MeshStandardMaterial({
color: 0x7d3a12,
roughness: 0.75
})
);
capSeam.rotation.x = Math.PI / 2;
capSeam.position.y = -1.55;
capSeam.castShadow = true;
capGroup.add(capSeam);
function capRadiusAt(y) {
const radii = [
[-2.88, 0.00],
[-2.82, 0.08],
[-2.64, 0.20],
[-2.38, 0.31],
[-2.08, 0.38],
[-1.82, 0.40],
[-1.58, 0.36],
[-1.38, 0.30]
];
if (y <= radii[0][0]) return radii[0][1];
if (y >= radii[radii.length - 1][0]) {
return radii[radii.length - 1][1];
}
for (let i = 0; i < radii.length - 1; i++) {
const [y0, r0] = radii[i];
const [y1, r1] = radii[i + 1];
if (y >= y0 && y <= y1) {
const t = (y - y0) / (y1 - y0);
return THREE.MathUtils.lerp(r0, r1, t);
}
}
return 0.3;
}
// Rounded cells give the root cap a distinct, tactile appearance.
const capCellCount = 52;
const capCellMesh = new THREE.InstancedMesh(
new THREE.SphereGeometry(1, 12, 8),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.58
}),
capCellCount
);
for (let i = 0; i < capCellCount; i++) {
const y = randomBetween(-2.76, -1.48);
const angle = randomBetween(0, Math.PI * 2);
const radius = capRadiusAt(y) + 0.004;
const size = randomBetween(0.035, 0.058);
dummy.position.set(
Math.sin(angle) * radius,
y,
Math.cos(angle) * radius
);
dummy.rotation.set(0, angle, 0);
dummy.scale.set(size, size * 1.12, size * 0.3);
dummy.updateMatrix();
capCellMesh.setMatrixAt(i, dummy.matrix);
sampledColor.setHSL(
randomBetween(0.075, 0.115),
randomBetween(0.62, 0.82),
randomBetween(0.43, 0.61)
);
capCellMesh.setColorAt(i, sampledColor);
}
capCellMesh.instanceMatrix.needsUpdate = true;
capCellMesh.instanceColor.needsUpdate = true;
capCellMesh.computeBoundingSphere();
capGroup.add(capCellMesh);
// ---------------------------------------------------------------------
// Downward gravity arrow
// ---------------------------------------------------------------------
const arrowGroup = new THREE.Group();
arrowGroup.position.set(2.42, 0.12, 0.15);
scene.add(arrowGroup);
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x50dfca,
emissive: 0x0b8e81,
emissiveIntensity: 0.85,
roughness: 0.32,
metalness: 0.08
});
const arrowGlowMaterial = new THREE.MeshBasicMaterial({
color: 0x55e6d0,
transparent: true,
opacity: 0.11,
depthWrite: false
});
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 2.35, 18),
arrowMaterial
);
shaft.position.y = 0.35;
arrowGroup.add(shaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.25, 0.62, 24),
arrowMaterial
);
arrowHead.rotation.z = Math.PI;
arrowHead.position.y = -1.13;
arrowGroup.add(arrowHead);
const shaftGlow = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.16, 2.36, 16),
arrowGlowMaterial
);
shaftGlow.position.copy(shaft.position);
arrowGroup.add(shaftGlow);
const headGlow = new THREE.Mesh(
new THREE.ConeGeometry(0.34, 0.68, 20),
arrowGlowMaterial
);
headGlow.rotation.z = Math.PI;
headGlow.position.copy(arrowHead.position);
arrowGroup.add(headGlow);
// A small light travels downward inside the arrow.
const gravityPulse = new THREE.Mesh(
new THREE.SphereGeometry(0.057, 14, 10),
new THREE.MeshBasicMaterial({ color: 0xd9fff7 })
);
arrowGroup.add(gravityPulse);
// ---------------------------------------------------------------------
// Custom camera controls: orbit, zoom, keyboard, and reset
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.08, -0.15, 0);
const desiredSpherical = new THREE.Spherical(8.9, 1.46, 0.18);
const currentSpherical = desiredSpherical.clone();
function updateCamera() {
currentSpherical.theta +=
(desiredSpherical.theta - currentSpherical.theta) * 0.09;
currentSpherical.phi +=
(desiredSpherical.phi - currentSpherical.phi) * 0.09;
currentSpherical.radius +=
(desiredSpherical.radius - currentSpherical.radius) * 0.09;
camera.position.setFromSpherical(currentSpherical);
camera.position.add(cameraTarget);
camera.lookAt(cameraTarget);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
desiredSpherical.theta -= deltaX * 0.006;
desiredSpherical.phi = THREE.MathUtils.clamp(
desiredSpherical.phi - deltaY * 0.006,
0.62,
2.28
);
});
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();
desiredSpherical.radius = THREE.MathUtils.clamp(
desiredSpherical.radius * Math.exp(event.deltaY * 0.001),
6.2,
13.5
);
},
{ passive: false }
);
function resetCamera() {
desiredSpherical.set(8.9, 1.46, 0.18);
}
renderer.domElement.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") desiredSpherical.theta += 0.12;
if (event.key === "ArrowRight") desiredSpherical.theta -= 0.12;
if (event.key === "ArrowUp") {
desiredSpherical.phi = THREE.MathUtils.clamp(
desiredSpherical.phi - 0.08,
0.62,
2.28
);
}
if (event.key === "ArrowDown") {
desiredSpherical.phi = THREE.MathUtils.clamp(
desiredSpherical.phi + 0.08,
0.62,
2.28
);
}
if (event.key === "+" || event.key === "=") {
desiredSpherical.radius = Math.max(6.2, desiredSpherical.radius - 0.5);
}
if (event.key === "-") {
desiredSpherical.radius = Math.min(13.5, desiredSpherical.radius + 0.5);
}
if (event.key.toLowerCase() === "r") resetCamera();
});
// ---------------------------------------------------------------------
// Projected HTML annotations
// ---------------------------------------------------------------------
const capTag = document.getElementById("cap-tag");
const rootTag = document.getElementById("root-tag");
const gravityTag = document.getElementById("gravity-tag");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.opacity = visible ? "1" : "0";
element.style.left = `${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
}
// ---------------------------------------------------------------------
// Resize
// ---------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
// Keep the gravity arrow visible on narrow screens.
arrowGroup.position.x = aspect < 0.8 ? 1.48 : 2.42;
}
window.addEventListener("resize", resize);
resize();
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
function animate() {
const elapsed = clock.getElapsedTime();
updateCamera();
if (!reducedMotion) {
arrowGroup.position.y = 0.12 + Math.sin(elapsed * 1.5) * 0.035;
const travel = (elapsed * 0.52) % 1;
gravityPulse.position.y = THREE.MathUtils.lerp(1.25, -0.73, travel);
gravityPulse.material.opacity = 0.65 + Math.sin(travel * Math.PI) * 0.35;
dust.rotation.y = Math.sin(elapsed * 0.07) * 0.018;
dust.position.y = Math.sin(elapsed * 0.18) * 0.025;
}
placeLabel(capTag, new THREE.Vector3(0.53, -2.12, 0.37));
placeLabel(rootTag, new THREE.Vector3(-0.54, 0.72, 0.48));
placeLabel(
gravityTag,
new THREE.Vector3(
arrowGroup.position.x + 0.02,
arrowGroup.position.y + 1.65,
arrowGroup.position.z
)
);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```