Skip to content

Instantly share code, notes, and snippets.

@baobao
Last active May 2, 2017
Embed
What would you like to do?
CameraBlur
using UnityEngine;
[ExecuteInEditMode]
public class CameraBlur : MonoBehaviour
{
public string shaderName = "Hidden/CameraBlur";
Material m_material;
RenderTexture[] m_rtList;
void OnRenderImage(RenderTexture src, RenderTexture dst)
{
if (m_material == null)
{
var shader = Shader.Find(shaderName);
if(shader == null) return;
m_material = new Material(shader);
m_material.hideFlags = HideFlags.DontSave;
int w = src.width;
int h = src.height;
// 1フレームずつずらしたRenderTextureの配列
m_rtList = new [] {
new RenderTexture(w, h, 0, RenderTextureFormat.RGB565),
new RenderTexture(w, h, 0, RenderTextureFormat.RGB565),
new RenderTexture(w, h, 0, RenderTextureFormat.RGB565)
};
}
// 1フレームずらしたフレームバッファをコピーする
var tmp = m_rtList[Time.frameCount % 3];
Graphics.Blit(src, tmp);
for(int i = 0; i<m_rtList.Length;i++)
{
// シェーダにレンダーテクスチャを渡す
m_material.SetTexture("_Tex" + i, m_rtList[i]);
}
// シェーダに渡されたレンダーテクスチャを合成して出力
Graphics.Blit(src, dst, m_material);
}
}
Shader "Hidden/CameraBlur"
{
Properties
{
_MainTex("Main",2D) = "white" {}
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _Tex0;
sampler2D _Tex1;
sampler2D _Tex2;
fixed4 frag (v2f_img i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv)*0.25;
fixed4 col0 = tex2D(_Tex0, i.uv)*0.25;
fixed4 col1 = tex2D(_Tex1, i.uv)*0.25;
fixed4 col2 = tex2D(_Tex2, i.uv)*0.25;
return col + col0 + col1 + col2;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment