particleSystemSet.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { Color3 } from "../Maths/math.color.js";
  2. import { CreateSphere } from "../Meshes/Builders/sphereBuilder.js";
  3. import { GPUParticleSystem } from "./gpuParticleSystem.js";
  4. import { EngineStore } from "../Engines/engineStore.js";
  5. import { ParticleSystem } from "../Particles/particleSystem.js";
  6. import { StandardMaterial } from "../Materials/standardMaterial.js";
  7. /** Internal class used to store shapes for emitters */
  8. class ParticleSystemSetEmitterCreationOptions {
  9. }
  10. /**
  11. * Represents a set of particle systems working together to create a specific effect
  12. */
  13. export class ParticleSystemSet {
  14. constructor() {
  15. this._emitterNodeIsOwned = true;
  16. /**
  17. * Gets the particle system list
  18. */
  19. this.systems = [];
  20. }
  21. /**
  22. * Gets or sets the emitter node used with this set
  23. */
  24. get emitterNode() {
  25. return this._emitterNode;
  26. }
  27. set emitterNode(value) {
  28. if (this._emitterNodeIsOwned && this._emitterNode) {
  29. if (this._emitterNode.dispose) {
  30. this._emitterNode.dispose();
  31. }
  32. this._emitterNodeIsOwned = false;
  33. }
  34. for (const system of this.systems) {
  35. system.emitter = value;
  36. }
  37. this._emitterNode = value;
  38. }
  39. /**
  40. * Creates a new emitter mesh as a sphere
  41. * @param options defines the options used to create the sphere
  42. * @param options.diameter
  43. * @param options.segments
  44. * @param options.color
  45. * @param renderingGroupId defines the renderingGroupId to use for the sphere
  46. * @param scene defines the hosting scene
  47. */
  48. setEmitterAsSphere(options, renderingGroupId, scene) {
  49. if (this._emitterNodeIsOwned && this._emitterNode) {
  50. if (this._emitterNode.dispose) {
  51. this._emitterNode.dispose();
  52. }
  53. }
  54. this._emitterNodeIsOwned = true;
  55. this._emitterCreationOptions = {
  56. kind: "Sphere",
  57. options: options,
  58. renderingGroupId: renderingGroupId,
  59. };
  60. const emitterMesh = CreateSphere("emitterSphere", { diameter: options.diameter, segments: options.segments }, scene);
  61. emitterMesh.renderingGroupId = renderingGroupId;
  62. const material = new StandardMaterial("emitterSphereMaterial", scene);
  63. material.emissiveColor = options.color;
  64. emitterMesh.material = material;
  65. for (const system of this.systems) {
  66. system.emitter = emitterMesh;
  67. }
  68. this._emitterNode = emitterMesh;
  69. }
  70. /**
  71. * Starts all particle systems of the set
  72. * @param emitter defines an optional mesh to use as emitter for the particle systems
  73. */
  74. start(emitter) {
  75. for (const system of this.systems) {
  76. if (emitter) {
  77. system.emitter = emitter;
  78. }
  79. system.start();
  80. }
  81. }
  82. /**
  83. * Release all associated resources
  84. */
  85. dispose() {
  86. for (const system of this.systems) {
  87. system.dispose();
  88. }
  89. this.systems.length = 0;
  90. if (this._emitterNode) {
  91. if (this._emitterNode.dispose) {
  92. this._emitterNode.dispose();
  93. }
  94. this._emitterNode = null;
  95. }
  96. }
  97. /**
  98. * Serialize the set into a JSON compatible object
  99. * @param serializeTexture defines if the texture must be serialized as well
  100. * @returns a JSON compatible representation of the set
  101. */
  102. serialize(serializeTexture = false) {
  103. const result = {};
  104. result.systems = [];
  105. for (const system of this.systems) {
  106. result.systems.push(system.serialize(serializeTexture));
  107. }
  108. if (this._emitterNode) {
  109. result.emitter = this._emitterCreationOptions;
  110. }
  111. return result;
  112. }
  113. /**
  114. * Parse a new ParticleSystemSet from a serialized source
  115. * @param data defines a JSON compatible representation of the set
  116. * @param scene defines the hosting scene
  117. * @param gpu defines if we want GPU particles or CPU particles
  118. * @param capacity defines the system capacity (if null or undefined the sotred capacity will be used)
  119. * @returns a new ParticleSystemSet
  120. */
  121. static Parse(data, scene, gpu = false, capacity) {
  122. const result = new ParticleSystemSet();
  123. const rootUrl = this.BaseAssetsUrl + "/textures/";
  124. scene = scene || EngineStore.LastCreatedScene;
  125. for (const system of data.systems) {
  126. result.systems.push(gpu ? GPUParticleSystem.Parse(system, scene, rootUrl, true, capacity) : ParticleSystem.Parse(system, scene, rootUrl, true, capacity));
  127. }
  128. if (data.emitter) {
  129. const options = data.emitter.options;
  130. switch (data.emitter.kind) {
  131. case "Sphere":
  132. result.setEmitterAsSphere({
  133. diameter: options.diameter,
  134. segments: options.segments,
  135. color: Color3.FromArray(options.color),
  136. }, data.emitter.renderingGroupId, scene);
  137. break;
  138. }
  139. }
  140. return result;
  141. }
  142. }
  143. /**
  144. * Gets or sets base Assets URL
  145. */
  146. ParticleSystemSet.BaseAssetsUrl = "https://assets.babylonjs.com/particles";
  147. //# sourceMappingURL=particleSystemSet.js.map