Search across packages, versions, files, declarations, and comments. Use multiple terms to narrow results (AND), and add @vertex, @fragment, or @compute to filter by shader stage.
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];
9 // because the shadow maps align with the axes and the frustum planes are at 45 degrees
10 // we can get the worldspace depth by taking the largest absolute axis
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));
15 // The normal bias here is already scaled by the texel size at 1 world unit from the light.
16 // The texel size increases proportionally with distance from the light so multiplying by
17 // distance to light scales the normal bias to the texel size at the fragment distance.
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;
22 // similar largest-absolute-axis trick as above, but now with the offset fragment position
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));
27 // NOTE: These simplifications come from multiplying:
28 // projection * vec4(0, 0, -major_axis_magnitude, 1.0)
29 // and keeping only the terms that have any impact on the depth.
30 // Projection-agnostic approach:
31 let zw = -major_axis_magnitude * light.light_custom_data.xy + light.light_custom_data.zw;
32 let depth = zw.x / zw.y;
34 // do the lookup, using HW PCF and comparison
35 // NOTE: Due to the non-uniform control flow above, we must use the Level variant of
36 // textureSampleCompare to avoid undefined behaviour due to some of the fragments in
37 // a quad (2x2 fragments) being processed not being sampled, and this messing with
38 // mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
39 // from LOD 0.
40#ifdef NO_ARRAY_TEXTURES_SUPPORT
41 return textureSampleCompare(Bindings::point_shadow_textures, Bindings::point_shadow_textures_sampler, frag_ls, depth);
42#else
43 return textureSampleCompareLevel(Bindings::point_shadow_textures, Bindings::point_shadow_textures_sampler, frag_ls, i32(light_id), depth);
44#endif
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;
52 // construct the light view matrix
53 var spot_dir = vec3<f32>(light.light_custom_data.x, 0.0, light.light_custom_data.y);
54 // reconstruct spot dir from x/z and y-direction flag
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;
58 }
60 // view matrix z_axis is the reverse of transform.forward()
61 let fwd = -spot_dir;
62 let distance_to_light = dot(fwd, surface_to_light);
63 let offset_position =
64 -surface_to_light
65 + (light.shadow_depth_bias * normalize(surface_to_light))
66 + (surface_normal.xyz * light.shadow_normal_bias) * distance_to_light;
68 // the construction of the up and right vectors needs to precisely mirror the code
69 // in render/light.rs:spot_light_view_matrix
70 var sign = -1.0;
71 if (fwd.z >= 0.0) {
72 sign = 1.0;
73 }
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);
80 // because the matrix is a pure rotation matrix, the inverse is just the transpose, and to calculate
81 // the product of the transpose with a vector we can just post-multiply instead of pre-multplying.
82 // this allows us to keep the matrix construction code identical between CPU and GPU.
83 let projected_position = offset_position * light_inv_rot;
85 // divide xy by perspective matrix "f" and by -projected.z (projected.z is -projection matrix's w)
86 // to get ndc coordinates
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;
89 // convert to uv coordinates
90 let shadow_uv = shadow_xy_ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);
92 // 0.1 must match POINT_LIGHT_NEAR_Z
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,
97 shadow_uv, depth);
98 #else
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);
101 #endif
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];
107 // The normal bias is scaled to the texel size.
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) {
114 return 1.0;
115 }
116 let offset_position_ndc = offset_position_clip.xyz / offset_position_clip.w;
117 // No shadow outside the orthographic projection volume
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))) {
120 return 1.0;
121 }
123 // compute texture coordinates for shadow lookup, compensating for the Y-flip difference
124 // between the NDC and texture coordinates
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;
129 // do the lookup, using HW PCF and comparison
130 // NOTE: Due to non-uniform control flow above, we must use the level variant of the texture
131 // sampler to avoid use of implicit derivatives causing possible undefined behavior.
132#ifdef NO_ARRAY_TEXTURES_SUPPORT
133 return textureSampleCompareLevel(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, light_local, depth);
134#else
135 return textureSampleCompareLevel(Bindings::directional_shadow_textures, Bindings::directional_shadow_textures_sampler, light_local, i32(light_id), depth);
136#endif
Contents