AnimationClipCreator.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. ( function () {
  2. class AnimationClipCreator {
  3. static CreateRotationAnimation( period, axis = 'x' ) {
  4. const times = [ 0, period ],
  5. values = [ 0, 360 ];
  6. const trackName = '.rotation[' + axis + ']';
  7. const track = new THREE.NumberKeyframeTrack( trackName, times, values );
  8. return new THREE.AnimationClip( null, period, [ track ] );
  9. }
  10. static CreateScaleAxisAnimation( period, axis = 'x' ) {
  11. const times = [ 0, period ],
  12. values = [ 0, 1 ];
  13. const trackName = '.scale[' + axis + ']';
  14. const track = new THREE.NumberKeyframeTrack( trackName, times, values );
  15. return new THREE.AnimationClip( null, period, [ track ] );
  16. }
  17. static CreateShakeAnimation( duration, shakeScale ) {
  18. const times = [],
  19. values = [],
  20. tmp = new THREE.Vector3();
  21. for ( let i = 0; i < duration * 10; i ++ ) {
  22. times.push( i / 10 );
  23. tmp.set( Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0 ).multiply( shakeScale ).toArray( values, values.length );
  24. }
  25. const trackName = '.position';
  26. const track = new THREE.VectorKeyframeTrack( trackName, times, values );
  27. return new THREE.AnimationClip( null, duration, [ track ] );
  28. }
  29. static CreatePulsationAnimation( duration, pulseScale ) {
  30. const times = [],
  31. values = [],
  32. tmp = new THREE.Vector3();
  33. for ( let i = 0; i < duration * 10; i ++ ) {
  34. times.push( i / 10 );
  35. const scaleFactor = Math.random() * pulseScale;
  36. tmp.set( scaleFactor, scaleFactor, scaleFactor ).toArray( values, values.length );
  37. }
  38. const trackName = '.scale';
  39. const track = new THREE.VectorKeyframeTrack( trackName, times, values );
  40. return new THREE.AnimationClip( null, duration, [ track ] );
  41. }
  42. static CreateVisibilityAnimation( duration ) {
  43. const times = [ 0, duration / 2, duration ],
  44. values = [ true, false, true ];
  45. const trackName = '.visible';
  46. const track = new THREE.BooleanKeyframeTrack( trackName, times, values );
  47. return new THREE.AnimationClip( null, duration, [ track ] );
  48. }
  49. static CreateMaterialColorAnimation( duration, colors ) {
  50. const times = [],
  51. values = [],
  52. timeStep = duration / colors.length;
  53. for ( let i = 0; i <= colors.length; i ++ ) {
  54. times.push( i * timeStep );
  55. values.push( colors[ i % colors.length ] );
  56. }
  57. const trackName = '.material[0].color';
  58. const track = new THREE.ColorKeyframeTrack( trackName, times, values );
  59. return new THREE.AnimationClip( null, duration, [ track ] );
  60. }
  61. }
  62. THREE.AnimationClipCreator = AnimationClipCreator;
  63. } )();