From cf4baf8fbfe5c01174e059a9c3661007afb08a9a Mon Sep 17 00:00:00 2001 From: Olle Lukowski <63189113+Olle-Lukowski@users.noreply.github.com> Date: Mon, 27 May 2024 15:48:47 +0200 Subject: [PATCH] Make gizmos take primitives by ref (#13534) # Objective Fixes #13427. ## Solution I changed the traits, and updated all usages. ## Testing The `render_primitives` example still works perfectly. --- ## Changelog - Made `gizmos.primitive_2d()` and `gizmos.primitive_3d()` take the primitives by ref. ## Migration Guide - Any usages of `gizmos.primitive_2d()` and/or `gizmos.primitive_3d()` need to be updated to pass the primitive in by reference. --- crates/bevy_gizmos/src/light.rs | 6 +-- crates/bevy_gizmos/src/primitives/dim2.rs | 44 +++++++++---------- crates/bevy_gizmos/src/primitives/dim3.rs | 38 ++++++++--------- examples/2d/bounding_2d.rs | 12 +++--- examples/math/render_primitives.rs | 52 ++++++++++++----------- 5 files changed, 77 insertions(+), 75 deletions(-) diff --git a/crates/bevy_gizmos/src/light.rs b/crates/bevy_gizmos/src/light.rs index 83829661f4..343f0bf33e 100644 --- a/crates/bevy_gizmos/src/light.rs +++ b/crates/bevy_gizmos/src/light.rs @@ -41,7 +41,7 @@ fn point_light_gizmo( let position = transform.translation(); gizmos .primitive_3d( - Sphere { + &Sphere { radius: point_light.radius, }, position, @@ -65,7 +65,7 @@ fn spot_light_gizmo( let (_, rotation, translation) = transform.to_scale_rotation_translation(); gizmos .primitive_3d( - Sphere { + &Sphere { radius: spot_light.radius, }, translation, @@ -80,7 +80,7 @@ fn spot_light_gizmo( let position = translation + rotation * Vec3::NEG_Z * height / 2.0; gizmos .primitive_3d( - Cone { + &Cone { radius: spot_light.range * angle.sin(), height, }, diff --git a/crates/bevy_gizmos/src/primitives/dim2.rs b/crates/bevy_gizmos/src/primitives/dim2.rs index 495e1cb687..651fd6cef0 100644 --- a/crates/bevy_gizmos/src/primitives/dim2.rs +++ b/crates/bevy_gizmos/src/primitives/dim2.rs @@ -29,7 +29,7 @@ pub trait GizmoPrimitive2d { /// Renders a 2D primitive with its associated details. fn primitive_2d( &mut self, - primitive: P, + primitive: &P, position: Vec2, angle: f32, color: impl Into, @@ -47,7 +47,7 @@ where fn primitive_2d( &mut self, - primitive: Dir2, + primitive: &Dir2, position: Vec2, angle: f32, color: impl Into, @@ -56,7 +56,7 @@ where return; } - let direction = Mat2::from_angle(angle) * *primitive; + let direction = Mat2::from_angle(angle) * **primitive; let start = position; let end = position + MIN_LINE_LEN * direction; @@ -75,7 +75,7 @@ where fn primitive_2d( &mut self, - primitive: Circle, + primitive: &Circle, position: Vec2, _angle: f32, color: impl Into, @@ -99,7 +99,7 @@ where fn primitive_2d( &mut self, - primitive: Ellipse, + primitive: &Ellipse, position: Vec2, angle: f32, color: impl Into, @@ -123,7 +123,7 @@ where fn primitive_2d( &mut self, - primitive: Annulus, + primitive: &Annulus, position: Vec2, angle: f32, color: impl Into, @@ -133,8 +133,8 @@ where } let color = color.into(); - self.primitive_2d(primitive.inner_circle, position, angle, color); - self.primitive_2d(primitive.outer_circle, position, angle, color); + self.primitive_2d(&primitive.inner_circle, position, angle, color); + self.primitive_2d(&primitive.outer_circle, position, angle, color); } } @@ -149,7 +149,7 @@ where fn primitive_2d( &mut self, - primitive: Rhombus, + primitive: &Rhombus, position: Vec2, angle: f32, color: impl Into, @@ -181,7 +181,7 @@ where fn primitive_2d( &mut self, - primitive: Capsule2d, + primitive: &Capsule2d, position: Vec2, angle: f32, color: impl Into, @@ -279,7 +279,7 @@ where fn primitive_2d( &mut self, - primitive: Line2d, + primitive: &Line2d, position: Vec2, angle: f32, color: impl Into, @@ -338,7 +338,7 @@ where fn primitive_2d( &mut self, - primitive: Plane2d, + primitive: &Plane2d, position: Vec2, angle: f32, color: impl Into, @@ -357,7 +357,7 @@ where half_length: HALF_MIN_LINE_LEN, }; self.primitive_2d( - normal_segment, + &normal_segment, // offset the normal so it starts on the plane line position + HALF_MIN_LINE_LEN * rotation * *normal, angle, @@ -367,7 +367,7 @@ where // draw the plane line let direction = Dir2::new_unchecked(-normal.perp()); - self.primitive_2d(Line2d { direction }, position, angle, polymorphic_color) + self.primitive_2d(&Line2d { direction }, position, angle, polymorphic_color) .draw_arrow(false); // draw an arrow such that the normal is always left side of the plane with respect to the @@ -421,7 +421,7 @@ where fn primitive_2d( &mut self, - primitive: Segment2d, + primitive: &Segment2d, position: Vec2, angle: f32, color: impl Into, @@ -474,7 +474,7 @@ where fn primitive_2d( &mut self, - primitive: Polyline2d, + primitive: &Polyline2d, position: Vec2, angle: f32, color: impl Into, @@ -505,7 +505,7 @@ where fn primitive_2d( &mut self, - primitive: BoxedPolyline2d, + primitive: &BoxedPolyline2d, position: Vec2, angle: f32, color: impl Into, @@ -536,7 +536,7 @@ where fn primitive_2d( &mut self, - primitive: Triangle2d, + primitive: &Triangle2d, position: Vec2, angle: f32, color: impl Into, @@ -561,7 +561,7 @@ where fn primitive_2d( &mut self, - primitive: Rectangle, + primitive: &Rectangle, position: Vec2, angle: f32, color: impl Into, @@ -594,7 +594,7 @@ where fn primitive_2d( &mut self, - primitive: Polygon, + primitive: &Polygon, position: Vec2, angle: f32, color: impl Into, @@ -635,7 +635,7 @@ where fn primitive_2d( &mut self, - primitive: BoxedPolygon, + primitive: &BoxedPolygon, position: Vec2, angle: f32, color: impl Into, @@ -674,7 +674,7 @@ where fn primitive_2d( &mut self, - primitive: RegularPolygon, + primitive: &RegularPolygon, position: Vec2, angle: f32, color: impl Into, diff --git a/crates/bevy_gizmos/src/primitives/dim3.rs b/crates/bevy_gizmos/src/primitives/dim3.rs index 1bf1a9d35a..ec6ab6d651 100644 --- a/crates/bevy_gizmos/src/primitives/dim3.rs +++ b/crates/bevy_gizmos/src/primitives/dim3.rs @@ -26,7 +26,7 @@ pub trait GizmoPrimitive3d { /// Renders a 3D primitive with its associated details. fn primitive_3d( &mut self, - primitive: P, + primitive: &P, position: Vec3, rotation: Quat, color: impl Into, @@ -44,12 +44,12 @@ where fn primitive_3d( &mut self, - primitive: Dir3, + primitive: &Dir3, position: Vec3, rotation: Quat, color: impl Into, ) -> Self::Output<'_> { - self.arrow(position, position + (rotation * *primitive), color); + self.arrow(position, position + (rotation * **primitive), color); } } @@ -99,7 +99,7 @@ where fn primitive_3d( &mut self, - primitive: Sphere, + primitive: &Sphere, position: Vec3, rotation: Quat, color: impl Into, @@ -221,7 +221,7 @@ where fn primitive_3d( &mut self, - primitive: Plane3d, + primitive: &Plane3d, position: Vec3, rotation: Quat, color: impl Into, @@ -252,7 +252,7 @@ where // draws the normal let normal = self.rotation * *self.normal; self.gizmos - .primitive_3d(self.normal, self.position, self.rotation, self.color); + .primitive_3d(&self.normal, self.position, self.rotation, self.color); let normals_normal = self.rotation * self.normal.any_orthonormal_vector(); // draws the axes @@ -272,7 +272,7 @@ where .take(self.segment_count) .for_each(|position| { self.gizmos.primitive_3d( - Segment3d { + &Segment3d { direction, half_length: self.segment_length * 0.5, }, @@ -296,7 +296,7 @@ where fn primitive_3d( &mut self, - primitive: Line3d, + primitive: &Line3d, position: Vec3, rotation: Quat, color: impl Into, @@ -328,7 +328,7 @@ where fn primitive_3d( &mut self, - primitive: Segment3d, + primitive: &Segment3d, position: Vec3, rotation: Quat, color: impl Into, @@ -356,7 +356,7 @@ where fn primitive_3d( &mut self, - primitive: Polyline3d, + primitive: &Polyline3d, position: Vec3, rotation: Quat, color: impl Into, @@ -385,7 +385,7 @@ where fn primitive_3d( &mut self, - primitive: BoxedPolyline3d, + primitive: &BoxedPolyline3d, position: Vec3, rotation: Quat, color: impl Into, @@ -416,7 +416,7 @@ where fn primitive_3d( &mut self, - primitive: Triangle3d, + primitive: &Triangle3d, position: Vec3, rotation: Quat, color: impl Into, @@ -444,7 +444,7 @@ where fn primitive_3d( &mut self, - primitive: Cuboid, + primitive: &Cuboid, position: Vec3, rotation: Quat, color: impl Into, @@ -541,7 +541,7 @@ where fn primitive_3d( &mut self, - primitive: Cylinder, + primitive: &Cylinder, position: Vec3, rotation: Quat, color: impl Into, @@ -654,7 +654,7 @@ where fn primitive_3d( &mut self, - primitive: Capsule3d, + primitive: &Capsule3d, position: Vec3, rotation: Quat, color: impl Into, @@ -785,7 +785,7 @@ where fn primitive_3d( &mut self, - primitive: Cone, + primitive: &Cone, position: Vec3, rotation: Quat, color: impl Into, @@ -899,7 +899,7 @@ where fn primitive_3d( &mut self, - primitive: ConicalFrustum, + primitive: &ConicalFrustum, position: Vec3, rotation: Quat, color: impl Into, @@ -1027,7 +1027,7 @@ where fn primitive_3d( &mut self, - primitive: Torus, + primitive: &Torus, position: Vec3, rotation: Quat, color: impl Into, @@ -1122,7 +1122,7 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive3d for Gizmos<'w, ' fn primitive_3d( &mut self, - primitive: Tetrahedron, + primitive: &Tetrahedron, position: Vec3, rotation: Quat, color: impl Into, diff --git a/examples/2d/bounding_2d.rs b/examples/2d/bounding_2d.rs index 57ad9738e4..0e8ee95e8e 100644 --- a/examples/2d/bounding_2d.rs +++ b/examples/2d/bounding_2d.rs @@ -103,22 +103,22 @@ fn render_shapes(mut gizmos: Gizmos, query: Query<(&Shape, &Transform)>) { let rotation = transform.rotation.to_euler(EulerRot::YXZ).2; match shape { Shape::Rectangle(r) => { - gizmos.primitive_2d(*r, translation, rotation, color); + gizmos.primitive_2d(r, translation, rotation, color); } Shape::Circle(c) => { - gizmos.primitive_2d(*c, translation, rotation, color); + gizmos.primitive_2d(c, translation, rotation, color); } Shape::Triangle(t) => { - gizmos.primitive_2d(*t, translation, rotation, color); + gizmos.primitive_2d(t, translation, rotation, color); } Shape::Line(l) => { - gizmos.primitive_2d(*l, translation, rotation, color); + gizmos.primitive_2d(l, translation, rotation, color); } Shape::Capsule(c) => { - gizmos.primitive_2d(*c, translation, rotation, color); + gizmos.primitive_2d(c, translation, rotation, color); } Shape::Polygon(p) => { - gizmos.primitive_2d(*p, translation, rotation, color); + gizmos.primitive_2d(p, translation, rotation, color); } } } diff --git a/examples/math/render_primitives.rs b/examples/math/render_primitives.rs index 44250c1cb6..8a38afb5e1 100644 --- a/examples/math/render_primitives.rs +++ b/examples/math/render_primitives.rs @@ -435,24 +435,26 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res>, time match state.get() { PrimitiveSelected::RectangleAndCuboid => { - gizmos.primitive_2d(RECTANGLE, POSITION, angle, color); + gizmos.primitive_2d(&RECTANGLE, POSITION, angle, color); } - PrimitiveSelected::CircleAndSphere => gizmos.primitive_2d(CIRCLE, POSITION, angle, color), - PrimitiveSelected::Ellipse => gizmos.primitive_2d(ELLIPSE, POSITION, angle, color), - PrimitiveSelected::Triangle => gizmos.primitive_2d(TRIANGLE_2D, POSITION, angle, color), - PrimitiveSelected::Plane => gizmos.primitive_2d(PLANE_2D, POSITION, angle, color), - PrimitiveSelected::Line => drop(gizmos.primitive_2d(LINE2D, POSITION, angle, color)), - PrimitiveSelected::Segment => drop(gizmos.primitive_2d(SEGMENT_2D, POSITION, angle, color)), - PrimitiveSelected::Polyline => gizmos.primitive_2d(POLYLINE_2D, POSITION, angle, color), - PrimitiveSelected::Polygon => gizmos.primitive_2d(POLYGON_2D, POSITION, angle, color), + PrimitiveSelected::CircleAndSphere => gizmos.primitive_2d(&CIRCLE, POSITION, angle, color), + PrimitiveSelected::Ellipse => gizmos.primitive_2d(&ELLIPSE, POSITION, angle, color), + PrimitiveSelected::Triangle => gizmos.primitive_2d(&TRIANGLE_2D, POSITION, angle, color), + PrimitiveSelected::Plane => gizmos.primitive_2d(&PLANE_2D, POSITION, angle, color), + PrimitiveSelected::Line => drop(gizmos.primitive_2d(&LINE2D, POSITION, angle, color)), + PrimitiveSelected::Segment => { + drop(gizmos.primitive_2d(&SEGMENT_2D, POSITION, angle, color)); + } + PrimitiveSelected::Polyline => gizmos.primitive_2d(&POLYLINE_2D, POSITION, angle, color), + PrimitiveSelected::Polygon => gizmos.primitive_2d(&POLYGON_2D, POSITION, angle, color), PrimitiveSelected::RegularPolygon => { - gizmos.primitive_2d(REGULAR_POLYGON, POSITION, angle, color); + gizmos.primitive_2d(®ULAR_POLYGON, POSITION, angle, color); } - PrimitiveSelected::Capsule => gizmos.primitive_2d(CAPSULE_2D, POSITION, angle, color), + PrimitiveSelected::Capsule => gizmos.primitive_2d(&CAPSULE_2D, POSITION, angle, color), PrimitiveSelected::Cylinder => {} PrimitiveSelected::Cone => {} PrimitiveSelected::ConicalFrustum => {} - PrimitiveSelected::Torus => gizmos.primitive_2d(ANNULUS, POSITION, angle, color), + PrimitiveSelected::Torus => gizmos.primitive_2d(&ANNULUS, POSITION, angle, color), PrimitiveSelected::Tetrahedron => {} } } @@ -640,48 +642,48 @@ fn draw_gizmos_3d(mut gizmos: Gizmos, state: Res>, time match state.get() { PrimitiveSelected::RectangleAndCuboid => { - gizmos.primitive_3d(CUBOID, POSITION, rotation, color); + gizmos.primitive_3d(&CUBOID, POSITION, rotation, color); } PrimitiveSelected::CircleAndSphere => drop( gizmos - .primitive_3d(SPHERE, POSITION, rotation, color) + .primitive_3d(&SPHERE, POSITION, rotation, color) .resolution(resolution), ), PrimitiveSelected::Ellipse => {} - PrimitiveSelected::Triangle => gizmos.primitive_3d(TRIANGLE_3D, POSITION, rotation, color), - PrimitiveSelected::Plane => drop(gizmos.primitive_3d(PLANE_3D, POSITION, rotation, color)), - PrimitiveSelected::Line => gizmos.primitive_3d(LINE3D, POSITION, rotation, color), - PrimitiveSelected::Segment => gizmos.primitive_3d(SEGMENT_3D, POSITION, rotation, color), - PrimitiveSelected::Polyline => gizmos.primitive_3d(POLYLINE_3D, POSITION, rotation, color), + PrimitiveSelected::Triangle => gizmos.primitive_3d(&TRIANGLE_3D, POSITION, rotation, color), + PrimitiveSelected::Plane => drop(gizmos.primitive_3d(&PLANE_3D, POSITION, rotation, color)), + PrimitiveSelected::Line => gizmos.primitive_3d(&LINE3D, POSITION, rotation, color), + PrimitiveSelected::Segment => gizmos.primitive_3d(&SEGMENT_3D, POSITION, rotation, color), + PrimitiveSelected::Polyline => gizmos.primitive_3d(&POLYLINE_3D, POSITION, rotation, color), PrimitiveSelected::Polygon => {} PrimitiveSelected::RegularPolygon => {} PrimitiveSelected::Capsule => drop( gizmos - .primitive_3d(CAPSULE_3D, POSITION, rotation, color) + .primitive_3d(&CAPSULE_3D, POSITION, rotation, color) .resolution(resolution), ), PrimitiveSelected::Cylinder => drop( gizmos - .primitive_3d(CYLINDER, POSITION, rotation, color) + .primitive_3d(&CYLINDER, POSITION, rotation, color) .resolution(resolution), ), PrimitiveSelected::Cone => drop( gizmos - .primitive_3d(CONE, POSITION, rotation, color) + .primitive_3d(&CONE, POSITION, rotation, color) .resolution(resolution), ), PrimitiveSelected::ConicalFrustum => { - gizmos.primitive_3d(CONICAL_FRUSTUM, POSITION, rotation, color); + gizmos.primitive_3d(&CONICAL_FRUSTUM, POSITION, rotation, color); } PrimitiveSelected::Torus => drop( gizmos - .primitive_3d(TORUS, POSITION, rotation, color) + .primitive_3d(&TORUS, POSITION, rotation, color) .minor_resolution(resolution) .major_resolution(resolution), ), PrimitiveSelected::Tetrahedron => { - gizmos.primitive_3d(TETRAHEDRON, POSITION, rotation, color); + gizmos.primitive_3d(&TETRAHEDRON, POSITION, rotation, color); } } }