(转)径向模糊效果shader
转自: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的更多相关文章
- NeHe OpenGL教程 第三十六课:从渲染到纹理
转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...
- 径向模糊(Radial Blur)
[径向模糊(Radial Blur)] 径向模糊,是一种从中心向外呈幅射状的逐渐模糊的效果,在图形处理软件photoshop里面也有这个模糊滤镜.而在游戏中常常用来模拟一些动感的效果,如鬼泣4中的场景 ...
- 学习PHP中好玩的Gmagick图像操作扩展的使用
在 PHP 的图像处理领域,要说最出名的 GD 库为什么好,那就是因为它不需要额外安装的别的什么图像处理工具,而且是随 PHP 源码一起发布的,只需要在安装 PHP 的时候添加上编译参数就可以了. G ...
- 详解Paint的setShader(Shader shader)
一.概述 setShader(Shader shader)中传入的自然是shader对象了,shader类是Android在图形变换中非常重要的一个类.Shader在三维软件中我们称之为着色器,其作用 ...
- Unity Shader:Blur
花了一晚上的时间终于看懂Image Effect中的Blur,其实很简单,就是一下子没有理解到. 原理:使用两个一维[1*7]的高斯滤波模板,一个用在x方向,另一个用在y方向.高斯滤波有模糊的效果. ...
- unity3d shader之God Ray上帝之光
又是一个post-process后期效果,god ray 上帝之光,说起上帝之光就是咱们再看太阳时太阳周围一圈的针状光芒先放组效果,本文的场景资源均来自浅墨大神,效果为本文shader效果 加入了前篇 ...
- Cocos2d-x shader学习2: 模糊(Blur)
模糊效果在游戏中经常会用到,有的为了突出前景会把背景给模糊化,有的是因为一些技能需要模糊效果.模糊是shader中较为简单的一种应用.cocos2dx 3.x给的demo中,就有sprite的模糊的效 ...
- Android为TV端助力 转载:Android绘图Canvas十八般武器之Shader详解及实战篇(下)
LinearGradient 线性渐变渲染器 LinearGradient中文翻译过来就是线性渐变的意思.线性渐变通俗来讲就是给起点设置一个颜色值如#faf84d,终点设置一个颜色值如#CC423C, ...
- Unity shader学习之屏幕后期处理效果之高斯模糊
高斯模糊,见 百度百科. 也使用卷积来实现,每个卷积元素的公式为: 其中б是标准方差,一般取值为1. x和y分别对应当前位置到卷积中心的整数距离. 由于需要对高斯核中的权重进行归一化,即使所有权重相加 ...
随机推荐
- MikroTik RouterOS 5.x使用HunterTik 2.3.1进行破解
一.加载光驱: 二.一路回车: 三.说明: 1.可以不安装Debian内核,但如果在无缝升级到6.6的版本,此项就一定要选择. 2.6版本的破解必须小于等于1G的空间,不然无法破解成功,亲测有效,如果 ...
- 在SpringMVC中使用@RequestBody注解处理json时,报出HTTP Status 415的解决方案
Spring的@RequestBody非常牛x,可以将提交的json直接转换成POJO对象. 正好今天有这样的需求,使用一下,结果一直报415,十分头疼. HTTP 415 错误 – 不支持的媒体类型 ...
- USBDM RS08/HCS08/HCS12/Coldfire V1,2,3,4/DSC/Kinetis Debugger and Programmer -- BDM Construction and Firmware
Construction. Build the hardware using the information provided in the PCB download. The following a ...
- win10企业版永久激活2017怎么用
Win10正式版永久激活用命令和密钥即可工具原料:电脑+win10win10企业版永久激活方法如下:1."WIN+R"打开运行对话框,输入命令slmgr.vbs -xpr,点击确定 ...
- On premise TFS Git OAuth clone failed on agent
利用TFS2015的跨平台生成代理,在mac osx 上生成xamarin.ios项目,agent费劲九牛二虎之力搞定了(参见http://www.cnblogs.com/zjoch/p/581101 ...
- C#中一种替换switch语句更优雅的写法
今天在项目中遇到了使用switch语句判断条件,但问题是条件比较多,大概有几十个条件,满屏幕的case判断,是否有更优雅的写法替代switch语句呢? 假设有这样的一个场景:商场经常会根据情况采取不同 ...
- CListCtrlEx:一个支持文件拖放和实时监视的列表控件——用未公开API函数实现Shell实时监视
一.需求无论何时,当你在Explorer窗口中创建.删除或重命名一个文件夹/文件,或者插入拔除移动存储器时,Windows总是能非常快速地更新它所有的视图.有时候我们的程序中也需要这样的功能,以便当用 ...
- 苹果无法连接到itunes store怎么办
方法1:设置--还原--还原网络设置,再进app store就可以了.方法2:重置访问限制“设置”–> “通用” –> “访问限制”,开启访问限制5秒,然后再关闭访问限制.方法3:重置当前 ...
- 识骨寻踪第十二季/全集Bones迅雷下载
本季 Bones (2015)看点:<识骨寻踪>(FOX)2005年推出的罪案题材的电视连续剧.该剧部分内容改编自前刑侦检验官.现任该剧制作人凯丝·莱克斯出版的一系列侦探小说.Bones的 ...
- mac下配置android开发环境
从昨天下午到如今.用了差点儿相同一整天的时间去熟悉mac和配置环境,基本已经OK了,大体记录了整个过程中遇到的问题. 1.安装java环境 之前听说mac自带java环境.java -version一 ...