From f9e39dbb3a8cefc063900ef33d71f1ddba958e03 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 14:59:34 +0200 Subject: [PATCH 01/10] Removed near and far from the Camera, the ExtractedView and the ViewUniform --- crates/bevy_render/src/camera/bundle.rs | 18 +++--------------- crates/bevy_render/src/camera/camera.rs | 4 ---- crates/bevy_render/src/view/mod.rs | 6 ------ 3 files changed, 3 insertions(+), 25 deletions(-) diff --git a/crates/bevy_render/src/camera/bundle.rs b/crates/bevy_render/src/camera/bundle.rs index cc185d788c625..c7cd4db6f28e0 100644 --- a/crates/bevy_render/src/camera/bundle.rs +++ b/crates/bevy_render/src/camera/bundle.rs @@ -55,11 +55,7 @@ impl PerspectiveCameraBundle { 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 { 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, @@ -151,11 +143,7 @@ impl OrthographicCameraBundle { 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, diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index ca9f69f5cf694..bcb7ba6cdba3f 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -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)] @@ -314,8 +312,6 @@ pub fn extract_cameras( transform: *transform, width: size.x.max(1), height: size.y.max(1), - near: camera.near, - far: camera.far, }, visible_entities.clone(), M::default(), diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index ba712a31943cd..e91147f63c27b 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -80,8 +80,6 @@ pub struct ExtractedView { pub transform: GlobalTransform, pub width: u32, pub height: u32, - pub near: f32, - pub far: f32, } #[derive(Clone, AsStd140)] @@ -91,8 +89,6 @@ pub struct ViewUniform { inverse_view: Mat4, projection: Mat4, world_position: Vec3, - near: f32, - far: f32, width: f32, height: f32, } @@ -156,8 +152,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, }), From 1dab4056396dd2694b330ab37b879732f85d0609 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 15:20:04 +0200 Subject: [PATCH 02/10] Fixed light ExtractedView --- crates/bevy_pbr/src/render/light.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index a1b527faa5e54..513c03f029c93 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -861,8 +861,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::::default(), LightEntity::Point { @@ -946,8 +944,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::::default(), LightEntity::Directional { light_entity }, From dd3c7747e3420a20a9da4b42530d64b9884db5ea Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 15:24:14 +0200 Subject: [PATCH 03/10] Removed ClusterFarZMode::CameraFarPlane since it can no longer get the far plane from the camera. Since the camera's far plane was an arbitrary number not derived from the projection, ClusterFarZMode::Constant can be used instead. --- crates/bevy_pbr/src/light.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 69eddb985944e..ccad757592ab4 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -213,8 +213,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 @@ -806,7 +804,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 From 2a402cb2fbb1b2285027392b512b6f8243a4e9f2 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 15:25:59 +0200 Subject: [PATCH 04/10] Removed unused fields of the ExtractedDirectionalLight --- crates/bevy_pbr/src/render/light.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 513c03f029c93..a0f7fd54f221b 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -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, )); From e3f577f94cb005f42c108118dacd693806acec65 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 15:27:29 +0200 Subject: [PATCH 05/10] Fixed Camera component initielization --- crates/bevy_gltf/src/loader.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 2b95f3e5541b4..7dc56edbd97f6 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -732,8 +732,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); @@ -753,8 +751,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); From e7eb0bee7d04e2509afb5212440a3ed2fbd346b9 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 15:28:18 +0200 Subject: [PATCH 06/10] Fixed Camera component initialization --- examples/tools/scene_viewer.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/tools/scene_viewer.rs b/examples/tools/scene_viewer.rs index bcb88251705c1..7124cdc517dd3 100644 --- a/examples/tools/scene_viewer.rs +++ b/examples/tools/scene_viewer.rs @@ -246,11 +246,7 @@ fn camera_spawn_check( info!("Spawning a 3D perspective camera"); commands.spawn_bundle(PerspectiveCameraBundle { - camera: Camera { - near: perspective_projection.near, - far: perspective_projection.far, - ..default() - }, + camera: Camera::default(), perspective_projection, frustum, transform, From 88b2f4e0a85bb6b98c73af80dea7711075663c42 Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 19:12:11 +0200 Subject: [PATCH 07/10] Removed near and far fields from the view bind groups --- crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl | 2 -- crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl | 2 -- 2 files changed, 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl b/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl index 581334676f2cd..54dd56c967a16 100644 --- a/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl +++ b/crates/bevy_pbr/src/render/mesh_view_bind_group.wgsl @@ -6,8 +6,6 @@ struct View { inverse_view: mat4x4; projection: mat4x4; world_position: vec3; - near: f32; - far: f32; width: f32; height: f32; }; diff --git a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl index 53b5513f6b311..b85d542fc0e0f 100644 --- a/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl +++ b/crates/bevy_sprite/src/mesh2d/mesh2d_view_bind_group.wgsl @@ -6,8 +6,6 @@ struct View { inverse_view: mat4x4; projection: mat4x4; world_position: vec3; - near: f32; - far: f32; width: f32; height: f32; }; From 934724cd732f331004796599adca8ca376cab25a Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Mon, 11 Apr 2022 19:17:44 +0200 Subject: [PATCH 08/10] Removed near and far values from the custom_material vertex shader --- assets/shaders/custom_material.vert | 2 -- 1 file changed, 2 deletions(-) diff --git a/assets/shaders/custom_material.vert b/assets/shaders/custom_material.vert index 56caf7719694e..f18a9fa534f0b 100644 --- a/assets/shaders/custom_material.vert +++ b/assets/shaders/custom_material.vert @@ -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; }; From 5fe54b4faaa176bda1c32d2ce5d6487df6cf251c Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Sat, 7 May 2022 10:16:24 +0200 Subject: [PATCH 09/10] Update crates/bevy_pbr/src/light.rs Fixed the near value calculation and added comments Co-authored-by: Robert Swain --- crates/bevy_pbr/src/light.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index a7911f1c048d6..524a439cd2746 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -766,8 +766,15 @@ pub(crate) fn assign_lights_to_clusters( }; let first_slice_depth = match (is_orthographic, requested_cluster_dimensions.z) { (true, _) => { - (camera.projection_matrix.w_axis.z - 1.0) - / (camera.projection_matrix.z_axis.z * 2.0) + // 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(), From b0e1168d88fe34a265020186144859cd1abf6dfa Mon Sep 17 00:00:00 2001 From: Aron Derenyi Date: Thu, 12 May 2022 14:44:16 +0200 Subject: [PATCH 10/10] Removed explicit camera construction from the scene_viewer example --- examples/tools/scene_viewer.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/tools/scene_viewer.rs b/examples/tools/scene_viewer.rs index e6141deba49b9..02ef61f59961c 100644 --- a/examples/tools/scene_viewer.rs +++ b/examples/tools/scene_viewer.rs @@ -286,9 +286,7 @@ fn camera_spawn_check( &transform.back(), perspective_projection.far(), ); - let camera = Camera::default(); PerspectiveCameraBundle { - camera, perspective_projection, frustum, transform,