Black hole
glint · Examples · Tutorials · Gallery
A black hole in front of a starfield: light passing close is bent (so the stars behind smear into rings), a shadow where nothing gets out, and a glowing accretion disc, brighter on the side coming towards you.

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.
fragment shader
#version 300 es
precision highp float;
uniform float time;
uniform vec2 resolution;
in vec2 v_texcoord;
out vec4 fragColor;
float hash(float n) { return fract(sin(n * 127.1) * 43758.5453); }
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
vec2 hash2(float n) { return fract(sin(vec2(n * 127.1, n * 269.5)) * 43758.5453); }
mat2 rot(float a) { float c = cos(a), s = sin(a); return mat2(c, s, -s, c); }
vec3 pal(float t) { return 0.5 + 0.5 * cos(6.28318 * (t + vec3(0.0, 0.33, 0.67))); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
f = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1.0, 0.0)), f.x), mix(hash(i + vec2(0.0, 1.0)), hash(i + vec2(1.0, 1.0)), f.x), f.y);
}
float fbm(vec2 p) {
float v = 0.0, a = 0.5;
for (int i = 0; i < 5; i++) { v += a * noise(p); p = p * 2.03 + 17.0; a *= 0.5; }
return v;
}
vec3 stars(vec2 p) {
vec2 id = floor(p * 90.0);
float s = step(0.992, hash(id)) * hash(id + 1.0);
vec3 nebula = vec3(0.25, 0.1, 0.35) * fbm(p * 3.0) + vec3(0.05, 0.1, 0.2) * fbm(p * 5.0 + 7.0);
return nebula * 0.6 + vec3(s);
}
// The disc, seen from slightly above: a flattened ring, turning, hot on the approaching side.
vec3 disc(vec2 p, float lens) {
vec2 q = vec2(p.x, p.y / 0.22);
float r = length(q);
if (r < 0.2 || r > 0.55) return vec3(0.0);
float a = atan(q.y, q.x);
float swirl = fbm(vec2(a * 3.0 - time * 1.5 / r, r * 12.0));
float heat = smoothstep(0.55, 0.2, r) * (0.5 + swirl);
float doppler = 1.0 + 0.7 * sin(a); // the side moving towards us is brighter
return mix(vec3(1.0, 0.35, 0.1), vec3(1.0, 0.9, 0.7), heat) * heat * doppler * lens;
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * resolution) / resolution.y;
float r = length(uv), rs = 0.12;
// Lensing: the background seen here comes from further out, pulled round the hole.
vec2 bent = uv - normalize(uv) * (rs * rs * 2.5 / max(r, 0.001));
vec3 col = stars(bent + vec2(time * 0.005, 0.0));
// The far side of the disc, bent up over the top (and under) as a ring.
float ringR = abs(r - rs * 1.6);
col += vec3(1.0, 0.6, 0.3) * 0.015 / (ringR + 0.015) * smoothstep(rs * 0.9, rs * 1.3, r) * 0.6;
col *= smoothstep(rs, rs * 1.05, r); // the shadow
// The near side of the disc passes in front.
vec3 d = disc(uv, 1.0);
col = uv.y < 0.0 || r > rs * 1.3 ? col + d : col + d * smoothstep(rs * 1.1, rs * 1.4, r);
fragColor = vec4(1.0 - exp(-col * 1.4), 1.0);
}More More GLSL examples
Maurer rose · Spirograph · Field lines · Tesseract · Low poly · 10 PRINT · Invaders · Gears