Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 150
- #define SAMPLES 16
- const float PI2 = 2 * 3.1415926535897932384626433832795;
- uniform sampler2D normalBuffer;
- uniform sampler2D depthBuffer;
- uniform mat4 inverseProj;
- uniform float inverseAspectRatio;
- uniform vec2 minMaxSsaoWidth;
- in vec2 texCoords;
- out float fragColor;
- vec2 offsets[16] = vec2[16](
- vec2(0.044821292, 0.110167414) * 2 - 1,
- vec2(0.1380541, 0.4925164) * 2 - 1,
- vec2(0.1711376, 0.60476863) * 2 - 1,
- vec2(0.0026801229, 0.8000565) * 2 - 1,
- vec2(0.4459834, 0.14888838) * 2 - 1,
- vec2(0.29496264, 0.38575724) * 2 - 1,
- vec2(0.4706212, 0.6823994) * 2 - 1,
- vec2(0.33042485, 0.8249533) * 2 - 1,
- vec2(0.57578623, 0.1047747) * 2 - 1,
- vec2(0.67860055, 0.25636286) * 2 - 1,
- vec2(0.6422993, 0.5212651) * 2 - 1,
- vec2(0.69669557, 0.9629153) * 2 - 1,
- vec2(0.9217044, 0.1944445) * 2 - 1,
- vec2(0.8729959, 0.4054296) * 2 - 1,
- vec2(0.76195055, 0.6761958) * 2 - 1,
- vec2(0.97839516, 0.90347415 * 2 - 1)
- );
- float rand(vec2 co){
- return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
- }
- #pragma optionNV(unroll all)
- void main(){
- float depthValue = texture(depthBuffer, texCoords).r;
- float ssao = 1.0;
- vec4 center = inverseProj * vec4(texCoords * 2.0 - 1.0, depthValue * 2 - 1, 1.0);
- center.xyz /= center.w;
- vec3 normal = texture(normalBuffer, texCoords).xyz;
- float invDistance = inversesqrt(dot(center.xyz, center.xyz));
- float rawOffsetScale = 0.25 * invDistance;
- //Fade out SSAO when close to the camera to reduce the effect of clamping the sample radius.
- float clampedOffsetScale = clamp(rawOffsetScale, minMaxSsaoWidth.x, minMaxSsaoWidth.y);
- float powerScale = clamp(clampedOffsetScale / rawOffsetScale, 0.0, 1);
- powerScale *= powerScale;
- //Compensates for aspect ratio when sampling area around center pixel.
- vec2 offsetScale = clampedOffsetScale * vec2(inverseAspectRatio, 1.0);
- //Calculate a rotation matrix to randomly offset the samples per pixel.
- float angle = rand(texCoords) * PI2;
- float sin = sin(angle);
- float cos = cos(angle);
- mat2 rotation = mat2(
- cos, -sin,
- sin, cos
- );
- for(int i = 0; i < SAMPLES; i++){
- vec2 offset = (offsets[i] * rotation) * offsetScale;
- vec2 sampleCoords = texCoords + offset;
- vec4 sample = inverseProj * vec4(vec3(sampleCoords, texture(depthBuffer, sampleCoords).r) * 2.0 - 1.0, 1.0);
- sample.xyz /= sample.w;
- vec3 dPos = sample.xyz - center.xyz;
- float d = dot(dPos, dPos);
- vec3 direction = dPos * inversesqrt(d);
- float falloff = 1 + d*50;
- ssao *= 1 - clamp(dot(direction, normal) * powerScale / falloff, 0, 1);
- }
- fragColor = ssao;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement