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值。
原圖:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DN3cTNxYDN0EzMxgDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
效果圖:
片元着色器代碼:
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"
}