Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_gltf/src/label.rs
9367 views
1
//! Labels that can be used to load part of a glTF
2
3
use 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_scene::prelude::*;
13
/// # use bevy_gltf::prelude::*;
14
///
15
/// fn load_gltf_scene(asset_server: Res<AssetServer>) {
16
/// let gltf_scene: Handle<Scene> = 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_scene::prelude::*;
26
/// # use bevy_gltf::prelude::*;
27
///
28
/// fn load_gltf_scene(asset_server: Res<AssetServer>) {
29
/// let gltf_scene: Handle<Scene> = asset_server.load(format!("models/FlightHelmet/FlightHelmet.gltf#{}", GltfAssetLabel::Scene(0)));
30
/// }
31
/// ```
32
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33
pub enum GltfAssetLabel {
34
/// `Scene{}`: glTF Scene as a Bevy [`Scene`](bevy_scene::Scene)
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
/// `Mesh{}/Primitive{}/MorphTargets`: Morph target animation data for a glTF Primitive
48
/// as a Bevy [`Image`](bevy_image::prelude::Image)
49
MorphTarget {
50
/// Index of the mesh for this primitive
51
mesh: usize,
52
/// Index of this primitive in its parent mesh
53
primitive: usize,
54
},
55
/// `Texture{}`: glTF Texture as a Bevy [`Image`](bevy_image::prelude::Image)
56
Texture(usize),
57
/// `Material{}`: glTF Material as Bevy [`GltfMaterial`](crate::GltfMaterial)
58
Material {
59
/// Index of this material
60
index: usize,
61
/// Used to set the [`Face`](bevy_render::render_resource::Face) of the material,
62
/// useful if it is used with negative scale
63
is_scale_inverted: bool,
64
},
65
/// `DefaultMaterial`: glTF's default Material
66
DefaultMaterial,
67
/// `Animation{}`: glTF Animation as Bevy [`AnimationClip`](bevy_animation::AnimationClip)
68
Animation(usize),
69
/// `Skin{}`: glTF mesh skin as [`GltfSkin`](crate::GltfSkin)
70
Skin(usize),
71
/// `Skin{}/InverseBindMatrices`: glTF mesh skin matrices as Bevy
72
/// [`SkinnedMeshInverseBindposes`](bevy_mesh::skinning::SkinnedMeshInverseBindposes)
73
InverseBindMatrices(usize),
74
}
75
76
impl core::fmt::Display for GltfAssetLabel {
77
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
78
match self {
79
GltfAssetLabel::Scene(index) => f.write_str(&format!("Scene{index}")),
80
GltfAssetLabel::Node(index) => f.write_str(&format!("Node{index}")),
81
GltfAssetLabel::Mesh(index) => f.write_str(&format!("Mesh{index}")),
82
GltfAssetLabel::Primitive { mesh, primitive } => {
83
f.write_str(&format!("Mesh{mesh}/Primitive{primitive}"))
84
}
85
GltfAssetLabel::MorphTarget { mesh, primitive } => {
86
f.write_str(&format!("Mesh{mesh}/Primitive{primitive}/MorphTargets"))
87
}
88
GltfAssetLabel::Texture(index) => f.write_str(&format!("Texture{index}")),
89
GltfAssetLabel::Material {
90
index,
91
is_scale_inverted,
92
} => f.write_str(&format!(
93
"Material{index}{}",
94
if *is_scale_inverted {
95
" (inverted)"
96
} else {
97
""
98
}
99
)),
100
GltfAssetLabel::DefaultMaterial => f.write_str("DefaultMaterial"),
101
GltfAssetLabel::Animation(index) => f.write_str(&format!("Animation{index}")),
102
GltfAssetLabel::Skin(index) => f.write_str(&format!("Skin{index}")),
103
GltfAssetLabel::InverseBindMatrices(index) => {
104
f.write_str(&format!("Skin{index}/InverseBindMatrices"))
105
}
106
}
107
}
108
}
109
110
impl GltfAssetLabel {
111
/// Add this label to an asset path
112
///
113
/// ```
114
/// # use bevy_ecs::prelude::*;
115
/// # use bevy_asset::prelude::*;
116
/// # use bevy_scene::prelude::*;
117
/// # use bevy_gltf::prelude::*;
118
///
119
/// fn load_gltf_scene(asset_server: Res<AssetServer>) {
120
/// let gltf_scene: Handle<Scene> = asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/FlightHelmet/FlightHelmet.gltf"));
121
/// }
122
/// ```
123
pub fn from_asset(&self, path: impl Into<AssetPath<'static>>) -> AssetPath<'static> {
124
path.into().with_label(self.to_string())
125
}
126
}
127
128