armory/raw/ssr_pass/ssr_pass.frag.glsl

231 lines
6.9 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;
uniform sampler2D gbuffer0; // Normal, depth
uniform sampler2D gbuffer1; // Position, roughness
uniform sampler2D gbuffer2;
uniform mat4 P;
uniform mat4 V;
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;
const float searchDist = 4.4;
const float falloffExp = 3.0;
2016-03-31 21:06:53 +02:00
// uniform float rayStep;
// uniform float minRayStep;
// uniform float searchDist;
2016-04-12 14:44:21 +02:00
// uniform float falloffExp;
2016-03-29 13:14:06 +02:00
2016-04-17 16:07:54 +02:00
in vec3 vViewRay;
2016-03-29 13:14:06 +02:00
in vec2 texCoord;
vec3 hitCoord;
2016-03-31 21:06:53 +02:00
float depth;
// float rand(vec2 co) { // Unreliable
// return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
// }
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
2016-03-31 21:06:53 +02:00
float getDeltaDepth(vec3 hitCoord) {
2016-04-17 16:07:54 +02:00
float d = texture(gbuffer0, getProjectedCoord(hitCoord).xy).a;
const float zNear = 0.1;
const float zFar = 1000.0;
float delinDepth = (-(d*(zFar-zNear)+zNear));
// vec3 reconViewPos = vViewRay * delinDepth;
// vec3 p = vec4(invV * vec4(reconViewPos, 1.0)).xyz;
// vec4 worldPos = vec4(p, 1.0);
// vec4 viewPos = vec4(texture(gbuffer1, getProjectedCoord(hitCoord).xy).rgb, 1.0);
// viewPos = V * viewPos;
// float depth = viewPos.z;
// return depth - hitCoord.z;
return delinDepth - hitCoord.z;
2016-03-31 21:06:53 +02:00
}
vec3 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-03-29 13:14:06 +02:00
// }
2016-03-31 21:06:53 +02:00
return vec3(getProjectedCoord(hitCoord).xy, depth);
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;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
2016-03-31 21:06:53 +02:00
2016-04-12 14:44:21 +02:00
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
hitCoord += dir;
if (getDeltaDepth(hitCoord) > 0.0) return vec4(binarySearch(dir), 1.0);
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));
}
2016-03-29 13:14:06 +02:00
void main() {
float roughness = texture(gbuffer1, texCoord).a;
2016-03-31 21:06:53 +02:00
float reflectivity = 1.0 - roughness;
if (reflectivity == 0.0) {
2016-04-12 14:44:21 +02:00
discard;
2016-03-29 13:14:06 +02:00
}
2016-04-16 18:57:32 +02:00
vec4 g0 = vec4(texture(gbuffer0, texCoord).rgb, 1.0);
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);//vec4(texture(gbuffer0, texCoord).rgb, 1.0);
2016-04-12 14:44:21 +02:00
if (viewNormal.z <= 0.9) discard; // Only up facing surfaces for now
viewNormal = tiV * normalize(viewNormal);
2016-03-31 21:06:53 +02:00
2016-04-17 16:07:54 +02:00
// vec4 viewPos = vec4(texture(gbuffer1, texCoord).rgb, 1.0);
// viewPos = V * viewPos;
float d = texture(gbuffer0, texCoord).a;
const float zNear = 0.1;
const float zFar = 1000.0;
float delinDepth = (-(d*(zFar-zNear)+zNear));
vec3 viewPos = vViewRay * delinDepth;
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-04-12 14:44:21 +02:00
vec3 dir = reflected * max(minRayStep, -viewPos.z);// * (1.0 - rand(texCoord) * 0.5);
2016-03-31 21:06:53 +02:00
vec4 coords = rayCast(dir);
2016-04-12 14:44:21 +02:00
// TODO: prevent sampling coords from envmap
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-03-31 21:06:53 +02:00
vec4 reflCol = vec4(texture(tex, coords.xy).rgb, 1.0);
2016-03-29 13:14:06 +02:00
gl_FragColor = texColor * (1.0 - intensity) + reflCol * intensity;
}