AnimationClip.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 * as MathUtils from '../math/MathUtils.js';
  10. import { NormalAnimationBlendMode } from '../constants.js';
  11. class AnimationClip {
  12. constructor( name, duration = - 1, tracks, blendMode = NormalAnimationBlendMode ) {
  13. this.name = name;
  14. this.tracks = tracks;
  15. this.duration = duration;
  16. this.blendMode = blendMode;
  17. this.uuid = MathUtils.generateUUID();
  18. // this means it should figure out its duration by scanning the tracks
  19. if ( this.duration < 0 ) {
  20. this.resetDuration();
  21. }
  22. }
  23. static parse( json ) {
  24. const tracks = [],
  25. jsonTracks = json.tracks,
  26. frameTime = 1.0 / ( json.fps || 1.0 );
  27. for ( let i = 0, n = jsonTracks.length; i !== n; ++ i ) {
  28. tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
  29. }
  30. const clip = new this( json.name, json.duration, tracks, json.blendMode );
  31. clip.uuid = json.uuid;
  32. return clip;
  33. }
  34. static toJSON( clip ) {
  35. const tracks = [],
  36. clipTracks = clip.tracks;
  37. const json = {
  38. 'name': clip.name,
  39. 'duration': clip.duration,
  40. 'tracks': tracks,
  41. 'uuid': clip.uuid,
  42. 'blendMode': clip.blendMode
  43. };
  44. for ( let i = 0, n = clipTracks.length; i !== n; ++ i ) {
  45. tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
  46. }
  47. return json;
  48. }
  49. static CreateFromMorphTargetSequence( name, morphTargetSequence, fps, noLoop ) {
  50. const numMorphTargets = morphTargetSequence.length;
  51. const tracks = [];
  52. for ( let i = 0; i < numMorphTargets; i ++ ) {
  53. let times = [];
  54. let values = [];
  55. times.push(
  56. ( i + numMorphTargets - 1 ) % numMorphTargets,
  57. i,
  58. ( i + 1 ) % numMorphTargets );
  59. values.push( 0, 1, 0 );
  60. const 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 this( name, - 1, tracks );
  76. }
  77. static findByName( objectOrClipArray, name ) {
  78. let clipArray = objectOrClipArray;
  79. if ( ! Array.isArray( objectOrClipArray ) ) {
  80. const o = objectOrClipArray;
  81. clipArray = o.geometry && o.geometry.animations || o.animations;
  82. }
  83. for ( let i = 0; i < clipArray.length; i ++ ) {
  84. if ( clipArray[ i ].name === name ) {
  85. return clipArray[ i ];
  86. }
  87. }
  88. return null;
  89. }
  90. static CreateClipsFromMorphTargetSequences( morphTargets, fps, noLoop ) {
  91. const animationToMorphTargets = {};
  92. // tested with https://regex101.com/ on trick sequences
  93. // such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  94. const 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 ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  98. const morphTarget = morphTargets[ i ];
  99. const parts = morphTarget.name.match( pattern );
  100. if ( parts && parts.length > 1 ) {
  101. const name = parts[ 1 ];
  102. let animationMorphTargets = animationToMorphTargets[ name ];
  103. if ( ! animationMorphTargets ) {
  104. animationToMorphTargets[ name ] = animationMorphTargets = [];
  105. }
  106. animationMorphTargets.push( morphTarget );
  107. }
  108. }
  109. const clips = [];
  110. for ( const name in animationToMorphTargets ) {
  111. clips.push( this.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
  112. }
  113. return clips;
  114. }
  115. // parse the animation.hierarchy format
  116. static parseAnimation( animation, bones ) {
  117. if ( ! animation ) {
  118. console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
  119. return null;
  120. }
  121. const addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
  122. // only return track if there are actually keys.
  123. if ( animationKeys.length !== 0 ) {
  124. const times = [];
  125. const 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. const tracks = [];
  134. const clipName = animation.name || 'default';
  135. const fps = animation.fps || 30;
  136. const blendMode = animation.blendMode;
  137. // automatic length determination in AnimationClip.
  138. let duration = animation.length || - 1;
  139. const hierarchyTracks = animation.hierarchy || [];
  140. for ( let h = 0; h < hierarchyTracks.length; h ++ ) {
  141. const animationKeys = hierarchyTracks[ h ].keys;
  142. // skip empty tracks
  143. if ( ! animationKeys || animationKeys.length === 0 ) continue;
  144. // process morph targets
  145. if ( animationKeys[ 0 ].morphTargets ) {
  146. // figure out all morph targets used in this track
  147. const morphTargetNames = {};
  148. let k;
  149. for ( k = 0; k < animationKeys.length; k ++ ) {
  150. if ( animationKeys[ k ].morphTargets ) {
  151. for ( let 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 ( const morphTargetName in morphTargetNames ) {
  160. const times = [];
  161. const values = [];
  162. for ( let m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
  163. const 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. const 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. const clip = new this( clipName, duration, tracks, blendMode );
  188. return clip;
  189. }
  190. resetDuration() {
  191. const tracks = this.tracks;
  192. let duration = 0;
  193. for ( let i = 0, n = tracks.length; i !== n; ++ i ) {
  194. const track = this.tracks[ i ];
  195. duration = Math.max( duration, track.times[ track.times.length - 1 ] );
  196. }
  197. this.duration = duration;
  198. return this;
  199. }
  200. trim() {
  201. for ( let i = 0; i < this.tracks.length; i ++ ) {
  202. this.tracks[ i ].trim( 0, this.duration );
  203. }
  204. return this;
  205. }
  206. validate() {
  207. let valid = true;
  208. for ( let i = 0; i < this.tracks.length; i ++ ) {
  209. valid = valid && this.tracks[ i ].validate();
  210. }
  211. return valid;
  212. }
  213. optimize() {
  214. for ( let i = 0; i < this.tracks.length; i ++ ) {
  215. this.tracks[ i ].optimize();
  216. }
  217. return this;
  218. }
  219. clone() {
  220. const tracks = [];
  221. for ( let i = 0; i < this.tracks.length; i ++ ) {
  222. tracks.push( this.tracks[ i ].clone() );
  223. }
  224. return new this.constructor( this.name, this.duration, tracks, this.blendMode );
  225. }
  226. toJSON() {
  227. return this.constructor.toJSON( this );
  228. }
  229. }
  230. function getTrackTypeForValueTypeName( typeName ) {
  231. switch ( typeName.toLowerCase() ) {
  232. case 'scalar':
  233. case 'double':
  234. case 'float':
  235. case 'number':
  236. case 'integer':
  237. return NumberKeyframeTrack;
  238. case 'vector':
  239. case 'vector2':
  240. case 'vector3':
  241. case 'vector4':
  242. return VectorKeyframeTrack;
  243. case 'color':
  244. return ColorKeyframeTrack;
  245. case 'quaternion':
  246. return QuaternionKeyframeTrack;
  247. case 'bool':
  248. case 'boolean':
  249. return BooleanKeyframeTrack;
  250. case 'string':
  251. return StringKeyframeTrack;
  252. }
  253. throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
  254. }
  255. function parseKeyframeTrack( json ) {
  256. if ( json.type === undefined ) {
  257. throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
  258. }
  259. const trackType = getTrackTypeForValueTypeName( json.type );
  260. if ( json.times === undefined ) {
  261. const times = [], values = [];
  262. AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
  263. json.times = times;
  264. json.values = values;
  265. }
  266. // derived classes can define a static parse method
  267. if ( trackType.parse !== undefined ) {
  268. return trackType.parse( json );
  269. } else {
  270. // by default, we assume a constructor compatible with the base
  271. return new trackType( json.name, json.times, json.values, json.interpolation );
  272. }
  273. }
  274. export { AnimationClip };
粤ICP备19079148号