1
0

add timeline for anime loader

This commit is contained in:
2026-01-10 20:29:50 +08:00
parent 2379a23477
commit 9c70ea9b6d
3 changed files with 78 additions and 45 deletions

View File

@@ -4,42 +4,42 @@ using ::basalt::shared::math::FloatPoint;
namespace basalt::shared::anime_loader {
#pragma region Anime Loader
#pragma region Timeline
IAnimeLoader::IAnimeLoader() : status(AnimeLoaderStatus::Ready), time(0) {}
IAnimeLoader::~IAnimeLoader() {}
void IAnimeLoader::load(AnimeLoaderConfig&& config) {
if (this->status != AnimeLoaderStatus::Ready) throw std::runtime_error("unexpected anime loader status");
this->config = std::move(config);
this->status = AnimeLoaderStatus::Loaded;
Timeline::Timeline() : keyframes(), max_frame_cache(0) {
// insert default frame for first index
this->keyframes[0] = KeyFrame{.position = {0, 0, 0}, .rotation = {0, 0, 0, 1}};
}
KeyFrameSpan IAnimeLoader::tick() {
if (this->status != AnimeLoaderStatus::Loaded) throw std::runtime_error("unexpected anime loader status");
Timeline::~Timeline() {}
// Get last frame index
auto last_pair = this->frames.end();
--last_pair;
auto last_index = last_pair->first;
// Accumulate time
++this->time;
if (this->time > last_index) {
this->time = 0;
void Timeline::add_key_frame(math::Index frame_index, KeyFrame frame_data) {
// insert data
this->keyframes[frame_index] = frame_data;
// update max frame
this->max_frame_cache = keyframes.rbegin()->first;
}
math::Index Timeline::max_frame() const {
return this->max_frame_cache;
}
KeyFrameSpan Timeline::tick(math::Index frame_index) const {
if (this->keyframes.empty()) [[unlikely]] {
throw std::runtime_error("No key frames in timeline");
}
// 首先用lower_bound获取到不小于自身的项目肯定能获取到
auto lower_bound = this->frames.lower_bound(this->time);
auto lower_bound = this->keyframes.lower_bound(frame_index);
// 获取下一帧,如果没有下一帧,就转换为自身
auto upper_bound = lower_bound;
++upper_bound;
if (upper_bound == this->frames.end()) {
if (upper_bound == this->keyframes.end()) {
upper_bound = lower_bound;
}
// 计算归一化时间
FloatPoint next_time = static_cast<FloatPoint>(this->time - lower_bound->first)
FloatPoint next_time = static_cast<FloatPoint>(frame_index - lower_bound->first)
/ static_cast<FloatPoint>(upper_bound->first - lower_bound->first + 1);
FloatPoint prev_time = static_cast<FloatPoint>(1) - next_time;
@@ -56,4 +56,30 @@ namespace basalt::shared::anime_loader {
#pragma endregion
#pragma region Anime Loader
IAnimeLoader::IAnimeLoader() : status(AnimeLoaderStatus::Ready), time(0), timeline() {}
IAnimeLoader::~IAnimeLoader() {}
void IAnimeLoader::load(AnimeLoaderConfig&& config) {
if (this->status != AnimeLoaderStatus::Ready) throw std::runtime_error("unexpected anime loader status");
this->config = std::move(config);
this->status = AnimeLoaderStatus::Loaded;
}
KeyFrameSpan IAnimeLoader::tick() {
if (this->status != AnimeLoaderStatus::Loaded) throw std::runtime_error("unexpected anime loader status");
// Accumulate time
++this->time;
if (this->time > this->timeline.max_frame()) {
this->time = 0;
}
// Return from timeline
return this->timeline.tick(this->time);
}
#pragma endregion
} // namespace basalt::shared::anime_loader