运动模糊,代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 using UnityEngine; 2 3 public class MotionBlurRenderer : PostEffectRenderer 4 { 5 [Range(0.1f, 0.9f)] 6 [SerializeField] 7 float m_blurAmount = 0.1f; 8 9 RenderTexture m_accumulationTexture; 10 11 void OnDisable() 12 { 13 DestroyImmediate(m_accumulationTexture); 14 } 15 16 protected override void OnRenderImage(RenderTexture src, RenderTexture dest) 17 { 18 if (!m_accumulationTexture || m_accumulationTexture.width != src.width || m_accumulationTexture.height != src.height) 19 { 20 DestroyImmediate(m_accumulationTexture); 21 m_accumulationTexture = new RenderTexture(src.width, src.height, 0); 22 //m_accumulationTexture.hideFlags = HideFlags.HideAndDontSave; 23 Graphics.Blit(src, m_accumulationTexture); 24 } 25 26 //m_accumulationTexture.MarkRestoreExpected(); 27 28 Mat.SetFloat("_BlurAmount", m_blurAmount); 29 30 Graphics.Blit(src, m_accumulationTexture, Mat); 31 Graphics.Blit(m_accumulationTexture, dest); 32 33 base.OnRenderImage(src, dest); 34 } 35 36 protected override string ShaderName 37 { 38 get { return "Custom/Study/Motion Shader"; } 39 } 40 }
对应shader如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' 2 3 Shader "Custom/Study/Motion Shader" 4 { 5 Properties 6 { 7 _MainTex ("Main Texture", 2D) = "white" {} 8 _BlurAmount ("Blur Amount", Range(0,1)) = 0.5 9 } 10 11 SubShader 12 { 13 ZWrite Off 14 ZTest Always 15 Cull Off 16 17 Pass 18 { 19 Blend SrcAlpha OneMinusSrcAlpha 20 21 CGPROGRAM 22 #pragma vertex vert 23 #pragma fragment frag 24 25 sampler2D _MainTex; 26 fixed _BlurAmount; 27 28 struct appdata 29 { 30 float4 vertex : POSITION; 31 float2 uv : TEXCOORD0; 32 }; 33 34 struct v2f 35 { 36 float4 pos : SV_POSITION; 37 float2 uv : TEXCOORD0; 38 }; 39 40 v2f vert(appdata v) 41 { 42 v2f o; 43 o.pos = UnityObjectToClipPos(v.vertex); 44 o.uv = v.uv; 45 return o; 46 } 47 48 fixed4 frag(v2f i) : SV_TARGET 49 { 50 fixed4 tex = tex2D(_MainTex, i.uv); 51 return fixed4(tex.rgb, _BlurAmount); 52 } 53 54 ENDCG 55 } 56 } 57 58 Fallback Off 59 }
效果如下: