LearnFirst steps
Lesson 1 of 30 · 30 min
Your first shader
Learn that a shader is a function that runs for every pixel at once, and paint your first color
In this lesson you will
- Understand that a shader is a small function that decides the color of one pixel
- Choose a color (red, green, blue, opacity) with the
vec4thatrenderreturns - Write
float,vec3andvec4, and know why we write1.0instead of1 - Read a compile error and fix it yourself
01What is a shader?
A screen is made of tiny dots called pixels. A Full HD screen has about two million of them. A shader is a small program that decides the color of a single pixel.
How does a program for one pixel paint the whole screen? The GPU (the graphics processor) runs the same program for every pixel at the same time — and does it again about 60 times per second.
On this site you write that small program as a function named render, in a language called GLSL. It always looks like this:
1
2
3
4
vec4 render(vec2 uv, vec2 p) {
// the math that decides this pixel's color goes here
return vec4(1.0, 0.5, 0.0, 1.0);
}
- The leading
vec4is the type of what the function returns. A color is avec4: four numbers in a row uvandptell you where the pixel being colored is. We peek at them later in this lesson and dig in next time- Everything from
//to the end of the line is a comment. The computer ignores it; it is a place for notes returnhands back the color you chose. Every statement ends with;
02Your first color
The code below returns the same orange for every pixel. Since every pixel gives the same answer, the whole screen turns orange. Change the numbers and the preview updates right away.
1
2
3
1.0 becomes 0.0? What if the third 0.0 becomes 1.0?The four numbers in vec4 are red, green, blue and opacity, in that order (RGBA for short). Each goes from 0.0 to 1.0: 0.0 means none and 1.0 means full. Note that this is not the 0–255 range you may know from image editors.
vec4(1.0, 0.0, 0.0, 1.0)… redvec4(1.0, 1.0, 0.0, 1.0)… yellow (red + green)vec4(0.0, 0.0, 0.0, 1.0)… blackvec4(1.0, 1.0, 1.0, 1.0)… whitevec4(0.5, 0.5, 0.5, 1.0)… gray (equal amounts make gray)
Tip
Light mixes the opposite way from paint: adding makes it brighter. Red light plus green light is yellow, and all three together make white.
Keep the fourth number, opacity, at 1.0 for works on this site. A work is drawn as a single picture, so there is nothing underneath to show through.
031 and 1.0 are different
GLSL is strict about whole numbers (int) versus decimal numbers (float). Almost everything in color and position math is a decimal, so make a habit of always writing the decimal point: 1.0, 0.5.
1
2
3
4
float a = 1.0; // OK
float b = 1; // error: putting an int into a float
float c = 0.5 * 2.0; // OK
float d = 0.5 * 2; // error: float times int
Watch out
As an exception, integers inside a constructor like vec4(1, 0, 0, 1) are allowed — they are converted for you. Still, writing 1.0 everywhere keeps you from tripping elsewhere.
04Giving values names
Instead of writing numbers inline, you can store them in named variables. The form is type name = value;.
1
2
3
4
5
6
7
r, g and b to find a color you likeTwo types appear here. float is one decimal number; vec3 is a group of three. The final vec4(color, 1.0) builds a vec4 by appending a fourth number, 1.0, to the three in color.
float… one number: a brightness, a time, a radiusvec2… two numbers: a position on the screen (x, y)vec3… three numbers: a color (r, g, b) or a 3D positionvec4… four numbers: a color with opacity
Passing a single number, as in vec3(0.5), fills all three with it — handy for grays.
05Doing math with colors
Adding or multiplying two vec3s works component by component. Multiplying by a single float scales all three. That alone lets you darken, brighten and combine colors.
1
2
3
4
5
6
7
8
9
// at the start of a line to switch it on. Try them one at a timeTip
On screen, values above 1.0 are clipped to 1.0 and values below 0.0 to 0.0. The red in orange + sky is 1.3, but it shows as 1.0.
06A different color in each place
So far every pixel has returned the same color. Here is where shaders get interesting. render receives uv, the position of the pixel being colored. uv.x is 0.0 at the left edge and 1.0 at the right edge.
1
2
3
uv.x becomes uv.y? What about vec4(uv.x, uv.y, 0.0, 1.0)?It is a one-line program, and every pixel runs the same thing. But uv.x differs from pixel to pixel, so you get a gradient from black on the left to red on the right.
This is the heart of thinking in shaders. You never command "draw a circle" or "draw a line". Instead, every pixel answers the question "what color am I?" on its own. The rest of this course is about clever ways to answer it.
07Changing color over time
One more value is available: time, the number of seconds since the work started. sin(time) swings between -1.0 and 1.0, so we map it to 0.0–1.0 with * 0.5 + 0.5 before using it as a color.
1
2
3
4
sin(time) to sin(time * 3.0) to speed it upValues like time, which are the same for every pixel within a frame, are called uniforms. This site gives you four: time, resolution, mouse and attention. We cover them in Time and motion and Touch and response.
08Reading errors
If there is a mistake, a compile error appears instead of the preview. Shaders are translated (compiled) into GPU instructions before they run, and translation stops at anything it cannot understand. The code below has one deliberate mistake.
1
2
3
4
; at the end of line 2 to fix itThe message looks like ERROR: 3: 'return' : syntax error. The 3 is the line number and the rest is the reason: "the grammar breaks at return on line 3". But the real mistake is on line 2 — without the ;, lines 2 and 3 run together into one statement.
Tip
When you see an error, look at the reported line and the one before it. The usual suspects are a missing ;, a missing decimal point in 1.0, an unclosed bracket, and a misspelled name.
09Exercises
Use what you have learned to write some code yourself. If you get stuck, open the hints one at a time. Save the example solution for checking at the end.
Pale purple
Make the whole screen a pale purple, like lavender.
1
2
3
A bottom-to-top gradient
Make a gradient that is black at the bottom and green at the top.
1
2
3
Fix the errors
This code has two mistakes. Use the error messages to fix them and get a yellowish screen.
1
2
3
4
5
10Recap
- A shader is a function that decides one pixel's color; the GPU runs it for every pixel at once
- A color is
vec4(red, green, blue, opacity), each from 0.0 to 1.0 - Write numbers with a decimal point:
1.0 uvmakes the color differ per pixel;timemakes it change over time- On an error, check the reported line and the line before it
Next time we look closely at the two coordinates, uv and p, so you can work freely with positions on the screen.
Finished lessons get a mark in the course contents