baked_lightmaps.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. .. _doc_baked_lightmaps:
  2. Baked lightmaps
  3. ===============
  4. Introduction
  5. ------------
  6. Baked lightmaps are an alternative workflow for adding indirect (or fully baked)
  7. lighting to a scene. Unlike the :ref:`doc_gi_probes` approach, baked lightmaps
  8. work fine on low-end PCs and mobile devices, as they consume almost no resources
  9. at run-time. Also unlike GIProbe, baked lightmaps can optionally be used to
  10. store direct lighting, which provides even further performance gains.
  11. Unlike GIProbes, Baked Lightmaps are completely static. Once baked, they
  12. can't be modified at all. They also don't provide the scene with reflections, so
  13. using :ref:`doc_reflection_probes` together with it on interiors (or using a Sky
  14. on exteriors) is a requirement to get good quality.
  15. As they are baked, they have fewer problems than ``GIProbe`` regarding light
  16. bleeding, and indirect light will often look better. The downside is that baking
  17. lightmaps takes much longer than baking a GIProbe. While baking a GIProbe can be
  18. done in a matter of seconds, baking lightmaps will take several minutes if not
  19. more. This can slow down iteration speed significantly, so it is recommended to
  20. bake lightmaps only when you actually need to see changes in lighting.
  21. Baking lightmaps will also reserve baked materials' UV2 slot, which means you can
  22. no longer use it for other purposes in materials (either in the built-in
  23. :ref:`doc_spatial_material` or in custom shaders).
  24. In the end, deciding which indirect lighting approach is better depends on your
  25. use case. In general, GIProbe is easier to set up and works better with dynamic
  26. objects. For mobile or low-end compatibility, though, baked lightmaps are your
  27. only choice.
  28. Visual comparison
  29. -----------------
  30. Here are some comparisons of how BakedLightmap vs. GIProbe look. Notice that
  31. lightmaps are more accurate, but also suffer from the fact
  32. that lighting is on an unwrapped texture, so transitions and resolution may not
  33. be that good. GIProbe looks less accurate (as it's an approximation), but
  34. smoother overall.
  35. .. image:: img/baked_light_comparison.png
  36. Setting up
  37. ----------
  38. First of all, before the lightmapper can do anything, the objects to be baked need
  39. an UV2 layer and a texture size. An UV2 layer is a set of secondary texture coordinates
  40. that ensures any face in the object has its own place in the UV map. Faces must
  41. not share pixels in the texture.
  42. There are a few ways to ensure your object has a unique UV2 layer and texture size:
  43. Unwrap on scene import
  44. ~~~~~~~~~~~~~~~~~~~~~~
  45. This is probably the best approach overall. The only downside is that, on large
  46. models, unwrapping can take a while on import. Nonetheless, Godot will cache the UV2
  47. across reimports, so it will only be regenerated when needed.
  48. Select the imported scene in the filesystem dock, then go to the **Import** dock.
  49. There, the following option can be modified:
  50. .. image:: img/baked_light_import.png
  51. The **Light Baking** mode needs to be set to **Gen Lightmaps**. A texel size
  52. in world units must also be provided, as this will determine the
  53. final size of the lightmap texture (and, in consequence, the UV padding in the map).
  54. The effect of setting this option is that all meshes within the scene will have
  55. their UV2 maps properly generated.
  56. .. warning::
  57. When reusing a mesh within a scene, keep in mind that UVs will be generated
  58. for the first instance found. If the mesh is re-used with different scales
  59. (and the scales are wildly different, more than half or twice), this will
  60. result in inefficient lightmaps. Don't reuse a source mesh at significantly
  61. different scales if you are planning to use lightmapping.
  62. Also, the ``*.unwrap_cache`` files should *not* be ignored in version control
  63. as these files guarantee that UV2 reimports are consistent across platforms
  64. and engine versions.
  65. Unwrap from within Godot
  66. ~~~~~~~~~~~~~~~~~~~~~~~~
  67. Godot has an option to unwrap meshes and visualize the UV channels.
  68. It can be found in the Mesh menu:
  69. .. image:: img/baked_light_mesh_menu.png
  70. This will generate a second set of UV2 coordinates which can be used for baking,
  71. and it will also set the texture size automatically.
  72. Unwrap from your 3D DCC
  73. ~~~~~~~~~~~~~~~~~~~~~~~
  74. The last option is to do it from your favorite 3D app. This approach is generally
  75. not recommended, but it's explained first so that you know it exists.
  76. The main advantage is that, on complex objects that you may want to re-import a
  77. lot, the texture generation process can be quite costly within Godot,
  78. so having it unwrapped before import can be faster.
  79. Simply do an unwrap on the second UV2 layer.
  80. .. image:: img/baked_light_blender.png
  81. Then import the 3D scene normally. Remember you will need to set the texture
  82. size on the mesh after import.
  83. .. image:: img/baked_light_lmsize.png
  84. If you use external meshes on import, the size will be kept.
  85. Be wary that most unwrappers in 3D DCCs are not quality oriented, as they are
  86. meant to work quickly. You will mostly need to use seams or other techniques to
  87. create better unwrapping.
  88. Checking UV2
  89. ~~~~~~~~~~~~
  90. In the mesh menu mentioned before, the UV2 texture coordinates can be visualized.
  91. Make sure, if something is failing, to check that the meshes have these UV2 coordinates:
  92. .. image:: img/baked_light_uvchannel.png
  93. Setting up the scene
  94. --------------------
  95. Before anything is done, a **BakedLightmap** node needs to be added to a scene.
  96. This will enable light baking on all nodes (and sub-nodes) in that scene, even
  97. on instanced scenes.
  98. .. image:: img/baked_light_scene.png
  99. A sub-scene can be instanced several times, as this is supported by the baker, and
  100. each will be assigned a lightmap of its own (just make sure to respect the rule
  101. about scaling mentioned before):
  102. Configure bounds
  103. ~~~~~~~~~~~~~~~~
  104. Lightmap needs an approximate volume of the area affected because it uses it to
  105. transfer light to dynamic objects inside it (more on that later). Just
  106. cover the scene with the volume as you do with ``GIProbe``:
  107. .. image:: img/baked_light_bounds.png
  108. Setting up meshes
  109. ~~~~~~~~~~~~~~~~~
  110. For a **MeshInstance** node to take part in the baking process, it needs to have
  111. the **Use in Baked Light** property enabled.
  112. .. image:: img/baked_light_use.png
  113. When auto-generating lightmaps on scene import, this is enabled automatically.
  114. Setting up lights
  115. ~~~~~~~~~~~~~~~~~
  116. Lights are baked with indirect light by default. This means that shadowmapping
  117. and lighting are still dynamic and affect moving objects, but light bounces from
  118. that light will be baked.
  119. Lights can be disabled (no bake) or be fully baked (direct and indirect). This
  120. can be controlled from the **Bake Mode** menu in lights:
  121. .. image:: img/baked_light_bake_mode.png
  122. The modes are:
  123. Disabled
  124. ^^^^^^^^
  125. The light is ignored when baking lightmaps. Keep in mind hiding a light will have
  126. no effect for baking, so this must be used instead of hiding the Light node.
  127. This is the mode to use for dynamic lighting effects such as explosions and weapon effects.
  128. Indirect
  129. ^^^^^^^^
  130. This is the default mode, and is a compromise between performance and real-time
  131. friendliness. Only indirect lighting will be baked. Direct light and shadows are
  132. still real-time, as they would be without BakedLightmap.
  133. This mode allows performing *subtle* changes to a light's color, energy and
  134. position while still looking fairly correct. For example, you can use this
  135. to create flickering static torches that have their indirect light baked.
  136. All
  137. ^^^
  138. Both indirect and direct lighting will be baked. Since static surfaces can skip
  139. lighting and shadow computations entirely, this mode provides the best
  140. performance along with smooth shadows that never fade based on distance. The
  141. real-time light will not affect baked surfaces anymore, but it will still affect
  142. dynamic objects. When using the **All** bake mode on a light, dynamic objects
  143. will not cast real-time shadows onto baked surfaces, so you need to use a
  144. different approach such as blob shadows instead. Blob shadows can be implemented
  145. with a Sprite3D + RayCast setup, or a negative SpotLight pointing down with its
  146. bake mode set to **Disabled**.
  147. The light will not be adjustable at all during gameplay. Moving
  148. the light and changing its color or energy will not have any effect on static surfaces.
  149. Since bake modes can be adjusted on a per-light basis, it is possible to create
  150. hybrid baked light setups. One popular option is to use a real-time
  151. DirectionalLight with its bake mode set to **Indirect**, and use the **All**
  152. bake mode for OmniLights and SpotLights. This provides good performance while
  153. still allowing dynamic objects to cast real-time shadows in outdoor areas.
  154. After selecting the **All** bake mode on a light, you can optionally
  155. specify a **Size** greater than 0 for the light in the inspector.
  156. This size is used to provide softer shadows depending on the distance between
  157. the shadow caster and the object receiving the shadow. This mimics real life
  158. shadow appearance:
  159. .. image:: img/baked_light_omnilight_size.png
  160. The light's **Size** property is ignored for real-time shadows; it will only affect baked
  161. shadows. When the **Size** property is changed, lightmaps must be baked again to
  162. make changes visible.
  163. Baking
  164. ------
  165. To begin the bake process, just push the **Bake Lightmaps** button on top
  166. when selecting the BakedLightmap node:
  167. .. image:: img/baked_light_bake.png
  168. This can take from seconds to minutes (or hours) depending on scene size, bake
  169. method and quality selected.
  170. Balancing bake times with quality
  171. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. Since high-quality bakes can take very long (up to several hours for large complex scenes),
  173. it is recommended to use lower quality settings at first. Then, once you are confident
  174. with your scene's lighting setup, raise the quality settings and perform a "final"
  175. bake before exporting your project.
  176. .. note::
  177. By default, the lightmap baker will use all the system's logical CPU cores
  178. to speed up baking. This can reduce system responsiveness. To preserve system
  179. responsiveness while lightmaps are baking, you can reduce the number of CPU threads
  180. used to bake lightmaps. Keeping 1 or 2 CPU threads free will help improve
  181. system responsiveness, which is useful when multi-tasking while lightmaps are
  182. baking at the cost of slowing down lightmap baking slightly.
  183. To do so, open **Editor > Editor Settings** and adjust
  184. **Editors > 3d > Lightmap Baking Number Of Cpu Threads**.
  185. The default value (``0``) uses all of the system's logical CPU cores.
  186. Positive values will specify a number of threads to use, while negative
  187. values will subtract from the total number of logical CPU cores in the system.
  188. For example, on a system with 8 logical CPU cores, adjusting the setting to
  189. ``-1`` will use 7 CPU threads for lightmap baking.
  190. Configuring bake
  191. ~~~~~~~~~~~~~~~~
  192. Several more options are present for baking:
  193. - **Bake Extents**: The size of the area affected. This can be edited in the 3D
  194. editor viewport using the handles. Any object that can have lightmaps baked and
  195. is *touching* the bake extents will have lightmaps baked for it, but dynamic
  196. object capture will only work within the extents.
  197. Tweaks
  198. ^^^^^^
  199. - **Quality:** Four bake quality modes are provided: Low, Medium, High, and Ultra.
  200. Higher quality takes more time, but result in a better-looking lightmap with
  201. less noise. The difference is especially noticeable with emissive materials or
  202. areas that get little to no direct lighting.
  203. - **Bounces:** The number of bounces to use for indirect lighting. The default value (3)
  204. is a good compromise between bake times and quality. Higher values will make
  205. light bounce around more times before it stops, which makes indirect lighting
  206. look smoother (but also brighter). During the initial lighting iteration work,
  207. it is recommended to decrease the number of bounces to 1 to speed up baking.
  208. Remember that your scene will be darker when decreasing the number of bounces.
  209. - **Use Denoiser:** If enabled, uses OpenImageDenoise to make the lightmap
  210. significantly less noisy. This increases bake times and can occasionally
  211. introduce artifacts, but the result is often worth it.
  212. - **Use Hdr:** If disabled, lightmaps are smaller on disk, but they won't be
  213. able to capture any light over white (1.0). This will result in visible clipping
  214. if you have bright lights in your scene. When HDR is disabled, banding may also
  215. be visible in the lightmap.
  216. - **Use Color:** If disabled, lightmaps are smaller on disk, but the lightmap
  217. won't be able to store colored lighting. When baking indirect light only, the
  218. difference may be barely visible since indirect light is generally not highly
  219. saturated. However, when baking both direct and indirect lighting using the
  220. **All** bake mode on a light, this will turn colored lighting into grayscale
  221. lighting. This can be disabled together with HDR to get the smallest possible
  222. lightmap file at a given resolution.
  223. - **Bias:** The offset value to use for shadows in 3D units. You generally don't
  224. need to change this value, except if you run into issues with light bleeding or
  225. dark spots in your lightmap after baking. This setting does not affect real-time
  226. shadows casted on baked surfaces.
  227. - **Default Texels Per Unit:** For meshes that do not specify their own lightmap
  228. texel density, this will be used as the value. Higher values result in
  229. *lower-resolution* lightmaps, which result in faster bake times and lower file
  230. sizes at the cost of blurrier indirect lighting and shadows.
  231. Atlas
  232. ^^^^^
  233. - **Generate:** If enabled, a texture atlas will be generated for the lightmap.
  234. This results in more efficient rendering, but is only compatible with the
  235. GLES3 rendering backend. Disable this setting if your project is allowed to
  236. fall back to GLES2. (This is not the case by default and must be enabled in
  237. the Project Settings.) *This setting is ignored when the project is configured
  238. to use GLES2 by default.*
  239. - **Max Size:** The maximum size of the atlas in pixels. Higher values result
  240. in a more efficient atlas, but are less compatible with old/low-end hardware.
  241. If in doubt, leave this setting on its default value (4096).
  242. Capture
  243. ^^^^^^^
  244. - **Enabled:** This enables probe capture so that dynamic objects can *receive* indirect lighting.
  245. Regardless of this setting's value, dynamic objects will not be able to
  246. *contribute* indirect lighting to the scene. This is a limitation of lightmaps.
  247. - **Cell Size:** The distance between lightmap probes in 3D units. Higher values
  248. result in more sparse probe placement, which decreases bake times and file
  249. size at the cost of lower lighting accuracy for dynamic objects.
  250. - **Quality:** The lightmap probe generation quality. Higher values result in
  251. more accurate lighting, but take longer to bake. This setting does not affect
  252. the *density* of the lightmap probes, only their quality.
  253. - **Propagation:** Similar to :ref:`GIProbe <doc_gi_probes>`'s Propagation property.
  254. Higher values result in brighter and more diffuse indirect lighting for
  255. dynamic objects. Adjust this value depending on your scene to make dynamic
  256. objects better fit with static baked lighting.
  257. Data
  258. ^^^^
  259. - **Light Data**: Contains the light baked data after baking. Textures are saved
  260. to disk, but this also contains the capture data for dynamic objects, which can
  261. be heavy. If you are using a scene in ``.tscn`` format, you should save this
  262. resource to an external binary ``.lmbake`` file to avoid bloating the ``.tscn``
  263. scene with binary data encoded in Base64.
  264. The Light Data resource can be edited to adjust two additional properties:
  265. - **Energy:** Adjusts the lightmap's brightness. Higher values result in brighter lightmaps.
  266. This can be adjusted at run-time for short-lived dynamic effects such as thunderstorms.
  267. However, keep in mind that it will affect *all* baked lights.
  268. - **Interior:** If enabled, dynamic objects will not make use of environment lighting
  269. and will use light probes for ambient lighting exclusively. If disabled, both
  270. environment lighting and light probes are used to light up dynamic objects.
  271. .. tip::
  272. The generated EXR file can be viewed and even edited using an image editor
  273. to perform post-processing if needed. However, keep in mind that changes to
  274. the EXR file will be lost when baking lightmaps again.
  275. Dynamic objects
  276. ---------------
  277. In other engines or lightmapper implementations, you are generally required to
  278. manually place small objects called "lightprobes" all around the level to
  279. generate *capture* data. This is then used to transfer the light to dynamic
  280. objects that move around the scene.
  281. However, this implementation of lightmapping uses a different method. The process is
  282. automatic, so you don't have to do anything. Just move your objects around, and
  283. they will be lit accordingly. Of course, you have to make sure you set up your
  284. scene bounds accordingly or it won't work.
  285. .. image:: img/baked_light_indirect.gif