2017-11-02 12:10:01 +01:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
#ifdef GL_ES
|
|
|
|
precision mediump float;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../compiled.glsl"
|
|
|
|
#include "../std/math.glsl"
|
|
|
|
#include "../std/gbuffer.glsl"
|
|
|
|
|
|
|
|
uniform sampler2D gbufferD;
|
2017-11-04 16:16:07 +01:00
|
|
|
uniform sampler2D gbuffer0; // Normal
|
|
|
|
#ifdef _RTGI
|
2017-11-04 16:17:06 +01:00
|
|
|
uniform sampler2D gbuffer1; // Basecol
|
2017-11-04 16:16:07 +01:00
|
|
|
#endif
|
2017-11-02 12:10:01 +01:00
|
|
|
uniform mat4 P;
|
|
|
|
uniform mat4 tiV;
|
|
|
|
|
2017-11-04 18:35:34 +01:00
|
|
|
uniform vec3 eye;
|
|
|
|
uniform vec3 eyeLook;
|
|
|
|
uniform vec2 cameraProj;
|
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
// const int ssgiStrength = 1.0;
|
|
|
|
// const int ssgiMaxSteps = 16;
|
|
|
|
// const int ssgiBinarySteps = 4;
|
|
|
|
// const float ssgiRayStep = 0.005;
|
|
|
|
const float searchDist = 5.0;
|
2017-11-02 12:10:01 +01:00
|
|
|
|
|
|
|
in vec3 viewRay;
|
|
|
|
in vec2 texCoord;
|
|
|
|
out vec4 fragColor;
|
|
|
|
|
|
|
|
vec3 hitCoord;
|
|
|
|
float depth;
|
2017-11-04 16:16:07 +01:00
|
|
|
float occ = 0.0;
|
|
|
|
#ifdef _RTGI
|
|
|
|
vec3 col = vec3(0.0);
|
|
|
|
#endif
|
2017-11-02 12:10:01 +01:00
|
|
|
|
|
|
|
vec2 getProjectedCoord(vec3 hitCoord) {
|
|
|
|
vec4 projectedCoord = P * vec4(hitCoord, 1.0);
|
|
|
|
projectedCoord.xy /= projectedCoord.w;
|
|
|
|
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
|
|
|
|
#ifdef _InvY
|
|
|
|
projectedCoord.y = 1.0 - projectedCoord.y;
|
|
|
|
#endif
|
|
|
|
return projectedCoord.xy;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getDeltaDepth(vec3 hitCoord) {
|
|
|
|
depth = texture(gbufferD, getProjectedCoord(hitCoord)).r * 2.0 - 1.0;
|
2017-11-04 18:35:34 +01:00
|
|
|
vec3 p = getPosView(viewRay, depth, cameraProj);
|
2017-11-04 16:16:07 +01:00
|
|
|
return p.z - hitCoord.z;
|
2017-11-02 12:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
vec4 binarySearch(vec3 dir) {
|
2017-11-04 16:16:07 +01:00
|
|
|
for (int i = 0; i < ssgiBinarySteps; i++) {
|
2017-11-02 12:10:01 +01:00
|
|
|
dir *= 0.5;
|
|
|
|
hitCoord -= dir;
|
|
|
|
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
|
2017-11-04 16:16:07 +01:00
|
|
|
}
|
2017-11-02 12:10:01 +01:00
|
|
|
return vec4(getProjectedCoord(hitCoord), 0.0, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 rayCast(vec3 dir) {
|
2017-11-04 16:16:07 +01:00
|
|
|
dir *= ssgiRayStep;
|
|
|
|
for (int i = 0; i < ssgiMaxSteps; i++) {
|
2017-11-02 12:10:01 +01:00
|
|
|
hitCoord += dir;
|
|
|
|
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
|
2017-11-04 16:16:07 +01:00
|
|
|
}
|
2017-11-02 12:10:01 +01:00
|
|
|
return vec4(0.0);
|
|
|
|
}
|
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
vec3 tangent(const vec3 n) {
|
|
|
|
vec3 t1 = cross(n, vec3(0, 0, 1));
|
|
|
|
vec3 t2 = cross(n, vec3(0, 1, 0));
|
|
|
|
if (length(t1) > length(t2)) return normalize(t1);
|
|
|
|
else return normalize(t2);
|
|
|
|
}
|
|
|
|
|
2017-11-02 12:10:01 +01:00
|
|
|
void main() {
|
|
|
|
vec4 g0 = texture(gbuffer0, texCoord);
|
|
|
|
float d = texture(gbufferD, texCoord).r * 2.0 - 1.0;
|
2017-11-04 16:16:07 +01:00
|
|
|
// if (d == 1.0) { fragColor.rgb = vec3(0.0); return; }
|
2017-11-02 12:10:01 +01:00
|
|
|
|
|
|
|
vec2 enc = g0.rg;
|
2017-11-04 16:16:07 +01:00
|
|
|
vec4 n;
|
2017-11-02 12:10:01 +01:00
|
|
|
n.z = 1.0 - abs(enc.x) - abs(enc.y);
|
|
|
|
n.xy = n.z >= 0.0 ? enc.xy : octahedronWrap(enc.xy);
|
2017-11-04 16:16:07 +01:00
|
|
|
n.xyz = normalize(n.xyz);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
n.w = 1.0;
|
|
|
|
n = tiV * n;
|
|
|
|
n.xyz = normalize(n.xyz);
|
|
|
|
|
2017-11-04 18:35:34 +01:00
|
|
|
vec3 p = getPosView(viewRay, d, cameraProj);
|
2017-11-04 16:16:07 +01:00
|
|
|
vec4 co;
|
|
|
|
|
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(n.xyz);
|
|
|
|
occ += distance(p, hitCoord);
|
|
|
|
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
vec3 o1 = normalize(tangent(n.xyz));
|
|
|
|
vec3 o2 = normalize(cross(o1, n.xyz));
|
|
|
|
vec3 c1 = 0.5f * (o1 + o2);
|
|
|
|
vec3 c2 = 0.5f * (o1 - o2);
|
|
|
|
const float angleMix = 0.5f;
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, o1, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, o2, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, -c1, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
|
|
|
|
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, -c2, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
#ifdef _SSGICone9
|
|
|
|
|
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, -o1, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, -o2, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, c1, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
|
|
|
|
|
|
|
hitCoord = p.xyz;
|
|
|
|
co = rayCast(mix(n.xyz, c2, angleMix));
|
|
|
|
#ifdef _RTGI
|
|
|
|
col += texture(gbuffer1, co.xy).rgb;
|
|
|
|
#endif
|
|
|
|
occ += distance(p, hitCoord);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const float strength = 2.0 * (1.0 / ssgiStrength);
|
2017-11-02 12:10:01 +01:00
|
|
|
|
2017-11-04 16:16:07 +01:00
|
|
|
#ifdef _RTGI
|
|
|
|
fragColor.rgb = vec3(occ * (col / 9.0) * strength);
|
|
|
|
#else
|
|
|
|
fragColor.rgb = vec3(occ * strength);
|
|
|
|
#endif
|
2017-11-02 12:10:01 +01:00
|
|
|
}
|