shader-programming-glsl
它能做什么
这份指南教你如何使用一种名为 GLSL 的专用语言,为游戏和网站制作视觉特效。它会告诉你如何让画面更好看,同时运行得更快。
适用场景
- ✓在游戏或网站中制作自定义视觉特效时。
- ✓优化画面在屏幕上的显示速度时。
- ✓为图像添加模糊、发光等效果时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/shader-programming-glsl/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我该如何用 GLSL 给一个 3D 物体添加发光效果?”
每周 5 个新技能,1 分钟读完。
工作原理
GLSL 用来直接向显卡(GPU)下达指令。它主要有两个部分:一个负责定义形状(顶点着色器),另一个负责决定屏幕上每个微小像素点的颜色(片元着色器)。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Shader Programming GLSL
Overview
A comprehensive guide to writing GPU shaders using GLSL (OpenGL Shading Language). Learn syntax, uniforms, varying variables, and key mathematical concepts like swizzling and vector operations for visual effects.
When to Use This Skill
- Use when creating custom visual effects in WebGL, Three.js, or game engines.
- Use when optimizing graphics rendering performance.
- Use when implementing post-processing effects (blur, bloom, color correction).
- Use when procedurally generating textures or geometry on the GPU.
Step-by-Step Guide
1. Structure: Vertex vs. Fragment
Understand the pipeline:
- Vertex Shader: Transforms 3D coordinates to 2D screen space (
gl_Position). - Fragment Shader: Colors individual pixels (
gl_FragColor).
// Vertex Shader (basic)
attribute vec3 position;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
// Fragment Shader (basic)
uniform vec3 color;
void main() {
gl_FragColor = vec4(color, 1.0);
}
2. Uniforms and Varyings
uniform: Data constant for all vertices/fragments (passed from CPU).varying: Data interpolated from vertex to fragment shader.
// Passing UV coordinates
varying vec2 vUv;
// In Vertex Shader
void main() {
vUv = uv;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
// In Fragment Shader
void main() {
// Gradient based on UV
gl_FragColor = vec4(vUv.x, vUv.y, 1.0, 1.0);
}
3. Swizzling & Vector Math
Access vector components freely: vec4 color = vec4(1.0, 0.5, 0.0, 1.0);
color.rgb->vec3(1.0, 0.5, 0.0)color.zyx->vec3(0.0, 0.5, 1.0)(reordering)
Examples
Example 1: Simple Raymarching (SDF Sphere)
float sdSphere(vec3 p, float s) {
return length(p) - s;
}
void mainImage(out vec4 fragColor, in vec2 fragCoord) {
vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
vec3 ro = vec3(0.0, 0.0, -3.0); // Ray Origin
vec3 rd = normalize(vec3(uv, 1.0)); // Ray Direction
float t = 0.0;
for(int i = 0; i < 64; i++) {
vec3 p = ro + rd * t;
float d = sdSphere(p, 1.0); // Sphere radius 1.0
if(d < 0.001) break;
t += d;
}
vec3 col = vec3(0.0);
if(t < 10.0) {
vec3 p = ro + rd * t;
vec3 normal = normalize(p);
col = normal * 0.5 + 0.5; // Color by normal
}
fragColor = vec4(col, 1.0);
}
Best Practices
- ✅ Do: Use
mix()for linear interpolation instead of manual math. - ✅ Do: Use
step()andsmoothstep()for thresholding and soft edges (avoidifbranches). - ✅ Do: Pack data into vectors (
vec4) to minimize memory access. - ❌ Don't: Use heavy branching (
if-else) inside loops if possible; it hurts GPU parallelism. - ❌ Don't: Calculate constant values inside the shader; pre-calculate them on the CPU (uniforms).
Troubleshooting
Problem: Shader compiles but screen is black.
Solution: Check if gl_Position.w is correct (usually 1.0). Check if uniforms are actually being set from the host application. Verify UV coordinates are within [0, 1].
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题
什么是 “uniforms” 和 “varyings”?
“uniforms” 是对所有物体都保持不变的信息,比如整体颜色。“varyings” 是在不同点之间会发生变化的信息,比如纹理细节。
为什么不建议在着色器里使用 “if” 语句?
“if” 可能会拖慢显卡速度,因为显卡是同时处理大量任务的。最好改用其他对 GPU 更友好、速度更快的函数。
着色器能编译通过,但屏幕是黑的,可能是什么问题?
检查一下坐标是否正确,并确认主程序是否正确设置了发送给着色器的信息(uniforms)。
详情
作者的原始描述
Expert guide for writing efficient GLSL shaders (Vertex/Fragment) for web and game engines, covering syntax, uniforms, and common effects.
热度
- GitHub 星标
- 48k
- 排名
- #5769
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。