3#define_import_path bevy_pbr::ssr 5#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput 9 lighting::{LAYER_BASE, LAYER_CLEARCOAT}, 10 mesh_view_bindings::{view, depth_prepass_texture, deferred_prepass_texture, ssr_settings}, 11 pbr_deferred_functions::pbr_input_from_deferred_gbuffer, 16 depth_ray_march_from_cs, 17 depth_ray_march_march, 18 depth_ray_march_new_from_depth, 19 depth_ray_march_to_ws_dir, 22 view_transformations::{ 28 position_world_to_ndc, 29 position_world_to_view, 32#import bevy_render::view::View 35#import bevy_pbr::environment_map 39@group(2) @binding(0) var color_texture: texture_2d<f32>; 42@group(2) @binding(1) var color_sampler: sampler; 60fn evaluate_ssr(R_world: vec3<f32>, P_world: vec3<f32>) -> vec4<f32> { 61 let depth_size = vec2<f32>(textureDimensions(depth_prepass_texture)); 63 var raymarch = depth_ray_march_new_from_depth(depth_size); 64 depth_ray_march_from_cs(&raymarch, position_world_to_ndc(P_world)); 65 depth_ray_march_to_ws_dir(&raymarch, normalize(R_world)); 66 raymarch.linear_steps = ssr_settings.linear_steps; 67 raymarch.bisection_steps = ssr_settings.bisection_steps; 68 raymarch.use_secant = ssr_settings.use_secant != 0u; 69 raymarch.depth_thickness_linear_z = ssr_settings.thickness; 70 raymarch.jitter = 1.0; 71 raymarch.march_behind_surfaces = false; 73 let raymarch_result = depth_ray_march_march(&raymarch); 74 if (raymarch_result.hit) { 76 textureSampleLevel(color_texture, color_sampler, raymarch_result.hit_uv, 0.0).rgb, 81 return vec4(0.0, 0.0, 0.0, 1.0); 85fn fragment(in: FullscreenVertexOutput) -> @location(0) vec4<f32> { 87 var frag_coord = in.position; 88 frag_coord.z = prepass_utils::prepass_depth(in.position, 0u); 91 let fragment = textureLoad(color_texture, vec2<i32>(frag_coord.xy), 0); 92 let gbuffer = textureLoad(deferred_prepass_texture, vec2<i32>(frag_coord.xy), 0); 93 let pbr_input = pbr_input_from_deferred_gbuffer(frag_coord, gbuffer); 97 let perceptual_roughness = pbr_input.material.perceptual_roughness; 98 if (perceptual_roughness > ssr_settings.perceptual_roughness_threshold) { 103 var specular_occlusion = pbr_input.specular_occlusion; 104 let world_position = pbr_input.world_position.xyz; 109 let R = reflect(-V, N); 112 let ssr_specular = evaluate_ssr(R, world_position); 113 var indirect_light = ssr_specular.rgb; 114 specular_occlusion *= ssr_specular.a; 122#ifdef ENVIRONMENT_MAP 124 let base_color = pbr_input.material.base_color.rgb; 125 let metallic = pbr_input.material.metallic; 126 let reflectance = pbr_input.material.reflectance; 127 let specular_transmission = pbr_input.material.specular_transmission; 128 let diffuse_transmission = pbr_input.material.diffuse_transmission; 129 let diffuse_occlusion = pbr_input.diffuse_occlusion; 131#ifdef STANDARD_MATERIAL_CLEARCOAT 134 let clearcoat = pbr_input.material.clearcoat; 135 let clearcoat_perceptual_roughness = pbr_input.material.clearcoat_perceptual_roughness; 136 let clearcoat_roughness = lighting::perceptualRoughnessToRoughness(clearcoat_perceptual_roughness); 137 let clearcoat_N = pbr_input.clearcoat_N; 138 let clearcoat_NdotV = max(dot(clearcoat_N, pbr_input.V), 0.0001); 139 let clearcoat_R = reflect(-pbr_input.V, clearcoat_N); 140#endif // STANDARD_MATERIAL_CLEARCOAT 143 let roughness = lighting::perceptualRoughnessToRoughness(perceptual_roughness); 144 let diffuse_color = pbr_functions::calculate_diffuse_color( 147 specular_transmission, 150 let NdotV = max(dot(N, V), 0.0001); 151 let F_ab = lighting::F_AB(perceptual_roughness, NdotV); 152 let F0 = pbr_functions::calculate_F0(base_color, metallic, reflectance); 155 var lighting_input: lighting::LightingInput; 156 lighting_input.layers[LAYER_BASE].NdotV = NdotV; 157 lighting_input.layers[LAYER_BASE].N = N; 158 lighting_input.layers[LAYER_BASE].R = R; 159 lighting_input.layers[LAYER_BASE].perceptual_roughness = perceptual_roughness; 160 lighting_input.layers[LAYER_BASE].roughness = roughness; 161 lighting_input.P = world_position.xyz; 162 lighting_input.V = V; 163 lighting_input.diffuse_color = diffuse_color; 164 lighting_input.F0_ = F0; 165 lighting_input.F_ab = F_ab; 166#ifdef STANDARD_MATERIAL_CLEARCOAT 167 lighting_input.layers[LAYER_CLEARCOAT].NdotV = clearcoat_NdotV; 168 lighting_input.layers[LAYER_CLEARCOAT].N = clearcoat_N; 169 lighting_input.layers[LAYER_CLEARCOAT].R = clearcoat_R; 170 lighting_input.layers[LAYER_CLEARCOAT].perceptual_roughness = clearcoat_perceptual_roughness; 171 lighting_input.layers[LAYER_CLEARCOAT].roughness = clearcoat_roughness; 172 lighting_input.clearcoat_strength = clearcoat; 173#endif // STANDARD_MATERIAL_CLEARCOAT 177 let cluster_index = clustered_forward::fragment_cluster_index( 178 frag_coord.xy, frag_coord.z, false); 179 var clusterable_object_index_ranges = 180 clustered_forward::unpack_clusterable_object_index_ranges(cluster_index); 183 let environment_light = environment_map::environment_map_light( 184 &lighting_input, &clusterable_object_index_ranges, false); 187 indirect_light += view.exposure * 188 (environment_light.diffuse * diffuse_occlusion + 189 environment_light.specular * specular_occlusion); 193 return vec4(fragment.rgb + indirect_light, 1.0);