babylon.particle.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. private static _Count = 0;
  8. /**
  9. * Unique ID of the particle
  10. */
  11. public id:number;
  12. /**
  13. * The world position of the particle in the scene.
  14. */
  15. public position = Vector3.Zero();
  16. /**
  17. * The world direction of the particle in the scene.
  18. */
  19. public direction = Vector3.Zero();
  20. /**
  21. * The color of the particle.
  22. */
  23. public color = new Color4(0, 0, 0, 0);
  24. /**
  25. * The color change of the particle per step.
  26. */
  27. public colorStep = new Color4(0, 0, 0, 0);
  28. /**
  29. * Defines how long will the life of the particle be.
  30. */
  31. public lifeTime = 1.0;
  32. /**
  33. * The current age of the particle.
  34. */
  35. public age = 0;
  36. /**
  37. * The current size of the particle.
  38. */
  39. public size = 0;
  40. /**
  41. * The current scale of the particle.
  42. */
  43. public scale = new Vector2(1, 1);
  44. /**
  45. * The current angle of the particle.
  46. */
  47. public angle = 0;
  48. /**
  49. * Defines how fast is the angle changing.
  50. */
  51. public angularSpeed = 0;
  52. /**
  53. * Defines the cell index used by the particle to be rendered from a sprite.
  54. */
  55. public cellIndex: number = 0;
  56. /** @hidden */
  57. public _initialDirection: Nullable<Vector3>;
  58. /** @hidden */
  59. public _attachedSubEmitters: Nullable<Array<SubEmitter>> = null;
  60. /** @hidden */
  61. public _initialStartSpriteCellID: number;
  62. public _initialEndSpriteCellID: number;
  63. /** @hidden */
  64. public _currentColorGradient: Nullable<ColorGradient>;
  65. /** @hidden */
  66. public _currentColor1 = new Color4(0, 0, 0, 0);
  67. /** @hidden */
  68. public _currentColor2 = new Color4(0, 0, 0, 0);
  69. /** @hidden */
  70. public _currentSizeGradient: Nullable<FactorGradient>;
  71. /** @hidden */
  72. public _currentSize1 = 0;
  73. /** @hidden */
  74. public _currentSize2 = 0;
  75. /** @hidden */
  76. public _currentAngularSpeedGradient: Nullable<FactorGradient>;
  77. /** @hidden */
  78. public _currentAngularSpeed1 = 0;
  79. /** @hidden */
  80. public _currentAngularSpeed2 = 0;
  81. /** @hidden */
  82. public _currentVelocityGradient: Nullable<FactorGradient>;
  83. /** @hidden */
  84. public _currentVelocity1 = 0;
  85. /** @hidden */
  86. public _currentVelocity2 = 0;
  87. /** @hidden */
  88. public _currentLimitVelocityGradient: Nullable<FactorGradient>;
  89. /** @hidden */
  90. public _currentLimitVelocity1 = 0;
  91. /** @hidden */
  92. public _currentLimitVelocity2 = 0;
  93. /** @hidden */
  94. public _currentDragGradient: Nullable<FactorGradient>;
  95. /** @hidden */
  96. public _currentDrag1 = 0;
  97. /** @hidden */
  98. public _currentDrag2 = 0;
  99. /**
  100. * Creates a new instance Particle
  101. * @param particleSystem the particle system the particle belongs to
  102. */
  103. constructor(
  104. /**
  105. * particleSystem the particle system the particle belongs to.
  106. */
  107. public particleSystem: ParticleSystem) {
  108. this.id = Particle._Count++;
  109. if (!this.particleSystem.isAnimationSheetEnabled) {
  110. return;
  111. }
  112. this.updateCellInfoFromSystem();
  113. }
  114. private updateCellInfoFromSystem(): void {
  115. this.cellIndex = this.particleSystem.startSpriteCellID;
  116. }
  117. /**
  118. * Defines how the sprite cell index is updated for the particle
  119. */
  120. public updateCellIndex(): void {
  121. let dist = (this._initialEndSpriteCellID - this._initialStartSpriteCellID);
  122. let ratio = Scalar.Clamp(((this.age * this.particleSystem.spriteCellChangeSpeed) % this.lifeTime) / this.lifeTime);
  123. this.cellIndex = this._initialStartSpriteCellID + (ratio * dist) | 0;
  124. }
  125. /**
  126. * Copy the properties of particle to another one.
  127. * @param other the particle to copy the information to.
  128. */
  129. public copyTo(other: Particle) {
  130. other.position.copyFrom(this.position);
  131. if (this._initialDirection) {
  132. if (other._initialDirection) {
  133. other._initialDirection.copyFrom(this._initialDirection);
  134. } else {
  135. other._initialDirection = this._initialDirection.clone();
  136. }
  137. } else {
  138. other._initialDirection = null;
  139. }
  140. other.direction.copyFrom(this.direction);
  141. other.color.copyFrom(this.color);
  142. other.colorStep.copyFrom(this.colorStep);
  143. other.lifeTime = this.lifeTime;
  144. other.age = this.age;
  145. other.size = this.size;
  146. other.scale.copyFrom(this.scale);
  147. other.angle = this.angle;
  148. other.angularSpeed = this.angularSpeed;
  149. other.particleSystem = this.particleSystem;
  150. other.cellIndex = this.cellIndex;
  151. other.id = this.id;
  152. other._attachedSubEmitters = this._attachedSubEmitters;
  153. if (this._currentColorGradient) {
  154. other._currentColorGradient = this._currentColorGradient;
  155. other._currentColor1.copyFrom(this._currentColor1);
  156. other._currentColor2.copyFrom(this._currentColor2);
  157. }
  158. if (this._currentSizeGradient) {
  159. other._currentSizeGradient = this._currentSizeGradient;
  160. other._currentSize1 = this._currentSize1;
  161. other._currentSize2 = this._currentSize2;
  162. }
  163. if (this._currentAngularSpeedGradient) {
  164. other._currentAngularSpeedGradient = this._currentAngularSpeedGradient;
  165. other._currentAngularSpeed1 = this._currentAngularSpeed1;
  166. other._currentAngularSpeed2 = this._currentAngularSpeed2;
  167. }
  168. if (this._currentVelocityGradient) {
  169. other._currentVelocityGradient = this._currentVelocityGradient;
  170. other._currentVelocity1 = this._currentVelocity1;
  171. other._currentVelocity2 = this._currentVelocity2;
  172. }
  173. if (this._currentLimitVelocityGradient) {
  174. other._currentLimitVelocityGradient = this._currentLimitVelocityGradient;
  175. other._currentLimitVelocity1 = this._currentLimitVelocity1;
  176. other._currentLimitVelocity2 = this._currentLimitVelocity2;
  177. }
  178. if (this._currentDragGradient) {
  179. other._currentDragGradient = this._currentDragGradient;
  180. other._currentDrag1 = this._currentDrag1;
  181. other._currentDrag2 = this._currentDrag2;
  182. }
  183. if (this.particleSystem.isAnimationSheetEnabled) {
  184. other._initialStartSpriteCellID = this._initialStartSpriteCellID;
  185. other._initialEndSpriteCellID = this._initialEndSpriteCellID;
  186. }
  187. }
  188. }
  189. }