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
//! Port of `three.js/src/objects/Skeleton.js`.
//!
//! `boneTexture` is the renderer's business (Three allocates a `DataTexture`
//! from `boneMatrices` in `Skeleton.computeBoneTexture`), so it is left as the
//! [`Skeleton::bone_matrices`] hook; the
//! renderer fills it in at rung 10.
use crate::core::Node;
use crate::math::Matrix4;
/// `class Skeleton`.
#[derive(Debug, Default)]
pub struct Skeleton {
/// `Skeleton.bones`.
pub bones: Vec<Node>,
/// `Skeleton.boneInverses`.
pub bone_inverses: Vec<Matrix4>,
/// `Skeleton.boneMatrices` — `new Float32Array( bones.length * 16 )`,
/// filled by [`Skeleton::update`].
pub bone_matrices: Vec<f32>,
/// `Skeleton.frame`, the renderer's "did the texture change" counter.
pub frame: i64,
}
impl Skeleton {
/// `new Skeleton( bones, boneInverses )`. `None` for the inverses is
/// three.js' `boneInverses === undefined` branch, which calls
/// `calculateInverses()`.
pub fn new(bones: Vec<Node>, bone_inverses: Option<Vec<Matrix4>>) -> Self {
let mut skeleton = Self {
bones,
bone_inverses: Vec::new(),
bone_matrices: Vec::new(),
frame: -1,
};
skeleton.init(bone_inverses);
skeleton
}
/// `Skeleton.init()`.
pub fn init(&mut self, bone_inverses: Option<Vec<Matrix4>>) {
self.bone_matrices = vec![0.0; self.bones.len() * 16];
match bone_inverses {
None => self.calculate_inverses(),
Some(inverses) => {
if inverses.len() == self.bones.len() {
self.bone_inverses = inverses;
} else {
// `THREE.Skeleton: Number of inverse bone matrices does not
// match amount of bones.`
self.bone_inverses = vec![Matrix4::identity(); self.bones.len()];
}
}
}
}
/// `Skeleton.calculateInverses()`.
pub fn calculate_inverses(&mut self) {
self.bone_inverses.clear();
for bone in &self.bones {
let mut inverse = bone.borrow().matrix_world;
inverse.invert();
self.bone_inverses.push(inverse);
}
}
/// `Skeleton.pose()` — put the skeleton back into its bind pose.
pub fn pose(&mut self) {
// recover the bind-time world matrices
for (bone, inverse) in self.bones.iter().zip(self.bone_inverses.iter()) {
let mut world = *inverse;
world.invert();
bone.borrow_mut().matrix_world = world;
}
// compute the local matrices, positions, rotations and scales
for bone in &self.bones {
let parent_world = bone.parent().map(|parent| parent.borrow().matrix_world);
let matrix = match parent_world {
Some(parent_world) => {
let mut inverse = parent_world;
inverse.invert();
let mut matrix = Matrix4::identity();
let world = bone.borrow().matrix_world;
matrix.multiply_matrices(&inverse, &world);
matrix
}
None => bone.borrow().matrix_world,
};
let mut object = bone.borrow_mut();
object.matrix.copy(&matrix);
let matrix = object.matrix;
let (mut position, mut quaternion, mut scale) =
(object.position, object.quaternion, object.scale);
matrix.decompose(&mut position, &mut quaternion, &mut scale);
object.position = position;
object.quaternion = quaternion;
object.scale = scale;
object.sync_rotation_from_quaternion();
}
}
/// `Skeleton.update()` — flatten `boneMatrix = boneWorld * boneInverse`
/// into `boneMatrices`.
pub fn update(&mut self) {
let mut offset = 0;
for (bone, inverse) in self.bones.iter().zip(self.bone_inverses.iter()) {
// `// compute the offset between the current and the original
// transformation`
let world = bone.borrow().matrix_world;
let mut matrix = Matrix4::identity();
matrix.multiply_matrices(&world, inverse);
for (i, value) in matrix.elements.iter().enumerate() {
self.bone_matrices[offset + i] = *value as f32;
}
offset += 16;
}
}
/// `Skeleton.getBoneByName( name )`.
pub fn get_bone_by_name(&self, name: &str) -> Option<Node> {
self.bones
.iter()
.find(|bone| bone.borrow().name == name)
.cloned()
}
}