MPImage.shader 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. Shader "MPUI/Procedural Image"
  2. {
  3. Properties
  4. {
  5. [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" { }
  6. _Color ("Tint", Color) = (1, 1, 1, 1)
  7. _TextureSize ("Texture Size", Vector) = (1, 1, 1, 1)
  8. _DrawShape ("Draw Shape", int) = 2
  9. _StrokeWidth ("Stroke Width", float) = 0
  10. _FalloffDistance ("Falloff Distance", float) = 0.5
  11. _PixelWorldScale ("Pixel world scale", Range(0.01, 5)) = 1
  12. _ShapeRotation ("shape Rotation", float) = 0
  13. _ConstrainRotation("Constrained Rotation", int) = 0
  14. _FlipHorizontal ("Flip Horizontal", int) = 0
  15. _FlipVertical ("Flip Vertical", int) = 0
  16. _RectangleCornerRadius ("Rectangle Corner Radius", Vector) = (0, 0, 0, 0)
  17. _CircleRadius ("Circle Radius", float) = 0
  18. _CircleFitRadius ("Fit Circle Radius", float) = 0
  19. _PentagonCornerRadius ("Pentagon Corner Radius", Vector) = (0, 0, 0, 0)
  20. _PentagonTipRadius ("Pentagon Triangle Radius", float) = 0
  21. _PentagonTipSize ("Pentagon Triangle Size", float) = 0
  22. _TriangleCornerRadius ("Triangle Radius", Vector) = (0, 0, 0, 0)
  23. _HexagonTipSize ("Hexagon Tip Size", Vector) = (0, 0, 0, 0)
  24. _HexagonTipRadius ("Hexagon Tip Radius", Vector) = (0, 0, 0, 0)
  25. _HexagonCornerRadius ("Hexagon Corner Radius", Vector) = (0, 0, 0, 0)
  26. _NStarPolygonSideCount ("NStar Polygon Side Count", float) = 3
  27. _NStarPolygonInset ("Nstar Polygon Inset", float) = 2
  28. _NStarPolygonCornerRadius ("Nstar Polygon Corner Radius", float) = 0
  29. _NStarPolygonOffset ("Nstar Polygon Offset", Vector) = (0, 0, 0, 0)
  30. _EnableGradient ("Enable GradientEffect", int) = 0
  31. _GradientType ("GradientEffect Type", int) = 0
  32. _GradientInterpolationType ("GradientEffect Interpolation Type", int) = 0
  33. _GradientRotation ("_GradientRotation", float) = 0
  34. _GradientColor0 ("GradientColor0", Vector) = (0, 0, 0, 0)
  35. _GradientColor1 ("GradientColor1", Vector) = (1, 1, 1, 1)
  36. _GradientColor2 ("GradientColor2", Vector) = (0, 0, 0, 0)
  37. _GradientColor3 ("GradientColor3", Vector) = (0, 0, 0, 0)
  38. _GradientColor4 ("GradientColor4", Vector) = (0, 0, 0, 0)
  39. _GradientColor5 ("GradientColor5", Vector) = (0, 0, 0, 0)
  40. _GradientColor6 ("GradientColor6", Vector) = (0, 0, 0, 0)
  41. _GradientColor7 ("GradientColor7", Vector) = (0, 0, 0, 0)
  42. _GradientColorLength ("GradientColorLength", int) = 0
  43. _GradientAlpha0 ("GradientAlpha0", Vector) = (1, 0, 0, 0)
  44. _GradientAlpha1 ("GradientAlpha1", Vector) = (1, 1, 0, 0)
  45. _GradientAlpha2 ("GradientAlpha2", Vector) = (0, 0, 0, 0)
  46. _GradientAlpha3 ("GradientAlpha3", Vector) = (0, 0, 0, 0)
  47. _GradientAlpha4 ("GradientAlpha4", Vector) = (0, 0, 0, 0)
  48. _GradientAlpha5 ("GradientAlpha5", Vector) = (0, 0, 0, 0)
  49. _GradientAlpha6 ("GradientAlpha6", Vector) = (0, 0, 0, 0)
  50. _GradientAlpha7 ("GradientAlpha7", Vector) = (0, 0, 0, 0)
  51. _GradientAlphaLength ("GradientAlphaLength", int) = 0
  52. _CornerGradientColor0 ("CornerGradientColor0", Color) = (1, 0, 0, 1)
  53. _CornerGradientColor1 ("CornerGradientColor1", Color) = (0, 1, 0, 1)
  54. _CornerGradientColor2 ("CornerGradientColor2", Color) = (0, 0, 1, 1)
  55. _CornerGradientColor3 ("CornerGradientColor3", Color) = (0, 0, 0, 1)
  56. _OutlineWidth ("Outline Width", float) = 0
  57. _OutlineColor ("Outline Color", Color) = (0, 0, 0, 1)
  58. _StencilComp ("Stencil Comparison", Float) = 8
  59. _Stencil ("Stencil ID", Float) = 0
  60. _StencilOp ("Stencil Operation", Float) = 0
  61. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  62. _StencilReadMask ("Stencil Read Mask", Float) = 255
  63. _ColorMask ("Color Mask", Float) = 15
  64. /* //SOFTMASK_HANDLE_START
  65. [PerRendererData] _SoftMask ("Mask", 2D) = "white" {}
  66. */ //SOFTMASK_HANDLE_END
  67. [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
  68. }
  69. SubShader
  70. {
  71. Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "PreviewType" = "Plane" "CanUseSpriteAtlas" = "True" }
  72. Stencil
  73. {
  74. Ref [_Stencil]
  75. Comp [_StencilComp]
  76. Pass [_StencilOp]
  77. ReadMask [_StencilReadMask]
  78. WriteMask [_StencilWriteMask]
  79. }
  80. Cull Off
  81. Lighting Off
  82. ZWrite Off
  83. ZTest [unity_GUIZTestMode]
  84. Blend SrcAlpha OneMinusSrcAlpha
  85. ColorMask [_ColorMask]
  86. Pass
  87. {
  88. Name "Default"
  89. CGPROGRAM
  90. #pragma vertex vert
  91. #pragma fragment frag
  92. #include "UnityCG.cginc"
  93. #include "UnityUI.cginc"
  94. #include "2D_SDF.cginc"
  95. /* //SOFTMASK_HANDLE_START
  96. #include "Assets/SoftMask/Shaders/SoftMask.cginc" //SOFTMASK_INCLUDE_HANDLE
  97. */ //SOFTMASK_HANDLE_END
  98. #pragma multi_compile_local _ UNITY_UI_CLIP_RECT
  99. #pragma multi_compile_local _ UNITY_UI_ALPHACLIP
  100. #pragma multi_compile_local _ CIRCLE TRIANGLE RECTANGLE PENTAGON HEXAGON NSTAR_POLYGON
  101. #pragma multi_compile_local _ STROKE OUTLINED OUTLINED_STROKE
  102. #pragma multi_compile_local _ GRADIENT_LINEAR GRADIENT_RADIAL GRADIENT_CORNER
  103. /* //SOFTMASK_HANDLE_START
  104. #pragma multi_compile _ SOFTMASK_SIMPLE
  105. */ //SOFTMASK_HANDLE_END
  106. struct appdata_t
  107. {
  108. float4 vertex: POSITION;
  109. float4 color: COLOR;
  110. float2 texcoord: TEXCOORD0;
  111. float2 uv1: TEXCOORD1;
  112. float2 size: TEXCOORD2;
  113. UNITY_VERTEX_INPUT_INSTANCE_ID
  114. };
  115. struct v2f
  116. {
  117. float4 vertex: SV_POSITION;
  118. fixed4 color: COLOR;
  119. float2 texcoord: TEXCOORD0;
  120. float4 shapeData: TEXCOORD1;
  121. float2 effectsUv: TEXCOORD2;
  122. float4 worldPosition : TEXCOORD3;
  123. /* //SOFTMASK_HANDLE_START
  124. SOFTMASK_COORDS(4)
  125. */ //SOFTMASK_HANDLE_END
  126. UNITY_VERTEX_OUTPUT_STEREO
  127. };
  128. sampler2D _MainTex; float4 _MainTex_ST;
  129. fixed4 _Color;
  130. fixed4 _TextureSize;
  131. fixed4 _TextureSampleAdd;
  132. float4 _ClipRect;
  133. half _PixelWorldScale;
  134. half _StrokeWidth;
  135. half _OutlineWidth;
  136. half4 _OutlineColor;
  137. half _FalloffDistance;
  138. half _ShapeRotation;
  139. half _ConstrainRotation;
  140. half _FlipHorizontal;
  141. half _FlipVertical;
  142. #if RECTANGLE
  143. float4 _RectangleCornerRadius;
  144. #endif
  145. #if CIRCLE
  146. float _CircleRadius;
  147. float _CircleFitRadius;
  148. #endif
  149. #if PENTAGON
  150. float4 _PentagonCornerRadius;
  151. float _PentagonTipRadius;
  152. float _PentagonTipSize;
  153. #endif
  154. #if TRIANGLE
  155. float3 _TriangleCornerRadius;
  156. #endif
  157. #if HEXAGON
  158. half2 _HexagonTipSize;
  159. half2 _HexagonTipRadius;
  160. half4 _HexagonCornerRadius;
  161. #endif
  162. #if NSTAR_POLYGON
  163. float _NStarPolygonSideCount;
  164. float _NStarPolygonCornerRadius;
  165. float _NStarPolygonInset;
  166. float2 _NStarPolygonOffset;
  167. #endif
  168. #if GRADIENT_LINEAR || GRADIENT_RADIAL
  169. half4 colors[8];
  170. half4 alphas[8];
  171. half _GradientInterpolationType;
  172. half _GradientColorLength;
  173. half _GradientAlphaLength;
  174. half _GradientRotation;
  175. half4 _GradientColor0;
  176. half4 _GradientColor1;
  177. half4 _GradientColor2;
  178. half4 _GradientColor3;
  179. half4 _GradientColor4;
  180. half4 _GradientColor5;
  181. half4 _GradientColor6;
  182. half4 _GradientColor7;
  183. half4 _GradientAlpha0;
  184. half4 _GradientAlpha1;
  185. half4 _GradientAlpha2;
  186. half4 _GradientAlpha3;
  187. half4 _GradientAlpha4;
  188. half4 _GradientAlpha5;
  189. half4 _GradientAlpha6;
  190. half4 _GradientAlpha7;
  191. #endif
  192. #if GRADIENT_CORNER
  193. half4 _CornerGradientColor0;
  194. half4 _CornerGradientColor1;
  195. half4 _CornerGradientColor2;
  196. half4 _CornerGradientColor3;
  197. #endif
  198. #if GRADIENT_LINEAR || GRADIENT_RADIAL
  199. float4 SampleGradient(float Time)
  200. {
  201. float3 color = colors[0].rgb;
  202. [unroll]
  203. for (int c = 1; c < 8; c ++)
  204. {
  205. float colorPos = saturate((Time - colors[c - 1].w) / (colors[c].w - colors[c - 1].w)) * step(c, _GradientColorLength - 1);
  206. color = lerp(color, colors[c].rgb, lerp(colorPos, step(0.01, colorPos), _GradientInterpolationType));
  207. }
  208. float alpha = alphas[0].x;
  209. [unroll]
  210. for (int a = 1; a < 8; a ++)
  211. {
  212. float alphaPos = saturate((Time - alphas[a - 1].y) / (alphas[a].y - alphas[a - 1].y)) * step(a, _GradientAlphaLength - 1);
  213. alpha = lerp(alpha, alphas[a].x, lerp(alphaPos, step(0.01, alphaPos), _GradientInterpolationType));
  214. }
  215. return float4(color, alpha);
  216. }
  217. #endif
  218. #if RECTANGLE
  219. half rectangleScene(float4 _additionalData)
  220. {
  221. float2 _texcoord = _additionalData.xy;
  222. float2 _size = float2(_additionalData.z, _additionalData.w);
  223. float4 radius = _RectangleCornerRadius;
  224. half4 c = half4(_texcoord, _size - _texcoord);
  225. half rect = min(min(min(c.x, c.y), c.z), c.w);
  226. bool4 cornerRects;
  227. cornerRects.x = _texcoord.x < radius.x && _texcoord.y < radius.x;
  228. cornerRects.y = _texcoord.x > _size.x - radius.y && _texcoord.y < radius.y;
  229. cornerRects.z = _texcoord.x > _size.x - radius.z && _texcoord.y > _size.y - radius.z;
  230. cornerRects.w = _texcoord.x < radius.w && _texcoord.y > _size.y - radius.w;
  231. half cornerMask = any(cornerRects);
  232. half4 cornerCircles;
  233. cornerCircles.x = radius.x - length(_texcoord - radius.xx);
  234. cornerCircles.y = radius.y - length(_texcoord - half2(_size.x - radius.y, radius.y));
  235. cornerCircles.z = radius.z - length(_texcoord - (half2(_size.x, _size.y) - radius.zz));
  236. cornerCircles.w = radius.w - length(_texcoord - half2(radius.w, _size.y - radius.w));
  237. cornerCircles = min(max(cornerCircles, 0) * cornerRects, rect);
  238. half corners = max(max(max(cornerCircles.x, cornerCircles.y), cornerCircles.z), cornerCircles.w);
  239. corners = max(corners, 0.0) * cornerMask;
  240. return rect*(cornerMask-1) - corners;
  241. /*
  242. float2 _texcoord = _additionalData.xy;
  243. float2 _size = float2(_additionalData.z, _additionalData.w);
  244. float4 radius = _RectangleCornerRadius;
  245. half rect = rectanlge(_texcoord - half2(_size.x / 2.0, _size.y / 2.0), _size.x, _size.y);
  246. half cornerCircle = circle(_texcoord - radius.xx, radius.x);
  247. rect = _texcoord.x < radius.x && _texcoord.y < radius.x ? cornerCircle: rect;
  248. cornerCircle = circle(_texcoord - half2(_size.x - radius.y, radius.y), radius.y);
  249. rect = _texcoord.x > _size.x - radius.y && _texcoord.y < radius.y ? cornerCircle: rect;
  250. cornerCircle = circle(_texcoord - (half2(_size.x, _size.y) - radius.zz), radius.z);
  251. rect = _texcoord.x > _size.x - radius.z && _texcoord.y > _size.y - radius.z ? cornerCircle: rect;
  252. cornerCircle = circle(_texcoord - half2(radius.w, _size.y - radius.w), radius.w);
  253. rect = _texcoord.x < radius.w && _texcoord.y > _size.y - radius.w ? cornerCircle: rect;
  254. return rect;
  255. */
  256. }
  257. #endif
  258. #if CIRCLE
  259. float circleScene(float4 _additionalData)
  260. {
  261. float2 _texcoord = _additionalData.xy;
  262. float2 _size = float2(_additionalData.z, _additionalData.w);
  263. float width = _size.x;
  264. float height = _size.y;
  265. float radius = lerp(_CircleRadius, min(width, height) / 2.0, _CircleFitRadius);
  266. half sdf = circle(_texcoord - float2(width / 2.0, height / 2.0), radius);
  267. return sdf;
  268. }
  269. #endif
  270. #if TRIANGLE
  271. half triangleScene(float4 _additionalData)
  272. {
  273. float2 _texcoord = _additionalData.xy;
  274. float2 _size = float2(_additionalData.z, _additionalData.w);
  275. float width = _size.x;//_additionalData.z;
  276. float height = _size.y;//_additionalData.w;
  277. half sdf = sdTriangleIsosceles(_texcoord - half2(width / 2.0, height), half2(width / 2.0, -height));
  278. //return sdf;
  279. _TriangleCornerRadius = max(_TriangleCornerRadius, half3(0.001, 0.001, 0.001));
  280. // Left Corner
  281. half halfWidth = width / 2.0;
  282. half m = height / halfWidth;
  283. half d = sqrt(1.0 + m * m);
  284. half c = 0.0;
  285. half k = -_TriangleCornerRadius.x * d + c;
  286. half x = (_TriangleCornerRadius.x - k) / m;
  287. half2 circlePivot = half2(x, _TriangleCornerRadius.x);
  288. half cornerCircle = circle(_texcoord - circlePivot, _TriangleCornerRadius.x);
  289. //sdf = sdfDifference(sdf, cornerCircle);
  290. //return sdf;
  291. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m);
  292. half y = m * x + c;
  293. half fy = map(_texcoord.x, x, circlePivot.x, y, circlePivot.y);
  294. sdf = _texcoord.y < fy && _texcoord.x < circlePivot.x ? cornerCircle: sdf;
  295. //return sdf;
  296. // Right Corner
  297. m = -m; c = 2.0 * height;
  298. k = -_TriangleCornerRadius.y * d + c;
  299. x = (_TriangleCornerRadius.y - k) / m;
  300. circlePivot = half2(x, _TriangleCornerRadius.y);
  301. cornerCircle = circle(_texcoord - circlePivot, _TriangleCornerRadius.y);
  302. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  303. fy = map(_texcoord.x, circlePivot.x, x, circlePivot.y, y);
  304. sdf = _texcoord.x > circlePivot.x && _texcoord.y < fy ? cornerCircle: sdf;
  305. //Top Corner
  306. k = -_TriangleCornerRadius.z * sqrt(1.0 + m * m) + c;
  307. y = m * (width / 2.0) + k;
  308. circlePivot = half2(halfWidth, y);
  309. cornerCircle = circle(_texcoord - circlePivot, _TriangleCornerRadius.z);
  310. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  311. fy = map(_texcoord.x, width - x, x, -1.0, 1.0);
  312. fy = lerp(circlePivot.y, y, abs(fy));
  313. sdf = _texcoord.y > fy ? cornerCircle: sdf;
  314. return sdf;
  315. }
  316. #endif
  317. #if PENTAGON
  318. half pentagonScene(float4 _additionalData)
  319. {
  320. float2 _texcoord = _additionalData.xy;
  321. float2 _size = float2(_additionalData.z, _additionalData.w);
  322. float width = _size.x;
  323. float height = _size.y;
  324. // solid pentagon
  325. half baseRect = rectanlge(_texcoord - half2(width / 2.0, height / 2.0), width, height);
  326. half scale = height / _PentagonTipSize;
  327. half rhombus = sdRhombus(_texcoord - float2(width / 2, _PentagonTipSize * scale), float2(width / 2, _PentagonTipSize) * scale);
  328. half sdfPentagon = sdfDifference(baseRect, sdfDifference(baseRect, rhombus));
  329. // Bottom rounded corner
  330. _PentagonTipRadius = max(_PentagonTipRadius, 0.001);
  331. float halfWidth = width / 2;
  332. float m = -_PentagonTipSize / halfWidth;
  333. float d = sqrt(1 + m * m);
  334. float c = _PentagonTipSize;
  335. float k = _PentagonTipRadius * d + _PentagonTipSize;
  336. half2 circlePivot = half2(halfWidth, m * halfWidth + k);
  337. half cornerCircle = circle(_texcoord - circlePivot, _PentagonTipRadius);
  338. half x = (circlePivot.y + circlePivot.x / m - c) / (m + 1 / m);
  339. half y = m * x + c;
  340. half fy = map(_texcoord.x, x, width - x, -1, 1);
  341. fy = lerp(_PentagonTipRadius, y, abs(fy));
  342. sdfPentagon = _texcoord.y < fy ? cornerCircle: sdfPentagon;
  343. // Mid Left rounded corner
  344. k = _PentagonCornerRadius.w * d + _PentagonTipSize;
  345. circlePivot = half2(_PentagonCornerRadius.w, m * _PentagonCornerRadius.w + k);
  346. cornerCircle = circle(_texcoord - circlePivot, _PentagonCornerRadius.w);
  347. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1 / m); y = m * x + c;
  348. fy = map(_texcoord.x, x, circlePivot.x, y, circlePivot.y);
  349. sdfPentagon = _texcoord.y > fy && _texcoord.y < circlePivot.y ? cornerCircle: sdfPentagon;
  350. // Mid Right rounded corner
  351. m = -m; k = _PentagonCornerRadius.z * d - _PentagonTipSize;
  352. circlePivot = half2(width - _PentagonCornerRadius.z, m * (width - _PentagonCornerRadius.z) + k);
  353. cornerCircle = circle(_texcoord - circlePivot, _PentagonCornerRadius.z);
  354. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1 / m); y = m * x + c;
  355. fy = map(_texcoord.x, circlePivot.x, x, circlePivot.y, y);
  356. sdfPentagon = _texcoord.y > fy && _texcoord.y < circlePivot.y ? cornerCircle: sdfPentagon;
  357. // Top rounded corners
  358. cornerCircle = circle(_texcoord - half2(_PentagonCornerRadius.x, height - _PentagonCornerRadius.x), _PentagonCornerRadius.x);
  359. bool mask = _texcoord.x < _PentagonCornerRadius.x && _texcoord.y > height - _PentagonCornerRadius.x;
  360. sdfPentagon = mask ? cornerCircle: sdfPentagon;
  361. cornerCircle = circle(_texcoord - half2(width - _PentagonCornerRadius.y, height - _PentagonCornerRadius.y), _PentagonCornerRadius.y);
  362. mask = _texcoord.x > width - _PentagonCornerRadius.y && _texcoord.y > height - _PentagonCornerRadius.y;
  363. sdfPentagon = mask ? cornerCircle: sdfPentagon;
  364. return sdfPentagon;
  365. }
  366. #endif
  367. #if HEXAGON
  368. half hexagonScene(float4 _additionalData)
  369. {
  370. float2 _texcoord = _additionalData.xy;
  371. float2 _size = float2(_additionalData.z, _additionalData.w);
  372. float width = _size.x;//_additionalData.z;
  373. float height = _size.y;//_additionalData.w;
  374. half baseRect = rectanlge(_texcoord - half2(width / 2.0, height / 2.0), width, height);
  375. half scale = width / _HexagonTipSize.x;
  376. half rhombus1 = sdRhombus(_texcoord - float2(_HexagonTipSize.x * scale, height / 2.0), float2(_HexagonTipSize.x, height / 2.0) * scale);
  377. scale = width / _HexagonTipSize.y;
  378. half rhombus2 = sdRhombus(_texcoord - float2(width - _HexagonTipSize.y * scale, height / 2.0), float2(_HexagonTipSize.y, height / 2.0) * scale);
  379. half sdfHexagon = sdfDifference(sdfDifference(baseRect, -rhombus1), -rhombus2);
  380. //Left Rounded Corners
  381. float halfHeight = height / 2.0;
  382. float m = -halfHeight / _HexagonTipSize.x;
  383. float c = halfHeight;
  384. float d = sqrt(1.0 + m * m);
  385. float k = _HexagonTipRadius.x * d + c;
  386. //middle
  387. half2 circlePivot = half2((halfHeight - k) / m, halfHeight);
  388. half cornerCircle = circle(_texcoord - circlePivot, _HexagonTipRadius.x);
  389. half x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m);
  390. half y = m * x + c;
  391. half fy = map(_texcoord.x, x, circlePivot.x, y, circlePivot.y);
  392. sdfHexagon = _texcoord.y > fy && _texcoord.y < height - fy ? cornerCircle: sdfHexagon;
  393. //return sdfHexagon;
  394. //bottom
  395. k = _HexagonCornerRadius.x * d + c;
  396. circlePivot = half2((_HexagonCornerRadius.x - k) / m, _HexagonCornerRadius.x);
  397. cornerCircle = circle(_texcoord - circlePivot, _HexagonCornerRadius.x);
  398. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  399. fy = map(_texcoord.x, x, circlePivot.x, y, circlePivot.y);
  400. sdfHexagon = _texcoord.y < fy && _texcoord.x < circlePivot.x ? cornerCircle: sdfHexagon;
  401. //return sdfHexagon;
  402. //top
  403. k = _HexagonCornerRadius.w * d + c;
  404. circlePivot = half2((_HexagonCornerRadius.w - k) / m, height - _HexagonCornerRadius.w);
  405. cornerCircle = circle(_texcoord - circlePivot, _HexagonCornerRadius.w);
  406. x = (_HexagonCornerRadius.w + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  407. fy = map(_texcoord.x, x, circlePivot.x, height - y, circlePivot.y);
  408. sdfHexagon = _texcoord.y > fy && _texcoord.x < circlePivot.x ? cornerCircle: sdfHexagon;
  409. //return sdfHexagon;
  410. //Right Rounded Corners
  411. m = halfHeight / _HexagonTipSize.y;
  412. d = sqrt(1.0 + m * m);
  413. c = halfHeight - m * width;
  414. k = _HexagonTipRadius.y * d + c;
  415. //middle
  416. circlePivot = half2((halfHeight - k) / m, halfHeight);
  417. cornerCircle = circle(_texcoord - circlePivot, _HexagonTipRadius.y);
  418. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  419. fy = map(_texcoord.x, circlePivot.x, x, circlePivot.y, y);
  420. sdfHexagon = _texcoord.y > fy && _texcoord.y < height - fy ? cornerCircle: sdfHexagon;
  421. //return sdfHexagon;
  422. //bottom
  423. k = _HexagonCornerRadius.y * d + c;
  424. circlePivot = half2((_HexagonCornerRadius.y - k) / m, _HexagonCornerRadius.y);
  425. cornerCircle = circle(_texcoord - circlePivot, _HexagonCornerRadius.y);
  426. x = (circlePivot.y + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  427. fy = map(_texcoord.x, circlePivot.x, x, circlePivot.y, y);
  428. sdfHexagon = _texcoord.y < fy && _texcoord.x > circlePivot.x ? cornerCircle: sdfHexagon;
  429. //return sdfHexagon;
  430. //top
  431. k = _HexagonCornerRadius.z * d + c;
  432. circlePivot = half2((_HexagonCornerRadius.z - k) / m, height - _HexagonCornerRadius.z);
  433. cornerCircle = circle(_texcoord - circlePivot, _HexagonCornerRadius.z);
  434. x = (_HexagonCornerRadius.z + circlePivot.x / m - c) / (m + 1.0 / m); y = m * x + c;
  435. fy = map(_texcoord.x, circlePivot.x, x, circlePivot.y, height - y);
  436. sdfHexagon = _texcoord.y > fy && _texcoord.x > circlePivot.x ? cornerCircle: sdfHexagon;
  437. return sdfHexagon;
  438. }
  439. #endif
  440. #if NSTAR_POLYGON
  441. half nStarPolygonScene(float4 _additionalData)
  442. {
  443. float2 _texcoord = _additionalData.xy;
  444. float width = _additionalData.z;
  445. float height = _additionalData.w;
  446. float size = height / 2 - _NStarPolygonCornerRadius;
  447. half str = sdNStarPolygon(_texcoord - half2(width / 2, height / 2) - _NStarPolygonOffset, size, _NStarPolygonSideCount, _NStarPolygonInset) - _NStarPolygonCornerRadius;
  448. return str;
  449. }
  450. #endif
  451. float2 rotateUV(float2 uv, float rotation, float2 mid)
  452. {
  453. return float2(
  454. cos(rotation) * (uv.x - mid.x) + sin(rotation) * (uv.y - mid.y) + mid.x,
  455. cos(rotation) * (uv.y - mid.y) - sin(rotation) * (uv.x - mid.x) + mid.y
  456. );
  457. }
  458. v2f vert(appdata_t v)
  459. {
  460. v2f OUT;
  461. UNITY_SETUP_INSTANCE_ID(v);
  462. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
  463. OUT.worldPosition = v.vertex;
  464. OUT.vertex = UnityObjectToClipPos(v.vertex);
  465. OUT.texcoord = v.texcoord;
  466. OUT.effectsUv = v.uv1;
  467. float2 size = float2(v.size.x + _FalloffDistance, v.size.y + _FalloffDistance);
  468. float shapeRotation = radians(_ShapeRotation);
  469. size = _ConstrainRotation > 0.0 && frac(abs(shapeRotation) / 3.14159) > 0.1? float2(size.y, size.x) : size;
  470. float2 shapeUv = _ConstrainRotation > 0 ? v.uv1 : v.uv1 * size;
  471. shapeUv = rotateUV(shapeUv, shapeRotation, _ConstrainRotation > 0? float2(0.5, 0.5) : size * 0.5);
  472. shapeUv*= _ConstrainRotation > 0.0? size : 1.0;
  473. shapeUv.x = lerp(shapeUv.x, abs(size.x - shapeUv.x), _FlipHorizontal);
  474. shapeUv.y = lerp(shapeUv.y, abs(size.y - shapeUv.y), _FlipVertical);
  475. OUT.shapeData = float4(shapeUv.x, shapeUv.y, size.x, size.y);
  476. #ifdef UNITY_HALF_TEXEL_OFFSET
  477. OUT.vertex.xy += (_ScreenParams.zw - 1.0) * float2(-1.0, 1.0);
  478. #endif
  479. OUT.color = v.color * _Color;
  480. /* //SOFTMASK_HANDLE_START
  481. SOFTMASK_CALCULATE_COORDS(OUT, v.vertex);
  482. */ //SOFTMASK_HANDLE_END
  483. return OUT;
  484. }
  485. fixed4 frag(v2f IN): SV_Target
  486. {
  487. half4 color = IN.color;
  488. half2 texcoord = IN.texcoord;
  489. color = (tex2D(_MainTex, texcoord) + _TextureSampleAdd) * color;
  490. #if GRADIENT_LINEAR || GRADIENT_RADIAL
  491. colors[0] = _GradientColor0;
  492. colors[1] = _GradientColor1;
  493. colors[2] = _GradientColor2;
  494. colors[3] = _GradientColor3;
  495. colors[4] = _GradientColor4;
  496. colors[5] = _GradientColor5;
  497. colors[6] = _GradientColor6;
  498. colors[7] = _GradientColor7;
  499. alphas[0] = _GradientAlpha0;
  500. alphas[1] = _GradientAlpha1;
  501. alphas[2] = _GradientAlpha2;
  502. alphas[3] = _GradientAlpha3;
  503. alphas[4] = _GradientAlpha4;
  504. alphas[5] = _GradientAlpha5;
  505. alphas[6] = _GradientAlpha6;
  506. alphas[7] = _GradientAlpha7;
  507. #endif
  508. #if GRADIENT_LINEAR
  509. half gradientRotation = radians(_GradientRotation);
  510. half t = cos(gradientRotation) * (IN.effectsUv.x - 0.5) +
  511. sin(gradientRotation) * (IN.effectsUv.y - 0.5) + 0.5;
  512. half4 grad = SampleGradient(t);
  513. color *= grad;
  514. #endif
  515. #if GRADIENT_RADIAL
  516. half fac = saturate(length(IN.effectsUv - float2(.5, .5)) * 2);
  517. half4 grad = SampleGradient(clamp(fac, 0, 1));
  518. color *= grad;
  519. #endif
  520. #if GRADIENT_CORNER
  521. half4 topCol = lerp(_CornerGradientColor2, _CornerGradientColor3, IN.effectsUv.x);
  522. half4 bottomCol = lerp(_CornerGradientColor0, _CornerGradientColor1, IN.effectsUv.x);
  523. half4 finalCol = lerp(topCol, bottomCol, IN.effectsUv.y);
  524. color *= finalCol;
  525. #endif
  526. #if RECTANGLE || CIRCLE || PENTAGON || TRIANGLE || HEXAGON || NSTAR_POLYGON
  527. float sdfData = 0;
  528. float pixelScale = clamp(1.0/_FalloffDistance, 1.0/2048.0, 2048.0);
  529. #if RECTANGLE
  530. sdfData = rectangleScene(IN.shapeData);
  531. #elif CIRCLE
  532. sdfData = circleScene(IN.shapeData);
  533. #elif PENTAGON
  534. sdfData = pentagonScene(IN.shapeData);
  535. #elif TRIANGLE
  536. sdfData = triangleScene(IN.shapeData);
  537. #elif HEXAGON
  538. sdfData = hexagonScene(IN.shapeData);
  539. #elif NSTAR_POLYGON
  540. sdfData = nStarPolygonScene(IN.shapeData);
  541. #endif
  542. #if !OUTLINED && !STROKE && !OUTLINED_STROKE
  543. float sdf = sampleSdf(sdfData, pixelScale);
  544. color.a *= sdf;
  545. #endif
  546. #if STROKE
  547. float sdf = sampleSdfStrip(sdfData, _StrokeWidth + _OutlineWidth, pixelScale);
  548. color.a *= sdf;
  549. #endif
  550. #if OUTLINED
  551. float alpha = sampleSdf(sdfData, pixelScale);
  552. float lerpFac = sampleSdf(sdfData + _OutlineWidth, pixelScale);
  553. color = half4(lerp(_OutlineColor.rgb, color.rgb, lerpFac), lerp(_OutlineColor.a * color.a, color.a, lerpFac));
  554. color.a *= alpha;
  555. #endif
  556. #if OUTLINED_STROKE
  557. float alpha = sampleSdfStrip(sdfData, _OutlineWidth + _StrokeWidth, pixelScale);
  558. float lerpFac = sampleSdfStrip(sdfData + _OutlineWidth, _StrokeWidth + _FalloffDistance, pixelScale);
  559. lerpFac = clamp(lerpFac, 0, 1);
  560. color = half4(lerp(_OutlineColor.rgb, color.rgb, lerpFac), lerp(_OutlineColor.a * color.a, color.a, lerpFac));
  561. color.a *= alpha;
  562. #endif
  563. #endif
  564. /* //SOFTMASK_HANDLE_START
  565. color.a *= SOFTMASK_GET_MASK(IN);
  566. */ //SOFTMASK_HANDLE_END
  567. #ifdef UNITY_UI_CLIP_RECT
  568. color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  569. #endif
  570. #ifdef UNITY_UI_ALPHACLIP
  571. clip(color.a - 0.001);
  572. #endif
  573. return fixed4(color);
  574. }
  575. ENDCG
  576. }
  577. }
  578. CustomEditor "MPUIKIT.Editor.MPImageShaderGUI"
  579. }