KeyframeTrack.js 8.8 KB

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