@@ -62,6 +62,22 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
62
62
return sample_shadow_cubemap (frag_ls * flip_z , distance_to_light , depth , light_id );
63
63
}
64
64
65
+ // this method of constructing a basis from a vec3 is used by glam::Vec3::any_orthonormal_pair
66
+ // so we reproduce it here to avoid a mismatch if glam changes. we also switch the handedness
67
+ // the construction of the orthonormal basis up and right vectors needs to precisely mirror the code
68
+ // in bevy_light/spot_light.rs:spot_light_world_from_view
69
+ fn spot_light_world_from_view (fwd : vec3 <f32 >) -> mat3x3 <f32 > {
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
+ return mat3x3 <f32 >(right_dir , up_dir , fwd );
79
+ }
80
+
65
81
fn fetch_spot_shadow (
66
82
light_id : u32 ,
67
83
frag_position : vec4 <f32 >,
@@ -88,17 +104,7 @@ fn fetch_spot_shadow(
88
104
+ ((*light ). shadow_depth_bias * normalize (surface_to_light ))
89
105
+ (surface_normal . xyz * (*light ). shadow_normal_bias) * distance_to_light ;
90
106
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 );
107
+ let light_inv_rot = spot_light_world_from_view (fwd );
102
108
103
109
// because the matrix is a pure rotation matrix, the inverse is just the transpose, and to calculate
104
110
// the product of the transpose with a vector we can just post-multiply instead of pre-multiplying.
0 commit comments