软件编程
位置:首页>> 软件编程>> C#编程>> Unity Shader实现径向模糊效果

Unity Shader实现径向模糊效果

作者:会编程的小毛驴  发布时间:2021-09-06 01:02:47 

标签:Unity,Shader,径向模糊

在游戏里面有很多模糊效果,像赛车类游戏。当你加速时,会发现2边的场景变模糊。如下图:

Unity Shader实现径向模糊效果

今天也来做一下径向模糊效果,首先创建一个Material,给它添加一个纹理后将Material拖到新建的Plane上。如图所示,可以看出模糊效果是从中心点由内往外扩散。接下来脑子里有了步骤

步骤一:定义径向模糊的中心点,通常取图像的正中心点。
步骤二:计算采样像素与中心点的距离,根据距离确定偏移程度,即离中心点越远,偏移量越大。
步骤三:将采样点的颜色值做平均求和。


Shader "liulongling/motion" {
Properties {
 _MainTex("纹理",2D)="while"{}
 _Level("强度",Range(0,100))=10
}
SubShader {
 Pass
 {
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "unitycg.cginc"
    sampler2D _MainTex;
    float _Level;
     struct v2f{
   fixed4 vertex:POSITION;
   fixed2 uv:TEXCOORD;
  };

v2f vert(appdata_base v){
   v2f o;
   o.vertex=mul(UNITY_MATRIX_MVP,v.vertex);
   o.uv=v.texcoord;
   return o;
  }

fixed4 frag(v2f i):COLOR{  
      fixed4 c;

fixed2 center=fixed2(.5,.5);
   fixed2 uv=i.uv-center;
   fixed3 c1=fixed3(0,0,0);
   for(fixed j=0;j<_Level;j++){
    c1+=tex2D(_MainTex,uv*(1-0.01*j)+center).rgb;
   }
   c.rgb=c1/_Level;
   c.a=1;
   return c;
     }
    ENDCG
 }
}
}

效果如下:

Unity Shader实现径向模糊效果

Unity Shader实现径向模糊效果

来源:https://blog.csdn.net/liulongling/article/details/50787445

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com