Skip to content

Commit

Permalink
add list of primitive per mesh, and their associated material
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Dec 7, 2020
1 parent 6450a9e commit eeb583e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ pub struct GltfPlugin;
impl Plugin for GltfPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_asset_loader::<GltfLoader>()
.add_asset::<GltfNode>();
.add_asset::<GltfNode>()
.add_asset::<GltfMesh>();
}
}

Expand All @@ -22,3 +23,15 @@ pub struct GltfNode {
pub mesh: Option<usize>,
pub transform: bevy_transform::prelude::Transform,
}

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

#[derive(Debug, Clone)]
pub struct GltfPrimitive {
index: usize,
material: Option<usize>,
}
13 changes: 13 additions & 0 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ async fn load_gltf<'a, 'b>(
}

for mesh in gltf.meshes() {
let mut primitives = vec![];
for primitive in mesh.primitives() {
primitives.push(super::GltfPrimitive {
index: primitive.index(),
material: primitive.material().index(),
});
let primitive_label = primitive_label(&mesh, &primitive);
if !load_context.has_labeled_asset(&primitive_label) {
let reader = primitive.reader(|buffer| Some(&buffer_data[buffer.index()]));
Expand Down Expand Up @@ -142,6 +147,10 @@ async fn load_gltf<'a, 'b>(
load_context.set_labeled_asset(&primitive_label, LoadedAsset::new(mesh));
};
}
load_context.set_labeled_asset(
&mesh_label(&mesh),
LoadedAsset::new(super::GltfMesh { primitives }),
);
}

for texture in gltf.textures() {
Expand Down Expand Up @@ -326,6 +335,10 @@ fn load_node(
}
}

fn mesh_label(mesh: &gltf::Mesh) -> String {
format!("Mesh{}", mesh.index())
}

fn primitive_label(mesh: &gltf::Mesh, primitive: &Primitive) -> String {
format!("Mesh{}/Primitive{}", mesh.index(), primitive.index())
}
Expand Down

0 comments on commit eeb583e

Please sign in to comment.