Mr.Tang

Godot Shader Development

开发编程v1.0.0
作者:Mr.Tang更新于 2026-05-11godot-shader@clawhub_thb32133451/godot-shader
代码生成前端开发#开发

据 AlphaMoat 统计,截至 2026-09,Godot Shader Development 累计下载 2,307 次、获 0 个星标,在全部 5,078 个 AI 技能中排名第 4,230 位,在「开发编程」分类(577 个)中排名第 520 位。

累计安装
240
累计下载
2,307
星标
-
全站排名
#4,230
全站前 84% · 「开发编程」前 91%
平台累计安装累计下载星标评论
ClawHub ↗301,123--
SkillHub ↗2402,307-—

各平台的计数口径不同,统计时间不一致。

这个技能是做什么的

着色器类型分三类:Spatial(3D 材质/光照/后处理)、CanvasItem(2D 精灵/UI/粒子)、Particles(GPU 粒子)。提供可直接使用的模板:发光、波纹扭曲、颜色替换、溶解、精灵闪烁等 2D 效果;PBR 材质、全息、水面等 3D 着色器;火焰与魔法粒子;屏幕抖动、暗角、色差等后处理。性能优化技巧:预计算常量、用 lowp/mediump 精度、合并纹理到图集、用 mix() 替代 if 减少动态分支、向量化计算。调试可用 ALBEDO = NORMAL * 0.5 + 0.5; 可视化法线、ALBEDO = vec3(UV, 0.0); 可视化 UV,并用 Godot 内置分析器查看 Draw Calls。注意:引用的补充参考文件不在本包内。

资料发布于 2026-10-27

可以用来做什么

  • 为 2D 精灵添加发光、波纹、溶解等效果
  • 编写 PBR 材质、全息或水面 3D 着色器
  • 制作火焰、魔法等 GPU 粒子着色器
  • 优化着色器性能并调试法线/UV

什么时候用

  • 在 Godot 游戏中创建自定义着色器或视觉特效时
  • 需要后处理效果或渲染性能优化时

示例提示词

以下是根据技能文档生成的示例,不代表实际运行结果。

给我的角色精灵加一个发光描边着色器
写一个带扫描线和边缘发光的全息着色器

月度数据趋势

累计下载2,307
3641,4082,4512026-03 · 累计下载 5082026-05 · 累计下载 9612026-06 · 累计下载 1,1092026-07 · 累计下载 1,3992026-08 · 累计下载 1,6852026-09 · 累计下载 2,30726-0326-09
星标-
0112026-03 · 星标 -2026-05 · 星标 -2026-06 · 星标 -2026-07 · 星标 -2026-08 · 星标 -2026-09 · 星标 -26-0326-09

2026-09 起统计方法调整:同名技能按作者分别统计,并覆盖全部技能。图中虚线为调整前的月份,与之后的数据不直接比较,也不计算跨越调整的增长。

月份累计下载环比星标
2026-092,307—-
2026-081,685+20.4%-
2026-071,399+26.1%-
2026-061,109+15.4%-
2026-05961—-
2026-03508—-

SKILL.md 原文

以下是作者发布的原始文件,按纯文本显示,本站未做修改或审核;其中的链接不可点击,命令请核实后再运行。抓取于 2026-10-23。

8.4 KB · ClawHub v1.0.0
---
name: godot-shader
description: Godot 4.x shader development guide covering visual effects, materials, particle shaders, and optimization. Use when creating custom shaders, visual effects, post-processing, or optimizing rendering performance in Godot games. Includes shader templates and common effects.
---

# Godot 着色器开发指南

## 着色器基础

### 着色器类型
展开全部(共 384 行)
```yaml
Spatial (3D):
  - 3D 模型材质
  - 光照和阴影
  - 后处理效果

CanvasItem (2D):
  - 2D 精灵效果
  - UI 特效
  - 粒子系统

Particles:
  - GPU 粒子
  - 高性能粒子效果
```

### 基本结构
```glsl
shader_type canvas_item;

// 顶点着色器(可选)
void vertex() {
    VERTEX += vec2(sin(TIME), cos(TIME));
}

// 片段着色器
void fragment() {
    COLOR = texture(TEXTURE, UV);
}
```

## 常用 2D 效果

### 1. 发光效果
```glsl
shader_type canvas_item;

uniform float glow_intensity : hint_range(0, 10) = 2.0;
uniform vec4 glow_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);

void fragment() {
    vec4 tex_color = texture(TEXTURE, UV);
    
    // 边缘检测
    vec2 size = TEXTURE_PIXEL_SIZE;
    float alpha = 0.0;
    alpha += texture(TEXTURE, UV + vec2(0.0, -size.y)).a;
    alpha += texture(TEXTURE, UV + vec2(0.0, size.y)).a;
    alpha += texture(TEXTURE, UV + vec2(-size.x, 0.0)).a;
    alpha += texture(TEXTURE, UV + vec2(size.x, 0.0)).a;
    alpha = max(0.0, alpha - tex_color.a * 4.0);
    
    COLOR = tex_color + glow_color * alpha * glow_intensity;
}
```

