bevy_mesh/primitives/mod.rs
1//! Mesh generation for [primitive shapes](bevy_math::primitives).
2//!
3//! Primitives that support meshing implement the [`Meshable`] trait.
4//! Calling [`mesh`](Meshable::mesh) will return either a [`Mesh`] or a builder
5//! that can be used to specify shape-specific configuration for creating the [`Mesh`].
6//!
7//! ```
8//! # use bevy_asset::Assets;
9//! # use bevy_ecs::prelude::ResMut;
10//! # use bevy_math::prelude::Circle;
11//! # use bevy_mesh::*;
12//! #
13//! # fn setup(mut meshes: ResMut<Assets<Mesh>>) {
14//! // Create circle mesh with default configuration
15//! let circle = meshes.add(Circle { radius: 25.0 });
16//!
17//! // Specify number of vertices
18//! let circle = meshes.add(Circle { radius: 25.0 }.mesh().resolution(64));
19//! # }
20//! ```
21
22mod dim2;
23pub use dim2::*;
24
25mod dim3;
26pub use dim3::*;
27
28mod extrusion;
29pub use extrusion::*;
30
31use super::Mesh;
32
33/// A trait for shapes that can be turned into a [`Mesh`].
34pub trait Meshable {
35 /// The output of [`Self::mesh`]. This will be a [`MeshBuilder`] used for creating a [`Mesh`].
36 type Output: MeshBuilder;
37
38 /// Creates a [`Mesh`] for a shape.
39 fn mesh(&self) -> Self::Output;
40}
41
42/// A trait used to build [`Mesh`]es from a configuration
43pub trait MeshBuilder {
44 /// Builds a [`Mesh`] based on the configuration in `self`.
45 fn build(&self) -> Mesh;
46}
47
48impl<T: MeshBuilder> From<T> for Mesh {
49 fn from(builder: T) -> Self {
50 builder.build()
51 }
52}