using_lightmap_gi.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. .. _doc_using_lightmap_gi:
  2. Using Lightmap global illumination
  3. ==================================
  4. Baked lightmaps are a workflow for adding indirect (or fully baked)
  5. lighting to a scene. Unlike the :ref:`VoxelGI <doc_using_voxel_gi>` and
  6. :ref:`SDFGI <doc_sdfgi>` approaches, baked lightmaps work fine on low-end PCs
  7. and mobile devices, as they consume almost no resources at run-time. Also unlike
  8. VoxelGI and SDFGI, baked lightmaps can optionally be used to store direct
  9. lighting, which provides even further performance gains.
  10. Unlike VoxelGI and SDFGI, baked lightmaps are completely static. Once baked, they
  11. can't be modified at all. They also don't provide the scene with reflections, so
  12. using :ref:`doc_reflection_probes` together with it on interiors (or using a Sky
  13. on exteriors) is a requirement to get good quality.
  14. As they are baked, they have fewer problems than VoxelGI and SDFGI regarding
  15. light bleeding, and indirect light will often look better. The downside is that
  16. baking lightmaps takes longer compared to baking VoxelGI. While baking VoxelGI
  17. can be done in a matter of seconds, baking lightmaps can take several minutes if
  18. not more. This can slow down iteration speed significantly, so it is recommended
  19. to bake lightmaps only when you actually need to see changes in lighting. Since
  20. Godot 4.0, lightmaps are baked on the GPU, making light baking faster if you
  21. have a mid-range or high-end dedicated GPU.
  22. Baking lightmaps will also reserve baked materials' UV2 slot, which means you can
  23. no longer use it for other purposes in materials (either in the built-in
  24. :ref:`doc_standard_material_3d` or in custom shaders).
  25. Despite their lack of flexibility, baked lightmaps typically offer both the best
  26. quality *and* performance at the same time in (mostly) static scenes. This makes
  27. lightmaps still popular in game development, despite lightmaps being the
  28. oldest technique for global illumination in video games.
  29. .. seealso::
  30. Not sure if LightmapGI is suited to your needs?
  31. See :ref:`doc_introduction_to_global_illumination_comparison`
  32. for a comparison of GI techniques available in Godot 4.
  33. Visual comparison
  34. -----------------
  35. .. figure:: img/gi_none.webp
  36. :alt: LightmapGI disabled.
  37. LightmapGI disabled.
  38. .. figure:: img/gi_lightmap_gi_indirect_only.webp
  39. :alt: LightmapGI enabled (with indirect light baked only).
  40. LightmapGI enabled (with indirect light baked only). Direct light is still
  41. real-time, allowing for subtle changes during gameplay.
  42. .. figure:: img/gi_lightmap_gi_direct_and_indirect.webp
  43. :alt: LightmapGI enabled (with direct and indirect light baked).
  44. LightmapGI enabled (with direct and indirect light baked). Best performance,
  45. but lower quality visuals. Notice the blurrier sun shadow in the top-right
  46. corner.
  47. Visual comparison
  48. -----------------
  49. Here are some comparisons of how LightmapGI vs. VoxelGI look. Notice that
  50. lightmaps are more accurate, but also suffer from the fact
  51. that lighting is on an unwrapped texture, so transitions and resolution may not
  52. be that good. VoxelGI looks less accurate (as it's an approximation), but
  53. smoother overall.
  54. .. image:: img/lightmap_gi_comparison.png
  55. SDFGI is also less accurate compared to LightmapGI. However, SDFGI can support
  56. large open worlds without any need for baking.
  57. Setting up
  58. ----------
  59. First of all, before the lightmapper can do anything, the objects to be baked need
  60. an UV2 layer and a texture size. An UV2 layer is a set of secondary texture coordinates
  61. that ensures any face in the object has its own place in the UV map. Faces must
  62. not share pixels in the texture.
  63. There are a few ways to ensure your object has a unique UV2 layer and texture size:
  64. Unwrap on scene import (recommended)
  65. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  66. In most scenarios, this is the best approach to use. The only downside is that,
  67. on large models, unwrapping can take a while on import. Nonetheless, Godot will
  68. cache the UV2 across reimports, so it will only be regenerated when needed.
  69. Select the imported scene in the filesystem dock, then go to the **Import** dock.
  70. There, the following option can be modified:
  71. .. image:: img/lightmap_gi_import.webp
  72. The **Meshes > Light Baking** option must be set to **Static Lightmaps (VoxelGI/SDFGI/LightmapGI)**:
  73. .. image:: img/lightmap_gi_mesh_import_meshes.webp
  74. When unwrapping on import, you can adjust the texture size using the **Meshes > Lightmap
  75. Texel Size** option. *Lower* values will result in more detailed lightmaps,
  76. possibly resulting in higher visual quality at the cost of longer bake times and
  77. larger lightmap file sizes. The default value of ``0.2`` is suited for
  78. small/medium-sized scenes, but you may want to increase it to ``0.5`` or even
  79. more for larger scenes. This is especially the case if you're baking indirect
  80. lighting only, as indirect light is low-frequency data (which means it doesn't
  81. need high-resolution textures to be accurately represented).
  82. The effect of setting this option is that all meshes within the scene will have
  83. their UV2 maps properly generated.
  84. .. warning::
  85. When reusing a mesh within a scene, keep in mind that UVs will be generated
  86. for the first instance found. If the mesh is re-used with different scales
  87. (and the scales are wildly different, more than half or twice), this will
  88. result in inefficient lightmaps. To avoid this, adjust the **Lightmap
  89. Scale** property in the GeometryInstance3D section of a MeshInstance3D node.
  90. This lets you *increase* the level of lightmap detail for specific
  91. MeshInstance3D nodes (but not decrease it).
  92. Also, the ``*.unwrap_cache`` files should *not* be ignored in version control
  93. as these files guarantee that UV2 reimports are consistent across platforms
  94. and engine versions.
  95. Unwrap from within Godot
  96. ^^^^^^^^^^^^^^^^^^^^^^^^
  97. .. warning::
  98. If this Mesh menu operation is used on an imported 3D scene, the generated
  99. UV2 will be lost when the scene is reloaded.
  100. Godot has an option to unwrap meshes and visualize the UV channels. After
  101. selecting a MeshInstance3D node, it can be found in the **Mesh** menu at the top
  102. of the 3D editor viewport:
  103. .. image:: img/lightmap_gi_mesh_menu.png
  104. This will generate a second set of UV2 coordinates which can be used for baking.
  105. It will also set the texture size automatically.
  106. Unwrap from your 3D DCC
  107. ^^^^^^^^^^^^^^^^^^^^^^^
  108. The last option is to do it from your favorite 3D app. This approach is
  109. generally **not recommended**, but it's explained so that you know it exists.
  110. The main advantage is that, on complex objects that you may want to re-import a
  111. lot, the texture generation process can be quite costly within Godot, so having
  112. it unwrapped before import can be faster.
  113. Simply do an unwrap on the second UV2 layer.
  114. .. image:: img/lightmap_gi_blender.png
  115. Then import the 3D scene normally. Remember you will need to set the texture
  116. size on the mesh after import.
  117. .. image:: img/lightmap_gi_lmsize.png
  118. If you use external meshes on import, the size will be kept.
  119. Be wary that most unwrappers in 3D DCCs are not quality oriented, as they are
  120. meant to work quickly. You will mostly need to use seams or other techniques to
  121. create better unwrapping.
  122. Checking UV2
  123. ^^^^^^^^^^^^
  124. In the **Mesh** menu mentioned before, the UV2 texture coordinates can be visualized.
  125. If something is failing, double-check that the meshes have these UV2 coordinates:
  126. .. image:: img/lightmap_gi_uvchannel.png
  127. Setting up the scene
  128. --------------------
  129. Before anything is done, a **LightmapGI** node needs to be added to a scene.
  130. This will enable light baking on all nodes (and sub-nodes) in that scene, even
  131. on instanced scenes.
  132. .. image:: img/lightmap_gi_scene.png
  133. A sub-scene can be instanced several times, as this is supported by the baker.
  134. Each instance will be assigned a lightmap of its own. To avoid issues with
  135. inconsistent lightmap texel scaling, make sure to respect the rule about mesh
  136. scaling mentioned before.
  137. Setting up meshes
  138. ^^^^^^^^^^^^^^^^^
  139. For a **MeshInstance3D** node to take part in the baking process, it needs to have
  140. its bake mode set to **Static**. Meshes that have their bake mode set to **Disabled**
  141. or **Dynamic** will be ignored by the lightmapper.
  142. .. image:: img/lightmap_gi_use.png
  143. When auto-generating lightmaps on scene import, this is enabled automatically.
  144. Setting up lights
  145. ^^^^^^^^^^^^^^^^^
  146. Lights are baked with indirect light only by default. This means that shadowmapping
  147. and lighting are still dynamic and affect moving objects, but light bounces from
  148. that light will be baked.
  149. Lights can be disabled (no bake) or be fully baked (direct and indirect). This
  150. can be controlled from the **Bake Mode** menu in lights:
  151. .. image:: img/lightmap_gi_bake_mode.png
  152. The modes are:
  153. Disabled
  154. ^^^^^^^^
  155. The light is ignored when baking lightmaps. Keep in mind hiding a light will have
  156. no effect for baking, so this must be used instead of hiding the Light node.
  157. This is the mode to use for dynamic lighting effects such as explosions and weapon effects.
  158. Dynamic
  159. ^^^^^^^
  160. This is the default mode, and is a compromise between performance and real-time
  161. friendliness. Only indirect lighting will be baked. Direct light and shadows are
  162. still real-time, as they would be without LightmapGI.
  163. This mode allows performing *subtle* changes to a light's color, energy and
  164. position while still looking fairly correct. For example, you can use this
  165. to create flickering static torches that have their indirect light baked.
  166. Static
  167. ^^^^^^
  168. Both indirect and direct lighting will be baked. Since static surfaces can skip
  169. lighting and shadow computations entirely, this mode provides the best
  170. performance along with smooth shadows that never fade based on distance. The
  171. real-time light will not affect baked surfaces anymore, but it will still affect
  172. dynamic objects. When using the **All** bake mode on a light, dynamic objects
  173. will not cast real-time shadows onto baked surfaces, so you need to use a
  174. different approach such as blob shadows instead. Blob shadows can be implemented
  175. with a Decal node.
  176. The light will not be adjustable at all during gameplay. Moving the light or
  177. changing its color (or energy) will not have any effect on static surfaces.
  178. Since bake modes can be adjusted on a per-light basis, it is possible to create
  179. hybrid baked light setups. One popular option is to use a real-time
  180. DirectionalLight with its bake mode set to **Dynamic**, and use the **Static**
  181. bake mode for OmniLights and SpotLights. This provides good performance while
  182. still allowing dynamic objects to cast real-time shadows in outdoor areas.
  183. Fully baked lights can also make use of light nodes' **Size** (omni/spot) or
  184. **Angular Distance** (directional) properties. This allows for shadows with
  185. realistic penumbra that increases in size as the distance between the caster and
  186. the shadow increases. This also has a lower performance cost compared to
  187. real-time PCSS shadows, as only dynamic objects have real-time shadows rendered
  188. on them.
  189. .. image:: img/lightmap_gi_omnilight_size.png
  190. Baking
  191. ------
  192. To begin the bake process, click the **Bake Lightmaps** button at the top of the
  193. 3D editor viewport when selecting the LightmapGI node:
  194. .. image:: img/lightmap_gi_bake.png
  195. This can take from seconds to minutes (or hours) depending on scene size, bake
  196. method and quality selected.
  197. Tweaks
  198. ^^^^^^
  199. - **Quality:** Four bake quality modes are provided: Low, Medium, High, and
  200. Ultra. Higher quality takes more time, but result in a better-looking lightmap
  201. with less noise. The difference is especially noticeable with emissive
  202. materials or areas that get little to no direct lighting. Each bake quality
  203. mode can be further adjusted in the Project Settings.
  204. - **Bounces:** The number of bounces to use for indirect lighting. The default
  205. value (``3``) is a good compromise between bake times and quality. Higher
  206. values will make light bounce around more times before it stops, which makes
  207. indirect lighting look smoother (but also brighter). During the initial
  208. lighting iteration work, it is recommended to decrease the number of bounces
  209. to ``1`` to speed up baking. Remember that your scene will be darker when
  210. decreasing the number of bounces.
  211. - **Directional:** If enabled, stores directional information for lightmaps.
  212. This improves normal mapped materials' appearance for baked surfaces,
  213. especially with fully baked lights (since they also have direct light baked).
  214. The downside is that directional lightmaps are slightly more expensive to render.
  215. They also require more time to bake and result in larger file sizes.
  216. - **Interior:** If enabled, environment lighting will not be sourced. Use this
  217. for purely indoor scenes to avoid light leaks.
  218. - **Use Denoiser:** If enabled, uses `OpenImageDenoise <https://www.openimagedenoise.org/>`__
  219. to make the lightmap significantly less noisy. This increases bake times and can
  220. occasionally introduce artifacts, but the result is often worth it.
  221. **All** bake mode on a light, this will turn colored lighting into grayscale
  222. lighting. This can be disabled together with HDR to get the smallest possible
  223. lightmap file at a given resolution.
  224. - **Bias:** The offset value to use for shadows in 3D units. You generally don't
  225. need to change this value, except if you run into issues with light bleeding or
  226. dark spots in your lightmap after baking. This setting does not affect real-time
  227. shadows casted on baked surfaces (for lights with **Dynamic** bake mode).
  228. - **Max Texture Size:** The maximum texture size for the generated texture
  229. atlas. Higher values will result in fewer slices being generated, but may not
  230. work on all hardware as a result of hardware limitations on texture sizes.
  231. Leave this at its default value of ``16384`` if unsure.
  232. - **Environment > Mode:** Controls how environment lighting is sourced when
  233. baking lightmaps. The default value of **Scene** is suited for levels with
  234. visible exterior parts. For purely indoor scenes, set this to **Disabled** to
  235. avoid light leaks and speed up baking. This can also be set to **Custom Sky**
  236. or **Custom Color** to use environment lighting that differs from the actual
  237. scene's environment sky.
  238. - **Gen Probes > Subdiv:** See :ref:`doc_using_lightmap_gi_dynamic_objects`.
  239. - **Data > Light Data:** See :ref:`doc_using_lightmap_gi_data`.
  240. Balancing bake times with quality
  241. ---------------------------------
  242. Since high-quality bakes can take very long (up to dozens of minutes for large
  243. complex scenes), it is recommended to use lower quality settings at first. Then,
  244. once you are confident with your scene's lighting setup, raise the quality
  245. settings and perform a "final" bake before exporting your project.
  246. Reducing the lightmap resolution by increasing **Lightmap Texel Size** on the
  247. imported 3D scenes will also speed up baking significantly. However, this will
  248. require you to reimport all lightmapped 3D scenes before you can bake lightmaps
  249. again.
  250. .. _doc_using_lightmap_gi_dynamic_objects:
  251. Dynamic objects
  252. ---------------
  253. Unlike VoxelGI and SDFGI, dynamic objects receive indirect lighting differently
  254. compared to static objects. This is because lightmapping is only performed on
  255. static objects.
  256. To display indirect lighting on dynamic objects, a 3D probe system is used, with
  257. light probes being spread throughout the scene. When baking lightmaps, the
  258. lightmapper will calculate the amount of *indirect* light received by the probe.
  259. Direct light is not stored within light probes, even for lights that have their
  260. bake mode set to **Static** (as dynamic objects continue to be lit in
  261. real-time).
  262. There are 2 ways to add light probes to a scene:
  263. - **Automatic:** Set **Gen Probes > Subdiv** to a value other than **Disabled**,
  264. then bake lightmaps. The default is ``8``, but you can choose a greater value
  265. to improve precision at the cost of longer bake times and larger output file
  266. size.
  267. - **Manual:** In addition or as an alternative to generating probes
  268. automatically, you can add light probes manually by adding LightmapProbe nodes
  269. to the scene. This can be used to improve lighting detail in areas frequently
  270. travelled by dynamic objects.
  271. .. _doc_using_lightmap_gi_data:
  272. Lightmap data
  273. -------------
  274. The **Data > Light Data** property in the LightmapGI node contains the lightmap
  275. data after baking. Textures are saved to disk, but this also contains the
  276. capture data for dynamic objects, which can be heavy. If you are using a scene
  277. in ``.tscn`` format, you should save this resource to an external binary
  278. ``.lmbake`` file to avoid bloating the ``.tscn`` scene with binary data encoded
  279. in Base64.
  280. .. tip::
  281. The generated EXR file can be viewed and even edited using an image editor
  282. to perform post-processing if needed. However, keep in mind that changes to
  283. the EXR file will be lost when baking lightmaps again.