给出高斯模糊的shader实现
2016-04-11 11:11
218 查看
主要是片元着色器的实现
precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
const float resolution=1024.0;
uniform float radius;//radius可为1.4
uniform vec2 dir;//若为x模糊,可传入(1.0,0.0) y模糊 (0.0,1.0)
void main() {
//this will be our RGBA sum
vec4 sum = vec4(0.0);
//our original texcoord for this fragment
vec2 tc = vTexCoord;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
//apply blurring, using a 9-tap filter with predefined gaussian weights
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
vec4 cc= texture2D(u_texture,vTexCoord );
//discard alpha for our simple demo, multiply by vertex color and return
gl_FragColor =vec4(sum.rgb, cc.a);
}
precision mediump float;
varying vec2 vTexCoord;
uniform sampler2D u_texture;
const float resolution=1024.0;
uniform float radius;//radius可为1.4
uniform vec2 dir;//若为x模糊,可传入(1.0,0.0) y模糊 (0.0,1.0)
void main() {
//this will be our RGBA sum
vec4 sum = vec4(0.0);
//our original texcoord for this fragment
vec2 tc = vTexCoord;
//the amount to blur, i.e. how far off center to sample from
//1.0 -> blur by one pixel
//2.0 -> blur by two pixels, etc.
float blur = radius/resolution;
//the direction of our blur
//(1.0, 0.0) -> x-axis blur
//(0.0, 1.0) -> y-axis blur
float hstep = dir.x;
float vstep = dir.y;
//apply blurring, using a 9-tap filter with predefined gaussian weights
sum += texture2D(u_texture, vec2(tc.x - 4.0*blur*hstep, tc.y - 4.0*blur*vstep)) * 0.0162162162;
sum += texture2D(u_texture, vec2(tc.x - 3.0*blur*hstep, tc.y - 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x - 2.0*blur*hstep, tc.y - 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x - 1.0*blur*hstep, tc.y - 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x, tc.y)) * 0.2270270270;
sum += texture2D(u_texture, vec2(tc.x + 1.0*blur*hstep, tc.y + 1.0*blur*vstep)) * 0.1945945946;
sum += texture2D(u_texture, vec2(tc.x + 2.0*blur*hstep, tc.y + 2.0*blur*vstep)) * 0.1216216216;
sum += texture2D(u_texture, vec2(tc.x + 3.0*blur*hstep, tc.y + 3.0*blur*vstep)) * 0.0540540541;
sum += texture2D(u_texture, vec2(tc.x + 4.0*blur*hstep, tc.y + 4.0*blur*vstep)) * 0.0162162162;
vec4 cc= texture2D(u_texture,vTexCoord );
//discard alpha for our simple demo, multiply by vertex color and return
gl_FragColor =vec4(sum.rgb, cc.a);
}
相关文章推荐
- Swiper使用
- struts2中<s:select>标签的使用
- MySQL数据表的操作
- 如何划分子网
- 8、数据库的备份(mongodump)和恢复(mongorestore)---> 备份具体的数据库
- React-native 环境搭建
- 深复制与浅复制
- Spring中的事务管理
- iOS图片设置圆角的三种方式
- Handler:Android异步消息处理机制完全解析
- 213. House Robber II
- MySQL数据类型
- awesome系列
- Android Studio导入 setting.jar 后报错 解决方案
- ELMAH--Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components 77 out of 90 rated th
- 移动互联网公司如何将BPM流程管理变身移动化?
- RabbitMQ 原文译04--路由
- iOS 视频播放 - YVideoPlayer - UIView
- Hibernate inverse用法(转载)
- ehcache 一二事 - ssm 中ehcashe的简单配置应用