Lesson 10 of 10 · GLSL shaders for beginners

The tunnel

Everything together. 1/distance. Everything you learned in one output.

This is the last lesson of Start Here. It brings the nine before it together in one shader: centered coordinates, time, waves, circles, colors that change. Next comes “Patterns”, where every shape is a distance equation.

The only line you haven’t seen before is inv = 1. / (d + 0.12). Near the center d is small and inv grows very fast; far out, it barely changes. That is why the rings crowd toward the center and it looks like you are looking down a tunnel. The 0.12 avoids dividing by zero right in the middle.

This is your first piece. Concentric rings that pulse, made with everything you learned in these lessons.

1/distance. Let's travel.

Reading the codesin(inv * 3.5 - T * 3.): the wave from “The wave”, now on inv. 3.5 = how many rings, 3 = how fast you travel.
mix(bg, ring, tunnel * (1. - d * 0.4)): paints the rings over the background, fainter toward the edges.
pow(tunnel, 4.): only the crest of each ring glows.
You know these: p (06 · The center), length(p) and exp() (09 · The glow), mix() (05 · The gradient), hueShift (03 · Mixing).
TryChange 3.5 (tunnel frequency) to 8. for thin rings. To 1.5 for wide rings. Change T * 3. (tunnel speed) to T * 1. and the ride goes hypnotic. Change both colors. This is your piece now. Make it yours.

The code

void main(){
  vec2 p = (2.*gl_FragCoord.xy - R) / min(R.x, R.y);
  float d = length(p);
  float inv = 1. / (d + 0.12);
  float tunnel = sin(inv * 3.5 - T * 3.) * .5 + .5;
  float glow = exp(-d * 2.5) * 0.6;
  vec3 bg = vec3(0.015, 0.008, 0.05);
  vec3 ring = vec3(0.75, 0.2, 0.9);
  vec3 core = vec3(0.95, 0.85, 1.0);
  vec3 col = mix(bg, ring, tunnel * (1. - d * 0.4));
  col += core * glow;
  col += ring * pow(tunnel, 4.) * 0.25;
  col *= 0.9 + 0.1 * tunnel;
  col = hueShift(col, 0.0);
  gl_FragColor = vec4(col, 1.);
}