babylon.particleHelper.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module BABYLON {
  2. /**
  3. * This class is made for on one-liner static method to help creating particle system set.
  4. */
  5. export class ParticleHelper {
  6. /**
  7. * Gets or sets base Assets URL
  8. */
  9. public static BaseAssetsUrl = "https://assets.babylonjs.com/particles";
  10. /**
  11. * Create a default particle system that you can tweak
  12. * @param emitter defines the emitter to use
  13. * @param capacity defines the system capacity (default is 500 particles)
  14. * @param scene defines the hosting scene
  15. * @returns the new Particle system
  16. */
  17. public static CreateDefault(emitter: Nullable<AbstractMesh | Vector3>, capacity = 500, scene?: Scene): ParticleSystem {
  18. var system = new ParticleSystem("default system", capacity, scene!);
  19. system.emitter = emitter;
  20. system.particleTexture = new Texture("https://www.babylonjs.com/assets/Flare.png", system.getScene());
  21. system.createConeEmitter(0.1, Math.PI / 4);
  22. // Particle color
  23. system.color1 = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  24. system.color2 = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  25. system.colorDead = new BABYLON.Color4(1.0, 1.0, 1.0, 0.0);
  26. // Particle Size
  27. system.minSize = 0.1;
  28. system.maxSize = 0.1;
  29. // Emission speed
  30. system.minEmitPower = 2;
  31. system.maxEmitPower = 2;
  32. // Update speed
  33. system.updateSpeed = 1 / 60;
  34. system.emitRate = 30;
  35. return system;
  36. }
  37. /**
  38. * This is the main static method (one-liner) of this helper to create different particle systems
  39. * @param type This string represents the type to the particle system to create
  40. * @param scene The scene where the particle system should live
  41. * @param gpu If the system will use gpu
  42. * @returns the ParticleSystemSet created
  43. */
  44. public static CreateAsync(type: string, scene: Nullable<Scene>, gpu: boolean = false): Promise<ParticleSystemSet> {
  45. if (!scene) {
  46. scene = Engine.LastCreatedScene;;
  47. }
  48. let token = {};
  49. scene!._addPendingData(token);
  50. return new Promise((resolve, reject) => {
  51. if (gpu && !GPUParticleSystem.IsSupported) {
  52. scene!._removePendingData(token);
  53. return reject("Particle system with GPU is not supported.");
  54. }
  55. Tools.LoadFile(`${ParticleHelper.BaseAssetsUrl}/systems/${type}.json`, (data, response) => {
  56. scene!._removePendingData(token);
  57. const newData = JSON.parse(data.toString());
  58. return resolve(ParticleSystemSet.Parse(newData, scene!, gpu));
  59. }, undefined, undefined, undefined, (req, exception) => {
  60. scene!._removePendingData(token);
  61. return reject(`An error occured while the creation of your particle system. Check if your type '${type}' exists.`);
  62. });
  63. });
  64. }
  65. /**
  66. * Static function used to export a particle system to a ParticleSystemSet variable.
  67. * Please note that the emitter shape is not exported
  68. * @param system defines the particle systems to export
  69. */
  70. public static ExportSet(systems: IParticleSystem[]): ParticleSystemSet {
  71. var set = new ParticleSystemSet();
  72. for (var system of systems) {
  73. set.systems.push(system);
  74. }
  75. return set;
  76. }
  77. }
  78. }