babylon.particle.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. private _currentFrameCounter = 0;
  52. /**
  53. * Creates a new instance Particle
  54. * @param particleSystem the particle system the particle belongs to
  55. */
  56. constructor(
  57. /**
  58. * particleSystem the particle system the particle belongs to.
  59. */
  60. public particleSystem: ParticleSystem) {
  61. if (!this.particleSystem.isAnimationSheetEnabled) {
  62. return;
  63. }
  64. this.updateCellInfoFromSystem();
  65. }
  66. private updateCellInfoFromSystem(): void {
  67. this.cellIndex = this.particleSystem.startSpriteCellID;
  68. if (this.particleSystem.spriteCellChangeSpeed == 0) {
  69. this.updateCellIndex = this._updateCellIndexWithSpeedCalculated;
  70. }
  71. else {
  72. this.updateCellIndex = this._updateCellIndexWithCustomSpeed;
  73. }
  74. }
  75. /**
  76. * Defines how the sprite cell index is updated for the particle. This is
  77. * defined as a callback.
  78. */
  79. public updateCellIndex: (scaledUpdateSpeed: number) => void;
  80. private _updateCellIndexWithSpeedCalculated(scaledUpdateSpeed: number): void {
  81. // (ageOffset / scaledUpdateSpeed) / available cells
  82. var numberOfScaledUpdatesPerCell = ((this.lifeTime - this.age) / scaledUpdateSpeed) / (this.particleSystem.endSpriteCellID + 1 - this.cellIndex);
  83. this._currentFrameCounter += scaledUpdateSpeed;
  84. if (this._currentFrameCounter >= numberOfScaledUpdatesPerCell * scaledUpdateSpeed) {
  85. this._currentFrameCounter = 0;
  86. this.cellIndex++;
  87. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  88. this.cellIndex = this.particleSystem.endSpriteCellID;
  89. }
  90. }
  91. }
  92. private _updateCellIndexWithCustomSpeed(): void {
  93. if (this._currentFrameCounter >= this.particleSystem.spriteCellChangeSpeed) {
  94. this.cellIndex++;
  95. this._currentFrameCounter = 0;
  96. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  97. if (this.particleSystem.spriteCellLoop) {
  98. this.cellIndex = this.particleSystem.startSpriteCellID;
  99. }
  100. else {
  101. this.cellIndex = this.particleSystem.endSpriteCellID;
  102. }
  103. }
  104. }
  105. else {
  106. this._currentFrameCounter++;
  107. }
  108. }
  109. /**
  110. * Copy the properties of particle to another one.
  111. * @param other the particle to copy the information to.
  112. */
  113. public copyTo(other: Particle) {
  114. other.position.copyFrom(this.position);
  115. other.direction.copyFrom(this.direction);
  116. other.color.copyFrom(this.color);
  117. other.colorStep.copyFrom(this.colorStep);
  118. other.lifeTime = this.lifeTime;
  119. other.age = this.age;
  120. other.size = this.size;
  121. other.scale.copyFrom(this.scale);
  122. other.angle = this.angle;
  123. other.angularSpeed = this.angularSpeed;
  124. other.particleSystem = this.particleSystem;
  125. other.cellIndex = this.cellIndex;
  126. }
  127. }
  128. }