Add Annulus-gizmos (#13233)

# Objective

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

## Changelog

- Updated the example `math/render_primitives`
This commit is contained in:
Lynn 2024-05-06 00:23:32 +02:00 committed by GitHub
parent 423a4732c3
commit 89cd5f54f8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View file

@ -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};
@ -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>

View file

@ -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,
@ -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),
}
}
@ -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)