babylon.particle.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. module BABYLON {
  2. /**
  3. * A particle represents one of the element emitted by a particle system.
  4. * This is mainly define by its coordinates, direction, velocity and age.
  5. */
  6. export class Particle {
  7. /**
  8. * The world position of the particle in the scene.
  9. */
  10. public position = Vector3.Zero();
  11. /**
  12. * The world direction of the particle in the scene.
  13. */
  14. public direction = Vector3.Zero();
  15. /**
  16. * The color of the particle.
  17. */
  18. public color = new Color4(0, 0, 0, 0);
  19. /**
  20. * The color change of the particle per step.
  21. */
  22. public colorStep = new Color4(0, 0, 0, 0);
  23. /**
  24. * Defines how long will the life of the particle be.
  25. */
  26. public lifeTime = 1.0;
  27. /**
  28. * The current age of the particle.
  29. */
  30. public age = 0;
  31. /**
  32. * The current size of the particle.
  33. */
  34. public size = 0;
  35. /**
  36. * The current scale of the particle.
  37. */
  38. public scale = new Vector2(1, 1);
  39. /**
  40. * The current angle of the particle.
  41. */
  42. public angle = 0;
  43. /**
  44. * Defines how fast is the angle changing.
  45. */
  46. public angularSpeed = 0;
  47. /**
  48. * Defines the cell index used by the particle to be rendered from a sprite.
  49. */
  50. public cellIndex: number = 0;
  51. /** @hidden */
  52. public _initialDirection: Nullable<Vector3>;
  53. /** @hidden */
  54. public _initialStartSpriteCellID: number;
  55. public _initialEndSpriteCellID: number;
  56. /** @hidden */
  57. public _currentColorGradient: Nullable<ColorGradient>;
  58. /** @hidden */
  59. public _currentColor1 = new Color4(0, 0, 0, 0);
  60. /** @hidden */
  61. public _currentColor2 = new Color4(0, 0, 0, 0);
  62. /** @hidden */
  63. public _currentSizeGradient: Nullable<FactorGradient>;
  64. /** @hidden */
  65. public _currentSize1 = 0;
  66. /** @hidden */
  67. public _currentSize2 = 0;
  68. /**
  69. * Creates a new instance Particle
  70. * @param particleSystem the particle system the particle belongs to
  71. */
  72. constructor(
  73. /**
  74. * particleSystem the particle system the particle belongs to.
  75. */
  76. public particleSystem: ParticleSystem) {
  77. if (!this.particleSystem.isAnimationSheetEnabled) {
  78. return;
  79. }
  80. this.updateCellInfoFromSystem();
  81. }
  82. private updateCellInfoFromSystem(): void {
  83. this.cellIndex = this.particleSystem.startSpriteCellID;
  84. }
  85. /**
  86. * Defines how the sprite cell index is updated for the particle
  87. */
  88. public updateCellIndex(): void {
  89. let dist = (this._initialEndSpriteCellID - this._initialStartSpriteCellID);
  90. let ratio = Scalar.Clamp(((this.age * this.particleSystem.spriteCellChangeSpeed) / this.lifeTime) % this.lifeTime);
  91. this.cellIndex = this._initialStartSpriteCellID + (ratio * dist) | 0;
  92. }
  93. /**
  94. * Copy the properties of particle to another one.
  95. * @param other the particle to copy the information to.
  96. */
  97. public copyTo(other: Particle) {
  98. other.position.copyFrom(this.position);
  99. if (this._initialDirection) {
  100. if (other._initialDirection) {
  101. other._initialDirection.copyFrom(this._initialDirection);
  102. } else {
  103. other._initialDirection = this._initialDirection.clone();
  104. }
  105. } else {
  106. other._initialDirection = null;
  107. }
  108. other.direction.copyFrom(this.direction);
  109. other.color.copyFrom(this.color);
  110. other.colorStep.copyFrom(this.colorStep);
  111. other.lifeTime = this.lifeTime;
  112. other.age = this.age;
  113. other.size = this.size;
  114. other.scale.copyFrom(this.scale);
  115. other.angle = this.angle;
  116. other.angularSpeed = this.angularSpeed;
  117. other.particleSystem = this.particleSystem;
  118. other.cellIndex = this.cellIndex;
  119. if (this._currentColorGradient) {
  120. other._currentColorGradient = this._currentColorGradient;
  121. other._currentColor1.copyFrom(this._currentColor1);
  122. other._currentColor2.copyFrom(this._currentColor2);
  123. }
  124. if (this._currentSizeGradient) {
  125. other._currentSizeGradient = this._currentSizeGradient;
  126. other._currentSize1 = this._currentSize1;
  127. other._currentSize2 = this._currentSize2;
  128. }
  129. if (this.particleSystem.isAnimationSheetEnabled) {
  130. other._initialStartSpriteCellID = this._initialStartSpriteCellID;
  131. other._initialEndSpriteCellID = this._initialEndSpriteCellID;
  132. }
  133. }
  134. }
  135. }