转自:http://blog.csdn.net/xoyojank/article/details/5146297

最先在这里看到:http://www.gamerendering.com/2008/12/20/radial-blur-filter/

这效果在鬼泣4中切换场景时见过, 极品飞车12的运动模糊也有这种感觉.

原理:

确定一个中心点(如0.5, 0.5), 跟当前像素连一条线. 以当前像素为中心, 在线上的附近像素进行采样, 最后取一下平均值.

代码翻译成HLSL:

// This texture should hold the image to blur.
sampler2D Texture0; // some const, tweak for best look
const float fSampleDist;
const float fSampleStrength; // some sample positions
float samples[] =
{
-0.08,
-0.05,
-0.03,
-0.02,
-0.01,
0.01,
0.02,
0.03,
0.05,
0.08
}; float4 ps_main( float2 texCoord : TEXCOORD0 ) : COLOR
{
// 0.5,0.5 is the center of the screen
// so substracting uv from it will result in
// a vector pointing to the middle of the screen
float2 dir = 0.5 - texCoord;
// calculate the distance to the center of the screen
float dist = length(dir);
// normalize the direction (reuse the distance)
dir /= dist; // this is the original colour of this pixel
// using only this would result in a nonblurred version
float4 color = tex2D(Texture0, texCoord); float4 sum = color;
// take 10 additional blur samples in the direction towards
// the center of the screen
for (int i = ; i < ; ++i)
{
sum += tex2D(Texture0, texCoord + dir * samples[i] * fSampleDist);
} // we have taken eleven samples
sum /= 11.0; // weighten the blur effect with the distance to the
// center of the screen ( further out is blurred more)
float t = saturate(dist * fSampleStrength); //Blend the original color with the averaged pixels
return lerp(color, sum, t);
}

Unity shaderLab:

//径向模糊后处理
Shader "RadialBlur" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_fSampleDist("SampleDist", Float) = //采样距离
_fSampleStrength("SampleStrength", Float) = 2.2 //采样力度
}
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag #include "UnityCG.cginc" struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD;
}; struct v2f {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD;
}; float4 _MainTex_ST; v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
} sampler2D _MainTex;
float _fSampleDist;
float _fSampleStrength; // some sample positions
static const float samples[] =
{
-0.05,
-0.03,
-0.01,
0.01,
0.03,
0.05,
}; half4 frag (v2f i) : SV_Target
{ //0.5,0.5屏幕中心
float2 dir = float2(0.5, 0.5) - i.texcoord;//从采样中心到uv的方向向量
float2 texcoord = i.texcoord;
float dist = length(dir);
dir = normalize(dir);
float4 color = tex2D(_MainTex, texcoord); float4 sum = color;
// 6次采样
for (int i = ; i < ; ++i)
{ sum += tex2D(_MainTex, texcoord + dir * samples[i] * _fSampleDist);
} //求均值
sum /= 7.0f; //越离采样中心近的地方,越不模糊
float t = saturate(dist * _fSampleStrength); //插值
return lerp(color, sum, t); }
ENDCG
}
}
Fallback off
}

两个参数, 动态调整的话可以产生极品飞车12那种速度感(也算是第一人称运动模糊的简单实现吧).

这是RM里的效果:

