From 0efe6318796f52530721f9bf9de1c1a32b2a2992 Mon Sep 17 00:00:00 2001 From: Alexander Raish Date: Mon, 12 Dec 2022 01:12:55 +0700 Subject: [PATCH 1/4] Add extras field to GltfNode --- crates/bevy_gltf/src/lib.rs | 1 + crates/bevy_gltf/src/loader.rs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index f86efd9732ffb..e7020ceb660e8 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -55,6 +55,7 @@ pub struct GltfNode { pub children: Vec, pub mesh: Option>, pub transform: bevy_transform::prelude::Transform, + pub extras: Option, } /// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive). diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 81d793993ac7c..3d0481c529dca 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -368,6 +368,12 @@ async fn load_gltf<'a, 'b>( scale: bevy_math::Vec3::from(scale), }, }, + extras: node + .mesh() + .and_then(|mesh| mesh.extras().as_ref()) + .map(|extras| super::GltfExtras { + value: extras.get().to_string(), + }), }, node.children() .map(|child| child.index()) @@ -1166,6 +1172,7 @@ mod test { children: vec![], mesh: None, transform: bevy_transform::prelude::Transform::IDENTITY, + extras: None, } } } From b632f38fa5c0bf880dfd364b304ab98452797a3b Mon Sep 17 00:00:00 2001 From: Alexander Raish Date: Mon, 23 Jan 2023 03:17:07 +0700 Subject: [PATCH 2/4] Add extras to node, mesh and primitive --- crates/bevy_gltf/src/lib.rs | 11 +++++++---- crates/bevy_gltf/src/loader.rs | 19 ++++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index e7020ceb660e8..234af12882586 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -47,8 +47,8 @@ pub struct Gltf { pub named_animations: HashMap>, } -/// A glTF node with all of its child nodes, its [`GltfMesh`] and -/// [`Transform`](bevy_transform::prelude::Transform). +/// A glTF node with all of its child nodes, its [`GltfMesh`], +/// [`Transform`](bevy_transform::prelude::Transform) and an optional [`GltfExtras`]. #[derive(Debug, Clone, TypeUuid)] #[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"] pub struct GltfNode { @@ -58,19 +58,22 @@ pub struct GltfNode { pub extras: Option, } -/// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive). +/// A glTF mesh, which may consist of multiple [`GltfPrimitives`](GltfPrimitive) +/// and an optional [`GltfExtras`]. #[derive(Debug, Clone, TypeUuid)] #[uuid = "8ceaec9a-926a-4f29-8ee3-578a69f42315"] pub struct GltfMesh { pub primitives: Vec, + pub extras: Option, } -/// Part of a [`GltfMesh`] that consists of a [`Mesh`] and an optional [`StandardMaterial`]. +/// Part of a [`GltfMesh`] that consists of a [`Mesh`], an optional [`StandardMaterial`] and [`GltfExtras`]. #[derive(Debug, Clone, TypeUuid)] #[uuid = "cbfca302-82fd-41cb-af77-cab6b3d50af1"] pub struct GltfPrimitive { pub mesh: Handle, pub material: Option>, + pub material_extras: Option, } #[derive(Clone, Debug, Reflect, Default, Component)] diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 3d0481c529dca..aa828587e711f 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -329,12 +329,20 @@ async fn load_gltf<'a, 'b>( .material() .index() .and_then(|i| materials.get(i).cloned()), + material_extras: primitive.extras().as_ref().map(|extras| super::GltfExtras { + value: extras.get().to_string(), + }), }); } let handle = load_context.set_labeled_asset( &mesh_label(&mesh), - LoadedAsset::new(super::GltfMesh { primitives }), + LoadedAsset::new(super::GltfMesh { + primitives, + extras: mesh.extras().as_ref().map(|extras| super::GltfExtras { + value: extras.get().to_string(), + }), + }), ); if let Some(name) = mesh.name() { named_meshes.insert(name.to_string(), handle.clone()); @@ -368,12 +376,9 @@ async fn load_gltf<'a, 'b>( scale: bevy_math::Vec3::from(scale), }, }, - extras: node - .mesh() - .and_then(|mesh| mesh.extras().as_ref()) - .map(|extras| super::GltfExtras { - value: extras.get().to_string(), - }), + extras: node.extras().as_ref().map(|extras| super::GltfExtras { + value: extras.get().to_string(), + }), }, node.children() .map(|child| child.index()) From eb3fdf4752df4869abb800e6dc574d510f25eaf6 Mon Sep 17 00:00:00 2001 From: Alexander Raish Date: Fri, 27 Jan 2023 00:26:05 +0700 Subject: [PATCH 3/4] Fix primitive extras and extract common logic --- crates/bevy_gltf/src/lib.rs | 1 + crates/bevy_gltf/src/loader.rs | 21 +++++++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index 234af12882586..4522bcf9dbffb 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -73,6 +73,7 @@ pub struct GltfMesh { pub struct GltfPrimitive { pub mesh: Handle, pub material: Option>, + pub extras: Option, pub material_extras: Option, } diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index aa828587e711f..30ac6d0968e55 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -43,7 +43,7 @@ use gltf::{ use std::{collections::VecDeque, path::Path}; use thiserror::Error; -use crate::{Gltf, GltfNode}; +use crate::{Gltf, GltfNode, GltfExtras}; /// An error that occurs when loading a glTF file. #[derive(Error, Debug)] @@ -329,9 +329,8 @@ async fn load_gltf<'a, 'b>( .material() .index() .and_then(|i| materials.get(i).cloned()), - material_extras: primitive.extras().as_ref().map(|extras| super::GltfExtras { - value: extras.get().to_string(), - }), + extras: get_gltf_extras(primitive.extras()), + material_extras: get_gltf_extras(primitive.material().extras()), }); } @@ -339,9 +338,7 @@ async fn load_gltf<'a, 'b>( &mesh_label(&mesh), LoadedAsset::new(super::GltfMesh { primitives, - extras: mesh.extras().as_ref().map(|extras| super::GltfExtras { - value: extras.get().to_string(), - }), + extras: get_gltf_extras(mesh.extras()), }), ); if let Some(name) = mesh.name() { @@ -376,9 +373,7 @@ async fn load_gltf<'a, 'b>( scale: bevy_math::Vec3::from(scale), }, }, - extras: node.extras().as_ref().map(|extras| super::GltfExtras { - value: extras.get().to_string(), - }), + extras: get_gltf_extras(node.extras()), }, node.children() .map(|child| child.index()) @@ -555,6 +550,12 @@ async fn load_gltf<'a, 'b>( Ok(()) } +fn get_gltf_extras(extras: &gltf::json::Extras) -> Option { + extras.as_ref().map(|extras| super::GltfExtras { + value: extras.get().to_string(), + }) +} + fn node_name(node: &Node) -> Name { let name = node .name() From 1502ab30c980acc956953485c3893fbc2bf5f586 Mon Sep 17 00:00:00 2001 From: Alexander Raish Date: Fri, 27 Jan 2023 00:39:58 +0700 Subject: [PATCH 4/4] Fix formatting --- crates/bevy_gltf/src/loader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 30ac6d0968e55..38594ca7e74d9 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -43,7 +43,7 @@ use gltf::{ use std::{collections::VecDeque, path::Path}; use thiserror::Error; -use crate::{Gltf, GltfNode, GltfExtras}; +use crate::{Gltf, GltfExtras, GltfNode}; /// An error that occurs when loading a glTF file. #[derive(Error, Debug)]