2
0

animation_cache.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*************************************************************************/
  2. /* animation_cache.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_cache.h"
  31. void AnimationCache::_node_exit_tree(Node *p_node) {
  32. //it is one shot, so it disconnects upon arrival
  33. ERR_FAIL_COND(!connected_nodes.has(p_node));
  34. connected_nodes.erase(p_node);
  35. for (int i = 0; i < path_cache.size(); i++) {
  36. if (path_cache[i].node != p_node)
  37. continue;
  38. path_cache.write[i].valid = false; //invalidate path cache
  39. }
  40. }
  41. void AnimationCache::_animation_changed() {
  42. _clear_cache();
  43. }
  44. void AnimationCache::_clear_cache() {
  45. while (connected_nodes.size()) {
  46. connected_nodes.front()->get()->disconnect("tree_exiting", this, "_node_exit_tree");
  47. connected_nodes.erase(connected_nodes.front());
  48. }
  49. path_cache.clear();
  50. cache_valid = false;
  51. cache_dirty = true;
  52. }
  53. void AnimationCache::_update_cache() {
  54. cache_valid = false;
  55. ERR_FAIL_COND(!root);
  56. ERR_FAIL_COND(!root->is_inside_tree());
  57. ERR_FAIL_COND(animation.is_null());
  58. for (int i = 0; i < animation->get_track_count(); i++) {
  59. NodePath np = animation->track_get_path(i);
  60. Node *node = root->get_node(np);
  61. if (!node) {
  62. path_cache.push_back(Path());
  63. ERR_CONTINUE_MSG(!node, "Invalid track path in animation '" + np + "'.");
  64. }
  65. Path path;
  66. Ref<Resource> res;
  67. if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
  68. if (np.get_subname_count() > 1) {
  69. path_cache.push_back(Path());
  70. ERR_CONTINUE_MSG(animation->track_get_type(i) == Animation::TYPE_TRANSFORM, "Transform tracks can't have a subpath '" + np + "'.");
  71. }
  72. Spatial *sp = Object::cast_to<Spatial>(node);
  73. if (!sp) {
  74. path_cache.push_back(Path());
  75. ERR_CONTINUE_MSG(!sp, "Transform track not of type Spatial '" + np + "'.");
  76. }
  77. if (np.get_subname_count() == 1) {
  78. StringName property = np.get_subname(0);
  79. String ps = property;
  80. Skeleton *sk = Object::cast_to<Skeleton>(node);
  81. if (!sk) {
  82. path_cache.push_back(Path());
  83. ERR_CONTINUE_MSG(!sk, "Property defined in Transform track, but not a Skeleton! '" + np + "'.");
  84. }
  85. int idx = sk->find_bone(ps);
  86. if (idx == -1) {
  87. path_cache.push_back(Path());
  88. ERR_CONTINUE_MSG(idx == -1, "Property defined in Transform track, but not a Skeleton Bone! '" + np + "'.");
  89. }
  90. path.bone_idx = idx;
  91. path.skeleton = sk;
  92. }
  93. path.spatial = sp;
  94. } else {
  95. if (np.get_subname_count() > 0) {
  96. RES res2;
  97. Vector<StringName> leftover_subpath;
  98. // We don't want to cache the last resource unless it is a method call
  99. bool is_method = animation->track_get_type(i) == Animation::TYPE_METHOD;
  100. root->get_node_and_resource(np, res2, leftover_subpath, is_method);
  101. if (res2.is_valid()) {
  102. path.resource = res2;
  103. } else {
  104. path.node = node;
  105. }
  106. path.object = res2.is_valid() ? res2.ptr() : (Object *)node;
  107. path.subpath = leftover_subpath;
  108. } else {
  109. path.node = node;
  110. path.object = node;
  111. path.subpath = np.get_subnames();
  112. }
  113. }
  114. if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
  115. if (np.get_subname_count() == 0) {
  116. path_cache.push_back(Path());
  117. ERR_CONTINUE_MSG(np.get_subname_count() == 0, "Value Track lacks property: " + np + ".");
  118. }
  119. } else if (animation->track_get_type(i) == Animation::TYPE_METHOD) {
  120. if (path.subpath.size() != 0) { // Trying to call a method of a non-resource
  121. path_cache.push_back(Path());
  122. ERR_CONTINUE_MSG(path.subpath.size() != 0, "Method Track has property: " + np + ".");
  123. }
  124. }
  125. path.valid = true;
  126. path_cache.push_back(path);
  127. if (!connected_nodes.has(path.node)) {
  128. connected_nodes.insert(path.node);
  129. path.node->connect("tree_exiting", this, "_node_exit_tree", Node::make_binds(path.node), CONNECT_ONESHOT);
  130. }
  131. }
  132. cache_dirty = false;
  133. cache_valid = true;
  134. }
  135. void AnimationCache::set_track_transform(int p_idx, const Transform &p_transform) {
  136. if (cache_dirty)
  137. _update_cache();
  138. ERR_FAIL_COND(!cache_valid);
  139. ERR_FAIL_INDEX(p_idx, path_cache.size());
  140. Path &p = path_cache.write[p_idx];
  141. if (!p.valid)
  142. return;
  143. ERR_FAIL_COND(!p.node);
  144. ERR_FAIL_COND(!p.spatial);
  145. if (p.skeleton) {
  146. p.skeleton->set_bone_pose(p.bone_idx, p_transform);
  147. } else {
  148. p.spatial->set_transform(p_transform);
  149. }
  150. }
  151. void AnimationCache::set_track_value(int p_idx, const Variant &p_value) {
  152. if (cache_dirty)
  153. _update_cache();
  154. ERR_FAIL_COND(!cache_valid);
  155. ERR_FAIL_INDEX(p_idx, path_cache.size());
  156. Path &p = path_cache.write[p_idx];
  157. if (!p.valid)
  158. return;
  159. ERR_FAIL_COND(!p.object);
  160. p.object->set_indexed(p.subpath, p_value);
  161. }
  162. void AnimationCache::call_track(int p_idx, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
  163. if (cache_dirty)
  164. _update_cache();
  165. ERR_FAIL_COND(!cache_valid);
  166. ERR_FAIL_INDEX(p_idx, path_cache.size());
  167. Path &p = path_cache.write[p_idx];
  168. if (!p.valid)
  169. return;
  170. ERR_FAIL_COND(!p.object);
  171. p.object->call(p_method, p_args, p_argcount, r_error);
  172. }
  173. void AnimationCache::set_all(float p_time, float p_delta) {
  174. if (cache_dirty)
  175. _update_cache();
  176. ERR_FAIL_COND(!cache_valid);
  177. int tc = animation->get_track_count();
  178. for (int i = 0; i < tc; i++) {
  179. switch (animation->track_get_type(i)) {
  180. case Animation::TYPE_TRANSFORM: {
  181. Vector3 loc, scale;
  182. Quat rot;
  183. animation->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
  184. Transform tr(Basis(rot), loc);
  185. tr.basis.scale(scale);
  186. set_track_transform(i, tr);
  187. } break;
  188. case Animation::TYPE_VALUE: {
  189. if (animation->value_track_get_update_mode(i) == Animation::UPDATE_CONTINUOUS || (animation->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE && p_delta == 0)) {
  190. Variant v = animation->value_track_interpolate(i, p_time);
  191. set_track_value(i, v);
  192. } else {
  193. List<int> indices;
  194. animation->value_track_get_key_indices(i, p_time, p_delta, &indices);
  195. for (List<int>::Element *E = indices.front(); E; E = E->next()) {
  196. Variant v = animation->track_get_key_value(i, E->get());
  197. set_track_value(i, v);
  198. }
  199. }
  200. } break;
  201. case Animation::TYPE_METHOD: {
  202. List<int> indices;
  203. animation->method_track_get_key_indices(i, p_time, p_delta, &indices);
  204. for (List<int>::Element *E = indices.front(); E; E = E->next()) {
  205. Vector<Variant> args = animation->method_track_get_params(i, E->get());
  206. StringName name = animation->method_track_get_name(i, E->get());
  207. Variant::CallError err;
  208. if (!args.size()) {
  209. call_track(i, name, NULL, 0, err);
  210. } else {
  211. Vector<const Variant *> argptrs;
  212. argptrs.resize(args.size());
  213. for (int j = 0; j < args.size(); j++) {
  214. argptrs.write[j] = &args.write[j];
  215. }
  216. call_track(i, name, (const Variant **)&argptrs[0], args.size(), err);
  217. }
  218. }
  219. } break;
  220. default: {
  221. }
  222. }
  223. }
  224. }
  225. void AnimationCache::set_animation(const Ref<Animation> &p_animation) {
  226. _clear_cache();
  227. if (animation.is_valid())
  228. animation->disconnect("changed", this, "_animation_changed");
  229. animation = p_animation;
  230. if (animation.is_valid())
  231. animation->connect("changed", this, "_animation_changed");
  232. }
  233. void AnimationCache::_bind_methods() {
  234. ClassDB::bind_method(D_METHOD("_node_exit_tree"), &AnimationCache::_node_exit_tree);
  235. ClassDB::bind_method(D_METHOD("_animation_changed"), &AnimationCache::_animation_changed);
  236. }
  237. void AnimationCache::set_root(Node *p_root) {
  238. _clear_cache();
  239. root = p_root;
  240. }
  241. AnimationCache::AnimationCache() {
  242. root = NULL;
  243. cache_dirty = true;
  244. cache_valid = false;
  245. }