滤镜管理代码(挂在Camera对象上)
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class CameraShader : MonoBehaviour {
private Material mat;
private Shader shader;
private float TimeX = 1.0f;
[Range(1, 10)]
public float Distortion = 1.0f;
[Range(0.5f, 1.0f)]
public float speed = 0.75f;
Material material
{
get
{
if(mat == null)
{
mat = new Material(shader);
//HideAndDontSave:保留对象到新场景 此属性的功能是用来设置是否将Object对象保留到新Scene中,如果使用HideFlags.HideAndDontSave,
//则Object对象将在新Scene中被保留下来,但不会显示在Hierarchy面板中。所以后面需要用DestroyImmediate()销毁。
mat.hideFlags = HideFlags.HideAndDontSave;
}
return mat;
}
}
// Use this for initialization
void Start () {
//获取shader
shader = Shader.Find("CameraFilterPack/TV_Old_Movie");
//判断显卡是否支持图像后期处理效果
if (!SystemInfo.supportsImageEffects)
{
enabled = false; //此为代码的enable
return;
}
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (shader != null)
{
//shader参数控制
TimeX += Time.deltaTime * speed;
if (TimeX > 100) TimeX = 0;
material.SetFloat("_TimeX", TimeX);
material.SetFloat("_Distortion", Distortion);
//渲染
Graphics.Blit(source, destination, material);
}
else
{
Graphics.Blit(source, destination);
}
}
// Update is called once per frame
void Update()
{
#if UNITY_EDITOR
if (Application.isPlaying != true)
{
shader = Shader.Find("CameraFilterPack/TV_Old_Movie");
}
#endif
}
void OnDisable()
{
if (mat)
{
DestroyImmediate(mat);
}
}
}
shader代码,根据需求选择所需的shader,下面这个是老电影风格的shader滤镜
Shader "CameraFilterPack/TV_Old_Movie" {
Properties
{
_MainTex ("Base (RGB)", 2D) = "white" {}
_TimeX ("Time", Range(0.0, 1.0)) = 1.0
_Distortion ("_Distortion", Range(1.0, 10.0)) = 1.0
}
SubShader
{
Pass
{
Cull Off ZWrite Off ZTest Always
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float _TimeX;
uniform float _Distortion;
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float2 texcoord : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 color : COLOR;
};
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color;
return OUT;
}
float rand(float2 co){
return frac(sin(dot(co.xy ,float2(12.9898,78.233))) * 43758.5453);
}
float rand(float c){
return rand(float2(c,1.0));
}
float randomLine(float seed, float2 uv)
{
float aa = rand(seed+1.0);
float b = 0.01 * aa;
float c = aa - 0.5;
float l;
if ( aa > 0.2)
l = pow( abs(aa * uv.x + b * uv.y + c ), 0.125);
else
l = 2.0 - pow( abs(aa * uv.x + b * uv.y + c), 0.125 );
return lerp(0.5, 1.0, l);
}
float randomBlotch(float seed, float2 uv)
{
float x = rand(seed);
float y = rand(seed+1.0);
float s = 0.01 * rand(seed+2.0);
float2 p = float2(x,y) - uv;
p.x *= 1;
float aa = atan(p.y/p.x);
float v = 1.0;
float ss = s*s * (sin(6.2831*aa*x)*0.1 + 1.0);
if ( dot(p,p) < ss ) v = 0.2;
else v = pow(dot(p,p) - ss, 1.0/16.0);
return lerp(0.3 + 0.2 * (1.0 - (s / 0.02)), 1.0, v);
}
half4 _MainTex_ST;
float4 frag(v2f i) : COLOR
{
float2 uvst = UnityStereoScreenSpaceUVAdjust(i.texcoord, _MainTex_ST);
float2 uv;
uv = uvst;
float t = float(int(_TimeX * 15.0));
float2 suv = uv + 0.002 * float2( rand(t), rand(t + 23.0));
float3 image = tex2D( _MainTex, float2(suv.x, suv.y) );
float luma = dot( float3(0.2126, 0.7152, 0.0722), image );
float3 oldImage = luma * float3(0.7, 0.7, 0.7);
float randx=rand(t + 8.);
float vI = 16.0 * (uv.x * (1.0-uv.x) * uv.y * (1.0-uv.y));
vI *= lerp( 0.7, 1.0, randx+.5);
vI += 1.0 + 0.4 *randx;
vI *= pow(16.0 * uv.x * (1.0-uv.x) * uv.y * (1.0-uv.y), 0.4);
int l = int(8.0 * randx);
if ( 0 < l ) vI *= randomLine( t+6.0+17.* float(0),uv);
if ( 1 < l ) vI *= randomLine( t+6.0+17.* float(1),uv);
int s = int( max(8.0 * rand(t+18.0) -2.0, 0.0 ));
if ( 0 < s ) vI *= randomBlotch( t+6.0+19.* float(0),uv);
if ( 1 < s ) vI *= randomBlotch( t+6.0+19.* float(1),uv);
return float4(oldImage * vI, 1.0);
}
ENDCG
}
}
}