AnimationMixer.js 16 KB

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