### 2. 波纹扭曲
```glsl
shader_type canvas_item;

uniform float wave_amplitude = 10.0;
uniform float wave_frequency = 5.0;
uniform float wave_speed = 2.0;

void fragment() {
    vec2 distorted_uv = UV;
    distorted_uv.x += sin(UV.y * wave_frequency + TIME * wave_speed) * wave_amplitude * 0.01;
    distorted_uv.y += cos(UV.x * wave_frequency + TIME * wave_speed) * wave_amplitude * 0.01;
    
    COLOR = texture(TEXTURE, distorted_uv);
}
```

### 3. 颜色替换
```glsl
shader_type canvas_item;

uniform vec4 old_color : source_color = vec4(1.0, 0.0, 0.0, 1.0);
uniform vec4 new_color : source_color = vec4(0.0, 1.0, 0.0, 1.0);
uniform float tolerance : hint_range(0, 1) = 0.1;

void fragment() {
    vec4 tex_color = texture(TEXTURE, UV);
    
    float dist = distance(tex_color.rgb, old_color.rgb);
    if (dist < tolerance) {
        COLOR = vec4(new_color.rgb, tex_color.a);
    } else {
        COLOR = tex_color;
    }
}
```

### 4. 溶解效果
```glsl
shader_type canvas_item;

uniform float dissolve_amount : hint_range(0, 1) = 0.5;
uniform sampler2D noise_texture;
uniform vec4 edge_color : source_color = vec4(1.0, 0.5, 0.0, 1.0);

void fragment() {
    vec4 tex_color = texture(TEXTURE, UV);
    float noise = texture(noise_texture, UV).r;
    
    if (noise < dissolve_amount) {
        discard; // 完全透明
    } else if (noise < dissolve_amount + 0.1) {
        COLOR = edge_color; // 边缘发光
    } else {
        COLOR = tex_color;
    }
}
```

### 5. 精灵闪烁
```glsl
shader_type canvas_item;

uniform float flash_speed = 5.0;
uniform float flash_intensity : hint_range(0, 1) = 0.5;

void fragment() {
    vec4 tex_color = texture(TEXTURE, UV);
    float flash = sin(TIME * flash_speed) * 0.5 + 0.5;
    tex_color.rgb += vec3(flash * flash_intensity);
    COLOR = tex_color;
}
```

## 3D 着色器

### PBR 材质
```glsl
shader_type spatial;

uniform vec4 albedo : source_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float metallic : hint_range(0, 1) = 0.0;
uniform float roughness : hint_range(0, 1) = 0.5;
uniform sampler2D albedo_texture;
uniform sampler2D normal_texture;

void fragment() {
    ALBEDO = albedo.rgb * texture(albedo_texture, UV).rgb;
    METALLIC = metallic;
    ROUGHNESS = roughness;
    NORMAL_MAP = texture(normal_texture, UV).rgb;
}
```

### 全息效果
```glsl
shader_type spatial;

uniform float scanline_speed = 2.0;
uniform float scanline_frequency = 50.0;
uniform vec4 hologram_color : source_color = vec4(0.0, 1.0, 1.0, 1.0);

void fragment() {
    // 扫描线
    float scanline = sin(UV.y * scanline_frequency + TIME * scanline_speed);
    scanline = smoothstep(0.0, 0.5, scanline);
    
    // 边缘发光
    float fresnel = pow(1.0 - dot(NORMAL, VIEW), 3.0);
    
    ALBEDO = hologram_color.rgb;
    ALPHA = (scanline * 0.3 + 0.7) * (fresnel + 0.3);
    EMISSION = hologram_color.rgb * fresnel * 2.0;
}
```

### 水面效果
```glsl
shader_type spatial;

uniform float wave_speed = 1.0;
uniform float wave_height = 0.5;
uniform sampler2D normal_map1;
uniform sampler2D normal_map2;

void vertex() {
    VERTEX.y += sin(VERTEX.x * 2.0 + TIME * wave_speed) * wave_height;
    VERTEX.y += cos(VERTEX.z * 2.0 + TIME * wave_speed * 0.8) * wave_height;
}

void fragment() {
    vec2 uv1 = UV * 1.0 + TIME * 0.05;
    vec2 uv2 = UV * 1.0 - TIME * 0.03;
    
    vec3 n1 = texture(normal_map1, uv1).rgb;
    vec3 n2 = texture(normal_map2, uv2).rgb;
    
    NORMAL_MAP = mix(n1, n2, 0.5);
    ALBEDO = vec3(0.1, 0.4, 0.8);
    METALLIC = 0.8;
    ROUGHNESS = 0.2;
}
```

