| 1 |
// S4AO (Stupid Simple Screen Space Ambient Occlusion) - Jonathan Dummer (O1S) |
| 2 |
|
| 3 |
// The sample_width should be even, else the midpoint is at UV. |
| 4 |
// Takes sample_width^2 samples in a grid, with the corners notched. |
| 5 |
#if defined(USE_SSAO_LOW) |
| 6 |
const int sample_width = 2; |
| 7 |
#elif defined(USE_SSAO_HIGH) |
| 8 |
const int sample_width = 6; |
| 9 |
#else |
| 10 |
const int sample_width = 4; |
| 11 |
#endif |
| 12 |
const int notch_01 = int(sample_width > 3); // Set to 1 to skip the corner samples, 0 to include them. |
| 13 |
const float sample_mid = (float(sample_width) - 1.0) * 0.50001; // Can't be exactly 0.5 in case sample_width is odd. |
| 14 |
#if defined(USE_SSAO_LOW) |
| 15 |
const float inv_half_width = 1.0 / sample_mid; // The 2x2 sampling looks wider as all samples are at radius. |
| 16 |
#else |
| 17 |
const float inv_half_width = 1.7 / sample_mid; // Bake in the 1.7 scale for the random rotation. |
| 18 |
#endif |
| 19 |
const float average_samples = 1.0 / float(sample_width * sample_width - 4 * notch_01); // 1 / number_of_samples |
| 20 |
const float ssao_falloff_frac = 0.25; |
| 21 |
// Perform the SSAO. |
| 22 |
float s4ao(vec2 UV) { |
| 23 |
#ifdef USE_MULTIVIEW |
| 24 |
float depth = texture(depth_buffer_array, vec3(UV, view)).r; |
| 25 |
#else |
| 26 |
float depth = texture(depth_buffer, UV).r; |
| 27 |
#endif |
| 28 |
float radius = max(1e-4f, depth * ssao_radius_frac); |
| 29 |
float inv_falloff = 1.0f / max(1e-4f, depth * ssao_falloff_frac); |
| 30 |
// Random 2D rotation per pixel (+/-45 deg, with 0 having a lower probability). |
| 31 |
// The random cosine vector is vec2( 0.5, -0.5 to +0.5 ) and *1.7 makes the average length ~ 1. |
| 32 |
vec2 rcos = (inv_half_width * radius) * vec2(0.5f, fract(dot(UV, ssao_prn_UV)) - 0.5f); |
| 33 |
vec2 rsin = rcos.yx * vec2(-1, 1); // Perpendicular to the random cosine vector. |
| 34 |
// Grab the samples and determine the occlusion. |
| 35 |
float occlusion = 0.0f; |
| 36 |
vec2 base_duv = -sample_mid * rsin; |
| 37 |
for (int j = sample_width; --j >= 0;) { |
| 38 |
#if defined(USE_SSAO_LOW) |
| 39 |
// Low quality uses 2x2 samples, no notching. |
| 40 |
vec2 duv = -sample_mid * rcos + base_duv; |
| 41 |
for (int i = sample_width; --i >= 0;) { |
| 42 |
#else |
| 43 |
// Will uses 4x4 or 6x6 samples, with the corners notched out. |
| 44 |
int o = /*notch_01 &*/ int((j <= 0) || (j >= (sample_width - 1))); // Notch corners of the grid. |
| 45 |
vec2 duv = (float(o) - sample_mid) * rcos + base_duv; |
| 46 |
for (int i = sample_width - o - o; --i >= 0;) { |
| 47 |
#endif |
| 48 |
#ifdef USE_MULTIVIEW |
| 49 |
float dz = texture(depth_buffer_array, vec3(UV + duv, view)).r - depth; |
| 50 |
#else |
| 51 |
float dz = texture(depth_buffer, UV + duv).r - depth; |
| 52 |
#endif |
| 53 |
float validity = smoothstep(1.0f, 0.0f, dz * inv_falloff); |
| 54 |
occlusion += normalize(vec3(duv, dz)).z * validity; // How 'directly overhead' is it? |
| 55 |
duv += rcos; // March along the rcos direction with i. |
| 56 |
} |
| 57 |
base_duv += rsin; // March along the rsin direction with j. |
| 58 |
} |
| 59 |
// Adjust the occlusion for intensity, and # samples. |
| 60 |
occlusion *= ssao_intensity * average_samples; |
| 61 |
occlusion = clamp(1.0f - occlusion, 0.0f, 1.0f); |
| 62 |
return occlusion * occlusion; |
| 63 |
} |