Skip to content

Commit a7b99f0

Browse files
GLTF extension support (#11138)
# Objective Adds support for accessing raw extension data of loaded GLTF assets ## Solution Via the GLTF loader settings, you can specify whether or not to include the GLTF source. While not the ideal way of solving this problem, modeling all of GLTF within Bevy just for extensions adds a lot of complexity to the way Bevy handles GLTF currently. See the example GLTF meta file and code ``` ( meta_format_version: "1.0", asset: Load( loader: "bevy_gltf::loader::GltfLoader", settings: ( load_meshes: true, load_cameras: true, load_lights: true, include_source: true, ), ), ) ``` ```rs pub fn load_gltf(mut commands: Commands, assets: Res<AssetServer>) { let my_gltf = assets.load("test_platform.gltf"); commands.insert_resource(MyAssetPack { spawned: false, handle: my_gltf, }); } #[derive(Resource)] pub struct MyAssetPack { pub spawned: bool, pub handle: Handle<Gltf>, } pub fn spawn_gltf_objects( mut commands: Commands, mut my: ResMut<MyAssetPack>, assets_gltf: Res<Assets<Gltf>>, ) { // This flag is used to because this system has to be run until the asset is loaded. // If there's a better way of going about this I am unaware of it. if my.spawned { return; } if let Some(gltf) = assets_gltf.get(&my.handle) { info!("spawn"); my.spawned = true; // spawn the first scene in the file commands.spawn(SceneBundle { scene: gltf.scenes[0].clone(), ..Default::default() }); let source = gltf.source.as_ref().unwrap(); info!("materials count {}", &source.materials().size_hint().0); info!( "materials ext is some {}", &source.materials().next().unwrap().extensions().is_some() ); } } ``` --- ## Changelog Added support for GLTF extensions through including raw GLTF source via loader flag `GltfLoaderSettings::include_source == true`, stored in `Gltf::source: Option<gltf::Gltf>` ## Migration Guide This will have issues with "asset migrations", as there is currently no way for .meta files to be migrated. Attempting to migrate .meta files without the new flag will yield the following error: ``` bevy_asset::server: Failed to deserialize meta for asset test_platform.gltf: Failed to deserialize asset meta: SpannedError { code: MissingStructField { field: "include_source", outer: Some("GltfLoaderSettings") }, position: Position { line: 9, col: 9 } } ``` This means users who want to migrate their .meta files will have to add the `include_source: true,` setting to their meta files by hand.
1 parent c29a972 commit a7b99f0

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

crates/bevy_gltf/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.12.0" }
3535
bevy_utils = { path = "../bevy_utils", version = "0.12.0" }
3636

3737
# other
38-
gltf = { version = "1.3.0", default-features = false, features = [
38+
gltf = { version = "1.4.0", default-features = false, features = [
3939
"KHR_lights_punctual",
4040
"KHR_materials_transmission",
4141
"KHR_materials_ior",
4242
"KHR_materials_volume",
4343
"KHR_materials_unlit",
4444
"KHR_materials_emissive_strength",
4545
"extras",
46+
"extensions",
4647
"names",
4748
"utils",
4849
] }

crates/bevy_gltf/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub struct Gltf {
9898
/// Named animations loaded from the glTF file.
9999
#[cfg(feature = "bevy_animation")]
100100
pub named_animations: HashMap<String, Handle<AnimationClip>>,
101+
/// The gltf root of the gltf asset, see <https://docs.rs/gltf/latest/gltf/struct.Gltf.html>. Only has a value when `GltfLoaderSettings::include_source` is true.
102+
pub source: Option<gltf::Gltf>,
101103
}
102104

103105
/// A glTF node with all of its child nodes, its [`GltfMesh`],

crates/bevy_gltf/src/loader.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ pub struct GltfLoaderSettings {
131131
pub load_cameras: bool,
132132
/// If true, the loader will spawn lights for gltf light nodes.
133133
pub load_lights: bool,
134+
/// If true, the loader will include the root of the gltf root node.
135+
pub include_source: bool,
134136
}
135137

136138
impl Default for GltfLoaderSettings {
@@ -139,6 +141,7 @@ impl Default for GltfLoaderSettings {
139141
load_meshes: true,
140142
load_cameras: true,
141143
load_lights: true,
144+
include_source: false,
142145
}
143146
}
144147
}
@@ -673,6 +676,11 @@ async fn load_gltf<'a, 'b, 'c>(
673676
animations,
674677
#[cfg(feature = "bevy_animation")]
675678
named_animations,
679+
source: if settings.include_source {
680+
Some(gltf)
681+
} else {
682+
None
683+
},
676684
})
677685
}
678686

0 commit comments

Comments
 (0)