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