source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao.cg @ 2871

Revision 2871, 7.8 KB checked in by mattausch, 16 years ago (diff)

working with 50 frames

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