source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/ssao3d.cg @ 2902

Revision 2902, 5.8 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
23float3 myreflect(float3 pt, float3 n)
24{
25        // distance to plane
26        float d = dot(n, pt);
27        // reflect around plane
28        float3 rpt = pt - d * 2.0f * n;
29
30        return rpt;
31}
32
33
34/** The ssao shader returning the an intensity value between 0 and 1
35*/
36float ssao(fragment IN,
37                   uniform sampler2D positions,
38                   uniform sampler2D noiseTexture,
39                   uniform float3 samples[NUM_SAMPLES],
40                   uniform float3 currentNormal,
41                   uniform float3 currentViewDir,
42                   uniform float noiseMultiplier,
43                   uniform float4 centerPosition,
44                   const uniform float4x4 modelViewProj,
45                   const float4 realPos
46                   )
47{
48        // the w coordinate from the persp. projection
49        const 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        float j = 0.0f;
58
59        for (int i = 0; i < NUM_SAMPLES; ++ i)
60        {
61                const float3 offset = samples[i];
62
63                ////////////////////
64                // add random noise: r stores costheta, g stores sintheta
65
66                float3 mynoise = (float3)tex2D(noiseTexture, IN.texCoord.xy);// * noiseMultiplier);
67                //const float3 offsetTransformed = myreflect(offset, mynoise) * AREA_SIZE;
68                const float3 offsetTransformed = offset * AREA_SIZE;
69
70                // compute position
71                const float4 offsetPos = float4(offsetTransformed + realPos.xyz, 1.0f);
72                const float4 projPos = mul(modelViewProj, offsetPos);
73                const float2 texcoord = projPos.xy / projPos.w;
74
75                if ((texcoord.x <= 1.0f) || (texcoord.x >= 0.0f) || (texcoord.y <= 1.0f) || (texcoord.y >= 0.0f))
76                {
77                        ++ j;
78                        // sample downsampled texture in order to speed up texture accesses
79                        float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
80                        //float3 sample_position = tex2D(positions, texcoord).xyz;
81
82                        float3 vector_to_sample = sample_position - centerPosition.xyz;
83                        const float length_to_sample = length(vector_to_sample);
84
85                        float3 direction_to_sample = vector_to_sample / length_to_sample;
86
87                        // angle between current normal and direction to sample controls AO intensity.
88                        const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
89
90                        // distance between current position and sample position controls AO intensity.
91                        const float distance_intensity =
92                                (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
93
94                        // if surface normal perpenticular to view dir, approx. half of the samples will not count
95                        // => compensate for this (on the other hand, projected sampling area could be larger!)
96                        //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
97                        //total_ao += cos_angle * distance_intensity * view_correction;
98
99                        total_ao += cos_angle * distance_intensity;
100                }
101        }
102
103        total_ao /= j;
104
105        return max(0.0f, 1.0f - total_ao);
106        //return saturate(dot(currentViewDir, currentNormal));
107}
108
109
110/** The mrt shader for screen space ambient occlusion
111*/
112pixel main(fragment IN,
113                   uniform sampler2D colors,
114                   uniform sampler2D positions,
115                   uniform sampler2D normals,
116                   uniform sampler2D noiseTexture,
117                   uniform float3 samples[NUM_SAMPLES],
118                   uniform float noiseMultiplier,
119                   uniform sampler2D oldTex,
120                   const uniform float4x4 oldModelViewProj,
121                   const uniform float4x4 mymodelViewProj,
122                   uniform float maxDepth,
123                   uniform float temporalCoherence
124                   )
125{
126        pixel OUT;
127
128        float4 norm = tex2D(normals, IN.texCoord.xy);
129       
130        // the ambient term
131        const float amb = norm.w;
132
133        // expand normal
134        float3 normal = normalize(norm.xyz);
135       
136        /// the current view direction
137        float3 viewDir;// = normalize(IN.view);
138
139        // the current world position
140        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
141       
142        // the current color
143        const float4 currentCol = tex2D(colors, IN.texCoord.xy);
144        const float currentDepth = currentCol.w;
145
146        float4 realPos = centerPosition * maxDepth;
147        realPos.w = 1.0f;
148
149        const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition, mymodelViewProj, realPos);
150        //const float ao = ssao(IN, positions, samples, normal, viewDir, centerPosition, mymodelViewProj, realPos);
151               
152
153        /////////////////
154        //-- compute temporally smoothing
155
156        // reproject
157        float4 oldPos = mul(oldModelViewProj, realPos);
158
159        const float newDepth = oldPos.z / oldPos.w;
160
161        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
162        float4 oldCol = tex2D(oldTex, tex);
163
164        const float oldDepth = oldCol.w;
165        const float depthDif = 1.0f - newDepth / oldDepth;
166
167        float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
168        float newWeight;
169
170        if ((temporalCoherence > 0) &&
171                (tex.x >= 0.0f) && (tex.x < 1.0f) &&
172                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
173                (abs(depthDif) < 1e-3f))
174        {
175                newWeight = oldWeight + 1;
176
177                //OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
178                OUT.illum_col = (float4)(ao + oldCol.x * oldWeight) / newWeight;
179        }
180        else
181        {
182                OUT.illum_col = (float4)ao;
183                newWeight = 0;
184        }
185
186
187        OUT.illum_col.z = newWeight;
188        OUT.illum_col.w = currentDepth;
189
190        return OUT;
191}
192
193
194pixel combine(fragment IN,
195                          uniform sampler2D colors,
196                          uniform sampler2D ssaoTex)
197{
198        pixel OUT;
199
200        float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
201        float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
202
203        //OUT.illum_col = col * ao.x;
204        OUT.illum_col = (float4)ao.x;
205
206        OUT.illum_col.w = ao.w;
207
208        return OUT;
209}
Note: See TracBrowser for help on using the repository browser.