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

Revision 2892, 5.7 KB checked in by mattausch, 16 years ago (diff)
Line 
1////////////////////
2// SSAO + color bleeding shader
3// based on shader of Alexander Kusternig
4
5#include "../shaderenv.h"
6
7
8struct fragment
9{
10         // normalized screen position
11        float4 pos: WPOS;
12        float4 texCoord: TEXCOORD0;
13        float3 view: COLOR0;
14};
15
16
17struct pixel2
18{
19        float4 ssao_col: COLOR0;
20        float4 illum_col: COLOR1;
21};
22
23
24struct pixel
25{
26        float4 illum_col: COLOR0;
27};
28
29
30float2 myreflect(float2 pt, float2 n)
31{
32        // distance to plane
33        float d = dot(n, pt);
34        // reflect around plane
35        float2 rpt = pt - d * 2.0f * n;
36        return rpt;
37}
38
39/** Computes  diffuse reflections + ambient occlusion
40*/
41float4 globIllum(fragment IN,
42                                 uniform sampler2D colors,
43                                 uniform sampler2D positions,
44                                 uniform sampler2D noiseTexture,
45                                 uniform float2 samples[NUM_SAMPLES],
46                                 uniform float3 currentNormal,
47                                 uniform float3 currentViewDir,
48                                 uniform float noiseMultiplier,
49                                 uniform float4 centerPosition
50                                 )
51{
52        // the w coordinate from the persp. projection
53        float w = centerPosition.w;
54
55        // Check in a circular area around the current position.
56        // Shoot vectors to the positions there, and check the angle to these positions.
57        // Summing up these angles gives an estimation of the occlusion at the current position.
58
59        // ao is in stored in the w component
60        float4 total_color = float4(0, 0, 0, 1);
61
62
63        ////////////
64        //-- the main sampling loop
65
66        for (int i = 0; i < NUM_SAMPLES; i ++)
67        {
68                float2 offset = samples[i];
69                //float3 offset = float3(samples[i], 0);
70
71                //sample noisetex; r stores costheta, g stores sintheta
72                float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
73                //float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy, 0);
74
75                // reflect sampling using the noise texture
76                float2 offsetTransformed = myreflect(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                // use lower lod level to improve cache coherence
82                float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
83                float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 2)).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                float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
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 normal perpenticular to view dir, only half of the samples count
98                //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
99                //total_color.w -= cos_angle * distance_intensity * view_correction;
100                //total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
101
102                total_color.w -= cos_angle * distance_intensity;
103                total_color.xyz += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
104        }
105
106        return saturate(total_color);
107}
108
109
110/** The mrt shader for screen space ambient occlusion + indirect illumination
111*/
112pixel2 main(fragment IN,
113                   uniform sampler2D colors,
114                   uniform sampler2D positions,
115                   uniform sampler2D normals,
116                   uniform sampler2D noiseTexture,
117                   uniform float2 samples[NUM_SAMPLES],
118                   uniform float noiseMultiplier,
119                   uniform sampler2D oldSsaoTex,
120                   uniform sampler2D oldIllumTex,
121                   const uniform float4x4 oldModelViewProj,
122                   uniform float maxDepth,
123                   uniform float expFactor
124                   )
125{
126        pixel2 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 = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
144        // the current depth is stored in the w component
145        const float currentDepth = currentCol.w;
146
147        float4 new_color = globIllum(IN, colors, positions, noiseTexture,
148                                 samples, normal, viewDir, noiseMultiplier, centerPosition);
149       
150
151        /////////////////
152        //-- compute temporally smoothing
153
154        float4 realPos = centerPosition * maxDepth;
155        realPos.w = 1.0f;
156
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
163        float4 oldSsao = tex2D(oldSsaoTex, tex);
164        float4 oldIllum = tex2D(oldIllumTex, tex);
165
166        const float oldDepth = oldSsao.w;
167        const float depthDif = 1.0f - newDepth / oldDepth;
168
169
170        if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
171                (tex.y >= 0.0f) && (tex.y < 1.0f) &&
172                (abs(depthDif)  < 1e-3f))
173        {
174                OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor);
175                OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor);
176        }
177        else
178        {
179                OUT.ssao_col.x = new_color.w;
180                OUT.illum_col = new_color;
181        }
182
183        OUT.ssao_col.w = currentDepth;
184
185        return OUT;
186}
187
188
189pixel combine(fragment IN,
190                          uniform sampler2D colors,
191                          uniform sampler2D ssaoTex,
192                          uniform sampler2D illumTex
193                   )
194{
195        pixel OUT;
196
197        float4 col = tex2D(colors, IN.texCoord.xy);
198        float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
199        float4 illum = tex2D(illumTex, IN.texCoord.xy);
200       
201        OUT.illum_col = (col + illum) * ao;
202        OUT.illum_col.w = col.w;
203
204        return OUT;
205}
Note: See TracBrowser for help on using the repository browser.