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

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

bug: downsampling of positions does not work

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