Shaders · Chapter 1 of 11
Shaders: What Is a Shader?
Written for version 2.7.0
A shader is a tiny recipe run for every pixel at once. See one built step by step: a flat colour, a colour that depends on place, and one that depends on time.
A shader is a tiny recipe that answers one question: what colour is this spot? The recipe is run once for every spot of a picture, all at the same moment, and the answers together are the picture. This chapter is for someone who has never heard the word. It builds the idea one step at a time, and every picture on the page is a real shader running as you read.
A picture is a grid of pixels
Look closely at any screen and the picture breaks up into tiny squares. Each square is a pixel, and each pixel shows exactly one colour. When you draw, you colour those pixels a few at a time, wherever your hand goes. A shader works the other way round. It does not go anywhere. It hands the same recipe to every pixel and each pixel works out its own colour.
Nothing is stored. A shader keeps no image of what it made. It is a set of steps that makes the picture afresh every time the screen needs it, many times a second. Change one number in the recipe and the next picture is different everywhere at once.
One colour for every pixel
The simplest recipe is one line long: whoever asks, the answer is orange. Every pixel gets the same colour, so the picture is a flat square.
The two boxes are nodes. A node does one small job: Color holds a colour, and Surface Output is the end of the recipe, the place where the answer is handed over. The line between them is a wire, and it carries the colour from one node to the next. The next chapter goes through nodes and wires properly. For now, read each picture from left to right.
Where am I? U and V
A flat colour is not much of a picture. To make anything more interesting, the recipe has to know which pixel is asking. Every pixel is told its own position as two numbers, called U and V.
- U says how far across the pixel is. It is 0 at the left edge and 1 at the right edge.
- V says how far up it is. It is 0 at the bottom edge and 1 at the top edge.
- Together they are written UV. The middle of the square is U 0.5, V 0.5.
On the square used in this path, and on each face of a cube in the Rig Editor, V counts upwards from the bottom. On a render layer it counts downwards from the top, and on a sphere it runs from the top pole down. On a model you made or imported, U and V follow that model's own UV layout. If a pattern comes out upside down, this is why.
A colour that depends on place
Now the recipe can use the position. Take U, the number for how far across, and use it as the brightness. Pixels on the left get a number near 0 and come out dark. Pixels on the right get a number near 1 and come out bright. No pixel was told to make a gradient. Each one only reported its own U.
A number used this way is called a value, and the span it can cover, here 0 to 1, is its range. 0 means none, 1 means all, and 0.25 means a quarter of the way. Shaders are mostly about values: making them, bending them and combining them. Colour comes last, when a value is finally turned into something you can see.
A colour that depends on time
Position is one thing a pixel is told. The time is another. Kilona hands every pixel the same clock, a number of seconds that keeps counting up, through a node called Elapsed Time. Put the clock into the recipe and the picture changes by itself.
Use place and time together and things start to move. Below, each pixel adds the clock to its own position, then keeps only the part after the decimal point. The ramp slides across the square for ever, yet no pixel moves. Each one simply gives a different answer as the clock runs.
Why shaders are fast, and why they feel strange
Every pixel runs the recipe alone. It cannot see what its neighbours answered, and it does not remember what it answered last time. All it has is its own position and whatever numbers the recipe is given, such as the clock or a colour you chose.
- Because no pixel waits for another, the graphics chip in your iPad can work on enormous numbers of them at once. That is what makes shaders fast enough to redo the whole picture many times a second.
- Because no pixel can see another, you cannot say draw a line from here to there. You say instead: a pixel is on the line if its position passes this test.
- Because nothing is remembered, a shader never builds up a picture stroke by stroke. Every picture is worked out from nothing.
This is the part that takes getting used to. With a brush you put colour where you want it. With a shader you write one rule that every pixel checks for itself. Shapes, patterns and movement all come from choosing the right rule, and the rest of this path is a collection of them.
Where shaders live in Kilona
- A material on a 3D model. In the Rig Editor every material is a shader, and it decides the colour of each pixel of the model it is on. This is where most of this path happens.
- Shader FX on a render layer. A layer in the Rig Editor can switch on Shader FX under Post FX Passes, and then a shader runs over that layer's finished picture, pixel by pixel.
Clip effects in the Timeline Editor are built in the same kind of graph, but from ready-made filter nodes such as blurs and colour changes, not from the pixel recipes taught here.
Put effects on a single clip by wiring filter nodes together.
Nodes, not code
In most tools a shader is written as lines of code. In Kilona you build one by placing nodes and joining them with wires, as in the pictures above. Each node is one step of the recipe, the wires say which step feeds which, and the result shows on the model as you work. You will not type a single line of code in this path.
What this path teaches
The next chapter, linked at the foot of this page, shows where the shader graph is and how to work in it. After that the path goes idea by idea:
Bend a value from 0 to 1 into edges, bands, curves, waves and repeats.
Turn values into colour: mix, ramp and adjust.
Circles, boxes and lines made from position alone.
Repeat, tile and turn a shape into a pattern.
Randomness that looks natural: clouds, grain and cells.
Make a shader move, pulse and scroll.
Shaders on a 3D model: light, roughness, metal and glow.
Shaders that treat a finished render layer.
Whole shaders to build from start to finish.
The order of ideas in this path follows The Book of Shaders by Patricio Gonzalez Vivo and Jen Lowe, a free online book that teaches the same ideas in code. Everything here is written afresh for Kilona's nodes. If you ever want to see how the same recipes look as code, it is a good place to go.
The free online book whose order of ideas this path follows.
Where the graph is in the Rig Editor, and how to add, wire and change nodes.
Tell us which idea lost you and what you expected the picture to show. If a picture on this page did not move or looked wrong, tell us which one.