Gradient.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //
  2. // Gradient.h
  3. // cocos2d_libs
  4. //
  5. // Created by 徐俊杰 on 2020/5/22.
  6. //
  7. #ifndef Gradient_h
  8. #define Gradient_h
  9. #include "rparticle/Macros/RParticleMacros.h"
  10. #include "rparticle/Math/FloatConversion.h"
  11. //#include "Color.h"
  12. //#include "Runtime/Utilities/LogAssert.h"
  13. NS_RRP_BEGIN
  14. enum
  15. {
  16. kGradientMaxNumKeys = 8,
  17. kOptimizedGradientMaxNumKeys = kGradientMaxNumKeys + kGradientMaxNumKeys, // color keys + alpha keys
  18. };
  19. // Optimized version of gradient
  20. struct OptimizedGradient
  21. {
  22. static inline UInt32 InverseLerpWordOptimized (UInt32 from, UInt32 rcp, UInt32 v)
  23. {
  24. DebugAssert((from & 0xffff) == from);
  25. DebugAssert((v & 0xffff) == v);
  26. return ((v - from) * rcp)>>16;
  27. }
  28. inline ColorRGBA32 Evaluate(float normalizedTime) const
  29. {
  30. DebugAssert((normalizedTime >= 0.0f) && (normalizedTime <= 1.0f));
  31. DebugAssert(keyCount >= 2);
  32. UInt32 time = NormalizedToWord(normalizedTime);
  33. // Color blend
  34. const UInt32 numKeys = keyCount;
  35. time = std::min(std::max((UInt32)times[0], time), (UInt32)times[keyCount-1]); // TODO: Is this necessary?
  36. for (int i = 1; i < numKeys; i++)
  37. {
  38. const UInt32 currTime = times[i];
  39. if(time <= currTime)
  40. {
  41. const UInt32 prevTime = times[i-1];
  42. const UInt32 frac = InverseLerpWordOptimized(prevTime, rcp[i], time);
  43. return Lerp (colors[i-1], colors[i], frac);
  44. }
  45. }
  46. return ColorRGBA32 (0xff,0xff,0xff,0xff);
  47. }
  48. ColorRGBA32 colors[kOptimizedGradientMaxNumKeys];
  49. UInt32 times[kOptimizedGradientMaxNumKeys];
  50. UInt32 rcp[kOptimizedGradientMaxNumKeys]; // precomputed reciprocals
  51. UInt32 keyCount;
  52. };
  53. // Work in progress (Rename NEW to something else when found..)
  54. class GradientNEW
  55. {
  56. public:
  57. GradientNEW ();
  58. ~GradientNEW ();
  59. //TODO: Serialize
  60. // DECLARE_SERIALIZE_NO_PPTR (GradientNEW)
  61. template<class TransferFunction>
  62. void Transfer (TransferFunction& transfer);
  63. ColorRGBA32 Evaluate(float time) const;
  64. struct ColorKey
  65. {
  66. //TODO: Serialize
  67. // DEFINE_GET_TYPESTRING (GradientColorKey)
  68. ColorKey () {}
  69. ColorKey (ColorRGBAf color, float time) {m_Color = color; m_Time = time;}
  70. ColorRGBAf m_Color;
  71. float m_Time;
  72. };
  73. struct AlphaKey
  74. {
  75. //TODO: Serialize
  76. // DEFINE_GET_TYPESTRING (GradientAlphaKey)
  77. AlphaKey () {}
  78. AlphaKey (float alpha, float time) {m_Alpha = alpha; m_Time = time;}
  79. float m_Alpha;
  80. float m_Time;
  81. };
  82. void SetKeys (ColorKey* colorKeys, unsigned numColorKeys, AlphaKey* alphaKeys, unsigned numAlphaKeys);
  83. void SetColorKeys (ColorKey* colorKeys, unsigned numKeys);
  84. void SetAlphaKeys (AlphaKey* alphaKeys, unsigned numKeys);
  85. void SetNumColorKeys (int numColorKeys) { m_NumColorKeys = numColorKeys;};
  86. void SetNumAlphaKeys (int numAlphaKeys) { m_NumAlphaKeys = numAlphaKeys; };
  87. int GetNumColorKeys () const { return m_NumColorKeys; }
  88. int GetNumAlphaKeys () const { return m_NumAlphaKeys; }
  89. ColorRGBA32& GetKey (unsigned index) { return m_Keys[index]; }
  90. const ColorRGBA32& GetKey (unsigned index) const { return m_Keys[index]; }
  91. void SetKey (unsigned index, ColorRGBA32 color) { m_Keys[index] = color; }
  92. UInt16& GetColorTime (unsigned index) { return m_ColorTime[index]; }
  93. const UInt16& GetColorTime (unsigned index) const { return m_ColorTime[index]; }
  94. void SetColorTime (unsigned index, UInt16 colorTime) { m_ColorTime[index] = colorTime; }
  95. UInt16& GetAlphaTime(unsigned index) { return m_AlphaTime[index]; }
  96. const UInt16& GetAlphaTime(unsigned index) const { return m_AlphaTime[index]; }
  97. void SetAlphaTime(unsigned index, UInt16 alphaTime) { m_AlphaTime[index] = alphaTime; }
  98. ColorRGBA32 GetConstantColor () const;
  99. void SetConstantColor (ColorRGBA32 color);
  100. void SwapColorKeys (int i, int j);
  101. void SwapAlphaKeys (int i, int j);
  102. void InitializeOptimized(OptimizedGradient& g);
  103. private:
  104. static inline UInt32 InverseLerpWord (UInt32 from, UInt32 to, UInt32 v)
  105. {
  106. DebugAssert((from & 0xffff) == from);
  107. DebugAssert((to & 0xffff) == to);
  108. DebugAssert((v & 0xffff) == v);
  109. DebugAssert (from <= to);
  110. UInt32 nom = (v - from) << 16;
  111. UInt32 den = std::max<UInt32>(to - from, 1);
  112. UInt32 res = nom / den;
  113. return res;
  114. }
  115. static inline UInt32 LerpByte(UInt32 u0, UInt32 u1, UInt32 scale)
  116. {
  117. DebugAssert((u0 & 0xff) == u0);
  118. DebugAssert((u1 & 0xff) == u1);
  119. //DebugAssert((scale & 0xff) == scale);
  120. return u0 + (((u1 - u0) * scale) >> 8) & 0xff;
  121. }
  122. void ValidateColorKeys();
  123. void ValidateAlphaKeys();
  124. ColorRGBA32 m_Keys[kGradientMaxNumKeys];
  125. UInt16 m_ColorTime[kGradientMaxNumKeys];
  126. UInt16 m_AlphaTime[kGradientMaxNumKeys];
  127. UInt8 m_NumColorKeys;
  128. UInt8 m_NumAlphaKeys;
  129. };
  130. inline ColorRGBA32 GradientNEW::Evaluate(float normalizedTime) const
  131. {
  132. DebugAssert((normalizedTime >= 0.0f) && (normalizedTime <= 1.0f));
  133. DebugAssert(m_NumColorKeys >= 2);
  134. DebugAssert(m_NumAlphaKeys >= 2);
  135. ColorRGBA32 color = ColorRGBA32 (0xff,0xff,0xff,0xff);
  136. const UInt32 time = NormalizedToWord(normalizedTime);
  137. // Color blend
  138. const UInt32 numColorKeys = m_NumColorKeys;
  139. const UInt32 timeColor = std::min(std::max((UInt32)m_ColorTime[0], time), (UInt32)m_ColorTime[numColorKeys-1]);
  140. for (int i = 1; i < numColorKeys; i++)
  141. {
  142. const UInt32 currTime = m_ColorTime[i];
  143. if(timeColor <= currTime)
  144. {
  145. const UInt32 prevTime = m_ColorTime[i-1];
  146. const UInt32 frac = InverseLerpWord(prevTime, currTime, timeColor) >> 8; // frac is byte
  147. color = Lerp (m_Keys[i-1], m_Keys[i], frac);
  148. break;
  149. }
  150. }
  151. // Alpha blend
  152. const UInt32 numAlphaKeys = m_NumAlphaKeys;
  153. const UInt32 timeAlpha = std::min(std::max((UInt32)m_AlphaTime[0], time), (UInt32)m_AlphaTime[numAlphaKeys-1]);
  154. for (int i = 1; i < numAlphaKeys; i++)
  155. {
  156. const UInt32 currTime = m_AlphaTime[i];
  157. if(timeAlpha <= currTime)
  158. {
  159. const UInt32 prevTime = m_AlphaTime[i-1];
  160. const UInt32 frac = InverseLerpWord(prevTime, currTime, timeAlpha) >> 8; // frac is byte
  161. color.a = LerpByte(m_Keys[i-1].a, m_Keys[i].a, frac);
  162. break;
  163. }
  164. }
  165. return color;
  166. }
  167. /// Simple class to interpolate between colors.
  168. template<int size>
  169. class GradientDeprecated
  170. {
  171. public:
  172. //TODO: Serialize
  173. /*
  174. DEFINE_GET_TYPESTRING (Gradient)
  175. */
  176. template<class TransferFunc>
  177. void Transfer (TransferFunc& transfer) {
  178. AssertIf (size > 9);
  179. char name[] = "m_Color[ ]";
  180. for (int i=0;i<size;i++)
  181. {
  182. name[8] = '0' + i;
  183. transfer.Transfer (m_Colors[i], name);
  184. }
  185. }
  186. /// Get a color
  187. ColorRGBA32 &operator[] (int i) { AssertIf (i < 0 || i >= size); return m_Colors[i]; }
  188. /// Get a color
  189. const ColorRGBA32 &operator[] (int i) const { AssertIf (i < 0 || i >= size); return m_Colors[i]; }
  190. /// Get the color value at a given position
  191. /// @param position a position in unnormalized 16.16 bit fixed
  192. ColorRGBA32 GetFixed (UInt32 position) const {
  193. AssertIf ((position >> 16) >= size - 1);
  194. return Lerp (m_Colors[position >> 16], m_Colors[(position >> 16) + 1], (position >> 8) & 255);
  195. }
  196. private:
  197. /// The array of colors this interpolator works through
  198. ColorRGBA32 m_Colors[size];
  199. };
  200. NS_RRP_END
  201. #endif /* Gradient_h */