/
ssao.shader
176 lines (148 loc) · 3.5 KB
/
ssao.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
[[FX]]
// Samplers
sampler2D gbuf0 = sampler_state
{
Address = Clamp;
};
sampler2D gbuf1 = sampler_state
{
Address = Clamp;
};
sampler2D gbuf2 = sampler_state
{
Address = Clamp;
};
sampler2D gbuf3 = sampler_state
{
Address = Clamp;
};
sampler2D randomBuf = sampler_state
{
Address = Wrap;
Filter = Trilinear;
Texture = "textures/ssaoRandom.jpg";
};
sampler2D buf0 = sampler_state
{
Address = Clamp;
Filter = None;
};
context SSAO
{
VertexShader = compile GLSL VS_FSQUAD;
PixelShader = compile GLSL FS_SSAO;
}
context VERTICALBLUR
{
VertexShader = compile GLSL VS_FSQUAD;
PixelShader = compile GLSL FS_VERTICALBLUR;
}
context HORIZONTALBLUR
{
VertexShader = compile GLSL VS_FSQUAD;
PixelShader = compile GLSL FS_HORIZONTALBLUR;
}
[[VS_FSQUAD]]
uniform mat4 projMat;
attribute vec3 vertPos;
varying vec2 texCoords;
void main( void )
{
texCoords = vertPos.xy;
gl_Position = projMat * vec4( vertPos, 1 );
}
[[FS_SSAO]]
//Thanks http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/a-simple-and-practical-approach-to-ssao-r2753
#include "shaders/utilityLib/fragDeferredRead.glsl"
uniform sampler2D randomBuf;
uniform vec2 frameBufSize;
varying vec2 texCoords;
vec2 getRandom(vec2 coord)
{
float randBufSize = 64.0;
return normalize(texture2D(randomBuf, frameBufSize * coord / randBufSize).xy * 2.0 - 1.0);
}
float doAmbientOcclusion(vec2 tcoord, vec2 uv, vec3 p, vec3 cnorm)
{
float g_scale = 5.5;
float g_bias = .02;
float g_intensity = 5.5;
vec3 diff = getPos(tcoord+uv)-p;
vec3 v = normalize(diff);
float d = length(diff)*g_scale;
return max(0.0, dot(cnorm, v)-g_bias)*(1.0/(1.0+d))*g_intensity;
}
void main( void )
{
vec2 vec[8];
vec[0] = vec2(1,0);
vec[1] = vec2(-1,0);
vec[2] = vec2(0,1);
vec[3] = vec2(0,-1);
vec[4] = vec2(.707, .707);
vec[5] = vec2(-.707, .707);
vec[6] = vec2(.707, -.707);
vec[7] = vec2(-.707, -.707);
vec3 p = getPos(texCoords);
vec3 n = getNormal(texCoords);
vec2 rand = getRandom(texCoords);
float ao = 0.0;
float rad = 0.25/p.z;
int iterations = 4;
for (int j=0; j < iterations; ++j) {
vec2 coord1 = reflect(vec[j], rand)*rad;
vec2 coord2 = vec2(coord1.x*.707 - coord1.y*0.707,
coord1.x*.707 + coord1.y*.707);
ao += doAmbientOcclusion(texCoords, coord1*.25, p, n);
ao += doAmbientOcclusion(texCoords, coord1*.5, p, n);
ao += doAmbientOcclusion(texCoords, coord1*.75, p, n);
ao += doAmbientOcclusion(texCoords, coord1*1., p, n);
}
ao /= iterations*4.0;
ao = 1-clamp(ao, 0, 1);
gl_FragColor = vec4(ao, ao, ao, 1);
}
[[FS_VERTICALBLUR]]
uniform sampler2D buf0;
uniform vec2 frameBufSize;
varying vec2 texCoords;
void main( void )
{
float blur[7];
blur[0] = 1;
blur[1] = 1;
blur[2] = 2;
blur[3] = 3;
blur[4] = 2;
blur[5] = 1;
blur[6] = 1;
float dev = 11.0;
float width = 3.5;
vec4 outCol = vec4(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < 7; ++i ) {
outCol += texture2D(buf0, texCoords+vec2(0, width*(i-3))/frameBufSize)/dev*blur[i];
}
gl_FragColor = outCol;
}
[[FS_HORIZONTALBLUR]]
uniform sampler2D buf0;
uniform vec2 frameBufSize;
varying vec2 texCoords;
void main( void )
{
float blur[7];
blur[0] = 1;
blur[1] = 1;
blur[2] = 2;
blur[3] = 3;
blur[4] = 2;
blur[5] = 1;
blur[6] = 1;
float dev = 11.0;
float width = 3.5;
vec4 outCol = vec4(0.0, 0.0, 0.0, 0.0);
for (int i = 0; i < 7; ++i ) {
outCol += texture2D(buf0, texCoords+vec2(width*(i-3), 0)/frameBufSize)/dev*blur[i];
}
gl_FragColor = outCol;
}