Skip to content

Commit

Permalink
Add extras field to GltfNode (bevyengine#6973)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
alradish committed Feb 13, 2023
1 parent f7fbfaf commit 96a1c6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 9 additions & 4 deletions crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
17 changes: 15 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1166,6 +1178,7 @@ mod test {
children: vec![],
mesh: None,
transform: bevy_transform::prelude::Transform::IDENTITY,
extras: None,
}
}
}
Expand Down

0 comments on commit 96a1c6c

Please sign in to comment.