之前是把径向模糊应用与粒子系统中,这次把效果整合进Post Processing Volume,也是做成Renderer Feature的形式,通过Volume Component控制参数,自定义Volume的代码与之前LetterBoxing的写法基本一致,只需把Shader和Volume参数的定义修改一下。
Shader
Shader "MyAct/RadialBlur" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } Cull Off ZWrite Off ZTest Always Pass { CGPROGRAM #pragma vertex Vert #pragma fragment Frag #include "UnityCG.cginc" sampler2D _MainTex; half4 _Param; #define _Offset _Param.x #define _Iteration _Param.y #define _Center _Param.zw 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; } fixed4 Frag (v2f i) : SV_Target { float2 blurVector = (_Center - i.uv.xy) * _Offset; half4 accumulateColor = half4(0, 0, 0, 0); [unroll(30)]//让编译器展开循环,执行效率更佳 for (int j = 0; j < _Iteration; j ++) { accumulateColor += tex2D(_MainTex, i.uv); i.uv.xy += blurVector; } return accumulateColor / _Iteration; } ENDCG } } }