[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,
|
---|
[2975] | 54 | uniform float4 centerPosition,
|
---|
| 55 | float w
|
---|
| 56 | //, uniform float3 currentViewDir
|
---|
[2882] | 57 | )
|
---|
| 58 | {
|
---|
[2904] | 59 | GiStruct gi;
|
---|
| 60 |
|
---|
[2882] | 61 | // Check in a circular area around the current position.
|
---|
| 62 | // Shoot vectors to the positions there, and check the angle to these positions.
|
---|
| 63 | // Summing up these angles gives an estimation of the occlusion at the current position.
|
---|
| 64 |
|
---|
| 65 | // ao is in stored in the w component
|
---|
[2904] | 66 | float3 total_color = float3(0, 0, 0);
|
---|
| 67 | float total_ao = 0.0f;
|
---|
| 68 | float numSamples = 0.0f;
|
---|
[2882] | 69 |
|
---|
| 70 | ////////////
|
---|
| 71 | //-- the main sampling loop
|
---|
| 72 |
|
---|
| 73 | for (int i = 0; i < NUM_SAMPLES; i ++)
|
---|
| 74 | {
|
---|
[2892] | 75 | float2 offset = samples[i];
|
---|
[2882] | 76 |
|
---|
[2903] | 77 | #if 1
|
---|
| 78 | ////////////////////
|
---|
| 79 | // add random noise: reflect around random normal vector (warning: slow!)
|
---|
| 80 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
[2892] | 81 | float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
[2903] | 82 | #else
|
---|
| 83 | float2 offsetTransformed = offset;
|
---|
| 84 | #endif
|
---|
[2882] | 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 |
|
---|
[2904] | 88 | if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
|
---|
| 89 | ++ numSamples;
|
---|
| 90 |
|
---|
[2882] | 91 | // use lower lod level to improve cache coherence
|
---|
[2964] | 92 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz;
|
---|
| 93 | float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, GI_MIPMAP_LEVEL)).xyz;
|
---|
[2882] | 94 |
|
---|
| 95 | float3 vector_to_sample = sample_position - centerPosition.xyz;
|
---|
| 96 | const float length_to_sample = length(vector_to_sample);
|
---|
| 97 |
|
---|
| 98 | float3 direction_to_sample = vector_to_sample / length_to_sample;
|
---|
| 99 |
|
---|
| 100 | // Angle between current normal and direction to sample controls AO intensity.
|
---|
| 101 | float cos_angle = max(dot(direction_to_sample, currentNormal), 0);
|
---|
| 102 |
|
---|
| 103 | // distance between current position and sample position controls AO intensity.
|
---|
| 104 | const float distance_intensity =
|
---|
| 105 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
|
---|
| 106 |
|
---|
| 107 | // if normal perpenticular to view dir, only half of the samples count
|
---|
[2903] | 108 | /*
|
---|
| 109 | const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
|
---|
| 110 | total_color.w -= cos_angle * distance_intensity * view_correction;
|
---|
| 111 | total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY;
|
---|
| 112 | */
|
---|
[2904] | 113 | total_ao += cos_angle * distance_intensity;
|
---|
| 114 | total_color += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY;
|
---|
[2882] | 115 | }
|
---|
| 116 |
|
---|
[2975] | 117 | gi.illum = total_color;
|
---|
[2904] | 118 | gi.ao = float2(max(0.0f, 1.0f - total_ao), numSamples);
|
---|
| 119 |
|
---|
| 120 | //return saturate(total_color);
|
---|
| 121 | return gi;
|
---|
[2882] | 122 | }
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | /** The mrt shader for screen space ambient occlusion + indirect illumination
|
---|
| 126 | */
|
---|
| 127 | pixel2 main(fragment IN,
|
---|
| 128 | uniform sampler2D colors,
|
---|
| 129 | uniform sampler2D positions,
|
---|
| 130 | uniform sampler2D normals,
|
---|
| 131 | uniform sampler2D noiseTexture,
|
---|
| 132 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 133 | uniform sampler2D oldSsaoTex,
|
---|
| 134 | uniform sampler2D oldIllumTex,
|
---|
| 135 | const uniform float4x4 oldModelViewProj,
|
---|
| 136 | uniform float maxDepth,
|
---|
[2897] | 137 | uniform float temporalCoherence
|
---|
[2882] | 138 | )
|
---|
| 139 | {
|
---|
| 140 | pixel2 OUT;
|
---|
| 141 |
|
---|
| 142 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
[2975] | 143 | float3 normal = normalize(norm.xyz);
|
---|
| 144 | // something like a constant ambient term
|
---|
[2882] | 145 | const float amb = norm.w;
|
---|
| 146 | /// the current view direction
|
---|
[2975] | 147 | //float3 viewDir = normalize(IN.view);
|
---|
[2882] | 148 |
|
---|
[2975] | 149 | // the w coordinate from the persp. projection
|
---|
| 150 | float w = norm.w;
|
---|
[2882] | 151 | // the current world position
|
---|
| 152 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
|
---|
| 153 | // the current color
|
---|
| 154 | const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
| 155 | // the current depth is stored in the w component
|
---|
[2975] | 156 | const float currentDepth = centerPosition.w;
|
---|
[2882] | 157 |
|
---|
[2975] | 158 | GiStruct gi = globIllum(IN, colors, positions, noiseTexture, samples, normal, centerPosition, w); //, viewDir);
|
---|
[2882] | 159 |
|
---|
| 160 |
|
---|
| 161 | /////////////////
|
---|
| 162 | //-- compute temporally smoothing
|
---|
| 163 |
|
---|
| 164 | float4 realPos = centerPosition * maxDepth;
|
---|
| 165 | realPos.w = 1.0f;
|
---|
| 166 |
|
---|
| 167 | float4 oldPos = mul(oldModelViewProj, realPos);
|
---|
| 168 |
|
---|
| 169 | const float newDepth = oldPos.z / oldPos.w;
|
---|
| 170 |
|
---|
| 171 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
|
---|
| 172 |
|
---|
| 173 | float4 oldSsao = tex2D(oldSsaoTex, tex);
|
---|
| 174 | float4 oldIllum = tex2D(oldIllumTex, tex);
|
---|
| 175 |
|
---|
| 176 | const float oldDepth = oldSsao.w;
|
---|
| 177 | const float depthDif = 1.0f - newDepth / oldDepth;
|
---|
| 178 |
|
---|
[2897] | 179 | float oldWeight = clamp(oldSsao.z, 0, temporalCoherence);
|
---|
| 180 | float newWeight;
|
---|
[2882] | 181 |
|
---|
[2904] | 182 | const float oldNumSamples = oldSsao.y;
|
---|
[2975] | 183 | const float oldAvgDepth = oldSsao.z;
|
---|
[2904] | 184 |
|
---|
[2897] | 185 | if ((temporalCoherence > 0.0f) &&
|
---|
| 186 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
[2882] | 187 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
[2975] | 188 | (abs(depthDif) < 1e-3f)
|
---|
| 189 | // check if something changed in the surrounding area
|
---|
| 190 | && (oldNumSamples > 0.2 * gi.ao.y)
|
---|
[2976] | 191 | //&& (oldAvgDepth / newAvgDepth > 0.99)
|
---|
[2904] | 192 | )
|
---|
[2882] | 193 | {
|
---|
[2897] | 194 | newWeight = oldWeight + 1;
|
---|
| 195 |
|
---|
[2904] | 196 | OUT.ssao_col.xy = (gi.ao + oldSsao.xy * oldWeight) / newWeight;
|
---|
| 197 | OUT.illum_col.xyz = (gi.illum + oldIllum.xyz * oldWeight) / newWeight;
|
---|
[2882] | 198 | }
|
---|
| 199 | else
|
---|
| 200 | {
|
---|
[2897] | 201 | newWeight = 0;
|
---|
| 202 |
|
---|
[2904] | 203 | OUT.ssao_col.xy = gi.ao.xy;
|
---|
| 204 | OUT.illum_col.xyz = gi.illum;
|
---|
[2882] | 205 | }
|
---|
| 206 |
|
---|
[2897] | 207 | OUT.ssao_col.z = newWeight;
|
---|
[2882] | 208 | OUT.ssao_col.w = currentDepth;
|
---|
| 209 |
|
---|
| 210 | return OUT;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
[2880] | 214 | pixel combine(fragment IN,
|
---|
| 215 | uniform sampler2D colors,
|
---|
| 216 | uniform sampler2D ssaoTex,
|
---|
| 217 | uniform sampler2D illumTex
|
---|
[2975] | 218 | )
|
---|
[2880] | 219 | {
|
---|
| 220 | pixel OUT;
|
---|
| 221 |
|
---|
| 222 | float4 col = tex2D(colors, IN.texCoord.xy);
|
---|
| 223 | float ao = tex2D(ssaoTex, IN.texCoord.xy).x;
|
---|
| 224 | float4 illum = tex2D(illumTex, IN.texCoord.xy);
|
---|
| 225 |
|
---|
[2882] | 226 | OUT.illum_col = (col + illum) * ao;
|
---|
[2884] | 227 | OUT.illum_col.w = col.w;
|
---|
[2880] | 228 |
|
---|
| 229 | return OUT;
|
---|
| 230 | }
|
---|