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

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