1
1
#define_import_path bevy_pbr ::shadows
2
2
3
+ const flip_z : vec3 <f32 > = vec3 <f32 >(1.0 , 1.0 , - 1.0 );
4
+
3
5
fn fetch_point_shadow (light_id : u32 , frag_position : vec4 <f32 >, surface_normal : vec3 <f32 >) -> f32 {
4
6
let light = & point_lights . data[light_id ];
5
7
@@ -17,7 +19,7 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
17
19
let offset_position = frag_position . xyz + normal_offset + depth_offset ;
18
20
19
21
// similar largest-absolute-axis trick as above, but now with the offset fragment position
20
- let frag_ls = (*light ). position_radius. xyz - offset_position . xyz ;
22
+ let frag_ls = offset_position . xyz - (*light ). position_radius. xyz ;
21
23
let abs_position_ls = abs (frag_ls );
22
24
let major_axis_magnitude = max (abs_position_ls . x, max (abs_position_ls . y, abs_position_ls . z));
23
25
@@ -28,16 +30,17 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
28
30
let zw = - major_axis_magnitude * (*light ). light_custom_data. xy + (*light ). light_custom_data. zw;
29
31
let depth = zw . x / zw . y;
30
32
31
- // do the lookup, using HW PCF and comparison
33
+ // Do the lookup, using HW PCF and comparison. Cubemaps assume a left-handed coordinate space,
34
+ // so we have to flip the z-axis when sampling.
32
35
// NOTE: Due to the non-uniform control flow above, we must use the Level variant of
33
36
// textureSampleCompare to avoid undefined behaviour due to some of the fragments in
34
37
// a quad (2x2 fragments) being processed not being sampled, and this messing with
35
38
// mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
36
39
// from LOD 0.
37
40
#ifdef NO_ARRAY_TEXTURES_SUPPORT
38
- return textureSampleCompare (point_shadow_textures , point_shadow_textures_sampler , frag_ls , depth );
41
+ return textureSampleCompare (point_shadow_textures , point_shadow_textures_sampler , frag_ls * flip_z , depth );
39
42
#else
40
- return textureSampleCompareLevel (point_shadow_textures , point_shadow_textures_sampler , frag_ls , i32 (light_id ), depth );
43
+ return textureSampleCompareLevel (point_shadow_textures , point_shadow_textures_sampler , frag_ls * flip_z , i32 (light_id ), depth );
41
44
#endif
42
45
}
43
46
0 commit comments