KeyframeTrack.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. import {
  2. InterpolateLinear,
  3. InterpolateSmooth,
  4. InterpolateDiscrete
  5. } from '../constants.js';
  6. import { CubicInterpolant } from '../math/interpolants/CubicInterpolant.js';
  7. import { LinearInterpolant } from '../math/interpolants/LinearInterpolant.js';
  8. import { DiscreteInterpolant } from '../math/interpolants/DiscreteInterpolant.js';
  9. import { AnimationUtils } from './AnimationUtils.js';
  10. function KeyframeTrack( name, times, values, interpolation ) {
  11. if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
  12. if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
  13. this.name = name;
  14. this.times = AnimationUtils.convertArray( times, this.TimeBufferType );
  15. this.values = AnimationUtils.convertArray( values, this.ValueBufferType );
  16. this.setInterpolation( interpolation || this.DefaultInterpolation );
  17. }
  18. // Static methods
  19. Object.assign( KeyframeTrack, {
  20. // Serialization (in static context, because of constructor invocation
  21. // and automatic invocation of .toJSON):
  22. toJSON: function ( track ) {
  23. const trackType = track.constructor;
  24. let json;
  25. // derived classes can define a static toJSON method
  26. if ( trackType.toJSON !== undefined ) {
  27. json = trackType.toJSON( track );
  28. } else {
  29. // by default, we assume the data can be serialized as-is
  30. json = {
  31. 'name': track.name,
  32. 'times': AnimationUtils.convertArray( track.times, Array ),
  33. 'values': AnimationUtils.convertArray( track.values, Array )
  34. };
  35. const interpolation = track.getInterpolation();
  36. if ( interpolation !== track.DefaultInterpolation ) {
  37. json.interpolation = interpolation;
  38. }
  39. }
  40. json.type = track.ValueTypeName; // mandatory
  41. return json;
  42. }
  43. } );
  44. Object.assign( KeyframeTrack.prototype, {
  45. constructor: KeyframeTrack,
  46. TimeBufferType: Float32Array,
  47. ValueBufferType: Float32Array,
  48. DefaultInterpolation: InterpolateLinear,
  49. InterpolantFactoryMethodDiscrete: function ( result ) {
  50. return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
  51. },
  52. InterpolantFactoryMethodLinear: function ( result ) {
  53. return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
  54. },
  55. InterpolantFactoryMethodSmooth: function ( result ) {
  56. return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
  57. },
  58. setInterpolation: function ( interpolation ) {
  59. let factoryMethod;
  60. switch ( interpolation ) {
  61. case InterpolateDiscrete:
  62. factoryMethod = this.InterpolantFactoryMethodDiscrete;
  63. break;
  64. case InterpolateLinear:
  65. factoryMethod = this.InterpolantFactoryMethodLinear;
  66. break;
  67. case InterpolateSmooth:
  68. factoryMethod = this.InterpolantFactoryMethodSmooth;
  69. break;
  70. }
  71. if ( factoryMethod === undefined ) {
  72. const message = 'unsupported interpolation for ' +
  73. this.ValueTypeName + ' keyframe track named ' + this.name;
  74. if ( this.createInterpolant === undefined ) {
  75. // fall back to default, unless the default itself is messed up
  76. if ( interpolation !== this.DefaultInterpolation ) {
  77. this.setInterpolation( this.DefaultInterpolation );
  78. } else {
  79. throw new Error( message ); // fatal, in this case
  80. }
  81. }
  82. console.warn( 'THREE.KeyframeTrack:', message );
  83. return this;
  84. }
  85. this.createInterpolant = factoryMethod;
  86. return this;
  87. },
  88. getInterpolation: function () {
  89. switch ( this.createInterpolant ) {
  90. case this.InterpolantFactoryMethodDiscrete:
  91. return InterpolateDiscrete;
  92. case this.InterpolantFactoryMethodLinear:
  93. return InterpolateLinear;
  94. case this.InterpolantFactoryMethodSmooth:
  95. return InterpolateSmooth;
  96. }
  97. },
  98. getValueSize: function () {
  99. return this.values.length / this.times.length;
  100. },
  101. // move all keyframes either forwards or backwards in time
  102. shift: function ( timeOffset ) {
  103. if ( timeOffset !== 0.0 ) {
  104. const times = this.times;
  105. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  106. times[ i ] += timeOffset;
  107. }
  108. }
  109. return this;
  110. },
  111. // scale all keyframe times by a factor (useful for frame <-> seconds conversions)
  112. scale: function ( timeScale ) {
  113. if ( timeScale !== 1.0 ) {
  114. const times = this.times;
  115. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  116. times[ i ] *= timeScale;
  117. }
  118. }
  119. return this;
  120. },
  121. // removes keyframes before and after animation without changing any values within the range [startTime, endTime].
  122. // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
  123. trim: function ( startTime, endTime ) {
  124. const times = this.times,
  125. nKeys = times.length;
  126. let from = 0,
  127. to = nKeys - 1;
  128. while ( from !== nKeys && times[ from ] < startTime ) {
  129. ++ from;
  130. }
  131. while ( to !== - 1 && times[ to ] > endTime ) {
  132. -- to;
  133. }
  134. ++ to; // inclusive -> exclusive bound
  135. if ( from !== 0 || to !== nKeys ) {
  136. // empty tracks are forbidden, so keep at least one keyframe
  137. if ( from >= to ) {
  138. to = Math.max( to, 1 );
  139. from = to - 1;
  140. }
  141. const stride = this.getValueSize();
  142. this.times = AnimationUtils.arraySlice( times, from, to );
  143. this.values = AnimationUtils.arraySlice( this.values, from * stride, to * stride );
  144. }
  145. return this;
  146. },
  147. // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
  148. validate: function () {
  149. let valid = true;
  150. const valueSize = this.getValueSize();
  151. if ( valueSize - Math.floor( valueSize ) !== 0 ) {
  152. console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
  153. valid = false;
  154. }
  155. const times = this.times,
  156. values = this.values,
  157. nKeys = times.length;
  158. if ( nKeys === 0 ) {
  159. console.error( 'THREE.KeyframeTrack: Track is empty.', this );
  160. valid = false;
  161. }
  162. let prevTime = null;
  163. for ( let i = 0; i !== nKeys; i ++ ) {
  164. const currTime = times[ i ];
  165. if ( typeof currTime === 'number' && isNaN( currTime ) ) {
  166. console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
  167. valid = false;
  168. break;
  169. }
  170. if ( prevTime !== null && prevTime > currTime ) {
  171. console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
  172. valid = false;
  173. break;
  174. }
  175. prevTime = currTime;
  176. }
  177. if ( values !== undefined ) {
  178. if ( AnimationUtils.isTypedArray( values ) ) {
  179. for ( let i = 0, n = values.length; i !== n; ++ i ) {
  180. const value = values[ i ];
  181. if ( isNaN( value ) ) {
  182. console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
  183. valid = false;
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. return valid;
  190. },
  191. // removes equivalent sequential keys as common in morph target sequences
  192. // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
  193. optimize: function () {
  194. // times or values may be shared with other tracks, so overwriting is unsafe
  195. const times = AnimationUtils.arraySlice( this.times ),
  196. values = AnimationUtils.arraySlice( this.values ),
  197. stride = this.getValueSize(),
  198. smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
  199. lastIndex = times.length - 1;
  200. let writeIndex = 1;
  201. for ( let i = 1; i < lastIndex; ++ i ) {
  202. let keep = false;
  203. const time = times[ i ];
  204. const timeNext = times[ i + 1 ];
  205. // remove adjacent keyframes scheduled at the same time
  206. if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) {
  207. if ( ! smoothInterpolation ) {
  208. // remove unnecessary keyframes same as their neighbors
  209. const offset = i * stride,
  210. offsetP = offset - stride,
  211. offsetN = offset + stride;
  212. for ( let j = 0; j !== stride; ++ j ) {
  213. const value = values[ offset + j ];
  214. if ( value !== values[ offsetP + j ] ||
  215. value !== values[ offsetN + j ] ) {
  216. keep = true;
  217. break;
  218. }
  219. }
  220. } else {
  221. keep = true;
  222. }
  223. }
  224. // in-place compaction
  225. if ( keep ) {
  226. if ( i !== writeIndex ) {
  227. times[ writeIndex ] = times[ i ];
  228. const readOffset = i * stride,
  229. writeOffset = writeIndex * stride;
  230. for ( let j = 0; j !== stride; ++ j ) {
  231. values[ writeOffset + j ] = values[ readOffset + j ];
  232. }
  233. }
  234. ++ writeIndex;
  235. }
  236. }
  237. // flush last keyframe (compaction looks ahead)
  238. if ( lastIndex > 0 ) {
  239. times[ writeIndex ] = times[ lastIndex ];
  240. for ( let readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
  241. values[ writeOffset + j ] = values[ readOffset + j ];
  242. }
  243. ++ writeIndex;
  244. }
  245. if ( writeIndex !== times.length ) {
  246. this.times = AnimationUtils.arraySlice( times, 0, writeIndex );
  247. this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride );
  248. } else {
  249. this.times = times;
  250. this.values = values;
  251. }
  252. return this;
  253. },
  254. clone: function () {
  255. const times = AnimationUtils.arraySlice( this.times, 0 );
  256. const values = AnimationUtils.arraySlice( this.values, 0 );
  257. const TypedKeyframeTrack = this.constructor;
  258. const track = new TypedKeyframeTrack( this.name, times, values );
  259. // Interpolant argument to constructor is not saved, so copy the factory method directly.
  260. track.createInterpolant = this.createInterpolant;
  261. return track;
  262. }
  263. } );
  264. export { KeyframeTrack };