forked from amethyst/amethyst
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use gltf::buffer::Data; | ||
use amethyst_core::ecs::{Entity, World, EntityStore}; | ||
use crate::importer::SkinInfo; | ||
use amethyst_error::Error; | ||
use std::collections::HashMap; | ||
use amethyst_core::math::{Matrix4, convert}; | ||
use amethyst_animation::{Skin, Joint}; | ||
use amethyst_rendy::skinning::JointTransforms; | ||
|
||
struct JointTransformInfos{ | ||
pub skin: Entity, | ||
pub size: usize, | ||
} | ||
|
||
pub fn load_skin( | ||
skin: &gltf::Skin<'_>, | ||
buffers: &Vec<Data>, | ||
entity: Entity, | ||
skin_infos: &SkinInfo, | ||
node_map: &HashMap<usize, Entity>, | ||
world: &mut World | ||
) -> Result<(), Error> { | ||
let joint_entities = skin | ||
.joints() | ||
.map(|j| { | ||
node_map.get(&j.index()).cloned().expect( | ||
"Unreachable: `node_map` is initialized with the indexes from the `Gltf` object", | ||
) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let reader = skin.reader(|buffer| Some(buffers.get(buffer.index()) | ||
.expect("Error while reading skin buffer") | ||
.0.as_slice())); | ||
|
||
let inverse_bind_matrices = reader | ||
.read_inverse_bind_matrices() | ||
.map(|matrices| { | ||
matrices | ||
.map(Matrix4::from) | ||
.map(convert::<_, Matrix4<f32>>) | ||
.collect() | ||
}) | ||
.unwrap_or_else(|| vec![Matrix4::identity(); joint_entities.len()]); | ||
|
||
let mut aggregator = HashMap::<Entity, Vec<Entity>>::new(); | ||
|
||
for (_bind_index, joint_entity) in joint_entities.iter().enumerate() { | ||
aggregator | ||
.entry(*joint_entity) | ||
.or_insert_with(||Vec::new()) | ||
.push(entity); | ||
} | ||
|
||
for (entity, skins) in aggregator.iter(){ | ||
let joint = Joint { | ||
skins: skins.clone(), | ||
}; | ||
world.entry(*entity) | ||
.expect("This can't be reached because the entity comes from this world") | ||
.add_component(joint); | ||
} | ||
|
||
let joint_transforms = JointTransforms{ skin: entity, matrices: vec![Matrix4::identity(); joint_entities.len()] }; | ||
for mesh_entity in &skin_infos.mesh_indices { | ||
world.entry(*mesh_entity) | ||
.expect("This can't be reached because the entity comes from this world") | ||
.add_component(joint_transforms.clone()); | ||
} | ||
let len = joint_entities.len(); | ||
let skin = Skin { | ||
joints: joint_entities, | ||
meshes: skin_infos.mesh_indices.clone(), | ||
bind_shape_matrix: Matrix4::identity(), | ||
inverse_bind_matrices, | ||
joint_matrices: Vec::with_capacity(len), | ||
}; | ||
|
||
world.entry(entity) | ||
.expect("This can't be reached because the entity comes from this world") | ||
.add_component(skin); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters