16#import bevy_core_pipeline::fullscreen_vertex_shader::FullscreenVertexOutput 17#import bevy_pbr::mesh_functions::{get_world_from_local, mesh_position_local_to_clip} 18#import bevy_pbr::mesh_view_bindings::{globals, lights, view, clusterable_objects} 19#import bevy_pbr::mesh_view_types::{ 20 DIRECTIONAL_LIGHT_FLAGS_VOLUMETRIC_BIT, 21 POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT, 22 POINT_LIGHT_FLAGS_VOLUMETRIC_BIT, 23 POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE, 26#import bevy_pbr::shadow_sampling::{ 27 sample_shadow_map_hardware, 28 sample_shadow_cubemap, 30 SPOT_SHADOW_TEXEL_SIZE 32#import bevy_pbr::shadows::{get_cascade_index, world_to_directional_light_local} 33#import bevy_pbr::utils::interleaved_gradient_noise 34#import bevy_pbr::view_transformations::{ 38 position_ndc_to_world, 39 position_view_to_world 41#import bevy_pbr::clustered_forward as clustering 42#import bevy_pbr::lighting::getDistanceAttenuation; 47 clip_from_local: mat4x4<f32>, 48 uvw_from_world: mat4x4<f32>, 49 far_planes: array<vec4<f32>, 3>, 51 light_tint: vec3<f32>, 52 ambient_color: vec3<f32>, 53 ambient_intensity: f32, 59 density_texture_offset: vec3<f32>, 60 scattering_asymmetry: f32, 65@group(1) @binding(0) var<uniform> volumetric_fog: VolumetricFog; 68@group(1) @binding(1) var depth_texture: texture_depth_multisampled_2d; 70@group(1) @binding(1) var depth_texture: texture_depth_2d; 74@group(1) @binding(2) var density_texture: texture_3d<f32>; 75@group(1) @binding(3) var density_sampler: sampler; 76#endif // DENSITY_TEXTURE 79const FRAC_4_PI: f32 = 0.07957747154594767; 82 @builtin(instance_index) instance_index: u32, 83 @location(0) position: vec3<f32>, 87fn vertex(vertex: Vertex) -> @builtin(position) vec4<f32> { 88 return volumetric_fog.clip_from_local * vec4<f32>(vertex.position, 1.0); 100fn henyey_greenstein(neg_LdotV: f32) -> f32 { 101 let g = volumetric_fog.scattering_asymmetry; 102 let denom = 1.0 + g * g - 2.0 * g * neg_LdotV; 103 return FRAC_4_PI * (1.0 - g * g) / (denom * sqrt(denom)); 107fn fragment(@builtin(position) position: vec4<f32>) -> @location(0) vec4<f32> { 109 let uvw_from_world = volumetric_fog.uvw_from_world; 110 let fog_color = volumetric_fog.fog_color; 111 let ambient_color = volumetric_fog.ambient_color; 112 let ambient_intensity = volumetric_fog.ambient_intensity; 113 let step_count = volumetric_fog.step_count; 114 let bounding_radius = volumetric_fog.bounding_radius; 115 let absorption = volumetric_fog.absorption; 116 let scattering = volumetric_fog.scattering; 117 let density_factor = volumetric_fog.density_factor; 118 let density_texture_offset = volumetric_fog.density_texture_offset; 119 let light_tint = volumetric_fog.light_tint; 120 let light_intensity = volumetric_fog.light_intensity; 121 let jitter_strength = volumetric_fog.jitter_strength; 124 let exposure = view.exposure; 129 let frag_coord = position; 130 let ndc_end_depth_from_buffer = textureLoad(depth_texture, vec2<i32>(frag_coord.xy), 0); 131 let view_end_depth_from_buffer = -position_ndc_to_view( 132 frag_coord_to_ndc(vec4(position.xy, ndc_end_depth_from_buffer, 1.0))).z; 136 let view_start_pos = position_ndc_to_view(frag_coord_to_ndc(frag_coord)); 140 var end_depth_view = 0.0; 141 for (var plane_index = 0; plane_index < 3; plane_index += 1) { 142 let plane = volumetric_fog.far_planes[plane_index]; 143 let other_plane_a = volumetric_fog.far_planes[(plane_index + 1) % 3]; 144 let other_plane_b = volumetric_fog.far_planes[(plane_index + 2) % 3]; 148 let t = -plane.w / dot(plane.xyz, view_start_pos.xyz); 152 let hit_pos = view_start_pos.xyz * t; 155 let other_sides = vec2( 156 dot(vec4(hit_pos, 1.0), other_plane_a) >= 0.0, 157 dot(vec4(hit_pos, 1.0), other_plane_b) >= 0.0 161 if (all(other_sides)) { 162 end_depth_view = -hit_pos.z; 169 end_depth_view = min(end_depth_view, view_end_depth_from_buffer); 172 let start_depth_view = -depth_ndc_to_view_z(frag_coord.z); 173 let ray_length_view = abs(end_depth_view - start_depth_view); 174 let inv_step_count = 1.0 / f32(step_count); 175 let step_size_world = ray_length_view * inv_step_count; 177 let directional_light_count = lights.n_directional_lights; 181 let Rd_ndc = vec3(frag_coord_to_ndc(position).xy, 1.0); 182 let Rd_view = normalize(position_ndc_to_view(Rd_ndc)); 183 var Ro_world = position_view_to_world(view_start_pos.xyz); 184 let Rd_world = normalize(position_ndc_to_world(Rd_ndc) - view.world_position); 187 let jitter = interleaved_gradient_noise(position.xy, globals.frame_count) * jitter_strength; 188 Ro_world += Rd_world * jitter; 200 var accumulated_color = exp(-ray_length_view * (absorption + scattering)) * ambient_color * 206 var background_alpha = 1.0; 209#ifdef DENSITY_TEXTURE 210 let Ro_uvw = (uvw_from_world * vec4(Ro_world, 1.0)).xyz; 211 let Rd_step_uvw = mat3x3(uvw_from_world[0].xyz, uvw_from_world[1].xyz, uvw_from_world[2].xyz) * 212 (Rd_world * step_size_world); 213#endif // DENSITY_TEXTURE 215 for (var light_index = 0u; light_index < directional_light_count; light_index += 1u) { 218 let light = &lights.directional_lights[light_index]; 219 if (((*light).flags & DIRECTIONAL_LIGHT_FLAGS_VOLUMETRIC_BIT) == 0) { 224 let depth_offset = (*light).shadow_depth_bias * (*light).direction_to_light.xyz; 228 let neg_LdotV = dot(normalize((*light).direction_to_light.xyz), Rd_world); 229 let phase = henyey_greenstein(neg_LdotV); 232 background_alpha = 1.0; 235 for (var step = 0u; step < step_count; step += 1u) { 237 if (background_alpha < 0.001) { 242 let P_world = Ro_world + Rd_world * f32(step) * step_size_world; 243 let P_view = Rd_view * f32(step) * step_size_world; 245 var density = density_factor; 246#ifdef DENSITY_TEXTURE 252 let P_uvw = Ro_uvw + Rd_step_uvw * f32(step); 253 if (all(P_uvw >= vec3(0.0)) && all(P_uvw <= vec3(1.0))) { 254 density *= textureSample(density_texture, density_sampler, P_uvw + density_texture_offset).r; 258#endif // DENSITY_TEXTURE 262 let sample_attenuation = exp(-step_size_world * density * (absorption + scattering)); 265 background_alpha *= sample_attenuation; 272 let cascade_index = get_cascade_index(light_index, P_view.z); 273 let light_local = world_to_directional_light_local( 276 vec4(P_world + depth_offset, 1.0) 281 var local_light_attenuation = f32(light_local.w != 0.0); 285 if (local_light_attenuation != 0.0) { 286 let cascade = &(*light).cascades[cascade_index]; 287 let array_index = i32((*light).depth_texture_base_index + cascade_index); 288 local_light_attenuation = 289 sample_shadow_map_hardware(light_local.xy, light_local.z, array_index); 292 if (local_light_attenuation != 0.0) { 293 let light_attenuation = exp(-density * bounding_radius * (absorption + scattering)); 294 let light_factors_per_step = fog_color * light_tint * light_attenuation * 295 scattering * density * step_size_world * light_intensity * exposure; 299 let light_color_per_step = (*light).color.rgb * phase * light_factors_per_step; 302 accumulated_color += light_color_per_step * local_light_attenuation * 309 let view_z = view_start_pos.z; 310 let is_orthographic = view.clip_from_view[3].w == 1.0; 311 let cluster_index = clustering::fragment_cluster_index(frag_coord.xy, view_z, is_orthographic); 312 var clusterable_object_index_ranges = 313 clustering::unpack_clusterable_object_index_ranges(cluster_index); 314 for (var i: u32 = clusterable_object_index_ranges.first_point_light_index_offset; 315 i < clusterable_object_index_ranges.first_reflection_probe_index_offset; 317 let light_id = clustering::get_clusterable_object_id(i); 318 let light = &clusterable_objects.data[light_id]; 319 if (((*light).flags & POINT_LIGHT_FLAGS_VOLUMETRIC_BIT) == 0) { 324 background_alpha = 1.0; 327 for (var step = 0u; step < step_count; step += 1u) { 329 if (background_alpha < 0.001) { 334 let P_world = Ro_world + Rd_world * f32(step) * step_size_world; 335 let P_view = Rd_view * f32(step) * step_size_world; 337 var density = density_factor; 339 let light_to_frag = (*light).position_radius.xyz - P_world; 341 let L = normalize(light_to_frag); 342 let distance_square = dot(light_to_frag, light_to_frag); 343 let distance_atten = getDistanceAttenuation(distance_square, (*light).color_inverse_square_range.w); 344 var local_light_attenuation = distance_atten; 345 if (i < clusterable_object_index_ranges.first_spot_light_index_offset) { 346 var shadow: f32 = 1.0; 347 if (((*light).flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 348 shadow = fetch_point_shadow_without_normal(light_id, vec4(P_world, 1.0)); 350 local_light_attenuation *= shadow; 354 var spot_dir = vec3<f32>((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y); 355 spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z)); 356 if ((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u { 357 spot_dir.y = -spot_dir.y; 359 let light_to_frag = (*light).position_radius.xyz - P_world; 364 let cd = dot(-spot_dir, normalize(light_to_frag)); 365 let attenuation = saturate(cd * (*light).light_custom_data.z + (*light).light_custom_data.w); 366 let spot_attenuation = attenuation * attenuation; 368 var shadow: f32 = 1.0; 369 if (((*light).flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { 370 shadow = fetch_spot_shadow_without_normal(light_id, vec4(P_world, 1.0)); 372 local_light_attenuation *= spot_attenuation * shadow; 377 let sample_attenuation = exp(-step_size_world * density * (absorption + scattering)); 380 background_alpha *= sample_attenuation; 382 let light_attenuation = exp(-density * bounding_radius * (absorption + scattering)); 383 let light_factors_per_step = fog_color * light_tint * light_attenuation * 384 scattering * density * step_size_world * light_intensity * 0.1; 388 let light_color_per_step = (*light).color_inverse_square_range.rgb * light_factors_per_step; 391 accumulated_color += light_color_per_step * local_light_attenuation * 398 return vec4(accumulated_color, 1.0 - background_alpha); 401fn fetch_point_shadow_without_normal(light_id: u32, frag_position: vec4<f32>) -> f32 { 402 let light = &clusterable_objects.data[light_id]; 406 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz; 407 let surface_to_light_abs = abs(surface_to_light); 408 let distance_to_light = max(surface_to_light_abs.x, max(surface_to_light_abs.y, surface_to_light_abs.z)); 413 let depth_offset = (*light).shadow_depth_bias * normalize(surface_to_light.xyz); 414 let offset_position = frag_position.xyz + depth_offset; 417 let frag_ls = offset_position.xyz - (*light).position_radius.xyz ; 418 let abs_position_ls = abs(frag_ls); 419 let major_axis_magnitude = max(abs_position_ls.x, max(abs_position_ls.y, abs_position_ls.z)); 425 let zw = -major_axis_magnitude * (*light).light_custom_data.xy + (*light).light_custom_data.zw; 426 let depth = zw.x / zw.y; 430 let flip_z = vec3(1.0, 1.0, -1.0); 431 return sample_shadow_cubemap(frag_ls * flip_z, distance_to_light, depth, light_id); 434fn fetch_spot_shadow_without_normal(light_id: u32, frag_position: vec4<f32>) -> f32 { 435 let light = &clusterable_objects.data[light_id]; 437 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz; 440 var spot_dir = vec3<f32>((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y); 442 spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z)); 443 if (((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) { 444 spot_dir.y = -spot_dir.y; 449 let offset_position = 451 + ((*light).shadow_depth_bias * normalize(surface_to_light)); 459 let a = -1.0 / (fwd.z + sign); 460 let b = fwd.x * fwd.y * a; 461 let up_dir = vec3<f32>(1.0 + sign * fwd.x * fwd.x * a, sign * b, -sign * fwd.x); 462 let right_dir = vec3<f32>(-b, -sign - fwd.y * fwd.y * a, fwd.y); 463 let light_inv_rot = mat3x3<f32>(right_dir, up_dir, fwd); 468 let projected_position = offset_position * light_inv_rot; 472 let f_div_minus_z = 1.0 / ((*light).spot_light_tan_angle * -projected_position.z); 473 let shadow_xy_ndc = projected_position.xy * f_div_minus_z; 475 let shadow_uv = shadow_xy_ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5); 478 let depth = 0.1 / -projected_position.z; 480 return sample_shadow_map( 483 i32(light_id) + lights.spot_light_shadowmap_offset, 484 SPOT_SHADOW_TEXEL_SIZE