Hi guys,
this is kind of a re post and I am sorry for that, but this just isn't really working, or I don't think it is. Here is the results:
Without SSAO diffuse:
Normals:
Depth:
With SSAO:
Now some HLSL code:
Texture2D t_dffmap : register(t0);
Texture2D t_depthmap : register(t1);
Texture2D t_normalmap : register(t2);
Texture2D t_random : register(t3);
Texture2D t_blmextract : register(t4);
SamplerState ss;
cbuffer PARAMSBUFFER : register(b0)
{
float time;
float hblur;
float bloomExtract;
float bloom;
float pixelDisortion;
float pixelDisorterAmount;
float ssao;
float space;
};
cbuffer BloomBuffer : register(b1)
{
float BloomThreshold;
float BloomSaturation;
float BaseSaturation;
float BloomIntensity;
float BaseIntensity;
};
cbuffer SSAOBuffer : register(b2)
{
float g_scale;
float g_bias;
float g_sample_rad;
float g_intensity;
};
struct VS_Output
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
float2 texCoord1 : TEXCOORD1;
float2 texCoord2 : TEXCOORD2;
float2 texCoord3 : TEXCOORD3;
float2 texCoord4 : TEXCOORD4;
float2 texCoord5 : TEXCOORD5;
float2 texCoord6 : TEXCOORD6;
float2 texCoord7 : TEXCOORD7;
float2 texCoord8 : TEXCOORD8;
float2 texCoord9 : TEXCOORD9;
};
VS_Output VShader(uint id : SV_VertexID)
{
VS_Output Output;
Output.Tex = float2((id << 1) & 2, id & 2);
Output.Pos = float4(Output.Tex * float2(2,-2) + float2(-1,1), 0, 1);
if (hblur == 1)
{
float texelSize = 1.0f / 800;
// Create UV coordinates for the pixel and its four horizontal neighbors on either side.
Output.texCoord1 = Output.Tex + float2(texelSize * -4.0f, 0.0f);
Output.texCoord2 = Output.Tex + float2(texelSize * -3.0f, 0.0f);
Output.texCoord3 = Output.Tex + float2(texelSize * -2.0f, 0.0f);
Output.texCoord4 = Output.Tex + float2(texelSize * -1.0f, 0.0f);
Output.texCoord5 = Output.Tex + float2(texelSize * 0.0f, 0.0f);
Output.texCoord6 = Output.Tex + float2(texelSize * 1.0f, 0.0f);
Output.texCoord7 = Output.Tex + float2(texelSize * 2.0f, 0.0f);
Output.texCoord8 = Output.Tex + float2(texelSize * 3.0f, 0.0f);
Output.texCoord9 = Output.Tex + float2(texelSize * 4.0f, 0.0f);
}
return Output;
}
// Helper for modifying the saturation of a color.
float4 AdjustSaturation(float4 color, float saturation)
{
// The constants 0.3, 0.59, and 0.11 are chosen because the
// human eye is more sensitive to green light, and less to blue.
float grey = dot(color, float3(0.3, 0.59, 0.11));
return lerp(grey, color, saturation);
}
// Ambient Occlusion Stuff --------------------------------------------------
float3 getPosition(in float2 uv)
{
return t_depthmap.Sample(ss, uv).xyz;
}
float3 getNormal(in float2 uv)
{
return normalize(t_normalmap.Sample(ss, uv).xyz * 2.0f - 1.0f);
}
float2 getRandom(in float2 uv)
{
return normalize(t_random.Sample(ss, float2(800, 600) * uv / float2(64, 64)).xy * 2.0f - 1.0f);
}
float doAmbientOcclusion(in float2 tcoord,in float2 uv, in float3 p, in float3 cnorm)
{
float3 diff = getPosition(tcoord + uv) - p;
const float3 v = normalize(diff);
const float d = length(diff)*g_scale;
return max(0.0,dot(cnorm,v)-g_bias)*(1.0/(1.0+d))*g_intensity;
}
// End
float4 PShader(VS_Output input) : SV_TARGET
{
if (bloomExtract == 1)
{
// Look up the original image color.
float4 c = t_dffmap.Sample(ss, input.Tex);
// Adjust it to keep only values brighter than the specified threshold.
return saturate((c - BloomThreshold) / (1 - BloomThreshold));
}
float4 color = float4(1.0f, 1.0f, 1.0f, 1.0f);
if (pixelDisortion == 1)
{
// Distortion factor
float NoiseX = pixelDisorterAmount * (time/1000) * sin(input.Tex.x * input.Tex.y+time/1000);
NoiseX=fmod(NoiseX,8) * fmod(NoiseX,4);
// Use our distortion factor to compute how much it will affect each
// texture coordinate
float DistortX = fmod(NoiseX,5);
float DistortY = fmod(NoiseX,5+0.002);
// Create our new texture coordinate based on our distortion factor
input.Tex = float2(DistortX,DistortY);
}
float4 dffMAP = t_dffmap.Sample(ss, input.Tex);
if (hblur == 1)
{
float weight0, weight1, weight2, weight3, weight4;
float normalization;
// Create the weights that each neighbor pixel will contribute to the blur.
weight0 = 1.0f;
weight1 = 0.9f;
weight2 = 0.55f;
weight3 = 0.18f;
weight4 = 0.1f;
// Create a normalized value to average the weights out a bit.
normalization = (weight0 + 2.0f * (weight1 + weight2 + weight3 + weight4));
// Normalize the weights.
weight0 = weight0 / normalization;
weight1 = weight1 / normalization;
weight2 = weight2 / normalization;
weight3 = weight3 / normalization;
weight4 = weight4 / normalization;
// Add the nine horizontal pixels to the color by the specific weight of each.
color += t_dffmap.Sample(ss, input.texCoord1) * weight4;
color += t_dffmap.Sample(ss, input.texCoord2) * weight3;
color += t_dffmap.Sample(ss, input.texCoord3) * weight2;
color += t_dffmap.Sample(ss, input.texCoord4) * weight1;
color += t_dffmap.Sample(ss, input.texCoord5) * weight0;
color += t_dffmap.Sample(ss, input.texCoord6) * weight1;
color += t_dffmap.Sample(ss, input.texCoord7) * weight2;
color += t_dffmap.Sample(ss, input.texCoord8) * weight3;
color += t_dffmap.Sample(ss, input.texCoord9) * weight4;
}
else
color *= dffMAP;
if (ssao == 1)
{
// Apply SSAO
const float2 vec[4] = {float2(1,0),float2(-1,0),
float2(0,1),float2(0,-1)};
float3 p = getPosition(input.Tex);
float3 n = getNormal(input.Tex);
float2 rand = getRandom(input.Tex);
float ao = 0.0f;
float rad = g_sample_rad/p.z; // g_s_r
//**SSAO Calculation**//
int iterations = 1;
for (int j = 0; j < iterations; ++j)
{
float2 coord1 = reflect(vec[j],rand)*rad;
float2 coord2 = float2(coord1.x*0.707 - coord1.y*0.707,
coord1.x*0.707 + coord1.y*0.707);
ao += doAmbientOcclusion(input.Tex,coord1*0.25, p, n);
ao += doAmbientOcclusion(input.Tex,coord2*0.5, p, n);
ao += doAmbientOcclusion(input.Tex,coord1*0.75, p, n);
ao += doAmbientOcclusion(input.Tex,coord2, p, n);
}
ao/=(float)iterations*4.0;
color.rgb *= ao;
}
if(bloom == 1)
{
// Look up the bloom and original base image colors.
float4 cbloom = t_blmextract.Sample(ss, input.Tex);
float4 base = color;
// Adjust color saturation and intensity.
cbloom = AdjustSaturation(cbloom, BloomSaturation) * BloomIntensity;
base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
// Darken down the base image in areas where there is a lot of bloom,
// to prevent things looking excessively burned-out.
base *= (1 - saturate(cbloom));
// Combine the two images.
color = base + cbloom;
}
return color;
}
The variables for the SSAO is the following: