Gradient Shader Visualizer
GPU Fragment Shader: Coordinate & Gradient Visualizer
Click or drag on the grid to shift the origin. Negative coordinates clamp to 0.0 (black).
When writing GLSL fragment shaders, the biggest mental shift from traditional CPU drawing (like HTML5 Canvas or SVG) is parallelism. Instead of looping through pixels from top to bottom, the GPU runs your mainImage() function simultaneously for every single pixel on the screen.
Each pixel only knows one thing about itself: its own screen coordinates (fragCoord). Everything you draw—gradients, shapes, and animations—comes down to doing math on those coordinates and returning a color (fragColor).
1. Normalizing the Canvas (uv Space)
Raw screen coordinates (fragCoord) are measured in pixels (e.g., 0 to 1920 wide). Hardcoding pixel sizes breaks when a screen resizes, so the first step in almost every shader is normalizing coordinates into a 0.0 to 1.0 range:
OpenGL Shading Language
vec2 uv = fragCoord / iResolution.xy;
- Bottom-Left
(0.0, 0.0): Bothuv.xanduv.yequal0.0. - Top-Right
(1.0, 1.0): Bothuv.xanduv.yequal1.0. - Center
(0.5, 0.5): Exactly halfway across both axes.
Try it in the visualizer: With the origin at
(0.0, 0.0), toggle X-Axis (Red) and Y-Axis (Green) individually. You are literally looking at the rawuv.xvalue mapped to the Red channel anduv.ymapped to the Green channel.
2. Shifting the Origin & Why Negative Numbers Turn Black
By default, (0.0, 0.0) sits in the bottom-left corner. To move the center of your math anywhere else—like the middle of the canvas—subtract your target origin from uv:
OpenGL Shading Language
vec2 origin = vec2(0.5, 0.5);
vec2 p = uv – origin;
Now, pixels to the left of 0.5 have a negative p.x, and pixels below 0.5 have a negative p.y.
When you output p directly as a color (vec4(p.x, p.y, 0.0, 1.0)), the GPU’s hardware frame buffer automatically clamps color channels between 0.0 (0% brightness) and 1.0 (100% brightness). Any negative coordinate (-0.5, -0.1, etc.) clamps to 0.0, rendering as pure black.
Try it in the visualizer: Click the (0.5, 0.5) origin preset. Notice how the bottom-left quadrant goes completely dark—not because the math stopped working, but because negative coordinates clamp to
0.0when visualized as RGB colors.
3. Drawing Shapes Without Polygons: length(xy)
GPUs don’t have a drawCircle() function inside a fragment shader. Instead, we use Signed Distance Fields (SDFs)—a fancy way of asking every pixel: “How far am I from the origin?”
Using the Pythagorean theorem ($\sqrt{x^2 + y^2}$), GLSL’s built-in length(p) function returns the exact straight-line distance from (0.0, 0.0) to the current pixel:
OpenGL Shading Language
float dist = length(p);
- At the exact origin
(0.0, 0.0),distis0.0. - Moving outward in any direction increases
distsmoothly into a radial gradient.
To turn that radial distance gradient into a crisp circle of radius 0.35, compare dist against the radius using step():
OpenGL Shading Language
// Returns 1.0 if dist <= 0.35 (inside circle), otherwise 0.0 (outside)
float circleMask = step(dist, 0.35);
- Inside the circle (
dist <= 0.35):step()outputs1.0, painting the pixel with your circle color. - Outside the circle (
dist > 0.35):step()outputs0.0, leaving the background untouched.
Try it in the visualizer: Turn on Show Circle, drag your mouse across the grid to see the yellow radius vector calculate
length(p)in real time, and switch betweenstep()(hard binary mask) andlength()(raw distance falloff).
Putting It Together: Complete Starter Shader
Here is the complete, minimal GLSL snippet combining coordinate normalization, origin shifting, and circle masking:
OpenGL Shading Language
A Quick Mental Checklist for Visualizing Any Shader
- Find
(0.0, 0.0)first: Look at whereuvis subtracted or shifted—that point is the anchor for all math in the scene. - Read colors as numbers: Red is
X, Green isY, and pure Black means a value is<= 0.0. - Think per-pixel, not per-object: Never ask “Where do I draw the circle?”—always ask “Given this pixel’s
(x, y)coordinate, what color should it return?”