Skip to content

Commit 6fc4bc1

Browse files
authored
Split out spot_light_world_from_view into a function in shadows.wgsl (#20050)
# Objective - Improve readability ## Solution - Split a function out ## Testing - spotlight example works
1 parent aa87581 commit 6fc4bc1

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

crates/bevy_pbr/src/render/shadows.wgsl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ fn fetch_point_shadow(light_id: u32, frag_position: vec4<f32>, surface_normal: v
6262
return sample_shadow_cubemap(frag_ls * flip_z, distance_to_light, depth, light_id);
6363
}
6464

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+
6581
fn fetch_spot_shadow(
6682
light_id: u32,
6783
frag_position: vec4<f32>,
@@ -88,17 +104,7 @@ fn fetch_spot_shadow(
88104
+ ((*light).shadow_depth_bias * normalize(surface_to_light))
89105
+ (surface_normal.xyz * (*light).shadow_normal_bias) * distance_to_light;
90106

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);
102108

103109
// because the matrix is a pure rotation matrix, the inverse is just the transpose, and to calculate
104110
// the product of the transpose with a vector we can just post-multiply instead of pre-multiplying.

0 commit comments

Comments
 (0)