high_dynamic_range.rst 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. High Dynamic Range
  2. ==================
  3. Introduction
  4. ------------
  5. Normally, an artist does all the 3D modelling, then all the texturing,
  6. looks at his or her awesome looking model in the 3D DCC and says "looks
  7. fantastic, ready for integration!" then goes into the game, lighting is
  8. setup and the game runs.
  9. So where does all this HDR stuff thing come from? The idea is that
  10. instead of dealing with colors that go from black to white (0 to 1), we
  11. use colors whiter than white (for example, 0 to 8 times white).
  12. | To be more practical, imagine that in a regular scene, the intensity
  13. of a light (generally 1.0) is set to 5.0. The whole scene will turn
  14. very bright (towards white) and look horrible.
  15. | After this the luminance of the scene is computed by averaging the
  16. luminance of every pixel of it, and this value is used to bring the
  17. scene back to normal ranges. This last operation is called
  18. tone-mapping. Finally, we are at a similar place from where we
  19. started:
  20. .. image:: /img/hdr_tonemap.png
  21. Except the scene is more contrasted, because there is a higher light
  22. range in play. What is this all useful for? The idea is that the scene
  23. luminance will change while you move through the world, allowing
  24. situations like this to happen:
  25. .. image:: /img/hdr_cave.png
  26. Additionally, it is possible to set a threshold value to send to the
  27. glow buffer depending on the pixel luminance. This allows for more
  28. realistic light bleeding effects in the scene.
  29. Linear Color Space
  30. ------------------
  31. | The problem with this technique is that computer monitors apply a
  32. gamma curve to adapt better to the way the human eye sees. Artists
  33. create their art on the screen too, so their art has an implicit gamma
  34. curve applied to it.
  35. | The color space where images created in computer monitors exist is
  36. called "sRGB". Every visual content that people has on their computers
  37. or downloads from the internet (such as pictures, movies, porn, etc)
  38. is in this colorspace.
  39. .. image:: /img/hdr_gamma.png
  40. The mathematics of HDR require that we multiply the scene by different
  41. values to adjust the luminance and exposure to different light ranges,
  42. and this curve gets in the way as we need colors in linear space for
  43. this.
  44. Linear Color Space & Asset Pipeline
  45. -----------------------------------
  46. Working in HDR is not just pressing a switch. First, imported image
  47. assets must be converted to linear space on import. There are two ways
  48. to do this:
  49. SRGB->Linear conversion on image import
  50. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. This is the most compatible way of using linear-space assets and it will
  52. work everywhere including all mobile devices. The main issue with this
  53. is loss of quality, as sRGB exists to avoid this same problem. Using 8
  54. bits per channel to represent linear colors is inefficient from the
  55. point of view of the human eye. These textures might be later compressed
  56. too, which makes the problem worse.
  57. In any case though, this is the easy solution that works everywhere.
  58. Hardware sRGB -> Linear conversion.
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. This is the most correct way to use assets in linear-space, as the
  61. texture sampler on the GPU will do the conversion after reading the
  62. texel using floating point. This works fine on PC and consoles, but most
  63. mobile devices do no support it, or do not support it on compressed
  64. texture format (iOS for example).
  65. Linear -> sRGB at the end.
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. After all the rendering is done, the linear-space rendered image must be
  68. converted back to sRGB. To do this, simply enable sRGB conversion in the
  69. current
  70. `Environment <https://github.com/okamstudio/godot/wiki/class_environment>`__
  71. (more on that below).
  72. Keep in mind that sRGB [STRIKEOUT:> Linear and Linear]> sRGB conversions
  73. must always be **both** enabled. Failing to enable one of them will
  74. result in horrible visuals suitable only for avant garde experimental
  75. indie games.
  76. Parameters of HDR
  77. -----------------
  78. HDR is found in the
  79. `Environment <https://github.com/okamstudio/godot/wiki/class_environment>`__
  80. resource. These are found most of the time inside a
  81. `WorldEnvironment <https://github.com/okamstudio/godot/wiki/class_worldenvironment>`__
  82. node, or set in a camera. There are many parameters for HDR:
  83. .. image:: /img/hdr_parameters.png
  84. ToneMapper
  85. ~~~~~~~~~~
  86. The ToneMapper is the heart of the algorithm. Many options for
  87. tonemappers are provided:
  88. - Linear: Simplest tonemapper. It does it's job for adjusting scene
  89. brightness, but if the differences in light are too big, it will
  90. cause colors to be too saturated.
  91. - Log: Similar to linear, but not as extreme.
  92. - Reinhardt: Classical tonemapper (modified so it will not desaturate
  93. as much)
  94. - ReinhardtAutoWhite: Same as above, but uses the max scene luminance
  95. to adjust the white value.
  96. Exposure
  97. ~~~~~~~~
  98. The same exposure parameter as in real cameras. Controls how much light
  99. enters the camera. Higher values will result in a brighter scene and
  100. lower values will result in a darker scene.
  101. White
  102. ~~~~~
  103. Maximum value of white.
  104. Glow Threshold
  105. ~~~~~~~~~~~~~~
  106. Determine above which value (from 0 to 1 after the scene is tonemapped),
  107. light will start bleeding.
  108. Glow Scale
  109. ~~~~~~~~~~
  110. Determine how much light will bleed.
  111. Min Luminance
  112. ~~~~~~~~~~~~~
  113. Lower bound value of light for the scene at which the tonemapper stops
  114. working. This allows dark scenes to remain dark.
  115. Max Luminance
  116. ~~~~~~~~~~~~~
  117. Upper bound value of light for the scene at which the tonemapper stops
  118. working. This allows bright scenes to remain saturated.
  119. Exposure Adjustment Speed
  120. ~~~~~~~~~~~~~~~~~~~~~~~~~
  121. Auto-exposure will change slowly and will take a while to adjust (like
  122. in real cameras). Bigger values means faster adjustment.
  123. *Juan Linietsky, Ariel Manzur, Distributed under the terms of the `CC
  124. By <https://creativecommons.org/licenses/by/3.0/legalcode>`__ license.*