Skip to main content

SSAO_GLSL

Constant SSAO_GLSL 

Source
pub const SSAO_GLSL: &str = r#"
uniform sampler2D u_depth_tex;
uniform sampler2D u_normal_tex;
uniform vec2 u_screen_size;
uniform float u_ao_radius;
uniform float u_ao_strength;

float hash(vec2 p) {
    return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
}

float compute_ssao(vec2 uv, vec3 position, vec3 normal) {
    float occlusion = 0.0;
    const int SAMPLES = 8;
    for (int i = 0; i < SAMPLES; i++) {
        float angle = float(i) * 6.2831853 / float(SAMPLES) + hash(uv + float(i));
        float r = u_ao_radius * (float(i + 1) / float(SAMPLES));
        vec2 offset = vec2(cos(angle), sin(angle)) * r / u_screen_size;
        float neighbor_depth = texture(u_depth_tex, uv + offset).r;
        vec3 neighbor_normal = texture(u_normal_tex, uv + offset).rgb * 2.0 - 1.0;
        float depth_diff = position.z - neighbor_depth;
        if (depth_diff > 0.001 && depth_diff < u_ao_radius) {
            occlusion += (1.0 - depth_diff / u_ao_radius) * max(dot(normal, neighbor_normal), 0.0);
        }
    }
    return 1.0 - clamp(occlusion / float(SAMPLES) * u_ao_strength, 0.0, 1.0);
}
"#;
Expand description

Screen-space AO kernel sampling in GLSL.