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.

The same picture made two ways. A hand colours one spot after another. A shader asks every spot at the same moment, so it has made the whole picture many times over before the hand has finished once.
A shader is a recipe, not a picture

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 whole recipe: a Color wired into Surface Output. The ring shows that it does not matter which spot asks. Pick another colour and every pixel changes together.

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.
The UV square. Here U is shown as red and V as green, so you can see both at once: black where both are 0, red where only U is high, green where only V is high, yellow where both are 1.
Which way is up?

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.

UV Coord gives the spot, Split Vector separates it into X (that is U) and Y (that is V), and X goes into Color: dark on the left, bright on the right. Move the wire to Y and the gradient runs from bottom to top.

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.

Elapsed Time counts the seconds. Ping-Pong folds that ever growing number into one that climbs to Scale and falls back to 0, and the whole square brightens and darkens. A smaller Scale gives a shorter, dimmer swing.

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.

Gradient gives a number for where the pixel is, Add puts the clock on top, and Fract keeps the part after the decimal point so the number wraps round. Two spots asked at the same instant give different answers.

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.

Nobody drew this circle. Gradient, set to Spherical, gives each pixel its distance from the middle, and Step answers 1 where B is at least A and 0 where it is not. The edge is simply where the answers change.
  • 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.
You do not draw. You describe

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

The Rig Editor. A material is a shader worn by a model, and with the material selected its recipe, the shader graph, sits in the pane under the viewport. A render layer can run a shader over its finished picture too.
  • 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.

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:

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.

Something unclear?

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.

Send Feedback