Shape Generation Foundation

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).

uv (screen): (0.50, 0.50)
p = uv – orig: (0.50, 0.50)
length(p): 0.71
gl_FragColor: vec3(0.50, 0.50, 0.00)
1. Active Channels & Gradient Sweep
Gradient Fill Sweep 1.00
2. Normalized Coordinate Origin
Origin X 0.00
Origin Y 0.00
3. SDF Circle: length(xy)
Live Fragment Shader GLSL

      

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

  • Bottom-Left (0.0, 0.0): Both uv.x and uv.y equal 0.0.
  • Top-Right (1.0, 1.0): Both uv.x and uv.y equal 1.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 raw uv.x value mapped to the Red channel and uv.y mapped 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

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.0 when 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

  • At the exact origin (0.0, 0.0), dist is 0.0.
  • Moving outward in any direction increases dist smoothly 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

  • Inside the circle (dist <= 0.35): step() outputs 1.0, painting the pixel with your circle color.
  • Outside the circle (dist > 0.35): step() outputs 0.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 between step() (hard binary mask) and length() (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

{"attributes":{"postId":802,"defaultIndex":1,"editorSize":{"width":{"value":100,"unit":"%"},"height":{"value":867,"unit":"px"}}},"isServer":false}

A Quick Mental Checklist for Visualizing Any Shader

  1. Find (0.0, 0.0) first: Look at where uv is subtracted or shifted—that point is the anchor for all math in the scene.
  2. Read colors as numbers: Red is X, Green is Y, and pure Black means a value is <= 0.0.
  3. 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?”