AnimationClip.js 7.7 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;
  10. this.tracks = tracks;
  11. this.duration = duration;
  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.CreateMorphAnimationFromNames = function( morphTargetNames, duration ) {
  49. var tracks = [];
  50. var frameStep = duration / morphTargetNames.length;
  51. for( var i = 0; i < morphTargetNames.length; i ++ ) {
  52. var keys = [];
  53. if( ( i - 1 ) >= 0 ) {
  54. keys.push( { time: ( i - 1 ) * frameStep, value: 0 } );
  55. }
  56. keys.push( { time: i * frameStep, value: 1 } );
  57. if( ( i + 1 ) <= morphTargetNames.length ) {
  58. keys.push( { time: ( i + 1 ) * frameStep, value: 0 } );
  59. }
  60. if( ( i - 1 ) < 0 ) {
  61. keys.push( { time: ( morphTargetNames.length - 1 ) * frameStep, value: 0 } );
  62. keys.push( { time: morphTargetNames.length * frameStep, value: 1 } );
  63. }
  64. var morphName = morphTargetNames[i];
  65. var trackName = '.morphTargetInfluences[' + morphName + ']';
  66. var track = new THREE.NumberKeyframeTrack( trackName, keys );
  67. tracks.push( track );
  68. }
  69. var clip = new THREE.AnimationClip( 'morphAnimation', duration, tracks );
  70. return clip;
  71. };
  72. THREE.AnimationClip.CreateMorphAnimation = function( morphTargets, duration ) {
  73. var morphTargetNames = [];
  74. for( var i = 0; i < morphTargets.length; i ++ ) {
  75. morphTargetNames.push( morphTargets[i].name );
  76. }
  77. return THREE.AnimationClip.CreateMorphAnimationFromNames( morphTargetNames, duration );
  78. };
  79. THREE.AnimationClip.FromImplicitMorphTargetAnimations = function( morphTargets, fps ) {
  80. var animations = {};
  81. var animationsArray = [];
  82. var pattern = /([a-z]+)_?(\d+)/;
  83. for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
  84. var morphTarget = morphTargets[ i ];
  85. var parts = morphTarget.name.match( pattern );
  86. if ( parts && parts.length > 1 ) {
  87. var animationName = parts[ 1 ];
  88. var animation = animations[ animationName ];
  89. if ( ! animation ) {
  90. animations[ animationName ] = animation = { name: animationName, morphTargetNames: [] };
  91. animationsArray.push( animation );
  92. }
  93. animation.morphTargetNames.push( morphTarget.name );
  94. }
  95. }
  96. var clips = [];
  97. for( var i = 0; i < animationsArray.length; i ++ ) {
  98. var animation = animationsArray[i];
  99. var clip = new THREE.AnimationClip.CreateMorphAnimationFromNames( animation.morphTargetNames, animation.morphTargetNames.length * fps );
  100. clip.name = animation.name;
  101. clips.push( clip );
  102. }
  103. return clips;
  104. };
  105. // parse the standard JSON format for clips
  106. THREE.AnimationClip.parse = function( json ) {
  107. var name = json.name || "default";
  108. var duration = json.duration || -1;
  109. var fps = json.fps || 30;
  110. var animationTracks = json.tracks || [];
  111. var tracks = [];
  112. for( var i = 0; i < animationTracks.length; i ++ ) {
  113. tracks.push( THREE.KeyframeTrack.parse( animationTracks[i] ).scale( 1 / fps ) );
  114. }
  115. if( tracks.length === 0 ) return null;
  116. return new THREE.AnimationClip( name, duration, tracks );
  117. };
  118. // parse the old animation.hierarchy format
  119. THREE.AnimationClip.parseAnimationHierarchy = function( animation, bones, nodeName ) {
  120. if( ! animation ) {
  121. console.error( " no animation in JSONLoader data" );
  122. return null;
  123. }
  124. var convertTrack = function( trackName, animationKeys, propertyName, trackType, animationKeyToValueFunc ) {
  125. var keys = [];
  126. for( var k = 0; k < animationKeys.length; k ++ ) {
  127. var animationKey = animationKeys[k];
  128. if( animationKey[propertyName] !== undefined ) {
  129. keys.push( { time: animationKey.time, value: animationKeyToValueFunc( animationKey ) } );
  130. }
  131. }
  132. // only return track if there are actually keys.
  133. if( keys.length > 0 ) {
  134. return new trackType( trackName, keys );
  135. }
  136. return null;
  137. };
  138. var tracks = [];
  139. var clipName = animation.name || 'default';
  140. var duration = animation.length || -1; // automatic length determination in AnimationClip.
  141. var fps = animation.fps || 30;
  142. var hierarchyTracks = animation.hierarchy || [];
  143. for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
  144. var animationKeys = hierarchyTracks[ h ].keys;
  145. // skip empty tracks
  146. if( ! animationKeys || animationKeys.length == 0 ) {
  147. continue;
  148. }
  149. // process morph targets in a way exactly compatible with AnimationHandler.init( animation )
  150. if( animationKeys[0].morphTargets ) {
  151. // figure out all morph targets used in this track
  152. var morphTargetNames = {};
  153. for( var k = 0; k < animationKeys.length; k ++ ) {
  154. if( animationKeys[k].morphTargets ) {
  155. for( var m = 0; m < animationKeys[k].morphTargets.length; m ++ ) {
  156. morphTargetNames[ animationKeys[k].morphTargets[m] ] = -1;
  157. }
  158. }
  159. }
  160. // create a track for each morph target with all zero morphTargetInfluences except for the keys in which the morphTarget is named.
  161. for( var morphTargetName in morphTargetNames ) {
  162. var keys = [];
  163. for( var m = 0; m < animationKeys[k].morphTargets.length; m ++ ) {
  164. var animationKey = animationKeys[k];
  165. keys.push( {
  166. time: animationKey.time,
  167. value: (( animationKey.morphTarget === morphTargetName ) ? 1 : 0 )
  168. });
  169. }
  170. tracks.push( new THREE.NumberKeyframeTrack( nodeName + '.morphTargetInfluence[' + morphTargetName + ']', keys ) );
  171. }
  172. duration = morphTargetNames.length * ( fps || 1.0 );
  173. }
  174. else {
  175. var boneName = nodeName + '.bones[' + bones[ h ].name + ']';
  176. // track contains positions...
  177. var positionTrack = convertTrack( boneName + '.position', animationKeys, 'pos', THREE.VectorKeyframeTrack, function( animationKey ) {
  178. return new THREE.Vector3().fromArray( animationKey.pos )
  179. } );
  180. if( positionTrack ) tracks.push( positionTrack );
  181. // track contains quaternions...
  182. var quaternionTrack = convertTrack( boneName + '.quaternion', animationKeys, 'rot', THREE.QuaternionKeyframeTrack, function( animationKey ) {
  183. if( animationKey.rot.slerp ) {
  184. return animationKey.rot.clone();
  185. }
  186. else {
  187. return new THREE.Quaternion().fromArray( animationKey.rot );
  188. }
  189. } );
  190. if( quaternionTrack ) tracks.push( quaternionTrack );
  191. // track contains quaternions...
  192. var scaleTrack = convertTrack( boneName + '.scale', animationKeys, 'scl', THREE.VectorKeyframeTrack, function( animationKey ) {
  193. return new THREE.Vector3().fromArray( animationKey.scl )
  194. } );
  195. if( scaleTrack ) tracks.push( scaleTrack );
  196. }
  197. }
  198. console.log( 'input animation', animation, 'resulting tracks', tracks );
  199. if( tracks.length === 0 ) {
  200. return null;
  201. }
  202. var clip = new THREE.AnimationClip( clipName, duration, tracks );
  203. return clip;
  204. };