AnimationObjectGroup.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. import { PropertyBinding } from './PropertyBinding.js';
  2. import { _Math } from '../math/Math.js';
  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( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
  81. }
  82. } else if ( index < nCachedObjects ) {
  83. knownObject = objects[ index ];
  84. // move existing object to the ACTIVE region
  85. var firstActiveIndex = -- nCachedObjects,
  86. lastCachedObject = objects[ firstActiveIndex ];
  87. indicesByUUID[ lastCachedObject.uuid ] = index;
  88. objects[ index ] = lastCachedObject;
  89. indicesByUUID[ uuid ] = firstActiveIndex;
  90. objects[ firstActiveIndex ] = object;
  91. // accounting is done, now do the same for all bindings
  92. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  93. var bindingsForPath = bindings[ j ],
  94. lastCached = bindingsForPath[ firstActiveIndex ],
  95. binding = bindingsForPath[ index ];
  96. bindingsForPath[ index ] = lastCached;
  97. if ( binding === undefined ) {
  98. // since we do not bother to create new bindings
  99. // for objects that are cached, the binding may
  100. // or may not exist
  101. binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
  102. }
  103. bindingsForPath[ firstActiveIndex ] = binding;
  104. }
  105. } else if ( objects[ index ] !== knownObject ) {
  106. console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
  107. 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
  108. } // else the object is already where we want it to be
  109. } // for arguments
  110. this.nCachedObjects_ = nCachedObjects;
  111. },
  112. remove: function ( var_args ) {
  113. var objects = this._objects,
  114. nCachedObjects = this.nCachedObjects_,
  115. indicesByUUID = this._indicesByUUID,
  116. bindings = this._bindings,
  117. nBindings = bindings.length;
  118. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  119. var object = arguments[ i ],
  120. uuid = object.uuid,
  121. index = indicesByUUID[ uuid ];
  122. if ( index !== undefined && index >= nCachedObjects ) {
  123. // move existing object into the CACHED region
  124. var lastCachedIndex = nCachedObjects ++,
  125. firstActiveObject = objects[ lastCachedIndex ];
  126. indicesByUUID[ firstActiveObject.uuid ] = index;
  127. objects[ index ] = firstActiveObject;
  128. indicesByUUID[ uuid ] = lastCachedIndex;
  129. objects[ lastCachedIndex ] = object;
  130. // accounting is done, now do the same for all bindings
  131. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  132. var bindingsForPath = bindings[ j ],
  133. firstActive = bindingsForPath[ lastCachedIndex ],
  134. binding = bindingsForPath[ index ];
  135. bindingsForPath[ index ] = firstActive;
  136. bindingsForPath[ lastCachedIndex ] = binding;
  137. }
  138. }
  139. } // for arguments
  140. this.nCachedObjects_ = nCachedObjects;
  141. },
  142. // remove & forget
  143. uncache: function ( var_args ) {
  144. var objects = this._objects,
  145. nObjects = objects.length,
  146. nCachedObjects = this.nCachedObjects_,
  147. indicesByUUID = this._indicesByUUID,
  148. bindings = this._bindings,
  149. nBindings = bindings.length;
  150. for ( var i = 0, n = arguments.length; i !== n; ++ i ) {
  151. var object = arguments[ i ],
  152. uuid = object.uuid,
  153. index = indicesByUUID[ uuid ];
  154. if ( index !== undefined ) {
  155. delete indicesByUUID[ uuid ];
  156. if ( index < nCachedObjects ) {
  157. // object is cached, shrink the CACHED region
  158. var firstActiveIndex = -- nCachedObjects,
  159. lastCachedObject = objects[ firstActiveIndex ],
  160. lastIndex = -- nObjects,
  161. lastObject = objects[ lastIndex ];
  162. // last cached object takes this object's place
  163. indicesByUUID[ lastCachedObject.uuid ] = index;
  164. objects[ index ] = lastCachedObject;
  165. // last object goes to the activated slot and pop
  166. indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
  167. objects[ firstActiveIndex ] = lastObject;
  168. objects.pop();
  169. // accounting is done, now do the same for all bindings
  170. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  171. var bindingsForPath = bindings[ j ],
  172. lastCached = bindingsForPath[ firstActiveIndex ],
  173. last = bindingsForPath[ lastIndex ];
  174. bindingsForPath[ index ] = lastCached;
  175. bindingsForPath[ firstActiveIndex ] = last;
  176. bindingsForPath.pop();
  177. }
  178. } else {
  179. // object is active, just swap with the last and pop
  180. var lastIndex = -- nObjects,
  181. lastObject = objects[ lastIndex ];
  182. indicesByUUID[ lastObject.uuid ] = index;
  183. objects[ index ] = lastObject;
  184. objects.pop();
  185. // accounting is done, now do the same for all bindings
  186. for ( var j = 0, m = nBindings; j !== m; ++ j ) {
  187. var bindingsForPath = bindings[ j ];
  188. bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
  189. bindingsForPath.pop();
  190. }
  191. } // cached or active
  192. } // if object is known
  193. } // for arguments
  194. this.nCachedObjects_ = nCachedObjects;
  195. },
  196. // Internal interface used by befriended PropertyBinding.Composite:
  197. subscribe_: function ( path, parsedPath ) {
  198. // returns an array of bindings for the given path that is changed
  199. // according to the contained objects in the group
  200. var indicesByPath = this._bindingsIndicesByPath,
  201. index = indicesByPath[ path ],
  202. bindings = this._bindings;
  203. if ( index !== undefined ) return bindings[ index ];
  204. var paths = this._paths,
  205. parsedPaths = this._parsedPaths,
  206. objects = this._objects,
  207. nObjects = objects.length,
  208. nCachedObjects = this.nCachedObjects_,
  209. bindingsForPath = new Array( nObjects );
  210. index = bindings.length;
  211. indicesByPath[ path ] = index;
  212. paths.push( path );
  213. parsedPaths.push( parsedPath );
  214. bindings.push( bindingsForPath );
  215. for ( var i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
  216. var object = objects[ i ];
  217. bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
  218. }
  219. return bindingsForPath;
  220. },
  221. unsubscribe_: function ( path ) {
  222. // tells the group to forget about a property path and no longer
  223. // update the array previously obtained with 'subscribe_'
  224. var indicesByPath = this._bindingsIndicesByPath,
  225. index = indicesByPath[ path ];
  226. if ( index !== undefined ) {
  227. var paths = this._paths,
  228. parsedPaths = this._parsedPaths,
  229. bindings = this._bindings,
  230. lastBindingsIndex = bindings.length - 1,
  231. lastBindings = bindings[ lastBindingsIndex ],
  232. lastBindingsPath = path[ lastBindingsIndex ];
  233. indicesByPath[ lastBindingsPath ] = index;
  234. bindings[ index ] = lastBindings;
  235. bindings.pop();
  236. parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
  237. parsedPaths.pop();
  238. paths[ index ] = paths[ lastBindingsIndex ];
  239. paths.pop();
  240. }
  241. }
  242. } );
  243. export { AnimationObjectGroup };