source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/shaders/globillum.cg @ 2880

Revision 2880, 5.4 KB checked in by mattausch, 16 years ago (diff)

downsampling not working yet

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#define ILLUM_SCALE 5e-1f;
19
20
21struct fragment
22{
23         // normalized screen position
24        float4 pos: WPOS;
25        float4 texCoord: TEXCOORD0;
26        float3 view: COLOR0;
27};
28
29
30
31struct pixel
32{
33        float4 ssao_col: COLOR0;
34        float4 illum_col: COLOR1;
35};
36
37
38float2 reflect(float2 pt, float2 n)
39{
40        // distance to plane
41        float d = dot(n, pt);
42        // reflect around plane
43        float2 rpt = pt - d * 2.0f * n;
44        return rpt;
45}
46
47/** Computes  diffuse reflections + ambient occlusion
48*/
49float4 globIllum(fragment IN,
50                                 uniform sampler2D colors,
51                                 uniform sampler2D positions,
52                                 uniform sampler2D noiseTexture,
53                                 uniform float2 samples[NUM_SAMPLES],
54                                 uniform float3 currentNormal,
55                                 uniform float3 currentViewDir,
56                                 uniform float noiseMultiplier,
57                                 uniform float4 centerPosition
58                                 )
59{
60        // the w coordinate from the persp. projection
61        float w = centerPosition.w;
62
63        // Check in a circular area around the current position.
64        // Shoot vectors to the positions there, and check the angle to these positions.
65        // Summing up these angles gives an estimation of the occlusion at the current position.
66
67        // ao is in stored in the w component
68        float4 total_color = float4(0, 0, 0, 1);
69
70
71        ////////////
72        //-- the main sampling loop
73
74        for (int i = 0; i < NUM_SAMPLES; i ++)
75        {
76                float2 offset = samples[i];
77
78                //sample noisetex; r stores costheta, g stores sintheta
79                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
80
81                // rotation
82                float2 offsetTransformed = reflect(offset, mynoise);
83
84                // weight with projected coordinate to reach similar kernel size for near and far
85                float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
86
87                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
88                float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 2)).xyz;
89
90                float3 vector_to_sample = sample_position - centerPosition.xyz;
91                const float length_to_sample = length(vector_to_sample);
92
93                float3 direction_to_sample = vector_to_sample / length_to_sample;
94
95                // Angle between current normal and direction to sample controls AO intensity.
96                float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
97
98                // distance between current position and sample position controls AO intensity.
99                const float distance_intensity =
100                        (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
101
102                // if normal perpenticular to view dir, only half of the samples count
103                const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
104
105                total_color.w -= cos_angle * distance_intensity * view_correction;
106
107                total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_SCALE;
108        }
109
110        return saturate(total_color);
111}
112
113
114/** The mrt shader for screen space ambient occlusion + indirect illumination
115*/
116pixel main(fragment IN,
117                   uniform sampler2D colors,
118                   uniform sampler2D positions,
119                   uniform sampler2D normals,
120                   uniform sampler2D noiseTexture,
121                   uniform float2 samples[NUM_SAMPLES],
122                   uniform float noiseMultiplier,
123                   uniform sampler2D oldSsaoTex,
124                   uniform sampler2D oldIllumTex,
125                   const uniform float4x4 oldModelViewProj,
126                   uniform float maxDepth,
127                   uniform float expFactor
128                   )
129{
130        pixel OUT;
131
132        float4 norm = tex2D(normals, IN.texCoord.xy);
133       
134        // the ambient term
135        const float amb = norm.w;
136
137        // expand normal
138        float3 normal = normalize(norm.xyz);
139        /// the current view direction
140        float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
141
142        // the current world position
143        const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
144       
145        // the current color
146        const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
147        // the current depth is stored in the w component
148        const float currentDepth = currentCol.w;
149
150        float4 new_color = globIllum(IN, colors, positions, noiseTexture,
151                                 samples, normal, viewDir, noiseMultiplier, centerPosition);
152       
153
154        /////////////////
155        //-- compute temporally smoothing
156
157        float4 realPos = centerPosition * maxDepth;
158        realPos.w = 1.0f;
159
160        float4 oldPos = mul(oldModelViewProj, realPos);
161
162        const float newDepth = oldPos.z / oldPos.w;
163
164        float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
165
166        float4 oldSsao = tex2D(oldSsaoTex, tex);
167        float4 oldIllum = tex2D(oldIllumTex, tex);
168
169        const float oldDepth = oldSsao.w;
170        const float depthDif = 1.0f - newDepth / oldDepth;
171
172
173        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
174                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
175                (abs(depthDif)  < 1e-3f))
176        {
177                OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
178                OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
179        }
180        else
181        {
182                OUT.ssao_col.x = new_color.w;
183                OUT.illum_col = new_color;
184        }
185
186        OUT.ssao_col.w = currentDepth;
187
188        return OUT;
189}
190
191
192pixel combine(fragment IN,
193                          uniform sampler2D colors,
194                          uniform sampler2D ssaoTex,
195                          uniform sampler2D illumTex
196                   )
197{
198        pixel OUT;
199
200        float4 col = tex2D(colors, IN.texCoord.xy);
201        float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
202        float4 illum = tex2D(illumTex, IN.texCoord.xy);
203       
204        OUT.illum_col = (col + illum) * ao;
205
206        return OUT;
207}
Note: See TracBrowser for help on using the repository browser.