babylon.particleSystemSet.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. module BABYLON {
  2. /** Internal class used to store shapes for emitters */
  3. class ParticleSystemSetEmitterCreationOptions {
  4. public kind: string;
  5. public options: any;
  6. public renderingGroupId: number;
  7. }
  8. /**
  9. * Represents a set of particle systems working together to create a specific effect
  10. */
  11. export class ParticleSystemSet implements IDisposable {
  12. private _emitterCreationOptions: ParticleSystemSetEmitterCreationOptions;
  13. private _emitterNode: Nullable<TransformNode>;
  14. /**
  15. * Gets the particle system list
  16. */
  17. public systems = new Array<IParticleSystem>();
  18. /**
  19. * Gets the emitter node used with this set
  20. */
  21. public get emitterNode(): Nullable<TransformNode> {
  22. return this._emitterNode;
  23. }
  24. /**
  25. * Creates a new emitter mesh as a sphere
  26. * @param options defines the options used to create the sphere
  27. * @param renderingGroupId defines the renderingGroupId to use for the sphere
  28. * @param scene defines the hosting scene
  29. */
  30. public setEmitterAsSphere(options: {diameter: number, segments: number, color: Color3} , renderingGroupId: number, scene: Scene) {
  31. if (this._emitterNode) {
  32. this._emitterNode.dispose();
  33. }
  34. this._emitterCreationOptions = {
  35. kind: "Sphere",
  36. options: options,
  37. renderingGroupId: renderingGroupId
  38. }
  39. let emitterMesh = MeshBuilder.CreateSphere("emitterSphere", {diameter: options.diameter, segments: options.segments}, scene);
  40. emitterMesh.renderingGroupId = renderingGroupId;
  41. var material = new BABYLON.StandardMaterial("emitterSphereMaterial", scene)
  42. material.emissiveColor = options.color;
  43. emitterMesh.material = material;
  44. for (var system of this.systems) {
  45. system.emitter = emitterMesh;
  46. }
  47. this._emitterNode = emitterMesh;
  48. }
  49. /**
  50. * Starts all particle systems of the set
  51. * @param emitter defines an optional mesh to use as emitter for the particle systems
  52. */
  53. public start(emitter?: AbstractMesh): void {
  54. for (var system of this.systems) {
  55. if (emitter) {
  56. system.emitter = emitter;
  57. }
  58. system.start();
  59. }
  60. }
  61. /**
  62. * Release all associated resources
  63. */
  64. public dispose(): void {
  65. for (var system of this.systems) {
  66. system.dispose();
  67. }
  68. this.systems = [];
  69. if (this._emitterNode) {
  70. this._emitterNode.dispose();
  71. this._emitterNode = null;
  72. }
  73. }
  74. /**
  75. * Serialize the set into a JSON compatible object
  76. * @returns a JSON compatible representation of the set
  77. */
  78. public serialize(): any {
  79. var result:any = {};
  80. result.systems = [];
  81. for (var system of this.systems) {
  82. result.systems.push(system.serialize());
  83. }
  84. if (this._emitterNode) {
  85. result.emitter = this._emitterCreationOptions;
  86. }
  87. return result;
  88. }
  89. /**
  90. * Parse a new ParticleSystemSet from a serialized source
  91. * @param data defines a JSON compatible representation of the set
  92. * @param scene defines the hosting scene
  93. * @param gpu defines if we want GPU particles or CPU particles
  94. * @returns a new ParticleSystemSet
  95. */
  96. public static Parse(data: any, scene: Scene, gpu = false): ParticleSystemSet {
  97. var result = new ParticleSystemSet();
  98. var rootUrl = ParticleHelper.BaseAssetsUrl + "/textures/";
  99. for (var system of data.systems) {
  100. result.systems.push(gpu ? GPUParticleSystem.Parse(system, scene, rootUrl) : ParticleSystem.Parse(system, scene, rootUrl));
  101. }
  102. if (data.emitter) {
  103. let options = data.emitter.options;
  104. switch (data.emitter.kind) {
  105. case "Sphere":
  106. result.setEmitterAsSphere({
  107. diameter: options.diameter,
  108. segments: options.segments,
  109. color: Color3.FromArray(options.color)
  110. }, data.emitter.renderingGroupId, scene);
  111. break;
  112. }
  113. }
  114. return result;
  115. }
  116. }
  117. }