1
0

AnimationObjectGroup.js 9.0 KB

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