armory/Shaders/ssr_pass/ssr_pass.frag.glsl

123 lines
3.4 KiB
Plaintext
Raw Permalink Normal View History

2016-03-29 13:14:06 +02:00
#version 450
#include "compiled.inc"
2017-12-13 14:21:42 +01:00
#include "std/math.glsl"
#include "std/gbuffer.glsl"
2016-07-10 00:51:39 +02:00
2016-03-29 13:14:06 +02:00
uniform sampler2D tex;
2016-05-13 00:08:11 +02:00
uniform sampler2D gbufferD;
2016-08-07 01:43:21 +02:00
uniform sampler2D gbuffer0; // Normal, roughness
2018-05-21 17:55:26 +02:00
uniform sampler2D gbuffer1; // basecol, spec
2016-03-29 13:14:06 +02:00
uniform mat4 P;
2018-12-06 15:23:08 +01:00
uniform mat3 V3;
2017-11-04 18:35:34 +01:00
uniform vec2 cameraProj;
2016-03-31 21:06:53 +02:00
#ifdef _CPostprocess
uniform vec3 PPComp9;
uniform vec3 PPComp10;
#endif
in vec3 viewRay;
2016-03-29 13:14:06 +02:00
in vec2 texCoord;
2016-10-12 17:52:27 +02:00
out vec4 fragColor;
2016-03-29 13:14:06 +02:00
vec3 hitCoord;
2016-03-31 21:06:53 +02:00
float depth;
2018-11-27 21:34:42 +01:00
const int numBinarySearchSteps = 7;
const int maxSteps = 18;
2018-12-05 17:47:45 +01:00
vec2 getProjectedCoord(const vec3 hit) {
vec4 projectedCoord = P * vec4(hit, 1.0);
2016-03-31 21:06:53 +02:00
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
2020-05-11 09:03:13 +02:00
#ifdef _InvY
2017-06-23 15:47:51 +02:00
projectedCoord.y = 1.0 - projectedCoord.y;
#endif
2017-01-17 14:48:47 +01:00
return projectedCoord.xy;
2016-03-31 21:06:53 +02:00
}
2016-03-29 13:14:06 +02:00
2020-05-11 09:03:13 +02:00
float getDeltaDepth(const vec3 hit) {
2018-12-05 17:47:45 +01:00
depth = textureLod(gbufferD, getProjectedCoord(hit), 0.0).r * 2.0 - 1.0;
2017-11-04 18:35:34 +01:00
vec3 viewPos = getPosView(viewRay, depth, cameraProj);
2018-12-05 17:47:45 +01:00
return viewPos.z - hit.z;
2016-03-31 21:06:53 +02:00
}
2018-12-05 17:47:45 +01:00
vec4 binarySearch(vec3 dir) {
float ddepth;
2018-12-06 15:23:08 +01:00
vec3 start = hitCoord;
2018-11-27 21:34:42 +01:00
for (int i = 0; i < numBinarySearchSteps; i++) {
2016-10-17 17:39:40 +02:00
dir *= 0.5;
hitCoord -= dir;
2018-12-05 17:47:45 +01:00
ddepth = getDeltaDepth(hitCoord);
if (ddepth < 0.0) hitCoord += dir;
2018-11-27 21:34:42 +01:00
}
// Ugly discard of hits too far away
#ifdef _CPostprocess
if (abs(ddepth) > PPComp9.z / 500) return vec4(0.0);
#else
if (abs(ddepth) > ssrSearchDist / 500) return vec4(0.0);
#endif
2017-01-17 14:48:47 +01:00
return vec4(getProjectedCoord(hitCoord), 0.0, 1.0);
2016-03-29 13:14:06 +02:00
}
2016-03-29 16:50:51 +02:00
vec4 rayCast(vec3 dir) {
#ifdef _CPostprocess
dir *= PPComp9.x;
#else
dir *= ssrRayStep;
#endif
2018-11-27 21:34:42 +01:00
for (int i = 0; i < maxSteps; i++) {
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-10-17 17:39:40 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2018-11-27 21:34:42 +01:00
}
2017-03-12 17:29:22 +01:00
return vec4(0.0);
}
2016-03-29 13:14:06 +02:00
void main() {
2018-12-05 17:47:45 +01:00
vec4 g0 = textureLod(gbuffer0, texCoord, 0.0);
float roughness = unpackFloat(g0.b).y;
2018-12-05 17:47:45 +01:00
if (roughness == 1.0) { fragColor.rgb = vec3(0.0); return; }
2016-08-07 01:43:21 +02:00
2018-12-05 17:47:45 +01:00
float spec = fract(textureLod(gbuffer1, texCoord, 0.0).a);
if (spec == 0.0) { fragColor.rgb = vec3(0.0); return; }
2020-05-11 09:03:13 +02:00
2018-12-05 17:47:45 +01:00
float d = textureLod(gbufferD, texCoord, 0.0).r * 2.0 - 1.0;
if (d == 1.0) { fragColor.rgb = vec3(0.0); return; }
2016-08-07 01:43:21 +02:00
2016-04-16 18:57:32 +02:00
vec2 enc = g0.rg;
2016-10-17 17:39:40 +02: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);
2020-05-11 09:03:13 +02:00
2018-12-06 15:23:08 +01:00
vec3 viewNormal = V3 * n;
2017-11-04 18:35:34 +01:00
vec3 viewPos = getPosView(viewRay, d, cameraProj);
2018-12-06 15:23:08 +01:00
vec3 reflected = normalize(reflect(viewPos, viewNormal));
hitCoord = viewPos;
2020-05-11 09:03:13 +02:00
#ifdef _CPostprocess
vec3 dir = reflected * (1.0 - rand(texCoord) * PPComp10.y * roughness) * 2.0;
#else
vec3 dir = reflected * (1.0 - rand(texCoord) * ssrJitter * roughness) * 2.0;
#endif
2020-05-11 09:03:13 +02:00
2018-12-06 15:23:08 +01:00
// * max(ssrMinRayStep, -viewPos.z)
2016-04-27 10:29:32 +02:00
vec4 coords = rayCast(dir);
2016-03-29 13:14:06 +02:00
2016-03-31 21:06:53 +02:00
vec2 deltaCoords = abs(vec2(0.5, 0.5) - coords.xy);
float screenEdgeFactor = clamp(1.0 - (deltaCoords.x + deltaCoords.y), 0.0, 1.0);
2016-03-29 13:14:06 +02:00
2017-03-12 17:29:22 +01:00
float reflectivity = 1.0 - roughness;
#ifdef _CPostprocess
float intensity = pow(reflectivity, PPComp10.x) * screenEdgeFactor * clamp(-reflected.z, 0.0, 1.0) * clamp((PPComp9.z - length(viewPos - hitCoord)) * (1.0 / PPComp9.z), 0.0, 1.0) * coords.w;
#else
float intensity = pow(reflectivity, ssrFalloffExp) * screenEdgeFactor * clamp(-reflected.z, 0.0, 1.0) * clamp((ssrSearchDist - length(viewPos - hitCoord)) * (1.0 / ssrSearchDist), 0.0, 1.0) * coords.w;
#endif
2016-03-29 13:14:06 +02:00
2016-06-21 13:29:27 +02:00
intensity = clamp(intensity, 0.0, 1.0);
2018-12-05 17:47:45 +01:00
vec3 reflCol = textureLod(tex, coords.xy, 0.0).rgb;
2016-06-21 13:29:27 +02:00
reflCol = clamp(reflCol, 0.0, 1.0);
2018-11-27 21:34:42 +01:00
fragColor.rgb = reflCol * intensity * 0.5;
2016-03-29 13:14:06 +02:00
}