diff --git a/crates/bevy_solari/src/realtime/specular_gi.wgsl b/crates/bevy_solari/src/realtime/specular_gi.wgsl index 3fbea809c4ebe..455cee7f47e20 100644 --- a/crates/bevy_solari/src/realtime/specular_gi.wgsl +++ b/crates/bevy_solari/src/realtime/specular_gi.wgsl @@ -2,7 +2,9 @@ enable wgpu_ray_query; #define_import_path bevy_solari::specular_gi +#import bevy_core_pipeline::tonemapping::tonemapping_luminance as luminance #import bevy_pbr::pbr_functions::{calculate_tbn_mikktspace, calculate_diffuse_color, calculate_F0} +#import bevy_pbr::utils::rand_f #import bevy_render::maths::{orthonormalize, PI} #import bevy_render::view::View #import bevy_solari::brdf::{evaluate_brdf, evaluate_specular_brdf} @@ -156,6 +158,11 @@ fn trace_glossy_path(pixel_id: vec2, primary_surface: ResolvedGPixel, initi throughput /= p_bounce; } + // Russian roulette for early termination + let p = luminance(throughput); + if rand_f(rng) > p { break; } + throughput /= p; + // Path spread increase path_spread += path_spread_heuristic(ray.t, ray_hit.material.roughness); }