A Appendix
Built-in function cheat sheet
Pick a function to plot it. Drag or edit the numbers in the code and the curve follows.
Shaping
Repeat
Periodic
Exponential
Vector
Sampling
smoothstep Soft switch: an S-curve between two edgesCareful: the first edge must be smaller than the second
See it → use it
- Circles, rings
length(p) − r; ring: abs(d) − w - Union, intersection, cut-outs
min · max · max(a, −b) - Liquid, shapes melting together
smin(a, b, k) - Petals, stars, rays, spirals
atan + cos(n · a) - Tiles, dots, grids
fract(p · n); floor for the cell - Every cell different
hash(floor(p)) - Symmetry, kaleidoscopes
abs(p); mod on the angle - Outlines, neon, contours
abs(d) · w / abs(d) · cos(d / w) - Clean edges at any zoom
smoothstep + fwidth - Rainbows, colour ramps
0.5 + 0.5 · cos(2π(t + phase)) - Clouds, marble, fire
fbm, domain warping - Spinning, flowing, flashing
uTime in the coordinates or the colour - Shaded 3D look
max(dot(N, L), 0.0) - 3D without triangles
raymarch + SDF
Exercise
Runs once per pixelDrag numbers, or edit the code
JS side
Remember functions by shape: shaping, repeat, periodic and vector cover most shaders. Then learn the traps: mod with negatives, atan’s argument order, smoothstep’s edge order, pow’s negative base.
Try it
- Swap the two edges in smoothstep: what does the curve become?
- Change fract(x) to fract(x * 3.0) and count the teeth
- Compare mod and fract on the negative side
- Open the pixel debugger, put the pointer on the curve and read x and y