AnimationClipCreator.js 2.5 KB

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