Skip to content

Commit 9a9097d

Browse files
committed
bevy_pbr: Fix point light cubemap face view transforms
Mat4::look_at_* produces inverse view matrices. Fix this at the source. The bug that was fixed here is that the translation was also being inversed, which was incorrect.
1 parent 9d3a92d commit 9a9097d

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ pub fn prepare_lights(
602602
Mat4::perspective_infinite_reverse_lh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z);
603603
let cube_face_rotations = CUBE_MAP_FACES
604604
.iter()
605-
.map(|CubeMapFace { target, up }| Mat4::look_at_lh(Vec3::ZERO, *target, *up))
605+
// NOTE: Inverse here as the Mat4::look_at_* seem to produce inverse matrices
606+
.map(|CubeMapFace { target, up }| Mat4::look_at_lh(Vec3::ZERO, *target, *up).inverse())
606607
.collect::<Vec<_>>();
607608

608609
global_light_meta.gpu_point_lights.clear();
@@ -776,8 +777,7 @@ pub fn prepare_lights(
776777
width: point_light_shadow_map.size as u32,
777778
height: point_light_shadow_map.size as u32,
778779
position: light_position_world_lh,
779-
// Inverse here as the Mat4::look_at_* seem to produce inverse matrices
780-
view: view.inverse(),
780+
view,
781781
projection: cube_face_projection,
782782
near: POINT_LIGHT_NEAR_Z,
783783
far: light.range,
@@ -816,7 +816,9 @@ pub fn prepare_lights(
816816

817817
// NOTE: A directional light seems to have to have an eye position on the line along the direction of the light
818818
// through the world origin. I (Rob Swain) do not yet understand why it cannot be translated away from this.
819-
let view = Mat4::look_at_rh(Vec3::ZERO, light.direction, Vec3::Y);
819+
// NOTE: Inverse here as the Mat4::look_at_* seem to produce inverse matrices
820+
let inverse_view = Mat4::look_at_rh(Vec3::ZERO, light.direction, Vec3::Y);
821+
let view = inverse_view.inverse();
820822
// NOTE: This orthographic projection defines the volume within which shadows from a directional light can be cast
821823
let projection = light.projection;
822824

@@ -830,8 +832,7 @@ pub fn prepare_lights(
830832
// we don't use the alpha at all, so no reason to multiply only [0..3]
831833
color: Vec4::from_slice(&light.color.as_linear_rgba_f32()) * intensity,
832834
dir_to_light,
833-
// NOTE: * view is correct, it should not be view.inverse() here
834-
view_projection: projection * view,
835+
view_projection: projection * inverse_view,
835836
flags: flags.bits,
836837
shadow_depth_bias: light.shadow_depth_bias,
837838
shadow_normal_bias: light.shadow_normal_bias,
@@ -863,7 +864,7 @@ pub fn prepare_lights(
863864
width: directional_light_shadow_map.size as u32,
864865
height: directional_light_shadow_map.size as u32,
865866
position: Vec3::ZERO,
866-
view: view.inverse(),
867+
view,
867868
projection,
868869
near: light.near,
869870
far: light.far,

0 commit comments

Comments
 (0)