Added initial support for blending of SyncTracks with differing numbers of intervals.
Not sure that the resulting blends are correct, but leave it for now.
This commit is contained in:
parent
c7660c7b19
commit
88b1caf885
@ -385,6 +385,10 @@ StringName BLTAnimationNodeSampler::get_animation() const {
|
||||
return animation_name;
|
||||
}
|
||||
|
||||
AnimationPlayer *BLTAnimationNodeSampler::get_animation_player() const {
|
||||
return animation_player;
|
||||
}
|
||||
|
||||
TypedArray<StringName> BLTAnimationNodeSampler::get_animations_as_typed_array() const {
|
||||
TypedArray<StringName> typed_arr;
|
||||
|
||||
@ -417,6 +421,7 @@ TypedArray<StringName> BLTAnimationNodeSampler::get_animations_as_typed_array()
|
||||
void BLTAnimationNodeSampler::_bind_methods() {
|
||||
ClassDB::bind_method(D_METHOD("set_animation", "name"), &BLTAnimationNodeSampler::set_animation);
|
||||
ClassDB::bind_method(D_METHOD("get_animation"), &BLTAnimationNodeSampler::get_animation);
|
||||
ClassDB::bind_method(D_METHOD("get_animation_player"), &BLTAnimationNodeSampler::get_animation_player);
|
||||
|
||||
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation"), "set_animation", "get_animation");
|
||||
|
||||
|
||||
22
blendalot_math_helper.h
Normal file
22
blendalot_math_helper.h
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// Created by martin on 20.02.26.
|
||||
//
|
||||
|
||||
#ifndef MASTER_BLENDALOT_MATH_HELPER_H
|
||||
#define MASTER_BLENDALOT_MATH_HELPER_H
|
||||
|
||||
inline int greatest_common_divisor(int a, int b) {
|
||||
while (b != 0) {
|
||||
int temp = b;
|
||||
b = a % b;
|
||||
a = temp;
|
||||
}
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
inline int least_common_multiple(int a, int b) {
|
||||
return (a / greatest_common_divisor(a, b)) * b;
|
||||
}
|
||||
|
||||
#endif //MASTER_BLENDALOT_MATH_HELPER_H
|
||||
36
sync_track.h
36
sync_track.h
@ -2,6 +2,7 @@
|
||||
|
||||
#include "core/templates/local_vector.h"
|
||||
|
||||
#include "blendalot_math_helper.h"
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
|
||||
@ -21,7 +22,7 @@
|
||||
* duration. Blended SyncTracks always have their first interval start at t = 0.0s.
|
||||
*/
|
||||
struct SyncTrack {
|
||||
static constexpr int cSyncTrackMaxIntervals = 8;
|
||||
static constexpr int cSyncTrackMaxIntervals = 32;
|
||||
|
||||
SyncTrack() :
|
||||
duration(0.f), num_intervals(1) {
|
||||
@ -59,6 +60,12 @@ struct SyncTrack {
|
||||
}
|
||||
|
||||
double calc_ratio_from_sync_time(double sync_time) const {
|
||||
// When blending SyncTracks with differing numbers of intervals the resulting SyncTrack may have
|
||||
// additional repeats of the animation (=> "virtual sync periods", https://youtu.be/Jkv0pbp0ckQ?t=8178).
|
||||
//
|
||||
// Therefore, we first have to transform it back to the numbers of intervals we actually have.
|
||||
sync_time = fmod(sync_time, num_intervals);
|
||||
|
||||
float interval_ratio = fmod(sync_time, 1.0f);
|
||||
int interval = int(sync_time - interval_ratio);
|
||||
|
||||
@ -126,19 +133,32 @@ struct SyncTrack {
|
||||
*/
|
||||
static SyncTrack
|
||||
blend(float weight, const SyncTrack &track_A, const SyncTrack &track_B) {
|
||||
assert(track_A.num_intervals == track_B.num_intervals);
|
||||
if (Math::is_zero_approx(weight)) {
|
||||
return track_A;
|
||||
}
|
||||
|
||||
if (Math::is_zero_approx(1.0 - weight)) {
|
||||
return track_B;
|
||||
}
|
||||
|
||||
SyncTrack result;
|
||||
result.num_intervals = track_A.num_intervals;
|
||||
|
||||
result.duration =
|
||||
(1.0f - weight) * track_A.duration + weight * track_B.duration;
|
||||
if (track_A.num_intervals != track_B.num_intervals) {
|
||||
result.num_intervals = least_common_multiple(track_A.num_intervals, track_B.num_intervals);
|
||||
} else {
|
||||
result.num_intervals = track_A.num_intervals;
|
||||
}
|
||||
assert(result.num_intervals < cSyncTrackMaxIntervals);
|
||||
|
||||
int track_A_repeats = result.num_intervals / track_A.num_intervals;
|
||||
int track_B_repeats = result.num_intervals / track_B.num_intervals;
|
||||
|
||||
result.duration = (1.0f - weight) * (track_A.duration * track_A_repeats) + weight * (track_B.duration * track_B_repeats);
|
||||
result.interval_start_ratio[0] = 0.f;
|
||||
|
||||
for (int i = 0; i < result.num_intervals; i++) {
|
||||
float interval_duration_A = track_A.interval_duration_ratio[i];
|
||||
float interval_duration_B = track_B.interval_duration_ratio[i];
|
||||
float interval_duration_A = track_A.interval_duration_ratio[i % track_A.num_intervals];
|
||||
float interval_duration_B = track_B.interval_duration_ratio[i % track_B.num_intervals];
|
||||
result.interval_duration_ratio[i] =
|
||||
(1.0f - weight) * interval_duration_A + weight * interval_duration_B;
|
||||
|
||||
@ -152,8 +172,6 @@ struct SyncTrack {
|
||||
}
|
||||
}
|
||||
|
||||
assert(result.num_intervals < cSyncTrackMaxIntervals);
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
@ -203,4 +203,46 @@ TEST_CASE("[Blendalot][SyncTrack] Sync Track blending") {
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace TestSyncedAnimationGraph
|
||||
TEST_CASE("[Blendalot][SyncTrack] Sync Track blending non-matching interval count") {
|
||||
SyncTrack track_a = SyncTrack::create_from_markers(2.0, { 0., 0.6, 1.8 });
|
||||
SyncTrack track_b = SyncTrack::create_from_markers(1.5f, { 1.05 });
|
||||
|
||||
WHEN("Blending two synctracks with weight 0.") {
|
||||
SyncTrack blended = SyncTrack::blend(0.f, track_a, track_b);
|
||||
|
||||
blended.duration = track_a.duration;
|
||||
blended.interval_start_ratio[0] = 0.0;
|
||||
for (int i = 0; i < track_a.num_intervals; i++) {
|
||||
CHECK(blended.interval_duration_ratio[i] == track_a.interval_duration_ratio[i]);
|
||||
}
|
||||
}
|
||||
WHEN("Blending two synctracks with weight 1.") {
|
||||
SyncTrack blended = SyncTrack::blend(1.f, track_a, track_b);
|
||||
|
||||
blended.duration = track_b.duration;
|
||||
blended.interval_start_ratio[0] = 0.0;
|
||||
for (int i = 0; i < track_b.num_intervals; i++) {
|
||||
CHECK(blended.interval_duration_ratio[i] == track_b.interval_duration_ratio[i]);
|
||||
}
|
||||
}
|
||||
|
||||
WHEN("Blending with weight 0.2") {
|
||||
float weight = 0.2f;
|
||||
SyncTrack blended = SyncTrack::blend(weight, track_a, track_b);
|
||||
|
||||
int track_b_repeats = 3;
|
||||
|
||||
REQUIRE(
|
||||
blended.duration == (1.0f - weight) * track_a.duration + weight * track_b.duration * track_b_repeats);
|
||||
REQUIRE(
|
||||
blended.interval_start_ratio[0] == 0.0);
|
||||
REQUIRE(
|
||||
blended.interval_duration_ratio[0] == (1.0f - weight) * (track_a.interval_duration_ratio[0]) + weight * (track_b.interval_duration_ratio[0]));
|
||||
REQUIRE(
|
||||
blended.interval_duration_ratio[1] == (1.0f - weight) * (track_a.interval_duration_ratio[1]) + weight * (track_b.interval_duration_ratio[0]));
|
||||
REQUIRE(
|
||||
blended.interval_duration_ratio[2] == (1.0f - weight) * (track_a.interval_duration_ratio[2]) + weight * (track_b.interval_duration_ratio[0]));
|
||||
}
|
||||
}
|
||||
|
||||
} //namespace TestBlendalotAnimationGraph
|
||||
Loading…
x
Reference in New Issue
Block a user