Skip to content

Commit

Permalink
make gltf nodes available as assets
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Dec 7, 2020
1 parent 1398d78 commit 6450a9e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
11 changes: 10 additions & 1 deletion crates/bevy_gltf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ pub struct GltfPlugin;

impl Plugin for GltfPlugin {
fn build(&self, app: &mut AppBuilder) {
app.init_asset_loader::<GltfLoader>();
app.init_asset_loader::<GltfLoader>()
.add_asset::<GltfNode>();
}
}

#[derive(Debug, Clone, bevy_reflect::TypeUuid)]
#[uuid = "dad74750-1fd6-460f-ac51-0a7937563865"]
pub struct GltfNode {
pub children: Vec<usize>,
pub mesh: Option<usize>,
pub transform: bevy_transform::prelude::Transform,
}
29 changes: 29 additions & 0 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ async fn load_gltf<'a, 'b>(

let world_builder = &mut world.build();

for node in gltf.nodes() {
let node_label = node_label(&node);
load_context.set_labeled_asset(
&node_label,
LoadedAsset::new(crate::GltfNode {
children: node.children().map(|child| child.index()).collect(),
mesh: node.mesh().map(|mesh| mesh.index()),
transform: match node.transform() {
gltf::scene::Transform::Matrix { matrix } => {
Transform::from_matrix(bevy_math::Mat4::from_cols_array_2d(&matrix))
}
gltf::scene::Transform::Decomposed {
translation,
rotation,
scale,
} => Transform {
translation: bevy_math::Vec3::from(translation),
rotation: bevy_math::Quat::from(rotation),
scale: bevy_math::Vec3::from(scale),
},
},
}),
);
}

for mesh in gltf.meshes() {
for primitive in mesh.primitives() {
let primitive_label = primitive_label(&mesh, &primitive);
Expand Down Expand Up @@ -317,6 +342,10 @@ fn texture_label(texture: &gltf::Texture) -> String {
format!("Texture{}", texture.index())
}

fn node_label(node: &gltf::Node) -> String {
format!("Node{}", node.index())
}

fn texture_sampler(texture: &gltf::Texture) -> Result<SamplerDescriptor, GltfError> {
let gltf_sampler = texture.sampler();

Expand Down

0 comments on commit 6450a9e

Please sign in to comment.