godot-shaders
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGodot Shaders (4.x)
Godot着色器(4.x版本)
Write (2D) and (3D) shaders in the Godot Shading Language, animate
with /, expose s, and read the screen. Targets Godot 4.3+.
canvas_itemspatialTIMEUVuniform使用Godot着色语言编写(2D)和(3D)着色器,通过/实现动画效果,暴露变量,并读取屏幕内容。本内容适用于Godot 4.3及以上版本。
canvas_itemspatialTIMEUVuniformWhen to use
适用场景
- Use when writing code or a
.gdshader: 2D effects (outline, dissolve, flash, water), 3D surface shaders (rim light, toon, scrolling UV), or screen-space post effects.ShaderMaterial
When not to use: the cross-engine concepts of shading (UVs, vertex/fragment
theory) → ; particles/VFX nodes → general 3D; non-shader visuals.
shader-programming- 编写代码或
.gdshader时适用:如2D特效(轮廓、溶解、闪烁、水面)、3D表面着色器(边缘光、卡通风格、UV滚动),或屏幕空间后期特效。ShaderMaterial
不适用场景:跨引擎的着色概念(UV、顶点/片段理论)→ 参考;粒子/视觉特效节点→ 通用3D开发;非着色器类视觉效果。
shader-programmingCore workflow
核心工作流程
- Pick the shader type on the first line: for 2D (Sprite2D, TextureRect, anything
shader_type canvas_item;) orCanvasItemfor 3D materials. (shader_type spatial;,particles,skyalso exist.)fog - Attach via a . Create a
ShaderMaterial, assign yourShaderMaterial, and put it on the node's.gdshader. Uniforms appear in the Inspector.material - Write to set the output:
fragment()(2D) orCOLOR/ALBEDO/EMISSION(3D). OptionallyALPHAto move geometry andvertex()for custom lighting.light() - Expose tunables as s with hints (
uniform,source_color) so they are editable and correctly color-managed.hint_range - Animate with the built-in and sample textures with
TIME.texture(tex, UV) - Set uniforms from code with .
material.set_shader_parameter("name", value)
- 选择着色器类型:在第一行声明类型,2D场景(Sprite2D、TextureRect等所有节点)使用
CanvasItem,3D材质使用shader_type canvas_item;。此外还有shader_type spatial;、particles、sky等类型可选。fog - 通过绑定:创建一个
ShaderMaterial,分配你的ShaderMaterial文件,然后将其设置到节点的.gdshader属性中。Uniform变量会显示在检查器面板中。material - 编写函数设置输出:2D场景使用
fragment(),3D场景使用COLOR/ALBEDO/EMISSION。可选编写ALPHA函数来移动几何体,或vertex()函数实现自定义光照。light() - 通过带提示的暴露可调参数:使用
uniform、source_color等提示,让参数可编辑并实现正确的色彩管理。hint_range - 通过内置变量实现动画,并使用
TIME采样纹理。texture(tex, UV) - 通过代码设置uniform变量:使用。
material.set_shader_parameter("name", value)
Patterns
常见模式
1. 2D (canvas_item): tint + scrolling UV
1. 2D(canvas_item):色调叠加+UV滚动
glsl
shader_type canvas_item;
uniform vec4 tint : source_color = vec4(1.0); // source_color = sRGB-correct color
uniform float scroll_speed : hint_range(0.0, 2.0) = 0.3;
void fragment() {
vec2 uv = UV;
uv.x += TIME * scroll_speed; // scroll horizontally over time
COLOR = texture(TEXTURE, uv) * tint; // TEXTURE = the node's texture
}glsl
shader_type canvas_item;
uniform vec4 tint : source_color = vec4(1.0); // source_color = 符合sRGB标准的颜色
uniform float scroll_speed : hint_range(0.0, 2.0) = 0.3;
void fragment() {
vec2 uv = UV;
uv.x += TIME * scroll_speed; // 随时间水平滚动
COLOR = texture(TEXTURE, uv) * tint; // TEXTURE = 当前节点的纹理
}2. 2D dissolve using a noise threshold
2. 使用噪波阈值实现2D溶解效果
glsl
shader_type canvas_item;
uniform sampler2D noise : repeat_enable; // a NoiseTexture2D
uniform float amount : hint_range(0.0, 1.0) = 0.0;
void fragment() {
vec4 tex = texture(TEXTURE, UV);
float n = texture(noise, UV).r;
if (n < amount) {
discard; // cut the pixel away
}
COLOR = tex;
}glsl
shader_type canvas_item;
uniform sampler2D noise : repeat_enable; // 一个NoiseTexture2D资源
uniform float amount : hint_range(0.0, 1.0) = 0.0;
void fragment() {
vec4 tex = texture(TEXTURE, UV);
float n = texture(noise, UV).r;
if (n < amount) {
discard; // 剔除该像素
}
COLOR = tex;
}3. 3D (spatial): emissive rim light
3. 3D(spatial):自发光边缘光
glsl
shader_type spatial;
uniform vec4 base_color : source_color = vec4(0.2, 0.5, 1.0, 1.0);
uniform vec3 rim_color : source_color = vec3(0.6, 0.8, 1.0);
uniform float rim_power : hint_range(0.5, 8.0) = 3.0;
void fragment() {
ALBEDO = base_color.rgb;
// VIEW and NORMAL are view-space built-ins; rim is strong at grazing angles.
float rim = pow(1.0 - dot(NORMAL, VIEW), rim_power);
EMISSION = rim_color * rim;
}glsl
shader_type spatial;
uniform vec4 base_color : source_color = vec4(0.2, 0.5, 1.0, 1.0);
uniform vec3 rim_color : source_color = vec3(0.6, 0.8, 1.0);
uniform float rim_power : hint_range(0.5, 8.0) = 3.0;
void fragment() {
ALBEDO = base_color.rgb;
// VIEW和NORMAL是视图空间的内置变量;边缘光在掠射角度下效果明显。
float rim = pow(1.0 - dot(NORMAL, VIEW), rim_power);
EMISSION = rim_color * rim;
}4. Screen-reading post effect (4.x hint, not SCREEN_TEXTURE)
4. 屏幕读取后期特效(4.x版本提示,替代SCREEN_TEXTURE)
glsl
shader_type canvas_item;
// 4.x: declare the screen as a uniform with hint_screen_texture.
uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;
uniform float blur : hint_range(0.0, 4.0) = 1.0;
void fragment() {
vec2 px = SCREEN_PIXEL_SIZE * blur;
vec4 c = texture(screen_tex, SCREEN_UV);
c += texture(screen_tex, SCREEN_UV + vec2(px.x, 0.0));
c += texture(screen_tex, SCREEN_UV - vec2(px.x, 0.0));
COLOR = c / 3.0;
}Set a uniform from GDScript:
gdscript
$Sprite2D.material.set_shader_parameter("amount", 0.7)glsl
shader_type canvas_item;
// 4.x版本:使用hint_screen_texture将屏幕声明为uniform变量
uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;
uniform float blur : hint_range(0.0, 4.0) = 1.0;
void fragment() {
vec2 px = SCREEN_PIXEL_SIZE * blur;
vec4 c = texture(screen_tex, SCREEN_UV);
c += texture(screen_tex, SCREEN_UV + vec2(px.x, 0.0));
c += texture(screen_tex, SCREEN_UV - vec2(px.x, 0.0));
COLOR = c / 3.0;
}通过GDScript设置uniform变量:
gdscript
$Sprite2D.material.set_shader_parameter("amount", 0.7)Pitfalls
常见陷阱
- 3.x → 4.x renames. is removed — declare
SCREEN_TEXTUREand sample withuniform sampler2D x : hint_screen_texture;. Color hintsSCREEN_UV→hint_color;source_color/hint_albedo→hint_white;source_colorstays. Depth/normal usehint_range/hint_depth_texture.hint_normal_roughness_texture - Wrong output variable. In write
canvas_item; inCOLORwritespatial(andALBEDO,EMISSION,ALPHA,ROUGHNESS). WritingMETALLICin a spatial shader does nothing.COLOR - Color uniforms without are treated as raw linear values and look wrong (washed/dark) because Godot won't sRGB-convert them.
source_color - Transparency needs opt-in (3D). For to blend, add a render mode or set the material transparency; otherwise it's opaque/cut.
ALPHA < 1.0 - Sampling outside [0,1] UV without clamps. Add
repeat_enableto the sampler uniform for tiling/scroll.: repeat_enable - is seconds since start and keeps growing — wrap with
TIME/fract()for periodic effects to avoid precision drift.mod() - is costly on some hardware and breaks early-Z; prefer setting
discard/ALPHAwhen you can.COLOR.a
- 3.x到4.x的命名变更:已被移除,需声明
SCREEN_TEXTURE并使用uniform sampler2D x : hint_screen_texture;进行采样。颜色提示SCREEN_UV改为hint_color;source_color/hint_albedo也改为hint_white;source_color保持不变。深度/法线纹理使用hint_range/hint_depth_texture。hint_normal_roughness_texture - 错误的输出变量:在中使用
canvas_item;在COLOR中使用spatial(以及ALBEDO、EMISSION、ALPHA、ROUGHNESS)。在spatial着色器中写入METALLIC不会产生任何效果。COLOR - 未使用的颜色uniform会被视为原始线性值,显示效果异常(褪色/偏暗),因为Godot不会对其进行sRGB转换。
source_color - 3D透明效果需手动开启:若要让的内容实现混合,需添加渲染模式或设置材质透明度;否则材质会是不透明或裁剪状态。
ALPHA < 1.0 - UV超出[0,1]范围采样:若未添加,采样会被钳制。如需平铺/滚动,需在采样器uniform后添加
repeat_enable。: repeat_enable - 是从启动开始的秒数,且持续增长——对于周期性效果,需使用
TIME/fract()进行包裹,避免精度漂移。mod() - 指令在部分硬件上性能开销大,且会破坏Early-Z优化;尽可能优先设置
discard/ALPHA来实现透明。COLOR.a
References
参考资料
- For built-in variables per shader type, render modes, , custom
varying,light()displacement, and the visual shader graph, readvertex().references/shading-language.md
- 如需了解各着色器类型的内置变量、渲染模式、变量、自定义
varying函数、light()位移以及可视化着色器图,请阅读vertex()。references/shading-language.md
Related skills
相关技能
- — engine-agnostic shader concepts (GLSL/HLSL).
shader-programming - — materials, environment, and where spatial shaders live.
godot-3d-essentials - — applying shaders to UI for effects.
godot-ui-control
- — 跨引擎的着色器概念(GLSL/HLSL)。
shader-programming - — 材质、环境以及spatial着色器的应用场景。
godot-3d-essentials - — 为UI应用着色器实现特效。
godot-ui-control