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

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