1 | #include "../shaderenv.h"
|
---|
2 |
|
---|
3 |
|
---|
4 | struct fragment
|
---|
5 | {
|
---|
6 | float2 texCoord: TEXCOORD0;
|
---|
7 | float3 view: TEXCOORD1;
|
---|
8 | };
|
---|
9 |
|
---|
10 |
|
---|
11 | struct pixel
|
---|
12 | {
|
---|
13 | float4 illum_col: COLOR0;
|
---|
14 | };
|
---|
15 |
|
---|
16 |
|
---|
17 |
|
---|
18 | float Filter(float2 texCoord,
|
---|
19 | uniform sampler2D ssaoTex,
|
---|
20 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
21 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES])
|
---|
22 | {
|
---|
23 | float average = .0f;
|
---|
24 | float w = .0f;
|
---|
25 |
|
---|
26 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
27 | {
|
---|
28 | average += filterWeights[i] * tex2Dlod(ssaoTex, float4(texCoord + filterOffs[i], 0, 0)).x;
|
---|
29 | w += filterWeights[i];
|
---|
30 | }
|
---|
31 |
|
---|
32 | average *= 1.0f / (float)w;
|
---|
33 |
|
---|
34 | return average;
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | float BilateralFilter(float2 texCoord,
|
---|
39 | float4 ao,
|
---|
40 | uniform sampler2D ssaoTex,
|
---|
41 | uniform sampler2D normalsTex,
|
---|
42 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
43 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES])
|
---|
44 | {
|
---|
45 | float average = .0f;
|
---|
46 | float total_w = .0f;
|
---|
47 |
|
---|
48 | //const float eyeSpaceDepth = ao.w;
|
---|
49 | const float3 norm = normalize(tex2Dlod(normalsTex, float4(texCoord, 0, 0)).xyz);
|
---|
50 |
|
---|
51 | float4 aoSample;
|
---|
52 | float3 sampleNorm;
|
---|
53 | float w;
|
---|
54 |
|
---|
55 | for (int i = 0; i < NUM_SSAO_FILTERSAMPLES; ++ i)
|
---|
56 | {
|
---|
57 | float4 offs = float4(texCoord + filterOffs[i], 0, 0);
|
---|
58 | aoSample = tex2Dlod(ssaoTex, offs);
|
---|
59 | //aoSample = tex2D(ssaoTex, texCoord + filterOffs[i]);
|
---|
60 | //float sampleEyeSpaceDepth = aoSample.w;
|
---|
61 | //factor = abs(eyeSpaceDepth - sampleEyeSpaceDepth) / eyeSpaceDepth;
|
---|
62 | sampleNorm = normalize(tex2Dlod(normalsTex, offs).xyz);
|
---|
63 | //sampleNorm = tex2Dlod(normalsTex, offs).xyz;
|
---|
64 |
|
---|
65 | w = filterWeights[i] * max(dot(sampleNorm, norm), .0f);
|
---|
66 |
|
---|
67 | average += w * aoSample.x;
|
---|
68 | total_w += w;
|
---|
69 | }
|
---|
70 |
|
---|
71 | average *= 1.0f / max(total_w, 1e-5f);
|
---|
72 |
|
---|
73 | return average;
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | pixel combine(fragment IN,
|
---|
78 | uniform sampler2D colorsTex,
|
---|
79 | uniform sampler2D ssaoTex,
|
---|
80 | uniform sampler2D normalsTex,
|
---|
81 | uniform float2 filterOffs[NUM_SSAO_FILTERSAMPLES],
|
---|
82 | uniform float filterWeights[NUM_SSAO_FILTERSAMPLES]
|
---|
83 | )
|
---|
84 | {
|
---|
85 | pixel OUT;
|
---|
86 |
|
---|
87 | float4 col = tex2Dlod(colorsTex, float4(IN.texCoord, 0, 0));
|
---|
88 | float4 ao = tex2Dlod(ssaoTex, float4(IN.texCoord, 0, 0));
|
---|
89 |
|
---|
90 | //if (ao.y < 10.0f) ao.x = Filter(IN.texCoord, ssaoTex, filterOffs, filterWeights);
|
---|
91 | //if (ao.y < 10.0f) ao.x = BilateralFilter(IN.texCoord, ao, ssaoTex, normalsTex, filterOffs, filterWeights);
|
---|
92 |
|
---|
93 | //OUT.illum_col = col * ao.x;
|
---|
94 | //OUT.illum_col = float4(ao.x, ao.y, ao.z, col.w);
|
---|
95 | OUT.illum_col = float4(ao.x, ao.x, ao.x, col.w);
|
---|
96 | //OUT.illum_col.xyz = float3(1.0f - ao.x, 1.0f - ao.y * 1e-2f, 1);
|
---|
97 | //OUT.illum_col.xyz = float3(1.0f - ao.x, ao.y, 0);
|
---|
98 | OUT.illum_col.w = col.w;
|
---|
99 |
|
---|
100 | return OUT;
|
---|
101 | }
|
---|