test_animation.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**************************************************************************/
  2. /* test_animation.h */
  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. #ifndef TEST_ANIMATION_H
  31. #define TEST_ANIMATION_H
  32. #include "scene/resources/animation.h"
  33. #include "tests/test_macros.h"
  34. namespace TestAnimation {
  35. TEST_CASE("[Animation] Empty animation getters") {
  36. const Ref<Animation> animation = memnew(Animation);
  37. CHECK(animation->get_length() == doctest::Approx(real_t(1.0)));
  38. CHECK(animation->get_step() == doctest::Approx(real_t(1.0 / 30)));
  39. }
  40. TEST_CASE("[Animation] Create value track") {
  41. // This creates an animation that makes the node "Enemy" move to the right by
  42. // 100 pixels in 0.5 seconds.
  43. Ref<Animation> animation = memnew(Animation);
  44. const int track_index = animation->add_track(Animation::TYPE_VALUE);
  45. CHECK(track_index == 0);
  46. animation->track_set_path(track_index, NodePath("Enemy:position:x"));
  47. animation->track_insert_key(track_index, 0.0, 0);
  48. animation->track_insert_key(track_index, 0.5, 100);
  49. CHECK(animation->get_track_count() == 1);
  50. CHECK(!animation->track_is_compressed(0));
  51. CHECK(int(animation->track_get_key_value(0, 0)) == 0);
  52. CHECK(int(animation->track_get_key_value(0, 1)) == 100);
  53. CHECK(animation->value_track_interpolate(0, -0.2) == doctest::Approx(0.0));
  54. CHECK(animation->value_track_interpolate(0, 0.0) == doctest::Approx(0.0));
  55. CHECK(animation->value_track_interpolate(0, 0.2) == doctest::Approx(40.0));
  56. CHECK(animation->value_track_interpolate(0, 0.4) == doctest::Approx(80.0));
  57. CHECK(animation->value_track_interpolate(0, 0.5) == doctest::Approx(100.0));
  58. CHECK(animation->value_track_interpolate(0, 0.6) == doctest::Approx(100.0));
  59. CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
  60. CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
  61. ERR_PRINT_OFF;
  62. // Nonexistent keys.
  63. CHECK(animation->track_get_key_value(0, 2).is_null());
  64. CHECK(animation->track_get_key_value(0, -1).is_null());
  65. CHECK(animation->track_get_key_transition(0, 2) == doctest::Approx(real_t(-1.0)));
  66. // Nonexistent track (and keys).
  67. CHECK(animation->track_get_key_value(1, 0).is_null());
  68. CHECK(animation->track_get_key_value(1, 1).is_null());
  69. CHECK(animation->track_get_key_value(1, 2).is_null());
  70. CHECK(animation->track_get_key_value(1, -1).is_null());
  71. CHECK(animation->track_get_key_transition(1, 0) == doctest::Approx(real_t(-1.0)));
  72. // This is a value track, so the methods below should return errors.
  73. CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  74. CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  75. CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  76. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
  77. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  78. ERR_PRINT_ON;
  79. }
  80. TEST_CASE("[Animation] Create 3D position track") {
  81. Ref<Animation> animation = memnew(Animation);
  82. const int track_index = animation->add_track(Animation::TYPE_POSITION_3D);
  83. animation->track_set_path(track_index, NodePath("Enemy:position"));
  84. animation->position_track_insert_key(track_index, 0.0, Vector3(0, 1, 2));
  85. animation->position_track_insert_key(track_index, 0.5, Vector3(3.5, 4, 5));
  86. CHECK(animation->get_track_count() == 1);
  87. CHECK(!animation->track_is_compressed(0));
  88. CHECK(Vector3(animation->track_get_key_value(0, 0)).is_equal_approx(Vector3(0, 1, 2)));
  89. CHECK(Vector3(animation->track_get_key_value(0, 1)).is_equal_approx(Vector3(3.5, 4, 5)));
  90. Vector3 r_interpolation;
  91. CHECK(animation->try_position_track_interpolate(0, -0.2, &r_interpolation) == OK);
  92. CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
  93. CHECK(animation->try_position_track_interpolate(0, 0.0, &r_interpolation) == OK);
  94. CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
  95. CHECK(animation->try_position_track_interpolate(0, 0.2, &r_interpolation) == OK);
  96. CHECK(r_interpolation.is_equal_approx(Vector3(1.4, 2.2, 3.2)));
  97. CHECK(animation->try_position_track_interpolate(0, 0.4, &r_interpolation) == OK);
  98. CHECK(r_interpolation.is_equal_approx(Vector3(2.8, 3.4, 4.4)));
  99. CHECK(animation->try_position_track_interpolate(0, 0.5, &r_interpolation) == OK);
  100. CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
  101. CHECK(animation->try_position_track_interpolate(0, 0.6, &r_interpolation) == OK);
  102. CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
  103. // 3D position tracks always use linear interpolation for performance reasons.
  104. CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
  105. CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
  106. // This is a 3D position track, so the methods below should return errors.
  107. ERR_PRINT_OFF;
  108. CHECK(animation->value_track_interpolate(0, 0.0).is_null());
  109. CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  110. CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  111. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
  112. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  113. ERR_PRINT_ON;
  114. }
  115. TEST_CASE("[Animation] Create 3D rotation track") {
  116. Ref<Animation> animation = memnew(Animation);
  117. const int track_index = animation->add_track(Animation::TYPE_ROTATION_3D);
  118. animation->track_set_path(track_index, NodePath("Enemy:rotation"));
  119. animation->rotation_track_insert_key(track_index, 0.0, Quaternion::from_euler(Vector3(0, 1, 2)));
  120. animation->rotation_track_insert_key(track_index, 0.5, Quaternion::from_euler(Vector3(3.5, 4, 5)));
  121. CHECK(animation->get_track_count() == 1);
  122. CHECK(!animation->track_is_compressed(0));
  123. CHECK(Quaternion(animation->track_get_key_value(0, 0)).is_equal_approx(Quaternion::from_euler(Vector3(0, 1, 2))));
  124. CHECK(Quaternion(animation->track_get_key_value(0, 1)).is_equal_approx(Quaternion::from_euler(Vector3(3.5, 4, 5))));
  125. Quaternion r_interpolation;
  126. CHECK(animation->try_rotation_track_interpolate(0, -0.2, &r_interpolation) == OK);
  127. CHECK(r_interpolation.is_equal_approx(Quaternion(0.403423, 0.259035, 0.73846, 0.47416)));
  128. CHECK(animation->try_rotation_track_interpolate(0, 0.0, &r_interpolation) == OK);
  129. CHECK(r_interpolation.is_equal_approx(Quaternion(0.403423, 0.259035, 0.73846, 0.47416)));
  130. CHECK(animation->try_rotation_track_interpolate(0, 0.2, &r_interpolation) == OK);
  131. CHECK(r_interpolation.is_equal_approx(Quaternion(0.336182, 0.30704, 0.751515, 0.477425)));
  132. CHECK(animation->try_rotation_track_interpolate(0, 0.4, &r_interpolation) == OK);
  133. CHECK(r_interpolation.is_equal_approx(Quaternion(0.266585, 0.352893, 0.759303, 0.477344)));
  134. CHECK(animation->try_rotation_track_interpolate(0, 0.5, &r_interpolation) == OK);
  135. CHECK(r_interpolation.is_equal_approx(Quaternion(0.231055, 0.374912, 0.761204, 0.476048)));
  136. CHECK(animation->try_rotation_track_interpolate(0, 0.6, &r_interpolation) == OK);
  137. CHECK(r_interpolation.is_equal_approx(Quaternion(0.231055, 0.374912, 0.761204, 0.476048)));
  138. // 3D rotation tracks always use linear interpolation for performance reasons.
  139. CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
  140. CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
  141. // This is a 3D rotation track, so the methods below should return errors.
  142. ERR_PRINT_OFF;
  143. CHECK(animation->value_track_interpolate(0, 0.0).is_null());
  144. CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  145. CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  146. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(real_t(0.0)));
  147. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  148. ERR_PRINT_ON;
  149. }
  150. TEST_CASE("[Animation] Create 3D scale track") {
  151. Ref<Animation> animation = memnew(Animation);
  152. const int track_index = animation->add_track(Animation::TYPE_SCALE_3D);
  153. animation->track_set_path(track_index, NodePath("Enemy:scale"));
  154. animation->scale_track_insert_key(track_index, 0.0, Vector3(0, 1, 2));
  155. animation->scale_track_insert_key(track_index, 0.5, Vector3(3.5, 4, 5));
  156. CHECK(animation->get_track_count() == 1);
  157. CHECK(!animation->track_is_compressed(0));
  158. CHECK(Vector3(animation->track_get_key_value(0, 0)).is_equal_approx(Vector3(0, 1, 2)));
  159. CHECK(Vector3(animation->track_get_key_value(0, 1)).is_equal_approx(Vector3(3.5, 4, 5)));
  160. Vector3 r_interpolation;
  161. CHECK(animation->try_scale_track_interpolate(0, -0.2, &r_interpolation) == OK);
  162. CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
  163. CHECK(animation->try_scale_track_interpolate(0, 0.0, &r_interpolation) == OK);
  164. CHECK(r_interpolation.is_equal_approx(Vector3(0, 1, 2)));
  165. CHECK(animation->try_scale_track_interpolate(0, 0.2, &r_interpolation) == OK);
  166. CHECK(r_interpolation.is_equal_approx(Vector3(1.4, 2.2, 3.2)));
  167. CHECK(animation->try_scale_track_interpolate(0, 0.4, &r_interpolation) == OK);
  168. CHECK(r_interpolation.is_equal_approx(Vector3(2.8, 3.4, 4.4)));
  169. CHECK(animation->try_scale_track_interpolate(0, 0.5, &r_interpolation) == OK);
  170. CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
  171. CHECK(animation->try_scale_track_interpolate(0, 0.6, &r_interpolation) == OK);
  172. CHECK(r_interpolation.is_equal_approx(Vector3(3.5, 4, 5)));
  173. // 3D scale tracks always use linear interpolation for performance reasons.
  174. CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(1.0));
  175. CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(1.0));
  176. // This is a 3D scale track, so the methods below should return errors.
  177. ERR_PRINT_OFF;
  178. CHECK(animation->value_track_interpolate(0, 0.0).is_null());
  179. CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  180. CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  181. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
  182. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  183. ERR_PRINT_ON;
  184. }
  185. TEST_CASE("[Animation] Create blend shape track") {
  186. Ref<Animation> animation = memnew(Animation);
  187. const int track_index = animation->add_track(Animation::TYPE_BLEND_SHAPE);
  188. animation->track_set_path(track_index, NodePath("Enemy:scale"));
  189. // Negative values for blend shapes should work as expected.
  190. animation->blend_shape_track_insert_key(track_index, 0.0, -1.0);
  191. animation->blend_shape_track_insert_key(track_index, 0.5, 1.0);
  192. CHECK(animation->get_track_count() == 1);
  193. CHECK(!animation->track_is_compressed(0));
  194. float r_blend = 0.0f;
  195. CHECK(animation->blend_shape_track_get_key(0, 0, &r_blend) == OK);
  196. CHECK(r_blend == doctest::Approx(-1.0f));
  197. CHECK(animation->blend_shape_track_get_key(0, 1, &r_blend) == OK);
  198. CHECK(r_blend == doctest::Approx(1.0f));
  199. CHECK(animation->try_blend_shape_track_interpolate(0, -0.2, &r_blend) == OK);
  200. CHECK(r_blend == doctest::Approx(-1.0f));
  201. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, &r_blend) == OK);
  202. CHECK(r_blend == doctest::Approx(-1.0f));
  203. CHECK(animation->try_blend_shape_track_interpolate(0, 0.2, &r_blend) == OK);
  204. CHECK(r_blend == doctest::Approx(-0.2f));
  205. CHECK(animation->try_blend_shape_track_interpolate(0, 0.4, &r_blend) == OK);
  206. CHECK(r_blend == doctest::Approx(0.6f));
  207. CHECK(animation->try_blend_shape_track_interpolate(0, 0.5, &r_blend) == OK);
  208. CHECK(r_blend == doctest::Approx(1.0f));
  209. CHECK(animation->try_blend_shape_track_interpolate(0, 0.6, &r_blend) == OK);
  210. CHECK(r_blend == doctest::Approx(1.0f));
  211. // Blend shape tracks always use linear interpolation for performance reasons.
  212. CHECK(animation->track_get_key_transition(0, 0) == doctest::Approx(real_t(1.0)));
  213. CHECK(animation->track_get_key_transition(0, 1) == doctest::Approx(real_t(1.0)));
  214. // This is a blend shape track, so the methods below should return errors.
  215. ERR_PRINT_OFF;
  216. CHECK(animation->value_track_interpolate(0, 0.0).is_null());
  217. CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  218. CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  219. CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  220. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(0.0));
  221. ERR_PRINT_ON;
  222. }
  223. TEST_CASE("[Animation] Create Bezier track") {
  224. Ref<Animation> animation = memnew(Animation);
  225. const int track_index = animation->add_track(Animation::TYPE_BEZIER);
  226. animation->track_set_path(track_index, NodePath("Enemy:scale"));
  227. animation->bezier_track_insert_key(track_index, 0.0, -1.0, Vector2(-1, -1), Vector2(1, 1));
  228. animation->bezier_track_insert_key(track_index, 0.5, 1.0, Vector2(0, 1), Vector2(1, 0.5));
  229. CHECK(animation->get_track_count() == 1);
  230. CHECK(!animation->track_is_compressed(0));
  231. CHECK(animation->bezier_track_get_key_value(0, 0) == doctest::Approx(real_t(-1.0)));
  232. CHECK(animation->bezier_track_get_key_value(0, 1) == doctest::Approx(real_t(1.0)));
  233. CHECK(animation->bezier_track_interpolate(0, -0.2) == doctest::Approx(real_t(-1.0)));
  234. CHECK(animation->bezier_track_interpolate(0, 0.0) == doctest::Approx(real_t(-1.0)));
  235. CHECK(animation->bezier_track_interpolate(0, 0.2) == doctest::Approx(real_t(-0.76057207584381)));
  236. CHECK(animation->bezier_track_interpolate(0, 0.4) == doctest::Approx(real_t(-0.39975279569626)));
  237. CHECK(animation->bezier_track_interpolate(0, 0.5) == doctest::Approx(real_t(1.0)));
  238. CHECK(animation->bezier_track_interpolate(0, 0.6) == doctest::Approx(real_t(1.0)));
  239. // This is a bezier track, so the methods below should return errors.
  240. ERR_PRINT_OFF;
  241. CHECK(animation->value_track_interpolate(0, 0.0).is_null());
  242. CHECK(animation->try_position_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  243. CHECK(animation->try_rotation_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  244. CHECK(animation->try_scale_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  245. CHECK(animation->try_blend_shape_track_interpolate(0, 0.0, nullptr) == ERR_INVALID_PARAMETER);
  246. ERR_PRINT_ON;
  247. }
  248. } // namespace TestAnimation
  249. #endif // TEST_ANIMATION_H