1 | ////////////////////
|
---|
2 | // SSAO + color bleeding shader
|
---|
3 | // based on shader of Alexander Kusternig
|
---|
4 |
|
---|
5 | #include "../shaderenv.h"
|
---|
6 |
|
---|
7 |
|
---|
8 | struct fragment
|
---|
9 | {
|
---|
10 | // normalized screen position
|
---|
11 | float4 pos: WPOS;
|
---|
12 | float4 texCoord: TEXCOORD0;
|
---|
13 | float3 view: COLOR0;
|
---|
14 | };
|
---|
15 |
|
---|
16 |
|
---|
17 | struct pixel2
|
---|
18 | {
|
---|
19 | float4 ssao_col: COLOR0;
|
---|
20 | float4 illum_col: COLOR1;
|
---|
21 | };
|
---|
22 |
|
---|
23 |
|
---|
24 | struct pixel
|
---|
25 | {
|
---|
26 | float4 illum_col: COLOR0;
|
---|
27 | };
|
---|
28 |
|
---|
29 |
|
---|
30 | float2 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 | struct GiStruct
|
---|
40 | {
|
---|
41 | float3 illum;
|
---|
42 | float2 ao;
|
---|
43 | };
|
---|
44 |
|
---|
45 |
|
---|
46 | /** Computes diffuse reflections + ambient occlusion
|
---|
47 | */
|
---|
48 | GiStruct globIllum(fragment IN,
|
---|
49 | uniform sampler2D colors,
|
---|
50 | uniform sampler2D positions,
|
---|
51 | uniform sampler2D noiseTexture,
|
---|
52 | uniform float2 samples[NUM_SAMPLES],
|
---|
53 | uniform float3 currentNormal,
|
---|
54 | uniform float3 currentViewDir,
|
---|
55 | uniform float4 centerPosition
|
---|
56 | )
|
---|
57 | {
|
---|
58 | GiStruct gi;
|
---|
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 | float3 total_color = float3(0, 0, 0);
|
---|
69 | float total_ao = 0.0f;
|
---|
70 | float numSamples = 0.0f;
|
---|
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 | #if 1
|
---|
80 | ////////////////////
|
---|
81 | // add random noise: reflect around random normal vector (warning: slow!)
|
---|
82 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
83 | float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
84 | #else
|
---|
85 | float2 offsetTransformed = offset;
|
---|
86 | #endif
|
---|
87 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
88 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
|
---|
89 |
|
---|
90 | if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
|
---|
91 | ++ numSamples;
|
---|
92 |
|
---|
93 | // use lower lod level to improve cache coherence
|
---|
94 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
|
---|
95 | float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 2)).xyz;
|
---|
96 |
|
---|
97 | float3 vector_to_sample = sample_position - centerPosition.xyz;
|
---|
98 | const float length_to_sample = length(vector_to_sample);
|
---|
99 |
|
---|
100 | float3 direction_to_sample = vector_to_sample / length_to_sample;
|
---|
101 |
|
---|
102 | // Angle between current normal and direction to sample controls AO intensity.
|
---|
103 | float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
|
---|
104 |
|
---|
105 | // distance between current position and sample position controls AO intensity.
|
---|
106 | const float distance_intensity =
|
---|
107 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
|
---|
108 |
|
---|
109 | // if normal perpenticular to view dir, only half of the samples count
|
---|
110 | /*
|
---|
111 | const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
|
---|
112 | total_color.w -= cos_angle * distance_intensity * view_correction;
|
---|
113 | total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
|
---|
114 | */
|
---|
115 | total_ao += cos_angle * distance_intensity;
|
---|
116 | total_color += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
|
---|
117 | }
|
---|
118 |
|
---|
119 | gi.illum = saturate(total_color);
|
---|
120 | gi.ao = float2(max(0.0f, 1.0f - total_ao), numSamples);
|
---|
121 |
|
---|
122 | //return saturate(total_color);
|
---|
123 | return gi;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /** The mrt shader for screen space ambient occlusion + indirect illumination
|
---|
128 | */
|
---|
129 | pixel2 main(fragment IN,
|
---|
130 | uniform sampler2D colors,
|
---|
131 | uniform sampler2D positions,
|
---|
132 | uniform sampler2D normals,
|
---|
133 | uniform sampler2D noiseTexture,
|
---|
134 | uniform float2 samples[NUM_SAMPLES],
|
---|
135 | uniform sampler2D oldSsaoTex,
|
---|
136 | uniform sampler2D oldIllumTex,
|
---|
137 | const uniform float4x4 oldModelViewProj,
|
---|
138 | uniform float maxDepth,
|
---|
139 | uniform float temporalCoherence
|
---|
140 | )
|
---|
141 | {
|
---|
142 | pixel2 OUT;
|
---|
143 |
|
---|
144 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
145 |
|
---|
146 | // the ambient term
|
---|
147 | const float amb = norm.w;
|
---|
148 |
|
---|
149 | // expand normal
|
---|
150 | float3 normal = normalize(norm.xyz);
|
---|
151 |
|
---|
152 | /// the current view direction
|
---|
153 | float3 viewDir;// = normalize(IN.view);
|
---|
154 |
|
---|
155 | // the current world position
|
---|
156 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
|
---|
157 |
|
---|
158 | // the current color
|
---|
159 | const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
160 | // the current depth is stored in the w component
|
---|
161 | const float currentDepth = currentCol.w;
|
---|
162 |
|
---|
163 | GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, centerPosition);
|
---|
164 |
|
---|
165 |
|
---|
166 | /////////////////
|
---|
167 | //-- compute temporally smoothing
|
---|
168 |
|
---|
169 | float4 realPos = centerPosition * maxDepth;
|
---|
170 | realPos.w = 1.0f;
|
---|
171 |
|
---|
172 | float4 oldPos = mul(oldModelViewProj, realPos);
|
---|
173 |
|
---|
174 | const float newDepth = oldPos.z / oldPos.w;
|
---|
175 |
|
---|
176 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
|
---|
177 |
|
---|
178 | float4 oldSsao = tex2D(oldSsaoTex, tex);
|
---|
179 | float4 oldIllum = tex2D(oldIllumTex, tex);
|
---|
180 |
|
---|
181 | const float oldDepth = oldSsao.w;
|
---|
182 | const float depthDif = 1.0f - newDepth / oldDepth;
|
---|
183 |
|
---|
184 | float oldWeight = clamp(oldSsao.z, 0, temporalCoherence);
|
---|
185 | float newWeight;
|
---|
186 |
|
---|
187 | const float oldNumSamples = oldSsao.y;
|
---|
188 |
|
---|
189 | if ((temporalCoherence > 0.0f) &&
|
---|
190 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
191 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
192 | (abs(depthDif) < 1e-3f)
|
---|
193 | && (oldNumSamples > gi.ao.y - 1.5f) // check if something happened in the surrounding area
|
---|
194 | )
|
---|
195 | {
|
---|
196 | newWeight = oldWeight + 1;
|
---|
197 |
|
---|
198 | OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight;
|
---|
199 | OUT.illum_col.xyz = (gi.illum + oldIllum.xyz * oldWeight) / newWeight;
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | newWeight = 0;
|
---|
204 |
|
---|
205 | OUT.ssao_col.xy = gi.ao.xy;
|
---|
206 | OUT.illum_col.xyz = gi.illum;
|
---|
207 | }
|
---|
208 |
|
---|
209 | OUT.ssao_col.z = newWeight;
|
---|
210 | OUT.ssao_col.w = currentDepth;
|
---|
211 |
|
---|
212 | return OUT;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | pixel combine(fragment IN,
|
---|
217 | uniform sampler2D colors,
|
---|
218 | uniform sampler2D ssaoTex,
|
---|
219 | uniform sampler2D illumTex
|
---|
220 | )
|
---|
221 | {
|
---|
222 | pixel OUT;
|
---|
223 |
|
---|
224 | float4 col = tex2D(colors, IN.texCoord.xy);
|
---|
225 | float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
|
---|
226 | float4 illum = tex2D(illumTex, IN.texCoord.xy);
|
---|
227 |
|
---|
228 | OUT.illum_col = (col + illum) * ao;
|
---|
229 | //OUT.illum_col = ao;
|
---|
230 |
|
---|
231 | OUT.illum_col.w = col.w;
|
---|
232 |
|
---|
233 | return OUT;
|
---|
234 | }
|
---|