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

Revision 2861, 8.1 KB checked in by mattausch, 16 years ago (diff)

cleaned up code

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 DISTANCE_SCALE 1e-6f
15
16struct fragment
17{
18         // normalized screen position
19        float4 pos: WPOS;
20        float4 texCoord: TEXCOORD0;
21        float3 view: COLOR0;
22};
23
24
25struct pixel
26{
27        float4 color: COLOR0;
28};
29
30
31float2 reflect(float2 pt, float2 n)
32{
33  // distance to plane
34  float d = dot(n, pt);
35  // reflect around plane
36  float2 rpt = pt - d * 2.0f * n;
37
38  //return pt;
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
53float ssao(fragment IN,
54                   uniform sampler2D positions,
55                   uniform sampler2D noiseTexture,
56                   uniform float2 samples[NUM_SAMPLES],
57                   uniform float3 currentNormal,
58                   uniform float3 currentViewDir,
59                   uniform float noiseMultiplier,
60                   uniform float4 centerPosition
61                   )
62{
63        // the w coordinate from the persp. projection
64        float w = centerPosition.w;
65
66        // Check in a circular area around the current position.
67        // Shoot vectors to the positions there, and check the angle to these positions.
68        // Summing up these angles gives an estimation of the occlusion at the current position.
69
70        float total_ao = 0.0;
71
72        const float areaSize = 5e-1f;
73
74        for (int i = 0; i < NUM_SAMPLES; i ++)
75        {
76                float2 offset = samples[i];
77
78                //sample noisetex; r stores costheta, g stores sintheta
79                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
80
81                // rotation
82                //float2 offsetTransformed = offset;
83                float2 offsetTransformed = rotate(offset, mynoise);
84                //float2 offsetTransformed = reflect(offset, noise);
85
86                // weight with projected coordinate to reach similar kernel size for near and far
87                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
88
89                float3 sample_position = tex2D(positions, texcoord).xyz;
90
91                float3 vector_to_sample = sample_position - centerPosition.xyz;
92                float length_to_sample = length(vector_to_sample);
93                float3 direction_to_sample = vector_to_sample / length_to_sample;
94
95                // Angle between current normal and direction to sample controls AO intensity.
96                float cos_angle = dot(direction_to_sample, currentNormal);
97                cos_angle = max(cos_angle, 0.0f);
98
99                // distance between current position and sample position controls AO intensity.
100                float distance_intensity =
101                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + 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 + VIEW_CORRECTION_SCALE * (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
113/** Computes ambient occlusion + diffuse reflections
114*/
115float4 globIllum(fragment IN,
116                                 uniform sampler2D colors,
117                                 uniform sampler2D positions,
118                                 uniform sampler2D noiseTexture,
119                                 uniform float2 samples[NUM_SAMPLES],
120                                 uniform float3 currentNormal,
121                                 uniform float3 currentViewDir,
122                                 uniform float noiseMultiplier,
123                                 uniform float4 centerPosition
124                                 )
125{
126        // the w coordinate from the persp. projection
127        float w = centerPosition.w;
128
129        // Check in a circular area around the current position.
130        // Shoot vectors to the positions there, and check the angle to these positions.
131        // Summing up these angles gives an estimation of the occlusion at the current position.
132
133        float total_ao = 0.0;
134        float3 total_color = float3(0.0f);
135
136        const float areaSize = 5e-1f;
137
138        for (int i = 0; i < NUM_SAMPLES; i ++)
139        {
140                float2 offset = samples[i];
141
142                //sample noisetex; r stores costheta, g stores sintheta
143                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
144
145                // rotation
146                float2 offsetTransformed = rotate(offset, mynoise);
147
148                // weight with projected coordinate to reach similar kernel size for near and far
149                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
150
151                float3 sample_position = tex2D(positions, texcoord).xyz;
152                float3 sample_color = tex2D(colors, texcoord).xyz;
153
154                float3 vector_to_sample = sample_position - centerPosition.xyz;
155                float length_to_sample = length(vector_to_sample);
156                float3 direction_to_sample = vector_to_sample / length_to_sample;
157
158                // Angle between current normal and direction to sample controls AO intensity.
159                float cos_angle = dot(direction_to_sample, currentNormal);
160                cos_angle = max(cos_angle, 0.0f);
161
162                // distance between current position and sample position controls AO intensity.
163                float distance_intensity =
164                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + 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 + 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
206/** The mrt shader for screen space ambient occlusion
207*/
208pixel main_ssao(fragment IN,
209                                uniform sampler2D colors,
210                                uniform sampler2D positions,
211                                uniform sampler2D normals,
212                                uniform sampler2D noiseTexture,
213                                uniform float2 samples[NUM_SAMPLES],
214                                uniform float noiseMultiplier,
215                                uniform sampler2D oldTex,
216                                const uniform float4x4 oldModelViewProj,
217                                uniform float maxDepth,
218                                uniform float expFactor
219                                )
220{
221        pixel OUT;
222
223        float4 normal = tex2D(normals, IN.texCoord.xy);
224        float amb = normal.w;
225
226        // expand normal
227        normal = normalize(normal * 2.0f - 1.0f);
228        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
229
230        // the current world position
231        float4 centerPosition = tex2D(positions, IN.texCoord.xy);
232       
233        float4 col = shade(IN, colors, positions, normal.xyz, amb);
234
235        float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
236        float4 attenuated_color = ao * col;
237
238        //float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
239        //float ao = new_col.w;
240        //float4 attenuated_color = ao * col + new_col;
241       
242        const float x = expFactor;
243
244        float4 dummy = centerPosition * maxDepth;
245        dummy.w = 1.0f;
246
247        float4 oldPos = mul(oldModelViewProj, dummy);
248
249        float newDepth = oldPos.z / oldPos.w;
250 
251        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
252        float4 col1 = tex2D(oldTex, tex);
253
254        float oldDepth = col1.w;
255        float depthDif = 1.0f - newDepth / oldDepth;
256
257        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
258                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
259                (abs(depthDif)  < 8e-5f))
260        {
261                OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor);
262        }
263        else
264        {
265                OUT.color = attenuated_color;
266        }
267
268        OUT.color.w = tex2D(colors, IN.texCoord.xy).w;
269
270        return OUT;
271}
272
273
274/** The mrt shader for standard rendering
275*/
276pixel main(fragment IN,
277                   uniform sampler2D colors,
278                   uniform sampler2D positions,
279                   uniform sampler2D normals
280                   )
281{
282        pixel OUT;
283
284        float4 normal = tex2D(normals, IN.texCoord.xy);
285        float amb = normal.w;
286
287        // expand normal
288        normal = normalize(normal * 2.0f - float4(1.0f));
289
290        float4 col = shade(IN, colors, positions, normal.xyz, amb);
291       
292        OUT.color = col;
293
294        return OUT;
295}
Note: See TracBrowser for help on using the repository browser.