Skip to content

Commit

Permalink
Add Annulus-gizmos (#13233)
Browse files Browse the repository at this point in the history
# Objective

- Add support for drawing `Annulus`-gizmos using
`gizmos.primitive_2d(...)`

## Changelog

- Updated the example `math/render_primitives`
  • Loading branch information
lynn-lumen authored May 5, 2024
1 parent 423a473 commit 89cd5f5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
28 changes: 27 additions & 1 deletion crates/bevy_gizmos/src/primitives/dim2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::helpers::*;

use bevy_color::Color;
use bevy_math::primitives::{
BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon,
Annulus, BoxedPolygon, BoxedPolyline2d, Capsule2d, Circle, Ellipse, Line2d, Plane2d, Polygon,
Polyline2d, Primitive2d, Rectangle, RegularPolygon, Segment2d, Triangle2d,
};
use bevy_math::{Dir2, Mat2, Vec2};
Expand Down Expand Up @@ -112,6 +112,32 @@ where
}
}

// annulus 2d

impl<'w, 's, Config, Clear> GizmoPrimitive2d<Annulus> for Gizmos<'w, 's, Config, Clear>
where
Config: GizmoConfigGroup,
Clear: 'static + Send + Sync,
{
type Output<'a> = () where Self: 'a;

fn primitive_2d(
&mut self,
primitive: Annulus,
position: Vec2,
angle: f32,
color: impl Into<Color>,
) -> Self::Output<'_> {
if !self.enabled {
return;
}

let color = color.into();
self.primitive_2d(primitive.inner_circle, position, angle, color);
self.primitive_2d(primitive.outer_circle, position, angle, color);
}
}

// capsule 2d

impl<'w, 's, Config, Clear> GizmoPrimitive2d<Capsule2d> for Gizmos<'w, 's, Config, Clear>
Expand Down
9 changes: 7 additions & 2 deletions examples/math/render_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ const CONICAL_FRUSTUM: ConicalFrustum = ConicalFrustum {
height: BIG_3D,
};

const ANNULUS: Annulus = Annulus {
inner_circle: Circle { radius: SMALL_2D },
outer_circle: Circle { radius: BIG_2D },
};

const TORUS: Torus = Torus {
minor_radius: SMALL_3D / 2.0,
major_radius: SMALL_3D * 1.5,
Expand Down Expand Up @@ -428,7 +433,7 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
PrimitiveSelected::Cylinder => {}
PrimitiveSelected::Cone => {}
PrimitiveSelected::ConicalFrustum => {}
PrimitiveSelected::Torus => {}
PrimitiveSelected::Torus => gizmos.primitive_2d(ANNULUS, POSITION, angle, color),
}
}

Expand Down Expand Up @@ -470,7 +475,7 @@ fn spawn_primitive_2d(
None, // cylinder
None, // cone
None, // conical frustum
None, // torus
Some(ANNULUS.mesh().build()),
]
.into_iter()
.zip(PrimitiveSelected::ALL)
Expand Down

0 comments on commit 89cd5f5

Please sign in to comment.