AnimationClipCreator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. *
  3. * Creator of typical test AnimationClips / KeyframeTracks
  4. *
  5. * @author Ben Houston / http://clara.io/
  6. * @author David Sarno / http://lighthaus.us/
  7. */
  8. import {
  9. AnimationClip,
  10. BooleanKeyframeTrack,
  11. ColorKeyframeTrack,
  12. NumberKeyframeTrack,
  13. Vector3,
  14. VectorKeyframeTrack
  15. } from "../../../build/three.module.js";
  16. var AnimationClipCreator = function () {};
  17. AnimationClipCreator.CreateRotationAnimation = function ( period, axis ) {
  18. var times = [ 0, period ], values = [ 0, 360 ];
  19. axis = axis || 'x';
  20. var trackName = '.rotation[' + axis + ']';
  21. var track = new NumberKeyframeTrack( trackName, times, values );
  22. return new AnimationClip( null, period, [ track ] );
  23. };
  24. AnimationClipCreator.CreateScaleAxisAnimation = function ( period, axis ) {
  25. var times = [ 0, period ], values = [ 0, 1 ];
  26. axis = axis || 'x';
  27. var trackName = '.scale[' + axis + ']';
  28. var track = new NumberKeyframeTrack( trackName, times, values );
  29. return new AnimationClip( null, period, [ track ] );
  30. };
  31. AnimationClipCreator.CreateShakeAnimation = function ( duration, shakeScale ) {
  32. var times = [], values = [], tmp = new Vector3();
  33. for ( var i = 0; i < duration * 10; i ++ ) {
  34. times.push( i / 10 );
  35. tmp.set( Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0 ).
  36. multiply( shakeScale ).
  37. toArray( values, values.length );
  38. }
  39. var trackName = '.position';
  40. var track = new VectorKeyframeTrack( trackName, times, values );
  41. return new AnimationClip( null, duration, [ track ] );
  42. };
  43. AnimationClipCreator.CreatePulsationAnimation = function ( duration, pulseScale ) {
  44. var times = [], values = [], tmp = new Vector3();
  45. for ( var i = 0; i < duration * 10; i ++ ) {
  46. times.push( i / 10 );
  47. var scaleFactor = Math.random() * pulseScale;
  48. tmp.set( scaleFactor, scaleFactor, scaleFactor ).
  49. toArray( values, values.length );
  50. }
  51. var trackName = '.scale';
  52. var track = new VectorKeyframeTrack( trackName, times, values );
  53. return new AnimationClip( null, duration, [ track ] );
  54. };
  55. AnimationClipCreator.CreateVisibilityAnimation = function ( duration ) {
  56. var times = [ 0, duration / 2, duration ], values = [ true, false, true ];
  57. var trackName = '.visible';
  58. var track = new BooleanKeyframeTrack( trackName, times, values );
  59. return new AnimationClip( null, duration, [ track ] );
  60. };
  61. AnimationClipCreator.CreateMaterialColorAnimation = function ( duration, colors ) {
  62. var times = [], values = [],
  63. timeStep = duration / colors.length;
  64. for ( var i = 0; i <= colors.length; i ++ ) {
  65. times.push( i * timeStep );
  66. values.push( colors[ i % colors.length ] );
  67. }
  68. var trackName = '.material[0].color';
  69. var track = new ColorKeyframeTrack( trackName, times, values );
  70. return new AnimationClip( null, duration, [ track ] );
  71. };
  72. export { AnimationClipCreator };