11#define_import_path bevy_pbr :: shadows
22
3+ const flip_z : vec3 <f32 > = vec3 <f32 >(1 .0 , 1 .0 , - 1 .0 );
4+
35fn fetch_point_shadow (light_id : u32 , frag_position : vec4 <f32 >, surface_normal : vec3 <f32 >) -> f32 {
46 let light = & point_lights . data [light_id ];
57
@@ -17,7 +19,7 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
1719 let offset_position = frag_position . xyz + normal_offset + depth_offset ;
1820
1921 // 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 ;
2123 let abs_position_ls = abs (frag_ls );
2224 let major_axis_magnitude = max (abs_position_ls . x , max (abs_position_ls . y , abs_position_ls . z ));
2325
@@ -28,16 +30,17 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
2830 let zw = - major_axis_magnitude * (*light ). light_custom_data . xy + (*light ). light_custom_data . zw ;
2931 let depth = zw . x / zw . y ;
3032
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.
3235 // NOTE: Due to the non-uniform control flow above, we must use the Level variant of
3336 // textureSampleCompare to avoid undefined behaviour due to some of the fragments in
3437 // a quad (2x2 fragments) being processed not being sampled, and this messing with
3538 // mip-mapping functionality. The shadow maps have no mipmaps so Level just samples
3639 // from LOD 0.
3740#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 );
3942#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 );
4144#endif
4245}
4346
0 commit comments