Search Unity

TextMesh Pro Cutout shader in order to get depth and shadows

Discussion in 'UGUI & TextMesh Pro' started by Johannski, Jun 8, 2017.

  1. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I'm using textmesh pro also ingame and I'm getting ugly post processing effects on the gui because of missing depth information. Is there a working cutout shader for the signed distance fields out there? I found this thread: http://digitalnativestudios.com/forum/index.php?topic=506.msg3800#msg3800 but the solution proposed there doesn't work anymore, since all shaders are now vertex fragment shaders.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    There hasn't been any fundamental changes to the surface shaders which would prevent this previous user implementation to work.
     
  3. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hi Stephan,

    thanks for the quick reply. I somehow missed the surface shaders in the plugin. Then it should indeed be no problem. Thanks a lot, I'll post the modified shader once finished :)

    Edit: Alrighty, that was actually really easy. Here is the modified shader for mobile surface:

    Code (CSharp):
    1.  
    2. // Simplified version of the SDF Surface shader :
    3. // - No support for Bevel, Bump or envmap
    4. // - Diffuse only lighting
    5. // - Fully supports only 1 directional light. Other lights can affect it, but it will be per-vertex/SH.
    6.  
    7. Shader "TextMeshPro/Mobile/Distance Field (Cutout)" {
    8.  
    9. Properties {
    10.     _FaceTex            ("Fill Texture", 2D) = "white" {}
    11.     _FaceColor            ("Fill Color", Color) = (1,1,1,1)
    12.     _FaceDilate            ("Face Dilate", Range(-1,1)) = 0
    13.  
    14.     _OutlineColor        ("Outline Color", Color) = (0,0,0,1)
    15.     _OutlineTex            ("Outline Texture", 2D) = "white" {}
    16.     _OutlineWidth        ("Outline Thickness", Range(0, 1)) = 0
    17.     _OutlineSoftness        ("Outline Softness", Range(0,1)) = 0
    18.  
    19.     _GlowColor            ("Color", Color) = (0, 1, 0, 0.5)
    20.     _GlowOffset            ("Offset", Range(-1,1)) = 0
    21.     _GlowInner            ("Inner", Range(0,1)) = 0.05
    22.     _GlowOuter            ("Outer", Range(0,1)) = 0.05
    23.     _GlowPower            ("Falloff", Range(1, 0)) = 0.75
    24.  
    25.     _WeightNormal        ("Weight Normal", float) = 0
    26.     _WeightBold            ("Weight Bold", float) = 0.5
    27.  
    28.     // Should not be directly exposed to the user
    29.     _ShaderFlags        ("Flags", float) = 0
    30.     _ScaleRatioA        ("Scale RatioA", float) = 1
    31.     _ScaleRatioB        ("Scale RatioB", float) = 1
    32.     _ScaleRatioC        ("Scale RatioC", float) = 1
    33.  
    34.     _MainTex            ("Font Atlas", 2D) = "white" {}
    35.     _TextureWidth        ("Texture Width", float) = 512
    36.     _TextureHeight        ("Texture Height", float) = 512
    37.     _GradientScale        ("Gradient Scale", float) = 5.0
    38.     _ScaleX                ("Scale X", float) = 1.0
    39.     _ScaleY                ("Scale Y", float) = 1.0
    40.     _PerspectiveFilter    ("Perspective Correction", Range(0, 1)) = 0.875
    41.  
    42.     _VertexOffsetX        ("Vertex OffsetX", float) = 0
    43.     _VertexOffsetY        ("Vertex OffsetY", float) = 0
    44.    
    45.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    46.    
    47.     //_MaskCoord        ("Mask Coords", vector) = (0,0,0,0)
    48.     //_MaskSoftness        ("Mask Softness", float) = 0
    49. }
    50.  
    51. SubShader {
    52.  
    53.     Tags {
    54.         "Queue"="AlphaTest"
    55.         "IgnoreProjector"="True"
    56.         "RenderType"="TransparentCutout"
    57.     }
    58.  
    59.     LOD 300
    60.     Cull [_CullMode]
    61.  
    62.     CGPROGRAM
    63.     #pragma surface PixShader Lambert vertex:VertShader noforwardadd nolightmap nodirlightmap fullforwardshadows addshadow alphatest:_Cutoff
    64.     #pragma target 3.0
    65.     #pragma shader_feature __ GLOW_ON
    66.  
    67.     #include "TMPro_Properties.cginc"
    68.     #include "TMPro.cginc"
    69.  
    70.     half _FaceShininess;
    71.     half _OutlineShininess;
    72.  
    73.     struct Input
    74.     {
    75.         fixed4    color        : COLOR;
    76.         float2    uv_MainTex;
    77.         float2    uv2_FaceTex;
    78.         float2  uv2_OutlineTex;
    79.         float2    param;                    // Weight, Scale
    80.         float3    viewDirEnv;      
    81.     };
    82.  
    83.     #include "TMPro_Surface.cginc"
    84.  
    85.     ENDCG
    86.  
    87.     // Pass to render object as a shadow caster
    88.     Pass
    89.     {
    90.         Name "Caster"
    91.         Tags { "LightMode" = "ShadowCaster" "RenderType"="TransparentCutout"}
    92.         Offset 1, 1
    93.  
    94.         Fog {Mode Off}
    95.         ZWrite On ZTest LEqual Cull Off
    96.  
    97.         CGPROGRAM
    98.         #pragma vertex vert
    99.         #pragma fragment frag
    100.         #pragma multi_compile_shadowcaster
    101.         #include "UnityCG.cginc"
    102.  
    103.         struct v2f {
    104.             V2F_SHADOW_CASTER;
    105.             float2    uv            : TEXCOORD1;
    106.             float2    uv2            : TEXCOORD3;
    107.             float    alphaClip    : TEXCOORD2;
    108.         };
    109.  
    110.         uniform float4 _MainTex_ST;
    111.         uniform float4 _OutlineTex_ST;
    112.         float _OutlineWidth;
    113.         float _FaceDilate;
    114.         float _ScaleRatioA;
    115.  
    116.         v2f vert( appdata_base v )
    117.         {
    118.             v2f o;
    119.             TRANSFER_SHADOW_CASTER(o)
    120.             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    121.             o.uv2 = TRANSFORM_TEX(v.texcoord, _OutlineTex);
    122.             o.alphaClip = (1.0 - _OutlineWidth * _ScaleRatioA - _FaceDilate * _ScaleRatioA) / 2.0;
    123.             return o;
    124.         }
    125.  
    126.         uniform sampler2D _MainTex;
    127.  
    128.         float4 frag(v2f i) : COLOR
    129.         {
    130.             fixed4 texcol = tex2D(_MainTex, i.uv).a;
    131.             clip(texcol.a - i.alphaClip);
    132.             SHADOW_CASTER_FRAGMENT(i)
    133.         }
    134.         ENDCG
    135.     }
    136. }
    137.  
    138. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    139. }
     
    Last edited: Jul 3, 2017
    TrentSterling, TeuTeuf, SQR and 2 others like this.