AnimationAction.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. import { WrapAroundEnding, ZeroCurvatureEnding, ZeroSlopeEnding, LoopPingPong, LoopOnce, LoopRepeat } from '../constants.js';
  2. /**
  3. *
  4. * Action provided by AnimationMixer for scheduling clip playback on specific
  5. * objects.
  6. *
  7. * @author Ben Houston / http://clara.io/
  8. * @author David Sarno / http://lighthaus.us/
  9. * @author tschw
  10. *
  11. */
  12. function AnimationAction( mixer, clip, localRoot, isAdditive = false ) {
  13. this._mixer = mixer;
  14. this._clip = clip;
  15. this._localRoot = localRoot || null;
  16. this.isAdditive = isAdditive;
  17. var tracks = clip.tracks,
  18. nTracks = tracks.length,
  19. interpolants = new Array( nTracks );
  20. var interpolantSettings = {
  21. endingStart: ZeroCurvatureEnding,
  22. endingEnd: ZeroCurvatureEnding
  23. };
  24. for ( var i = 0; i !== nTracks; ++ i ) {
  25. var interpolant = tracks[ i ].createInterpolant( null );
  26. interpolants[ i ] = interpolant;
  27. interpolant.settings = interpolantSettings;
  28. }
  29. this._interpolantSettings = interpolantSettings;
  30. this._interpolants = interpolants; // bound by the mixer
  31. // inside: PropertyMixer (managed by the mixer)
  32. this._propertyBindings = new Array( nTracks );
  33. this._cacheIndex = null; // for the memory manager
  34. this._byClipCacheIndex = null; // for the memory manager
  35. this._timeScaleInterpolant = null;
  36. this._weightInterpolant = null;
  37. this.loop = LoopRepeat;
  38. this._loopCount = - 1;
  39. // global mixer time when the action is to be started
  40. // it's set back to 'null' upon start of the action
  41. this._startTime = null;
  42. // scaled local time of the action
  43. // gets clamped or wrapped to 0..clip.duration according to loop
  44. this.time = 0;
  45. this.timeScale = 1;
  46. this._effectiveTimeScale = 1;
  47. this.weight = 1;
  48. this._effectiveWeight = 1;
  49. this.repetitions = Infinity; // no. of repetitions when looping
  50. this.paused = false; // true -> zero effective time scale
  51. this.enabled = true; // false -> zero effective weight
  52. this.clampWhenFinished = false;// keep feeding the last frame?
  53. this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate
  54. this.zeroSlopeAtEnd = true;// clips for start, loop and end
  55. }
  56. Object.assign( AnimationAction.prototype, {
  57. // State & Scheduling
  58. play: function () {
  59. this._mixer._activateAction( this );
  60. return this;
  61. },
  62. stop: function () {
  63. this._mixer._deactivateAction( this );
  64. return this.reset();
  65. },
  66. reset: function () {
  67. this.paused = false;
  68. this.enabled = true;
  69. this.time = 0; // restart clip
  70. this._loopCount = - 1;// forget previous loops
  71. this._startTime = null;// forget scheduling
  72. return this.stopFading().stopWarping();
  73. },
  74. isRunning: function () {
  75. return this.enabled && ! this.paused && this.timeScale !== 0 &&
  76. this._startTime === null && this._mixer._isActiveAction( this );
  77. },
  78. // return true when play has been called
  79. isScheduled: function () {
  80. return this._mixer._isActiveAction( this );
  81. },
  82. startAt: function ( time ) {
  83. this._startTime = time;
  84. return this;
  85. },
  86. setLoop: function ( mode, repetitions ) {
  87. this.loop = mode;
  88. this.repetitions = repetitions;
  89. return this;
  90. },
  91. // Weight
  92. // set the weight stopping any scheduled fading
  93. // although .enabled = false yields an effective weight of zero, this
  94. // method does *not* change .enabled, because it would be confusing
  95. setEffectiveWeight: function ( weight ) {
  96. this.weight = weight;
  97. // note: same logic as when updated at runtime
  98. this._effectiveWeight = this.enabled ? weight : 0;
  99. return this.stopFading();
  100. },
  101. // return the weight considering fading and .enabled
  102. getEffectiveWeight: function () {
  103. return this._effectiveWeight;
  104. },
  105. fadeIn: function ( duration ) {
  106. return this._scheduleFading( duration, 0, 1 );
  107. },
  108. fadeOut: function ( duration ) {
  109. return this._scheduleFading( duration, 1, 0 );
  110. },
  111. crossFadeFrom: function ( fadeOutAction, duration, warp ) {
  112. fadeOutAction.fadeOut( duration );
  113. this.fadeIn( duration );
  114. if ( warp ) {
  115. var fadeInDuration = this._clip.duration,
  116. fadeOutDuration = fadeOutAction._clip.duration,
  117. startEndRatio = fadeOutDuration / fadeInDuration,
  118. endStartRatio = fadeInDuration / fadeOutDuration;
  119. fadeOutAction.warp( 1.0, startEndRatio, duration );
  120. this.warp( endStartRatio, 1.0, duration );
  121. }
  122. return this;
  123. },
  124. crossFadeTo: function ( fadeInAction, duration, warp ) {
  125. return fadeInAction.crossFadeFrom( this, duration, warp );
  126. },
  127. stopFading: function () {
  128. var weightInterpolant = this._weightInterpolant;
  129. if ( weightInterpolant !== null ) {
  130. this._weightInterpolant = null;
  131. this._mixer._takeBackControlInterpolant( weightInterpolant );
  132. }
  133. return this;
  134. },
  135. // Time Scale Control
  136. // set the time scale stopping any scheduled warping
  137. // although .paused = true yields an effective time scale of zero, this
  138. // method does *not* change .paused, because it would be confusing
  139. setEffectiveTimeScale: function ( timeScale ) {
  140. this.timeScale = timeScale;
  141. this._effectiveTimeScale = this.paused ? 0 : timeScale;
  142. return this.stopWarping();
  143. },
  144. // return the time scale considering warping and .paused
  145. getEffectiveTimeScale: function () {
  146. return this._effectiveTimeScale;
  147. },
  148. setDuration: function ( duration ) {
  149. this.timeScale = this._clip.duration / duration;
  150. return this.stopWarping();
  151. },
  152. syncWith: function ( action ) {
  153. this.time = action.time;
  154. this.timeScale = action.timeScale;
  155. return this.stopWarping();
  156. },
  157. halt: function ( duration ) {
  158. return this.warp( this._effectiveTimeScale, 0, duration );
  159. },
  160. warp: function ( startTimeScale, endTimeScale, duration ) {
  161. var mixer = this._mixer, now = mixer.time,
  162. interpolant = this._timeScaleInterpolant,
  163. timeScale = this.timeScale;
  164. if ( interpolant === null ) {
  165. interpolant = mixer._lendControlInterpolant();
  166. this._timeScaleInterpolant = interpolant;
  167. }
  168. var times = interpolant.parameterPositions,
  169. values = interpolant.sampleValues;
  170. times[ 0 ] = now;
  171. times[ 1 ] = now + duration;
  172. values[ 0 ] = startTimeScale / timeScale;
  173. values[ 1 ] = endTimeScale / timeScale;
  174. return this;
  175. },
  176. stopWarping: function () {
  177. var timeScaleInterpolant = this._timeScaleInterpolant;
  178. if ( timeScaleInterpolant !== null ) {
  179. this._timeScaleInterpolant = null;
  180. this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
  181. }
  182. return this;
  183. },
  184. // Object Accessors
  185. getMixer: function () {
  186. return this._mixer;
  187. },
  188. getClip: function () {
  189. return this._clip;
  190. },
  191. getRoot: function () {
  192. return this._localRoot || this._mixer._root;
  193. },
  194. // Interna
  195. _update: function ( time, deltaTime, timeDirection, accuIndex ) {
  196. // called by the mixer
  197. if ( ! this.enabled ) {
  198. // call ._updateWeight() to update ._effectiveWeight
  199. this._updateWeight( time );
  200. return;
  201. }
  202. var startTime = this._startTime;
  203. if ( startTime !== null ) {
  204. // check for scheduled start of action
  205. var timeRunning = ( time - startTime ) * timeDirection;
  206. if ( timeRunning < 0 || timeDirection === 0 ) {
  207. return; // yet to come / don't decide when delta = 0
  208. }
  209. // start
  210. this._startTime = null; // unschedule
  211. deltaTime = timeDirection * timeRunning;
  212. }
  213. // apply time scale and advance time
  214. deltaTime *= this._updateTimeScale( time );
  215. var clipTime = this._updateTime( deltaTime );
  216. // note: _updateTime may disable the action resulting in
  217. // an effective weight of 0
  218. var weight = this._updateWeight( time );
  219. if ( weight > 0 ) {
  220. var interpolants = this._interpolants;
  221. var propertyMixers = this._propertyBindings;
  222. for ( var j = 0, m = interpolants.length; j !== m; ++ j ) {
  223. interpolants[ j ].evaluate( clipTime );
  224. propertyMixers[ j ].accumulate( accuIndex, weight, this.isAdditive );
  225. }
  226. }
  227. },
  228. _updateWeight: function ( time ) {
  229. var weight = 0;
  230. if ( this.enabled ) {
  231. weight = this.weight;
  232. var interpolant = this._weightInterpolant;
  233. if ( interpolant !== null ) {
  234. var interpolantValue = interpolant.evaluate( time )[ 0 ];
  235. weight *= interpolantValue;
  236. if ( time > interpolant.parameterPositions[ 1 ] ) {
  237. this.stopFading();
  238. if ( interpolantValue === 0 ) {
  239. // faded out, disable
  240. this.enabled = false;
  241. }
  242. }
  243. }
  244. }
  245. this._effectiveWeight = weight;
  246. return weight;
  247. },
  248. _updateTimeScale: function ( time ) {
  249. var timeScale = 0;
  250. if ( ! this.paused ) {
  251. timeScale = this.timeScale;
  252. var interpolant = this._timeScaleInterpolant;
  253. if ( interpolant !== null ) {
  254. var interpolantValue = interpolant.evaluate( time )[ 0 ];
  255. timeScale *= interpolantValue;
  256. if ( time > interpolant.parameterPositions[ 1 ] ) {
  257. this.stopWarping();
  258. if ( timeScale === 0 ) {
  259. // motion has halted, pause
  260. this.paused = true;
  261. } else {
  262. // warp done - apply final time scale
  263. this.timeScale = timeScale;
  264. }
  265. }
  266. }
  267. }
  268. this._effectiveTimeScale = timeScale;
  269. return timeScale;
  270. },
  271. _updateTime: function ( deltaTime ) {
  272. var time = this.time + deltaTime;
  273. var duration = this._clip.duration;
  274. var loop = this.loop;
  275. var loopCount = this._loopCount;
  276. var pingPong = ( loop === LoopPingPong );
  277. if ( deltaTime === 0 ) {
  278. if ( loopCount === - 1 ) return time;
  279. return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
  280. }
  281. if ( loop === LoopOnce ) {
  282. if ( loopCount === - 1 ) {
  283. // just started
  284. this._loopCount = 0;
  285. this._setEndings( true, true, false );
  286. }
  287. handle_stop: {
  288. if ( time >= duration ) {
  289. time = duration;
  290. } else if ( time < 0 ) {
  291. time = 0;
  292. } else {
  293. this.time = time;
  294. break handle_stop;
  295. }
  296. if ( this.clampWhenFinished ) this.paused = true;
  297. else this.enabled = false;
  298. this.time = time;
  299. this._mixer.dispatchEvent( {
  300. type: 'finished', action: this,
  301. direction: deltaTime < 0 ? - 1 : 1
  302. } );
  303. }
  304. } else { // repetitive Repeat or PingPong
  305. if ( loopCount === - 1 ) {
  306. // just started
  307. if ( deltaTime >= 0 ) {
  308. loopCount = 0;
  309. this._setEndings( true, this.repetitions === 0, pingPong );
  310. } else {
  311. // when looping in reverse direction, the initial
  312. // transition through zero counts as a repetition,
  313. // so leave loopCount at -1
  314. this._setEndings( this.repetitions === 0, true, pingPong );
  315. }
  316. }
  317. if ( time >= duration || time < 0 ) {
  318. // wrap around
  319. var loopDelta = Math.floor( time / duration ); // signed
  320. time -= duration * loopDelta;
  321. loopCount += Math.abs( loopDelta );
  322. var pending = this.repetitions - loopCount;
  323. if ( pending <= 0 ) {
  324. // have to stop (switch state, clamp time, fire event)
  325. if ( this.clampWhenFinished ) this.paused = true;
  326. else this.enabled = false;
  327. time = deltaTime > 0 ? duration : 0;
  328. this.time = time;
  329. this._mixer.dispatchEvent( {
  330. type: 'finished', action: this,
  331. direction: deltaTime > 0 ? 1 : - 1
  332. } );
  333. } else {
  334. // keep running
  335. if ( pending === 1 ) {
  336. // entering the last round
  337. var atStart = deltaTime < 0;
  338. this._setEndings( atStart, ! atStart, pingPong );
  339. } else {
  340. this._setEndings( false, false, pingPong );
  341. }
  342. this._loopCount = loopCount;
  343. this.time = time;
  344. this._mixer.dispatchEvent( {
  345. type: 'loop', action: this, loopDelta: loopDelta
  346. } );
  347. }
  348. } else {
  349. this.time = time;
  350. }
  351. if ( pingPong && ( loopCount & 1 ) === 1 ) {
  352. // invert time for the "pong round"
  353. return duration - time;
  354. }
  355. }
  356. return time;
  357. },
  358. _setEndings: function ( atStart, atEnd, pingPong ) {
  359. var settings = this._interpolantSettings;
  360. if ( pingPong ) {
  361. settings.endingStart = ZeroSlopeEnding;
  362. settings.endingEnd = ZeroSlopeEnding;
  363. } else {
  364. // assuming for LoopOnce atStart == atEnd == true
  365. if ( atStart ) {
  366. settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
  367. } else {
  368. settings.endingStart = WrapAroundEnding;
  369. }
  370. if ( atEnd ) {
  371. settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
  372. } else {
  373. settings.endingEnd = WrapAroundEnding;
  374. }
  375. }
  376. },
  377. _scheduleFading: function ( duration, weightNow, weightThen ) {
  378. var mixer = this._mixer, now = mixer.time,
  379. interpolant = this._weightInterpolant;
  380. if ( interpolant === null ) {
  381. interpolant = mixer._lendControlInterpolant();
  382. this._weightInterpolant = interpolant;
  383. }
  384. var times = interpolant.parameterPositions,
  385. values = interpolant.sampleValues;
  386. times[ 0 ] = now;
  387. values[ 0 ] = weightNow;
  388. times[ 1 ] = now + duration;
  389. values[ 1 ] = weightThen;
  390. return this;
  391. }
  392. } );
  393. export { AnimationAction };