AnimationAction.js 12 KB

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