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

Revision 2838, 7.9 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
[2837]9#define SAMPLE_INTENSITY 0.2
10//#define SAMPLE_INTENSITY 0.125f
[2834]11
[2822]12#define AREA_SIZE 5e-1f
[2837]13#define VIEW_CORRECTION_SCALE 0.3f
14#define DISTANCE_SCALE 1e-6f
[2821]15
[2810]16struct fragment
17{
[2836]18         // normalized screen position
19        float4 pos: WPOS;
20        float4 texCoord: TEXCOORD0;
21        float3 view: COLOR0;
[2810]22};
23
24
25struct pixel
26{
[2837]27        float4 color: COLOR0;
[2810]28};
29
30
[2814]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;
[2834]37
[2814]38  //return pt;
39  return rpt;
40}
41
42
[2821]43float2 rotate(float2 pt, float2 n)
44{
[2834]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;
[2821]48
[2834]49        return ptTransformed;
[2821]50}
51
52
[2810]53float ssao(fragment IN,
[2834]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                   )
[2810]62{
[2834]63        // the w coordinate from the persp. projection
64        float w = centerPosition.w;
[2810]65
[2834]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.
[2810]69
[2834]70        float total_ao = 0.0;
[2825]71
[2834]72        const float areaSize = 5e-1f;
[2833]73
[2834]74        for (int i = 0; i < NUM_SAMPLES; i ++)
75        {
76                float2 offset = samples[i];
[2833]77
[2834]78                //sample noisetex; r stores costheta, g stores sintheta
[2835]79                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
[2833]80
[2834]81                // rotation
82                //float2 offsetTransformed = offset;
[2835]83                float2 offsetTransformed = rotate(offset, mynoise);
[2834]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 =
[2837]101                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
[2834]102
103                // if normal perpenticular to view dir, only half of the samples count
[2837]104                float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
[2834]105
106                total_ao += cos_angle * distance_intensity * view_correction;
107        }
108
109        return (1.0f - total_ao);
[2810]110}
111
112
[2834]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,
[2837]121                                 uniform float4 centerPosition
122                                 )
[2834]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
[2835]141                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
[2834]142
143                // rotation
[2835]144                float2 offsetTransformed = rotate(offset, mynoise);
[2834]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                float distance_intensity =
[2837]162                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
[2834]163
164                // if normal perpenticular to view dir, only half of the samples count
[2837]165                float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
[2834]166
167                total_ao += cos_angle * distance_intensity * view_correction;
[2836]168                total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f;
[2834]169        }
170
171        return float4(total_color, 1.0f - total_ao);
172}
173
174
[2820]175float4 shade(fragment IN,
[2834]176                         uniform sampler2D colors,
177                         uniform sampler2D positions,
178                         uniform float3 normal,
179                         uniform float amb)
[2810]180{
[2834]181        float4 lightDir = float4(0.8f, -1.0f, 0.7f, 0.0f);
182        float4 lightDir2 = float4(-0.5f, 0.5f, 0.4f, 0.0f);
[2810]183
[2834]184        float4 color = tex2D(colors, IN.texCoord.xy);
[2810]185
[2834]186        float4 position = tex2D(positions, IN.texCoord.xy);
[2810]187
[2834]188        float4 ambient = 0.3f;
[2815]189
[2834]190        // float3 L = normalize(lightPosition - position);
191        float3 light = normalize(lightDir.xyz);
192        float3 light2 = normalize(lightDir2.xyz);
193
194        float diffuseLight = max(dot(normal, light), 0.0f);
195        float diffuseLight2 = max(dot(normal, light2), 0.0f);
196
197        float diffuse = diffuseLight + diffuseLight2;
198        //float diffuse = diffuseLight;
199
200        return (ambient + diffuse) * color * (1.0f - amb) + amb * color;
[2820]201}
202
203
[2821]204pixel main_ssao(fragment IN,
[2834]205                                uniform sampler2D colors,
206                                uniform sampler2D positions,
207                                uniform sampler2D normals,
208                                uniform sampler2D noiseTexture,
209                                uniform float2 samples[NUM_SAMPLES],
210                                uniform float noiseMultiplier,
211                                uniform sampler2D oldTex,
[2835]212                                const uniform float4x4 oldModelViewProj,
[2837]213                                uniform float maxDepth,
214                                uniform float expFactor
[2834]215                                )
[2821]216{
[2834]217        pixel OUT;
[2821]218
[2834]219        float4 normal = tex2D(normals, IN.texCoord.xy);
220        float amb = normal.w;
[2822]221
[2834]222        // expand normal
223        normal = normalize(normal * 2.0f - 1.0f);
224        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
[2822]225
[2834]226        // the current world position
227        float4 centerPosition = tex2D(positions, IN.texCoord.xy);
228       
229        float4 col = shade(IN, colors, positions, normal.xyz, amb);
[2821]230
[2837]231        float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
232        float4 attenuated_color = ao * col;
[2834]233
[2837]234        //float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition);
235        //float ao = new_col.w;
236        //float4 attenuated_color = ao * col + new_col;
237       
238        const float x = expFactor;
[2834]239
[2835]240        float4 dummy = centerPosition * maxDepth;
[2834]241        dummy.w = 1.0f;
[2836]242
[2834]243        float4 oldPos = mul(oldModelViewProj, dummy);
244
245        float newDepth = oldPos.z / oldPos.w;
246 
247        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
248        float4 col1 = tex2D(oldTex, tex);
249
250        float oldDepth = col1.w;
[2837]251        float depthDif = 1.0f - newDepth / oldDepth;
[2834]252
[2837]253        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
254                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
[2838]255                (abs(depthDif)  < 8e-5f))
[2837]256                OUT.color = attenuated_color * expFactor + col1 * float4(1.0f - expFactor);
[2835]257        else
258                OUT.color = attenuated_color;
[2834]259
260        OUT.color.w = tex2D(colors, IN.texCoord.xy).w;
261
262        return OUT;
[2821]263}
264
265
[2820]266pixel main(fragment IN,
[2834]267                   uniform sampler2D colors,
268                   uniform sampler2D positions,
[2837]269                   uniform sampler2D normals
[2834]270                   )
[2820]271{
[2834]272        pixel OUT;
[2820]273
[2834]274        float4 normal = tex2D(normals, IN.texCoord.xy);
275        float amb = normal.w;
[2821]276
[2834]277        // expand normal
278        normal = normalize(normal * 2.0f - float4(1.0f));
279
280        float4 col = shade(IN, colors, positions, normal.xyz, amb);
[2837]281        OUT.color = col;
[2834]282
283        return OUT;
[2810]284}
Note: See TracBrowser for help on using the repository browser.