这个shader也是好多年前摸索着写的了,主要针对一些2D透明图片做描边效果,现在用起来居然感觉还不错,这里也发出来一下
Shader "Custom/OutterGlow"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Color("Color", Color) = (1,1,1,1)
_Factor("Factor", Range(0, 10)) = 1
_TexSize("Texture Size", vector) = (256,256,0,0)
_radius("_radius", Range(0, 20)) = 5
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull back
Lighting Off
ZWrite Off
Offset -1, -1
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
sampler2D _MainTex;
float4 _Color;
float _Factor;
float2 _TexSize;
float _radius;
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert(appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv = v.texcoord;
o.color = v.color;
return o;
}
half4 frag(v2f i) : COLOR
{
float radiusX = _radius / _TexSize.x;
float radiusY = _radius / _TexSize.y;
float inner = 0;
float outter = 0;
int count = 0;
float4 m;
for (int k = -5; k <= 5; ++k)
{
for (int j = -5; j <= 5; ++j)
{
m = tex2D(_MainTex, float2(i.uv.x + k*radiusX , i.uv.y + j*radiusY));
outter += 1 - m.a;
inner += m.a;
count += 1;
}
}
inner /= count;
outter /= count;
float4 col = tex2D(_MainTex, i.uv) * i.color;
float out_alpha = max(col.a, inner);
col.rgb = col.rgb + (1-col.a) * _Factor * _Color.a * _Color.rgb;
col.a = out_alpha;
return col;
}
ENDCG
}
}
}
网友评论