赞
踩
十种模糊效果,以后处理方式实现
一,高斯模糊(Gaussian Blur)
片源代码
- float4 FragGaussianBlur(v2f i): SV_Target
- {
- half4 color = float4(0, 0, 0, 0);
-
- color += 0.40 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
- color += 0.15 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.xy);
- color += 0.15 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.zw);
- color += 0.10 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.xy);
- color += 0.10 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.zw);
- color += 0.05 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv45.xy);
- color += 0.05 * SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv45.zw);
-
- return color;
- }
Gaussian Blur Off
Gaussian Blur On
二,方框模糊(Box Blur)
片源代码
- half4 BoxFilter_4Tap(TEXTURE2D_ARGS(tex, samplerTex), float2 uv, float2 texelSize)
- {
- float4 d = texelSize.xyxy * float4(-1.0, -1.0, 1.0, 1.0);
-
- half4 s = 0;
- s = SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.xy) * 0.25h; // 1 MUL
- s += SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.zy) * 0.25h; // 1 MAD
- s += SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.xw) * 0.25h; // 1 MAD
- s += SAMPLE_TEXTURE2D(tex, samplerTex, uv + d.zw) * 0.25h; // 1 MAD
-
- return s;
- }
Box Blur Off
Box Blur On
三,Kawase模糊(Kawase Blur)
片源代码
- half4 KawaseBlur(TEXTURE2D_ARGS(tex, samplerTex), float2 uv, float2 texelSize, half pixelOffset)
- {
- half4 o = 0;
- o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(pixelOffset +0.5, pixelOffset +0.5) * texelSize);
- o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(-pixelOffset -0.5, pixelOffset +0.5) * texelSize);
- o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(-pixelOffset -0.5, -pixelOffset -0.5) * texelSize);
- o += SAMPLE_TEXTURE2D(tex, samplerTex, uv + float2(pixelOffset +0.5, -pixelOffset -0.5) * texelSize);
- return o * 0.25;
- }
Kawase Blur Off
Kawase Blur On
四、双重模糊(Dual Blur)
片源代码
- half4 Frag_DownSample(v2f_DownSample i): SV_Target
- {
- half4 sum = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv) * 4;
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.zw);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.zw);
-
- return sum * 0.125;
- }
-
- half4 Frag_UpSample(v2f_UpSample i): SV_Target
- {
- half4 sum = 0;
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv01.zw) * 2;
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv23.zw) * 2;
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv45.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv45.zw) * 2;
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv67.xy);
- sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv67.zw) * 2;
-
- return sum * 0.0833;
- }
Dual Kawase Blur Off
Dual Kawase Blur On
五、散景模糊(Bokeh Blur)
片源代码
- half4 BokehBlur(VaryingsDefault i)
- {
- half2x2 rot = half2x2(_GoldenRot);
- half4 accumulator = 0.0;
- half4 divisor = 0.0;
-
- half r = 1.0;
- half2 angle = half2(0.0, _Radius);
-
- for (int j = 0; j < _Iteration; j++)
- {
- r += 1.0 / r;
- angle = mul(rot, angle);
- half4 bokeh = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, float2(i.texcoord + _PixelSize * (r - 1.0) * angle));
- accumulator += bokeh * bokeh;
- divisor += bokeh;
- }
- return accumulator / divisor;
- }
Bokeh Blur Off
Bokeh Blur On
六、移轴模糊 (Tilt Shift Blur)
片源代码
- float TiltShiftMask(float2 uv)
- {
- float centerY = uv.y * 2.0 - 1.0 + _Offset; // [0,1] -> [-1,1]
- return pow(abs(centerY * _Area), _Spread);
- }
Tilt Shift Blur Off
Tilt Shift Blur On
七、光圈模糊(Iris Blur)
片源代码
- float IrisMask(float2 uv)
- {
- float2 center = uv * 2.0 - 1.0 + _Offset; // [0,1] -> [-1,1]
- return dot(center, center) * _AreaSize;
- }
Iris Blur Off
Iris Blur On
八、粒状模糊(Grainy Blur)
片源代码
- float Rand(float2 n)
- {
- return sin(dot(n, half2(1233.224, 1743.335)));
- }
-
- half4 GrainyBlur(VaryingsDefault i)
- {
- half2 randomOffset = float2(0.0, 0.0);
- half4 finalColor = half4(0.0, 0.0, 0.0, 0.0);
- float random = Rand(i.texcoord);
-
- for (int k = 0; k < int(_Iteration); k ++)
- {
- random = frac(43758.5453 * random + 0.61432);;
- randomOffset.x = (random - 0.5) * 2.0;
- random = frac(43758.5453 * random + 0.61432);
- randomOffset.y = (random - 0.5) * 2.0;
-
- finalColor += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, half2(i.texcoord + randomOffset * _BlurRadius));
- }
- return finalColor / _Iteration;
- }
Grainy Blur Off
Grainy Blur On
九、径向模糊(Radial Blur)
片源代码
- half4 RadialBlur(VaryingsDefault i)
- {
- float2 blurVector = (_RadialCenter - i.texcoord.xy) * _BlurRadius;
-
- half4 acumulateColor = half4(0, 0, 0, 0);
-
- [unroll(30)]
- for (int j = 0; j < _Iteration; j ++)
- {
- acumulateColor += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord);
- i.texcoord.xy += blurVector;
- }
-
- return acumulateColor / _Iteration;
- }
Radial Blur Off
Radial Blur On
十、方向模糊(Directional Blur)
片源代码
- half4 DirectionalBlur(VaryingsDefault i)
- {
- half4 color = half4(0.0, 0.0, 0.0, 0.0);
-
- for (int k = -_Iteration; k < _Iteration; k++)
- {
- color += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord - _Direction * k);
- }
- half4 finalColor = color / (_Iteration * 2.0);
-
- return finalColor;
- }
Directional Blur Off
Directional Blur On
性能对比
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。