mirror of
https://github.com/bevyengine/bevy
synced 2025-03-02 14:27:21 +00:00
Take fog
as an argument instead of relying on fog
uniform
This commit is contained in:
parent
77b94e4fe9
commit
a39364f926
3 changed files with 15 additions and 10 deletions
|
@ -6,6 +6,7 @@
|
||||||
// https://iquilezles.org/articles/fog/ (Atmospheric Fog and Scattering)
|
// https://iquilezles.org/articles/fog/ (Atmospheric Fog and Scattering)
|
||||||
|
|
||||||
fn scattering_adjusted_fog_color(
|
fn scattering_adjusted_fog_color(
|
||||||
|
fog: Fog,
|
||||||
scattering: vec3<f32>,
|
scattering: vec3<f32>,
|
||||||
) -> vec4<f32> {
|
) -> vec4<f32> {
|
||||||
if (fog.directional_light_color.a > 0.0) {
|
if (fog.directional_light_color.a > 0.0) {
|
||||||
|
@ -20,11 +21,12 @@ fn scattering_adjusted_fog_color(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn linear_fog(
|
fn linear_fog(
|
||||||
|
fog: Fog,
|
||||||
input_color: vec4<f32>,
|
input_color: vec4<f32>,
|
||||||
distance: f32,
|
distance: f32,
|
||||||
scattering: vec3<f32>,
|
scattering: vec3<f32>,
|
||||||
) -> vec4<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 start = fog.be.x;
|
||||||
let end = fog.be.y;
|
let end = fog.be.y;
|
||||||
fog_color.a *= 1.0 - clamp((end - distance) / (end - start), 0.0, 1.0);
|
fog_color.a *= 1.0 - clamp((end - distance) / (end - start), 0.0, 1.0);
|
||||||
|
@ -32,33 +34,36 @@ fn linear_fog(
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exponential_fog(
|
fn exponential_fog(
|
||||||
|
fog: Fog,
|
||||||
input_color: vec4<f32>,
|
input_color: vec4<f32>,
|
||||||
distance: f32,
|
distance: f32,
|
||||||
scattering: vec3<f32>,
|
scattering: vec3<f32>,
|
||||||
) -> vec4<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;
|
let density = fog.be.x;
|
||||||
fog_color.a *= 1.0 - 1.0 / exp(distance * density);
|
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);
|
return vec4<f32>(mix(input_color.rgb, fog_color.rgb, fog_color.a), input_color.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn exponential_squared_fog(
|
fn exponential_squared_fog(
|
||||||
|
fog: Fog,
|
||||||
input_color: vec4<f32>,
|
input_color: vec4<f32>,
|
||||||
distance: f32,
|
distance: f32,
|
||||||
scattering: vec3<f32>,
|
scattering: vec3<f32>,
|
||||||
) -> vec4<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;
|
let distance_times_density = distance * fog.be.x;
|
||||||
fog_color.a *= 1.0 - 1.0 / exp(distance_times_density * distance_times_density);
|
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);
|
return vec4<f32>(mix(input_color.rgb, fog_color.rgb, fog_color.a), input_color.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn atmospheric_fog(
|
fn atmospheric_fog(
|
||||||
|
fog: Fog,
|
||||||
input_color: vec4<f32>,
|
input_color: vec4<f32>,
|
||||||
distance: f32,
|
distance: f32,
|
||||||
scattering: vec3<f32>,
|
scattering: vec3<f32>,
|
||||||
) -> vec4<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 extinction_factor = 1.0 - 1.0 / exp(distance * fog.be);
|
||||||
let inscattering_factor = 1.0 - 1.0 / exp(distance * fog.bi);
|
let inscattering_factor = 1.0 - 1.0 / exp(distance * fog.bi);
|
||||||
return vec4<f32>(
|
return vec4<f32>(
|
||||||
|
|
|
@ -133,7 +133,7 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
|
||||||
|
|
||||||
// fog
|
// fog
|
||||||
if (fog.mode != FOG_MODE_OFF && (material.flags & STANDARD_MATERIAL_FLAGS_FOG_ENABLED_BIT) != 0u) {
|
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
|
#ifdef TONEMAP_IN_SHADER
|
||||||
|
|
|
@ -271,7 +271,7 @@ fn pbr(
|
||||||
#endif // PREPASS_FRAGMENT
|
#endif // PREPASS_FRAGMENT
|
||||||
|
|
||||||
#ifndef 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;
|
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
|
// `length()` is used here instead of just `view_to_world.z` since that produces more
|
||||||
|
@ -297,13 +297,13 @@ fn apply_fog(input_color: vec4<f32>, fragment_world_position: vec3<f32>, view_wo
|
||||||
}
|
}
|
||||||
|
|
||||||
if fog.mode == FOG_MODE_LINEAR {
|
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 {
|
} 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 {
|
} 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 {
|
} else if fog.mode == FOG_MODE_ATMOSPHERIC {
|
||||||
return atmospheric_fog(input_color, distance, scattering);
|
return atmospheric_fog(fog, input_color, distance, scattering);
|
||||||
} else {
|
} else {
|
||||||
return input_color;
|
return input_color;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue