source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/deferred.cg @ 2863

Revision 2863, 8.4 KB checked in by mattausch, 16 years ago (diff)

repaired view vector correction for ssao

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