KeyframeTrackPrototype.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import { InterpolateLinear } from '../constants';
  2. import { AnimationUtils } from './AnimationUtils';
  3. import { InterpolateSmooth, InterpolateDiscrete } from '../constants';
  4. import { CubicInterpolant } from '../math/interpolants/CubicInterpolant';
  5. import { LinearInterpolant } from '../math/interpolants/LinearInterpolant';
  6. import { DiscreteInterpolant } from '../math/interpolants/DiscreteInterpolant';
  7. var KeyframeTrackPrototype;
  8. KeyframeTrackPrototype = {
  9. TimeBufferType: Float32Array,
  10. ValueBufferType: Float32Array,
  11. DefaultInterpolation: InterpolateLinear,
  12. InterpolantFactoryMethodDiscrete: function ( result ) {
  13. return new DiscreteInterpolant(
  14. this.times, this.values, this.getValueSize(), result );
  15. },
  16. InterpolantFactoryMethodLinear: function ( result ) {
  17. return new LinearInterpolant(
  18. this.times, this.values, this.getValueSize(), result );
  19. },
  20. InterpolantFactoryMethodSmooth: function ( result ) {
  21. return new CubicInterpolant(
  22. this.times, this.values, this.getValueSize(), result );
  23. },
  24. setInterpolation: function ( interpolation ) {
  25. var factoryMethod;
  26. switch ( interpolation ) {
  27. case InterpolateDiscrete:
  28. factoryMethod = this.InterpolantFactoryMethodDiscrete;
  29. break;
  30. case InterpolateLinear:
  31. factoryMethod = this.InterpolantFactoryMethodLinear;
  32. break;
  33. case InterpolateSmooth:
  34. factoryMethod = this.InterpolantFactoryMethodSmooth;
  35. break;
  36. }
  37. if ( factoryMethod === undefined ) {
  38. var message = "unsupported interpolation for " +
  39. this.ValueTypeName + " keyframe track named " + this.name;
  40. if ( this.createInterpolant === undefined ) {
  41. // fall back to default, unless the default itself is messed up
  42. if ( interpolation !== this.DefaultInterpolation ) {
  43. this.setInterpolation( this.DefaultInterpolation );
  44. } else {
  45. throw new Error( message ); // fatal, in this case
  46. }
  47. }
  48. console.warn( message );
  49. return;
  50. }
  51. this.createInterpolant = factoryMethod;
  52. },
  53. getInterpolation: function () {
  54. switch ( this.createInterpolant ) {
  55. case this.InterpolantFactoryMethodDiscrete:
  56. return InterpolateDiscrete;
  57. case this.InterpolantFactoryMethodLinear:
  58. return InterpolateLinear;
  59. case this.InterpolantFactoryMethodSmooth:
  60. return InterpolateSmooth;
  61. }
  62. },
  63. getValueSize: function () {
  64. return this.values.length / this.times.length;
  65. },
  66. // move all keyframes either forwards or backwards in time
  67. shift: function ( timeOffset ) {
  68. if ( timeOffset !== 0.0 ) {
  69. var times = this.times;
  70. for ( var i = 0, n = times.length; i !== n; ++ i ) {
  71. times[ i ] += timeOffset;
  72. }
  73. }
  74. return this;
  75. },
  76. // scale all keyframe times by a factor (useful for frame <-> seconds conversions)
  77. scale: function ( timeScale ) {
  78. if ( timeScale !== 1.0 ) {
  79. var times = this.times;
  80. for ( var i = 0, n = times.length; i !== n; ++ i ) {
  81. times[ i ] *= timeScale;
  82. }
  83. }
  84. return this;
  85. },
  86. // removes keyframes before and after animation without changing any values within the range [startTime, endTime].
  87. // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
  88. trim: function ( startTime, endTime ) {
  89. var times = this.times,
  90. nKeys = times.length,
  91. from = 0,
  92. to = nKeys - 1;
  93. while ( from !== nKeys && times[ from ] < startTime ) ++ from;
  94. while ( to !== - 1 && times[ to ] > endTime ) -- to;
  95. ++ to; // inclusive -> exclusive bound
  96. if ( from !== 0 || to !== nKeys ) {
  97. // empty tracks are forbidden, so keep at least one keyframe
  98. if ( from >= to ) to = Math.max( to, 1 ), from = to - 1;
  99. var stride = this.getValueSize();
  100. this.times = AnimationUtils.arraySlice( times, from, to );
  101. this.values = AnimationUtils.
  102. arraySlice( this.values, from * stride, to * stride );
  103. }
  104. return this;
  105. },
  106. // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
  107. validate: function () {
  108. var valid = true;
  109. var valueSize = this.getValueSize();
  110. if ( valueSize - Math.floor( valueSize ) !== 0 ) {
  111. console.error( "invalid value size in track", this );
  112. valid = false;
  113. }
  114. var times = this.times,
  115. values = this.values,
  116. nKeys = times.length;
  117. if ( nKeys === 0 ) {
  118. console.error( "track is empty", this );
  119. valid = false;
  120. }
  121. var prevTime = null;
  122. for ( var i = 0; i !== nKeys; i ++ ) {
  123. var currTime = times[ i ];
  124. if ( typeof currTime === 'number' && isNaN( currTime ) ) {
  125. console.error( "time is not a valid number", this, i, currTime );
  126. valid = false;
  127. break;
  128. }
  129. if ( prevTime !== null && prevTime > currTime ) {
  130. console.error( "out of order keys", this, i, currTime, prevTime );
  131. valid = false;
  132. break;
  133. }
  134. prevTime = currTime;
  135. }
  136. if ( values !== undefined ) {
  137. if ( AnimationUtils.isTypedArray( values ) ) {
  138. for ( var i = 0, n = values.length; i !== n; ++ i ) {
  139. var value = values[ i ];
  140. if ( isNaN( value ) ) {
  141. console.error( "value is not a valid number", this, i, value );
  142. valid = false;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. return valid;
  149. },
  150. // removes equivalent sequential keys as common in morph target sequences
  151. // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
  152. optimize: function () {
  153. var times = this.times,
  154. values = this.values,
  155. stride = this.getValueSize(),
  156. smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
  157. writeIndex = 1,
  158. lastIndex = times.length - 1;
  159. for ( var i = 1; i < lastIndex; ++ i ) {
  160. var keep = false;
  161. var time = times[ i ];
  162. var timeNext = times[ i + 1 ];
  163. // remove adjacent keyframes scheduled at the same time
  164. if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) {
  165. if ( ! smoothInterpolation ) {
  166. // remove unnecessary keyframes same as their neighbors
  167. var offset = i * stride,
  168. offsetP = offset - stride,
  169. offsetN = offset + stride;
  170. for ( var j = 0; j !== stride; ++ j ) {
  171. var value = values[ offset + j ];
  172. if ( value !== values[ offsetP + j ] ||
  173. value !== values[ offsetN + j ] ) {
  174. keep = true;
  175. break;
  176. }
  177. }
  178. } else keep = true;
  179. }
  180. // in-place compaction
  181. if ( keep ) {
  182. if ( i !== writeIndex ) {
  183. times[ writeIndex ] = times[ i ];
  184. var readOffset = i * stride,
  185. writeOffset = writeIndex * stride;
  186. for ( var j = 0; j !== stride; ++ j )
  187. values[ writeOffset + j ] = values[ readOffset + j ];
  188. }
  189. ++ writeIndex;
  190. }
  191. }
  192. // flush last keyframe (compaction looks ahead)
  193. if ( lastIndex > 0 ) {
  194. times[ writeIndex ] = times[ lastIndex ];
  195. for ( var readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j )
  196. values[ writeOffset + j ] = values[ readOffset + j ];
  197. ++ writeIndex;
  198. }
  199. if ( writeIndex !== times.length ) {
  200. this.times = AnimationUtils.arraySlice( times, 0, writeIndex );
  201. this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride );
  202. }
  203. return this;
  204. }
  205. };
  206. export { KeyframeTrackPrototype };