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.
1import package::render::{
2 mesh_view_types::POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE,
3 mesh_view_bindings as view_bindings,
4 shadow_sampling::{
5 SPOT_SHADOW_TEXEL_SIZE, sample_shadow_cubemap, sample_shadow_cubemap_pcss,
6 sample_shadow_map, sample_shadow_map_pcss,
7 }
8};
10import bevy_render::{
11 color_operations::hsv_to_rgb,
12 maths::{orthonormalize, PI_2}
13};
15const flip_z: vec3<f32> = vec3<f32>(1.0, 1.0, -1.0);
17fn fetch_point_shadow(
18 light_id: u32,
19 frag_position: vec4<f32>,
20 surface_normal: vec3<f32>,
21 frag_coord_xy: vec2<f32>,
22) -> f32 {
23 let light = &view_bindings::clustered_lights.data[light_id];
25 // because the shadow maps align with the axes and the frustum planes are at 45 degrees
26 // we can get the worldspace depth by taking the largest absolute axis
27 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz;
28 let surface_to_light_abs = abs(surface_to_light);
29 let distance_to_light = max(surface_to_light_abs.x, max(surface_to_light_abs.y, surface_to_light_abs.z));
31 // The normal bias here is already scaled by the texel size at 1 world unit from the light.
32 // The texel size increases proportionally with distance from the light so multiplying by
33 // distance to light scales the normal bias to the texel size at the fragment distance.
34 let normal_offset = (*light).shadow_normal_bias * distance_to_light * surface_normal.xyz;
35 let depth_offset = (*light).shadow_depth_bias * normalize(surface_to_light.xyz);
36 let offset_position = frag_position.xyz + normal_offset + depth_offset;
38 // similar largest-absolute-axis trick as above, but now with the offset fragment position
39 let frag_ls = offset_position.xyz - (*light).position_radius.xyz ;
40 let abs_position_ls = abs(frag_ls);
41 let major_axis_magnitude = max(abs_position_ls.x, max(abs_position_ls.y, abs_position_ls.z));
43 // NOTE: These simplifications come from multiplying:
44 // projection * vec4(0, 0, -major_axis_magnitude, 1.0)
45 // and keeping only the terms that have any impact on the depth.
46 // Projection-agnostic approach:
47 let zw = -major_axis_magnitude * (*light).light_custom_data.xy + (*light).light_custom_data.zw;
48 let depth = zw.x / zw.y;
50 // If soft shadows are enabled, use the PCSS path. Cubemaps assume a
51 // left-handed coordinate space, so we have to flip the z-axis when
52 // sampling.
53 if ((*light).soft_shadow_size > 0.0) {
54 return sample_shadow_cubemap_pcss(
55 frag_ls * flip_z,
56 distance_to_light,
57 depth,
58 light_id,
59 (*light).soft_shadow_size,
60 frag_coord_xy,
61 );
62 }
64 // Do the lookup, using HW PCF and comparison. Cubemaps assume a left-handed
65 // coordinate space, so we have to flip the z-axis when sampling.
66 return sample_shadow_cubemap(frag_ls * flip_z, distance_to_light, depth, light_id, frag_coord_xy);
69fn fetch_spot_shadow(
70 light_id: u32,
71 frag_position: vec4<f32>,
72 surface_normal: vec3<f32>,
73 near_z: f32,
74 frag_coord_xy: vec2<f32>,
75) -> f32 {
76 let light = &view_bindings::clustered_lights.data[light_id];
78 let surface_to_light = (*light).position_radius.xyz - frag_position.xyz;
80 // construct the light view matrix
81 var spot_dir = vec3<f32>((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y);
82 // reconstruct spot dir from x/z and y-direction flag
83 spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z));
84 if (((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) {
85 spot_dir.y = -spot_dir.y;
86 }
88 // view matrix z_axis is the reverse of transform.forward()
89 let fwd = -spot_dir;
90 let distance_to_light = dot(fwd, surface_to_light);
91 let offset_position =
92 -surface_to_light
93 + ((*light).shadow_depth_bias * normalize(surface_to_light))
94 + (surface_normal.xyz * (*light).shadow_normal_bias) * distance_to_light;
96 let light_inv_rot = orthonormalize(fwd);
98 // because the matrix is a pure rotation matrix, the inverse is just the transpose, and to calculate
99 // the product of the transpose with a vector we can just post-multiply instead of pre-multiplying.
100 // this allows us to keep the matrix construction code identical between CPU and GPU.
101 let projected_position = offset_position * light_inv_rot;
103 // divide xy by perspective matrix "f" and by -projected.z (projected.z is -projection matrix's w)
104 // to get ndc coordinates
105 let f_div_minus_z = 1.0 / ((*light).spot_light_tan_angle * -projected_position.z);
106 let shadow_xy_ndc = projected_position.xy * f_div_minus_z;
107 // convert to uv coordinates
108 let shadow_uv = shadow_xy_ndc * vec2<f32>(0.5, -0.5) + vec2<f32>(0.5, 0.5);
110 let depth = near_z / -projected_position.z;
112 // If soft shadows are enabled, use the PCSS path.
113 let array_index = i32(light_id) + view_bindings::lights.spot_light_shadowmap_offset;
114 if ((*light).soft_shadow_size > 0.0) {
115 return sample_shadow_map_pcss(
116 shadow_uv,
117 depth,
118 array_index,
119 frag_coord_xy,
120 SPOT_SHADOW_TEXEL_SIZE,
121 (*light).soft_shadow_size,
122 );
123 }
125 return sample_shadow_map(
126 shadow_uv,
127 depth,
128 array_index,
129 frag_coord_xy,
130 SPOT_SHADOW_TEXEL_SIZE,
131 );
134fn get_cascade_index(light_id: u32, view_z: f32) -> u32 {
135 let light = &view_bindings::lights.directional_lights[light_id];
137 for (var i: u32 = 0u; i < (*light).num_cascades; i = i + 1u) {
138 if (-view_z < (*light).cascades[i].far_bound) {
139 return i;
140 }
141 }
142 return (*light).num_cascades;
145/// Converts from world space to the uv position in the light's shadow map.
146///
147/// The depth is stored in the return value's z coordinate. If the return value's
148/// w coordinate is 0.0, then we landed outside the shadow map entirely.
149fn world_to_directional_light_local(
150 light_id: u32,
151 cascade_index: u32,
152 offset_position: vec4<f32>
153) -> vec4<f32> {
154 let light = &view_bindings::lights.directional_lights[light_id];
155 let cascade = &(*light).cascades[cascade_index];
157 let offset_position_clip = (*cascade).clip_from_world * offset_position;
158 if (offset_position_clip.w <= 0.0) {
159 return vec4(0.0);
160 }
161 let offset_position_ndc = offset_position_clip.xyz / offset_position_clip.w;
162 // No shadow outside the orthographic projection volume
163 if (any(offset_position_ndc.xy < vec2<f32>(-1.0)) || offset_position_ndc.z < 0.0
164 || any(offset_position_ndc > vec3<f32>(1.0))) {
165 return vec4(0.0);
166 }
168 // compute texture coordinates for shadow lookup, compensating for the Y-flip difference
169 // between the NDC and texture coordinates
170 let flip_correction = vec2<f32>(0.5, -0.5);
171 let light_local = offset_position_ndc.xy * flip_correction + vec2<f32>(0.5, 0.5);
173 let depth = offset_position_ndc.z;
175 return vec4(light_local, depth, 1.0);
178fn sample_directional_cascade(
179 light_id: u32,
180 cascade_index: u32,
181 frag_position: vec4<f32>,
182 surface_normal: vec3<f32>,
183 frag_coord_xy: vec2<f32>,
184) -> f32 {
185 let light = &view_bindings::lights.directional_lights[light_id];
186 let cascade = &(*light).cascades[cascade_index];
188 // The normal bias is scaled to the texel size.
189 let normal_offset = (*light).shadow_normal_bias * (*cascade).texel_size * surface_normal.xyz;
190 let depth_offset = (*light).shadow_depth_bias * (*light).direction_to_light.xyz;
191 let offset_position = vec4<f32>(frag_position.xyz + normal_offset + depth_offset, frag_position.w);
193 let light_local = world_to_directional_light_local(light_id, cascade_index, offset_position);
194 if (light_local.w == 0.0) {
195 return 1.0;
196 }
198 let array_index = i32((*light).depth_texture_base_index + cascade_index);
199 let texel_size = (*cascade).texel_size;
201 // If soft shadows are enabled, use the PCSS path.
202 if ((*light).soft_shadow_size > 0.0) {
203 return sample_shadow_map_pcss(
204 light_local.xy,
205 light_local.z,
206 array_index,
207 frag_coord_xy,
208 texel_size,
209 (*light).soft_shadow_size,
210 );
211 }
213 return sample_shadow_map(
214 light_local.xy,
215 light_local.z,
216 array_index,
217 frag_coord_xy,
218 texel_size,
219 );
222fn fetch_directional_shadow(
223 light_id: u32,
224 frag_position: vec4<f32>,
225 surface_normal: vec3<f32>,
226 view_z: f32,
227 frag_coord_xy: vec2<f32>,
228) -> f32 {
229 let light = &view_bindings::lights.directional_lights[light_id];
230 let cascade_index = get_cascade_index(light_id, view_z);
232 if (cascade_index >= (*light).num_cascades) {
233 return 1.0;
234 }
236 var shadow = sample_directional_cascade(
237 light_id,
238 cascade_index,
239 frag_position,
240 surface_normal,
241 frag_coord_xy,
242 );
244 // Blend with the next cascade, if there is one.
245 let next_cascade_index = cascade_index + 1u;
246 if (next_cascade_index < (*light).num_cascades) {
247 let this_far_bound = (*light).cascades[cascade_index].far_bound;
248 let next_near_bound = (1.0 - (*light).cascades_overlap_proportion) * this_far_bound;
249 if (-view_z >= next_near_bound) {
250 let next_shadow = sample_directional_cascade(
251 light_id,
252 next_cascade_index,
253 frag_position,
254 surface_normal,
255 frag_coord_xy,
256 );
257 shadow = mix(shadow, next_shadow, (-view_z - next_near_bound) / (this_far_bound - next_near_bound));
258 }
259 }
260 return shadow;
263fn cascade_debug_visualization(
264 output_color: vec3<f32>,
265 light_id: u32,
266 view_z: f32,
267) -> vec3<f32> {
268 let overlay_alpha = 0.95;
269 let cascade_index = get_cascade_index(light_id, view_z);
270 let cascade_color_hsv = vec3(
271 f32(cascade_index) / f32(u32(constants::MAX_CASCADES_PER_LIGHT) + 1u) * PI_2,
272 1.0,
273 0.5
274 );
275 let cascade_color = hsv_to_rgb(cascade_color_hsv);
276 return vec3<f32>(
277 (1.0 - overlay_alpha) * output_color.rgb + overlay_alpha * cascade_color
278 );
Contents