// Simplified SDF shader: // - No Shading Option (bevel / bump / env map) // - No Glow Option // - Softness is applied on both side of the outline Shader "BikeStunt/3D Text" { Properties { [HDR]_FaceColor ("Face Color", Color) = (1,1,1,1) _FaceDilate ("Face Dilate", Range(-1,1)) = 0 [HDR]_OutlineColor ("Outline Color", Color) = (0,0,0,1) _OutlineWidth ("Outline Thickness", Range(0,1)) = 0 _OutlineSoftness ("Outline Softness", Range(0,1)) = 0 [HDR]_UnderlayColor ("Border Color", Color) = (0,0,0,.5) _UnderlayOffsetX ("Border OffsetX", Range(-1,1)) = 0 _UnderlayOffsetY ("Border OffsetY", Range(-1,1)) = 0 _UnderlayDilate ("Border Dilate", Range(-1,1)) = 0 _UnderlaySoftness ("Border Softness", Range(0,1)) = 0 _WeightNormal ("Weight Normal", float) = 0 _WeightBold ("Weight Bold", float) = .5 _ShaderFlags ("Flags", float) = 0 _ScaleRatioA ("Scale RatioA", float) = 1 _ScaleRatioB ("Scale RatioB", float) = 1 _ScaleRatioC ("Scale RatioC", float) = 1 _MainTex ("Font Atlas", 2D) = "white" {} _TextureWidth ("Texture Width", float) = 512 _TextureHeight ("Texture Height", float) = 512 _GradientScale ("Gradient Scale", float) = 5 _ScaleX ("Scale X", float) = 1 _ScaleY ("Scale Y", float) = 1 _PerspectiveFilter ("Perspective Correction", Range(0, 1)) = 0.875 _Sharpness ("Sharpness", Range(-1,1)) = 0 _VertexOffsetX ("Vertex OffsetX", float) = 0 _VertexOffsetY ("Vertex OffsetY", float) = 0 _ClipRect ("Clip Rect", vector) = (-32767, -32767, 32767, 32767) _MaskSoftnessX ("Mask SoftnessX", float) = 0 _MaskSoftnessY ("Mask SoftnessY", float) = 0 _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _CullMode ("Cull Mode", Float) = 0 _ColorMask ("Color Mask", Float) = 15 } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" } Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Cull [_CullMode] ZWrite On //Off Lighting Off Fog { Mode Off } ZTest [unity_GUIZTestMode] Blend One OneMinusSrcAlpha ColorMask [_ColorMask] Pass { CGPROGRAM #pragma vertex VertShader #pragma fragment PixShader #pragma shader_feature __ OUTLINE_ON #pragma shader_feature __ UNDERLAY_ON UNDERLAY_INNER #pragma multi_compile __ UNITY_UI_CLIP_RECT #pragma multi_compile __ UNITY_UI_ALPHACLIP #include "UnityCG.cginc" #include "UnityUI.cginc" #include "TMPro_Properties.cginc" struct vertex_t { float4 vertex : POSITION; half3 normal : NORMAL; fixed4 color : COLOR; half2 texcoord0 : TEXCOORD0; half2 texcoord1 : TEXCOORD1; }; struct pixel_t { float4 vertex : SV_POSITION; fixed4 faceColor : COLOR; fixed4 outlineColor : COLOR1; half4 texcoord0 : TEXCOORD0; // Texture UV, Mask UV half4 param : TEXCOORD1; // Scale(x), BiasIn(y), BiasOut(z), Bias(w) half4 mask : TEXCOORD2; // Position in clip space(xy), Softness(zw) #if (UNDERLAY_ON | UNDERLAY_INNER) half4 texcoord1 : TEXCOORD3; // Texture UV, alpha, reserved half2 underlayParam : TEXCOORD4; // Scale(x), Bias(y) #endif }; pixel_t VertShader(vertex_t input) { pixel_t output; UNITY_INITIALIZE_OUTPUT(pixel_t, output); half bold = step(input.texcoord1.y, half(0)); half4 vert = input.vertex; vert.x += _VertexOffsetX; vert.y += _VertexOffsetY; float4 vPosition = UnityObjectToClipPos(vert); half2 pixelSize = vPosition.w; pixelSize /= half2(_ScaleX, _ScaleY) * abs(mul((half2x2)UNITY_MATRIX_P, _ScreenParams.xy)); half scale = rsqrt(dot(pixelSize, pixelSize)); scale *= abs(input.texcoord1.y) * _GradientScale * (_Sharpness + half(1)); half weight = lerp(_WeightNormal, _WeightBold, bold) / half(4.0); weight = (weight + _FaceDilate) * _ScaleRatioA * half(0.5); half layerScale = scale; scale /= half(1) + (_OutlineSoftness * _ScaleRatioA * scale); half bias = (half(0.5) - weight) * scale - half(0.5); half outline = _OutlineWidth * _ScaleRatioA * half(0.5) * scale; half opacity = input.color.a; #if (UNDERLAY_ON | UNDERLAY_INNER) opacity = half(1.0); #endif fixed4 faceColor = fixed4(input.color.rgb, opacity) * _FaceColor; faceColor.rgb *= faceColor.a; fixed4 outlineColor = _OutlineColor; outlineColor.a *= opacity; outlineColor.rgb *= outlineColor.a; outlineColor = lerp(faceColor, outlineColor, sqrt(min(half(1.0), (outline * half(2))))); #if (UNDERLAY_ON | UNDERLAY_INNER) layerScale /= half(1) + ((_UnderlaySoftness * _ScaleRatioC) * layerScale); half layerBias = (half(.5) - weight) * layerScale - half(.5) - ((_UnderlayDilate * _ScaleRatioC) * half(.5) * layerScale); half x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth; half y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight; half2 layerOffset = half2(x, y); #endif // Generate UV for the Masking Texture half4 clampedRect = clamp(_ClipRect, half(-2e10), half(2e10)); half2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy); // Populate structure for pixel shader output.vertex = vPosition; output.faceColor = faceColor; output.outlineColor = outlineColor; output.texcoord0 = half4(input.texcoord0.x, input.texcoord0.y, maskUV.x, maskUV.y); output.param = half4(scale, bias - outline, bias + outline, bias); output.mask = half4(vert.xy * half(2) - clampedRect.xy - clampedRect.zw, half(0.25) / (half(0.25) * half2(_MaskSoftnessX, _MaskSoftnessY) + pixelSize.xy)); #if (UNDERLAY_ON || UNDERLAY_INNER) output.texcoord1 = half4(input.texcoord0 + layerOffset, input.color.a, half(0)); output.underlayParam = half2(layerScale, layerBias); #endif return output; } // PIXEL SHADER fixed4 PixShader(pixel_t input) : SV_Target { half d = tex2D(_MainTex, input.texcoord0.xy).a * input.param.x; half4 c = input.faceColor * saturate(d - input.param.w); #ifdef OUTLINE_ON c = lerp(input.outlineColor, input.faceColor, saturate(d - input.param.z)); c *= saturate(d - input.param.y); #endif #if UNDERLAY_ON d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; c += half4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - input.underlayParam.y) * (half(1) - c.a); #endif #if UNDERLAY_INNER half sd = saturate(d - input.param.z); d = tex2D(_MainTex, input.texcoord1.xy).a * input.underlayParam.x; c += half4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (half(1) - saturate(d - input.underlayParam.y)) * sd * (half(1) - c.a); #endif // Alternative implementation to UnityGet2DClipping with support for softness. #if UNITY_UI_CLIP_RECT half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * input.mask.zw); c *= m.x * m.y; #endif #if (UNDERLAY_ON | UNDERLAY_INNER) c *= input.texcoord1.z; #endif return c; } ENDCG } } CustomEditor "TMPro.EditorUtilities.TMP_SDFShaderGUI" }