ParticleInstance.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { FactorGradient, ColorGradient } from '../Util/Gradient'
  2. import CustomParticle from '../index'
  3. const xrFrameSystem = wx.getXrFrameSystem();
  4. // 粒子实例,代表每一个粒子的具体属性
  5. export default class ParticleInstance {
  6. public static count: number = 0;
  7. public id: number;
  8. public position: xrFrameSystem.Vector3;
  9. public direction: xrFrameSystem.Vector3;
  10. public speed: number;
  11. public color: xrFrameSystem.Vector4;
  12. public colorStep: xrFrameSystem.Vector4;
  13. public rampPos: xrFrameSystem.Vector4;
  14. // 粒子生命周期
  15. public lifeTime: number;
  16. public age: number;
  17. public drag: number;
  18. public size: number;
  19. public startSize: number;
  20. public sizeGradientFactor: number;
  21. public scale: xrFrameSystem.Vector2;
  22. public angle: number;
  23. public angularSpeed: number;
  24. public particleSystem: CustomParticle;
  25. public currentSize: number;
  26. public currentSize2: number;
  27. public currentSizeGradient: FactorGradient;
  28. public currentColor: xrFrameSystem.Vector4;
  29. public currentColor2: xrFrameSystem.Vector4;
  30. public currentColorGradient: ColorGradient;
  31. public currentAlpha: number;
  32. public currentAlpha2: number;
  33. public currentAlphaGradient: FactorGradient;
  34. public currentSpeedScale: number;
  35. public currentSpeedScale2: number;
  36. public currentSpeedScaleGradient: FactorGradient;
  37. public currentLimitSpeed: number;
  38. public currentLimitSpeed2: number;
  39. public currentLimitSpeedGradient: FactorGradient;
  40. public currentDrag: number;
  41. public currentDrag2: number;
  42. public currentDragGradient: FactorGradient;
  43. public subEmitterMuster;
  44. public startSpriteCellIndex: number;
  45. public endSpriteCellIndex: number;
  46. public cellIndex: number = 0;
  47. public randomCellOffset;
  48. constructor(particle: CustomParticle) {
  49. this.particleSystem = particle;
  50. this.position = xrFrameSystem.Vector3.createFromNumber(0, 0, 0);
  51. this.direction = xrFrameSystem.Vector3.createFromNumber(0, 0, 0);
  52. this.speed = 0;
  53. this.color = xrFrameSystem.Vector4.createFromNumber(0, 0, 0, 0);
  54. this.colorStep = xrFrameSystem.Vector4.createFromNumber(0, 0, 0, 0);
  55. this.rampPos = xrFrameSystem.Vector4.createFromNumber(0, 0, 0, 1);
  56. this.currentSize = 0;
  57. this.currentSize2 = 0;
  58. this.currentColor = xrFrameSystem.Vector4.createFromNumber(0, 0, 0, 0);
  59. this.currentColor2 = xrFrameSystem.Vector4.createFromNumber(0, 0, 0, 1);
  60. this.currentAlpha = 0;
  61. this.currentAlpha2 = 0;
  62. this.startSpriteCellIndex = 0;
  63. this.endSpriteCellIndex = 0;
  64. this.lifeTime = 1.0;
  65. this.age = 0;
  66. this.drag = 0;
  67. this.size = 1;
  68. this.startSize = 1;
  69. this.sizeGradientFactor = 1;
  70. this.scale = xrFrameSystem.Vector2.createFromNumber(0, 0);
  71. this.angle = 0;
  72. this.angularSpeed = 0;
  73. this.id = ParticleInstance.count++;
  74. }
  75. /**
  76. * 重置粒子实例的状态。
  77. */
  78. public reset() {
  79. this.age = 0;
  80. this.id = ParticleInstance.count++;
  81. this.randomCellOffset = undefined;
  82. this.cellIndex = this.startSpriteCellIndex;
  83. }
  84. /**
  85. * 将当前粒子实例的状态拷贝到目标实例。
  86. * @param {ParticleInstance} other 目标粒子实例
  87. */
  88. public copyTo(other: ParticleInstance) {
  89. other.position.set(this.position);
  90. other.direction.set(this.direction);
  91. other.color.set(this.color);
  92. other.colorStep.set(this.colorStep);
  93. other.rampPos.set(this.rampPos);
  94. other.speed = this.speed;
  95. other.lifeTime = this.lifeTime;
  96. other.size = this.size;
  97. other.scale.set(this.scale);
  98. other.angle = this.angle;
  99. other.angularSpeed = this.angularSpeed;
  100. other.particleSystem = this.particleSystem;
  101. other.id = this.id;
  102. other.age = this.age;
  103. other.subEmitterMuster = this.subEmitterMuster;
  104. // 判断是否使用了颜色渐变
  105. if (this.currentColorGradient) {
  106. other.currentColorGradient = this.currentColorGradient;
  107. other.currentColor = this.currentColor.clone();
  108. other.currentColor2 = this.currentColor2.clone();
  109. }
  110. // 判断是否使用了大小渐变
  111. if (this.currentSizeGradient) {
  112. other.currentSizeGradient = this.currentSizeGradient;
  113. other.currentSize = this.currentSize;
  114. other.currentSize2 = this.currentSize2;
  115. }
  116. // 判断是否使用了透明度渐变
  117. if (this.currentAlphaGradient) {
  118. other.currentAlphaGradient = this.currentAlphaGradient;
  119. other.currentAlpha = this.currentAlpha;
  120. other.currentAlpha2 = this.currentAlpha2;
  121. }
  122. // 判断是否使用了速度渐变
  123. if (this.currentSpeedScaleGradient) {
  124. other.currentSpeedScaleGradient = this.currentSpeedScaleGradient;
  125. other.currentSpeedScale = this.currentSpeedScale;
  126. other.currentSpeedScale2 = this.currentSpeedScale2;
  127. }
  128. // 判断是否使用了限速渐变
  129. if (this.currentLimitSpeedGradient) {
  130. other.currentLimitSpeedGradient = this.currentLimitSpeedGradient;
  131. other.currentLimitSpeed = this.currentLimitSpeed;
  132. other.currentLimitSpeed2 = this.currentLimitSpeed2;
  133. }
  134. // 判断是否使用了阻力渐变
  135. if (this.currentDragGradient) {
  136. other.currentDragGradient = this.currentDragGradient;
  137. other.currentDrag = this.currentDrag;
  138. other.currentDrag2 = this.currentDrag2;
  139. }
  140. // 判断是否使用了动画图集
  141. if (this.particleSystem.useSpriteCellLoop) {
  142. other.randomCellOffset = this.randomCellOffset;
  143. other.startSpriteCellIndex = this.startSpriteCellIndex;
  144. other.endSpriteCellIndex = this.endSpriteCellIndex;
  145. }
  146. }
  147. /**
  148. * 更新从动画图集采样的帧序号
  149. */
  150. public updateCellIndex() {
  151. var offset = this.age;
  152. // 设置图集变化的速度
  153. var changeSpeed = this.particleSystem.spriteChangeSpeed;
  154. if (this.particleSystem.useRandomSpriteCellIndex) {
  155. if (this.randomCellOffset == undefined) {
  156. //随机给图集中的一帧作为初始帧
  157. this.randomCellOffset = Math.random() * this.lifeTime;
  158. }
  159. if (changeSpeed == 0) {
  160. // 如果播放速度为0,则不会播放到下一动画帧
  161. changeSpeed = 1;
  162. offset = this.randomCellOffset;
  163. } else {
  164. offset += this.randomCellOffset;
  165. }
  166. }
  167. var ratio;
  168. var loopDist = this.endSpriteCellIndex - this.startSpriteCellIndex;
  169. if (this.particleSystem.useSpriteCellLoop) {
  170. // 粒子将循环播放动画图集
  171. ratio = this.clamp((offset * changeSpeed) % this.lifeTime / this.lifeTime);
  172. } else {
  173. ratio = this.clamp(offset * changeSpeed / this.lifeTime);
  174. }
  175. //round
  176. this.cellIndex = (this.startSpriteCellIndex + ratio * loopDist) | 0;
  177. }
  178. /**
  179. * 限制num的值在left与right的区间内
  180. */
  181. public clamp(num, left = 0, right = 1) {
  182. if (num < left)
  183. return left;
  184. if (num > right)
  185. return right;
  186. return num;
  187. }
  188. };