Mostly fixed up load_keyframe_data. Switched from using a curve to using transition easing.

This commit is contained in:
Relintai 2020-04-01 02:09:42 +02:00
parent ab4a274057
commit 1fae51e51f
4 changed files with 137 additions and 57 deletions

View File

@ -166,6 +166,20 @@ void ProceduralAnimation::set_keyframe_next_keyframe_index(const int keyframe_in
process_animation_data(); process_animation_data();
} }
float ProceduralAnimation::get_keyframe_transition(const int keyframe_index) const {
ERR_FAIL_COND_V(!_keyframes.has(keyframe_index), 0);
return _keyframes[keyframe_index]->transition;
}
void ProceduralAnimation::set_keyframe_transition(const int keyframe_index, const float value) {
ERR_FAIL_COND(!_keyframes.has(keyframe_index));
_keyframes[keyframe_index]->transition = value;
process_animation_data();
}
/*
Ref<Curve> ProceduralAnimation::get_keyframe_in_curve(const int keyframe_index) const { Ref<Curve> ProceduralAnimation::get_keyframe_in_curve(const int keyframe_index) const {
ERR_FAIL_COND_V(!_keyframes.has(keyframe_index), Ref<Curve>()); ERR_FAIL_COND_V(!_keyframes.has(keyframe_index), Ref<Curve>());
@ -177,7 +191,7 @@ void ProceduralAnimation::set_keyframe_in_curve(const int keyframe_index, const
_keyframes[keyframe_index]->in_curve = value; _keyframes[keyframe_index]->in_curve = value;
process_animation_data(); process_animation_data();
} }*/
Vector2 ProceduralAnimation::get_keyframe_node_position(const int keyframe_index) const { Vector2 ProceduralAnimation::get_keyframe_node_position(const int keyframe_index) const {
ERR_FAIL_COND_V(!_keyframes.has(keyframe_index), Vector2()); ERR_FAIL_COND_V(!_keyframes.has(keyframe_index), Vector2());
@ -201,8 +215,7 @@ void ProceduralAnimation::process_animation_data() {
add_track(type); add_track(type);
//TODO setting for this track_set_interpolation_type(i, _animation->track_get_interpolation_type(i));
track_set_interpolation_type(i, Animation::INTERPOLATION_CUBIC);
track_set_interpolation_loop_wrap(i, _animation->track_get_interpolation_loop_wrap(i)); track_set_interpolation_loop_wrap(i, _animation->track_get_interpolation_loop_wrap(i));
track_set_path(i, _animation->track_get_path(i)); track_set_path(i, _animation->track_get_path(i));
track_set_enabled(i, _animation->track_is_enabled(i)); track_set_enabled(i, _animation->track_is_enabled(i));
@ -229,67 +242,98 @@ void ProceduralAnimation::process_animation_data() {
} }
} }
float previous_keyframe_time = 0;
float target_keyframe_time = 0; float target_keyframe_time = 0;
for (Map<int, AnimationKeyFrame *>::Element *K = _keyframes.front(); K; K = K->next()) { for (Map<int, AnimationKeyFrame *>::Element *K = _keyframes.front(); K; K = K->next()) {
int keyframe_index = K->get()->animation_keyframe_index; int keyframe_index = K->get()->animation_keyframe_index;
load_keyframe_data(target_keyframe_time, keyframe_index); load_keyframe_data(target_keyframe_time, previous_keyframe_time, keyframe_index);
//TODO add param //TODO add param
previous_keyframe_time = target_keyframe_time;
target_keyframe_time += 1.0; target_keyframe_time += 1.0;
} }
} }
void ProceduralAnimation::load_keyframe_data(const float target_keyframe_time, const int keyframe_index, const bool interpolation_allowed) { void ProceduralAnimation::load_keyframe_data(const float target_keyframe_time, const float previous_keyframe_time, const int keyframe_index, const bool interpolation_allowed) {
ERR_FAIL_COND(!_animation.is_valid()); ERR_FAIL_COND(!_animation.is_valid());
ERR_FAIL_COND(!_keyframes.has(keyframe_index));
float time = keyframe_index * _animation->get_length() / static_cast<float>(_animation_fps); float time = keyframe_index * _animation->get_length() / static_cast<float>(_animation_fps);
float step_time = 1 / static_cast<float>(_animation_fps);
int steps = static_cast<int>((target_keyframe_time - previous_keyframe_time) / step_time + 0.5);
for (int i = 0; i < _animation->get_track_count(); ++i) { for (int i = 0; i < _animation->get_track_count(); ++i) {
int key_index = _animation->track_find_key(i, time, true); int key_index = _animation->track_find_key(i, time, true);
Variant key_value;
if (key_index == -1) { if (key_index == -1) {
if (!interpolation_allowed) { key_index = _animation->track_find_key(i, time, false);
key_index = _animation->track_find_key(i, time, false);
if (key_index != -1) if (key_index != -1)
track_insert_key(i, target_keyframe_time, _animation->track_get_key_value(i, key_index)); key_value = _animation->track_get_key_value(i, key_index);
/*
else {
if (interpolation_allowed) {
//track doesn't have a key at the specified time. Try to create one with interpolations.
continue; Animation::TrackType tt = _animation->track_get_type(i);
}
//track doesn't have a key at the specified time. Try to create one with interpolations. switch (tt) {
case Animation::TYPE_VALUE: {
Variant val = value_track_interpolate(i, time);
Animation::TrackType tt = _animation->track_get_type(i); track_insert_key(i, target_keyframe_time, val);
} break;
case Animation::TYPE_TRANSFORM: {
Vector3 loc;
Quat rot;
Vector3 scale;
switch (tt) { if (_animation->transform_track_interpolate(i, time, &loc, &rot, &scale) == OK) {
case Animation::TYPE_VALUE: { transform_track_insert_key(i, target_keyframe_time, loc, rot, scale);
Variant val = value_track_interpolate(i, time); }
} break;
track_insert_key(i, target_keyframe_time, val); case Animation::TYPE_METHOD: {
} break; } break;
case Animation::TYPE_TRANSFORM: { case Animation::TYPE_BEZIER: {
Vector3 loc; } break;
Quat rot; case Animation::TYPE_AUDIO: {
Vector3 scale; } break;
case Animation::TYPE_ANIMATION: {
if (_animation->transform_track_interpolate(i, time, &loc, &rot, &scale) == OK) { } break;
transform_track_insert_key(i, target_keyframe_time, loc, rot, scale);
} }
} break; }
case Animation::TYPE_METHOD: { }*/
} break;
case Animation::TYPE_BEZIER: {
} break;
case Animation::TYPE_AUDIO: {
} break;
case Animation::TYPE_ANIMATION: {
} break;
}
} else {
track_insert_key(i, target_keyframe_time, _animation->track_get_key_value(i, key_index));
} }
if (key_value.get_type() == Variant::NIL)
continue;
AnimationKeyFrame *frame = _keyframes[keyframe_index];
track_insert_key(i, target_keyframe_time, key_value, frame->transition);
/*
if (!frame->in_curve.is_valid()) {
track_insert_key(i, target_keyframe_time, key_value);
continue;
}
Ref<Curve> in_curve = frame->in_curve;
float targ_step_val = (target_keyframe_time - previous_keyframe_time) / static_cast<float>(steps);
float step_val = 1.0 / static_cast<float>(steps);
for (int j = 0; j < steps; ++j) {
float tr = in_curve->interpolate(j * step_val);
track_insert_key(i, previous_keyframe_time + (j * targ_step_val), key_value, tr);
}
*/
} }
optimize();
} }
ProceduralAnimation::ProceduralAnimation() { ProceduralAnimation::ProceduralAnimation() {
@ -342,10 +386,13 @@ bool ProceduralAnimation::_set(const StringName &p_name, const Variant &p_value)
keyframe->next_keyframe = p_value; keyframe->next_keyframe = p_value;
return true; return true;
} else if (keyframe_name == "in_curve") { } else if (keyframe_name == "transition") {
keyframe->in_curve = p_value; keyframe->transition = p_value;
return true; return true;
//} else if (keyframe_name == "in_curve") {
// keyframe->in_curve = p_value;
// return true;
} else if (keyframe_name == "position") { } else if (keyframe_name == "position") {
keyframe->position = p_value; keyframe->position = p_value;
@ -389,10 +436,14 @@ bool ProceduralAnimation::_get(const StringName &p_name, Variant &r_ret) const {
r_ret = keyframe->next_keyframe; r_ret = keyframe->next_keyframe;
return true; return true;
} else if (keyframe_prop_name == "in_curve") { } else if (keyframe_prop_name == "transition") {
r_ret = keyframe->in_curve; r_ret = keyframe->transition;
return true; return true;
//} else if (keyframe_prop_name == "in_curve") {
// r_ret = keyframe->in_curve;
// return true;
} else if (keyframe_prop_name == "position") { } else if (keyframe_prop_name == "position") {
r_ret = keyframe->position; r_ret = keyframe->position;
@ -418,7 +469,8 @@ void ProceduralAnimation::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::STRING, "keyframe/" + itos(K->key()) + "/name", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::STRING, "keyframe/" + itos(K->key()) + "/name", PROPERTY_HINT_NONE, "", property_usange));
p_list->push_back(PropertyInfo(Variant::INT, "keyframe/" + itos(K->key()) + "/animation_keyframe_index", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::INT, "keyframe/" + itos(K->key()) + "/animation_keyframe_index", PROPERTY_HINT_NONE, "", property_usange));
p_list->push_back(PropertyInfo(Variant::INT, "keyframe/" + itos(K->key()) + "/next_keyframe", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::INT, "keyframe/" + itos(K->key()) + "/next_keyframe", PROPERTY_HINT_NONE, "", property_usange));
p_list->push_back(PropertyInfo(Variant::OBJECT, "keyframe/" + itos(K->key()) + "/in_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve", property_usange)); //p_list->push_back(PropertyInfo(Variant::OBJECT, "keyframe/" + itos(K->key()) + "/in_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve", property_usange));
p_list->push_back(PropertyInfo(Variant::REAL, "keyframe/" + itos(K->key()) + "/transition", PROPERTY_HINT_EXP_EASING, "", property_usange));
p_list->push_back(PropertyInfo(Variant::VECTOR2, "keyframe/" + itos(K->key()) + "/position", PROPERTY_HINT_NONE, "", property_usange)); p_list->push_back(PropertyInfo(Variant::VECTOR2, "keyframe/" + itos(K->key()) + "/position", PROPERTY_HINT_NONE, "", property_usange));
} }
} }
@ -458,8 +510,11 @@ void ProceduralAnimation::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_keyframe_next_keyframe_index", "keyframe_index"), &ProceduralAnimation::get_keyframe_next_keyframe_index); ClassDB::bind_method(D_METHOD("get_keyframe_next_keyframe_index", "keyframe_index"), &ProceduralAnimation::get_keyframe_next_keyframe_index);
ClassDB::bind_method(D_METHOD("set_keyframe_next_keyframe_index", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_next_keyframe_index); ClassDB::bind_method(D_METHOD("set_keyframe_next_keyframe_index", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_next_keyframe_index);
ClassDB::bind_method(D_METHOD("get_keyframe_in_curve", "keyframe_index"), &ProceduralAnimation::get_keyframe_in_curve); ClassDB::bind_method(D_METHOD("get_keyframe_transition", "keyframe_index"), &ProceduralAnimation::get_keyframe_transition);
ClassDB::bind_method(D_METHOD("set_keyframe_in_curve", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_in_curve); ClassDB::bind_method(D_METHOD("set_keyframe_transition", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_transition);
//ClassDB::bind_method(D_METHOD("get_keyframe_in_curve", "keyframe_index"), &ProceduralAnimation::get_keyframe_in_curve);
//ClassDB::bind_method(D_METHOD("set_keyframe_in_curve", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_in_curve);
ClassDB::bind_method(D_METHOD("get_keyframe_node_position", "keyframe_index"), &ProceduralAnimation::get_keyframe_node_position); ClassDB::bind_method(D_METHOD("get_keyframe_node_position", "keyframe_index"), &ProceduralAnimation::get_keyframe_node_position);
ClassDB::bind_method(D_METHOD("set_keyframe_node_position", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_node_position); ClassDB::bind_method(D_METHOD("set_keyframe_node_position", "keyframe_index", "value"), &ProceduralAnimation::set_keyframe_node_position);

View File

@ -41,17 +41,19 @@ protected:
String name; String name;
int animation_keyframe_index; int animation_keyframe_index;
int next_keyframe; int next_keyframe;
Ref<Curve> in_curve; //Ref<Curve> in_curve;
Vector2 position; Vector2 position;
float transition;
AnimationKeyFrame() { AnimationKeyFrame() {
animation_keyframe_index = 0; animation_keyframe_index = 0;
transition = 1.0;
next_keyframe = -1; next_keyframe = -1;
in_curve.instance(); //in_curve.instance();
} }
~AnimationKeyFrame() { ~AnimationKeyFrame() {
in_curve.unref(); //in_curve.unref();
} }
}; };
@ -88,14 +90,17 @@ public:
int get_keyframe_next_keyframe_index(const int keyframe_index) const; int get_keyframe_next_keyframe_index(const int keyframe_index) const;
void set_keyframe_next_keyframe_index(const int keyframe_index, const int value); void set_keyframe_next_keyframe_index(const int keyframe_index, const int value);
Ref<Curve> get_keyframe_in_curve(const int keyframe_index) const; float get_keyframe_transition(const int keyframe_index) const;
void set_keyframe_in_curve(const int keyframe_index, const Ref<Curve> &value); void set_keyframe_transition(const int keyframe_index, const float value);
//Ref<Curve> get_keyframe_in_curve(const int keyframe_index) const;
//void set_keyframe_in_curve(const int keyframe_index, const Ref<Curve> &value);
Vector2 get_keyframe_node_position(const int keyframe_index) const; Vector2 get_keyframe_node_position(const int keyframe_index) const;
void set_keyframe_node_position(const int keyframe_index, const Vector2 &value); void set_keyframe_node_position(const int keyframe_index, const Vector2 &value);
void process_animation_data(); void process_animation_data();
void load_keyframe_data(const float keyframe_time, const int keyframe_index, const bool interpolation_allowed = false); void load_keyframe_data(const float keyframe_time, const float previous_keyframe_time, const int keyframe_index, const bool interpolation_allowed = false);
ProceduralAnimation(); ProceduralAnimation();
~ProceduralAnimation(); ~ProceduralAnimation();

View File

@ -310,6 +310,21 @@ void ProceduralAnimationEditorGraphNode::set_next_keyframe(const int value) {
changed(); changed();
} }
float ProceduralAnimationEditorGraphNode::get_transition() const {
return _transition;
}
void ProceduralAnimationEditorGraphNode::set_transition(const float value) {
_transition = value;
if (!_animation.is_valid())
return;
_animation->set_keyframe_transition(_id, value);
changed();
}
/*
Ref<Curve> ProceduralAnimationEditorGraphNode::get_in_curve() const { Ref<Curve> ProceduralAnimationEditorGraphNode::get_in_curve() const {
return _in_curve; return _in_curve;
} }
@ -328,7 +343,7 @@ void ProceduralAnimationEditorGraphNode::set_in_curve(const Ref<Curve> &value) {
_animation->set_keyframe_in_curve(_id, _in_curve); _animation->set_keyframe_in_curve(_id, _in_curve);
changed(); changed();
} }*/
Ref<ProceduralAnimation> ProceduralAnimationEditorGraphNode::get_animation() { Ref<ProceduralAnimation> ProceduralAnimationEditorGraphNode::get_animation() {
return _animation; return _animation;
@ -342,7 +357,8 @@ void ProceduralAnimationEditorGraphNode::set_animation(const Ref<ProceduralAnima
set_offset(animation->get_keyframe_node_position(_id)); set_offset(animation->get_keyframe_node_position(_id));
set_keyframe_name(animation->get_keyframe_name(_id)); set_keyframe_name(animation->get_keyframe_name(_id));
set_next_keyframe(animation->get_keyframe_next_keyframe_index(_id)); set_next_keyframe(animation->get_keyframe_next_keyframe_index(_id));
set_in_curve(animation->get_keyframe_in_curve(_id)); set_transition(animation->get_keyframe_transition(_id));
//set_in_curve(animation->get_keyframe_in_curve(_id));
set_animation_keyframe_index(animation->get_keyframe_animation_keyframe_index(_id)); set_animation_keyframe_index(animation->get_keyframe_animation_keyframe_index(_id));
_animation = animation; _animation = animation;
@ -387,7 +403,7 @@ ProceduralAnimationEditorGraphNode::ProceduralAnimationEditorGraphNode() {
} }
ProceduralAnimationEditorGraphNode::~ProceduralAnimationEditorGraphNode() { ProceduralAnimationEditorGraphNode::~ProceduralAnimationEditorGraphNode() {
_in_curve.unref(); //_in_curve.unref();
} }
void ProceduralAnimationEditorGraphNode::on_animation_keyframe_spinbox_value_changed(float value) { void ProceduralAnimationEditorGraphNode::on_animation_keyframe_spinbox_value_changed(float value) {

View File

@ -97,8 +97,11 @@ public:
int get_next_keyframe() const; int get_next_keyframe() const;
void set_next_keyframe(const int value); void set_next_keyframe(const int value);
Ref<Curve> get_in_curve() const; //Ref<Curve> get_in_curve() const;
void set_in_curve(const Ref<Curve> &value); //void set_in_curve(const Ref<Curve> &value);
float get_transition() const;
void set_transition(const float value);
Ref<ProceduralAnimation> get_animation(); Ref<ProceduralAnimation> get_animation();
void set_animation(const Ref<ProceduralAnimation> &animation); void set_animation(const Ref<ProceduralAnimation> &animation);
@ -123,7 +126,8 @@ private:
int _animation_keyframe_index; int _animation_keyframe_index;
int _next_keyframe; int _next_keyframe;
CurveEditor *_curve_editor; CurveEditor *_curve_editor;
Ref<Curve> _in_curve; //Ref<Curve> _in_curve;
float _transition;
Ref<ProceduralAnimation> _animation; Ref<ProceduralAnimation> _animation;
}; };