From 96a1c6ce15b9435d345980f53f46b811acbdc39e Mon Sep 17 00:00:00 2001 From: Alexander Raish Date: Mon, 13 Feb 2023 17:56:36 +0000 Subject: [PATCH] Add extras field to GltfNode (#6973) # Objective In our project we parse `GltfNode` from `*.gltf` file, and we need extra properties information from Blender. Right now there is no way to get this properties from GltfNode (only through query when you spawn scene), so objective of this PR is to add extra properties to `GltfNode` ## Solution Store extra properties inside `Gltf` structs --- ## Changelog - Add pub field `extras` to `GltfNode`/`GltfMesh`/`GltfPrimitive` which store extras - Add pub field `material_extras` to `GltfPrimitive` which store material extras --- crates/bevy_gltf/src/lib.rs | 13 +++++++++---- crates/bevy_gltf/src/loader.rs | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/crates/bevy_gltf/src/lib.rs b/crates/bevy_gltf/src/lib.rs index f86efd9732ffb..4522bcf9dbffb 100644 --- a/crates/bevy_gltf/src/lib.rs +++ b/crates/bevy_gltf/src/lib.rs @@ -47,29 +47,34 @@ 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 { 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). +/// 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 extras: 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 73dd95d27606d..616f26451e35c 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, GltfExtras, GltfNode}; /// An error that occurs when loading a glTF file. #[derive(Error, Debug)] @@ -329,12 +329,17 @@ async fn load_gltf<'a, 'b>( .material() .index() .and_then(|i| materials.get(i).cloned()), + extras: get_gltf_extras(primitive.extras()), + material_extras: get_gltf_extras(primitive.material().extras()), }); } let handle = load_context.set_labeled_asset( &mesh_label(&mesh), - LoadedAsset::new(super::GltfMesh { primitives }), + LoadedAsset::new(super::GltfMesh { + primitives, + extras: get_gltf_extras(mesh.extras()), + }), ); if let Some(name) = mesh.name() { named_meshes.insert(name.to_string(), handle.clone()); @@ -368,6 +373,7 @@ async fn load_gltf<'a, 'b>( scale: bevy_math::Vec3::from(scale), }, }, + extras: get_gltf_extras(node.extras()), }, node.children() .map(|child| child.index()) @@ -544,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() @@ -1166,6 +1178,7 @@ mod test { children: vec![], mesh: None, transform: bevy_transform::prelude::Transform::IDENTITY, + extras: None, } } }