Skip to content

Commit

Permalink
bevy_pbr: Fix point light cubemap face view transforms
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
superdump committed Apr 4, 2022
1 parent 9d3a92d commit aa4dc3a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ pub fn prepare_lights(
Mat4::perspective_infinite_reverse_lh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z);
let cube_face_rotations = CUBE_MAP_FACES
.iter()
.map(|CubeMapFace { target, up }| Mat4::look_at_lh(Vec3::ZERO, *target, *up))
// NOTE: Inverse here as the Mat4::look_at_* seem to produce inverse matrices
.map(|CubeMapFace { target, up }| Mat4::look_at_lh(Vec3::ZERO, *target, *up).inverse())
.collect::<Vec<_>>();

global_light_meta.gpu_point_lights.clear();
Expand Down Expand Up @@ -776,8 +777,7 @@ pub fn prepare_lights(
width: point_light_shadow_map.size as u32,
height: point_light_shadow_map.size as u32,
position: light_position_world_lh,
// Inverse here as the Mat4::look_at_* seem to produce inverse matrices
view: view.inverse(),
view,
projection: cube_face_projection,
near: POINT_LIGHT_NEAR_Z,
far: light.range,
Expand Down Expand Up @@ -816,7 +816,8 @@ pub fn prepare_lights(

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

Expand Down Expand Up @@ -863,7 +864,7 @@ pub fn prepare_lights(
width: directional_light_shadow_map.size as u32,
height: directional_light_shadow_map.size as u32,
position: Vec3::ZERO,
view: view.inverse(),
view,
projection,
near: light.near,
far: light.far,
Expand Down

0 comments on commit aa4dc3a

Please sign in to comment.