AnimationClip.js 7.4 KB

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