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

make more information available from loaded GLTF model #1020

Merged
merged 7 commits into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,10 @@ impl<'a> LoadContext<'a> {
self.labeled_assets.insert(None, asset);
}

pub fn set_labeled_asset(&mut self, label: &str, asset: LoadedAsset) {
pub fn set_labeled_asset<T: Asset>(&mut self, label: &str, asset: LoadedAsset) -> Handle<T> {
Copy link
Member

Choose a reason for hiding this comment

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

This change makes this kind of code fail compilation:

load_context.set_labeled_asset("cube", LoadedAsset::new(mesh));

Is there a way to not be bothered with specifying the T parameter?

Copy link
Member

Choose a reason for hiding this comment

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

Hmm yeah that is suboptimal. Its both less ergonomic and creates a class of bug where the user specifies the wrong asset type.

We could make it LoadedAsset<T>, then created a BoxedLoadedAsset that we coerce it into within set_labeled_asset

assert!(!label.is_empty());
self.labeled_assets.insert(Some(label.to_string()), asset);
self.get_handle(AssetPath::new_ref(self.path(), Some(label)))
}

pub fn get_handle<I: Into<HandleId>, T: Asset>(&self, id: I) -> Handle<T> {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_gltf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ keywords = ["bevy"]
# bevy
bevy_app = { path = "../bevy_app", version = "0.4.0" }
bevy_asset = { path = "../bevy_asset", version = "0.4.0" }
bevy_core = { path = "../bevy_core", version = "0.4.0" }
bevy_ecs = { path = "../bevy_ecs", version = "0.4.0" }
bevy_pbr = { path = "../bevy_pbr", version = "0.4.0" }
bevy_reflect = { path = "../bevy_reflect", version = "0.4.0", features = ["bevy"] }
Expand All @@ -25,7 +26,7 @@ bevy_math = { path = "../bevy_math", version = "0.4.0" }
bevy_scene = { path = "../bevy_scene", version = "0.4.0" }

# other
gltf = { version = "0.15.2", default-features = false, features = ["utils"] }
gltf = { version = "0.15.2", default-features = false, features = ["utils", "names"] }
image = { version = "0.23.12", default-features = false }
thiserror = "1.0"
anyhow = "1.0"
Expand Down
49 changes: 47 additions & 2 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,60 @@
use std::collections::HashMap;

mod loader;
pub use loader::*;

use bevy_app::prelude::*;
use bevy_asset::AddAsset;
use bevy_asset::{AddAsset, Handle};
use bevy_pbr::prelude::StandardMaterial;
use bevy_reflect::TypeUuid;
use bevy_render::mesh::Mesh;
use bevy_scene::Scene;

/// Adds support for GLTF file loading to Apps
#[derive(Default)]
pub struct GltfPlugin;

impl Plugin for GltfPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_asset_loader::<GltfLoader>();
app.init_asset_loader::<GltfLoader>()
.add_asset::<Gltf>()
.add_asset::<GltfNode>()
.add_asset::<GltfPrimitive>()
.add_asset::<GltfMesh>();
}
}

#[derive(Debug, TypeUuid)]
#[uuid = "5c7d5f8a-f7b0-4e45-a09e-406c0372fea2"]
pub struct Gltf {
pub scenes: Vec<Handle<Scene>>,
pub named_scenes: HashMap<String, Handle<Scene>>,
pub meshes: Vec<Handle<GltfMesh>>,
pub named_meshes: HashMap<String, Handle<GltfMesh>>,
pub materials: Vec<Handle<StandardMaterial>>,
pub named_materials: HashMap<String, Handle<StandardMaterial>>,
pub nodes: Vec<Handle<GltfNode>>,
pub named_nodes: HashMap<String, Handle<GltfNode>>,
pub default_scene: Option<Handle<Scene>>,
}

#[derive(Debug, Clone, TypeUuid)]
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
pub struct GltfNode {
pub children: Vec<GltfNode>,
pub mesh: Option<Handle<GltfMesh>>,
pub transform: bevy_transform::prelude::Transform,
}

#[derive(Debug, Clone, TypeUuid)]
#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"]
pub struct GltfMesh {
pub primitives: Vec<GltfPrimitive>,
}

#[derive(Debug, Clone, TypeUuid)]
#[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"]
pub struct GltfPrimitive {
pub mesh: Handle<Mesh>,
pub material: Option<Handle<StandardMaterial>>,
}
Loading