babylon.particle.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 _initialSize: number;
  55. /** @hidden */
  56. public _currentColorGradient: Nullable<ColorGradient>;
  57. /** @hidden */
  58. public _currentColor1 = new Color4(0, 0, 0, 0);
  59. /** @hidden */
  60. public _currentColor2 = new Color4(0, 0, 0, 0);
  61. /**
  62. * Creates a new instance Particle
  63. * @param particleSystem the particle system the particle belongs to
  64. */
  65. constructor(
  66. /**
  67. * particleSystem the particle system the particle belongs to.
  68. */
  69. public particleSystem: ParticleSystem) {
  70. if (!this.particleSystem.isAnimationSheetEnabled) {
  71. return;
  72. }
  73. this.updateCellInfoFromSystem();
  74. }
  75. private updateCellInfoFromSystem(): void {
  76. this.cellIndex = this.particleSystem.startSpriteCellID;
  77. }
  78. /**
  79. * Defines how the sprite cell index is updated for the particle
  80. */
  81. public updateCellIndex(): void {
  82. let dist = (this.particleSystem.endSpriteCellID - this.particleSystem.startSpriteCellID);
  83. let ratio = Scalar.Clamp(((this.age * this.particleSystem.spriteCellChangeSpeed) / this.lifeTime) % this.lifeTime);
  84. this.cellIndex = this.particleSystem.startSpriteCellID + (ratio * dist) | 0;
  85. }
  86. /**
  87. * Copy the properties of particle to another one.
  88. * @param other the particle to copy the information to.
  89. */
  90. public copyTo(other: Particle) {
  91. other.position.copyFrom(this.position);
  92. if (this._initialDirection) {
  93. if (other._initialDirection) {
  94. other._initialDirection.copyFrom(this._initialDirection);
  95. } else {
  96. other._initialDirection = this._initialDirection.clone();
  97. }
  98. } else {
  99. other._initialDirection = null;
  100. }
  101. other.direction.copyFrom(this.direction);
  102. other.color.copyFrom(this.color);
  103. other.colorStep.copyFrom(this.colorStep);
  104. other.lifeTime = this.lifeTime;
  105. other.age = this.age;
  106. other.size = this.size;
  107. other.scale.copyFrom(this.scale);
  108. other.angle = this.angle;
  109. other.angularSpeed = this.angularSpeed;
  110. other.particleSystem = this.particleSystem;
  111. other.cellIndex = this.cellIndex;
  112. if (this._currentColorGradient) {
  113. other._currentColorGradient = this._currentColorGradient;
  114. other._currentColor1.copyFrom(this._currentColor1);
  115. other._currentColor2.copyFrom(this._currentColor2);
  116. }
  117. }
  118. }
  119. }