Learn with me: Getting Started with Shader Functions (step, mix, smoothstep)
Introduction
Hey everyone, welcome back to my "Learn with me" series! In the last post, we looked at the very basics of GLSL shaders. Today, we go a bit further with a few essential shader functions. We use them to control pixels and create visual effects.
This article (and hopefully the whole series) is interactive: most examples come with a live shader editor right on the page. Change the code or the values, and the result updates instantly. You can also use the editors to try your own ideas. For the challenges, your code is saved in your browser's local storage, so you won't lose your work if you leave the page.
Quick tip: if the preview suddenly turns black, there's probably an error in your shader code. Open your browser's developer console (usually with F12), and you should find an error message pointing to the faulty line.
Full disclaimer: I'm still learning shaders myself! This series is my public learning journal. The examples are meant to be simple, so they're probably not the most optimized or technically perfect code you'll ever see. I want to understand these concepts and share what I learn as I go. If you spot mistakes or know better ways of doing things, tell me, and we'll all learn something.
The awesome thing about shaders is that a lot of effects boil down to math. Once we know a few core functions, we can start combining them. Let's try some examples!
What Shader Functions Do
Think of the fragment shader as a tiny program that runs for every single pixel on your geometry. Functions like step, mix, and smoothstep give us precise control over the color (or other properties) of each pixel based on its position, distance, or other calculations.
Essential Shader Functions
Let's start with three very useful functions.
1. The step Function: Creating Hard Edges
The step function works like a switch. step(edge, x) compares two values and returns either 0.0 or 1.0:
- If
xis less thanedge, it returns0.0. - If
xis greater than or equal toedge, it returns1.0.
Use it to create sharp divisions or masks.
Example: A Simple Vertical Line
Let's divide the plane in half vertically using vUv.x (the horizontal texture coordinate, which goes from 0.0 on the left to 1.0 on the right).
Try it yourself:
- Change the
edgevalue (e.g., to0.2or0.8) to move the dividing line. - Try using
vUv.yinstead ofvUv.xto create a horizontal line. - Can you make the left side white and the right side black? (Hint: You might need to swap something)
2. The mix Function: Blending Between Values
The mix function (also known as lerp, or linear interpolation) blends between two values. mix(x, y, a) returns a value between x and y, and a controls the amount of blending:
- If
ais0.0, it returnsx. - If
ais1.0, it returnsy. - If
ais0.5, it returns a 50/50 mix ofxandy. - Values between 0.0 and 1.0 give proportional blends.
Example: A Simple Horizontal Gradient
Let's blend between two colors using vUv.x as the blending factor.
Try it yourself:
- Change
colorAandcolorBto create different gradients. - Use
vUv.yas the blend factor for a vertical gradient. - What happens if you use something like
vUv.x * 0.5as the blend factor?
Challenge #1: Creating a Four-Quadrant Color Grid
Okay, first challenge! Let's combine step and mix to create a grid with four different colors. Use step to define the horizontal and vertical dividing lines, and then use mix (maybe nested?) to select the correct color for each quadrant.
Goal:
- Top-Left: Red
- Top-Right: Green
- Bottom-Left: Blue
- Bottom-Right: Yellow
3. The smoothstep Function: Creating Smooth Transitions
step gives us hard edges. smoothstep gives a smooth transition between 0.0 and 1.0 instead. smoothstep(edge0, edge1, x) works like this:
- If
xis less thanedge0, it returns0.0. - If
xis greater thanedge1, it returns1.0. - If
xis betweenedge0andedge1, it returns a smoothly interpolated value (using a Hermite curve) between0.0and1.0.
Use it for soft gradients, fades or feathered edges.
Example: A Smooth Gradient
Let's create a smooth transition between two colors.
Try it yourself:
- Change
edge0andedge1to see how the transition width and position change. - What happens if
edge0is greater thanedge1? (e.g.,smoothstep(0.7, 0.3, vUv.x)) - Can you make a smooth vertical gradient?
Applying smoothstep: Vignette Effect
A common use for smoothstep is creating a vignette (darkening the corners/edges). We can calculate the distance of each pixel from the center and use smoothstep to make it darker further away.
Challenge #2: Modifying the Vignette
Using the vignette code above as a starting point, can you:
- Make the vignette effect oval instead of circular? (Hint: You need to adjust how
distis calculated. Maybe scale thexorycomponent of the vector before calculating thedistance? Or calculate dx and dy separately and combine them differently.) - Make the vignette sharper (less smooth)? (Hint: Adjust the
smoothstepedges.)
(Feel free to experiment directly in the editor above!)
Bonus Function: fract for Repetition
Often we want repeating patterns. The fract function is great for this. It simply returns the fractional part of a number (e.g., fract(3.7) is 0.7, fract(1.2) is 0.2).
If you multiply vUv.x or vUv.y by a number before applying fract, the pattern will repeat that many times across the surface.
Example: Simple Stripes
Challenge #3: Checkerboard Pattern
Combine horizontal and vertical stripes (using fract and step) to create a checkerboard pattern.
Hint: a common way to make a checkerboard is to get the step value for x and y (let's call them sx and sy, both being 0 or 1). The checkerboard alternates when either sx or sy is 1, but not both, which is exactly what XOR (exclusive OR) does. In GLSL, you can simulate XOR for 0/1 values like this: abs(sx - sy) or mod(sx + sy, 2.0).
Practical Project: Mondrian-Style Composition (Challenge #4)
Now for a slightly bigger challenge that combines these ideas. Let's create a composition inspired by Piet Mondrian's geometric art.
I first came across this exercise in Wawa Sensei's React Three Fiber course, and it was a huge "aha!" moment for me. It finally clicked how simple step and mix calls can be layered to build a structured image. It's a good exercise to understand boundaries and color layering.
The goal is to use step functions to define rectangular areas, and mix to "paint" them with Mondrian's colors (red, blue, yellow, white, and black lines).
We'll use a helper function drawRectangle that returns 1.0 inside the defined rectangle and 0.0 outside. Your task is to call this function multiple times with different coordinates and colors, layering them using mix. Remember that black lines often separate the colored blocks.
Conclusion
We've played with step, mix, smoothstep and fract. With just these basic functions, we can already make divisions, gradients, simple shapes and repeating patterns. The Mondrian challenge shows how far you can get by layering them.
My biggest lesson so far: just try stuff! Change values, swap vUv.x for vUv.y, combine functions in weird ways. Sometimes it breaks, sometimes it looks ugly, but sometimes you stumble onto something cool. That's how we learn, right?
In the next part, we might look at time (uniform float u_time), noise functions or textures. Tell me which one you'd like to see first! Until then, keep experimenting.