0.2.0-dev Download ZIP
32 lines ·1.4 KB Raw
| 1 | // S4AO (Stupid Simple Screen Space Ambient Occlusion) - Jonathan Dummer (O1S) |
| 2 | // This micro version uses only 3 depth samples, the midpoint and a randomly-rotated, balanced pair. |
| 3 | |
| 4 | const mediump float ssao_falloff_frac = 0.25; |
| 5 | // Perform the SSAO. |
| 6 | float s4ao(vec2 UV) { |
| 7 | #ifdef USE_MULTIVIEW |
| 8 | mediump float depth = texture(depth_buffer_array, vec3(UV, view)).r; |
| 9 | #else |
| 10 | mediump float depth = texture(depth_buffer, UV).r; |
| 11 | #endif |
| 12 | mediump float inv_falloff = 1.0f / max(1e-4f, depth * ssao_falloff_frac); |
| 13 | // Random 2D rotation per pixel (0..1 -> parabola approximating a 180 deg arc) |
| 14 | mediump float r01 = fract(dot(UV, ssao_prn_UV)); |
| 15 | mediump vec2 duv = vec2(r01 - 0.5f, 2.0f * (r01 - r01 * r01)) * (2.0f * depth * ssao_radius_frac); // 180 degrees. |
| 16 | // Grab the samples and determine the occlusion. |
| 17 | mediump float occlusion = 0.0f; |
| 18 | for (int s = 0; s < 2; ++s) { |
| 19 | #ifdef USE_MULTIVIEW |
| 20 | mediump float dz = texture(depth_buffer_array, vec3(UV + duv, view)).r - depth; |
| 21 | #else |
| 22 | mediump float dz = texture(depth_buffer, UV + duv).r - depth; |
| 23 | #endif |
| 24 | // How 'directly overhead' is it? Factor in the falloff depth. |
| 25 | occlusion += normalize(vec3(duv, dz)).z * mix(1.0f, 0.0f, dz * inv_falloff); |
| 26 | // Mirror the next sample. |
| 27 | duv = -duv; |
| 28 | } |
| 29 | // Adjust the occlusion for intensity, and # samples. |
| 30 | occlusion = 1.0f - clamp(occlusion * 0.5f * ssao_intensity, 0.0f, 1.0f); |
| 31 | return occlusion * occlusion; |
| 32 | } |