Gradient.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. const xrFrameSystem = wx.getXrFrameSystem();
  2. // 控制粒子颜色渐变
  3. export class ColorGradient {
  4. public gradient: number;
  5. public color: xrFrameSystem.Vector4;
  6. public color2: xrFrameSystem.Vector4;
  7. constructor(gradient, color, color2) {
  8. this.gradient = gradient;
  9. this.color = color;
  10. this.color2 = color2;
  11. }
  12. /**
  13. * 获取具体颜色属性值
  14. * @param {Vector4} 用于存储结果的临时变量
  15. */
  16. public getColor(colorTemp: xrFrameSystem.Vector4) {
  17. if (!this.color2) {
  18. colorTemp.x = this.color.x;
  19. colorTemp.y = this.color.y;
  20. colorTemp.z = this.color.z;
  21. colorTemp.w = this.color.w;
  22. return;
  23. }
  24. var lerp = Math.random();
  25. colorTemp.x = this.color.x + (this.color2.x - this.color.x) * lerp;
  26. colorTemp.y = this.color.y + (this.color2.y - this.color.y) * lerp;
  27. colorTemp.z = this.color.z + (this.color2.z - this.color.z) * lerp;
  28. colorTemp.w = this.color.w + (this.color2.w - this.color.w) * lerp;
  29. }
  30. }
  31. export class Color3Gradient {
  32. public gradient: number;
  33. public color: xrFrameSystem.Vector3;
  34. constructor(gradient, color) {
  35. this.gradient = gradient;
  36. this.color = color;
  37. }
  38. }
  39. // 控制粒子变化的属性
  40. export class FactorGradient {
  41. public gradient: number;
  42. public factor: number;
  43. public factor2: number;
  44. constructor(gradient, factor, factor2) {
  45. this.gradient = gradient;
  46. this.factor = factor;
  47. this.factor2 = factor2;
  48. }
  49. /**
  50. * 获取具体属性值
  51. * @return {number} 插值后的属性大小
  52. */
  53. public getFactor() {
  54. if (!this.factor2 || this.factor2 == this.factor) {
  55. return this.factor;
  56. }
  57. return this.factor + (this.factor2 - this.factor) * Math.random();
  58. }
  59. }
  60. export class BasicGradientMethod {
  61. /**
  62. * 从获取具体时刻的属性大小
  63. * @param {number} ratio 粒子所处生命周期的阶段
  64. * @param {Array} gradients 存储不同时刻指定属性变化的数组
  65. * @param {Callback} updateFunc 回调函数
  66. */
  67. public static GetCurrentGradient(ratio, gradients, updateFunc) {
  68. if (gradients[0].graident > ratio) {
  69. updateFunc(gradients[0], gradients[0], 1.0)
  70. return;
  71. }
  72. var lastIndex = gradients.length - 1;
  73. for (var index = 0; index < lastIndex; index++) {
  74. var currentGradient = gradients[index];
  75. var nextGradient = gradients[index + 1];
  76. if (ratio >= currentGradient.gradient && ratio <= nextGradient.gradient) {
  77. var lerp = (ratio - currentGradient.gradient) / (nextGradient.gradient - currentGradient.gradient);
  78. updateFunc(currentGradient, nextGradient, lerp);
  79. return;
  80. }
  81. }
  82. updateFunc(gradients[lastIndex], gradients[lastIndex], 1.0);
  83. }
  84. }