Skip to content

Commit e606dd6

Browse files
author
Dahmen issam
committed
Use dedicated volume attenuation function
1 parent 91894fe commit e606dd6

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

crates/bevy_pbr/src/render/pbr_functions.wgsl

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,20 @@ fn calculate_F0(base_color: vec3<f32>, metallic: f32, reflectance: vec3<f32>) ->
302302
return mix(calculate_F0_dielectric(reflectance), base_color, metallic);
303303
}
304304

305+
#ifdef STANDARD_MATERIAL_SPECULAR_TRANSMISSION
306+
fn volume_attenuation(
307+
color: vec3<f32>,
308+
attenuation_color: vec3<f32>,
309+
attenuation_distance: f32,
310+
transmission_distance: f32,
311+
) -> vec3<f32> {
312+
let safe_attenuation_color = clamp(attenuation_color, vec3<f32>(1e-5), vec3<f32>(1.0));
313+
let safe_attenuation_distance = max(attenuation_distance, 1e-5);
314+
let absorption = -log(safe_attenuation_color) / safe_attenuation_distance;
315+
return color * exp(-absorption * transmission_distance);
316+
}
317+
#endif // STANDARD_MATERIAL_SPECULAR_TRANSMISSION
318+
305319
#ifdef CONTACT_SHADOWS
306320
#ifdef DEPTH_PREPASS
307321
fn calculate_contact_shadow(
@@ -852,19 +866,12 @@ fn apply_pbr_lighting(
852866
transmitted_light += transmission::specular_transmissive_light(in.world_position, in.frag_coord.xyz, view_z, in.N, in.V, F0, ior, thickness, perceptual_roughness, specular_transmissive_color, specular_transmitted_environment_light).rgb;
853867

854868
if (in.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ATTENUATION_ENABLED_BIT) != 0u {
855-
// We reuse the `atmospheric_fog()` function here, as it's fundamentally
856-
// equivalent to the attenuation that takes place inside the material volume,
857-
// and will allow us to eventually hook up subsurface scattering more easily
858-
var attenuation_fog: mesh_view_types::Fog;
859-
attenuation_fog.base_color.a = 1.0;
860-
let attenuation_color = max(in.material.attenuation_color.rgb, vec3<f32>(1e-6));
861-
attenuation_fog.be = -log(attenuation_color) / in.material.attenuation_distance;
862-
// TODO: Add the subsurface scattering factor below
863-
// attenuation_fog.bi = /* ... */
864-
transmitted_light = bevy_pbr::fog::atmospheric_fog(
865-
attenuation_fog, vec4<f32>(transmitted_light, 1.0), thickness,
866-
vec3<f32>(0.0) // TODO: Pass in (pre-attenuated) scattered light contribution here
867-
).rgb;
869+
transmitted_light = volume_attenuation(
870+
transmitted_light,
871+
in.material.attenuation_color.rgb,
872+
in.material.attenuation_distance,
873+
thickness,
874+
);
868875
}
869876
#endif
870877

0 commit comments

Comments
 (0)