(转)【风宇冲】Unity3D教程宝典之Blur

原创文章如需转载请注明:转载自风宇冲Unity3D教程学院

 

                               

                Blur
Blur模糊其实理解了以后非常简单。核心原理就是 1个点的颜色 并不用该点的颜色,而是用该点周围所有点的均值 (1)确定取点范围, 例如周围3个像素 或者周围10个像素 (2)确定各点权重,这也是高斯模糊的由来,主要颜色分配的比重为正态分布,即高斯分布。
例子1:最简单的模糊 (1)新场景,plane上面放一张贴图 (2)plane上的shader如下
  1. Shader "Custom/ObjectBlur" {
  2.     Properties {
  3.         _MainTex ("Base (RGB)", 2D) = "white" {}
  4.     }
  5.     SubShader
  6.     {
  7.          Tags{"Queue"="Transparent"}
  8.      
  9.         pass
  10.         {
  11.             CGPROGRAM
  12.             #pragma vertex vert
  13.             #pragma fragment frag
  14.            
  15.             #include "UnityCG.cginc"
  16.             sampler2D _MainTex;
  17.             float4 _MainTex_ST;
  18.             float uvOffset;
  19.            
  20.             struct v2f {
  21.                 float4  pos : SV_POSITION;
  22.                 float2  uv : TEXCOORD0;
  23.             } ;
  24.             v2f vert (appdata_base v)
  25.             {
  26.                 v2f o;
  27.                 o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
  28.                 o.uv =  TRANSFORM_TEX(v.texcoord,_MainTex);
  29.                 return o;
  30.             }
  31.             float4 frag (v2f i) : COLOR
  32.             {
  33.                 float4 s1 = tex2D(_MainTex,i.uv + float2(uvOffset,0.00));
  34.                 float4 s2 = tex2D(_MainTex,i.uv + float2(-uvOffset,0.00));
  35.                 float4 s3 = tex2D(_MainTex,i.uv + float2(0.00,uvOffset));
  36.                 float4 s4 = tex2D(_MainTex,i.uv + float2(0.00,-uvOffset));
  37.                
  38.                 float4 texCol = tex2D(_MainTex,i.uv);
  39.                 float4 outp;
  40.                
  41.                 float pct=0.2;
  42.                 outp = texCol* (1- pct*4) + s1* pct + s2* pct+ s3* pct + s4* pct;
  43.                 return outp;
  44.             }
  45.             ENDCG
  46.         }
  47.     }
  48. }
以及BlurManager.cs脚本,如下
  1. using UnityEngine;
  2. using System.Collections;
  3. public class BlurManager : MonoBehaviour {
  4.     private float length =3f;
  5.     private float showTime = -100;
  6.     private float hideTime = -100;
  7.     void Update () {
  8.         if(showTime >0)
  9.         {
  10.             showTime -= Time.deltaTime;
  11.             Shader.SetGlobalFloat("uvOffset", (showTime/length) * 0.005f);
  12.         }
  13.        
  14.         if(hideTime >0)
  15.         {
  16.             hideTime -= Time.deltaTime;
  17.             Shader.SetGlobalFloat("uvOffset", (1- hideTime/length) * 0.005f);
  18.         }
  19.     }
  20.    
  21.     void OnGUI()
  22.     {
  23.         if(GUI.Button(new Rect(0,0,100,50),"Show"))
  24.         {
  25.             showTime = length;
  26.         }
  27.        
  28.         if(GUI.Button(new Rect(100,0,100,50),"Hide"))
  29.         {
  30.             hideTime = length;
  31.         }
  32.     }
  33. }
运行后,点击show按钮,图会从模糊变清晰,点击hide按钮会从清晰变模糊。 这基本是最简单的模糊了,取本点 和其上下左右的4个偏移点。各点权重均为0.2。uv偏移从0至0.005 效果如下图还不错。
原图 【风宇冲】Unity3D教程宝典之Blur
模糊后的效果 【风宇冲】Unity3D教程宝典之Blur
参考文章
posted @ 2015-07-13 20:26  ☆A希亿  阅读(678)  评论(0编辑  收藏  举报