unity外发光描边shader_skybeauty_新浪博客

Shader "Custom/Edge"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_OffsetUV("OffsetUV", Range(0, 0.005)) = 0
_EdgeColor("EdgeColor", Color) = (1, 0, 0, 1)
_AlphaTreshold("Treshold", Range(0, 1)) = 0.5
_EdgeWidth("EdgeWidth",Range(0,0.03)) = 0.003
_EdgeColorPower("EdgeColorPower",Range(0,1))=1
_EdgeStep("EdgeStep",Range(0,0.005)) = 0.001
}
SubShader
{
Tags{ "Queue" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
fixed2 uv : TEXCOORD0;
};

struct v2f
{
float4 vertex : SV_POSITION;
fixed2 uv[5] : TEXCOORD0;
};

sampler2D _MainTex;
float4 _MainTex_ST;
fixed _OffsetUV;
fixed4 _EdgeColor;
fixed _AlphaTreshold;
fixed _EdgeWidth;
fixed _EdgeColorPower;
fixed _EdgeStep;

struct CheckData
{
fixed alpha;
bool isCanDraw;
};

v2f vert(appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv[0] = v.uv;
o.uv[1] = v.uv + fixed2(0, _OffsetUV); //up  
o.uv[2] = v.uv + fixed2(-_OffsetUV, 0); //left  
o.uv[3] = v.uv + fixed2(0, -_OffsetUV); //bottom  
o.uv[4] = v.uv + fixed2(_OffsetUV, 0); //right 

return o;
}


fixed check(fixed2 uv)
{
fixed alpha = 0;
fixed step = _EdgeStep;
fixed curStep = step;
fixed4 fColor = tex2D(_MainTex, uv);
fixed2 curUv = uv;
if (fColor.a <= 0)
{
[unroll(20)] for (; curStep <= _EdgeWidth;)
{
curUv = uv + fixed2(0, curStep);
fColor = tex2D(_MainTex, curUv);
if (fColor.a <= 0)
{
curUv = uv + fixed2(-curStep, 0);
fColor = tex2D(_MainTex, curUv);
}
else
{
break;
}
if (fColor.a <= 0)
{
curUv = uv + fixed2(0, -curStep);
fColor = tex2D(_MainTex, curUv);
}
else
{
break;
}
if (fColor.a <= 0)
{
curUv = uv + fixed2(curStep, 0);
fColor = tex2D(_MainTex, curUv);
}
else
{
break;
}
curStep += step;
}
}


if (fColor.a > 0)
{
fixed dis = distance(uv, curUv);
alpha = _EdgeColor.a*(1-dis/ _EdgeWidth);
}
return alpha;
}

fixed4 frag(v2f i) : SV_Target
{
fixed4 original = tex2D(_MainTex, i.uv[0]);
fixed alpha = original.a;
fixed p1 = tex2D(_MainTex, i.uv[1]).a;
fixed p2 = tex2D(_MainTex, i.uv[2]).a;
fixed p3 = tex2D(_MainTex, i.uv[3]).a;
fixed p4 = tex2D(_MainTex, i.uv[4]).a;

alpha = p1 + p2 + p3 + p4 + alpha;
alpha /= 5;
if (alpha <= _AlphaTreshold)
{
original.rgb = _EdgeColor.rgb+ _EdgeColorPower;
original.a = check(i.uv[0]);
}

return original;
}
ENDCG
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值