bevy_mesh/
lib.rs

1#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
2
3extern crate alloc;
4extern crate core;
5
6mod components;
7mod conversions;
8mod index;
9mod mesh;
10#[cfg(feature = "bevy_mikktspace")]
11mod mikktspace;
12#[cfg(feature = "morph")]
13pub mod morph;
14pub mod primitives;
15pub mod skinning;
16mod vertex;
17use bevy_app::{App, Plugin, PostUpdate};
18use bevy_asset::{AssetApp, AssetEventSystems};
19use bevy_ecs::schedule::{IntoScheduleConfigs, SystemSet};
20use bitflags::bitflags;
21pub use components::*;
22pub use index::*;
23pub use mesh::*;
24#[cfg(feature = "bevy_mikktspace")]
25pub use mikktspace::*;
26pub use primitives::*;
27pub use vertex::*;
28pub use wgpu_types::VertexFormat;
29
30/// The mesh prelude.
31///
32/// This includes the most common types in this crate, re-exported for your convenience.
33pub mod prelude {
34    #[cfg(feature = "morph")]
35    pub use crate::morph::MorphWeights;
36    #[doc(hidden)]
37    pub use crate::{primitives::MeshBuilder, primitives::Meshable, Mesh, Mesh2d, Mesh3d};
38}
39
40bitflags! {
41    /// Our base mesh pipeline key bits start from the highest bit and go
42    /// downward. The PBR mesh pipeline key bits start from the lowest bit and
43    /// go upward. This allows the PBR bits in the downstream crate `bevy_pbr`
44    /// to coexist in the same field without any shifts.
45    #[derive(Clone, Debug)]
46    pub struct BaseMeshPipelineKey: u64 {
47        const MORPH_TARGETS = 1 << (u64::BITS - 1);
48    }
49}
50
51/// Adds [`Mesh`] as an asset.
52#[derive(Default)]
53pub struct MeshPlugin;
54
55impl Plugin for MeshPlugin {
56    fn build(&self, app: &mut App) {
57        app.init_asset::<Mesh>()
58            .init_asset::<skinning::SkinnedMeshInverseBindposes>()
59            .register_asset_reflect::<Mesh>()
60            .add_systems(
61                PostUpdate,
62                mark_3d_meshes_as_changed_if_their_assets_changed.after(AssetEventSystems),
63            );
64    }
65}
66
67impl BaseMeshPipelineKey {
68    pub const PRIMITIVE_TOPOLOGY_MASK_BITS: u64 = 0b111;
69    pub const PRIMITIVE_TOPOLOGY_SHIFT_BITS: u64 =
70        (u64::BITS - 1 - Self::PRIMITIVE_TOPOLOGY_MASK_BITS.count_ones()) as u64;
71
72    pub fn from_primitive_topology(primitive_topology: PrimitiveTopology) -> Self {
73        let primitive_topology_bits = ((primitive_topology as u64)
74            & Self::PRIMITIVE_TOPOLOGY_MASK_BITS)
75            << Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS;
76        Self::from_bits_retain(primitive_topology_bits)
77    }
78
79    pub fn primitive_topology(&self) -> PrimitiveTopology {
80        let primitive_topology_bits = (self.bits() >> Self::PRIMITIVE_TOPOLOGY_SHIFT_BITS)
81            & Self::PRIMITIVE_TOPOLOGY_MASK_BITS;
82        match primitive_topology_bits {
83            x if x == PrimitiveTopology::PointList as u64 => PrimitiveTopology::PointList,
84            x if x == PrimitiveTopology::LineList as u64 => PrimitiveTopology::LineList,
85            x if x == PrimitiveTopology::LineStrip as u64 => PrimitiveTopology::LineStrip,
86            x if x == PrimitiveTopology::TriangleList as u64 => PrimitiveTopology::TriangleList,
87            x if x == PrimitiveTopology::TriangleStrip as u64 => PrimitiveTopology::TriangleStrip,
88            _ => PrimitiveTopology::default(),
89        }
90    }
91}
92
93/// `bevy_render::mesh::inherit_weights` runs in this `SystemSet`
94#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
95pub struct InheritWeightSystems;