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

Revision 2903, 5.3 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "../shaderenv.h"
2
3////////////////////
4// Screen Spaced Ambient Occlusion shader
5// based on shader of Alexander Kusternig
6
7
8struct fragment
9{
10         // normalized screen position
11        float4 pos: WPOS;
12        float2 texCoord: TEXCOORD0;
13        float3 view: TEXCOORD1;
14};
15
16
17struct pixel
18{
19        float4 illum_col: COLOR0;
20};
21
22
23float2 myreflect(float2 pt, float2 n)
24{
25        // distance to plane
26        float d = dot(n, pt);
27        // reflect around plane
28        float2 rpt = pt - d * 2.0f * n;
29
30        return rpt;
31}
32
33
34
35/** The ssao shader returning the an intensity value between 0 and 1
36*/
37float ssao(fragment IN,
38                   uniform sampler2D positions,
39                   uniform sampler2D noiseTexture,
40                   uniform float2 samples[NUM_SAMPLES],
41                   uniform float3 currentNormal,
42                   uniform float3 currentViewDir,
43                   uniform float4 centerPosition
44                   )
45{
46        // the w coordinate from the persp. projection
47        float w = centerPosition.w;
48
49        // Check in a circular area around the current position.
50        // Shoot vectors to the positions there, and check the angle to these positions.
51        // Summing up these angles gives an estimation of the occlusion at the current position.
52
53        float total_ao = 0.0;
54
55        for (int i = 0; i < NUM_SAMPLES; ++ i)
56        {
57                const float2 offset = samples[i];
58
59#if 1
60                ////////////////////
61                // add random noise: reflect around random normal vector (warning: slow!)
62                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
63                const float2 offsetTransformed = myreflect(offset, mynoise);
64#else
65                const float2 offsetTransformed = offset;
66#endif
67                // weight with projected coordinate to reach similar kernel size for near and far
68                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
69
70                //if ((texcoord.x <= 1.0f) || (texcoord.x >= 0.0f) || (texcoord.y <= 1.0f) || (texcoord.y >= 0.0f))
71               
72                // sample downsampled texture in order to speed up texture accesses
73                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
74                //float3 sample_position = tex2D(positions, texcoord).xyz;
75
76                float3 vector_to_sample = sample_position - centerPosition.xyz;
77                const float length_to_sample = length(vector_to_sample);
78
79                float3 direction_to_sample = vector_to_sample / length_to_sample;
80
81                // angle between current normal and direction to sample controls AO intensity.
82                const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
83
84                // distance between current position and sample position controls AO intensity.
85                const float distance_intensity =
86                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
87
88                // if surface normal perpenticular to view dir, approx. half of the samples will not count
89                // => compensate for this (on the other hand, projected sampling area could be larger!)
90                //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
91                //total_ao += cos_angle * distance_intensity * view_correction;
92
93                total_ao += cos_angle * distance_intensity;
94        }
95
96        return max(0.0f, 1.0f - total_ao);
97        //return saturate(dot(currentViewDir, currentNormal));
98}
99
100
101/** The mrt shader for screen space ambient occlusion
102*/
103pixel main(fragment IN,
104                   uniform sampler2D colors,
105                   uniform sampler2D positions,
106                   uniform sampler2D normals,
107                   uniform sampler2D noiseTexture,
108                   uniform float2 samples[NUM_SAMPLES],
109                   uniform sampler2D oldTex,
110                   const uniform float4x4 oldModelViewProj,
111                   uniform float maxDepth,
112                   uniform float temporalCoherence
113                   )
114{
115        pixel OUT;
116
117        float4 norm = tex2D(normals, IN.texCoord.xy);
118       
119        // the ambient term
120        const float amb = norm.w;
121
122        // expand normal
123        float3 normal = normalize(norm.xyz);
124       
125        /// the current view direction
126        float3 viewDir;// = normalize(IN.view);
127
128        // the current world position
129        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
130       
131        // the current color
132        const float4 currentCol = tex2D(colors, IN.texCoord.xy);
133        const float currentDepth = currentCol.w;
134
135        const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, centerPosition);
136               
137
138        /////////////////
139        //-- compute temporally smoothing
140
141        float4 realPos = centerPosition * maxDepth;
142        realPos.w = 1.0f;
143
144        // reproject
145        float4 oldPos = mul(oldModelViewProj, realPos);
146
147        const float newDepth = oldPos.z / oldPos.w;
148
149        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
150        float4 oldCol = tex2D(oldTex, tex);
151
152        const float oldDepth = oldCol.w;
153        const float depthDif = 1.0f - newDepth / oldDepth;
154
155        float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
156        float newWeight;
157
158        if ((temporalCoherence > 0) &&
159                (tex.x >= 0.0f) && (tex.x < 1.0f) &&
160                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
161                (abs(depthDif) < 1e-4f))
162        {
163                newWeight = oldWeight + 1;
164
165                //OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
166                OUT.illum_col = (float4)(ao + oldCol.x * oldWeight) / newWeight;
167        }
168        else
169        {
170                OUT.illum_col = (float4)ao;
171                newWeight = 0;
172        }
173
174
175        OUT.illum_col.z = newWeight;
176        OUT.illum_col.w = currentDepth;
177
178        return OUT;
179}
180
181
182pixel combine(fragment IN,
183                          uniform sampler2D colors,
184                          uniform sampler2D ssaoTex)
185{
186        pixel OUT;
187
188        float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
189        float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
190
191        OUT.illum_col = col * ao.x;
192        //OUT.illum_col = (float4)ao.x;
193
194        OUT.illum_col.w = ao.w;
195
196        return OUT;
197}
Note: See TracBrowser for help on using the repository browser.