(转)径向模糊效果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分别对应当前位置到卷积中心的整数距离. 由于需要对高斯核中的权重进行归一化,即使所有权重相加 ...
随机推荐
- Linux下Shell函数返回值实现种类
shell在执行的时候是顺序执行的,也不存在什么多线程什么的. 一下是实现种类: 1.全局 g_result="" function testFunc() { g_result=' ...
- memcached对key和value的限制 memcached的key最大长度和Value最大长度
memcached的简单限制就是键(key)和item的限制.最大键长为250个字符.可以接受的储存数据不能超过1MB,因为这是典型slab 的最大值.这里我们可以突破对key长度的限制.问题解决:修 ...
- oracle 树型结构数据的查询
Oracle中start by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename start with 条件1 con ...
- 用Docker下搭建GitLab
最近试了一下Docker,发现用它搭建服务十分方便,就用它搭建了一个gitlab练练手. 首先下载gitlab镜像: docker image pull gitlab/gitlab-c ...
- HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- VGA Output from STM32F4 Discovery board
VGA Output from STM32F4 Discovery board I love the web! There are so many cool projects out there, a ...
- STM32F4, USB HS with ULPI and Suspend/Wakeup
Hi guys,I am in need of your help, unfortunately STs documentation is lacking some information here. ...
- AngularJS一个由于未声明对象而报的错
实现这样的一个需求:点击某个按钮,然后显示或隐藏某块区域. 先注册一个AngularJS的一个module: var myApp = angular.module("myApp", ...
- npm WARN saveError ENOENT: no such file or directory
转自树之名原文npm WARN saveError ENOENT: no such file or directory解决 我是在安装sequelize时出错的.提示的错误没有保存,类似于参考的文章中 ...
- How to update jQuery Mobile in Dreamweaver CS6
来源:http://wpguru.co.uk/2013/01/how-to-update-jquery-mobile-in-dreamweaver-cs6/ Since the release of ...