Shader dev

From bighardperson/computer-science-skills-collection · View interactive version

Shader dev is an AI agent skill from the GitHub repository bighardperson/computer-science-skills-collection. Comprehensive GLSL shader techniques for creating stunning visual effects — ray marching, SDF modeling, fluid simulation, particle systems, procedural generation, lighting, post-processing, and more.

How to use this skill

Skill file

Shader Craft

A unified skill covering 36 GLSL shader techniques (ShaderToy-compatible) for real-time visual effects.

Invocation

/shader-dev <request>

$ARGUMENTS contains the user's request (e.g. "create a raymarched SDF scene with soft shadows").

Skill Structure

shader-dev/
├── SKILL.md                      # Core skill (this file)
├── techniques/                   # Implementation guides (read per routing table)
│   ├── ray-marching.md           # Sphere tracing with SDF
│   ├── sdf-3d.md                 # 3D signed distance functions
│   ├── lighting-model.md         # PBR, Phong, toon shading
│   ├── procedural-noise.md       # Perlin, Simplex, FBM
│   └── ...                       # 34 more technique files
└── reference/                    # Detailed guides (read as needed)
    ├── ray-marching.md           # Math derivations & advanced patterns
    ├── sdf-3d.md                 # Extended SDF theory
    ├── lighting-model.md         # Lighting math deep-dive
    ├── procedural-noise.md       # Noise function theory
    └── ...                       # 34 more reference files

How to Use

  1. Read the Technique Routing Table below to identify which technique(s) match the user's request
  2. Read the relevant file(s) from techniques/ — each file contains core principles, implementation steps, and complete code templates
  3. If you need deeper understanding (math derivations, advanced patterns), follow the reference link at the bottom of each technique file to reference/
  4. Apply the WebGL2 Adaptation Rules below when generating standalone HTML pages

Technique Routing Table

| User wants to create... | Primary technique | Combine with |

|---|---|---|

| 3D objects / scenes from math | ray-marching + sdf-3d | lighting-model, shadow-techniques |

| Complex 3D shapes (booleans, blends) | csg-boolean-operations | sdf-3d, ray-marching |

| Infinite repeating patterns in 3D | domain-repetition | sdf-3d, ray-marching |

| Organic / warped shapes | domain-warping | procedural-noise |

| Fluid / smoke / ink effects | fluid-simulation | multipass-buffer |

| Particle effects (fire, sparks, snow) | particle-system | procedural-noise, color-palette |

| Physically-based simulations | simulation-physics | multipass-buffer |

| Game of Life / reaction-diffusion | cellular-automata | multipass-buffer, color-palette |

| Ocean / water surface | water-ocean | atmospheric-scattering, lighting-model |

| Terrain / landscape | terrain-rendering | atmospheric-scattering, procedural-noise |

| Clouds / fog / volumetric fire | volumetric-rendering | procedural-noise, atmospheric-scattering |

| Sky / sunset / atmosphere | atmospheric-scattering | volumetric-rendering |

| Realistic lighting (PBR, Phong) | lighting-model | shadow-techniques, ambient-occlusion |

| Shadows (soft / hard) | shadow-techniques | lighting-model |

| Ambient occlusion | ambient-occlusion | lighting-model, normal-estimation |

| Path tracing / global illumination | path-tracing-gi | analytic-ray-tracing, multipass-buffer |

| Precise ray-geometry intersections | analytic-ray-tracing | lighting-model |

| Voxel worlds (Minecraft-style) | voxel-rendering | lighting-model, shadow-techniques |

| Noise / FBM textures | procedural-noise | domain-warping |

| Tiled 2D patterns | procedural-2d-pattern | polar-uv-manipulation |

| Voronoi / cell patterns | voronoi-cellular-noise | color-palette |

| Fractals (Mandelbrot, Julia, 3D) | fractal-rendering | color-palette, polar-uv-manipulation |

| Color grading / palettes | color-palette | — |

| Bloom / tone mapping / glitch | post-processing | multipass-buffer |

| Multi-pass ping-pong buffers | multipass-buffer | — |

| Texture / sampling techniques | texture-sampling | — |

| Camera / matrix transforms | matrix-transform | — |

| Surface normals | normal-estimation | — |

| Polar coords / kaleidoscope | polar-uv-manipulation | procedural-2d-pattern |

| 2D shapes / UI from SDF | sdf-2d | color-palette |

| Procedural audio / music | sound-synthesis | — |

| SDF tricks / optimization | sdf-tricks | sdf-3d, ray-marching |

| Anti-aliased rendering | anti-aliasing | sdf-2d, post-processing |

| Depth of field / motion blur / lens effects | camera-effects | post-processing, multipass-buffer |

| Advanced texture mapping / no-tile textures | texture-mapping-advanced | terrain-rendering, texture-sampling |

| WebGL2 shader errors / debugging | webgl-pitfalls | — |

Technique Index

Geometry & SDF

Ray Casting & Lighting

Simulation & Physics

Natural Phenomena

Procedural Generation

Post-Processing & Infrastructure

