(转)径向模糊效果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分别对应当前位置到卷积中心的整数距离. 由于需要对高斯核中的权重进行归一化,即使所有权重相加 ...
随机推荐
- 针对MyISAM锁表的解决方案
最近服务器上经常出现mysql进程占CPU100%的情况,使用show processlist命令后,看到出现了很多状态为LOCKED的sql.使用show status like 'table%'检 ...
- 群晖NAS百度云Docker客户端下载目录没有权限的问题解决
针对这篇文章:https://zhuanlan.zhihu.com/p/42267779的问题,需要ssh进去群晖,然后把目录设置成777权限.命令如下: sudo chmod -R 777 /vol ...
- HDU 4453 Looploop (伸展树splay tree)
Looploop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- STM32 USB FS Core and USB OTG Core
STM32 USB-FS-Device development kit Compatible with the STM32F102xx and STM32F103xx series, STM32 L1 ...
- Execution Plan 执行计划介绍
后面的练习中需要下载 Demo 数据库, 有很多不同的版本, 可以根据个人需要下载. 下载地址 -http://msftdbprodsamples.codeplex.com/ 1. 什么是执行计划 ...
- android adb命令 unable to connect to 192.168.1.155:5555
如果使用有线网络无法用adb connect命令连接设备的话,可以选择使用无线wifi来连接. 首先在android设备上装一个叫做Adb Wireless的软件,打开wifi,然后打开adb wir ...
- Linux isql
登录Linux环境,执行 isql -s 命令,进入isql命令页面 进入后页面如下: 选择Query-Language回车,选项可进行数据库操作 进入后,选择要操作的数据库,回车 进行入数据库后的数 ...
- WordPress主题开发:循环代码
have_posts() 有没有文章信息 if...else <?php if( have_posts() ) : while( have_posts() ) : the_post(); ?&g ...
- 使用系统自带的 UIRefreshControl 实现下拉刷新
UIRefreshControl 为 UITableViewController 中的一个属性,从以下可以看出, IOS6.0 以上才支持. @property (nonatomic,retain) ...
- 邪恶力量第一至九季/全集Supernatural迅雷下载
邪恶力量 第一季 Supernatural Season 1 (2005) 本季看点:一部充满吸引力的系列剧,超自然现象题材中的亲情与正义.迪恩(简森·阿克斯 Jensen Ackles 饰)和萨姆( ...