How to Write Your First GLSL Fragment Shader
A beginner-friendly guide to fragment shaders: coordinates, colour, time, mouse input, distance functions and exporting stills and loops from WebGL.

How to Write Your First GLSL Fragment Shader
Fragment shaders feel like magic the first time you see them: a dozen lines of code produce flowing colours, fractals or a lit 3D sphere, at sixty frames per second. The trick is a different way of thinking. Instead of drawing shapes one after another, you write one function that decides the colour of a single pixel, and the graphics card runs it for every pixel at once. This guide builds that intuition step by step, and you can follow along in the free GLSL shader playground.
The one question a shader answers
A fragment shader receives the position of one pixel and must return a colour. In Shadertoy style the function looks like this:
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
fragColor = vec4(1.0, 0.5, 0.0, 1.0);
}
That paints every pixel orange. Colours are four numbers from 0 to 1: red, green, blue and alpha.
Normalise your coordinates
fragCoord is in pixels, which depends on the canvas size. Divide by the resolution to get values from 0 to 1, then use them as colour:
vec2 uv = fragCoord / iResolution.xy;
fragColor = vec4(uv, 0.5, 1.0);
You now see a gradient: red grows to the right, green grows upwards. Most shaders start with a line like this. For shapes that should stay round on wide screens, centre the coordinates and divide by the height only: (fragCoord - 0.5 * iResolution.xy) / iResolution.y.
Draw a circle with distance
The distance from the centre is length(uv). A pixel is inside a circle of radius 0.3 when that distance is below 0.3. smoothstep turns the hard edge into a soft one:
float d = length(uv);
float circle = 1.0 - smoothstep(0.29, 0.31, d);
Use circle to mix two colours. Distance functions like this are the building blocks of most shader art: boxes, rings, lines and even 3D scenes are described by "how far is this point from the shape".
Bring it to life with time
iTime is the number of seconds since the shader started. Feed it into a sine wave and things move: 0.3 + 0.05 * sin(iTime * 2.0) makes the radius pulse. Adding iTime to the coordinates inside sin and cos produces scrolling waves; combine several and you get the classic plasma effect.
React to the mouse
iMouse holds the pointer position in pixels while you drag. Normalise it the same way as fragCoord and use it as the circle's centre, and the shape follows your hand. In the raw style of The Book of Shaders the same values are called u_mouse, u_time and u_resolution.
Expose parameters as sliders
Hard-coded numbers make experimentation slow. Declare a uniform such as uniform float uSpeed; and the playground gives it a slider. Colours become colour pickers. This is also how shaders are prepared for artists and game designers: the code stays fixed and the look is tuned with controls.
Read compile errors calmly
GLSL is strict. 1 and 1.0 are different types, every statement needs a semicolon, and there is no automatic conversion from int to float. The compiler reports a line number and a short message; fix the first error first, because later ones are often caused by it.
Keep performance in mind
The function runs millions of times per second. Loops should have a fixed upper bound, and expensive work like raymarching benefits from fewer steps or a lower render scale on slow laptops. If the frame rate drops, halve the quality first and optimise later.
Save your work
A still frame can be exported as a PNG at any resolution, which makes shaders a great source of wallpapers and print-quality abstract art. Recording a few seconds as video gives you a loop for a website background or a social media post.
Limits
This guide covers single-pass fragment shaders. Textures, multiple buffers that feed into each other, vertex shaders and 3D meshes are the next steps, and they need a fuller graphics framework such as three.js. A shader that runs smoothly on a desktop graphics card may be slow on a phone, so test on the devices your audience uses.