## 粒子着色器

### 火焰粒子
```glsl
shader_type particles;

void start() {
    // 初始速度向上
    VELOCITY = vec3(randf_range(-1.0, 1.0), randf_range(2.0, 5.0), randf_range(-1.0, 1.0));
    
    // 随机大小
    float scale = randf_range(0.5, 1.5);
    TRANSFORM[0][0] *= scale;
    TRANSFORM[1][1] *= scale;
    TRANSFORM[2][2] *= scale;
    
    // 随机颜色(红到黄)
    COLOR.r = 1.0;
    COLOR.g = randf_range(0.0, 1.0);
    COLOR.b = 0.0;
}

void process() {
    // 逐渐变小
    float scale = 1.0 - LIFETIME / LIFETIME;
    TRANSFORM[0][0] *= scale;
    TRANSFORM[1][1] *= scale;
    
    // 逐渐透明
    COLOR.a = 1.0 - LIFETIME / LIFETIME;
}
```

### 魔法粒子
```glsl
shader_type particles;

uniform float orbit_radius = 2.0;
uniform float orbit_speed = 2.0;

void start() {
    COLOR = vec4(0.5, 0.0, 1.0, 1.0); // 紫色
    TRANSFORM[3].xyz = vec3(0.0, 0.0, 0.0); // 从中心开始
}

void process() {
    // 环绕运动
    float angle = LIFETIME * orbit_speed;
    TRANSFORM[3].x = cos(angle) * orbit_radius;
    TRANSFORM[3].z = sin(angle) * orbit_radius;
    
    // 闪烁
    COLOR.a = sin(LIFETIME * 10.0) * 0.5 + 0.5;
}
```

## 后处理效果

### 屏幕抖动
```glsl
shader_type canvas_item;

uniform float shake_intensity = 5.0;
uniform float shake_speed = 10.0;

void fragment() {
    vec2 shake = vec2(
        sin(TIME * shake_speed) * shake_intensity,
        cos(TIME * shake_speed * 1.3) * shake_intensity
    ) * 0.001;
    
    COLOR = texture(TEXTURE, UV + shake);
}
```

### 暗角效果
```glsl
shader_type canvas_item;

uniform float vignette_intensity : hint_range(0, 2) = 0.5;
uniform float vignette_radius : hint_range(0, 1) = 0.8;

void fragment() {
    vec4 tex_color = texture(TEXTURE, UV);
    
    vec2 center = UV - 0.5;
    float dist = length(center);
    float vignette = smoothstep(vignette_radius, 0.0, dist);
    
    tex_color.rgb *= mix(1.0 - vignette_intensity, 1.0, vignette);
    COLOR = tex_color;
}
```

### 色差效果
```glsl
shader_type canvas_item;

uniform float aberration_amount = 0.005;

void fragment() {
    vec2 direction = normalize(UV - 0.5);
    float r = texture(TEXTURE, UV + direction * aberration_amount).r;
    float g = texture(TEXTURE, UV).g;
    float b = texture(TEXTURE, UV - direction * aberration_amount).b;
    
    COLOR = vec4(r, g, b, 1.0);
}
```

## 性能优化

### 技巧
```yaml
1. 避免复杂计算:
   - 预计算常量
   - 使用 lowp/mediump 精度
   
2. 减少纹理采样:
   - 合并纹理到图集
   - 使用 mipmaps
   
3. 简化条件分支:
   - 使用 mix() 替代 if
   - 避免动态分支
   
4. 利用 GPU 并行:
   - 向量化计算
   - 避免依赖前一个像素
```

### 精度选择
```glsl
// 低精度(移动端)
uniform lowp vec4 color;

// 中精度(大多数情况)
uniform mediump float intensity;

// 高精度(需要精确计算)
uniform highp mat4 transform;
```

## 调试技巧

### 可视化法线
```glsl
void fragment() {
    ALBEDO = NORMAL * 0.5 + 0.5; // 法线可视化
}
```

### 可视化 UV
```glsl
void fragment() {
    ALBEDO = vec3(UV, 0.0); // UV 可视化
}
```

### 性能分析
```
# 使用 Godot 内置分析器
Debugger > Profiler > Start

# 查看 Draw Calls
Debugger > Monitors > 2D Draw Calls
```

## 参考资源

- **常用效果**: [references/common-effects.md](references/common-effects.md)
- **优化指南**: [references/optimization.md](references/optimization.md)
- **数学函数**: [references/math-functions.md](references/math-functions.md)

「开发编程」分类下的热门技能

数据月份:2026-09 · 下载/星标/安装数聚合自公开技能注册库(ClawHub、SkillHub),按月更新。口径说明见数据方法。