Danbias/Code/OysterGraphics/Shader/HLSL/Deffered Shaders/SSAO.hlsli

53 lines
1.5 KiB
HLSL
Raw Normal View History

2014-01-08 07:01:59 +01:00
#include "Defines.hlsli"
#include "PosManipulation.hlsli"
2014-01-22 16:31:33 +01:00
static float Radius = 100;
2014-01-08 07:01:59 +01:00
float GetSSAO(float3 pos, float2 uv, int2 texCoord2, uint2 rndID)
{
float occlusion = 0.0f;
//create sample coordinate system
2014-01-22 16:31:33 +01:00
float4 rnd = float4( SSAORand[int2(rndID.x % (SSAORand.Length.x), rndID.y % (SSAORand.Length.y))].xyz, 0.0f );
2014-01-08 07:01:59 +01:00
rnd = normalize(rnd);
2014-01-22 16:31:33 +01:00
float3 normal = NormalSpec[texCoord2].xyz;
float3 tangent = float3( normalize(rnd.xyz - (normal * dot(rnd.xyz, normal))));
float3 biTangent = float3( cross(tangent.xyz, normal));
2014-01-08 07:01:59 +01:00
2014-01-22 16:31:33 +01:00
float3x3 tbn = float3x3(tangent, biTangent, normal);
2014-01-08 07:01:59 +01:00
for( uint i = 0; i < SSAOKernel.Length.x; ++i )
{
2014-01-22 16:31:33 +01:00
//int i = 0;
2014-01-08 07:01:59 +01:00
//take sample from localspace to viewspace
2014-01-22 16:31:33 +01:00
float3 sampled = mul(tbn, SSAOKernel[i].xyz);
sampled = sampled * Radius + pos;
2014-01-08 07:01:59 +01:00
//project sample to get uv.xy
2014-01-22 16:31:33 +01:00
float4 ProjOffset = float4(sampled,1);
2014-01-08 07:01:59 +01:00
ProjOffset = mul(Proj, ProjOffset);
float4 offset = ProjOffset;
2014-01-28 13:48:15 +01:00
float2 UV = offset.xy;
2014-01-08 07:01:59 +01:00
offset /= offset.w;
offset.xyz = offset.xyz * 0.5f + 0.5f;
//extra invert y axis, DX11
offset.y = 1.0f - offset.y;
// get depth from that point in screenspace
uint2 texCoord;
texCoord = (uint2)(offset.xy * Pixels);
float3 ViewPos = ToVpos(texCoord, UV);
float sampleDepth = ViewPos.z;
//compare to depth from sample
float rangeCheck = (abs(pos.z - sampleDepth) < Radius) ? 1.0f : 0.0f;
2014-01-22 16:31:33 +01:00
occlusion += (sampleDepth <= sampled.z ? 1.0f : 0.0f) * rangeCheck;
2014-01-08 07:01:59 +01:00
}
occlusion /= (float)(SSAOKernel.Length.x);
occlusion = 1.0f - occlusion;
return occlusion;
}