-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Move Circle Gizmos to Their Own File #10631
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
Merged
mockersf
merged 10 commits into
bevyengine:main
from
SIGSTACKFAULT:gizmo-seperate-files
Nov 20, 2023
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d75266e
split circle gizmos out into their own file
SIGSTACKFAULT 89211d2
pub use DEFAULT_CIRCLE_SEGMENTS
SIGSTACKFAULT 60395b8
Revert "pub use DEFAULT_CIRCLE_SEGMENTS"
SIGSTACKFAULT 84acba4
make circle_inner private
SIGSTACKFAULT 39dc3a5
make circles and arrows public
SIGSTACKFAULT 52311a8
document ArrowBuilder
SIGSTACKFAULT d83aea1
make DEFAULT_CIRCLE_SEGMENTS pub(crate)
SIGSTACKFAULT aaae891
correct link to `Gizmos`
SIGSTACKFAULT da30530
add more documentation to circles and arrows
SIGSTACKFAULT edce806
Spell "Circles" correctly
SIGSTACKFAULT 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 hidden or 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 hidden or 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,145 @@ | ||
//! Additional [`Gizmos`] Functions -- Arrows | ||
//! | ||
//! Includes the implementation of [`Gizmos::circle`] and [`Gizmos::circle_2d`], | ||
//! and assorted support items. | ||
|
||
use crate::prelude::Gizmos; | ||
use bevy_math::{Quat, Vec2, Vec3}; | ||
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 | ||
}) | ||
} | ||
|
||
impl<'s> Gizmos<'s> { | ||
/// 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) { | ||
/// gizmos.circle(Vec3::ZERO, Vec3::Z, 1., Color::GREEN); | ||
/// | ||
/// // Circles have 32 line-segments by default. | ||
/// // You may want to increase this for larger circles. | ||
/// gizmos | ||
/// .circle(Vec3::ZERO, Vec3::Z, 5., Color::RED) | ||
/// .segments(64); | ||
/// } | ||
/// # bevy_ecs::system::assert_is_system(system); | ||
/// ``` | ||
#[inline] | ||
pub fn circle( | ||
&mut self, | ||
position: Vec3, | ||
normal: Vec3, | ||
radius: f32, | ||
color: Color, | ||
) -> CircleBuilder<'_, 's> { | ||
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, | ||
) -> Circle2dBuilder<'_, 's> { | ||
Circle2dBuilder { | ||
gizmos: self, | ||
position, | ||
radius, | ||
color, | ||
segments: DEFAULT_CIRCLE_SEGMENTS, | ||
} | ||
} | ||
} | ||
|
||
/// A builder returned by [`Gizmos::circle`]. | ||
pub struct CircleBuilder<'a, 's> { | ||
gizmos: &'a mut Gizmos<'s>, | ||
position: Vec3, | ||
normal: Vec3, | ||
radius: f32, | ||
color: Color, | ||
segments: usize, | ||
} | ||
|
||
impl CircleBuilder<'_, '_> { | ||
/// Set the number of line-segments for this circle. | ||
pub fn segments(mut self, segments: usize) -> Self { | ||
self.segments = segments; | ||
self | ||
} | ||
} | ||
|
||
impl Drop for CircleBuilder<'_, '_> { | ||
fn drop(&mut self) { | ||
let rotation = Quat::from_rotation_arc(Vec3::Z, self.normal); | ||
let positions = circle_inner(self.radius, self.segments) | ||
.map(|vec2| (self.position + rotation * vec2.extend(0.))); | ||
self.gizmos.linestrip(positions, self.color); | ||
} | ||
} | ||
|
||
/// A builder returned by [`Gizmos::circle_2d`]. | ||
pub struct Circle2dBuilder<'a, 's> { | ||
gizmos: &'a mut Gizmos<'s>, | ||
position: Vec2, | ||
radius: f32, | ||
color: Color, | ||
segments: usize, | ||
} | ||
|
||
impl Circle2dBuilder<'_, '_> { | ||
/// Set the number of line-segments for this circle. | ||
pub fn segments(mut self, segments: usize) -> Self { | ||
self.segments = segments; | ||
self | ||
} | ||
} | ||
|
||
impl Drop for Circle2dBuilder<'_, '_> { | ||
fn drop(&mut self) { | ||
let positions = circle_inner(self.radius, self.segments).map(|vec2| (vec2 + self.position)); | ||
self.gizmos.linestrip_2d(positions, self.color); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.