(转)径向模糊效果shader的更多相关文章

  1. NeHe OpenGL教程 第三十六课:从渲染到纹理

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. 径向模糊(Radial Blur)

    [径向模糊(Radial Blur)] 径向模糊,是一种从中心向外呈幅射状的逐渐模糊的效果,在图形处理软件photoshop里面也有这个模糊滤镜.而在游戏中常常用来模拟一些动感的效果,如鬼泣4中的场景 ...

  3. 学习PHP中好玩的Gmagick图像操作扩展的使用

    在 PHP 的图像处理领域,要说最出名的 GD 库为什么好,那就是因为它不需要额外安装的别的什么图像处理工具,而且是随 PHP 源码一起发布的,只需要在安装 PHP 的时候添加上编译参数就可以了. G ...

  4. 详解Paint的setShader(Shader shader)

    一.概述 setShader(Shader shader)中传入的自然是shader对象了,shader类是Android在图形变换中非常重要的一个类.Shader在三维软件中我们称之为着色器,其作用 ...

  5. Unity Shader:Blur

    花了一晚上的时间终于看懂Image Effect中的Blur,其实很简单,就是一下子没有理解到. 原理:使用两个一维[1*7]的高斯滤波模板,一个用在x方向,另一个用在y方向.高斯滤波有模糊的效果. ...

  6. unity3d shader之God Ray上帝之光

    又是一个post-process后期效果,god ray 上帝之光,说起上帝之光就是咱们再看太阳时太阳周围一圈的针状光芒先放组效果,本文的场景资源均来自浅墨大神,效果为本文shader效果 加入了前篇 ...

  7. Cocos2d-x shader学习2: 模糊(Blur)

    模糊效果在游戏中经常会用到,有的为了突出前景会把背景给模糊化,有的是因为一些技能需要模糊效果.模糊是shader中较为简单的一种应用.cocos2dx 3.x给的demo中,就有sprite的模糊的效 ...

  8. Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(下)

    LinearGradient 线性渐变渲染器 LinearGradient中文翻译过来就是线性渐变的意思.线性渐变通俗来讲就是给起点设置一个颜色值如#faf84d,终点设置一个颜色值如#CC423C, ...

  9. Unity shader学习之屏幕后期处理效果之高斯模糊

    高斯模糊,见 百度百科. 也使用卷积来实现,每个卷积元素的公式为: 其中б是标准方差,一般取值为1. x和y分别对应当前位置到卷积中心的整数距离. 由于需要对高斯核中的权重进行归一化,即使所有权重相加 ...

随机推荐

  1. MikroTik RouterOS官方教程Wiki(入门教程)

    https://wiki.mikrotik.com/wiki/Manual:TOC 其实还有一本<ROS从入门到精通> 学习路由可以从这两个教程先入手.

  2. [Go] Http / Net 相关资料

    [astaxie] [基础]GO搭建一个简单的Web服务器 [astaxie] Go如何使得Web工作 [astaxie] Go 的 Http 包详解 [叶剑峰] Go语言_HTTP包 [叶剑峰] 使 ...

  3. 初识序列化和反序列化,使用BinaryFormatter类、ISerializable接口、XmlSerializer类进行序列化和反序列化

    序列化是将对象转换成字节流的过程,反序列化是把字节流转换成对象的过程.对象一旦被序列化,就可以把对象状态保存到硬盘的某个位置,甚至还可以通过网络发送给另外一台机器上运行的进程.本篇主要包括: ● 使用 ...

  4. 在ASP.NET MVC下扩展一个带验证的RadioButtonList

    在ASP.NET MVC4中,HtmlHelper为我们提供了Html.RadioButton()方法用来显示Radio Button单选按钮.如果想显示一组单选按钮,通常的做法是遍历一个集合把每个单 ...

  5. 在ASP.NET MVC中使用Knockout实践07,自定义验证信息的位置与内容

    在前两篇中,体验了Knockout的基本验证和自定义验证.本篇自定义验证信息的显示位置与内容. 自定义验证信息的显示位置 通常,Knockout的验证信息紧跟在input后面,通过validation ...

  6. 使用Coding4Fun工具包

    Coding4Fun是一款很受WP开发者喜爱的开源类库,对于开发者来说,Coding4Fun上手很简单.只要从CodePlex下载Coding4Fun工具包,下载完成后,解压文件到一个文件夹中,里面有 ...

  7. View Programming Guide for iOS_读书笔记

    关于Window的定义的要点 Windows do not have any visible content themselves but provide a basic container for ...

  8. Ioc模式和MEF

    IOC模式 Ioc模式(又称DI:Dependency Injection 依赖注射). 分离关注( Separation of Concerns : SOC)是Ioc模式和AOP产生最原始动力,通过 ...

  9. Android避免过度绘制之道

    如果一个布局十分复杂,那么就需要来排查是否出现了过度绘制,如果出现了,那么很可能会造成刷新率下降,造成卡顿的现象.那么什么是过度绘制呢?过度绘制就是在同一个区域中叠加了多个控件.这就像小时候我们画画, ...

  10. attrs.xml中declare-styleable 详解(用于自定义控件的属性)

    1. 框架定义: <declare-styleable name = "名称"> <attr name = "……" format = &qu ...