Shader Craft
A unified skill covering 36 GLSL shader techniques (ShaderToy-compatible) for real-time visual effects.
Invocation
CODEBLOCK0
INLINECODE0 contains the user's request (e.g. "create a raymarched SDF scene with soft shadows").
Skill Structure
CODEBLOCK1
How to Use
- 1. Read the Technique Routing Table below to identify which technique(s) match the user's request
- Read the relevant file(s) from
techniques/ — each file contains core principles, implementation steps, and complete code templates - If you need deeper understanding (math derivations, advanced patterns), follow the reference link at the bottom of each technique file to INLINECODE2
- 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
- - sdf-2d — 2D signed distance functions for shapes, UI, anti-aliased rendering
- sdf-3d — 3D signed distance functions for real-time implicit surface modeling
- csg-boolean-operations — Constructive solid geometry: union, subtraction, intersection with smooth blending
- domain-repetition — Infinite space repetition, folding, and limited tiling
- domain-warping — Distort domains with noise for organic, flowing shapes
- sdf-tricks — SDF optimization, bounding volumes, binary search refinement, hollowing, layered edges, debug visualization
Ray Casting & Lighting
- - ray-marching — Sphere tracing with SDF for 3D scene rendering
- analytic-ray-tracing — Closed-form ray-primitive intersections (sphere, plane, box, torus)
- path-tracing-gi — Monte Carlo path tracing for photorealistic global illumination
- lighting-model — Phong, Blinn-Phong, PBR (Cook-Torrance), and toon shading
- shadow-techniques — Hard shadows, soft shadows (penumbra estimation), cascade shadows
- ambient-occlusion — SDF-based AO, screen-space AO approximation
- normal-estimation — Finite-difference normals, tetrahedron technique
Simulation & Physics
- - fluid-simulation — Navier-Stokes fluid solver with advection, diffusion, pressure projection
- simulation-physics — GPU-based physics: springs, cloth, N-body gravity, collision
- particle-system — Stateless and stateful particle systems (fire, rain, sparks, galaxies)
- cellular-automata — Game of Life, reaction-diffusion (Turing patterns), sand simulation
Natural Phenomena
- - water-ocean — Gerstner waves, FFT ocean, caustics, underwater fog
- terrain-rendering — Heightfield ray marching, FBM terrain, erosion
- atmospheric-scattering — Rayleigh/Mie scattering, god rays, SSS approximation
- volumetric-rendering — Volume ray marching for clouds, fog, fire, explosions
Procedural Generation
- - procedural-noise — Value noise, Perlin, Simplex, Worley, FBM, ridged noise
- procedural-2d-pattern — Brick, hexagon, truchet, Islamic geometric patterns
- voronoi-cellular-noise — Voronoi diagrams, Worley noise, cracked earth, crystal
- fractal-rendering — Mandelbrot, Julia sets, 3D fractals (Mandelbox, Mandelbulb)
- color-palette — Cosine palettes, HSL/HSV/Oklab, dynamic color mapping
Post-Processing & Infrastructure
- - post-processing — Bloom, tone mapping (ACES, Reinhard), vignette, chromatic aberration, glitch
- multipass-buffer — Ping-pong FBO setup, state persistence across frames
- texture-sampling — Bilinear, bicubic, mipmap, procedural texture lookup
- matrix-transform — Camera look-at, projection, rotation, orbit controls
- polar-uv-manipulation — Polar/log-polar coordinates, kaleidoscope, spiral mapping
- anti-aliasing — SSAA, SDF analytical AA, temporal anti-aliasing (TAA), FXAA post-process
- camera-effects — Depth of field (thin lens), motion blur, lens distortion, film grain, vignette
- texture-mapping-advanced — Biplanar mapping, texture repetition avoidance, ray differential filtering
Audio
- - sound-synthesis — Procedural audio in GLSL: oscillators, envelopes, filters, FM synthesis
Debugging & Validation
- - webgl-pitfalls — Common WebGL2/GLSL errors:
fragCoord, main() wrapper, function order, macro limitations, uniform null
WebGL2 Adaptation Rules
All technique files use ShaderToy GLSL style. When generating standalone HTML pages, apply these adaptations:
Shader Version & Output
- - Use INLINECODE5
- Shader first line:
#version 300 es, fragment shader adds INLINECODE7 - Fragment shader must declare: INLINECODE8
- Vertex shader:
attribute → in, varying → INLINECODE12 - Fragment shader:
varying → in, gl_FragColor → fragColor, texture2D() → INLINECODE18
Fragment Coordinate
- - Use
gl_FragCoord.xy instead of fragCoord (WebGL2 does not have fragCoord built-in)
CODEBLOCK2
main() Wrapper for ShaderToy Templates
- - ShaderToy uses INLINECODE22
- WebGL2 requires standard
void main() entry point — always wrap mainImage:
CODEBLOCK3
Function Declaration Order
- - GLSL requires functions to be declared before use — either declare before use or reorder:
CODEBLOCK4
Macro Limitations
- -
#define cannot use function calls — use const instead:
CODEBLOCK5
Script Tag Extraction
- - When extracting shader source from
<script> tags, ensure #version is the first character — use .trim():
CODEBLOCK6
Common Pitfalls
- - Unused uniforms: Compiler may optimize away unused uniforms, causing
gl.getUniformLocation() to return null — always use uniforms in a way the compiler cannot optimize out - Loop indices: Use runtime constants in loops, not
#define macros in some ES versions - Terrain functions: Functions like
terrainM(vec2) need XZ components — use terrainM(pos.xz + offset) not INLINECODE34
HTML Page Setup
When generating a standalone HTML page:
- - Canvas fills the entire viewport, auto-resizes on window resize
- Page background black, no scrollbars: INLINECODE35
- Implement ShaderToy-compatible uniforms:
iTime, iResolution, iMouse, INLINECODE39 - For multi-pass effects (Buffer A/B), use WebGL2 framebuffer + ping-pong (see multipass-buffer technique)
Common Pitfalls
JS Variable Declaration Order (TDZ — causes white screen crash)
INLINECODE40 /const variables must be declared at the top of the <script> block, before any function that references them:
CODEBLOCK7
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)
- - Function signature mismatch: Call must exactly match definition in parameter count and types. If defined as
float fbm(vec3 p), cannot call fbm(uv) with a INLINECODE48 - Reserved words as variable names: Do not use:
patch, cast, sample, filter, input, output, common, partition, INLINECODE57 - Strict type matching:
vec3 x = 1.0 is illegal — use vec3 x = vec3(1.0); cannot use .z to access a INLINECODE61 - No ternary on structs: ESSL does not allow ternary operator on struct types — use
if/else instead
Performance Budget
Deployment environments may use headless software rendering with limited GPU power. Stay within these limits:
- - Ray marching main loop: ≤ 128 steps
- Volume sampling / lighting inner loops: ≤ 32 steps
- FBM octaves: ≤ 6 layers
- Total nested loop iterations per pixel: ≤ 1000 (exceeding this freezes the browser)
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)
- Rendering: ray-marching + normal-estimation (tetrahedron method)
- Lighting: lighting-model (outdoor three-light model) + shadow-techniques (improved soft shadow) + ambient-occlusion
- Atmosphere: atmospheric-scattering (height-based fog with sun tint)
- 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)
- Detail: procedural-noise (FBM with derivatives) + domain-warping
- Surface: lighting-model (subsurface scattering approximation via half-Lambert)
Procedural Landscape
- 1. Terrain: terrain-rendering + procedural-noise (erosion FBM with derivatives)
- Texturing: texture-mapping-advanced (biplanar mapping + no-tile)
- Sky: atmospheric-scattering (Rayleigh/Mie + height fog)
- Water: water-ocean (Gerstner waves) + lighting-model (Fresnel reflections)
Stylized 2D Art
- 1. Shapes: sdf-2d (extended library) + sdf-tricks (layered edges, hollowing)
- Color: color-palette (cosine palettes) + polar-uv-manipulation (kaleidoscope)
- 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 | INLINECODE64 | 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 |
Shader Craft
一个统一的技能,涵盖36种GLSL着色器技术(兼容ShaderToy),用于实时视觉效果。
调用方式
/shader-dev <请求>
$ARGUMENTS 包含用户的请求(例如创建一个带柔和阴影的光线步进SDF场景)。
技能结构
shader-dev/
├── SKILL.md # 核心技能(本文件)
├── techniques/ # 实现指南(根据路由表读取)
│ ├── ray-marching.md # 基于SDF的球体追踪
│ ├── sdf-3d.md # 3D有符号距离函数
│ ├── lighting-model.md # PBR、Phong、卡通着色
│ ├── procedural-noise.md # Perlin、Simplex、FBM噪声
│ └── ... # 另外34个技术文件
└── reference/ # 详细指南(按需读取)
├── ray-marching.md # 数学推导与高级模式
├── sdf-3d.md # 扩展SDF理论
├── lighting-model.md # 光照数学深入
├── procedural-noise.md # 噪声函数理论
└── ... # 另外34个参考文件
使用方法
- 1. 阅读下方的技术路由表,确定与用户请求匹配的技术
- 从 techniques/ 中读取相关文件——每个文件包含核心原理、实现步骤和完整代码模板
- 如需更深入的理解(数学推导、高级模式),按照每个技术文件底部的参考链接进入 reference/
- 在生成独立HTML页面时,应用下方的WebGL2适配规则
技术路由表
| 用户想要创建... | 主要技术 | 可组合的技术 |
|---|
| 基于数学的3D物体/场景 | ray-marching + sdf-3d | lighting-model, shadow-techniques |
| 复杂3D形状(布尔运算、混合) |
csg-boolean-operations | sdf-3d, ray-marching |
| 无限重复的3D图案 |
domain-repetition | sdf-3d, ray-marching |
| 有机/扭曲形状 |
domain-warping | procedural-noise |
| 流体/烟雾/墨迹效果 |
fluid-simulation | multipass-buffer |
| 粒子效果(火焰、火花、雪) |
particle-system | procedural-noise, color-palette |
| 基于物理的模拟 |
simulation-physics | multipass-buffer |
| 生命游戏/反应扩散 |
cellular-automata | multipass-buffer, color-palette |
| 海洋/水面 |
water-ocean | atmospheric-scattering, lighting-model |
| 地形/景观 |
terrain-rendering | atmospheric-scattering, procedural-noise |
| 云/雾/体积火焰 |
volumetric-rendering | procedural-noise, atmospheric-scattering |
| 天空/日落/大气 |
atmospheric-scattering | volumetric-rendering |
| 真实感光照(PBR、Phong) |
lighting-model | shadow-techniques, ambient-occlusion |
| 阴影(柔和/硬边) |
shadow-techniques | lighting-model |
| 环境光遮蔽 |
ambient-occlusion | lighting-model, normal-estimation |
| 路径追踪/全局光照 |
path-tracing-gi | analytic-ray-tracing, multipass-buffer |
| 精确光线-几何体相交 |
analytic-ray-tracing | lighting-model |
| 体素世界(Minecraft风格) |
voxel-rendering | lighting-model, shadow-techniques |
| 噪声/FBM纹理 |
procedural-noise | domain-warping |
| 平铺2D图案 |
procedural-2d-pattern | polar-uv-manipulation |
| Voronoi/细胞图案 |
voronoi-cellular-noise | color-palette |
| 分形(Mandelbrot、Julia、3D) |
fractal-rendering | color-palette, polar-uv-manipulation |
| 色彩分级/调色板 |
color-palette | — |
| 泛光/色调映射/故障效果 |
post-processing | multipass-buffer |
| 多通道乒乓缓冲区 |
multipass-buffer | — |
| 纹理/采样技术 |
texture-sampling | — |
| 相机/矩阵变换 |
matrix-transform | — |
| 表面法线 |
normal-estimation | — |
| 极坐标/万花筒 |
polar-uv-manipulation | procedural-2d-pattern |
| 基于SDF的2D形状/UI |
sdf-2d | color-palette |
| 程序化音频/音乐 |
sound-synthesis | — |
| SDF技巧/优化 |
sdf-tricks | sdf-3d, ray-marching |
| 抗锯齿渲染 |
anti-aliasing | sdf-2d, post-processing |
| 景深/运动模糊/镜头效果 |
camera-effects | post-processing, multipass-buffer |
| 高级纹理映射/无平铺纹理 |
texture-mapping-advanced | terrain-rendering, texture-sampling |
| WebGL2着色器错误/调试 |
webgl-pitfalls | — |
技术索引
几何体与SDF
- - sdf-2d — 用于形状、UI、抗锯齿渲染的2D有符号距离函数
- sdf-3d — 用于实时隐式曲面建模的3D有符号距离函数
- csg-boolean-operations — 构造实体几何:并集、差集、交集及平滑混合
- domain-repetition — 无限空间重复、折叠和有限平铺
- domain-warping — 使用噪声扭曲域以产生有机流动形状
- sdf-tricks — SDF优化、包围体、二分搜索细化、掏空、分层边缘、调试可视化
光线投射与光照
- - ray-marching — 使用SDF进行球体追踪的3D场景渲染
- analytic-ray-tracing — 光线-基元相交的闭式解(球体、平面、盒子、环面)
- path-tracing-gi — 用于照片级真实感全局光照的蒙特卡洛路径追踪
- lighting-model — Phong、Blinn-Phong、PBR(Cook-Torrance)和卡通着色
- shadow-techniques — 硬阴影、柔和阴影(半影估计)、级联阴影
- ambient-occlusion — 基于SDF的AO、屏幕空间AO近似
- normal-estimation — 有限差分法线、四面体技术
模拟与物理
- - fluid-simulation — 带平流、扩散、压力投影的Navier-Stokes流体求解器
- simulation-physics — 基于GPU的物理:弹簧、布料、N体引力、碰撞
- particle-system — 无状态和有状态粒子系统(火焰、雨、火花、星系)
- cellular-automata — 生命游戏、反应扩散(图灵图案)、沙粒模拟
自然现象
- - water-ocean — Gerstner波、FFT海洋、焦散、水下雾