1 | //////////////////// |
---|
2 | // Screen Spaced Ambient Occlusion shader |
---|
3 | // based on shader of Alexander Kusternig |
---|
4 | |
---|
5 | #define NUM_SAMPLES 10 |
---|
6 | //#define NUM_SAMPLES 16 |
---|
7 | |
---|
8 | // rule of thumb: approx 1 / NUM_SAMPLES |
---|
9 | //#define SAMPLE_INTENSITY 0.15f |
---|
10 | #define SAMPLE_INTENSITY 0.2f |
---|
11 | |
---|
12 | #define AREA_SIZE 7e-1f |
---|
13 | //#define AREA_SIZE 3e-1f |
---|
14 | #define VIEW_CORRECTION_SCALE 0.3f |
---|
15 | //#define VIEW_CORRECTION_SCALE 0.5f |
---|
16 | #define DISTANCE_SCALE 1e-6f |
---|
17 | |
---|
18 | |
---|
19 | struct fragment |
---|
20 | { |
---|
21 | // normalized screen position |
---|
22 | float4 pos: WPOS; |
---|
23 | float4 texCoord: TEXCOORD0; |
---|
24 | float3 view: COLOR0; |
---|
25 | }; |
---|
26 | |
---|
27 | |
---|
28 | struct pixel |
---|
29 | { |
---|
30 | float4 illum_col: COLOR0; |
---|
31 | float4 combined_col: COLOR1; |
---|
32 | }; |
---|
33 | |
---|
34 | |
---|
35 | float2 reflect(float2 pt, float2 n) |
---|
36 | { |
---|
37 | // distance to plane |
---|
38 | float d = dot(n, pt); |
---|
39 | // reflect around plane |
---|
40 | float2 rpt = pt - d * 2.0f * n; |
---|
41 | return rpt; |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | /** The ssao shader returning the an intensity value between 0 and 1 |
---|
47 | */ |
---|
48 | float ssao(fragment IN, |
---|
49 | uniform sampler2D positions, |
---|
50 | uniform sampler2D noiseTexture, |
---|
51 | uniform float2 samples[NUM_SAMPLES], |
---|
52 | uniform float3 currentNormal, |
---|
53 | uniform float3 currentViewDir, |
---|
54 | uniform float noiseMultiplier, |
---|
55 | uniform float4 centerPosition |
---|
56 | ) |
---|
57 | { |
---|
58 | // the w coordinate from the persp. projection |
---|
59 | float w = centerPosition.w; |
---|
60 | |
---|
61 | // Check in a circular area around the current position. |
---|
62 | // Shoot vectors to the positions there, and check the angle to these positions. |
---|
63 | // Summing up these angles gives an estimation of the occlusion at the current position. |
---|
64 | |
---|
65 | float total_ao = 0.0; |
---|
66 | |
---|
67 | for (int i = 0; i < NUM_SAMPLES; i ++) |
---|
68 | { |
---|
69 | float2 offset = samples[i]; |
---|
70 | |
---|
71 | //////////////////// |
---|
72 | // add random noise: r stores costheta, g stores sintheta |
---|
73 | |
---|
74 | //float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy * 2.0f - 1.0f; |
---|
75 | float2 mynoise = tex2D(noiseTexture, IN.texCoord.xy * noiseMultiplier).xy; |
---|
76 | |
---|
77 | float2 offsetTransformed = reflect(offset, mynoise); |
---|
78 | |
---|
79 | // weight with projected coordinate to reach similar kernel size for near and far |
---|
80 | float2 texcoord = IN.texCoord.xy + offsetTransformed * AREA_SIZE * w; |
---|
81 | |
---|
82 | // sample downsampled texture in order to speed up texture accesses |
---|
83 | float3 sample_position = tex2Dlod(positions, float4(texcoord, 0, 1)).xyz; |
---|
84 | //float3 sample_position = tex2D(positions, texcoord).xyz; |
---|
85 | |
---|
86 | float3 vector_to_sample = sample_position - centerPosition.xyz; |
---|
87 | float length_to_sample = length(vector_to_sample); |
---|
88 | float3 direction_to_sample = vector_to_sample / length_to_sample; |
---|
89 | |
---|
90 | // Angle between current normal and direction to sample controls AO intensity. |
---|
91 | float cos_angle = dot(direction_to_sample, currentNormal); |
---|
92 | cos_angle = max(cos_angle, 0.0f); |
---|
93 | |
---|
94 | // distance between current position and sample position controls AO intensity. |
---|
95 | float distance_intensity = |
---|
96 | (SAMPLE_INTENSITY * DISTANCE_SCALE) / (DISTANCE_SCALE + length_to_sample * length_to_sample); |
---|
97 | |
---|
98 | // if surface normal perpenticular to view dir, some samples probably count less |
---|
99 | // => compensate for this |
---|
100 | float view_correction = 1.0f + VIEW_CORRECTION_SCALE * (1.0f - dot(currentViewDir, currentNormal)); |
---|
101 | |
---|
102 | total_ao += cos_angle * distance_intensity * view_correction; |
---|
103 | } |
---|
104 | |
---|
105 | return max(0.0f, 1.0f - total_ao); |
---|
106 | //return dot(currentViewDir, currentNormal); |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | /** The mrt shader for screen space ambient occlusion |
---|
111 | */ |
---|
112 | pixel main(fragment IN, |
---|
113 | uniform sampler2D colors, |
---|
114 | uniform sampler2D positions, |
---|
115 | uniform sampler2D normals, |
---|
116 | uniform sampler2D noiseTexture, |
---|
117 | uniform float2 samples[NUM_SAMPLES], |
---|
118 | uniform float noiseMultiplier, |
---|
119 | uniform sampler2D oldTex, |
---|
120 | const uniform float4x4 oldModelViewProj, |
---|
121 | uniform float maxDepth, |
---|
122 | uniform float expFactor |
---|
123 | ) |
---|
124 | { |
---|
125 | pixel OUT; |
---|
126 | |
---|
127 | float4 norm = tex2D(normals, IN.texCoord.xy); |
---|
128 | |
---|
129 | // the ambient term |
---|
130 | const float amb = norm.w; |
---|
131 | |
---|
132 | // expand normal |
---|
133 | float3 normal = normalize(norm.xyz); |
---|
134 | /// the current view direction |
---|
135 | float3 viewDir = normalize(IN.view * 2.0f - float3(1.0f)); |
---|
136 | |
---|
137 | // the current world position |
---|
138 | const float4 centerPosition = tex2D(positions, IN.texCoord.xy); |
---|
139 | |
---|
140 | // the current color |
---|
141 | const float4 currentCol = tex2D(colors, IN.texCoord.xy); |
---|
142 | const float currentDepth = currentCol.w; |
---|
143 | |
---|
144 | const float ao = ssao(IN, positions, noiseTexture, samples, normal, viewDir, noiseMultiplier, centerPosition); |
---|
145 | |
---|
146 | |
---|
147 | ///////////////// |
---|
148 | //-- compute temporally smoothing |
---|
149 | |
---|
150 | float4 realPos = centerPosition * maxDepth; |
---|
151 | realPos.w = 1.0f; |
---|
152 | |
---|
153 | float4 oldPos = mul(oldModelViewProj, realPos); |
---|
154 | |
---|
155 | const float newDepth = oldPos.z / oldPos.w; |
---|
156 | |
---|
157 | float2 tex = (oldPos.xy / oldPos.w) * 0.5f + 0.5f; |
---|
158 | float4 oldCol = tex2D(oldTex, tex); |
---|
159 | |
---|
160 | const float oldDepth = oldCol.w; |
---|
161 | const float depthDif = 1.0f - newDepth / oldDepth; |
---|
162 | |
---|
163 | |
---|
164 | if ((tex.x >= 0.0f) && (tex.x < 1.0f) && |
---|
165 | (tex.y >= 0.0f) && (tex.y < 1.0f) && |
---|
166 | (abs(depthDif) < 1e-3f)) |
---|
167 | { |
---|
168 | OUT.illum_col.x = ao * expFactor + oldCol.x * (1.0f - expFactor); |
---|
169 | } |
---|
170 | else |
---|
171 | { |
---|
172 | OUT.illum_col.x = ao; |
---|
173 | } |
---|
174 | |
---|
175 | OUT.combined_col = currentCol * OUT.illum_col.x; |
---|
176 | OUT.illum_col.w = currentDepth; |
---|
177 | |
---|
178 | |
---|
179 | |
---|
180 | return OUT; |
---|
181 | } |
---|