From bc4acdd257c304be7f1e79c863740847a740d67a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Mockers?= Date: Thu, 13 May 2021 01:26:36 +0200 Subject: [PATCH 1/3] expose extras from gltf nodes --- crates/bevy_gltf/Cargo.toml | 1 + crates/bevy_gltf/src/lib.rs | 10 +++++++++- crates/bevy_gltf/src/loader.rs | 6 ++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/bevy_gltf/Cargo.toml b/crates/bevy_gltf/Cargo.toml index c29efda18b5b3..13c0da7038928 100644 --- a/crates/bevy_gltf/Cargo.toml +++ b/crates/bevy_gltf/Cargo.toml @@ -29,6 +29,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.7.0-dev" } gltf = { version = "1.0.0", default-features = false, features = [ "KHR_lights_punctual", "KHR_materials_unlit", + "extras", "names", "utils", ] } diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index fa55f1031e35d..0a9255b912105 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -7,8 +7,9 @@ pub use loader::*; use bevy_app::prelude::*; use bevy_asset::{AddAsset, Handle}; +use bevy_ecs::{prelude::Component, reflect::ReflectComponent}; use bevy_pbr::StandardMaterial; -use bevy_reflect::TypeUuid; +use bevy_reflect::{Reflect, TypeUuid}; use bevy_render::mesh::Mesh; use bevy_scene::Scene; @@ -19,6 +20,7 @@ pub struct GltfPlugin; impl Plugin for GltfPlugin { fn build(&self, app: &mut App) { app.init_asset_loader::() + .register_type::() .add_asset::() .add_asset::() .add_asset::() @@ -69,3 +71,9 @@ pub struct GltfPrimitive { pub mesh: Handle, pub material: Option>, } + +#[derive(Debug, Reflect, Default, Component)] +#[reflect(Component)] +pub struct GltfExtras { + pub value: String, +} diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 43068b6e3f60b..96bf62f3c70d1 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -703,6 +703,12 @@ fn load_node( node.insert(node_name(gltf_node)); + if let Some(extras) = gltf_node.extras() { + node.insert(super::GltfExtras { + value: extras.get().to_string(), + }); + } + // create camera node if let Some(camera) = gltf_node.camera() { node.insert_bundle(( From cb77c66b80b377d9fb079eefd56be63e1de434ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 13 May 2021 03:05:14 +0200 Subject: [PATCH 2/3] clone and reorder derive Co-authored-by: Nathan Ward <43621845+NathanSWard@users.noreply.github.com> --- crates/bevy_gltf/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 0a9255b912105..b1748f8be8629 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -72,7 +72,7 @@ pub struct GltfPrimitive { pub material: Option>, } -#[derive(Debug, Reflect, Default, Component)] +#[derive(Clone, Debug, Reflect, Default, Component)] #[reflect(Component)] pub struct GltfExtras { pub value: String, From cef631b62caa3a124523f9daf27600d8b5c58900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Thu, 20 Jan 2022 23:10:06 +0100 Subject: [PATCH 3/3] extra extras --- crates/bevy_gltf/src/loader.rs | 39 ++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 96bf62f3c70d1..fc5d798757e3f 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -786,21 +786,24 @@ fn load_node( let material_asset_path = AssetPath::new_ref(load_context.path(), Some(&material_label)); - let node = parent - .spawn_bundle(PbrBundle { - mesh: load_context.get_handle(mesh_asset_path), - material: load_context.get_handle(material_asset_path), - ..Default::default() - }) - .insert(Aabb::from_min_max( - Vec3::from_slice(&bounds.min), - Vec3::from_slice(&bounds.max), - )) - .id(); - + let mut mesh_entity = parent.spawn_bundle(PbrBundle { + mesh: load_context.get_handle(mesh_asset_path), + material: load_context.get_handle(material_asset_path), + ..Default::default() + }); + mesh_entity.insert(Aabb::from_min_max( + Vec3::from_slice(&bounds.min), + Vec3::from_slice(&bounds.max), + )); + + if let Some(extras) = primitive.extras() { + mesh_entity.insert(super::GltfExtras { + value: extras.get().to_string(), + }); + } // Mark for adding skinned mesh if let Some(skin) = gltf_node.skin() { - entity_to_skin_index_map.insert(node, skin.index()); + entity_to_skin_index_map.insert(mesh_entity.id(), skin.index()); } } } @@ -821,6 +824,11 @@ fn load_node( if let Some(name) = light.name() { entity.insert(Name::new(name.to_string())); } + if let Some(extras) = light.extras() { + entity.insert(super::GltfExtras { + value: extras.get().to_string(), + }); + } } gltf::khr_lights_punctual::Kind::Point => { let mut entity = parent.spawn_bundle(PointLightBundle { @@ -839,6 +847,11 @@ fn load_node( if let Some(name) = light.name() { entity.insert(Name::new(name.to_string())); } + if let Some(extras) = light.extras() { + entity.insert(super::GltfExtras { + value: extras.get().to_string(), + }); + } } gltf::khr_lights_punctual::Kind::Spot { inner_cone_angle: _inner_cone_angle,