Easing functions.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 100, 100), scene);
  4. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  5. camera.attachControl(canvas, true);
  6. // Torus
  7. var torus = BABYLON.Mesh.CreateTorus("torus", 8, 2, 32, scene, false);
  8. torus.position.x = 25;
  9. torus.position.z = 30;
  10. var materialBox = new BABYLON.StandardMaterial("texture1", scene);
  11. materialBox.diffuseColor = new BABYLON.Color3(0, 1, 0);//Green
  12. // -----------------------------------------
  13. // Creation of an easing animation within predefined easing functions
  14. // ------------------------------------------
  15. //Create a Vector3 animation at 30 FPS
  16. var animationTorus = new BABYLON.Animation("torusEasingAnimation", "position", 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  17. // the torus destination position
  18. var nextPos = torus.position.add(new BABYLON.Vector3(-80, 0, 0));
  19. // Animation keys
  20. var keysTorus = [];
  21. keysTorus.push({ frame: 0, value: torus.position });
  22. keysTorus.push({ frame: 120, value: nextPos });
  23. animationTorus.setKeys(keysTorus);
  24. // Adding an easing function
  25. // You can use :
  26. //1. CircleEase()
  27. //2. BackEase(amplitude)
  28. //3. BounceEase(bounces, bounciness)
  29. //4. CubicEase()
  30. //5. ElasticEase(oscillations, springiness)
  31. //6. ExponentialEase(exponent)
  32. //7. PowerEase(power)
  33. //8. QuadraticEase()
  34. //9. QuarticEase()
  35. //10. QuinticEase()
  36. //11. SineEase()
  37. // And if you want a total control, you can use a Bezier Curve animation
  38. //12. BezierCurveEase(x1, y1, x2, y2)
  39. var easingFunction = new BABYLON.CircleEase();
  40. // For each easing function, you can choose beetween EASEIN (default), EASEOUT, EASEINOUT
  41. easingFunction.setEasingMode(BABYLON.EasingFunction.EASINGMODE_EASEINOUT);
  42. // Adding easing function to my animation
  43. animationTorus.setEasingFunction(easingFunction);
  44. // Adding animation to my torus animations collection
  45. torus.animations.push(animationTorus);
  46. //Finally, launch animations on torus, from key 0 to key 120 with loop activated
  47. scene.beginAnimation(torus, 0, 120, true);
  48. // ------------------------------------------
  49. // Using Bezier curve to create a custom easing function
  50. // See here to see some samples and values : http://cubic-bezier.com/
  51. // -----------------------------------------
  52. // Torus
  53. var bezierTorus = BABYLON.Mesh.CreateTorus("torus", 8, 2, 32, scene, false);
  54. bezierTorus.position.x = 25;
  55. bezierTorus.position.z = 0;
  56. // Create the animation
  57. var animationBezierTorus = new BABYLON.Animation("animationBezierTorus", "position", 30, BABYLON.Animation.ANIMATIONTYPE_VECTOR3, BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  58. var keysBezierTorus = [];
  59. keysBezierTorus.push({ frame: 0, value: bezierTorus.position });
  60. keysBezierTorus.push({ frame: 120, value: bezierTorus.position.add(new BABYLON.Vector3(-80, 0, 0)) });
  61. animationBezierTorus.setKeys(keysBezierTorus);
  62. var bezierEase = new BABYLON.BezierCurveEase(0.32, -0.73, 0.69, 1.59);
  63. animationBezierTorus.setEasingFunction(bezierEase);
  64. bezierTorus.animations.push(animationBezierTorus);
  65. scene.beginAnimation(bezierTorus, 0, 120, true);
  66. // ------------------------------------------
  67. // Create a simple animation without easing functions
  68. // ------------------------------------------
  69. var torus0 = BABYLON.Mesh.CreateTorus("torus", 8, 2, 32, scene, false);
  70. torus0.position.x = 25;
  71. torus0.position.z = -30;
  72. torus0.material = materialBox;
  73. BABYLON.Animation.CreateAndStartAnimation("anim", torus0, "position", 30, 120,
  74. torus0.position, torus0.position.add(new BABYLON.Vector3(-80, 0, 0)));
  75. return scene;
  76. }