npcs and tutorial stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / shaders / depth_compare.glsl
1 uniform sampler2D uTexSceneDepth;
2 uniform vec3 uInverseRatioDepth;
3 uniform vec3 uInverseRatioMain;
4 uniform int uDepthMode;
5 uniform float uDitherCutoff;
6
7 float linear_depth( float depth, float near, float far ) {
8 float z = depth * 2.0 - 1.0;
9 return (2.0 * near * far) / (far + near - z * (far - near));
10 }
11
12 void depth_compare_dither()
13 {
14 if( uDepthMode == 1 )
15 {
16 vec2 back_coord = gl_FragCoord.xy * uInverseRatioMain.xy
17 * uInverseRatioDepth.xy;
18 float back_depth = texture( uTexSceneDepth, back_coord ).r;
19 float front_depth = gl_FragCoord.z/gl_FragCoord.w;
20
21 back_depth = linear_depth( back_depth, 0.1, 2100.0 );
22 float diff = back_depth - front_depth;
23
24 vec2 ssuv = gl_FragCoord.xy;
25 vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );
26 float dither = fract( vDither.g / 71.0 ) - 0.5;
27
28 if( step(0.0,diff)+dither<0.3 )
29 discard;
30 }
31
32 if( uDepthMode == 2 )
33 {
34 vec2 ssuv = gl_FragCoord.xy;
35 vec3 vDither = vec3( dot( vec2( 171.0, 231.0 ), ssuv) );
36 float dither = fract( vDither.g / 71.0 );
37 if( dither<uDitherCutoff ) discard;
38 }
39 }