TMP_SDF SSD.shader 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. Shader "TextMeshPro/Distance Field SSD" {
  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. _Sharpness ("Sharpness", Range(-1,1)) = 0
  56. _VertexOffsetX ("Vertex OffsetX", float) = 0
  57. _VertexOffsetY ("Vertex OffsetY", float) = 0
  58. _MaskCoord ("Mask Coordinates", vector) = (0, 0, 32767, 32767)
  59. _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
  60. _MaskSoftnessX ("Mask SoftnessX", float) = 0
  61. _MaskSoftnessY ("Mask SoftnessY", float) = 0
  62. _StencilComp ("Stencil Comparison", Float) = 8
  63. _Stencil ("Stencil ID", Float) = 0
  64. _StencilOp ("Stencil Operation", Float) = 0
  65. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  66. _StencilReadMask ("Stencil Read Mask", Float) = 255
  67. _CullMode ("Cull Mode", Float) = 0
  68. _ColorMask ("Color Mask", Float) = 15
  69. }
  70. SubShader {
  71. Tags
  72. {
  73. "Queue" = "Transparent"
  74. "IgnoreProjector" = "True"
  75. "RenderType" = "Transparent"
  76. }
  77. Stencil
  78. {
  79. Ref[_Stencil]
  80. Comp[_StencilComp]
  81. Pass[_StencilOp]
  82. ReadMask[_StencilReadMask]
  83. WriteMask[_StencilWriteMask]
  84. }
  85. Cull[_CullMode]
  86. ZWrite Off
  87. Lighting Off
  88. Fog { Mode Off }
  89. ZTest[unity_GUIZTestMode]
  90. Blend One OneMinusSrcAlpha
  91. ColorMask[_ColorMask]
  92. Pass
  93. {
  94. CGPROGRAM
  95. #pragma target 3.0
  96. #pragma vertex VertShader
  97. #pragma fragment PixShader
  98. #pragma shader_feature __ BEVEL_ON
  99. #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  100. #pragma shader_feature __ GLOW_ON
  101. #pragma shader_feature __ FORCE_LINEAR
  102. #pragma multi_compile __ UNITY_UI_CLIP_RECT
  103. #pragma multi_compile __ UNITY_UI_ALPHACLIP
  104. #include "UnityCG.cginc"
  105. #include "UnityUI.cginc"
  106. #include "TMPro_Properties.cginc"
  107. #include "TMPro.cginc"
  108. struct vertex_t
  109. {
  110. UNITY_VERTEX_INPUT_INSTANCE_ID
  111. float4 position : POSITION;
  112. float3 normal : NORMAL;
  113. float4 color : COLOR;
  114. float4 texcoord0 : TEXCOORD0;
  115. float2 texcoord1 : TEXCOORD1;
  116. };
  117. struct pixel_t
  118. {
  119. UNITY_VERTEX_INPUT_INSTANCE_ID
  120. UNITY_VERTEX_OUTPUT_STEREO
  121. float4 position : SV_POSITION;
  122. float4 color : COLOR;
  123. float2 atlas : TEXCOORD0;
  124. float weight : TEXCOORD1;
  125. float2 mask : TEXCOORD2; // Position in object space(xy)
  126. float3 viewDir : TEXCOORD3;
  127. #if (UNDERLAY_ON || UNDERLAY_INNER)
  128. float2 texcoord2 : TEXCOORD4;
  129. float4 underlayColor : COLOR1;
  130. #endif
  131. float4 textures : TEXCOORD5;
  132. };
  133. // Used by Unity internally to handle Texture Tiling and Offset.
  134. float4 _FaceTex_ST;
  135. float4 _OutlineTex_ST;
  136. float _UIMaskSoftnessX;
  137. float _UIMaskSoftnessY;
  138. int _UIVertexColorAlwaysGammaSpace;
  139. float4 SRGBToLinear(float4 rgba)
  140. {
  141. return float4(lerp(rgba.rgb / 12.92f, pow((rgba.rgb + 0.055f) / 1.055f, 2.4f), step(0.04045f, rgba.rgb)), rgba.a);
  142. }
  143. pixel_t VertShader(vertex_t input)
  144. {
  145. pixel_t output;
  146. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  147. UNITY_SETUP_INSTANCE_ID(input);
  148. UNITY_TRANSFER_INSTANCE_ID(input,output);
  149. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  150. float bold = step(input.texcoord0.w, 0);
  151. float4 vert = input.position;
  152. vert.x += _VertexOffsetX;
  153. vert.y += _VertexOffsetY;
  154. float4 vPosition = UnityObjectToClipPos(vert);
  155. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  156. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  157. #if (UNDERLAY_ON || UNDERLAY_INNER)
  158. float4 underlayColor = _UnderlayColor;
  159. underlayColor.rgb *= underlayColor.a;
  160. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  161. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  162. float2 bOffset = float2(x, y);
  163. #endif
  164. // Generate UV for the Masking Texture
  165. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  166. // Support for texture tiling and offset
  167. float2 textureUV = input.texcoord1;
  168. float2 faceUV = TRANSFORM_TEX(textureUV, _FaceTex);
  169. float2 outlineUV = TRANSFORM_TEX(textureUV, _OutlineTex);
  170. if (_UIVertexColorAlwaysGammaSpace && !IsGammaSpace())
  171. {
  172. input.color.rgb = UIGammaToLinear(input.color.rgb);
  173. }
  174. float4 color = input.color;
  175. #if (FORCE_LINEAR && !UNITY_COLORSPACE_GAMMA)
  176. color = SRGBToLinear(input.color);
  177. #endif
  178. output.position = vPosition;
  179. output.color = color;
  180. output.atlas = input.texcoord0;
  181. output.weight = weight;
  182. output.mask = half2(vert.xy * 2 - clampedRect.xy - clampedRect.zw);
  183. output.viewDir = mul((float3x3)_EnvMatrix, _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, vert).xyz);
  184. #if (UNDERLAY_ON || UNDERLAY_INNER)
  185. output.texcoord2 = input.texcoord0 + bOffset;
  186. output.underlayColor = underlayColor;
  187. #endif
  188. output.textures = float4(faceUV, outlineUV);
  189. return output;
  190. }
  191. fixed4 PixShader(pixel_t input) : SV_Target
  192. {
  193. UNITY_SETUP_INSTANCE_ID(input);
  194. float c = tex2D(_MainTex, input.atlas).a;
  195. float pixelSize = abs(ddx(input.atlas.y)) + abs(ddy(input.atlas.y));
  196. pixelSize *= _TextureHeight * 0.75;
  197. float scale = 1 / pixelSize * _GradientScale * (_Sharpness + 1);
  198. float weight = input.weight;
  199. float bias = (.5 - weight) + (.5 / scale);
  200. float sd = (bias - c) * scale;
  201. float outline = (_OutlineWidth * _ScaleRatioA) * scale;
  202. float softness = (_OutlineSoftness * _ScaleRatioA) * scale;
  203. half4 faceColor = _FaceColor;
  204. half4 outlineColor = _OutlineColor;
  205. faceColor.rgb *= input.color.rgb;
  206. faceColor *= tex2D(_FaceTex, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y);
  207. outlineColor *= tex2D(_OutlineTex, input.textures.zw + float2(_OutlineUVSpeedX, _OutlineUVSpeedY) * _Time.y);
  208. faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
  209. #if BEVEL_ON
  210. float3 dxy = float3(0.5 / _TextureWidth, 0.5 / _TextureHeight, 0);
  211. float3 n = GetSurfaceNormal(input.atlas, weight, dxy);
  212. float3 bump = UnpackNormal(tex2D(_BumpMap, input.textures.xy + float2(_FaceUVSpeedX, _FaceUVSpeedY) * _Time.y)).xyz;
  213. bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
  214. n = normalize(n - bump);
  215. float3 light = normalize(float3(sin(_LightAngle), cos(_LightAngle), -1.0));
  216. float3 col = GetSpecular(n, light);
  217. faceColor.rgb += col * faceColor.a;
  218. faceColor.rgb *= 1 - (dot(n, light) * _Diffuse);
  219. faceColor.rgb *= lerp(_Ambient, 1, n.z * n.z);
  220. fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDir, -n));
  221. faceColor.rgb += reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  222. #endif
  223. #if (UNDERLAY_ON || UNDERLAY_INNER)
  224. float bScale = scale;
  225. bScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * bScale);
  226. float bBias = (0.5 - weight) * bScale - 0.5 - ((_UnderlayDilate * _ScaleRatioC) * 0.5 * bScale);
  227. #endif
  228. #if UNDERLAY_ON
  229. float d = tex2D(_MainTex, input.texcoord2.xy).a * bScale;
  230. faceColor += input.underlayColor * saturate(d - bBias) * (1 - faceColor.a);
  231. #endif
  232. #if UNDERLAY_INNER
  233. float d = tex2D(_MainTex, input.texcoord2.xy).a * bScale;
  234. faceColor += input.underlayColor * (1 - saturate(d - bBias)) * saturate(1 - sd) * (1 - faceColor.a);
  235. #endif
  236. #if GLOW_ON
  237. float4 glowColor = GetGlowColor(sd, scale);
  238. faceColor.rgb += glowColor.rgb * glowColor.a;
  239. #endif
  240. // Alternative implementation to UnityGet2DClipping with support for softness.
  241. #if UNITY_UI_CLIP_RECT
  242. half2 maskSoftness = half2(max(_UIMaskSoftnessX, _MaskSoftnessX), max(_UIMaskSoftnessY, _MaskSoftnessY));
  243. float2 maskZW = 0.25 / (0.25 * maskSoftness + 1 / scale);
  244. half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * maskZW);
  245. faceColor *= m.x * m.y;
  246. #endif
  247. #if UNITY_UI_ALPHACLIP
  248. clip(faceColor.a - 0.001);
  249. #endif
  250. return faceColor * input.color.a;
  251. }
  252. ENDCG
  253. }
  254. }
  255. Fallback "TextMeshPro/Mobile/Distance Field"
  256. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  257. }