particleHelper.ts 4.2 KB

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