diff options
Diffstat (limited to 'scene/animation')
-rw-r--r-- | scene/animation/SCsub | 2 | ||||
-rw-r--r-- | scene/animation/animation_blend_tree.cpp | 2 | ||||
-rw-r--r-- | scene/animation/animation_cache.cpp | 10 | ||||
-rw-r--r-- | scene/animation/animation_cache.h | 14 | ||||
-rw-r--r-- | scene/animation/animation_node_state_machine.cpp | 31 | ||||
-rw-r--r-- | scene/animation/animation_node_state_machine.h | 1 | ||||
-rw-r--r-- | scene/animation/animation_player.cpp | 37 | ||||
-rw-r--r-- | scene/animation/animation_player.h | 26 | ||||
-rw-r--r-- | scene/animation/animation_tree.cpp | 42 | ||||
-rw-r--r-- | scene/animation/animation_tree.h | 20 | ||||
-rw-r--r-- | scene/animation/root_motion_view.cpp | 28 | ||||
-rw-r--r-- | scene/animation/root_motion_view.h | 6 | ||||
-rw-r--r-- | scene/animation/skeleton_ik.cpp | 575 | ||||
-rw-r--r-- | scene/animation/skeleton_ik.h | 220 | ||||
-rw-r--r-- | scene/animation/tween.cpp | 56 |
15 files changed, 145 insertions, 925 deletions
diff --git a/scene/animation/SCsub b/scene/animation/SCsub index b01e2fd54d..fc61250247 100644 --- a/scene/animation/SCsub +++ b/scene/animation/SCsub @@ -1,5 +1,5 @@ #!/usr/bin/env python -Import('env') +Import("env") env.add_source_files(env.scene_sources, "*.cpp") diff --git a/scene/animation/animation_blend_tree.cpp b/scene/animation/animation_blend_tree.cpp index 8ba7a38628..570735ad87 100644 --- a/scene/animation/animation_blend_tree.cpp +++ b/scene/animation/animation_blend_tree.cpp @@ -41,7 +41,7 @@ StringName AnimationNodeAnimation::get_animation() const { return animation; } -Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = NULL; +Vector<String> (*AnimationNodeAnimation::get_editable_animation_list)() = nullptr; void AnimationNodeAnimation::get_parameter_list(List<PropertyInfo> *r_list) const { r_list->push_back(PropertyInfo(Variant::FLOAT, time, PROPERTY_HINT_NONE, "", 0)); diff --git a/scene/animation/animation_cache.cpp b/scene/animation/animation_cache.cpp index 9ed8155bdc..ab8be47b4d 100644 --- a/scene/animation/animation_cache.cpp +++ b/scene/animation/animation_cache.cpp @@ -94,19 +94,19 @@ void AnimationCache::_update_cache() { ERR_CONTINUE_MSG(animation->track_get_type(i) == Animation::TYPE_TRANSFORM, "Transform tracks can't have a subpath '" + np + "'."); } - Spatial *sp = Object::cast_to<Spatial>(node); + Node3D *sp = Object::cast_to<Node3D>(node); if (!sp) { path_cache.push_back(Path()); - ERR_CONTINUE_MSG(!sp, "Transform track not of type Spatial '" + np + "'."); + ERR_CONTINUE_MSG(!sp, "Transform track not of type Node3D '" + np + "'."); } if (np.get_subname_count() == 1) { StringName property = np.get_subname(0); String ps = property; - Skeleton *sk = Object::cast_to<Skeleton>(node); + Skeleton3D *sk = Object::cast_to<Skeleton3D>(node); if (!sk) { path_cache.push_back(Path()); @@ -287,7 +287,7 @@ void AnimationCache::set_all(float p_time, float p_delta) { if (!args.size()) { - call_track(i, name, NULL, 0, err); + call_track(i, name, nullptr, 0, err); } else { Vector<const Variant *> argptrs; @@ -332,7 +332,7 @@ void AnimationCache::set_root(Node *p_root) { AnimationCache::AnimationCache() { - root = NULL; + root = nullptr; cache_dirty = true; cache_valid = false; } diff --git a/scene/animation/animation_cache.h b/scene/animation/animation_cache.h index e73b9e2498..23312ca7ec 100644 --- a/scene/animation/animation_cache.h +++ b/scene/animation/animation_cache.h @@ -31,7 +31,7 @@ #ifndef ANIMATION_CACHE_H #define ANIMATION_CACHE_H -#include "scene/3d/skeleton.h" +#include "scene/3d/skeleton_3d.h" #include "scene/resources/animation.h" class AnimationCache : public Object { @@ -42,20 +42,20 @@ class AnimationCache : public Object { RES resource; Object *object; - Skeleton *skeleton; // haxor + Skeleton3D *skeleton; // haxor Node *node; - Spatial *spatial; + Node3D *spatial; int bone_idx; Vector<StringName> subpath; bool valid; Path() { - object = NULL; - skeleton = NULL; - node = NULL; + object = nullptr; + skeleton = nullptr; + node = nullptr; bone_idx = -1; valid = false; - spatial = NULL; + spatial = nullptr; } }; diff --git a/scene/animation/animation_node_state_machine.cpp b/scene/animation/animation_node_state_machine.cpp index fbd9a2aa7d..9f5e06c43d 100644 --- a/scene/animation/animation_node_state_machine.cpp +++ b/scene/animation/animation_node_state_machine.cpp @@ -225,7 +225,7 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_sta } //find the last cost transition - List<int>::Element *least_cost_transition = NULL; + List<int>::Element *least_cost_transition = nullptr; float least_cost = 1e20; for (List<int>::Element *E = open_list.front(); E; E = E->next()) { @@ -516,6 +516,11 @@ AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() { len_current = 0; fading_time = 0; stop_request = false; + len_total = 0.0; + pos_current = 0.0; + loops_current = 0; + fading_pos = 0.0; + start_request_travel = false; } /////////////////////////////////////////////////////// @@ -525,7 +530,7 @@ void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) c List<StringName> advance_conditions; for (int i = 0; i < transitions.size(); i++) { StringName ac = transitions[i].transition->get_advance_condition_name(); - if (ac != StringName() && advance_conditions.find(ac) == NULL) { + if (ac != StringName() && advance_conditions.find(ac) == nullptr) { advance_conditions.push_back(ac); } } @@ -565,6 +570,27 @@ void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<Animation p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), varray(), CONNECT_REFERENCE_COUNTED); } +void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) { + + ERR_FAIL_COND(states.has(p_name) == false); + ERR_FAIL_COND(p_node.is_null()); + ERR_FAIL_COND(String(p_name).find("/") != -1); + + { + Ref<AnimationNode> node = states[p_name].node; + if (node.is_valid()) { + node->disconnect_compat("tree_changed", this, "_tree_changed"); + } + } + + states[p_name].node = p_node; + + emit_changed(); + emit_signal("tree_changed"); + + p_node->connect_compat("tree_changed", this, "_tree_changed", varray(), CONNECT_REFERENCE_COUNTED); +} + Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const { ERR_FAIL_COND_V(!states.has(p_name), Ref<AnimationNode>()); @@ -949,6 +975,7 @@ void AnimationNodeStateMachine::_tree_changed() { void AnimationNodeStateMachine::_bind_methods() { ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2())); + ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node); ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node); ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node); ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node); diff --git a/scene/animation/animation_node_state_machine.h b/scene/animation/animation_node_state_machine.h index 55c9c3f00e..27a4451f08 100644 --- a/scene/animation/animation_node_state_machine.h +++ b/scene/animation/animation_node_state_machine.h @@ -178,6 +178,7 @@ public: virtual Variant get_parameter_default_value(const StringName &p_parameter) const; void add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position = Vector2()); + void replace_node(const StringName &p_name, Ref<AnimationNode> p_node); Ref<AnimationNode> get_node(const StringName &p_name) const; void remove_node(const StringName &p_name); void rename_node(const StringName &p_name, const StringName &p_new_name); diff --git a/scene/animation/animation_player.cpp b/scene/animation/animation_player.cpp index 587485669e..b657833a3b 100644 --- a/scene/animation/animation_player.cpp +++ b/scene/animation/animation_player.cpp @@ -44,7 +44,7 @@ void AnimatedValuesBackup::update_skeletons() { for (int i = 0; i < entries.size(); i++) { if (entries[i].bone_idx != -1) { // 3D bone - Object::cast_to<Skeleton>(entries[i].object)->notification(Skeleton::NOTIFICATION_UPDATE_SKELETON); + Object::cast_to<Skeleton3D>(entries[i].object)->notification(Skeleton3D::NOTIFICATION_UPDATE_SKELETON); } else { Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object); if (bone && bone->skeleton) { @@ -244,7 +244,7 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { for (int i = 0; i < a->get_track_count(); i++) { - p_anim->node_cache.write[i] = NULL; + p_anim->node_cache.write[i] = nullptr; RES resource; Vector<StringName> leftover_path; Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path); @@ -252,9 +252,9 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { ObjectID id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id(); int bone_idx = -1; - if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton>(child)) { + if (a->track_get_path(i).get_subname_count() == 1 && Object::cast_to<Skeleton3D>(child)) { - Skeleton *sk = Object::cast_to<Skeleton>(child); + Skeleton3D *sk = Object::cast_to<Skeleton3D>(child); bone_idx = sk->find_bone(a->track_get_path(i).get_subname(0)); if (bone_idx == -1) { @@ -283,9 +283,9 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { // special cases and caches for transform tracks // cache spatial - p_anim->node_cache[i]->spatial = Object::cast_to<Spatial>(child); + p_anim->node_cache[i]->spatial = Object::cast_to<Node3D>(child); // cache skeleton - p_anim->node_cache[i]->skeleton = Object::cast_to<Skeleton>(child); + p_anim->node_cache[i]->skeleton = Object::cast_to<Skeleton3D>(child); if (p_anim->node_cache[i]->skeleton) { if (a->track_get_path(i).get_subname_count() == 1) { StringName bone_name = a->track_get_path(i).get_subname(0); @@ -293,13 +293,13 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { p_anim->node_cache[i]->bone_idx = p_anim->node_cache[i]->skeleton->find_bone(bone_name); if (p_anim->node_cache[i]->bone_idx < 0) { // broken track (nonexistent bone) - p_anim->node_cache[i]->skeleton = NULL; - p_anim->node_cache[i]->spatial = NULL; + p_anim->node_cache[i]->skeleton = nullptr; + p_anim->node_cache[i]->spatial = nullptr; ERR_CONTINUE(p_anim->node_cache[i]->bone_idx < 0); } } else { // no property, just use spatialnode - p_anim->node_cache[i]->skeleton = NULL; + p_anim->node_cache[i]->skeleton = nullptr; } } } @@ -830,7 +830,7 @@ void AnimationPlayer::_animation_process2(float p_delta, bool p_started) { c.seeked = false; } - List<Blend>::Element *prev = NULL; + List<Blend>::Element *prev = nullptr; for (List<Blend>::Element *E = c.blend.back(); E; E = prev) { Blend &b = E->get(); @@ -1242,19 +1242,6 @@ void AnimationPlayer::play(const StringName &p_name, float p_custom_blend, float bool AnimationPlayer::is_playing() const { return playing; - /* - if (playback.current.from==NULL) - return false; - - float len=playback.current.from->animation->get_length(); - float pos = playback.current.pos; - bool loop=playback.current.from->animation->has_loop(); - if (!loop && pos >= len) { - return false; - }; - - return true; - */ } void AnimationPlayer::set_current_animation(const String &p_anim) { @@ -1296,7 +1283,7 @@ void AnimationPlayer::stop(bool p_reset) { Playback &c = playback; c.blend.clear(); if (p_reset) { - c.current.from = NULL; + c.current.from = nullptr; c.current.speed_scale = 1; c.current.pos = 0; } @@ -1612,7 +1599,7 @@ void AnimationPlayer::restore_animated_values(const AnimatedValuesBackup &p_back if (entry->bone_idx == -1) { entry->object->set_indexed(entry->subpath, entry->value); } else { - Object::cast_to<Skeleton>(entry->object)->set_bone_pose(entry->bone_idx, entry->value); + Object::cast_to<Skeleton3D>(entry->object)->set_bone_pose(entry->bone_idx, entry->value); } } } diff --git a/scene/animation/animation_player.h b/scene/animation/animation_player.h index 24f60363ed..c134aff707 100644 --- a/scene/animation/animation_player.h +++ b/scene/animation/animation_player.h @@ -32,8 +32,8 @@ #define ANIMATION_PLAYER_H #include "scene/2d/node_2d.h" -#include "scene/3d/skeleton.h" -#include "scene/3d/spatial.h" +#include "scene/3d/node_3d.h" +#include "scene/3d/skeleton_3d.h" #include "scene/resources/animation.h" #ifdef TOOLS_ENABLED @@ -90,9 +90,9 @@ private: uint32_t id; RES resource; Node *node; - Spatial *spatial; + Node3D *spatial; Node2D *node_2d; - Skeleton *skeleton; + Skeleton3D *skeleton; int bone_idx; // accumulated transforms @@ -118,9 +118,9 @@ private: Variant capture; PropertyAnim() : - owner(NULL), + owner(nullptr), special(SP_NONE), - object(NULL), + object(nullptr), accum_pass(0) {} }; @@ -135,9 +135,9 @@ private: uint64_t accum_pass; BezierAnim() : - owner(NULL), + owner(nullptr), bezier_accum(0.0), - object(NULL), + object(nullptr), accum_pass(0) {} }; @@ -145,10 +145,10 @@ private: TrackNodeCache() : id(0), - node(NULL), - spatial(NULL), - node_2d(NULL), - skeleton(NULL), + node(nullptr), + spatial(nullptr), + node_2d(nullptr), + skeleton(nullptr), bone_idx(-1), accum_pass(0), audio_playing(false), @@ -212,7 +212,7 @@ private: pos = 0; speed_scale = 1.0; - from = NULL; + from = nullptr; } }; diff --git a/scene/animation/animation_tree.cpp b/scene/animation/animation_tree.cpp index 95afd74ee5..f8b3ca291b 100644 --- a/scene/animation/animation_tree.cpp +++ b/scene/animation/animation_tree.cpp @@ -128,8 +128,8 @@ float AnimationNode::_pre_process(const StringName &p_base_path, AnimationNode * float t = process(p_time, p_seek); - state = NULL; - parent = NULL; + state = nullptr; + parent = nullptr; base_path = StringName(); connections.clear(); @@ -164,7 +164,7 @@ float AnimationNode::blend_input(int p_input, float p_time, bool p_seek, float p //inputs.write[p_input].last_pass = state->last_pass; float activity = 0; - float ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), NULL, node, p_time, p_seek, p_blend, p_filter, p_optimize, &activity); + float ret = _blend_node(node_name, blend_tree->get_node_connection_array(node_name), nullptr, node, p_time, p_seek, p_blend, p_filter, p_optimize, &activity); Vector<AnimationTree::Activity> *activity_ptr = state->tree->input_activity_map.getptr(base_path); @@ -202,7 +202,7 @@ float AnimationNode::_blend_node(const StringName &p_subpath, const Vector<Strin blendw[i] = 0.0; //all to zero by default } - const NodePath *K = NULL; + const NodePath *K = nullptr; while ((K = filter.next(K))) { if (!state->track_map.has(*K)) { continue; @@ -316,7 +316,7 @@ String AnimationNode::get_caption() const { void AnimationNode::add_input(const String &p_name) { //root nodes can't add inputs - ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != NULL); + ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != nullptr); Input input; ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1); input.name = p_name; @@ -374,7 +374,7 @@ Array AnimationNode::_get_filters() const { Array paths; - const NodePath *K = NULL; + const NodePath *K = nullptr; while ((K = filter.next(K))) { paths.push_back(String(*K)); //use strings, so sorting is possible } @@ -453,8 +453,8 @@ void AnimationNode::_bind_methods() { AnimationNode::AnimationNode() { - state = NULL; - parent = NULL; + state = nullptr; + parent = nullptr; filter_enabled = false; } @@ -558,17 +558,17 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { NodePath path = anim->track_get_path(i); Animation::TrackType track_type = anim->track_get_type(i); - TrackCache *track = NULL; + TrackCache *track = nullptr; if (track_cache.has(path)) { track = track_cache.get(path); } //if not valid, delete track - if (track && (track->type != track_type || ObjectDB::get_instance(track->object_id) == NULL)) { + if (track && (track->type != track_type || ObjectDB::get_instance(track->object_id) == nullptr)) { playing_caches.erase(track); memdelete(track); track_cache.erase(path); - track = NULL; + track = nullptr; } if (!track) { @@ -605,7 +605,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { } break; case Animation::TYPE_TRANSFORM: { - Spatial *spatial = Object::cast_to<Spatial>(child); + Node3D *spatial = Object::cast_to<Node3D>(child); if (!spatial) { ERR_PRINT("AnimationTree: '" + String(E->get()) + "', transform track does not point to spatial: '" + String(path) + "'"); @@ -615,12 +615,12 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { TrackCacheTransform *track_xform = memnew(TrackCacheTransform); track_xform->spatial = spatial; - track_xform->skeleton = NULL; + track_xform->skeleton = nullptr; track_xform->bone_idx = -1; - if (path.get_subname_count() == 1 && Object::cast_to<Skeleton>(spatial)) { + if (path.get_subname_count() == 1 && Object::cast_to<Skeleton3D>(spatial)) { - Skeleton *sk = Object::cast_to<Skeleton>(spatial); + Skeleton3D *sk = Object::cast_to<Skeleton3D>(spatial); int bone_idx = sk->find_bone(path.get_subname(0)); if (bone_idx != -1) { @@ -700,7 +700,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { List<NodePath> to_delete; - const NodePath *K = NULL; + const NodePath *K = nullptr; while ((K = track_cache.next(K))) { TrackCache *tc = track_cache[*K]; if (tc->setup_pass != setup_pass) { @@ -717,7 +717,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { state.track_map.clear(); - K = NULL; + K = nullptr; int idx = 0; while ((K = track_cache.next(K))) { state.track_map[*K] = idx; @@ -733,7 +733,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { void AnimationTree::_clear_caches() { - const NodePath *K = NULL; + const NodePath *K = nullptr; while ((K = track_cache.next(K))) { memdelete(track_cache[*K]); } @@ -829,11 +829,11 @@ void AnimationTree::_process_graph(float p_delta) { if (started) { //if started, seek - root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, NULL, &state, 0, true, Vector<StringName>()); + root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, 0, true, Vector<StringName>()); started = false; } - root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, NULL, &state, p_delta, false, Vector<StringName>()); + root->_pre_process(SceneStringNames::get_singleton()->parameters_base_path, nullptr, &state, p_delta, false, Vector<StringName>()); } if (!state.valid) { @@ -1224,7 +1224,7 @@ void AnimationTree::_process_graph(float p_delta) { { // finally, set the tracks - const NodePath *K = NULL; + const NodePath *K = nullptr; while ((K = track_cache.next(K))) { TrackCache *track = track_cache[*K]; if (track->process_pass != process_pass) diff --git a/scene/animation/animation_tree.h b/scene/animation/animation_tree.h index 8769e2c61d..d558170f23 100644 --- a/scene/animation/animation_tree.h +++ b/scene/animation/animation_tree.h @@ -32,8 +32,8 @@ #define ANIMATION_GRAPH_PLAYER_H #include "animation_player.h" -#include "scene/3d/skeleton.h" -#include "scene/3d/spatial.h" +#include "scene/3d/node_3d.h" +#include "scene/3d/skeleton_3d.h" #include "scene/resources/animation.h" class AnimationNodeBlendTree; @@ -101,7 +101,7 @@ public: Array _get_filters() const; void _set_filters(const Array &p_filters); friend class AnimationNodeBlendTree; - float _blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true, float *r_max = NULL); + float _blend_node(const StringName &p_subpath, const Vector<StringName> &p_connections, AnimationNode *p_new_parent, Ref<AnimationNode> p_node, float p_time, bool p_seek, float p_blend, FilterAction p_filter = FILTER_IGNORE, bool p_optimize = true, float *r_max = nullptr); protected: void blend_animation(const StringName &p_animation, float p_time, float p_delta, bool p_seeked, float p_blend); @@ -186,14 +186,14 @@ private: root_motion = false; setup_pass = 0; process_pass = 0; - object = NULL; + object = nullptr; } virtual ~TrackCache() {} }; struct TrackCacheTransform : public TrackCache { - Spatial *spatial; - Skeleton *skeleton; + Node3D *spatial; + Skeleton3D *skeleton; int bone_idx; Vector3 loc; Quat rot; @@ -202,9 +202,9 @@ private: TrackCacheTransform() { type = Animation::TYPE_TRANSFORM; - spatial = NULL; + spatial = nullptr; bone_idx = -1; - skeleton = NULL; + skeleton = nullptr; } }; @@ -285,7 +285,7 @@ private: void _tree_changed(); void _update_properties(); List<PropertyInfo> properties; - HashMap<StringName, HashMap<StringName, StringName> > property_parent_map; + HashMap<StringName, HashMap<StringName, StringName>> property_parent_map; HashMap<StringName, Variant> property_map; struct Activity { @@ -293,7 +293,7 @@ private: float activity; }; - HashMap<StringName, Vector<Activity> > input_activity_map; + HashMap<StringName, Vector<Activity>> input_activity_map; HashMap<StringName, Vector<Activity> *> input_activity_map_get; void _update_properties_for_node(const String &p_base_path, Ref<AnimationNode> node); diff --git a/scene/animation/root_motion_view.cpp b/scene/animation/root_motion_view.cpp index ce9b8bd213..f993127b07 100644 --- a/scene/animation/root_motion_view.cpp +++ b/scene/animation/root_motion_view.cpp @@ -79,7 +79,7 @@ void RootMotionView::_notification(int p_what) { if (p_what == NOTIFICATION_ENTER_TREE) { - VS::get_singleton()->immediate_set_material(immediate, StandardMaterial3D::get_material_rid_for_2d(false, true, false, false, false)); + RS::get_singleton()->immediate_set_material(immediate, StandardMaterial3D::get_material_rid_for_2d(false, true, false, false, false)); first = true; } @@ -122,11 +122,11 @@ void RootMotionView::_notification(int p_what) { } accumulated.origin.z = Math::fposmod(accumulated.origin.z, cell_size); - VS::get_singleton()->immediate_clear(immediate); + RS::get_singleton()->immediate_clear(immediate); int cells_in_radius = int((radius / cell_size) + 1.0); - VS::get_singleton()->immediate_begin(immediate, VS::PRIMITIVE_LINES); + RS::get_singleton()->immediate_begin(immediate, RS::PRIMITIVE_LINES); for (int i = -cells_in_radius; i < cells_in_radius; i++) { for (int j = -cells_in_radius; j < cells_in_radius; j++) { @@ -142,21 +142,21 @@ void RootMotionView::_notification(int p_what) { c_i.a *= MAX(0, 1.0 - from_i.length() / radius); c_j.a *= MAX(0, 1.0 - from_j.length() / radius); - VS::get_singleton()->immediate_color(immediate, c); - VS::get_singleton()->immediate_vertex(immediate, from); + RS::get_singleton()->immediate_color(immediate, c); + RS::get_singleton()->immediate_vertex(immediate, from); - VS::get_singleton()->immediate_color(immediate, c_i); - VS::get_singleton()->immediate_vertex(immediate, from_i); + RS::get_singleton()->immediate_color(immediate, c_i); + RS::get_singleton()->immediate_vertex(immediate, from_i); - VS::get_singleton()->immediate_color(immediate, c); - VS::get_singleton()->immediate_vertex(immediate, from); + RS::get_singleton()->immediate_color(immediate, c); + RS::get_singleton()->immediate_vertex(immediate, from); - VS::get_singleton()->immediate_color(immediate, c_j); - VS::get_singleton()->immediate_vertex(immediate, from_j); + RS::get_singleton()->immediate_color(immediate, c_j); + RS::get_singleton()->immediate_vertex(immediate, from_j); } } - VS::get_singleton()->immediate_end(immediate); + RS::get_singleton()->immediate_end(immediate); } } @@ -197,12 +197,12 @@ RootMotionView::RootMotionView() { radius = 10; cell_size = 1; set_process_internal(true); - immediate = VisualServer::get_singleton()->immediate_create(); + immediate = RenderingServer::get_singleton()->immediate_create(); set_base(immediate); color = Color(0.5, 0.5, 1.0); } RootMotionView::~RootMotionView() { set_base(RID()); - VisualServer::get_singleton()->free(immediate); + RenderingServer::get_singleton()->free(immediate); } diff --git a/scene/animation/root_motion_view.h b/scene/animation/root_motion_view.h index 42950dde42..c8a755a854 100644 --- a/scene/animation/root_motion_view.h +++ b/scene/animation/root_motion_view.h @@ -31,10 +31,10 @@ #ifndef ROOT_MOTION_VIEW_H #define ROOT_MOTION_VIEW_H -#include "scene/3d/visual_instance.h" +#include "scene/3d/visual_instance_3d.h" -class RootMotionView : public VisualInstance { - GDCLASS(RootMotionView, VisualInstance); +class RootMotionView : public VisualInstance3D { + GDCLASS(RootMotionView, VisualInstance3D); public: RID immediate; diff --git a/scene/animation/skeleton_ik.cpp b/scene/animation/skeleton_ik.cpp deleted file mode 100644 index 5cdb38b5c2..0000000000 --- a/scene/animation/skeleton_ik.cpp +++ /dev/null @@ -1,575 +0,0 @@ -/*************************************************************************/ -/* skeleton_ik.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -/** - * @author AndreaCatania - */ - -#include "skeleton_ik.h" - -#ifndef _3D_DISABLED - -FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::find_child(const BoneId p_bone_id) { - for (int i = children.size() - 1; 0 <= i; --i) { - if (p_bone_id == children[i].bone) { - return &children.write[i]; - } - } - return NULL; -} - -FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::add_child(const BoneId p_bone_id) { - const int infant_child_id = children.size(); - children.resize(infant_child_id + 1); - children.write[infant_child_id].bone = p_bone_id; - children.write[infant_child_id].parent_item = this; - return &children.write[infant_child_id]; -} - -/// Build a chain that starts from the root to tip -bool FabrikInverseKinematic::build_chain(Task *p_task, bool p_force_simple_chain) { - - ERR_FAIL_COND_V(-1 == p_task->root_bone, false); - - Chain &chain(p_task->chain); - - chain.tips.resize(p_task->end_effectors.size()); - chain.chain_root.bone = p_task->root_bone; - chain.chain_root.initial_transform = p_task->skeleton->get_bone_global_pose(chain.chain_root.bone); - chain.chain_root.current_pos = chain.chain_root.initial_transform.origin; - chain.chain_root.pb = p_task->skeleton->get_physical_bone(chain.chain_root.bone); - chain.middle_chain_item = NULL; - - // Holds all IDs that are composing a single chain in reverse order - Vector<BoneId> chain_ids; - // This is used to know the chain size - int sub_chain_size; - // Resize only one time in order to fit all joints for performance reason - chain_ids.resize(p_task->skeleton->get_bone_count()); - - for (int x = p_task->end_effectors.size() - 1; 0 <= x; --x) { - - const EndEffector *ee(&p_task->end_effectors[x]); - ERR_FAIL_COND_V(p_task->root_bone >= ee->tip_bone, false); - ERR_FAIL_INDEX_V(ee->tip_bone, p_task->skeleton->get_bone_count(), false); - - sub_chain_size = 0; - // Picks all IDs that composing a single chain in reverse order (except the root) - BoneId chain_sub_tip(ee->tip_bone); - while (chain_sub_tip > p_task->root_bone) { - - chain_ids.write[sub_chain_size++] = chain_sub_tip; - chain_sub_tip = p_task->skeleton->get_bone_parent(chain_sub_tip); - } - - BoneId middle_chain_item_id = (((float)sub_chain_size) * 0.5); - - // Build chain by reading chain ids in reverse order - // For each chain item id will be created a ChainItem if doesn't exists - ChainItem *sub_chain(&chain.chain_root); - for (int i = sub_chain_size - 1; 0 <= i; --i) { - - ChainItem *child_ci(sub_chain->find_child(chain_ids[i])); - if (!child_ci) { - - child_ci = sub_chain->add_child(chain_ids[i]); - - child_ci->pb = p_task->skeleton->get_physical_bone(child_ci->bone); - - child_ci->initial_transform = p_task->skeleton->get_bone_global_pose(child_ci->bone); - child_ci->current_pos = child_ci->initial_transform.origin; - - if (child_ci->parent_item) { - child_ci->length = (child_ci->current_pos - child_ci->parent_item->current_pos).length(); - } - } - - sub_chain = child_ci; - - if (middle_chain_item_id == i) { - chain.middle_chain_item = child_ci; - } - } - - if (!middle_chain_item_id) - chain.middle_chain_item = NULL; - - // Initialize current tip - chain.tips.write[x].chain_item = sub_chain; - chain.tips.write[x].end_effector = ee; - - if (p_force_simple_chain) { - // NOTE: - // This is an "hack" that force to create only one tip per chain since the solver of multi tip (end effector) - // is not yet created. - // Remove this code when this is done - break; - } - } - return true; -} - -void FabrikInverseKinematic::update_chain(const Skeleton *p_sk, ChainItem *p_chain_item) { - - if (!p_chain_item) - return; - - p_chain_item->initial_transform = p_sk->get_bone_global_pose(p_chain_item->bone); - p_chain_item->current_pos = p_chain_item->initial_transform.origin; - - for (int i = p_chain_item->children.size() - 1; 0 <= i; --i) { - update_chain(p_sk, &p_chain_item->children.write[i]); - } -} - -void FabrikInverseKinematic::solve_simple(Task *p_task, bool p_solve_magnet) { - - real_t distance_to_goal(1e4); - real_t previous_distance_to_goal(0); - int can_solve(p_task->max_iterations); - while (distance_to_goal > p_task->min_distance && Math::abs(previous_distance_to_goal - distance_to_goal) > 0.005 && can_solve) { - previous_distance_to_goal = distance_to_goal; - --can_solve; - - solve_simple_backwards(p_task->chain, p_solve_magnet); - solve_simple_forwards(p_task->chain, p_solve_magnet); - - distance_to_goal = (p_task->chain.tips[0].chain_item->current_pos - p_task->chain.tips[0].end_effector->goal_transform.origin).length(); - } -} - -void FabrikInverseKinematic::solve_simple_backwards(Chain &r_chain, bool p_solve_magnet) { - - if (p_solve_magnet && !r_chain.middle_chain_item) { - return; - } - - Vector3 goal; - ChainItem *sub_chain_tip; - if (p_solve_magnet) { - goal = r_chain.magnet_position; - sub_chain_tip = r_chain.middle_chain_item; - } else { - goal = r_chain.tips[0].end_effector->goal_transform.origin; - sub_chain_tip = r_chain.tips[0].chain_item; - } - - while (sub_chain_tip) { - sub_chain_tip->current_pos = goal; - - if (sub_chain_tip->parent_item) { - // Not yet in the chain root - // So calculate next goal location - - const Vector3 look_parent((sub_chain_tip->parent_item->current_pos - sub_chain_tip->current_pos).normalized()); - goal = sub_chain_tip->current_pos + (look_parent * sub_chain_tip->length); - - // [TODO] Constraints goes here - } - - sub_chain_tip = sub_chain_tip->parent_item; - } -} - -void FabrikInverseKinematic::solve_simple_forwards(Chain &r_chain, bool p_solve_magnet) { - - if (p_solve_magnet && !r_chain.middle_chain_item) { - return; - } - - ChainItem *sub_chain_root(&r_chain.chain_root); - Vector3 origin(r_chain.chain_root.initial_transform.origin); - - while (sub_chain_root) { // Reach the tip - sub_chain_root->current_pos = origin; - - if (!sub_chain_root->children.empty()) { - - ChainItem &child(sub_chain_root->children.write[0]); - - // Is not tip - // So calculate next origin location - - // Look child - sub_chain_root->current_ori = (child.current_pos - sub_chain_root->current_pos).normalized(); - origin = sub_chain_root->current_pos + (sub_chain_root->current_ori * child.length); - - // [TODO] Constraints goes here - - if (p_solve_magnet && sub_chain_root == r_chain.middle_chain_item) { - // In case of magnet solving this is the tip - sub_chain_root = NULL; - } else { - sub_chain_root = &child; - } - } else { - - // Is tip - sub_chain_root = NULL; - } - } -} - -FabrikInverseKinematic::Task *FabrikInverseKinematic::create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform) { - - FabrikInverseKinematic::EndEffector ee; - ee.tip_bone = tip_bone; - - Task *task(memnew(Task)); - task->skeleton = p_sk; - task->root_bone = root_bone; - task->end_effectors.push_back(ee); - task->goal_global_transform = goal_transform; - - if (!build_chain(task)) { - free_task(task); - return NULL; - } - - return task; -} - -void FabrikInverseKinematic::free_task(Task *p_task) { - if (p_task) - memdelete(p_task); -} - -void FabrikInverseKinematic::set_goal(Task *p_task, const Transform &p_goal) { - p_task->goal_global_transform = p_goal; -} - -void FabrikInverseKinematic::make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta) { - - if (blending_delta >= 0.99f) { - // Update the end_effector (local transform) without blending - p_task->end_effectors.write[0].goal_transform = p_inverse_transf * p_task->goal_global_transform; - } else { - - // End effector in local transform - const Transform end_effector_pose(p_task->skeleton->get_bone_global_pose(p_task->end_effectors.write[0].tip_bone)); - - // Update the end_effector (local transform) by blending with current pose - p_task->end_effectors.write[0].goal_transform = end_effector_pose.interpolate_with(p_inverse_transf * p_task->goal_global_transform, blending_delta); - } -} - -void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position) { - - if (blending_delta <= 0.01f) { - return; // Skip solving - } - - make_goal(p_task, p_task->skeleton->get_global_transform().affine_inverse().scaled(p_task->skeleton->get_global_transform().get_basis().get_scale()), blending_delta); - - update_chain(p_task->skeleton, &p_task->chain.chain_root); - - if (p_use_magnet && p_task->chain.middle_chain_item) { - p_task->chain.magnet_position = p_task->chain.middle_chain_item->initial_transform.origin.linear_interpolate(p_magnet_position, blending_delta); - solve_simple(p_task, true); - } - solve_simple(p_task, false); - - // Assign new bone position. - ChainItem *ci(&p_task->chain.chain_root); - while (ci) { - Transform new_bone_pose(ci->initial_transform); - new_bone_pose.origin = ci->current_pos; - - if (!ci->children.empty()) { - - /// Rotate basis - const Vector3 initial_ori((ci->children[0].initial_transform.origin - ci->initial_transform.origin).normalized()); - const Vector3 rot_axis(initial_ori.cross(ci->current_ori).normalized()); - - if (rot_axis[0] != 0 && rot_axis[1] != 0 && rot_axis[2] != 0) { - const real_t rot_angle(Math::acos(CLAMP(initial_ori.dot(ci->current_ori), -1, 1))); - new_bone_pose.basis.rotate(rot_axis, rot_angle); - } - } else { - // Set target orientation to tip - if (override_tip_basis) - new_bone_pose.basis = p_task->chain.tips[0].end_effector->goal_transform.basis; - else - new_bone_pose.basis = new_bone_pose.basis * p_task->chain.tips[0].end_effector->goal_transform.basis; - } - - p_task->skeleton->set_bone_global_pose_override(ci->bone, new_bone_pose, 1.0, true); - - if (!ci->children.empty()) - ci = &ci->children.write[0]; - else - ci = NULL; - } -} - -void SkeletonIK::_validate_property(PropertyInfo &property) const { - - if (property.name == "root_bone" || property.name == "tip_bone") { - - if (skeleton) { - - String names("--,"); - for (int i = 0; i < skeleton->get_bone_count(); i++) { - if (i > 0) - names += ","; - names += skeleton->get_bone_name(i); - } - - property.hint = PROPERTY_HINT_ENUM; - property.hint_string = names; - } else { - - property.hint = PROPERTY_HINT_NONE; - property.hint_string = ""; - } - } -} - -void SkeletonIK::_bind_methods() { - - ClassDB::bind_method(D_METHOD("set_root_bone", "root_bone"), &SkeletonIK::set_root_bone); - ClassDB::bind_method(D_METHOD("get_root_bone"), &SkeletonIK::get_root_bone); - - ClassDB::bind_method(D_METHOD("set_tip_bone", "tip_bone"), &SkeletonIK::set_tip_bone); - ClassDB::bind_method(D_METHOD("get_tip_bone"), &SkeletonIK::get_tip_bone); - - ClassDB::bind_method(D_METHOD("set_interpolation", "interpolation"), &SkeletonIK::set_interpolation); - ClassDB::bind_method(D_METHOD("get_interpolation"), &SkeletonIK::get_interpolation); - - ClassDB::bind_method(D_METHOD("set_target_transform", "target"), &SkeletonIK::set_target_transform); - ClassDB::bind_method(D_METHOD("get_target_transform"), &SkeletonIK::get_target_transform); - - ClassDB::bind_method(D_METHOD("set_target_node", "node"), &SkeletonIK::set_target_node); - ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonIK::get_target_node); - - ClassDB::bind_method(D_METHOD("set_override_tip_basis", "override"), &SkeletonIK::set_override_tip_basis); - ClassDB::bind_method(D_METHOD("is_override_tip_basis"), &SkeletonIK::is_override_tip_basis); - - ClassDB::bind_method(D_METHOD("set_use_magnet", "use"), &SkeletonIK::set_use_magnet); - ClassDB::bind_method(D_METHOD("is_using_magnet"), &SkeletonIK::is_using_magnet); - - ClassDB::bind_method(D_METHOD("set_magnet_position", "local_position"), &SkeletonIK::set_magnet_position); - ClassDB::bind_method(D_METHOD("get_magnet_position"), &SkeletonIK::get_magnet_position); - - ClassDB::bind_method(D_METHOD("get_parent_skeleton"), &SkeletonIK::get_parent_skeleton); - ClassDB::bind_method(D_METHOD("is_running"), &SkeletonIK::is_running); - - ClassDB::bind_method(D_METHOD("set_min_distance", "min_distance"), &SkeletonIK::set_min_distance); - ClassDB::bind_method(D_METHOD("get_min_distance"), &SkeletonIK::get_min_distance); - - ClassDB::bind_method(D_METHOD("set_max_iterations", "iterations"), &SkeletonIK::set_max_iterations); - ClassDB::bind_method(D_METHOD("get_max_iterations"), &SkeletonIK::get_max_iterations); - - ClassDB::bind_method(D_METHOD("start", "one_time"), &SkeletonIK::start, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("stop"), &SkeletonIK::stop); - - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "root_bone"), "set_root_bone", "get_root_bone"); - ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "tip_bone"), "set_tip_bone", "get_tip_bone"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interpolation", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_interpolation", "get_interpolation"); - ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "target"), "set_target_transform", "get_target_transform"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_tip_basis"), "set_override_tip_basis", "is_override_tip_basis"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_magnet"), "set_use_magnet", "is_using_magnet"); - ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "magnet"), "set_magnet_position", "get_magnet_position"); - ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node"), "set_target_node", "get_target_node"); - ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_distance"), "set_min_distance", "get_min_distance"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "max_iterations"), "set_max_iterations", "get_max_iterations"); -} - -void SkeletonIK::_notification(int p_what) { - switch (p_what) { - case NOTIFICATION_ENTER_TREE: { - skeleton = Object::cast_to<Skeleton>(get_parent()); - set_process_priority(1); - reload_chain(); - } break; - case NOTIFICATION_INTERNAL_PROCESS: { - - if (target_node_override) - reload_goal(); - - _solve_chain(); - - } break; - case NOTIFICATION_EXIT_TREE: { - reload_chain(); - } break; - } -} - -SkeletonIK::SkeletonIK() : - interpolation(1), - override_tip_basis(true), - use_magnet(false), - min_distance(0.01), - max_iterations(10), - skeleton(NULL), - target_node_override(NULL), - task(NULL) { -} - -SkeletonIK::~SkeletonIK() { - FabrikInverseKinematic::free_task(task); - task = NULL; -} - -void SkeletonIK::set_root_bone(const StringName &p_root_bone) { - root_bone = p_root_bone; - reload_chain(); -} - -StringName SkeletonIK::get_root_bone() const { - return root_bone; -} - -void SkeletonIK::set_tip_bone(const StringName &p_tip_bone) { - tip_bone = p_tip_bone; - reload_chain(); -} - -StringName SkeletonIK::get_tip_bone() const { - return tip_bone; -} - -void SkeletonIK::set_interpolation(real_t p_interpolation) { - interpolation = p_interpolation; -} - -real_t SkeletonIK::get_interpolation() const { - return interpolation; -} - -void SkeletonIK::set_target_transform(const Transform &p_target) { - target = p_target; - reload_goal(); -} - -const Transform &SkeletonIK::get_target_transform() const { - return target; -} - -void SkeletonIK::set_target_node(const NodePath &p_node) { - target_node_path_override = p_node; - target_node_override = NULL; - reload_goal(); -} - -NodePath SkeletonIK::get_target_node() { - return target_node_path_override; -} - -void SkeletonIK::set_override_tip_basis(bool p_override) { - override_tip_basis = p_override; -} - -bool SkeletonIK::is_override_tip_basis() const { - return override_tip_basis; -} - -void SkeletonIK::set_use_magnet(bool p_use) { - use_magnet = p_use; -} - -bool SkeletonIK::is_using_magnet() const { - return use_magnet; -} - -void SkeletonIK::set_magnet_position(const Vector3 &p_local_position) { - magnet_position = p_local_position; -} - -const Vector3 &SkeletonIK::get_magnet_position() const { - return magnet_position; -} - -void SkeletonIK::set_min_distance(real_t p_min_distance) { - min_distance = p_min_distance; -} - -void SkeletonIK::set_max_iterations(int p_iterations) { - max_iterations = p_iterations; -} - -bool SkeletonIK::is_running() { - return is_processing_internal(); -} - -void SkeletonIK::start(bool p_one_time) { - if (p_one_time) { - set_process_internal(false); - _solve_chain(); - } else { - set_process_internal(true); - } -} - -void SkeletonIK::stop() { - set_process_internal(false); -} - -Transform SkeletonIK::_get_target_transform() { - - if (!target_node_override && !target_node_path_override.is_empty()) - target_node_override = Object::cast_to<Spatial>(get_node(target_node_path_override)); - - if (target_node_override) - return target_node_override->get_global_transform(); - else - return target; -} - -void SkeletonIK::reload_chain() { - - FabrikInverseKinematic::free_task(task); - task = NULL; - - if (!skeleton) - return; - - task = FabrikInverseKinematic::create_simple_task(skeleton, skeleton->find_bone(root_bone), skeleton->find_bone(tip_bone), _get_target_transform()); - if (task) { - task->max_iterations = max_iterations; - task->min_distance = min_distance; - } -} - -void SkeletonIK::reload_goal() { - if (!task) - return; - - FabrikInverseKinematic::set_goal(task, _get_target_transform()); -} - -void SkeletonIK::_solve_chain() { - if (!task) - return; - FabrikInverseKinematic::solve(task, interpolation, override_tip_basis, use_magnet, magnet_position); -} - -#endif // _3D_DISABLED diff --git a/scene/animation/skeleton_ik.h b/scene/animation/skeleton_ik.h deleted file mode 100644 index 02d5aba5ba..0000000000 --- a/scene/animation/skeleton_ik.h +++ /dev/null @@ -1,220 +0,0 @@ -/*************************************************************************/ -/* skeleton_ik.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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 SKELETON_IK_H -#define SKELETON_IK_H - -#ifndef _3D_DISABLED - -/** - * @author AndreaCatania - */ - -#include "core/math/transform.h" -#include "scene/3d/skeleton.h" - -class FabrikInverseKinematic { - - struct EndEffector { - BoneId tip_bone; - Transform goal_transform; - }; - - struct ChainItem { - - Vector<ChainItem> children; - ChainItem *parent_item; - - // Bone info - BoneId bone; - PhysicalBone *pb; - - real_t length; - /// Positions relative to root bone - Transform initial_transform; - Vector3 current_pos; - // Direction from this bone to child - Vector3 current_ori; - - ChainItem() : - parent_item(NULL), - bone(-1), - pb(NULL), - length(0) {} - - ChainItem *find_child(const BoneId p_bone_id); - ChainItem *add_child(const BoneId p_bone_id); - }; - - struct ChainTip { - ChainItem *chain_item; - const EndEffector *end_effector; - - ChainTip() : - chain_item(NULL), - end_effector(NULL) {} - - ChainTip(ChainItem *p_chain_item, const EndEffector *p_end_effector) : - chain_item(p_chain_item), - end_effector(p_end_effector) {} - - ChainTip(const ChainTip &p_other_ct) : - chain_item(p_other_ct.chain_item), - end_effector(p_other_ct.end_effector) {} - }; - - struct Chain { - ChainItem chain_root; - ChainItem *middle_chain_item; - Vector<ChainTip> tips; - Vector3 magnet_position; - }; - -public: - struct Task { - RID self; - Skeleton *skeleton; - - Chain chain; - - // Settings - real_t min_distance; - int max_iterations; - - // Bone data - BoneId root_bone; - Vector<EndEffector> end_effectors; - - Transform goal_global_transform; - - Task() : - skeleton(NULL), - min_distance(0.01), - max_iterations(10), - root_bone(-1) {} - }; - -private: - /// Init a chain that starts from the root to tip - static bool build_chain(Task *p_task, bool p_force_simple_chain = true); - - static void update_chain(const Skeleton *p_sk, ChainItem *p_chain_item); - - static void solve_simple(Task *p_task, bool p_solve_magnet); - /// Special solvers that solve only chains with one end effector - static void solve_simple_backwards(Chain &r_chain, bool p_solve_magnet); - static void solve_simple_forwards(Chain &r_chain, bool p_solve_magnet); - -public: - static Task *create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform); - static void free_task(Task *p_task); - // The goal of chain should be always in local space - static void set_goal(Task *p_task, const Transform &p_goal); - static void make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta); - static void solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position); -}; - -class SkeletonIK : public Node { - GDCLASS(SkeletonIK, Node); - - StringName root_bone; - StringName tip_bone; - real_t interpolation; - Transform target; - NodePath target_node_path_override; - bool override_tip_basis; - bool use_magnet; - Vector3 magnet_position; - - real_t min_distance; - int max_iterations; - - Skeleton *skeleton; - Spatial *target_node_override; - FabrikInverseKinematic::Task *task; - -protected: - virtual void - _validate_property(PropertyInfo &property) const; - - static void _bind_methods(); - virtual void _notification(int p_what); - -public: - SkeletonIK(); - virtual ~SkeletonIK(); - - void set_root_bone(const StringName &p_root_bone); - StringName get_root_bone() const; - - void set_tip_bone(const StringName &p_tip_bone); - StringName get_tip_bone() const; - - void set_interpolation(real_t p_interpolation); - real_t get_interpolation() const; - - void set_target_transform(const Transform &p_target); - const Transform &get_target_transform() const; - - void set_target_node(const NodePath &p_node); - NodePath get_target_node(); - - void set_override_tip_basis(bool p_override); - bool is_override_tip_basis() const; - - void set_use_magnet(bool p_use); - bool is_using_magnet() const; - - void set_magnet_position(const Vector3 &p_local_position); - const Vector3 &get_magnet_position() const; - - void set_min_distance(real_t p_min_distance); - real_t get_min_distance() const { return min_distance; } - - void set_max_iterations(int p_iterations); - int get_max_iterations() const { return max_iterations; } - - Skeleton *get_parent_skeleton() const { return skeleton; } - - bool is_running(); - - void start(bool p_one_time = false); - void stop(); - -private: - Transform _get_target_transform(); - void reload_chain(); - void reload_goal(); - void _solve_chain(); -}; - -#endif // _3D_DISABLED - -#endif // SKELETON_IK_H diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index 628568afbb..bc28c38e2c 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -297,7 +297,7 @@ Variant Tween::_get_initial_val(const InterpolateData &p_data) const { case TARGETING_METHOD: { // Get the object that is being targeted Object *object = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(object == NULL, p_data.initial_val); + ERR_FAIL_COND_V(object == nullptr, p_data.initial_val); // Are we targeting a property or a method? Variant initial_val; @@ -309,7 +309,7 @@ Variant Tween::_get_initial_val(const InterpolateData &p_data) const { } else { // Call the method and get the initial value from it Callable::CallError error; - initial_val = object->call(p_data.target_key[0], NULL, 0, error); + initial_val = object->call(p_data.target_key[0], nullptr, 0, error); ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); } return initial_val; @@ -329,7 +329,7 @@ Variant Tween::_get_final_val(const InterpolateData &p_data) const { case FOLLOW_METHOD: { // Get the object that is being followed Object *target = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(target == NULL, p_data.initial_val); + ERR_FAIL_COND_V(target == nullptr, p_data.initial_val); // We want to figure out the final value Variant final_val; @@ -341,7 +341,7 @@ Variant Tween::_get_final_val(const InterpolateData &p_data) const { } else { // We're looking at a method. Call the method on the target object Callable::CallError error; - final_val = target->call(p_data.target_key[0], NULL, 0, error); + final_val = target->call(p_data.target_key[0], nullptr, 0, error); ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); } @@ -371,7 +371,7 @@ Variant &Tween::_get_delta_val(InterpolateData &p_data) { case FOLLOW_METHOD: { // We're following an object, so grab that instance Object *target = ObjectDB::get_instance(p_data.target_id); - ERR_FAIL_COND_V(target == NULL, p_data.initial_val); + ERR_FAIL_COND_V(target == nullptr, p_data.initial_val); // We want to figure out the final value Variant final_val; @@ -383,7 +383,7 @@ Variant &Tween::_get_delta_val(InterpolateData &p_data) { } else { // We're looking at a method. Call the method on the target object Callable::CallError error; - final_val = target->call(p_data.target_key[0], NULL, 0, error); + final_val = target->call(p_data.target_key[0], nullptr, 0, error); ERR_FAIL_COND_V(error.error != Callable::CallError::CALL_OK, p_data.initial_val); } @@ -607,7 +607,7 @@ bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) { // Get the object we want to apply the new value to Object *object = ObjectDB::get_instance(p_data.id); - ERR_FAIL_COND_V(object == NULL, false); + ERR_FAIL_COND_V(object == nullptr, false); // What kind of data are we mutating? switch (p_data.type) { @@ -634,7 +634,7 @@ bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) { object->call(p_data.key[0], (const Variant **)arg, 1, error); } else { // Don't pass any argument - object->call(p_data.key[0], NULL, 0, error); + object->call(p_data.key[0], nullptr, 0, error); } // Did we get an error from the function call? @@ -700,7 +700,7 @@ void Tween::_tween_process(float p_delta) { // Get the target object for this interpolation Object *object = ObjectDB::get_instance(data.id); - if (object == NULL) + if (object == nullptr) continue; // Are we still delaying this tween? @@ -860,7 +860,7 @@ void Tween::reset(Object *p_object, StringName p_key) { // Get the target object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); - if (object == NULL) + if (object == nullptr) continue; // Do we have the correct object and key? @@ -901,7 +901,7 @@ void Tween::stop(Object *p_object, StringName p_key) { // Get the object the tween is targeting InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); - if (object == NULL) + if (object == nullptr) continue; // Is this the correct object and does it have the given key? @@ -937,7 +937,7 @@ void Tween::resume(Object *p_object, StringName p_key) { // Grab the object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); - if (object == NULL) + if (object == nullptr) continue; // If the object and string key match, activate it @@ -975,7 +975,7 @@ void Tween::remove(Object *p_object, StringName p_key) { // Get the target object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); - if (object == NULL) + if (object == nullptr) continue; // If the target object and string key match, queue it for removal @@ -1264,7 +1264,7 @@ void Tween::_build_interpolation(InterpolateType p_interpolation_type, Object *p // Validate and apply interpolation data // Give it the object - ERR_FAIL_COND_MSG(p_object == NULL, "Invalid object provided to Tween."); + ERR_FAIL_COND_MSG(p_object == nullptr, "Invalid object provided to Tween."); data.id = p_object->get_instance_id(); // Validate the initial and final values @@ -1335,7 +1335,7 @@ void Tween::interpolate_property(Object *p_object, NodePath p_property, Variant if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); // Build the interpolation data - _build_interpolation(INTER_PROPERTY, p_object, &p_property, NULL, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + _build_interpolation(INTER_PROPERTY, p_object, &p_property, nullptr, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); } void Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { @@ -1350,7 +1350,7 @@ void Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_ if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); // Build the interpolation data - _build_interpolation(INTER_METHOD, p_object, NULL, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + _build_interpolation(INTER_METHOD, p_object, nullptr, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); } void Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) { @@ -1361,7 +1361,7 @@ void Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_c } // Check that the target object is valid - ERR_FAIL_COND(p_object == NULL); + ERR_FAIL_COND(p_object == nullptr); // Duration cannot be negative ERR_FAIL_COND(p_duration < 0); @@ -1418,7 +1418,7 @@ void Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, S } // Check that the target object is valid - ERR_FAIL_COND(p_object == NULL); + ERR_FAIL_COND(p_object == nullptr); // No negative durations allowed ERR_FAIL_COND(p_duration < 0); @@ -1486,8 +1486,8 @@ void Tween::follow_property(Object *p_object, NodePath p_property, Variant p_ini if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); // Confirm the source and target objects are valid - ERR_FAIL_COND(p_object == NULL); - ERR_FAIL_COND(p_target == NULL); + ERR_FAIL_COND(p_object == nullptr); + ERR_FAIL_COND(p_target == nullptr); // No negative durations ERR_FAIL_COND(p_duration < 0); @@ -1547,8 +1547,8 @@ void Tween::follow_method(Object *p_object, StringName p_method, Variant p_initi if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); // Verify the source and target objects are valid - ERR_FAIL_COND(p_object == NULL); - ERR_FAIL_COND(p_target == NULL); + ERR_FAIL_COND(p_object == nullptr); + ERR_FAIL_COND(p_target == nullptr); // No negative durations ERR_FAIL_COND(p_duration < 0); @@ -1566,7 +1566,7 @@ void Tween::follow_method(Object *p_object, StringName p_method, Variant p_initi // Call the method to get the target value Callable::CallError error; - Variant target_val = p_target->call(p_target_method, NULL, 0, error); + Variant target_val = p_target->call(p_target_method, nullptr, 0, error); ERR_FAIL_COND(error.error != Callable::CallError::CALL_OK); // Convert target INT values to FLOAT as they are better for interpolation @@ -1610,8 +1610,8 @@ void Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_ if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); // Verify both objects are valid - ERR_FAIL_COND(p_object == NULL); - ERR_FAIL_COND(p_initial == NULL); + ERR_FAIL_COND(p_object == nullptr); + ERR_FAIL_COND(p_initial == nullptr); // No negative durations ERR_FAIL_COND(p_duration < 0); @@ -1676,8 +1676,8 @@ void Tween::targeting_method(Object *p_object, StringName p_method, Object *p_in if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); // Make sure the given objects are valid - ERR_FAIL_COND(p_object == NULL); - ERR_FAIL_COND(p_initial == NULL); + ERR_FAIL_COND(p_object == nullptr); + ERR_FAIL_COND(p_initial == nullptr); // No negative durations ERR_FAIL_COND(p_duration < 0); @@ -1695,7 +1695,7 @@ void Tween::targeting_method(Object *p_object, StringName p_method, Object *p_in // Call the method to get the initial value Callable::CallError error; - Variant initial_val = p_initial->call(p_initial_method, NULL, 0, error); + Variant initial_val = p_initial->call(p_initial_method, nullptr, 0, error); ERR_FAIL_COND(error.error != Callable::CallError::CALL_OK); // Convert initial INT values to FLOAT as they aer better for interpolation |