Shader创建老电影风格的屏幕特效

Shader:


Shader "Custom/EffectShader" {

Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_VignetteTex("Vignette Texture",2D)="white"{}
_ScratchesTex("Scarthes Texture",2D)="white"{}
_DustTex("Dust Texture",2D) = "white"{}
_SepiaColor("Sepia Color",color)=(1,1,1,1)
_EffectAmount("Old Film Effect Amount",Range(0,1)) = 1.0

_VignetteAmount("Vignette Opacity",Range(0,1)) = 1.0
_ScratchesYSpeed("Scratches Y Speed",float) = 10.0
_ScratchesXSpeed("Scratches X Speed",float) = 10.0
_dustXSpeed("Dust X Speed",float) = 10.0
_dustYSpeed("Dust Y Speed",float) = 10.0
_RandomValue("Random Value",float) = 1
}
SubShader {
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _VignetteTex;
uniform sampler2D _ScratchesTex;
uniform sampler2D _DustTex;
fixed4 _SepiaColor;
fixed _VignetteAmount;
fixed _ScratchesXSpeed;
fixed _ScratchesYSpeed;
fixed _dustXSpeed;
fixed _dustYSpeed;
fixed _EffectAmount;
fixed _RandomValue;
fixed4 frag(v2f_img i) : COLOR
{
half2 renderTexUV = half2(i.uv.x,i.uv.y+(_RandomValue * _SinTime.z * 0.005));//产生一个随机的UV``内置的_SinTime变量来得到一个-1到1范围内的值
fixed4 renderTex = tex2D(_MainTex,renderTexUV);//每次取样,对源纹理Y轴进行-1~1的取样偏移
fixed4 vignetteTex = tex2D(_VignetteTex,i.uv);
half2 scratchesUV = half2(i.uv.x + (_RandomValue * _SinTime.z * _ScratchesXSpeed),i.uv.y + (_Time.x * _ScratchesYSpeed));
fixed4 scratchesTex = tex2D(_ScratchesTex,scratchesUV);
half2 dustUV = half2(i.uv.x + (_RandomValue * (_SinTime.z * _dustXSpeed)),i.uv.y + (_RandomValue * (_SinTime.z * _dustYSpeed)));
fixed4 dustTex= tex2D(_DustTex,dustUV);//根据XY速度偏移
fixed lum = dot(fixed3(0.299,0.587,0.114),renderTex.rgb);//原图进行混合

fixed4 finalColor = lum + lerp(_SepiaColor,_SepiaColor + fixed4(0.1f,0.1f,0.1f,1.0f),_RandomValue);

fixed3 constantWhite = fixed3(1,1,1);

finalColor = lerp(finalColor,finalColor * vignetteTex,_VignetteAmount);
finalColor.rgb *= lerp(scratchesTex,constantWhite,(_RandomValue));

finalColor.rgb *= lerp(dustTex.rgb,constantWhite,(_RandomValue * _SinTime.z));
finalColor = lerp(renderTex,finalColor,_EffectAmount);
return finalColor;
}
ENDCG



}

}


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);
}
}
}


Shader创建老电影风格的屏幕特效_第1张图片Shader创建老电影风格的屏幕特效_第2张图片

Shader创建老电影风格的屏幕特效_第3张图片

你可能感兴趣的:(ShaderLab)