[2884] | 1 | #include "../shaderenv.h"
|
---|
| 2 |
|
---|
[2881] | 3 | ////////////////////
|
---|
| 4 | // Screen Spaced Ambient Occlusion shader
|
---|
| 5 | // based on shader of Alexander Kusternig
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | struct fragment
|
---|
| 10 | {
|
---|
| 11 | // normalized screen position
|
---|
| 12 | float4 pos: WPOS;
|
---|
| 13 | float4 texCoord: TEXCOORD0;
|
---|
| 14 | float3 view: COLOR0;
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | struct pixel
|
---|
| 19 | {
|
---|
| 20 | float4 illum_col: COLOR0;
|
---|
| 21 | };
|
---|
| 22 |
|
---|
| 23 |
|
---|
| 24 | float2 reflect(float2 pt, float2 n)
|
---|
| 25 | {
|
---|
| 26 | // distance to plane
|
---|
| 27 | float d = dot(n, pt);
|
---|
| 28 | // reflect around plane
|
---|
| 29 | float2 rpt = pt - d * 2.0f * n;
|
---|
| 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 | {
|
---|
[2884] | 58 | const float2 offset = samples[i];
|
---|
[2881] | 59 |
|
---|
| 60 | ////////////////////
|
---|
| 61 | // add random noise: r stores costheta, g stores sintheta
|
---|
| 62 |
|
---|
[2884] | 63 | //const float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f;
|
---|
[2881] | 64 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy;
|
---|
| 65 |
|
---|
[2884] | 66 | const float2 offsetTransformed = reflect(offset, mynoise);
|
---|
[2881] | 67 |
|
---|
| 68 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
| 69 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w;
|
---|
| 70 |
|
---|
| 71 | // sample downsampled texture in order to speed up texture accesses
|
---|
| 72 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz;
|
---|
| 73 | //float3 sample_position = tex2D(positions, texcoord).xyz;
|
---|
| 74 |
|
---|
| 75 | float3 vector_to_sample = sample_position - centerPosition.xyz;
|
---|
| 76 | const float length_to_sample = length(vector_to_sample);
|
---|
| 77 |
|
---|
| 78 | float3 direction_to_sample = vector_to_sample / length_to_sample;
|
---|
| 79 |
|
---|
| 80 | // Angle between current normal and direction to sample controls AO intensity.
|
---|
| 81 | const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
|
---|
| 82 |
|
---|
| 83 | // distance between current position and sample position controls AO intensity.
|
---|
| 84 | const float distance_intensity =
|
---|
| 85 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
|
---|
| 86 |
|
---|
| 87 | // if surface normal perpenticular to view dir, some samples probably count less
|
---|
| 88 | // => compensate for this
|
---|
| 89 | const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
|
---|
| 90 |
|
---|
| 91 | total_ao += cos_angle * distance_intensity * view_correction;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return max(0.0f, 1.0f - total_ao);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | /** The mrt shader for screen space ambient occlusion
|
---|
| 99 | */
|
---|
| 100 | pixel main(fragment IN,
|
---|
| 101 | uniform sampler2D colors,
|
---|
| 102 | uniform sampler2D positions,
|
---|
| 103 | uniform sampler2D normals,
|
---|
| 104 | uniform sampler2D noiseTexture,
|
---|
| 105 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 106 | uniform float noiseMultiplier,
|
---|
| 107 | uniform sampler2D oldTex,
|
---|
| 108 | const uniform float4x4 oldModelViewProj,
|
---|
| 109 | uniform float maxDepth,
|
---|
| 110 | uniform float expFactor
|
---|
| 111 | )
|
---|
| 112 | {
|
---|
| 113 | pixel OUT;
|
---|
| 114 |
|
---|
| 115 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
| 116 |
|
---|
| 117 | // the ambient term
|
---|
| 118 | const float amb = norm.w;
|
---|
| 119 |
|
---|
| 120 | // expand normal
|
---|
| 121 | float3 normal = normalize(norm.xyz);
|
---|
| 122 | /// the current view direction
|
---|
| 123 | float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f));
|
---|
| 124 |
|
---|
| 125 | // the current world position
|
---|
| 126 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
|
---|
| 127 |
|
---|
| 128 | // the current color
|
---|
| 129 | const float4 currentCol = tex2D(colors, IN.texCoord.xy);
|
---|
| 130 | const float currentDepth = currentCol.w;
|
---|
| 131 |
|
---|
| 132 | const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition);
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | /////////////////
|
---|
| 136 | //-- compute temporally smoothing
|
---|
| 137 |
|
---|
| 138 | float4 realPos = centerPosition * maxDepth;
|
---|
| 139 | realPos.w = 1.0f;
|
---|
| 140 |
|
---|
| 141 | // reproject
|
---|
| 142 | float4 oldPos = mul(oldModelViewProj, realPos);
|
---|
| 143 |
|
---|
| 144 | const float newDepth = oldPos.z / oldPos.w;
|
---|
| 145 |
|
---|
| 146 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
|
---|
| 147 | float4 oldCol = tex2D(oldTex, tex);
|
---|
| 148 |
|
---|
| 149 | const float oldDepth = oldCol.w;
|
---|
| 150 | const float depthDif = 1.0f - newDepth / oldDepth;
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | if ((tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
| 154 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
| 155 | (abs(depthDif) < 1e-3f))
|
---|
| 156 | {
|
---|
[2884] | 157 | OUT.illum_col = (float4)ao * expFactor + oldCol.x * (1.0f - expFactor);
|
---|
[2881] | 158 | }
|
---|
| 159 | else
|
---|
| 160 | {
|
---|
[2884] | 161 | OUT.illum_col = (float4)ao;
|
---|
[2881] | 162 | }
|
---|
| 163 |
|
---|
[2880] | 164 | OUT.illum_col.w = currentDepth;
|
---|
| 165 |
|
---|
[2881] | 166 | return OUT;
|
---|
| 167 | }
|
---|
[2880] | 168 |
|
---|
[2881] | 169 |
|
---|
[2880] | 170 | pixel combine(fragment IN,
|
---|
| 171 | uniform sampler2D colors,
|
---|
| 172 | uniform sampler2D ssaoTex)
|
---|
| 173 | {
|
---|
| 174 | pixel OUT;
|
---|
| 175 |
|
---|
[2884] | 176 | float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
[2880] | 177 | float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
|
---|
| 178 |
|
---|
[2881] | 179 | OUT.illum_col = col * ao.x;
|
---|
[2884] | 180 | OUT.illum_col.w = ao.w;
|
---|
[2880] | 181 |
|
---|
| 182 | return OUT;
|
---|
[2873] | 183 | } |
---|