Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CameraProjection::compute_frustum #11139

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions crates/bevy_core_pipeline/src/core_2d/camera_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ impl Default for Camera2dBundle {
..Default::default()
};
let transform = Transform::default();
let view_projection =
projection.get_projection_matrix() * transform.compute_matrix().inverse();
let frustum = Frustum::from_view_projection_custom_far(
&view_projection,
&transform.translation,
&transform.back(),
projection.far(),
);
let frustum = projection.compute_frustum(&GlobalTransform::from(transform));
Self {
camera_render_graph: CameraRenderGraph::new(crate::core_2d::graph::NAME),
projection,
Expand Down Expand Up @@ -79,14 +72,7 @@ impl Camera2dBundle {
..Default::default()
};
let transform = Transform::from_xyz(0.0, 0.0, far - 0.1);
let view_projection =
projection.get_projection_matrix() * transform.compute_matrix().inverse();
let frustum = Frustum::from_view_projection_custom_far(
&view_projection,
&transform.translation,
&transform.back(),
projection.far(),
);
let frustum = projection.compute_frustum(&GlobalTransform::from(transform));
Self {
camera_render_graph: CameraRenderGraph::new(crate::core_2d::graph::NAME),
projection,
Expand Down
19 changes: 19 additions & 0 deletions crates/bevy_render/src/camera/projection.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::marker::PhantomData;

use crate::primitives::Frustum;
use bevy_app::{App, Plugin, PostStartup, PostUpdate};
use bevy_ecs::{prelude::*, reflect::ReflectComponent};
use bevy_math::{AspectRatio, Mat4, Rect, Vec2, Vec3A};
use bevy_reflect::{
std_traits::ReflectDefault, GetTypeRegistration, Reflect, ReflectDeserialize, ReflectSerialize,
};
use bevy_transform::components::GlobalTransform;
use serde::{Deserialize, Serialize};

/// Adds [`Camera`](crate::camera::Camera) driver systems for a given projection type.
Expand Down Expand Up @@ -59,6 +61,23 @@ pub trait CameraProjection {
fn update(&mut self, width: f32, height: f32);
fn far(&self) -> f32;
fn get_frustum_corners(&self, z_near: f32, z_far: f32) -> [Vec3A; 8];

// Provided.
stepancheg marked this conversation as resolved.
Show resolved Hide resolved

/// Compute camera frustum for camera with given projection and transform.
///
/// This code is called by [`update_frusta`](crate::view::visibility::update_frusta) system
/// for each camera to update its frustum.
fn compute_frustum(&self, camera_transform: &GlobalTransform) -> Frustum {
let view_projection =
self.get_projection_matrix() * camera_transform.compute_matrix().inverse();
Frustum::from_view_projection_custom_far(
&view_projection,
&camera_transform.translation(),
&camera_transform.back(),
self.far(),
)
}
}

/// A configurable [`CameraProjection`] that can select its projection type at runtime.
Expand Down
9 changes: 1 addition & 8 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,14 +280,7 @@ pub fn update_frusta<T: Component + CameraProjection + Send + Sync + 'static>(
>,
) {
for (transform, projection, mut frustum) in &mut views {
let view_projection =
projection.get_projection_matrix() * transform.compute_matrix().inverse();
*frustum = Frustum::from_view_projection_custom_far(
&view_projection,
&transform.translation(),
&transform.back(),
projection.far(),
);
*frustum = projection.compute_frustum(transform);
}
}

Expand Down