AnimationClip.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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;
  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.keys[ track.keys.length - 1 ].time );
  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. this.results = [];
  24. };
  25. THREE.AnimationClip.prototype = {
  26. constructor: THREE.AnimationClip,
  27. getAt: function( clipTime ) {
  28. clipTime = Math.max( 0, Math.min( clipTime, this.duration ) );
  29. for ( var i = 0; i < this.tracks.length; i ++ ) {
  30. var track = this.tracks[ i ];
  31. this.results[ i ] = track.getAt( clipTime );
  32. }
  33. return this.results;
  34. },
  35. trim: function() {
  36. for ( var i = 0; i < this.tracks.length; i ++ ) {
  37. this.tracks[ i ].trim( 0, this.duration );
  38. }
  39. return this;
  40. },
  41. optimize: function() {
  42. for ( var i = 0; i < this.tracks.length; i ++ ) {
  43. this.tracks[ i ].optimize();
  44. }
  45. return this;
  46. }
  47. };
  48. THREE.AnimationClip.CreateFromMorphTargetSequence = function( name, morphTargetSequence, fps ) {
  49. var numMorphTargets = morphTargetSequence.length;
  50. var tracks = [];
  51. for ( var i = 0; i < numMorphTargets; i ++ ) {
  52. var keys = [];
  53. keys.push( { time: ( i + numMorphTargets - 1 ) % numMorphTargets, value: 0 } );
  54. keys.push( { time: i, value: 1 } );
  55. keys.push( { time: ( i + 1 ) % numMorphTargets, value: 0 } );
  56. keys.sort( THREE.KeyframeTrack.keyComparer );
  57. // if there is a key at the first frame, duplicate it as the last frame as well for perfect loop.
  58. if ( keys[0].time === 0 ) {
  59. keys.push( {
  60. time: numMorphTargets,
  61. value: keys[0].value
  62. });
  63. }
  64. tracks.push( new THREE.NumberKeyframeTrack( '.morphTargetInfluences[' + morphTargetSequence[i].name + ']', keys ).scale( 1.0 / fps ) );
  65. }
  66. return new THREE.AnimationClip( name, -1, tracks );
  67. };
  68. THREE.AnimationClip.findByName = function( clipArray, name ) {
  69. for ( var i = 0; i < clipArray.length; i ++ ) {
  70. if ( clipArray[i].name === name ) {
  71. return clipArray[i];
  72. }
  73. }
  74. return null;
  75. };
  76. THREE.AnimationClip.CreateClipsFromMorphTargetSequences = function( morphTargets, fps ) {
  77. var animationToMorphTargets = {};
  78. // tested with https://regex101.com/ on trick sequences such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  79. var pattern = /^([\w-]*?)([\d]+)$/;
  80. // sort morph target names into animation groups based patterns like Walk_001, Walk_002, Run_001, Run_002
  81. for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
  82. var morphTarget = morphTargets[ i ];
  83. var parts = morphTarget.name.match( pattern );
  84. if ( parts && parts.length > 1 ) {
  85. var name = parts[ 1 ];
  86. var animationMorphTargets = animationToMorphTargets[ name ];
  87. if ( ! animationMorphTargets ) {
  88. animationToMorphTargets[ name ] = animationMorphTargets = [];
  89. }
  90. animationMorphTargets.push( morphTarget );
  91. }
  92. }
  93. var clips = [];
  94. for ( var name in animationToMorphTargets ) {
  95. clips.push( THREE.AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps ) );
  96. }
  97. return clips;
  98. };
  99. // parse the standard JSON format for clips
  100. THREE.AnimationClip.parse = function( json ) {
  101. var tracks = [];
  102. for ( var i = 0; i < json.tracks.length; i ++ ) {
  103. tracks.push( THREE.KeyframeTrack.parse( json.tracks[i] ).scale( 1.0 / json.fps ) );
  104. }
  105. return new THREE.AnimationClip( json.name, json.duration, tracks );
  106. };
  107. // parse the animation.hierarchy format
  108. THREE.AnimationClip.parseAnimation = function( animation, bones, nodeName ) {
  109. if ( ! animation ) {
  110. console.error( " no animation in JSONLoader data" );
  111. return null;
  112. }
  113. var convertTrack = function( trackName, animationKeys, propertyName, trackType, animationKeyToValueFunc ) {
  114. var keys = [];
  115. for ( var k = 0; k < animationKeys.length; k ++ ) {
  116. var animationKey = animationKeys[k];
  117. if ( animationKey[propertyName] !== undefined ) {
  118. keys.push( { time: animationKey.time, value: animationKeyToValueFunc( animationKey ) } );
  119. }
  120. }
  121. // only return track if there are actually keys.
  122. if ( keys.length > 0 ) {
  123. return new trackType( trackName, keys );
  124. }
  125. return null;
  126. };
  127. var tracks = [];
  128. var clipName = animation.name || 'default';
  129. var duration = animation.length || -1; // automatic length determination in AnimationClip.
  130. var fps = animation.fps || 30;
  131. var hierarchyTracks = animation.hierarchy || [];
  132. for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
  133. var animationKeys = hierarchyTracks[ h ].keys;
  134. // skip empty tracks
  135. if ( ! animationKeys || animationKeys.length == 0 ) {
  136. continue;
  137. }
  138. // process morph targets in a way exactly compatible with AnimationHandler.init( animation )
  139. if ( animationKeys[0].morphTargets ) {
  140. // figure out all morph targets used in this track
  141. var morphTargetNames = {};
  142. for ( var k = 0; k < animationKeys.length; k ++ ) {
  143. if ( animationKeys[k].morphTargets ) {
  144. for ( var m = 0; m < animationKeys[k].morphTargets.length; m ++ ) {
  145. morphTargetNames[ animationKeys[k].morphTargets[m] ] = -1;
  146. }
  147. }
  148. }
  149. // create a track for each morph target with all zero morphTargetInfluences except for the keys in which the morphTarget is named.
  150. for ( var morphTargetName in morphTargetNames ) {
  151. var keys = [];
  152. for ( var m = 0; m < animationKeys[k].morphTargets.length; m ++ ) {
  153. var animationKey = animationKeys[k];
  154. keys.push( {
  155. time: animationKey.time,
  156. value: (( animationKey.morphTarget === morphTargetName ) ? 1 : 0 )
  157. });
  158. }
  159. tracks.push( new THREE.NumberKeyframeTrack( nodeName + '.morphTargetInfluence[' + morphTargetName + ']', keys ) );
  160. }
  161. duration = morphTargetNames.length * ( fps || 1.0 );
  162. } else {
  163. var boneName = nodeName + '.bones[' + bones[ h ].name + ']';
  164. // track contains positions...
  165. var positionTrack = convertTrack( boneName + '.position', animationKeys, 'pos', THREE.VectorKeyframeTrack, function( animationKey ) {
  166. return new THREE.Vector3().fromArray( animationKey.pos )
  167. } );
  168. if ( positionTrack ) tracks.push( positionTrack );
  169. // track contains quaternions...
  170. var quaternionTrack = convertTrack( boneName + '.quaternion', animationKeys, 'rot', THREE.QuaternionKeyframeTrack, function( animationKey ) {
  171. if ( animationKey.rot.slerp ) {
  172. return animationKey.rot.clone();
  173. } else {
  174. return new THREE.Quaternion().fromArray( animationKey.rot );
  175. }
  176. } );
  177. if ( quaternionTrack ) tracks.push( quaternionTrack );
  178. // track contains quaternions...
  179. var scaleTrack = convertTrack( boneName + '.scale', animationKeys, 'scl', THREE.VectorKeyframeTrack, function( animationKey ) {
  180. return new THREE.Vector3().fromArray( animationKey.scl )
  181. } );
  182. if ( scaleTrack ) tracks.push( scaleTrack );
  183. }
  184. }
  185. if ( tracks.length === 0 ) {
  186. return null;
  187. }
  188. var clip = new THREE.AnimationClip( clipName, duration, tracks );
  189. return clip;
  190. };