Shadertoy Shader Development
Overview
Shadertoy is a platform for creating and sharing GLSL fragment shaders that run in the browser using WebGL. This skill provides comprehensive guidance for writing shaders including GLSL ES syntax, common patterns, mathematical techniques, and best practices specific to real-time procedural graphics.
When to Use This Skill
Activate this skill when:
- Writing or editing
.glslshader files - Creating procedural graphics, generative art, or visual effects
- Working with Shadertoy.com projects or WebGL fragment shaders
- Implementing ray marching, distance fields, or procedural textures
- Debugging shader code or optimizing shader performance
- Need GLSL ES syntax reference or Shadertoy input variables
Core Concepts
Shader Entry Point
Every Shadertoy shader implements the mainImage function:
Shadertoy Built-in Inputs
Always available in shaders:
Coordinate System Setup
Standard patterns for normalizing coordinates:
Common Shader Patterns
1. Procedural Color Palettes
Use Inigo Quilez's cosine palette for smooth color gradients:
2. Hash Functions (Pseudo-Random)
Simple 2D hash for noise and randomness:
3. Ray Marching
Standard pattern for 3D rendering via sphere tracing:
4. Rotations
2D rotation:
3D axis-angle rotation (modifies in-place):
5. Domain Repetition and Folding
Create fractal-like structures:
6. Post-Processing
Vignette:
Film grain/dithering (reduces banding):
Gamma correction:
Multi-Pass Rendering
For complex effects requiring temporal feedback or multiple rendering stages:
Buffer A (Computation):
Buffer B (Feedback/Blending):
Main (Final Output):
Critical GLSL ES Rules
ALWAYS follow these rules to avoid compilation errors:
- NO
fsuffix: Use1.0NOT1.0f - NO
saturate(): Useclamp(x, 0.0, 1.0)instead - Protect pow/sqrt: Wrap arguments:
pow(max(x, 0.0), p),sqrt(abs(x)) - Avoid division by zero: Check denominators or add epsilon
- Initialize variables: Don't assume default values
- Avoid name conflicts: Don't name functions like variables
- NO interactive commands: Avoid
find,grep- use Glob/Grep tools instead
Workflow Guide
Creating a New Shader
- Set up coordinate system - Choose appropriate UV normalization
- Define core effect - Implement main visual algorithm
- Add animation - Use
iTimefor temporal variation - Apply color palette - Use cosine palette or custom scheme
- Add post-processing - Vignette, dither, gamma correction
- Optimize - Reduce iterations, use early exits, minimize branches
Common Tasks
Visualizing complex numbers:
- Use the complex math functions in
references/common-patterns.md - Plot with
cx_log(),cx_pow(), or polynomial evaluation - Map complex results to color via palette
Ray marching 3D scenes:
- Define distance field in
map()function - Set up camera (ray origin
ro, ray directionrd) - March using standard loop pattern
- Calculate normals with tetrahedron method
- Apply lighting and material properties
Creating noise/organic effects:
- Use
hash21()for random values - Implement
fbm()(fractional Brownian motion) for natural variation - Combine with
sin()/cos()for structured patterns - Apply domain warping for organic distortion
Multi-layer composition:
- Render multiple passes with different parameters
- Blend layers using
mix()or custom blend modes - Add interference patterns by comparing layer differences
- Use
smoothstep()for soft transitions
Debugging Strategies
Visualize intermediate values:
Simplify progressively:
- Comment out post-processing
- Reduce iteration counts
- Replace complex functions with simple placeholders
- Check coordinate transformations step-by-step
Check for NaN/Inf:
- Add guards:
if (isnan(value) || isinf(value)) return vec3(1.0, 0.0, 0.0); - Validate divisions and roots
Performance Optimization
- Fixed iteration counts - Avoid dynamic loops
- Early exit conditions - Break when threshold met
- Step multiplier tuning - Balance quality vs speed (0.5 to 1.0)
- Minimize texture reads - Cache repeated lookups
- Avoid conditionals - Use
mix(),step(),smoothstep()instead ofif - Reduce precision - Use
mediumporlowpwhere appropriate (mobile)
Naming Conventions
Based on observed patterns in creative work:
- Poetic/evocative names - "alien-water", "heavenly-wisp", "comprehension"
- Technical descriptors - "complex-plot", "noise-circuits", "ray-marching-demo"
- Compound phrases - "coming-apart-at-the-seams", "form-without-form"
- Lowercase with hyphens -
my-shader-name.glsl
Attribution and Forking
When forking or remixing shaders:
Resources
references/glsl-reference.md
Complete GLSL ES syntax reference including:
- Built-in functions (trig, math, vectors, matrices, textures)
- Shadertoy input variables specification
- Type conversions and swizzling
- Common pitfalls and corrections
Search with: Read /references/glsl-reference.md for complete language reference.
references/common-patterns.md
Comprehensive pattern library including:
- Complex number mathematics (cx_mul, cx_div, cx_sin, cx_cos, cx_log, cx_pow)
- Color palette functions (cosine palette, multi-layer palettes)
- Hash functions (hash21, PCG hash)
- Ray marching templates (render loop, normal calculation)
- 3D transformations (rotations, domain folding)
- Distance fields (sphere, box, octahedron)
- Noise functions (simplex, FBM)
- Post-processing (vignette, blur, film grain, gamma)
- Blend modes (soft light, hard light, vivid light)
- Multi-pass rendering patterns
Search with: Grep "pattern" references/common-patterns.md for specific techniques.
references/example-compact-shader.glsl
Reference implementation showing:
- Compact, algorithmic shader coding style
- Efficient ray marching in minimal code
- Advanced matrix operations and transformations
- Creative Commons licensed example
Quick Reference
Common Shader Types in Collection
- Mathematical Visualizations - Complex number plots, function graphs
- Ray Marched 3D - Distance field rendering, folded geometries
- Procedural Textures - Noise-based patterns, organic effects
- Multi-Pass Effects - Temporal feedback, buffer composition
- Particle Systems - Point-based simulations
- 2D Patterns - Geometric, kaleidoscopic, interference effects
Tips for Creative Coding
- Start simple - Get basic structure working, then iterate
- Use time creatively -
sin(iTime),mod(iTime, period),smoothstep()transitions - Layer effects - Combine multiple techniques for richness
- Embrace accidents - Bugs often lead to interesting visuals
- Study references - Learn from existing shaders, understand techniques
- Optimize later - Prioritize visual quality first, then performance

