armory/Shaders/ssr_pass/ssr_pass.frag.glsl

188 lines
5.1 KiB
Plaintext
Raw 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;
2016-03-31 21:06:53 +02:00
uniform mat4 tiV;
2017-11-04 18:35:34 +01:00
uniform vec2 cameraProj;
2016-03-31 21:06:53 +02:00
// const int maxSteps = 20;
// const int numBinarySearchSteps = 5;
// const float ssrRayStep = 0.04;
// const float ssrMinRayStep = 0.05;
// const float ssrSearchDist = 5.0;
// const float ssrFalloffExp = 5.0;
// const float ssrJitter = 0.6;
2016-03-29 13:14:06 +02:00
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;
2017-01-17 14:48:47 +01:00
vec2 getProjectedCoord(vec3 hitCoord) {
2016-03-31 21:06:53 +02:00
vec4 projectedCoord = P * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
2017-06-23 15:47:51 +02:00
#ifdef _InvY
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
float getDeltaDepth(vec3 hitCoord) {
2017-01-17 14:48:47 +01:00
// depth = 1.0 - texture(gbuffer0, getProjectedCoord(hitCoord)).a;
depth = texture(gbufferD, getProjectedCoord(hitCoord)).r * 2.0 - 1.0;
2017-11-04 18:35:34 +01:00
vec3 viewPos = getPosView(viewRay, depth, cameraProj);
return viewPos.z - hitCoord.z;
2016-03-31 21:06:53 +02:00
}
2016-06-21 13:29:27 +02:00
vec4 binarySearch(vec3 dir) {
2016-10-17 17:39:40 +02:00
// for (int i = 0; i < numBinarySearchSteps; i++) {
2016-03-31 21:06:53 +02:00
dir *= 0.5;
2016-10-17 17:39:40 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-03-29 13:14:06 +02:00
2016-06-21 13:29:27 +02:00
dir *= 0.5;
2016-10-17 17:39:40 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
dir *= 0.5;
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
dir *= 0.5;
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
dir *= 0.5;
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-06-21 13:29:27 +02:00
dir *= 0.5;
2016-10-17 17:39:40 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
dir *= 0.5;
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-06-21 13:29:27 +02:00
// Ugly discard of hits too far away
if (abs(getDeltaDepth(hitCoord)) > 0.01) {
return vec4(0.0);
}
2016-10-17 17:39:40 +02:00
// }
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) {
dir *= ssrRayStep;
2016-03-29 13:14:06 +02:00
2016-10-17 17:39:40 +02:00
// for (int i = 0; i < maxSteps; i++) {
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-03-31 21:06:53 +02:00
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
// }
2017-03-12 17:29:22 +01:00
return vec4(0.0);
}
2016-03-29 13:14:06 +02:00
void main() {
2016-08-07 01:43:21 +02:00
vec4 g0 = texture(gbuffer0, texCoord);
2016-10-17 17:39:40 +02:00
float roughness = unpackFloat(g0.b).y;
2016-08-07 01:43:21 +02:00
2016-10-17 17:39:40 +02:00
if (roughness == 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb = vec3(0.0);
return;
}
2018-05-21 17:55:26 +02:00
2018-05-26 16:39:10 +02:00
float spec = fract(texture(gbuffer1, texCoord).a);
2018-05-21 17:55:26 +02:00
if (spec == 0.0) {
fragColor.rgb = vec3(0.0);
return;
}
2016-03-29 13:14:06 +02:00
2016-07-12 00:09:02 +02:00
float d = texture(gbufferD, texCoord).r * 2.0 - 1.0;
if (d == 1.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb = vec3(0.0);
2016-07-12 00:09:02 +02:00
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);
2016-04-16 18:57:32 +02:00
2018-03-26 23:23:27 +02:00
#ifdef _SSRZOnly
if (n.z <= 0.9) {
2017-03-12 17:29:22 +01:00
fragColor.rgb = vec3(0.0);
return;
2016-10-17 00:02:51 +02:00
}
2018-03-26 23:23:27 +02:00
#endif
2017-03-12 17:29:22 +01:00
vec4 viewNormal = vec4(n, 1.0);
viewNormal = tiV * viewNormal;
2017-11-04 18:35:34 +01:00
vec3 viewPos = getPosView(viewRay, d, cameraProj);
2016-03-31 21:06:53 +02:00
2017-03-12 17:29:22 +01:00
vec3 reflected = normalize(reflect((viewPos), normalize(viewNormal.xyz)));
2016-03-31 21:06:53 +02:00
hitCoord = viewPos.xyz;
2016-03-29 13:14:06 +02:00
vec3 dir = reflected * max(ssrMinRayStep, -viewPos.z) * (1.0 - rand(texCoord) * ssrJitter * roughness);
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;
float intensity = pow(reflectivity, ssrFalloffExp) *
2016-03-31 21:06:53 +02:00
screenEdgeFactor * clamp(-reflected.z, 0.0, 1.0) *
clamp((ssrSearchDist - length(viewPos.xyz - hitCoord)) * (1.0 / ssrSearchDist), 0.0, 1.0) * coords.w;
2016-03-29 13:14:06 +02:00
2016-06-21 13:29:27 +02:00
intensity = clamp(intensity, 0.0, 1.0);
2016-08-09 23:51:40 +02:00
if (intensity == 0.0) {
2017-03-12 17:29:22 +01:00
fragColor.rgb = vec3(0.0);
2016-06-21 13:29:27 +02:00
return;
}
2017-03-12 17:29:22 +01:00
vec3 reflCol = texture(tex, coords.xy).rgb;
2016-06-21 13:29:27 +02:00
reflCol = clamp(reflCol, 0.0, 1.0);
2017-03-12 17:29:22 +01:00
fragColor.rgb = reflCol * intensity * 0.5; //
2016-03-29 13:14:06 +02:00
}