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

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