Galaxy

glint · Examples · Tutorials · Gallery

150,000 stars placed by the vertex shader alone: logarithmic spiral arms, differential rotation, a dusty core, drawn additively with a perspective camera.

Audio-reactive: it moves with music from a microphone, line-in or a file.

Galaxy: an audio-reactive GLSL shader

An example in glint, a free live shader editor in the browser: the GLSL code floats over the running picture and recompiles as you type.

The code

One pass, in GLSL.

vertex shader

#version 300 es
uniform mat4 mvp;
uniform float time;
uniform int vertexCount;
uniform vec3 spectrum;
uniform float beat;
uniform float arms;
uniform float twist;

out vec3 v_color;
out float v_size;

float hash(float n) { return fract(sin(n) * 43758.5453123); }

void main() {
    float id = float(gl_VertexID);
    float u = hash(id * 1.37), v = hash(id * 7.13), w = hash(id * 3.71);
    // Radius: dense core, long tail.
    float r = pow(u, 1.8) * 1.6;
    // Pick an arm and wind it into a logarithmic spiral; scatter across it more at larger radius.
    float arm = floor(v * arms) / arms * 6.2831;
    float spread = (w - 0.5) * (0.25 + 0.6 * r) * (hash(id * 9.1) < 0.2 ? 3.0 : 1.0);
    float angle = arm + twist * log(r + 0.05) + spread;
    // Differential rotation: inner stars orbit faster.
    angle += time * 0.25 / (r + 0.3);
    float h = (hash(id * 2.9) - 0.5) * 0.08 * exp(-r * 1.5);
    vec3 p = vec3(cos(angle) * r, h, sin(angle) * r);
    gl_Position = mvp * vec4(p, 1.0);
    float core = exp(-r * 3.0);
    v_color = mix(vec3(0.55, 0.65, 1.0), vec3(1.0, 0.8, 0.55), core) * (0.35 + hash(id * 5.5));
    v_color += vec3(1.0, 0.4, 0.6) * step(0.995, hash(id * 11.3));    // rare pink nebula stars
    v_color *= 1.0 + spectrum.x * 1.5 + beat;
    gl_PointSize = (1.0 + 2.5 * step(0.98, w)) * (1.0 + core);
    v_size = gl_PointSize;
}

fragment shader

#version 300 es
precision highp float;
in vec3 v_color;
in float v_size;
out vec4 fragColor;
void main() {
    float d = length(gl_PointCoord - 0.5);
    fragColor = vec4(v_color * smoothstep(0.5, 0.0, d) * 0.35, 1.0);
}

More Showpieces examples

Truchet weave · Voronoi crystals · Domain warping · Neon city · Fluid · GPU particles · Physarum · SmoothLife