babylon.particle.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 _randomCellOffset?: number;
  53. /** @hidden */
  54. public _initialDirection: Nullable<Vector3>;
  55. /** @hidden */
  56. public _initialStartSpriteCellID: number;
  57. public _initialEndSpriteCellID: number;
  58. /** @hidden */
  59. public _currentColorGradient: Nullable<ColorGradient>;
  60. /** @hidden */
  61. public _currentColor1 = new Color4(0, 0, 0, 0);
  62. /** @hidden */
  63. public _currentColor2 = new Color4(0, 0, 0, 0);
  64. /** @hidden */
  65. public _currentSizeGradient: Nullable<FactorGradient>;
  66. /** @hidden */
  67. public _currentSize1 = 0;
  68. /** @hidden */
  69. public _currentSize2 = 0;
  70. /** @hidden */
  71. public _currentAngularSpeedGradient: Nullable<FactorGradient>;
  72. /** @hidden */
  73. public _currentAngularSpeed1 = 0;
  74. /** @hidden */
  75. public _currentAngularSpeed2 = 0;
  76. /** @hidden */
  77. public _currentVelocityGradient: Nullable<FactorGradient>;
  78. /** @hidden */
  79. public _currentVelocity1 = 0;
  80. /** @hidden */
  81. public _currentVelocity2 = 0;
  82. /** @hidden */
  83. public _currentLimitVelocityGradient: Nullable<FactorGradient>;
  84. /** @hidden */
  85. public _currentLimitVelocity1 = 0;
  86. /** @hidden */
  87. public _currentLimitVelocity2 = 0;
  88. /** @hidden */
  89. public _currentDragGradient: Nullable<FactorGradient>;
  90. /** @hidden */
  91. public _currentDrag1 = 0;
  92. /** @hidden */
  93. public _currentDrag2 = 0;
  94. /**
  95. * Creates a new instance Particle
  96. * @param particleSystem the particle system the particle belongs to
  97. */
  98. constructor(
  99. /**
  100. * The particle system the particle belongs to.
  101. */
  102. public particleSystem: ParticleSystem) {
  103. if (!this.particleSystem.isAnimationSheetEnabled) {
  104. return;
  105. }
  106. this.updateCellInfoFromSystem();
  107. }
  108. private updateCellInfoFromSystem(): void {
  109. this.cellIndex = this.particleSystem.startSpriteCellID;
  110. }
  111. /**
  112. * Defines how the sprite cell index is updated for the particle
  113. */
  114. public updateCellIndex(): void {
  115. let offsetAge = this.age;
  116. if (this.particleSystem.spriteRandomStartCell) {
  117. if (this._randomCellOffset === undefined) {
  118. this._randomCellOffset = Math.random() * this.lifeTime;
  119. }
  120. offsetAge += this._randomCellOffset;
  121. }
  122. let dist = (this._initialEndSpriteCellID - this._initialStartSpriteCellID);
  123. let ratio = Scalar.Clamp(((offsetAge * this.particleSystem.spriteCellChangeSpeed) % this.lifeTime) / this.lifeTime);
  124. this.cellIndex = this._initialStartSpriteCellID + (ratio * dist) | 0;
  125. }
  126. /** @hidden */
  127. public _reset() {
  128. this.age = 0;
  129. this._currentColorGradient = null;
  130. this._currentSizeGradient = null;
  131. this._currentAngularSpeedGradient = null;
  132. this._currentVelocityGradient = null;
  133. this._currentLimitVelocityGradient = null;
  134. this._currentDragGradient = null;
  135. this.cellIndex = this.particleSystem.startSpriteCellID;
  136. this._randomCellOffset = undefined;
  137. }
  138. /**
  139. * Copy the properties of particle to another one.
  140. * @param other the particle to copy the information to.
  141. */
  142. public copyTo(other: Particle) {
  143. other.position.copyFrom(this.position);
  144. if (this._initialDirection) {
  145. if (other._initialDirection) {
  146. other._initialDirection.copyFrom(this._initialDirection);
  147. } else {
  148. other._initialDirection = this._initialDirection.clone();
  149. }
  150. } else {
  151. other._initialDirection = null;
  152. }
  153. other.direction.copyFrom(this.direction);
  154. other.color.copyFrom(this.color);
  155. other.colorStep.copyFrom(this.colorStep);
  156. other.lifeTime = this.lifeTime;
  157. other.age = this.age;
  158. other._randomCellOffset = undefined;
  159. other.size = this.size;
  160. other.scale.copyFrom(this.scale);
  161. other.angle = this.angle;
  162. other.angularSpeed = this.angularSpeed;
  163. other.particleSystem = this.particleSystem;
  164. other.cellIndex = this.cellIndex;
  165. if (this._currentColorGradient) {
  166. other._currentColorGradient = this._currentColorGradient;
  167. other._currentColor1.copyFrom(this._currentColor1);
  168. other._currentColor2.copyFrom(this._currentColor2);
  169. }
  170. if (this._currentSizeGradient) {
  171. other._currentSizeGradient = this._currentSizeGradient;
  172. other._currentSize1 = this._currentSize1;
  173. other._currentSize2 = this._currentSize2;
  174. }
  175. if (this._currentAngularSpeedGradient) {
  176. other._currentAngularSpeedGradient = this._currentAngularSpeedGradient;
  177. other._currentAngularSpeed1 = this._currentAngularSpeed1;
  178. other._currentAngularSpeed2 = this._currentAngularSpeed2;
  179. }
  180. if (this._currentVelocityGradient) {
  181. other._currentVelocityGradient = this._currentVelocityGradient;
  182. other._currentVelocity1 = this._currentVelocity1;
  183. other._currentVelocity2 = this._currentVelocity2;
  184. }
  185. if (this._currentLimitVelocityGradient) {
  186. other._currentLimitVelocityGradient = this._currentLimitVelocityGradient;
  187. other._currentLimitVelocity1 = this._currentLimitVelocity1;
  188. other._currentLimitVelocity2 = this._currentLimitVelocity2;
  189. }
  190. if (this._currentDragGradient) {
  191. other._currentDragGradient = this._currentDragGradient;
  192. other._currentDrag1 = this._currentDrag1;
  193. other._currentDrag2 = this._currentDrag2;
  194. }
  195. if (this.particleSystem.isAnimationSheetEnabled) {
  196. other._initialStartSpriteCellID = this._initialStartSpriteCellID;
  197. other._initialEndSpriteCellID = this._initialEndSpriteCellID;
  198. }
  199. }
  200. }
  201. }