Unity全屏模糊
先上效果,左边模糊
其实用的是Unity Stard Effect里的资源,一个脚本一个shader
//脚本代码
using UnityEngine; using System.Collections; [ExecuteInEditMode] [AddComponentMenu("Image Effects/Blur/Blur")] public class BlurEffect : MonoBehaviour { /// Blur iterations - larger number means more blur. public int iterations = 3; /// Blur spread for each iteration. Lower values /// give better looking blur, but require more iterations to /// get large blurs. Value is usually between 0.5 and 1.0. public float blurSpread = 0.6f; // -------------------------------------------------------- // The blur iteration shader. // Basically it just takes 4 texture samples and averages them. // By applying it repeatedly and spreading out sample locations // we get a Gaussian blur approximation. public Shader blurShader = null; //private static string blurMatString = static Material m_Material = null; protected Material material { get { if (m_Material == null) { m_Material = new Material(blurShader); m_Material.hideFlags = HideFlags.DontSave; } return m_Material; } } protected void OnDisable() { if( m_Material ) { DestroyImmediate( m_Material ); } } // -------------------------------------------------------- protected void Start() { // Disable if we don't support image effects if (!SystemInfo.supportsImageEffects) { enabled = false; return; } // Disable if the shader can't run on the users graphics card if (!blurShader || !material.shader.isSupported) { enabled = false; return; } } // Performs one blur iteration. public void FourTapCone (RenderTexture source, RenderTexture dest, int iteration) { float off = 0.5f + iteration*blurSpread; Graphics.BlitMultiTap (source, dest, material, new Vector2(-off, -off), new Vector2(-off, off), new Vector2( off, off), new Vector2( off, -off) ); } // Downsamples the texture to a quarter resolution. private void DownSample4x (RenderTexture source, RenderTexture dest) { float off = 1.0f; Graphics.BlitMultiTap (source, dest, material, new Vector2(-off, -off), new Vector2(-off, off), new Vector2( off, off), new Vector2( off, -off) ); } // Called by the camera to apply the image effect void OnRenderImage (RenderTexture source, RenderTexture destination) { int rtW = source.width/4; int rtH = source.height/4; RenderTexture buffer = RenderTexture.GetTemporary(rtW, rtH, 0); // Copy source to the 4x4 smaller texture. DownSample4x (source, buffer); // Blur the small texture for(int i = 0; i < iterations; i++) { RenderTexture buffer2 = RenderTexture.GetTemporary(rtW, rtH, 0); FourTapCone (buffer, buffer2, i); RenderTexture.ReleaseTemporary(buffer); buffer = buffer2; } Graphics.Blit(buffer, destination); RenderTexture.ReleaseTemporary(buffer); } }
//shader
// electricity/lightning shader // pixel shader 2.0 based rendering of electric spark // by Ori Hanegby // Free for any kind of use. Shader "FX/Lightning" { Properties { _SparkDist ("Spark Distribution", range(-1,1)) = 0 _MainTex ("MainTex (RGB)", 2D) = "white" {} _Noise ("Noise", 2D) = "noise" {} _StartSeed ("StartSeed", Float) = 0 _SparkMag ("Spark Magnitude" , range(1,100)) = 1 _SparkWidth ("Spark Width" , range(0.001,0.499)) = 0.25 } Category { // We must be transparent, so other objects are drawn before this one. Tags { "Queue"="Transparent" } SubShader { // Main pass: Take the texture grabbed above and use the bumpmap to perturb it // on to the screen Blend one one ZWrite off Pass { Name "BASE" Tags { "LightMode" = "Always" } CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma fragmentoption ARB_precision_hint_fastest #include "UnityCG.cginc" struct appdata_t { float4 vertex : POSITION; float2 texcoord: TEXCOORD0; }; struct v2f { float4 vertex : POSITION; float2 uvmain : TEXCOORD0; }; float _SparkDist; float4 _Noise_ST; float4 _MainTex_ST; float4 _ObjectScale; v2f vert (appdata_t v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex ); return o; } sampler2D _GrabTexture; float4 _GrabTexture_TexelSize; sampler2D _Noise; sampler2D _MainTex; float _GlowSpread; float _GlowIntensity; float _StartSeed; float _SparkMag; float _SparkWidth; half4 frag( v2f i ) : COLOR { float2 noiseVec = float2(i.uvmain.y / 5,abs(sin(_Time.x + _StartSeed)) * 256); float4 noiseSamp = tex2D( _Noise,noiseVec); float dvdr = 1.0 - abs(i.uvmain.y - 0.5) * 2; dvdr = clamp(dvdr+_SparkDist,0,1); float fullWidth = 1 - _SparkWidth * 2; // Center the scaled texture float scaledTexel = (i.uvmain.x - _SparkWidth) / fullWidth; float offs = scaledTexel + ((0.5 - noiseSamp.x)/2) * _SparkMag * dvdr; offs = clamp(offs,0,1); float2 texSampVec = float2(offs,i.uvmain.y); half4 col = tex2D( _MainTex, texSampVec); return col; } ENDCG } } // ------------------------------------------------------------------ // Fallback for older cards SubShader { Blend one one ZWrite off Pass { Name "BASE" SetTexture [_MainTex] { combine texture } } } } }
用法:
创建个摄像机,挂上上面的脚本,然后把Blur Shader用这个shader赋值就行了