Skip to content

Instantly share code, notes, and snippets.

@lyuma
Last active July 7, 2020 03:38
Show Gist options
  • Save lyuma/18bf52da92428340bab524a025b24101 to your computer and use it in GitHub Desktop.
Save lyuma/18bf52da92428340bab524a025b24101 to your computer and use it in GitHub Desktop.
// Pixelated font shader by lox9973
// It is a cutout shader, using alpha blending for 1 pixel antialiasing on the edges.
// Intended use is "3D Text" on an opaque object like a sign.
Shader "UI/PixelFont" {
Properties {
_MainTex ("Font Texture", 2D) = "white" {}
_Color ("Text Color", Color) = (1,1,1,1)
}
SubShader {
// Below Transparent queue. Renders after the skybox, but writes to depth.
Tags { "Queue"="Transparent-400" "RenderType"="TransparentCutout" "PreviewType"="Plane" }
Lighting Off Cull Back
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
float4 _Color;
struct VertInput {
half4 color : COLOR;
float3 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct VertOutput {
half4 color : COLOR;
float4 pos : SV_POSITION;
float2 tex : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
void vert(VertInput i, out VertOutput o) {
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.color = i.color * _Color;
o.tex = TRANSFORM_TEX(i.texcoord, _MainTex);
o.pos = UnityObjectToClipPos(i.vertex);
}
half4 frag(VertOutput i) : SV_Target {
float2 coord = i.tex.xy * _MainTex_TexelSize.zw;
float2 fr = frac(coord + 0.5);
float2 fw = max(abs(ddx(coord)), abs(ddy(coord)));
i.tex.xy += (saturate((fr-(1-fw)*0.5)/fw) - fr) * _MainTex_TexelSize.xy;
half4 color = i.color;
color.a *= tex2D(_MainTex, i.tex.xy).a;
clip(color.a - 0.01);
return color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment