Skip to main content

bevy_gltf/
label.rs

1//! Labels that can be used to load part of a glTF
2
3use bevy_asset::AssetPath;
4
5/// Labels that can be used to load part of a glTF
6///
7/// You can use [`GltfAssetLabel::from_asset`] to add it to an asset path
8///
9/// ```
10/// # use bevy_ecs::prelude::*;
11/// # use bevy_asset::prelude::*;
12/// # use bevy_world_serialization::prelude::*;
13/// # use bevy_gltf::prelude::*;
14///
15/// fn load_gltf_scene(asset_server: Res<AssetServer>) {
16///     let gltf_scene: Handle<WorldAsset> = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf"));
17/// }
18/// ```
19///
20/// Or when formatting a string for the path
21///
22/// ```
23/// # use bevy_ecs::prelude::*;
24/// # use bevy_asset::prelude::*;
25/// # use bevy_world_serialization::prelude::*;
26/// # use bevy_gltf::prelude::*;
27///
28/// fn load_gltf_scene(asset_server: Res<AssetServer>) {
29///     let gltf_scene: Handle<WorldAsset> = asset_server.load(format!("models/FlightHelmet/FlightHelmet.gltf#{}", GltfAssetLabel::Scene(0)));
30/// }
31/// ```
32#[derive(#[automatically_derived]
impl ::core::fmt::Debug for GltfAssetLabel {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            GltfAssetLabel::Scene(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Scene",
                    &__self_0),
            GltfAssetLabel::Node(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Node",
                    &__self_0),
            GltfAssetLabel::Mesh(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Mesh",
                    &__self_0),
            GltfAssetLabel::Primitive { mesh: __self_0, primitive: __self_1 }
                =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Primitive", "mesh", __self_0, "primitive", &__self_1),
            GltfAssetLabel::Texture(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Texture", &__self_0),
            GltfAssetLabel::Material {
                index: __self_0, is_scale_inverted: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "Material", "index", __self_0, "is_scale_inverted",
                    &__self_1),
            GltfAssetLabel::DefaultMaterial =>
                ::core::fmt::Formatter::write_str(f, "DefaultMaterial"),
            GltfAssetLabel::Animation(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Animation", &__self_0),
            GltfAssetLabel::Skin(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Skin",
                    &__self_0),
            GltfAssetLabel::InverseBindMatrices(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "InverseBindMatrices", &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for GltfAssetLabel {
    #[inline]
    fn clone(&self) -> GltfAssetLabel {
        let _: ::core::clone::AssertParamIsClone<usize>;
        let _: ::core::clone::AssertParamIsClone<bool>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for GltfAssetLabel { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for GltfAssetLabel {
    #[inline]
    fn eq(&self, other: &GltfAssetLabel) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (GltfAssetLabel::Scene(__self_0),
                    GltfAssetLabel::Scene(__arg1_0)) => __self_0 == __arg1_0,
                (GltfAssetLabel::Node(__self_0),
                    GltfAssetLabel::Node(__arg1_0)) => __self_0 == __arg1_0,
                (GltfAssetLabel::Mesh(__self_0),
                    GltfAssetLabel::Mesh(__arg1_0)) => __self_0 == __arg1_0,
                (GltfAssetLabel::Primitive {
                    mesh: __self_0, primitive: __self_1 },
                    GltfAssetLabel::Primitive {
                    mesh: __arg1_0, primitive: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                (GltfAssetLabel::Texture(__self_0),
                    GltfAssetLabel::Texture(__arg1_0)) => __self_0 == __arg1_0,
                (GltfAssetLabel::Material {
                    index: __self_0, is_scale_inverted: __self_1 },
                    GltfAssetLabel::Material {
                    index: __arg1_0, is_scale_inverted: __arg1_1 }) =>
                    __self_1 == __arg1_1 && __self_0 == __arg1_0,
                (GltfAssetLabel::Animation(__self_0),
                    GltfAssetLabel::Animation(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (GltfAssetLabel::Skin(__self_0),
                    GltfAssetLabel::Skin(__arg1_0)) => __self_0 == __arg1_0,
                (GltfAssetLabel::InverseBindMatrices(__self_0),
                    GltfAssetLabel::InverseBindMatrices(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for GltfAssetLabel {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<usize>;
        let _: ::core::cmp::AssertParamIsEq<bool>;
    }
}Eq)]
33pub enum GltfAssetLabel {
34    /// `Scene{}`: glTF Scene as a Bevy [`WorldAsset`](bevy_world_serialization::WorldAsset)
35    Scene(usize),
36    /// `Node{}`: glTF Node as a [`GltfNode`](crate::GltfNode)
37    Node(usize),
38    /// `Mesh{}`: glTF Mesh as a [`GltfMesh`](crate::GltfMesh)
39    Mesh(usize),
40    /// `Mesh{}/Primitive{}`: glTF Primitive as a Bevy [`Mesh`](bevy_mesh::Mesh)
41    Primitive {
42        /// Index of the mesh for this primitive
43        mesh: usize,
44        /// Index of this primitive in its parent mesh
45        primitive: usize,
46    },
47    /// `Texture{}`: glTF Texture as a Bevy [`Image`](bevy_image::prelude::Image)
48    Texture(usize),
49    /// `Material{}`: glTF Material as Bevy [`GltfMaterial`](crate::GltfMaterial)
50    Material {
51        /// Index of this material
52        index: usize,
53        /// Used to set the [`Face`](wgpu_types::Face) of the material,
54        /// useful if it is used with negative scale
55        is_scale_inverted: bool,
56    },
57    /// `DefaultMaterial`: glTF's default Material
58    DefaultMaterial,
59    /// `Animation{}`: glTF Animation as Bevy [`AnimationClip`](bevy_animation::AnimationClip)
60    Animation(usize),
61    /// `Skin{}`: glTF mesh skin as [`GltfSkin`](crate::GltfSkin)
62    Skin(usize),
63    /// `Skin{}/InverseBindMatrices`: glTF mesh skin matrices as Bevy
64    /// [`SkinnedMeshInverseBindposes`](bevy_mesh::skinning::SkinnedMeshInverseBindposes)
65    InverseBindMatrices(usize),
66}
67
68impl core::fmt::Display for GltfAssetLabel {
69    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
70        match self {
71            GltfAssetLabel::Scene(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Scene{0}", index))
    })format!("Scene{index}")),
72            GltfAssetLabel::Node(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Node{0}", index))
    })format!("Node{index}")),
73            GltfAssetLabel::Mesh(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Mesh{0}", index))
    })format!("Mesh{index}")),
74            GltfAssetLabel::Primitive { mesh, primitive } => {
75                f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Mesh{0}/Primitive{1}", mesh,
                primitive))
    })format!("Mesh{mesh}/Primitive{primitive}"))
76            }
77            GltfAssetLabel::Texture(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Texture{0}", index))
    })format!("Texture{index}")),
