subEmitter.ts 5.0 KB

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