Search Unity

Text Mesh Pro Shader - How To Dim Based On Distance?

Discussion in 'Shaders' started by konstantin_lozev, Apr 14, 2019.

  1. konstantin_lozev

    konstantin_lozev

    Joined:
    May 8, 2015
    Posts:
    99
    Hi all,
    I am quite new to shaders, but find it really useful for my current project. I have (many) objects that consist of a mesh and a text element (using Text Mesh Pro). For the mesh I managed to tweak my simple instanced unlit shader in order to achieve an effect of dimming the output color depending on how far from the camera the vertex is:
    Code (CSharp):
    1.  
    2. Shader "SimplestInstancedShaderDistDim"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Color", Color) = (1, 1, 1, 1)
    7.         //_Dim ("Radius", Range(0, 1)) = 0.5
    8.         _Radius1 ("Radius1", Range(0.001, 30)) = 1
    9.         _Radius2 ("Radius2", Range(0.001, 30)) = 10
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags { "RenderType"="Opaque" }
    15.         LOD 100
    16.  
    17.         Pass
    18.         {
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.             #pragma multi_compile_instancing
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 UNITY_VERTEX_INPUT_INSTANCE_ID
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 vertex : SV_POSITION;
    34.                 float4 worldPos : TEXCOORD1;
    35.                 UNITY_VERTEX_INPUT_INSTANCE_ID // necessary only if you want to access instanced properties in fragment Shader.
    36.             };
    37.  
    38.             UNITY_INSTANCING_BUFFER_START(Props)
    39.                 UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
    40.             UNITY_INSTANCING_BUFFER_END(Props)
    41.        
    42.             v2f vert(appdata v)
    43.             {
    44.                 v2f o;
    45.  
    46.                 UNITY_SETUP_INSTANCE_ID(v);
    47.               UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.
    48.  
    49.                 o.vertex = UnityObjectToClipPos(v.vertex);
    50.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
    51.                 return o;
    52.             }
    53.  
    54.             //float _Dim;
    55.             float _Radius1;
    56.             float _Radius2;
    57.        
    58.             fixed4 frag(v2f i) : SV_Target
    59.             {
    60.               UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
    61.                 float pos = distance(i.worldPos, _WorldSpaceCameraPos);
    62.                 float clampPos = clamp(pos, _Radius1, _Radius2);
    63.                 float dim = 1-(clampPos-_Radius1)/(_Radius2-_Radius1);
    64.                 fixed3 color = UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
    65.                 return fixed4(color*dim, 1);
    66.             }
    67.             ENDCG
    68.         }
    69.     }
    70. }
    I want to make the same linear dimming effect for the Text Mesh Pro elements, but I have no idea how to edit that shader (it looks much different in structure to my shader). Here is the TMP 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 Dist Dim" {
    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.  
    38.     _VertexOffsetX        ("Vertex OffsetX", float) = 0
    39.     _VertexOffsetY        ("Vertex OffsetY", float) = 0
    40.  
    41.     _ClipRect            ("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
    42.     _MaskSoftnessX        ("Mask SoftnessX", float) = 0
    43.     _MaskSoftnessY        ("Mask SoftnessY", float) = 0
    44.  
    45.     _StencilComp        ("Stencil Comparison", Float) = 8
    46.     _Stencil            ("Stencil ID", Float) = 0
    47.     _StencilOp            ("Stencil Operation", Float) = 0
    48.     _StencilWriteMask    ("Stencil Write Mask", Float) = 255
    49.     _StencilReadMask    ("Stencil Read Mask", Float) = 255
    50.  
    51.     _ColorMask            ("Color Mask", Float) = 15
    52. }
    53.  
    54. SubShader {
    55.     Tags
    56.     {
    57.         "Queue"="Transparent"
    58.         "IgnoreProjector"="True"
    59.         "RenderType"="Transparent"
    60.     }
    61.  
    62.  
    63.     Stencil
    64.     {
    65.         Ref [_Stencil]
    66.         Comp [_StencilComp]
    67.         Pass [_StencilOp]
    68.         ReadMask [_StencilReadMask]
    69.         WriteMask [_StencilWriteMask]
    70.     }
    71.  
    72.     Cull [_CullMode]
    73.     ZWrite Off
    74.     Lighting Off
    75.     Fog { Mode Off }
    76.     ZTest [unity_GUIZTestMode]
    77.     Blend One OneMinusSrcAlpha
    78.     ColorMask [_ColorMask]
    79.  
    80.     Pass {
    81.         CGPROGRAM
    82.         #pragma vertex VertShader
    83.         #pragma fragment PixShader
    84.         #pragma shader_feature __ OUTLINE_ON
    85.         #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
    86.  
    87.         #pragma multi_compile __ UNITY_UI_CLIP_RECT
    88.         #pragma multi_compile __ UNITY_UI_ALPHACLIP
    89.  
    90.         #include "UnityCG.cginc"
    91.         #include "UnityUI.cginc"
    92.         #include "TMPro_Properties.cginc"
    93.  
    94.         struct vertex_t {
    95.             float4    vertex            : POSITION;
    96.             float3    normal            : NORMAL;
    97.             fixed4    color            : COLOR;
    98.             float2    texcoord0        : TEXCOORD0;
    99.             float2    texcoord1        : TEXCOORD1;
    100.  
    101.         };
    102.  
    103.         struct pixel_t {
    104.             float4    vertex            : SV_POSITION;
    105.             fixed4    faceColor        : COLOR;
    106.             fixed4    outlineColor    : COLOR1;
    107.             float4    texcoord0        : TEXCOORD0;            // Texture UV, Mask UV
    108.             half4    param            : TEXCOORD1;            // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
    109.             half4    mask            : TEXCOORD2;            // Position in clip space(xy), Softness(zw)
    110.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    111.             float4    texcoord1        : TEXCOORD3;            // Texture UV, alpha, reserved
    112.             half2    underlayParam    : TEXCOORD4;            // Scale(x), Bias(y)
    113.         #endif
    114.         };
    115.  
    116.  
    117.         pixel_t VertShader(vertex_t input)
    118.         {
    119.             float bold = step(input.texcoord1.y, 0);
    120.  
    121.             float4 vert = input.vertex;
    122.             vert.x += _VertexOffsetX;
    123.             vert.y += _VertexOffsetY;
    124.             float4 vPosition = UnityObjectToClipPos(vert);
    125.  
    126.             float2 pixelSize = vPosition.w;
    127.             pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    128.        
    129.             float scale = rsqrt(dot(pixelSize, pixelSize));
    130.             scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
    131.           if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
    132.  
    133.             float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
    134.             weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
    135.  
    136.             float layerScale = scale;
    137.  
    138.             scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
    139.             float bias = (0.5 - weight) * scale - 0.5;
    140.             float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
    141.  
    142.             float opacity = input.color.a;
    143.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    144.                 opacity = 1.0;
    145.         #endif
    146.  
    147.             fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
    148.             faceColor.rgb *= faceColor.a;
    149.  
    150.             fixed4 outlineColor = _OutlineColor;
    151.             outlineColor.a *= opacity;
    152.             outlineColor.rgb *= outlineColor.a;
    153.             outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
    154.  
    155.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    156.  
    157.             layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
    158.             float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
    159.  
    160.             float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
    161.             float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
    162.             float2 layerOffset = float2(x, y);
    163.         #endif
    164.  
    165.             // Generate UV for the Masking Texture
    166.             float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
    167.             float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
    168.  
    169.             // Structure for pixel shader
    170.             pixel_t output = {
    171.                 vPosition,
    172.                 faceColor,
    173.                 outlineColor,
    174.                 float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y),
    175.                 half4(scale, bias - outline, bias + outline, bias),
    176.               half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)),
    177.             #if (UNDERLAY_ON | UNDERLAY_INNER)
    178.                 float4(input.texcoord0 + layerOffset, input.color.a, 0),
    179.                 half2(layerScale, layerBias),
    180.             #endif
    181.             };
    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 += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (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 += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (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.         #if UNITY_UI_ALPHACLIP
    220.             clip(c.a - 0.001);
    221.         #endif
    222.  
    223.             return c;
    224.         }
    225.         ENDCG
    226.     }
    227. }
    228.  
    229. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    230. }
    231.  
    Thanks for any pointers on how to achieve such effect in the TMP shader.
     
    Last edited: Apr 15, 2019
  2. konstantin_lozev

    konstantin_lozev

    Joined:
    May 8, 2015
    Posts:
    99
    On second look, the structure is not much different and I tried to adapt it, I will test this evening, but here is what I came up with (edit, I checked it and fixed a small thing and the shader below is working now):
    Code (CSharp):
    1.  
    2. // Simplified SDF shader:
    3. // - No Shading Option (bevel / bump / env map)
    4. // - No Glow Option
    5. // - Softness is applied on both side of the outline
    6.  
    7. Shader "TextMeshPro/Mobile/Distance Field Dist Dim" {
    8.  
    9. Properties {
    10.     _FaceColor            ("Face Color", Color) = (1,1,1,1)
    11.     _FaceDilate            ("Face Dilate", Range(-1,1)) = 0
    12.  
    13.     _OutlineColor        ("Outline Color", Color) = (0,0,0,1)
    14.     _OutlineWidth        ("Outline Thickness", Range(0,1)) = 0
    15.     _OutlineSoftness    ("Outline Softness", Range(0,1)) = 0
    16.  
    17.     _UnderlayColor        ("Border Color", Color) = (0,0,0,.5)
    18.     _UnderlayOffsetX     ("Border OffsetX", Range(-1,1)) = 0
    19.     _UnderlayOffsetY     ("Border OffsetY", Range(-1,1)) = 0
    20.     _UnderlayDilate        ("Border Dilate", Range(-1,1)) = 0
    21.     _UnderlaySoftness     ("Border Softness", Range(0,1)) = 0
    22.  
    23.     _WeightNormal        ("Weight Normal", float) = 0
    24.     _WeightBold            ("Weight Bold", float) = .5
    25.  
    26.     _ShaderFlags        ("Flags", float) = 0
    27.     _ScaleRatioA        ("Scale RatioA", float) = 1
    28.     _ScaleRatioB        ("Scale RatioB", float) = 1
    29.     _ScaleRatioC        ("Scale RatioC", float) = 1
    30.  
    31.     _MainTex            ("Font Atlas", 2D) = "white" {}
    32.     _TextureWidth        ("Texture Width", float) = 512
    33.     _TextureHeight        ("Texture Height", float) = 512
    34.     _GradientScale        ("Gradient Scale", float) = 5
    35.     _ScaleX                ("Scale X", float) = 1
    36.     _ScaleY                ("Scale Y", float) = 1
    37.     _PerspectiveFilter    ("Perspective Correction", Range(0, 1)) = 0.875
    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.      _Radius1 ("Radius1", Range(0.001, 30)) = 0.001
    55.      _Radius2 ("Radius2", Range(0.001, 30)) = 0.002
    56. }
    57.  
    58. SubShader {
    59.     Tags
    60.     {
    61.         "Queue"="Transparent"
    62.         "IgnoreProjector"="True"
    63.         "RenderType"="Transparent"
    64.     }
    65.  
    66.  
    67.     Stencil
    68.     {
    69.         Ref [_Stencil]
    70.         Comp [_StencilComp]
    71.         Pass [_StencilOp]
    72.         ReadMask [_StencilReadMask]
    73.         WriteMask [_StencilWriteMask]
    74.     }
    75.  
    76.     Cull [_CullMode]
    77.     ZWrite Off
    78.     Lighting Off
    79.     Fog { Mode Off }
    80.     ZTest [unity_GUIZTestMode]
    81.     Blend One OneMinusSrcAlpha
    82.     ColorMask [_ColorMask]
    83.  
    84.     Pass {
    85.         CGPROGRAM
    86.         #pragma vertex VertShader
    87.         #pragma fragment PixShader
    88.         #pragma shader_feature __ OUTLINE_ON
    89.         #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER
    90.  
    91.         #pragma multi_compile __ UNITY_UI_CLIP_RECT
    92.         #pragma multi_compile __ UNITY_UI_ALPHACLIP
    93.  
    94.         #include "UnityCG.cginc"
    95.         #include "UnityUI.cginc"
    96.         #include "TMPro_Properties.cginc"
    97.  
    98.         struct vertex_t {
    99.             float4    vertex            : POSITION;
    100.             float3    normal            : NORMAL;
    101.             fixed4    color            : COLOR;
    102.             float2    texcoord0        : TEXCOORD0;
    103.             float2    texcoord1        : TEXCOORD1;
    104.         };
    105.  
    106.         struct pixel_t {
    107.             float4    vertex            : SV_POSITION;
    108.             fixed4    faceColor        : COLOR;
    109.             fixed4    outlineColor    : COLOR1;
    110.             float4    texcoord0        : TEXCOORD0;            // Texture UV, Mask UV
    111.             half4    param            : TEXCOORD1;            // Scale(x), BiasIn(y), BiasOut(z), Bias(w)
    112.             half4    mask            : TEXCOORD2;            // Position in clip space(xy), Softness(zw)
    113.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    114.             float4    texcoord1        : TEXCOORD3;            // Texture UV, alpha, reserved
    115.             half2    underlayParam    : TEXCOORD4;            // Scale(x), Bias(y)
    116.         #endif
    117.             float4 worldPos : TEXCOORD5;
    118.         };
    119.  
    120.  
    121.         pixel_t VertShader(vertex_t input)
    122.         {
    123.             float bold = step(input.texcoord1.y, 0);
    124.  
    125.             float4 vert = input.vertex;
    126.             vert.x += _VertexOffsetX;
    127.             vert.y += _VertexOffsetY;
    128.             float4 vPosition = UnityObjectToClipPos(vert);
    129.             float4 worldPos = mul(unity_ObjectToWorld, vert);
    130.  
    131.             float2 pixelSize = vPosition.w;
    132.             pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy));
    133.          
    134.             float scale = rsqrt(dot(pixelSize, pixelSize));
    135.             scale *= abs(input.texcoord1.y) * _GradientScale * 1.5;
    136.            if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
    137.  
    138.             float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
    139.             weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
    140.  
    141.             float layerScale = scale;
    142.  
    143.             scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
    144.             float bias = (0.5 - weight) * scale - 0.5;
    145.             float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale;
    146.  
    147.             float opacity = input.color.a;
    148.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    149.                 opacity = 1.0;
    150.         #endif
    151.  
    152.             fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor;
    153.             faceColor.rgb *= faceColor.a;
    154.  
    155.             fixed4 outlineColor = _OutlineColor;
    156.             outlineColor.a *= opacity;
    157.             outlineColor.rgb *= outlineColor.a;
    158.             outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2))));
    159.  
    160.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    161.  
    162.             layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
    163.             float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
    164.  
    165.             float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
    166.             float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
    167.             float2 layerOffset = float2(x, y);
    168.         #endif
    169.  
    170.             // Generate UV for the Masking Texture
    171.             float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
    172.             float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
    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.                 worldPos,
    187.             };
    188.  
    189.             return output;
    190.         }
    191.  
    192.           float _Radius1;
    193.           float _Radius2;
    194.  
    195.         // PIXEL SHADER
    196.         fixed4 PixShader(pixel_t input) : SV_Target
    197.         {
    198.             half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x;
    199.             half4 c = input.faceColor * saturate(d - input.param.w);
    200.  
    201.         #ifdef OUTLINE_ON
    202.             c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z));
    203.             c *= saturate(d - input.param.y);
    204.         #endif
    205.  
    206.         #if UNDERLAY_ON
    207.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    208.            c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a);
    209.         #endif
    210.  
    211.         #if UNDERLAY_INNER
    212.             half sd = saturate(d - input.param.z);
    213.             d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x;
    214.            c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a);
    215.         #endif
    216.  
    217.         // Alternative implementation to UnityGet2DClipping with support for softness.
    218.         #if UNITY_UI_CLIP_RECT
    219.             half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw);
    220.             c *= m.x * m.y;
    221.         #endif
    222.  
    223.         #if (UNDERLAY_ON | UNDERLAY_INNER)
    224.             c *= input.texcoord1.z;
    225.         #endif
    226.  
    227.         #if UNITY_UI_ALPHACLIP
    228.             clip(c.a - 0.001);
    229.         #endif
    230.  
    231.            float pos = distance(input.worldPos, _WorldSpaceCameraPos);
    232.            float clampPos = clamp(pos, _Radius1, _Radius2);
    233.            float dim = 1-(clampPos-_Radius1)/(_Radius2-_Radius1);
    234.  
    235.             return c*dim;
    236.         }
    237.         ENDCG
    238.     }
    239. }
    240.  
    241. CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI"
    242. }
    243.  
     
    Last edited: Apr 15, 2019
    bgolus likes this.