nametag rendering
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / depth_compare.glsl
diff --git a/shaders/depth_compare.glsl b/shaders/depth_compare.glsl
new file mode 100644 (file)
index 0000000..5e02144
--- /dev/null
@@ -0,0 +1,28 @@
+uniform sampler2D uTexSceneDepth;
+uniform vec3 uInverseRatioDepth;
+uniform vec3 uInverseRatioMain;
+uniform bool uDepthCompare;
+
+float linear_depth( float depth, float near, float far ) {
+   float z = depth * 2.0 - 1.0;
+   return (2.0 * near * far) / (far + near - z * (far - near));        
+}
+
+void depth_compare_dither(){
+   if( uDepthCompare ){
+      vec2 back_coord = gl_FragCoord.xy * uInverseRatioMain.xy 
+                                        * uInverseRatioDepth.xy;
+      float back_depth = texture( uTexSceneDepth, back_coord ).r;
+      float front_depth = gl_FragCoord.z/gl_FragCoord.w;
+
+      back_depth = linear_depth( back_depth, 0.1, 2100.0 );
+      float diff = back_depth - front_depth;
+
+      vec2 ssuv = gl_FragCoord.xy;
+      vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );
+      float dither = fract( vDither.g / 71.0 ) - 0.5;
+
+      if( step(0.0,diff)+dither<0.3 )
+         discard;
+   }
+}