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