home/ modules/ building-glsl-color-and-coordinate-toolkit

GLSL fundamentals: coordinates, vectors, and color output

  • learner can use swizzling, component aliases, and correct float literals to manipulate GLSL vectors
  • learner can correct aspect ratio and normalize to clip space so shaders are resolution-independent
  • learner can pass CPU uniforms to the GPU and manage clamped color output channels
  • learner can use length() to build distance-from-origin radial gradients

Build a resolution-independent GLSL sketch that draws an aspect-corrected, clip-space-centered radial gradient wheel with length()-based falloff, using uniforms, swizzles, and clamped color output.

When you project shaders behind a live set — a Hydra patch, a Shadertoy port, a custom fragment stage in your VJ rig — the venue decides your resolution. A sketch that looks right on your 16:9 laptop will smear into ellipses on a square LED wall unless coordinates are handled correctly from line one. This module builds that reflex: a small toolkit of vector, coordinate, and color moves that every later shader module assumes.

The arc starts supported. First, manipulate vectors in a working scaffold: component aliases (.xyzw / .rgba are the same storage), swizzling to reorder channels in one expression, and the strict-typing gotcha that every float literal needs a decimal point — the single most common compile error for anyone arriving from JavaScript. Next, take over the opening lines of the shader yourself: normalize pixel coordinates to [0,1], recenter to clip space [-1,1], and apply the one-line aspect-ratio correction so circles stay circular. Then wire the CPU side: declare uniforms for resolution and time, and learn why colors saturate — every output channel clamps to [0,1], so anything unnormalized flattens to white. Finally, put the corrected coordinates to work: length(uv) gives each pixel its distance from center, the seed of every radial gradient and circular shape.

The capstone removes the scaffold: from a blank file, draw a radial gradient wheel that stays centered and round at any canvas size. Every required atom gates it — the wheel’s falloff is literally length()‘s distance-from-origin value, its roundness is the aspect correction, its portability is the uniform-fed normalization, and its smooth ramp depends on respecting channel clamping. Supporting atoms enrich rather than gate: the Shadertoy-flavored take on normalized camera coordinates (the same clip-space move seen from a second angle), the vertex/fragment pipeline picture, function argument qualifiers, the pow()-of-negative trap, and mix()-based color blending all deepen the toolkit but the wheel ships without them. Drill the normalization one-liner, float literals, and swizzles until they are automatic — they open virtually every shader you will ever write live.

Runnable examples

Generated from the context/ instrument corpus by concept (redistributable idioms only). Do not edit — regenerate with gen-module-examples.mjs.

gradient-ramp

gradient(0.3).out()

hydra-0170 · MIT

[fr, fr*0.5, 1-fr] >> rgb

punctual-0033 · CC0-1.0

Atoms in this module

Required — these gate the capstone

A GLSL vector's components have interchangeable names: .xyzw, .rgba, .stpq, and [i]
Concept L1 Foundations G
GLSL swizzling reads or writes a vector's components in any order in one expression
Concept L1 Foundations GH
GLSL requires a decimal point on all floating-point literals
Fact L2 First instrument G
Multiplying st.x by width/height corrects the UV space for non-square canvases
Procedure L1 Foundations G
Normalizing UV coordinates to clip space (−1 to 1, aspect-ratio-corrected) makes shaders independent of canvas resolution
Procedure L2 First instrument G
GLSL uniforms pass values from the CPU to the GPU each frame
Concept L2 First instrument GJ
GPU fragment shader color output channels are clamped to [0,1]; values exceeding 1 saturate and produce incorrect gradients
Concept L2 First instrument G
The GLSL length() function computes distance from the origin, enabling radial gradients and circular shapes
Concept L2 First instrument G

Supporting — enrichment, not gating

Normalizing pixel coordinates to [-1,1] makes shaders resolution-independent and centers the mathematical origin
Concept L1 Foundations G
A GLSL shader program splits into a vertex shader run per-vertex and a fragment shader run per-pixel
Concept L2 First instrument GH
GLSL's in/out/inout qualifiers set whether a function reads, writes, or modifies an argument
Concept L2 First instrument G
GLSL pow(x,y) returns undefined for negative x, causing silent visual bugs
Misconception L2 First instrument G
GLSL's mix() linearly interpolates between two colors by a 0.0–1.0 factor
Concept L1 Foundations GL