mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 02:53:07 +00:00
d2a07f9f72
# Objective Add a way to use the gizmo API in a retained manner, for increased performance. ## Solution - Move gizmo API from `Gizmos` to `GizmoBuffer`, ~ab~using `Deref` to keep usage the same as before. - Merge non-strip and strip variant of `LineGizmo` into one, storing the data in a `GizmoBuffer` to have the same API for retained `LineGizmo`s. ### Review guide - The meat of the changes are in `lib.rs`, `retained.rs`, `gizmos.rs`, `pipeline_3d.rs` and `pipeline_2d.rs` - The other files contain almost exclusively the churn from moving the gizmo API from `Gizmos` to `GizmoBuffer` ## Testing ### Performance Performance compared to the immediate mode API is from 65 to 80 times better for static lines. ``` 7900 XTX, 3700X 1707.9k lines/ms: gizmos_retained (21.3ms) 3488.5k lines/ms: gizmos_retained_continuous_polyline (31.3ms) 0.5k lines/ms: gizmos_retained_separate (97.7ms) 3054.9k lines/ms: bevy_polyline_retained_nan (16.8ms) 3596.3k lines/ms: bevy_polyline_retained_continuous_polyline (14.2ms) 0.6k lines/ms: bevy_polyline_retained_separate (78.9ms) 26.9k lines/ms: gizmos_immediate (14.9ms) 43.8k lines/ms: gizmos_immediate_continuous_polyline (18.3ms) ``` Looks like performance is good enough, being close to par with `bevy_polyline`. Benchmarks can be found here: This branch: https://github.com/tim-blackbird/line_racing/tree/retained-gizmos Bevy 0.14: https://github.com/DGriffin91/line_racing ## Showcase ```rust fn setup( mut commands: Commands, mut gizmo_assets: ResMut<Assets<GizmoAsset>> ) { let mut gizmo = GizmoAsset::default(); // A sphere made out of one million lines! gizmo .sphere(default(), 1., CRIMSON) .resolution(1_000_000 / 3); commands.spawn(Gizmo { handle: gizmo_assets.add(gizmo), ..default() }); } ``` ## Follow-up work - Port over to the retained rendering world proper - Calculate visibility and cull `Gizmo`s
232 lines
7.6 KiB
Rust
232 lines
7.6 KiB
Rust
//! Additional [`GizmoBuffer`] Functions -- Arrows
|
|
//!
|
|
//! Includes the implementation of [`GizmoBuffer::arrow`] and [`GizmoBuffer::arrow_2d`],
|
|
//! and assorted support items.
|
|
|
|
use crate::{gizmos::GizmoBuffer, prelude::GizmoConfigGroup};
|
|
use bevy_color::{
|
|
palettes::basic::{BLUE, GREEN, RED},
|
|
Color,
|
|
};
|
|
use bevy_math::{Quat, Vec2, Vec3, Vec3Swizzles};
|
|
use bevy_transform::TransformPoint;
|
|
|
|
/// A builder returned by [`GizmoBuffer::arrow`] and [`GizmoBuffer::arrow_2d`]
|
|
pub struct ArrowBuilder<'a, Config, Clear>
|
|
where
|
|
Config: GizmoConfigGroup,
|
|
Clear: 'static + Send + Sync,
|
|
{
|
|
gizmos: &'a mut GizmoBuffer<Config, Clear>,
|
|
start: Vec3,
|
|
end: Vec3,
|
|
color: Color,
|
|
double_ended: bool,
|
|
tip_length: f32,
|
|
}
|
|
|
|
impl<Config, Clear> ArrowBuilder<'_, Config, Clear>
|
|
where
|
|
Config: GizmoConfigGroup,
|
|
Clear: 'static + Send + Sync,
|
|
{
|
|
/// Change the length of the tips to be `length`.
|
|
/// The default tip length is [length of the arrow]/10.
|
|
///
|
|
/// # Example
|
|
/// ```
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// # use bevy_color::palettes::basic::GREEN;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN)
|
|
/// .with_tip_length(3.);
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
/// ```
|
|
#[doc(alias = "arrow_head_length")]
|
|
pub fn with_tip_length(mut self, length: f32) -> Self {
|
|
self.tip_length = length;
|
|
self
|
|
}
|
|
|
|
/// Adds another tip to the arrow, appended in the start point.
|
|
/// the default is only one tip at the end point.
|
|
pub fn with_double_end(mut self) -> Self {
|
|
self.double_ended = true;
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<Config, Clear> Drop for ArrowBuilder<'_, Config, Clear>
|
|
where
|
|
Config: GizmoConfigGroup,
|
|
Clear: 'static + Send + Sync,
|
|
{
|
|
/// Draws the arrow, by drawing lines with the stored [`GizmoBuffer`]
|
|
fn drop(&mut self) {
|
|
if !self.gizmos.enabled {
|
|
return;
|
|
}
|
|
// first, draw the body of the arrow
|
|
self.gizmos.line(self.start, self.end, self.color);
|
|
// now the hard part is to draw the head in a sensible way
|
|
// put us in a coordinate system where the arrow is pointing towards +x and ends at the origin
|
|
let pointing_end = (self.end - self.start).normalize();
|
|
let rotation_end = Quat::from_rotation_arc(Vec3::X, pointing_end);
|
|
let tips = [
|
|
Vec3::new(-1., 1., 0.),
|
|
Vec3::new(-1., 0., 1.),
|
|
Vec3::new(-1., -1., 0.),
|
|
Vec3::new(-1., 0., -1.),
|
|
];
|
|
// - extend the vectors so their length is `tip_length`
|
|
// - rotate the world so +x is facing in the same direction as the arrow
|
|
// - translate over to the tip of the arrow
|
|
let tips_end = tips.map(|v| rotation_end * (v.normalize() * self.tip_length) + self.end);
|
|
for v in tips_end {
|
|
// then actually draw the tips
|
|
self.gizmos.line(self.end, v, self.color);
|
|
}
|
|
if self.double_ended {
|
|
let pointing_start = (self.start - self.end).normalize();
|
|
let rotation_start = Quat::from_rotation_arc(Vec3::X, pointing_start);
|
|
let tips_start =
|
|
tips.map(|v| rotation_start * (v.normalize() * self.tip_length) + self.start);
|
|
for v in tips_start {
|
|
// draw the start points tips
|
|
self.gizmos.line(self.start, v, self.color);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Config, Clear> GizmoBuffer<Config, Clear>
|
|
where
|
|
Config: GizmoConfigGroup,
|
|
Clear: 'static + Send + Sync,
|
|
{
|
|
/// Draw an arrow in 3D, from `start` to `end`. Has four tips for convenient viewing from any direction.
|
|
///
|
|
/// This should be called for each frame the arrow needs to be rendered.
|
|
///
|
|
/// # Example
|
|
/// ```
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// # use bevy_color::palettes::basic::GREEN;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, GREEN);
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
/// ```
|
|
pub fn arrow(
|
|
&mut self,
|
|
start: Vec3,
|
|
end: Vec3,
|
|
color: impl Into<Color>,
|
|
) -> ArrowBuilder<'_, Config, Clear> {
|
|
let length = (end - start).length();
|
|
ArrowBuilder {
|
|
gizmos: self,
|
|
start,
|
|
end,
|
|
color: color.into(),
|
|
double_ended: false,
|
|
tip_length: length / 10.,
|
|
}
|
|
}
|
|
|
|
/// Draw an arrow in 2D (on the xy plane), from `start` to `end`.
|
|
///
|
|
/// This should be called for each frame the arrow needs to be rendered.
|
|
///
|
|
/// # Example
|
|
/// ```
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// # use bevy_color::palettes::basic::GREEN;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, GREEN);
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
/// ```
|
|
pub fn arrow_2d(
|
|
&mut self,
|
|
start: Vec2,
|
|
end: Vec2,
|
|
color: impl Into<Color>,
|
|
) -> ArrowBuilder<'_, Config, Clear> {
|
|
self.arrow(start.extend(0.), end.extend(0.), color)
|
|
}
|
|
}
|
|
|
|
impl<Config, Clear> GizmoBuffer<Config, Clear>
|
|
where
|
|
Config: GizmoConfigGroup,
|
|
Clear: 'static + Send + Sync,
|
|
{
|
|
/// Draw a set of axes local to the given transform (`transform`), with length scaled by a factor
|
|
/// of `base_length`.
|
|
///
|
|
/// This should be called for each frame the axes need to be rendered.
|
|
///
|
|
/// # Example
|
|
/// ```
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_ecs::prelude::*;
|
|
/// # use bevy_transform::components::Transform;
|
|
/// # #[derive(Component)]
|
|
/// # struct MyComponent;
|
|
/// fn draw_axes(
|
|
/// mut gizmos: Gizmos,
|
|
/// query: Query<&Transform, With<MyComponent>>,
|
|
/// ) {
|
|
/// for &transform in &query {
|
|
/// gizmos.axes(transform, 1.);
|
|
/// }
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(draw_axes);
|
|
/// ```
|
|
pub fn axes(&mut self, transform: impl TransformPoint, base_length: f32) {
|
|
let start = transform.transform_point(Vec3::ZERO);
|
|
let end_x = transform.transform_point(base_length * Vec3::X);
|
|
let end_y = transform.transform_point(base_length * Vec3::Y);
|
|
let end_z = transform.transform_point(base_length * Vec3::Z);
|
|
|
|
self.arrow(start, end_x, RED);
|
|
self.arrow(start, end_y, GREEN);
|
|
self.arrow(start, end_z, BLUE);
|
|
}
|
|
|
|
/// Draw a set of axes local to the given transform (`transform`), with length scaled by a factor
|
|
/// of `base_length`.
|
|
///
|
|
/// This should be called for each frame the axes need to be rendered.
|
|
///
|
|
/// # Example
|
|
/// ```
|
|
/// # use bevy_gizmos::prelude::*;
|
|
/// # use bevy_ecs::prelude::*;
|
|
/// # use bevy_transform::components::Transform;
|
|
/// # #[derive(Component)]
|
|
/// # struct AxesComponent;
|
|
/// fn draw_axes_2d(
|
|
/// mut gizmos: Gizmos,
|
|
/// query: Query<&Transform, With<AxesComponent>>,
|
|
/// ) {
|
|
/// for &transform in &query {
|
|
/// gizmos.axes_2d(transform, 1.);
|
|
/// }
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(draw_axes_2d);
|
|
/// ```
|
|
pub fn axes_2d(&mut self, transform: impl TransformPoint, base_length: f32) {
|
|
let start = transform.transform_point(Vec3::ZERO);
|
|
let end_x = transform.transform_point(base_length * Vec3::X);
|
|
let end_y = transform.transform_point(base_length * Vec3::Y);
|
|
|
|
self.arrow_2d(start.xy(), end_x.xy(), RED);
|
|
self.arrow_2d(start.xy(), end_y.xy(), GREEN);
|
|
}
|
|
}
|