particles.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. // Setup environment
  4. var light0 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 2, 8), scene);
  5. var camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", 1, 0.8, 20, new BABYLON.Vector3(0, 0, 0), scene);
  6. camera.attachControl(canvas, true);
  7. // Fountain object
  8. var fountain = BABYLON.Mesh.CreateBox("foutain", 1.0, scene);
  9. // Ground
  10. var ground = BABYLON.Mesh.CreatePlane("ground", 50.0, scene);
  11. ground.position = new BABYLON.Vector3(0, -10, 0);
  12. ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);
  13. ground.material = new BABYLON.StandardMaterial("groundMat", scene);
  14. ground.material.backFaceCulling = false;
  15. ground.material.diffuseColor = new BABYLON.Color3(0.3, 0.3, 1);
  16. // Create a particle system
  17. var particleSystem = new BABYLON.ParticleSystem("particles", 2000, scene);
  18. //Texture of each particle
  19. particleSystem.particleTexture = new BABYLON.Texture("textures/flare.png", scene);
  20. // Where the particles come from
  21. particleSystem.emitter = fountain; // the starting object, the emitter
  22. particleSystem.minEmitBox = new BABYLON.Vector3(-1, 0, 0); // Starting all from
  23. particleSystem.maxEmitBox = new BABYLON.Vector3(1, 0, 0); // To...
  24. // Colors of all particles
  25. particleSystem.color1 = new BABYLON.Color4(0.7, 0.8, 1.0, 1.0);
  26. particleSystem.color2 = new BABYLON.Color4(0.2, 0.5, 1.0, 1.0);
  27. particleSystem.colorDead = new BABYLON.Color4(0, 0, 0.2, 0.0);
  28. // Size of each particle (random between...
  29. particleSystem.minSize = 0.1;
  30. particleSystem.maxSize = 0.5;
  31. // Life time of each particle (random between...
  32. particleSystem.minLifeTime = 0.3;
  33. particleSystem.maxLifeTime = 1.5;
  34. // Emission rate
  35. particleSystem.emitRate = 1500;
  36. // Blend mode : BLENDMODE_ONEONE, or BLENDMODE_STANDARD
  37. particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
  38. // Set the gravity of all particles
  39. particleSystem.gravity = new BABYLON.Vector3(0, -9.81, 0);
  40. // Direction of each particle after it has been emitted
  41. particleSystem.direction1 = new BABYLON.Vector3(-7, 8, 3);
  42. particleSystem.direction2 = new BABYLON.Vector3(7, 8, -3);
  43. // Angular speed, in radians
  44. particleSystem.minAngularSpeed = 0;
  45. particleSystem.maxAngularSpeed = Math.PI;
  46. // Speed
  47. particleSystem.minEmitPower = 1;
  48. particleSystem.maxEmitPower = 3;
  49. particleSystem.updateSpeed = 0.005;
  50. // Start the particle system
  51. particleSystem.start();
  52. // Fountain's animation
  53. var keys = [];
  54. var animation = new BABYLON.Animation("animation", "rotation.x", 30, BABYLON.Animation.ANIMATIONTYPE_FLOAT,
  55. BABYLON.Animation.ANIMATIONLOOPMODE_CYCLE);
  56. // At the animation key 0, the value of scaling is "1"
  57. keys.push({
  58. frame: 0,
  59. value: 0
  60. });
  61. // At the animation key 50, the value of scaling is "0.2"
  62. keys.push({
  63. frame: 50,
  64. value: Math.PI
  65. });
  66. // At the animation key 100, the value of scaling is "1"
  67. keys.push({
  68. frame: 100,
  69. value: 0
  70. });
  71. // Launch animation
  72. animation.setKeys(keys);
  73. fountain.animations.push(animation);
  74. scene.beginAnimation(fountain, 0, 100, true);
  75. return scene;
  76. }