TExcelTAD
Home › Shaders & Graphics Programming › UVs and Color

UVs and Color

Every fragment shader starts with the same question: where am I? The GPU hands you fragCoord, the pixel position in pixels. Pixels are awkward to work with, because the frame can be 400 or 4000 pixels wide. So the first line of almost every shader turns pixels into UV coordinates: numbers that run from 0 to 1 across the frame, whatever its size.

From pixels to UV

Dividing by the resolution maps the frame to the unit square: (0, 0) at the bottom-left, (1, 1) at the top-right. Writing the UV straight into the red and green channels is the "hello world" of shaders, and the best way to see a coordinate system:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    fragColor = vec4(uv.x, uv.y, 0.0, 1.0);   // black bottom-left, red right, green up, yellow top-right
}

Colors in a shader are vec4(red, green, blue, alpha) with each channel from 0.0 to 1.0, not 0–255. Values outside the range are clamped when they are written to an 8-bit screen, so 2.0 looks the same as 1.0 and -1.0 the same as 0.0. That clamping hides bugs: a coordinate you think is 0..1 may really be 0..3, and you only see a big flat yellow area.

Centered coordinates and the aspect ratio

UVs stretch with the frame: a circle drawn as length(uv - 0.5) < 0.3 becomes an ellipse in a wide frame. For shapes you want coordinates that are centered and use the same scale on both axes. Divide by the height only:

glsl
vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;

Now p.y runs from -0.5 to 0.5, (0, 0) is the center, and p.x goes as far as the aspect ratio allows (about ±0.9 in a 16:9 frame). Most shaders in this course use this p. Compare the two below: the left half uses stretched UVs, the right half uses aspect-corrected ones, and only the right circle is round.

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec2 p  = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;

    float stretched = length(uv - vec2(0.25, 0.5)) < 0.2 ? 1.0 : 0.0;
    float round     = length(p - vec2(0.4, 0.0)) < 0.2 ? 1.0 : 0.0;

    vec3 color = uv.x < 0.5 ? vec3(stretched, 0.3, 0.5) : vec3(0.2, round, 0.5);
    fragColor = vec4(color, 1.0);
}

Mixing colors

mix(a, b, t) is GLSL's lerp: a when t = 0, b when t = 1, and a blend in between. It works on floats and on whole vectors at once, so it is the natural way to build gradients. Here a sky fades from a warm horizon to a deep blue, with a sun added on top:

glsl · try it yourself
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec2 p  = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;

    vec3 horizon = vec3(1.0, 0.55, 0.3);
    vec3 zenith  = vec3(0.1, 0.2, 0.5);
    vec3 sky = mix(horizon, zenith, smoothstep(0.0, 0.8, uv.y));

    float d = length(p - vec2(0.3, -0.05));
    vec3 sun = vec3(1.0, 0.9, 0.6) * (1.0 - smoothstep(0.10, 0.12, d));   // hard disc
    vec3 glow = vec3(1.0, 0.6, 0.3) * 0.05 / (d + 0.02);           // soft halo

    fragColor = vec4(sky + sun + glow, 1.0);
}

smoothstep(edge0, edge1, x) returns 0 below edge0, 1 above edge1, and a smooth S-curve between. You will use it in nearly every shader: for soft gradients, and with a tiny gap between the edges for anti-aliased shape borders. 1.0 - smoothstep(0.10, 0.12, d) flips it into "1 inside, 0 outside". You will see shaders that swap the edges instead (smoothstep(0.12, 0.10, d)); it works on most GPUs, but the GLSL specification says the result is undefined when edge0 >= edge1, so prefer the 1.0 - form.

Color spaces in one paragraph

The numbers you write are not the brightness that leaves the screen. Monitors expect sRGB-encoded values, which are roughly the physical (linear) intensity raised to the power 1/2.2. Lighting math (adding lights, blurring, blending) is only correct in linear space. The rule used by every engine: compute lighting in linear values, then convert once at the very end. In these playgrounds nobody converts for you, so later lessons end with:

glsl
color = pow(color, vec3(1.0 / 2.2));   // linear -> approximately sRGB, as the last step

For simple gradients like the ones above, skipping it is fine. The Field Notes lesson shows what goes wrong when you forget it in lighting.

A palette function

Hand-picking colors for every effect gets old. A cosine palette maps one number t to a smooth, looping range of colors with four vec3 parameters (an idea popularized by Inigo Quilez). Change d to get a whole new palette:

glsl · try it yourself
vec3 palette(float t) {
    vec3 a = vec3(0.5), b = vec3(0.5), c = vec3(1.0);
    vec3 d = vec3(0.0, 0.33, 0.67);
    return a + b * cos(6.28318 * (c * t + d));
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 uv = fragCoord / iResolution.xy;
    vec3 color = palette(uv.x + iTime * 0.1);
    color *= 0.6 + 0.4 * step(0.5, uv.y);       // darker bottom half
    fragColor = vec4(color, 1.0);
}

Useful built-ins

Function Does Typical use
mix(a, b, t) a + (b - a) * t gradients, blending
clamp(x, lo, hi) limit to a range keep colors in 0..1
step(edge, x) 0 if x < edge, else 1 hard masks
smoothstep(e0, e1, x) smooth 0 → 1 between edges soft masks, anti-aliasing
fract(x) x - floor(x) repetition (next lesson)
length(v), distance(a, b) vector length circles, falloff

Test yourself

Which line gives centered coordinates with the same scale on both axes?

  1. fragCoord / iResolution.xy
  2. (fragCoord - 0.5 * iResolution.xy) / iResolution.y
  3. fragCoord / iResolution.x
  4. fragCoord - iResolution.xy

What does 1.0 - smoothstep(0.10, 0.12, d) return when d is 0.05?

  1. 0.0
  2. 0.5
  3. 1.0
  4. 0.05

A shader writes vec4(3.0, 0.0, 0.0, 1.0) to an 8-bit screen. What appears?

  1. Three times brighter red than 1.0
  2. Full red, the same as 1.0
  3. Black
  4. A compile error