Shader:
Shader "Custom/EffectShader" {
Properties {}
c#:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class OldFilmEffect : MonoBehaviour {
#region Variables
public Shader curShader;
public float OldFilmEffectAmount = 1.0f;
public Color sepiaColor = Color.white;
public Texture2D vignetteTexture;
public float vignetteAmount = 1.0f;
public Texture2D scratchesTexture;
public float scratchesYSpeed = 10.0f;
public float scratchesXSpeed = 10.0f;
public Texture2D dustTexture;
public float dustYSpeed = 10.0f;
public float dustXSpeed= 10.0f;
private Material curMaterial;
private float randomValue;
#endregion
#region Properties
Material material
{
get
{
if(curMaterial==null)
{
curMaterial = new Material(curShader);
curMaterial.hideFlags=HideFlags.HideAndDontSave;//隐藏并且不自动销毁 保存到下个场景、程序结束也不销毁
}
return curMaterial;
}
}
#endregion
void Start () {
if(!SystemInfo.supportsImageEffects)//系统找不到这个类的信息。就关闭
{
enabled = false;
return;
}
if(!curShader && !curShader.isSupported)//如果shader是空的。并且找不到shader帮助。
{
enabled =false;
}
}
// Update is called once per frame
void Update () {
//grayScaleAmount = Mathf.Clamp(grayScaleAmount,0.0f,1.0f);
Camera.main.depthTextureMode=DepthTextureMode.Depth;
//blendOpacity = Mathf.Clamp(blendOpacity,0.0f,1.0f);
//depthPower=Mathf.Clamp(depthPower,0,5);
vignetteAmount = Mathf.Clamp01(vignetteAmount);
OldFilmEffectAmount=Mathf.Clamp(OldFilmEffectAmount,0f,1.5f);
randomValue = Random.Range(-1f,1f);
}
RenderTexture r;
void OnRenderImage(RenderTexture sourceTexture,RenderTexture destTexture)//最终纹理
{
if(curShader!=null)
{
//material.SetFloat("_LuminosityAmount",grayScaleAmount);
//RenderTexture r;//=new RenderTexture(1000,1000,1000);
// if(sourceTexture==null) Debug.Log("1"); else Debug.Log("you1");
// Debug.Log(sourceTexture.width);
//material.SetFloat("_DepthPower",depthPower);
//material.SetFloat("_BrightnessAmount",brightnessAmount);
//material.SetFloat("_satAmount",saturationAmount);
//material.SetFloat("_conAmount",contrastAmount);
//material.SetTexture("_BlendTex",blendTexture);
//material.SetFloat("_Opacity",blendOpacity);
material.SetColor("_SepiaColor",sepiaColor);
material.SetFloat("_VignetteAmount",vignetteAmount);
material.SetFloat("_EffectAmount",OldFilmEffectAmount);
if(vignetteTexture)
{
material.SetTexture("_VignetteTex",vignetteTexture);
}
if(scratchesTexture)
{
material.SetTexture("_ScratchesTex",scratchesTexture);
material.SetFloat("_ScratchesYSpeed",scratchesYSpeed);
material.SetFloat("_ScratchesXSpeed",scratchesXSpeed);
}
if(dustTexture)
{
material.SetTexture("_DustTex",dustTexture);
material.SetFloat("_dustYSpedd",dustYSpeed);
material.SetFloat("_dustXSpeed",dustXSpeed);
material.SetFloat("_RandomValue",randomValue);
}
Graphics.Blit(sourceTexture,destTexture,material); //拷贝源纹理到目的渲染纹理。Blit设置dest到激活的渲染纹理,在材质上设置source作为_MainTex属性,并且绘制一个全屏方块。
// if(sourceTexture==null) Debug.Log("2"); else Debug.Log("you2");
}
else
{
Graphics.Blit(sourceTexture,destTexture);
}
}
void OnDisable()
{
if(curMaterial)
{
DestroyImmediate(curMaterial);
}
}
}