Lesson 5 of 10 · GLSL shaders for beginners
The gradient
A position controls a color. There you go, you are an artist.
In “Coordinates” every frag got its position p, from 0 to 1. Here position becomes color. And the other way around: the math you apply to the position shows up as an image. The rest of Start Here comes from this idea.
Position becomes image. Welcome to shaders.
A gradient is just that: you take p.x (the horizontal position) and use it as a color channel. The frag on the left has p.x = 0, the one on the right has p.x = 1. Connect it to red, and you get a gradient from black to red.
mix()The function
mix(A, B, t) "blends" two things. When t is 0, you get pure A. When it is 1, you get pure B. When it is 0.5, you get half and half. It is like a slider.Reading the code
You know these:
Next: in “The center” 0 moves to the middle of the screen.
vec3 a and vec3 b: the colors of the two edges, a pink and a green.float t = p.x * p.x;: the position squared. Halfway across the screen it is 0.25, so the pink covers most of the left.mix(a, b, t): at t = 0 it gives the pink, at t = 1 the green.You know these:
p (04 · Coordinates), hueShift (03 · Mixing).Next: in “The center” 0 moves to the middle of the screen.
Change
p.x to p.y and the gradient goes vertical. Change the colors vec3(0.95, 0.15, 0.55) and vec3(0.1, 0.85, 0.5) to anything you like. Change p.x * p.x to p.x and the transition becomes linear instead of curved.The code
void main(){
vec2 p = gl_FragCoord.xy / R;
vec3 a = vec3(0.95, 0.15, 0.55);
vec3 b = vec3(0.1, 0.85, 0.5);
float t = p.x * p.x;
vec3 col = mix(a, b, t);
col = hueShift(col, 0.0);
gl_FragColor = vec4(col, 1.);
}