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