Prompt specification
Build or customize the Three.js 3D scene "Onion ring cross-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: Onion ring cross-section
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/onion-ring-cross-section-x0125
Scene Description Prompt:
Build a halved onion with nested translucent fleshy layers and thin papery skin; separate one ring slightly so the continuous concentric structure becomes clear. Build this with Three.js from cdnjs as a stylized scene in one self-contained HTML file.
Working Three.js Starter (onion-ring-cross-section-x0125):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Halved Onion — Botanical Section</title>
<style>
:root {
color-scheme: dark;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 38%, #27463d 0%, #142a25 34%, #091512 78%),
#091512;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
color: #f3ead8;
}
.heading {
position: absolute;
top: clamp(24px, 5vw, 64px);
left: clamp(24px, 5vw, 72px);
max-width: 390px;
}
.eyebrow {
margin: 0 0 13px;
color: #d4b789;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.28em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(34px, 5vw, 66px);
font-weight: 400;
line-height: 0.94;
letter-spacing: -0.045em;
}
.description {
width: min(100%, 320px);
margin: 20px 0 0;
color: rgba(239, 235, 220, 0.62);
font-size: 12px;
line-height: 1.65;
}
.hint {
position: absolute;
right: clamp(22px, 4vw, 56px);
bottom: clamp(22px, 4vw, 44px);
padding: 10px 14px;
border: 1px solid rgba(229, 220, 199, 0.13);
border-radius: 999px;
background: rgba(7, 19, 16, 0.38);
color: rgba(239, 235, 220, 0.55);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
backdrop-filter: blur(8px);
transition: opacity 500ms ease;
}
.hint.hidden {
opacity: 0;
}
.caption {
position: absolute;
left: clamp(24px, 5vw, 72px);
bottom: clamp(24px, 4vw, 48px);
display: flex;
align-items: center;
gap: 12px;
color: rgba(239, 235, 220, 0.4);
font-family: Georgia, "Times New Roman", serif;
font-size: 12px;
font-style: italic;
}
.caption::before {
width: 34px;
height: 1px;
content: "";
background: rgba(212, 183, 137, 0.55);
}
@media (max-width: 640px) {
.description {
max-width: 245px;
}
.caption {
display: none;
}
}
</style>
</head>
<body>
<div class="overlay">
<header class="heading">
<p class="eyebrow">Allium cepa · Botanical study</p>
<h1>Layers<br>of an onion</h1>
<p class="description">
A translucent botanical section revealing the nested structure of the bulb,
with one fleshy ring gently drawn away from the core.
</p>
</header>
<div class="caption">Concentric anatomy / study no. 01</div>
<div class="hint" id="hint">Drag to orbit · Scroll to zoom</div>
</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,
alpha: true,
powerPreference: "high-performance"
});
renderer.setClearColor(0x000000, 0);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional botanical model of a halved onion"
);
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x10211d, 0.028);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
// ------------------------------------------------------------
// Procedural studio environment for subtle PBR reflections
// ------------------------------------------------------------
const pmremGenerator = new THREE.PMREMGenerator(renderer);
const environmentScene = new THREE.Scene();
environmentScene.background = new THREE.Color(0x263b34);
const environmentSphere = new THREE.Mesh(
new THREE.SphereGeometry(10, 32, 16),
new THREE.MeshBasicMaterial({
color: 0x263b34,
side: THREE.BackSide
})
);
environmentScene.add(environmentSphere);
const warmPanel = new THREE.Mesh(
new THREE.PlaneGeometry(8, 8),
new THREE.MeshBasicMaterial({
color: 0xffebc7,
side: THREE.DoubleSide
})
);
warmPanel.position.set(-1, 6, 1);
warmPanel.rotation.x = Math.PI / 2;
environmentScene.add(warmPanel);
const coolPanel = new THREE.Mesh(
new THREE.PlaneGeometry(6, 7),
new THREE.MeshBasicMaterial({
color: 0x8ac4b7,
side: THREE.DoubleSide
})
);
coolPanel.position.set(6, 1, 1);
coolPanel.rotation.y = -Math.PI / 2;
environmentScene.add(coolPanel);
const environmentTarget = pmremGenerator.fromScene(environmentScene, 0.04);
scene.environment = environmentTarget.texture;
pmremGenerator.dispose();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xffefd6, 0x18332c, 1.45);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffdfae, 3.4);
keyLight.position.set(-3.5, 5.5, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 15;
keyLight.shadow.camera.left = -4;
keyLight.shadow.camera.right = 4;
keyLight.shadow.camera.top = 4;
keyLight.shadow.camera.bottom = -4;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.PointLight(0x9ad7ca, 15, 12, 2);
fillLight.position.set(4, 1.8, 4.2);
scene.add(fillLight);
const rimLight = new THREE.SpotLight(
0xccefe3,
34,
13,
Math.PI / 5,
0.8,
1.7
);
rimLight.position.set(-4, 3.8, -3.5);
rimLight.target.position.set(0, 0.2, 0);
scene.add(rimLight);
scene.add(rimLight.target);
const lowWarmLight = new THREE.PointLight(0xffbd78, 7, 5, 2);
lowWarmLight.position.set(-1.5, -1.1, 2);
scene.add(lowWarmLight);
// ------------------------------------------------------------
// Ground and pedestal
// ------------------------------------------------------------
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1c18,
roughness: 0.96,
metalness: 0
});
const ground = new THREE.Mesh(
new THREE.CircleGeometry(22, 128),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.44;
ground.receiveShadow = true;
scene.add(ground);
const pedestalMaterial = new THREE.MeshStandardMaterial({
color: 0x193b33,
roughness: 0.76,
metalness: 0.06
});
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(1.66, 1.76, 0.16, 96),
pedestalMaterial
);
pedestal.position.y = -1.36;
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
const pedestalInset = new THREE.Mesh(
new THREE.TorusGeometry(1.47, 0.012, 8, 128),
new THREE.MeshBasicMaterial({
color: 0x66877b,
transparent: true,
opacity: 0.2
})
);
pedestalInset.rotation.x = Math.PI / 2;
pedestalInset.position.y = -1.272;
scene.add(pedestalInset);
// ------------------------------------------------------------
// Onion model
// ------------------------------------------------------------
const onion = new THREE.Group();
scene.add(onion);
const RX = 1.08;
const RY = 1.18;
const CENTER_Y = 0.02;
function seededRandom(seed) {
return function () {
seed |= 0;
seed = seed + 0x6d2b79f5 | 0;
let value = Math.imul(seed ^ seed >>> 15, 1 | seed);
value = value + Math.imul(value ^ value >>> 7, 61 | value) ^ value;
return ((value ^ value >>> 14) >>> 0) / 4294967296;
};
}
const random = seededRandom(73129);
// Procedural papery skin texture.
function createSkinTexture() {
const size = 512;
const canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
const context = canvas.getContext("2d");
const gradient = context.createLinearGradient(0, 0, 0, size);
gradient.addColorStop(0, "#704225");
gradient.addColorStop(0.25, "#b5773d");
gradient.addColorStop(0.58, "#d49a5a");
gradient.addColorStop(1, "#9b5e31");
context.fillStyle = gradient;
context.fillRect(0, 0, size, size);
for (let i = 0; i < 310; i++) {
const x = random() * size;
const width = 0.4 + random() * 2.4;
const alpha = 0.025 + random() * 0.09;
context.beginPath();
context.moveTo(x, 0);
context.bezierCurveTo(
x + (random() - 0.5) * 16,
size * 0.32,
x + (random() - 0.5) * 18,
size * 0.72,
x + (random() - 0.5) * 10,
size
);
context.strokeStyle =
random() > 0.48
? `rgba(255, 218, 157, ${alpha})`
: `rgba(70, 34, 18, ${alpha * 1.5})`;
context.lineWidth = width;
context.stroke();
}
for (let i = 0; i < 75; i++) {
const x = random() * size;
const y = random() * size;
const radiusX = 5 + random() * 24;
const radiusY = 2 + random() * 10;
context.save();
context.translate(x, y);
context.rotate((random() - 0.5) * 0.4);
context.scale(1, 0.35 + random() * 0.35);
context.beginPath();
context.arc(0, 0, radiusX, 0, Math.PI * 2);
context.fillStyle = `rgba(74, 39, 20, ${0.015 + random() * 0.045})`;
context.fill();
context.restore();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.ClampToEdgeWrapping;
texture.repeat.set(2.1, 1.35);
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
return texture;
}
const skinTexture = createSkinTexture();
const outerSkinMaterial = new THREE.MeshPhysicalMaterial({
color: 0xffffff,
map: skinTexture,
roughness: 0.74,
metalness: 0,
transmission: 0.055,
thickness: 0.16,
ior: 1.32,
clearcoat: 0.05,
sheen: 0.2,
sheenColor: new THREE.Color(0xd4a56e),
side: THREE.DoubleSide
});
// Rear half of the bulb: the cut plane faces +Z.
const rearSkinGeometry = new THREE.SphereGeometry(
1,
96,
64,
Math.PI / 2,
Math.PI
);
const rearSkin = new THREE.Mesh(rearSkinGeometry, outerSkinMaterial);
rearSkin.scale.set(RX, RY, 1.03);
rearSkin.position.y = CENTER_Y;
rearSkin.castShadow = true;
rearSkin.receiveShadow = true;
onion.add(rearSkin);
// Fine longitudinal fibers in the papery outer skin.
const fiberMaterial = new THREE.LineBasicMaterial({
color: 0x5b321d,
transparent: true,
opacity: 0.23,
depthWrite: false
});
for (let i = 0; i < 22; i++) {
const phi = Math.PI / 2 + ((i + 0.35) / 22) * Math.PI;
const points = [];
for (let j = 0; j <= 80; j++) {
const theta = 0.13 + (j / 80) * (Math.PI - 0.26);
const sinTheta = Math.sin(theta);
points.push(
new THREE.Vector3(
-RX * 1.004 * Math.cos(phi) * sinTheta,
CENTER_Y + RY * 1.004 * Math.cos(theta),
-1.034 * Math.sin(phi) * sinTheta
)
);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
onion.add(new THREE.Line(geometry, fiberMaterial));
}
// Shared irregularity keeps all concentric layers anatomically aligned.
function boundaryWobble(factor, angle) {
const shared =
1 +
0.011 * Math.sin(3 * angle + 0.55) +
0.006 * Math.sin(7 * angle - 0.35) +
0.003 * Math.sin(11 * angle + 1.2);
return factor * shared;
}
function boundaryPoint(factor, angle) {
const radius = boundaryWobble(factor, angle);
return new THREE.Vector3(
RX * radius * Math.cos(angle),
CENTER_Y + RY * radius * Math.sin(angle),
0
);
}
// Dark warm surface visible in the narrow spaces between layers.
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x80584c,
roughness: 0.78,
metalness: 0
});
const base = new THREE.Mesh(
new THREE.CircleGeometry(1, 128),
baseMaterial
);
base.scale.set(RX, RY, 1);
base.position.set(0, CENTER_Y, -0.035);
base.receiveShadow = true;
onion.add(base);
// Closed, slightly irregular annular geometry with visible depth.
function createAnnularLayer(innerFactor, outerFactor, material, frontZ) {
const segmentCount = 192;
const backZ = -0.028;
const positions = [];
const uvs = [];
const indices = [];
for (let i = 0; i <= segmentCount; i++) {
const angle = (i / segmentCount) * Math.PI * 2;
const inner = boundaryPoint(innerFactor, angle);
const outer = boundaryPoint(outerFactor, angle);
const u = i / segmentCount;
positions.push(
inner.x, inner.y, frontZ,
outer.x, outer.y, frontZ,
inner.x, inner.y, backZ,
outer.x, outer.y, backZ
);
uvs.push(
u, 0,
u, 1,
u, 0.48,
u, 0.52
);
}
for (let i = 0; i < segmentCount; i++) {
const current = i * 4;
const next = current + 4;
// Front face.
indices.push(
current + 1, next + 1, current,
current, next + 1, next
);
// Back face.
indices.push(
current + 2, next + 3, current + 3,
current + 2, next + 2, next + 3
);
// Outer wall.
indices.push(
current + 1, current + 3, next + 1,
next + 1, current + 3, next + 3
);
// Inner wall.
indices.push(
current, next, current + 2,
next, next + 2, current + 2
);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("uv", new THREE.Float32BufferAttribute(uvs, 2));
geometry.setIndex(indices);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
const layerColors = [
0xd69b60,
0xe8bd8d,
0xf2d5b0,
0xe7bfc9,
0xf4dfc9,
0xd7bdd9,
0xf6e4cd,
0xe4c4de,
0xf2dac7,
0xe6bed5,
0xf4e2ca,
0xeac5d6
];
function createFleshMaterial(color) {
return new THREE.MeshPhysicalMaterial({
color,
roughness: 0.4,
metalness: 0,
transmission: 0.13,
thickness: 0.3,
ior: 1.36,
attenuationColor: new THREE.Color(color),
attenuationDistance: 1.8,
clearcoat: 0.14,
clearcoatRoughness: 0.48,
sheen: 0.28,
sheenColor: new THREE.Color(color).lerp(
new THREE.Color(0xfff7e9),
0.58
),
side: THREE.DoubleSide
});
}
const boundaries = [
1.0,
0.928,
0.852,
0.778,
0.698,
0.618,
0.538,
0.458,
0.376,
0.294,
0.214,
0.134,
0.058
];
const separatedLayerIndex = 4;
const explodedRing = new THREE.Group();
for (let i = 0; i < boundaries.length - 1; i++) {
const innerFactor = boundaries[i + 1];
const outerFactor = boundaries[i];
const middleFactor = (innerFactor + outerFactor) * 0.5;
const frontZ = 0.05 + (1 - middleFactor) * 0.025;
const material = createFleshMaterial(layerColors[i]);
const layer = createAnnularLayer(
innerFactor,
outerFactor,
material,
frontZ
);
if (i === separatedLayerIndex) {
explodedRing.add(layer);
} else {
onion.add(layer);
}
}
// The slightly separated ring.
explodedRing.position.set(0.31, 0.035, 0.19);
explodedRing.rotation.set(0.01, -0.075, 0.035);
onion.add(explodedRing);
function createBoundaryLoop(factor, z, color, opacity) {
const points = [];
for (let i = 0; i < 240; i++) {
const angle = (i / 240) * Math.PI * 2;
const point = boundaryPoint(factor, angle);
point.z = z;
points.push(point);
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
return new THREE.LineLoop(geometry, material);
}
explodedRing.add(
createBoundaryLoop(
boundaries[separatedLayerIndex],
0.084,
0x9b6c62,
0.28
)
);
explodedRing.add(
createBoundaryLoop(
boundaries[separatedLayerIndex + 1],
0.084,
0x9b6c62,
0.24
)
);
// Thin papery skin around the cut perimeter.
const skinRimMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb6763e,
roughness: 0.8,
transmission: 0.045,
thickness: 0.1,
ior: 1.3,
sheen: 0.32,
sheenColor: new THREE.Color(0xe2b878),
side: THREE.DoubleSide
});
const skinRim = createAnnularLayer(
0.976,
1.0,
skinRimMaterial,
0.082
);
onion.add(skinRim);
onion.add(
createBoundaryLoop(1.0, 0.088, 0x6f3d22, 0.62)
);
// A small lifted curl of papery skin.
function createSkinCurl(startAngle, endAngle) {
const positions = [];
const indices = [];
const segments = 28;
for (let i = 0; i <= segments; i++) {
const t = i / segments;
const angle = THREE.MathUtils.lerp(startAngle, endAngle, t);
const lift = Math.sin(t * Math.PI) * 0.075;
const inner = boundaryPoint(0.987, angle);
const outer = boundaryPoint(1.035, angle);
inner.z = 0.083 + lift * 0.4;
outer.z = 0.095 + lift;
positions.push(
inner.x, inner.y, inner.z,
outer.x, outer.y, outer.z
);
}
for (let i = 0; i < segments; i++) {
const a = i * 2;
indices.push(a, a + 1, a + 2, a + 1, a + 3, a + 2);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
const material = new THREE.MeshPhysicalMaterial({
color: 0xb97a42,
roughness: 0.82,
transmission: 0.05,
side: THREE.DoubleSide
});
const curl = new THREE.Mesh(geometry, material);
curl.castShadow = true;
return curl;
}
onion.add(createSkinCurl(2.15, 2.72));
// Fine loose fibers at the skin edge.
const edgeFiberPositions = [];
for (let i = 0; i < 38; i++) {
const angle = random() * Math.PI * 2;
const point = boundaryPoint(1.0, angle);
const tangent = new THREE.Vector3(
-Math.sin(angle) * RX,
Math.cos(angle) * RY,
0
).normalize();
const length = 0.018 + random() * 0.055;
const end = point.clone()
.addScaledVector(tangent, length)
.add(new THREE.Vector3(0, 0, random() * 0.018));
edgeFiberPositions.push(
point.x, point.y, 0.09,
end.x, end.y, end.z
);
}
const edgeFiberGeometry = new THREE.BufferGeometry();
edgeFiberGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(edgeFiberPositions, 3)
);
onion.add(
new THREE.LineSegments(
edgeFiberGeometry,
new THREE.LineBasicMaterial({
color: 0xd09a5d,
transparent: true,
opacity: 0.43
})
)
);
// Dried tapered neck.
const neckMaterial = new THREE.MeshStandardMaterial({
color: 0x9d6839,
roughness: 0.9,
map: skinTexture
});
const neck = new THREE.Mesh(
new THREE.CylinderGeometry(0.072, 0.155, 0.42, 18, 8),
neckMaterial
);
neck.position.set(0.015, 1.25, -0.015);
neck.rotation.z = -0.025;
neck.castShadow = true;
onion.add(neck);
const dryTip = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.24, 12),
new THREE.MeshStandardMaterial({
color: 0x6f482d,
roughness: 1
})
);
dryTip.position.set(0.025, 1.52, -0.02);
dryTip.rotation.z = 0.035;
dryTip.castShadow = true;
onion.add(dryTip);
// Root end.
const root = new THREE.Mesh(
new THREE.CylinderGeometry(0.012, 0.13, 0.28, 20),
new THREE.MeshStandardMaterial({
color: 0xb88b5c,
roughness: 0.93
})
);
root.position.set(0, -1.25, -0.025);
root.castShadow = true;
onion.add(root);
const rootFiberMaterial = new THREE.LineBasicMaterial({
color: 0xbba078,
transparent: true,
opacity: 0.55
});
for (let i = 0; i < 15; i++) {
const angle = (i / 15) * Math.PI * 2 + random() * 0.16;
const length = 0.08 + random() * 0.14;
const curve = new THREE.QuadraticBezierCurve3(
new THREE.Vector3(
Math.cos(angle) * 0.055,
-1.31,
Math.sin(angle) * 0.055
),
new THREE.Vector3(
Math.cos(angle + 0.08) * 0.085,
-1.35,
Math.sin(angle + 0.08) * 0.085
),
new THREE.Vector3(
Math.cos(angle + 0.13) * 0.12,
-1.31 - length,
Math.sin(angle + 0.13) * 0.12
)
);
onion.add(
new THREE.Line(
new THREE.BufferGeometry().setFromPoints(curve.getPoints(18)),
rootFiberMaterial
)
);
}
// Small central core.
const coreMaterial = createFleshMaterial(0xf5dfc3);
const core = new THREE.Mesh(
new THREE.SphereGeometry(1, 40, 24),
coreMaterial
);
core.scale.set(0.064, 0.074, 0.028);
core.position.set(0, CENTER_Y, 0.054);
core.castShadow = true;
onion.add(core);
// ------------------------------------------------------------
// Suspended botanical dust
// ------------------------------------------------------------
const dustPositions = [];
for (let i = 0; i < 95; i++) {
dustPositions.push(
(random() - 0.5) * 8,
random() * 5.4 - 1.3,
(random() - 0.5) * 5
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(dustPositions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0xe7d6b7,
size: 0.014,
transparent: true,
opacity: 0.24,
depthWrite: false,
sizeAttenuation: true
})
);
scene.add(dust);
// ------------------------------------------------------------
// Custom camera controls — no external controls addon
// ------------------------------------------------------------
const target = new THREE.Vector3(0.08, 0.03, 0);
const initialCamera = {
radius: 6.45,
phi: 1.34,
theta: 0.31
};
const cameraState = {
radius: initialCamera.radius,
phi: initialCamera.phi,
theta: initialCamera.theta
};
const cameraGoal = {
radius: initialCamera.radius,
phi: initialCamera.phi,
theta: initialCamera.theta
};
const hint = document.getElementById("hint");
let dragging = false;
let previousX = 0;
let previousY = 0;
let interacted = false;
function hideHint() {
if (!interacted) {
interacted = true;
hint.classList.add("hidden");
}
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
hideHint();
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraGoal.theta -= deltaX * 0.0052;
cameraGoal.phi -= deltaY * 0.0045;
cameraGoal.phi = THREE.MathUtils.clamp(
cameraGoal.phi,
0.62,
2.18
);
});
function stopDragging(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (event && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("lostpointercapture", () => {
dragging = false;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius + event.deltaY * 0.004,
4.15,
9.2
);
hideHint();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
cameraGoal.radius = initialCamera.radius;
cameraGoal.phi = initialCamera.phi;
cameraGoal.theta = initialCamera.theta;
});
renderer.domElement.addEventListener("keydown", (event) => {
const keyStep = 0.08;
if (event.key === "ArrowLeft") {
cameraGoal.theta += keyStep;
} else if (event.key === "ArrowRight") {
cameraGoal.theta -= keyStep;
} else if (event.key === "ArrowUp") {
cameraGoal.phi = Math.max(0.62, cameraGoal.phi - keyStep);
} else if (event.key === "ArrowDown") {
cameraGoal.phi = Math.min(2.18, cameraGoal.phi + keyStep);
} else if (event.key === "+" || event.key === "=") {
cameraGoal.radius = Math.max(4.15, cameraGoal.radius - 0.25);
} else if (event.key === "-") {
cameraGoal.radius = Math.min(9.2, cameraGoal.radius + 0.25);
} else {
return;
}
hideHint();
});
function updateCamera(deltaTime) {
const smoothing = 1 - Math.exp(-8 * deltaTime);
cameraState.radius +=
(cameraGoal.radius - cameraState.radius) * smoothing;
cameraState.phi +=
(cameraGoal.phi - cameraState.phi) * smoothing;
cameraState.theta +=
(cameraGoal.theta - cameraState.theta) * smoothing;
const spherical = new THREE.Spherical(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.setFromSpherical(spherical).add(target);
camera.lookAt(target);
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const elapsedTime = clock.elapsedTime;
updateCamera(deltaTime);
// A restrained floating motion emphasizes the separated ring.
explodedRing.position.z =
0.19 + Math.sin(elapsedTime * 0.72) * 0.011;
explodedRing.rotation.z =
0.035 + Math.sin(elapsedTime * 0.55) * 0.006;
dust.rotation.y = elapsedTime * 0.008;
renderer.render(scene, camera);
});
</script>
</body>
</html>
```