Techniques
GLSL is the language shaders are written in. A shader is a small program that runs on the graphics card and draws a picture. Each Technique below is one GLSL skill you can use while decoding: turning a scrambled picture back into the original. A Technique explains the idea, gives the core snippet to copy, and runs it live on an ordinary photo, so you can change the code and see what happens. Nothing on this page is scrambled.
In these editors the photo is called u_picture. On a Milestone page, the picture you decode is called u_encodedAsset instead. Change the name when you copy a snippet.
Drawing a texture
The canvas is the picture area at the top of each editor. A fragment shader is a shader that runs once for every pixel of the canvas, and sets that pixel's colour by assigning it to gl_FragColor. A texture is a picture a shader can read colours from.
To draw a texture, each pixel works out where it is on the canvas, then reads the colour at the same place in the texture. You have three things to work with:
gl_FragCoordis built in. Itsxyis the position of the pixel being drawn, in device pixels, with (0, 0) at the bottom left. A device pixel is one dot on the screen. On many screens there are two or more for every pixel the page asks for.u_canvasResolutionis the size of the canvas in device pixels. It changes with the screen and the page width, so don't assume a size.texture2D(sampler, coord)reads a colour from a texture.sampleris the texture to read, such asu_picture.coordis a texture coordinate: (0, 0) is the bottom-left corner of the texture and (1, 1) is the top right, whatever its size.
Dividing gl_FragCoord.xy by u_canvasResolution gives a position that runs from 0 to 1 across the canvas, which is exactly a texture coordinate:
vec2 textureCoord = gl_FragCoord.xy / u_canvasResolution; // 0 to 1 across the canvas
gl_FragColor = texture2D(u_picture, textureCoord); // the texture's colour at that point
texture2D gives back a vec4: red, green, blue and alpha (how see-through the colour is), each from 0 to 1. Try changing textureCoord before the read, for example textureCoord * 2.0, and see which part of the photo you get.
If GLSL is new to you, The Book of Shaders chapter on uniforms covers gl_FragCoord and the canvas size in more depth. It calls the canvas size u_resolution, and that name works in these editors too.
Working with colour
A colour in GLSL is a vector: a small list of numbers. A vec3 holds three numbers and a vec4 holds four. For a colour they are red, green, blue and, in a vec4, alpha, each from 0 to 1. Each number is one component of the vector.
- Read one component with
.r,.g,.bor.a. For example,colour.gis the green ofcolour. - Build a vector from parts:
vec3(colour.g, colour.b, colour.r), orvec4(colour, 1.0)to add an alpha of 1 to avec3. - Swizzle: list components after the dot, in any order, to get them back as a new vector.
colour.gbris the same asvec3(colour.g, colour.b, colour.r), andcolour.rgbtakes the first three components of avec4. - Do arithmetic on a whole vector at once.
1.0 - colourtakes every component away from 1, which turns a colour into its negative: black becomes white, and red becomes cyan.
This snippet swaps the red and blue of every pixel with one swizzle:
vec3 colour = texture2D(u_picture, textureCoord).rgb; // red, green and blue
gl_FragColor = vec4(colour.bgr, 1.0); // blue and red swap places
The Book of Shaders chapter on colour covers vectors as colours and swizzling.
Before
After, live. Try 1.0 - colour in place of colour.bgr, or another order of r, g and b.
Working backwards
A recipe on a Milestone page lists the steps that scrambled a picture: where each pixel was moved to, and what was done to its colour. Your shader has to undo them. A shader can't move a pixel or send a colour somewhere else. It runs once for each pixel of the canvas, and that pixel chooses its own colour by reading it from somewhere.
So each pixel of the picture asks: where did my colour end up in the scrambled picture, and what was done to it on the way? It reads the colour from that place, then undoes the changes to the colour.
When a recipe has more than one step, undo the steps in reverse order: the last step that was done is the first one you undo. The editors below show this with two steps:
- The photo is flipped upside down, so the pixel at height
ymoves to height1 − y. - Every colour is turned into a dim negative: each value
vbecomes(1 − v) / 2.
To undo it, each pixel of the photo reads its colour from height 1 − y of the scrambled picture, because that's where the flip moved it to. Then it undoes the colour steps, last one first: it doubles the value, which undoes step 2's halving, and only then takes it away from 1, which undoes the negative:
// The flip moved my colour to height 1 − y.
vec3 scrambledColour = scrambled(vec2(textureCoord.x, 1.0 - textureCoord.y));
// Undo step 2, the halving, then step 1, the negative.
vec3 colour = 1.0 - scrambledColour * 2.0;
Undoing them in the wrong order, (1.0 - scrambledColour) * 2.0, gives a different colour. Try it in the second editor.
Before: the scrambled picture. A shader can only read colours, so even the scrambling is written as each pixel reading from somewhere.
After: the scrambled picture, unscrambled. scrambled gives the colour the editor above draws at a texture coordinate.
Pixel coordinates
The photo is 512 × 168 picture pixels. The canvas is made of device pixels: the dots on the screen. They are rarely the same.
gl_FragCoord counts device pixels. On a screen with two dots for every pixel the page asks for, such as most phones and Macs, a canvas 512 × 168 pixels wide on the page is 1024×336 device pixels. The canvas on this site also stretches to fit the page. So gl_FragCoord.xy on its own could be anything from 0 to over 1000, and it tells you nothing about which picture pixel you're on.
To count in picture pixels, take three steps:
- Divide
gl_FragCoord.xybyu_canvasResolution, the canvas size in device pixels. That gives 0 to 1 across the canvas, on any screen. - Multiply by
vec2(W, H). That gives 0 to 512 across and 0 to 168 up. - Round down with
floor, sofloor(1.0)andfloor(1.9)are both 1. A position like (37.6, 12.2) becomes the pixel it falls in, (37, 12).
To read that pixel, turn it back into a texture coordinate at its centre, with (pixel + 0.5) / vec2(W, H). Reading the centre gives exactly that pixel's colour. Reading its edge can give a neighbour's.
// Which picture pixel this is, then the colour at its centre.
vec2 pixel = floor(gl_FragCoord.xy / u_canvasResolution * vec2(W, H));
vec3 colour = texture2D(u_picture, (pixel + 0.5) / vec2(W, H)).rgb;
Before
After, live: the bottom-left corner of the photo, 8 times bigger, with a dark edge round every picture pixel. Each block is one of the 512 × 168 pixels your shader counts in. Try changing ZOOM.
Wrapping
Wrapping means that whatever goes off one edge of the picture comes back in at the opposite edge.
mod(a, n) wraps a number. It's the remainder after dividing a by n, so it always lands between 0 and n. mod(530.0, 512.0) is 18: going 530 pixels across a 512-pixel picture wraps round to pixel 18. That's what "mod 512" means in a recipe.
Be careful which way round W and H go. W = 512 is the width, so x wraps at W. H = 168 is the height, so y wraps at H. Mixing them up breaks the picture.
GLSL does mod on floats: numbers with a fractional part, such as 0.5. On a graphics card, dividing floats can give an answer a tiny amount out. A remainder that should be exactly 0 can come back as 511.99999, and floor makes that 511, the wrong pixel. This helper, a small function you call instead of mod, avoids that. It adds half a pixel first, so a tiny error either way never crosses a whole number:
// GLSL's mod, but exact for whole numbers of pixels.
float exactMod(float a, float n) { return floor(mod(a + 0.5, n)); }
Paste it above main, and use it wherever you wrap a pixel coordinate.
Before: each pixel reads from 128 pixels to its right, with no wrapping. The photo slides left, and the right-hand part reads past the edge, so it's filled with the colours at the edge, stretched.
After: the same slide, wrapped with exactMod. What went off the left edge comes back in at the right.
Shifting rows and columns
Wrapping slides the whole photo by the same amount. A slide can also be different for every row: each row slides by an amount that depends on its height, y. Rows are counted from the bottom, starting at 0, so the bottom row doesn't move and each row above it slides a little further. That slants the photo, and whatever slides off one edge wraps round to the other.
Each pixel reads from SLANT * pixel.y pixels to its right, on the same row, so the photo in that row slides left. It wraps at W because it's a position across:
// Row y reads from SLANT · y pixels to the right, wrapping at the width.
vec2 sourcePixel = vec2(exactMod(pixel.x + SLANT * pixel.y, W), pixel.y);
Columns work the same way, the other way round. Each pixel reads from SLANT * pixel.x pixels higher, on the same column, so the photo in that column slides down, by an amount that depends on its position across, x. It wraps at H because it's a height:
// Column x reads from SLANT · x pixels higher, wrapping at the height.
vec2 sourcePixel = vec2(pixel.x, exactMod(pixel.y + SLANT * pixel.x, H));
Before
After, live: each row reads from SLANT * pixel.y pixels to its right, so each row slides left a little further than the row below it. The photo leans over and wraps round at the sides. Try a different SLANT, or the column line in place of the row line.
Precision
A float is a number with a fractional part, such as 0.5. The first line of every shader on this site is:
precision highp float;
A fragment shader has to say how precise its floats are. highp asks for the most precision the device has, which is 32-bit floats on almost every phone and computer.
Many shader examples online say precision mediump float; instead. Don't copy that line here. On many phones, mediump floats count whole numbers exactly only up to 2048, and their rounding is coarse enough to put some pixels in the wrong place. A decode can multiply pixel numbers by a recipe's keys, the numbers that say how far it moved things, so its numbers get much bigger than 2048. Computers usually run mediump at full precision anyway, so a mediump shader can look fine on your laptop and break on your phone.
Leaving the line out is an error. Try deleting the precision line below, and the editor shows the error.
Splitting the canvas
One shader can draw several different pictures side by side. Split the canvas into tiles: smaller rectangles in a grid. Each pixel works out which tile it's in, and where it is inside that tile. Then each tile can draw something different, such as the same steps with a different number in each tile. That lets you compare several tries at once, instead of changing a number and looking again each time.
Start from the texture coordinate, which runs from 0 to 1 across the canvas. Multiply it by TILES, the number of tiles across and up, so it runs from 0 to TILES. Then split that number in two:
floorrounds down, so it gives the whole-number part: which tile the pixel is in. With 2 tiles, 1.3 becomes 1, the second tile. Tiles are counted from 0, from the bottom left.fractgives the part after the decimal point: 1.3 becomes 0.3. That's where the pixel is inside its tile, from 0 to 1, so each tile can treat it as a texture coordinate, as if the tile were a whole canvas.
To give each tile its own number, count them along each row, from the bottom row up:
vec2 tile = floor(textureCoord * TILES); // which tile
vec2 withinTile = fract(textureCoord * TILES); // 0 to 1 across it
float tileNumber = tile.y * TILES + tile.x; // 0, 1, 2, 3, ...
With the same number of tiles across and up, every tile has the same shape as the canvas, so the photo isn't stretched in any of them.
Before
After, live: 2 tiles across and 2 up. Each tile draws the whole photo, smaller, slid left and wrapped round like in Wrapping. Each tile slides it 128 pixels further than the tile before. Try TILES = 3.0 or 4.0, or a different SLIDE.
Image: Bathing boxes at Port Phillip Bay, Australia; 2005 by Bernard Spragg. NZ, CC0 1.0, cropped and scaled.