#version 410 uniform sampler2D texture1; in vec2 uvV; out vec4 fragColor; uniform float opacity = 1.0; uniform float zNear = 0.1; uniform float zFar = 500.0; uniform vec2 meScreenSize; // capture the depth from texture1.x... // see: http://stackoverflow.com/questions/6652253/getting-the-true-z-value-from-the-depth-buffer for some helpful math /* // == Post-process frag shader =========================================== uniform sampler2D depthBuffTex; uniform float zNear; uniform float zFar; varying vec2 vTexCoord; void main(void) { float z_b = texture2D(depthBuffTex, vTexCoord).x; float z_n = 2.0 * z_b - 1.0; float z_e = 2.0 * zNear * zFar / (zFar + zNear - z_n * (zFar - zNear)); } */ float depthAt(vec2 pixelCoord) { float depthSample = texture(texture1, pixelCoord / meScreenSize).r; float z_n = 2.0 * depthSample - 1.0; float z_e = 2.0 * zFar * zNear / (zFar + zNear - z_n * (zFar - zNear)); return z_e; } void main (void) { // vec2 co = gl_FragCoord.xy / vec2(meScreenSize.x, meScreenSize.y); // // float n = 0.1; // float f = 500.0; // // float depthSample = texture(texture1, uvV).r; // // float z_n = 2.0 * depthSample - 1.0; // // float z_e = 2 * n * f / (f + n - z_n * (f - n)); float z00 = depthAt(gl_FragCoord.xy); float z0p = depthAt(gl_FragCoord.xy + vec2(0,1)); float z0n = depthAt(gl_FragCoord.xy + vec2(0,-1)); float zp0 = depthAt(gl_FragCoord.xy + vec2(1,0)); float zn0 = depthAt(gl_FragCoord.xy + vec2(-1,0)); float ang0p = abs(z00 - z0p) > 0.5 ? 0 : 1; float ang0n = abs(z00 - z0p) > 0.5 ? 0 : 1; float angp0 = abs(z00 - zp0) > 0.5 ? 0 : 1; float angn0 = abs(z00 - zn0) > 0.5 ? 0 : 1; float g = (ang0n != ang0p) || (angn0 != angp0) ? 0 : 1; z0p -= z00; z0n -= z00; zp0 -= z00; zn0 -= z00; const float thresh = 0.001; bool enuf01 = abs(z0p) > thresh || abs(z0n) > thresh; bool enuf10 = abs(zp0) > thresh || abs(zn0) > thresh; bool sgn0p = z0p > 0; bool sgn0n = z0n > 0; bool sgnp0 = zp0 > 0; bool sgnn0 = zn0 > 0; g *= (enuf01 && sgn0p == sgn0n) || (enuf10 && sgnp0 == sgnn0) ? 0 : 1; // float del = max(abs(z00 - z01), abs(z00 - z10)); // float g = del > 0.5 ? 0 : 1; // float z_e = depthAt(gl_FragCoord.xy); // z_e -= depthAt(gl_FragCoord.xy + vec2(1,1)); // float g = z_e / 100; g *= z00 / 75; fragColor = texture(texture1, uvV); fragColor = vec4(fragColor.rrr, opacity); fragColor = vec4(g,g,g,1); }