mirror of
https://github.com/bevyengine/bevy
synced 2024-12-22 11:03:06 +00:00
041731b7e0
The PR is in a reviewable state now in the sense that the basic implementations are there. There are still some ToDos that I'm aware of: - [x] docs for all the new structs and traits - [x] implement `Default` and derive other useful traits for the new structs - [x] Take a look at the notes again (Do this after a first round of reviews) - [x] Take care of the repetition in the circle drawing functions --- # Objective - TLDR: This PR enables us to quickly draw all the newly added primitives from `bevy_math` in immediate mode with gizmos - Addresses #10571 ## Solution - This implements the first design idea I had that covered everything that was mentioned in the Issue https://github.com/bevyengine/bevy/issues/10571#issuecomment-1863646197 --- ## Caveats - I added the `Primitive(2/3)d` impls for `Direction(2/3)d` to make them work with the current solution. We could impose less strict requirements for the gizmoable objects and remove the impls afterwards if the community doesn't like the current approach. --- ## Changelog - implement capabilities to draw ellipses on the gizmo in general (this was required to have some code which is able to draw the ellipse primitive) - refactored circle drawing code to use the more general ellipse drawing code to keep code duplication low - implement `Primitive2d` for `Direction2d` and impl `Primitive3d` for `Direction3d` - implement trait to draw primitives with specialized details with gizmos - `GizmoPrimitive2d` for all the 2D primitives - `GizmoPrimitive3d` for all the 3D primitives - (question while writing this: Does it actually matter if we split this in 2D and 3D? I guess it could be useful in the future if we do something based on the main rendering mode even though atm it's kinda useless) --- --------- Co-authored-by: nothendev <borodinov.ilya@gmail.com>
117 lines
3.9 KiB
Rust
117 lines
3.9 KiB
Rust
//! Additional [`Gizmos`] Functions -- Arrows
|
|
//!
|
|
//! Includes the implementation of [`Gizmos::arrow`] and [`Gizmos::arrow_2d`],
|
|
//! and assorted support items.
|
|
|
|
use crate::prelude::{GizmoConfigGroup, Gizmos};
|
|
use bevy_math::{Quat, Vec2, Vec3};
|
|
use bevy_render::color::Color;
|
|
|
|
/// A builder returned by [`Gizmos::arrow`] and [`Gizmos::arrow_2d`]
|
|
pub struct ArrowBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
|
|
gizmos: &'a mut Gizmos<'w, 's, T>,
|
|
start: Vec3,
|
|
end: Vec3,
|
|
color: Color,
|
|
tip_length: f32,
|
|
}
|
|
|
|
impl<T: GizmoConfigGroup> ArrowBuilder<'_, '_, '_, T> {
|
|
/// 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_render::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::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.tip_length = length;
|
|
}
|
|
}
|
|
|
|
impl<T: GizmoConfigGroup> Drop for ArrowBuilder<'_, '_, '_, T> {
|
|
/// Draws the arrow, by drawing lines with the stored [`Gizmos`]
|
|
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 = (self.end - self.start).normalize();
|
|
let rotation = Quat::from_rotation_arc(Vec3::X, pointing);
|
|
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 = tips.map(|v| rotation * (v.normalize() * self.tip_length) + self.end);
|
|
for v in tips {
|
|
// then actually draw the tips
|
|
self.gizmos.line(self.end, v, self.color);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
|
|
/// 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_render::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow(Vec3::ZERO, Vec3::ONE, Color::GREEN);
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
/// ```
|
|
pub fn arrow(&mut self, start: Vec3, end: Vec3, color: Color) -> ArrowBuilder<'_, 'w, 's, T> {
|
|
let length = (end - start).length();
|
|
ArrowBuilder {
|
|
gizmos: self,
|
|
start,
|
|
end,
|
|
color,
|
|
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_render::prelude::*;
|
|
/// # use bevy_math::prelude::*;
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
/// gizmos.arrow_2d(Vec2::ZERO, Vec2::X, Color::GREEN);
|
|
/// }
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
/// ```
|
|
pub fn arrow_2d(
|
|
&mut self,
|
|
start: Vec2,
|
|
end: Vec2,
|
|
color: Color,
|
|
) -> ArrowBuilder<'_, 'w, 's, T> {
|
|
self.arrow(start.extend(0.), end.extend(0.), color)
|
|
}
|
|
}
|