Audio

Debugging & Validation

WebGL2 Adaptation Rules

All technique files use ShaderToy GLSL style. When generating standalone HTML pages, apply these adaptations:

Shader Version & Output

Fragment Coordinate

// WRONG
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
// CORRECT
vec2 uv = (2.0 * gl_FragCoord.xy - iResolution.xy) / iResolution.y;

main() Wrapper for ShaderToy Templates

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    // shader code...
    fragColor = vec4(col, 1.0);
}

void main() {
    mainImage(fragColor, gl_FragCoord.xy);
}

Function Declaration Order

// WRONG — getAtmosphere() calls getSunDirection() before it's defined
vec3 getAtmosphere(vec3 dir) { return getSunDirection(); } // Error!
vec3 getSunDirection() { return normalize(vec3(1.0)); }

// CORRECT — define callee first
vec3 getSunDirection() { return normalize(vec3(1.0)); }
vec3 getAtmosphere(vec3 dir) { return getSunDirection(); } // Works

Macro Limitations

// WRONG
#define SUN_DIR normalize(vec3(0.8, 0.4, -0.6))

// CORRECT
const vec3 SUN_DIR = vec3(0.756, 0.378, -0.567); // Pre-computed normalized value

Script Tag Extraction

const fs = document.getElementById('fs').text.trim();

Common Pitfalls

HTML Page Setup

When generating a standalone HTML page:

Common Pitfalls

JS Variable Declaration Order (TDZ — causes white screen crash)

let/const variables must be declared at the top of the <script> block, before any function that references them:

// 1. State variables FIRST
let frameCount = 0;
let startTime = Date.now();

// 2. Canvas/GL init, shader compile, FBO creation
const canvas = document.getElementById('canvas');
const gl = canvas.getContext('webgl2');
// ...

// 3. Functions and event bindings LAST
function resize() { /* can now safely reference frameCount */ }
function render() { /* ... */ }
window.addEventListener('resize', resize);

Reason: let/const have a Temporal Dead Zone — referencing them before declaration throws ReferenceError, causing a white screen.

GLSL Compilation Errors (self-check after writing shaders)

Performance Budget

Deployment environments may use headless software rendering with limited GPU power. Stay within these limits:

Quick Recipes

Common effect combinations — complete rendering pipelines assembled from technique modules.

Photorealistic SDF Scene

  1. Geometry: sdf-3d (extended primitives) + csg-boolean-operations (cubic/quartic smin)
  2. Rendering: ray-marching + normal-estimation (tetrahedron method)
  3. Lighting: lighting-model (outdoor three-light model) + shadow-techniques (improved soft shadow) + ambient-occlusion
  4. Atmosphere: atmospheric-scattering (height-based fog with sun tint)
  5. Post: post-processing (ACES tone mapping) + anti-aliasing (2x SSAA) + camera-effects (vignette)

Organic / Biological Forms

  1. Geometry: sdf-3d (extended primitives + deformation operators: twist, bend) + csg-boolean (gradient-aware smin for material blending)
  2. Detail: procedural-noise (FBM with derivatives) + domain-warping
  3. Surface: lighting-model (subsurface scattering approximation via half-Lambert)

Procedural Landscape

  1. Terrain: terrain-rendering + procedural-noise (erosion FBM with derivatives)
  2. Texturing: texture-mapping-advanced (biplanar mapping + no-tile)
  3. Sky: atmospheric-scattering (Rayleigh/Mie + height fog)
  4. Water: water-ocean (Gerstner waves) + lighting-model (Fresnel reflections)

Stylized 2D Art

  1. Shapes: sdf-2d (extended library) + sdf-tricks (layered edges, hollowing)
  2. Color: color-palette (cosine palettes) + polar-uv-manipulation (kaleidoscope)
  3. Polish: anti-aliasing (SDF analytical AA) + post-processing (bloom, chromatic aberration)

Shader Debugging Techniques

Visual debugging methods — temporarily replace your output to diagnose issues.

| What to check | Code | What to look for |

|---|---|---|

| Surface normals | col = nor * 0.5 + 0.5; | Smooth gradients = correct normals; banding = epsilon too large |

| Ray march step count | col = vec3(float(steps) / float(MAX_STEPS)); | Red hotspots = performance bottleneck; uniform = wasted iterations |

| Depth / distance | col = vec3(t / MAX_DIST); | Verify correct hit distances |

| UV coordinates | col = vec3(uv, 0.0); | Check coordinate mapping |

| SDF distance field | col = (d > 0.0 ? vec3(0.9,0.6,0.3) : vec3(0.4,0.7,0.85)) * (0.8 + 0.2*cos(150.0*d)); | Visualize SDF bands and zero-crossing |

| Checker pattern (UV) | col = vec3(mod(floor(uv.x*10.)+floor(uv.y*10.), 2.0)); | Verify UV distortion, seams |

| Lighting only | col = vec3(shadow); or col = vec3(ao); | Isolate shadow/AO contributions |

| Material ID | col = palette(matId / maxMatId); | Verify material assignment |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.