Lesson 9 of 10 · GLSL for beginners
The glow
Distance to center. exp() does the rest.
To make a circle you need to know: how far is each frag from the center?
Far from center: darkness. Close: light explodes.
The function length(p) tells you. If the frag is at the center, it returns 0. If it is in a corner, it returns something like 1.4.
If you take that distance and say "frags closer than 0.5 are white, the rest are black", you have a perfect circle.
smoothstep()Makes a smooth transition between two values.
smoothstep(0.5, 0.45, d) says: when d is 0.5, give 0. When it is 0.45, give 1. And in between, a smooth curve. Without this, the circle edge looks jagged. With it, buttery smooth.Change
0.35 to 0.6 and the circle grows. To 0.2 and it shrinks. Change T * 1.2 (the pulse) to T * 5. and the circle beats nervous. To T * 0.3 and it breathes slow.The code
void main(){
vec2 p = (2.*gl_FragCoord.xy - R) / min(R.x, R.y);
float d = length(p);
float r = 0.35 + 0.05 * sin(T * 1.2);
float circ = smoothstep(r, r - 0.02, d);
float glow = exp(-d * 2.5) * 0.7;
float outerGlow = exp(-d * 1.2) * 0.15;
vec3 core = vec3(0.3, 0.95, 0.9);
vec3 halo = vec3(0.15, 0.65, 0.6);
vec3 outer = vec3(0.08, 0.25, 0.25);
vec3 bg = vec3(0.015, 0.04, 0.05);
vec3 col = bg;
col += outer * outerGlow;
col += halo * glow;
col = mix(col, core, circ);
gl_FragColor = vec4(col, 1.);
}