2014-03-01 14:08:17 -05:00
|
|
|
uniform sampler2D ntex;
|
2014-10-05 20:19:21 -04:00
|
|
|
uniform sampler2D dtex;
|
2019-03-09 05:10:47 -05:00
|
|
|
uniform sampler2D albedo;
|
2014-03-01 14:08:17 -05:00
|
|
|
|
2016-09-12 15:39:52 -04:00
|
|
|
#ifdef GL_ES
|
|
|
|
layout (location = 0) out vec4 Diff;
|
|
|
|
layout (location = 1) out vec4 Spec;
|
|
|
|
#else
|
2014-03-01 14:08:17 -05:00
|
|
|
out vec4 Diff;
|
2014-10-05 20:19:21 -04:00
|
|
|
out vec4 Spec;
|
2016-09-12 15:39:52 -04:00
|
|
|
#endif
|
2014-03-01 14:08:17 -05:00
|
|
|
|
2016-06-27 07:11:27 -04:00
|
|
|
#stk_include "utils/decodeNormal.frag"
|
|
|
|
#stk_include "utils/getPosFromUVDepth.frag"
|
|
|
|
#stk_include "utils/DiffuseIBL.frag"
|
|
|
|
#stk_include "utils/SpecularIBL.frag"
|
2014-03-01 14:08:17 -05:00
|
|
|
|
2019-03-08 16:37:42 -05:00
|
|
|
|
|
|
|
float makeLinear(float f, float n, float z)
|
|
|
|
{
|
2019-06-26 04:25:13 -04:00
|
|
|
return (2.0f * n) / (f + n - z * (f - n));
|
2019-03-08 16:37:42 -05:00
|
|
|
}
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
vec3 CalcViewPositionFromDepth(in vec2 TexCoord)
|
2019-03-08 16:37:42 -05:00
|
|
|
{
|
|
|
|
// Combine UV & depth into XY & Z (NDC)
|
2021-08-30 04:34:02 -04:00
|
|
|
float z = makeLinear(1000.0, 1.0, textureLod(dtex, TexCoord, 0.).x);
|
2019-03-08 16:37:42 -05:00
|
|
|
vec3 rawPosition = vec3(TexCoord, z);
|
|
|
|
|
|
|
|
// Convert from (0, 1) range to (-1, 1)
|
2019-06-26 04:25:13 -04:00
|
|
|
vec4 ScreenSpacePosition = vec4( rawPosition * 2.0 - 1.0, 1.0);
|
2019-03-08 16:37:42 -05:00
|
|
|
|
|
|
|
// Undo Perspective transformation to bring into view space
|
|
|
|
vec4 ViewPosition = u_inverse_projection_matrix * ScreenSpacePosition;
|
|
|
|
|
|
|
|
// Perform perspective divide and return
|
|
|
|
return ViewPosition.xyz / ViewPosition.w;
|
|
|
|
}
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
// Fade out edges of screen buffer tex
|
|
|
|
// 1 means full render tex, 0 means full IBL tex
|
|
|
|
float GetEdgeFade(vec2 coords)
|
2019-06-24 17:31:51 -04:00
|
|
|
{
|
2021-08-30 04:34:02 -04:00
|
|
|
float gradL = smoothstep(0.0, 0.4, coords.x);
|
|
|
|
float gradR = 1.0 - smoothstep(0.6, 1.0, coords.x);
|
|
|
|
float gradT = smoothstep(0.0, 0.4, coords.y);
|
|
|
|
float gradB = 1.0 - smoothstep(0.6, 1.0, coords.y);
|
|
|
|
return min(min(gradL, gradR), min(gradT, gradB));
|
2019-06-24 17:31:51 -04:00
|
|
|
}
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
vec2 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
|
2019-03-08 16:37:42 -05:00
|
|
|
{
|
|
|
|
dir *= 0.25f;
|
|
|
|
|
2019-03-10 18:25:49 -04:00
|
|
|
for(int i = 0; i < 8; ++i) {
|
2019-03-08 16:37:42 -05:00
|
|
|
hitCoord += dir;
|
|
|
|
|
|
|
|
vec4 projectedCoord = u_projection_matrix * vec4(hitCoord, 1.0);
|
|
|
|
projectedCoord.xy /= projectedCoord.w;
|
|
|
|
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
float depth = CalcViewPositionFromDepth(projectedCoord.xy).z;
|
2019-03-08 16:37:42 -05:00
|
|
|
dDepth = hitCoord.z - depth;
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
if (dDepth < 0.0)
|
2019-03-08 16:37:42 -05:00
|
|
|
{
|
2021-08-30 04:34:02 -04:00
|
|
|
if (projectedCoord.x > 0.0 && projectedCoord.x < 1.0 &&
|
|
|
|
projectedCoord.y > 0.0 && projectedCoord.y < 1.0)
|
2019-03-08 16:37:42 -05:00
|
|
|
{
|
2021-08-30 04:34:02 -04:00
|
|
|
return projectedCoord.xy;
|
2019-03-08 16:37:42 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-30 04:34:02 -04:00
|
|
|
return vec2(0.f);
|
2019-03-08 16:37:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
return vec2(0.f);
|
2019-03-08 16:37:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Main ===================================================================
|
|
|
|
|
2014-03-01 14:08:17 -05:00
|
|
|
void main(void)
|
|
|
|
{
|
2017-12-25 01:00:10 -05:00
|
|
|
vec2 uv = gl_FragCoord.xy / u_screen;
|
2014-03-01 14:08:17 -05:00
|
|
|
vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
2014-10-05 20:19:21 -04:00
|
|
|
|
2014-12-07 13:10:22 -05:00
|
|
|
Diff = vec4(0.25 * DiffuseIBL(normal), 1.);
|
2014-10-05 20:19:21 -04:00
|
|
|
|
|
|
|
float z = texture(dtex, uv).x;
|
|
|
|
|
2017-12-25 01:00:10 -05:00
|
|
|
vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix);
|
2014-10-05 20:19:21 -04:00
|
|
|
vec3 eyedir = -normalize(xpos.xyz);
|
2019-03-08 16:37:42 -05:00
|
|
|
// Extract roughness
|
2014-11-20 20:44:27 -05:00
|
|
|
float specval = texture(ntex, uv).z;
|
2014-12-07 13:10:22 -05:00
|
|
|
|
2019-03-11 17:27:15 -04:00
|
|
|
#ifdef GL_ES
|
2014-12-08 13:36:23 -05:00
|
|
|
Spec = vec4(.25 * SpecularIBL(normal, eyedir, specval), 1.);
|
2019-03-11 17:27:15 -04:00
|
|
|
#else
|
2019-06-24 17:31:51 -04:00
|
|
|
// :::::::: Compute Space Screen Reflection ::::::::::::::::::::::::::::::::::::
|
2019-03-08 16:37:42 -05:00
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
// Output color
|
|
|
|
vec3 outColor;
|
2019-03-08 16:37:42 -05:00
|
|
|
|
2019-06-24 17:31:51 -04:00
|
|
|
// Fallback (if the ray can't find an intersection we display the sky)
|
2019-03-11 16:35:55 -04:00
|
|
|
vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval);
|
|
|
|
|
2021-08-30 04:34:02 -04:00
|
|
|
// Only calculate reflections if the reflectivity value is high enough,
|
|
|
|
// otherwise just use specular IBL
|
|
|
|
if (specval > 0.5)
|
|
|
|
{
|
|
|
|
vec3 View_Pos = CalcViewPositionFromDepth(uv);
|
|
|
|
|
|
|
|
// Reflection vector
|
|
|
|
vec3 reflected = normalize(reflect(eyedir, normal));
|
|
|
|
|
|
|
|
// Ray cast
|
|
|
|
vec3 hitPos = View_Pos.xyz;
|
|
|
|
float dDepth;
|
|
|
|
float minRayStep = 50.0f;
|
|
|
|
|
|
|
|
vec2 coords = RayCast(reflected * max(minRayStep, -View_Pos.z),
|
|
|
|
hitPos, dDepth);
|
|
|
|
|
|
|
|
if (coords.x == 0.0 && coords.y == 0.0) {
|
|
|
|
outColor = fallback;
|
|
|
|
} else {
|
|
|
|
// FIXME We need to generate mipmap to take into account the gloss map
|
|
|
|
outColor = textureLod(albedo, coords, 0.f).rgb;
|
|
|
|
outColor = mix(fallback, outColor, GetEdgeFade(coords));
|
|
|
|
// TODO temporary measure the lack of mipmapping for RTT albedo
|
|
|
|
// Implement it in proper way
|
|
|
|
// Use (specval - 0.5) * 2.0 to bring specval from 0.5-1.0 range to 0.0-1.0 range
|
|
|
|
outColor = mix(fallback, outColor, (specval - 0.5) * 2.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outColor = fallback;
|
|
|
|
}
|
|
|
|
|
2019-03-08 16:37:42 -05:00
|
|
|
Spec = vec4(outColor.rgb, 1.0);
|
2019-03-11 17:27:15 -04:00
|
|
|
#endif
|
2019-03-08 16:37:42 -05:00
|
|
|
|
2014-03-01 14:08:17 -05:00
|
|
|
}
|