evershade/Assets/Shaders/Pixel Border/TMP_Border_Bitmap.shader

276 lines
9.7 KiB
GLSL

Shader "TextMeshPro/Bitmap Border"
{
Properties
{
[Header(Text)]
_MainTex ("Font Atlas", 2D) = "white" {}
_FaceTex ("Font Texture", 2D) = "white" {}
[HDR]_FaceColor ("Text Color", Color) = (1,1,1,1)
[Header(Border)]
[Toggle(VERTICAL_GRADIENT)]_VerticalGradient ("Vertical Gradient", Float) = 1
[HDR]_BorderColor1 ("Border Color 1", Color) = (1,0,0,1)
[HDR]_BorderColor2 ("Border Color 2", Color) = (0,0,1,1)
_BorderWidth ("Border Width", Range(0, 20)) = 1
_PixelSize ("Pixel Size", Range(0, 20)) = 1
_GradientAngle ("Gradient Angle", Range(0, 360)) = 90
_BorderSharpness ("Border Sharpness", Range(0, 1)) = 1
_GradientHeight ("Gradient Height", Range(0, 20)) = 12
_CharacterSize ("Character Size", Vector) = (32,32,0,0)
_VertexOffsetX ("Vertex OffsetX", float) = 0
_VertexOffsetY ("Vertex OffsetY", float) = 0
_MaskSoftnessX ("Mask SoftnessX", float) = 0
_MaskSoftnessY ("Mask SoftnessY", float) = 0
_ClipRect("Clip Rect", vector) = (-32767, -32767, 32767, 32767)
_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]
}
Lighting Off
Cull [_CullMode]
ZTest [unity_GUIZTestMode]
ZWrite Off
Fog
{
Mode Off
}
Blend SrcAlpha OneMinusSrcAlpha
ColorMask[_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile __ UNITY_UI_CLIP_RECT
#pragma multi_compile __ UNITY_UI_ALPHACLIP
#pragma shader_feature VERTICAL_GRADIENT
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
fixed4 color : COLOR;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
float2 texcoord0 : TEXCOORD0;
float2 texcoord1 : TEXCOORD1;
float4 mask : TEXCOORD2;
float4 characterBounds : TEXCOORD3;
float2 characterUV : TEXCOORD4;
float4 screenPos : TEXCOORD5;
};
uniform sampler2D _MainTex;
uniform sampler2D _FaceTex;
uniform float4 _FaceTex_ST;
uniform fixed4 _FaceColor;
uniform fixed4 _BorderColor1;
uniform fixed4 _BorderColor2;
uniform float _BorderWidth;
uniform float _PixelSize;
uniform float _GradientAngle;
uniform float _GradientHeight;
uniform float _VertexOffsetX;
uniform float _VertexOffsetY;
uniform float4 _ClipRect;
uniform float _MaskSoftnessX;
uniform float _MaskSoftnessY;
float2 UnpackUV(float uv)
{
float2 output;
output.x = floor(uv / 4096);
output.y = uv - 4096 * output.x;
return output * 0.001953125;
}
v2f vert(appdata_t v)
{
float4 vert = v.vertex;
vert.x += _VertexOffsetX;
vert.y += _VertexOffsetY;
vert.xy += (vert.w * 0.5) / _ScreenParams.xy;
float4 vPosition = UnityPixelSnap(UnityObjectToClipPos(vert));
fixed4 faceColor = v.color;
faceColor *= _FaceColor;
v2f OUT;
OUT.vertex = vPosition;
OUT.color = faceColor;
OUT.texcoord0 = v.texcoord0;
OUT.texcoord1 = TRANSFORM_TEX(UnpackUV(v.texcoord1), _FaceTex);
float2 pixelSize = vPosition.w;
pixelSize /= abs(float2(_ScreenParams.x * UNITY_MATRIX_P[0][0],
_ScreenParams.y * UNITY_MATRIX_P[1][1]));
// Clamp _ClipRect to 16bit.
float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
OUT.mask = float4(vert.xy * 2 - clampedRect.xy - clampedRect.zw,
0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) +
pixelSize.xy));
OUT.characterBounds = float4(v.texcoord0, v.texcoord0);
OUT.characterUV = v.texcoord0;
OUT.screenPos = ComputeScreenPos(vPosition);
return OUT;
}
/*
fixed4 frag(v2f IN) : SV_Target
{
float4 faceColor = fixed4(tex2D(_FaceTex, IN.texcoord1).rgb * IN.color.rgb, IN.color.a * color.a);
// Calculate border
float2 pixelSize = float2(_PixelSize, _PixelSize) / _ScreenParams.xy;
float borderWidth = max(_BorderWidth, 1);
float border = 0;
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;
float2 offset = float2(x, y) * pixelSize * borderWidth;
float2 sampleUV = IN.texcoord0 + offset;
border = max(border, tex2D(_MainTex, sampleUV).a);
}
}
border = saturate(border - color.a);
// Determine if we're in the top or bottom half of the character
float splitY = 0.5; // Split at the middle
fixed4 borderColor = (IN.texcoord0.y > splitY) ? _BorderColor1 : _BorderColor2;
// Combine face and border
fixed4 finalColor = lerp(faceColor, borderColor, border);
// Ensure the border doesn't affect transparent areas
finalColor.a = max(faceColor.a, border);
// Apply clipping and alpha clip
#if UNITY_UI_CLIP_RECT
half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
finalColor *= m.x * m.y;
#endif
#if UNITY_UI_ALPHACLIP
clip(finalColor.a - 0.001);
#endif
return finalColor;#1#
}
*/
fixed4 frag(v2f IN) : SV_Target
{
float4 color = tex2D(_MainTex, IN.texcoord0);
float4 faceColor = fixed4(tex2D(_FaceTex, IN.texcoord1).rgb * IN.color.rgb, IN.color.a * color.a);
// Calculate border
float2 pixelSize = float2(_PixelSize, _PixelSize) / _ScreenParams.xy;
float borderWidth = max(_BorderWidth, 1);
/*float border = 0;
for (int i = 0; i < 8; i++)
{
float2 offset = float2(cos(i * UNITY_PI / 4), sin(i * UNITY_PI / 4)) * borderWidth * pixelSize;
float2 sampleUV = IN.texcoord0 + offset;
border = max(border, tex2D(_MainTex, sampleUV).a);
}
border = saturate(border - color.a);#2#*/
float border = 0;
// float borderUV = IN.texcoord0;
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;
float2 offset = float2(x, y) * pixelSize * borderWidth;
float2 sampleUV = IN.texcoord0 + offset;
border = max(border, tex2D(_MainTex, sampleUV).a);
}
}
border = saturate(border - color.a);
// float gradientT = IN.texcoord0.y;
float2 screenPos = IN.vertex.xy;
float gradientHeight = _GradientHeight;
float gradientT = frac(screenPos.y / gradientHeight);
fixed4 borderColor = lerp(_BorderColor1, _BorderColor2, gradientT);
fixed4 finalColor = lerp(faceColor, borderColor, border);
finalColor.a = max(faceColor.a, border);
// float gradientT = borderUV.y;
// fixed4 borderColor = lerp(_BorderColor1, _BorderColor2, gradientT);
// Combine face and border
// fixed4 finalColor = lerp(faceColor, borderColor, border);
// Ensure the border doesn't affect transparent areas
// finalColor.a = max(faceColor.a, border);
// Alternative implementation to UnityGet2DClipping with support for softness.
#if UNITY_UI_CLIP_RECT
half2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(IN.mask.xy)) * IN.mask.zw);
finalColor *= m.x * m.y;
#endif
#if UNITY_UI_ALPHACLIP
clip(finalColor.a - 0.001);
#endif
return finalColor;
}
ENDCG
}
}
// CustomEditor "TMPro.EditorUtilities.TMP_BitmapShaderGUI"
}