Search Unity

Text Mesh Pro, world space mask

Discussion in 'UGUI & TextMesh Pro' started by Aleksander87, Jul 24, 2019.

  1. Aleksander87

    Aleksander87

    Joined:
    Jul 7, 2017
    Posts:
    20
    Hi!
    I've the need to implement a world space mask on TextMeshPro component.
    I saw the shader "Distance Field - Masking", but it does that in object space with a clip rect. Is it possible to get the same behaviour in world space?

    Thank you in advance
     
  2. BBO_Lagoon

    BBO_Lagoon

    Joined:
    Mar 2, 2017
    Posts:
    200
    Hi,
    Better late than never... in case someone still need this.
    I didn't remember where I found this Shader but it's very usefull and I think must be included in TextMeshPro package.

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Copyright (C) 2014 - 2016 Stephan Schaem - All Rights Reserved
    4. // This code can only be used under the standard Unity Asset Store End User License Agreement
    5. // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms
    6.  
    7. // Simplified SDF shader:
    8. // - No Shading Option (bevel / bump / env map)
    9. // - No Glow Option
    10. // - Softness is applied on both side of the outline
    11.  
    12. Shader "TextMeshPro/Mobile/Distance Field - Worldspace Masking" {
    13.  
    14. Properties {
    15.     _FaceColor            ("Face Color", Color) = (1,1,1,1)
    16.     _FaceDilate            ("Face Dilate", Range(-1,1)) = 0
    17.  
    18.     _OutlineColor        ("Outline Color", Color) = (0,0,0,1)
    19.     _OutlineWidth        ("Outline Thickness", Range(0,1)) = 0
    20.     _OutlineSoftness    ("Outline Softness", Range(0,1)) = 0
    21.  
    22.     _UnderlayColor        ("Border Color", Color) = (0,0,0,.5)
    23.     _UnderlayOffsetX     ("Border OffsetX", Range(-1,1)) = 0
    24.     _UnderlayOffsetY     ("Border OffsetY", Range(-1,1)) = 0
    25.     _UnderlayDilate        ("Border Dilate", Range(-1,1)) = 0
    26.     _UnderlaySoftness     ("Border Softness", Range(0,1)) = 0
    27.  
    28.     _WeightNormal        ("Weight Normal", float) = 0
    29.     _WeightBold            ("Weight Bold", float) = .5
    30.  
    31.     _ShaderFlags        ("Flags", float) = 0
    32.     _ScaleRatioA        ("Scale RatioA", float) = 1
    33.     _ScaleRatioB        ("Scale RatioB", float) = 1
    34.     _ScaleRatioC        ("Scale RatioC", float) = 1
    35.  
    36.     _MainTex            ("Font Atlas", 2D) = "white" {}
    37.     _TextureWidth        ("Texture Width", float) = 512
    38.     _TextureHeight        ("Texture Height", float) = 512
    39.     _GradientScale        ("Gradient Scale", float) = 5
    40.     _ScaleX                ("Scale X", float) = 1
    41.     _ScaleY                ("Scale Y", float) = 1
    42.     _PerspectiveFilter    ("Perspective Correction", Range(0, 1)) = 0.875
    43.  
    44.     _VertexOffsetX        ("Vertex OffsetX", float) = 0
    45.     _VertexOffsetY        ("Vertex OffsetY", float) = 0
    46.  
    47.     _ClipRect            ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
    48.     _MaskSoftnessX        ("Mask SoftnessX", float) = 0
    49.     _MaskSoftnessY        ("Mask SoftnessY", float) = 0
    50.  
    51.     _StencilComp        ("Stencil Comparison", Float) = 8
    52.     _Stencil            ("Stencil ID", Float) = 0
    53.     _StencilOp            ("Stencil Operation", Float) = 0
    54.     _StencilWriteMask    ("Stencil Write Mask", Float) = 255
    55.     _StencilReadMask    ("Stencil Read Mask", Float) = 255
    56.  
    57.     _ColorMask            ("Color Mask", Float) = 15
    58. }
    59.  
    60. SubShader {
    61.     Tags
    62.     {
    63.         "Queue"="Transparent"
    64.         "IgnoreProjector"="True"
    65.         "RenderType"="Transparent"
    66.     }
    67.  
    68.  
    69.     Stencil
    70.     {
    71.         Ref [_Stencil]
    72.         Comp [_StencilComp]
    73.         Pass [_StencilOp]
    74.         ReadMask [_StencilReadMask]
    75.         WriteMask [_StencilWriteMask]
    76.     }
    77.  
    78.     Cull [_CullMode]
    79.     ZWrite Off
    80.     Lighting Off
    81.     Fog { Mode Off }
    82.     ZTest [unity_GUIZTestMode]
    83.     Blend One OneMinusSrcAlpha
    84.     ColorMask [_ColorMask]
    85.  
    86.     Pass {
    87.         CGPROGRAM
    88.         #pragma vertex VertShader
    89.         #pragma fragment PixShader
    90.         #pragma shader_feature __ OUTLINE_ON
    91.         #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
    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.         pixel_t VertShader(vertex_t input)
    120.         {
    121.             float bold = step(input.texcoord1.y, 0);
    122.  
    123.             float4 vert = input.vertex;
    124.             vert.x += _VertexOffsetX;
    125.             vert.y += _VertexOffsetY;
    126.             float4 vPosition = UnityObjectToClipPos(vert);
    127.  
    128.             float2 pixelSize = vPosition.w;
    129.             pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    130.          
    131.             float scale = rsqrt(dot(pixelSize, pixelSize));
    132.             scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
    133.             if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
    134.  
    135.             float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
    136.             weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
    137.  
    138.             float layerScale = scale;
    139.  
    140.             scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
    141.             float bias = (0.5 - weight) * scale - 0.5;
    142.             float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
    143.  
    144.             float opacity = input.color.a;
    145.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    146.                 opacity = 1.0;
    147.         #endif
    148.  
    149.             fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
    150.             faceColor.rgb *= faceColor.a;
    151.  
    152.             fixed4 outlineColor = _OutlineColor;
    153.             outlineColor.a *= opacity;
    154.             outlineColor.rgb *= outlineColor.a;
    155.             outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
    156.  
    157.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    158.  
    159.             layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
    160.             float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
    161.  
    162.             float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
    163.             float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
    164.             float2 layerOffset = float2(x, y);
    165.         #endif
    166.  
    167.             // Generate UV for the Masking Texture
    168.             float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
    169.             float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
    170.  
    171.             // Convert vertex position to world space position
    172.             vert = mul(unity_ObjectToWorld, vert);
    173.  
    174.             // Structure for pixel shader
    175.             pixel_t output = {
    176.                 vPosition,
    177.                 faceColor,
    178.                 outlineColor,
    179.                 float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
    180.                 half4(scale, bias - outline, bias + outline, bias),
    181.                 half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
    182.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    183.                 float4(input.texcoord0 + layerOffset, input.color.a, 0),
    184.                 half2(layerScale, layerBias),
    185.             #endif
    186.             };
    187.  
    188.             return output;
    189.         }
    190.  
    191.  
    192.         // PIXEL SHADER
    193.         fixed4 PixShader(pixel_t input) : SV_Target
    194.         {
    195.             half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
    196.             half4 c = input.faceColor * saturate(d - input.param.w);
    197.  
    198.         #ifdef OUTLINE_ON
    199.             c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
    200.             c *= saturate(d - input.param.y);
    201.         #endif
    202.  
    203.         #if UNDERLAY_ON
    204.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    205.             c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
    206.         #endif
    207.  
    208.         #if UNDERLAY_INNER
    209.             half sd = saturate(d - input.param.z);
    210.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    211.             c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
    212.         #endif
    213.  
    214.        
    215.         /*#if UNITY_VERSION < 530
    216.             // Unity 5.2 2D Rect Mask Support
    217.             if (_UseClipRect)
    218.             {
    219.                 half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
    220.                 c *= m.x * m.y;
    221.             }
    222.         #else*/
    223.             // Alternative implementation to UnityGet2DClipping with support for softness.
    224.             half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
    225.             c *= m.x * m.y;
    226.         //#endif
    227.  
    228.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    229.             c *= input.texcoord1.z;
    230.         #endif
    231.  
    232.             clip(c.a - 0.001);
    233.  
    234.             return c;
    235.         }
    236.         ENDCG
    237.     }
    238. }
    239.  
    240. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    241. }
    242.  
    Still working with Unity 2021.1 and URP.