AnimationClip.js 7.4 KB

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