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

Revision 2886, 5.0 KB checked in by mattausch, 16 years ago (diff)

using gaussian distribution for sampling

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