Skip to content

Commit 4618413

Browse files
Add missing primitives to the render_primitives example (#21563)
# Objective Render primitives example is missing primitives thats implements `Meshable`. ## Solution Updated `examples/math/render_primitives.rs` with new meshable primitives (`ConvexPolygon`, `Segment2d`, `Polyline2d`, `CircularSector`, `CircularSegment`, `Segment3d`, `Polyline3d`, `Cone`, `ConicalFrustum`) ## Testing `cargo run --example render_primitives` --- ## Showcase <img width="966" height="710" alt="image" src="https://github.com/user-attachments/assets/fe73dfc6-0811-49b6-a03e-92fd86317121" />
1 parent 3fc0ad0 commit 4618413

File tree

1 file changed

+59
-23
lines changed

1 file changed

+59
-23
lines changed

examples/math/render_primitives.rs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ enum PrimitiveSelected {
7474
Segment,
7575
Polyline,
7676
Polygon,
77+
ConvexPolygon,
7778
RegularPolygon,
7879
Capsule,
7980
Cylinder,
@@ -98,7 +99,7 @@ impl std::fmt::Display for PrimitiveSelected {
9899
}
99100

100101
impl PrimitiveSelected {
101-
const ALL: [Self; 19] = [
102+
const ALL: [Self; 20] = [
102103
Self::RectangleAndCuboid,
103104
Self::CircleAndSphere,
104105
Self::Ellipse,
@@ -108,6 +109,7 @@ impl PrimitiveSelected {
108109
Self::Segment,
109110
Self::Polyline,
110111
Self::Polygon,
112+
Self::ConvexPolygon,
111113
Self::RegularPolygon,
112114
Self::Capsule,
113115
Self::Cylinder,
@@ -183,8 +185,8 @@ const PLANE_3D: Plane3d = Plane3d {
183185
half_size: Vec2::new(BIG_3D, BIG_3D),
184186
};
185187

186-
const LINE2D: Line2d = Line2d { direction: Dir2::X };
187-
const LINE3D: Line3d = Line3d { direction: Dir3::X };
188+
const LINE_2D: Line2d = Line2d { direction: Dir2::X };
189+
const LINE_3D: Line3d = Line3d { direction: Dir3::X };
188190

189191
const SEGMENT_2D: Segment2d = Segment2d {
190192
vertices: [Vec2::new(-BIG_2D / 2., 0.), Vec2::new(BIG_2D / 2., 0.)],
@@ -197,6 +199,28 @@ const SEGMENT_3D: Segment3d = Segment3d {
197199
],
198200
};
199201

202+
const POLYLINE_2D_VERTICES: [Vec2; 4] = [
203+
Vec2::new(-BIG_2D, -SMALL_2D),
204+
Vec2::new(-SMALL_2D, SMALL_2D),
205+
Vec2::new(SMALL_2D, -SMALL_2D),
206+
Vec2::new(BIG_2D, SMALL_2D),
207+
];
208+
209+
const POLYLINE_3D_VERTICES: [Vec3; 4] = [
210+
Vec3::new(-BIG_3D, -SMALL_3D, -SMALL_3D),
211+
Vec3::new(SMALL_3D, SMALL_3D, 0.0),
212+
Vec3::new(-SMALL_3D, -SMALL_3D, 0.0),
213+
Vec3::new(BIG_3D, SMALL_3D, SMALL_3D),
214+
];
215+
216+
const CONVEX_POLYGON_VERTICES: [Vec2; 5] = [
217+
Vec2::new(-BIG_2D, -SMALL_2D),
218+
Vec2::new(BIG_2D, -SMALL_2D),
219+
Vec2::new(BIG_2D, SMALL_2D),
220+
Vec2::new(BIG_2D / 2.0, SMALL_2D * 2.0),
221+
Vec2::new(-BIG_2D, SMALL_2D),
222+
];
223+
200224
const REGULAR_POLYGON: RegularPolygon = RegularPolygon {
201225
circumcircle: Circle { radius: BIG_2D },
202226
sides: 5,
@@ -206,6 +230,7 @@ const CAPSULE_2D: Capsule2d = Capsule2d {
206230
radius: SMALL_2D,
207231
half_length: SMALL_2D,
208232
};
233+
209234
const CAPSULE_3D: Capsule3d = Capsule3d {
210235
radius: SMALL_3D,
211236
half_length: SMALL_3D,
@@ -421,22 +446,22 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
421446
PrimitiveSelected::Ellipse => drop(gizmos.primitive_2d(&ELLIPSE, isometry, color)),
422447
PrimitiveSelected::Triangle => gizmos.primitive_2d(&TRIANGLE_2D, isometry, color),
423448
PrimitiveSelected::Plane => gizmos.primitive_2d(&PLANE_2D, isometry, color),
424-
PrimitiveSelected::Line => drop(gizmos.primitive_2d(&LINE2D, isometry, color)),
449+
PrimitiveSelected::Line => drop(gizmos.primitive_2d(&LINE_2D, isometry, color)),
425450
PrimitiveSelected::Segment => {
426451
drop(gizmos.primitive_2d(&SEGMENT_2D, isometry, color));
427452
}
428453
PrimitiveSelected::Polyline => gizmos.primitive_2d(
429454
&Polyline2d {
430-
vertices: vec![
431-
Vec2::new(-BIG_2D, -SMALL_2D),
432-
Vec2::new(-SMALL_2D, SMALL_2D),
433-
Vec2::new(SMALL_2D, -SMALL_2D),
434-
Vec2::new(BIG_2D, SMALL_2D),
435-
],
455+
vertices: POLYLINE_2D_VERTICES.to_vec(),
436456
},
437457
isometry,
438458
color,
439459
),
460+
PrimitiveSelected::ConvexPolygon => gizmos.primitive_2d(
461+
&Polygon::from(ConvexPolygon::new(CONVEX_POLYGON_VERTICES).unwrap()),
462+
isometry,
463+
color,
464+
),
440465
PrimitiveSelected::Polygon => gizmos.primitive_2d(
441466
&Polygon {
442467
vertices: vec![
@@ -492,23 +517,31 @@ fn spawn_primitive_2d(
492517
const POSITION: Vec3 = Vec3::new(LEFT_RIGHT_OFFSET_2D, 0.0, 0.0);
493518
let material: Handle<ColorMaterial> = materials.add(Color::WHITE);
494519
let camera_mode = CameraActive::Dim2;
520+
let polyline_2d = Polyline2d {
521+
vertices: POLYLINE_2D_VERTICES.to_vec(),
522+
};
523+
let convex_polygon = ConvexPolygon::new(CONVEX_POLYGON_VERTICES).unwrap();
495524
[
496525
Some(RECTANGLE.mesh().build()),
497526
Some(CIRCLE.mesh().build()),
498527
Some(ELLIPSE.mesh().build()),
499528
Some(TRIANGLE_2D.mesh().build()),
500529
None, // plane
501530
None, // line
502-
None, // segment
503-
None, // polyline
531+
Some(SEGMENT_2D.mesh().build()),
532+
Some(polyline_2d.mesh().build()),
504533
None, // polygon
534+
Some(convex_polygon.mesh().build()),
505535
Some(REGULAR_POLYGON.mesh().build()),
506536
Some(CAPSULE_2D.mesh().build()),
507537
None, // cylinder
508538
None, // cone
509539
None, // conical frustum
510540
Some(ANNULUS.mesh().build()),
511541
None, // tetrahedron
542+
None, // arc
543+
Some(CIRCULAR_SECTOR.mesh().build()),
544+
Some(CIRCULAR_SEGMENT.mesh().build()),
512545
]
513546
.into_iter()
514547
.zip(PrimitiveSelected::ALL)
@@ -536,23 +569,30 @@ fn spawn_primitive_3d(
536569
const POSITION: Vec3 = Vec3::new(-LEFT_RIGHT_OFFSET_3D, 0.0, 0.0);
537570
let material: Handle<StandardMaterial> = materials.add(Color::WHITE);
538571
let camera_mode = CameraActive::Dim3;
572+
let polyline_3d = Polyline3d {
573+
vertices: POLYLINE_3D_VERTICES.to_vec(),
574+
};
539575
[
540576
Some(CUBOID.mesh().build()),
541577
Some(SPHERE.mesh().build()),
542578
None, // ellipse
543579
Some(TRIANGLE_3D.mesh().build()),
544580
Some(PLANE_3D.mesh().build()),
545581
None, // line
546-
None, // segment
547-
None, // polyline
582+
Some(SEGMENT_3D.mesh().build()),
583+
Some(polyline_3d.mesh().build()),
548584
None, // polygon
585+
None, // convex polygon
549586
None, // regular polygon
550587
Some(CAPSULE_3D.mesh().build()),
551588
Some(CYLINDER.mesh().build()),
552-
None, // cone
553-
None, // conical frustum
589+
Some(CONE.mesh().build()),
590+
Some(CONICAL_FRUSTUM.mesh().build()),
554591
Some(TORUS.mesh().build()),
555592
Some(TETRAHEDRON.mesh().build()),
593+
None, // arc
594+
None, // circular sector
595+
None, // circular segment
556596
]
557597
.into_iter()
558598
.zip(PrimitiveSelected::ALL)
@@ -661,21 +701,17 @@ fn draw_gizmos_3d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
661701
PrimitiveSelected::Ellipse => {}
662702
PrimitiveSelected::Triangle => gizmos.primitive_3d(&TRIANGLE_3D, isometry, color),
663703
PrimitiveSelected::Plane => drop(gizmos.primitive_3d(&PLANE_3D, isometry, color)),
664-
PrimitiveSelected::Line => gizmos.primitive_3d(&LINE3D, isometry, color),
704+
PrimitiveSelected::Line => gizmos.primitive_3d(&LINE_3D, isometry, color),
665705
PrimitiveSelected::Segment => gizmos.primitive_3d(&SEGMENT_3D, isometry, color),
666706
PrimitiveSelected::Polyline => gizmos.primitive_3d(
667707
&Polyline3d {
668-
vertices: vec![
669-
Vec3::new(-BIG_3D, -SMALL_3D, -SMALL_3D),
670-
Vec3::new(SMALL_3D, SMALL_3D, 0.0),
671-
Vec3::new(-SMALL_3D, -SMALL_3D, 0.0),
672-
Vec3::new(BIG_3D, SMALL_3D, SMALL_3D),
673-
],
708+
vertices: POLYLINE_3D_VERTICES.to_vec(),
674709
},
675710
isometry,
676711
color,
677712
),
678713
PrimitiveSelected::Polygon => {}
714+
PrimitiveSelected::ConvexPolygon => {}
679715
PrimitiveSelected::RegularPolygon => {}
680716
PrimitiveSelected::Capsule => drop(
681717
gizmos

0 commit comments

Comments
 (0)