AnimationObjectGroup.js 9.0 KB

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