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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use anims::phase::Phase;
use core::{
    assets::protocol::{AssetLoadResult, AssetProtocol},
    Ignite, Scalar,
};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::from_utf8};

#[derive(Ignite, Debug, Clone, Serialize, Deserialize)]
pub struct MeshAnimationSequence {
    #[serde(default)]
    pub submesh_alpha: HashMap<usize, Phase>,
    #[serde(default)]
    pub submesh_order: HashMap<usize, Phase>,
    #[serde(default)]
    pub bone_position_x: HashMap<String, Phase>,
    #[serde(default)]
    pub bone_position_y: HashMap<String, Phase>,
    #[serde(default)]
    pub bone_rotation: HashMap<String, Phase>,
    #[serde(default)]
    pub bone_scale_x: HashMap<String, Phase>,
    #[serde(default)]
    pub bone_scale_y: HashMap<String, Phase>,
    #[serde(skip)]
    #[ignite(ignore)]
    length: Scalar,
}

impl MeshAnimationSequence {
    pub fn initialize(&mut self) {
        self.length = 0.0;
        for key_frames in self.submesh_alpha.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.submesh_order.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.bone_position_x.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.bone_position_y.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.bone_rotation.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.bone_scale_x.values() {
            self.length = self.length.max(key_frames.duration());
        }
        for key_frames in self.bone_scale_y.values() {
            self.length = self.length.max(key_frames.duration());
        }
    }

    pub fn length(&self) -> Scalar {
        self.length
    }

    pub fn sample_submesh_alpha(&self, time: Scalar, index: usize, current: Scalar) -> Scalar {
        Self::sample_indexed(&self.submesh_alpha, time, index, current)
    }

    pub fn sample_submesh_order(&self, time: Scalar, index: usize, current: Scalar) -> Scalar {
        Self::sample_indexed(&self.submesh_order, time, index, current)
    }

    pub fn sample_bone_position_x(&self, time: Scalar, name: &str, current: Scalar) -> Scalar {
        Self::sample_named(&self.bone_position_x, time, name, current)
    }

    pub fn sample_bone_position_y(&self, time: Scalar, name: &str, current: Scalar) -> Scalar {
        Self::sample_named(&self.bone_position_y, time, name, current)
    }

    pub fn sample_bone_rotation(&self, time: Scalar, name: &str, current: Scalar) -> Scalar {
        Self::sample_named(&self.bone_rotation, time, name, current)
    }

    pub fn sample_bone_scale_x(&self, time: Scalar, name: &str, current: Scalar) -> Scalar {
        Self::sample_named(&self.bone_scale_x, time, name, current)
    }

    pub fn sample_bone_scale_y(&self, time: Scalar, name: &str, current: Scalar) -> Scalar {
        Self::sample_named(&self.bone_scale_y, time, name, current)
    }

    fn sample_key_frames(key_frames: &Phase, time: Scalar, current: Scalar) -> Scalar {
        if key_frames.points().is_empty() {
            current
        } else {
            key_frames.sample(time)
        }
    }

    fn sample_indexed(
        database: &HashMap<usize, Phase>,
        time: Scalar,
        index: usize,
        current: Scalar,
    ) -> Scalar {
        database
            .get(&index)
            .map(|key_frames| Self::sample_key_frames(key_frames, time, current))
            .unwrap_or(current)
    }

    fn sample_named(
        database: &HashMap<String, Phase>,
        time: Scalar,
        name: &str,
        current: Scalar,
    ) -> Scalar {
        database
            .get(name)
            .map(|key_frames| Self::sample_key_frames(key_frames, time, current))
            .unwrap_or(current)
    }
}

#[derive(Ignite, Debug, Clone, Serialize, Deserialize)]
pub struct MeshAnimation {
    pub sequences: HashMap<String, MeshAnimationSequence>,
}

impl MeshAnimation {
    pub fn initialize(&mut self) {
        for seq in self.sequences.values_mut() {
            seq.initialize();
        }
    }
}

pub struct MeshAnimationAsset(MeshAnimation);

impl MeshAnimationAsset {
    pub fn animation(&self) -> &MeshAnimation {
        &self.0
    }
}

pub struct MeshAnimationAssetProtocol;

impl AssetProtocol for MeshAnimationAssetProtocol {
    fn name(&self) -> &str {
        "mesh-anim"
    }

    fn on_load_with_path(&mut self, path: &str, data: Vec<u8>) -> AssetLoadResult {
        let mut anim = if path.ends_with(".json") {
            let data = from_utf8(&data).unwrap();
            serde_json::from_str::<MeshAnimation>(&data).unwrap()
        } else if path.ends_with(".yaml") {
            let data = from_utf8(&data).unwrap();
            serde_yaml::from_str::<MeshAnimation>(&data).unwrap()
        } else {
            bincode::deserialize::<MeshAnimation>(&data).unwrap()
        };
        anim.initialize();
        AssetLoadResult::Data(Box::new(MeshAnimationAsset(anim)))
    }

    // on_load_with_path() handles loading so this is not needed, so we just make it unreachable.
    fn on_load(&mut self, _data: Vec<u8>) -> AssetLoadResult {
        unreachable!()
    }
}