From 230ada7ba1ff09e30bc8a6713eb220bc5af5981a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Mon, 7 Dec 2020 03:38:19 +0100 Subject: [PATCH] add list of primitive per mesh, and their associated material --- crates/bevy_gltf/src/lib.rs | 15 ++++++++++++++- crates/bevy_gltf/src/loader.rs | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 636c0925233d10..2e7702b436575f 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -11,7 +11,8 @@ pub struct GltfPlugin; impl Plugin for GltfPlugin { fn build(&self, app: &mut AppBuilder) { app.init_asset_loader::() - .add_asset::(); + .add_asset::() + .add_asset::(); } } @@ -22,3 +23,15 @@ pub struct GltfNode { pub mesh: Option, pub transform: bevy_transform::prelude::Transform, } + +#[derive(Debug, Clone, bevy_reflect::TypeUuid)] +#[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"] +pub struct GltfMesh { + pub primitives: Vec, +} + +#[derive(Debug, Clone)] +pub struct GltfPrimitive { + index: usize, + material: Option, +} diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index c881d192ec2fc5..4a371bd972d3f5 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -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()])); @@ -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() { @@ -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()) }