volumetric_fog.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. .. _doc_volumetric_fog:
  2. Volumetric fog and fog volumes
  3. ==============================
  4. .. note::
  5. Volumetric fog is only supported in the Forward+ renderer, not the Mobile or
  6. Compatibility renderers.
  7. As described in :ref:`doc_environment_and_post_processing`, Godot supports
  8. various visual effects including two types of fog: traditional (non-volumetric)
  9. fog and volumetric fog. Traditional fog affects the entire scene at once and
  10. cannot be customized with :ref:`doc_fog_shader`.
  11. Volumetric fog can be used at the same time as non-volumetric fog if desired.
  12. On this page, you'll learn:
  13. - How to set up volumetric fog in Godot.
  14. - What fog volumes are and how they differ from "global" volumetric fog.
  15. .. seealso::
  16. You can see how volumetric fog works in action using the
  17. `Volumetric Fog demo project <https://github.com/godotengine/godot-demo-projects/tree/master/3d/volumetric_fog>`__.
  18. Here is a comparison between traditional fog (which does not interact with lighting)
  19. and volumetric fog, which is able to interact with lighting:
  20. .. image:: img/volumetric_fog_comparison.png
  21. Volumetric fog properties
  22. -------------------------
  23. After enabling volumetric fog in the WorldEnvironment node's Environment
  24. resource, you can edit the following properties:
  25. - **Density:** The base *exponential* density of the volumetric fog. Set this to
  26. the lowest density you want to have globally. FogVolumes can be used to add to
  27. or subtract from this density in specific areas. A value of ``0.0`` disables
  28. global volumetric fog while allowing FogVolumes to display volumetric fog in
  29. specific areas. Fog rendering is exponential as in real life.
  30. - **Albedo:** The Color of the volumetric fog when interacting with lights. Mist
  31. and fog have an albedo close to white (``Color(1, 1, 1, 1)``) while smoke
  32. has a darker albedo.
  33. - **Emission:** The emitted light from the volumetric fog. Even with emission,
  34. volumetric fog will not cast light onto other surfaces. Emission is useful to
  35. establish an ambient color. As the volumetric fog effect uses
  36. single-scattering only, fog tends to need a little bit of emission to soften
  37. the harsh shadows.
  38. - **Emission Energy:** The brightness of the emitted light from the volumetric
  39. fog.
  40. - **GI Inject:** Scales the strength of Global Illumination used in the
  41. volumetric fog's albedo color. A value of ``0.0`` means that Global
  42. Illumination will not impact the volumetric fog. This has a small performance
  43. cost when set above ``0.0``.
  44. - **Anisotropy:** The direction of scattered light as it goes through the
  45. volumetric fog. A value close to ``1.0`` means almost all light is scattered
  46. forward. A value close to ``0.0`` means light is scattered equally in all
  47. directions. A value close to ``-1.0`` means light is scattered mostly
  48. backward. Fog and mist scatter light slightly forward, while smoke scatters
  49. light equally in all directions.
  50. - **Length:** The distance over which the volumetric fog is computed. Increase
  51. to compute fog over a greater range, decrease to add more detail when a long
  52. range is not needed. For best quality fog, keep this as low as possible.
  53. - **Detail Spread:** The distribution of size down the length of the froxel
  54. buffer. A higher value compresses the froxels closer to the camera and places
  55. more detail closer to the camera.
  56. - **Ambient Inject:** Scales the strength of ambient light used in the
  57. volumetric fog. A value of ``0.0`` means that ambient light will not impact
  58. the volumetric fog. This has a small performance cost when set above ``0.0``.
  59. - **Sky Affect:** Controls how much volumetric fog should be drawn onto the
  60. background sky. If set to ``0.0``, volumetric fog won't affect sky rendering
  61. at all (including FogVolumes).
  62. Two additional properties are offered in the **Temporal Reprojection** section:
  63. - **Temporal Reprojection > Enabled:** Enables temporal reprojection in the
  64. volumetric fog. Temporal reprojection blends the current frame's volumetric
  65. fog with the last frame's volumetric fog to smooth out jagged edges. The
  66. performance cost is minimal, however it does lead to moving FogVolumes and
  67. Light3Ds "ghosting" and leaving a trail behind them. When temporal
  68. reprojection is enabled, try to avoid moving FogVolumes or Light3Ds too fast.
  69. Short-lived dynamic lighting effects should have **Volumetric Fog Energy** set
  70. to ``0.0`` to avoid ghosting.
  71. - **Temporal Reprojection > Amount:** The amount by which to blend the last
  72. frame with the current frame. A higher number results in smoother volumetric
  73. fog, but makes "ghosting" much worse. A lower value reduces ghosting but can
  74. result in the per-frame temporal jitter becoming visible.
  75. .. note::
  76. Unlike non-volumetric fog, volumetric fog has a *finite* range. This means
  77. volumetric fog cannot entirely cover a large world, as it will eventually
  78. stop being rendered in the distance.
  79. If you wish to hide distant areas from the player, it's recommended to
  80. enable both non-volumetric fog and volumetric fog at the same time, and
  81. adjust their density accordingly.
  82. Light interaction with volumetric fog
  83. -------------------------------------
  84. To simulate fog light scattering behavior in real life, all light types will
  85. interact with volumetric fog. How much each light will affect volumetric fog can
  86. be adjusted using the **Volumetric Fog Energy** property on each light. Enabling
  87. shadows on a light will also make those shadows visible on volumetric fog.
  88. If fog light interaction is not desired for artistic reasons, this can be
  89. globally disabled by setting **Volumetric Fog > Albedo** to a pure black color
  90. in the Environment resource. Fog light interaction can also be disabled for
  91. specific lights by setting its **Volumetric Fog Energy** to ``0``. Doing so will
  92. also improve performance slightly by excluding the light from volumetric fog
  93. computations.
  94. Using volumetric fog as a volumetric lighting solution
  95. ------------------------------------------------------
  96. While not physically accurate, it is possible to tune volumetric fog's settings
  97. to work as volumetric *lighting* solution. This means that unlit parts of the
  98. environment will not be darkened anymore by fog, but light will still be able to
  99. make fog brighter in specific areas.
  100. This can be done by setting volumetric fog density to the lowest permitted value
  101. *greater than zero* (``0.0001``), then increasing the **Volumetric Fog Energy**
  102. property on lights to much higher values than the default to compensate. Values
  103. between ``10000`` and ``100000`` usually work well for this.
  104. .. image:: img/volumetric_fog_lighting.png
  105. Balancing performance and quality
  106. ---------------------------------
  107. There are a few project settings available to adjust volumetric fog performance
  108. and quality:
  109. - **Rendering > Environment > Volumetric Fog > Volume Size:** Base size used to
  110. determine size of froxel buffer in the camera X-axis and Y-axis. The final
  111. size is scaled by the aspect ratio of the screen, so actual values may differ
  112. from what is set. Set a larger size for more detailed fog, set a smaller size
  113. for better performance.
  114. - **Rendering > Environment > Volumetric Fog > Volume Depth:** Number of slices
  115. to use along the depth of the froxel buffer for volumetric fog. A lower number
  116. will be more efficient, but may result in artifacts appearing during camera
  117. movement.
  118. - **Rendering > Environment > Volumetric Fog > Use Filter:** Enables filtering
  119. of the volumetric fog effect prior to integration. This substantially blurs
  120. the fog which reduces fine details, but also smooths out harsh edges and
  121. aliasing artifacts. Disable when more detail is required.
  122. .. note::
  123. Volumetric fog can cause banding to appear on the viewport, especially at
  124. higher density levels. See :ref:`doc_3d_rendering_limitations_color_banding`
  125. for guidance on reducing banding.
  126. Using fog volumes for local volumetric fog
  127. ------------------------------------------
  128. Sometimes, you want fog to be constrained to specific areas. Conversely, you may
  129. want to have global volumetric fog, but fog should be excluded from certain
  130. areas. Both approaches can be followed using FogVolume nodes.
  131. Here's a quick start guide to using FogVolumes:
  132. - Make sure **Volumetric Fog** is enabled in the Environment properties. If
  133. global volumetric fog is undesired, set its **Density** to ``0.0``.
  134. - Create a FogVolume node.
  135. - Assign a new FogMaterial to the FogVolume node's **Material** property.
  136. - In the FogMaterial, set **Density** to a positive value to increase density
  137. within the FogVolume, or a negative value to subtract the density from global
  138. volumetric fog.
  139. - Configure the FogVolume's extents and shape as needed.
  140. .. note::
  141. Thin fog volumes may appear to flicker when the camera moves or rotates.
  142. This can be alleviated by increasing the
  143. **Rendering > Environment > Volumetric Fog > Volume Depth** project setting
  144. (at a performance cost) or by decreasing **Length** in the Environment
  145. volumetric fog properties (at no performance cost, but at the cost of lower
  146. fog range). Alternatively, the FogVolume can be made thicker and use a lower
  147. density in the **Material**.
  148. FogVolume properties
  149. --------------------
  150. - **Extents:** The size of the FogVolume when **Shape** is **Ellipsoid**,
  151. **Cone**, **Cylinder** or **Box**. If **Shape** is **Cone** or **Cylinder**,
  152. the cone/cylinder will be adjusted to fit within the extents. Non-uniform
  153. scaling of cone/cylinder shapes via the **Extents** property is not supported,
  154. but you can scale the FogVolume node instead.
  155. - **Shape:** The shape of the FogVolume. This can be set to **Ellipsoid**,
  156. **Cone**, **Cylinder**, **Box** or **World** (acts as global volumetric fog).
  157. - **Material:** The material used by the FogVolume. Can be either a
  158. built-in FogMaterial or a custom ShaderMaterial (:ref:`doc_fog_shader`).
  159. After choosing **New FogMaterial** in the **Material** property, you can adjust
  160. the following properties in FogMaterial:
  161. - **Density:** The density of the FogVolume. Denser objects are more opaque, but
  162. may suffer from under-sampling artifacts that look like stripes. Negative
  163. values can be used to subtract fog from other FogVolumes or global volumetric
  164. fog.
  165. - **Albedo:** The single-scattering Color of the FogVolume. Internally, member
  166. albedo is converted into single-scattering, which is additively blended with
  167. other FogVolumes and global volumetric fog's **Albedo**.
  168. - **Emission:** The Color of the light emitted by the FogVolume. Emitted light
  169. will not cast light or shadows on other objects, but can be useful for
  170. modulating the Color of the FogVolume independently from light sources.
  171. - **Height Falloff:** The rate by which the height-based fog decreases in
  172. density as height increases in world space. A high falloff will result in a
  173. sharp transition, while a low falloff will result in a smoother transition.
  174. A value of ``0.0`` results in uniform-density fog. The height threshold is
  175. determined by the height of the associated FogVolume.
  176. - **Edge Fade:** The hardness of the edges of the FogVolume. A higher value will
  177. result in softer edges, while a lower value will result in harder edges.
  178. - **Density Texture:** The 3D texture that is used to scale the member density
  179. of the FogVolume. This can be used to vary fog density within the FogVolume
  180. with any kind of static pattern. For animated effects, consider using a custom
  181. :ref:`fog shader <doc_fog_shader>`.
  182. You can import any image as a 3D texture by
  183. :ref:`changing its import type in the Import dock <doc_importing_images_changing_import_type>`.
  184. Using 3D noise density textures
  185. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  186. Since Godot 4.1, there is a NoiseTexture3D resource that can be used to
  187. procedurally generate 3D noise. This is well-suited to FogMaterial density
  188. textures, which can result in more detailed fog effects:
  189. .. figure:: img/volumetric_fog_fog_material_density_texture.webp
  190. :alt: FogMaterial comparison (without and with density texture)
  191. Screenshot taken with **Volume Size** project setting set to 192 to make
  192. high-frequency detail more visible in the fog.
  193. To do so, select the **Density Texture** property and choose **New NoiseTexture3D**.
  194. Edit this NoiseTexture3D by clicking it, then click **Noise** at the bottom of the
  195. NoiseTexture3D properties and choose **New FastNoiseLite**. Adjust the noise texture's
  196. width, height and depth according to your fog volume's dimensions.
  197. To improve performance, it's recommended to use low texture sizes (64×64×64 or lower),
  198. as high-frequency detail is difficult to notice in a FogVolume. If you wish to represent
  199. more detailed density variations, you will need to increase
  200. **Rendering > Environment > Volumetric Fog > Volume Size** in the project settings,
  201. which has a performance cost.
  202. .. note::
  203. NoiseTexture3D's **Color Ramp** affects FogMaterial density textures, but
  204. since only the texture's red channel is sampled, only the color ramp's red
  205. channel will affect the resulting density.
  206. However, using a color ramp will *not* tint the fog volume according to the
  207. texture. You would need to use a custom shader that reads a Texture3D to
  208. achieve this.
  209. Custom FogVolume shaders
  210. ------------------------
  211. This page only covers the built-in settings offered by FogMaterial. If you need
  212. to customize fog behavior within a FogVolume node (such as creating animated fog),
  213. FogVolume nodes' appearance can be customized using :ref:`doc_fog_shader`.
  214. Faking volumetric fog using quads
  215. ---------------------------------
  216. In some cases, it may be better to use specially configured QuadMeshes as an
  217. alternative to volumetric fog:
  218. - Quads work with any rendering method, including Mobile and Compatibility.
  219. - Quads do not require temporal reprojection to look smooth, which makes
  220. them suited to fast-moving dynamic effects such as lasers. They can also
  221. represent small details which volumetric fog cannot do efficiently.
  222. - Quads generally have a lower performance cost than volumetric fog.
  223. This approach has a few downsides though:
  224. - The fog effect has less realistic falloff, especially if the camera enters the fog.
  225. - Transparency sorting issues may occur when sprites overlap.
  226. - Performance will not necessarily be better than volumetric fog if there are
  227. lots of sprites close to the camera.
  228. To create a QuadMesh-based fog sprite:
  229. 1. Create a MeshInstance3D node with a QuadMesh resource in the **Mesh**
  230. property. Set the size as desired.
  231. 2. Create a new StandardMaterial3D in the mesh's **Material** property.
  232. 3. In the StandardMaterial3D, set **Shading > Shading Mode** to **Unshaded**,
  233. **Billboard > Mode** to **Enabled**, enable **Proximity Fade** and set
  234. **Distance Fade** to **Pixel Alpha**.
  235. 4. Set the **Albedo > Texture** to the texture below (right-click and choose **Save as…**):
  236. .. image:: img/volumetric_fog_quad_mesh_texture.webp
  237. 5. *After* setting the albedo texture, go to the Import dock, select the texture
  238. and change its compression mode to **Lossless** to improve quality.
  239. The fog's color is set using the **Albedo > Color** property; its density is set
  240. using the color's alpha channel. For best results, you will have to adjust
  241. **Proximity Fade > Distance** and **Distance Fade > Max Distance** depending on
  242. the size of your QuadMesh.
  243. Optionally, billboarding may be left disabled if you place the quad in a way
  244. where all of its corners are in solid geometry. This can be useful for fogging
  245. large planes that the camera cannot enter, such as bottomless pits.