1#define_import_path bevy_pbr::shadows 3#import bevy_pbr::mesh_view_types as Types 4#import bevy_pbr::mesh_view_bindings as Bindings 6fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 { 7 let light = Bindings::point_lights.data[light_id]; 11 let surface_to_light = light.position_radius.xyz - frag_position.xyz; 12 let surface_to_light_abs = abs(surface_to_light); 13 let distance_to_light = max(surface_to_light_abs.x, max(surface_to_light_abs.y, surface_to_light_abs.z)); 18 let normal_offset = light.shadow_normal_bias * distance_to_light * surface_normal.xyz; 19 let depth_offset = light.shadow_depth_bias * normalize(surface_to_light.xyz); 20 let offset_position = frag_position.xyz + normal_offset + depth_offset; 23 let frag_ls = light.position_radius.xyz - offset_position.xyz; 24 let abs_position_ls = abs(frag_ls); 25 let major_axis_magnitude = max(abs_position_ls.x, max(abs_position_ls.y, abs_position_ls.z)); 31 let zw = -major_axis_magnitude * light.light_custom_data.xy + light.light_custom_data.zw; 32 let depth = zw.x / zw.y; 40#ifdef NO_ARRAY_TEXTURES_SUPPORT 41 return textureSampleCompare(Bindings::point_shadow_textures, Bindings::point_shadow_textures_sampler, frag_ls, depth); 43 return textureSampleCompareLevel(Bindings::point_shadow_textures, Bindings::point_shadow_textures_sampler, frag_ls, i32(light_id), depth); 47fn fetch_spot_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 { 48 let light = Bindings::point_lights.data[light_id]; 50 let surface_to_light = light.position_radius.xyz - frag_position.xyz; 53 var spot_dir = vec3<f32>(light.light_custom_data.x, 0.0, light.light_custom_data.y); 55 spot_dir.y = sqrt(1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z); 56 if ((light.flags & Types::POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) { 57 spot_dir.y = -spot_dir.y; 62 let distance_to_light = dot(fwd, surface_to_light); 65 + (light.shadow_depth_bias * normalize(surface_to_light)) 66 + (surface_normal.xyz * light.shadow_normal_bias) * distance_to_light; 74 let a = -1.0 / (fwd.z + sign); 75 let b = fwd.x * fwd.y * a; 76 let up_dir = vec3<f32>(1.0 + sign * fwd.x * fwd.x * a, sign * b, -sign * fwd.x); 77 let right_dir = vec3<f32>(-b, -sign - fwd.y * fwd.y * a, fwd.y); 78 let light_inv_rot = mat3x3<f32>(right_dir, up_dir, fwd); 83 let projected_position = offset_position * light_inv_rot; 87 let f_div_minus_z = 1.0 / (light.spot_light_tan_angle * -projected_position.z); 88 let shadow_xy_ndc = projected_position.xy * f_div_minus_z; 90 let shadow_uv = shadow_xy_ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5); 93 let depth = 0.1 / -projected_position.z; 95 #ifdef NO_ARRAY_TEXTURES_SUPPORT 96 return textureSampleCompare(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, 99 return textureSampleCompareLevel(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, 100 shadow_uv, i32(light_id) + Bindings::lights.spot_light_shadowmap_offset, depth); 104fn fetch_directional_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 { 105 let light = Bindings::lights.directional_lights[light_id]; 108 let normal_offset = light.shadow_normal_bias * surface_normal.xyz; 109 let depth_offset = light.shadow_depth_bias * light.direction_to_light.xyz; 110 let offset_position = vec4<f32>(frag_position.xyz + normal_offset + depth_offset, frag_position.w); 112 let offset_position_clip = light.view_projection * offset_position; 113 if (offset_position_clip.w <= 0.0) { 116 let offset_position_ndc = offset_position_clip.xyz / offset_position_clip.w; 118 if (any(offset_position_ndc.xy < vec2<f32>(-1.0)) || offset_position_ndc.z < 0.0 119 || any(offset_position_ndc > vec3<f32>(1.0))) { 125 let flip_correction = vec2<f32>(0.5, -0.5); 126 let light_local = offset_position_ndc.xy * flip_correction + vec2<f32>(0.5, 0.5); 128 let depth = offset_position_ndc.z; 132#ifdef NO_ARRAY_TEXTURES_SUPPORT 133 return textureSampleCompareLevel(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, light_local, depth); 135 return textureSampleCompareLevel(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, light_local, i32(light_id), depth);