custom_postprocessing.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. .. _doc_custom_postprocessing:
  2. Custom post-processing
  3. ======================
  4. Introduction
  5. ------------
  6. Godot provides many post-processing effects out of the box, including Bloom,
  7. DOF, and SSAO. However, advanced use cases may require custom effects. This
  8. article explains how to write your own custom effects.
  9. The easiest way to implement a custom post-processing shader is to use Godot's
  10. built-in ability to read from the screen texture. If you're not familiar with
  11. this, you should read the
  12. :ref:`Screen Reading Shaders Tutorial <doc_screen-reading_shaders>` first.
  13. Single pass post-processing
  14. ---------------------------
  15. Post-processing effects are shaders applied to a frame after Godot has rendered
  16. it. To apply a shader to a frame, create a :ref:`CanvasLayer
  17. <class_CanvasLayer>`, and give it a :ref:`ColorRect <class_ColorRect>`. Assign a
  18. new :ref:`ShaderMaterial <class_ShaderMaterial>` to the newly created
  19. ``ColorRect``, and set the ``ColorRect``'s layout to "Full Rect".
  20. Your scene tree will look something like this:
  21. .. image:: img/post_tree1.png
  22. .. note::
  23. Another more efficient method is to use a :ref:`BackBufferCopy
  24. <class_BackBufferCopy>` to copy a region of the screen to a buffer and to
  25. access it in a shader script through ``texture(SCREEN_TEXTURE, ...)``.
  26. .. note::
  27. As of the time of writing, Godot does not support rendering to multiple
  28. buffers at the same time. Your post-processing shader will not have access
  29. to normals or other render passes. You only have access to the rendered
  30. frame.
  31. For this demo, we will use this :ref:`Sprite <class_Sprite2D>` of a sheep.
  32. .. image:: img/post_example1.png
  33. Assign a new :ref:`Shader <class_Shader>` to the ``ColorRect``'s
  34. ``ShaderMaterial``. You can access the frame's texture and UV with the built in
  35. ``SCREEN_TEXTURE`` and ``SCREEN_UV`` uniforms.
  36. Copy the following code to your shader. The code below is a hex pixelization
  37. shader by `arlez80 <https://bitbucket.org/arlez80/hex-mosaic/src/master/>`_,
  38. .. code-block:: glsl
  39. shader_type canvas_item;
  40. uniform vec2 size = vec2(32.0, 28.0);
  41. void fragment() {
  42. vec2 norm_size = size * SCREEN_PIXEL_SIZE;
  43. bool half = mod(SCREEN_UV.y / 2.0, norm_size.y) / norm_size.y < 0.5;
  44. vec2 uv = SCREEN_UV + vec2(norm_size.x * 0.5 * float(half), 0.0);
  45. vec2 center_uv = floor(uv / norm_size) * norm_size;
  46. vec2 norm_uv = mod(uv, norm_size) / norm_size;
  47. center_uv += mix(vec2(0.0, 0.0),
  48. mix(mix(vec2(norm_size.x, -norm_size.y),
  49. vec2(0.0, -norm_size.y),
  50. float(norm_uv.x < 0.5)),
  51. mix(vec2(0.0, -norm_size.y),
  52. vec2(-norm_size.x, -norm_size.y),
  53. float(norm_uv.x < 0.5)),
  54. float(half)),
  55. float(norm_uv.y < 0.3333333) * float(norm_uv.y / 0.3333333 < (abs(norm_uv.x - 0.5) * 2.0)));
  56. COLOR = textureLod(SCREEN_TEXTURE, center_uv, 0.0);
  57. }
  58. The sheep will look something like this:
  59. .. image:: img/post_example2.png
  60. Multi-pass post-processing
  61. --------------------------
  62. Some post-processing effects like blurs are resource intensive. You can make
  63. them run a lot faster if you break them down in multiple passes. In a multipass
  64. material, each pass takes the result from the previous pass as an input and
  65. processes it.
  66. To produce a multi-pass post-processing shader, you stack ``CanvasLayer`` and
  67. ``ColorRect`` nodes. In the example above, you use a ``CanvasLayer`` object to
  68. render a shader using the frame on the layer below. Apart from the node
  69. structure, the steps are the same as with the single-pass post-processing
  70. shader.
  71. Your scene tree will look something like this:
  72. .. image:: img/post_tree2.png
  73. As an example, you could write a full screen Gaussian blur effect by attaching
  74. the following pieces of code to each of the ``ColorRect`` nodes. The order in
  75. which you apply the shaders depends on the position of the ``CanvasLayer`` in
  76. the scene tree, higher means sooner. For this blur shader, the order does not
  77. matter.
  78. .. code-block:: glsl
  79. shader_type canvas_item;
  80. // Blurs the screen in the X-direction.
  81. void fragment() {
  82. vec3 col = texture(SCREEN_TEXTURE, SCREEN_UV).xyz * 0.16;
  83. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.15;
  84. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(-SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.15;
  85. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(2.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.12;
  86. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(2.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.12;
  87. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(3.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.09;
  88. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(3.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.09;
  89. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(4.0 * SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.05;
  90. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(4.0 * -SCREEN_PIXEL_SIZE.x, 0.0)).xyz * 0.05;
  91. COLOR.xyz = col;
  92. }
  93. .. code-block:: glsl
  94. shader_type canvas_item;
  95. // Blurs the screen in the Y-direction.
  96. void fragment() {
  97. vec3 col = texture(SCREEN_TEXTURE, SCREEN_UV).xyz * 0.16;
  98. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, SCREEN_PIXEL_SIZE.y)).xyz * 0.15;
  99. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, -SCREEN_PIXEL_SIZE.y)).xyz * 0.15;
  100. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 2.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.12;
  101. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 2.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.12;
  102. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 3.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.09;
  103. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 3.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.09;
  104. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 4.0 * SCREEN_PIXEL_SIZE.y)).xyz * 0.05;
  105. col += texture(SCREEN_TEXTURE, SCREEN_UV + vec2(0.0, 4.0 * -SCREEN_PIXEL_SIZE.y)).xyz * 0.05;
  106. COLOR.xyz = col;
  107. }
  108. Using the above code, you should end up with a full screen blur effect like
  109. below.
  110. .. image:: img/post_example3.png