Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add extras field to GltfNode #6973

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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