cg 語言之扇形徑向模糊
阿新 • • 發佈:2018-12-11
模糊的原理:像周圍的借取顏色,放到本畫素。不同的模糊效果是採用不同的去顏色位置和演算法。徑向模糊原理是沿著某方向取顏色,而扇形徑向則是沿著扇形的切線方向取色。
sampler2D RT : register(s0);
///////////////////////////////////入口函式
float4 Distortion_ps(//in float4 pos : POSITION,
float2 iTexCoord : TEXCOORD0,
) : COLOR
{
float4 co=float4(0);
///////徑向模糊
float2 dir =float2(0.5,0)-iTexCoord;
float dist=length(dir);
dir/=dist;
float3 dirDot=cross(float3(0.5,0,1),float3(dir.x,dir.y,0));
//normalize(dirDot);
dir=float2(dirDot.x,dirDot.y);
dir=dir/(length(dir));
dir*=0.35*dist;//模糊程度與距離中心的長度相關
float4 sum =float4(0 );
sum= tex2D(RT, iTexCoord);
sum +=tex2D(RT, iTexCoord - dir*0.01)*0.8;
sum += tex2D(RT, iTexCoord - dir*0.02)*0.6;
sum += tex2D(RT, iTexCoord - dir*0.03)*0.5;
sum += tex2D(RT, iTexCoord - dir*0.04)*0.3;
sum += tex2D(RT, iTexCoord - dir*0.05)*0.1;
sum += tex2D(RT, iTexCoord + dir *0.01)*0.8;
sum += tex2D(RT, iTexCoord + dir*0.02)*0.6;
sum += tex2D(RT, iTexCoord + dir*0.03)*0.5;
sum += tex2D(RT, iTexCoord + dir*0.04)*0.3;
sum += tex2D(RT, iTexCoord + dir*0.05)*0.1;
sum /= 5.6;
co=sum;
co.a=1;
return co;
}