TMP_SDF-Mobile Masking.shader (7715B)
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 - Masking" { 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 _MaskTex ("Mask Texture", 2D) = "white" {} 45 _MaskInverse ("Inverse", float) = 0 46 _MaskEdgeColor ("Edge Color", Color) = (1,1,1,1) 47 _MaskEdgeSoftness ("Edge Softness", Range(0, 1)) = 0.01 48 _MaskWipeControl ("Wipe Position", Range(0, 1)) = 0.5 49 50 _StencilComp ("Stencil Comparison", Float) = 8 51 _Stencil ("Stencil ID", Float) = 0 52 _StencilOp ("Stencil Operation", Float) = 0 53 _StencilWriteMask ("Stencil Write Mask", Float) = 255 54 _StencilReadMask ("Stencil Read Mask", Float) = 255 55 56 _ColorMask ("Color Mask", Float) = 15 57 } 58 59 SubShader { 60 Tags 61 { 62 "Queue"="Transparent" 63 "IgnoreProjector"="True" 64 "RenderType"="Transparent" 65 } 66 67 68 Stencil 69 { 70 Ref [_Stencil] 71 Comp [_StencilComp] 72 Pass [_StencilOp] 73 ReadMask [_StencilReadMask] 74 WriteMask [_StencilWriteMask] 75 } 76 77 Cull [_CullMode] 78 ZWrite Off 79 Lighting Off 80 Fog { Mode Off } 81 ZTest [unity_GUIZTestMode] 82 Blend One OneMinusSrcAlpha 83 ColorMask [_ColorMask] 84 85 Pass { 86 CGPROGRAM 87 #pragma vertex VertShader 88 #pragma fragment PixShader 89 #pragma shader_feature __ OUTLINE_ON 90 #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER 91 92 #pragma multi_compile __ UNITY_UI_CLIP_RECT 93 #pragma multi_compile __ UNITY_UI_ALPHACLIP 94 95 96 #include "UnityCG.cginc" 97 #include "UnityUI.cginc" 98 #include "TMPro_Properties.cginc" 99 100 struct vertex_t { 101 float4 vertex : POSITION; 102 float3 normal : NORMAL; 103 fixed4 color : COLOR; 104 float2 texcoord0 : TEXCOORD0; 105 float2 texcoord1 : TEXCOORD1; 106 }; 107 108 struct pixel_t { 109 float4 vertex : SV_POSITION; 110 fixed4 faceColor : COLOR; 111 fixed4 outlineColor : COLOR1; 112 float4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV 113 half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w) 114 half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw) 115 #if (UNDERLAY_ON | UNDERLAY_INNER) 116 float4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved 117 half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y) 118 #endif 119 }; 120 121 float _MaskWipeControl; 122 float _MaskEdgeSoftness; 123 fixed4 _MaskEdgeColor; 124 bool _MaskInverse; 125 126 pixel_t VertShader(vertex_t input) 127 { 128 float bold = step(input.texcoord1.y, 0); 129 130 float4 vert = input.vertex; 131 vert.x += _VertexOffsetX; 132 vert.y += _VertexOffsetY; 133 float4 vPosition = UnityObjectToClipPos(vert); 134 135 float2 pixelSize = vPosition.w; 136 pixelSize /= float2(_ScaleX, _ScaleY) * abs(mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy)); 137 138 float scale = rsqrt(dot(pixelSize, pixelSize)); 139 scale *= abs(input.texcoord1.y) * _GradientScale * 1.5; 140 if(UNITY_MATRIX_P[3][3] == 0) scale = lerp(abs(scale) * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(input.normal.xyz), normalize(WorldSpaceViewDir(vert))))); 141 142 float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0; 143 weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5; 144 145 float layerScale = scale; 146 147 scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale); 148 float bias = (0.5 - weight) * scale - 0.5; 149 float outline = _OutlineWidth * _ScaleRatioA * 0.5 * scale; 150 151 float opacity = input.color.a; 152 #if (UNDERLAY_ON | UNDERLAY_INNER) 153 opacity = 1.0; 154 #endif 155 156 fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor; 157 faceColor.rgb *= faceColor.a; 158 159 fixed4 outlineColor = _OutlineColor; 160 outlineColor.a *= opacity; 161 outlineColor.rgb *= outlineColor.a; 162 outlineColor = lerp(faceColor, outlineColor, sqrt(min(1.0, (outline * 2)))); 163 164 #if (UNDERLAY_ON | UNDERLAY_INNER) 165 166 layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale); 167 float layerBias = (.5 - weight) * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale); 168 169 float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth; 170 float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight; 171 float2 layerOffset = float2(x, y); 172 #endif 173 174 // Generate UV for the Masking Texture 175 float4 clampedRect = clamp(_ClipRect, -2e10, 2e10); 176 float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy); 177 178 // Structure for pixel shader 179 pixel_t output = { 180 vPosition, 181 faceColor, 182 outlineColor, 183 float4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y), 184 half4(scale, bias - outline, bias + outline, bias), 185 half4(vert.xy * 2 - clampedRect.xy - clampedRect.zw, 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)), 186 #if (UNDERLAY_ON | UNDERLAY_INNER) 187 float4(input.texcoord0 + layerOffset, input.color.a, 0), 188 half2(layerScale, layerBias), 189 #endif 190 }; 191 192 return output; 193 } 194 195 196 // PIXEL SHADER 197 fixed4 PixShader(pixel_t input) : SV_Target 198 { 199 half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x; 200 half4 c = input.faceColor * saturate(d - input.param.w); 201 202 #ifdef OUTLINE_ON 203 c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z)); 204 c *= saturate(d - input.param.y); 205 #endif 206 207 #if UNDERLAY_ON 208 d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; 209 c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (1 - c.a); 210 #endif 211 212 #if UNDERLAY_INNER 213 half sd = saturate(d - input.param.z); 214 d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; 215 c += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - input.underlayParam.y)) * sd * (1 - c.a); 216 #endif 217 218 // Alternative implementation to UnityGet2DClipping with support for softness. 219 #if UNITY_UI_CLIP_RECT 220 half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw); 221 c *= m.x * m.y; 222 #endif 223 224 float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a); 225 float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl; 226 a = saturate(t / _MaskEdgeSoftness); 227 c.rgb = lerp(_MaskEdgeColor.rgb*c.a, c.rgb, a); 228 c *= a; 229 230 #if (UNDERLAY_ON | UNDERLAY_INNER) 231 c *= input.texcoord1.z; 232 #endif 233 234 #if UNITY_UI_ALPHACLIP 235 clip(c.a - 0.001); 236 #endif 237 238 return c; 239 } 240 ENDCG 241 } 242 } 243 244 CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI" 245 }