Skip to content

Commit

Permalink
bevy_pbr: Make choosing of diffuse indirect lighting explicit. (#15093)
Browse files Browse the repository at this point in the history
# Objective

Make choosing of diffuse indirect lighting explicit, instead of using
numerical conditions like `all(indirect_light == vec3(0.0f))`, as using
that may lead to unwanted light leakage.

## Solution

Use an explicit `found_diffuse_indirect` condition to indicate the found
indirect lighting source.

## Testing

I have tested examples `lightmaps`, `irradiance_volumes` and
`reflection_probes`, there are no visual changes. For further testing,
consider a "cave" scene with lightmaps and irradiance volumes. In the
cave there are some purly dark occluded area, those dark area will
sample the irradiance volume, and that is easy to leak light.
  • Loading branch information
cryscan authored Sep 9, 2024
1 parent 29c632b commit 9b006fd
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,20 @@ fn apply_pbr_lighting(
// example, both lightmaps and irradiance volumes are present.

var indirect_light = vec3(0.0f);
var found_diffuse_indirect = false;

#ifdef LIGHTMAP
if (all(indirect_light == vec3(0.0f))) {
indirect_light += in.lightmap_light * diffuse_color;
}
indirect_light += in.lightmap_light * diffuse_color;
found_diffuse_indirect = true;
#endif

#ifdef IRRADIANCE_VOLUME {
#ifdef IRRADIANCE_VOLUME
// Irradiance volume light (indirect)
if (all(indirect_light == vec3(0.0f))) {
if (!found_diffuse_indirect) {
let irradiance_volume_light = irradiance_volume::irradiance_volume_light(
in.world_position.xyz, in.N);
indirect_light += irradiance_volume_light * diffuse_color * diffuse_occlusion;
found_diffuse_indirect = true;
}
#endif

Expand All @@ -574,7 +575,7 @@ fn apply_pbr_lighting(

let environment_light = environment_map::environment_map_light(
environment_map_lighting_input,
any(indirect_light != vec3(0.0f))
found_diffuse_indirect
);

// If screen space reflections are going to be used for this material, don't
Expand All @@ -589,7 +590,7 @@ fn apply_pbr_lighting(
if (!use_ssr) {
let environment_light = environment_map::environment_map_light(
&lighting_input,
any(indirect_light != vec3(0.0f))
found_diffuse_indirect
);

indirect_light += environment_light.diffuse * diffuse_occlusion +
Expand Down

0 comments on commit 9b006fd

Please sign in to comment.