AnimationClip.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import { AnimationUtils } from './AnimationUtils.js';
  2. import { KeyframeTrack } from './KeyframeTrack.js';
  3. import { BooleanKeyframeTrack } from './tracks/BooleanKeyframeTrack.js';
  4. import { ColorKeyframeTrack } from './tracks/ColorKeyframeTrack.js';
  5. import { NumberKeyframeTrack } from './tracks/NumberKeyframeTrack.js';
  6. import { QuaternionKeyframeTrack } from './tracks/QuaternionKeyframeTrack.js';
  7. import { StringKeyframeTrack } from './tracks/StringKeyframeTrack.js';
  8. import { VectorKeyframeTrack } from './tracks/VectorKeyframeTrack.js';
  9. import { _Math } from '../math/Math.js';
  10. /**
  11. *
  12. * Reusable set of Tracks that represent an animation.
  13. *
  14. * @author Ben Houston / http://clara.io/
  15. * @author David Sarno / http://lighthaus.us/
  16. */
  17. function AnimationClip( name, duration, tracks ) {
  18. this.name = name;
  19. this.tracks = tracks;
  20. this.duration = ( duration !== undefined ) ? duration : - 1;
  21. this.uuid = _Math.generateUUID();
  22. // this means it should figure out its duration by scanning the tracks
  23. if ( this.duration < 0 ) {
  24. this.resetDuration();
  25. }
  26. }
  27. function getTrackTypeForValueTypeName( typeName ) {
  28. switch ( typeName.toLowerCase() ) {
  29. case 'scalar':
  30. case 'double':
  31. case 'float':
  32. case 'number':
  33. case 'integer':
  34. return NumberKeyframeTrack;
  35. case 'vector':
  36. case 'vector2':
  37. case 'vector3':
  38. case 'vector4':
  39. return VectorKeyframeTrack;
  40. case 'color':
  41. return ColorKeyframeTrack;
  42. case 'quaternion':
  43. return QuaternionKeyframeTrack;
  44. case 'bool':
  45. case 'boolean':
  46. return BooleanKeyframeTrack;
  47. case 'string':
  48. return StringKeyframeTrack;
  49. }
  50. throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
  51. }
  52. function parseKeyframeTrack( json ) {
  53. if ( json.type === undefined ) {
  54. throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
  55. }
  56. var trackType = getTrackTypeForValueTypeName( json.type );
  57. if ( json.times === undefined ) {
  58. var times = [], values = [];
  59. AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
  60. json.times = times;
  61. json.values = values;
  62. }
  63. // derived classes can define a static parse method
  64. if ( trackType.parse !== undefined ) {
  65. return trackType.parse( json );
  66. } else {
  67. // by default, we assume a constructor compatible with the base
  68. return new trackType( json.name, json.times, json.values, json.interpolation );
  69. }
  70. }
  71. Object.assign( AnimationClip, {
  72. parse: function ( json ) {
  73. var tracks = [],
  74. jsonTracks = json.tracks,
  75. frameTime = 1.0 / ( json.fps || 1.0 );
  76. for ( var i = 0, n = jsonTracks.length; i !== n; ++ i ) {
  77. tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
  78. }
  79. return new AnimationClip( json.name, json.duration, tracks );
  80. },
  81. toJSON: function ( clip ) {
  82. var tracks = [],
  83. clipTracks = clip.tracks;
  84. var json = {
  85. 'name': clip.name,
  86. 'duration': clip.duration,
  87. 'tracks': tracks,
  88. 'uuid': clip.uuid
  89. };
  90. for ( var i = 0, n = clipTracks.length; i !== n; ++ i ) {
  91. tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
  92. }
  93. return json;
  94. },
  95. CreateFromMorphTargetSequence: function ( name, morphTargetSequence, fps, noLoop ) {
  96. var numMorphTargets = morphTargetSequence.length;
  97. var tracks = [];
  98. for ( var i = 0; i < numMorphTargets; i ++ ) {
  99. var times = [];
  100. var values = [];
  101. times.push(
  102. ( i + numMorphTargets - 1 ) % numMorphTargets,
  103. i,
  104. ( i + 1 ) % numMorphTargets );
  105. values.push( 0, 1, 0 );
  106. var order = AnimationUtils.getKeyframeOrder( times );
  107. times = AnimationUtils.sortedArray( times, 1, order );
  108. values = AnimationUtils.sortedArray( values, 1, order );
  109. // if there is a key at the first frame, duplicate it as the
  110. // last frame as well for perfect loop.
  111. if ( ! noLoop && times[ 0 ] === 0 ) {
  112. times.push( numMorphTargets );
  113. values.push( values[ 0 ] );
  114. }
  115. tracks.push(
  116. new NumberKeyframeTrack(
  117. '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
  118. times, values
  119. ).scale( 1.0 / fps ) );
  120. }
  121. return new AnimationClip( name, - 1, tracks );
  122. },
  123. findByName: function ( objectOrClipArray, name ) {
  124. var clipArray = objectOrClipArray;
  125. if ( ! Array.isArray( objectOrClipArray ) ) {
  126. var o = objectOrClipArray;
  127. clipArray = o.geometry && o.geometry.animations || o.animations;
  128. }
  129. for ( var i = 0; i < clipArray.length; i ++ ) {
  130. if ( clipArray[ i ].name === name ) {
  131. return clipArray[ i ];
  132. }
  133. }
  134. return null;
  135. },
  136. CreateClipsFromMorphTargetSequences: function ( morphTargets, fps, noLoop ) {
  137. var animationToMorphTargets = {};
  138. // tested with https://regex101.com/ on trick sequences
  139. // such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  140. var pattern = /^([\w-]*?)([\d]+)$/;
  141. // sort morph target names into animation groups based
  142. // patterns like Walk_001, Walk_002, Run_001, Run_002
  143. for ( var i = 0, il = morphTargets.length; i < il; i ++ ) {
  144. var morphTarget = morphTargets[ i ];
  145. var parts = morphTarget.name.match( pattern );
  146. if ( parts && parts.length > 1 ) {
  147. var name = parts[ 1 ];
  148. var animationMorphTargets = animationToMorphTargets[ name ];
  149. if ( ! animationMorphTargets ) {
  150. animationToMorphTargets[ name ] = animationMorphTargets = [];
  151. }
  152. animationMorphTargets.push( morphTarget );
  153. }
  154. }
  155. var clips = [];
  156. for ( var name in animationToMorphTargets ) {
  157. clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
  158. }
  159. return clips;
  160. },
  161. // parse the animation.hierarchy format
  162. parseAnimation: function ( animation, bones ) {
  163. if ( ! animation ) {
  164. console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
  165. return null;
  166. }
  167. var addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
  168. // only return track if there are actually keys.
  169. if ( animationKeys.length !== 0 ) {
  170. var times = [];
  171. var values = [];
  172. AnimationUtils.flattenJSON( animationKeys, times, values, propertyName );
  173. // empty keys are filtered out, so check again
  174. if ( times.length !== 0 ) {
  175. destTracks.push( new trackType( trackName, times, values ) );
  176. }
  177. }
  178. };
  179. var tracks = [];
  180. var clipName = animation.name || 'default';
  181. // automatic length determination in AnimationClip.
  182. var duration = animation.length || - 1;
  183. var fps = animation.fps || 30;
  184. var hierarchyTracks = animation.hierarchy || [];
  185. for ( var h = 0; h < hierarchyTracks.length; h ++ ) {
  186. var animationKeys = hierarchyTracks[ h ].keys;
  187. // skip empty tracks
  188. if ( ! animationKeys || animationKeys.length === 0 ) continue;
  189. // process morph targets
  190. if ( animationKeys[ 0 ].morphTargets ) {
  191. // figure out all morph targets used in this track
  192. var morphTargetNames = {};
  193. for ( var k = 0; k < animationKeys.length; k ++ ) {
  194. if ( animationKeys[ k ].morphTargets ) {
  195. for ( var m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
  196. morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
  197. }
  198. }
  199. }
  200. // create a track for each morph target with all zero
  201. // morphTargetInfluences except for the keys in which
  202. // the morphTarget is named.
  203. for ( var morphTargetName in morphTargetNames ) {
  204. var times = [];
  205. var values = [];
  206. for ( var m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
  207. var animationKey = animationKeys[ k ];
  208. times.push( animationKey.time );
  209. values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
  210. }
  211. tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
  212. }
  213. duration = morphTargetNames.length * ( fps || 1.0 );
  214. } else {
  215. // ...assume skeletal animation
  216. var boneName = '.bones[' + bones[ h ].name + ']';
  217. addNonemptyTrack(
  218. VectorKeyframeTrack, boneName + '.position',
  219. animationKeys, 'pos', tracks );
  220. addNonemptyTrack(
  221. QuaternionKeyframeTrack, boneName + '.quaternion',
  222. animationKeys, 'rot', tracks );
  223. addNonemptyTrack(
  224. VectorKeyframeTrack, boneName + '.scale',
  225. animationKeys, 'scl', tracks );
  226. }
  227. }
  228. if ( tracks.length === 0 ) {
  229. return null;
  230. }
  231. var clip = new AnimationClip( clipName, duration, tracks );
  232. return clip;
  233. }
  234. } );
  235. Object.assign( AnimationClip.prototype, {
  236. resetDuration: function () {
  237. var tracks = this.tracks, duration = 0;
  238. for ( var i = 0, n = tracks.length; i !== n; ++ i ) {
  239. var track = this.tracks[ i ];
  240. duration = Math.max( duration, track.times[ track.times.length - 1 ] );
  241. }
  242. this.duration = duration;
  243. return this;
  244. },
  245. trim: function () {
  246. for ( var i = 0; i < this.tracks.length; i ++ ) {
  247. this.tracks[ i ].trim( 0, this.duration );
  248. }
  249. return this;
  250. },
  251. validate: function () {
  252. var valid = true;
  253. for ( var i = 0; i < this.tracks.length; i ++ ) {
  254. valid = valid && this.tracks[ i ].validate();
  255. }
  256. return valid;
  257. },
  258. optimize: function () {
  259. for ( var i = 0; i < this.tracks.length; i ++ ) {
  260. this.tracks[ i ].optimize();
  261. }
  262. return this;
  263. },
  264. clone: function () {
  265. var tracks = [];
  266. for ( var i = 0; i < this.tracks.length; i ++ ) {
  267. tracks.push( this.tracks[ i ].clone() );
  268. }
  269. return new AnimationClip( this.name, this.duration, tracks );
  270. }
  271. } );
  272. export { AnimationClip };
粤ICP备19079148号