test_animation.h 16 KB

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