float4 frag(v2f i) :COLOR { float4 c = tex2D(_MainTex, i.uv_MainTex); float y = dot(float4(0.3,0.59,0.11,1),c); float yd = _Exp * (_Exp / _BM + 1) / (_Exp + 1); return c*yd; }
权重分布如下,离当前像素越远,权重越低
double sigma = (double)radius / 3.0; double sigma2 = 2.0 * sigma * sigma; double sigmap = sigma2 * PI; for(long n = 0, i = - radius; i <=radius; ++i) { long i2 = i * i; for(long j = -radius; j <= radius; ++j, ++n) kernel[n] = exp(-(double)(i2 + j * j) / sigma2) / sigmap; }
针对这个公式我们可以算出3*3,5*5,7*7等滤波器,出于性能考虑,我们还是使用5*5滤波器
3*3滤波器
5*5滤波器float3 mc00 = tex2D (_MainTex, i.uv_MainTex-fixed2(2,2)/_inten).rgb; float3 mc10 = tex2D (_MainTex, i.uv_MainTex-fixed2(1,2)/_inten).rgb; float3 mc20 = tex2D (_MainTex, i.uv_MainTex-fixed2(0,2)/_inten).rgb; float3 mc30 = tex2D (_MainTex, i.uv_MainTex-fixed2(-1,2)/_inten).rgb; float3 mc40 = tex2D (_MainTex, i.uv_MainTex-fixed2(-2,2)/_inten).rgb; float3 mc01 = tex2D (_MainTex, i.uv_MainTex-fixed2(2,1)/_inten).rgb; float3 mc11 = tex2D (_MainTex, i.uv_MainTex-fixed2(1,1)/_inten).rgb; float3 mc21 = tex2D (_MainTex, i.uv_MainTex-fixed2(0,1)/_inten).rgb; float3 mc31 = tex2D (_MainTex, i.uv_MainTex-fixed2(-1,1)/_inten).rgb; float3 mc41 = tex2D (_MainTex, i.uv_MainTex-fixed2(-2,1)/_inten).rgb; float3 mc02 = tex2D (_MainTex, i.uv_MainTex-fixed2(2,0)/_inten).rgb; float3 mc12 = tex2D (_MainTex, i.uv_MainTex-fixed2(1,0)/_inten).rgb; float3 mc22mc = tex2D (_MainTex, i.uv_MainTex).rgb; float3 mc32 = tex2D (_MainTex, i.uv_MainTex-fixed2(-1,0)/_inten).rgb; float3 mc42 = tex2D (_MainTex, i.uv_MainTex-fixed2(-2,0)/_inten).rgb; float3 mc03 = tex2D (_MainTex, i.uv_MainTex-fixed2(2,-1)/_inten).rgb; float3 mc13 = tex2D (_MainTex, i.uv_MainTex-fixed2(1,-1)/_inten).rgb; float3 mc23 = tex2D (_MainTex, i.uv_MainTex-fixed2(0,-1)/_inten).rgb; float3 mc33 = tex2D (_MainTex, i.uv_MainTex-fixed2(-1,-1)/_inten).rgb; float3 mc43 = tex2D (_MainTex, i.uv_MainTex-fixed2(-2,-1)/_inten).rgb; float3 mc04 = tex2D (_MainTex, i.uv_MainTex-fixed2(2,-2)/_inten).rgb; float3 mc14 = tex2D (_MainTex, i.uv_MainTex-fixed2(1,-2)/_inten).rgb; float3 mc24 = tex2D (_MainTex, i.uv_MainTex-fixed2(0,-2)/_inten).rgb; float3 mc34 = tex2D (_MainTex, i.uv_MainTex-fixed2(-1,-2)/_inten).rgb; float3 mc44 = tex2D (_MainTex, i.uv_MainTex-fixed2(-2,-2)/_inten).rgb; float3 c=0; c+=(mc00+mc40+mc04+mc44);//4 c+=4*(mc10+mc30+mc14+mc34+mc01+mc41+mc03+mc43);//16 c+=7*(mc20+mc24+mc02+mc42);//16 c+=16*(mc11+mc13+mc03+mc33);//32 c+=26*(mc21+mc23+mc12+mc32);//64 c+=41*mc22mc;//32 c/=273;
float lum = Luminance(c); c = mc22mc + c * (lum+0.1) * _Lum; return float4(c,1);
代码如下:
using UnityEngine; using System.Collections; [ExecuteInEditMode] public class HDRGlow : MonoBehaviour { #region Variables public Shader curShader; private Material curMaterial; public float exp = 0.4f; public float bm = 0.4f; public int inten = 512; public float lum = 1f; #endregion #region Properties Material material { get { if (curMaterial == null) { curMaterial = new Material(curShader); curMaterial.hideFlags = HideFlags.HideAndDontSave; } return curMaterial; } } #endregion void Start() { if (!SystemInfo.supportsImageEffects) { enabled = false; return; } if (!curShader && !curShader.isSupported) { enabled = false; } } void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture) { if (curShader != null) { material.SetFloat("_Exp", exp); material.SetFloat("_BM", bm); material.SetFloat("_Inten", inten); material.SetFloat("_Lum", lum); Graphics.Blit(sourceTexture, destTexture, material); } else { Graphics.Blit(sourceTexture, destTexture); } } void OnDisable() { if (curMaterial) { DestroyImmediate(curMaterial); } } }
与本文的差别是多了色彩平衡和lens flare效果,可以试着再加上去
------- by wolf96 http://blog.csdn.net/wolf96