sky_shader.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 ``fragment()``
  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 fragment() {
  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_DirectionalLight>` 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. Render modes
  54. ^^^^^^^^^^^^
  55. Subpasses allow you to do more expensive calculations at a lower resolution
  56. to speed up your shaders. For example the following code renders clouds at
  57. a lower resolution than the rest of the sky:
  58. .. code-block:: glsl
  59. shader_type sky;
  60. render_mode use_half_res_pass;
  61. void fragment() {
  62. if (AT_HALF_RES_PASS) {
  63. // Run cloud calculation for 1/4 of the pixels
  64. vec4 color = generate_clouds(EYEDIR);
  65. COLOR = color.rgb;
  66. ALPHA = color.a;
  67. } else {
  68. // At full resolution pass, blend sky and clouds together
  69. vec3 color = generate_sky(EYEDIR);
  70. COLOR = color + HALF_RES_COLOR.rgb * HALF_RES_COLOR.a;
  71. }
  72. }
  73. +---------------------------------+----------------------------------------------------------------------+
  74. | Render mode | Description |
  75. +=================================+======================================================================+
  76. | **use_half_res_pass** | Allows the shader to write to and access the half resolution pass. |
  77. +---------------------------------+----------------------------------------------------------------------+
  78. | **use_quarter_res_pass** | Allows the shader to write to and access the quarter resolution pass.|
  79. +---------------------------------+----------------------------------------------------------------------+
  80. | **disable_fog** | If used, fog will not affect the sky. |
  81. +---------------------------------+----------------------------------------------------------------------+
  82. Built-ins
  83. ^^^^^^^^^
  84. Values marked as "in" are read-only. Values marked as "out" are for optional writing and will
  85. not necessarily contain sensible values. Values marked as "inout" provide a sensible default
  86. value, and can optionally be written to. Samplers are not subjects of writing and they are
  87. not marked.
  88. Global built-ins
  89. ^^^^^^^^^^^^^^^^
  90. Global built-ins are available everywhere, including custom functions.
  91. There are 4 ``LIGHTX`` lights, accessed as ``LIGHT0``, ``LIGHT1``, ``LIGHT2``, and ``LIGHT3``.
  92. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  93. | Built-in | Description |
  94. +=================================+==========================================================================================================================+
  95. | in float **TIME** | Global time, in seconds. |
  96. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  97. | in vec3 **POSITION** | Camera position in world space |
  98. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  99. | in SamplerCube **RADIANCE** | Radiance cubemap. Can only be read from during background pass. Check ``!AT_CUBEMAP_PASS`` before using. |
  100. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  101. | in bool **AT_HALF_RES_PASS** | Currently rendering to half resolution pass. |
  102. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  103. | in bool **AT_QUARTER_RES_PASS** | Currently rendering to quarter resolution pass. |
  104. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  105. | in bool **AT_CUBEMAP_PASS** | Currently rendering to radiance cubemap. |
  106. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  107. | in bool **LIGHTX_ENABLED** | ``LightX`` is visible and in the scene. If ``false``, other light properties may be garbage. |
  108. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  109. | in float **LIGHTX_ENERGY** | Energy multiplier for ``LIGHTX``. |
  110. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  111. | in vec3 **LIGHTX_DIRECTION** | Direction that ``LIGHTX`` is facing. |
  112. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  113. | in vec3 **LIGHTX_COLOR** | Color of ``LIGHTX``. |
  114. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  115. | 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. |
  116. +---------------------------------+--------------------------------------------------------------------------------------------------------------------------+
  117. Fragment built-ins
  118. ^^^^^^^^^^^^^^^^^^
  119. +---------------------------------+-------------------------------------------------------------------------------------------------+
  120. | Built-in | Description |
  121. +=================================+=================================================================================================+
  122. | out vec3 **COLOR** | Output color |
  123. +---------------------------------+-------------------------------------------------------------------------------------------------+
  124. | out float **ALPHA** | Output alpha value, can only be used in subpasses. |
  125. +---------------------------------+-------------------------------------------------------------------------------------------------+
  126. | in vec3 **EYEDIR** | Normalized direction of current pixel. Use this as your basic direction for procedural effects. |
  127. +---------------------------------+-------------------------------------------------------------------------------------------------+
  128. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. Used to map a texture to the full screen. |
  129. +---------------------------------+-------------------------------------------------------------------------------------------------+
  130. | in vec2 **SKY_COORDS** | Sphere UV. Used to map a panorama texture to the sky. |
  131. +---------------------------------+-------------------------------------------------------------------------------------------------+
  132. | in vec4 **HALF_RES_COLOR** | Color value of corresponding pixel from half resolution pass. Uses linear filter. |
  133. +---------------------------------+-------------------------------------------------------------------------------------------------+
  134. | in vec4 **QUARTER_RES_COLOR** | Color value of corresponding pixel from quarter resolution pass. Uses linear filter. |
  135. +---------------------------------+-------------------------------------------------------------------------------------------------+