Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Triangle3d / Tetrahedron to render_primitives example #13504

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion crates/bevy_gizmos/src/primitives/dim3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::f32::consts::TAU;
use bevy_color::Color;
use bevy_math::primitives::{
BoxedPolyline3d, Capsule3d, Cone, ConicalFrustum, Cuboid, Cylinder, Line3d, Plane3d,
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus,
Polyline3d, Primitive3d, Segment3d, Sphere, Tetrahedron, Torus, Triangle3d,
};
use bevy_math::{Dir3, Quat, Vec3};

Expand Down Expand Up @@ -405,6 +405,34 @@ where
}
}

// triangle 3d

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

fn primitive_3d(
&mut self,
primitive: Triangle3d,
position: Vec3,
rotation: Quat,
color: impl Into<Color>,
) -> Self::Output<'_> {
if !self.enabled {
return;
}

let [a, b, c] = primitive.vertices;
self.linestrip(
[a, b, c, a].map(rotate_then_translate_3d(rotation, position)),
color,
);
}
}

// cuboid

impl<'w, 's, Config, Clear> GizmoPrimitive3d<Cuboid> for Gizmos<'w, 's, Config, Clear>
Expand Down
43 changes: 34 additions & 9 deletions examples/math/render_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ enum PrimitiveSelected {
Cone,
ConicalFrustum,
Torus,
Tetrahedron,
}

impl std::fmt::Display for PrimitiveSelected {
Expand All @@ -98,7 +99,7 @@ impl std::fmt::Display for PrimitiveSelected {
}

impl PrimitiveSelected {
const ALL: [Self; 15] = [
const ALL: [Self; 16] = [
Self::RectangleAndCuboid,
Self::CircleAndSphere,
Self::Ellipse,
Expand All @@ -114,6 +115,7 @@ impl PrimitiveSelected {
Self::Cone,
Self::ConicalFrustum,
Self::Torus,
Self::Tetrahedron,
];

fn next(self) -> Self {
Expand Down Expand Up @@ -157,11 +159,19 @@ const ELLIPSE: Ellipse = Ellipse {
half_size: Vec2::new(BIG_2D, SMALL_2D),
};

const TRIANGLE: Triangle2d = Triangle2d {
const TRIANGLE_2D: Triangle2d = Triangle2d {
vertices: [
Vec2::new(SMALL_2D, 0.0),
Vec2::new(0.0, SMALL_2D),
Vec2::new(-SMALL_2D, 0.0),
Vec2::new(BIG_2D, 0.0),
Vec2::new(0.0, BIG_2D),
Vec2::new(-BIG_2D, 0.0),
],
};

const TRIANGLE_3D: Triangle3d = Triangle3d {
vertices: [
Vec3::new(BIG_3D, 0.0, 0.0),
Vec3::new(0.0, BIG_3D, 0.0),
Vec3::new(-BIG_3D, 0.0, 0.0),
],
};

Expand Down Expand Up @@ -250,6 +260,15 @@ const TORUS: Torus = Torus {
major_radius: SMALL_3D * 1.5,
};

const TETRAHEDRON: Tetrahedron = Tetrahedron {
vertices: [
Vec3::new(-BIG_3D, 0.0, 0.0),
Vec3::new(BIG_3D, 0.0, 0.0),
Vec3::new(0.0, 0.0, -BIG_3D * 1.67),
Vec3::new(0.0, BIG_3D * 1.67, -BIG_3D * 0.5),
],
};

fn setup_cameras(mut commands: Commands) {
let start_in_2d = true;
let make_camera = |is_active| Camera {
Expand Down Expand Up @@ -420,7 +439,7 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
}
PrimitiveSelected::CircleAndSphere => gizmos.primitive_2d(CIRCLE, POSITION, angle, color),
PrimitiveSelected::Ellipse => gizmos.primitive_2d(ELLIPSE, POSITION, angle, color),
PrimitiveSelected::Triangle => gizmos.primitive_2d(TRIANGLE, 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)),
Expand All @@ -434,6 +453,7 @@ fn draw_gizmos_2d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
PrimitiveSelected::Cone => {}
PrimitiveSelected::ConicalFrustum => {}
PrimitiveSelected::Torus => gizmos.primitive_2d(ANNULUS, POSITION, angle, color),
PrimitiveSelected::Tetrahedron => {}
}
}

Expand Down Expand Up @@ -464,7 +484,7 @@ fn spawn_primitive_2d(
Some(RECTANGLE.mesh()),
Some(CIRCLE.mesh().build()),
Some(ELLIPSE.mesh().build()),
Some(TRIANGLE.mesh()),
Some(TRIANGLE_2D.mesh()),
None, // plane
None, // line
None, // segment
Expand All @@ -476,6 +496,7 @@ fn spawn_primitive_2d(
None, // cone
None, // conical frustum
Some(ANNULUS.mesh().build()),
None, // tetrahedron
]
.into_iter()
.zip(PrimitiveSelected::ALL)
Expand Down Expand Up @@ -510,7 +531,7 @@ fn spawn_primitive_3d(
Some(CUBOID.mesh()),
Some(SPHERE.mesh().build()),
None, // ellipse
None, // triangle
Some(TRIANGLE_3D.mesh()),
Some(PLANE_3D.mesh().build()),
None, // line
None, // segment
Expand All @@ -522,6 +543,7 @@ fn spawn_primitive_3d(
None, // cone
None, // conical frustum
Some(TORUS.mesh().build()),
Some(TETRAHEDRON.mesh()),
]
.into_iter()
.zip(PrimitiveSelected::ALL)
Expand Down Expand Up @@ -626,7 +648,7 @@ fn draw_gizmos_3d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
.resolution(resolution),
),
PrimitiveSelected::Ellipse => {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like Ellipse was missed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ellipses are only missing from 3d gizmos and meshes, where the more natural thing would be ellipsoids (paralleling circle/sphere), which we don't have yet.

PrimitiveSelected::Triangle => {}
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),
Expand Down Expand Up @@ -658,5 +680,8 @@ fn draw_gizmos_3d(mut gizmos: Gizmos, state: Res<State<PrimitiveSelected>>, time
.minor_resolution(resolution)
.major_resolution(resolution),
),
PrimitiveSelected::Tetrahedron => {
gizmos.primitive_3d(TETRAHEDRON, POSITION, rotation, color);
}
}
}