advanced_postprocessing.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. .. _doc_advanced_postprocessing:
  2. Advanced post-processing
  3. ========================
  4. Introduction
  5. ------------
  6. This tutorial describes an advanced method for post-processing in Godot.
  7. In particular, it will explain how to write a post-processing shader that
  8. uses the depth buffer. You should already be familiar with post-processing
  9. generally and, in particular, with the methods outlined in the :ref:`custom post-processing tutorial <doc_custom_postprocessing>`.
  10. In the previous post-processing tutorial, we rendered the scene to a :ref:`Viewport <class_Viewport>`
  11. and then rendered the Viewport in a :ref:`ViewportContainer <class_ViewportContainer>`
  12. to the main scene. One limitation of this method is that we could not access the
  13. depth buffer because the depth buffer is only available in spatial shaders and
  14. Viewports do not maintain depth information.
  15. Full screen quad
  16. ----------------
  17. In the :ref:`custom post-processing tutorial <doc_custom_postprocessing>`, we
  18. covered how to use a Viewport to make custom post-processing effects. There are
  19. two main drawbacks of using a Viewport:
  20. 1. The depth buffer cannot be accessed
  21. 2. The effect of the post-processing shader is not visible in the editor
  22. To get around the limitation on using the depth buffer, use a :ref:`MeshInstance <class_MeshInstance>`
  23. with a :ref:`QuadMesh <class_QuadMesh>` primitive. This allows us to use a spatial
  24. shader and to access the depth texture of the scene. Next, use a vertex shader
  25. to make the quad cover the screen at all times so that the post-processing
  26. effect will be applied at all times, including in the editor.
  27. First, create a new MeshInstance and set its mesh to a QuadMesh. This creates a quad
  28. centered at position ``(0, 0, 0)`` with a width and height of ``1``. Set the width
  29. and height to ``2``. Right now, the quad occupies a position in world space at the
  30. origin; however, we want it to move with the camera so that it always covers the
  31. entire screen. To do this, we will bypass the coordinate transforms that translate
  32. the vertex positions through the difference coordinate spaces and treat the vertices
  33. as if they were already in clip space.
  34. The vertex shader expects coordinates to be output in clip space, which are coordinates
  35. ranging from ``-1`` at the left and bottom of the screen to ``1`` at the top and right
  36. of the screen. This is why the QuadMesh needs to have height and width of ``2``.
  37. Godot handles the transform from model to view space to clip space behind the scenes,
  38. so we need to nullify the effects of Godot's transformations. We do this by setting the
  39. ``POSITION`` built-in to our desired position. ``POSITION`` bypasses the built-in transformations
  40. and sets the vertex position directly.
  41. .. code-block:: glsl
  42. shader_type spatial;
  43. void vertex() {
  44. POSITION = vec4(VERTEX, 1.0);
  45. }
  46. Even with this vertex shader, the quad keeps disappearing. This is due to frustum
  47. culling, which is done on the CPU. Frustum culling uses the camera matrix and the
  48. AABBs of Meshes to determine if the Mesh will be visible *before* passing it to the GPU.
  49. The CPU has no knowledge of what we are doing with the vertices, so it assumes the
  50. coordinates specified refer to world positions, not clip space positions, which results
  51. in Godot culling the quad when we turn away from the center of the scene. In
  52. order to keep the quad from being culled, there are a few options:
  53. 1. Add the QuadMesh as a child to the camera, so the camera is always pointed at it
  54. 2. Set the Geometry property ``extra_cull_margin`` as large as possible in the QuadMesh
  55. The second option ensures that the quad is visible in the editor, while the first
  56. option guarantees that it will still be visible even if the camera moves outside the cull margin.
  57. You can also use both options.
  58. Depth texture
  59. -------------
  60. To read from the depth texture, perform a texture lookup using ``texture()`` and
  61. the uniform variable ``DEPTH_TEXTURE``.
  62. .. code-block:: glsl
  63. float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
  64. .. note:: Similar to accessing the screen texture, accessing the depth texture is only
  65. possible when reading from the current viewport. The depth texture cannot be
  66. accessed from another viewport to which you have rendered.
  67. The values returned by ``DEPTH_TEXTURE`` are between ``0`` and ``1`` and are nonlinear.
  68. When displaying depth directly from the ``DEPTH_TEXTURE``, everything will look almost
  69. white unless it is very close. This is because the depth buffer stores objects closer
  70. to the camera using more bits than those further, so most of the detail in depth
  71. buffer is found close to the camera. In order to make the depth value align with world or
  72. model coordinates, we need to linearize the value. When we apply the projection matrix to the
  73. vertex position, the z value is made nonlinear, so to linearize it, we multiply it by the
  74. inverse of the projection matrix, which in Godot, is accessible with the variable
  75. ``INV_PROJECTION_MATRIX``.
  76. Firstly, take the screen space coordinates and transform them into normalized device
  77. coordinates (NDC). NDC run from ``-1`` to ``1``, similar to clip space coordinates.
  78. Reconstruct the NDC using ``SCREEN_UV`` for the ``x`` and ``y`` axis, and
  79. the depth value for ``z``.
  80. .. code-block:: glsl
  81. void fragment() {
  82. float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
  83. vec3 ndc = vec3(SCREEN_UV, depth) * 2.0 - 1.0;
  84. }
  85. Convert NDC to view space by multiplying the NDC by ``INV_PROJECTION_MATRIX``.
  86. Recall that view space gives positions relative to the camera, so the ``z`` value will give us
  87. the distance to the point.
  88. .. code-block:: glsl
  89. void fragment() {
  90. ...
  91. vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
  92. view.xyz /= view.w;
  93. float linear_depth = -view.z;
  94. }
  95. Because the camera is facing the negative ``z`` direction, the position will have a negative ``z`` value.
  96. In order to get a usable depth value, we have to negate ``view.z``.
  97. The world position can be constructed from the depth buffer using the following code. Note
  98. that the ``CAMERA_MATRIX`` is needed to transform the position from view space into world space, so
  99. it needs to be passed to the fragment shader with a varying.
  100. .. code-block:: glsl
  101. varying mat4 CAMERA;
  102. void vertex() {
  103. CAMERA = CAMERA_MATRIX;
  104. }
  105. void fragment() {
  106. ...
  107. vec4 world = CAMERA * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
  108. vec3 world_position = world.xyz / world.w;
  109. }
  110. An optimization
  111. ---------------
  112. You can benefit from using a single large triangle rather than using a full
  113. screen quad. The reason for this is explained `here <https://michaldrobot.com/2014/04/01/gcn-execution-patterns-in-full-screen-passes>`_.
  114. However, the benefit is quite small and only beneficial when running especially
  115. complex fragment shaders.
  116. Set the Mesh in the MeshInstance to an :ref:`ArrayMesh <class_ArrayMesh>`. An
  117. ArrayMesh is a tool that allows you to easily construct a Mesh from Arrays for
  118. vertices, normals, colors, etc.
  119. Now, attach a script to the MeshInstance and use the following code:
  120. ::
  121. extends MeshInstance
  122. func _ready():
  123. # Create a single triangle out of vertices:
  124. var verts = PoolVector3Array()
  125. verts.append(Vector3(-1.0, -1.0, 0.0))
  126. verts.append(Vector3(-1.0, 3.0, 0.0))
  127. verts.append(Vector3(3.0, -1.0, 0.0))
  128. # Create an array of arrays.
  129. # This could contain normals, colors, UVs, etc.
  130. var mesh_array = []
  131. mesh_array.resize(Mesh.ARRAY_MAX) #required size for ArrayMesh Array
  132. mesh_array[Mesh.ARRAY_VERTEX] = verts #position of vertex array in ArrayMesh Array
  133. # Create mesh from mesh_array:
  134. mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, mesh_array)
  135. .. note:: The triangle is specified in normalized device coordinates. Recall, NDC run
  136. from ``-1`` to ``1`` in both the ``x`` and ``y`` directions. This makes the screen
  137. ``2`` units wide and ``2`` units tall. In order to cover the entire screen with
  138. a single triangle, use a triangle that is ``4`` units wide and ``4``
  139. units tall, double its height and width.
  140. Assign the same vertex shader from above and everything should look exactly the same.
  141. The one drawback to using an ArrayMesh over using a QuadMesh is that the ArrayMesh
  142. is not visible in the editor because the triangle is not constructed until the scene
  143. is run. To get around that, construct a single triangle Mesh in a modelling program
  144. and use that in the MeshInstance instead.