Skip to content

Commit

Permalink
get names of gltf assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Dec 10, 2020
1 parent c559126 commit ad928c8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_gltf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bevy_math = { path = "../bevy_math", version = "0.3.0" }
bevy_scene = { path = "../bevy_scene", version = "0.3.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
6 changes: 6 additions & 0 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

mod loader;
pub use loader::*;

Expand Down Expand Up @@ -26,9 +28,13 @@ impl Plugin for GltfPlugin {
#[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>>,
}

Expand Down
63 changes: 46 additions & 17 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use gltf::{
Primitive,
};
use image::{GenericImageView, ImageFormat};
use std::collections::HashMap;
use std::path::Path;
use thiserror::Error;

Expand Down Expand Up @@ -80,6 +81,7 @@ async fn load_gltf<'a, 'b>(
let buffer_data = load_buffers(&gltf, load_context, load_context.path()).await?;

let mut materials = vec![];
let mut named_materials = HashMap::new();
for material in gltf.materials() {
let material_label = material_label(&material);
let pbr = material.pbr_metallic_roughness();
Expand All @@ -104,20 +106,23 @@ async fn load_gltf<'a, 'b>(
None
};
let color = pbr.base_color_factor();
materials.push(
load_context.set_labeled_asset(
&material_label,
LoadedAsset::new(StandardMaterial {
albedo: Color::rgba(color[0], color[1], color[2], color[3]),
albedo_texture: texture_handle,
..Default::default()
})
.with_dependencies(dependencies),
),
let handle = load_context.set_labeled_asset(
&material_label,
LoadedAsset::new(StandardMaterial {
albedo: Color::rgba(color[0], color[1], color[2], color[3]),
albedo_texture: texture_handle,
..Default::default()
})
.with_dependencies(dependencies),
);
if let Some(name) = material.name() {
named_materials.insert(name.to_string(), handle.clone());
}
materials.push(handle);
}

let mut meshes = vec![];
let mut named_meshes = HashMap::new();
for mesh in gltf.meshes() {
let mut primitives = vec![];
for primitive in mesh.primitives() {
Expand Down Expand Up @@ -161,13 +166,18 @@ async fn load_gltf<'a, 'b>(
.and_then(|i| materials.get(i).cloned()),
});
}
meshes.push(load_context.set_labeled_asset(
let handle = load_context.set_labeled_asset(
&mesh_label(&mesh),
LoadedAsset::new(super::GltfMesh { primitives }),
));
);
if let Some(name) = mesh.name() {
named_meshes.insert(name.to_string(), handle.clone());
}
meshes.push(handle);
}

let mut nodes_intermediate = vec![];
let mut named_nodes_intermediate = HashMap::new();
for node in gltf.nodes() {
let node_label = node_label(&node);
nodes_intermediate.push((
Expand Down Expand Up @@ -196,12 +206,23 @@ async fn load_gltf<'a, 'b>(
node.children()
.map(|child| child.index())
.collect::<Vec<_>>(),
))
));
if let Some(name) = node.name() {
named_nodes_intermediate.insert(name, node.index());
}
}
let nodes = resolve_node_hierarchy(nodes_intermediate)
.into_iter()
.map(|(label, node)| load_context.set_labeled_asset(&label, LoadedAsset::new(node)))
.collect::<Vec<bevy_asset::Handle<GltfNode>>>();
let named_nodes = named_nodes_intermediate
.into_iter()
.filter_map(|(name, index)| {
nodes
.get(index)
.map(|handle| (name.to_string(), handle.clone()))
})
.collect();

for texture in gltf.textures() {
if let gltf::image::Source::View { view, mime_type } = texture.source().source() {
Expand Down Expand Up @@ -232,6 +253,7 @@ async fn load_gltf<'a, 'b>(
}

let mut scenes = vec![];
let mut named_scenes = HashMap::new();
for scene in gltf.scenes() {
let mut err = None;
let mut world = World::default();
Expand All @@ -250,10 +272,13 @@ async fn load_gltf<'a, 'b>(
if let Some(Err(err)) = err {
return Err(err);
}
scenes.push(
load_context
.set_labeled_asset(&scene_label(&scene), LoadedAsset::new(Scene::new(world))),
);
let scene_handle = load_context
.set_labeled_asset(&scene_label(&scene), LoadedAsset::new(Scene::new(world)));

if let Some(name) = scene.name() {
named_scenes.insert(name.to_string(), scene_handle.clone());
}
scenes.push(scene_handle);
}

load_context.set_default_asset(LoadedAsset::new(Gltf {
Expand All @@ -262,9 +287,13 @@ async fn load_gltf<'a, 'b>(
.and_then(|scene| scenes.get(scene.index()))
.cloned(),
scenes,
named_scenes,
meshes,
named_meshes,
materials,
named_materials,
nodes,
named_nodes,
}));

Ok(())
Expand Down

0 comments on commit ad928c8

Please sign in to comment.