by sickn33
创建令人惊艳的视觉效果并优化图形渲染。本指南将教授 GLSL 语法、顶点和片段着色器,以及用于实时图形的着色器数学。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 shader-programming-glsl 技能" 开始使用
=== shader-programming-glsl 技能 === 作者: sickn33 描述: 创建令人惊艳的视觉效果并优化图形渲染。本指南将教授 GLSL 语法、顶点和片段着色器,以及用于实时图形的着色器数学。 使用方法: 1. 调用技能: "使用 shader-programming-glsl 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 shader-programming-glsl 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
coding
safe
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.
Understand the pipeline:
gl_Position).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);
}
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);
}
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)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);
}
mix() for linear interpolation instead of manual math.step() and smoothstep() for thresholding and soft edges (avoid if branches).vec4) to minimize memory access.if-else) inside loops if possible; it hurts GPU parallelism.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].
View Count
0
Download Count
0
Favorite Count
0
Quality Score
70