animations.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, new BABYLON.Vector3.Zero(), scene);
  5. camera.attachControl(canvas, true);
  6. //Boxes
  7. var box1 = BABYLON.Mesh.CreateBox("Box1", 10.0, scene);
  8. box1.position.x = -20;
  9. var box2 = BABYLON.Mesh.CreateBox("Box2", 10.0, scene);
  10. var materialBox = new BABYLON.StandardMaterial("texture1", scene);
  11. materialBox.diffuseColor = new BABYLON.Color3(0, 1, 0);//Green
  12. var materialBox2 = new BABYLON.StandardMaterial("texture2", scene);
  13. //Applying materials
  14. box1.material = materialBox;
  15. box2.material = materialBox2;
  16. //Positioning box
  17. box2.position.x = 20;
  18. // Creation of a basic animation with box 1
  19. //----------------------------------------
  20. //Create a scaling animation at 30 FPS
  21. var animationBox = new BABYLON.Animation("tutoAnimation", "scaling.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  22. BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  23. //Here we have chosen a loop mode, but you can change to :
  24. // Use previous values and increment it (BABYLON.Animation.ANIMATIONLOOPMODE_RELATIVE)
  25. // Restart from initial value (BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE)
  26. // Keep the final value (BABYLON.Animation.ANIMATIONLOOPMODE_CONSTANT)
  27. // Animation keys
  28. var keys = [];
  29. //At the animation key 0, the value of scaling is "1"
  30. keys.push({
  31. frame: 0,
  32. value: 1
  33. });
  34. //At the animation key 20, the value of scaling is "0.2"
  35. keys.push({
  36. frame: 20,
  37. value: 0.2
  38. });
  39. //At the animation key 100, the value of scaling is "1"
  40. keys.push({
  41. frame: 100,
  42. value: 1
  43. });
  44. //Adding keys to the animation object
  45. animationBox.setKeys(keys);
  46. //Then add the animation object to box1
  47. box1.animations.push(animationBox);
  48. //Finally, launch animations on box1, from key 0 to key 100 with loop activated
  49. scene.beginAnimation(box1, 0, 100, true);
  50. // Creation of a manual animation with box 2
  51. //------------------------------------------
  52. scene.registerBeforeRender(function () {
  53. //The color is defined at run time with random()
  54. box2.material.diffuseColor = new BABYLON.Color3(Math.random(), Math.random(), Math.random());
  55. });
  56. return scene;
  57. }