Search Unity

Distance Field (Surface) not working

Discussion in 'UGUI & TextMesh Pro' started by LSThimo, Jan 31, 2019.

  1. LSThimo

    LSThimo

    Joined:
    Sep 14, 2017
    Posts:
    5
    Hey everyone,

    The Distance Field (Surface) shader isn't working for me, the normal Distance Field shader works fine.
    I'm working in Unity 2016.2.16f1 and I'm using the HDRP.


    upload_2019-1-31_17-58-23.png
     
    sj631 likes this.
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Indeed (and sadly for the time being), the surface versions of the Distance Field shaders do not work with SRP.

    When I have time, I do plan on providing revised shaders (not surface shaders since that doesn't work with SRP) to replace the functionality of these shaders.
     
    anolan23 and LSThimo like this.
  3. anolan23

    anolan23

    Joined:
    Nov 15, 2019
    Posts:
    1
    Any update on this?
     
  4. dval

    dval

    Joined:
    Jul 25, 2012
    Posts:
    24
    Been over a year. Still broken.
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Hoping to have a set of HDRP compatible shaders with support for Shader Graph available within the next 60 days.

    Some of the required functionality for this was just added to Shader Graph in 2020.2 alpha. Waiting on seeing if these changes can be backported to 2020.1 and maybe 2019.3... will provide an update when I have more information.
     
  6. Duckocide

    Duckocide

    Joined:
    Aug 30, 2014
    Posts:
    35
    @Stephan_B Any news on the back port to 2020.1 ?
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The functionality we need from Shader Graph isn't there yet but was told that it will available for 2019.3 or newer versions of Unity.
     
  8. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    Hey @Stephan_B , do you know which version of SRP this feature is scheduled for? Is it 9.0?
     
  9. zee_ps19

    zee_ps19

    Joined:
    Jun 13, 2020
    Posts:
    2
    I am using 2019.4 LTS, still facing the same issue with HDRP. Don't know what unity is up to.
     
  10. ESludden

    ESludden

    Joined:
    Aug 27, 2012
    Posts:
    19
    I am also using 2019.4 LTS and need localisation and TextMeshPro with ingame signage. Currently the text we use in game is not being lit by level lights.
     
  11. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    Hello, I'm trying to use the SDF material (Surface) on a textmeshpro asset, and I'd like to add a +1 request for this (Unity 2020).
     
  12. ShadowLairGames

    ShadowLairGames

    Joined:
    Jan 17, 2019
    Posts:
    45
    Bump for still waiting on this on 2020.1.6

    Edit:
    Meanwhile, as a workaround, I managed to solve lit text by tinkering with the Mobile Distance Field shader included with TMP.


    For those interested, this is the updated shader:

    Code (CSharp):
    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 "TextMeshPro/Mobile/Distance Field Shadowed" {
    7.  
    8. Properties {
    9.     _FaceColor            ("Face Color", Color) = (1,1,1,1)
    10.     _FaceDilate            ("Face Dilate", Range(-1,1)) = 0
    11.  
    12.     _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.     _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.     _ColorMask            ("Color Mask", Float) = 15
    53. }
    54.  
    55. HLSLINCLUDE
    56. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
    57. #include "Packages/com.unity.shadergraph/ShaderGraphLibrary/ShaderVariablesFunctions.hlsl"
    58. ENDHLSL
    59.  
    60. SubShader {
    61.     Tags
    62.     {
    63.         "Queue"="Transparent"
    64.         "IgnoreProjector"="True"
    65.         "RenderType"="Transparent"
    66.         "RenderPipeline" = "UniversalPipeline"
    67.     }
    68.  
    69.  
    70.     Stencil
    71.     {
    72.         Ref [_Stencil]
    73.         Comp [_StencilComp]
    74.         Pass [_StencilOp]
    75.         ReadMask [_StencilReadMask]
    76.         WriteMask [_StencilWriteMask]
    77.     }
    78.  
    79.     Cull [_CullMode]
    80.     ZWrite Off
    81.     Lighting Off
    82.     Fog { Mode Off }
    83.     ZTest [unity_GUIZTestMode]
    84.     Blend One OneMinusSrcAlpha
    85.     ColorMask [_ColorMask]
    86.  
    87.     Pass {
    88.         HLSLPROGRAM
    89.         #pragma prefer_hlslcc gles
    90.  
    91.         #pragma vertex VertShader
    92.         #pragma fragment PixShader
    93.         #pragma shader_feature __ OUTLINE_ON
    94.         #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
    95.  
    96.         #pragma multi_compile __ UNITY_UI_CLIP_RECT
    97.         #pragma multi_compile __ UNITY_UI_ALPHACLIP
    98.  
    99.         #pragma multi_compile USE_SHAPE_LIGHT_TYPE_0 __
    100.         #pragma multi_compile USE_SHAPE_LIGHT_TYPE_1 __
    101.         #pragma multi_compile USE_SHAPE_LIGHT_TYPE_2 __
    102.         #pragma multi_compile USE_SHAPE_LIGHT_TYPE_3 __
    103.  
    104.         #include "TMPro_Properties_HLSL.cginc"
    105.  
    106.         struct vertex_t {
    107.             UNITY_VERTEX_INPUT_INSTANCE_ID
    108.             float4    vertex            : POSITION;
    109.             float3    normal            : NORMAL;
    110.             float4  tangent            : TANGENT;
    111.             half4    color            : COLOR;
    112.             float2    texcoord0        : TEXCOORD0;
    113.             float2    texcoord1        : TEXCOORD1;
    114.         };
    115.  
    116.         struct pixel_t {
    117.             UNITY_VERTEX_INPUT_INSTANCE_ID
    118.             UNITY_VERTEX_OUTPUT_STEREO
    119.             float4    vertex            : SV_POSITION;
    120.             half4    faceColor        : COLOR;
    121.             half4    outlineColor    : COLOR1;
    122.             float4    texcoord0        : TEXCOORD0;            // Texture UV, Mask UV
    123.             half4    param            : TEXCOORD1;            // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
    124.             half4    mask            : TEXCOORD2;            // Position in clip space(xy), Softness(zw)
    125.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    126.             float4    texcoord1        : TEXCOORD3;            // Texture UV, alpha, reserved
    127.             half2    underlayParam    : TEXCOORD4;            // Scale(x), Bias(y)
    128.             #endif
    129.  
    130.             float2 lightingUV : TEXCOORD5;
    131.         };
    132.  
    133.         #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/LightingUtility.hlsl"
    134.  
    135.         #if USE_SHAPE_LIGHT_TYPE_0
    136.         SHAPE_LIGHT(0)
    137.         #endif
    138.  
    139.         #if USE_SHAPE_LIGHT_TYPE_1
    140.         SHAPE_LIGHT(1)
    141.         #endif
    142.  
    143.         #if USE_SHAPE_LIGHT_TYPE_2
    144.         SHAPE_LIGHT(2)
    145.         #endif
    146.  
    147.         #if USE_SHAPE_LIGHT_TYPE_3
    148.         SHAPE_LIGHT(3)
    149.         #endif
    150.  
    151.         pixel_t VertShader(vertex_t input)
    152.         {
    153.             pixel_t output = (pixel_t)0;
    154.  
    155.             UNITY_SETUP_INSTANCE_ID(input);
    156.             UNITY_TRANSFER_INSTANCE_ID(input, output);
    157.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
    158.  
    159.             float bold = step(input.texcoord1.y, 0);
    160.  
    161.             float4 vert = input.vertex;
    162.             vert.x += _VertexOffsetX;
    163.             vert.y += _VertexOffsetY;
    164.             float4 vPosition = TransformObjectToHClip(vert);
    165.  
    166.             float2 pixelSize = vPosition.w;
    167.             pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    168.            
    169.             float scale = rsqrt(dot(pixelSize, pixelSize));
    170.             scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + 1);
    171.  
    172.  
    173.             VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normal, input.tangent);
    174.             if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(vertexNormalInput.normalWS, normalize(GetWorldSpaceNormalizeViewDir(vert)))));
    175.  
    176.             float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
    177.             weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
    178.  
    179.             float layerScale = scale;
    180.  
    181.             scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
    182.             float bias = (0.5 - weight) * scale - 0.5;
    183.             float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
    184.  
    185.             float opacity = input.color.a;
    186.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    187.             opacity = 1.0;
    188.             #endif
    189.  
    190.             half4 faceColor = half4(input.color.rgb, opacity) * _FaceColor;
    191.             faceColor.rgb *= faceColor.a;
    192.  
    193.             half4 outlineColor = _OutlineColor;
    194.             outlineColor.a *= opacity;
    195.             outlineColor.rgb *= outlineColor.a;
    196.             outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
    197.  
    198.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    199.             layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
    200.             float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
    201.  
    202.             float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
    203.             float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
    204.             float2 layerOffset = float2(x, y);
    205.             #endif
    206.  
    207.             // Generate UV for the Masking Texture
    208.             float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
    209.             float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
    210.  
    211.             // Populate structure for pixel shader
    212.             output.vertex = vPosition;
    213.             output.faceColor = faceColor;
    214.             output.outlineColor = outlineColor;
    215.             output.texcoord0 = float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y);
    216.             output.param = half4(scale, bias - outline, bias + outline, bias);
    217.             output.mask = half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy));
    218.             #if (UNDERLAY_ON || UNDERLAY_INNER)
    219.             output.texcoord1 = float4(input.texcoord0 + layerOffset, input.color.a, 0);
    220.             output.underlayParam = half2(layerScale, layerBias);
    221.             #endif
    222.  
    223.             float4 clipVertex = output.vertex / output.vertex.w;
    224.             output.lightingUV = ComputeScreenPos(clipVertex).xy;
    225.  
    226.             return output;
    227.         }
    228.  
    229.         #include "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/CombinedShapeLightShared.hlsl"
    230.  
    231.         // PIXEL SHADER
    232.         half4 PixShader(pixel_t input) : SV_Target
    233.         {
    234.             UNITY_SETUP_INSTANCE_ID(input);
    235.            
    236.             half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
    237.             half4 c = input.faceColor * saturate(d - input.param.w);
    238.  
    239.             #ifdef OUTLINE_ON
    240.             c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
    241.             c *= saturate(d - input.param.y);
    242.             #endif
    243.  
    244.             #if UNDERLAY_ON
    245.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    246.             c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
    247.             #endif
    248.  
    249.             #if UNDERLAY_INNER
    250.             half sd = saturate(d - input.param.z);
    251.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    252.             c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
    253.             #endif
    254.  
    255.             // Alternative implementation to UnityGet2DClipping with support for softness.
    256.             #if UNITY_UI_CLIP_RECT
    257.             half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
    258.             c *= m.x * m.y;
    259.             #endif
    260.  
    261.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    262.             c *= input.texcoord1.z;
    263.             #endif
    264.  
    265.             #if UNITY_UI_ALPHACLIP
    266.             clip(c.a - 0.001);
    267.             #endif
    268.  
    269.             half4 litResult = CombinedShapeLightShared(c, input.mask, input.lightingUV);
    270.  
    271.             return litResult;
    272.         }
    273.         ENDHLSL
    274.     }
    275. }
    276.  
    277. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    278. }
    279.  
    The only actual change to the logic of the shader is the addition of lightingUV to the frag input, and the line
    half4 litResult = CombinedShapeLightShared(c, input.mask, input.lightingUV);
    Which actually applies the shadows.
    The rest of the changes are just basically to update the script to use HLSL instead of CG so we can use this function.

    Note that I also needed to use a different version of the TMPro_Properties include, since it uses fixed4 variables which are no longer supported in HLSL.
    So this version just replaces them with half4s instead:

    Code (CSharp):
    1. // UI Editable properties
    2. uniform sampler2D    _FaceTex;                    // Alpha : Signed Distance
    3. uniform float        _FaceUVSpeedX;
    4. uniform float        _FaceUVSpeedY;
    5. uniform half4        _FaceColor;                    // RGBA : Color + Opacity
    6. uniform float        _FaceDilate;                // v[ 0, 1]
    7. uniform float        _OutlineSoftness;            // v[ 0, 1]
    8.  
    9. uniform sampler2D    _OutlineTex;                // RGBA : Color + Opacity
    10. uniform float        _OutlineUVSpeedX;
    11. uniform float        _OutlineUVSpeedY;
    12. uniform half4        _OutlineColor;                // RGBA : Color + Opacity
    13. uniform float        _OutlineWidth;                // v[ 0, 1]
    14.  
    15. uniform float        _Bevel;                        // v[ 0, 1]
    16. uniform float        _BevelOffset;                // v[-1, 1]
    17. uniform float        _BevelWidth;                // v[-1, 1]
    18. uniform float        _BevelClamp;                // v[ 0, 1]
    19. uniform float        _BevelRoundness;            // v[ 0, 1]
    20.  
    21. uniform sampler2D    _BumpMap;                    // Normal map
    22. uniform float        _BumpOutline;                // v[ 0, 1]
    23. uniform float        _BumpFace;                    // v[ 0, 1]
    24.  
    25. uniform samplerCUBE    _Cube;                        // Cube / sphere map
    26. uniform half4         _ReflectFaceColor;            // RGB intensity
    27. uniform half4        _ReflectOutlineColor;
    28. //uniform float        _EnvTiltX;                    // v[-1, 1]
    29. //uniform float        _EnvTiltY;                    // v[-1, 1]
    30. uniform float3      _EnvMatrixRotation;
    31. uniform float4x4    _EnvMatrix;
    32.  
    33. uniform half4        _SpecularColor;                // RGB intensity
    34. uniform float        _LightAngle;                // v[ 0,Tau]
    35. uniform float        _SpecularPower;                // v[ 0, 1]
    36. uniform float        _Reflectivity;                // v[ 5, 15]
    37. uniform float        _Diffuse;                    // v[ 0, 1]
    38. uniform float        _Ambient;                    // v[ 0, 1]
    39.  
    40. uniform half4        _UnderlayColor;                // RGBA : Color + Opacity
    41. uniform float        _UnderlayOffsetX;            // v[-1, 1]
    42. uniform float        _UnderlayOffsetY;            // v[-1, 1]
    43. uniform float        _UnderlayDilate;            // v[-1, 1]
    44. uniform float        _UnderlaySoftness;            // v[ 0, 1]
    45.  
    46. uniform half4         _GlowColor;                    // RGBA : Color + Intesity
    47. uniform float         _GlowOffset;                // v[-1, 1]
    48. uniform float         _GlowOuter;                    // v[ 0, 1]
    49. uniform float         _GlowInner;                    // v[ 0, 1]
    50. uniform float         _GlowPower;                    // v[ 1, 1/(1+4*4)]
    51.  
    52. // API Editable properties
    53. uniform float         _ShaderFlags;
    54. uniform float        _WeightNormal;
    55. uniform float        _WeightBold;
    56.  
    57. uniform float        _ScaleRatioA;
    58. uniform float        _ScaleRatioB;
    59. uniform float        _ScaleRatioC;
    60.  
    61. uniform float        _VertexOffsetX;
    62. uniform float        _VertexOffsetY;
    63.  
    64. //uniform float        _UseClipRect;
    65. uniform float        _MaskID;
    66. uniform sampler2D    _MaskTex;
    67. uniform float4        _MaskCoord;
    68. uniform float4        _ClipRect;    // bottom left(x,y) : top right(z,w)
    69. //uniform float        _MaskWipeControl;
    70. //uniform float        _MaskEdgeSoftness;
    71. //uniform fixed4        _MaskEdgeColor;
    72. //uniform bool        _MaskInverse;
    73.  
    74. uniform float        _MaskSoftnessX;
    75. uniform float        _MaskSoftnessY;
    76.  
    77. // Font Atlas properties
    78. uniform sampler2D    _MainTex;
    79. uniform float        _TextureWidth;
    80. uniform float        _TextureHeight;
    81. uniform float         _GradientScale;
    82. uniform float        _ScaleX;
    83. uniform float        _ScaleY;
    84. uniform float        _PerspectiveFilter;
    85. uniform float        _Sharpness;
    86.  
    This was NOT extensively tested. It just worked for the case I was working with (2D lights affecting non-canvas TMP renderers), so take it with a grain of salt.
    Hope this helps someone until official support is implemented for this.
     
    Last edited: Sep 28, 2020
    Kayntt and cxode like this.
  13. cxode

    cxode

    Joined:
    Jun 7, 2017
    Posts:
    268
    @Stephan_B I'm sorry to bug you, but do you have any updates on this? It's an important feature for my project.
     
    vincewins likes this.
  14. vincewins

    vincewins

    Joined:
    Dec 19, 2016
    Posts:
    21
    bump?
     
    cxode likes this.
  15. vincewins

    vincewins

    Joined:
    Dec 19, 2016
    Posts:
    21
    @JimmyCushnie have you tried re-generating your font assets? also, have you completely removed TextMeshPro from your assets folder? while trying that, dont forget to make backups. I somehow resolved it by deleting and re-generating..
    also, there's other TMP shaders that might work, but you will have to change them manually in your font materials.
     
  16. celeronpm

    celeronpm

    Joined:
    Feb 15, 2017
    Posts:
    11
    Any updates on this?
     
    coidevoid and beatdesign like this.
  17. beatdesign

    beatdesign

    Joined:
    Apr 3, 2015
    Posts:
    137
    Bump!
     
    coidevoid likes this.
  18. FloBeber

    FloBeber

    Joined:
    Jun 9, 2015
    Posts:
    166
    Bump! Not working with URP either :confused:
     
  19. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    See the following post.
     
    sj631, FloBeber and cxode like this.
  20. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    any one finded a way to solve this problem?
     
  21. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Surface shaders do not work with SRP.

    However, if you upgrade to Unity 2020.3 and use version 3.2.0-pre.2 of the TMP package, it includes new SRP compatible shaders which are contained in the TMP Essential Resources.
     
  22. Ikaro88

    Ikaro88

    Joined:
    Jun 6, 2016
    Posts:
    300
    Ok thanks.

    I cannot upgrade the project...
    There is any way to have 3d text thet is not always on top on URP and 2019.4 version?