subEmitter.js 4.7 KB

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