ProceduralImageRuntime.shader 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. Shader "UI/Procedural UI Image"
  3. {
  4. Properties
  5. {
  6. [PerRendererData]_MainTex ("Base (RGB)", 2D) = "white" {}
  7. _Width("width", float) = 100
  8. _Height("height", float) = 100
  9. _Radius("radius", Vector) = (0,0,0,0)
  10. _LineWeight("line weight", float) = 0
  11. _PixelWorldScale("Pixel world scale", float) = 1
  12. // required for UI.Mask
  13. _StencilComp ("Stencil Comparison", Float) = 8
  14. _Stencil ("Stencil ID", Float) = 0
  15. _StencilOp ("Stencil Operation", Float) = 0
  16. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  17. _StencilReadMask ("Stencil Read Mask", Float) = 255
  18. _ColorMask ("Color Mask", Float) = 15
  19. }
  20. SubShader
  21. {
  22. Tags
  23. {
  24. "Queue"="Transparent"
  25. "IgnoreProjector"="True"
  26. "RenderType"="Transparent"
  27. "PreviewType"="Plane"
  28. "CanUseSpriteAtlas"="True"
  29. }
  30. Stencil
  31. {
  32. Ref [_Stencil]
  33. Comp [_StencilComp]
  34. Pass [_StencilOp]
  35. ReadMask [_StencilReadMask]
  36. WriteMask [_StencilWriteMask]
  37. }
  38. Cull Off
  39. Lighting Off
  40. ZWrite Off
  41. ZTest [unity_GUIZTestMode]
  42. Blend SrcAlpha OneMinusSrcAlpha
  43. ColorMask [_ColorMask]
  44. Pass
  45. {
  46. CGPROGRAM
  47. #pragma vertex vert
  48. #pragma fragment frag
  49. //#pragma exclude_renderers gles3 metal d3d11_9x xbox360 xboxone ps3 ps4 psp2
  50. #include "UnityCG.cginc"
  51. #include "UnityUI.cginc"
  52. //#pragma target 3.0
  53. struct appdata_t
  54. {
  55. float4 vertex : POSITION;
  56. float4 color : COLOR;
  57. float2 texcoord : TEXCOORD0;
  58. };
  59. struct v2f
  60. {
  61. float4 vertex : POSITION;
  62. fixed4 color : COLOR;
  63. half2 texcoord : TEXCOORD0;
  64. float4 worldPosition : TEXCOORD1;
  65. float2 uv : TEXCOORD2;
  66. };
  67. fixed4 _TextureSampleAdd;
  68. bool _UseClipRect;
  69. float4 _ClipRect;
  70. bool _UseAlphaClip;
  71. half _Width;
  72. half _Height;
  73. half _PixelWorldScale;
  74. half4 _Radius;
  75. half _LineWeight;
  76. sampler2D _MainTex;
  77. v2f vert(appdata_t IN){
  78. v2f OUT;
  79. OUT.worldPosition = IN.vertex;
  80. OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);
  81. OUT.texcoord = IN.texcoord*float2(_Width,_Height);
  82. OUT.uv = IN.texcoord;
  83. #ifdef UNITY_HALF_TEXEL_OFFSET
  84. OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
  85. #endif
  86. OUT.color = IN.color*(1+_TextureSampleAdd);
  87. return OUT;
  88. }
  89. // half visible(half2 pos,half4 r){
  90. // half4 p = half4(pos,_Width-pos.x,_Height-pos.y);
  91. // half v = min(min(min(p.x,p.y),p.z),p.w);
  92. // if(all(p.xw<r[0])){
  93. // //v = min(r[0]-distance(p.xw,half2(r[0],r[0])),v);
  94. // v = min(r[0]-length(p.xw-r[0]),v);
  95. // }
  96. // else if(all(p.zw<r[1])){
  97. // //v = min(r[1]-distance(p.zw,half2(r[1],r[1])),v);
  98. // v = min(r[1]-length(p.zw-r[1]),v);
  99. // }
  100. // if(all(p.zy<r[2])){
  101. // //v = min(r[2]-distance(p.zy,half2(r[2],r[2])),v);
  102. // v = min(r[2]-length(p.zy-r[2]),v);
  103. // }
  104. // else if(all(p.xy<r[3])){
  105. // //v = min(r[3]-distance(p.xy,half2(r[3],r[3])),v);
  106. // v = min(r[3]-length(p.xy-r[3]),v);
  107. // }
  108. // return v;
  109. // }
  110. //more optmised version without dynamic branching
  111. half visible(half2 pos,half4 r){
  112. half4 p = half4(pos,_Width-pos.x,_Height-pos.y);
  113. half v = min(min(min(p.x,p.y),p.z),p.w);
  114. bool4 b = bool4(all(p.xw<r[0]),all(p.zw<r[1]),all(p.zy<r[2]),all(p.xy<r[3]));
  115. half4 vis = r-half4(length(p.xw-r[0]),length(p.zw-r[1]),length(p.zy-r[2]),length(p.xy-r[3]));
  116. half4 foo = min(b*max(vis,0),v)+(1-b)*v;
  117. v = any(b)*min(min(min(foo.x,foo.y),foo.z),foo.w)+v*(1-any(b));
  118. return v;
  119. }
  120. fixed4 frag (v2f IN) : SV_Target
  121. {
  122. //half4 color = IN.color;
  123. half4 color = tex2D(_MainTex, IN.uv)*IN.color;
  124. if (_UseClipRect)
  125. color *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
  126. if (_UseAlphaClip)
  127. clip (color.a - 0.001);
  128. if(_LineWeight>0){
  129. half l = (_LineWeight+1/_PixelWorldScale)/2;
  130. color.a *= saturate((l-distance(visible(IN.texcoord,_Radius),l))*_PixelWorldScale);
  131. }
  132. else{
  133. color.a *= saturate(visible(IN.texcoord,_Radius)*_PixelWorldScale);
  134. }
  135. return color;
  136. }
  137. ENDCG
  138. }
  139. }
  140. }