lights_and_shadows.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. .. _doc_lights_and_shadows:
  2. 3D lights and shadows
  3. =====================
  4. Introduction
  5. ------------
  6. Light sources emit light that mixes with the materials and produces a visible
  7. result. Light can come from several types of sources in a scene:
  8. - From the Material itself in the form of the emission color (though
  9. it does not affect nearby objects unless baked).
  10. - Light Nodes: Directional, Omni and Spot.
  11. - Ambient Light in the
  12. :ref:`Environment <class_Environment>`.
  13. - Baked Light (read :ref:`doc_baked_lightmaps`).
  14. The emission color is a material property. You can read more about it
  15. in the :ref:`doc_spatial_material` tutorial.
  16. Light nodes
  17. -----------
  18. There are three types of light nodes: `Directional light`_,
  19. `Omni light`_ and `Spot light`_. Let's take a look at the common
  20. parameters for lights:
  21. .. image:: img/light_params.png
  22. Each one has a specific function:
  23. - **Color**: Base color for emitted light.
  24. - **Energy**: Energy multiplier. This is useful for saturating lights or working with :ref:`doc_high_dynamic_range`.
  25. - **Indirect Energy**: Secondary multiplier used with indirect light (light bounces). This works in baked light or GIProbe.
  26. - **Negative**: Light becomes subtractive instead of additive. It's sometimes useful to manually compensate some dark corners.
  27. - **Specular**: Affects the intensity of the specular blob in objects affected by this light. At zero, this light becomes a pure diffuse light.
  28. - **Bake Mode**: Sets the bake mode for the light. For more information see :ref:`doc_baked_lightmaps`
  29. - **Cull Mask**: Objects that are in the selected layers below will be affected by this light.
  30. Note that objects disabled via this cull mask will still cast shadows.
  31. If you don't want disabled objects to cast shadows, adjust the ``cast_shadow`` property on the
  32. GeometryInstance to the desired value.
  33. Shadow mapping
  34. ^^^^^^^^^^^^^^
  35. Lights can optionally cast shadows. This gives them greater realism (light does
  36. not reach occluded areas), but it can incur a bigger performance cost.
  37. There is a list of generic shadow parameters, each also has a specific function:
  38. - **Enabled**: Check to enable shadow mapping in this light.
  39. - **Color**: Areas occluded are multiplied by this color. It is black by default, but it can be changed to tint shadows.
  40. - **Bias**: When this parameter is too small, self shadowing occurs. When too large, shadows separate from the casters. Tweak to what works best for you.
  41. - **Contact**: Performs a short screen-space raycast to reduce the gap generated by the bias. Contact shadows are only available when using the GLES3 backend.
  42. - **Reverse Cull Faces**: Some scenes work better when shadow mapping is rendered with face-culling inverted.
  43. Below is an image of what tweaking bias looks like. Default values work for most
  44. cases, but in general it depends on the size and complexity of geometry.
  45. .. image:: img/shadow_bias.png
  46. Finally, if gaps can't be solved, the **Contact** option can help:
  47. .. image:: img/shadow_contact.png
  48. Any sort of bias issues can always be fixed by increasing the shadow map resolution,
  49. although that may lead to decreased performance on low-end hardware.
  50. Directional light
  51. ~~~~~~~~~~~~~~~~~
  52. This is the most common type of light and represents a light source
  53. very far away (such as the sun). It is also the cheapest light to compute and should be used whenever possible
  54. (although it's not the cheapest shadow-map to compute, but more on that later).
  55. Directional light models an infinite number of parallel light rays
  56. covering the whole scene. The directional light node is represented by a big arrow which
  57. indicates the direction of the light rays. However, the position of the node
  58. does not affect the lighting at all and can be anywhere.
  59. .. image:: img/light_directional.png
  60. Every face whose front-side is hit by the light rays is lit, while the others stay dark. Most light types
  61. have specific parameters, but directional lights are pretty simple in nature, so they don't.
  62. Directional shadow mapping
  63. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  64. To compute shadow maps, the scene is rendered (only depth) from an orthogonal point of view that covers
  65. the whole scene (or up to the max distance). There is, however, a problem with this approach because objects
  66. closer to the camera receive blocky shadows.
  67. .. image:: img/shadow_blocky.png
  68. To fix this, a technique named "Parallel Split Shadow Maps" (or PSSM) is used. This splits the view frustum in 2 or 4 areas. Each
  69. area gets its own shadow map. This allows small areas close to the viewer to have the same shadow resolution as a huge, far-away area.
  70. .. image:: img/pssm_explained.png
  71. With this, shadows become more detailed:
  72. .. image:: img/shadow_pssm.png
  73. To control PSSM, a number of parameters are exposed:
  74. .. image:: img/directional_shadow_params.png
  75. Each split distance is controlled relative to the camera far (or shadow
  76. **Max Distance** if greater than zero), so *0.0* is the eye position and *1.0*
  77. is where the shadow ends at a distance. Splits are in-between. Default values
  78. generally work well, but tweaking the first split a bit is common to give more
  79. detail to close objects (like a character in a third person game).
  80. Always make sure to set a shadow *Max Distance* according to what the scene needs.
  81. A lower maximum distance will result in better-looking shadows.
  82. Sometimes, the transition between a split and the next can look bad. To fix this,
  83. the **"Blend Splits"** option can be turned on, which sacrifices detail in exchange
  84. for smoother transitions:
  85. .. image:: img/blend_splits.png
  86. The **"Normal Bias"** parameter can be used to fix special cases of self shadowing
  87. when objects are perpendicular to the light. The only downside is that it makes
  88. the shadow a bit thinner.
  89. .. image:: img/normal_bias.png
  90. The **"Bias Split Scale"** parameter can control extra bias for the splits that
  91. are far away. If self shadowing occurs only on the splits far away, this value can fix them.
  92. Finally, the **"Depth Range"** has two settings:
  93. - **Stable**: Keeps the shadow stable while the camera moves, and the blocks that appear in the outline when close to the shadow edges remain in-place. This is the default and generally desired, but it reduces the effective shadow resolution.
  94. - **Optimized**: Tries to achieve the maximum resolution available at any given time. This may result in a "moving saw" effect on shadow edges, but at the same time the shadow looks more detailed (so this effect may be subtle enough to be forgiven).
  95. Just experiment which setting works better for your scene.
  96. Shadowmap size for directional lights can be changed in Project Settings -> Rendering -> Quality:
  97. .. image:: img/project_setting_shadow.png
  98. Increasing it can solve bias problems, but decrease performance. Shadow mapping is an art of tweaking.
  99. Omni light
  100. ~~~~~~~~~~
  101. Omni light is a point source that emits light spherically in all directions up to a given
  102. radius.
  103. .. image:: img/light_omni.png
  104. In real life, light attenuation is an inverse function, which means omni lights don't have a radius.
  105. This is a problem because it means computing several omni lights would become demanding.
  106. To solve this, a *Range* is introduced together with an attenuation function.
  107. .. image:: img/light_omni_params.png
  108. These two parameters allow tweaking how this works visually in order to find aesthetically pleasing results.
  109. .. image:: img/light_attenuation.png
  110. Omni shadow mapping
  111. ^^^^^^^^^^^^^^^^^^^
  112. Omni light shadow mapping is relatively straightforward. The main issue that needs to be
  113. considered is the algorithm used to render it.
  114. Omni Shadows can be rendered as either **"Dual Paraboloid" or "Cube Mapped"**.
  115. The former renders quickly, but can cause deformations,
  116. while the later is more correct, but costlier.
  117. .. image:: img/shadow_omni_dp_cm.png
  118. If the objects being rendered are mostly irregular, Dual Paraboloid is usually
  119. enough. In any case, as these shadows are cached in a shadow atlas (more on that at the end), it
  120. may not make a difference in performance for most scenes.
  121. Spot light
  122. ~~~~~~~~~~
  123. Spot lights are similar to omni lights, except they emit light only into a cone
  124. (or "cutoff"). They are useful to simulate flashlights,
  125. car lights, reflectors, spots, etc. This type of light is also attenuated towards the
  126. opposite direction it points to.
  127. .. image:: img/light_spot.png
  128. Spot lights share the same **Range** and **Attenuation** as **OmniLight**, and add two extra parameters:
  129. - **Angle**: The aperture angle of the light
  130. - **Angle Attenuation**: The cone attenuation, which helps soften the cone borders.
  131. Spot shadow mapping
  132. ^^^^^^^^^^^^^^^^^^^
  133. Spots don't need any parameters for shadow mapping. Keep in mind that, at more than 89 degrees of aperture, shadows
  134. stop functioning for spots, and you should consider using an Omni light instead.
  135. Shadow atlas
  136. ~~~~~~~~~~~~
  137. Unlike Directional lights, which have their own shadow texture, Omni and Spot lights are assigned to slots of a shadow atlas.
  138. This atlas can be configured in Project Settings -> Rendering -> Quality -> Shadow Atlas.
  139. .. image:: img/shadow_atlas.png
  140. The resolution applies to the whole Shadow Atlas. This atlas is divided into four quadrants:
  141. .. image:: img/shadow_quadrants.png
  142. Each quadrant can be subdivided to allocate any number of shadow maps; the following is the default subdivision:
  143. .. image:: img/shadow_quadrants2.png
  144. The allocation logic is simple. The biggest shadow map size (when no subdivision is used)
  145. represents a light the size of the screen (or bigger).
  146. Subdivisions (smaller maps) represent shadows for lights that are further away
  147. from view and proportionally smaller.
  148. Every frame, the following procedure is performed for all lights:
  149. 1. Check if the light is on a slot of the right size. If not, re-render it and move it to a larger/smaller slot.
  150. 2. Check if any object affecting the shadow map has changed. If it did, re-render the light.
  151. 3. If neither of the above has happened, nothing is done, and the shadow is left untouched.
  152. If the slots in a quadrant are full, lights are pushed back to smaller slots, depending on size and distance.
  153. This allocation strategy works for most games, but you may want to use a separate one in some cases (for example, a top-down game where
  154. all lights are around the same size and quadrants may all have the same subdivision).
  155. Shadow filter quality
  156. ~~~~~~~~~~~~~~~~~~~~~
  157. The filter quality of shadows can be tweaked. This can be found in
  158. Project Settings -> Rendering -> Quality -> Shadows.
  159. Godot supports no filter, PCF5 and PCF13.
  160. .. image:: img/shadow_pcf1.png
  161. It affects the blockyness of the shadow outline:
  162. .. image:: img/shadow_pcf2.png