Fixing confusing near and far fields in Camera (#4457)

# Objective

- Fixes #4456 

## Solution

- Removed the `near` and `far` fields from the camera and the views.

---

## Changelog

- Removed the `near` and `far` fields from the camera and the views.
- Removed the `ClusterFarZMode::CameraFarPlane` far z mode.

## Migration Guide

- Cameras no longer accept near and far values during initialization
- `ClusterFarZMode::Constant` should be used with the far value instead of `ClusterFarZMode::CameraFarPlane`
This commit is contained in:
Aron Derenyi 2022-05-16 16:37:33 +00:00
parent 1ba7429371
commit 2e8dfc02ef
10 changed files with 14 additions and 53 deletions

View file

@ -10,8 +10,6 @@ layout(set = 0, binding = 0) uniform CameraViewProj {
mat4 InverseView;
mat4 Projection;
vec3 WorldPosition;
float near;
float far;
float width;
float height;
};

View file

@ -733,8 +733,6 @@ fn load_node(
node.insert(Camera {
projection_matrix: orthographic_projection.get_projection_matrix(),
near: orthographic_projection.near,
far: orthographic_projection.far,
..Default::default()
});
node.insert(orthographic_projection);
@ -754,8 +752,6 @@ fn load_node(
}
node.insert(Camera {
projection_matrix: perspective_projection.get_projection_matrix(),
near: perspective_projection.near,
far: perspective_projection.far,
..Default::default()
});
node.insert(perspective_projection);

View file

@ -217,8 +217,6 @@ pub enum SimulationLightSystems {
/// rendering
#[derive(Debug, Copy, Clone)]
pub enum ClusterFarZMode {
/// Use the camera far-plane to determine the z-depth of the furthest cluster layer
CameraFarPlane,
/// Calculate the required maximum z-depth based on currently visible lights.
/// Makes better use of available clusters, speeding up GPU lighting operations
/// at the expense of some CPU time and using more indices in the cluster light
@ -758,7 +756,6 @@ pub(crate) fn assign_lights_to_clusters(
let is_orthographic = camera.projection_matrix.w_axis.w == 1.0;
let far_z = match config.far_z_mode() {
ClusterFarZMode::CameraFarPlane => camera.far,
ClusterFarZMode::MaxLightRange => {
let inverse_view_row_2 = inverse_view_transform.row(2);
lights
@ -772,7 +769,17 @@ pub(crate) fn assign_lights_to_clusters(
ClusterFarZMode::Constant(far) => far,
};
let first_slice_depth = match (is_orthographic, requested_cluster_dimensions.z) {
(true, _) => camera.near,
(true, _) => {
// NOTE: Based on glam's Mat4::orthographic_rh(), as used to calculate the orthographic projection
// matrix, we can calculate the projection's view-space near plane as follows:
// component 3,2 = r * near and 2,2 = r where r = 1.0 / (near - far)
// There is a caveat here that when calculating the projection matrix, near and far were swapped to give
// reversed z, consistent with the perspective projection. So,
// 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
}
(false, 1) => config.first_slice_depth().max(far_z),
_ => config.first_slice_depth(),
};

View file

@ -74,8 +74,6 @@ pub struct ExtractedDirectionalLight {
shadows_enabled: bool,
shadow_depth_bias: f32,
shadow_normal_bias: f32,
near: f32,
far: f32,
}
pub type ExtractedDirectionalLightShadowMap = DirectionalLightShadowMap;
@ -513,8 +511,6 @@ pub fn extract_lights(
shadow_normal_bias: directional_light.shadow_normal_bias
* directional_light_texel_size
* std::f32::consts::SQRT_2,
near: directional_light.shadow_projection.near,
far: directional_light.shadow_projection.far,
},
render_visible_entities,
));
@ -861,8 +857,6 @@ pub fn prepare_lights(
height: point_light_shadow_map.size as u32,
transform: view_translation * *view_rotation,
projection: cube_face_projection,
near: POINT_LIGHT_NEAR_Z,
far: light.range,
},
RenderPhase::<Shadow>::default(),
LightEntity::Point {
@ -946,8 +940,6 @@ pub fn prepare_lights(
height: directional_light_shadow_map.size as u32,
transform: GlobalTransform::from_matrix(view.inverse()),
projection,
near: light.near,
far: light.far,
},
RenderPhase::<Shadow>::default(),
LightEntity::Directional { light_entity },

View file

@ -6,8 +6,6 @@ struct View {
inverse_view: mat4x4<f32>;
projection: mat4x4<f32>;
world_position: vec3<f32>;
near: f32;
far: f32;
width: f32;
height: f32;
};

View file

@ -55,11 +55,7 @@ impl<M: Component + Default> PerspectiveCameraBundle<M> {
perspective_projection.far(),
);
PerspectiveCameraBundle {
camera: Camera {
near: perspective_projection.near,
far: perspective_projection.far,
..Default::default()
},
camera: Camera::default(),
perspective_projection,
visible_entities: VisibleEntities::default(),
frustum,
@ -99,11 +95,7 @@ impl OrthographicCameraBundle<Camera3d> {
orthographic_projection.far(),
);
OrthographicCameraBundle {
camera: Camera {
near: orthographic_projection.near,
far: orthographic_projection.far,
..Default::default()
},
camera: Camera::default(),
orthographic_projection,
visible_entities: VisibleEntities::default(),
frustum,
@ -160,11 +152,7 @@ impl OrthographicCameraBundle<Camera2d> {
orthographic_projection.far(),
);
OrthographicCameraBundle {
camera: Camera {
near: orthographic_projection.near,
far: orthographic_projection.far,
..Default::default()
},
camera: Camera::default(),
orthographic_projection,
visible_entities: VisibleEntities::default(),
frustum,

View file

@ -36,8 +36,6 @@ pub struct Camera {
pub target: RenderTarget,
#[reflect(ignore)]
pub depth_calculation: DepthCalculation,
pub near: f32,
pub far: f32,
}
#[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash)]
@ -333,8 +331,6 @@ pub fn extract_cameras<M: Component + Default>(
transform: *transform,
width: size.x,
height: size.y,
near: camera.near,
far: camera.far,
},
visible_entities.clone(),
M::default(),

View file

@ -81,8 +81,6 @@ pub struct ExtractedView {
pub transform: GlobalTransform,
pub width: u32,
pub height: u32,
pub near: f32,
pub far: f32,
}
#[derive(Clone, AsStd140)]
@ -92,8 +90,6 @@ pub struct ViewUniform {
inverse_view: Mat4,
projection: Mat4,
world_position: Vec3,
near: f32,
far: f32,
width: f32,
height: f32,
}
@ -157,8 +153,6 @@ fn prepare_view_uniforms(
inverse_view,
projection,
world_position: camera.transform.translation,
near: camera.near,
far: camera.far,
width: camera.width as f32,
height: camera.height as f32,
}),

View file

@ -6,8 +6,6 @@ struct View {
inverse_view: mat4x4<f32>;
projection: mat4x4<f32>;
world_position: vec3<f32>;
near: f32;
far: f32;
width: f32;
height: f32;
};

View file

@ -293,13 +293,7 @@ fn camera_spawn_check(
&transform.back(),
perspective_projection.far(),
);
let camera = Camera {
near: perspective_projection.near,
far: perspective_projection.far,
..default()
};
PerspectiveCameraBundle {
camera,
perspective_projection,
frustum,
transform,