Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_mesh/src/primitives/mod.rs
6596 views
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
22
mod dim2;
23
pub use dim2::*;
24
25
mod dim3;
26
pub use dim3::*;
27
28
mod extrusion;
29
pub use extrusion::*;
30
31
use super::Mesh;
32
33
/// A trait for shapes that can be turned into a [`Mesh`].
34
pub 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
43
pub trait MeshBuilder {
44
/// Builds a [`Mesh`] based on the configuration in `self`.
45
fn build(&self) -> Mesh;
46
}
47
48
impl<T: MeshBuilder> From<T> for Mesh {
49
fn from(builder: T) -> Self {
50
builder.build()
51
}
52
}
53
54