2023-11-20 09:47:50 +00:00
|
|
|
//! Additional [`Gizmos`] Functions -- Circles
|
|
|
|
//!
|
|
|
|
//! Includes the implementation of [`Gizmos::circle`] and [`Gizmos::circle_2d`],
|
|
|
|
//! and assorted support items.
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
use crate::prelude::{GizmoConfigGroup, Gizmos};
|
2024-01-21 18:03:26 +00:00
|
|
|
use bevy_math::{primitives::Direction3d, Quat, Vec2, Vec3};
|
2023-11-20 09:47:50 +00:00
|
|
|
use bevy_render::color::Color;
|
|
|
|
use std::f32::consts::TAU;
|
|
|
|
|
|
|
|
pub(crate) const DEFAULT_CIRCLE_SEGMENTS: usize = 32;
|
|
|
|
|
|
|
|
fn circle_inner(radius: f32, segments: usize) -> impl Iterator<Item = Vec2> {
|
|
|
|
(0..segments + 1).map(move |i| {
|
|
|
|
let angle = i as f32 * TAU / segments as f32;
|
|
|
|
Vec2::from(angle.sin_cos()) * radius
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<'w, 's, T: GizmoConfigGroup> Gizmos<'w, 's, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
/// Draw a circle in 3D at `position` with the flat side facing `normal`.
|
|
|
|
///
|
|
|
|
/// This should be called for each frame the circle needs to be rendered.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_gizmos::prelude::*;
|
|
|
|
/// # use bevy_render::prelude::*;
|
|
|
|
/// # use bevy_math::prelude::*;
|
|
|
|
/// fn system(mut gizmos: Gizmos) {
|
2024-01-21 18:03:26 +00:00
|
|
|
/// gizmos.circle(Vec3::ZERO, Direction3d::Z, 1., Color::GREEN);
|
2023-11-20 09:47:50 +00:00
|
|
|
///
|
|
|
|
/// // Circles have 32 line-segments by default.
|
|
|
|
/// // You may want to increase this for larger circles.
|
|
|
|
/// gizmos
|
2024-01-21 18:03:26 +00:00
|
|
|
/// .circle(Vec3::ZERO, Direction3d::Z, 5., Color::RED)
|
2023-11-20 09:47:50 +00:00
|
|
|
/// .segments(64);
|
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn circle(
|
|
|
|
&mut self,
|
|
|
|
position: Vec3,
|
2024-01-21 18:03:26 +00:00
|
|
|
normal: Direction3d,
|
2023-11-20 09:47:50 +00:00
|
|
|
radius: f32,
|
|
|
|
color: Color,
|
2024-01-18 15:52:50 +00:00
|
|
|
) -> CircleBuilder<'_, 'w, 's, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
CircleBuilder {
|
|
|
|
gizmos: self,
|
|
|
|
position,
|
|
|
|
normal,
|
|
|
|
radius,
|
|
|
|
color,
|
|
|
|
segments: DEFAULT_CIRCLE_SEGMENTS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Draw a circle in 2D.
|
|
|
|
///
|
|
|
|
/// This should be called for each frame the circle needs to be rendered.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// ```
|
|
|
|
/// # use bevy_gizmos::prelude::*;
|
|
|
|
/// # use bevy_render::prelude::*;
|
|
|
|
/// # use bevy_math::prelude::*;
|
|
|
|
/// fn system(mut gizmos: Gizmos) {
|
|
|
|
/// gizmos.circle_2d(Vec2::ZERO, 1., Color::GREEN);
|
|
|
|
///
|
|
|
|
/// // Circles have 32 line-segments by default.
|
|
|
|
/// // You may want to increase this for larger circles.
|
|
|
|
/// gizmos
|
|
|
|
/// .circle_2d(Vec2::ZERO, 5., Color::RED)
|
|
|
|
/// .segments(64);
|
|
|
|
/// }
|
|
|
|
/// # bevy_ecs::system::assert_is_system(system);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
pub fn circle_2d(
|
|
|
|
&mut self,
|
|
|
|
position: Vec2,
|
|
|
|
radius: f32,
|
|
|
|
color: Color,
|
2024-01-18 15:52:50 +00:00
|
|
|
) -> Circle2dBuilder<'_, 'w, 's, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
Circle2dBuilder {
|
|
|
|
gizmos: self,
|
|
|
|
position,
|
|
|
|
radius,
|
|
|
|
color,
|
|
|
|
segments: DEFAULT_CIRCLE_SEGMENTS,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A builder returned by [`Gizmos::circle`].
|
2024-01-18 15:52:50 +00:00
|
|
|
pub struct CircleBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
|
|
|
|
gizmos: &'a mut Gizmos<'w, 's, T>,
|
2023-11-20 09:47:50 +00:00
|
|
|
position: Vec3,
|
2024-01-21 18:03:26 +00:00
|
|
|
normal: Direction3d,
|
2023-11-20 09:47:50 +00:00
|
|
|
radius: f32,
|
|
|
|
color: Color,
|
|
|
|
segments: usize,
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> CircleBuilder<'_, '_, '_, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
/// Set the number of line-segments for this circle.
|
|
|
|
pub fn segments(mut self, segments: usize) -> Self {
|
|
|
|
self.segments = segments;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> Drop for CircleBuilder<'_, '_, '_, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
fn drop(&mut self) {
|
2024-01-18 15:52:50 +00:00
|
|
|
if !self.gizmos.enabled {
|
|
|
|
return;
|
|
|
|
}
|
2024-01-21 18:03:26 +00:00
|
|
|
let rotation = Quat::from_rotation_arc(Vec3::Z, *self.normal);
|
2023-11-20 09:47:50 +00:00
|
|
|
let positions = circle_inner(self.radius, self.segments)
|
2023-12-24 17:43:01 +00:00
|
|
|
.map(|vec2| self.position + rotation * vec2.extend(0.));
|
2023-11-20 09:47:50 +00:00
|
|
|
self.gizmos.linestrip(positions, self.color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A builder returned by [`Gizmos::circle_2d`].
|
2024-01-18 15:52:50 +00:00
|
|
|
pub struct Circle2dBuilder<'a, 'w, 's, T: GizmoConfigGroup> {
|
|
|
|
gizmos: &'a mut Gizmos<'w, 's, T>,
|
2023-11-20 09:47:50 +00:00
|
|
|
position: Vec2,
|
|
|
|
radius: f32,
|
|
|
|
color: Color,
|
|
|
|
segments: usize,
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> Circle2dBuilder<'_, '_, '_, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
/// Set the number of line-segments for this circle.
|
|
|
|
pub fn segments(mut self, segments: usize) -> Self {
|
|
|
|
self.segments = segments;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 15:52:50 +00:00
|
|
|
impl<T: GizmoConfigGroup> Drop for Circle2dBuilder<'_, '_, '_, T> {
|
2023-11-20 09:47:50 +00:00
|
|
|
fn drop(&mut self) {
|
2024-01-18 15:52:50 +00:00
|
|
|
if !self.gizmos.enabled {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-24 17:43:01 +00:00
|
|
|
let positions = circle_inner(self.radius, self.segments).map(|vec2| vec2 + self.position);
|
2023-11-20 09:47:50 +00:00
|
|
|
self.gizmos.linestrip_2d(positions, self.color);
|
|
|
|
}
|
|
|
|
}
|