1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
float random_size = 64.0f;
float g_sample_rad = 1.0f/10.0f;
float g_intensity = 3.0f;
float g_scale = 1.0f;
float g_bias = 0.1f;
float2 g_screen_size = float2(800.0f,600.0f);
float3 getPosition(in float2 uv)
{
return tex2D(posMap,uv).xyz;
}
float3 getNormal(in float2 uv)
{
return tex2D(normalMap, uv).xyz;
}
float2 getRandom(in float2 uv)
{
return normalize(tex2D(RandomVecSampler, g_screen_size * uv / random_size).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;
}
float4 PS_SSAO(in float2 texCoord : TEXCOORD0) : COLOR0
{
const float2 vec[4] =
{float2(1,0),
float2(-1,0),
float2(0,1),
float2(0,-1)};
float3 p = getPosition(texCoord);
float3 n = getNormal(texCoord);
float2 rand = getRandom(texCoord);
float ao = 0.0f;
float rad = g_sample_rad/p.z;
//**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(texCoord,coord1*0.25, p, n);
ao += doAmbientOcclusion(texCoord,coord2*0.5, p, n);
ao += doAmbientOcclusion(texCoord,coord1*0.75, p, n);
ao += doAmbientOcclusion(texCoord,coord2, p, n);
}
ao/=(float)iterations*4.0;
//**END**//
return ao;
}
VS_OUTPUT VS_Model(VS_INPUT IN)
{
VS_OUTPUT OUT;
OUT.position = mul(float4(IN.position.xyz, 1.0f), g_WorldViewProjectionMatrix);
OUT.texCoord = IN.texCoord;
return OUT;
}
float4 PS_Model(VS_OUTPUT IN) : COLOR0
{
float4 color;
color = tex2D(colorMap, IN.texCoord);
return color;
}
float4 PS_PostProcess(in float2 texCoord : TEXCOORD0) : COLOR0
{
float4 color;
color = tex2D(PPMap, texCoord) * (tex2D(SSAOMap, texCoord));
return color;
}
|