bevyengine / bevy

A refreshingly simple data-driven game engine built in Rust
https://bevyengine.org
Apache License 2.0
35.46k stars 3.51k forks source link

Support loading GLTF assets by name #2948

Open pop opened 2 years ago

pop commented 2 years ago

What problem does this solve or what need does it fill?

To improve ergonomics of loading GLTF assets, Bevy ought to support loading GLTF assets (Mesh/Primitive/Material/Texture/Node/Scene) by Name as well as by Index

What solution would you like?

Add the ability to fetch assets by name. Something like this:

let my_gltf_scene_handle = asset_server.load("my_scenes.gltf#Scene1")

Could become:

let my_gltf_scene_handle = asset_server.load("my_scenes.gltf#Scene:'Winter Scene'")

This should probably be additive. Users can fetch GLTF assets with either Indexing or Naming.

If you think this feature is worth adding, I would be more than happy to implement it! I am enjoying Bevy and would love to contribute. šŸ˜„

What alternative(s) have you considered?

One could use the named_scenes field on the GLTF struct. One would load the GLTF asset file and then spawn specific scenes from the already loaded GLTF asset. In my opinion is not as ergonomic as it could be.

Additional context

The unofficial Bevy Cheatsheet makes it seem like this feature currently exists , but when I looked through the code this does not seem to be supported. Instead of correcting the cookbook, I think it's worth adding the feature.

alice-i-cecile commented 2 years ago

Ping @inodentry for Cheatbook relevance.

djeedai commented 2 years ago

The unofficial Bevy Cheatsheet makes it seem like this feature currently exists , but when I looked through the code this does not seem to be supported.

I got caught by this, wasted a lot of time trying to figure out what I was doing wrong...

mockersf commented 2 years ago

Oh I had that in a commit but it seems I never pushed it in a PR...

It's a small change to add scenes accessible directly by their name, here: https://github.com/bevyengine/bevy/blob/997eae61854b362c10810884966ac605121e04d7/crates/bevy_gltf/src/loader.rs#L299-L301

You would need to add something like

load_context.set_labeled_asset(&format!("Scene-{}", name), LoadedAsset::new(Scene::new(world)));

The LoadedAsset::new(Scene::new(world)) part should probably called only once, so saved it in a temporary variable on line 297 and introduce a way to "alias" names.

And ideally, the same could be done for all kind of assets available from a gltf with names (materials, meshes, nodes).

Also, if someone more aware of the rendering rework could chime in, but it seems this change should be done on branch pipelined-rendering

inodentry commented 2 years ago

I've fixed the misleading information in Cheatbook. Also, now there is a complete page on GLTF which goes into detail about the format and the current state of support in Bevy.

If there is anything still missing/incorrect/misleading, please file issues! :)

I don't want to waste people's time by not documenting things correctly.