animation_cache.cpp 9.5 KB

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