Debian Codegraph @

resources/shaders/decorate_shadow/ssao/ssao.frag

raw
1 uniform sampler2D rnm;
2 uniform sampler2D normalMap;
3 uniform sampler2D depthMap;
4
5 uniform mat4 proj;
6 uniform mat4 invProj;
7
8 uniform float rad;
9
10 const float totStrength = 4.0;
11 const float strength = 0.0005;
12 const float offset = 18.0;
13 const float falloff = 0.002;
14
15 #define SAMPLES 16
16
17 //restituisce la depth presa dalla texture depth map
18 float getDepth(in vec2 texCoord)
19 {
20 return texture2D(depthMap, texCoord).x;
21 }
22
23 vec3 getViewPosition(in vec2 texCoord)
24 {
25 vec4 pos = vec4(texCoord.x, texCoord.y, getDepth(texCoord), 1.0);
26 pos.xyz=(pos.xyz*2.0)-1.0;
27 pos = invProj * pos;
28 return pos.xyz/pos.w;
29 }
30
31 void main(void)
32 {
33 vec2 texCoord = gl_TexCoord[0].st;
34 //Depth del pixel corrente recuperata dalla texture di depth
35 float currentPixelDepth = getDepth(texCoord);
36
37 if(currentPixelDepth == 1.0)
38 discard;
39
40 // these are the random vectors inside a unit sphere
41 vec3 pSphere[SAMPLES];
42 pSphere[0] = vec3(0.355512, -0.709318, -0.102371);
43 pSphere[1] = vec3(0.534186, 0.71511, -0.115167);
44 pSphere[2] = vec3(-0.87866, 0.157139, -0.115167);
45 pSphere[3] = vec3(0.140679, -0.475516, -0.0639818);
46 pSphere[4] = vec3(-0.0796121, 0.158842, -0.677075);
47 pSphere[5] = vec3(-0.0759516, -0.101676, -0.483625);
48 pSphere[6] = vec3(0.12493, -0.0223423, -0.483625);
49 pSphere[7] = vec3(-0.0720074, 0.243395, -0.967251);
50 pSphere[8] = vec3(-0.207641, 0.414286, 0.187755);
51 pSphere[9] = vec3(-0.277332, -0.371262, 0.187755);
52 pSphere[10] = vec3(0.63864, -0.114214, 0.262857);
53 pSphere[11] = vec3(-0.184051, 0.622119, 0.262857);
54 pSphere[12] = vec3(0.110007, -0.219486, 0.435574);
55 pSphere[13] = vec3(0.235085, 0.314707, 0.696918);
56 pSphere[14] = vec3(-0.290012, 0.0518654, 0.522688);
57 pSphere[15] = vec3(0.0975089, -0.329594, 0.609803);
58
59
60 // grab a normal for reflecting the sample rays later on
61 vec3 fres = (normalize(texture2D(rnm , (texCoord.st * offset)).xyz)-vec3(0.5,0.5,0.5) )*2.0;
62 //vec3 fres = vec3(1.0,0.0,0.0);
63
64 //view space position del corrente frammento
65 vec3 viewPos = getViewPosition(texCoord);
66
67 // normale del frammento corrente
68 vec3 normal = normalize(texture2D(normalMap,texCoord.st).xyz*2.0-1.0);
69
70 float bl = 0.0;
71
72 vec2 modifiers = texture2D(rnm , texCoord.st).xy;
73
74 //vec3 ray, se, occNorm;
75 float sampleDepth, depthDifference;
76 vec3 sampleNormal, ray;
77 vec4 sample, sampleView, viewRayPos;
78
79 for(int i=0; i < SAMPLES; ++i)
80 {
81 // get a vector (randomized inside of a sphere with radius 1.0) from a texture and reflect it
82 ray = rad*reflect(pSphere[i],fres);
83
84 //se normale e raggio formano un angolo maggiore di 90 gradi il raggio viene flippato.
85 ray = sign(dot(ray,normal)) * ray;
86
87 //viewRayPos = invProj * vec4(ray, 1.0);
88 viewRayPos = vec4(ray, 1.0);
89
90 //coordinate del campione
91 sample = vec4(vec3(viewPos.xyz + viewRayPos.xyz), 0.0);
92
93 //riproietto il campione
94 sampleView = ( proj * sample );
95 sampleView.xyz = (sampleView.xyz / sampleView.w)*0.5 + 0.5;
96
97 //recupero una nuova normale dalla texture di noise
98 fres = normalize((texture2D(rnm, (ray.st * offset)).xyz));
99
100 //depth del campione
101 sampleDepth = getDepth(sampleView.st);
102 //normale del campione
103 sampleNormal = normalize(texture2D(normalMap,sampleView.st).xyz*2.0-1.0);
104
105 depthDifference = currentPixelDepth - sampleDepth;
106
107 // depthDifference > 0 -> sono sotto (e.g. il sample ha una z minore quindi e' piu' vicino)
108 // depthDifference < 0 -> sono sopra (e.g. il sample ha una z maggiore quindi e' piu' lontano)
109 // depthDifference << 0 -> sono molto sopra (e.g. il sample ha una z molto maggiore quindi e' molto piu' lontano)
110
111 float lightContrib = float(depthDifference<0.001); // se non e' proprio molto sotto lo considero in piena luce
112
113 // se era un po' sotto ma (non tanto), non sarebbe illuminato e allora ci metto un po' di luce con un test delle normali.
114 if(lightContrib<0.5 && depthDifference>-0.1) lightContrib = dot(normal, sampleNormal);
115
116 bl += lightContrib;
117 }
118 // remember: ao is skylight
119 float ao = bl/float(SAMPLES);
120 ao = ao*ao;
121
122 gl_FragColor = vec4(vec3(ao), 1.0);
123 }