标签:
Shader "Custom/ObjectBlur" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}
SubShader
{
Tags{"Queue"="Transparent"}
pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
float uvOffset;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
} ;
v2f vert (appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
return o;
}
float4 frag (v2f i) : COLOR
{
float4 s1 = tex2D(_MainTex,i.uv + float2(uvOffset,0.00));
float4 s2 = tex2D(_MainTex,i.uv + float2(-uvOffset,0.00));
float4 s3 = tex2D(_MainTex,i.uv + float2(0.00,uvOffset));
float4 s4 = tex2D(_MainTex,i.uv + float2(0.00,-uvOffset));
float4 texCol = tex2D(_MainTex,i.uv);
float4 outp;
float pct=0.2;
outp = texCol* (1- pct*4) + s1* pct + s2* pct+ s3* pct + s4* pct;
return outp;
}
ENDCG
}
}
}
using UnityEngine;
using System.Collections;
public class BlurManager : MonoBehaviour {
private float length =3f;
private float showTime = -100;
private float hideTime = -100;
void Update () {
if(showTime >0)
{
showTime -= Time.deltaTime;
Shader.SetGlobalFloat("uvOffset", (showTime/length) * 0.005f);
}
if(hideTime >0)
{
hideTime -= Time.deltaTime;
Shader.SetGlobalFloat("uvOffset", (1- hideTime/length) * 0.005f);
}
}
void OnGUI()
{
if(GUI.Button(new Rect(0,0,100,50),"Show"))
{
showTime = length;
}
if(GUI.Button(new Rect(100,0,100,50),"Hide"))
{
hideTime = length;
}
}
}
运行后,点击show按钮,图会从模糊变清晰,点击hide按钮会从清晰变模糊。


标签:
原文地址:http://www.cnblogs.com/zdlbbg/p/4329540.html