SVGRadialGradientBrush.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SVGRadialGradientBrush {
  5. private SVGRadialGradientElement _radialGradElement;
  6. private float _cx, _cy, _r, _fx, _fy;
  7. private List<Color> _stopColorList;
  8. private List<float> _stopOffsetList;
  9. private SVGSpreadMethod _spreadMethod;
  10. public SVGRadialGradientBrush(SVGRadialGradientElement radialGradElement) {
  11. _radialGradElement = radialGradElement;
  12. Initialize();
  13. }
  14. public SVGRadialGradientBrush(SVGRadialGradientElement radialGradElement, SVGGraphicsPath graphicsPath) {
  15. _radialGradElement = radialGradElement;
  16. Initialize();
  17. SetGradientVector(graphicsPath);
  18. }
  19. private void Initialize() {
  20. _cx = _radialGradElement.cx.value;
  21. _cy = _radialGradElement.cy.value;
  22. _r = _radialGradElement.r.value;
  23. _fx = _radialGradElement.fx.value;
  24. _fy = _radialGradElement.fy.value;
  25. _stopColorList = new List<Color>();
  26. _stopOffsetList = new List<float>();
  27. _spreadMethod = _radialGradElement.spreadMethod;
  28. GetStopList();
  29. FixF();
  30. _vitriOffset = 0;
  31. PreColorProcess(_vitriOffset);
  32. }
  33. //Sap xep lai Offset va Stop-color List
  34. private void GetStopList() {
  35. List<SVGStopElement> _stopList = _radialGradElement.stopList;
  36. int _length = _stopList.Count;
  37. if(_length == 0)
  38. return;
  39. _stopColorList.Add(_stopList[0].stopColor.color);
  40. _stopOffsetList.Add(0f);
  41. int i = 0;
  42. for(i = 0; i < _length; i++) {
  43. float t_offset = _stopList[i].offset;
  44. if((t_offset > _stopOffsetList[_stopOffsetList.Count - 1]) && (t_offset <= 100f)) {
  45. _stopColorList.Add(_stopList[i].stopColor.color);
  46. _stopOffsetList.Add(t_offset);
  47. } else if(t_offset == _stopOffsetList[_stopOffsetList.Count - 1])
  48. _stopColorList[_stopOffsetList.Count - 1] = _stopList[i].stopColor.color;
  49. }
  50. if(_stopOffsetList[_stopOffsetList.Count - 1] != 100f) {
  51. _stopColorList.Add(_stopColorList[_stopOffsetList.Count - 1]);
  52. _stopOffsetList.Add(100f);
  53. }
  54. }
  55. //Sua lai vi tri cua diem x,y
  56. private void FixF() {
  57. // TODO: Measure performance of comparing squared values instead of taking square root.
  58. if((float)Math.Sqrt((_fx - _cx) * (_fx - _cx)) + ((_fy - _cy) * (_fy - _cy)) > _r) {
  59. float dx = _fx - _cx;
  60. float dy = _fy - _cy;
  61. if(dx == 0)
  62. _fy = (_fy > _cy) ? (_cy + _r) : (_cy - _r);
  63. else {
  64. float a, b;
  65. a = dy / dx;
  66. b = _fy - a * _fx;
  67. double ta, tb, tc;
  68. ta = 1 + a * a;
  69. tb = 2 * (a * (b - _cy) - _cx);
  70. tc = (_cx * _cx) + (b - _cy) * (b - _cy) - (_r * _r);
  71. float delta = (float)((tb * tb) - 4 * ta * tc);
  72. delta = (float)Math.Sqrt(delta);
  73. float x1 = (float)((-tb + delta) / (2 * ta));
  74. float y1 = (float)(a * x1 + b);
  75. float x2 = (float)((-tb - delta) / (2 * ta));
  76. float y2 = (float)(a * x2 + b);
  77. if((_cx < x1) && (x1 < _fx)) {
  78. _fx = x1 - 1;
  79. _fy = y1;
  80. } else {
  81. _fx = x2 + 1;
  82. _fy = y2;
  83. }
  84. }
  85. }
  86. }
  87. private float _deltaR, _deltaG, _deltaB;
  88. private int _vitriOffset = 0;
  89. private void PreColorProcess(int index) {
  90. float dp = _stopOffsetList[index + 1] - _stopOffsetList[index];
  91. _deltaR = (_stopColorList[index + 1].r - _stopColorList[index].r) / dp;
  92. _deltaG = (_stopColorList[index + 1].g - _stopColorList[index].g) / dp;
  93. _deltaB = (_stopColorList[index + 1].b - _stopColorList[index].b) / dp;
  94. }
  95. //Tim giao diem giua duong thang(x,y)->(fx, fy)voi duong tron
  96. private Vector2 CrossPoint(float x, float y) {
  97. Vector2 _point = new Vector2(0f, 0f);
  98. float dx = _fx - x;
  99. float dy = _fy - y;
  100. if(dx == 0) {
  101. _point.x = _fx;
  102. _point.y = (_fy > y) ? (_fy - _r) : (_fy + _r);
  103. } else {
  104. float a, b;
  105. a = dy / dx;
  106. b = _fy - a * _fx;
  107. double ta, tb, tc;
  108. ta = 1 + a * a;
  109. tb = 2 * (a * (b - _cy) - _cx);
  110. tc = (_cx * _cx) + (b - _cy) * (b - _cy) - (_r * _r);
  111. float delta = (float)((tb * tb) - 4 * ta * tc);
  112. delta = (float)Math.Sqrt(delta);
  113. float x1 = (float)((-tb + delta) / (2 * ta));
  114. float y1 = (float)(a * x1 + b);
  115. float x2 = (float)((-tb - delta) / (2 * ta));
  116. float y2 = (float)(a * x2 + b);
  117. Vector2 vt1 = new Vector2(x1 - _fx, y1 - _fy);
  118. Vector2 vt2 = new Vector2(x - _fx, y - _fy);
  119. if(((vt1.x * vt2.x) >= 0) && ((vt1.y * vt2.y) >= 0)) {
  120. _point.x = x1;
  121. _point.y = y1;
  122. } else {
  123. _point.x = x2;
  124. _point.y = y2;
  125. }
  126. }
  127. return _point;
  128. }
  129. //Tinh % tai vi tri x,y
  130. private float Percent(float x, float y) {
  131. Vector2 _cP = CrossPoint(x, y);
  132. //float d1 = (float)Math.Sqrt((_cP.x - x)*(_cP.x - x)+(_cP.y - y)*(_cP.y - y));
  133. float d2 = (float)Math.Sqrt((_fx - x) * (_fx - x) + (_fy - y) * (_fy - y));
  134. float dd = (float)Math.Sqrt((_cP.x - _fx) * (_cP.x - _fx) + (_cP.y - _fy) * (_cP.y - _fy));
  135. //0 giua, 1 ngoai
  136. int vt = 0;
  137. if(d2 > dd)
  138. vt = 1;
  139. int _reflectTimes;
  140. float _remainder;
  141. switch(_spreadMethod) {
  142. case SVGSpreadMethod.Pad:
  143. if(vt == 1)
  144. return 100f;
  145. return (d2 / dd * 100f);
  146. case SVGSpreadMethod.Reflect:
  147. _reflectTimes = (int)(d2 / dd);
  148. _remainder = d2 - (dd * (float)_reflectTimes);
  149. int _od = (int)(_reflectTimes) % 2;
  150. return ((100f * _od) + (1 - 2 * _od) * (_remainder / dd * 100f));
  151. case SVGSpreadMethod.Repeat:
  152. _reflectTimes = (int)(d2 / dd);
  153. _remainder = d2 - (dd * (float)_reflectTimes);
  154. return (_remainder / dd * 100f);
  155. }
  156. return 100f;
  157. }
  158. private void SetGradientVector(SVGGraphicsPath graphicsPath) {
  159. Rect bound = graphicsPath.GetBound();
  160. if(_radialGradElement.cx.unitType == SVGLengthType.Percentage)
  161. _cx = bound.x + (bound.width * _cx / 100f);
  162. if(_radialGradElement.cy.unitType == SVGLengthType.Percentage)
  163. _cy = bound.y + (bound.height * _cy / 100f);
  164. if(_radialGradElement.r.unitType == SVGLengthType.Percentage) {
  165. Vector2 _p1 = new Vector2(bound.x, bound.y);
  166. Vector2 _p2 = new Vector2(bound.x + bound.width, bound.y + bound.height);
  167. _p1 = graphicsPath.matrixTransform.Transform(_p1);
  168. _p2 = graphicsPath.matrixTransform.Transform(_p2);
  169. float dd = (float)Math.Sqrt((_p2.x - _p1.x) * (_p2.x - _p1.x) + (_p2.y - _p1.y) * (_p2.y - _p1.y));
  170. _r = (dd * _r / 100f);
  171. }
  172. if(_radialGradElement.fx.unitType == SVGLengthType.Percentage)
  173. _fx = bound.x + (bound.width * _fx / 100f);
  174. if(_radialGradElement.fy.unitType == SVGLengthType.Percentage)
  175. _fy = bound.y + (bound.height * _fy / 100f);
  176. if((float)Math.Sqrt((_cx - _fx) * (_cx - _fx) + (_cy - _fy) * (_cy - _fy)) > _r) {
  177. Vector2 _cP = CrossPoint(_cx, _cy);
  178. _fx = _cP.x;
  179. _fy = _cP.y;
  180. }
  181. if(_radialGradElement.gradientUnits == SVGGradientUnit.ObjectBoundingBox) {
  182. Vector2 _point = new Vector2(_cx, _cy);
  183. _point = graphicsPath.matrixTransform.Transform(_point);
  184. _cx = _point.x;
  185. _cy = _point.y;
  186. _point = new Vector2(_fx, _fy);
  187. _point = graphicsPath.matrixTransform.Transform(_point);
  188. _fx = _point.x;
  189. _fy = _point.y;
  190. }
  191. }
  192. public Color GetColor(float x, float y) {
  193. Color _color = Color.black;
  194. //if((_ox != x)&&(y == 200)) {
  195. // _ox = x;
  196. // _dem ++ ;
  197. // if(_dem < 300) {
  198. // _show = true;
  199. // }
  200. //}
  201. float _percent = Percent(x, y);
  202. //if(_show == true) {
  203. // UnityEngine.Debug.Log("x " + x + " y " + y + " percent " + _percent);
  204. //}
  205. if((_stopOffsetList[_vitriOffset] <= _percent) && (_percent <= _stopOffsetList[_vitriOffset + 1])) {
  206. _color.r = ((_percent - _stopOffsetList[_vitriOffset]) * _deltaR) + _stopColorList[_vitriOffset].r;
  207. _color.g = ((_percent - _stopOffsetList[_vitriOffset]) * _deltaG) + _stopColorList[_vitriOffset].g;
  208. _color.b = ((_percent - _stopOffsetList[_vitriOffset]) * _deltaB) + _stopColorList[_vitriOffset].b;
  209. } else {
  210. for(int i = 0; i < _stopOffsetList.Count - 1; i++)
  211. if((_stopOffsetList[i] <= _percent) && (_percent <= _stopOffsetList[i + 1])) {
  212. _vitriOffset = i;
  213. PreColorProcess(_vitriOffset);
  214. _color.r = ((_percent - _stopOffsetList[i]) * _deltaR) + _stopColorList[i].r;
  215. _color.g = ((_percent - _stopOffsetList[i]) * _deltaG) + _stopColorList[i].g;
  216. _color.b = ((_percent - _stopOffsetList[i]) * _deltaB) + _stopColorList[i].b;
  217. break;
  218. }
  219. }
  220. //_show = false;
  221. return _color;
  222. }
  223. }