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

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