| 1 |
uniform sampler2D u_ScreenImageMap; |
| 2 |
uniform sampler2D u_ScreenDepthMap; |
| 3 |
|
| 4 |
uniform vec4 u_ViewInfo; // zfar / znear, zfar, 1/width, 1/height |
| 5 |
varying vec2 var_ScreenTex; |
| 6 |
|
| 7 |
//float gauss[8] = float[8](0.17, 0.17, 0.16, 0.14, 0.12, 0.1, 0.08, 0.06); |
| 8 |
//float gauss[5] = float[5](0.30, 0.23, 0.097, 0.024, 0.0033); |
| 9 |
//float gauss[4] = float[4](0.40, 0.24, 0.054, 0.0044); |
| 10 |
//float gauss[3] = float[3](0.60, 0.19, 0.0066); |
| 11 |
#define BLUR_SIZE 4 |
| 12 |
|
| 13 |
#if !defined(USE_DEPTH) |
| 14 |
//#define USE_GAUSS |
| 15 |
#endif |
| 16 |
|
| 17 |
float getLinearDepth(sampler2D depthMap, const vec2 tex, const float zFarDivZNear) |
| 18 |
{ |
| 19 |
float sampleZDivW = texture2D(depthMap, tex).r; |
| 20 |
return 1.0 / mix(zFarDivZNear, 1.0, sampleZDivW); |
| 21 |
} |
| 22 |
|
| 23 |
vec4 depthGaussian1D(sampler2D imageMap, sampler2D depthMap, vec2 tex, float zFarDivZNear, float zFar, vec2 scale) |
| 24 |
{ |
| 25 |
float gauss[4]; |
| 26 |
|
| 27 |
gauss[0] = 0.40; |
| 28 |
gauss[1] = 0.24; |
| 29 |
gauss[2] = 0.054; |
| 30 |
gauss[3] = 0.0044; |
| 31 |
|
| 32 |
#if defined(USE_DEPTH) |
| 33 |
float depthCenter = getLinearDepth(depthMap, tex, zFarDivZNear); |
| 34 |
vec2 slope = vec2(dFdx(depthCenter), dFdy(depthCenter)) / vec2(dFdx(tex.x), dFdy(tex.y)); |
| 35 |
scale /= clamp(zFarDivZNear * depthCenter / 32.0, 1.0, 2.0); |
| 36 |
#endif |
| 37 |
|
| 38 |
#if defined(USE_HORIZONTAL_BLUR) |
| 39 |
vec2 direction = vec2(scale.x * 2.0, 0.0); |
| 40 |
vec2 nudge = vec2(0.0, scale.y * 0.5); |
| 41 |
#else // if defined(USE_VERTICAL_BLUR) |
| 42 |
vec2 direction = vec2(0.0, scale.y * 2.0); |
| 43 |
vec2 nudge = vec2(-scale.x * 0.5, 0.0); |
| 44 |
#endif |
| 45 |
|
| 46 |
#if defined(USE_GAUSS) |
| 47 |
vec4 result = texture2D(imageMap, tex) * gauss[0]; |
| 48 |
float total = gauss[0]; |
| 49 |
#else |
| 50 |
vec4 result = texture2D(imageMap, tex); |
| 51 |
float total = 1.0; |
| 52 |
#endif |
| 53 |
|
| 54 |
float zLimit = 5.0 / zFar; |
| 55 |
for (int i = 0; i < 2; i++) |
| 56 |
{ |
| 57 |
for (int j = 1; j < BLUR_SIZE; j++) |
| 58 |
{ |
| 59 |
vec2 offset = direction * (float(j) - 0.25) + nudge; |
| 60 |
#if defined(USE_DEPTH) |
| 61 |
float depthSample = getLinearDepth(depthMap, tex + offset, zFarDivZNear); |
| 62 |
float depthExpected = depthCenter + dot(slope, offset); |
| 63 |
float useSample = float(abs(depthSample - depthExpected) < zLimit); |
| 64 |
#else |
| 65 |
float useSample = 1.0; |
| 66 |
#endif |
| 67 |
#if defined(USE_GAUSS) |
| 68 |
result += texture2D(imageMap, tex + offset) * (gauss[j] * useSample); |
| 69 |
total += gauss[j] * useSample; |
| 70 |
#else |
| 71 |
result += texture2D(imageMap, tex + offset) * useSample; |
| 72 |
total += useSample; |
| 73 |
#endif |
| 74 |
nudge = -nudge; |
| 75 |
} |
| 76 |
|
| 77 |
direction = -direction; |
| 78 |
nudge = -nudge; |
| 79 |
} |
| 80 |
|
| 81 |
return result / total; |
| 82 |
} |
| 83 |
|
| 84 |
void main() |
| 85 |
{ |
| 86 |
gl_FragColor = depthGaussian1D(u_ScreenImageMap, u_ScreenDepthMap, var_ScreenTex, u_ViewInfo.x, u_ViewInfo.y, u_ViewInfo.zw); |
| 87 |
} |