AnimationClip.js 7.4 KB

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