animation_cache.cpp 9.5 KB

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