uniform sampler2D color_texture;
uniform sampler2D depth_texture;
varying vec2 vTexCoord;
const float maxdiff = 0.0001;
varying vec2 blurCoords[4];
void main(void)
{
vec4 center = texture2D(color_texture, vTexCoord);
float centerdepth = texture2D(depth_texture, vTexCoord).r;
vec4 sum = center * 0.2270270270;
float weightSum = 0.2270270270;
vec4 sample = texture2D(color_texture, blurCoords[0]);
float depth = texture2D(depth_texture, blurCoords[0]).r;
float closeness = step(abs(centerdepth - depth), maxdiff);
float weight = 0.0702702703 * closeness;
sum += sample * weight;
weightSum += weight;
sample = texture2D(color_texture, blurCoords[1]);
depth = texture2D(depth_texture, blurCoords[1]).r;
closeness = step(abs(centerdepth - depth), maxdiff);
weight = 0.3162162162 * closeness;
sum += sample * weight;
weightSum += weight;
sample = texture2D(color_texture, blurCoords[2]);
depth = texture2D(depth_texture, blurCoords[2]).r;
closeness = step(abs(centerdepth - depth), maxdiff);
weight = 0.3162162162 * closeness;
sum += sample * weight;
weightSum += weight;
sample = texture2D(color_texture, blurCoords[3]);
depth = texture2D(depth_texture, blurCoords[3]).r;
closeness = step(abs(centerdepth - depth), maxdiff);
weight = 0.0702702703 * closeness;
sum += sample * weight;
weightSum += weight;
gl_FragColor = vec4(sum.rgb, 1.0) / weightSum;
}
Attached is a screenshot of my shader in action using the common Sibenik cathedral test model. As you can see the edges are quite pronounced on the players weapon and on columns close to the camera but become blurry as the distance increases.