subEmitter.ts 4.7 KB

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