Shaders From Zero · Episode 13
Procedural Galaxy
Procedural Galaxy is Episode 13 of Shaders From Zero, a MasterAllArts free interactive lesson for artists learning creative 3D on the web.
Part of the MasterAllArts Creative 3D Roadmap .
A galaxy is 80,000 points arranged in a spiral, colored by distance from the
center, and rotated by a vertex shader. If you've followed Season 1, you have every
piece — noise, color palettes, vertex math. The only new thing in this episode is
THREE.Points instead of THREE.Mesh.
It's also the most-shared shader on Twitter. The visual payoff per line of code is unreasonable.
THREE.Points — the basics
A BufferGeometry with a position attribute, plus a
ShaderMaterial, wrapped in new THREE.Points(geometry, material).
Three changes from a Mesh:
- The vertex shader sets both
gl_Position(where on screen) andgl_PointSize(how big in pixels). - The fragment shader has access to
gl_PointCoord— avec2in[0,1], the coord within the point sprite. - Material flags must include
transparent: true,depthWrite: false, andblending: THREE.AdditiveBlending. Additive blending is what makes overlapping particles ADD brightness — without it, dense clusters look like solid spots, not a glowing core.
Drawing a soft disc
The simplest particle look. Use gl_PointCoord as the local UV of
the point sprite, compute distance from center, fade out toward the edge.
void main() {
float d = length(gl_PointCoord - 0.5);
float alpha = smoothstep(0.5, 0.0, d);
gl_FragColor = vec4(vColor, alpha);
}
Combined with additive blending, overlapping particles add brightness — the dense core glows automatically.
Layer 1 — Random scatter in a disc
Generate positions on the JS side. The trick for uniform area distribution:
use sqrt(random) for the radius. (If you used random
directly, points would cluster near the center.)
const count = 80000;
const positions = new Float32Array(count * 3);
for (let i = 0; i < count; i++) {
const r = Math.sqrt(Math.random()) * 3.0;
const a = Math.random() * Math.PI * 2;
positions[i * 3 + 0] = r * Math.cos(a);
positions[i * 3 + 1] = (Math.random() - 0.5) * 0.2; // tiny Y jitter
positions[i * 3 + 2] = r * Math.sin(a);
}
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
Stage 1 in the demo. Featureless cloud. No structure.
Layer 2 — Spiral arrangement
Take the same disc, but add a radius-dependent angle offset and a branch index. Three branches → 3-arm spiral. The angle offset means outer particles get rotated more than inner ones — that's what makes the spiral curl.
const branchCount = 3;
for (let i = 0; i < count; i++) {
const r = Math.sqrt(Math.random()) * 3.0;
const branch = i % branchCount;
const branchAngle = (branch / branchCount) * Math.PI * 2;
const spinAngle = r * 1.2;
// Gaussian-ish jitter scales with radius (outer arms are noisier)
const jitter = (Math.random() + Math.random() + Math.random() - 1.5) * 0.3 * r;
const a = branchAngle + spinAngle + jitter;
positions[i * 3 + 0] = r * Math.cos(a);
positions[i * 3 + 1] = (Math.random() - 0.5) * 0.2;
positions[i * 3 + 2] = r * Math.sin(a);
}
Layer 3 — Color ramp by distance
Vertex shader computes color from the IQ cosine palette (Episode 04), using the point's distance from origin as the parameter. Hot center, cool outer arms.
// VERTEX
varying vec3 vColor;
void main() {
float r = length(position);
float t = r / 3.0;
vColor = vec3(0.5) + vec3(0.5) * cos(6.2832 * (t + vec3(0.0, 0.33, 0.67)));
// gl_Position + gl_PointSize as usual
}
The palette knobs are taste. Magenta-to-cyan looks like the Milky Way reference photos. Warm-to-cool looks like Andromeda.
Layer 4 — GPU differential rotation
Don't rotate the whole Points object with points.rotation.y
— that's a single CPU value, uniform across all particles, gives a rigid spin.
Instead, rotate each particle in the vertex shader, with rotation
speed inversely proportional to radius.
// VERTEX
uniform float uTime;
void main() {
float r = length(position.xz);
float a = atan(position.z, position.x);
a += uTime * (1.0 / (r + 1.0)); // inner faster
vec3 p = vec3(cos(a) * r, position.y, sin(a) * r);
// ... use p for gl_Position
}
The 1.0 / (r + 1.0) is the key — small r gives a large
rotation speed; large r gives a small one. The galaxy now spins
DIFFERENTIALLY, like a real one.
Layer 5 — Brightness and size variation
Vary gl_PointSize per-particle with a hash. Some particles are
bright pinpoints; some are dim dust. Breaks up uniformity.
// VERTEX
float h = fract(sin(dot(position.xz, vec2(127.1, 311.7))) * 43758.5453);
gl_PointSize = (2.0 + 6.0 * h) * uPixelRatio;
vBrightness = 0.5 + 0.5 * h;
// FRAGMENT
gl_FragColor = vec4(vColor * vBrightness, alpha);
Pass uPixelRatio so particle sizes are stable across Retina and
non-Retina displays.
Common pitfalls
- Forgot
transparent: true. You see opaque squares. - Forgot additive blending. The dense core looks like a solid
disc, not a glow. Set
blending: THREE.AdditiveBlending. gl_PointSizenot scaled by DPR. Particles look tiny on Retina screens. PassuPixelRatio.- Linear instead of
sqrtradius distribution. Particles cluster too tightly at the center. Thesqrttrick gives uniform area distribution. - Rotated the
Pointsobject instead of the vertex shader. Lose differential rotation. Inner and outer particles spin at the same angular velocity, looks like a rigid disc.
Exercises
- Color by spiral arm (branch index) instead of distance — pass a
vertexColorattribute per-particle, set once in JS. - Add a vertical bulge at the center: lift Y by
exp(-r * 2.0) * 0.5. Now the galaxy has a thick core. - Render two galaxies in the same scene with different palettes and rotation
speeds. Position them at
(±3, 0, 0)with slight Y-rotation offset.
What's next
Episode 14 — Portal. Back to a flat plane, SDF disc, rotating noise, IQ palette, and animated rim shimmer.