render: SSAO
This commit is contained in:
78
internal/asset/shader/world/ssao.frag
Normal file
78
internal/asset/shader/world/ssao.frag
Normal file
@@ -0,0 +1,78 @@
|
||||
#version 330
|
||||
|
||||
#define SSAO_SAMPLE_COUNT 32
|
||||
const float radius = 0.5, bias = 0;
|
||||
|
||||
uniform vec3 viewPos;
|
||||
uniform mat4 view, projection;
|
||||
uniform float time; // current time in seconds, for extra noise
|
||||
|
||||
uniform sampler2D rand;
|
||||
uniform vec2 randSize; // in pixels
|
||||
|
||||
// SSAO samples (normalized, in tangent space)
|
||||
layout (std140) uniform uboSamples {
|
||||
vec3 samples[SSAO_SAMPLE_COUNT];
|
||||
};
|
||||
|
||||
// G-Buffers
|
||||
uniform sampler2D gPos;
|
||||
uniform sampler2D gNorm;
|
||||
// Fragment information from G-Buffers
|
||||
vec3 fragPos; // World position
|
||||
vec3 fragNormal; // World normal
|
||||
float fragDepthView;
|
||||
|
||||
|
||||
in vec2 fragPosScreen;
|
||||
layout (location = 0) out float output;
|
||||
|
||||
|
||||
// A random number generator from the web.
|
||||
// https://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
|
||||
float whatever(vec2 co){
|
||||
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
|
||||
}
|
||||
|
||||
|
||||
void loadGBuffer() {
|
||||
vec4 fragGNormal = texture(gNorm, fragPosScreen);
|
||||
fragNormal = fragGNormal.xyz;
|
||||
if (fragNormal == vec3(0.0f, 0.0f, 0.0f))
|
||||
discard;
|
||||
|
||||
vec4 fragGPos = texture(gPos, fragPosScreen);
|
||||
fragPos = fragGPos.xyz + viewPos;
|
||||
fragDepthView = fragGPos.w;
|
||||
gl_FragDepth = fragGPos.w * 0.5 + 0.5;
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
|
||||
loadGBuffer();
|
||||
|
||||
vec4 randval = texture(rand, gl_FragCoord.xy / randSize);
|
||||
|
||||
vec3 tangent = normalize(randval.xyz - fragNormal*dot(randval.xyz, fragNormal));
|
||||
vec3 bitangent = cross(fragNormal, tangent);
|
||||
mat3 TBN = mat3(tangent, bitangent, fragNormal);
|
||||
|
||||
float occlusion = 0;
|
||||
for (int i = 0; i < SSAO_SAMPLE_COUNT; i++) {
|
||||
vec3 samplePos = TBN * samples[i];
|
||||
samplePos = fragPos + samplePos * radius;
|
||||
|
||||
vec4 viewpos = view * vec4(samplePos, 1.0);
|
||||
|
||||
vec4 ndcpos = projection * viewpos;
|
||||
ndcpos.xyz /= ndcpos.w;
|
||||
|
||||
float sampleDepth = texture(gPos, ndcpos.xy*0.5 + 0.5).w;
|
||||
|
||||
float rangeCheck = smoothstep(0.0, 1.0, radius / abs(fragDepthView - sampleDepth));
|
||||
occlusion += (sampleDepth >= viewpos.z/viewpos.w + bias ? 1.0 : 0.0) * rangeCheck;
|
||||
}
|
||||
|
||||
output = 1.0 - (occlusion / SSAO_SAMPLE_COUNT);
|
||||
}
|
||||
Reference in New Issue
Block a user