AnimationAction.tests.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /**
  2. * @author TristanVALCKE / https://github.com/Itee
  3. */
  4. /* global QUnit */
  5. import { AnimationAction } from '../../../../src/animation/AnimationAction';
  6. import { AnimationMixer } from '../../../../src/animation/AnimationMixer';
  7. import { AnimationClip } from '../../../../src/animation/AnimationClip';
  8. import { NumberKeyframeTrack } from '../../../../src/animation/tracks/NumberKeyframeTrack';
  9. import { Object3D } from '../../../../src/core/Object3D';
  10. import { LoopOnce, LoopRepeat, LoopPingPong } from '../../../../src/constants';
  11. function createAnimation() {
  12. var root = new Object3D();
  13. var mixer = new AnimationMixer( root );
  14. var track = new NumberKeyframeTrack( ".rotation[x]", [ 0, 1000 ], [ 0, 360 ] );
  15. var clip = new AnimationClip( "clip1", 1000, [ track ] );
  16. var animationAction = mixer.clipAction( clip );
  17. return {
  18. root: root,
  19. mixer: mixer,
  20. track: track,
  21. clip: clip,
  22. animationAction: animationAction
  23. };
  24. }
  25. function createTwoAnimations() {
  26. var root = new Object3D();
  27. var mixer = new AnimationMixer( root );
  28. var track = new NumberKeyframeTrack( ".rotation[x]", [ 0, 1000 ], [ 0, 360 ] );
  29. var clip = new AnimationClip( "clip1", 1000, [ track ] );
  30. var animationAction = mixer.clipAction( clip );
  31. var track2 = new NumberKeyframeTrack( ".rotation[y]", [ 0, 1000 ], [ 0, 360 ] );
  32. var clip2 = new AnimationClip( "clip2", 1000, [ track ] );
  33. var animationAction2 = mixer.clipAction( clip2 );
  34. return {
  35. root: root,
  36. mixer: mixer,
  37. track: track,
  38. clip: clip,
  39. animationAction: animationAction,
  40. track2: track2,
  41. clip2: clip2,
  42. animationAction2: animationAction2
  43. };
  44. }
  45. export default QUnit.module( 'Animation', () => {
  46. QUnit.module( 'AnimationAction', () => {
  47. // INSTANCING
  48. QUnit.test( "Instancing", ( assert ) => {
  49. var mixer = new AnimationMixer();
  50. var clip = new AnimationClip( "nonname", - 1, [] );
  51. var animationAction = new AnimationAction( mixer, clip );
  52. assert.ok( animationAction, "animationAction instanciated" );
  53. } );
  54. // PUBLIC STUFF
  55. QUnit.test( "play", ( assert ) => {
  56. var { mixer, animationAction } = createAnimation();
  57. var animationAction2 = animationAction.play();
  58. assert.equal( animationAction, animationAction2, "AnimationAction.play can be chained." );
  59. var UserException = function () {
  60. this.message = "AnimationMixer must activate AnimationAction on play.";
  61. };
  62. mixer._activateAction = function ( action ) {
  63. if ( action === animationAction ) {
  64. throw new UserException();
  65. }
  66. };
  67. assert.throws( () => {
  68. animationAction.play();
  69. }, new UserException() );
  70. } );
  71. QUnit.test( "stop", ( assert ) => {
  72. var { mixer, animationAction } = createAnimation();
  73. var animationAction2 = animationAction.stop();
  74. assert.equal( animationAction, animationAction2, "AnimationAction.stop can be chained." );
  75. var UserException = function () {
  76. this.message = "AnimationMixer must deactivate AnimationAction on stop.";
  77. };
  78. mixer._deactivateAction = function ( action ) {
  79. if ( action === animationAction ) {
  80. throw new UserException();
  81. }
  82. };
  83. assert.throws( () => {
  84. animationAction.stop();
  85. }, new UserException() );
  86. } );
  87. QUnit.test( "reset", ( assert ) => {
  88. var { mixer, animationAction } = createAnimation();
  89. var animationAction2 = animationAction.stop();
  90. assert.equal( animationAction, animationAction2, "AnimationAction.reset can be chained." );
  91. assert.equal( animationAction2.paused, false, "AnimationAction.reset() sets paused false" );
  92. assert.equal( animationAction2.enabled, true, "AnimationAction.reset() sets enabled true" );
  93. assert.equal( animationAction2.time, 0, "AnimationAction.reset() resets time." );
  94. assert.equal( animationAction2._loopCount, - 1, "AnimationAction.reset() resets loopcount." );
  95. assert.equal( animationAction2._startTime, null, "AnimationAction.reset() removes starttime." );
  96. } );
  97. QUnit.test( "isRunning", ( assert ) => {
  98. var { mixer, animationAction } = createAnimation();
  99. assert.notOk( animationAction.isRunning(), "When an animation is just made, it is not running." );
  100. animationAction.play();
  101. assert.ok( animationAction.isRunning(), "When an animation is started, it is running." );
  102. animationAction.stop();
  103. assert.notOk( animationAction.isRunning(), "When an animation is stopped, it is not running." );
  104. animationAction.play();
  105. animationAction.paused = true;
  106. assert.notOk( animationAction.isRunning(), "When an animation is paused, it is not running." );
  107. animationAction.paused = false;
  108. animationAction.enabled = false;
  109. assert.notOk( animationAction.isRunning(), "When an animation is not enabled, it is not running." );
  110. animationAction.enabled = true;
  111. assert.ok( animationAction.isRunning(), "When an animation is enabled, it is running." );
  112. } );
  113. QUnit.test( "isScheduled", ( assert ) => {
  114. var { mixer, animationAction } = createAnimation();
  115. assert.notOk( animationAction.isScheduled(), "When an animation is just made, it is not scheduled." );
  116. animationAction.play();
  117. assert.ok( animationAction.isScheduled(), "When an animation is started, it is scheduled." );
  118. mixer.update( 1 );
  119. assert.ok( animationAction.isScheduled(), "When an animation is updated, it is scheduled." );
  120. animationAction.stop();
  121. assert.notOk( animationAction.isScheduled(), "When an animation is stopped, it isn't scheduled anymore." );
  122. } );
  123. QUnit.test( "startAt", ( assert ) => {
  124. var { mixer, animationAction } = createAnimation();
  125. animationAction.startAt( 2 );
  126. animationAction.play();
  127. assert.notOk( animationAction.isRunning(), "When an animation is started at a specific time, it is not running." );
  128. assert.ok( animationAction.isScheduled(), "When an animation is started at a specific time, it is scheduled." );
  129. mixer.update( 1 );
  130. assert.notOk( animationAction.isRunning(), "When an animation is started at a specific time and the interval is not passed, it is not running." );
  131. assert.ok( animationAction.isScheduled(), "When an animation is started at a specific time and the interval is not passed, it is scheduled." );
  132. mixer.update( 1 );
  133. assert.ok( animationAction.isRunning(), "When an animation is started at a specific time and the interval is passed, it is running." );
  134. assert.ok( animationAction.isScheduled(), "When an animation is started at a specific time and the interval is passed, it is scheduled." );
  135. animationAction.stop();
  136. assert.notOk( animationAction.isRunning(), "When an animation is stopped, it is not running." );
  137. assert.notOk( animationAction.isScheduled(), "When an animation is stopped, it is not scheduled." );
  138. } );
  139. QUnit.test( "setLoop LoopOnce", ( assert ) => {
  140. var { mixer, animationAction } = createAnimation();
  141. animationAction.setLoop( LoopOnce );
  142. animationAction.play();
  143. assert.ok( animationAction.isRunning(), "When an animation is started, it is running." );
  144. mixer.update( 500 );
  145. assert.ok( animationAction.isRunning(), "When an animation is in the first loop, it is running." );
  146. mixer.update( 500 );
  147. assert.notOk( animationAction.isRunning(), "When an animation is ended, it is not running." );
  148. mixer.update( 500 );
  149. assert.notOk( animationAction.isRunning(), "When an animation is ended, it is not running." );
  150. } );
  151. QUnit.test( "setLoop LoopRepeat", ( assert ) => {
  152. var { root, mixer, animationAction } = createAnimation();
  153. animationAction.setLoop( LoopRepeat, 3 );
  154. animationAction.play();
  155. assert.ok( animationAction.isRunning(), "When an animation is started, it is running." );
  156. mixer.update( 750 );
  157. assert.equal( root.rotation.x, 270, "When an animation is 3/4 in the first loop, it has changed to 3/4 when LoopRepeat." );
  158. assert.ok( animationAction.isRunning(), "When an animation is in the first loop, it is running." );
  159. mixer.update( 1000 );
  160. assert.equal( root.rotation.x, 270, "When an animation is 3/4 in the second loop, it has changed to 3/4 when LoopRepeat." );
  161. assert.ok( animationAction.isRunning(), "When an animation is in second loop when in looprepeat 3 times, it is running." );
  162. mixer.update( 1000 );
  163. assert.equal( root.rotation.x, 270, "When an animation is 3/4 in the third loop, it has changed to 3/4 when LoopRepeat." );
  164. assert.ok( animationAction.isRunning(), "When an animation is in third loop when in looprepeat 3 times, it is running." );
  165. mixer.update( 1000 );
  166. assert.equal( root.rotation.x, 0, "When an animation ended his third loop when in looprepeat 3 times, it stays on the end result." );
  167. assert.notOk( animationAction.isRunning(), "When an animation ended his third loop when in looprepeat 3 times, it stays not running anymore." );
  168. } );
  169. QUnit.test( "setLoop LoopPingPong", ( assert ) => {
  170. var { root, mixer, animationAction } = createAnimation();
  171. animationAction.setLoop( LoopPingPong, 3 );
  172. animationAction.play();
  173. assert.ok( animationAction.isRunning(), "When an animation is started, it is running." );
  174. mixer.update( 750 );
  175. assert.equal( root.rotation.x, 270, "When an animation is 3/4 in the first loop, it has changed to 3/4 when LoopPingPong." );
  176. assert.ok( animationAction.isRunning(), "When an animation is in the first loop, it is running." );
  177. mixer.update( 1000 );
  178. assert.equal( root.rotation.x, 90, "When an animation is 3/4 in the second loop, it has changed to 1/4 when LoopPingPong." );
  179. assert.ok( animationAction.isRunning(), "When an animation is in second loop when in looprepeat 3 times, it is running." );
  180. mixer.update( 1000 );
  181. assert.equal( root.rotation.x, 270, "When an animation is 3/4 in the third loop, it has changed to 3/4 when LoopPingPong." );
  182. assert.ok( animationAction.isRunning(), "When an animation is in third loop when in looprepeat 3 times, it is running." );
  183. mixer.update( 1000 );
  184. assert.equal( root.rotation.x, 0, "When an animation ended his fourth loop when in looprepeat 3 times, it stays on the end result." );
  185. assert.notOk( animationAction.isRunning(), "When an animation ended his fourth loop when in looprepeat 3 times, it stays not running anymore." );
  186. } );
  187. QUnit.test( "setEffectiveWeight", ( assert ) => {
  188. var { animationAction } = createAnimation();
  189. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation is created, EffectiveWeight is 1." );
  190. animationAction.setEffectiveWeight( 0.3 );
  191. assert.equal( animationAction.getEffectiveWeight(), 0.3, "When EffectiveWeight is set to 0.3 , EffectiveWeight is 0.3." );
  192. var { animationAction } = createAnimation();
  193. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation is created, EffectiveWeight is 1." );
  194. animationAction.enabled = false;
  195. animationAction.setEffectiveWeight( 0.3 );
  196. assert.equal( animationAction.getEffectiveWeight(), 0, "When EffectiveWeight is set to 0.3 when disabled , EffectiveWeight is 0." );
  197. var { root, mixer, animationAction } = createAnimation();
  198. animationAction.setEffectiveWeight( 0.5 );
  199. animationAction.play();
  200. mixer.update( 500 );
  201. assert.equal( root.rotation.x, 90, "When an animation has weight 0.5 and runs half through the animation, it has changed to 1/4." );
  202. mixer.update( 1000 );
  203. assert.equal( root.rotation.x, 90, "When an animation has weight 0.5 and runs one and half through the animation, it has changed to 1/4." );
  204. } );
  205. QUnit.test( "getEffectiveWeight", ( assert ) => {
  206. var { animationAction } = createAnimation();
  207. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation is created, EffectiveWeight is 1." );
  208. animationAction.setEffectiveWeight( 0.3 );
  209. assert.equal( animationAction.getEffectiveWeight(), 0.3, "When EffectiveWeight is set to 0.3 , EffectiveWeight is 0.3." );
  210. var { animationAction } = createAnimation();
  211. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation is created, EffectiveWeight is 1." );
  212. animationAction.enabled = false;
  213. animationAction.setEffectiveWeight( 0.3 );
  214. assert.equal( animationAction.getEffectiveWeight(), 0, "When EffectiveWeight is set to 0.3 when disabled , EffectiveWeight is 0." );
  215. } );
  216. QUnit.test( "fadeIn", ( assert ) => {
  217. var { mixer, animationAction } = createAnimation();
  218. animationAction.fadeIn( 1000 );
  219. animationAction.play();
  220. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation fadeIn is started, EffectiveWeight is 1." );
  221. mixer.update( 250 );
  222. assert.equal( animationAction.getEffectiveWeight(), 0.25, "When an animation fadeIn happened 1/4, EffectiveWeight is 0.25." );
  223. mixer.update( 250 );
  224. assert.equal( animationAction.getEffectiveWeight(), 0.5, "When an animation fadeIn is halfway , EffectiveWeight is 0.5." );
  225. mixer.update( 250 );
  226. assert.equal( animationAction.getEffectiveWeight(), 0.75, "When an animation fadeIn is halfway , EffectiveWeight is 0.75." );
  227. mixer.update( 500 );
  228. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation fadeIn is ended , EffectiveWeight is 1." );
  229. } );
  230. QUnit.test( "fadeOut", ( assert ) => {
  231. var { mixer, animationAction } = createAnimation();
  232. animationAction.fadeOut( 1000 );
  233. animationAction.play();
  234. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation fadeOut is started, EffectiveWeight is 1." );
  235. mixer.update( 250 );
  236. assert.equal( animationAction.getEffectiveWeight(), 0.75, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  237. mixer.update( 250 );
  238. assert.equal( animationAction.getEffectiveWeight(), 0.5, "When an animation fadeOut is halfway , EffectiveWeight is 0.5." );
  239. mixer.update( 250 );
  240. assert.equal( animationAction.getEffectiveWeight(), 0.25, "When an animation fadeOut is happened 3/4 , EffectiveWeight is 0.25." );
  241. mixer.update( 500 );
  242. assert.equal( animationAction.getEffectiveWeight(), 0, "When an animation fadeOut is ended , EffectiveWeight is 0." );
  243. } );
  244. QUnit.test( "crossFadeFrom", ( assert ) => {
  245. var { mixer, animationAction, animationAction2 } = createTwoAnimations();
  246. animationAction.crossFadeFrom( animationAction2, 1000, false );
  247. animationAction.play();
  248. animationAction2.play();
  249. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation crossFadeFrom is started, EffectiveWeight is 1." );
  250. assert.equal( animationAction2.getEffectiveWeight(), 1, "When an animation crossFadeFrom is started, EffectiveWeight is 1." );
  251. mixer.update( 250 );
  252. assert.equal( animationAction.getEffectiveWeight(), 0.25, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  253. assert.equal( animationAction2.getEffectiveWeight(), 0.75, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  254. mixer.update( 250 );
  255. assert.equal( animationAction.getEffectiveWeight(), 0.5, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  256. assert.equal( animationAction2.getEffectiveWeight(), 0.5, "When an animation fadeOut is halfway , EffectiveWeight is 0.5." );
  257. mixer.update( 250 );
  258. assert.equal( animationAction.getEffectiveWeight(), 0.75, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  259. assert.equal( animationAction2.getEffectiveWeight(), 0.25, "When an animation fadeOut is happened 3/4 , EffectiveWeight is 0.25." );
  260. mixer.update( 500 );
  261. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  262. assert.equal( animationAction2.getEffectiveWeight(), 0, "When an animation fadeOut is ended , EffectiveWeight is 0." );
  263. } );
  264. QUnit.test( "crossFadeTo", ( assert ) => {
  265. var { mixer, animationAction, animationAction2 } = createTwoAnimations();
  266. animationAction2.crossFadeTo( animationAction, 1000, false );
  267. animationAction.play();
  268. animationAction2.play();
  269. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation crossFadeFrom is started, EffectiveWeight is 1." );
  270. assert.equal( animationAction2.getEffectiveWeight(), 1, "When an animation crossFadeFrom is started, EffectiveWeight is 1." );
  271. mixer.update( 250 );
  272. assert.equal( animationAction.getEffectiveWeight(), 0.25, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  273. assert.equal( animationAction2.getEffectiveWeight(), 0.75, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  274. mixer.update( 250 );
  275. assert.equal( animationAction.getEffectiveWeight(), 0.5, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  276. assert.equal( animationAction2.getEffectiveWeight(), 0.5, "When an animation fadeOut is halfway , EffectiveWeight is 0.5." );
  277. mixer.update( 250 );
  278. assert.equal( animationAction.getEffectiveWeight(), 0.75, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  279. assert.equal( animationAction2.getEffectiveWeight(), 0.25, "When an animation fadeOut is happened 3/4 , EffectiveWeight is 0.25." );
  280. mixer.update( 500 );
  281. assert.equal( animationAction.getEffectiveWeight(), 1, "When an animation fadeOut happened 1/4, EffectiveWeight is 0.75." );
  282. assert.equal( animationAction2.getEffectiveWeight(), 0, "When an animation fadeOut is ended , EffectiveWeight is 0." );
  283. } );
  284. QUnit.todo( "stopFading", ( assert ) => {
  285. assert.ok( false, "everything's gonna be alright" );
  286. } );
  287. QUnit.todo( "setEffectiveTimeScale", ( assert ) => {
  288. assert.ok( false, "everything's gonna be alright" );
  289. } );
  290. QUnit.todo( "getEffectiveTimeScale", ( assert ) => {
  291. assert.ok( false, "everything's gonna be alright" );
  292. } );
  293. QUnit.todo( "setDuration", ( assert ) => {
  294. assert.ok( false, "everything's gonna be alright" );
  295. } );
  296. QUnit.todo( "syncWith", ( assert ) => {
  297. assert.ok( false, "everything's gonna be alright" );
  298. } );
  299. QUnit.todo( "halt", ( assert ) => {
  300. assert.ok( false, "everything's gonna be alright" );
  301. } );
  302. QUnit.todo( "warp", ( assert ) => {
  303. assert.ok( false, "everything's gonna be alright" );
  304. } );
  305. QUnit.todo( "stopWarping", ( assert ) => {
  306. assert.ok( false, "everything's gonna be alright" );
  307. } );
  308. QUnit.test( "getMixer", ( assert ) => {
  309. var { mixer, animationAction } = createAnimation();
  310. var mixer2 = animationAction.getMixer();
  311. assert.equal( mixer, mixer2, "mixer should be returned by getMixer." );
  312. } );
  313. QUnit.test( "getClip", ( assert ) => {
  314. var { clip, animationAction } = createAnimation();
  315. var clip2 = animationAction.getClip();
  316. assert.equal( clip, clip2, "clip should be returned by getClip." );
  317. } );
  318. QUnit.test( "getRoot", ( assert ) => {
  319. var { root, animationAction } = createAnimation();
  320. var root2 = animationAction.getRoot();
  321. assert.equal( root, root2, "root should be returned by getRoot." );
  322. } );
  323. } );
  324. } );
粤ICP备19079148号