Skip to content

Commit 0497911

Browse files
AronDerenyiexjam
authored andcommitted
Fixing confusing near and far fields in Camera (bevyengine#4457)
# Objective - Fixes bevyengine#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`
1 parent 57e4f0e commit 0497911

File tree

10 files changed

+14
-53
lines changed

10 files changed

+14
-53
lines changed

assets/shaders/custom_material.vert

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ layout(set = 0, binding = 0) uniform CameraViewProj {
1010
mat4 InverseView;
1111
mat4 Projection;
1212
vec3 WorldPosition;
13-
float near;
14-
float far;
1513
float width;
1614
float height;
1715
};

crates/bevy_gltf/src/loader.rs

-4
Original file line numberDiff line numberDiff line change
@@ -746,8 +746,6 @@ fn load_node(
746746

747747
node.insert(Camera {
748748
projection_matrix: orthographic_projection.get_projection_matrix(),
749-
near: orthographic_projection.near,
750-
far: orthographic_projection.far,
751749
..Default::default()
752750
});
753751
node.insert(orthographic_projection);
@@ -767,8 +765,6 @@ fn load_node(
767765
}
768766
node.insert(Camera {
769767
projection_matrix: perspective_projection.get_projection_matrix(),
770-
near: perspective_projection.near,
771-
far: perspective_projection.far,
772768
..Default::default()
773769
});
774770
node.insert(perspective_projection);

crates/bevy_pbr/src/light.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ pub enum SimulationLightSystems {
217217
/// rendering
218218
#[derive(Debug, Copy, Clone)]
219219
pub enum ClusterFarZMode {
220-
/// Use the camera far-plane to determine the z-depth of the furthest cluster layer
221-
CameraFarPlane,
222220
/// Calculate the required maximum z-depth based on currently visible lights.
223221
/// Makes better use of available clusters, speeding up GPU lighting operations
224222
/// 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(
758756
let is_orthographic = camera.projection_matrix.w_axis.w == 1.0;
759757

760758
let far_z = match config.far_z_mode() {
761-
ClusterFarZMode::CameraFarPlane => camera.far,
762759
ClusterFarZMode::MaxLightRange => {
763760
let inverse_view_row_2 = inverse_view_transform.row(2);
764761
lights
@@ -772,7 +769,17 @@ pub(crate) fn assign_lights_to_clusters(
772769
ClusterFarZMode::Constant(far) => far,
773770
};
774771
let first_slice_depth = match (is_orthographic, requested_cluster_dimensions.z) {
775-
(true, _) => camera.near,
772+
(true, _) => {
773+
// NOTE: Based on glam's Mat4::orthographic_rh(), as used to calculate the orthographic projection
774+
// matrix, we can calculate the projection's view-space near plane as follows:
775+
// component 3,2 = r * near and 2,2 = r where r = 1.0 / (near - far)
776+
// There is a caveat here that when calculating the projection matrix, near and far were swapped to give
777+
// reversed z, consistent with the perspective projection. So,
778+
// 3,2 = r * far and 2,2 = r where r = 1.0 / (far - near)
779+
// rearranging r = 1.0 / (far - near), r * (far - near) = 1.0, r * far - 1.0 = r * near, near = (r * far - 1.0) / r
780+
// = (3,2 - 1.0) / 2,2
781+
(camera.projection_matrix.w_axis.z - 1.0) / camera.projection_matrix.z_axis.z
782+
}
776783
(false, 1) => config.first_slice_depth().max(far_z),
777784
_ => config.first_slice_depth(),
778785
};

crates/bevy_pbr/src/render/light.rs

-8
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ pub struct ExtractedDirectionalLight {
7474
shadows_enabled: bool,
7575
shadow_depth_bias: f32,
7676
shadow_normal_bias: f32,
77-
near: f32,
78-
far: f32,
7977
}
8078

8179
pub type ExtractedDirectionalLightShadowMap = DirectionalLightShadowMap;
@@ -513,8 +511,6 @@ pub fn extract_lights(
513511
shadow_normal_bias: directional_light.shadow_normal_bias
514512
* directional_light_texel_size
515513
* std::f32::consts::SQRT_2,
516-
near: directional_light.shadow_projection.near,
517-
far: directional_light.shadow_projection.far,
518514
},
519515
render_visible_entities,
520516
));
@@ -861,8 +857,6 @@ pub fn prepare_lights(
861857
height: point_light_shadow_map.size as u32,
862858
transform: view_translation * *view_rotation,
863859
projection: cube_face_projection,
864-
near: POINT_LIGHT_NEAR_Z,
865-
far: light.range,
866860
},
867861
RenderPhase::<Shadow>::default(),
868862
LightEntity::Point {
@@ -946,8 +940,6 @@ pub fn prepare_lights(
946940
height: directional_light_shadow_map.size as u32,
947941
transform: GlobalTransform::from_matrix(view.inverse()),
948942
projection,
949-
near: light.near,
950-
far: light.far,
951943
},
952944
RenderPhase::<Shadow>::default(),
953945
LightEntity::Directional { light_entity },

crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ struct View {
66
inverse_view: mat4x4<f32>;
77
projection: mat4x4<f32>;
88
world_position: vec3<f32>;
9-
near: f32;
10-
far: f32;
119
width: f32;
1210
height: f32;
1311
};

crates/bevy_render/src/camera/bundle.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ impl<M: Component + Default> PerspectiveCameraBundle<M> {
5555
perspective_projection.far(),
5656
);
5757
PerspectiveCameraBundle {
58-
camera: Camera {
59-
near: perspective_projection.near,
60-
far: perspective_projection.far,
61-
..Default::default()
62-
},
58+
camera: Camera::default(),
6359
perspective_projection,
6460
visible_entities: VisibleEntities::default(),
6561
frustum,
@@ -99,11 +95,7 @@ impl OrthographicCameraBundle<Camera3d> {
9995
orthographic_projection.far(),
10096
);
10197
OrthographicCameraBundle {
102-
camera: Camera {
103-
near: orthographic_projection.near,
104-
far: orthographic_projection.far,
105-
..Default::default()
106-
},
98+
camera: Camera::default(),
10799
orthographic_projection,
108100
visible_entities: VisibleEntities::default(),
109101
frustum,
@@ -160,11 +152,7 @@ impl OrthographicCameraBundle<Camera2d> {
160152
orthographic_projection.far(),
161153
);
162154
OrthographicCameraBundle {
163-
camera: Camera {
164-
near: orthographic_projection.near,
165-
far: orthographic_projection.far,
166-
..Default::default()
167-
},
155+
camera: Camera::default(),
168156
orthographic_projection,
169157
visible_entities: VisibleEntities::default(),
170158
frustum,

crates/bevy_render/src/camera/camera.rs

-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ pub struct Camera {
3636
pub target: RenderTarget,
3737
#[reflect(ignore)]
3838
pub depth_calculation: DepthCalculation,
39-
pub near: f32,
40-
pub far: f32,
4139
}
4240

4341
#[derive(Debug, Clone, Reflect, PartialEq, Eq, Hash)]
@@ -333,8 +331,6 @@ pub fn extract_cameras<M: Component + Default>(
333331
transform: *transform,
334332
width: size.x,
335333
height: size.y,
336-
near: camera.near,
337-
far: camera.far,
338334
},
339335
visible_entities.clone(),
340336
M::default(),

crates/bevy_render/src/view/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ pub struct ExtractedView {
8181
pub transform: GlobalTransform,
8282
pub width: u32,
8383
pub height: u32,
84-
pub near: f32,
85-
pub far: f32,
8684
}
8785

8886
#[derive(Clone, AsStd140)]
@@ -92,8 +90,6 @@ pub struct ViewUniform {
9290
inverse_view: Mat4,
9391
projection: Mat4,
9492
world_position: Vec3,
95-
near: f32,
96-
far: f32,
9793
width: f32,
9894
height: f32,
9995
}
@@ -157,8 +153,6 @@ fn prepare_view_uniforms(
157153
inverse_view,
158154
projection,
159155
world_position: camera.transform.translation,
160-
near: camera.near,
161-
far: camera.far,
162156
width: camera.width as f32,
163157
height: camera.height as f32,
164158
}),

crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ struct View {
66
inverse_view: mat4x4<f32>;
77
projection: mat4x4<f32>;
88
world_position: vec3<f32>;
9-
near: f32;
10-
far: f32;
119
width: f32;
1210
height: f32;
1311
};

examples/tools/scene_viewer.rs

-6
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,7 @@ fn camera_spawn_check(
293293
&transform.back(),
294294
perspective_projection.far(),
295295
);
296-
let camera = Camera {
297-
near: perspective_projection.near,
298-
far: perspective_projection.far,
299-
..default()
300-
};
301296
PerspectiveCameraBundle {
302-
camera,
303297
perspective_projection,
304298
frustum,
305299
transform,

0 commit comments

Comments
 (0)