Skip to content

Commit aa4dc3a

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 aa4dc3a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

crates/bevy_pbr/src/render/light.rs

Lines changed: 6 additions & 5 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,8 @@ 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 view = Mat4::look_at_rh(Vec3::ZERO, light.direction, Vec3::Y).inverse();
820821
// NOTE: This orthographic projection defines the volume within which shadows from a directional light can be cast
821822
let projection = light.projection;
822823

@@ -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)