I'm so terribly sorry to re-re-re open this one, but I never fully succeeded at this one, SSAO.
SSAO Shader:
Texture2D t_depthmap : register(t0);
Texture2D t_normalmap : register(t1);
Texture2D t_random : register(t2);
SamplerState ss;
cbuffer SSAOBuffer : register(c0)
{
float g_scale;
float g_bias;
float g_sample_rad;
float g_intensity;
float ssaoIterations;
float3 pppspace;
matrix view;
};
struct VS_Output
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
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);
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, uv ).xy * 2.0f - 1.0f); // ~100FPS
return normalize(t_random.Sample(ss, float2(600, 800) * uv / float2(60, 60)).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
{
// ADD 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 = 4;
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;
ao = saturate(ao);
return float4(ao, ao, ao, 1.0f);
}
How I write my depth map to the ssao:
VS
output.depth = output.position.z / output.position.w;
PS
float depth = input.depth;
output.Depth = float4(depth, depth, depth, 1);
How i write my normal map to the ssao:
VS -- I think this is wrong
output.NormalW = mul(mul(worldMatrix, viewMatrix), float4(normal.xyz,0) );
PS
output.Normals = float4(input.NormalW, 1);
Render Result:
Ehh.
Again, I'm so sorry for re-posting this, it's just, i really want this feature completed (well, working at least).
Thank You, like always...