expose extras from gltf nodes (#2154)

fixes #2153 

expose the `extras` field value as a string
This commit is contained in:
François 2022-04-07 21:30:52 +00:00
parent b1afe2dcca
commit 8268e7fa9e
3 changed files with 41 additions and 13 deletions

View File

@ -29,6 +29,7 @@ bevy_utils = { path = "../bevy_utils", version = "0.7.0-dev" }
gltf = { version = "1.0.0", default-features = false, features = [ gltf = { version = "1.0.0", default-features = false, features = [
"KHR_lights_punctual", "KHR_lights_punctual",
"KHR_materials_unlit", "KHR_materials_unlit",
"extras",
"names", "names",
"utils", "utils",
] } ] }

View File

@ -7,8 +7,9 @@ pub use loader::*;
use bevy_app::prelude::*; use bevy_app::prelude::*;
use bevy_asset::{AddAsset, Handle}; use bevy_asset::{AddAsset, Handle};
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_pbr::StandardMaterial; use bevy_pbr::StandardMaterial;
use bevy_reflect::TypeUuid; use bevy_reflect::{Reflect, TypeUuid};
use bevy_render::mesh::Mesh; use bevy_render::mesh::Mesh;
use bevy_scene::Scene; use bevy_scene::Scene;
@ -19,6 +20,7 @@ pub struct GltfPlugin;
impl Plugin for GltfPlugin { impl Plugin for GltfPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_asset_loader::<GltfLoader>() app.init_asset_loader::<GltfLoader>()
.register_type::<GltfExtras>()
.add_asset::<Gltf>() .add_asset::<Gltf>()
.add_asset::<GltfNode>() .add_asset::<GltfNode>()
.add_asset::<GltfPrimitive>() .add_asset::<GltfPrimitive>()
@ -69,3 +71,9 @@ pub struct GltfPrimitive {
pub mesh: Handle<Mesh>, pub mesh: Handle<Mesh>,
pub material: Option<Handle<StandardMaterial>>, pub material: Option<Handle<StandardMaterial>>,
} }
#[derive(Clone, Debug, Reflect, Default, Component)]
#[reflect(Component)]
pub struct GltfExtras {
pub value: String,
}

View File

@ -703,6 +703,12 @@ fn load_node(
node.insert(node_name(gltf_node)); node.insert(node_name(gltf_node));
if let Some(extras) = gltf_node.extras() {
node.insert(super::GltfExtras {
value: extras.get().to_string(),
});
}
// create camera node // create camera node
if let Some(camera) = gltf_node.camera() { if let Some(camera) = gltf_node.camera() {
node.insert_bundle(( node.insert_bundle((
@ -780,21 +786,24 @@ fn load_node(
let material_asset_path = let material_asset_path =
AssetPath::new_ref(load_context.path(), Some(&material_label)); AssetPath::new_ref(load_context.path(), Some(&material_label));
let node = parent let mut mesh_entity = parent.spawn_bundle(PbrBundle {
.spawn_bundle(PbrBundle {
mesh: load_context.get_handle(mesh_asset_path), mesh: load_context.get_handle(mesh_asset_path),
material: load_context.get_handle(material_asset_path), material: load_context.get_handle(material_asset_path),
..Default::default() ..Default::default()
}) });
.insert(Aabb::from_min_max( mesh_entity.insert(Aabb::from_min_max(
Vec3::from_slice(&bounds.min), Vec3::from_slice(&bounds.min),
Vec3::from_slice(&bounds.max), Vec3::from_slice(&bounds.max),
)) ));
.id();
if let Some(extras) = primitive.extras() {
mesh_entity.insert(super::GltfExtras {
value: extras.get().to_string(),
});
}
// Mark for adding skinned mesh // Mark for adding skinned mesh
if let Some(skin) = gltf_node.skin() { if let Some(skin) = gltf_node.skin() {
entity_to_skin_index_map.insert(node, skin.index()); entity_to_skin_index_map.insert(mesh_entity.id(), skin.index());
} }
} }
} }
@ -815,6 +824,11 @@ fn load_node(
if let Some(name) = light.name() { if let Some(name) = light.name() {
entity.insert(Name::new(name.to_string())); entity.insert(Name::new(name.to_string()));
} }
if let Some(extras) = light.extras() {
entity.insert(super::GltfExtras {
value: extras.get().to_string(),
});
}
} }
gltf::khr_lights_punctual::Kind::Point => { gltf::khr_lights_punctual::Kind::Point => {
let mut entity = parent.spawn_bundle(PointLightBundle { let mut entity = parent.spawn_bundle(PointLightBundle {
@ -833,6 +847,11 @@ fn load_node(
if let Some(name) = light.name() { if let Some(name) = light.name() {
entity.insert(Name::new(name.to_string())); entity.insert(Name::new(name.to_string()));
} }
if let Some(extras) = light.extras() {
entity.insert(super::GltfExtras {
value: extras.get().to_string(),
});
}
} }
gltf::khr_lights_punctual::Kind::Spot { gltf::khr_lights_punctual::Kind::Spot {
inner_cone_angle: _inner_cone_angle, inner_cone_angle: _inner_cone_angle,