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

Revision 2881, 5.1 KB checked in by mattausch, 16 years ago (diff)

working quite well

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};
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
45/** The ssao shader returning the an intensity value between 0 and 1
46*/
47float ssao(fragment IN,
48                   uniform sampler2D positions,
49                   uniform sampler2D noiseTexture,
50                   uniform float2 samples[NUM_SAMPLES],
51                   uniform float3 currentNormal,
52                   uniform float3 currentViewDir,
53                   uniform float noiseMultiplier,
54                   uniform float4 centerPosition
55                   )
56{
57        // the w coordinate from the persp. projection
58        float w = centerPosition.w;
59
60        // Check in a circular area around the current position.
61        // Shoot vectors to the positions there, and check the angle to these positions.
62        // Summing up these angles gives an estimation of the occlusion at the current position.
63
64        float total_ao = 0.0;
65
66        for (int i = 0; i < NUM_SAMPLES; ++ i)
67        {
68                float2 offset = samples[i];
69
70                ////////////////////
71                // add random noise: r stores costheta, g stores sintheta
72
73                //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
74                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
75
76                float2 offsetTransformed = reflect(offset, mynoise);
77
78                // weight with projected coordinate to reach similar kernel size for near and far
79                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
80
81                // sample downsampled texture in order to speed up texture accesses
82                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
83                //float3 sample_position = tex2D(positions, texcoord).xyz;
84
85                float3 vector_to_sample = sample_position - centerPosition.xyz;
86                const float length_to_sample = length(vector_to_sample);
87
88                float3 direction_to_sample = vector_to_sample / length_to_sample;
89
90                // Angle between current normal and direction to sample controls AO intensity.
91                const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
92
93                // distance between current position and sample position controls AO intensity.
94                const float distance_intensity =
95                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
96
97                // if surface normal perpenticular to view dir, some samples probably count less
98                // => compensate for this
99                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
100
101                total_ao += cos_angle * distance_intensity * view_correction;
102        }
103
104        return max(0.0f, 1.0f - total_ao);
105}
106
107
108/** The mrt shader for screen space ambient occlusion
109*/
110pixel main(fragment IN,
111                   uniform sampler2D colors,
112                   uniform sampler2D positions,
113                   uniform sampler2D normals,
114                   uniform sampler2D noiseTexture,
115                   uniform float2 samples[NUM_SAMPLES],
116                   uniform float noiseMultiplier,
117                   uniform sampler2D oldTex,
118                   const uniform float4x4 oldModelViewProj,
119                   uniform float maxDepth,
120                   uniform float expFactor
121                   )
122{
123        pixel OUT;
124
125        float4 norm = tex2D(normals, IN.texCoord.xy);
126       
127        // the ambient term
128        const float amb = norm.w;
129
130        // expand normal
131        float3 normal = normalize(norm.xyz);
132        /// the current view direction
133        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
134
135        // the current world position
136        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
137       
138        // the current color
139        const float4 currentCol = tex2D(colors, IN.texCoord.xy);
140        const float currentDepth = currentCol.w;
141
142        const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition);
143               
144
145        /////////////////
146        //-- compute temporally smoothing
147
148        float4 realPos = centerPosition * maxDepth;
149        realPos.w = 1.0f;
150
151        // reproject
152        float4 oldPos = mul(oldModelViewProj, realPos);
153
154        const float newDepth = oldPos.z / oldPos.w;
155
156        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
157        float4 oldCol = tex2D(oldTex, tex);
158
159        const float oldDepth = oldCol.w;
160        const float depthDif = 1.0f - newDepth / oldDepth;
161
162
163        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
164                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
165                (abs(depthDif) < 1e-3f))
166        {
167                OUT.illum_col.x = ao * expFactor + oldCol.x * (1.0f - expFactor);
168        }
169        else
170        {
171                OUT.illum_col.x = ao;
172        }
173
174        OUT.illum_col.w = currentDepth;
175
176        return OUT;
177}
178
179
180
181pixel combine(fragment IN,
182                          uniform sampler2D colors,
183                          uniform sampler2D ssaoTex)
184{
185        pixel OUT;
186
187        float4 col = tex2D(colors, IN.texCoord.xy);
188        float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
189
190        OUT.illum_col = col * ao.x;
191
192        return OUT;
193}
Note: See TracBrowser for help on using the repository browser.