[2882] | 1 | ////////////////////
|
---|
[2884] | 2 | // SSAO + color bleeding shader
|
---|
[2882] | 3 | // based on shader of Alexander Kusternig
|
---|
| 4 |
|
---|
[2884] | 5 | #include "../shaderenv.h"
|
---|
[2882] | 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 |
|
---|
[2891] | 30 | float2 myreflect(float2 pt, float2 n)
|
---|
[2882] | 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 |
|
---|
[2904] | 39 | struct GiStruct
|
---|
| 40 | {
|
---|
| 41 | float3 illum;
|
---|
| 42 | float2 ao;
|
---|
| 43 | };
|
---|
| 44 |
|
---|
| 45 |
|
---|
[2882] | 46 | /** Computes diffuse reflections + ambient occlusion
|
---|
| 47 | */
|
---|
[2904] | 48 | GiStruct globIllum(fragment IN,
|
---|
[2882] | 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 | {
|
---|
[2904] | 58 | GiStruct gi;
|
---|
| 59 |
|
---|
[2882] | 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
|
---|
[2904] | 68 | float3 total_color = float3(0, 0, 0);
|
---|
| 69 | float total_ao = 0.0f;
|
---|
| 70 | float numSamples = 0.0f;
|
---|
[2882] | 71 |
|
---|
| 72 | ////////////
|
---|
| 73 | //-- the main sampling loop
|
---|
| 74 |
|
---|
| 75 | for (int i = 0; i < NUM_SAMPLES; i ++)
|
---|
| 76 | {
|
---|
[2892] | 77 | float2 offset = samples[i];
|
---|
[2882] | 78 |
|
---|
[2903] | 79 | #if 1
|
---|
| 80 | ////////////////////
|
---|
| 81 | // add random noise: reflect around random normal vector (warning: slow!)
|
---|
| 82 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
[2892] | 83 | float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
[2903] | 84 | #else
|
---|
| 85 | float2 offsetTransformed = offset;
|
---|
| 86 | #endif
|
---|
[2882] | 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 |
|
---|
[2904] | 90 | if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
|
---|
| 91 | ++ numSamples;
|
---|
| 92 |
|
---|
[2882] | 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
|
---|
[2903] | 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 | */
|
---|
[2904] | 115 | total_ao += cos_angle * distance_intensity;
|
---|
| 116 | total_color += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
|
---|
[2882] | 117 | }
|
---|
| 118 |
|
---|
[2904] | 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;
|
---|
[2882] | 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,
|
---|
[2897] | 139 | uniform float temporalCoherence
|
---|
[2882] | 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);
|
---|
[2891] | 151 |
|
---|
[2882] | 152 | /// the current view direction
|
---|
[2891] | 153 | float3 viewDir;// = normalize(IN.view);
|
---|
[2882] | 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 |
|
---|
[2904] | 163 | GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, centerPosition);
|
---|
[2882] | 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 |
|
---|
[2897] | 184 | float oldWeight = clamp(oldSsao.z, 0, temporalCoherence);
|
---|
| 185 | float newWeight;
|
---|
[2882] | 186 |
|
---|
[2904] | 187 | const float oldNumSamples = oldSsao.y;
|
---|
| 188 |
|
---|
[2897] | 189 | if ((temporalCoherence > 0.0f) &&
|
---|
| 190 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
[2882] | 191 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
[2904] | 192 | (abs(depthDif) < 1e-3f)
|
---|
| 193 | && (oldNumSamples > gi.ao.y - 1.5f) // check if something happened in the surrounding area
|
---|
| 194 | )
|
---|
[2882] | 195 | {
|
---|
[2897] | 196 | newWeight = oldWeight + 1;
|
---|
| 197 |
|
---|
[2904] | 198 | OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight;
|
---|
| 199 | OUT.illum_col.xyz = (gi.illum + oldIllum.xyz * oldWeight) / newWeight;
|
---|
[2882] | 200 | }
|
---|
| 201 | else
|
---|
| 202 | {
|
---|
[2897] | 203 | newWeight = 0;
|
---|
| 204 |
|
---|
[2904] | 205 | OUT.ssao_col.xy = gi.ao.xy;
|
---|
| 206 | OUT.illum_col.xyz = gi.illum;
|
---|
[2882] | 207 | }
|
---|
| 208 |
|
---|
[2897] | 209 | OUT.ssao_col.z = newWeight;
|
---|
[2882] | 210 | OUT.ssao_col.w = currentDepth;
|
---|
| 211 |
|
---|
| 212 | return OUT;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 |
|
---|
[2880] | 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 |
|
---|
[2882] | 228 | OUT.illum_col = (col + illum) * ao;
|
---|
[2904] | 229 | //OUT.illum_col = ao;
|
---|
| 230 |
|
---|
[2884] | 231 | OUT.illum_col.w = col.w;
|
---|
[2880] | 232 |
|
---|
| 233 | return OUT;
|
---|
| 234 | }
|
---|