78            GltfAssetLabel::Material {
79                index,
80                is_scale_inverted,
81            } => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Material{1}{0}",
                if *is_scale_inverted { " (inverted)" } else { "" }, index))
    })format!(
82                "Material{index}{}",
83                if *is_scale_inverted {
84                    " (inverted)"
85                } else {
86                    ""
87                }
88            )),
89            GltfAssetLabel::DefaultMaterial => f.write_str("DefaultMaterial"),
90            GltfAssetLabel::Animation(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Animation{0}", index))
    })format!("Animation{index}")),
91            GltfAssetLabel::Skin(index) => f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Skin{0}", index))
    })format!("Skin{index}")),
92            GltfAssetLabel::InverseBindMatrices(index) => {
93                f.write_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("Skin{0}/InverseBindMatrices",
                index))
    })format!("Skin{index}/InverseBindMatrices"))
94            }
95        }
96    }
97}
98
99impl GltfAssetLabel {
100    /// Add this label to an asset path
101    ///
102    /// ```
103    /// # use bevy_ecs::prelude::*;
104    /// # use bevy_asset::prelude::*;
105    /// # use bevy_world_serialization::prelude::*;
106    /// # use bevy_gltf::prelude::*;
107    ///
108    /// fn load_gltf_scene(asset_server: Res<AssetServer>) {
109    ///     let gltf_scene: Handle<WorldAsset> = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf"));
110    /// }
111    /// ```
112    pub fn from_asset(&self, path: impl Into<AssetPath<'static>>) -> AssetPath<'static> {
113        path.into().with_label(self.to_string())
114    }
115}