Godot Shaders (4.x)
Write canvas_item (2D) and spatial (3D) shaders in the Godot Shading Language, animate
with TIME/UV, expose uniforms, and read the screen. Targets Godot 4.7.
When to use
- Use when writing
.gdshadercode or aShaderMaterial: 2D effects (outline, dissolve, flash, water), 3D surface shaders (rim light, toon, scrolling UV), or screen-space post effects.
When not to use: the cross-engine concepts of shading (UVs, vertex/fragment
theory) → shader-programming; particles/VFX nodes → general 3D; non-shader visuals.
Core workflow
- Pick the shader type on the first line:
shader_type canvas_item;for 2D (Sprite2D, TextureRect, anythingCanvasItem) orshader_type spatial;for 3D materials. (particles,sky,fogalso exist.) - Attach via a
ShaderMaterial. Create aShaderMaterial, assign your.gdshader, and put it on the node'smaterial. Uniforms appear in the Inspector. - Write
fragment()to set the output:COLOR(2D) orALBEDO/EMISSION/ALPHA(3D). Optionallyvertex()to move geometry andlight()for custom lighting. - Expose tunables as
uniforms with hints (source_color,hint_range) so they are editable and correctly color-managed. - Animate with the built-in
TIMEand sample textures withtexture(tex, UV). - Set uniforms from code with
material.set_shader_parameter("name", value).
Patterns
1. 2D (canvas_item): tint + scrolling UV
2. 2D dissolve using a noise threshold
3. 3D (spatial): emissive rim light
4. Screen-reading post effect (4.x hint, not SCREEN_TEXTURE)
Set a uniform from GDScript:
Pitfalls
- 3.x → 4.x renames.
SCREEN_TEXTUREis removed — declareuniform sampler2D x : hint_screen_texture;and sample withSCREEN_UV. Color hintshint_color→source_color;hint_albedo/hint_white→source_color;hint_rangestays. Depth/normal usehint_depth_texture/hint_normal_roughness_texture. - Wrong output variable. In
canvas_itemwriteCOLOR; inspatialwriteALBEDO(andEMISSION,ALPHA,ROUGHNESS,METALLIC). WritingCOLORin a spatial shader does nothing. - Color uniforms without
source_colorare treated as raw linear values and look wrong (washed/dark) because Godot won't sRGB-convert them. - Transparency needs opt-in (3D). For
ALPHA < 1.0to blend, add a render mode or set the material transparency; otherwise it's opaque/cut. - Sampling outside [0,1] UV without
repeat_enableclamps. Add: repeat_enableto the sampler uniform for tiling/scroll. TIMEis seconds since start and keeps growing — wrap withfract()/mod()for periodic effects to avoid precision drift.discardis costly on some hardware and breaks early-Z; prefer settingALPHA/COLOR.awhen you can.
References
- For built-in variables per shader type, render modes,
varying, customlight(),vertex()displacement, and the visual shader graph, readreferences/shading-language.md.
Related skills
shader-programming— engine-agnostic shader concepts (GLSL/HLSL).godot-3d-essentials— materials, environment, and where spatial shaders live.godot-ui-control— applying shaders to UI for effects.


