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.
This commit is contained in:
Olle Lukowski 2024-05-27 15:48:47 +02:00 committed by GitHub
parent 787df44288
commit cf4baf8fbf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 77 additions and 75 deletions

View File

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

View File

@ -29,7 +29,7 @@ pub trait GizmoPrimitive2d<P: Primitive2d> {
/// Renders a 2D primitive with its associated details.
fn primitive_2d(
&mut self,
primitive: P,
primitive: &P,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -47,7 +47,7 @@ where
fn primitive_2d(
&mut self,
primitive: Dir2,
primitive: &Dir2,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -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<Color>,
@ -99,7 +99,7 @@ where
fn primitive_2d(
&mut self,
primitive: Ellipse,
primitive: &Ellipse,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -123,7 +123,7 @@ where
fn primitive_2d(
&mut self,
primitive: Annulus,
primitive: &Annulus,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -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<Color>,
@ -181,7 +181,7 @@ where
fn primitive_2d(
&mut self,
primitive: Capsule2d,
primitive: &Capsule2d,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -279,7 +279,7 @@ where
fn primitive_2d(
&mut self,
primitive: Line2d,
primitive: &Line2d,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -338,7 +338,7 @@ where
fn primitive_2d(
&mut self,
primitive: Plane2d,
primitive: &Plane2d,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -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<Color>,
@ -474,7 +474,7 @@ where
fn primitive_2d(
&mut self,
primitive: Polyline2d<N>,
primitive: &Polyline2d<N>,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -505,7 +505,7 @@ where
fn primitive_2d(
&mut self,
primitive: BoxedPolyline2d,
primitive: &BoxedPolyline2d,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -536,7 +536,7 @@ where
fn primitive_2d(
&mut self,
primitive: Triangle2d,
primitive: &Triangle2d,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -561,7 +561,7 @@ where
fn primitive_2d(
&mut self,
primitive: Rectangle,
primitive: &Rectangle,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -594,7 +594,7 @@ where
fn primitive_2d(
&mut self,
primitive: Polygon<N>,
primitive: &Polygon<N>,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -635,7 +635,7 @@ where
fn primitive_2d(
&mut self,
primitive: BoxedPolygon,
primitive: &BoxedPolygon,
position: Vec2,
angle: f32,
color: impl Into<Color>,
@ -674,7 +674,7 @@ where
fn primitive_2d(
&mut self,
primitive: RegularPolygon,
primitive: &RegularPolygon,
position: Vec2,
angle: f32,
color: impl Into<Color>,

View File

@ -26,7 +26,7 @@ pub trait GizmoPrimitive3d<P: Primitive3d> {
/// Renders a 3D primitive with its associated details.
fn primitive_3d(
&mut self,
primitive: P,
primitive: &P,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -44,12 +44,12 @@ where
fn primitive_3d(
&mut self,
primitive: Dir3,
primitive: &Dir3,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
) -> 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<Color>,
@ -221,7 +221,7 @@ where
fn primitive_3d(
&mut self,
primitive: Plane3d,
primitive: &Plane3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -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<Color>,
@ -328,7 +328,7 @@ where
fn primitive_3d(
&mut self,
primitive: Segment3d,
primitive: &Segment3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -356,7 +356,7 @@ where
fn primitive_3d(
&mut self,
primitive: Polyline3d<N>,
primitive: &Polyline3d<N>,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -385,7 +385,7 @@ where
fn primitive_3d(
&mut self,
primitive: BoxedPolyline3d,
primitive: &BoxedPolyline3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -416,7 +416,7 @@ where
fn primitive_3d(
&mut self,
primitive: Triangle3d,
primitive: &Triangle3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -444,7 +444,7 @@ where
fn primitive_3d(
&mut self,
primitive: Cuboid,
primitive: &Cuboid,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -541,7 +541,7 @@ where
fn primitive_3d(
&mut self,
primitive: Cylinder,
primitive: &Cylinder,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -654,7 +654,7 @@ where
fn primitive_3d(
&mut self,
primitive: Capsule3d,
primitive: &Capsule3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -785,7 +785,7 @@ where
fn primitive_3d(
&mut self,
primitive: Cone,
primitive: &Cone,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -899,7 +899,7 @@ where
fn primitive_3d(
&mut self,
primitive: ConicalFrustum,
primitive: &ConicalFrustum,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -1027,7 +1027,7 @@ where
fn primitive_3d(
&mut self,
primitive: Torus,
primitive: &Torus,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
@ -1122,7 +1122,7 @@ impl<'w, 's, T: GizmoConfigGroup> GizmoPrimitive3d<Tetrahedron> for Gizmos<'w, '
fn primitive_3d(
&mut self,
primitive: Tetrahedron,
primitive: &Tetrahedron,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,

View File

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

View File

@ -435,24 +435,26 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, 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(&REGULAR_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<State<PrimitiveSelected>>, 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);
}
}
}