godot icon indicating copy to clipboard operation
godot copied to clipboard

Add optional depth fog to Environment

Open HybridEidolon opened this issue 1 year ago • 3 comments

This is a feature addition that accomplishes godotengine/godot-proposals#4619, addresses the needs of godotengine/godot-proposals#3429, and supercedes the implementation in #55388 by partially restoring some of the depth fog parameters in the Godot 3.x Environment.

The following properties are added to the Environment resource:

  • fog_depth_enabled: used in a specialization constant in the relevant scene shaders. This addresses the performance caveat discussed in #55388 by ensuring that when only exponential fog is used, the branch is never present in the compiled program. It is not possible to use quadratic fog exclusively at the shader level in this implementation, but you can set the parameters of the environment such that the exponential fog density is 0 while still having quadratic fog if you must have no exponential fog at all. During project conversion, this property should be set to true if fog was enabled, the new fog density should be set to 0, and the sky affect scale should also be set to 0.
  • fog_depth_curve: Same as in Godot 3.x
  • fog_depth_density: replaces the alpha value of fog_depth_color in Godot 3.x. Project conversion should take the fog color's alpha and put it in this property instead.
  • fog_depth_begin: Same as in Godot 3.x
  • fog_depth_end: Same as in Godot 3.x

The depth fog interacts with the new fog model gracefully, supporting sun scattering and the improved light transmission. It does not affect the behavior of exponential fog with depth fog disabled at all.

Implementations are provided for gles3, vulkan mobile and vulkan clustered. Although, in the commit this branch is based on, vulkan mobile's support for fog is broken (there is already an MR to fix it) and gles3 seems to be very broken regardless.

image

Todo:

  • [x] Documentation entries for new Environment properties
  • [ ] Changes to the project convertor to migrate the old fog properties

HybridEidolon avatar Sep 18 '22 05:09 HybridEidolon

Do note that this implementation fails to hide Z-far effectively.

Here is a Plane (default camera settings, fog settings in pic) See banding visible on the Left and Right of the Fog Circle in this Image which is actually not fully faded Plane: image

The effect is clearer when inverting the Fog like here: image

Do note that this is also the case in the above mentioned PR, and the issue seems to be due to aerial perspective.

mrjustaguy avatar Sep 18 '22 19:09 mrjustaguy

Do note that this is also the case in the above mentioned PR, and the issue seems to be due to aerial perspective.

Aerial perspective uses a blurred version of the background sky, not an exact copy of the background sky. It's meant to mimic fog light scattering.

Calinou avatar Sep 18 '22 21:09 Calinou

After talking with a friend about the lost fog features in the 4.0 beta, there is one more 3.x fog property that isn't present: light transmittance (fog_transmit_enabled, fog_transmit_curve). Volumetric fog appears to support this, but not distance fog anymore.

From a cursory glance on how fog light transmittance is implemented in 3.x, it relies on the light information, but fog is evaluated before lighting in 4.0 rather than after it in 3.x; a comment notes that this is to maximize VGPR occupancy. I could perform the transmittance after lighting in a specialization constant branch to restore the property, but I am wary to add another specialization permutation to the standard scene shader too. Curious what y'all's thoughts are.

HybridEidolon avatar Sep 19 '22 17:09 HybridEidolon

We discussed this in today's PR review meeting. The main idea has been approved for implementation but the consensus was to expose the feature a little bit differently. Instead of exposing both exponential fog and depth fog side by side they should be exposed as options toggleable in the shader with a specialization constant (so only one version is compiled).

In other words, we would add a FogType enum that can be used to select between exponential and depth-based fog. In the Environment you would only see the settings related to the type of fog you have selected. Then in the shader the specialization constant would be used to toggle between exponential fog and the depth fog so only one is enabled at a time and the user doesn't pay the cost for having both.

After talking with a friend about the lost fog features in the 4.0 beta, there is one more 3.x fog property that isn't present: light transmittance (fog_transmit_enabled, fog_transmit_curve). Volumetric fog appears to support this, but not distance fog anymore.

From a cursory glance on how fog light transmittance is implemented in 3.x, it relies on the light information, but fog is evaluated before lighting in 4.0 rather than after it in 3.x; a comment notes that this is to maximize VGPR occupancy. I could perform the transmittance after lighting in a specialization constant branch to restore the property, but I am wary to add another specialization permutation to the standard scene shader too. Curious what y'all's thoughts are.

fog_transmit was renamed tosun_scatter in 4.0. It works essentially the same way as it used to and is used to produce the same effect. So there is not need to add fog_transmit back in.

clayjohn avatar Oct 20 '22 16:10 clayjohn

We discussed this in today's PR review meeting. The main idea has been approved for implementation but the consensus was to expose the feature a little bit differently. Instead of exposing both exponential fog and depth fog side by side they should be exposed as options toggleable in the shader with a specialization constant (so only one version is compiled).

In other words, we would add a FogType enum that can be used to select between exponential and depth-based fog. In the Environment you would only see the settings related to the type of fog you have selected. Then in the shader the specialization constant would be used to toggle between exponential fog and the depth fog so only one is enabled at a time and the user doesn't pay the cost for having both.

After talking with a friend about the lost fog features in the 4.0 beta, there is one more 3.x fog property that isn't present: light transmittance (fog_transmit_enabled, fog_transmit_curve). Volumetric fog appears to support this, but not distance fog anymore. From a cursory glance on how fog light transmittance is implemented in 3.x, it relies on the light information, but fog is evaluated before lighting in 4.0 rather than after it in 3.x; a comment notes that this is to maximize VGPR occupancy. I could perform the transmittance after lighting in a specialization constant branch to restore the property, but I am wary to add another specialization permutation to the standard scene shader too. Curious what y'all's thoughts are.

fog_transmit was renamed tosun_scatter in 4.0. It works essentially the same way as it used to and is used to produce the same effect. So there is not need to add fog_transmit back in.

Sounds good, just make sure to try to implement sky affect from exponential fog to depth fog.

ettiSurreal avatar Oct 21 '22 16:10 ettiSurreal