Skip to content

Commit

Permalink
Swap names of projection_matrix functions so unchecked one is explici…
Browse files Browse the repository at this point in the history
…tly named.

This addresses bevyengine#9770 (comment)
  • Loading branch information
tormeh committed Aug 19, 2024
1 parent 1cafc09 commit d147b0a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
13 changes: 7 additions & 6 deletions crates/bevy_pbr/src/light/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ pub(crate) fn assign_lights_to_clusters(
let view_inv_scale = camera_transform.compute_transform().scale.recip();
let view_inv_scale_max = view_inv_scale.abs().max_element();
let inverse_view_transform = view_transform.inverse();
let is_orthographic = camera.projection_matrix().w_axis.w == 1.0;
let is_orthographic = camera.projection_matrix_unchecked().w_axis.w == 1.0;

let far_z = match config.far_z_mode() {
ClusterFarZMode::MaxLightRange => {
Expand All @@ -1248,7 +1248,8 @@ pub(crate) fn assign_lights_to_clusters(
// 3,2 = r * far and 2,2 = r where r = 1.0 / (far - near)
// rearranging r = 1.0 / (far - near), r * (far - near) = 1.0, r * far - 1.0 = r * near, near = (r * far - 1.0) / r
// = (3,2 - 1.0) / 2,2
(camera.projection_matrix().w_axis.z - 1.0) / camera.projection_matrix().z_axis.z
(camera.projection_matrix_unchecked().w_axis.z - 1.0)
/ camera.projection_matrix_unchecked().z_axis.z
}
(false, 1) => config.first_slice_depth().max(far_z),
_ => config.first_slice_depth(),
Expand Down Expand Up @@ -1280,7 +1281,7 @@ pub(crate) fn assign_lights_to_clusters(
let (light_aabb_min, light_aabb_max) = cluster_space_light_aabb(
inverse_view_transform,
view_inv_scale,
camera.projection_matrix(),
camera.projection_matrix_unchecked(),
&light_sphere,
);

Expand Down Expand Up @@ -1346,7 +1347,7 @@ pub(crate) fn assign_lights_to_clusters(
clusters.dimensions.x * clusters.dimensions.y * clusters.dimensions.z <= 4096
);

let inverse_projection = camera.projection_matrix().inverse();
let inverse_projection = camera.projection_matrix_unchecked().inverse();

for lights in &mut clusters.lights {
lights.entities.clear();
Expand Down Expand Up @@ -1443,7 +1444,7 @@ pub(crate) fn assign_lights_to_clusters(
cluster_space_light_aabb(
inverse_view_transform,
view_inv_scale,
camera.projection_matrix(),
camera.projection_matrix_unchecked(),
&light_sphere,
);

Expand Down Expand Up @@ -1486,7 +1487,7 @@ pub(crate) fn assign_lights_to_clusters(
)
});
let light_center_clip =
camera.projection_matrix() * view_light_sphere.center.extend(1.0);
camera.projection_matrix_unchecked() * view_light_sphere.center.extend(1.0);
let light_center_ndc = light_center_clip.xyz() / light_center_clip.w;
let cluster_coordinates = ndc_position_to_cluster(
clusters.dimensions,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/prepass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ pub fn update_previous_view_data(
let inverse_view = camera_transform.compute_matrix().inverse();
commands.entity(entity).try_insert(PreviousViewData {
inverse_view,
view_proj: camera.projection_matrix() * inverse_view,
view_proj: camera.projection_matrix_unchecked() * inverse_view,
});
}
}
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,12 +349,12 @@ impl Camera {

/// The projection matrix computed using this camera's [`CameraProjection`].
#[inline]
pub fn projection_matrix(&self) -> Mat4 {
pub fn projection_matrix_unchecked(&self) -> Mat4 {
self.computed.projection_matrix.unwrap_or_default()
}

#[inline]
fn checked_projection_matrix(&self) -> Result<Mat4, BadProjectionMatrixError> {
fn projection_matrix(&self) -> Result<Mat4, BadProjectionMatrixError> {
let projection_matrix = self
.computed
.projection_matrix
Expand Down Expand Up @@ -443,7 +443,7 @@ impl Camera {
let camera_transform_matrix = Self::finite_camera_transform_matrix(camera_transform)
.map_err(ViewportToWorldError::CameraTransformNotFinite)?;
let projection_matrix = self
.checked_projection_matrix()
.projection_matrix()
.map_err(ViewportToWorldError::ProjectionMatrixNotFinite)?;

let ndc_to_world = camera_transform_matrix * projection_matrix.inverse();
Expand Down Expand Up @@ -505,7 +505,7 @@ impl Camera {
.map_err(WorldToNdcError::CameraTransformNotFinite)?;

let projection_matrix = self
.checked_projection_matrix()
.projection_matrix()
.map_err(WorldToNdcError::ProjectionMatrixNotFinite)?;

// Build a transformation matrix to convert from world space to NDC using camera data
Expand Down Expand Up @@ -537,7 +537,7 @@ impl Camera {
.map_err(NdcToWorldError::CameraTransformNotFinite)?;

let projection_matrix = self
.checked_projection_matrix()
.projection_matrix()
.map_err(NdcToWorldError::ProjectionMatrixNotFinite)?;

// Build a transformation matrix to convert from NDC to world space using camera data
Expand Down Expand Up @@ -1037,7 +1037,7 @@ pub fn extract_cameras(
.unwrap_or_else(|| Exposure::default().exposure()),
},
ExtractedView {
projection: camera.projection_matrix(),
projection: camera.projection_matrix_unchecked(),
transform: *transform,
view_projection: None,
hdr: camera.hdr,
Expand Down

0 comments on commit d147b0a

Please sign in to comment.