[2868] | 1 | //////////////////// |
---|
| 2 | // Screen Spaced Ambient Occlusion shader |
---|
| 3 | // based on shader of Alexander Kusternig |
---|
| 4 | |
---|
| 5 | //#define NUM_SAMPLES 8 |
---|
| 6 | #define NUM_SAMPLES 16 |
---|
| 7 | |
---|
| 8 | // rule of thumb: approx 1 / NUM_SAMPLES |
---|
| 9 | #define SAMPLE_INTENSITY 0.15 |
---|
| 10 | //#define SAMPLE_INTENSITY 0.125f |
---|
| 11 | |
---|
| 12 | #define AREA_SIZE 9e-1f |
---|
| 13 | //#define VIEW_CORRECTION_SCALE 0.3f |
---|
| 14 | #define VIEW_CORRECTION_SCALE 0.5f |
---|
| 15 | #define DISTANCE_SCALE 1e-6f |
---|
| 16 | |
---|
| 17 | |
---|
[2809] | 18 | struct fragment |
---|
| 19 | { |
---|
[2868] | 20 | // normalized screen position |
---|
| 21 | float4 pos: WPOS; |
---|
| 22 | float4 texCoord: TEXCOORD0; |
---|
| 23 | float3 view: COLOR0; |
---|
[2809] | 24 | }; |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | struct pixel |
---|
| 28 | { |
---|
[2868] | 29 | float4 color: COLOR0; |
---|
[2809] | 30 | }; |
---|
| 31 | |
---|
| 32 | |
---|
[2868] | 33 | float2 reflect(float2 pt, float2 n) |
---|
[2809] | 34 | { |
---|
| 35 | // distance to plane |
---|
| 36 | float d = dot(n, pt); |
---|
| 37 | // reflect around plane |
---|
[2868] | 38 | float2 rpt = pt - d * 2.0f * n; |
---|
| 39 | |
---|
[2809] | 40 | return rpt; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
[2868] | 44 | float2 rotate(float2 pt, float2 n) |
---|
| 45 | { |
---|
| 46 | float2 ptTransformed; |
---|
| 47 | ptTransformed.x = n.r * pt.x - n.g * pt.y; |
---|
| 48 | ptTransformed.y = n.g * pt.x + n.r * pt.y; |
---|
| 49 | |
---|
| 50 | return ptTransformed; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | /** The ssao shader returning the an intensity value between 0 and 1 |
---|
| 55 | */ |
---|
| 56 | float ssao(fragment IN, |
---|
| 57 | uniform sampler2D positions, |
---|
| 58 | uniform sampler2D noiseTexture, |
---|
| 59 | uniform float2 samples[NUM_SAMPLES], |
---|
| 60 | uniform float3 currentNormal, |
---|
| 61 | uniform float3 currentViewDir, |
---|
| 62 | uniform float noiseMultiplier, |
---|
| 63 | uniform float4 centerPosition |
---|
| 64 | ) |
---|
| 65 | { |
---|
| 66 | // the w coordinate from the persp. projection |
---|
| 67 | float w = centerPosition.w; |
---|
| 68 | |
---|
| 69 | // Check in a circular area around the current position. |
---|
| 70 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
| 71 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
| 72 | |
---|
| 73 | float total_ao = 0.0; |
---|
| 74 | |
---|
| 75 | const float areaSize = 5e-1f; |
---|
| 76 | |
---|
| 77 | for (int i = 0; i < NUM_SAMPLES; i ++) |
---|
| 78 | { |
---|
| 79 | float2 offset = samples[i]; |
---|
| 80 | |
---|
| 81 | //sample noisetex; r stores costheta, g stores sintheta |
---|
| 82 | //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; |
---|
| 83 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; |
---|
| 84 | |
---|
| 85 | // rotation |
---|
| 86 | //float2 offsetTransformed = offset; |
---|
| 87 | //float2 offsetTransformed = rotate(offset, mynoise); |
---|
| 88 | float2 offsetTransformed = reflect(offset, mynoise); |
---|
| 89 | |
---|
| 90 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
| 91 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
| 92 | |
---|
| 93 | // sample downsampled texture in order to speed up texture accesses |
---|
| 94 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; |
---|
| 95 | //float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
| 96 | |
---|
| 97 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
| 98 | float length_to_sample = length(vector_to_sample); |
---|
| 99 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
| 100 | |
---|
| 101 | // Angle between current normal and direction to sample controls AO intensity. |
---|
| 102 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
| 103 | cos_angle = max(cos_angle, 0.0f); |
---|
| 104 | |
---|
| 105 | // distance between current position and sample position controls AO intensity. |
---|
| 106 | float distance_intensity = |
---|
| 107 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); |
---|
| 108 | |
---|
| 109 | // if surface normal perpenticular to view dir, some samples probably count less |
---|
| 110 | // => compensate for this |
---|
| 111 | float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); |
---|
| 112 | |
---|
| 113 | total_ao += cos_angle * distance_intensity * view_correction; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | return (1.0f - total_ao); |
---|
| 117 | //return dot(currentViewDir, currentNormal); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | |
---|
| 121 | /** Computes ambient occlusion + diffuse reflections |
---|
| 122 | */ |
---|
| 123 | float4 globIllum(fragment IN, |
---|
| 124 | uniform sampler2D colors, |
---|
| 125 | uniform sampler2D positions, |
---|
| 126 | uniform sampler2D noiseTexture, |
---|
| 127 | uniform float2 samples[NUM_SAMPLES], |
---|
| 128 | uniform float3 currentNormal, |
---|
| 129 | uniform float3 currentViewDir, |
---|
| 130 | uniform float noiseMultiplier, |
---|
| 131 | uniform float4 centerPosition |
---|
| 132 | ) |
---|
| 133 | { |
---|
| 134 | // the w coordinate from the persp. projection |
---|
| 135 | float w = centerPosition.w; |
---|
| 136 | |
---|
| 137 | // Check in a circular area around the current position. |
---|
| 138 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
| 139 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
| 140 | |
---|
| 141 | float total_ao = 0.0; |
---|
| 142 | float3 total_color = float3(0.0f); |
---|
| 143 | |
---|
| 144 | const float areaSize = 5e-1f; |
---|
| 145 | |
---|
| 146 | for (int i = 0; i < NUM_SAMPLES; i ++) |
---|
| 147 | { |
---|
| 148 | float2 offset = samples[i]; |
---|
| 149 | |
---|
| 150 | //sample noisetex; r stores costheta, g stores sintheta |
---|
| 151 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; |
---|
| 152 | |
---|
| 153 | // rotation |
---|
| 154 | float2 offsetTransformed = rotate(offset, mynoise); |
---|
| 155 | |
---|
| 156 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
| 157 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
| 158 | |
---|
| 159 | float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
| 160 | float3 sample_color = tex2D(colors, texcoord).xyz; |
---|
| 161 | |
---|
| 162 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
| 163 | float length_to_sample = length(vector_to_sample); |
---|
| 164 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
| 165 | |
---|
| 166 | // Angle between current normal and direction to sample controls AO intensity. |
---|
| 167 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
| 168 | cos_angle = max(cos_angle, 0.0f); |
---|
| 169 | |
---|
| 170 | // distance between current position and sample position controls AO intensity. |
---|
| 171 | float distance_intensity = |
---|
| 172 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); |
---|
| 173 | |
---|
| 174 | // if normal perpenticular to view dir, only half of the samples count |
---|
| 175 | float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); |
---|
| 176 | |
---|
| 177 | total_ao += cos_angle * distance_intensity * view_correction; |
---|
| 178 | total_color += cos_angle * distance_intensity * view_correction * sample_color * 0.3f; |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | return float4(total_color, 1.0f - total_ao); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | |
---|
| 185 | /** The mrt shader for screen space ambient occlusion |
---|
| 186 | */ |
---|
[2809] | 187 | pixel main(fragment IN, |
---|
[2868] | 188 | uniform sampler2D colors, |
---|
| 189 | uniform sampler2D positions, |
---|
| 190 | uniform sampler2D normals, |
---|
| 191 | uniform sampler2D noiseTexture, |
---|
| 192 | uniform float2 samples[NUM_SAMPLES], |
---|
| 193 | uniform float noiseMultiplier, |
---|
| 194 | uniform sampler2D oldTex, |
---|
| 195 | const uniform float4x4 oldModelViewProj, |
---|
| 196 | uniform float maxDepth, |
---|
| 197 | uniform float expFactor |
---|
| 198 | ) |
---|
[2809] | 199 | { |
---|
[2868] | 200 | pixel OUT; |
---|
[2809] | 201 | |
---|
[2868] | 202 | float4 normal = tex2D(normals, IN.texCoord.xy); |
---|
| 203 | |
---|
| 204 | // the ambient term |
---|
| 205 | float amb = normal.w; |
---|
[2809] | 206 | |
---|
[2868] | 207 | // expand normal |
---|
| 208 | normal = normalize(normal);// * 2.0f - 1.0f); |
---|
| 209 | /// the current view direction |
---|
| 210 | float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); |
---|
[2809] | 211 | |
---|
[2868] | 212 | // the current world position |
---|
| 213 | float4 centerPosition = tex2D(positions, IN.texCoord.xy); |
---|
| 214 | |
---|
[2870] | 215 | // the current color |
---|
| 216 | float4 currentCol = tex2D(colors, IN.texCoord.xy); |
---|
| 217 | float currentDepth = currentCol.w; |
---|
[2809] | 218 | |
---|
[2868] | 219 | float ao = ssao(IN, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); |
---|
[2809] | 220 | |
---|
[2868] | 221 | //float4 new_col = globIllum(IN, colors, positions, noiseTexture, samples, normal.xyz, viewDir, noiseMultiplier, centerPosition); |
---|
| 222 | |
---|
[2869] | 223 | // compute temporally smoothed color |
---|
[2870] | 224 | float4 realPos = centerPosition * maxDepth; |
---|
| 225 | realPos.w = 1.0f; |
---|
[2809] | 226 | |
---|
[2870] | 227 | float4 oldPos = mul(oldModelViewProj, realPos); |
---|
| 228 | |
---|
| 229 | float newDepth = oldPos.z / oldPos.w; |
---|
| 230 | |
---|
| 231 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; |
---|
| 232 | float4 oldCol = tex2D(oldTex, tex); |
---|
| 233 | |
---|
| 234 | float oldDepth = oldCol.w; |
---|
| 235 | float depthDif = 1.0f - newDepth / oldDepth; |
---|
| 236 | |
---|
| 237 | if ((tex.x >= 0.0f) && (tex.x < 1.0f) && |
---|
| 238 | (tex.y >= 0.0f) && (tex.y < 1.0f) && |
---|
| 239 | (abs(depthDif) < 1e-4f)) |
---|
| 240 | { |
---|
| 241 | OUT.color = float4(ao * expFactor + oldCol * float4(1.0f - expFactor)); |
---|
| 242 | } |
---|
| 243 | else |
---|
| 244 | { |
---|
| 245 | OUT.color = (float4)ao; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | |
---|
[2869] | 249 | //OUT.color.xyz = viewDir; |
---|
| 250 | //OUT.color = attenuated_color; |
---|
| 251 | |
---|
| 252 | OUT.color.w = currentDepth; |
---|
[2809] | 253 | |
---|
[2869] | 254 | return OUT; |
---|
| 255 | } |
---|
[2809] | 256 | |
---|
| 257 | |
---|
[2869] | 258 | pixel combined(fragment IN, |
---|
| 259 | uniform sampler2D colors, |
---|
| 260 | uniform sampler2D ssaoTex |
---|
| 261 | ) |
---|
| 262 | { |
---|
| 263 | pixel OUT; |
---|
[2868] | 264 | |
---|
[2869] | 265 | float4 col = tex2D(colors, IN.texCoord.xy); |
---|
| 266 | float4 ao = tex2D(ssaoTex, IN.texCoord.xy); |
---|
[2868] | 267 | |
---|
[2869] | 268 | OUT.color = float4(1,0,0,0); |
---|
| 269 | OUT.color = col * ao; |
---|
[2868] | 270 | |
---|
| 271 | return OUT; |
---|
| 272 | } |
---|