babylon.particle.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. module BABYLON {
  2. export class Particle {
  3. public position = Vector3.Zero();
  4. public direction = Vector3.Zero();
  5. public color = new Color4(0, 0, 0, 0);
  6. public colorStep = new Color4(0, 0, 0, 0);
  7. public lifeTime = 1.0;
  8. public age = 0;
  9. public size = 0;
  10. public angle = 0;
  11. public angularSpeed = 0;
  12. private _currentFrameCounter = 0;
  13. public cellIndex: number = 0;
  14. constructor(private particleSystem: ParticleSystem) {
  15. if (!this.particleSystem.isAnimationSheetEnabled) {
  16. return;
  17. }
  18. this.cellIndex = this.particleSystem.startSpriteCellID;
  19. if (this.particleSystem.spriteCellChangeSpeed == 0) {
  20. this.updateCellIndex = this.updateCellIndexWithSpeedCalculated;
  21. }
  22. else {
  23. this.updateCellIndex = this.updateCellIndexWithCustomSpeed;
  24. }
  25. }
  26. public updateCellIndex: (scaledUpdateSpeed: number) => void;
  27. private updateCellIndexWithSpeedCalculated(scaledUpdateSpeed: number): void {
  28. // (ageOffset / scaledUpdateSpeed) / available cells
  29. var numberOfScaledUpdatesPerCell = ((this.lifeTime - this.age) / scaledUpdateSpeed) / (this.particleSystem.endSpriteCellID + 1 - this.cellIndex);
  30. this._currentFrameCounter += scaledUpdateSpeed;
  31. if (this._currentFrameCounter >= numberOfScaledUpdatesPerCell * scaledUpdateSpeed) {
  32. this._currentFrameCounter = 0;
  33. this.cellIndex++;
  34. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  35. this.cellIndex = this.particleSystem.endSpriteCellID;
  36. }
  37. }
  38. }
  39. private updateCellIndexWithCustomSpeed(): void {
  40. if (this._currentFrameCounter >= this.particleSystem.spriteCellChangeSpeed) {
  41. this.cellIndex++;
  42. this._currentFrameCounter = 0;
  43. if (this.cellIndex > this.particleSystem.endSpriteCellID) {
  44. if (this.particleSystem.spriteCellLoop) {
  45. this.cellIndex = this.particleSystem.startSpriteCellID;
  46. }
  47. else {
  48. this.cellIndex = this.particleSystem.endSpriteCellID;
  49. }
  50. }
  51. }
  52. else {
  53. this._currentFrameCounter++;
  54. }
  55. }
  56. public copyTo(other: Particle) {
  57. other.position.copyFrom(this.position);
  58. other.direction.copyFrom(this.direction);
  59. other.color.copyFrom(this.color);
  60. other.colorStep.copyFrom(this.colorStep);
  61. other.lifeTime = this.lifeTime;
  62. other.age = this.age;
  63. other.size = this.size;
  64. other.angle = this.angle;
  65. other.angularSpeed = this.angularSpeed;
  66. other.particleSystem = this.particleSystem;
  67. other.cellIndex = this.cellIndex;
  68. }
  69. }
  70. }