Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

Custom post-processing

Introduction

Godot provides many post-processing effects out of the box, including Bloom, DOF, and SSAO. However, advanced use cases may require custom effects. This article explains how to write your own custom effects.

The easiest way to implement a custom post-processing shader is to use Godot’s built-in ability to read from the screen texture. If you’re not familiar with this, you should read the Screen Reading Shaders Tutorial first.

Single pass post-processing

Post-processing effects are shaders applied to a frame after Godot has rendered it. To apply a shader to a frame, create a CanvasLayer, and give it a ColorRect. Assign a new ShaderMaterial to the newly created ColorRect, and set the ColorRect‘s layout to “Full Rect”.

Your scene tree will look something like this:

../../_images/post_tree1.png

Note

Another more efficient method is to use a BackBufferCopy to copy a region of the screen to a buffer and to access it in a shader script through a sampler2D using hint_screen_texture.

Note

As of the time of writing, Godot does not support rendering to multiple buffers at the same time. Your post-processing shader will not have access to normals or other render passes. You only have access to the rendered frame.

For this demo, we will use this Sprite of a sheep.

../../_images/post_example1.png

Assign a new Shader to the ColorRect‘s ShaderMaterial. You can access the frame’s texture and UV with a sampler2D using hint_screen_texture and the built in SCREEN_UV uniforms.

Copy the following code to your shader. The code below is a hex pixelization shader by arlez80,

  1. shader_type canvas_item;
  2. uniform vec2 size = vec2(32.0, 28.0);
  3. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  4. void fragment() {
  5. vec2 norm_size = size * SCREEN_PIXEL_SIZE;
  6. bool half = mod(SCREEN_UV.y / 2.0, norm_size.y) / norm_size.y < 0.5;
  7. vec2 uv = SCREEN_UV + vec2(norm_size.x * 0.5 * float(half), 0.0);
  8. vec2 center_uv = floor(uv / norm_size) * norm_size;
  9. vec2 norm_uv = mod(uv, norm_size) / norm_size;
  10. center_uv += mix(vec2(0.0, 0.0),
  11. mix(mix(vec2(norm_size.x, -norm_size.y),
  12. vec2(0.0, -norm_size.y),
  13. float(norm_uv.x < 0.5)),
  14. mix(vec2(0.0, -norm_size.y),
  15. vec2(-norm_size.x, -norm_size.y),
  16. float(norm_uv.x < 0.5)),
  17. float(half)),
  18. float(norm_uv.y < 0.3333333) * float(norm_uv.y / 0.3333333 < (abs(norm_uv.x - 0.5) * 2.0)));
  19. COLOR = textureLod(screen_texture, center_uv, 0.0);
  20. }

The sheep will look something like this:

../../_images/post_example2.png

Multi-pass post-processing

Some post-processing effects like blurs are resource intensive. You can make them run a lot faster if you break them down in multiple passes. In a multipass material, each pass takes the result from the previous pass as an input and processes it.

To produce a multi-pass post-processing shader, you stack CanvasLayer and ColorRect nodes. In the example above, you use a CanvasLayer object to render a shader using the frame on the layer below. Apart from the node structure, the steps are the same as with the single-pass post-processing shader.

Your scene tree will look something like this:

../../_images/post_tree2.png

As an example, you could write a full screen Gaussian blur effect by attaching the following pieces of code to each of the ColorRect nodes. The order in which you apply the shaders depends on the position of the CanvasLayer in the scene tree, higher means sooner. For this blur shader, the order does not matter.

  1. shader_type canvas_item;
  2. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  3. // Blurs the screen in the X-direction.
  4. void fragment() {
  5. vec3 col = texture(screen_texture, SCREEN_UV).xyz * 0.16;
  6. col += texture(screen_texture, SCREEN_UV + vec2(SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.15;
  7. col += texture(screen_texture, SCREEN_UV + vec2(-SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.15;
  8. col += texture(screen_texture, SCREEN_UV + vec2(2.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.12;
  9. col += texture(screen_texture, SCREEN_UV + vec2(2.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.12;
  10. col += texture(screen_texture, SCREEN_UV + vec2(3.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.09;
  11. col += texture(screen_texture, SCREEN_UV + vec2(3.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.09;
  12. col += texture(screen_texture, SCREEN_UV + vec2(4.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.05;
  13. col += texture(screen_texture, SCREEN_UV + vec2(4.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.05;
  14. COLOR.xyz = col;
  15. }
  1. shader_type canvas_item;
  2. uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;
  3. // Blurs the screen in the Y-direction.
  4. void fragment() {
  5. vec3 col = texture(screen_texture, SCREEN_UV).xyz * 0.16;
  6. col += texture(screen_texture, SCREEN_UV + vec2(0.0, SCREEN_PIXEL_SIZE.y)).xyz * 0.15;
  7. col += texture(screen_texture, SCREEN_UV + vec2(0.0, -SCREEN_PIXEL_SIZE.y)).xyz * 0.15;
  8. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 2.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.12;
  9. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 2.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.12;
  10. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 3.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.09;
  11. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 3.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.09;
  12. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 4.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.05;
  13. col += texture(screen_texture, SCREEN_UV + vec2(0.0, 4.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.05;
  14. COLOR.xyz = col;
  15. }

Using the above code, you should end up with a full screen blur effect like below.

../../_images/post_example3.png