ButtonBGMaskRoundedCorner.shader 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "Unlit/ButtonBGMask/RoundedCorner"
  3. {
  4. Properties
  5. {
  6. _MainTex("Texture", 2D) = "white" {}
  7. _Color("Main Color", Color) = (1,1,1,1)
  8. _Radius("Radius", Range(0.25, 0.5)) = 0.5
  9. }
  10. SubShader
  11. {
  12. Pass
  13. {
  14. Tags {"Queue" = "Transparent"}
  15. ZWrite Off
  16. Cull Off
  17. Blend SrcAlpha OneMinusSrcAlpha
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #include "UnityCG.cginc"
  22. sampler2D _MainTex;
  23. fixed4 _Color;
  24. float4 _MainTex_ST;
  25. float _Radius;
  26. struct VertexOutPut
  27. {
  28. float4 pos : SV_POSITION;
  29. float2 uv : TEXCOORD0;
  30. };
  31. VertexOutPut vert(appdata_full v)
  32. {
  33. VertexOutPut o;
  34. o.pos = UnityObjectToClipPos(v.vertex);
  35. o.uv = v.texcoord.xy;
  36. o.uv.x = 1.0 - o.uv.x;
  37. return o;
  38. }
  39. fixed4 frag(VertexOutPut i) : COLOR
  40. {
  41. fixed4 col = tex2D(_MainTex, i.uv) * _Color;
  42. //圆角
  43. float2 uv = i.uv.xy - float2(0.5,0.5);//移动UV坐标中心
  44. float rx = fmod(uv.x, _Radius);//圆角所在区域,也就是圆角半径为0.1
  45. float ry = fmod(uv.y, _Radius);//
  46. float mx = step(_Radius, abs(uv.x));//大于0.4的部分, step(a,x):x<a取0,否则返回1
  47. float my = step(_Radius, abs(uv.y));//          
  48. float alpha = 1 - mx * my * step(0.5 - _Radius, length(half2(rx,ry)));//在[0,0.4]范围,mx*my始终为0,最终值始终为1;在(0.4,0.5]范围,所在的圆角区域,mx*my使用为1,大小由圆角半径决定;在剩下的其他区域,mx*my也是为0,最终值为;         
  49. return fixed4(col.rgb,alpha * col.a);//        
  50. }
  51. ENDCG
  52. }
  53. }
  54. }