Skip to content

Commit

Permalink
Automatically fade to Z far in environment depth fog
Browse files Browse the repository at this point in the history
This is useful for open world games to smoothly fade out distant
scenery in the fog. Exponential fog cannot do this on its own,
so quadratic fog is combined with exponential fog using a `max()`
function. The quadratic fog start distance is automatically set at 50%
of the camera's Z far distance.
  • Loading branch information
Calinou committed Nov 27, 2021
1 parent 52b7d5f commit bd202f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/classes/Camera3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
</member>
<member name="far" type="float" setter="set_far" getter="get_far" default="4000.0">
The distance to the far culling boundary for this camera relative to its local Z axis.
[b]Note:[/b] When [member Environment.fog_enabled] is [code]true[/code], this is used as a base for open world fog fading (with fog starting at 50% of the Z far distance).
</member>
<member name="fov" type="float" setter="set_fov" getter="get_fov" default="75.0">
The camera's field of view angle (in degrees). Only applicable in perspective mode. Since [member keep_aspect] locks one axis, [code]fov[/code] sets the other axis' field of view angle.
Expand Down
1 change: 1 addition & 0 deletions doc/classes/Environment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
</member>
<member name="fog_enabled" type="bool" setter="set_fog_enabled" getter="is_fog_enabled" default="false">
If [code]true[/code], fog effects are enabled.
[b]Note:[/b] Enabling fog automatically fades starting from 50% of the [member Camera3D.far] distance, regardless of [member fog_depth_density]. This is used for open world fog fading. To disable this, increase the camera's [member Camera3D.far] property.
</member>
<member name="fog_height" type="float" setter="set_fog_height" getter="get_fog_height" default="0.0">
The height at which the height fog effect begins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,16 @@ vec4 fog_process(vec3 vertex) {
}
}

float fog_amount = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));
// Quadratic fade off to the Z far distance (for open world fog fading).
// Not physically accurate, but prevents visible sudden cutoffs near the Z far clip plane.
// This starts at 50% of the Z far distance, which is a good compromise between visibility
// and smoothness.
float fog_amount_quad = pow(smoothstep(scene_data.z_far * 0.5, scene_data.z_far, length(vertex)), 2.0);

// Exponential fog (physically accurate).
float fog_amount_exp = 1.0 - exp(min(0.0, -length(vertex) * scene_data.fog_density));

float fog_amount = max(fog_amount_quad, fog_amount_exp);

if (abs(scene_data.fog_height_density) >= 0.0001) {
float y = (scene_data.camera_matrix * vec4(vertex, 1.0)).y;
Expand Down

0 comments on commit bd202f2

Please sign in to comment.