animation_blend_space_1d.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**************************************************************************/
  2. /* animation_blend_space_1d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "animation_blend_space_1d.h"
  31. #include "animation_blend_tree.h"
  32. void AnimationNodeBlendSpace1D::get_parameter_list(List<PropertyInfo> *r_list) const {
  33. r_list->push_back(PropertyInfo(Variant::FLOAT, blend_position));
  34. r_list->push_back(PropertyInfo(Variant::INT, closest, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  35. r_list->push_back(PropertyInfo(Variant::FLOAT, length_internal, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE));
  36. }
  37. Variant AnimationNodeBlendSpace1D::get_parameter_default_value(const StringName &p_parameter) const {
  38. if (p_parameter == closest) {
  39. return -1;
  40. } else {
  41. return 0;
  42. }
  43. }
  44. Ref<AnimationNode> AnimationNodeBlendSpace1D::get_child_by_name(const StringName &p_name) const {
  45. return get_blend_point_node(p_name.operator String().to_int());
  46. }
  47. void AnimationNodeBlendSpace1D::_validate_property(PropertyInfo &p_property) const {
  48. if (p_property.name.begins_with("blend_point_")) {
  49. String left = p_property.name.get_slicec('/', 0);
  50. int idx = left.get_slicec('_', 2).to_int();
  51. if (idx >= blend_points_used) {
  52. p_property.usage = PROPERTY_USAGE_NONE;
  53. }
  54. }
  55. }
  56. void AnimationNodeBlendSpace1D::_tree_changed() {
  57. AnimationRootNode::_tree_changed();
  58. }
  59. void AnimationNodeBlendSpace1D::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {
  60. AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);
  61. }
  62. void AnimationNodeBlendSpace1D::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {
  63. AnimationRootNode::_animation_node_removed(p_oid, p_node);
  64. }
  65. void AnimationNodeBlendSpace1D::_bind_methods() {
  66. ClassDB::bind_method(D_METHOD("add_blend_point", "node", "pos", "at_index"), &AnimationNodeBlendSpace1D::add_blend_point, DEFVAL(-1));
  67. ClassDB::bind_method(D_METHOD("set_blend_point_position", "point", "pos"), &AnimationNodeBlendSpace1D::set_blend_point_position);
  68. ClassDB::bind_method(D_METHOD("get_blend_point_position", "point"), &AnimationNodeBlendSpace1D::get_blend_point_position);
  69. ClassDB::bind_method(D_METHOD("set_blend_point_node", "point", "node"), &AnimationNodeBlendSpace1D::set_blend_point_node);
  70. ClassDB::bind_method(D_METHOD("get_blend_point_node", "point"), &AnimationNodeBlendSpace1D::get_blend_point_node);
  71. ClassDB::bind_method(D_METHOD("remove_blend_point", "point"), &AnimationNodeBlendSpace1D::remove_blend_point);
  72. ClassDB::bind_method(D_METHOD("get_blend_point_count"), &AnimationNodeBlendSpace1D::get_blend_point_count);
  73. ClassDB::bind_method(D_METHOD("set_min_space", "min_space"), &AnimationNodeBlendSpace1D::set_min_space);
  74. ClassDB::bind_method(D_METHOD("get_min_space"), &AnimationNodeBlendSpace1D::get_min_space);
  75. ClassDB::bind_method(D_METHOD("set_max_space", "max_space"), &AnimationNodeBlendSpace1D::set_max_space);
  76. ClassDB::bind_method(D_METHOD("get_max_space"), &AnimationNodeBlendSpace1D::get_max_space);
  77. ClassDB::bind_method(D_METHOD("set_snap", "snap"), &AnimationNodeBlendSpace1D::set_snap);
  78. ClassDB::bind_method(D_METHOD("get_snap"), &AnimationNodeBlendSpace1D::get_snap);
  79. ClassDB::bind_method(D_METHOD("set_value_label", "text"), &AnimationNodeBlendSpace1D::set_value_label);
  80. ClassDB::bind_method(D_METHOD("get_value_label"), &AnimationNodeBlendSpace1D::get_value_label);
  81. ClassDB::bind_method(D_METHOD("set_blend_mode", "mode"), &AnimationNodeBlendSpace1D::set_blend_mode);
  82. ClassDB::bind_method(D_METHOD("get_blend_mode"), &AnimationNodeBlendSpace1D::get_blend_mode);
  83. ClassDB::bind_method(D_METHOD("set_use_sync", "enable"), &AnimationNodeBlendSpace1D::set_use_sync);
  84. ClassDB::bind_method(D_METHOD("is_using_sync"), &AnimationNodeBlendSpace1D::is_using_sync);
  85. ClassDB::bind_method(D_METHOD("_add_blend_point", "index", "node"), &AnimationNodeBlendSpace1D::_add_blend_point);
  86. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  87. ADD_PROPERTYI(PropertyInfo(Variant::OBJECT, "blend_point_" + itos(i) + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationRootNode", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_add_blend_point", "get_blend_point_node", i);
  88. ADD_PROPERTYI(PropertyInfo(Variant::FLOAT, "blend_point_" + itos(i) + "/pos", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_blend_point_position", "get_blend_point_position", i);
  89. }
  90. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_min_space", "get_min_space");
  91. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_max_space", "get_max_space");
  92. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "snap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_snap", "get_snap");
  93. ADD_PROPERTY(PropertyInfo(Variant::STRING, "value_label", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_value_label", "get_value_label");
  94. ADD_PROPERTY(PropertyInfo(Variant::INT, "blend_mode", PROPERTY_HINT_ENUM, "Interpolated,Discrete,Carry", PROPERTY_USAGE_NO_EDITOR), "set_blend_mode", "get_blend_mode");
  95. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sync", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_use_sync", "is_using_sync");
  96. BIND_ENUM_CONSTANT(BLEND_MODE_INTERPOLATED);
  97. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE);
  98. BIND_ENUM_CONSTANT(BLEND_MODE_DISCRETE_CARRY);
  99. }
  100. void AnimationNodeBlendSpace1D::get_child_nodes(List<ChildNode> *r_child_nodes) {
  101. for (int i = 0; i < blend_points_used; i++) {
  102. ChildNode cn;
  103. cn.name = itos(i);
  104. cn.node = blend_points[i].node;
  105. r_child_nodes->push_back(cn);
  106. }
  107. }
  108. void AnimationNodeBlendSpace1D::add_blend_point(const Ref<AnimationRootNode> &p_node, float p_position, int p_at_index) {
  109. ERR_FAIL_COND(blend_points_used >= MAX_BLEND_POINTS);
  110. ERR_FAIL_COND(p_node.is_null());
  111. ERR_FAIL_COND(p_at_index < -1 || p_at_index > blend_points_used);
  112. if (p_at_index == -1 || p_at_index == blend_points_used) {
  113. p_at_index = blend_points_used;
  114. } else {
  115. for (int i = blend_points_used - 1; i > p_at_index; i--) {
  116. blend_points[i] = blend_points[i - 1];
  117. }
  118. }
  119. blend_points[p_at_index].node = p_node;
  120. blend_points[p_at_index].position = p_position;
  121. blend_points[p_at_index].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  122. blend_points[p_at_index].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  123. blend_points[p_at_index].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  124. blend_points_used++;
  125. emit_signal(SNAME("tree_changed"));
  126. }
  127. void AnimationNodeBlendSpace1D::set_blend_point_position(int p_point, float p_position) {
  128. ERR_FAIL_INDEX(p_point, blend_points_used);
  129. blend_points[p_point].position = p_position;
  130. }
  131. void AnimationNodeBlendSpace1D::set_blend_point_node(int p_point, const Ref<AnimationRootNode> &p_node) {
  132. ERR_FAIL_INDEX(p_point, blend_points_used);
  133. ERR_FAIL_COND(p_node.is_null());
  134. if (blend_points[p_point].node.is_valid()) {
  135. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
  136. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
  137. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
  138. }
  139. blend_points[p_point].node = p_node;
  140. blend_points[p_point].node->connect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed), CONNECT_REFERENCE_COUNTED);
  141. blend_points[p_point].node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);
  142. blend_points[p_point].node->connect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed), CONNECT_REFERENCE_COUNTED);
  143. emit_signal(SNAME("tree_changed"));
  144. }
  145. float AnimationNodeBlendSpace1D::get_blend_point_position(int p_point) const {
  146. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, 0);
  147. return blend_points[p_point].position;
  148. }
  149. Ref<AnimationRootNode> AnimationNodeBlendSpace1D::get_blend_point_node(int p_point) const {
  150. ERR_FAIL_INDEX_V(p_point, MAX_BLEND_POINTS, Ref<AnimationRootNode>());
  151. return blend_points[p_point].node;
  152. }
  153. void AnimationNodeBlendSpace1D::remove_blend_point(int p_point) {
  154. ERR_FAIL_INDEX(p_point, blend_points_used);
  155. ERR_FAIL_COND(blend_points[p_point].node.is_null());
  156. blend_points[p_point].node->disconnect("tree_changed", callable_mp(this, &AnimationNodeBlendSpace1D::_tree_changed));
  157. blend_points[p_point].node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_renamed));
  158. blend_points[p_point].node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeBlendSpace1D::_animation_node_removed));
  159. for (int i = p_point; i < blend_points_used - 1; i++) {
  160. blend_points[i] = blend_points[i + 1];
  161. }
  162. blend_points_used--;
  163. emit_signal(SNAME("animation_node_removed"), get_instance_id(), itos(p_point));
  164. emit_signal(SNAME("tree_changed"));
  165. }
  166. int AnimationNodeBlendSpace1D::get_blend_point_count() const {
  167. return blend_points_used;
  168. }
  169. void AnimationNodeBlendSpace1D::set_min_space(float p_min) {
  170. min_space = p_min;
  171. if (min_space >= max_space) {
  172. min_space = max_space - 1;
  173. }
  174. }
  175. float AnimationNodeBlendSpace1D::get_min_space() const {
  176. return min_space;
  177. }
  178. void AnimationNodeBlendSpace1D::set_max_space(float p_max) {
  179. max_space = p_max;
  180. if (max_space <= min_space) {
  181. max_space = min_space + 1;
  182. }
  183. }
  184. float AnimationNodeBlendSpace1D::get_max_space() const {
  185. return max_space;
  186. }
  187. void AnimationNodeBlendSpace1D::set_snap(float p_snap) {
  188. snap = p_snap;
  189. }
  190. float AnimationNodeBlendSpace1D::get_snap() const {
  191. return snap;
  192. }
  193. void AnimationNodeBlendSpace1D::set_value_label(const String &p_label) {
  194. value_label = p_label;
  195. }
  196. String AnimationNodeBlendSpace1D::get_value_label() const {
  197. return value_label;
  198. }
  199. void AnimationNodeBlendSpace1D::set_blend_mode(BlendMode p_blend_mode) {
  200. blend_mode = p_blend_mode;
  201. }
  202. AnimationNodeBlendSpace1D::BlendMode AnimationNodeBlendSpace1D::get_blend_mode() const {
  203. return blend_mode;
  204. }
  205. void AnimationNodeBlendSpace1D::set_use_sync(bool p_sync) {
  206. sync = p_sync;
  207. }
  208. bool AnimationNodeBlendSpace1D::is_using_sync() const {
  209. return sync;
  210. }
  211. void AnimationNodeBlendSpace1D::_add_blend_point(int p_index, const Ref<AnimationRootNode> &p_node) {
  212. if (p_index == blend_points_used) {
  213. add_blend_point(p_node, 0);
  214. } else {
  215. set_blend_point_node(p_index, p_node);
  216. }
  217. }
  218. double AnimationNodeBlendSpace1D::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {
  219. if (blend_points_used == 0) {
  220. return 0.0;
  221. }
  222. AnimationMixer::PlaybackInfo pi = p_playback_info;
  223. if (blend_points_used == 1) {
  224. // only one point available, just play that animation
  225. pi.weight = 1.0;
  226. return blend_node(blend_points[0].node, blend_points[0].name, pi, FILTER_IGNORE, true, p_test_only);
  227. }
  228. double blend_pos = get_parameter(blend_position);
  229. int cur_closest = get_parameter(closest);
  230. double cur_length_internal = get_parameter(length_internal);
  231. double max_time_remaining = 0.0;
  232. if (blend_mode == BLEND_MODE_INTERPOLATED) {
  233. int point_lower = -1;
  234. float pos_lower = 0.0;
  235. int point_higher = -1;
  236. float pos_higher = 0.0;
  237. // find the closest two points to blend between
  238. for (int i = 0; i < blend_points_used; i++) {
  239. float pos = blend_points[i].position;
  240. if (pos <= blend_pos) {
  241. if (point_lower == -1 || pos > pos_lower) {
  242. point_lower = i;
  243. pos_lower = pos;
  244. }
  245. } else if (point_higher == -1 || pos < pos_higher) {
  246. point_higher = i;
  247. pos_higher = pos;
  248. }
  249. }
  250. // fill in weights
  251. float weights[MAX_BLEND_POINTS] = {};
  252. if (point_lower == -1 && point_higher != -1) {
  253. // we are on the left side, no other point to the left
  254. // we just play the next point.
  255. weights[point_higher] = 1.0;
  256. } else if (point_higher == -1) {
  257. // we are on the right side, no other point to the right
  258. // we just play the previous point
  259. weights[point_lower] = 1.0;
  260. } else {
  261. // we are between two points.
  262. // figure out weights, then blend the animations
  263. float distance_between_points = pos_higher - pos_lower;
  264. float current_pos_inbetween = blend_pos - pos_lower;
  265. float blend_percentage = current_pos_inbetween / distance_between_points;
  266. float blend_lower = 1.0 - blend_percentage;
  267. float blend_higher = blend_percentage;
  268. weights[point_lower] = blend_lower;
  269. weights[point_higher] = blend_higher;
  270. }
  271. // actually blend the animations now
  272. for (int i = 0; i < blend_points_used; i++) {
  273. if (i == point_lower || i == point_higher) {
  274. pi.weight = weights[i];
  275. double remaining = blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  276. max_time_remaining = MAX(max_time_remaining, remaining);
  277. } else if (sync) {
  278. pi.weight = 0;
  279. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  280. }
  281. }
  282. } else {
  283. int new_closest = -1;
  284. double new_closest_dist = 1e20;
  285. for (int i = 0; i < blend_points_used; i++) {
  286. double d = abs(blend_points[i].position - blend_pos);
  287. if (d < new_closest_dist) {
  288. new_closest = i;
  289. new_closest_dist = d;
  290. }
  291. }
  292. if (new_closest != cur_closest && new_closest != -1) {
  293. double from = 0.0;
  294. if (blend_mode == BLEND_MODE_DISCRETE_CARRY && cur_closest != -1) {
  295. //for ping-pong loop
  296. Ref<AnimationNodeAnimation> na_c = static_cast<Ref<AnimationNodeAnimation>>(blend_points[cur_closest].node);
  297. Ref<AnimationNodeAnimation> na_n = static_cast<Ref<AnimationNodeAnimation>>(blend_points[new_closest].node);
  298. if (!na_c.is_null() && !na_n.is_null()) {
  299. na_n->set_backward(na_c->is_backward());
  300. }
  301. //see how much animation remains
  302. pi.seeked = false;
  303. pi.weight = 0;
  304. from = cur_length_internal - blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  305. }
  306. pi.time = from;
  307. pi.seeked = true;
  308. pi.weight = 1.0;
  309. max_time_remaining = blend_node(blend_points[new_closest].node, blend_points[new_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  310. cur_length_internal = from + max_time_remaining;
  311. cur_closest = new_closest;
  312. } else {
  313. pi.weight = 1.0;
  314. max_time_remaining = blend_node(blend_points[cur_closest].node, blend_points[cur_closest].name, pi, FILTER_IGNORE, true, p_test_only);
  315. }
  316. if (sync) {
  317. pi = p_playback_info;
  318. pi.weight = 0;
  319. for (int i = 0; i < blend_points_used; i++) {
  320. if (i != cur_closest) {
  321. blend_node(blend_points[i].node, blend_points[i].name, pi, FILTER_IGNORE, true, p_test_only);
  322. }
  323. }
  324. }
  325. }
  326. set_parameter(closest, cur_closest);
  327. set_parameter(length_internal, cur_length_internal);
  328. return max_time_remaining;
  329. }
  330. String AnimationNodeBlendSpace1D::get_caption() const {
  331. return "BlendSpace1D";
  332. }
  333. AnimationNodeBlendSpace1D::AnimationNodeBlendSpace1D() {
  334. for (int i = 0; i < MAX_BLEND_POINTS; i++) {
  335. blend_points[i].name = itos(i);
  336. }
  337. }
  338. AnimationNodeBlendSpace1D::~AnimationNodeBlendSpace1D() {
  339. }