Unity – Radial Blur Filter

一個簡易且不錯用的 shader !!

原文:Radial Blur Filter
轉寫原文: Unity Radial Blur Post Process Shader

實際測試後,加入了一部份用以解決反轉問題,以及可調整的參數:

Shader "IE/RadialBlurFilter" {

	Properties
	{
	    _MainTex ("Base (RGB)", 2D) = "white" {}
	    _SampleDist ("SampleDist", Float) = 1
	    _SampleStrength ("SampleStrength", Float) = 2.2
	}

	SubShader
	{
	    Pass
	    {
	        ZTest Always Cull Off ZWrite Off
	        Fog { Mode off }

			CGPROGRAM
			// Upgrade NOTE: excluded shader from DX11, Xbox360, OpenGL ES 2.0 because it uses unsized arrays
			#pragma exclude_renderers d3d11 xbox360 gles
			#pragma exclude_renderers xbox360
			#pragma target 3.0
			#pragma vertex vert
			#pragma fragment frag
			#pragma fragmentoption ARB_precision_hint_fastest

			#include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform float4 _MainTex_ST;
			uniform float4 _MainTex_TexelSize;
			float _SampleDist;
			float _SampleStrength;

			struct v2f {
			    float4 pos : POSITION;
			    float2 uv : TEXCOORD0;
			};

			v2f vert (appdata_img v)
			{
			    v2f o;
			    o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
			    o.uv = v.texcoord.xy;
			    return o;
			}

			float4 frag (v2f i) : COLOR
			{
				float2 texCoord = i.uv;

				#if SHADER_API_D3D9
				if (_MainTex_TexelSize.y < 0)
				        texCoord.y = 1 - texCoord.y;
				#endif

			    float2 dir = 0.5 - texCoord;

			    float dist = length(dir);

			    dir /= dist;

			    float4 color = tex2D(_MainTex, texCoord);

			    float4 sum = color;

			    float samples[10] = float[](-0.08,-0.05,-0.03,-0.02,-0.01,0.01,0.02,0.03,0.05,0.08);

			    for (int i = 0; i < 10; ++i)
			    {
			        sum += tex2D(_MainTex, texCoord + dir * samples[i] * _SampleDist);
			    }

			    sum *= 1.0 / 11.0;

			    float t = saturate(dist * _SampleStrength);

			    return lerp(color, sum, t);
			}
			ENDCG
		}
	}
	Fallback off
}

反轉問題的成因應該是 Render Texture Coordinates 的差異。
詳細可參考:[譯]Platform Specific Rendering Differences

接著是把這個特效實作 Camera 的畫面擷取與套用的部分:

using UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Image Effects/RadialBlurFilter")]
class GD_IE_RadialBlurFilter : ImageEffectBase
{
	public float SampleDist = 1f;
	public float SampleStrength = 2.2f;

	override protected void Start()
	{
		base.Start();
		// set shader
		this.shader = Shader.Find("IE/RadialBlurFilter");
	}

	void OnRenderImage(RenderTexture source, RenderTexture destination)
	{
		// 結合
		this.material.SetTexture("_MainTex", source);
		this.material.SetFloat("_SampleDist", SampleDist);
		this.material.SetFloat("_SampleStrength", SampleStrength);

		Graphics.Blit(null, destination, this.material);
	}
}

祝您使用愉快:)

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s