AnimationClip.js 7.1 KB

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