Lesson 3 of 10 · GLSL shaders for beginners

Mixing

Three operations. Total control.

In “The three numbers” you built a color from three values, R, G and B. Here you do math with them. Color numbers can be added, subtracted, multiplied, and divided. Just like the numbers you already know.

If you multiply a color by 0.5, it gets darker (half the light). Multiply by 2. and it gets brighter. Subtract 0.3 and you take a little away from each channel.

Math you can see.

vec3A vec3 is like a pack with three numbers inside. We use it to hold a color (R, G, B). When you add another pack to it, the GPU operates all three at once.
Reading the codevec3 base = vec3(0.9, 0.3, 0.3);: a coral, kept in one pack of three.
base * 0.85: all three channels at 85%. Darker, same hue.
pow(col, vec3(0.95)): each channel to the power of 0.95. Below 1 lightens, above 1 deepens. It is the Intensity slider.
hueShift(col, 0.0): turns the hue around the rainbow. At 0 nothing changes and at 0.5 it reaches the opposite color. It is the Color slider.
You know these: vec4(col, 1.) (02 · The three numbers).
Next: in “Coordinates” every frag gets a different color.
TryChange 0.85 to 0.3 to see the color darker. To 1.5 to see it glow. Change 0.95 (the exponent) to 0.4 and the color gets lighter and washed out. At 2. it turns deeper and darker.

The code

void main(){
  vec3 base = vec3(0.9, 0.3, 0.3);
  vec3 col = base * 0.85;
  col = pow(col, vec3(0.95));
  col = hueShift(col, 0.0);
  gl_FragColor = vec4(col, 1.);
}