2012-12-04 03:05:34 +00:00
|
|
|
uniform sampler2D u_ScreenImageMap;
|
|
|
|
uniform sampler2D u_ScreenDepthMap;
|
|
|
|
|
2016-03-07 10:27:03 +00:00
|
|
|
uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height
|
2012-12-04 03:05:34 +00:00
|
|
|
varying vec2 var_ScreenTex;
|
|
|
|
|
2016-03-10 11:44:21 +00:00
|
|
|
//float gauss[8] = float[8](0.17, 0.17, 0.16, 0.14, 0.12, 0.1, 0.08, 0.06);
|
2012-12-04 03:05:34 +00:00
|
|
|
//float gauss[5] = float[5](0.30, 0.23, 0.097, 0.024, 0.0033);
|
|
|
|
float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044);
|
|
|
|
//float gauss[3] = float[3](0.60, 0.19, 0.0066);
|
2016-03-07 10:27:03 +00:00
|
|
|
#define BLUR_SIZE 4
|
2016-03-11 12:37:50 +00:00
|
|
|
//#define USE_GAUSS
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear)
|
|
|
|
{
|
2016-03-11 12:37:50 +00:00
|
|
|
float sampleZDivW = texture2D(depthMap, tex).r;
|
2016-03-09 02:30:51 +00:00
|
|
|
return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW);
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 02:30:51 +00:00
|
|
|
vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar, vec2 scale)
|
2012-12-04 03:05:34 +00:00
|
|
|
{
|
2016-03-09 02:30:51 +00:00
|
|
|
float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear);
|
2016-03-10 11:44:21 +00:00
|
|
|
|
|
|
|
// enable for less blurring for farther objects
|
2016-03-11 12:37:50 +00:00
|
|
|
scale /= clamp(zFarDivZNear * depthCenter / 32.0, 1.0, 2.0);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
#if defined(USE_HORIZONTAL_BLUR)
|
2016-03-11 12:37:50 +00:00
|
|
|
vec2 direction = vec2(scale.x * 2.0, 0.0);
|
|
|
|
vec2 nudge = vec2(0.0, scale.y * 0.5);
|
2012-12-04 03:05:34 +00:00
|
|
|
#else // if defined(USE_VERTICAL_BLUR)
|
2016-03-11 12:37:50 +00:00
|
|
|
vec2 direction = vec2(0.0, scale.y * 2.0);
|
|
|
|
vec2 nudge = vec2(scale.x * 0.5, 0.0);
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
2016-03-09 02:30:51 +00:00
|
|
|
|
|
|
|
vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y));
|
2016-03-07 10:27:03 +00:00
|
|
|
|
|
|
|
#if defined(USE_GAUSS)
|
2012-12-04 03:05:34 +00:00
|
|
|
vec4 result = texture2D(imageMap, tex) * gauss[0];
|
|
|
|
float total = gauss[0];
|
2016-03-07 10:27:03 +00:00
|
|
|
#else
|
|
|
|
vec4 result = texture2D(imageMap, tex);
|
|
|
|
float total = 1.0;
|
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2016-03-09 02:30:51 +00:00
|
|
|
float zLimit = 5.0 / zFar;
|
2012-12-04 03:05:34 +00:00
|
|
|
int i, j;
|
|
|
|
for (i = 0; i < 2; i++)
|
|
|
|
{
|
2016-03-07 10:27:03 +00:00
|
|
|
for (j = 1; j < BLUR_SIZE; j++)
|
2012-12-04 03:05:34 +00:00
|
|
|
{
|
2016-03-11 12:37:50 +00:00
|
|
|
vec2 offset = direction * (float(j) - 0.25) + nudge;
|
2016-03-09 02:30:51 +00:00
|
|
|
float depthSample = getLinearDepth(depthMap, tex + offset, zFarDivZNear);
|
|
|
|
float depthExpected = depthCenter + dot(slope, offset);
|
|
|
|
float useSample = float(abs(depthSample - depthExpected) < zLimit);
|
2016-03-07 10:27:03 +00:00
|
|
|
#if defined(USE_GAUSS)
|
2016-03-09 02:30:51 +00:00
|
|
|
result += texture2D(imageMap, tex + offset) * (gauss[j] * useSample);
|
|
|
|
total += gauss[j] * useSample;
|
2016-03-07 10:27:03 +00:00
|
|
|
#else
|
2016-03-09 02:30:51 +00:00
|
|
|
result += texture2D(imageMap, tex + offset) * useSample;
|
|
|
|
total += useSample;
|
2016-03-07 10:27:03 +00:00
|
|
|
#endif
|
2016-03-11 12:37:50 +00:00
|
|
|
nudge = -nudge;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
2016-03-09 02:30:51 +00:00
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
direction = -direction;
|
2016-03-11 12:37:50 +00:00
|
|
|
nudge = -nudge;
|
2016-03-09 02:30:51 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
return result / total;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
2016-03-09 02:30:51 +00:00
|
|
|
{
|
2016-03-11 12:37:50 +00:00
|
|
|
gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw);
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|