[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 | */
|
---|
[2904] | 37 | float2 ssao(fragment IN,
|
---|
[2881] | 38 | uniform sampler2D positions,
|
---|
| 39 | uniform sampler2D noiseTexture,
|
---|
| 40 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 41 | uniform float3 currentNormal,
|
---|
[2974] | 42 | uniform float4 centerPosition,
|
---|
| 43 | uniform float scaleFactor
|
---|
[2904] | 44 | //,uniform float3 currentViewDir
|
---|
[2881] | 45 | )
|
---|
| 46 | {
|
---|
| 47 | // Check in a circular area around the current position.
|
---|
| 48 | // Shoot vectors to the positions there, and check the angle to these positions.
|
---|
| 49 | // Summing up these angles gives an estimation of the occlusion at the current position.
|
---|
| 50 |
|
---|
| 51 | float total_ao = 0.0;
|
---|
[2904] | 52 | float numSamples = 0;
|
---|
[2881] | 53 |
|
---|
| 54 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
| 55 | {
|
---|
[2892] | 56 | const float2 offset = samples[i];
|
---|
[2881] | 57 |
|
---|
[2903] | 58 | #if 1
|
---|
[2881] | 59 | ////////////////////
|
---|
[2903] | 60 | // add random noise: reflect around random normal vector (warning: slow!)
|
---|
| 61 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy).xy;
|
---|
[2892] | 62 | const float2 offsetTransformed = myreflect(offset, mynoise);
|
---|
[2903] | 63 | #else
|
---|
| 64 | const float2 offsetTransformed = offset;
|
---|
| 65 | #endif
|
---|
[2881] | 66 | // weight with projected coordinate to reach similar kernel size for near and far
|
---|
[2974] | 67 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * scaleFactor;
|
---|
[2881] | 68 |
|
---|
[2904] | 69 | if ((texcoord.x <= 1.0f) && (texcoord.x >= 0.0f) && (texcoord.y <= 1.0f) && (texcoord.y >= 0.0f))
|
---|
| 70 | ++ numSamples;
|
---|
| 71 |
|
---|
[2881] | 72 | // sample downsampled texture in order to speed up texture accesses
|
---|
[2964] | 73 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, SSAO_MIPMAP_LEVEL)).xyz;
|
---|
[2881] | 74 | //float3 sample_position = tex2D(positions, texcoord).xyz;
|
---|
| 75 |
|
---|
| 76 | float3 vector_to_sample = sample_position - centerPosition.xyz;
|
---|
| 77 | const float length_to_sample = length(vector_to_sample);
|
---|
| 78 |
|
---|
| 79 | float3 direction_to_sample = vector_to_sample / length_to_sample;
|
---|
| 80 |
|
---|
[2885] | 81 | // angle between current normal and direction to sample controls AO intensity.
|
---|
[2881] | 82 | const float cos_angle = max(dot(direction_to_sample, currentNormal), 0.0f);
|
---|
| 83 |
|
---|
[2979] | 84 | // the distance_scale offset is used to avoid singularity that occurs at global illumination when
|
---|
| 85 | // the distance to a sample approaches zero
|
---|
[2881] | 86 | const float distance_intensity =
|
---|
| 87 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample);
|
---|
| 88 |
|
---|
[2911] | 89 | #if 0
|
---|
[2885] | 90 | // if surface normal perpenticular to view dir, approx. half of the samples will not count
|
---|
| 91 | // => compensate for this (on the other hand, projected sampling area could be larger!)
|
---|
[2911] | 92 | const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal));
|
---|
| 93 | total_ao += cos_angle * distance_intensity * view_correction;
|
---|
| 94 | #endif
|
---|
[2887] | 95 | total_ao += cos_angle * distance_intensity;
|
---|
[2881] | 96 | }
|
---|
| 97 |
|
---|
[2904] | 98 | return float2(max(0.0f, 1.0f - total_ao), numSamples);
|
---|
[2890] | 99 | //return saturate(dot(currentViewDir, currentNormal));
|
---|
[2881] | 100 | }
|
---|
| 101 |
|
---|
| 102 |
|
---|
| 103 | /** The mrt shader for screen space ambient occlusion
|
---|
| 104 | */
|
---|
| 105 | pixel main(fragment IN,
|
---|
| 106 | uniform sampler2D colors,
|
---|
| 107 | uniform sampler2D positions,
|
---|
| 108 | uniform sampler2D normals,
|
---|
| 109 | uniform sampler2D noiseTexture,
|
---|
| 110 | uniform float2 samples[NUM_SAMPLES],
|
---|
| 111 | uniform sampler2D oldTex,
|
---|
| 112 | const uniform float4x4 oldModelViewProj,
|
---|
| 113 | uniform float maxDepth,
|
---|
[2897] | 114 | uniform float temporalCoherence
|
---|
[2881] | 115 | )
|
---|
| 116 | {
|
---|
| 117 | pixel OUT;
|
---|
| 118 |
|
---|
| 119 | float4 norm = tex2D(normals, IN.texCoord.xy);
|
---|
| 120 | float3 normal = normalize(norm.xyz);
|
---|
[2975] | 121 |
|
---|
[2974] | 122 | // a constant ambient term
|
---|
| 123 | const float amb = norm.w;
|
---|
| 124 | // the w coordinate from the persp. projection
|
---|
| 125 | float w = norm.w;
|
---|
[2881] | 126 | // the current world position
|
---|
| 127 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy);
|
---|
| 128 | // the current color
|
---|
[2974] | 129 | const float currentDepth = centerPosition.w;
|
---|
[2881] | 130 |
|
---|
[2974] | 131 | const float2 ao = ssao(IN, positions, noiseTexture, samples, normal, centerPosition, w);//, viewDir);
|
---|
[2881] | 132 |
|
---|
| 133 |
|
---|
| 134 | /////////////////
|
---|
| 135 | //-- compute temporally smoothing
|
---|
| 136 |
|
---|
| 137 | float4 realPos = centerPosition * maxDepth;
|
---|
| 138 | realPos.w = 1.0f;
|
---|
| 139 |
|
---|
[2975] | 140 |
|
---|
| 141 | ///////////
|
---|
| 142 | //-- reprojection from new frame into old one
|
---|
| 143 |
|
---|
[2881] | 144 | float4 oldPos = mul(oldModelViewProj, realPos);
|
---|
| 145 |
|
---|
| 146 | const float newDepth = oldPos.z / oldPos.w;
|
---|
| 147 |
|
---|
| 148 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f;
|
---|
| 149 | float4 oldCol = tex2D(oldTex, tex);
|
---|
| 150 |
|
---|
| 151 | const float oldDepth = oldCol.w;
|
---|
| 152 | const float depthDif = 1.0f - newDepth / oldDepth;
|
---|
| 153 |
|
---|
[2897] | 154 | float oldWeight = clamp(oldCol.z, 0, temporalCoherence);
|
---|
[2904] | 155 | float oldNumSamples = oldCol.y;
|
---|
| 156 |
|
---|
[2897] | 157 | float newWeight;
|
---|
[2881] | 158 |
|
---|
[2982] | 159 | float newNumSamples = ao.y;
|
---|
| 160 |
|
---|
[2904] | 161 | if (//(temporalCoherence > 0) &&
|
---|
[2897] | 162 | (tex.x >= 0.0f) && (tex.x < 1.0f) &&
|
---|
[2881] | 163 | (tex.y >= 0.0f) && (tex.y < 1.0f) &&
|
---|
[2982] | 164 | (abs(depthDif) < 1e-4f)
|
---|
[2975] | 165 | // check if visibility changed in the surrounding area:
|
---|
| 166 | // then we have to recompute
|
---|
[2982] | 167 | && ((newWeight < 10) || (oldNumSamples > 0.9f * newNumSamples))
|
---|
| 168 | //&& (oldAvgDepth / newAvgDepth > 0.99)
|
---|
[2904] | 169 | )
|
---|
[2881] | 170 | {
|
---|
[2904] | 171 | // increase the weight for convergence
|
---|
| 172 | newWeight = oldWeight + 1.0f;
|
---|
| 173 | OUT.illum_col.xy = (ao.xy + oldCol.xy * oldWeight) / newWeight;
|
---|
| 174 |
|
---|
| 175 | //if (!(oldNumSamples > ao.y - 1.5f)) newWeight = 0;
|
---|
[2881] | 176 | }
|
---|
| 177 | else
|
---|
[2982] | 178 | {
|
---|
[2904] | 179 | OUT.illum_col.xy = ao.xy;
|
---|
[2897] | 180 | newWeight = 0;
|
---|
[2881] | 181 | }
|
---|
| 182 |
|
---|
[2897] | 183 | OUT.illum_col.z = newWeight;
|
---|
[2880] | 184 | OUT.illum_col.w = currentDepth;
|
---|
| 185 |
|
---|
[2881] | 186 | return OUT;
|
---|
| 187 | }
|
---|
[2880] | 188 |
|
---|
[2881] | 189 |
|
---|
[2880] | 190 | pixel combine(fragment IN,
|
---|
| 191 | uniform sampler2D colors,
|
---|
[2974] | 192 | uniform sampler2D ssaoTex,
|
---|
| 193 | uniform sampler2D positions)
|
---|
[2880] | 194 | {
|
---|
| 195 | pixel OUT;
|
---|
| 196 |
|
---|
[2884] | 197 | float4 col = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0));
|
---|
[2880] | 198 | float4 ao = tex2D(ssaoTex, IN.texCoord.xy);
|
---|
| 199 |
|
---|
[2891] | 200 | OUT.illum_col = col * ao.x;
|
---|
[2975] | 201 | //OUT.illum_col.w = ao.w;
|
---|
| 202 | OUT.illum_col.w = col.w;
|
---|
[2880] | 203 |
|
---|
| 204 | return OUT;
|
---|
[2873] | 205 | } |
---|