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::{
4 mesh_view_types::POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE,
5 mesh_view_bindings as view_bindings,
6 shadow_sampling::{
7 SPOT_SHADOW_TEXEL_SIZE, sample_shadow_cubemap, sample_shadow_cubemap_pcss,
8 sample_shadow_map, sample_shadow_map_pcss,
9 }
12#import bevy_render::{
13 color_operations::hsv_to_rgb,
14 maths::PI_2
17const flip_z: vec3<f32> = vec3<f32>(1.0, 1.0, -1.0);
19fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>) -> f32 {
20 let light = &view_bindings::clusterable_objects.data[light_id];
22 // because the shadow maps align with the axes and the frustum planes are at 45 degrees
23 // we can get the worldspace depth by taking the largest absolute axis
24 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz;
25 let surface_to_light_abs = abs(surface_to_light);
26 let distance_to_light = max(surface_to_light_abs.x, max(surface_to_light_abs.y, surface_to_light_abs.z));
28 // The normal bias here is already scaled by the texel size at 1 world unit from the light.
29 // The texel size increases proportionally with distance from the light so multiplying by
30 // distance to light scales the normal bias to the texel size at the fragment distance.
31 let normal_offset = (*light).shadow_normal_bias * distance_to_light * surface_normal.xyz;
32 let depth_offset = (*light).shadow_depth_bias * normalize(surface_to_light.xyz);
33 let offset_position = frag_position.xyz + normal_offset + depth_offset;
35 // similar largest-absolute-axis trick as above, but now with the offset fragment position
36 let frag_ls = offset_position.xyz - (*light).position_radius.xyz ;
37 let abs_position_ls = abs(frag_ls);
38 let major_axis_magnitude = max(abs_position_ls.x, max(abs_position_ls.y, abs_position_ls.z));
40 // NOTE: These simplifications come from multiplying:
41 // projection * vec4(0, 0, -major_axis_magnitude, 1.0)
42 // and keeping only the terms that have any impact on the depth.
43 // Projection-agnostic approach:
44 let zw = -major_axis_magnitude * (*light).light_custom_data.xy + (*light).light_custom_data.zw;
45 let depth = zw.x / zw.y;
47 // If soft shadows are enabled, use the PCSS path. Cubemaps assume a
48 // left-handed coordinate space, so we have to flip the z-axis when
49 // sampling.
50 if ((*light).soft_shadow_size > 0.0) {
51 return sample_shadow_cubemap_pcss(
52 frag_ls * flip_z,
53 distance_to_light,
54 depth,
55 light_id,
56 (*light).soft_shadow_size,
57 );
58 }
60 // Do the lookup, using HW PCF and comparison. Cubemaps assume a left-handed
61 // coordinate space, so we have to flip the z-axis when sampling.
62 return sample_shadow_cubemap(frag_ls * flip_z, distance_to_light, depth, light_id);
65fn fetch_spot_shadow(
66 light_id: u32,
67 frag_position: vec4<f32>,
68 surface_normal: vec3<f32>,
69 near_z: f32,
70) -> f32 {
71 let light = &view_bindings::clusterable_objects.data[light_id];
73 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz;
75 // construct the light view matrix
76 var spot_dir = vec3<f32>((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y);
77 // reconstruct spot dir from x/z and y-direction flag
78 spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z));
79 if (((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) {
80 spot_dir.y = -spot_dir.y;
81 }
83 // view matrix z_axis is the reverse of transform.forward()
84 let fwd = -spot_dir;
85 let distance_to_light = dot(fwd, surface_to_light);
86 let offset_position =
87 -surface_to_light
88 + ((*light).shadow_depth_bias * normalize(surface_to_light))
89 + (surface_normal.xyz * (*light).shadow_normal_bias) * distance_to_light;
91 // the construction of the up and right vectors needs to precisely mirror the code
92 // in render/light.rs:spot_light_view_matrix
93 var sign = -1.0;
94 if (fwd.z >= 0.0) {
95 sign = 1.0;
96 }
97 let a = -1.0 / (fwd.z + sign);
98 let b = fwd.x * fwd.y * a;
99 let up_dir = vec3<f32>(1.0 + sign * fwd.x * fwd.x * a, sign * b, -sign * fwd.x);
100 let right_dir = vec3<f32>(-b, -sign - fwd.y * fwd.y * a, fwd.y);
101 let light_inv_rot = mat3x3<f32>(right_dir, up_dir, fwd);
103 // because the matrix is a pure rotation matrix, the inverse is just the transpose, and to calculate
104 // the product of the transpose with a vector we can just post-multiply instead of pre-multiplying.
105 // this allows us to keep the matrix construction code identical between CPU and GPU.
106 let projected_position = offset_position * light_inv_rot;
108 // divide xy by perspective matrix "f" and by -projected.z (projected.z is -projection matrix's w)
109 // to get ndc coordinates
110 let f_div_minus_z = 1.0 / ((*light).spot_light_tan_angle * -projected_position.z);
111 let shadow_xy_ndc = projected_position.xy * f_div_minus_z;
112 // convert to uv coordinates
113 let shadow_uv = shadow_xy_ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);
115 let depth = near_z / -projected_position.z;
117 // If soft shadows are enabled, use the PCSS path.
118 let array_index = i32(light_id) + view_bindings::lights.spot_light_shadowmap_offset;
119 if ((*light).soft_shadow_size > 0.0) {
120 return sample_shadow_map_pcss(
121 shadow_uv, depth, array_index, SPOT_SHADOW_TEXEL_SIZE, (*light).soft_shadow_size);
122 }
124 return sample_shadow_map(shadow_uv, depth, array_index, SPOT_SHADOW_TEXEL_SIZE);
127fn get_cascade_index(light_id: u32, view_z: f32) -> u32 {
128 let light = &view_bindings::lights.directional_lights[light_id];
130 for (var i: u32 = 0u; i < (*light).num_cascades; i = i + 1u) {
131 if (-view_z < (*light).cascades[i].far_bound) {
132 return i;
133 }
134 }
135 return (*light).num_cascades;
138// Converts from world space to the uv position in the light's shadow map.
139//
140// The depth is stored in the return value's z coordinate. If the return value's
141// w coordinate is 0.0, then we landed outside the shadow map entirely.
142fn world_to_directional_light_local(
143 light_id: u32,
144 cascade_index: u32,
145 offset_position: vec4<f32>
146) -> vec4<f32> {
147 let light = &view_bindings::lights.directional_lights[light_id];
148 let cascade = &(*light).cascades[cascade_index];
150 let offset_position_clip = (*cascade).clip_from_world * offset_position;
151 if (offset_position_clip.w <= 0.0) {
152 return vec4(0.0);
153 }
154 let offset_position_ndc = offset_position_clip.xyz / offset_position_clip.w;
155 // No shadow outside the orthographic projection volume
156 if (any(offset_position_ndc.xy < vec2<f32>(-1.0)) || offset_position_ndc.z < 0.0
157 || any(offset_position_ndc > vec3<f32>(1.0))) {
158 return vec4(0.0);
159 }
161 // compute texture coordinates for shadow lookup, compensating for the Y-flip difference
162 // between the NDC and texture coordinates
163 let flip_correction = vec2<f32>(0.5, -0.5);
164 let light_local = offset_position_ndc.xy * flip_correction + vec2<f32>(0.5, 0.5);
166 let depth = offset_position_ndc.z;
168 return vec4(light_local, depth, 1.0);
171fn sample_directional_cascade(
172 light_id: u32,
173 cascade_index: u32,
174 frag_position: vec4<f32>,
175 surface_normal: vec3<f32>,
176) -> f32 {
177 let light = &view_bindings::lights.directional_lights[light_id];
178 let cascade = &(*light).cascades[cascade_index];
180 // The normal bias is scaled to the texel size.
181 let normal_offset = (*light).shadow_normal_bias * (*cascade).texel_size * surface_normal.xyz;
182 let depth_offset = (*light).shadow_depth_bias * (*light).direction_to_light.xyz;
183 let offset_position = vec4<f32>(frag_position.xyz + normal_offset + depth_offset, frag_position.w);
185 let light_local = world_to_directional_light_local(light_id, cascade_index, offset_position);
186 if (light_local.w == 0.0) {
187 return 1.0;
188 }
190 let array_index = i32((*light).depth_texture_base_index + cascade_index);
191 let texel_size = (*cascade).texel_size;
193 // If soft shadows are enabled, use the PCSS path.
194 if ((*light).soft_shadow_size > 0.0) {
195 return sample_shadow_map_pcss(
196 light_local.xy, light_local.z, array_index, texel_size, (*light).soft_shadow_size);
197 }
199 return sample_shadow_map(light_local.xy, light_local.z, array_index, texel_size);
202fn fetch_directional_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: vec3<f32>, view_z: f32) -> f32 {
203 let light = &view_bindings::lights.directional_lights[light_id];
204 let cascade_index = get_cascade_index(light_id, view_z);
206 if (cascade_index >= (*light).num_cascades) {
207 return 1.0;
208 }
210 var shadow = sample_directional_cascade(light_id, cascade_index, frag_position, surface_normal);
212 // Blend with the next cascade, if there is one.
213 let next_cascade_index = cascade_index + 1u;
214 if (next_cascade_index < (*light).num_cascades) {
215 let this_far_bound = (*light).cascades[cascade_index].far_bound;
216 let next_near_bound = (1.0 - (*light).cascades_overlap_proportion) * this_far_bound;
217 if (-view_z >= next_near_bound) {
218 let next_shadow = sample_directional_cascade(light_id, next_cascade_index, frag_position, surface_normal);
219 shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
220 }
221 }
222 return shadow;
225fn cascade_debug_visualization(
226 output_color: vec3<f32>,
227 light_id: u32,
228 view_z: f32,
229) -> vec3<f32> {
230 let overlay_alpha = 0.95;
231 let cascade_index = get_cascade_index(light_id, view_z);
232 let cascade_color_hsv = vec3(
233 f32(cascade_index) / f32(#{MAX_CASCADES_PER_LIGHT}u + 1u) * PI_2,
234 1.0,
235 0.5
236 );
237 let cascade_color = hsv_to_rgb(cascade_color_hsv);
238 return vec3<f32>(
239 (1.0 - overlay_alpha) * output_color.rgb + overlay_alpha * cascade_color
240 );
Contents