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

Revision 2835, 8.3 KB checked in by mattausch, 16 years ago (diff)

removed ambient occlusion bugs

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