armory/raw/ssr_pass/ssr_pass.frag.glsl

248 lines
7.8 KiB
Plaintext
Raw Normal View History

2016-03-29 13:14:06 +02:00
#version 450
#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D tex;
2016-05-13 00:08:11 +02:00
uniform sampler2D gbufferD;
uniform sampler2D gbuffer0; // Normal
uniform sampler2D gbuffer1; // Color, roughness
2016-03-29 13:14:06 +02:00
uniform mat4 P;
2016-03-31 21:06:53 +02:00
uniform mat4 tiV;
const int maxSteps = 20;
const int numBinarySearchSteps = 5;
2016-03-29 13:14:06 +02:00
2016-04-12 14:44:21 +02:00
const float rayStep = 0.04;
const float minRayStep = 0.05;
2016-06-21 13:29:27 +02:00
const float searchDist = 5.0;
const float falloffExp = 5.0;
2016-03-29 13:14:06 +02:00
in vec3 viewRay;
2016-03-29 13:14:06 +02:00
in vec2 texCoord;
vec3 hitCoord;
2016-03-31 21:06:53 +02:00
float depth;
2016-06-21 13:29:27 +02:00
float rand(vec2 co) { // Unreliable
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}
2016-03-31 21:06:53 +02:00
vec4 getProjectedCoord(vec3 hitCoord) {
vec4 projectedCoord = P * vec4(hitCoord, 1.0);
projectedCoord.xy /= projectedCoord.w;
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
return projectedCoord;
}
2016-03-29 13:14:06 +02:00
vec3 getPos(float depth) {
const float znear = 0.1;
const float zfar = 1000.0;
const float projectionA = zfar / (zfar - znear);
const float projectionB = (-zfar * znear) / (zfar - znear);
float linearDepth = projectionB / (projectionA - depth);
return viewRay * linearDepth;
}
2016-04-17 16:07:54 +02:00
float getDeltaDepth(vec3 hitCoord) {
2016-05-13 00:08:11 +02:00
// depth = 1.0 - texture(gbuffer0, getProjectedCoord(hitCoord).xy).a;
depth = texture(gbufferD, getProjectedCoord(hitCoord).xy).r * 2.0 - 1.0;
vec3 viewPos = getPos(depth);
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-03-29 13:14:06 +02:00
// for (int i = 0; i < numBinarySearchSteps; i++) {
2016-03-31 21:06:53 +02:00
dir *= 0.5;
2016-04-12 14:44:21 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-03-29 13:14:06 +02:00
dir *= 0.5;
2016-04-12 14:44:21 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-03-29 13:14:06 +02:00
dir *= 0.5;
2016-04-12 14:44:21 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-03-29 13:14:06 +02:00
dir *= 0.5;
2016-04-12 14:44:21 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-03-29 13:14:06 +02:00
dir *= 0.5;
2016-04-12 14:44:21 +02:00
hitCoord -= dir;
if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-06-21 13:29:27 +02:00
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;
// dir *= 0.5;
// hitCoord -= dir;
// if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
2016-06-21 13:29:27 +02:00
// 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;
// dir *= 0.5;
// hitCoord -= dir;
// if (getDeltaDepth(hitCoord) < 0.0) hitCoord += dir;
// Ugly discard of hits too far away
if (abs(getDeltaDepth(hitCoord)) > 0.01) {
return vec4(0.0);
}
2016-03-29 13:14:06 +02:00
// }
2016-06-21 13:29:27 +02:00
return vec4(getProjectedCoord(hitCoord).xy, 0.0, 1.0);
2016-03-29 13:14:06 +02:00
}
2016-03-29 16:50:51 +02:00
vec4 rayCast(vec3 dir) {
2016-03-31 21:06:53 +02:00
dir *= rayStep;
2016-03-29 13:14:06 +02:00
// for (int i = 0; i < maxSteps; i++) {
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
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-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-04-12 14:44:21 +02:00
hitCoord += dir;
2016-06-21 13:29:27 +02:00
if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
////
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
// hitCoord += dir;
2016-06-21 13:29:27 +02:00
// if (getDeltaDepth(hitCoord) > 0.0) return binarySearch(dir);
2016-03-29 13:14:06 +02:00
// }
return vec4(0.0, 0.0, 0.0, 0.0);
}
2016-04-16 18:57:32 +02:00
vec2 octahedronWrap(vec2 v) {
return (1.0 - abs(v.yx)) * (vec2(v.x >= 0.0 ? 1.0 : -1.0, v.y >= 0.0 ? 1.0 : -1.0));
}
vec2 unpackFloat(float f) {
float index = floor(f) / 1000.0;
float alpha = fract(f);
return vec2(index, alpha);
}
2016-03-29 13:14:06 +02:00
void main() {
float roughness = unpackFloat(texture(gbuffer1, texCoord).a).x;
if (roughness == 1.0) {
2016-06-21 13:29:27 +02:00
// gl_FragColor = texture(tex, texCoord);
gl_FragColor = vec4(0.0);
return;
// discard;
}
float reflectivity = 1.0 - roughness;
2016-03-29 13:14:06 +02:00
vec4 g0 = texture(gbuffer0, texCoord);
2016-04-16 18:57:32 +02:00
vec2 enc = g0.rg;
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);
vec4 viewNormal = vec4(n, 1.0);
2016-06-21 13:29:27 +02:00
// if (viewNormal.z <= 0.9) {
// gl_FragColor = texture(tex, texCoord);
// gl_FragColor = vec4(0.0);
// return;
// discard; // Only up facing surfaces for now
2016-06-21 13:29:27 +02:00
// }
2016-04-12 14:44:21 +02:00
viewNormal = tiV * normalize(viewNormal);
2016-03-31 21:06:53 +02:00
2016-05-13 00:08:11 +02:00
// float d = 1.0 - g0.a;
float d = texture(gbufferD, texCoord).r * 2.0 - 1.0;
vec3 viewPos = getPos(d);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
vec3 reflected = normalize(reflect((viewPos.xyz), normalize(viewNormal.xyz)));
2016-03-31 21:06:53 +02:00
hitCoord = viewPos.xyz;
2016-03-29 13:14:06 +02:00
2016-06-21 13:29:27 +02:00
// vec3 dir = reflected * max(minRayStep, -viewPos.z);
vec3 dir = reflected * max(minRayStep, -viewPos.z) * (1.0 - rand(texCoord) * 0.4);
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
2016-03-31 21:06:53 +02:00
float intensity = pow(reflectivity, falloffExp) *
screenEdgeFactor * clamp(-reflected.z, 0.0, 1.0) *
clamp((searchDist - length(viewPos.xyz - hitCoord)) * (1.0 / searchDist), 0.0, 1.0) * coords.w;
2016-03-29 13:14:06 +02:00
vec4 texColor = texture(tex, texCoord);
2016-06-21 13:29:27 +02:00
// float brightness = dot(texColor.rgb, vec3(0.2126, 0.7152, 0.0722));
// intensity *= min(brightness, 1.0);
intensity = clamp(intensity, 0.0, 1.0);
if (intensity == 0.0) { //
gl_FragColor = vec4(0.0);
return;
// discard;
}
2016-03-31 21:06:53 +02:00
vec4 reflCol = vec4(texture(tex, coords.xy).rgb, 1.0);
2016-06-21 13:29:27 +02:00
reflCol = clamp(reflCol, 0.0, 1.0);
// gl_FragColor = texColor * (1.0 - intensity) + reflCol * intensity;
gl_FragColor = reflCol * intensity; //
2016-03-29 13:14:06 +02:00
}