armory/Shaders/ssao_pass/ssao_pass.frag.glsl

66 lines
1.9 KiB
Plaintext
Raw Normal View History

2018-12-07 13:48:40 +01:00
// Alchemy AO / Scalable Ambient Obscurance
2016-03-15 11:29:53 +01:00
#version 450
#include "compiled.inc"
2017-12-13 14:21:42 +01:00
#include "std/gbuffer.glsl"
2016-07-17 20:29:53 +02:00
2016-05-13 00:08:11 +02:00
uniform sampler2D gbufferD;
2016-03-22 12:04:08 +01:00
uniform sampler2D gbuffer0;
2018-12-05 17:47:45 +01:00
uniform vec2 cameraProj;
2018-12-07 13:48:40 +01:00
uniform vec3 eye;
2018-12-05 17:47:45 +01:00
uniform vec3 eyeLook;
2016-06-21 13:29:27 +02:00
uniform vec2 screenSize;
2018-12-07 13:48:40 +01:00
uniform mat4 invVP;
2016-03-26 12:53:25 +01:00
#ifdef _CPostprocess
uniform vec3 PPComp11;
uniform vec3 PPComp12;
#endif
2016-03-15 11:29:53 +01:00
in vec2 texCoord;
2018-12-05 17:47:45 +01:00
in vec3 viewRay;
2018-12-06 15:23:08 +01:00
out float fragColor;
2016-03-15 11:29:53 +01:00
void main() {
2018-12-05 17:47:45 +01:00
float depth = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
2018-12-06 15:23:08 +01:00
if (depth == 1.0) { fragColor = 1.0; return; }
2016-08-07 23:12:14 +02:00
2021-09-02 17:14:37 +02:00
vec2 enc = textureLod(gbuffer0, texCoord, 0.0).rg;
2017-03-12 17:29:22 +01:00
vec3 n;
n.z = 1.0 - abs(enc.x) - abs(enc.y);
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
n = normalize(n);
2021-09-02 17:14:37 +02:00
2018-12-05 17:47:45 +01:00
vec3 vray = normalize(viewRay);
vec3 currentPos = getPosNoEye(eyeLook, vray, depth, cameraProj);
2018-12-07 13:48:40 +01:00
// vec3 currentPos = getPos2NoEye(eye, invVP, depth, texCoord);
float currentDistance = length(currentPos);
#ifdef _CPostprocess
float currentDistanceA = currentDistance * PPComp12.y * (1.0 / PPComp11.z);
#else
float currentDistanceA = currentDistance * ssaoScale * (1.0 / ssaoRadius);
#endif
2018-12-07 13:48:40 +01:00
float currentDistanceB = currentDistance * 0.0005;
ivec2 px = ivec2(texCoord * screenSize);
float phi = (3 * px.x ^ px.y + px.x * px.y) * 10;
2018-12-05 17:47:45 +01:00
2018-12-06 15:23:08 +01:00
fragColor = 0;
2018-12-07 13:48:40 +01:00
const int samples = 8;
const float samplesInv = PI2 * (1.0 / samples);
for (int i = 0; i < samples; ++i) {
float theta = samplesInv * (i + 0.5) + phi;
vec2 k = vec2(cos(theta), sin(theta)) / currentDistanceA;
2018-12-05 17:47:45 +01:00
depth = textureLod(gbufferD, texCoord + k, 0.0).r * 2.0 - 1.0;
2018-12-07 13:48:40 +01:00
// vec3 pos = getPosNoEye(eyeLook, vray, depth, cameraProj) - currentPos;
vec3 pos = getPos2NoEye(eye, invVP, depth, texCoord + k) - currentPos;
fragColor += max(0, dot(pos, n) - currentDistanceB) / (dot(pos, pos) + 0.015);
2018-11-27 21:34:42 +01:00
}
2021-09-02 17:14:37 +02:00
#ifdef _CPostprocess
fragColor *= (PPComp12.x * 0.3) / samples;
#else
fragColor *= (ssaoStrength * 0.3) / samples;
#endif
2018-12-06 15:23:08 +01:00
fragColor = 1.0 - fragColor;
2016-03-15 11:29:53 +01:00
}