sky_shader.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. .. _doc_sky_shader:
  2. Sky shaders
  3. ===========
  4. Sky shaders are a special type of shader used for drawing sky backgrounds
  5. and for updating radiance cubemaps which are used for image-based lighting
  6. (IBL). Sky shaders only have one processing function, the ``sky()``
  7. function.
  8. There are three places the sky shader is used.
  9. * First the sky shader is used to draw the sky when you have selected to use
  10. a Sky as the background in your scene.
  11. * Second, the sky shader is used to update the radiance cubemap
  12. when using the Sky for ambient color or reflections.
  13. * Third, the sky shader is used to draw the lower res subpasses which can be
  14. used in the high-res background or cubemap pass.
  15. In total, this means the sky shader can run up
  16. to six times per frame, however, in practice it will be much less than that
  17. because the radiance cubemap does not need to be updated every frame, and
  18. not all subpasses will be used. You can change the behavior of the shader
  19. based on where it is called by checking the ``AT_*_PASS`` booleans. For
  20. example:
  21. .. code-block:: glsl
  22. shader_type sky;
  23. void sky() {
  24. if (AT_CUBEMAP_PASS) {
  25. // Sets the radiance cubemap to a nice shade of blue instead of doing
  26. // expensive sky calculations
  27. COLOR = vec3(0.2, 0.6, 1.0);
  28. } else {
  29. // Do expensive sky calculations for background sky only
  30. COLOR = get_sky_color(EYEDIR);
  31. }
  32. }
  33. When using the sky shader to draw a background, the shader will be called for
  34. all non-occluded fragments on the screen. However, for the background's
  35. subpasses, the shader will be called for every pixel of the subpass.
  36. When using the sky shader to update the radiance cubemap, the sky shader
  37. will be called for every pixel in the cubemap. On the other hand, the shader
  38. will only be called when the radiance cubemap needs to be updated. The radiance
  39. cubemap needs to be updated when any of the shader parameters are updated.
  40. For example, if ``TIME`` is used in the shader, then the radiance cubemap
  41. will update every frame. The following list of changes force an update of
  42. the radiance cubemap:
  43. * ``TIME`` is used.
  44. * ``POSITION`` is used and the camera position changes.
  45. * If any ``LIGHTX_*`` properties are used and any
  46. :ref:`DirectionalLight3D <class_DirectionalLight3D>` changes.
  47. * If any uniform is changed in the shader.
  48. * If the screen is resized and either of the subpasses are used.
  49. Try to avoid updating the radiance cubemap needlessly. If you do need to
  50. update the radiance cubemap each frame, make sure your
  51. :ref:`Sky process mode <class_Sky_property_process_mode>` is set to
  52. :ref:`REALTIME <class_Sky_constant_PROCESS_MODE_REALTIME>`.
  53. Note that the :ref:`process mode <class_Sky_property_process_mode>` only
  54. affects the rendering of the radiance cubemap. The visible sky is always
  55. rendered by calling the fragment shader for every pixel. With complex fragment
  56. shaders, this can result in a high rendering overhead. If the sky is static
  57. (the conditions listed above are met) or changes slowly, running the full
  58. fragment shader every frame is not needed. This can be avoided by rendering the
  59. full sky into the radiance cubemap, and reading from this cubemap when
  60. rendering the visible sky. With a completely static sky, this means that it
  61. needs to be rendered only once.
  62. The following code renders the full sky into the radiance cubemap and reads
  63. from that cubemap for displaying the visible sky:
  64. .. code-block:: glsl
  65. shader_type sky;
  66. void sky() {
  67. if (AT_CUBEMAP_PASS) {
  68. vec3 dir = EYEDIR;
  69. vec4 col = vec4(0.0);
  70. // Complex color calculation
  71. COLOR = col.xyz;
  72. ALPHA = 1.0;
  73. } else {
  74. COLOR = texture(RADIANCE, EYEDIR).rgb;
  75. }
  76. }
  77. This way, the complex calculations happen only in the cubemap pass, which can
  78. be optimized by setting the sky's :ref:`process mode <class_Sky_property_process_mode>`
  79. and the :ref:`radiance size <class_Sky_property_radiance_size>` to get the
  80. desired balance between performance and visual fidelity.
  81. Render modes
  82. ^^^^^^^^^^^^
  83. Subpasses allow you to do more expensive calculations at a lower resolution
  84. to speed up your shaders. For example the following code renders clouds at
  85. a lower resolution than the rest of the sky:
  86. .. code-block:: glsl
  87. shader_type sky;
  88. render_mode use_half_res_pass;
  89. void sky() {
  90. if (AT_HALF_RES_PASS) {
  91. // Run cloud calculation for 1/4 of the pixels
  92. vec4 color = generate_clouds(EYEDIR);
  93. COLOR = color.rgb;
  94. ALPHA = color.a;
  95. } else {
  96. // At full resolution pass, blend sky and clouds together
  97. vec3 color = generate_sky(EYEDIR);
  98. COLOR = color + HALF_RES_COLOR.rgb * HALF_RES_COLOR.a;
  99. }
  100. }
  101. +--------------------------+-----------------------------------------------------------------------+
  102. | Render mode | Description |
  103. +==========================+=======================================================================+
  104. | **use_half_res_pass** | Allows the shader to write to and access the half resolution pass. |
  105. +--------------------------+-----------------------------------------------------------------------+
  106. | **use_quarter_res_pass** | Allows the shader to write to and access the quarter resolution pass. |
  107. +--------------------------+-----------------------------------------------------------------------+
  108. | **disable_fog** | If used, fog will not affect the sky. |
  109. +--------------------------+-----------------------------------------------------------------------+
  110. Built-ins
  111. ^^^^^^^^^
  112. Values marked as "in" are read-only. Values marked as "out" are for optional
  113. writing and will not necessarily contain sensible values. Samplers cannot be
  114. written to so they are not marked.
  115. Global built-ins
  116. ^^^^^^^^^^^^^^^^
  117. Global built-ins are available everywhere, including in custom functions.
  118. There are 4 ``LIGHTX`` lights, accessed as ``LIGHT0``, ``LIGHT1``, ``LIGHT2``, and ``LIGHT3``.
  119. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  120. | Built-in | Description |
  121. +=================================+==========================================================================================================================+
  122. | in float **TIME** | Global time, in seconds. |
  123. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  124. | in vec3 **POSITION** | Camera position in world space |
  125. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  126. | samplerCube **RADIANCE** | Radiance cubemap. Can only be read from during background pass. Check ``!AT_CUBEMAP_PASS`` before using. |
  127. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  128. | in bool **AT_HALF_RES_PASS** | Currently rendering to half resolution pass. |
  129. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  130. | in bool **AT_QUARTER_RES_PASS** | Currently rendering to quarter resolution pass. |
  131. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  132. | in bool **AT_CUBEMAP_PASS** | Currently rendering to radiance cubemap. |
  133. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  134. | in bool **LIGHTX_ENABLED** | ``LightX`` is visible and in the scene. If ``false``, other light properties may be garbage. |
  135. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  136. | in float **LIGHTX_ENERGY** | Energy multiplier for ``LIGHTX``. |
  137. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  138. | in vec3 **LIGHTX_DIRECTION** | Direction that ``LIGHTX`` is facing. |
  139. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  140. | in vec3 **LIGHTX_COLOR** | Color of ``LIGHTX``. |
  141. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  142. | in float **LIGHTX_SIZE** | Angular diameter of ``LIGHTX`` in the sky. Expressed in degrees. For reference, the sun from earth is about 0.5 degrees. |
  143. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  144. | in float **PI** | A ``PI`` constant (``3.141592``). |
  145. | | A ratio of a circle's circumference to its diameter and amount of radians in half turn. |
  146. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  147. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  148. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  149. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  150. | in float **E** | A ``E`` constant (``2.718281``). |
  151. | | Euler's number and a base of the natural logarithm. |
  152. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  153. Sky built-ins
  154. ^^^^^^^^^^^^^
  155. +-------------------------------+-------------------------------------------------------------------------------------------------+
  156. | Built-in | Description |
  157. +===============================+=================================================================================================+
  158. | in vec3 **EYEDIR** | Normalized direction of current pixel. Use this as your basic direction for procedural effects. |
  159. +-------------------------------+-------------------------------------------------------------------------------------------------+
  160. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. Used to map a texture to the full screen. |
  161. +-------------------------------+-------------------------------------------------------------------------------------------------+
  162. | in vec2 **SKY_COORDS** | Sphere UV. Used to map a panorama texture to the sky. |
  163. +-------------------------------+-------------------------------------------------------------------------------------------------+
  164. | in vec4 **HALF_RES_COLOR** | Color value of corresponding pixel from half resolution pass. Uses linear filter. |
  165. +-------------------------------+-------------------------------------------------------------------------------------------------+
  166. | in vec4 **QUARTER_RES_COLOR** | Color value of corresponding pixel from quarter resolution pass. Uses linear filter. |
  167. +-------------------------------+-------------------------------------------------------------------------------------------------+
  168. | out vec3 **COLOR** | Output color. |
  169. +-------------------------------+-------------------------------------------------------------------------------------------------+
  170. | out float **ALPHA** | Output alpha value, can only be used in subpasses. |
  171. +-------------------------------+-------------------------------------------------------------------------------------------------+
  172. | out vec4 **FOG** | |
  173. +-------------------------------+-------------------------------------------------------------------------------------------------+