From 45a37eb7d7b2f45d13fa7c68319ff6a17b42bccf Mon Sep 17 00:00:00 2001 From: Marco Buono Date: Sun, 24 Sep 2023 16:26:26 -0300 Subject: [PATCH] Use `view_z` to scale specular transmission blur --- crates/bevy_pbr/src/render/pbr_functions.wgsl | 2 +- crates/bevy_pbr/src/render/pbr_lighting.wgsl | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr_functions.wgsl b/crates/bevy_pbr/src/render/pbr_functions.wgsl index 41d0cf4f43..69f125ace2 100644 --- a/crates/bevy_pbr/src/render/pbr_functions.wgsl +++ b/crates/bevy_pbr/src/render/pbr_functions.wgsl @@ -382,7 +382,7 @@ fn pbr( let emissive_light = emissive.rgb * output_color.a; if transmission > 0.0 { - transmitted_light += lighting::transmissive_light(in.world_position, in.frag_coord.xyz, in.N, in.V, ior, thickness, perceptual_roughness, transmissive_color, transmitted_environment_light_specular).rgb; + transmitted_light += lighting::transmissive_light(in.world_position, in.frag_coord.xyz, view_z, in.N, in.V, ior, thickness, perceptual_roughness, transmissive_color, transmitted_environment_light_specular).rgb; } if (in.material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_ATTENUATION_ENABLED_BIT) != 0u { diff --git a/crates/bevy_pbr/src/render/pbr_lighting.wgsl b/crates/bevy_pbr/src/render/pbr_lighting.wgsl index 9269d77b29..f70e843fea 100644 --- a/crates/bevy_pbr/src/render/pbr_lighting.wgsl +++ b/crates/bevy_pbr/src/render/pbr_lighting.wgsl @@ -286,10 +286,7 @@ fn directional_light(light_id: u32, roughness: f32, NdotV: f32, normal: vec3, frag_coord: vec3, N: vec3, V: vec3, ior: f32, thickness: f32, perceptual_roughness: f32, transmissive_color: vec3, transmitted_environment_light_specular: vec3) -> vec3 { - // Calculate distance from the camera, used to scale roughness transmission blur - let distance = length(view_bindings::view.world_position - world_position.xyz); - +fn transmissive_light(world_position: vec4, frag_coord: vec3, view_z: f32, N: vec3, V: vec3, ior: f32, thickness: f32, perceptual_roughness: f32, transmissive_color: vec3, transmitted_environment_light_specular: vec3) -> vec3 { // Calculate the ratio between refaction indexes. Assume air/vacuum for the space outside the mesh let eta = 1.0 / ior; @@ -311,7 +308,7 @@ fn transmissive_light(world_position: vec4, frag_coord: vec3, N: vec3< let offset_position = (clip_exit_position.xy / clip_exit_position.w) * vec2(0.5, -0.5) + 0.5; // Fetch background color - let background_color = fetch_transmissive_background(offset_position, frag_coord, perceptual_roughness, distance); + let background_color = fetch_transmissive_background(offset_position, frag_coord, view_z, perceptual_roughness); // Calculate final color by applying transmissive color to a mix of background color and transmitted specular environment light // TODO: Add support for attenuationColor and attenuationDistance @@ -344,7 +341,7 @@ const SPIRAL_OFFSETS: array, 8> = array, 8>( const MAX_TRANSMISSIVE_TAPS = 16; -fn fetch_transmissive_background(offset_position: vec2, frag_coord: vec3, perceptual_roughness: f32, distance: f32) -> vec4 { +fn fetch_transmissive_background(offset_position: vec2, frag_coord: vec3, view_z: f32, perceptual_roughness: f32) -> vec4 { // Calculate view aspect ratio, used to scale offset so that it's proportionate let aspect = view_bindings::view.viewport.z / view_bindings::view.viewport.w; @@ -357,8 +354,8 @@ fn fetch_transmissive_background(offset_position: vec2, frag_coord: vec3