AnimationClip.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { VectorKeyframeTrack } from './tracks/VectorKeyframeTrack';
  2. import { QuaternionKeyframeTrack } from './tracks/QuaternionKeyframeTrack';
  3. import { NumberKeyframeTrack } from './tracks/NumberKeyframeTrack';
  4. import { AnimationUtils } from './AnimationUtils';
  5. import { KeyframeTrack } from './KeyframeTrack';
  6. import { _Math } from '../math/Math';
  7. /**
  8. *
  9. * Reusable set of Tracks that represent an animation.
  10. *
  11. * @author Ben Houston / http://clara.io/
  12. * @author David Sarno / http://lighthaus.us/
  13. */
  14. function AnimationClip( name, duration, tracks ) {
  15. this.name = name;
  16. this.tracks = tracks;
  17. this.duration = ( duration !== undefined ) ? duration : -1;
  18. this.uuid = _Math.generateUUID();
  19. // this means it should figure out its duration by scanning the tracks
  20. if ( this.duration < 0 ) {
  21. this.resetDuration();
  22. }
  23. // maybe only do these on demand, as doing them here could potentially slow down loading
  24. // but leaving these here during development as this ensures a lot of testing of these functions
  25. this.trim();
  26. this.optimize();
  27. };
  28. AnimationClip.prototype = {
  29. constructor: AnimationClip,
  30. resetDuration: function() {
  31. var tracks = this.tracks,
  32. duration = 0;
  33. for ( var i = 0, n = tracks.length; i !== n; ++ i ) {
  34. var track = this.tracks[ i ];
  35. duration = Math.max(
  36. duration, track.times[ track.times.length - 1 ] );
  37. }
  38. this.duration = duration;
  39. },
  40. trim: function() {
  41. for ( var i = 0; i < this.tracks.length; i ++ ) {
  42. this.tracks[ i ].trim( 0, this.duration );
  43. }
  44. return this;
  45. },
  46. optimize: function() {
  47. for ( var i = 0; i < this.tracks.length; i ++ ) {
  48. this.tracks[ i ].optimize();
  49. }
  50. return this;
  51. }
  52. };
  53. // Static methods:
  54. Object.assign( AnimationClip, {
  55. parse: function( json ) {
  56. var tracks = [],
  57. jsonTracks = json.tracks,
  58. frameTime = 1.0 / ( json.fps || 1.0 );
  59. for ( var i = 0, n = jsonTracks.length; i !== n; ++ i ) {
  60. tracks.push( KeyframeTrack.parse( jsonTracks[ i ] ).scale( frameTime ) );
  61. }
  62. return new AnimationClip( json.name, json.duration, tracks );
  63. },
  64. toJSON: function( clip ) {
  65. var tracks = [],
  66. clipTracks = clip.tracks;
  67. var json = {
  68. 'name': clip.name,
  69. 'duration': clip.duration,
  70. 'tracks': tracks
  71. };
  72. for ( var i = 0, n = clipTracks.length; i !== n; ++ i ) {
  73. tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
  74. }
  75. return json;
  76. },
  77. CreateFromMorphTargetSequence: function( name, morphTargetSequence, fps, noLoop ) {
  78. var numMorphTargets = morphTargetSequence.length;
  79. var tracks = [];
  80. for ( var i = 0; i < numMorphTargets; i ++ ) {
  81. var times = [];
  82. var values = [];
  83. times.push(
  84. ( i + numMorphTargets - 1 ) % numMorphTargets,
  85. i,
  86. ( i + 1 ) % numMorphTargets );
  87. values.push( 0, 1, 0 );
  88. var order = AnimationUtils.getKeyframeOrder( times );
  89. times = AnimationUtils.sortedArray( times, 1, order );
  90. values = AnimationUtils.sortedArray( values, 1, order );
  91. // if there is a key at the first frame, duplicate it as the
  92. // last frame as well for perfect loop.
  93. if ( ! noLoop && times[ 0 ] === 0 ) {
  94. times.push( numMorphTargets );
  95. values.push( values[ 0 ] );
  96. }
  97. tracks.push(
  98. new NumberKeyframeTrack(
  99. '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
  100. times, values
  101. ).scale( 1.0 / fps ) );
  102. }
  103. return new AnimationClip( name, -1, tracks );
  104. },
  105. findByName: function( objectOrClipArray, name ) {
  106. var clipArray = objectOrClipArray;
  107. if ( ! Array.isArray( objectOrClipArray ) ) {
  108. var o = objectOrClipArray;
  109. clipArray = o.geometry && o.geometry.animations || o.animations;
  110. }
  111. for ( var i = 0; i < clipArray.length; i ++ ) {
  112. if ( clipArray[ i ].name === name ) {
  113. return clipArray[ i ];
  114. }
  115. }
  116. return null;
  117. },
  118. CreateClipsFromMorphTargetSequences: function( morphTargets, fps, noLoop ) {
  119. var animationToMorphTargets = {};
  120. // tested with https://regex101.com/ on trick sequences
  121. // such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  122. var pattern = /^([\w-]*?)([\d]+)$/;
  123. // sort morph target names into animation groups based
  124. // patterns like Walk_001, Walk_002, Run_001, Run_002
  125. for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
  126. var morphTarget = morphTargets[ i ];
  127. var parts = morphTarget.name.match( pattern );
  128. if ( parts && parts.length > 1 ) {
  129. var name = parts[ 1 ];
  130. var animationMorphTargets = animationToMorphTargets[ name ];
  131. if ( ! animationMorphTargets ) {
  132. animationToMorphTargets[ name ] = animationMorphTargets = [];
  133. }
  134. animationMorphTargets.push( morphTarget );
  135. }
  136. }
  137. var clips = [];
  138. for ( var name in animationToMorphTargets ) {
  139. clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
  140. }
  141. return clips;
  142. },
  143. // parse the animation.hierarchy format
  144. parseAnimation: function( animation, bones, nodeName ) {
  145. if ( ! animation ) {
  146. console.error( " no animation in JSONLoader data" );
  147. return null;
  148. }
  149. var addNonemptyTrack = function(
  150. trackType, trackName, animationKeys, propertyName, destTracks ) {
  151. // only return track if there are actually keys.
  152. if ( animationKeys.length !== 0 ) {
  153. var times = [];
  154. var values = [];
  155. AnimationUtils.flattenJSON(
  156. animationKeys, times, values, propertyName );
  157. // empty keys are filtered out, so check again
  158. if ( times.length !== 0 ) {
  159. destTracks.push( new trackType( trackName, times, values ) );
  160. }
  161. }
  162. };
  163. var tracks = [];
  164. var clipName = animation.name || 'default';
  165. // automatic length determination in AnimationClip.
  166. var duration = animation.length || -1;
  167. var fps = animation.fps || 30;
  168. var hierarchyTracks = animation.hierarchy || [];
  169. for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
  170. var animationKeys = hierarchyTracks[ h ].keys;
  171. // skip empty tracks
  172. if ( ! animationKeys || animationKeys.length === 0 ) continue;
  173. // process morph targets in a way exactly compatible
  174. // with AnimationHandler.init( animation )
  175. if ( animationKeys[0].morphTargets ) {
  176. // figure out all morph targets used in this track
  177. var morphTargetNames = {};
  178. for ( var k = 0; k < animationKeys.length; k ++ ) {
  179. if ( animationKeys[k].morphTargets ) {
  180. for ( var m = 0; m < animationKeys[k].morphTargets.length; m ++ ) {
  181. morphTargetNames[ animationKeys[k].morphTargets[m] ] = -1;
  182. }
  183. }
  184. }
  185. // create a track for each morph target with all zero
  186. // morphTargetInfluences except for the keys in which
  187. // the morphTarget is named.
  188. for ( var morphTargetName in morphTargetNames ) {
  189. var times = [];
  190. var values = [];
  191. for ( var m = 0;
  192. m !== animationKeys[k].morphTargets.length; ++ m ) {
  193. var animationKey = animationKeys[k];
  194. times.push( animationKey.time );
  195. values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
  196. }
  197. tracks.push( new NumberKeyframeTrack(
  198. '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
  199. }
  200. duration = morphTargetNames.length * ( fps || 1.0 );
  201. } else {
  202. // ...assume skeletal animation
  203. var boneName = '.bones[' + bones[ h ].name + ']';
  204. addNonemptyTrack(
  205. VectorKeyframeTrack, boneName + '.position',
  206. animationKeys, 'pos', tracks );
  207. addNonemptyTrack(
  208. QuaternionKeyframeTrack, boneName + '.quaternion',
  209. animationKeys, 'rot', tracks );
  210. addNonemptyTrack(
  211. VectorKeyframeTrack, boneName + '.scale',
  212. animationKeys, 'scl', tracks );
  213. }
  214. }
  215. if ( tracks.length === 0 ) {
  216. return null;
  217. }
  218. var clip = new AnimationClip( clipName, duration, tracks );
  219. return clip;
  220. }
  221. } );
  222. export { AnimationClip };