Loading...
Loading...
Compare original and translation side by side
/shader-dev <request>$ARGUMENTS/shader-dev <request>$ARGUMENTSshader-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 filesshader-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个参考文件techniques/reference/techniques/reference/| 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 | — |
| 用户想要创建... | 核心技术 | 可结合使用的技术 |
|---|---|---|
| 基于数学的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 |
| 体素世界(我的世界风格) | voxel-rendering | lighting-model、shadow-techniques |
| 噪声/FBM纹理 | procedural-noise | domain-warping |
| 平铺2D图案 | procedural-2d-pattern | polar-uv-manipulation |
| Voronoi/细胞图案 | voronoi-cellular-noise | color-palette |
| 分形(曼德博集合、朱利亚集合、3D分形) | fractal-rendering | color-palette、polar-uv-manipulation |
| 颜色分级/调色板 | color-palette | — |
| Bloom/色调映射/故障效果 | 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 | — |
fragCoordmain()fragCoordmain()canvas.getContext("webgl2")#version 300 esprecision highp float;out vec4 fragColor;attributeinvaryingoutvaryingingl_FragColorfragColortexture2D()texture()canvas.getContext("webgl2")#version 300 esprecision highp float;out vec4 fragColor;attributeinvaryingoutvaryingingl_FragColorfragColortexture2D()texture()gl_FragCoord.xyfragCoordfragCoord// WRONG
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
// CORRECT
vec2 uv = (2.0 * gl_FragCoord.xy - iResolution.xy) / iResolution.y;gl_FragCoord.xyfragCoordfragCoord// 错误写法
vec2 uv = (2.0 * fragCoord - iResolution.xy) / iResolution.y;
// 正确写法
vec2 uv = (2.0 * gl_FragCoord.xy - iResolution.xy) / iResolution.y;void mainImage(out vec4 fragColor, in vec2 fragCoord)void main()void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// shader code...
fragColor = vec4(col, 1.0);
}
void main() {
mainImage(fragColor, gl_FragCoord.xy);
}void mainImage(out vec4 fragColor, in vec2 fragCoord)void main()void mainImage(out vec4 fragColor, in vec2 fragCoord) {
// 着色器代码...
fragColor = vec4(col, 1.0);
}
void main() {
mainImage(fragColor, gl_FragCoord.xy);
}// 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// 错误写法 — getAtmosphere()在getSunDirection()定义前调用它
vec3 getAtmosphere(vec3 dir) { return getSunDirection(); } // 错误!
vec3 getSunDirection() { return normalize(vec3(1.0)); }
// 正确写法 — 先定义被调用的函数
vec3 getSunDirection() { return normalize(vec3(1.0)); }
vec3 getAtmosphere(vec3 dir) { return getSunDirection(); } // 可正常运行#defineconst// 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#defineconst// 错误写法
#define SUN_DIR normalize(vec3(0.8, 0.4, -0.6))
// 正确写法
const vec3 SUN_DIR = vec3(0.756, 0.378, -0.567); // 预计算的归一化值<script>#version.trim()const fs = document.getElementById('fs').text.trim();<script>#version.trim()const fs = document.getElementById('fs').text.trim();gl.getUniformLocation()null#defineterrainM(vec2)terrainM(pos.xz + offset)terrainM(pos + offset)gl.getUniformLocation()null#defineterrainM(vec2)terrainM(pos.xz + offset)terrainM(pos + offset)body { margin: 0; overflow: hidden; background: #000; }iTimeiResolutioniMouseiFramebody { margin: 0; overflow: hidden; background: #000; }iTimeiResolutioniMouseiFrameletconst<script>// 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);letconstReferenceErrorletconst<script>// 1. 状态变量优先声明
let frameCount = 0;
let startTime = Date.now();
// 2. Canvas/GL初始化、着色器编译、FBO创建
const canvas = document.getElementById('canvas');
const gl = canvas.getContext('webgl2');
// ...
// 3. 函数和事件绑定最后定义
function resize() { /* 现在可以安全引用frameCount */ }
function render() { /* ... */ }
window.addEventListener('resize', resize);letconstReferenceErrorfloat fbm(vec3 p)fbm(uv)vec2patchcastsamplefilterinputoutputcommonpartitionactivevec3 x = 1.0vec3 x = vec3(1.0).zvec2ifelsefloat fbm(vec3 p)vec2uvfbm(uv)patchcastsamplefilterinputoutputcommonpartitionactivevec3 x = 1.0vec3 x = vec3(1.0).zvec2ifelse| What to check | Code | What to look for |
|---|---|---|
| Surface normals | | Smooth gradients = correct normals; banding = epsilon too large |
| Ray march step count | | Red hotspots = performance bottleneck; uniform = wasted iterations |
| Depth / distance | | Verify correct hit distances |
| UV coordinates | | Check coordinate mapping |
| SDF distance field | | Visualize SDF bands and zero-crossing |
| Checker pattern (UV) | | Verify UV distortion, seams |
| Lighting only | | Isolate shadow/AO contributions |
| Material ID | | Verify material assignment |
| 检查内容 | 代码 | 观察要点 |
|---|---|---|
| 表面法线 | | 平滑渐变=法线正确;条带化=epsilon值过大 |
| 光线步进步数 | | 红色热点=性能瓶颈;颜色均匀=迭代次数浪费 |
| 深度/距离 | | 验证正确的命中距离 |
| UV坐标 | | 检查坐标映射 |
| SDF距离场 | | 可视化SDF条带和零交叉点 |
| 棋盘格图案(UV) | | 验证UV扭曲、接缝 |
| 仅光照 | | 隔离阴影/环境光遮蔽的贡献 |
| 材质ID | | 验证材质分配 |