场景渲染完以后再添加一些特效
模糊:将一个像素周围的几个像素求平均(一般区周围4个点的像素))
-
引擎渲染后最终的结果是一张图片
void OnRenderImage(RenderTexture src, RenderTexture dest) //入口函数
Graphics.Blit(src,dest,myMaterial);——拦截相机渲染出来的图片,用指定方式修改后再重新交给引擎 -
将图片传给Shader进行二次计算
C#核心函数
private void OnRenderImage(RenderTexture src, RenderTexture dest) {
Graphics.Blit(src,dest,myMaterial);
//src 摄像机输入,dest指定材质球处理之后的输出图片
//myMaterial 指定的材质球
}
Shader部分代码:
Shader "Hidden/GaoSi"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Ambient("Ambient",float)=0.001
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
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;
float _Ambient;
fixed4 frag (v2f i) : SV_Target
{
float2 tmpUV=i.uv;
fixed4 col0 = tex2D(_MainTex, tmpUV+float2(-_Ambient,0));
fixed4 col1 = tex2D(_MainTex, tmpUV+float2(0,-_Ambient));
fixed4 col2 = tex2D(_MainTex, tmpUV+float2(0,_Ambient));
fixed4 col3 = tex2D(_MainTex, tmpUV+float2(_Ambient,0));
fixed4 col4 = tex2D(_MainTex, tmpUV);
fixed4 col=(col0+col1+col2+col3+col4)/5; //周围五个点求平均
// just invert the colors
//col.rgb = 1 - col.rgb;
return col;
}
ENDCG
}
}
}