Facebook
From Idiotic Meerkat, 1 Year ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 70
  1. // Simplified SDF shader:
  2. // - No Shading Option (bevel / bump / env map)
  3. // - No Glow Option
  4. // - Softness is applied on both side of the outline
  5.  
  6. Shader "BikeStunt/3D Text" {
  7.  
  8. Properties {
  9.         [HDR]_FaceColor     ("Face Color", Color) = (1,1,1,1)
  10.         _FaceDilate                     ("Face Dilate", Range(-1,1)) = 0
  11.  
  12.         [HDR]_OutlineColor      ("Outline Color", Color) = (0,0,0,1)
  13.         _OutlineWidth           ("Outline Thickness", Range(0,1)) = 0
  14.         _OutlineSoftness        ("Outline Softness", Range(0,1)) = 0
  15.  
  16.         [HDR]_UnderlayColor     ("Border Color", Color) = (0,0,0,.5)
  17.         _UnderlayOffsetX        ("Border OffsetX", Range(-1,1)) = 0
  18.         _UnderlayOffsetY        ("Border OffsetY", Range(-1,1)) = 0
  19.         _UnderlayDilate         ("Border Dilate", Range(-1,1)) = 0
  20.         _UnderlaySoftness       ("Border Softness", Range(0,1)) = 0
  21.  
  22.         _WeightNormal           ("Weight Normal", float) = 0
  23.         _WeightBold                     ("Weight Bold", float) = .5
  24.  
  25.         _ShaderFlags            ("Flags", float) = 0
  26.         _ScaleRatioA            ("Scale RatioA", float) = 1
  27.         _ScaleRatioB            ("Scale RatioB", float) = 1
  28.         _ScaleRatioC            ("Scale RatioC", float) = 1
  29.  
  30.         _MainTex                        ("Font Atlas", 2D) = "white" {}
  31.         _TextureWidth           ("Texture Width", float) = 512
  32.         _TextureHeight          ("Texture Height", float) = 512
  33.         _GradientScale          ("Gradient Scale", float) = 5
  34.         _ScaleX                         ("Scale X", float) = 1
  35.         _ScaleY                         ("Scale Y", float) = 1
  36.         _PerspectiveFilter      ("Perspective Correction", Range(0, 1)) = 0.875
  37.         _Sharpness                      ("Sharpness", Range(-1,1)) = 0
  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.         _CullMode                       ("Cull Mode", Float) = 0
  53.         _ColorMask                      ("Color Mask", Float) = 15
  54. }
  55.  
  56. SubShader {
  57.         Tags
  58.         {
  59.                 "Queue"="Transparent"
  60.                 "IgnoreProjector"="True"
  61.                 "RenderType"="Transparent"
  62.         }
  63.  
  64.  
  65.         Stencil
  66.         {
  67.                 Ref [_Stencil]
  68.                 Comp [_StencilComp]
  69.                 Pass [_StencilOp]
  70.                 ReadMask [_StencilReadMask]
  71.                 WriteMask [_StencilWriteMask]
  72.         }
  73.  
  74.         Cull [_CullMode]
  75.         ZWrite On //Off
  76.         Lighting Off
  77.         Fog { Mode Off }
  78.         ZTest [unity_GUIZTestMode]
  79.         Blend One OneMinusSrcAlpha
  80.         ColorMask [_ColorMask]
  81.  
  82.         Pass {
  83.                 CGPROGRAM
  84.                 #pragma vertex VertShader
  85.                 #pragma fragment PixShader
  86.                 #pragma shader_feature __ OUTLINE_ON
  87.                 #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
  88.  
  89.                 #pragma multi_compile __ UNITY_UI_CLIP_RECT
  90.                 #pragma multi_compile __ UNITY_UI_ALPHACLIP
  91.  
  92.                 #include "UnityCG.cginc"
  93.                 #include "UnityUI.cginc"
  94.                 #include "TMPro_Properties.cginc"
  95.  
  96.                 struct vertex_t {
  97.                         float4  vertex                  : POSITION;
  98.                         half3   normal                  : NORMAL;
  99.                         fixed4  color                   : COLOR;
  100.                         half2   texcoord0               : TEXCOORD0;
  101.                         half2   texcoord1               : TEXCOORD1;
  102.                 };
  103.  
  104.                 struct pixel_t {
  105.                         float4  vertex                  : SV_POSITION;
  106.                         fixed4  faceColor               : COLOR;
  107.                         fixed4  outlineColor    : COLOR1;
  108.                         half4   texcoord0               : TEXCOORD0;                    // Texture UV, Mask UV
  109.                         half4   param                   : TEXCOORD1;                    // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
  110.                         half4   mask                    : TEXCOORD2;                    // Position in clip space(xy), Softness(zw)
  111.                         #if (UNDERLAY_ON | UNDERLAY_INNER)
  112.                         half4   texcoord1               : TEXCOORD3;                    // Texture UV, alpha, reserved
  113.                         half2   underlayParam   : TEXCOORD4;                    // Scale(x), Bias(y)
  114.                         #endif
  115.                 };
  116.  
  117.  
  118.                 pixel_t VertShader(vertex_t input)
  119.                 {
  120.                         pixel_t output;
  121.                         UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  122.  
  123.                         half bold = step(input.texcoord1.y, half(0));
  124.  
  125.                         half4 vert = input.vertex;
  126.                         vert.x += _VertexOffsetX;
  127.                         vert.y += _VertexOffsetY;
  128.                         float4 vPosition = UnityObjectToClipPos(vert);
  129.  
  130.                         half2 pixelSize = vPosition.w;
  131.                         pixelSize /= half2(_ScaleX, _ScaleY) * abs(mul((half2x2)UNITY_MATRIX_P, _ScreenParams.xy));
  132.  
  133.                         half scale = rsqrt(dot(pixelSize, pixelSize));
  134.                         scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + half(1));
  135.  
  136.                         half weight = lerp(_WeightNormal, _WeightBold, bold) / half(4.0);
  137.                         weight = (weight + _FaceDilate) * _ScaleRatioA * half(0.5);
  138.  
  139.                         half layerScale = scale;
  140.  
  141.                         scale /= half(1) + (_OutlineSoftness * _ScaleRatioA * scale);
  142.                         half bias = (half(0.5) - weight) * scale - half(0.5);
  143.                         half outline = _OutlineWidth * _ScaleRatioA * half(0.5) * scale;
  144.  
  145.                         half opacity = input.color.a;
  146.                         #if (UNDERLAY_ON | UNDERLAY_INNER)
  147.                         opacity = half(1.0);
  148.                         #endif
  149.  
  150.                         fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
  151.                         faceColor.rgb *= faceColor.a;
  152.  
  153.                         fixed4 outlineColor = _OutlineColor;
  154.                         outlineColor.a *= opacity;
  155.                         outlineColor.rgb *= outlineColor.a;
  156.                         outlineColor = lerp(faceColor, outlineColor, sqrt(min(half(1.0), (outline * half(2)))));
  157.  
  158.                         #if (UNDERLAY_ON | UNDERLAY_INNER)
  159.                         layerScale /= half(1) + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  160.                         half layerBias = (half(.5) - weight) * layerScale - half(.5) - ((_UnderlayDilate * _ScaleRatioC) * half(.5) * layerScale);
  161.  
  162.                         half x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  163.                         half y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  164.                         half2 layerOffset = half2(x, y);
  165.                         #endif
  166.  
  167.                         // Generate UV for the Masking Texture
  168.                         half4 clampedRect = clamp(_ClipRect, half(-2e10), half(2e10));
  169.                         half2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  170.  
  171.                         // Populate structure for pixel shader
  172.                         output.vertex = vPosition;
  173.                         output.faceColor = faceColor;
  174.                         output.outlineColor = outlineColor;
  175.                         output.texcoord0 = half4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y);
  176.                         output.param = half4(scale, bias - outline, bias + outline, bias);
  177.                         output.mask = half4(vert.xy * half(2) - clampedRect.xy - clampedRect.zw, half(0.25) / (half(0.25) * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
  178.                         #if (UNDERLAY_ON || UNDERLAY_INNER)
  179.                         output.texcoord1 = half4(input.texcoord0 + layerOffset, input.color.a, half(0));
  180.                         output.underlayParam = half2(layerScale, layerBias);
  181.                         #endif
  182.  
  183.                         return output;
  184.                 }
  185.  
  186.  
  187.                 // PIXEL SHADER
  188.                 fixed4 PixShader(pixel_t input) : SV_Target
  189.                 {
  190.                         half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
  191.                         half4 c = input.faceColor * saturate(d - input.param.w);
  192.  
  193.                         #ifdef OUTLINE_ON
  194.                         c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
  195.                         c *= saturate(d - input.param.y);
  196.                         #endif
  197.  
  198.                         #if UNDERLAY_ON
  199.                         d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  200.                         c += half4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (half(1) - c.a);
  201.                         #endif
  202.  
  203.                         #if UNDERLAY_INNER
  204.                         half sd = saturate(d - input.param.z);
  205.                         d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
  206.                         c += half4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (half(1) - saturate(d - input.underlayParam.y)) * sd * (half(1) - c.a);
  207.                         #endif
  208.  
  209.                         // Alternative implementation to UnityGet2DClipping with support for softness.
  210.                         #if UNITY_UI_CLIP_RECT
  211.                         half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
  212.                         c *= m.x * m.y;
  213.                         #endif
  214.  
  215.                         #if (UNDERLAY_ON | UNDERLAY_INNER)
  216.                         c *= input.texcoord1.z;
  217.                         #endif
  218.  
  219.                         return c;
  220.                 }
  221.                 ENDCG
  222.         }
  223. }
  224.  
  225. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
  226. }
  227.