[–] 3 points 1 day ago

The Book of Shaders is a good base. What helped me most for 2D in Godot was pairing it with two short pages in the official docs: "Your first 2D shader" and the CanvasItem shader reference (it lists what COLOR, UV, TEXTURE and SCREEN_UV mean in Godot, which most general shader books don't cover).

Your two examples are nice first exercises:

Grayscale, as a material on any Sprite2D or TextureRect:

shader_type canvas_item;

uniform float amount : hint_range(0.0, 1.0) = 1.0;

void fragment() {
    float g = dot(COLOR.rgb, vec3(0.299, 0.587, 0.114));
    COLOR.rgb = mix(COLOR.rgb, vec3(g), amount);
}

Blur of everything behind a ColorRect (handy for pause menus):

shader_type canvas_item;

uniform sampler2D screen_tex : hint_screen_texture, filter_linear_mipmap;
uniform float blur : hint_range(0.0, 5.0) = 2.0;

void fragment() {
    COLOR = textureLod(screen_tex, SCREEN_UV, blur);
}

The second one reads mipmaps of the screen, so it is cheap but a bit blocky at high values. A proper Gaussian blur samples neighbours in two passes, which is a good next step once these make sense.

Reading small, complete shaders also helped me more than long chapters. godotshaders.com has a lot of 2D ones. Disclosure: I make a small 2D shader pack for Godot 4, and the free Lite version has two effects with commented source if you want more examples to pick apart: https://trungcaolenam.itch.io/shader-pack-lite-for-godot-4

  • source