Prompt specification
Build or customize the Three.js 3D scene "Disk Bulge and Halo Component Model" 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: Disk Bulge and Halo Component Model
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/disk-bulge-and-halo-component-model-x3781
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this stylized scene: Model a schematic galaxy with distinct disk, central bulge, and diffuse halo components. Label it representative and avoid treating the visible stars as a complete mass map.
Working Three.js Starter (disk-bulge-and-halo-component-model-x3781):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Representative Galaxy Schematic</title>
<style>
:root {
color-scheme: dark;
--text: #eef3ff;
--muted: #9ba9c5;
--panel: rgba(9, 14, 29, 0.72);
--border: rgba(172, 197, 255, 0.16);
--disk: #75b7ff;
--bulge: #ffc275;
--halo: #a79aff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 52% 45%, #10162c 0%, #070b18 37%, #02040b 78%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 48%, transparent 35%, rgba(0, 0, 0, 0.48) 100%);
}
.brand {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
max-width: 460px;
pointer-events: none;
}
.eyebrow {
display: inline-flex;
align-items: center;
gap: 9px;
padding: 7px 10px;
border: 1px solid rgba(157, 185, 255, 0.25);
border-radius: 999px;
background: rgba(10, 17, 36, 0.62);
color: #b9c9e7;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.eyebrow::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: #83b9ff;
box-shadow: 0 0 12px #83b9ff;
content: "";
}
h1 {
margin: 15px 0 7px;
font-size: clamp(29px, 3.5vw, 49px);
font-weight: 530;
letter-spacing: -0.045em;
line-height: 1.02;
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.panel {
position: fixed;
top: 26px;
right: 26px;
z-index: 6;
width: 246px;
padding: 15px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(18px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-heading span {
color: #7584a1;
font-size: 10px;
}
.layer {
width: 100%;
display: grid;
grid-template-columns: 12px 1fr auto;
align-items: center;
gap: 10px;
margin-top: 7px;
padding: 10px;
border: 1px solid rgba(255, 255, 255, 0.055);
border-radius: 10px;
background: rgba(255, 255, 255, 0.035);
color: var(--text);
text-align: left;
cursor: pointer;
transition: 160ms ease;
}
.layer:hover {
border-color: rgba(255, 255, 255, 0.14);
background: rgba(255, 255, 255, 0.065);
}
.layer.off {
opacity: 0.42;
}
.layer .dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--color);
box-shadow: 0 0 10px var(--color);
}
.layer .name {
font-size: 12px;
font-weight: 650;
}
.layer .state {
color: #8594af;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
}
.panel-actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 12px;
}
.action {
padding: 9px 8px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 9px;
background: rgba(255, 255, 255, 0.035);
color: #b9c5dc;
font: inherit;
font-size: 10px;
cursor: pointer;
transition: 150ms ease;
}
.action:hover,
.action.active {
border-color: rgba(137, 181, 255, 0.35);
background: rgba(102, 151, 235, 0.12);
color: #eef5ff;
}
.component-label {
position: fixed;
z-index: 4;
min-width: 112px;
padding: 8px 10px;
border-left: 2px solid var(--component-color);
border-radius: 4px 9px 9px 4px;
background: rgba(7, 11, 25, 0.7);
box-shadow: 0 8px 30px rgba(0, 0, 0, 0.22);
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(9px);
transition: opacity 180ms ease;
}
.component-label strong {
display: block;
color: #f4f7ff;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.035em;
}
.component-label small {
display: block;
margin-top: 2px;
color: #8e9bb4;
font-size: 9px;
line-height: 1.35;
}
.caveat {
position: fixed;
left: 28px;
bottom: 28px;
z-index: 5;
width: min(385px, calc(100vw - 56px));
padding: 14px 16px;
border: 1px solid rgba(143, 169, 220, 0.17);
border-radius: 13px;
background: rgba(7, 11, 24, 0.68);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(16px);
pointer-events: none;
}
.caveat-label {
display: block;
margin-bottom: 5px;
color: #7e94bb;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.caveat strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
font-weight: 650;
}
.caveat p {
margin: 0;
color: #98a5bd;
font-size: 10.5px;
line-height: 1.55;
}
.hint {
position: fixed;
right: 28px;
bottom: 29px;
z-index: 4;
color: #677792;
font-size: 10px;
letter-spacing: 0.035em;
pointer-events: none;
}
@media (max-width: 720px) {
.brand {
top: 16px;
left: 16px;
right: 16px;
}
.brand p {
max-width: 300px;
font-size: 12px;
}
.panel {
top: 122px;
right: 14px;
width: 182px;
padding: 11px;
}
.panel-heading span,
.layer .state {
display: none;
}
.layer {
grid-template-columns: 10px 1fr;
padding: 8px;
}
.component-label {
min-width: 0;
padding: 6px 8px;
}
.component-label small {
display: none;
}
.caveat {
left: 14px;
bottom: 43px;
width: calc(100vw - 28px);
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<canvas
id="scene"
tabindex="0"
aria-label="Interactive schematic of a galaxy with disk, central bulge, and diffuse halo"
></canvas>
<div class="vignette"></div>
<header class="brand">
<div class="eyebrow">Representative schematic · not to scale</div>
<h1>Anatomy of a Galaxy</h1>
<p>
A stylized separation of the disk, central bulge, and diffuse halo.
Visible points are illustrative stellar tracers.
</p>
</header>
<aside class="panel" aria-label="Galaxy component controls">
<div class="panel-heading">
<strong>Component layers</strong>
<span>Click to toggle</span>
</div>
<button class="layer" data-component="disk" style="--color: var(--disk)">
<span class="dot"></span>
<span class="name">Galactic disk</span>
<span class="state">ON</span>
</button>
<button class="layer" data-component="bulge" style="--color: var(--bulge)">
<span class="dot"></span>
<span class="name">Central bulge</span>
<span class="state">ON</span>
</button>
<button class="layer" data-component="halo" style="--color: var(--halo)">
<span class="dot"></span>
<span class="name">Diffuse halo</span>
<span class="state">ON</span>
</button>
<div class="panel-actions">
<button id="guides" class="action active">Guides on</button>
<button id="reset" class="action">Reset view</button>
</div>
</aside>
<div
id="label-disk"
class="component-label"
style="--component-color: var(--disk)"
>
<strong>Galactic disk</strong>
<small>Flattened stellar component</small>
</div>
<div
id="label-bulge"
class="component-label"
style="--component-color: var(--bulge)"
>
<strong>Central bulge</strong>
<small>Compact central concentration</small>
</div>
<div
id="label-halo"
class="component-label"
style="--component-color: var(--halo)"
>
<strong>Diffuse halo</strong>
<small>Extended, sparsely populated region</small>
</div>
<section class="caveat">
<span class="caveat-label">Interpretation note</span>
<strong>Representative structure, not a measurement.</strong>
<p>
The visible points are representative stellar tracers—not a complete
census of stars or a complete mass map. Component extents and envelopes
are illustrative.
</p>
</section>
<div class="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";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setClearColor(0x02040b, 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.12;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
46,
window.innerWidth / window.innerHeight,
0.1,
100
);
const galaxy = new THREE.Group();
scene.add(galaxy);
const componentGroups = {
disk: new THREE.Group(),
bulge: new THREE.Group(),
halo: new THREE.Group()
};
Object.values(componentGroups).forEach((group) => galaxy.add(group));
const guideObjects = [];
const starMaterials = [];
// Deterministic pseudo-random generator so the schematic remains stable.
function mulberry32(seed) {
return function random() {
let value = seed += 0x6D2B79F5;
value = Math.imul(value ^ (value >>> 15), value | 1);
value ^= value + Math.imul(value ^ (value >>> 7), value | 61);
return ((value ^ (value >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(74191);
function gaussian(randomValue) {
const u = Math.max(randomValue(), 1e-7);
const v = random();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
}
function paletteColor(palette, value) {
const scaled = THREE.MathUtils.clamp(value, 0, 0.999999) *
(palette.length - 1);
const index = Math.floor(scaled);
const fraction = scaled - index;
return new THREE.Color(palette[index]).lerp(
new THREE.Color(palette[index + 1]),
fraction
);
}
const starVertexShader = `
attribute float aSize;
attribute float aPhase;
attribute vec3 color;
uniform float uTime;
uniform float uPixelRatio;
varying vec3 vColor;
varying float vTwinkle;
void main() {
vColor = color;
vTwinkle = 0.86 + 0.14 * sin(uTime * 1.1 + aPhase);
vec4 mvPosition = modelViewMatrix * vec4(position, 1.0);
float perspectiveScale = 10.0 / max(5.0, -mvPosition.z);
gl_PointSize = aSize * uPixelRatio * perspectiveScale;
gl_Position = projectionMatrix * mvPosition;
}
`;
const starFragmentShader = `
uniform float uOpacity;
varying vec3 vColor;
varying float vTwinkle;
void main() {
float distanceToCenter = length(gl_PointCoord - vec2(0.5));
if (distanceToCenter > 0.5) discard;
float glow = 1.0 - smoothstep(0.05, 0.5, distanceToCenter);
float core = 1.0 - smoothstep(0.0, 0.16, distanceToCenter);
float alpha = (glow * 0.72 + core * 0.28) * uOpacity * vTwinkle;
if (alpha < 0.008) discard;
gl_FragColor = vec4(vColor * (0.88 + core * 0.42), alpha);
#include <tonemapping_fragment>
#include <colorspace_fragment>
}
`;
function createStarMaterial(opacity) {
const material = new THREE.ShaderMaterial({
uniforms: {
uTime: { value: 0 },
uPixelRatio: { value: renderer.getPixelRatio() },
uOpacity: { value: opacity }
},
vertexShader: starVertexShader,
fragmentShader: starFragmentShader,
transparent: true,
depthWrite: false,
blending: THREE.AdditiveBlending
});
starMaterials.push(material);
return material;
}
function createStarPoints(positions, colors, sizes, phases, opacity) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute(
"color",
new THREE.Float32BufferAttribute(colors, 3)
);
geometry.setAttribute(
"aSize",
new THREE.Float32BufferAttribute(sizes, 1)
);
geometry.setAttribute(
"aPhase",
new THREE.Float32BufferAttribute(phases, 1)
);
const points = new THREE.Points(
geometry,
createStarMaterial(opacity)
);
points.frustumCulled = false;
return points;
}
function createGlowTexture() {
const glowCanvas = document.createElement("canvas");
glowCanvas.width = 128;
glowCanvas.height = 128;
const context = glowCanvas.getContext("2d");
const gradient = context.createRadialGradient(
64, 64, 0,
64, 64, 64
);
gradient.addColorStop(0.0, "rgba(255,255,255,1)");
gradient.addColorStop(0.16, "rgba(255,255,255,0.54)");
gradient.addColorStop(0.48, "rgba(255,255,255,0.13)");
gradient.addColorStop(1.0, "rgba(255,255,255,0)");
context.fillStyle = gradient;
context.fillRect(0, 0, 128, 128);
const texture = new THREE.CanvasTexture(glowCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
return texture;
}
const glowTexture = createGlowTexture();
// ---------------------------------------------------------------
// Galactic disk: logarithmic spiral arms plus a sparse field.
// ---------------------------------------------------------------
const diskPositions = [];
const diskColors = [];
const diskSizes = [];
const diskPhases = [];
const diskPalette = [
"#75aaff",
"#bad8ff",
"#eef5ff",
"#9dbdff",
"#ffe4bd"
];
for (let index = 0; index < 9200; index++) {
const radius = 0.72 + Math.pow(random(), 0.64) * 7.15;
const arm = Math.floor(random() * 4);
const spread = 0.115 + radius * 0.025;
const angle =
arm * Math.PI * 0.5 +
1.58 * Math.log(radius + 0.62) +
gaussian(random) * spread;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const y = gaussian(random) * (0.025 + radius * 0.012);
const colorValue = Math.min(
0.999,
Math.max(0, random() * 0.72 + (radius / 8) * 0.26)
);
const color = paletteColor(diskPalette, colorValue);
diskPositions.push(x, y, z);
diskColors.push(color.r, color.g, color.b);
diskSizes.push(0.85 + random() * 1.45);
diskPhases.push(random() * Math.PI * 2);
}
// Low-density interarm stars keep the disk from looking like only
// four perfectly clean curves.
for (let index = 0; index < 1700; index++) {
const radius = 0.8 + Math.sqrt(random()) * 7.0;
const angle = random() * Math.PI * 2;
const color = paletteColor(diskPalette, random());
diskPositions.push(
Math.cos(angle) * radius,
gaussian(random) * 0.045,
Math.sin(angle) * radius
);
diskColors.push(color.r, color.g, color.b);
diskSizes.push(0.65 + random() * 0.85);
diskPhases.push(random() * Math.PI * 2);
}
const diskStars = createStarPoints(
diskPositions,
diskColors,
diskSizes,
diskPhases,
0.74
);
componentGroups.disk.add(diskStars);
// Four faint logarithmic guides matching the illustrative star arms.
for (let arm = 0; arm < 4; arm++) {
const curvePositions = [];
const pointCount = 220;
for (let index = 0; index <= pointCount; index++) {
const radius = 0.65 + (7.9 - 0.65) * index / pointCount;
const angle =
arm * Math.PI * 0.5 +
1.58 * Math.log(radius + 0.62);
curvePositions.push(
Math.cos(angle) * radius,
0,
Math.sin(angle) * radius
);
}
const curveGeometry = new THREE.BufferGeometry();
curveGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(curvePositions, 3)
);
const curve = new THREE.Line(
curveGeometry,
new THREE.LineBasicMaterial({
color: arm % 2 === 0 ? 0x6ca9ff : 0xa9c9ff,
transparent: true,
opacity: 0.16,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
guideObjects.push(curve);
componentGroups.disk.add(curve);
}
const diskRingPoints = [];
for (let index = 0; index <= 240; index++) {
const angle = index / 240 * Math.PI * 2;
diskRingPoints.push(
Math.cos(angle) * 7.9,
0,
Math.sin(angle) * 7.9
);
}
const diskRingGeometry = new THREE.BufferGeometry();
diskRingGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(diskRingPoints, 3)
);
const diskRing = new THREE.Line(
diskRingGeometry,
new THREE.LineBasicMaterial({
color: 0x6a9ed9,
transparent: true,
opacity: 0.11,
depthWrite: false
})
);
guideObjects.push(diskRing);
componentGroups.disk.add(diskRing);
const diskHaze = new THREE.Mesh(
new THREE.RingGeometry(0.7, 8.1, 160),
new THREE.MeshBasicMaterial({
color: 0x416da8,
transparent: true,
opacity: 0.025,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
diskHaze.rotation.x = -Math.PI / 2;
componentGroups.disk.add(diskHaze);
// ---------------------------------------------------------------
// Central bulge: compact, warm-colored concentration.
// ---------------------------------------------------------------
const bulgePositions = [];
const bulgeColors = [];
const bulgeSizes = [];
const bulgePhases = [];
const bulgePalette = [
"#ffb76b",
"#ffd292",
"#fff0cf",
"#e99a58"
];
for (let index = 0; index < 2600; index++) {
const radius = Math.pow(random(), 0.56) * 1.5;
const vertical = random() * 2 - 1;
const azimuth = random() * Math.PI * 2;
const horizontal = Math.sqrt(1 - vertical * vertical);
const color = paletteColor(bulgePalette, random());
bulgePositions.push(
Math.cos(azimuth) * horizontal * radius,
vertical * radius * 0.72,
Math.sin(azimuth) * horizontal * radius
);
bulgeColors.push(color.r, color.g, color.b);
bulgeSizes.push(0.9 + random() * 1.55);
bulgePhases.push(random() * Math.PI * 2);
}
const bulgeStars = createStarPoints(
bulgePositions,
bulgeColors,
bulgeSizes,
bulgePhases,
0.9
);
componentGroups.bulge.add(bulgeStars);
const bulgeGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: glowTexture,
color: 0xffb56d,
transparent: true,
opacity: 0.34,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
bulgeGlow.scale.set(4.4, 4.4, 1);
componentGroups.bulge.add(bulgeGlow);
const bulgeEnvelope = new THREE.Mesh(
new THREE.SphereGeometry(1.58, 18, 12),
new THREE.MeshBasicMaterial({
color: 0xffb56d,
wireframe: true,
transparent: true,
opacity: 0.075,
depthWrite: false
})
);
guideObjects.push(bulgeEnvelope);
componentGroups.bulge.add(bulgeEnvelope);
// ---------------------------------------------------------------
// Diffuse halo: sparse, extended, cool-colored points.
// ---------------------------------------------------------------
const haloPositions = [];
const haloColors = [];
const haloSizes = [];
const haloPhases = [];
const haloPalette = [
"#777fca",
"#94a9ec",
"#b4a6ef",
"#7199d4"
];
for (let index = 0; index < 6000; index++) {
const radius = 1.55 + Math.pow(random(), 1.18) * 8.5;
const vertical = random() * 2 - 1;
const azimuth = random() * Math.PI * 2;
const horizontal = Math.sqrt(1 - vertical * vertical);
const color = paletteColor(haloPalette, random());
haloPositions.push(
Math.cos(azimuth) * horizontal * radius,
vertical * radius,
Math.sin(azimuth) * horizontal * radius
);
haloColors.push(color.r, color.g, color.b);
haloSizes.push(0.8 + random() * 1.55);
haloPhases.push(random() * Math.PI * 2);
}
const haloStars = createStarPoints(
haloPositions,
haloColors,
haloSizes,
haloPhases,
0.19
);
componentGroups.halo.add(haloStars);
const haloEnvelope = new THREE.Mesh(
new THREE.SphereGeometry(10.15, 20, 14),
new THREE.MeshBasicMaterial({
color: 0x8279c9,
wireframe: true,
transparent: true,
opacity: 0.022,
depthWrite: false
})
);
guideObjects.push(haloEnvelope);
componentGroups.halo.add(haloEnvelope);
const haloGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: glowTexture,
color: 0x596cb8,
transparent: true,
opacity: 0.045,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
haloGlow.scale.set(22, 22, 1);
componentGroups.halo.add(haloGlow);
// ---------------------------------------------------------------
// Custom camera controls. No Three.js addons are used.
// ---------------------------------------------------------------
const initialCamera = {
theta: 0.22,
phi: 1.15,
radius: 17.5
};
const cameraState = {
theta: initialCamera.theta,
phi: initialCamera.phi,
radius: initialCamera.radius,
targetTheta: initialCamera.theta,
targetPhi: initialCamera.phi,
targetRadius: initialCamera.radius
};
const pointers = new Map();
let previousPinchDistance = 0;
function resetCamera() {
cameraState.targetTheta = initialCamera.theta;
cameraState.targetPhi = initialCamera.phi;
cameraState.targetRadius = initialCamera.radius;
}
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.focus();
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
previousPinchDistance = pointerDistance();
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const deltaX = event.clientX - previous.x;
const deltaY = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
cameraState.targetTheta -= deltaX * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + deltaY * 0.005,
0.28,
2.62
);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius *
(previousPinchDistance / distance),
9,
29
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointerDistance();
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius * Math.exp(event.deltaY * 0.001),
9,
29
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener("keydown", (event) => {
const key = event.key;
if (key === "ArrowLeft") {
cameraState.targetTheta += 0.09;
} else if (key === "ArrowRight") {
cameraState.targetTheta -= 0.09;
} else if (key === "ArrowUp") {
cameraState.targetPhi = Math.max(
0.28,
cameraState.targetPhi - 0.07
);
} else if (key === "ArrowDown") {
cameraState.targetPhi = Math.min(
2.62,
cameraState.targetPhi + 0.07
);
} else if (key === "+" || key === "=") {
cameraState.targetRadius = Math.max(
9,
cameraState.targetRadius * 0.9
);
} else if (key === "-" || key === "_") {
cameraState.targetRadius = Math.min(
29,
cameraState.targetRadius * 1.1
);
} else if (key.toLowerCase() === "r") {
resetCamera();
} else {
return;
}
event.preventDefault();
});
document.querySelector("#reset").addEventListener("click", resetCamera);
// Component visibility controls.
document.querySelectorAll(".layer").forEach((button) => {
button.addEventListener("click", () => {
const name = button.dataset.component;
const group = componentGroups[name];
const enabled = !group.visible;
group.visible = enabled;
button.classList.toggle("off", !enabled);
button.setAttribute("aria-pressed", String(enabled));
const state = button.querySelector(".state");
if (state) state.textContent = enabled ? "ON" : "OFF";
});
});
const guidesButton = document.querySelector("#guides");
guidesButton.addEventListener("click", () => {
const enabled = !guidesButton.classList.contains("active");
guidesButton.classList.toggle("active", enabled);
guidesButton.textContent = enabled ? "Guides on" : "Guides off";
guidesButton.setAttribute("aria-pressed", String(enabled));
guideObjects.forEach((object) => {
object.visible = enabled;
});
});
// ---------------------------------------------------------------
// Screen-space component labels.
// ---------------------------------------------------------------
const labelAnchors = {
disk: {
element: document.querySelector("#label-disk"),
position: new THREE.Vector3(5.9, 0.05, 4.25)
},
bulge: {
element: document.querySelector("#label-bulge"),
position: new THREE.Vector3(0.15, 1.55, 0.05)
},
halo: {
element: document.querySelector("#label-halo"),
position: new THREE.Vector3(-4.7, 4.15, -5.7)
}
};
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const [name, label] of Object.entries(labelAnchors)) {
projected.copy(label.position).project(camera);
const visible =
componentGroups[name].visible &&
projected.z > -1 &&
projected.z < 1;
label.element.style.opacity = visible ? "1" : "0";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
function updateCamera(deltaTime) {
const smoothing = 1 - Math.exp(-8 * deltaTime);
cameraState.theta +=
(cameraState.targetTheta - cameraState.theta) * smoothing;
cameraState.phi +=
(cameraState.targetPhi - cameraState.phi) * smoothing;
cameraState.radius +=
(cameraState.targetRadius - cameraState.radius) * smoothing;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.radius *
sinPhi *
Math.sin(cameraState.theta),
cameraState.radius * Math.cos(cameraState.phi),
cameraState.radius *
sinPhi *
Math.cos(cameraState.theta)
);
camera.lookAt(0, 0, 0);
camera.updateMatrixWorld();
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
starMaterials.forEach((material) => {
material.uniforms.uPixelRatio.value = renderer.getPixelRatio();
});
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
function animate() {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const elapsedTime = clock.elapsedTime;
updateCamera(deltaTime);
starMaterials.forEach((material) => {
material.uniforms.uTime.value = elapsedTime;
});
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```