From 6ce7a8e3f9bf4296f5a87ad825e2b002409f9cd7 Mon Sep 17 00:00:00 2001 From: Robert Swain Date: Wed, 1 Feb 2023 20:09:23 +0100 Subject: [PATCH] bevy_pbr: Add cascade debug visualization --- crates/bevy_pbr/src/render/pbr_functions.wgsl | 5 ++++- crates/bevy_pbr/src/render/shadows.wgsl | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr_functions.wgsl b/crates/bevy_pbr/src/render/pbr_functions.wgsl index 57b516202..c33cea15b 100644 --- a/crates/bevy_pbr/src/render/pbr_functions.wgsl +++ b/crates/bevy_pbr/src/render/pbr_functions.wgsl @@ -226,7 +226,10 @@ fn pbr( && (lights.directional_lights[i].flags & DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { shadow = fetch_directional_shadow(i, in.world_position, in.world_normal, view_z); } - let light_contrib = directional_light(i, roughness, NdotV, in.N, in.V, R, F0, diffuse_color); + var light_contrib = directional_light(i, roughness, NdotV, in.N, in.V, R, F0, diffuse_color); +#ifdef DIRECTIONAL_LIGHT_SHADOW_MAP_DEBUG_CASCADES + light_contrib = cascade_debug_visualization(light_contrib, i, view_z); +#endif light_accum = light_accum + light_contrib * shadow; } diff --git a/crates/bevy_pbr/src/render/shadows.wgsl b/crates/bevy_pbr/src/render/shadows.wgsl index a3f1f79a9..f74733c90 100644 --- a/crates/bevy_pbr/src/render/shadows.wgsl +++ b/crates/bevy_pbr/src/render/shadows.wgsl @@ -144,7 +144,7 @@ fn sample_cascade(light_id: u32, cascade_index: u32, frag_position: vec4, s directional_shadow_textures_sampler, light_local, depth - ); + ); #else return textureSampleCompareLevel( directional_shadow_textures, @@ -152,14 +152,14 @@ fn sample_cascade(light_id: u32, cascade_index: u32, frag_position: vec4, s light_local, i32((*light).depth_texture_base_index + cascade_index), depth - ); + ); #endif } fn fetch_directional_shadow(light_id: u32, frag_position: vec4, surface_normal: vec3, view_z: f32) -> f32 { let light = &lights.directional_lights[light_id]; let cascade_index = get_cascade_index(light_id, view_z); - + if (cascade_index >= (*light).num_cascades) { return 1.0; } @@ -178,3 +178,16 @@ fn fetch_directional_shadow(light_id: u32, frag_position: vec4, surface_nor } return shadow; } + +fn cascade_debug_visualization( + output_color: vec3, + light_id: u32, + view_z: f32, +) -> vec3 { + let overlay_alpha = 0.95; + let cascade_index = get_cascade_index(light_id, view_z); + let cascade_color = hsv2rgb(f32(cascade_index) / f32(#{MAX_CASCADES_PER_LIGHT}u + 1u), 1.0, 0.5); + return vec3( + (1.0 - overlay_alpha) * output_color.rgb + overlay_alpha * cascade_color + ); +} -- 2.39.1