[2884] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
[2881] | 3 | ////////////////////
|
---|
| 4 | // Screen Spaced Ambient Occlusion shader
|
---|
| 5 | // based on shader of Alexander Kusternig
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 | struct fragment
|
---|
| 9 | {
|
---|
| 10 | // normalized screen position
|
---|
| 11 | float4 pos: WPOS;
|
---|
[2889] | 12 | float2 texCoord: TEXCOORD0;
|
---|
| 13 | float3 view: TEXCOORD1;
|
---|
[2881] | 14 | };
|
---|
| 15 |
|
---|
| 16 |
|
---|
| 17 | struct pixel
|
---|
| 18 | {
|
---|
| 19 | float4 illum_col: COLOR0;
|
---|
| 20 | };
|
---|
| 21 |
|
---|
| 22 |
|
---|
[2891] | 23 | float2 myreflect(float2 pt, float2 n)
|
---|
[2881] | 24 | {
|
---|
| 25 | // distance to plane
|
---|
| 26 | float d = dot(n, pt);
|
---|
| 27 | // reflect around plane
|
---|
| 28 | float2 rpt = pt - d * 2.0f * n;
|
---|
[2886] | 29 |
|
---|
[2881] | 30 | return rpt;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 |
|
---|
| 35 | /** The ssao shader returning the an intensity value between 0 and 1
|
---|
| 36 | */
|
---|
| 37 | float ssao(fragment IN,
|
---|
| 38 | uniform sampler2D positions,
|
---|
| 39 | uniform sampler2D noiseTexture,
|
---|
| 40 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 41 | uniform float3 currentNormal,
|
---|
| 42 | uniform float3 currentViewDir,
|
---|
| 43 | uniform float noiseMultiplier,
|
---|
| 44 | uniform float4 centerPosition
|
---|
| 45 | )
|
---|
| 46 | {
|
---|
| 47 | // the w coordinate from the persp. projection
|
---|
| 48 | float w = centerPosition.w;
|
---|
| 49 |
|
---|
| 50 | // Check in a circular area around the current position.
|
---|
| 51 | // Shoot vectors to the positions there, and check the angle to these positions.
|
---|
| 52 | // Summing up these angles gives an estimation of the occlusion at the current position.
|
---|
| 53 |
|
---|
| 54 | float total_ao = 0.0;
|
---|
| 55 |
|
---|
| 56 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 57 | {
|
---|
[2892] | 58 | const float2 offset = samples[i];
|
---|
| 59 | //const float3 offset = float3(samples[i], 0);
|
---|
[2881] | 60 |
|
---|
| 61 | ////////////////////
|
---|
| 62 | // add random noise: r stores costheta, g stores sintheta
|
---|
| 63 |
|
---|
[2884] | 64 | //const float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
|
---|
[2892] | 65 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
|
---|
| 66 | //float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy,0);
|
---|
[2881] | 67 |
|
---|
[2892] | 68 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
[2881] | 69 |
|
---|
| 70 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
| 71 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
|
---|
| 72 |
|
---|
[2893] | 73 | //if ((texcoord.x <= 1.0f) || (texcoord.x >= 0.0f) || (texcoord.y <= 1.0f) || (texcoord.y >= 0.0f))
|
---|
| 74 |
|
---|
[2881] | 75 | // sample downsampled texture in order to speed up texture accesses
|
---|
| 76 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
|
---|
| 77 | //float3 sample_position = tex2D(positions, texcoord).xyz;
|
---|
| 78 |
|
---|
| 79 | float3 vector_to_sample = sample_position - centerPosition.xyz;
|
---|
| 80 | const float length_to_sample = length(vector_to_sample);
|
---|
| 81 |
|
---|
| 82 | float3 direction_to_sample = vector_to_sample / length_to_sample;
|
---|
| 83 |
|
---|
[2885] | 84 | // angle between current normal and direction to sample controls AO intensity.
|
---|
[2881] | 85 | const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
|
---|
| 86 |
|
---|
| 87 | // distance between current position and sample position controls AO intensity.
|
---|
| 88 | const float distance_intensity =
|
---|
| 89 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
|
---|
| 90 |
|
---|
[2885] | 91 | // if surface normal perpenticular to view dir, approx. half of the samples will not count
|
---|
| 92 | // => compensate for this (on the other hand, projected sampling area could be larger!)
|
---|
[2887] | 93 | //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
|
---|
| 94 | //total_ao += cos_angle * distance_intensity * view_correction;
|
---|
[2881] | 95 |
|
---|
[2887] | 96 | total_ao += cos_angle * distance_intensity;
|
---|
[2881] | 97 | }
|
---|
| 98 |
|
---|
[2890] | 99 | return max(0.0f, 1.0f - total_ao);
|
---|
| 100 | //return saturate(dot(currentViewDir, currentNormal));
|
---|
[2881] | 101 | }
|
---|
| 102 |
|
---|
| 103 |
|
---|
| 104 | /** The mrt shader for screen space ambient occlusion
|
---|
| 105 | */
|
---|
| 106 | pixel main(fragment IN,
|
---|
| 107 | uniform sampler2D colors,
|
---|
| 108 | uniform sampler2D positions,
|
---|
| 109 | uniform sampler2D normals,
|
---|
| 110 | uniform sampler2D noiseTexture,
|
---|
| 111 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 112 | uniform float noiseMultiplier,
|
---|
| 113 | uniform sampler2D oldTex,
|
---|
| 114 | const uniform float4x4 oldModelViewProj,
|
---|
| 115 | uniform float maxDepth,
|
---|
[2897] | 116 | uniform float temporalCoherence
|
---|
[2881] | 117 | )
|
---|
| 118 | {
|
---|
| 119 | pixel OUT;
|
---|
| 120 |
|
---|
| 121 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
| 122 |
|
---|
| 123 | // the ambient term
|
---|
| 124 | const float amb = norm.w;
|
---|
| 125 |
|
---|
| 126 | // expand normal
|
---|
| 127 | float3 normal = normalize(norm.xyz);
|
---|
[2891] | 128 |
|
---|
[2881] | 129 | /// the current view direction
|
---|
[2891] | 130 | float3 viewDir;// = normalize(IN.view);
|
---|
[2881] | 131 |
|
---|
| 132 | // the current world position
|
---|
| 133 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
|
---|
| 134 |
|
---|
| 135 | // the current color
|
---|
| 136 | const float4 currentCol = tex2D(colors, IN.texCoord.xy);
|
---|
| 137 | const float currentDepth = currentCol.w;
|
---|
| 138 |
|
---|
| 139 | const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition);
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 | /////////////////
|
---|
| 143 | //-- compute temporally smoothing
|
---|
| 144 |
|
---|
| 145 | float4 realPos = centerPosition * maxDepth;
|
---|
| 146 | realPos.w = 1.0f;
|
---|
| 147 |
|
---|
| 148 | // reproject
|
---|
| 149 | float4 oldPos = mul(oldModelViewProj, realPos);
|
---|
| 150 |
|
---|
| 151 | const float newDepth = oldPos.z / oldPos.w;
|
---|
| 152 |
|
---|
| 153 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
|
---|
| 154 | float4 oldCol = tex2D(oldTex, tex);
|
---|
| 155 |
|
---|
| 156 | const float oldDepth = oldCol.w;
|
---|
| 157 | const float depthDif = 1.0f - newDepth / oldDepth;
|
---|
| 158 |
|
---|
[2897] | 159 | float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
|
---|
| 160 | float newWeight;
|
---|
[2881] | 161 |
|
---|
[2897] | 162 | if ((temporalCoherence > 0) &&
|
---|
| 163 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
[2881] | 164 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
[2897] | 165 | (abs(depthDif) < 1e-4f))
|
---|
[2881] | 166 | {
|
---|
[2897] | 167 | newWeight = oldWeight + 1;
|
---|
| 168 |
|
---|
| 169 | //OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
|
---|
| 170 | OUT.illum_col = (float4)(ao + oldCol.x * oldWeight) / newWeight;
|
---|
[2881] | 171 | }
|
---|
| 172 | else
|
---|
| 173 | {
|
---|
[2884] | 174 | OUT.illum_col = (float4)ao;
|
---|
[2897] | 175 | newWeight = 0;
|
---|
[2881] | 176 | }
|
---|
| 177 |
|
---|
[2897] | 178 |
|
---|
| 179 | OUT.illum_col.z = newWeight;
|
---|
[2880] | 180 | OUT.illum_col.w = currentDepth;
|
---|
| 181 |
|
---|
[2881] | 182 | return OUT;
|
---|
| 183 | }
|
---|
[2880] | 184 |
|
---|
[2881] | 185 |
|
---|
[2880] | 186 | pixel combine(fragment IN,
|
---|
| 187 | uniform sampler2D colors,
|
---|
| 188 | uniform sampler2D ssaoTex)
|
---|
| 189 | {
|
---|
| 190 | pixel OUT;
|
---|
| 191 |
|
---|
[2884] | 192 | float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
[2880] | 193 | float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
|
---|
| 194 |
|
---|
[2891] | 195 | OUT.illum_col = col * ao.x;
|
---|
| 196 | //OUT.illum_col = ao;
|
---|
[2887] | 197 |
|
---|
[2897] | 198 | //OUT.illum_col.z = ao.z;
|
---|
[2884] | 199 | OUT.illum_col.w = ao.w;
|
---|
[2880] | 200 |
|
---|
| 201 | return OUT;
|
---|
[2873] | 202 | } |
---|