AnimationMixer.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. import { AnimationAction } from './AnimationAction';
  2. import { EventDispatcher } from '../core/EventDispatcher';
  3. import { LinearInterpolant } from '../math/interpolants/LinearInterpolant';
  4. import { PropertyBinding } from './PropertyBinding';
  5. import { PropertyMixer } from './PropertyMixer';
  6. import { AnimationClip } from './AnimationClip';
  7. /**
  8. *
  9. * Player for AnimationClips.
  10. *
  11. *
  12. * @author Ben Houston / http://clara.io/
  13. * @author David Sarno / http://lighthaus.us/
  14. * @author tschw
  15. */
  16. function AnimationMixer( root ) {
  17. this._root = root;
  18. this._initMemoryManager();
  19. this._accuIndex = 0;
  20. this.time = 0;
  21. this.timeScale = 1.0;
  22. }
  23. Object.assign( AnimationMixer.prototype, EventDispatcher.prototype, {
  24. constructor: AnimationMixer,
  25. _bindAction: function ( action, prototypeAction ) {
  26. var root = action._localRoot || this._root,
  27. tracks = action._clip.tracks,
  28. nTracks = tracks.length,
  29. bindings = action._propertyBindings,
  30. interpolants = action._interpolants,
  31. rootUuid = root.uuid,
  32. bindingsByRoot = this._bindingsByRootAndName,
  33. bindingsByName = bindingsByRoot[ rootUuid ];
  34. if ( bindingsByName === undefined ) {
  35. bindingsByName = {};
  36. bindingsByRoot[ rootUuid ] = bindingsByName;
  37. }
  38. for ( var i = 0; i !== nTracks; ++ i ) {
  39. var track = tracks[ i ],
  40. trackName = track.name,
  41. binding = bindingsByName[ trackName ];
  42. if ( binding !== undefined ) {
  43. bindings[ i ] = binding;
  44. } else {
  45. binding = bindings[ i ];
  46. if ( binding !== undefined ) {
  47. // existing binding, make sure the cache knows
  48. if ( binding._cacheIndex === null ) {
  49. ++ binding.referenceCount;
  50. this._addInactiveBinding( binding, rootUuid, trackName );
  51. }
  52. continue;
  53. }
  54. var path = prototypeAction && prototypeAction.
  55. _propertyBindings[ i ].binding.parsedPath;
  56. binding = new PropertyMixer(
  57. PropertyBinding.create( root, trackName, path ),
  58. track.ValueTypeName, track.getValueSize() );
  59. ++ binding.referenceCount;
  60. this._addInactiveBinding( binding, rootUuid, trackName );
  61. bindings[ i ] = binding;
  62. }
  63. interpolants[ i ].resultBuffer = binding.buffer;
  64. }
  65. },
  66. _activateAction: function ( action ) {
  67. if ( ! this._isActiveAction( action ) ) {
  68. if ( action._cacheIndex === null ) {
  69. // this action has been forgotten by the cache, but the user
  70. // appears to be still using it -> rebind
  71. var rootUuid = ( action._localRoot || this._root ).uuid,
  72. clipUuid = action._clip.uuid,
  73. actionsForClip = this._actionsByClip[ clipUuid ];
  74. this._bindAction( action,
  75. actionsForClip && actionsForClip.knownActions[ 0 ] );
  76. this._addInactiveAction( action, clipUuid, rootUuid );
  77. }
  78. var bindings = action._propertyBindings;
  79. // increment reference counts / sort out state
  80. for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
  81. var binding = bindings[ i ];
  82. if ( binding.useCount ++ === 0 ) {
  83. this._lendBinding( binding );
  84. binding.saveOriginalState();
  85. }
  86. }
  87. this._lendAction( action );
  88. }
  89. },
  90. _deactivateAction: function ( action ) {
  91. if ( this._isActiveAction( action ) ) {
  92. var bindings = action._propertyBindings;
  93. // decrement reference counts / sort out state
  94. for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
  95. var binding = bindings[ i ];
  96. if ( -- binding.useCount === 0 ) {
  97. binding.restoreOriginalState();
  98. this._takeBackBinding( binding );
  99. }
  100. }
  101. this._takeBackAction( action );
  102. }
  103. },
  104. // Memory manager
  105. _initMemoryManager: function () {
  106. this._actions = []; // 'nActiveActions' followed by inactive ones
  107. this._nActiveActions = 0;
  108. this._actionsByClip = {};
  109. // inside:
  110. // {
  111. // knownActions: Array< AnimationAction > - used as prototypes
  112. // actionByRoot: AnimationAction - lookup
  113. // }
  114. this._bindings = []; // 'nActiveBindings' followed by inactive ones
  115. this._nActiveBindings = 0;
  116. this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
  117. this._controlInterpolants = []; // same game as above
  118. this._nActiveControlInterpolants = 0;
  119. var scope = this;
  120. this.stats = {
  121. actions: {
  122. get total() { return scope._actions.length; },
  123. get inUse() { return scope._nActiveActions; }
  124. },
  125. bindings: {
  126. get total() { return scope._bindings.length; },
  127. get inUse() { return scope._nActiveBindings; }
  128. },
  129. controlInterpolants: {
  130. get total() { return scope._controlInterpolants.length; },
  131. get inUse() { return scope._nActiveControlInterpolants; }
  132. }
  133. };
  134. },
  135. // Memory management for AnimationAction objects
  136. _isActiveAction: function ( action ) {
  137. var index = action._cacheIndex;
  138. return index !== null && index < this._nActiveActions;
  139. },
  140. _addInactiveAction: function ( action, clipUuid, rootUuid ) {
  141. var actions = this._actions,
  142. actionsByClip = this._actionsByClip,
  143. actionsForClip = actionsByClip[ clipUuid ];
  144. if ( actionsForClip === undefined ) {
  145. actionsForClip = {
  146. knownActions: [ action ],
  147. actionByRoot: {}
  148. };
  149. action._byClipCacheIndex = 0;
  150. actionsByClip[ clipUuid ] = actionsForClip;
  151. } else {
  152. var knownActions = actionsForClip.knownActions;
  153. action._byClipCacheIndex = knownActions.length;
  154. knownActions.push( action );
  155. }
  156. action._cacheIndex = actions.length;
  157. actions.push( action );
  158. actionsForClip.actionByRoot[ rootUuid ] = action;
  159. },
  160. _removeInactiveAction: function ( action ) {
  161. var actions = this._actions,
  162. lastInactiveAction = actions[ actions.length - 1 ],
  163. cacheIndex = action._cacheIndex;
  164. lastInactiveAction._cacheIndex = cacheIndex;
  165. actions[ cacheIndex ] = lastInactiveAction;
  166. actions.pop();
  167. action._cacheIndex = null;
  168. var clipUuid = action._clip.uuid,
  169. actionsByClip = this._actionsByClip,
  170. actionsForClip = actionsByClip[ clipUuid ],
  171. knownActionsForClip = actionsForClip.knownActions,
  172. lastKnownAction =
  173. knownActionsForClip[ knownActionsForClip.length - 1 ],
  174. byClipCacheIndex = action._byClipCacheIndex;
  175. lastKnownAction._byClipCacheIndex = byClipCacheIndex;
  176. knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
  177. knownActionsForClip.pop();
  178. action._byClipCacheIndex = null;
  179. var actionByRoot = actionsForClip.actionByRoot,
  180. rootUuid = ( actions._localRoot || this._root ).uuid;
  181. delete actionByRoot[ rootUuid ];
  182. if ( knownActionsForClip.length === 0 ) {
  183. delete actionsByClip[ clipUuid ];
  184. }
  185. this._removeInactiveBindingsForAction( action );
  186. },
  187. _removeInactiveBindingsForAction: function ( action ) {
  188. var bindings = action._propertyBindings;
  189. for ( var i = 0, n = bindings.length; i !== n; ++ i ) {
  190. var binding = bindings[ i ];
  191. if ( -- binding.referenceCount === 0 ) {
  192. this._removeInactiveBinding( binding );
  193. }
  194. }
  195. },
  196. _lendAction: function ( action ) {
  197. // [ active actions | inactive actions ]
  198. // [ active actions >| inactive actions ]
  199. // s a
  200. // <-swap->
  201. // a s
  202. var actions = this._actions,
  203. prevIndex = action._cacheIndex,
  204. lastActiveIndex = this._nActiveActions ++,
  205. firstInactiveAction = actions[ lastActiveIndex ];
  206. action._cacheIndex = lastActiveIndex;
  207. actions[ lastActiveIndex ] = action;
  208. firstInactiveAction._cacheIndex = prevIndex;
  209. actions[ prevIndex ] = firstInactiveAction;
  210. },
  211. _takeBackAction: function ( action ) {
  212. // [ active actions | inactive actions ]
  213. // [ active actions |< inactive actions ]
  214. // a s
  215. // <-swap->
  216. // s a
  217. var actions = this._actions,
  218. prevIndex = action._cacheIndex,
  219. firstInactiveIndex = -- this._nActiveActions,
  220. lastActiveAction = actions[ firstInactiveIndex ];
  221. action._cacheIndex = firstInactiveIndex;
  222. actions[ firstInactiveIndex ] = action;
  223. lastActiveAction._cacheIndex = prevIndex;
  224. actions[ prevIndex ] = lastActiveAction;
  225. },
  226. // Memory management for PropertyMixer objects
  227. _addInactiveBinding: function ( binding, rootUuid, trackName ) {
  228. var bindingsByRoot = this._bindingsByRootAndName,
  229. bindingByName = bindingsByRoot[ rootUuid ],
  230. bindings = this._bindings;
  231. if ( bindingByName === undefined ) {
  232. bindingByName = {};
  233. bindingsByRoot[ rootUuid ] = bindingByName;
  234. }
  235. bindingByName[ trackName ] = binding;
  236. binding._cacheIndex = bindings.length;
  237. bindings.push( binding );
  238. },
  239. _removeInactiveBinding: function ( binding ) {
  240. var bindings = this._bindings,
  241. propBinding = binding.binding,
  242. rootUuid = propBinding.rootNode.uuid,
  243. trackName = propBinding.path,
  244. bindingsByRoot = this._bindingsByRootAndName,
  245. bindingByName = bindingsByRoot[ rootUuid ],
  246. lastInactiveBinding = bindings[ bindings.length - 1 ],
  247. cacheIndex = binding._cacheIndex;
  248. lastInactiveBinding._cacheIndex = cacheIndex;
  249. bindings[ cacheIndex ] = lastInactiveBinding;
  250. bindings.pop();
  251. delete bindingByName[ trackName ];
  252. remove_empty_map: {
  253. for ( var _ in bindingByName ) break remove_empty_map;
  254. delete bindingsByRoot[ rootUuid ];
  255. }
  256. },
  257. _lendBinding: function ( binding ) {
  258. var bindings = this._bindings,
  259. prevIndex = binding._cacheIndex,
  260. lastActiveIndex = this._nActiveBindings ++,
  261. firstInactiveBinding = bindings[ lastActiveIndex ];
  262. binding._cacheIndex = lastActiveIndex;
  263. bindings[ lastActiveIndex ] = binding;
  264. firstInactiveBinding._cacheIndex = prevIndex;
  265. bindings[ prevIndex ] = firstInactiveBinding;
  266. },
  267. _takeBackBinding: function ( binding ) {
  268. var bindings = this._bindings,
  269. prevIndex = binding._cacheIndex,
  270. firstInactiveIndex = -- this._nActiveBindings,
  271. lastActiveBinding = bindings[ firstInactiveIndex ];
  272. binding._cacheIndex = firstInactiveIndex;
  273. bindings[ firstInactiveIndex ] = binding;
  274. lastActiveBinding._cacheIndex = prevIndex;
  275. bindings[ prevIndex ] = lastActiveBinding;
  276. },
  277. // Memory management of Interpolants for weight and time scale
  278. _lendControlInterpolant: function () {
  279. var interpolants = this._controlInterpolants,
  280. lastActiveIndex = this._nActiveControlInterpolants ++,
  281. interpolant = interpolants[ lastActiveIndex ];
  282. if ( interpolant === undefined ) {
  283. interpolant = new LinearInterpolant(
  284. new Float32Array( 2 ), new Float32Array( 2 ),
  285. 1, this._controlInterpolantsResultBuffer );
  286. interpolant.__cacheIndex = lastActiveIndex;
  287. interpolants[ lastActiveIndex ] = interpolant;
  288. }
  289. return interpolant;
  290. },
  291. _takeBackControlInterpolant: function ( interpolant ) {
  292. var interpolants = this._controlInterpolants,
  293. prevIndex = interpolant.__cacheIndex,
  294. firstInactiveIndex = -- this._nActiveControlInterpolants,
  295. lastActiveInterpolant = interpolants[ firstInactiveIndex ];
  296. interpolant.__cacheIndex = firstInactiveIndex;
  297. interpolants[ firstInactiveIndex ] = interpolant;
  298. lastActiveInterpolant.__cacheIndex = prevIndex;
  299. interpolants[ prevIndex ] = lastActiveInterpolant;
  300. },
  301. _controlInterpolantsResultBuffer: new Float32Array( 1 ),
  302. // return an action for a clip optionally using a custom root target
  303. // object (this method allocates a lot of dynamic memory in case a
  304. // previously unknown clip/root combination is specified)
  305. clipAction: function ( clip, optionalRoot ) {
  306. var root = optionalRoot || this._root,
  307. rootUuid = root.uuid,
  308. clipObject = typeof clip === 'string' ?
  309. AnimationClip.findByName( root, clip ) : clip,
  310. clipUuid = clipObject !== null ? clipObject.uuid : clip,
  311. actionsForClip = this._actionsByClip[ clipUuid ],
  312. prototypeAction = null;
  313. if ( actionsForClip !== undefined ) {
  314. var existingAction =
  315. actionsForClip.actionByRoot[ rootUuid ];
  316. if ( existingAction !== undefined ) {
  317. return existingAction;
  318. }
  319. // we know the clip, so we don't have to parse all
  320. // the bindings again but can just copy
  321. prototypeAction = actionsForClip.knownActions[ 0 ];
  322. // also, take the clip from the prototype action
  323. if ( clipObject === null )
  324. clipObject = prototypeAction._clip;
  325. }
  326. // clip must be known when specified via string
  327. if ( clipObject === null ) return null;
  328. // allocate all resources required to run it
  329. var newAction = new AnimationAction( this, clipObject, optionalRoot );
  330. this._bindAction( newAction, prototypeAction );
  331. // and make the action known to the memory manager
  332. this._addInactiveAction( newAction, clipUuid, rootUuid );
  333. return newAction;
  334. },
  335. // get an existing action
  336. existingAction: function ( clip, optionalRoot ) {
  337. var root = optionalRoot || this._root,
  338. rootUuid = root.uuid,
  339. clipObject = typeof clip === 'string' ?
  340. AnimationClip.findByName( root, clip ) : clip,
  341. clipUuid = clipObject ? clipObject.uuid : clip,
  342. actionsForClip = this._actionsByClip[ clipUuid ];
  343. if ( actionsForClip !== undefined ) {
  344. return actionsForClip.actionByRoot[ rootUuid ] || null;
  345. }
  346. return null;
  347. },
  348. // deactivates all previously scheduled actions
  349. stopAllAction: function () {
  350. var actions = this._actions,
  351. nActions = this._nActiveActions,
  352. bindings = this._bindings,
  353. nBindings = this._nActiveBindings;
  354. this._nActiveActions = 0;
  355. this._nActiveBindings = 0;
  356. for ( var i = 0; i !== nActions; ++ i ) {
  357. actions[ i ].reset();
  358. }
  359. for ( var i = 0; i !== nBindings; ++ i ) {
  360. bindings[ i ].useCount = 0;
  361. }
  362. return this;
  363. },
  364. // advance the time and update apply the animation
  365. update: function ( deltaTime ) {
  366. deltaTime *= this.timeScale;
  367. var actions = this._actions,
  368. nActions = this._nActiveActions,
  369. time = this.time += deltaTime,
  370. timeDirection = Math.sign( deltaTime ),
  371. accuIndex = this._accuIndex ^= 1;
  372. // run active actions
  373. for ( var i = 0; i !== nActions; ++ i ) {
  374. var action = actions[ i ];
  375. if ( action.enabled ) {
  376. action._update( time, deltaTime, timeDirection, accuIndex );
  377. }
  378. }
  379. // update scene graph
  380. var bindings = this._bindings,
  381. nBindings = this._nActiveBindings;
  382. for ( var i = 0; i !== nBindings; ++ i ) {
  383. bindings[ i ].apply( accuIndex );
  384. }
  385. return this;
  386. },
  387. // return this mixer's root target object
  388. getRoot: function () {
  389. return this._root;
  390. },
  391. // free all resources specific to a particular clip
  392. uncacheClip: function ( clip ) {
  393. var actions = this._actions,
  394. clipUuid = clip.uuid,
  395. actionsByClip = this._actionsByClip,
  396. actionsForClip = actionsByClip[ clipUuid ];
  397. if ( actionsForClip !== undefined ) {
  398. // note: just calling _removeInactiveAction would mess up the
  399. // iteration state and also require updating the state we can
  400. // just throw away
  401. var actionsToRemove = actionsForClip.knownActions;
  402. for ( var i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
  403. var action = actionsToRemove[ i ];
  404. this._deactivateAction( action );
  405. var cacheIndex = action._cacheIndex,
  406. lastInactiveAction = actions[ actions.length - 1 ];
  407. action._cacheIndex = null;
  408. action._byClipCacheIndex = null;
  409. lastInactiveAction._cacheIndex = cacheIndex;
  410. actions[ cacheIndex ] = lastInactiveAction;
  411. actions.pop();
  412. this._removeInactiveBindingsForAction( action );
  413. }
  414. delete actionsByClip[ clipUuid ];
  415. }
  416. },
  417. // free all resources specific to a particular root target object
  418. uncacheRoot: function ( root ) {
  419. var rootUuid = root.uuid,
  420. actionsByClip = this._actionsByClip;
  421. for ( var clipUuid in actionsByClip ) {
  422. var actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
  423. action = actionByRoot[ rootUuid ];
  424. if ( action !== undefined ) {
  425. this._deactivateAction( action );
  426. this._removeInactiveAction( action );
  427. }
  428. }
  429. var bindingsByRoot = this._bindingsByRootAndName,
  430. bindingByName = bindingsByRoot[ rootUuid ];
  431. if ( bindingByName !== undefined ) {
  432. for ( var trackName in bindingByName ) {
  433. var binding = bindingByName[ trackName ];
  434. binding.restoreOriginalState();
  435. this._removeInactiveBinding( binding );
  436. }
  437. }
  438. },
  439. // remove a targeted clip from the cache
  440. uncacheAction: function ( clip, optionalRoot ) {
  441. var action = this.existingAction( clip, optionalRoot );
  442. if ( action !== null ) {
  443. this._deactivateAction( action );
  444. this._removeInactiveAction( action );
  445. }
  446. }
  447. } );
  448. export { AnimationMixer };