挂在主相机上的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]//在编译器下执行脚本(不用点击开始即执行)
public class Post : MonoBehaviour
{
public Material EffectMat;//承载后处理Shader的材质
private void OnRenderImage(RenderTexture source, RenderTexture destination)//该函数在所有的渲染完成后由monobehavior自动调用。
//官方解释:该函数允许我们使用着色器滤波操作来修改最终的图像,输入原图像source,输出的图像放在desitination里。
{
if (EffectMat !=null)
{
Graphics.Blit(source, destination, EffectMat);//该函数的作用就是通过一个shader将原图的像素放置到destionation中。
}
}
}
——————————————————————————————————————
Shader1画面溶解流动效果:
Shader "Shader104/Distortion"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_DisplaceTex("Displacement Texture", 2D) = "white" {}
_Magnitude("Magnitude", Range(0,0.1)) = 1
}
SubShader
{
Tags
{
"Queue" = "Transparent"
}
Pass{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
};
sampler2D _MainTex;
sampler2D _DisplaceTex;//噪波贴图
float _Magnitude;
float4 frag(v2f i): SV_Target//画面流动效果
{
float2 distuv = float2(i.uv.x + _Time.x * 2, i.uv.y + _Time.x * 2);
float2 disp = tex2D(_DisplaceTex, distuv).xy;//噪波贴图的.rg
disp = ((disp * 2) - 1) * _Magnitude;
float4 col = tex2D(_MainTex, i.uv + disp);
return col;
}
ENDCG
}
}
}
————————————————————————————————————————
Shader2屏幕黑白、反色、RGB分离、像素效果:
Shader "Shader104/GreyScale"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_Slider("Slider",Range(0,1)) = 0.5
}
SubShader
{
Tags
{
"PreviewType" = "Plane"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv: TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv: TEXCOORD0;
};
sampler2D _MainTex;
float _Slider;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float4 frag(v2f i) : SV_Target
{
/*pixelate像素效果对UV进行变换
float2 uv = i.uv;
uv.x *= _PixelateAmt;
uv.y *= _PixelateAmt;
uv.x = round(uv.x);
uv.y = round(uv.y);
uv.x /= _PixelateAmt;
uv.y /= _PixelateAmt;*/
float4 color = tex2D(_MainTex,i.uv);//默认混合颜色(白色):不进行后处理
float average = 0.212 * color.r + 0.7152 * color.g + 0.0722 * color.b;//颜色黑白化
if (i.uv.x > _Slider)//大于Slidr的部分进行后处理
return float4(average, average, average, 1);
//return float4(1-color.r, 1-color.g, 1-color.b, 1);//反色
else
return color;
/*chromatic aberration RGB分离
float4 red = tex2D(_MainTex , i.uv - _Offset);
float4 green = tex2D(_MainTex, i.uv);
float4 blue = tex2D(_MainTex, i.uv + _Offset);
float4 color = float4(red.r,green.g,blue.b,1);
return color;*/
}
ENDCG
}
}
}
————————————————————————————————————————
Shader3Kernal矩阵边缘检测、渐变着色:
Shader "Shader104/Kernal"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Matrix00 ("Matrix00",float) = 1
_Matrix01 ("Matrix01",float) = 1
_Matrix02 ("Matrix02",float) = 1
_Matrix10 ("Matrix10",float) = 1
_Matrix11 ("Matrix11",float) = -8
_Matrix12 ("Matrix12",float) = 1
_Matrix20 ("Matrix20",float) = 1
_Matrix21 ("Matrix21",float) = 1
_Matrix22 ("Matrix22",float) = 1
_Slider("Slider",Range(0,1)) = 0.5
_Threshold("_Threshold",Range(0,1)) = 0.1
_EdgeColor("_EdgeColor",Color) = (1,1,1,1)
_BackColor("_BackColor",Color) = (0,0,0,0)
}
SubShader
{
Tags
{
"PreviewType" = "Plane"
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _MainTex_TexelSize;
float _Matrix00;
float _Matrix01;
float _Matrix02;
float _Matrix10;
float _Matrix11;
float _Matrix12;
float _Matrix20;
float _Matrix21;
float _Matrix22;
float _Slider;
float _Threshold;
float4 _EdgeColor;//边缘色
float4 _BackColor;//背景色
float4 box(sampler2D tex, float2 uv, float4 size)//检测函数
{
float4 c = tex2D(tex, uv + float2(-size.x, size.y))*_Matrix00 + tex2D(tex, uv + float2(0, size.y))*_Matrix01
+ tex2D(tex, uv + float2(size.x, size.y)) *_Matrix02 + tex2D(tex, uv + float2(-size.x, 0)) * _Matrix10
+ tex2D(tex, uv + float2(0, 0))*_Matrix11 + tex2D(tex, uv + float2(size.x, 0))*_Matrix12
+ tex2D(tex, uv + float2(-size.x, -size.y))*_Matrix20 + tex2D(tex, uv + float2(0, -size.y))*_Matrix21
+ tex2D(tex, uv + float2(size.x, -size.y))*_Matrix22;
return c / (_Matrix00+_Matrix01+_Matrix02+_Matrix10+_Matrix11+_Matrix12+_Matrix20+_Matrix21+_Matrix22);
}
float4 frag (v2f i) : SV_Target
{
float4 color = tex2D(_MainTex, i.uv);
if(i.uv.x>_Slider)
{//边缘检测
color = box(_MainTex, i.uv, _MainTex_TexelSize);
if (color.r <= _Threshold && color.g <= _Threshold && color.b <= _Threshold)//渐变着色
return _EdgeColor;
else
return _BackColor;
}
else
if (color.r <= _Threshold && color.g <= _Threshold && color.b <= _Threshold)//渐变着色
return color;
else
return float4(1, 1, 1, 1);//未上色部分为白色
}
ENDCG
}
}
}
————————————————————————————————————
Shader深度值渲染/范围扫描:
相机脚本:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class ReplacementShaderEffect : MonoBehaviour
{
public Color color;
public float slider;
public Shader ReplacementShader;
void OnValidate()
{
Shader.SetGlobalColor("_Color_1", color);//获取Shader中未公开的全局变量
}
private void Update()
{
Shader.SetGlobalFloat("_Temp", slider);//获取Shader中未公开的全局变量
}
void OnEnable()
{
if (ReplacementShader != null)
GetComponent<Camera>().SetReplacementShader(ReplacementShader, "");
}
void OnDisable()
{
GetComponent<Camera>().ResetReplacementShader();
}
}
————————————————————————————————
Shader程序:
Shader "Shader102/Depth"
{
SubShader
{
Tags
{
"RenderType" = "Opaque"
}
ZWrite On
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float4 vertex : SV_POSITION;
float depth : DEPTH;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.depth = -(mul(UNITY_MATRIX_MV, v.vertex).z) * _ProjectionParams.w;//相机坐标空间下的深度值
return o;
}
//未公开的全局变量
float4 _Color_1;
float _Temp;
//根据深度值切换渲染方式
/*fixed4 frag(v2f i) : SV_Target
{
float invert = 1 - i.depth;//近处颜色为白色
if (i.depth <_Temp / 2)//不同深度下实现不同的渲染方式
return fixed4(invert, invert, invert, 1);
else
return fixed4(invert*1.5, 0.3, 0.3,1) ;
}*/
//范围扫描
fixed4 frag(v2f i) : SV_Target
{
float invert = 1 - i.depth;//近处颜色为白色
if (abs(i.depth- _Temp / 2)<0.005)//深度范围绝对值内扫描的渲染方式
return fixed4(invert*2, 0.2, 0.2, 1);
else//正常渲染方式
return fixed4(invert, 0.3, 0.3,1) ;
}
ENDCG
}
}
}
数值动画/代码:
timer += Time.deltaTime;
if (timer >2.0f)
{
ht += Time.deltaTime*(1/5f);
slider = Mathf.Lerp(0f, 10f, 0.5f * ht);
if (ht>=1f)
{
ht = 0;
slider = 0;
timer = 0;
}
}