-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Add frustum gizmo #10038
Open
tim-blackbird
wants to merge
5
commits into
bevyengine:main
Choose a base branch
from
tim-blackbird:frustum-gizmo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add frustum gizmo #10038
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
878afe8
Add frustum gizmo
tim-blackbird 5f97b85
Use correct default_color setting
tim-blackbird 958053f
Fix doc
tim-blackbird c15c048
frustums > frusta
tim-blackbird ae5b5d4
Move `HalfSpace` intersection and `Frustum` corner positions calculat…
tim-blackbird File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
//! Module for the drawing of [`Frustum`]s. | ||
|
||
use bevy_app::{App, Plugin, PostUpdate}; | ||
use bevy_asset::{Assets, Handle}; | ||
use bevy_ecs::{ | ||
component::Component, | ||
entity::Entity, | ||
query::{Changed, Or, With, Without}, | ||
reflect::ReflectComponent, | ||
removal_detection::RemovedComponents, | ||
schedule::IntoSystemConfigs, | ||
system::{Commands, Query, Res, ResMut}, | ||
}; | ||
use bevy_math::Vec3; | ||
use bevy_reflect::{std_traits::ReflectDefault, Reflect, ReflectFromReflect}; | ||
use bevy_render::{color::Color, primitives::Frustum, view::VisibilitySystems}; | ||
|
||
use crate::{color_from_entity, GizmoConfig, LineGizmo}; | ||
|
||
/// Plugin for the drawing of [`Frustum`]s. | ||
pub struct FrustumGizmoPlugin; | ||
|
||
impl Plugin for FrustumGizmoPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems( | ||
PostUpdate, | ||
( | ||
frustum_gizmos, | ||
all_frustum_gizmos.run_if(|config: Res<GizmoConfig>| config.frustum.draw_all), | ||
remove_frustum_gizmos.run_if(|config: Res<GizmoConfig>| !config.frustum.draw_all), | ||
) | ||
.after(VisibilitySystems::UpdateOrthographicFrusta) | ||
.after(VisibilitySystems::UpdatePerspectiveFrusta) | ||
.after(VisibilitySystems::UpdateProjectionFrusta), | ||
); | ||
} | ||
} | ||
|
||
/// Configuration for drawing the [`Frustum`] component on entities. | ||
#[derive(Clone, Default)] | ||
pub struct FrustumGizmoConfig { | ||
/// Draws all frusta in the scene when set to `true`. | ||
/// | ||
/// To draw a specific entity's frustum, you can add the [`FrustumGizmo`] component. | ||
/// | ||
/// Defaults to `false`. | ||
pub draw_all: bool, | ||
/// The default color for frustum gizmos. | ||
/// | ||
/// A random color is chosen per frustum if `None`. | ||
/// | ||
/// Defaults to `None`. | ||
pub default_color: Option<Color>, | ||
} | ||
|
||
/// Add this [`Component`] to an entity to draw its [`Frustum`] component. | ||
#[derive(Component, Reflect, Default, Debug)] | ||
#[reflect(Component, FromReflect, Default)] | ||
pub struct FrustumGizmo { | ||
/// The color of the frustum. | ||
/// | ||
/// The default color from the [`GizmoConfig`] resource is used if `None`, | ||
pub color: Option<Color>, | ||
} | ||
|
||
fn frustum_gizmos( | ||
query: Query< | ||
(Entity, &Frustum, &FrustumGizmo, Option<&Handle<LineGizmo>>), | ||
Or<(Changed<Frustum>, Changed<FrustumGizmo>)>, | ||
>, | ||
config: Res<GizmoConfig>, | ||
mut commands: Commands, | ||
mut lines: ResMut<Assets<LineGizmo>>, | ||
mut removed: RemovedComponents<FrustumGizmo>, | ||
) { | ||
for entity in removed.read() { | ||
if !query.contains(entity) { | ||
commands.entity(entity).remove::<Handle<LineGizmo>>(); | ||
} | ||
} | ||
|
||
for (entity, frustum, gizmo, line_handle) in &query { | ||
let color = gizmo | ||
.color | ||
.or(config.frustum.default_color) | ||
.unwrap_or_else(|| color_from_entity(entity)); | ||
|
||
frustum_inner( | ||
&mut commands, | ||
&mut lines, | ||
entity, | ||
frustum, | ||
line_handle, | ||
color, | ||
); | ||
} | ||
} | ||
|
||
fn all_frustum_gizmos( | ||
query: Query< | ||
(Entity, &Frustum, Option<&Handle<LineGizmo>>), | ||
(Without<FrustumGizmo>, Changed<Frustum>), | ||
>, | ||
config: Res<GizmoConfig>, | ||
mut commands: Commands, | ||
mut lines: ResMut<Assets<LineGizmo>>, | ||
) { | ||
for (entity, frustum, line_handle) in &query { | ||
let color = config | ||
.frustum | ||
.default_color | ||
.unwrap_or_else(|| color_from_entity(entity)); | ||
|
||
frustum_inner( | ||
&mut commands, | ||
&mut lines, | ||
entity, | ||
frustum, | ||
line_handle, | ||
color, | ||
); | ||
} | ||
} | ||
|
||
fn frustum_inner( | ||
commands: &mut Commands, | ||
lines: &mut ResMut<Assets<LineGizmo>>, | ||
entity: Entity, | ||
frustum: &Frustum, | ||
line_handle: Option<&Handle<LineGizmo>>, | ||
color: Color, | ||
) { | ||
let Some([tln, trn, brn, bln, tlf, trf, brf, blf]) = frustum.corners() else { | ||
return; | ||
}; | ||
|
||
#[rustfmt::skip] | ||
let positions: Vec<_> = [ | ||
tln, trn, brn, bln, tln, // Near | ||
tlf, trf, brf, blf, tlf, // Far | ||
Vec3::NAN, trn, trf, // Near to far | ||
Vec3::NAN, brn, brf, | ||
Vec3::NAN, bln, blf, | ||
].into_iter().map(|v| v.to_array()).collect(); | ||
|
||
let line = LineGizmo { | ||
colors: std::iter::repeat(color.as_linear_rgba_f32()) | ||
.take(positions.len()) | ||
.collect(), | ||
positions, | ||
strip: true, | ||
}; | ||
|
||
if let Some(handle) = line_handle { | ||
lines.insert(handle, line); | ||
} else { | ||
commands.entity(entity).insert(lines.add(line)); | ||
} | ||
} | ||
|
||
fn remove_frustum_gizmos( | ||
query: Query<Entity, (With<Handle<LineGizmo>>, Without<FrustumGizmo>)>, | ||
mut commands: Commands, | ||
) { | ||
for entity in &query { | ||
commands.entity(entity).remove::<Handle<LineGizmo>>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.