//////////////////// // SSAO + color bleeding shader // based on shader of Alexander Kusternig #include "../shaderenv.h" struct fragment { // normalized screen position float4 pos: WPOS; float4 texCoord: TEXCOORD0; float3 view: COLOR0; }; struct pixel2 { float4 ssao_col: COLOR0; float4 illum_col: COLOR1; }; struct pixel { float4 illum_col: COLOR0; }; float2 myreflect(float2 pt, float2 n) { // distance to plane float d = dot(n, pt); // reflect around plane float2 rpt = pt - d * 2.0f * n; return rpt; } /** Computes diffuse reflections + ambient occlusion */ float4 globIllum(fragment IN, uniform sampler2D colors, uniform sampler2D positions, uniform sampler2D noiseTexture, uniform float2 samples[NUM_SAMPLES], uniform float3 currentNormal, uniform float3 currentViewDir, uniform float noiseMultiplier, uniform float4 centerPosition ) { // the w coordinate from the persp. projection float w = centerPosition.w; // Check in a circular area around the current position. // Shoot vectors to the positions there, and check the angle to these positions. // Summing up these angles gives an estimation of the occlusion at the current position. // ao is in stored in the w component float4 total_color = float4(0, 0, 0, 1); //////////// //-- the main sampling loop for (int i = 0; i < NUM_SAMPLES; i ++) { //float2 offset = samples[i]; float3 offset = float3(samples[i], 0); //sample noisetex; r stores costheta, g stores sintheta //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; float3 mynoise = float3(tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy, 0); // rotation float2 offsetTransformed = reflect(offset, mynoise); // weight with projected coordinate to reach similar kernel size for near and far float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; // use lower lod level to improve cache coherence float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; float3 sample_color = tex2Dlod(colors, float4(texcoord, 0, 2)).xyz; float3 vector_to_sample = sample_position - centerPosition.xyz; const float length_to_sample = length(vector_to_sample); float3 direction_to_sample = vector_to_sample / length_to_sample; // Angle between current normal and direction to sample controls AO intensity. float cos_angle = max(dot(direction_to_sample, currentNormal), 0); // distance between current position and sample position controls AO intensity. const float distance_intensity = (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); // if normal perpenticular to view dir, only half of the samples count //const float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); //total_color.w -= cos_angle * distance_intensity * view_correction; //total_color.xyz += cos_angle * distance_intensity * view_correction * sample_color * ILLUM_INTENSITY; total_color.w -= cos_angle * distance_intensity; total_color.xyz += cos_angle * distance_intensity * sample_color * ILLUM_INTENSITY; } return saturate(total_color); } /** The mrt shader for screen space ambient occlusion + indirect illumination */ pixel2 main(fragment IN, uniform sampler2D colors, uniform sampler2D positions, uniform sampler2D normals, uniform sampler2D noiseTexture, uniform float2 samples[NUM_SAMPLES], uniform float noiseMultiplier, uniform sampler2D oldSsaoTex, uniform sampler2D oldIllumTex, const uniform float4x4 oldModelViewProj, uniform float maxDepth, uniform float expFactor ) { pixel2 OUT; float4 norm = tex2D(normals, IN.texCoord.xy); // the ambient term const float amb = norm.w; // expand normal float3 normal = normalize(norm.xyz); /// the current view direction float3 viewDir;// = normalize(IN.view); // the current world position const float4 centerPosition = tex2D(positions, IN.texCoord.xy); // the current color const float4 currentCol = tex2Dlod(colors, float4(IN.texCoord.xy, 0, 0)); // the current depth is stored in the w component const float currentDepth = currentCol.w; float4 new_color = globIllum(IN, colors, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); ///////////////// //-- compute temporally smoothing float4 realPos = centerPosition * maxDepth; realPos.w = 1.0f; float4 oldPos = mul(oldModelViewProj, realPos); const float newDepth = oldPos.z / oldPos.w; float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; float4 oldSsao = tex2D(oldSsaoTex, tex); float4 oldIllum = tex2D(oldIllumTex, tex); const float oldDepth = oldSsao.w; const float depthDif = 1.0f - newDepth / oldDepth; if ((tex.x >= 0.0f) && (tex.x < 1.0f) && (tex.y >= 0.0f) && (tex.y < 1.0f) && (abs(depthDif) < 1e-3f)) { OUT.ssao_col.x = new_color.w * expFactor + oldSsao.x * (1.0f - expFactor); OUT.illum_col = new_color * expFactor + oldIllum * (1.0f - expFactor); } else { OUT.ssao_col.x = new_color.w; OUT.illum_col = new_color; } OUT.ssao_col.w = currentDepth; return OUT; } pixel combine(fragment IN, uniform sampler2D colors, uniform sampler2D ssaoTex, uniform sampler2D illumTex ) { pixel OUT; float4 col = tex2D(colors, IN.texCoord.xy); float ao = tex2D(ssaoTex, IN.texCoord.xy).x; float4 illum = tex2D(illumTex, IN.texCoord.xy); OUT.illum_col = (col + illum) * ao; OUT.illum_col.w = col.w; return OUT; }