You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
9.8 KiB

  1. Shader "TextMeshPro/Distance Field Overlay" {
  2. Properties {
  3. _FaceTex ("Face Texture", 2D) = "white" {}
  4. _FaceUVSpeedX ("Face UV Speed X", Range(-5, 5)) = 0.0
  5. _FaceUVSpeedY ("Face UV Speed Y", Range(-5, 5)) = 0.0
  6. _FaceColor ("Face Color", Color) = (1,1,1,1)
  7. _FaceDilate ("Face Dilate", Range(-1,1)) = 0
  8. _OutlineColor ("Outline Color", Color) = (0,0,0,1)
  9. _OutlineTex ("Outline Texture", 2D) = "white" {}
  10. _OutlineUVSpeedX ("Outline UV Speed X", Range(-5, 5)) = 0.0
  11. _OutlineUVSpeedY ("Outline UV Speed Y", Range(-5, 5)) = 0.0
  12. _OutlineWidth ("Outline Thickness", Range(0, 1)) = 0
  13. _OutlineSoftness ("Outline Softness", Range(0,1)) = 0
  14. _Bevel ("Bevel", Range(0,1)) = 0.5
  15. _BevelOffset ("Bevel Offset", Range(-0.5,0.5)) = 0
  16. _BevelWidth ("Bevel Width", Range(-.5,0.5)) = 0
  17. _BevelClamp ("Bevel Clamp", Range(0,1)) = 0
  18. _BevelRoundness ("Bevel Roundness", Range(0,1)) = 0
  19. _LightAngle ("Light Angle", Range(0.0, 6.2831853)) = 3.1416
  20. _SpecularColor ("Specular", Color) = (1,1,1,1)
  21. _SpecularPower ("Specular", Range(0,4)) = 2.0
  22. _Reflectivity ("Reflectivity", Range(5.0,15.0)) = 10
  23. _Diffuse ("Diffuse", Range(0,1)) = 0.5
  24. _Ambient ("Ambient", Range(1,0)) = 0.5
  25. _BumpMap ("Normal map", 2D) = "bump" {}
  26. _BumpOutline ("Bump Outline", Range(0,1)) = 0
  27. _BumpFace ("Bump Face", Range(0,1)) = 0
  28. _ReflectFaceColor ("Reflection Color", Color) = (0,0,0,1)
  29. _ReflectOutlineColor("Reflection Color", Color) = (0,0,0,1)
  30. _Cube ("Reflection Cubemap", Cube) = "black" { /* TexGen CubeReflect */ }
  31. _EnvMatrixRotation ("Texture Rotation", vector) = (0, 0, 0, 0)
  32. _UnderlayColor ("Border Color", Color) = (0,0,0, 0.5)
  33. _UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0
  34. _UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0
  35. _UnderlayDilate ("Border Dilate", Range(-1,1)) = 0
  36. _UnderlaySoftness ("Border Softness", Range(0,1)) = 0
  37. _GlowColor ("Color", Color) = (0, 1, 0, 0.5)
  38. _GlowOffset ("Offset", Range(-1,1)) = 0
  39. _GlowInner ("Inner", Range(0,1)) = 0.05
  40. _GlowOuter ("Outer", Range(0,1)) = 0.05
  41. _GlowPower ("Falloff", Range(1, 0)) = 0.75
  42. _WeightNormal ("Weight Normal", float) = 0
  43. _WeightBold ("Weight Bold", float) = 0.5
  44. _ShaderFlags ("Flags", float) = 0
  45. _ScaleRatioA ("Scale RatioA", float) = 1
  46. _ScaleRatioB ("Scale RatioB", float) = 1
  47. _ScaleRatioC ("Scale RatioC", float) = 1
  48. _MainTex ("Font Atlas", 2D) = "white" {}
  49. _TextureWidth ("Texture Width", float) = 512
  50. _TextureHeight ("Texture Height", float) = 512
  51. _GradientScale ("Gradient Scale", float) = 5.0
  52. _ScaleX ("Scale X", float) = 1.0
  53. _ScaleY ("Scale Y", float) = 1.0
  54. _PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875
  55. _VertexOffsetX ("Vertex OffsetX", float) = 0
  56. _VertexOffsetY ("Vertex OffsetY", float) = 0
  57. _MaskCoord ("Mask Coordinates", vector) = (0, 0, 32767, 32767)
  58. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  59. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  60. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  61. _StencilComp ("Stencil Comparison", Float) = 8
  62. _Stencil ("Stencil ID", Float) = 0
  63. _StencilOp ("Stencil Operation", Float) = 0
  64. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  65. _StencilReadMask ("Stencil Read Mask", Float) = 255
  66. _ColorMask ("Color Mask", Float) = 15
  67. }
  68. SubShader {
  69. Tags
  70. {
  71. "Queue"="Overlay"
  72. "IgnoreProjector"="True"
  73. "RenderType"="Transparent"
  74. }
  75. Stencil
  76. {
  77. Ref [_Stencil]
  78. Comp [_StencilComp]
  79. Pass [_StencilOp]
  80. ReadMask [_StencilReadMask]
  81. WriteMask [_StencilWriteMask]
  82. }
  83. Cull [_CullMode]
  84. ZWrite Off
  85. Lighting Off
  86. Fog { Mode Off }
  87. ZTest Always
  88. Blend One OneMinusSrcAlpha
  89. ColorMask [_ColorMask]
  90. Pass {
  91. CGPROGRAM
  92. #pragma target 3.0
  93. #pragma vertex VertShader
  94. #pragma fragment PixShader
  95. #pragma shader_feature __ BEVEL_ON
  96. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  97. #pragma shader_feature __ GLOW_ON
  98. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  99. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  100. #include "UnityCG.cginc"
  101. #include "UnityUI.cginc"
  102. #include "TMPro_Properties.cginc"
  103. #include "TMPro.cginc"
  104. struct vertex_t {
  105. float4 position : POSITION;
  106. float3 normal : NORMAL;
  107. fixed4 color : COLOR;
  108. float2 texcoord0 : TEXCOORD0;
  109. float2 texcoord1 : TEXCOORD1;
  110. };
  111. struct pixel_t {
  112. float4 position : SV_POSITION;
  113. fixed4 color : COLOR;
  114. float2 atlas : TEXCOORD0; // Atlas
  115. float4 param : TEXCOORD1; // alphaClip, scale, bias, weight
  116. float4 mask : TEXCOORD2; // Position in object space(xy), pixel Size(zw)
  117. float3 viewDir : TEXCOORD3;
  118. #if (UNDERLAY_ON || UNDERLAY_INNER)
  119. float4 texcoord2 : TEXCOORD4; // u,v, scale, bias
  120. fixed4 underlayColor : COLOR1;
  121. #endif
  122. float4 textures : TEXCOORD5;
  123. };
  124. // Used by Unity internally to handle Texture Tiling and Offset.
  125. float4 _FaceTex_ST;
  126. float4 _OutlineTex_ST;
  127. pixel_t VertShader(vertex_t input)
  128. {
  129. float bold = step(input.texcoord1.y, 0);
  130. float4 vert = input.position;
  131. vert.x += _VertexOffsetX;
  132. vert.y += _VertexOffsetY;
  133. float4 vPosition = UnityObjectToClipPos(vert);
  134. float2 pixelSize = vPosition.w;
  135. pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  136. float scale = rsqrt(dot(pixelSize, pixelSize));
  137. scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
  138. if (UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  139. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  140. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  141. float bias =(.5 - weight) + (.5 / scale);
  142. float alphaClip = (1.0 - _OutlineWidth*_ScaleRatioA - _OutlineSoftness*_ScaleRatioA);
  143. #if GLOW_ON
  144. alphaClip = min(alphaClip, 1.0 - _GlowOffset * _ScaleRatioB - _GlowOuter * _ScaleRatioB);
  145. #endif
  146. alphaClip = alphaClip / 2.0 - ( .5 / scale) - weight;
  147. #if (UNDERLAY_ON || UNDERLAY_INNER)
  148. float4 underlayColor = _UnderlayColor;
  149. underlayColor.rgb *= underlayColor.a;
  150. float bScale = scale;
  151. bScale /= 1 + ((_UnderlaySoftness*_ScaleRatioC) * bScale);
  152. float bBias = (0.5 - weight) * bScale - 0.5 - ((_UnderlayDilate * _ScaleRatioC) * 0.5 * bScale);
  153. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  154. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  155. float2 bOffset = float2(x, y);
  156. #endif
  157. // Generate UV for the Masking Texture
  158. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  159. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  160. // Support for texture tiling and offset
  161. float2 textureUV = UnpackUV(input.texcoord1.x);
  162. float2 faceUV = TRANSFORM_TEX(textureUV, _FaceTex);
  163. float2 outlineUV = TRANSFORM_TEX(textureUV, _OutlineTex);
  164. pixel_t output = {
  165. vPosition,
  166. input.color,
  167. input.texcoord0,
  168. float4(alphaClip, scale, bias, weight),
  169. half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
  170. mul((float3x3)_EnvMatrix, _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, vert).xyz),
  171. #if (UNDERLAY_ON || UNDERLAY_INNER)
  172. float4(input.texcoord0 + bOffset, bScale, bBias),
  173. underlayColor,
  174. #endif
  175. float4(faceUV, outlineUV),
  176. };
  177. return output;
  178. }
  179. fixed4 PixShader(pixel_t input) : SV_Target
  180. {
  181. float c = tex2D(_MainTex, input.atlas).a;
  182. #ifndef UNDERLAY_ON
  183. clip(c - input.param.x);
  184. #endif
  185. float scale = input.param.y;
  186. float bias = input.param.z;
  187. float weight = input.param.w;
  188. float sd = (bias - c) * scale;
  189. float outline = (_OutlineWidth * _ScaleRatioA) * scale;
  190. float softness = (_OutlineSoftness * _ScaleRatioA) * scale;
  191. half4 faceColor = _FaceColor;
  192. half4 outlineColor = _OutlineColor;
  193. faceColor.rgb *= input.color.rgb;
  194. faceColor *= tex2D(_FaceTex, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y);
  195. outlineColor *= tex2D(_OutlineTex, input.textures.zw + float2(_OutlineUVSpeedX, _OutlineUVSpeedY) * _Time.y);
  196. faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
  197. #if BEVEL_ON
  198. float3 dxy = float3(0.5 / _TextureWidth, 0.5 / _TextureHeight, 0);
  199. float3 n = GetSurfaceNormal(input.atlas, weight, dxy);
  200. float3 bump = UnpackNormal(tex2D(_BumpMap, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y)).xyz;
  201. bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
  202. n = normalize(n- bump);
  203. float3 light = normalize(float3(sin(_LightAngle), cos(_LightAngle), -1.0));
  204. float3 col = GetSpecular(n, light);
  205. faceColor.rgb += col*faceColor.a;
  206. faceColor.rgb *= 1-(dot(n, light)*_Diffuse);
  207. faceColor.rgb *= lerp(_Ambient, 1, n.z*n.z);
  208. fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDir, -n));
  209. faceColor.rgb += reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  210. #endif
  211. #if UNDERLAY_ON
  212. float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
  213. faceColor += input.underlayColor * saturate(d - input.texcoord2.w) * (1 - faceColor.a);
  214. #endif
  215. #if UNDERLAY_INNER
  216. float d = tex2D(_MainTex, input.texcoord2.xy).a * input.texcoord2.z;
  217. faceColor += input.underlayColor * (1 - saturate(d - input.texcoord2.w)) * saturate(1 - sd) * (1 - faceColor.a);
  218. #endif
  219. #if GLOW_ON
  220. float4 glowColor = GetGlowColor(sd, scale);
  221. faceColor.rgb += glowColor.rgb * glowColor.a;
  222. #endif
  223. // Alternative implementation to UnityGet2DClipping with support for softness.
  224. #if UNITY_UI_CLIP_RECT
  225. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  226. faceColor *= m.x * m.y;
  227. #endif
  228. #if UNITY_UI_ALPHACLIP
  229. clip(faceColor.a - 0.001);
  230. #endif
  231. return faceColor * input.color.a;
  232. }
  233. ENDCG
  234. }
  235. }
  236. Fallback "TextMeshPro/Mobile/Distance Field"
  237. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  238. }