Skip to content

t1k:web:3d:shader

FieldValue
Module3d
Version1.7.0
Effortmedium
Tools

Keywords: fragment-shader, glsl, procedural, shaders, webgl

/t1k:web:3d:shader
[effect or pattern]

Write GPU-accelerated fragment shaders for procedural graphics, textures, and visual effects.

  • Creating procedural textures (wood, marble, clouds, terrain)
  • Drawing shapes with distance fields (SDF)
  • Generating patterns, noise, gradients
  • Building visual effects and animations
  • Writing custom shaders for Three.js, WebGL, Processing

Fragment shaders execute simultaneously on every pixel. Each thread:

  • Receives pixel position via gl_FragCoord
  • Returns color via gl_FragColor (vec4: RGBA 0.0-1.0)
  • Cannot communicate with other threads (stateless)
uniform float u_time; // Elapsed seconds
uniform vec2 u_resolution; // Canvas size (width, height)
uniform vec2 u_mouse; // Mouse position in pixels

Normalize coordinates: vec2 st = gl_FragCoord.xy / u_resolution;

FunctionPurposeExample
mix(a,b,t)Linear interpolatemix(red, blue, 0.5)
step(edge,x)Hard thresholdstep(0.5, st.x)
smoothstep(e0,e1,x)Smooth thresholdsmoothstep(0.2, 0.8, st.x)
fract(x)Fractional partfract(st * 3.0) for tiling
mod(x,y)Modulomod(st.x, 0.25)
clamp(x,min,max)Constrain valueclamp(col, 0.0, 1.0)
length(v)Vector magnitudelength(st - 0.5)
distance(a,b)Euclidean distancedistance(st, center)
dot(a,b)Dot productdot(normal, lightDir)
normalize(v)Unit vectornormalize(direction)
atan(y,x)Angle (radians)atan(st.y-0.5, st.x-0.5)
sin/cos/pow/absMathHardware-accelerated

Circle:

float d = distance(st, vec2(0.5));
float circle = 1.0 - smoothstep(0.2, 0.21, d);

Rectangle:

vec2 bl = step(vec2(0.1), st);
vec2 tr = step(vec2(0.1), 1.0 - st);
float rect = bl.x * bl.y * tr.x * tr.y;

Tiling:

st = fract(st * 4.0); // 4x4 grid

Animation:

float wave = sin(st.x * 10.0 + u_time) * 0.5 + 0.5;
  • references/glsl-fundamentals-data-types-vectors-precision-coordinates.md
  • references/glsl-shaping-functions-step-smoothstep-curves-interpolation.md
  • references/glsl-colors-rgb-hsb-gradients-mixing-color-spaces.md
  • references/glsl-shapes-sdf-circles-rectangles-polar-distance-fields.md
  • references/glsl-shapes-polygon-star-polar-sdf-combinations.md
  • references/glsl-patterns-tiling-fract-matrices-transformations.md
  • references/glsl-pattern-symmetry-truchet-domain-warping.md
  • references/glsl-noise-random-perlin-simplex-cellular-voronoi.md
  • references/glsl-cellular-voronoi-worley-noise-patterns.md
  • references/glsl-fbm-fractional-brownian-motion-turbulence-octaves.md
  • references/glsl-procedural-textures-clouds-marble-wood-terrain.md
  • references/glsl-shader-builtin-functions-complete-api-reference.md
  • Online Editor: editor.thebookofshaders.com
  • glslViewer: CLI tool for running .frag files
  • glslCanvas: HTML embed for live shaders
  • ShaderToy: iTime, iResolution, iMouse uniforms

GLSL shader patterns are based on mathematical techniques that are generally in the public domain. The Book of Shaders is CC BY-NC-SA 4.0 — reference it for learning but write your own implementations.

  • WebGL2 vs WebGPU shader languages diverge — GLSL ES 3.0 vs WGSL; cross-compilation requires explicit toolchain.
  • Mobile GPUs have tiled architectures — clear-on-bind matters, full-buffer reads kill perf.
  • Float32 precision differs by GPU vendor on mobile — Adreno is mediump enough; Mali sometimes isn’t.