Modify fog functions to take fog as an argument

This commit is contained in:
Marco Buono 2023-04-24 23:24:25 -03:00
parent 23711c8d3e
commit 50b32da4c8
3 changed files with 15 additions and 10 deletions

View file

@ -6,6 +6,7 @@
// https://iquilezles.org/articles/fog/ (Atmospheric Fog and Scattering)
fn scattering_adjusted_fog_color(
fog: Fog,
scattering: vec3<f32>,
) -> vec4<f32> {
if (fog.directional_light_color.a > 0.0) {
@ -20,11 +21,12 @@ fn scattering_adjusted_fog_color(
}
fn linear_fog(
fog: Fog,
input_color: vec4<f32>,
distance: f32,
scattering: vec3<f32>,
) -> vec4<f32> {
var fog_color = scattering_adjusted_fog_color(scattering);
var fog_color = scattering_adjusted_fog_color(fog, scattering);
let start = fog.be.x;
let end = fog.be.y;
fog_color.a *= 1.0 - clamp((end - distance) / (end - start), 0.0, 1.0);
@ -32,33 +34,36 @@ fn linear_fog(
}
fn exponential_fog(
fog: Fog,
input_color: vec4<f32>,
distance: f32,
scattering: vec3<f32>,
) -> vec4<f32> {
var fog_color = scattering_adjusted_fog_color(scattering);
var fog_color = scattering_adjusted_fog_color(fog, scattering);
let density = fog.be.x;
fog_color.a *= 1.0 - 1.0 / exp(distance * density);
return vec4<f32>(mix(input_color.rgb, fog_color.rgb, fog_color.a), input_color.a);
}
fn exponential_squared_fog(
fog: Fog,
input_color: vec4<f32>,
distance: f32,
scattering: vec3<f32>,
) -> vec4<f32> {
var fog_color = scattering_adjusted_fog_color(scattering);
var fog_color = scattering_adjusted_fog_color(fog, scattering);
let distance_times_density = distance * fog.be.x;
fog_color.a *= 1.0 - 1.0 / exp(distance_times_density * distance_times_density);
return vec4<f32>(mix(input_color.rgb, fog_color.rgb, fog_color.a), input_color.a);
}
fn atmospheric_fog(
fog: Fog,
input_color: vec4<f32>,
distance: f32,
scattering: vec3<f32>,
) -> vec4<f32> {
var fog_color = scattering_adjusted_fog_color(scattering);
var fog_color = scattering_adjusted_fog_color(fog, scattering);
let extinction_factor = 1.0 - 1.0 / exp(distance * fog.be);
let inscattering_factor = 1.0 - 1.0 / exp(distance * fog.bi);
return vec4<f32>(

View file

@ -155,7 +155,7 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
// fog
if (fog.mode != FOG_MODE_OFF && (material.flags & STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT) != 0u) {
output_color = apply_fog(output_color, in.world_position.xyz, view.world_position.xyz);
output_color = apply_fog(fog, output_color, in.world_position.xyz, view.world_position.xyz);
}
#ifdef TONEMAP_IN_SHADER

View file

@ -385,7 +385,7 @@ fn pbr(
#endif // PREPASS_FRAGMENT
#ifndef PREPASS_FRAGMENT
fn apply_fog(input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_world_position: vec3<f32>) -> vec4<f32> {
fn apply_fog(fog: Fog, input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_world_position: vec3<f32>) -> vec4<f32> {
let view_to_world = fragment_world_position.xyz - view_world_position.xyz;
// `length()` is used here instead of just `view_to_world.z` since that produces more
@ -411,13 +411,13 @@ fn apply_fog(input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_wo
}
if fog.mode == FOG_MODE_LINEAR {
return linear_fog(input_color, distance, scattering);
return linear_fog(fog, input_color, distance, scattering);
} else if fog.mode == FOG_MODE_EXPONENTIAL {
return exponential_fog(input_color, distance, scattering);
return exponential_fog(fog, input_color, distance, scattering);
} else if fog.mode == FOG_MODE_EXPONENTIAL_SQUARED {
return exponential_squared_fog(input_color, distance, scattering);
return exponential_squared_fog(fog, input_color, distance, scattering);
} else if fog.mode == FOG_MODE_ATMOSPHERIC {
return atmospheric_fog(input_color, distance, scattering);
return atmospheric_fog(fog, input_color, distance, scattering);
} else {
return input_color;
}