Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTF extension support #11138

Merged
merged 17 commits into from
Jan 15, 2024
Merged

GLTF extension support #11138

merged 17 commits into from
Jan 15, 2024

Conversation

CorneliusCornbread
Copy link
Contributor

@CorneliusCornbread CorneliusCornbread commented Dec 30, 2023

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,
        ),
    ),
)
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.

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Assets Load files from disk to use for things like images, models, and sounds labels Dec 30, 2023
@alice-i-cecile alice-i-cecile added this to the 0.13 milestone Dec 30, 2023
@kaosat-dev
Copy link
Contributor

Hi @CorneliusCornbread !
Thanks for the pull request !
Just wanted to let you know I started looking at your changes & proposal, will do a full review this week.

Copy link
Contributor

@kaosat-dev kaosat-dev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple & clear, works fine locally I think it is all good ! Thanks a lot for your PR ! :)
Some minor thoughts:

  • I do not really think the meta migration is much of an issue, but that could be just me
  • I usually favour having a small example for new features, but I seem to recall the Bevy team likes to keep the examples minimal to prevent too much bloat, but perhaps @alice-i-cecile has some input on that ?

@@ -35,14 +35,15 @@ bevy_tasks = { path = "../bevy_tasks", version = "0.12.0" }
bevy_utils = { path = "../bevy_utils", version = "0.12.0" }

# other
gltf = { version = "1.3.0", default-features = false, features = [
gltf = { version = "1.4.0", default-features = false, features = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call ! v 1.4.0 has some nice improvements

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also a requirement for the extensions, 1.4.0 adds the extensions feature flag. There's also some fixes for large gltf's with large buffers which is nice

@@ -98,6 +98,8 @@ pub struct Gltf {
/// Named animations loaded from the glTF file.
#[cfg(feature = "bevy_animation")]
pub named_animations: HashMap<String, Handle<AnimationClip>>,
/// 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.
pub source: Option<gltf::Gltf>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is ok.
I wondered if it would be better hidden behind a feature flag like named_animations but it would not really make sense at the bevy level imho.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this makes sense, the idea is that if you don't need the extra data for some assets you can save on memory for those models specifically. Sometimes we only need the model data we're extracting normally.

@kaosat-dev
Copy link
Contributor

Just an additional 2 cents regarding your comment in your example code:

// 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;
}

you can use asset_server.get_load_state(handle) to get the LoadState of an asset and match on that :)

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jan 15, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 15, 2024
Merged via the queue into bevyengine:main with commit a7b99f0 Jan 15, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Feature A new feature, making something new possible S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants