TMP_SDF-Mobile.shader (7098B)
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" { 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 struct pixel_t { 103 float4 vertex : SV_POSITION; 104 fixed4 faceColor : COLOR; 105 fixed4 outlineColor : COLOR1; 106 float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV 107 half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w) 108 half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw) 109 #if (UNDERLAY_ON | UNDERLAY_INNER) 110 float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved 111 half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y) 112 #endif 113 }; 114 115 116 pixel_t VertShader(vertex_t input) 117 { 118 float bold = step(input.texcoord1.y, 0); 119 120 float4 vert = input.vertex; 121 vert.x += _VertexOffsetX; 122 vert.y += _VertexOffsetY; 123 float4 vPosition = UnityObjectToClipPos(vert); 124 125 float2 pixelSize = vPosition.w; 126 pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy)); 127 128 float scale = rsqrt(dot(pixelSize, pixelSize)); 129 scale *= abs(input.texcoord1.y) * _GradientScale * 1.5; 130 if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert))))); 131 132 float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0; 133 weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5; 134 135 float layerScale = scale; 136 137 scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale); 138 float bias = (0.5 - weight) * scale - 0.5; 139 float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale; 140 141 float opacity = input.color.a; 142 #if (UNDERLAY_ON | UNDERLAY_INNER) 143 opacity = 1.0; 144 #endif 145 146 fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor; 147 faceColor.rgb *= faceColor.a; 148 149 fixed4 outlineColor = _OutlineColor; 150 outlineColor.a *= opacity; 151 outlineColor.rgb *= outlineColor.a; 152 outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2)))); 153 154 #if (UNDERLAY_ON | UNDERLAY_INNER) 155 156 layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale); 157 float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale); 158 159 float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth; 160 float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight; 161 float2 layerOffset = float2(x, y); 162 #endif 163 164 // Generate UV for the Masking Texture 165 float4 clampedRect = clamp(_ClipRect, -2e10, 2e10); 166 float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy); 167 168 // Structure for pixel shader 169 pixel_t output = { 170 vPosition, 171 faceColor, 172 outlineColor, 173 float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y), 174 half4(scale, bias - outline, bias + outline, bias), 175 half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)), 176 #if (UNDERLAY_ON | UNDERLAY_INNER) 177 float4(input.texcoord0 + layerOffset, input.color.a, 0), 178 half2(layerScale, layerBias), 179 #endif 180 }; 181 182 return output; 183 } 184 185 186 // PIXEL SHADER 187 fixed4 PixShader(pixel_t input) : SV_Target 188 { 189 half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x; 190 half4 c = input.faceColor * saturate(d - input.param.w); 191 192 #ifdef OUTLINE_ON 193 c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z)); 194 c *= saturate(d - input.param.y); 195 #endif 196 197 #if UNDERLAY_ON 198 d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; 199 c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a); 200 #endif 201 202 #if UNDERLAY_INNER 203 half sd = saturate(d - input.param.z); 204 d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; 205 c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a); 206 #endif 207 208 // Alternative implementation to UnityGet2DClipping with support for softness. 209 #if UNITY_UI_CLIP_RECT 210 half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw); 211 c *= m.x * m.y; 212 #endif 213 214 #if (UNDERLAY_ON | UNDERLAY_INNER) 215 c *= input.texcoord1.z; 216 #endif 217 218 #if UNITY_UI_ALPHACLIP 219 clip(c.a - 0.001); 220 #endif 221 222 return c; 223 } 224 ENDCG 225 } 226 } 227 228 CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI" 229 }