AnimationClip.js 9.6 KB

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