babylon.particle.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 angle of the particle.
  37. */
  38. public angle = 0;
  39. /**
  40. * Defines how fast is the angle changing.
  41. */
  42. public angularSpeed = 0;
  43. /**
  44. * Defines the cell index used by the particle to be rendered from a sprite.
  45. */
  46. public cellIndex: number = 0;
  47. private _currentFrameCounter = 0;
  48. /**
  49. * Creates a new instance of @see Particle
  50. * @param particleSystem the particle system the particle belongs to
  51. */
  52. constructor(private particleSystem: ParticleSystem) {
  53. if (!this.particleSystem.isAnimationSheetEnabled) {
  54. return;
  55. }
  56. this.cellIndex = this.particleSystem.startSpriteCellID;
  57. if (this.particleSystem.spriteCellChangeSpeed == 0) {
  58. this.updateCellIndex = this.updateCellIndexWithSpeedCalculated;
  59. }
  60. else {
  61. this.updateCellIndex = this.updateCellIndexWithCustomSpeed;
  62. }
  63. }
  64. /**
  65. * Defines how the sprite cell index is updated for the particle. This is
  66. * defined as a callback.
  67. */
  68. public updateCellIndex: (scaledUpdateSpeed: number) => void;
  69. private updateCellIndexWithSpeedCalculated(scaledUpdateSpeed: number): void {
  70. // (ageOffset / scaledUpdateSpeed) / available cells
  71. var numberOfScaledUpdatesPerCell = ((this.lifeTime - this.age) / scaledUpdateSpeed) / (this.particleSystem.endSpriteCellID + 1 - this.cellIndex);
  72. this._currentFrameCounter += scaledUpdateSpeed;
  73. if (this._currentFrameCounter >= numberOfScaledUpdatesPerCell * scaledUpdateSpeed) {
  74. this._currentFrameCounter = 0;
  75. this.cellIndex++;
  76. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  77. this.cellIndex = this.particleSystem.endSpriteCellID;
  78. }
  79. }
  80. }
  81. private updateCellIndexWithCustomSpeed(): void {
  82. if (this._currentFrameCounter >= this.particleSystem.spriteCellChangeSpeed) {
  83. this.cellIndex++;
  84. this._currentFrameCounter = 0;
  85. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  86. if (this.particleSystem.spriteCellLoop) {
  87. this.cellIndex = this.particleSystem.startSpriteCellID;
  88. }
  89. else {
  90. this.cellIndex = this.particleSystem.endSpriteCellID;
  91. }
  92. }
  93. }
  94. else {
  95. this._currentFrameCounter++;
  96. }
  97. }
  98. /**
  99. * Copy the properties of particle to another one.
  100. * @param other the particle to copy the information to.
  101. */
  102. public copyTo(other: Particle) {
  103. other.position.copyFrom(this.position);
  104. other.direction.copyFrom(this.direction);
  105. other.color.copyFrom(this.color);
  106. other.colorStep.copyFrom(this.colorStep);
  107. other.lifeTime = this.lifeTime;
  108. other.age = this.age;
  109. other.size = this.size;
  110. other.angle = this.angle;
  111. other.angularSpeed = this.angularSpeed;
  112. other.particleSystem = this.particleSystem;
  113. other.cellIndex = this.cellIndex;
  114. }
  115. }
  116. }