http://blog.csdn.net/panda1234lee/article/details/52199296
这篇文章是之前群里一个比较厉害的图形程序写的.
在PHOTOSHOP里,羽化就是使你选定范围的图边缘达到朦胧的效果。
羽化值越大,朦胧范围越宽,羽化值越小,朦胧范围越窄。可根据你想留下图的大小来调节。
算法分析:
1、通过对rgb值增加额外的V值实现朦胧效果
2、通过控制V值的大小实现范围控制。
3、V = 255 * 当前点Point距中点距离的平方s1 / (顶点距中点的距离平方 *mSize)s2;
4、s1 有根据 ratio 修正 dx dy值。
原图:
效果图:
片元着色器代码:
uniform sampler2D Tex;
const float size = 0.5;
void main(void)
{
vec2 realSize = vec2(textureSize(Tex, 0));
float ratio = (realSize.x > realSize.y) ?
realSize.y/realSize.x : realSize.x/realSize.y;
vec2 texSize = vec2(512., 512.);
vec2 xy = gl_FragCoord.xy;
if(realSize.x > realSize.y)
{
xy.x = xy.x * ratio;
}
else
{
xy.y = xy.y * ratio;
}
vec2 center = vec2(texSize/2.);
// ----------------------------------------------------
float maxV = dot(center, center);
float minV = floor(maxV*(1. - size));
float diff = maxV - minV;
vec2 uv = xy / texSize;
vec4 srcColor = texture2D(Tex, uv);
float dx = center.x - xy.x;
float dy = center.y - xy.y;
float dstSq = pow(dx, 2.) + pow(dy, 2.);
float v = (dstSq / diff);
float r = clamp(srcColor.r + v, 0., 1.);
float g = clamp(srcColor.g + v, 0., 1.);
float b = clamp(srcColor.b + v, 0., 1.);
gl_FragColor = vec4( r, g, b, 1.0 );
}
以下是翻译成unity shader的版本
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/eclosion" {
Properties {
_MainTex ("Main Tex", 2D) = "white" {}
_Diff("Diff", Range(0, 1)) = 0.5
}
SubShader {
Pass {
Tags { "LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
float _Diff;
struct a2v {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD1;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 srcColor = tex2D(_MainTex, i.uv).rgb;
float dx = i.uv.x - 0.5;
float dy = i.uv.y - 0.5;
float dstSq = pow(dx, 2.0) + pow(dy, 2.0);
float v = (dstSq / _Diff);
float r = clamp(srcColor.r + v, 0.0, 1.0);
float g = clamp(srcColor.g + v, 0.0, 1.0);
float b = clamp(srcColor.b + v, 0.0, 1.0);
return fixed4(r,g,b,1.0);
}
ENDCG
}
}
FallBack "Specular"
}