Hyluss

test

Jun 17th, 2019
300
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.04 KB | None | 0 0
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2.  
  3. Shader "Custom/AlphaDependingDistance"
  4. {
  5. Properties
  6. {
  7. //_MainTex("Texture", 2D) = "white" {}
  8. _Radius("RadiusX", Range(0.001, 500)) = 10
  9.  
  10. _FaceColor("Face Color", Color) = (1,1,1,1)
  11. _FaceDilate("Face Dilate", Range(-1,1)) = 0
  12.  
  13. _OutlineColor("Outline Color", Color) = (0,0,0,1)
  14. _OutlineWidth("Outline Thickness", Range(0,1)) = 0
  15. _OutlineSoftness("Outline Softness", Range(0,1)) = 0
  16.  
  17. _UnderlayColor("Border Color", Color) = (0,0,0,.5)
  18. _UnderlayOffsetX("Border OffsetX", Range(-1,1)) = 0
  19. _UnderlayOffsetY("Border OffsetY", Range(-1,1)) = 0
  20. _UnderlayDilate("Border Dilate", Range(-1,1)) = 0
  21. _UnderlaySoftness("Border Softness", Range(0,1)) = 0
  22.  
  23. _WeightNormal("Weight Normal", float) = 0
  24. _WeightBold("Weight Bold", float) = .5
  25.  
  26. _ShaderFlags("Flags", float) = 0
  27. _ScaleRatioA("Scale RatioA", float) = 1
  28. _ScaleRatioB("Scale RatioB", float) = 1
  29. _ScaleRatioC("Scale RatioC", float) = 1
  30.  
  31. _MainTex("Font Atlas", 2D) = "white" {}
  32. _TextureWidth("Texture Width", float) = 512
  33. _TextureHeight("Texture Height", float) = 512
  34. _GradientScale("Gradient Scale", float) = 5
  35. _ScaleX("Scale X", float) = 1
  36. _ScaleY("Scale Y", float) = 1
  37. _PerspectiveFilter("Perspective Correction", Range(0, 1)) = 0.875
  38.  
  39. _VertexOffsetX("Vertex OffsetX", float) = 0
  40. _VertexOffsetY("Vertex OffsetY", float) = 0
  41.  
  42. _ClipRect("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  43. _MaskSoftnessX("Mask SoftnessX", float) = 0
  44. _MaskSoftnessY("Mask SoftnessY", float) = 0
  45.  
  46. _StencilComp("Stencil Comparison", Float) = 8
  47. _Stencil("Stencil ID", Float) = 0
  48. _StencilOp("Stencil Operation", Float) = 0
  49. _StencilWriteMask("Stencil Write Mask", Float) = 255
  50. _StencilReadMask("Stencil Read Mask", Float) = 255
  51.  
  52. _ColorMask("Color Mask", Float) = 15
  53. }
  54.  
  55.  
  56. SubShader
  57. {
  58. Tags
  59. {
  60. "Queue" = "Transparent"
  61. "IgnoreProjector" = "True"
  62. "RenderType" = "Transparent"
  63. }
  64.  
  65.  
  66. Stencil
  67. {
  68. Ref[_Stencil]
  69. Comp[_StencilComp]
  70. Pass[_StencilOp]
  71. ReadMask[_StencilReadMask]
  72. WriteMask[_StencilWriteMask]
  73. }
  74.  
  75. Cull[_CullMode]
  76. ZWrite Off
  77. Lighting Off
  78. Fog { Mode Off }
  79. ZTest[unity_GUIZTestMode]
  80. Blend One OneMinusSrcAlpha
  81. ColorMask[_ColorMask]
  82.  
  83. Pass {
  84. CGPROGRAM
  85. #pragma vertex VertShader
  86. #pragma fragment PixShader
  87. #pragma shader_feature __ OUTLINE_ON
  88. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  89.  
  90. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  91. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  92.  
  93. #include "UnityCG.cginc"
  94. #include "UnityUI.cginc"
  95. #include "TMPro_Properties.cginc"
  96.  
  97. struct vertex_t {
  98. float4 vertex : POSITION;
  99. float3 normal : NORMAL;
  100. fixed4 color : COLOR;
  101. float2 texcoord0 : TEXCOORD0;
  102. float2 texcoord1 : TEXCOORD1;
  103. };
  104.  
  105. struct pixel_t {
  106. float4 vertex : SV_POSITION;
  107. fixed4 faceColor : COLOR;
  108. fixed4 outlineColor : COLOR1;
  109. float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV
  110. half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  111. half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw)
  112. #if (UNDERLAY_ON | UNDERLAY_INNER)
  113. float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved
  114. half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y)
  115. #endif
  116. };
  117.  
  118.  
  119.  
  120.  
  121.  
  122. struct v2f {
  123. float4 pos : SV_POSITION;
  124. float2 uv : TEXCOORD0;
  125. float4 worldPos : TEXCOORD1;
  126. };
  127.  
  128. v2f vert(appdata_base v) {
  129. v2f o;
  130. o.pos = UnityObjectToClipPos(v.vertex);
  131. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  132. o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  133. return o;
  134. }
  135.  
  136. float _Radius;
  137.  
  138. fixed4 frag(v2f i) : SV_Target{
  139. fixed4 col = tex2D(_MainTex, i.uv);
  140. float dist = distance(i.worldPos, _WorldSpaceCameraPos);
  141. col.a = saturate(dist / _Radius);
  142. return col;
  143. }
  144.  
  145.  
  146.  
  147. pixel_t VertShader(vertex_t input)
  148. {
  149. float bold = step(input.texcoord1.y, 0);
  150.  
  151. float4 vert = input.vertex;
  152. vert.x += _VertexOffsetX;
  153. vert.y += _VertexOffsetY;
  154. float4 vPosition = UnityObjectToClipPos(vert);
  155.  
  156. float2 pixelSize = vPosition.w;
  157. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  158.  
  159. float scale = rsqrt(dot(pixelSize, pixelSize));
  160. scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
  161. if (UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  162.  
  163. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  164. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  165.  
  166. float layerScale = scale;
  167.  
  168. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  169. float bias = (0.5 - weight) * scale - 0.5;
  170. float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
  171.  
  172. float opacity = input.color.a;
  173. #if (UNDERLAY_ON | UNDERLAY_INNER)
  174. opacity = 1.0;
  175. #endif
  176.  
  177. fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  178. faceColor.rgb *= faceColor.a;
  179.  
  180. fixed4 outlineColor = _OutlineColor;
  181. outlineColor.a *= opacity;
  182. outlineColor.rgb *= outlineColor.a;
  183. outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
  184.  
  185. #if (UNDERLAY_ON | UNDERLAY_INNER)
  186.  
  187. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  188. float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  189.  
  190. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  191. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  192. float2 layerOffset = float2(x, y);
  193. #endif
  194.  
  195. // Generate UV for the Masking Texture
  196. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  197. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  198.  
  199. // Structure for pixel shader
  200. pixel_t output = {
  201. vPosition,
  202. faceColor,
  203. outlineColor,
  204. float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
  205. half4(scale, bias - outline, bias + outline, bias),
  206. half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
  207. #if (UNDERLAY_ON | UNDERLAY_INNER)
  208. float4(input.texcoord0 + layerOffset, input.color.a, 0),
  209. half2(layerScale, layerBias),
  210. #endif
  211. };
  212.  
  213. return output;
  214. }
  215.  
  216.  
  217. // PIXEL SHADER
  218. fixed4 PixShader(pixel_t input) : SV_Target
  219. {
  220. half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  221. half4 c = input.faceColor * saturate(d - input.param.w);
  222.  
  223. #ifdef OUTLINE_ON
  224. c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  225. c *= saturate(d - input.param.y);
  226. #endif
  227.  
  228. #if UNDERLAY_ON
  229. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  230. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
  231. #endif
  232.  
  233. #if UNDERLAY_INNER
  234. half sd = saturate(d - input.param.z);
  235. d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  236. c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
  237. #endif
  238.  
  239. // Alternative implementation to UnityGet2DClipping with support for softness.
  240. #if UNITY_UI_CLIP_RECT
  241. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  242. c *= m.x * m.y;
  243. #endif
  244.  
  245. #if (UNDERLAY_ON | UNDERLAY_INNER)
  246. c *= input.texcoord1.z;
  247. #endif
  248.  
  249. #if UNITY_UI_ALPHACLIP
  250. clip(c.a - 0.001);
  251. #endif
  252.  
  253. return c;
  254. }
  255. ENDCG
  256. }
  257. }
  258.  
  259.  
  260.  
  261.  
  262. SubShader
  263. {
  264. Tags {"Queue" = "Transparent" "RenderType" = "Transparent" }
  265.  
  266. Blend SrcAlpha OneMinusSrcAlpha
  267.  
  268. Pass
  269. {
  270. CGPROGRAM
  271. #pragma vertex vert
  272. #pragma fragment frag
  273. #include "UnityCG.cginc"
  274.  
  275. struct v2f {
  276. float4 pos : SV_POSITION;
  277. float2 uv : TEXCOORD0;
  278. float4 worldPos : TEXCOORD1;
  279. };
  280.  
  281. sampler2D _MainTex;
  282. float4 _MainTex_ST;
  283.  
  284. v2f vert(appdata_base v) {
  285. v2f o;
  286. o.pos = UnityObjectToClipPos(v.vertex);
  287. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  288. o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  289. return o;
  290. }
  291.  
  292. float _Radius;
  293.  
  294. fixed4 frag(v2f i) : SV_Target {
  295. fixed4 col = tex2D(_MainTex, i.uv);
  296. float dist = distance(i.worldPos, _WorldSpaceCameraPos);
  297. col.a = saturate(dist / _Radius);
  298. return col;
  299. }
  300.  
  301. ENDCG
  302. }
  303. }
  304.  
  305.  
  306.  
  307.  
  308. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  309. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment