shadow_mapping.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. .. _doc_shadow_mapping:
  2. Shadow mapping
  3. ==============
  4. Introduction
  5. ------------
  6. Simply throwing a light is not enough to realistically illuminate a
  7. scene. It should be, in theory, but given the way video hardware
  8. works, parts of objects that should not be reached by light are lit
  9. anyway.
  10. Most people (including artists), see shadows as something projected by
  11. light, as if they were created by the light itself by darkening places
  12. that are hidden from the light source.
  13. This is actually not correct and it's important to understand that
  14. shadows are places where light simply does not reach. As a rule (and
  15. without counting indirect light) if a light is turned off, the places
  16. where shadow appear should remain the same. In other words, shadows
  17. should not be seen as something "added" to the scene, but as an area
  18. that "remains dark".
  19. All light types in Godot can use shadow mapping, and all support several
  20. different techniques that trade quality by performance. Shadow mapping
  21. uses a texture storing the "depth view" of the light and checks against
  22. it in real-time for each pixel it renders.
  23. The bigger the resolution of the shadow map texture, the more detail the
  24. shadow has, but more video memory and bandwidth consumed (which means
  25. frame-rate goes down).
  26. Shadows by light type
  27. ---------------------
  28. Directional light shadows
  29. ~~~~~~~~~~~~~~~~~~~~~~~~~
  30. Directional lights can affect a really big area. The bigger the scene,
  31. the bigger the affected area. Given the shadow map resolution stays the
  32. same, the same amount of shadow pixels cover a bigger area, resulting in
  33. blocky shadows. Multiple techniques exist to deal with resolution
  34. problems, but the most common one is PSSM (Parallel Split Shadow Maps):
  35. .. image:: /img/shadow_directional.png
  36. These techniques divide the view in 2 or 4 sections, and a shadow is
  37. rendered for each. This way, close objects can use larger shadow while
  38. further away objects will use one in less detail, but in proportion this
  39. seems to make the shadow map size increase while it's actually kept the
  40. same. Of course, this technique is not free, the more splits the more
  41. the performance goes down. On mobile, it is generally inconvenient to
  42. use more than 2 splits.
  43. An alternative technique is PSM (Perspective Shadow Mapping). This
  44. technique is much cheaper than PSSM (as cheap as orthogonal), but it
  45. only really works for a few camera angles respect to the light. In other
  46. words, PSM is only useful for games where the camera direction and light
  47. direction are both fixed, and the light is not parallel to the camera
  48. (which is when PSM completely breaks).
  49. Omni light shadows
  50. ~~~~~~~~~~~~~~~~~~
  51. Omnidirectional lights are also troublesome. How to represent 360 degrees
  52. of light with a single texture? There are two alternatives, the first
  53. one is to use DPSM (Dual Paraboloid Shadow Mapping). This technique is
  54. fast, but it requires DISCARD to be used (which makes it not very usable
  55. on mobile). DPSM can also look rather bad if the geometry is not
  56. tessellated enough, so more vertices might be necessary if it doesn't
  57. look tight. The second option is to simply not use a shadow map, and use
  58. a shadow cubemap. This is faster, but requires six passes to render all
  59. directions and is not supported on the current (GLES2) renderer.
  60. .. image:: /img/shadow_omni.png
  61. As few considerations when using DPSM shadow maps:
  62. - Keep Slope-Scale on 0.
  63. - Use a small value for Z-Offset, if this look wrong, make it smaller.
  64. - ESM filtering can improve the look.
  65. - The seams between the two halves of the shadow are generally
  66. noticeable, so rotate the light to make them show less.
  67. Spot light shadows
  68. ~~~~~~~~~~~~~~~~~~
  69. Spot light shadows are generally the simpler, just needing a single
  70. texture and no special techniques.
  71. .. image:: /img/shadow_spot.png
  72. Shadows parameters
  73. ------------------
  74. The fact that shadows are actually a texture can generate several
  75. problems. The most common is Z fighting (lines at the edge of the
  76. objects that cast the shadows. There are two ways to fix this, the first
  77. is to tweak the offset parameters, and the second is to use a filtered
  78. shadow algorithm, which generally looks better and has not as many
  79. glitches, but consumes more GPU time.
  80. Adjusting z-offset
  81. ~~~~~~~~~~~~~~~~~~
  82. So, you have decided to go with non-filtered shadows because they are
  83. faster, you want a little more detail or maybe you just like the sexy
  84. saw-like shadow outlines because they remind you of your favorite
  85. previous-gen games. Truth is, this can be kind of be a pain, but most of the
  86. time it can be adjusted to have nice results. There is no magic number and
  87. whatever result you come up will be different from scene to scene, it
  88. just takes a while of tweaking. Let's go step by step.
  89. First step is to turn on the shadows, let's assume that both Z-Offset
  90. and Z-Slope-Scale are at 0. You will be greeted by this:
  91. .. image:: /img/shadow_offset_1.png
  92. Holy crap, the shadow is all over the place and extremely glitchy! This
  93. happens because the shadow is fighting with the same geometry that is
  94. casting it. This is called "self-shadowing". To avoid this meaningless
  95. fight, you realize you need to make peace between the shadow and the
  96. geometry, so you push back the shadow a little by increasing the shadow
  97. Z-Offset. This improves things a lot:
  98. .. image:: /img/shadow_offset_2.png
  99. But it's not quite perfect, self shadowing did not disappear completely.
  100. So close to perfection but still not there.. so in a turn of greed you
  101. increase the Z-Offset even more!
  102. .. image:: /img/shadow_offset_3.png
  103. And it gets rid of those self-shadowings! Hooray! Except something is
  104. wrong.. oh, right. Being pushed back too much, the shadows start
  105. disconnecting from their casters, which looks pretty awful. Ok, you go
  106. back to the previous Z-offset.
  107. This is when Z-Slope-Scale comes to save the day. This setting makes
  108. shadow caster objects thinner, so the borders don't self-shadow:
  109. .. image:: /img/shadow_offset_4.png
  110. Aha! Finally something that looks acceptable. It's perfectly acceptable
  111. and you can perfectly ship a game that looks like this (imagine you are
  112. looking at Final Fantasy quality art btw, not this horrible attempt at 3D
  113. modelling). There may be very tiny bits left of self shadowing that no
  114. one cares about, so your inextinguishable greed kicks in again and you
  115. raise the Z-Slope Scale again:
  116. .. image:: /img/shadow_offset_5.png
  117. Well, that was too much, shadows casted are way too thin and don't look
  118. good anymore. Well, though luck, the previous setting was good anyway,
  119. let's accept that perfection does not exist and move on to something
  120. else.
  121. Important!
  122. ~~~~~~~~~~
  123. If you are using shadow maps with directional lights, make sure that
  124. the *view distance* of the *camera* is set to an *optimal range*. This
  125. means, if the distance between your camera and the visible end of the
  126. scene is 100, then set the view distance to that value. If a greater
  127. than necessary value is used, the shadow maps will lose detail as they
  128. will try to cover a bigger area.
  129. So, always make sure to use the optimal range!
  130. Shadow filtering
  131. ~~~~~~~~~~~~~~~~
  132. Raw shadows are blocky. Increasing their resolution just makes smaller
  133. blocks, but they are still blocks.
  134. Godot offers a few ways to filter them (shadow in the example is
  135. low-resolution on purpose!):
  136. .. image:: /img/shadow_filter_options.png
  137. PCF5 and PCF13 are simple texture-space filtering. Will make the texture
  138. a little more acceptable but still needs considerable resolution for it
  139. to look good.
  140. ESM is a more complex filter and has a few more tweaking parameters. ESM
  141. uses shadow blurring (amount of blur passes and multiplier can be
  142. adjusted).