1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use amethyst_assets::{PrefabData, PrefabError};
use amethyst_core::cgmath::Matrix4;
use amethyst_core::specs::prelude::{Component, DenseVecStorage, Entity, WriteStorage};
use amethyst_renderer::JointTransformsPrefab;
use hibitset::BitSet;
#[derive(Debug, Clone)]
pub struct Joint {
pub inverse_bind_matrix: Matrix4<f32>,
pub skin: Entity,
}
impl Component for Joint {
type Storage = DenseVecStorage<Self>;
}
#[derive(Debug)]
pub struct Skin {
pub joints: Vec<Entity>,
pub meshes: BitSet,
pub bind_shape_matrix: Matrix4<f32>,
}
impl Component for Skin {
type Storage = DenseVecStorage<Self>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JointPrefab {
pub skin: usize,
pub inverse_bind_matrix: Matrix4<f32>,
}
impl<'a> PrefabData<'a> for JointPrefab {
type SystemData = WriteStorage<'a, Joint>;
type Result = ();
fn load_prefab(
&self,
entity: Entity,
storage: &mut Self::SystemData,
entities: &[Entity],
) -> Result<(), PrefabError> {
storage
.insert(
entity,
Joint {
skin: entities[self.skin],
inverse_bind_matrix: self.inverse_bind_matrix,
},
)
.map(|_| ())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SkinPrefab {
pub joints: Vec<usize>,
pub bind_shape_matrix: Matrix4<f32>,
pub meshes: Vec<usize>,
}
impl<'a> PrefabData<'a> for SkinPrefab {
type SystemData = WriteStorage<'a, Skin>;
type Result = ();
fn load_prefab(
&self,
entity: Entity,
storage: &mut Self::SystemData,
entities: &[Entity],
) -> Result<(), PrefabError> {
storage
.insert(
entity,
Skin {
joints: self.joints.iter().map(|index| entities[*index]).collect(),
meshes: self.meshes
.iter()
.map(|index| entities[*index].id())
.collect(),
bind_shape_matrix: self.bind_shape_matrix,
},
)
.map(|_| ())
}
}
#[derive(Clone, Default, Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct SkinnablePrefab {
pub skin: Option<SkinPrefab>,
pub joint: Option<JointPrefab>,
pub joint_transforms: Option<JointTransformsPrefab>,
}
impl<'a> PrefabData<'a> for SkinnablePrefab {
type SystemData = (
<SkinPrefab as PrefabData<'a>>::SystemData,
<JointPrefab as PrefabData<'a>>::SystemData,
<JointTransformsPrefab as PrefabData<'a>>::SystemData,
);
type Result = ();
fn load_prefab(
&self,
entity: Entity,
system_data: &mut Self::SystemData,
entities: &[Entity],
) -> Result<(), PrefabError> {
if let Some(ref prefab) = self.skin {
prefab.load_prefab(entity, &mut system_data.0, entities)?;
}
if let Some(ref prefab) = self.joint {
prefab.load_prefab(entity, &mut system_data.1, entities)?;
}
if let Some(ref prefab) = self.joint_transforms {
prefab.load_prefab(entity, &mut system_data.2, entities)?;
}
Ok(())
}
}