babylon.particleHelper.ts 4.0 KB

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