Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically fade to Z far in environment depth fog #55388

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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