AnimationObjectGroup.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { PropertyBinding } from './PropertyBinding';
  2. import { _Math } from '../math/Math';
  3. /**
  4. *
  5. * A group of objects that receives a shared animation state.
  6. *
  7. * Usage:
  8. *
  9. * - Add objects you would otherwise pass as 'root' to the
  10. * constructor or the .clipAction method of AnimationMixer.
  11. *
  12. * - Instead pass this object as 'root'.
  13. *
  14. * - You can also add and remove objects later when the mixer
  15. * is running.
  16. *
  17. * Note:
  18. *
  19. * Objects of this class appear as one object to the mixer,
  20. * so cache control of the individual objects must be done
  21. * on the group.
  22. *
  23. * Limitation:
  24. *
  25. * - The animated properties must be compatible among the
  26. * all objects in the group.
  27. *
  28. * - A single property can either be controlled through a
  29. * target group or directly, but not both.
  30. *
  31. * @author tschw
  32. */
  33. function AnimationObjectGroup( var_args ) {
  34. this.uuid = _Math.generateUUID();
  35. // cached objects followed by the active ones
  36. this._objects = Array.prototype.slice.call( arguments );
  37. this.nCachedObjects_ = 0; // threshold
  38. // note: read by PropertyBinding.Composite
  39. var indices = {};
  40. this._indicesByUUID = indices; // for bookkeeping
  41. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  42. indices[ arguments[ i ].uuid ] = i;
  43. }
  44. this._paths = []; // inside: string
  45. this._parsedPaths = []; // inside: { we don't care, here }
  46. this._bindings = []; // inside: Array< PropertyBinding >
  47. this._bindingsIndicesByPath = {}; // inside: indices in these arrays
  48. var scope = this;
  49. this.stats = {
  50. objects: {
  51. get total() { return scope._objects.length; },
  52. get inUse() { return this.total - scope.nCachedObjects_; }
  53. },
  54. get bindingsPerObject() { return scope._bindings.length; }
  55. };
  56. }
  57. Object.assign( AnimationObjectGroup.prototype, {
  58. isAnimationObjectGroup: true,
  59. add: function( var_args ) {
  60. var objects = this._objects,
  61. nObjects = objects.length,
  62. nCachedObjects = this.nCachedObjects_,
  63. indicesByUUID = this._indicesByUUID,
  64. paths = this._paths,
  65. parsedPaths = this._parsedPaths,
  66. bindings = this._bindings,
  67. nBindings = bindings.length;
  68. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  69. var object = arguments[ i ],
  70. uuid = object.uuid,
  71. index = indicesByUUID[ uuid ],
  72. knownObject = undefined;
  73. if ( index === undefined ) {
  74. // unknown object -> add it to the ACTIVE region
  75. index = nObjects ++;
  76. indicesByUUID[ uuid ] = index;
  77. objects.push( object );
  78. // accounting is done, now do the same for all bindings
  79. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  80. bindings[ j ].push(
  81. new PropertyBinding(
  82. object, paths[ j ], parsedPaths[ j ] ) );
  83. }
  84. } else if ( index < nCachedObjects ) {
  85. knownObject = objects[ index ];
  86. // move existing object to the ACTIVE region
  87. var firstActiveIndex = -- nCachedObjects,
  88. lastCachedObject = objects[ firstActiveIndex ];
  89. indicesByUUID[ lastCachedObject.uuid ] = index;
  90. objects[ index ] = lastCachedObject;
  91. indicesByUUID[ uuid ] = firstActiveIndex;
  92. objects[ firstActiveIndex ] = object;
  93. // accounting is done, now do the same for all bindings
  94. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  95. var bindingsForPath = bindings[ j ],
  96. lastCached = bindingsForPath[ firstActiveIndex ],
  97. binding = bindingsForPath[ index ];
  98. bindingsForPath[ index ] = lastCached;
  99. if ( binding === undefined ) {
  100. // since we do not bother to create new bindings
  101. // for objects that are cached, the binding may
  102. // or may not exist
  103. binding = new PropertyBinding(
  104. object, paths[ j ], parsedPaths[ j ] );
  105. }
  106. bindingsForPath[ firstActiveIndex ] = binding;
  107. }
  108. } else if ( objects[ index ] !== knownObject ) {
  109. console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
  110. 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
  111. } // else the object is already where we want it to be
  112. } // for arguments
  113. this.nCachedObjects_ = nCachedObjects;
  114. },
  115. remove: function( var_args ) {
  116. var objects = this._objects,
  117. nCachedObjects = this.nCachedObjects_,
  118. indicesByUUID = this._indicesByUUID,
  119. bindings = this._bindings,
  120. nBindings = bindings.length;
  121. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  122. var object = arguments[ i ],
  123. uuid = object.uuid,
  124. index = indicesByUUID[ uuid ];
  125. if ( index !== undefined && index >= nCachedObjects ) {
  126. // move existing object into the CACHED region
  127. var lastCachedIndex = nCachedObjects ++,
  128. firstActiveObject = objects[ lastCachedIndex ];
  129. indicesByUUID[ firstActiveObject.uuid ] = index;
  130. objects[ index ] = firstActiveObject;
  131. indicesByUUID[ uuid ] = lastCachedIndex;
  132. objects[ lastCachedIndex ] = object;
  133. // accounting is done, now do the same for all bindings
  134. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  135. var bindingsForPath = bindings[ j ],
  136. firstActive = bindingsForPath[ lastCachedIndex ],
  137. binding = bindingsForPath[ index ];
  138. bindingsForPath[ index ] = firstActive;
  139. bindingsForPath[ lastCachedIndex ] = binding;
  140. }
  141. }
  142. } // for arguments
  143. this.nCachedObjects_ = nCachedObjects;
  144. },
  145. // remove & forget
  146. uncache: function( var_args ) {
  147. var objects = this._objects,
  148. nObjects = objects.length,
  149. nCachedObjects = this.nCachedObjects_,
  150. indicesByUUID = this._indicesByUUID,
  151. bindings = this._bindings,
  152. nBindings = bindings.length;
  153. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  154. var object = arguments[ i ],
  155. uuid = object.uuid,
  156. index = indicesByUUID[ uuid ];
  157. if ( index !== undefined ) {
  158. delete indicesByUUID[ uuid ];
  159. if ( index < nCachedObjects ) {
  160. // object is cached, shrink the CACHED region
  161. var firstActiveIndex = -- nCachedObjects,
  162. lastCachedObject = objects[ firstActiveIndex ],
  163. lastIndex = -- nObjects,
  164. lastObject = objects[ lastIndex ];
  165. // last cached object takes this object's place
  166. indicesByUUID[ lastCachedObject.uuid ] = index;
  167. objects[ index ] = lastCachedObject;
  168. // last object goes to the activated slot and pop
  169. indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
  170. objects[ firstActiveIndex ] = lastObject;
  171. objects.pop();
  172. // accounting is done, now do the same for all bindings
  173. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  174. var bindingsForPath = bindings[ j ],
  175. lastCached = bindingsForPath[ firstActiveIndex ],
  176. last = bindingsForPath[ lastIndex ];
  177. bindingsForPath[ index ] = lastCached;
  178. bindingsForPath[ firstActiveIndex ] = last;
  179. bindingsForPath.pop();
  180. }
  181. } else {
  182. // object is active, just swap with the last and pop
  183. var lastIndex = -- nObjects,
  184. lastObject = objects[ lastIndex ];
  185. indicesByUUID[ lastObject.uuid ] = index;
  186. objects[ index ] = lastObject;
  187. objects.pop();
  188. // accounting is done, now do the same for all bindings
  189. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  190. var bindingsForPath = bindings[ j ];
  191. bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
  192. bindingsForPath.pop();
  193. }
  194. } // cached or active
  195. } // if object is known
  196. } // for arguments
  197. this.nCachedObjects_ = nCachedObjects;
  198. },
  199. // Internal interface used by befriended PropertyBinding.Composite:
  200. subscribe_: function ( path, parsedPath ) {
  201. // returns an array of bindings for the given path that is changed
  202. // according to the contained objects in the group
  203. var indicesByPath = this._bindingsIndicesByPath,
  204. index = indicesByPath[ path ],
  205. bindings = this._bindings;
  206. if ( index !== undefined ) return bindings[ index ];
  207. var paths = this._paths,
  208. parsedPaths = this._parsedPaths,
  209. objects = this._objects,
  210. nObjects = objects.length,
  211. nCachedObjects = this.nCachedObjects_,
  212. bindingsForPath = new Array( nObjects );
  213. index = bindings.length;
  214. indicesByPath[ path ] = index;
  215. paths.push( path );
  216. parsedPaths.push( parsedPath );
  217. bindings.push( bindingsForPath );
  218. for ( var i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
  219. var object = objects[ i ];
  220. bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
  221. }
  222. return bindingsForPath;
  223. },
  224. unsubscribe_: function ( path ) {
  225. // tells the group to forget about a property path and no longer
  226. // update the array previously obtained with 'subscribe_'
  227. var indicesByPath = this._bindingsIndicesByPath,
  228. index = indicesByPath[ path ];
  229. if ( index !== undefined ) {
  230. var paths = this._paths,
  231. parsedPaths = this._parsedPaths,
  232. bindings = this._bindings,
  233. lastBindingsIndex = bindings.length - 1,
  234. lastBindings = bindings[ lastBindingsIndex ],
  235. lastBindingsPath = path[ lastBindingsIndex ];
  236. indicesByPath[ lastBindingsPath ] = index;
  237. bindings[ index ] = lastBindings;
  238. bindings.pop();
  239. parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
  240. parsedPaths.pop();
  241. paths[ index ] = paths[ lastBindingsIndex ];
  242. paths.pop();
  243. }
  244. }
  245. } );
  246. export { AnimationObjectGroup };