subEmitter.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Vector3 } from "../Maths/math.vector";
  2. import { AbstractMesh } from "../Meshes/abstractMesh";
  3. import { Mesh } from "../Meshes/mesh";
  4. import { _DevTools } from '../Misc/devTools';
  5. declare type Scene = import("../scene").Scene;
  6. declare type ParticleSystem = import("../Particles/particleSystem").ParticleSystem;
  7. /**
  8. * Type of sub emitter
  9. */
  10. export enum SubEmitterType {
  11. /**
  12. * Attached to the particle over it's lifetime
  13. */
  14. ATTACHED,
  15. /**
  16. * Created when the particle dies
  17. */
  18. END
  19. }
  20. /**
  21. * Sub emitter class used to emit particles from an existing particle
  22. */
  23. export class SubEmitter {
  24. /**
  25. * Type of the submitter (Default: END)
  26. */
  27. public type = SubEmitterType.END;
  28. /**
  29. * If the particle should inherit the direction from the particle it's attached to. (+Y will face the direction the particle is moving) (Default: false)
  30. * Note: This only is supported when using an emitter of type Mesh
  31. */
  32. public inheritDirection = false;
  33. /**
  34. * How much of the attached particles speed should be added to the sub emitted particle (default: 0)
  35. */
  36. public inheritedVelocityAmount = 0;
  37. /**
  38. * Creates a sub emitter
  39. * @param particleSystem the particle system to be used by the sub emitter
  40. */
  41. constructor(
  42. /**
  43. * the particle system to be used by the sub emitter
  44. */
  45. public particleSystem: ParticleSystem
  46. ) {
  47. // Create mesh as emitter to support rotation
  48. if (!particleSystem.emitter || !(<AbstractMesh>particleSystem.emitter).dispose) {
  49. particleSystem.emitter = new AbstractMesh("SubemitterSystemEmitter", particleSystem.getScene());
  50. }
  51. // Automatically dispose of subemitter when system is disposed
  52. particleSystem.onDisposeObservable.add(() => {
  53. if (particleSystem.emitter && (<AbstractMesh>particleSystem.emitter).dispose) {
  54. (<AbstractMesh>particleSystem.emitter).dispose();
  55. }
  56. });
  57. }
  58. /**
  59. * Clones the sub emitter
  60. * @returns the cloned sub emitter
  61. */
  62. public clone(): SubEmitter {
  63. // Clone particle system
  64. var emitter = this.particleSystem.emitter;
  65. if (!emitter) {
  66. emitter = new Vector3();
  67. } else if (emitter instanceof Vector3) {
  68. emitter = emitter.clone();
  69. } else if (emitter instanceof AbstractMesh) {
  70. emitter = new Mesh("", emitter.getScene());
  71. emitter.isVisible = false;
  72. }
  73. var clone = new SubEmitter(this.particleSystem.clone("", emitter));
  74. // Clone properties
  75. clone.particleSystem.name += "Clone";
  76. clone.type = this.type;
  77. clone.inheritDirection = this.inheritDirection;
  78. clone.inheritedVelocityAmount = this.inheritedVelocityAmount;
  79. clone.particleSystem._disposeEmitterOnDispose = true;
  80. clone.particleSystem.disposeOnStop = true;
  81. return clone;
  82. }
  83. /**
  84. * Serialize current object to a JSON object
  85. * @returns the serialized object
  86. */
  87. public serialize(): any {
  88. let serializationObject: any = {};
  89. serializationObject.type = this.type;
  90. serializationObject.inheritDirection = this.inheritDirection;
  91. serializationObject.inheritedVelocityAmount = this.inheritedVelocityAmount;
  92. serializationObject.particleSystem = this.particleSystem.serialize();
  93. return serializationObject;
  94. }
  95. /** @hidden */
  96. public static _ParseParticleSystem(system: any, scene: Scene, rootUrl: string): ParticleSystem {
  97. throw _DevTools.WarnImport("ParseParticle");
  98. }
  99. /**
  100. * Creates a new SubEmitter from a serialized JSON version
  101. * @param serializationObject defines the JSON object to read from
  102. * @param scene defines the hosting scene
  103. * @param rootUrl defines the rootUrl for data loading
  104. * @returns a new SubEmitter
  105. */
  106. public static Parse(serializationObject: any, scene: Scene, rootUrl: string): SubEmitter {
  107. let system = serializationObject.particleSystem;
  108. let subEmitter = new SubEmitter(SubEmitter._ParseParticleSystem(system, scene, rootUrl));
  109. subEmitter.type = serializationObject.type;
  110. subEmitter.inheritDirection = serializationObject.inheritDirection;
  111. subEmitter.inheritedVelocityAmount = serializationObject.inheritedVelocityAmount;
  112. subEmitter.particleSystem._isSubEmitter = true;
  113. return subEmitter;
  114. }
  115. /** Release associated resources */
  116. public dispose() {
  117. this.particleSystem.dispose();
  118. }
  119. }