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

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