Debian Codegraph @

src/scene/glsl/source/ssao.glsl

raw
1 ivec2 screen_position();
2 float screen_get_depth_fast(ivec2 position);
3 vec4 screen_get_color(ivec2 position);
4
5 // shader from http://www.pasteall.org/12282, http://www.youtube.com/watch?v=R_L-_oGTbqw
6
7 uniform float near; //Z-near
8 uniform float far; //Z-far
9
10 #define PI 3.14159265
11
12 //samples on the first ring (was 8)
13 #define SSAO_SAMPLES 6
14
15 //ring count (was 6)
16 #define SSAO_RINGS 3
17
18 vec2 rand(in vec2 coord) //generating random noise
19 {
20 float noiseX = (fract(sin(dot(coord ,vec2(12.9898,78.233))) * 43758.5453));
21 float noiseY = (fract(sin(dot(coord ,vec2(12.9898,78.233)*2.0)) * 43758.5453));
22 return vec2(noiseX,noiseY)*0.004;
23 }
24
25 float readDepth(const in ivec2 coord)
26 {
27 return (2.0 * near) / (far + near - screen_get_depth_fast(coord) * (far-near));
28 }
29
30 float compareDepths( in float depth1, in float depth2 )
31 {
32 const float aoCap = 0.8;//1.0;
33 const float aoMultiplier = 100.0;
34 const float depthTolerance = 0.0001;
35 const float aorange = 60.0;// units in space the AO effect extends to (this gets divided by the camera far range
36 float diff = sqrt(clamp(1.0-(depth1-depth2) / (aorange/(far-near)),0.0,1.0));
37 float ao = min(aoCap,max(0.0,depth1-depth2-depthTolerance) * aoMultiplier) * diff;
38 return ao;
39 }
40
41 void main(void)
42 {
43 ivec2 current_pos = screen_position();
44 float depth = readDepth(current_pos);
45 float d;
46
47 float width = float(screen_width);
48 float height = float(screen_height);
49
50 float aspect = width/height;
51 vec2 noise = rand(vec2(current_pos));
52
53 float w = (1.0 / width)/clamp(depth,0.05,1.0)+(noise.x*(1.0-noise.x));
54 float h = (1.0 / height)/clamp(depth,0.05,1.0)+(noise.y*(1.0-noise.y));
55
56 w *= width/2.0; h *= height/2.0; // JA added this line !!
57
58 float pw;
59 float ph;
60
61 float ao = 0.0;
62 float s = 0.0;
63 float fade = 1.0;
64
65 for (int i = 0 ; i < SSAO_RINGS; i += 1)
66 {
67 fade *= 0.5;
68 for (int j = 0 ; j < SSAO_SAMPLES*i; j += 1)
69 {
70 float step = PI*2.0 / float(SSAO_SAMPLES*i);
71 pw = (cos(float(j)*step)*float(i));
72 ph = (sin(float(j)*step)*float(i));
73 d = readDepth(current_pos + ivec2(int(pw * w), int(ph * h)));
74 ao += compareDepths(depth,d)*fade;
75 s += 1.0*fade;
76 }
77 }
78
79 ao /= s;
80 ao = 1.0-ao;
81
82 vec3 color = screen_get_color(current_pos).rgb;
83 //vec3 luminance = texture2D(bgl_LuminanceTexture,vec2(current_pos)).rgb;
84 //luminance = clamp(max(0.0,luminance-0.2)+max(0.0,luminance-0.2)+max(0.0,luminance-0.2),0.0,1.0);
85 //gl_FragColor = vec4(color*mix(vec3(ao),vec3(1.0),luminance),1.0);
86
87 ao = ao * 0.8 + 0.2;
88 gl_FragColor = vec4(color*vec3(ao),1.0);
89 //gl_FragColor = vec4(vec3(ao), 1.0); // ssao only
90 }