1 | //////////////////// |
---|
2 | // Screen Spaced Ambient Occlusion shader |
---|
3 | // based on shader of Alexander Kusternig |
---|
4 | |
---|
5 | //#define NUM_SAMPLES 8 |
---|
6 | #define NUM_SAMPLES 16 |
---|
7 | |
---|
8 | // rule of thumb: approx 1 / NUM_SAMPLES |
---|
9 | #define SAMPLE_INTENSITY 0.15 |
---|
10 | //#define SAMPLE_INTENSITY 0.125f |
---|
11 | |
---|
12 | #define AREA_SIZE 7e-1f |
---|
13 | //#define AREA_SIZE 3e-1f |
---|
14 | #define VIEW_CORRECTION_SCALE 0.3f |
---|
15 | //#define VIEW_CORRECTION_SCALE 0.5f |
---|
16 | #define DISTANCE_SCALE 1e-6f |
---|
17 | |
---|
18 | |
---|
19 | struct fragment |
---|
20 | { |
---|
21 | // normalized screen position |
---|
22 | float4 pos: WPOS; |
---|
23 | float4 texCoord: TEXCOORD0; |
---|
24 | float3 view: COLOR0; |
---|
25 | }; |
---|
26 | |
---|
27 | |
---|
28 | struct pixel |
---|
29 | { |
---|
30 | float4 illum_col: COLOR0; |
---|
31 | float4 combined_col: COLOR1; |
---|
32 | }; |
---|
33 | |
---|
34 | |
---|
35 | float2 reflect(float2 pt, float2 n) |
---|
36 | { |
---|
37 | // distance to plane |
---|
38 | float d = dot(n, pt); |
---|
39 | // reflect around plane |
---|
40 | float2 rpt = pt - d * 2.0f * n; |
---|
41 | return rpt; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | /*float2 rotate(float2 pt, float2 n) |
---|
46 | { |
---|
47 | float2 ptTransformed; |
---|
48 | ptTransformed.x = n.r * pt.x - n.g * pt.y; |
---|
49 | ptTransformed.y = n.g * pt.x + n.r * pt.y; |
---|
50 | |
---|
51 | return ptTransformed; |
---|
52 | }*/ |
---|
53 | |
---|
54 | |
---|
55 | /** The ssao shader returning the an intensity value between 0 and 1 |
---|
56 | */ |
---|
57 | float ssao(fragment IN, |
---|
58 | uniform sampler2D positions, |
---|
59 | uniform sampler2D noiseTexture, |
---|
60 | uniform float2 samples[NUM_SAMPLES], |
---|
61 | uniform float3 currentNormal, |
---|
62 | uniform float3 currentViewDir, |
---|
63 | uniform float noiseMultiplier, |
---|
64 | uniform float4 centerPosition |
---|
65 | ) |
---|
66 | { |
---|
67 | // the w coordinate from the persp. projection |
---|
68 | float w = centerPosition.w; |
---|
69 | |
---|
70 | // Check in a circular area around the current position. |
---|
71 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
72 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
73 | |
---|
74 | float total_ao = 0.0; |
---|
75 | |
---|
76 | const float areaSize = 5e-1f; |
---|
77 | |
---|
78 | for (int i = 0; i < NUM_SAMPLES; i ++) |
---|
79 | { |
---|
80 | float2 offset = samples[i]; |
---|
81 | |
---|
82 | //sample noisetex; r stores costheta, g stores sintheta |
---|
83 | //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; |
---|
84 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; |
---|
85 | |
---|
86 | // rotation |
---|
87 | //float2 offsetTransformed = offset; |
---|
88 | //float2 offsetTransformed = rotate(offset, mynoise); |
---|
89 | float2 offsetTransformed = reflect(offset, mynoise); |
---|
90 | |
---|
91 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
92 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
93 | |
---|
94 | // sample downsampled texture in order to speed up texture accesses |
---|
95 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; |
---|
96 | //float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
97 | |
---|
98 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
99 | float length_to_sample = length(vector_to_sample); |
---|
100 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
101 | |
---|
102 | // Angle between current normal and direction to sample controls AO intensity. |
---|
103 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
104 | cos_angle = max(cos_angle, 0.0f); |
---|
105 | |
---|
106 | // distance between current position and sample position controls AO intensity. |
---|
107 | float distance_intensity = |
---|
108 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); |
---|
109 | |
---|
110 | // if surface normal perpenticular to view dir, some samples probably count less |
---|
111 | // => compensate for this |
---|
112 | float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); |
---|
113 | |
---|
114 | total_ao += cos_angle * distance_intensity * view_correction; |
---|
115 | } |
---|
116 | |
---|
117 | return max(0.0f, 1.0f - total_ao); |
---|
118 | //return dot(currentViewDir, currentNormal); |
---|
119 | } |
---|
120 | |
---|
121 | |
---|
122 | /** Computes ambient occlusion + diffuse reflections |
---|
123 | */ |
---|
124 | float4 globIllum(fragment IN, |
---|
125 | uniform sampler2D colors, |
---|
126 | uniform sampler2D positions, |
---|
127 | uniform sampler2D noiseTexture, |
---|
128 | uniform float2 samples[NUM_SAMPLES], |
---|
129 | uniform float3 currentNormal, |
---|
130 | uniform float3 currentViewDir, |
---|
131 | uniform float noiseMultiplier, |
---|
132 | uniform float4 centerPosition |
---|
133 | ) |
---|
134 | { |
---|
135 | // the w coordinate from the persp. projection |
---|
136 | float w = centerPosition.w; |
---|
137 | |
---|
138 | // Check in a circular area around the current position. |
---|
139 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
140 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
141 | |
---|
142 | // ao is in stored in the w |
---|
143 | float4 total_color = float4(0, 0, 0, 1); |
---|
144 | |
---|
145 | const float areaSize = 5e-1f; |
---|
146 | |
---|
147 | for (int i = 0; i < NUM_SAMPLES; i ++) |
---|
148 | { |
---|
149 | float2 offset = samples[i]; |
---|
150 | |
---|
151 | //sample noisetex; r stores costheta, g stores sintheta |
---|
152 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; |
---|
153 | |
---|
154 | // rotation |
---|
155 | float2 offsetTransformed = reflect(offset, mynoise); |
---|
156 | |
---|
157 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
158 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
159 | |
---|
160 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; |
---|
161 | float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 1)).xyz; |
---|
162 | |
---|
163 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
164 | float length_to_sample = length(vector_to_sample); |
---|
165 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
166 | |
---|
167 | // Angle between current normal and direction to sample controls AO intensity. |
---|
168 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
169 | cos_angle = max(cos_angle, 0.0f); |
---|
170 | |
---|
171 | // distance between current position and sample position controls AO intensity. |
---|
172 | float distance_intensity = |
---|
173 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); |
---|
174 | |
---|
175 | // if normal perpenticular to view dir, only half of the samples count |
---|
176 | float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); |
---|
177 | |
---|
178 | total_color.w -= cos_angle * distance_intensity * view_correction; |
---|
179 | |
---|
180 | const float scale_factor = 0.3f; |
---|
181 | total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * scale_factor; |
---|
182 | } |
---|
183 | |
---|
184 | return saturate(total_color); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | /** The mrt shader for screen space ambient occlusion |
---|
189 | */ |
---|
190 | pixel main(fragment IN, |
---|
191 | uniform sampler2D colors, |
---|
192 | uniform sampler2D positions, |
---|
193 | uniform sampler2D normals, |
---|
194 | uniform sampler2D noiseTexture, |
---|
195 | uniform float2 samples[NUM_SAMPLES], |
---|
196 | uniform float noiseMultiplier, |
---|
197 | uniform sampler2D oldTex, |
---|
198 | const uniform float4x4 oldModelViewProj, |
---|
199 | uniform float maxDepth, |
---|
200 | uniform float expFactor |
---|
201 | ) |
---|
202 | { |
---|
203 | pixel OUT; |
---|
204 | |
---|
205 | float4 norm = tex2D(normals, IN.texCoord.xy); |
---|
206 | |
---|
207 | // the ambient term |
---|
208 | const float amb = norm.w; |
---|
209 | |
---|
210 | // expand normal |
---|
211 | float3 normal = normalize(norm.xyz); |
---|
212 | /// the current view direction |
---|
213 | float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); |
---|
214 | |
---|
215 | // the current world position |
---|
216 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy); |
---|
217 | |
---|
218 | // the current color |
---|
219 | const float4 currentCol = tex2D(colors, IN.texCoord.xy); |
---|
220 | const float currentDepth = currentCol.w; |
---|
221 | |
---|
222 | //float4 new_col = (float4)ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); |
---|
223 | float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); |
---|
224 | |
---|
225 | |
---|
226 | ///////////////// |
---|
227 | //-- compute temporally smoothed value |
---|
228 | |
---|
229 | float4 realPos = centerPosition * maxDepth; |
---|
230 | realPos.w = 1.0f; |
---|
231 | |
---|
232 | float4 oldPos = mul(oldModelViewProj, realPos); |
---|
233 | |
---|
234 | const float newDepth = oldPos.z / oldPos.w; |
---|
235 | |
---|
236 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; |
---|
237 | float4 oldCol = tex2D(oldTex, tex); |
---|
238 | |
---|
239 | const float oldDepth = oldCol.w; |
---|
240 | const float depthDif = 1.0f - newDepth / oldDepth; |
---|
241 | |
---|
242 | if ((tex.x >= 0.0f) && (tex.x < 1.0f) && |
---|
243 | (tex.y >= 0.0f) && (tex.y < 1.0f) && |
---|
244 | (abs(depthDif) < 1e-4f)) |
---|
245 | { |
---|
246 | OUT.illum_col = new_col * expFactor + oldCol * float4(1.0f - expFactor); |
---|
247 | } |
---|
248 | else |
---|
249 | { |
---|
250 | OUT.illum_col = new_col; |
---|
251 | } |
---|
252 | |
---|
253 | //const float ao = OUT.illum_col.x; |
---|
254 | const float ao = OUT.illum_col.w; |
---|
255 | |
---|
256 | //OUT.combined_col = (currentCol + OUT.illum_col) * ao; |
---|
257 | OUT.combined_col = currentCol * ao; |
---|
258 | |
---|
259 | OUT.illum_col.w = currentDepth; |
---|
260 | |
---|
261 | return OUT; |
---|
262 | } |
---|
263 | |
---|
264 | /* |
---|
265 | pixel combined(fragment IN, |
---|
266 | uniform sampler2D colors, |
---|
267 | uniform sampler2D ssaoTex |
---|
268 | ) |
---|
269 | { |
---|
270 | pixel OUT; |
---|
271 | |
---|
272 | float4 col = tex2D(colors, IN.texCoord.xy); |
---|
273 | float4 ao = tex2D(ssaoTex, IN.texCoord.xy); |
---|
274 | //float4 illum = tex2D(ssaoTex, IN.texCoord.xy); |
---|
275 | |
---|
276 | OUT.illum_col = col * ao; |
---|
277 | |
---|
278 | return OUT; |
---|
279 | }*/ |
---|