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

Revision 2836, 8.3 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//#define SAMPLE_INTENSITY 0.125f
8// rule of thumb: approx 1 / NUM_SAMPLES
9#define SAMPLE_INTENSITY 0.22 //0.0625f
10
11#define AREA_SIZE 5e-1f
12
13
14struct fragment
15{
16         // normalized screen position
17        float4 pos: WPOS;
18        float4 texCoord: TEXCOORD0;
19        float3 view: COLOR0;
20};
21
22
23struct pixel
24{
25  float4 color: COLOR0;
26};
27
28
29float2 reflect(float2 pt, float2 n)
30{
31  // distance to plane
32  float d = dot(n, pt);
33  // reflect around plane
34  float2 rpt = pt - d * 2.0f * n;
35
36  //return pt;
37  return rpt;
38}
39
40
41float2 rotate(float2 pt, float2 n)
42{
43        float2 ptTransformed;
44        ptTransformed.x = n.r * pt.x - n.g * pt.y;
45        ptTransformed.y = n.g * pt.x + n.r * pt.y;
46
47        return ptTransformed;
48}
49
50
51float ssao(fragment IN,
52                   uniform sampler2D positions,
53                   uniform sampler2D noiseTexture,
54                   uniform float2 samples[NUM_SAMPLES],
55                   uniform float3 currentNormal,
56                   uniform float3 currentViewDir,
57                   uniform float noiseMultiplier,
58                   uniform float4 centerPosition
59                   )
60{
61        // the w coordinate from the persp. projection
62        float w = centerPosition.w;
63
64        // Check in a circular area around the current position.
65        // Shoot vectors to the positions there, and check the angle to these positions.
66        // Summing up these angles gives an estimation of the occlusion at the current position.
67
68        float total_ao = 0.0;
69
70        const float areaSize = 5e-1f;
71
72        for (int i = 0; i < NUM_SAMPLES; i ++)
73        {
74                float2 offset = samples[i];
75
76                //sample noisetex; r stores costheta, g stores sintheta
77                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
78
79                // rotation
80                //float2 offsetTransformed = offset;
81                float2 offsetTransformed = rotate(offset, mynoise);
82                //float2 offsetTransformed = reflect(offset, noise);
83
84                // weight with projected coordinate to reach similar kernel size for near and far
85                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
86
87                float3 sample_position = tex2D(positions, texcoord).xyz;
88
89                float3 vector_to_sample = sample_position - centerPosition.xyz;
90                float length_to_sample = length(vector_to_sample);
91                float3 direction_to_sample = vector_to_sample / length_to_sample;
92
93                // Angle between current normal and direction to sample controls AO intensity.
94                float cos_angle = dot(direction_to_sample, currentNormal);
95                cos_angle = max(cos_angle, 0.0f);
96
97                // distance between current position and sample position controls AO intensity.
98                const float distanceScale = 1e-6f;
99
100                float distance_intensity =
101                        (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample);
102
103                // if normal perpenticular to view dir, only half of the samples count
104                float view_correction = 1.0f + 0.5f * (1.0f - dot(currentViewDir, currentNormal));
105
106                total_ao += cos_angle * distance_intensity * view_correction;
107        }
108
109        return (1.0f - total_ao);
110}
111
112
113float4 globIllum(fragment IN,
114                                 uniform sampler2D colors,
115                                 uniform sampler2D positions,
116                                 uniform sampler2D noiseTexture,
117                                 uniform float2 samples[NUM_SAMPLES],
118                                 uniform float3 currentNormal,
119                                 uniform float3 currentViewDir,
120                                 uniform float noiseMultiplier,
121                                 uniform float4 centerPosition)
122{
123        // the w coordinate from the persp. projection
124        float w = centerPosition.w;
125
126        // Check in a circular area around the current position.
127        // Shoot vectors to the positions there, and check the angle to these positions.
128        // Summing up these angles gives an estimation of the occlusion at the current position.
129
130        float total_ao = 0.0;
131        float3 total_color = float3(0.0f);
132
133        const float areaSize = 5e-1f;
134
135        for (int i = 0; i < NUM_SAMPLES; i ++)
136        {
137                float2 offset = samples[i];
138
139                //sample noisetex; r stores costheta, g stores sintheta
140                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
141
142                // rotation
143                float2 offsetTransformed = rotate(offset, mynoise);
144
145                // weight with projected coordinate to reach similar kernel size for near and far
146                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
147
148                float3 sample_position = tex2D(positions, texcoord).xyz;
149                float3 sample_color = tex2D(colors, texcoord).xyz;
150
151                float3 vector_to_sample = sample_position - centerPosition.xyz;
152                float length_to_sample = length(vector_to_sample);
153                float3 direction_to_sample = vector_to_sample / length_to_sample;
154
155                // Angle between current normal and direction to sample controls AO intensity.
156                float cos_angle = dot(direction_to_sample, currentNormal);
157                cos_angle = max(cos_angle, 0.0f);
158
159                // distance between current position and sample position controls AO intensity.
160                const float distanceScale = 1e-6f;
161
162                float distance_intensity =
163                        (SAMPLE_INTENSITY * distanceScale) / (distanceScale + length_to_sample * length_to_sample);
164
165                const float view_correction_scale = 0.5f;
166                // if normal perpenticular to view dir, only half of the samples count
167                float view_correction = 1.0f + view_correction_scale * (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 * 0.3f;
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 col1 = attenuated_color;
242
243        const float x = 0.1f;
244
245        float4 dummy = centerPosition * maxDepth;
246        dummy.w = 1.0f;
247
248        float4 oldPos = mul(oldModelViewProj, dummy);
249
250        float newDepth = oldPos.z / oldPos.w;
251 
252        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
253        float4 col1 = tex2D(oldTex, tex);
254
255        float oldDepth = col1.w;
256
257        if ((tex.x >= 0.0f) && (tex.x < 1.0f) && (tex.y >= 0.0f) && (tex.y < 1.0f) && (abs(newDepth - oldDepth) < 9e-4f))
258                OUT.color = attenuated_color * x + col1 * float4(1.0f - x);
259        else
260                OUT.color = attenuated_color;
261
262        //OUT.color = float4(centerPosition.w);
263        //OUT.color = float4(oldDepth);
264        //OUT.color = float4(newDepth);
265        //OUT.color = float4(abs(newDepth - oldDepth));
266        OUT.color.w = tex2D(colors, IN.texCoord.xy).w;
267
268        return OUT;
269}
270
271
272pixel main(fragment IN,
273                   uniform sampler2D colors,
274                   uniform sampler2D positions,
275                   uniform sampler2D normals,
276                   uniform sampler2D oldTex
277                   )
278{
279        pixel OUT;
280
281        float4 normal = tex2D(normals, IN.texCoord.xy);
282        float amb = normal.w;
283
284        //OUT.color.xyz = IN.view;
285        // expand normal
286        normal = normalize(normal * 2.0f - float4(1.0f));
287
288        float4 col = shade(IN, colors, positions, normal.xyz, amb);
289        //OUT.color = col;
290
291        float4 col1 = tex2D(oldTex, IN.texCoord.xy);
292
293        const float x = 0.01f;
294        OUT.color = col * x + col1 * float4(1.0f - x);
295
296        return OUT;
297}
Note: See TracBrowser for help on using the repository browser.