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