AnimationClip.js 7.0 KB

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