Prompt specification
Build or customize the Three.js 3D scene "Same Galaxy Face-On and Edge-On" 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: Same Galaxy Face-On and Edge-On
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/same-galaxy-face-on-and-edge-on-x3782
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows galaxy structure and viewing orientation. Orbit the camera around one fixed procedural stellar distribution between face-on and edge-on views. Preserve every star's world position and show how projection alone changes apparent shape. Voxel style.
Working Three.js Starter (same-galaxy-face-on-and-edge-on-x3782):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#03050d">
<title>Voxel Galaxy — Projection Study</title>
<style>
:root {
color-scheme: dark;
--bg: #03050d;
--panel: rgba(8, 13, 27, 0.72);
--border: rgba(155, 190, 255, 0.18);
--text: #eef5ff;
--muted: #8fa4c7;
--blue: #8fc7ff;
--gold: #ffd08a;
--pink: #ff9fc8;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(ellipse at 50% 45%, #11172c 0%, #080c19 38%, #03050d 76%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 48%, transparent 42%, rgba(0, 2, 9, 0.48) 100%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
border-radius: 16px;
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.intro {
position: absolute;
top: 18px;
left: 18px;
width: min(330px, calc(100vw - 36px));
padding: 18px;
}
.eyebrow {
margin-bottom: 10px;
color: var(--blue);
font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 39px);
line-height: 0.92;
letter-spacing: 0.12em;
font-weight: 760;
}
.lock {
display: flex;
align-items: center;
gap: 8px;
margin: 15px 0 8px;
color: #c6d8f5;
font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.1em;
}
.lock-dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: #76f0b1;
box-shadow: 0 0 12px rgba(118, 240, 177, 0.8);
}
.intro p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 16px;
}
.metric {
min-width: 0;
padding: 10px 8px;
border: 1px solid rgba(155, 190, 255, 0.11);
border-radius: 10px;
background: rgba(255, 255, 255, 0.025);
}
.metric strong {
display: block;
overflow: hidden;
color: #f3f8ff;
font: 650 15px/1.1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric small {
display: block;
margin-top: 6px;
color: #6f86aa;
font: 600 8px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.09em;
}
.ratio-track {
height: 3px;
margin-top: 13px;
overflow: hidden;
border-radius: 3px;
background: rgba(143, 199, 255, 0.12);
}
.ratio-track i {
display: block;
width: 100%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--pink), var(--gold), var(--blue));
transform-origin: left center;
}
.orientation {
position: absolute;
top: 18px;
right: 18px;
width: 248px;
padding: 15px;
}
.panel-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
color: #91a7c9;
font: 600 9px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.12em;
}
.mode {
color: var(--gold);
text-align: right;
}
.orientation svg {
display: block;
width: 100%;
height: auto;
margin: 5px 0 3px;
overflow: visible;
}
.orientation-caption {
display: flex;
justify-content: space-between;
color: #7288aa;
font: 600 8px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.08em;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 12px;
margin-top: 13px;
padding-top: 12px;
border-top: 1px solid rgba(155, 190, 255, 0.1);
color: #8398b9;
font-size: 9px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.legend i {
width: 6px;
height: 6px;
border-radius: 1px;
background: currentColor;
box-shadow: 0 0 7px currentColor;
}
.legend .arms { color: #9ccbff; }
.legend .core { color: #ffd08a; }
.legend .halo { color: #9d91d9; }
.dock {
position: absolute;
left: 50%;
bottom: 18px;
display: flex;
align-items: center;
gap: 6px;
max-width: calc(100vw - 36px);
padding: 7px;
border: 1px solid var(--border);
border-radius: 14px;
background: rgba(8, 13, 27, 0.82);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: auto;
transform: translateX(-50%);
}
.dock button {
min-height: 34px;
padding: 0 12px;
border: 1px solid transparent;
border-radius: 9px;
background: transparent;
color: #90a5c7;
cursor: pointer;
font: 650 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.055em;
transition:
color 160ms ease,
background 160ms ease,
border-color 160ms ease;
white-space: nowrap;
}
.dock button:hover {
color: #f2f7ff;
background: rgba(255, 255, 255, 0.055);
}
.dock button.active {
color: #eaf5ff;
border-color: rgba(143, 199, 255, 0.22);
background: rgba(143, 199, 255, 0.11);
}
.separator {
width: 1px;
height: 21px;
margin: 0 2px;
background: rgba(155, 190, 255, 0.13);
}
.auto-dot {
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 50%;
background: #687b9d;
vertical-align: 1px;
}
.active .auto-dot {
background: #76f0b1;
box-shadow: 0 0 9px rgba(118, 240, 177, 0.9);
}
.dock-hint {
padding: 0 8px;
color: #60769b;
font: 500 9px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.03em;
white-space: nowrap;
}
.selection {
position: absolute;
left: 18px;
bottom: 18px;
width: 250px;
padding: 14px;
opacity: 1;
transform: translateY(0);
transition:
opacity 180ms ease,
transform 180ms ease;
}
.selection.hidden {
opacity: 0;
transform: translateY(10px);
pointer-events: none;
}
.selection .close {
width: 22px;
height: 22px;
padding: 0;
border: 0;
border-radius: 6px;
background: transparent;
color: #758bae;
cursor: pointer;
pointer-events: auto;
}
.selection .close:hover {
color: white;
background: rgba(255, 255, 255, 0.06);
}
.selected-id {
margin: 12px 0 10px;
color: var(--gold);
font: 650 16px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.coordinates {
display: grid;
gap: 6px;
}
.coordinates div {
display: flex;
justify-content: space-between;
padding: 6px 8px;
border-radius: 7px;
background: rgba(255, 255, 255, 0.025);
color: #7188ac;
font: 600 9px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.coordinates b {
color: #dbe9ff;
font-weight: 550;
}
.selection-note {
margin: 10px 0 0;
color: #6d83a5;
font-size: 9px;
line-height: 1.45;
}
#loader {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-content: center;
gap: 18px;
background: #03050d;
color: #9fb4d6;
font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
letter-spacing: 0.18em;
text-align: center;
transition: opacity 450ms ease;
}
#loader.done {
opacity: 0;
pointer-events: none;
}
.loader-cube {
position: relative;
width: 32px;
height: 32px;
margin: auto;
border: 2px solid #83baff;
box-shadow: 0 0 24px rgba(131, 186, 255, 0.25);
transform: rotate(30deg) skew(-8deg, -8deg);
animation: cubePulse 1.5s ease-in-out infinite;
}
@keyframes cubePulse {
0%, 100% { transform: rotate(30deg) skew(-8deg, -8deg) scale(0.88); }
50% { transform: rotate(30deg) skew(-8deg, -8deg) scale(1.08); }
}
@media (max-width: 980px) {
.dock-hint {
display: none;
}
}
@media (max-width: 720px) {
.intro {
width: 260px;
padding: 14px;
}
.intro p,
.legend {
display: none;
}
.metrics {
margin-top: 12px;
}
.orientation {
display: none;
}
.dock {
bottom: 12px;
gap: 2px;
padding: 5px;
}
.dock button {
padding: 0 9px;
font-size: 9px;
}
.selection {
left: 12px;
bottom: 74px;
width: 220px;
}
}
@media (max-width: 480px) {
.intro {
top: 12px;
left: 12px;
}
.intro h1 {
font-size: 26px;
}
.metric {
padding: 8px 6px;
}
.metric strong {
font-size: 12px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel galaxy"></canvas>
<div class="vignette"></div>
<div class="hud">
<section class="panel intro">
<div class="eyebrow">Projection study / 01</div>
<h1>VOXEL<br>GALAXY</h1>
<div class="lock">
<span class="lock-dot"></span>
FIXED WORLD COORDINATES
</div>
<p>
One procedurally generated stellar distribution.
Only the camera moves; every voxel keeps its exact world position.
</p>
<div class="metrics">
<div class="metric">
<strong id="inclination">0°</strong>
<small>INCLINATION</small>
</div>
<div class="metric">
<strong id="ratio">1.000</strong>
<small>AXIS RATIO</small>
</div>
<div class="metric">
<strong id="starCount">0</strong>
<small>VOXEL STARS</small>
</div>
</div>
<div class="ratio-track"><i id="ratioFill"></i></div>
</section>
<section class="panel orientation">
<div class="panel-head">
<span>VIEW ORIENTATION</span>
<span class="mode" id="mode">AUTO SWEEP</span>
</div>
<svg viewBox="0 0 220 126" role="img" aria-label="Projected galaxy shape">
<defs>
<radialGradient id="diskGradient">
<stop offset="0" stop-color="#ffe1a6" stop-opacity="0.95"/>
<stop offset="0.22" stop-color="#b9cfff" stop-opacity="0.82"/>
<stop offset="0.7" stop-color="#7faeff" stop-opacity="0.38"/>
<stop offset="1" stop-color="#6686d5" stop-opacity="0.05"/>
</radialGradient>
</defs>
<g fill="none" stroke="#607ba8" stroke-width="1" opacity="0.22">
<path d="M34 66H186"/>
<path d="M110 15V117"/>
<path d="M55 31L165 101"/>
<path d="M165 31L55 101"/>
</g>
<ellipse
id="gaugeDisk"
cx="110"
cy="66"
rx="76"
ry="76"
fill="url(#diskGradient)"
stroke="#8fbcff"
stroke-width="1.3"
opacity="0.9"
/>
<ellipse
id="gaugeCore"
cx="110"
cy="66"
rx="15"
ry="15"
fill="#ffd08a"
opacity="0.8"
/>
<g fill="#657d9f" font-family="monospace" font-size="8">
<text x="34" y="120">X / Z DISK</text>
<text x="171" y="120">+Y NORMAL</text>
</g>
</svg>
<div class="orientation-caption">
<span>PROJECTED MINOR / MAJOR</span>
<span id="gaugeRatio">1.000</span>
</div>
<div class="panel-head" style="margin-top: 10px">
<span>CAMERA AZIMUTH</span>
<span id="azimuth">000°</span>
</div>
<div class="legend">
<span class="arms"><i></i> Spiral arms</span>
<span class="core"><i></i> Central bulge</span>
<span class="halo"><i></i> Sparse halo</span>
</div>
</section>
<section id="selectionPanel" class="panel selection hidden">
<div class="panel-head">
<span>LOCKED VOXEL</span>
<button id="clearSelection" class="close" aria-label="Clear selected star">×</button>
</div>
<div id="selectedId" class="selected-id">VOXEL #00000</div>
<div class="coordinates">
<div><span>WORLD X</span><b id="coordX">+0.000</b></div>
<div><span>WORLD Y</span><b id="coordY">+0.000</b></div>
<div><span>WORLD Z</span><b id="coordZ">+0.000</b></div>
</div>
<p class="selection-note">
These coordinates remain unchanged while the camera changes orientation.
</p>
</section>
<nav class="dock" aria-label="Galaxy view controls">
<button type="button" data-view="face">Face-on</button>
<button type="button" data-view="edge">Edge-on</button>
<button type="button" data-view="oblique">¾ view</button>
<span class="separator"></span>
<button type="button" id="autoBtn" class="active">
<span class="auto-dot"></span><span id="autoText">Auto orbit</span>
</button>
<button type="button" id="guidesBtn" class="active">Guides</button>
<span class="separator"></span>
<span class="dock-hint">DRAG TO ORBIT · WHEEL TO ZOOM</span>
</nav>
</div>
<div id="loader">
<div class="loader-cube"></div>
<div>ASSEMBLING FIXED VOXELS</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and renderer
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const loader = document.querySelector("#loader");
const ui = {
inclination: document.querySelector("#inclination"),
ratio: document.querySelector("#ratio"),
ratioFill: document.querySelector("#ratioFill"),
starCount: document.querySelector("#starCount"),
mode: document.querySelector("#mode"),
azimuth: document.querySelector("#azimuth"),
gaugeDisk: document.querySelector("#gaugeDisk"),
gaugeCore: document.querySelector("#gaugeCore"),
gaugeRatio: document.querySelector("#gaugeRatio"),
autoBtn: document.querySelector("#autoBtn"),
autoText: document.querySelector("#autoText"),
guidesBtn: document.querySelector("#guidesBtn"),
selectionPanel: document.querySelector("#selectionPanel"),
selectedId: document.querySelector("#selectedId"),
coordX: document.querySelector("#coordX"),
coordY: document.querySelector("#coordY"),
coordZ: document.querySelector("#coordZ")
};
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setClearColor(0x03050d, 0);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
600
);
scene.add(new THREE.HemisphereLight(0x8faeff, 0x12071b, 1.35));
const keyLight = new THREE.DirectionalLight(0xffffff, 1.8);
keyLight.position.set(9, 13, 8);
scene.add(keyLight);
// -------------------------------------------------------------------------
// Deterministic procedural generation
// -------------------------------------------------------------------------
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(0xC0FFEE);
function gaussian() {
let u = 0;
let v = 0;
while (u === 0) u = random();
while (v === 0) v = random();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(Math.PI * 2 * v);
}
const TAU = Math.PI * 2;
const stars = [];
const dust = [];
const armColors = [
new THREE.Color(0x8fc9ff),
new THREE.Color(0xffc58e),
new THREE.Color(0xb9a3ff),
new THREE.Color(0xff9fc7)
];
const white = new THREE.Color(0xffffff);
const tempColor = new THREE.Color();
function addStar(x, y, z, size, color) {
stars.push({
x,
y,
z,
size,
r: color.r,
g: color.g,
b: color.b
});
}
// Four logarithmic spiral arms.
for (let i = 0; i < 5200; i++) {
const arm = i % 4;
const radius = 3 + Math.pow(random(), 0.9) * 22;
const winding = 2.3 * Math.log(radius / 2.8);
const angularScatter = gaussian() * (0.075 + radius * 0.0027);
const angle = arm * TAU / 4 + winding + angularScatter;
const radialScatter = gaussian() * 0.22;
const r = radius + radialScatter;
const x = Math.cos(angle) * r;
const z = Math.sin(angle) * r;
const thickness = 0.14 + radius * 0.009;
const y = gaussian() * thickness;
tempColor.copy(armColors[arm]).lerp(white, random() * 0.24);
const size =
0.055 + random() * 0.105 +
(radius < 6 ? 0.025 : 0);
addStar(x, y, z, size, tempColor);
}
// Diffuse disk population.
for (let i = 0; i < 1600; i++) {
const radius = 2 + Math.sqrt(random()) * 23;
const angle = random() * TAU;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const y = gaussian() * (0.19 + radius * 0.008);
tempColor.setHex(0x7891bd).lerp(white, random() * 0.28);
const size = 0.04 + random() * 0.075;
addStar(x, y, z, size, tempColor);
}
// Dense, warm central bulge.
for (let i = 0; i < 900; i++) {
const radius = Math.pow(random(), 0.56) * 3.5;
const angle = random() * TAU;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius;
const y = gaussian() * 0.66;
tempColor.setHex(0xffbd72).lerp(white, random() * 0.32);
const size = 0.07 + random() * 0.12;
addStar(x, y, z, size, tempColor);
}
// Sparse three-dimensional halo.
for (let i = 0; i < 700; i++) {
const radius = 4 + Math.pow(random(), 0.6) * 25;
const yUnit = random() * 2 - 1;
const angle = random() * TAU;
const ringRadius = Math.sqrt(1 - yUnit * yUnit);
const x = Math.cos(angle) * ringRadius * radius;
const y = yUnit * radius;
const z = Math.sin(angle) * ringRadius * radius;
tempColor.setHex(0x7189cf).lerp(white, random() * 0.15);
const size = 0.035 + random() * 0.055;
addStar(x, y, z, size, tempColor);
}
// Elongated central bar.
for (let i = 0; i < 600; i++) {
const x = gaussian() * 2.45;
const z = gaussian() * 0.56;
const y = gaussian() * 0.32;
tempColor.setHex(0xffd19a).lerp(white, random() * 0.25);
const size = 0.055 + random() * 0.09;
addStar(x, y, z, size, tempColor);
}
// Fixed voxel dust blocks, placed as broken lanes within the arms.
for (let i = 0; i < 220; i++) {
const arm = Math.floor(random() * 4);
const radius = 6 + Math.pow(random(), 0.9) * 16;
const winding = 2.3 * Math.log(radius / 2.8);
const angle = arm * TAU / 4 + winding + gaussian() * 0.065;
dust.push({
x: Math.cos(angle) * radius,
y: 0.13 + random() * 0.18,
z: Math.sin(angle) * radius,
size: 0.38 + random() * 0.8
});
}
// -------------------------------------------------------------------------
// Static instanced voxel meshes
//
// IMPORTANT: these matrices are written once and are never changed.
// The animation loop updates only the camera.
// -------------------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const starMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
vertexColors: true,
toneMapped: false
});
const glowMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
vertexColors: true,
transparent: true,
opacity: 0.075,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
});
const starMesh = new THREE.InstancedMesh(
cubeGeometry,
starMaterial,
stars.length
);
const glowMesh = new THREE.InstancedMesh(
cubeGeometry,
glowMaterial,
stars.length
);
starMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
glowMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
starMesh.frustumCulled = false;
glowMesh.frustumCulled = false;
for (let i = 0; i < stars.length; i++) {
const star = stars[i];
dummy.position.set(star.x, star.y, star.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(star.size);
dummy.updateMatrix();
starMesh.setMatrixAt(i, dummy.matrix);
dummy.scale.setScalar(star.size * 2.45);
dummy.updateMatrix();
glowMesh.setMatrixAt(i, dummy.matrix);
tempColor.setRGB(star.r, star.g, star.b);
starMesh.setColorAt(i, tempColor);
glowMesh.setColorAt(i, tempColor);
}
starMesh.instanceMatrix.needsUpdate = true;
glowMesh.instanceMatrix.needsUpdate = true;
starMesh.instanceColor.needsUpdate = true;
glowMesh.instanceColor.needsUpdate = true;
scene.add(glowMesh);
scene.add(starMesh);
const dustMaterial = new THREE.MeshStandardMaterial({
color: 0x080c19,
roughness: 0.95,
metalness: 0,
vertexColors: true
});
const dustMesh = new THREE.InstancedMesh(
cubeGeometry,
dustMaterial,
dust.length
);
dustMesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
dustMesh.frustumCulled = false;
for (let i = 0; i < dust.length; i++) {
const block = dust[i];
dummy.position.set(block.x, block.y, block.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(
block.size,
block.size * (0.22 + random() * 0.18),
block.size * (0.45 + random() * 0.45)
);
dummy.updateMatrix();
dustMesh.setMatrixAt(i, dummy.matrix);
tempColor.setHex(0x070a15).lerp(
new THREE.Color(0x263252),
random() * 0.22
);
dustMesh.setColorAt(i, tempColor);
}
dustMesh.instanceMatrix.needsUpdate = true;
dustMesh.instanceColor.needsUpdate = true;
scene.add(dustMesh);
ui.starCount.textContent = stars.length.toLocaleString();
// -------------------------------------------------------------------------
// Orientation guides: X/Z is the disk plane; Y is the thickness axis
// -------------------------------------------------------------------------
const guideGroup = new THREE.Group();
scene.add(guideGroup);
const grid = new THREE.GridHelper(64, 32, 0x31527e, 0x17243c);
grid.position.y = -0.75;
grid.material.transparent = true;
grid.material.opacity = 0.23;
grid.material.depthWrite = false;
guideGroup.add(grid);
function makeLine(points, color, opacity = 1) {
const geometry = new THREE.BufferGeometry().setFromPoints(
points.map((point) => new THREE.Vector3(...point))
);
const material = new THREE.LineBasicMaterial({
color,
transparent: opacity < 1,
opacity,
toneMapped: false
});
return new THREE.Line(geometry, material);
}
guideGroup.add(
makeLine(
[[-31, -0.72, 0], [31, -0.72, 0]],
0xb56c7d,
0.62
)
);
guideGroup.add(
makeLine(
[[0, -0.72, -31], [0, -0.72, 31]],
0x617fbd,
0.62
)
);
guideGroup.add(
makeLine(
[[0, -8, 0], [0, 10, 0]],
0x6ecb9b,
0.62
)
);
const boundaryPoints = [];
for (let i = 0; i < 180; i++) {
const angle = (i / 180) * TAU;
boundaryPoints.push(
new THREE.Vector3(
Math.cos(angle) * 25.5,
0,
Math.sin(angle) * 25.5
)
);
}
const boundary = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(boundaryPoints),
new THREE.LineBasicMaterial({
color: 0x6f9dd2,
transparent: true,
opacity: 0.24,
toneMapped: false
})
);
guideGroup.add(boundary);
function createLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 128;
labelCanvas.height = 64;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 128, 64);
context.font = "700 40px monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillStyle = color;
context.fillText(text, 64, 32);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(3.2, 1.6, 1);
return sprite;
}
const xLabel = createLabel("X", "#e58b9b");
xLabel.position.set(31.5, -0.7, 0);
guideGroup.add(xLabel);
const zLabel = createLabel("Z", "#89a6ff");
zLabel.position.set(0, -0.7, 31.5);
guideGroup.add(zLabel);
const yLabel = createLabel("Y", "#82e3b1");
yLabel.position.set(0, 10.5, 0);
guideGroup.add(yLabel);
// -------------------------------------------------------------------------
// Selected voxel marker
// -------------------------------------------------------------------------
const selectionMarker = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(1, 1, 1)),
new THREE.LineBasicMaterial({
color: 0xffd08a,
transparent: true,
opacity: 0.95,
depthTest: false,
toneMapped: false
})
);
selectionMarker.visible = false;
selectionMarker.renderOrder = 100;
scene.add(selectionMarker);
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function formatCoordinate(value) {
const sign = value >= 0 ? "+" : "−";
return `${sign}${Math.abs(value).toFixed(3)}`;
}
function clearSelection() {
selectionMarker.visible = false;
ui.selectionPanel.classList.add("hidden");
}
function showSelection(star, index) {
selectionMarker.visible = true;
selectionMarker.position.set(star.x, star.y, star.z);
selectionMarker.scale.setScalar(star.size * 3.8);
ui.selectedId.textContent =
`VOXEL #${String(index + 1).padStart(5, "0")}`;
ui.coordX.textContent = formatCoordinate(star.x);
ui.coordY.textContent = formatCoordinate(star.y);
ui.coordZ.textContent = formatCoordinate(star.z);
ui.selectionPanel.classList.remove("hidden");
}
function pickVoxel(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointerNDC.x =
((clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y =
-((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(
[glowMesh, starMesh],
false
);
if (hits.length && Number.isInteger(hits[0].instanceId)) {
const index = hits[0].instanceId;
showSelection(stars[index], index);
} else {
clearSelection();
}
}
document.querySelector("#clearSelection").addEventListener("click", clearSelection);
// -------------------------------------------------------------------------
// Direct camera controls — no external controls addon
// -------------------------------------------------------------------------
const MIN_INCLINATION = 0.001;
const MAX_INCLINATION = Math.PI / 2 - 0.001;
const orbit = {
theta: 0.001, // polar angle from the +Y face-on axis
targetTheta: 0.001,
phi: 0.55, // azimuth
targetPhi: 0.55,
radius: 69,
targetRadius: 69,
auto: true,
phase: 0,
preset: null
};
const pointers = new Map();
let gestureMoved = false;
let pinchDistance = 0;
function updateButtons() {
document.querySelectorAll("[data-view]").forEach((button) => {
button.classList.toggle(
"active",
!orbit.auto && button.dataset.view === orbit.preset
);
});
ui.autoBtn.classList.toggle("active", orbit.auto);
ui.autoText.textContent = orbit.auto ? "Auto orbit" : "Auto orbit";
}
function disableAuto() {
if (!orbit.auto) return;
orbit.auto = false;
orbit.preset = null;
updateButtons();
}
function setPreset(view) {
orbit.auto = false;
orbit.preset = view;
if (view === "face") {
orbit.targetTheta = MIN_INCLINATION;
orbit.targetPhi = 0.55;
} else if (view === "edge") {
orbit.targetTheta = MAX_INCLINATION;
orbit.targetPhi = 0.55;
} else {
orbit.targetTheta = 1.02;
orbit.targetPhi = 0.72;
}
updateButtons();
}
document.querySelectorAll("[data-view]").forEach((button) => {
button.addEventListener("click", () => {
setPreset(button.dataset.view);
});
});
ui.autoBtn.addEventListener("click", () => {
orbit.auto = !orbit.auto;
orbit.preset = null;
if (orbit.auto) {
orbit.phase = 0;
orbit.targetPhi = orbit.phi;
}
updateButtons();
});
ui.guidesBtn.addEventListener("click", () => {
guideGroup.visible = !guideGroup.visible;
ui.guidesBtn.classList.toggle("active", guideGroup.visible);
});
canvas.addEventListener("pointerdown", (event) => {
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.setPointerCapture(event.pointerId);
if (pointers.size > 1) {
gestureMoved = true;
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
if (Math.abs(dx) + Math.abs(dy) > 2) {
gestureMoved = true;
disableAuto();
}
orbit.targetPhi -= dx * 0.006;
orbit.targetTheta = THREE.MathUtils.clamp(
orbit.targetTheta + dy * 0.005,
MIN_INCLINATION,
MAX_INCLINATION
);
} else if (pointers.size === 2) {
gestureMoved = true;
disableAuto();
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && distance > 0) {
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * (pinchDistance / distance),
38,
108
);
}
pinchDistance = distance;
}
});
canvas.addEventListener("pointerup", (event) => {
const canSelect =
pointers.size === 1 &&
!gestureMoved &&
event.button === 0;
pointers.delete(event.pointerId);
if (canSelect) {
pickVoxel(event.clientX, event.clientY);
}
if (pointers.size < 2) {
pinchDistance = 0;
}
if (pointers.size === 0) {
gestureMoved = false;
}
});
canvas.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
if (pointers.size === 0) {
gestureMoved = false;
pinchDistance = 0;
}
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
disableAuto();
orbit.targetRadius = THREE.MathUtils.clamp(
orbit.targetRadius * Math.exp(event.deltaY * 0.001),
38,
108
);
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target.tagName === "BUTTON") {
return;
}
switch (event.code) {
case "KeyF":
setPreset("face");
break;
case "KeyE":
setPreset("edge");
break;
case "KeyO":
setPreset("oblique");
break;
case "Space":
event.preventDefault();
ui.autoBtn.click();
break;
case "ArrowLeft":
disableAuto();
orbit.targetPhi -= 0.08;
break;
case "ArrowRight":
disableAuto();
orbit.targetPhi += 0.08;
break;
case "ArrowUp":
disableAuto();
orbit.targetTheta = Math.max(
MIN_INCLINATION,
orbit.targetTheta - 0.04
);
break;
case "ArrowDown":
disableAuto();
orbit.targetTheta = Math.min(
MAX_INCLINATION,
orbit.targetTheta + 0.04
);
break;
case "Equal":
case "NumpadAdd":
disableAuto();
orbit.targetRadius = Math.max(38, orbit.targetRadius - 3);
break;
case "Minus":
case "NumpadSubtract":
disableAuto();
orbit.targetRadius = Math.min(108, orbit.targetRadius + 3);
break;
}
});
// -------------------------------------------------------------------------
// Camera and UI updates
// -------------------------------------------------------------------------
const cameraDirection = new THREE.Vector3();
const worldUp = new THREE.Vector3(0, 1, 0);
const cameraUp = new THREE.Vector3();
function updateCamera() {
const sinTheta = Math.sin(orbit.theta);
const cosTheta = Math.cos(orbit.theta);
const sinPhi = Math.sin(orbit.phi);
const cosPhi = Math.cos(orbit.phi);
cameraDirection.set(
sinTheta * sinPhi,
cosTheta,
sinTheta * cosPhi
);
// Keep the full disk visible on portrait displays.
const fitScale = Math.max(1, 1.15 / camera.aspect);
const distance = orbit.radius * fitScale;
camera.position.copy(cameraDirection).multiplyScalar(distance);
// Project world +Y into the image plane. The tiny-inclination fallback
// avoids the singularity of looking exactly along +Y.
if (orbit.theta < 0.00001) {
cameraUp.set(-sinPhi, 0, -cosPhi);
} else {
cameraUp
.copy(worldUp)
.addScaledVector(cameraDirection, -cameraDirection.y)
.normalize();
}
camera.up.copy(cameraUp);
camera.lookAt(0, 0, 0);
camera.updateMatrixWorld();
}
let lastUIUpdate = 0;
function updateReadouts(now) {
if (now - lastUIUpdate < 45) return;
lastUIUpdate = now;
const inclinationDegrees = orbit.theta * 180 / Math.PI;
const axisRatio = Math.cos(orbit.theta);
ui.inclination.textContent = `${Math.round(inclinationDegrees)}°`;
ui.ratio.textContent = axisRatio.toFixed(3);
ui.ratioFill.style.transform = `scaleX(${axisRatio})`;
ui.gaugeRatio.textContent = axisRatio.toFixed(3);
const gaugeRadius = Math.max(2, 76 * axisRatio);
ui.gaugeDisk.setAttribute("ry", gaugeRadius.toFixed(2));
ui.gaugeCore.setAttribute(
"ry",
Math.max(1.3, 15 * axisRatio).toFixed(2)
);
const azimuth = ((orbit.phi * 180 / Math.PI) % 360 + 360) % 360;
ui.azimuth.textContent =
`${String(Math.round(azimuth)).padStart(3, "0")}°`;
if (orbit.auto) {
ui.mode.textContent = "AUTO SWEEP";
} else if (inclinationDegrees < 3) {
ui.mode.textContent = "FACE-ON";
} else if (inclinationDegrees > 87) {
ui.mode.textContent = "EDGE-ON";
} else {
ui.mode.textContent = "OBLIQUE";
}
}
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", onResize);
let previousTime = performance.now();
function animate(now) {
requestAnimationFrame(animate);
const dt = THREE.MathUtils.clamp((now - previousTime) / 1000, 0, 0.05);
previousTime = now;
if (orbit.auto) {
// A 24-second cycle travels face-on → edge-on → face-on while the
// camera also advances in azimuth around the stationary galaxy.
orbit.phase = (orbit.phase + dt / 24) % 1;
orbit.targetTheta =
(Math.PI / 2) *
0.5 *
(1 - Math.cos(orbit.phase * TAU));
orbit.targetPhi += dt * 0.045;
}
const smoothing = 1 - Math.exp(-dt * 8);
orbit.theta +=
(orbit.targetTheta - orbit.theta) * smoothing;
orbit.phi +=
(orbit.targetPhi - orbit.phi) * smoothing;
orbit.radius +=
(orbit.targetRadius - orbit.radius) * smoothing;
// Only the camera changes. Star and dust instance matrices remain static.
updateCamera();
updateReadouts(now);
renderer.render(scene, camera);
}
updateButtons();
updateCamera();
renderer.render(scene, camera);
requestAnimationFrame(() => {
loader.classList.add("done");
setTimeout(() => loader.remove(), 500);
});
requestAnimationFrame(animate);
</script>
</body>
</html>
```