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
This commit is contained in:
Alexander Raish 2023-02-13 17:56:36 +00:00
parent f7fbfaf9c7
commit 96a1c6ce15
2 changed files with 24 additions and 6 deletions

View file

@ -47,29 +47,34 @@ pub struct Gltf {
pub named_animations: HashMap<String, Handle<AnimationClip>>,
}
/// 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<GltfNode>,
pub mesh: Option<Handle<GltfMesh>>,
pub transform: bevy_transform::prelude::Transform,
pub extras: Option<GltfExtras>,
}
/// 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<GltfPrimitive>,
pub extras: Option<GltfExtras>,
}
/// 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<Mesh>,
pub material: Option<Handle<StandardMaterial>>,
pub extras: Option<GltfExtras>,
pub material_extras: Option<GltfExtras>,
}
#[derive(Clone, Debug, Reflect, Default, Component)]

View file

@ -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<GltfExtras> {
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,
}
}
}