Started work on a better solution. Removed most of the things I added yesterday, also removed the ProceduralAnimationPlayer. ProceduralAnimation is now inherited from Animation.

This commit is contained in:
Relintai 2020-03-26 10:27:39 +01:00
parent 513e89b511
commit 424d721c68
8 changed files with 8 additions and 3159 deletions

1
SCsub
View File

@ -12,7 +12,6 @@ sources = [
"register_types.cpp", "register_types.cpp",
"procedural_animation.cpp", "procedural_animation.cpp",
"procedural_animation_player.cpp",
"procedural_animation_editor_plugin.cpp", "procedural_animation_editor_plugin.cpp",
] ]

File diff suppressed because it is too large Load Diff

View File

@ -23,96 +23,20 @@ SOFTWARE.
#ifndef PROCEDURAL_ANIMATION_H #ifndef PROCEDURAL_ANIMATION_H
#define PROCEDURAL_ANIMATION_H #define PROCEDURAL_ANIMATION_H
#include "core/resource.h" #include "scene/resources/animation.h"
#include "core/map.h" #include "core/map.h"
#include "core/math/vector2.h" #include "core/math/vector2.h"
#include "core/pool_vector.h" #include "core/pool_vector.h"
#include "core/vector.h" #include "core/vector.h"
#include "scene/resources/animation.h" #include "scene/resources/animation.h"
#include "scene/resources/curve.h" #include "scene/resources/curve.h"
#include "scene/resources/animation.h" class ProceduralAnimation : public Animation {
GDCLASS(ProceduralAnimation, Animation);
class ProceduralAnimation : public Resource {
GDCLASS(ProceduralAnimation, Resource);
friend class Animation;
protected: protected:
enum AnimationKeyTrackType {
TYPE_VALUE = Animation::TYPE_VALUE,
TYPE_TRANSFORM = Animation::TYPE_TRANSFORM,
TYPE_METHOD = Animation::TYPE_METHOD,
TYPE_BEZIER = Animation::TYPE_BEZIER,
TYPE_AUDIO = Animation::TYPE_AUDIO,
TYPE_ANIMATION = Animation::TYPE_ANIMATION,
TYPE_NONE,
};
enum KeyInterpolationType {
INTERPOLATION_NEAREST = Animation::INTERPOLATION_NEAREST,
INTERPOLATION_LINEAR = Animation::INTERPOLATION_LINEAR,
INTERPOLATION_CUBIC = Animation::INTERPOLATION_CUBIC,
INTERPOLATION_CURVE,
};
/* Key data */
struct AnimationKey {
AnimationKeyTrackType type;
NodePath path;
bool enabled;
AnimationKey() {
type = TYPE_NONE;
enabled = true;
}
};
struct VariantAnimationKey : public AnimationKey {
Variant value;
VariantAnimationKey() {
type = TYPE_VALUE;
}
};
struct TransformAnimationKey : public AnimationKey {
Vector3 loc;
Quat rot;
Vector3 scale;
TransformAnimationKey() :
AnimationKey() {
type = TYPE_TRANSFORM;
}
};
struct MethodAnimationKey : public AnimationKey {
StringName method;
Vector<Variant> params;
MethodAnimationKey() :
AnimationKey() {
type = TYPE_METHOD;
}
};
struct AudioAnimationKey : public AnimationKey {
RES stream;
float start_offset;
float end_offset;
AudioAnimationKey() :
AnimationKey() {
type = TYPE_AUDIO;
start_offset = 0;
end_offset = 0;
}
};
/* Animation data */
struct AnimationKeyFrame { struct AnimationKeyFrame {
String name; String name;
int animation_keyframe_index; int animation_keyframe_index;
@ -149,12 +73,6 @@ public:
int get_start_frame_index() const; int get_start_frame_index() const;
void set_start_frame_index(const int value); void set_start_frame_index(const int value);
void set_length(float p_length);
float get_length() const;
void set_loop(bool p_enabled);
bool has_loop() const;
//Keyframes //Keyframes
PoolVector<int> get_keyframe_indices() const; PoolVector<int> get_keyframe_indices() const;
int add_keyframe(); int add_keyframe();
@ -179,62 +97,9 @@ public:
void initialize(); void initialize();
void load_keyframe_data(int keyframe_index); void load_keyframe_data(int keyframe_index);
float track_get_key_time(int p_track, int p_key_idx) const;
float track_get_key_transition(int p_track, int p_key_idx) const;
Variant track_get_key_value(int p_track, int p_key_idx) const;
void get_key_indices(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
void track_get_key_indices_in_range(int p_track, float p_time, float p_delta, List<int> *p_indices) const;
int track_find_key(int p_track, float p_time, bool p_exact = false) const;
Vector<Variant> method_track_get_params(int p_track, int p_key_idx) const;
StringName method_track_get_name(int p_track, int p_key_idx) const;
RES audio_track_get_key_stream(int p_track, int p_key) const;
float audio_track_get_key_start_offset(int p_track, int p_key) const;
float audio_track_get_key_end_offset(int p_track, int p_key) const;
StringName animation_track_get_key_animation(int p_track, int p_key) const;
//Interpolations
Error transform_track_interpolate(int p_track, float p_time, Vector3 *r_loc, Quat *r_rot, Vector3 *r_scale) const;
Variant value_track_interpolate(int p_track, float p_time) const;
float bezier_track_interpolate(int p_track, float p_time) const;
//Animation forwards
int get_track_count() const;
Animation::TrackType track_get_type(int p_track) const;
NodePath track_get_path(int p_track) const;
int find_track(const NodePath &p_path) const;
bool track_is_enabled(int p_track) const;
int track_get_key_count(int p_track) const;
Animation::UpdateMode value_track_get_update_mode(int p_track) const;
ProceduralAnimation(); ProceduralAnimation();
~ProceduralAnimation(); ~ProceduralAnimation();
protected:
_FORCE_INLINE_ TransformAnimationKey _interpolate(const TransformAnimationKey &p_a, const TransformAnimationKey &p_b, float p_c) const;
_FORCE_INLINE_ Vector3 _interpolate(const Vector3 &p_a, const Vector3 &p_b, float p_c) const;
_FORCE_INLINE_ Quat _interpolate(const Quat &p_a, const Quat &p_b, float p_c) const;
_FORCE_INLINE_ Variant _interpolate(const Variant &p_a, const Variant &p_b, float p_c) const;
_FORCE_INLINE_ float _interpolate(const float &p_a, const float &p_b, float p_c) const;
_FORCE_INLINE_ TransformAnimationKey _cubic_interpolate(const TransformAnimationKey &p_pre_a, const TransformAnimationKey &p_a, const TransformAnimationKey &p_b, const TransformAnimationKey &p_post_b, float p_c) const;
_FORCE_INLINE_ Vector3 _cubic_interpolate(const Vector3 &p_pre_a, const Vector3 &p_a, const Vector3 &p_b, const Vector3 &p_post_b, float p_c) const;
_FORCE_INLINE_ Quat _cubic_interpolate(const Quat &p_pre_a, const Quat &p_a, const Quat &p_b, const Quat &p_post_b, float p_c) const;
_FORCE_INLINE_ Variant _cubic_interpolate(const Variant &p_pre_a, const Variant &p_a, const Variant &p_b, const Variant &p_post_b, float p_c) const;
_FORCE_INLINE_ float _cubic_interpolate(const float &p_pre_a, const float &p_a, const float &p_b, const float &p_post_b, float p_c) const;
template <class T>
_FORCE_INLINE_ T _interpolate(const Vector<AnimationKey> &p_keys, float p_time, KeyInterpolationType p_interp, bool p_loop_wrap, bool *p_ok) const;
protected: protected:
bool _set(const StringName &p_name, const Variant &p_value); bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const; bool _get(const StringName &p_name, Variant &r_ret) const;
@ -244,8 +109,6 @@ protected:
private: private:
bool _initialized; bool _initialized;
int _animation_fps; int _animation_fps;
float _length;
bool _loop;
String _editor_add_category_name; String _editor_add_category_name;
String _add_editor_category_animation_name; String _add_editor_category_animation_name;
@ -254,8 +117,6 @@ private:
int _start_frame_index; int _start_frame_index;
Map<int, AnimationKeyFrame *> _keyframes; Map<int, AnimationKeyFrame *> _keyframes;
Map<int, Vector<AnimationKey> *> _animation_data;
Ref<Animation> _animation; Ref<Animation> _animation;
Map<int, String> _keyframe_names; Map<int, String> _keyframe_names;
}; };

View File

@ -36,20 +36,6 @@ void ProceduralAnimationEditor::edit(const Ref<ProceduralAnimation> &animation)
clear_keyframe_nodes(); clear_keyframe_nodes();
} }
void ProceduralAnimationEditor::edit(ProceduralAnimationPlayer *player) {
_animation_player = player;
_stop->show();
_play->show();
_play_from->show();
_play_bw->show();
_play_bw_from->show();
//_animation = player->get_animation();
clear_keyframe_nodes();
}
void ProceduralAnimationEditor::load_selected_animation() { void ProceduralAnimationEditor::load_selected_animation() {
clear_keyframe_nodes(); clear_keyframe_nodes();
@ -216,12 +202,9 @@ void ProceduralAnimationEditor::_bind_methods() {
} }
ProceduralAnimationEditor::ProceduralAnimationEditor() { ProceduralAnimationEditor::ProceduralAnimationEditor() {
_animation_player = NULL;
} }
ProceduralAnimationEditor::ProceduralAnimationEditor(EditorNode *p_editor) { ProceduralAnimationEditor::ProceduralAnimationEditor(EditorNode *p_editor) {
_animation_player = NULL;
set_h_size_flags(SIZE_EXPAND_FILL); set_h_size_flags(SIZE_EXPAND_FILL);
//top bar //top bar
@ -466,9 +449,6 @@ void ProceduralAnimationEditorPlugin::edit(Object *p_object) {
if (Object::cast_to<ProceduralAnimation>(p_object)) { if (Object::cast_to<ProceduralAnimation>(p_object)) {
animation_editor->edit(Object::cast_to<ProceduralAnimation>(p_object)); animation_editor->edit(Object::cast_to<ProceduralAnimation>(p_object));
animation_editor_button->show(); animation_editor_button->show();
} else if (Object::cast_to<ProceduralAnimationPlayer>(p_object)) {
animation_editor->edit(Object::cast_to<ProceduralAnimationPlayer>(p_object));
animation_editor_button->show();
} else { } else {
animation_editor_button->set_pressed(false); animation_editor_button->set_pressed(false);
animation_editor_button->hide(); animation_editor_button->hide();

View File

@ -31,7 +31,6 @@ SOFTWARE.
#include "core/core_string_names.h" #include "core/core_string_names.h"
#include "procedural_animation.h" #include "procedural_animation.h"
#include "procedural_animation_player.h"
#include "editor/plugins/curve_editor_plugin.h" #include "editor/plugins/curve_editor_plugin.h"
@ -45,7 +44,6 @@ public:
public: public:
void edit(const Ref<ProceduralAnimation> &animation); void edit(const Ref<ProceduralAnimation> &animation);
void edit(ProceduralAnimationPlayer *player);
void add_frame_button_pressed(); void add_frame_button_pressed();
@ -87,8 +85,6 @@ private:
Button *_play_bw; Button *_play_bw;
Button *_play_bw_from; Button *_play_bw_from;
ToolButton *_pin; ToolButton *_pin;
ProceduralAnimationPlayer *_animation_player;
}; };
class ProceduralAnimationEditorGraphNode : public GraphNode { class ProceduralAnimationEditorGraphNode : public GraphNode {

File diff suppressed because it is too large Load Diff

View File

@ -1,345 +0,0 @@
/*
Copyright (c) 2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef PROCEDURAL_ANIMATION_PLAYER_H
#define PROCEDURAL_ANIMATION_PLAYER_H
#include "scene/main/node.h"
#include "procedural_animation.h"
#include "scene/2d/node_2d.h"
#include "scene/3d/skeleton.h"
#include "scene/3d/spatial.h"
class ProceduralAnimationPlayer : public Node {
GDCLASS(ProceduralAnimationPlayer, Node);
OBJ_CATEGORY("Animation Nodes");
public:
enum AnimationProcessMode {
ANIMATION_PROCESS_PHYSICS,
ANIMATION_PROCESS_IDLE,
ANIMATION_PROCESS_MANUAL,
};
enum AnimationMethodCallMode {
ANIMATION_METHOD_CALL_DEFERRED,
ANIMATION_METHOD_CALL_IMMEDIATE,
};
private:
enum {
NODE_CACHE_UPDATE_MAX = 1024,
BLEND_FROM_MAX = 3
};
enum SpecialProperty {
SP_NONE,
SP_NODE2D_POS,
SP_NODE2D_ROT,
SP_NODE2D_SCALE,
};
struct TrackNodeCache {
NodePath path;
uint32_t id;
RES resource;
Node *node;
Spatial *spatial;
Node2D *node_2d;
Skeleton *skeleton;
int bone_idx;
// accumulated transforms
Vector3 loc_accum;
Quat rot_accum;
Vector3 scale_accum;
uint64_t accum_pass;
bool audio_playing;
float audio_start;
float audio_len;
bool animation_playing;
struct PropertyAnim {
TrackNodeCache *owner;
SpecialProperty special; //small optimization
Vector<StringName> subpath;
Object *object;
Variant value_accum;
uint64_t accum_pass;
Variant capture;
PropertyAnim() :
owner(NULL),
special(SP_NONE),
object(NULL),
accum_pass(0) {}
};
Map<StringName, PropertyAnim> property_anim;
struct BezierAnim {
Vector<StringName> bezier_property;
TrackNodeCache *owner;
float bezier_accum;
Object *object;
uint64_t accum_pass;
BezierAnim() :
owner(NULL),
bezier_accum(0.0),
object(NULL),
accum_pass(0) {}
};
Map<StringName, BezierAnim> bezier_anim;
TrackNodeCache() :
id(0),
node(NULL),
spatial(NULL),
node_2d(NULL),
skeleton(NULL),
bone_idx(-1),
accum_pass(0),
audio_playing(false),
audio_start(0.0),
audio_len(0.0),
animation_playing(false) {}
};
struct TrackNodeCacheKey {
uint32_t id;
int bone_idx;
inline bool operator<(const TrackNodeCacheKey &p_right) const {
if (id < p_right.id)
return true;
else if (id > p_right.id)
return false;
else
return bone_idx < p_right.bone_idx;
}
};
Map<TrackNodeCacheKey, TrackNodeCache> node_cache_map;
TrackNodeCache *cache_update[NODE_CACHE_UPDATE_MAX];
int cache_update_size;
TrackNodeCache::PropertyAnim *cache_update_prop[NODE_CACHE_UPDATE_MAX];
int cache_update_prop_size;
TrackNodeCache::BezierAnim *cache_update_bezier[NODE_CACHE_UPDATE_MAX];
int cache_update_bezier_size;
Set<TrackNodeCache *> playing_caches;
uint64_t accum_pass;
float speed_scale;
float default_blend_time;
struct AnimationData {
String name;
StringName next;
Vector<TrackNodeCache *> node_cache;
Ref<ProceduralAnimation> animation;
};
Map<StringName, AnimationData> animation_set;
struct BlendKey {
StringName from;
StringName to;
bool operator<(const BlendKey &bk) const { return from == bk.from ? String(to) < String(bk.to) : String(from) < String(bk.from); }
};
Map<BlendKey, float> blend_times;
struct PlaybackData {
AnimationData *from;
float pos;
float speed_scale;
PlaybackData() {
pos = 0;
speed_scale = 1.0;
from = NULL;
}
};
struct Blend {
PlaybackData data;
float blend_time;
float blend_left;
Blend() {
blend_left = 0;
blend_time = 0;
}
};
struct Playback {
List<Blend> blend;
PlaybackData current;
StringName assigned;
bool seeked;
bool started;
} playback;
List<StringName> queued;
bool end_reached;
bool end_notify;
String autoplay;
AnimationProcessMode animation_process_mode;
AnimationMethodCallMode method_call_mode;
bool processing;
bool active;
NodePath root;
void _animation_process_animation(AnimationData *p_anim, float p_time, float p_delta, float p_interp, bool p_is_current = true, bool p_seeked = false, bool p_started = false);
void _ensure_node_caches(AnimationData *p_anim);
void _animation_process_data(PlaybackData &cd, float p_delta, float p_blend, bool p_seeked, bool p_started);
void _animation_process2(float p_delta, bool p_started);
void _animation_update_transforms();
void _animation_process(float p_delta);
void _node_removed(Node *p_node);
void _stop_playing_caches();
// bind helpers
PoolVector<String> _get_animation_list() const {
List<StringName> animations;
get_animation_list(&animations);
PoolVector<String> ret;
while (animations.size()) {
ret.push_back(animations.front()->get());
animations.pop_front();
}
return ret;
}
void _animation_changed();
void _ref_anim(const Ref<ProceduralAnimation> &p_anim);
void _unref_anim(const Ref<ProceduralAnimation> &p_anim);
void _set_process(bool p_process, bool p_force = false);
bool playing;
protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
virtual void _validate_property(PropertyInfo &property) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _notification(int p_what);
static void _bind_methods();
public:
StringName find_animation(const Ref<ProceduralAnimation> &p_animation) const;
Error add_animation(const StringName &p_name, const Ref<ProceduralAnimation> &p_animation);
void remove_animation(const StringName &p_name);
void rename_animation(const StringName &p_name, const StringName &p_new_name);
bool has_animation(const StringName &p_name) const;
Ref<ProceduralAnimation> get_animation(const StringName &p_name) const;
void get_animation_list(List<StringName> *p_animations) const;
void set_blend_time(const StringName &p_animation1, const StringName &p_animation2, float p_time);
float get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const;
void animation_set_next(const StringName &p_animation, const StringName &p_next);
StringName animation_get_next(const StringName &p_animation) const;
void set_default_blend_time(float p_default);
float get_default_blend_time() const;
void play(const StringName &p_name = StringName(), float p_custom_blend = -1, float p_custom_scale = 1.0, bool p_from_end = false);
void play_backwards(const StringName &p_name = StringName(), float p_custom_blend = -1);
void queue(const StringName &p_name);
PoolVector<String> get_queue();
void clear_queue();
void stop(bool p_reset = true);
bool is_playing() const;
String get_current_animation() const;
void set_current_animation(const String &p_anim);
String get_assigned_animation() const;
void set_assigned_animation(const String &p_anim);
void stop_all();
void set_active(bool p_active);
bool is_active() const;
bool is_valid() const;
void set_speed_scale(float p_speed);
float get_speed_scale() const;
float get_playing_speed() const;
void set_autoplay(const String &p_name);
String get_autoplay() const;
void set_animation_process_mode(AnimationProcessMode p_mode);
AnimationProcessMode get_animation_process_mode() const;
void set_method_call_mode(AnimationMethodCallMode p_mode);
AnimationMethodCallMode get_method_call_mode() const;
void seek(float p_time, bool p_update = false);
void seek_delta(float p_time, float p_delta);
float get_current_animation_position() const;
float get_current_animation_length() const;
void advance(float p_time);
void set_root(const NodePath &p_root);
NodePath get_root() const;
void clear_caches(); ///< must be called by hand if an animation was modified after added
void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
ProceduralAnimationPlayer();
~ProceduralAnimationPlayer();
};
VARIANT_ENUM_CAST(ProceduralAnimationPlayer::AnimationProcessMode);
VARIANT_ENUM_CAST(ProceduralAnimationPlayer::AnimationMethodCallMode);
#endif

View File

@ -23,13 +23,11 @@ SOFTWARE.
#include "register_types.h" #include "register_types.h"
#include "procedural_animation.h" #include "procedural_animation.h"
#include "procedural_animation_player.h"
#include "procedural_animation_editor_plugin.h" #include "procedural_animation_editor_plugin.h"
void register_procedural_animations_types() { void register_procedural_animations_types() {
ClassDB::register_class<ProceduralAnimation>(); ClassDB::register_class<ProceduralAnimation>();
ClassDB::register_class<ProceduralAnimationPlayer>();
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<ProceduralAnimationEditorPlugin>(); EditorPlugins::add_by_type<ProceduralAnimationEditorPlugin>();