From 025001993db99b65043a6bcc55e6fb9acd2824ff Mon Sep 17 00:00:00 2001 From: samuncle Date: Fri, 8 Mar 2019 22:37:42 +0100 Subject: [PATCH 01/18] Add space screen reflexion shader --- data/shaders/IBL.frag | 134 ++++++++++++++++++++++++ data/shaders/combine_diffuse_color.frag | 2 +- data/shaders/sp_solid.frag | 1 + 3 files changed, 136 insertions(+), 1 deletion(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 914e38f07..7a074a8f8 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -1,5 +1,6 @@ uniform sampler2D ntex; uniform sampler2D dtex; +uniform sampler2D colorBuffer; #ifdef GL_ES layout (location = 0) out vec4 Diff; @@ -14,6 +15,87 @@ out vec4 Spec; #stk_include "utils/DiffuseIBL.frag" #stk_include "utils/SpecularIBL.frag" + +vec3 getXcYcZc(int x, int y, float zC) +{ + // We use perspective symetric projection matrix hence P(0,2) = P(1, 2) = 0 + float xC= (2. * (float(x)) / u_screen.x - 1.) * zC / u_projection_matrix[0][0]; + float yC= (2. * (float(y)) / u_screen.y - 1.) * zC / u_projection_matrix[1][1]; + return vec3(xC, yC, zC); +} + +float makeLinear(float f, float n, float z) +{ + return pow(z, 10);//(2 * n) / (f + n - z * (f - n)); +} + + +vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) +{ + // Combine UV & depth into XY & Z (NDC) + float z = makeLinear(1000.0, 0.01, textureLod(DepthMap, TexCoord, 0.).x); + vec3 rawPosition = vec3(TexCoord, z); + + // Convert from (0, 1) range to (-1, 1) + vec4 ScreenSpacePosition = vec4( rawPosition * 2 - 1, 1); + + // 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; +} + + +vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback) +{ + dir *= 0.25f; + + for(int i = 0; i < 10; ++i) { + hitCoord += dir; + + vec4 projectedCoord = u_projection_matrix * vec4(hitCoord, 1.0); + projectedCoord.xy /= projectedCoord.w; + projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5; + + float depth = CalcViewPositionFromDepth(projectedCoord.xy, DepthMap).z; + dDepth = hitCoord.z - depth; + + if(dDepth < 0.0) + { + //return vec3(1.0, 0.0, 0.0); + if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) + { + // Mix with fallback (black area should be dark anyway) + vec3 finalColor = textureLod(ntex, projectedCoord.xy, 1.0).rgb; + if ((finalColor.r + finalColor.g + finalColor.b) > 0.) + { + return finalColor; + } + else + { + return fallback; + } + } + else + { + return fallback; + } + //return textureLod(ntex, vec2(clamp(projectedCoord.x, 0.1, 0.9), clamp(projectedCoord.y, 0.1, 0.9)), 1.0).rgb; + // return projectedCoord.xy; + } + } + + return fallback; +} + + + + + + +// Main =================================================================== + void main(void) { vec2 uv = gl_FragCoord.xy / u_screen; @@ -25,7 +107,59 @@ void main(void) vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix); vec3 eyedir = -normalize(xpos.xyz); + // Extract roughness float specval = texture(ntex, uv).z; Spec = vec4(.25 * SpecularIBL(normal, eyedir, specval), 1.); + + // Compute Space Screen Reflection ========================================================= + + float lineardepth = textureLod(dtex, uv, 0.).x; + int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y); + vec3 FragPos = getXcYcZc(x, y, lineardepth); + + // Better implementation: ::::::::::::::::::::::::::::::::::: + + vec3 View_Normal = normal; + float View_Depth = makeLinear(1000.0, 0.01, lineardepth); + vec3 ScreenPos = xpos.xyz; + vec4 View_Pos = u_inverse_projection_matrix * vec4(ScreenPos, 1.0f); + View_Pos /= View_Pos.w; + + // Reflection vector + vec3 reflected = normalize(reflect(eyedir, normal)); // normalize(reflect(normalize(View_Pos.xyz), normalize(View_Normal))); + + // Ray cast + vec3 hitPos = View_Pos.xyz; + float dDepth; + float minRayStep = 100.0f; + // Fallback + vec3 fallback = .25 * SpecularIBL(normal, eyedir, 1.); + vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback); + + // float lodval = 7. * (1 - specval); + //vec4 finalColor = textureLod(ntex, coords, lodval); + + + vec2 inside = uv - 0.5; + float vignette = 1. - dot(inside, inside) * 3; + vignette = clamp(pow(vignette, 0.8), 0., 1.); + + + Spec = vec4(outColor.rgb, 1.0); + + // Normal vis + + vec3 reflection = reflect(eyedir, normal); + + float red = 0.5+0.5*reflection.r; + float green = 0.5+0.5*reflection.g; + float blue = 0.5+0.5*reflection.b; + + + + //Diff = vec4(red, green, blue, 1.0); + + //Diff = vec4(reflection.rgb, 1.0); + } diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 3afbec781..9f32a830e 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -58,5 +58,5 @@ void main() color_2.g = ls.g + color_1.g * (1.0 - ls.a); color_2.b = ls.b + color_1.b * (1.0 - ls.a); color_2.a = ls.a + color_1.a * (1.0 - ls.a); - o_final_color = color_2; + o_final_color = vec4(SpecularComponent, 1.0); } diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index 9a68416ed..a620094fe 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -39,6 +39,7 @@ void main(void) o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_color.zw = layer_2.xy; + o_normal_color.z = 1.0; #else o_diffuse_color = vec4(final_color, 1.0); #endif From cb8f9ecf430638bb844c51c0fd7a95121d215544 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sat, 9 Mar 2019 00:41:59 +0100 Subject: [PATCH 02/18] Minor changes --- data/shaders/IBL.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 7a074a8f8..2f64e86f5 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -134,7 +134,7 @@ void main(void) float dDepth; float minRayStep = 100.0f; // Fallback - vec3 fallback = .25 * SpecularIBL(normal, eyedir, 1.); + vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback); // float lodval = 7. * (1 - specval); From 13d121e45d84d40f5820b8e1c648eb7934b0e670 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sat, 9 Mar 2019 00:44:35 +0100 Subject: [PATCH 03/18] remove dead code --- data/shaders/IBL.frag | 1 - 1 file changed, 1 deletion(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 2f64e86f5..3759cdb68 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -63,7 +63,6 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth if(dDepth < 0.0) { - //return vec3(1.0, 0.0, 0.0); if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) { // Mix with fallback (black area should be dark anyway) From a5e40f9a8a2678877c2f16c2004b15081377737d Mon Sep 17 00:00:00 2001 From: samuncle Date: Sat, 9 Mar 2019 11:10:47 +0100 Subject: [PATCH 04/18] Add color buffer to compute proper space screen reflexion --- data/shaders/IBL.frag | 6 +++--- src/graphics/lighting_passes.cpp | 15 ++++++++++----- src/graphics/lighting_passes.hpp | 4 +++- src/graphics/shader_based_renderer.cpp | 1 + 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 3759cdb68..b1e015485 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -1,6 +1,6 @@ uniform sampler2D ntex; uniform sampler2D dtex; -uniform sampler2D colorBuffer; +uniform sampler2D albedo; #ifdef GL_ES layout (location = 0) out vec4 Diff; @@ -26,7 +26,7 @@ vec3 getXcYcZc(int x, int y, float zC) float makeLinear(float f, float n, float z) { - return pow(z, 10);//(2 * n) / (f + n - z * (f - n)); + return (2 * n) / (f + n - z * (f - n)); } @@ -66,7 +66,7 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) { // Mix with fallback (black area should be dark anyway) - vec3 finalColor = textureLod(ntex, projectedCoord.xy, 1.0).rgb; + vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { return finalColor; diff --git a/src/graphics/lighting_passes.cpp b/src/graphics/lighting_passes.cpp index 051a59b17..159e816fd 100644 --- a/src/graphics/lighting_passes.cpp +++ b/src/graphics/lighting_passes.cpp @@ -168,7 +168,7 @@ public: }; // ============================================================================ -class IBLShader : public TextureShader +class IBLShader : public TextureShader { public: IBLShader() @@ -178,7 +178,8 @@ public: assignUniforms(); assignSamplerNames(0, "ntex", ST_NEAREST_FILTERED, 1, "dtex", ST_NEAREST_FILTERED, - 2, "probe", ST_TRILINEAR_CUBEMAP); + 2, "probe", ST_TRILINEAR_CUBEMAP, + 3, "albedo",ST_NEAREST_FILTERED); } // IBLShader }; // IBLShader @@ -293,7 +294,8 @@ static void renderPointLights(unsigned count, // ---------------------------------------------------------------------------- void LightingPasses::renderEnvMap(GLuint normal_depth_texture, GLuint depth_stencil_texture, - GLuint specular_probe) + GLuint specular_probe, + GLuint albedo_buffer) { glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); @@ -317,7 +319,8 @@ void LightingPasses::renderEnvMap(GLuint normal_depth_texture, IBLShader::getInstance()->setTextureUnits( normal_depth_texture, depth_stencil_texture, - specular_probe); + specular_probe, + albedo_buffer); IBLShader::getInstance()->setUniforms(); } @@ -426,6 +429,7 @@ void LightingPasses::updateLightsInfo(scene::ICameraSceneNode * const camnode, void LightingPasses::renderLights( bool has_shadow, GLuint normal_depth_texture, GLuint depth_stencil_texture, + GLuint albedo_texture, const FrameBuffer* shadow_framebuffer, GLuint specular_probe) { @@ -433,7 +437,8 @@ void LightingPasses::renderLights( bool has_shadow, ScopedGPUTimer timer(irr_driver->getGPUTimer(Q_ENVMAP)); renderEnvMap(normal_depth_texture, depth_stencil_texture, - specular_probe); + specular_probe, + albedo_texture); } // Render sunlight if and only if track supports shadow diff --git a/src/graphics/lighting_passes.hpp b/src/graphics/lighting_passes.hpp index 522c2782b..441706471 100644 --- a/src/graphics/lighting_passes.hpp +++ b/src/graphics/lighting_passes.hpp @@ -34,7 +34,8 @@ private: void renderEnvMap(GLuint normal_depth_texture, GLuint depth_stencil_texture, - GLuint specular_probe); + GLuint specular_probe, + GLuint albedo_buffer); /** Generate diffuse and specular map */ void renderSunlight(const core::vector3df &direction, @@ -50,6 +51,7 @@ public: void renderLights( bool has_shadow, GLuint normal_depth_texture, GLuint depth_stencil_texture, + GLuint albedo_texture, const FrameBuffer* shadow_framebuffer, GLuint specular_probe); void renderLightsScatter(GLuint depth_stencil_texture, diff --git a/src/graphics/shader_based_renderer.cpp b/src/graphics/shader_based_renderer.cpp index 433a15a01..70924b3dd 100644 --- a/src/graphics/shader_based_renderer.cpp +++ b/src/graphics/shader_based_renderer.cpp @@ -279,6 +279,7 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca m_lighting_passes.renderLights( hasShadow, m_rtts->getRenderTarget(RTT_NORMAL_AND_DEPTH), m_rtts->getDepthStencilTexture(), + m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR), m_rtts->getShadowFrameBuffer(), specular_probe); PROFILER_POP_CPU_MARKER(); From 11562a541c09ee51ec7da9316ec4d148d8066086 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sun, 10 Mar 2019 09:20:19 +0100 Subject: [PATCH 05/18] Experimental blur test --- data/shaders/IBL.frag | 53 +++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index b1e015485..2a255d341 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -29,6 +29,28 @@ float makeLinear(float f, float n, float z) return (2 * n) / (f + n - z * (f - n)); } +vec3 fastBlur(vec2 uv) +{ + vec4 sum = vec4(0.0); + float X = uv.x; + float Y = uv.y; + vec2 pixel = vec2(0.001); +/* + sum += textureLod(albedo, vec2(X, Y - 3.0 * pixel.y), 1.0) * 0.03125; + sum += textureLod(albedo, vec2(X, Y - 1.3333 * pixel.y), 1.0) * 0.328125; + sum += textureLod(albedo, vec2(X, Y), 1.0) * 0.273438; + sum += textureLod(albedo, vec2(X, Y + 1.3333 * pixel.y), 1.0) * 0.328125; + sum += textureLod(albedo, vec2(X, Y + 3.0 * pixel.y), 1.0) * 0.03125;*/ + + sum += textureLod(albedo, vec2(X - 3.0 * pixel.x, Y), 1.0) * 0.03125; + sum += textureLod(albedo, vec2(X - 1.3333 * pixel.x, Y), 1.0) * 0.328125; + sum += textureLod(albedo, vec2(X, Y), 1.0) * 0.273438; + sum += textureLod(albedo, vec2(X + 1.3333 * pixel.x, Y), 1.0) * 0.328125; + sum += textureLod(albedo, vec2(X + 3.0 * pixel.x, Y), 1.0) * 0.03125; + + return sum.rgb; +} + vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) { @@ -67,9 +89,13 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth { // Mix with fallback (black area should be dark anyway) vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; + finalColor = fastBlur(projectedCoord.xy); if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { - return finalColor; + vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; + float vignette = 1. - dot(inside, inside) * 3; + vignette = clamp(pow(vignette, 10.0), 0., 0.2); + return mix(fallback, finalColor, vignette); } else { @@ -80,8 +106,6 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth { return fallback; } - //return textureLod(ntex, vec2(clamp(projectedCoord.x, 0.1, 0.9), clamp(projectedCoord.y, 0.1, 0.9)), 1.0).rgb; - // return projectedCoord.xy; } } @@ -119,14 +143,13 @@ void main(void) // Better implementation: ::::::::::::::::::::::::::::::::::: - vec3 View_Normal = normal; - float View_Depth = makeLinear(1000.0, 0.01, lineardepth); + float View_Depth = makeLinear(1000.0, 0.001, lineardepth); vec3 ScreenPos = xpos.xyz; vec4 View_Pos = u_inverse_projection_matrix * vec4(ScreenPos, 1.0f); View_Pos /= View_Pos.w; // Reflection vector - vec3 reflected = normalize(reflect(eyedir, normal)); // normalize(reflect(normalize(View_Pos.xyz), normalize(View_Normal))); + vec3 reflected = normalize(reflect(eyedir, normal)); // Ray cast vec3 hitPos = View_Pos.xyz; @@ -136,27 +159,9 @@ void main(void) vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback); - // float lodval = 7. * (1 - specval); - //vec4 finalColor = textureLod(ntex, coords, lodval); - - - vec2 inside = uv - 0.5; - float vignette = 1. - dot(inside, inside) * 3; - vignette = clamp(pow(vignette, 0.8), 0., 1.); - Spec = vec4(outColor.rgb, 1.0); - // Normal vis - - vec3 reflection = reflect(eyedir, normal); - - float red = 0.5+0.5*reflection.r; - float green = 0.5+0.5*reflection.g; - float blue = 0.5+0.5*reflection.b; - - - //Diff = vec4(red, green, blue, 1.0); //Diff = vec4(reflection.rgb, 1.0); From 476b73375a0541c08d1443c2c6e1df030114ef33 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sun, 10 Mar 2019 23:25:49 +0100 Subject: [PATCH 06/18] Experimental raytracer with multisampling (todo: implement this properly) --- data/shaders/IBL.frag | 54 +++++++++++++------------ data/shaders/combine_diffuse_color.frag | 7 ++-- data/shaders/sp_solid.frag | 2 +- src/graphics/shader_based_renderer.cpp | 3 +- 4 files changed, 35 insertions(+), 31 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 2a255d341..468af4541 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -29,29 +29,23 @@ float makeLinear(float f, float n, float z) return (2 * n) / (f + n - z * (f - n)); } -vec3 fastBlur(vec2 uv) +vec3 fastBlur(vec2 uv, float spread) { vec4 sum = vec4(0.0); float X = uv.x; float Y = uv.y; - vec2 pixel = vec2(0.001); -/* - sum += textureLod(albedo, vec2(X, Y - 3.0 * pixel.y), 1.0) * 0.03125; - sum += textureLod(albedo, vec2(X, Y - 1.3333 * pixel.y), 1.0) * 0.328125; - sum += textureLod(albedo, vec2(X, Y), 1.0) * 0.273438; - sum += textureLod(albedo, vec2(X, Y + 1.3333 * pixel.y), 1.0) * 0.328125; - sum += textureLod(albedo, vec2(X, Y + 3.0 * pixel.y), 1.0) * 0.03125;*/ + vec2 pixel = vec2(spread); + float biais = 1.0; - sum += textureLod(albedo, vec2(X - 3.0 * pixel.x, Y), 1.0) * 0.03125; - sum += textureLod(albedo, vec2(X - 1.3333 * pixel.x, Y), 1.0) * 0.328125; - sum += textureLod(albedo, vec2(X, Y), 1.0) * 0.273438; - sum += textureLod(albedo, vec2(X + 1.3333 * pixel.x, Y), 1.0) * 0.328125; - sum += textureLod(albedo, vec2(X + 3.0 * pixel.x, Y), 1.0) * 0.03125; + sum += textureLod(albedo, vec2(X - 3.0 * pixel.x, Y), biais) * 0.03125; + sum += textureLod(albedo, vec2(X - 1.3333 * pixel.x, Y), biais) * 0.328125; + sum += textureLod(albedo, vec2(X, Y), biais) * 0.273438; + sum += textureLod(albedo, vec2(X + 1.3333 * pixel.x, Y), biais) * 0.328125; + sum += textureLod(albedo, vec2(X + 3.0 * pixel.x, Y), biais) * 0.03125; return sum.rgb; } - vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) { // Combine UV & depth into XY & Z (NDC) @@ -69,11 +63,11 @@ vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) } -vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback) +vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback, float spread) { dir *= 0.25f; - for(int i = 0; i < 10; ++i) { + for(int i = 0; i < 8; ++i) { hitCoord += dir; vec4 projectedCoord = u_projection_matrix * vec4(hitCoord, 1.0); @@ -89,12 +83,12 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth { // Mix with fallback (black area should be dark anyway) vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; - finalColor = fastBlur(projectedCoord.xy); + finalColor = fastBlur(projectedCoord.xy, spread); if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; - float vignette = 1. - dot(inside, inside) * 3; - vignette = clamp(pow(vignette, 10.0), 0., 0.2); + float vignette = 1. - dot(inside, inside) * 5; + vignette = clamp(pow(vignette, 10.0), 0., 1.0); return mix(fallback, finalColor, vignette); } else @@ -114,8 +108,15 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth +float rand(vec2 co) +{ + return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453); +} - +float rand2(vec2 co) +{ + return fract(sin(dot(co.xy,vec2(45.454545,5631.4))) * 43758.5453); +} // Main =================================================================== @@ -149,7 +150,12 @@ void main(void) View_Pos /= View_Pos.w; // Reflection vector + vec3 eyedir2 = eyedir; + eyedir2.y *= clamp(rand(uv), 0.01, 2.7); + vec3 normal2 = normal; + normal2.y = 0.5; //clamp(rand2(uv), 0.01, 2.7); vec3 reflected = normalize(reflect(eyedir, normal)); + vec3 reflected2 = normalize(reflect(eyedir2, normal2)); // Ray cast vec3 hitPos = View_Pos.xyz; @@ -157,13 +163,11 @@ void main(void) float minRayStep = 100.0f; // Fallback vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); - vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback); + vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); + vec3 outColor2 = RayCast(reflected2 * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); + outColor = mix(outColor, outColor2, 1.0 - specval); Spec = vec4(outColor.rgb, 1.0); - //Diff = vec4(red, green, blue, 1.0); - - //Diff = vec4(reflection.rgb, 1.0); - } diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 9f32a830e..71f0b2874 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -20,7 +20,7 @@ void main() // Polish map is stored in normal color framebuffer .z // Metallic map is stored in normal color framebuffer .w // Emit map is stored in diffuse color framebuffer.w - float metallicMapValue = texture(normal_color, tc).w; + float metallicMapValue = 0.0; //texture(normal_color, tc).w; float emitMapValue = diffuseMatColor.w; float ao = texture(ssao_tex, tc).x; @@ -28,11 +28,12 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.04), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), 1.0); + //color_1 = vec4(tmp, 1.); // Fog float depth = texture(depth_stencil, tc).x; @@ -58,5 +59,5 @@ void main() color_2.g = ls.g + color_1.g * (1.0 - ls.a); color_2.b = ls.b + color_1.b * (1.0 - ls.a); color_2.a = ls.a + color_1.a * (1.0 - ls.a); - o_final_color = vec4(SpecularComponent, 1.0); + o_final_color = vec4(color_2.rgb, 1.0); } diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index a620094fe..5f6d728d7 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -39,7 +39,7 @@ void main(void) o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_color.zw = layer_2.xy; - o_normal_color.z = 1.0; + o_normal_color.z = 0.1; #else o_diffuse_color = vec4(final_color, 1.0); #endif diff --git a/src/graphics/shader_based_renderer.cpp b/src/graphics/shader_based_renderer.cpp index 70924b3dd..b2834f05b 100644 --- a/src/graphics/shader_based_renderer.cpp +++ b/src/graphics/shader_based_renderer.cpp @@ -275,11 +275,10 @@ void ShaderBasedRenderer::renderSceneDeferred(scene::ICameraSceneNode * const ca { specular_probe = m_skybox->getSpecularProbe(); } - m_lighting_passes.renderLights( hasShadow, m_rtts->getRenderTarget(RTT_NORMAL_AND_DEPTH), m_rtts->getDepthStencilTexture(), - m_rtts->getRenderTarget(RTT_SP_DIFF_COLOR), + m_rtts->getRenderTarget(RTT_COLOR), m_rtts->getShadowFrameBuffer(), specular_probe); PROFILER_POP_CPU_MARKER(); From be577df2ecb65e550b6815e052aac7fdd11b9e63 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sun, 10 Mar 2019 23:46:18 +0100 Subject: [PATCH 07/18] Update with more probable values --- data/shaders/combine_diffuse_color.frag | 4 ++-- data/shaders/sp_solid.frag | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 71f0b2874..9aa4865b4 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -20,7 +20,7 @@ void main() // Polish map is stored in normal color framebuffer .z // Metallic map is stored in normal color framebuffer .w // Emit map is stored in diffuse color framebuffer.w - float metallicMapValue = 0.0; //texture(normal_color, tc).w; + float metallicMapValue = texture(normal_color, tc).w; float emitMapValue = diffuseMatColor.w; float ao = texture(ssao_tex, tc).x; @@ -28,7 +28,7 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.2), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index 5f6d728d7..e8d74a38d 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -39,7 +39,7 @@ void main(void) o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_color.zw = layer_2.xy; - o_normal_color.z = 0.1; + //o_normal_color.z = 0; #else o_diffuse_color = vec4(final_color, 1.0); #endif From bc49620b230875cacba6b8ee093adc5bdd3ef8f1 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 21:35:55 +0100 Subject: [PATCH 08/18] A few minor changes --- data/shaders/IBL.frag | 6 ++++-- data/shaders/combine_diffuse_color.frag | 2 +- data/shaders/sp_solid.frag | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 468af4541..b7be2db54 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -142,6 +142,9 @@ void main(void) int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y); vec3 FragPos = getXcYcZc(x, y, lineardepth); + // Fallback + vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); + // Better implementation: ::::::::::::::::::::::::::::::::::: float View_Depth = makeLinear(1000.0, 0.001, lineardepth); @@ -161,8 +164,7 @@ void main(void) vec3 hitPos = View_Pos.xyz; float dDepth; float minRayStep = 100.0f; - // Fallback - vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); + vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); vec3 outColor2 = RayCast(reflected2 * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); outColor = mix(outColor, outColor2, 1.0 - specval); diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 9aa4865b4..a870b16be 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -28,7 +28,7 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.2), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index e8d74a38d..b57f7e2c8 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -39,7 +39,7 @@ void main(void) o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_color.zw = layer_2.xy; - //o_normal_color.z = 0; + o_normal_color.z = 0.5; #else o_diffuse_color = vec4(final_color, 1.0); #endif From 5633d217a62f781619c04fbd8ad5cf39f24ff2b2 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 21:41:49 +0100 Subject: [PATCH 09/18] Reset the shader to fix the minimap --- data/shaders/combine_diffuse_color.frag | 5 ++--- data/shaders/sp_solid.frag | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index a870b16be..3afbec781 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -28,12 +28,11 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.04), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); vec4 color_1 = vec4(tmp * ao + (emitMapValue * emitCol), 1.0); - //color_1 = vec4(tmp, 1.); // Fog float depth = texture(depth_stencil, tc).x; @@ -59,5 +58,5 @@ void main() color_2.g = ls.g + color_1.g * (1.0 - ls.a); color_2.b = ls.b + color_1.b * (1.0 - ls.a); color_2.a = ls.a + color_1.a * (1.0 - ls.a); - o_final_color = vec4(color_2.rgb, 1.0); + o_final_color = color_2; } diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index b57f7e2c8..9a68416ed 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -39,7 +39,6 @@ void main(void) o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5; o_normal_color.zw = layer_2.xy; - o_normal_color.z = 0.5; #else o_diffuse_color = vec4(final_color, 1.0); #endif From 3cbac8e6eaa92eb7b061a1f54ac289dccaf34a27 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 21:43:19 +0100 Subject: [PATCH 10/18] Reset the shader to fix the minimap --- data/shaders/combine_diffuse_color.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 3afbec781..29debb220 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -28,7 +28,7 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.04), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); From 674a449c100a99596b5489f972502e2475a63a11 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 22:13:06 +0100 Subject: [PATCH 11/18] Reset the shader to fix the minimap --- data/shaders/IBL.frag | 22 +++++++++------------- data/shaders/combine_diffuse_color.frag | 3 ++- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index b7be2db54..6c10d804d 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -62,6 +62,11 @@ vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) return ViewPosition.xyz / ViewPosition.w; } +float rand(vec2 co) +{ + return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453); +} + vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback, float spread) { @@ -82,8 +87,8 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) { // Mix with fallback (black area should be dark anyway) - vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; - finalColor = fastBlur(projectedCoord.xy, spread); + //vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; + vec3 finalColor = fastBlur(projectedCoord.xy, spread); if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; @@ -106,13 +111,6 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth return fallback; } - - -float rand(vec2 co) -{ - return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453); -} - float rand2(vec2 co) { return fract(sin(dot(co.xy,vec2(45.454545,5631.4))) * 43758.5453); @@ -164,11 +162,9 @@ void main(void) vec3 hitPos = View_Pos.xyz; float dDepth; float minRayStep = 100.0f; - - vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); - vec3 outColor2 = RayCast(reflected2 * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.001); - outColor = mix(outColor, outColor2, 1.0 - specval); + vec3 outColor = RayCast(mix(reflected, reflected2, 1.0 - specval) * max(minRayStep, -View_Pos.z), + hitPos, dDepth, dtex, fallback, mix(0.001, 0.01, 1.0 - specval)); Spec = vec4(outColor.rgb, 1.0); diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 29debb220..1d60e0792 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -21,6 +21,7 @@ void main() // Metallic map is stored in normal color framebuffer .w // Emit map is stored in diffuse color framebuffer.w float metallicMapValue = texture(normal_color, tc).w; + float specMapValue = texture(normal_color, tc).z; float emitMapValue = diffuseMatColor.w; float ao = texture(ssao_tex, tc).x; @@ -28,7 +29,7 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(0.5), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(specMapValue), diffuse_color_for_mix, metallicMapValue); vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0); From 3291e23ca605e85a19c171944dcf9250ffd09dd5 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 22:13:56 +0100 Subject: [PATCH 12/18] Reset the shader to fix the minimap --- data/shaders/IBL.frag | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 6c10d804d..63be4fabd 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -87,8 +87,8 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) { // Mix with fallback (black area should be dark anyway) - //vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; - vec3 finalColor = fastBlur(projectedCoord.xy, spread); + vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; + //vec3 finalColor = fastBlur(projectedCoord.xy, spread); if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; From 104de39a04d2e2f6cb4d776b72e57d12b552cb15 Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 11 Mar 2019 22:27:15 +0100 Subject: [PATCH 13/18] Disable completly space screen reflexion for opengl-es (probably too slow for android --- data/shaders/IBL.frag | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 63be4fabd..9281ee09a 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -88,6 +88,7 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth { // Mix with fallback (black area should be dark anyway) vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; + // FIXME, this is heavy, needs to be an option in the settings //vec3 finalColor = fastBlur(projectedCoord.xy, spread); if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { @@ -132,8 +133,9 @@ void main(void) // Extract roughness float specval = texture(ntex, uv).z; +#ifdef GL_ES Spec = vec4(.25 * SpecularIBL(normal, eyedir, specval), 1.); - +#else // Compute Space Screen Reflection ========================================================= float lineardepth = textureLod(dtex, uv, 0.).x; @@ -167,5 +169,6 @@ void main(void) hitPos, dDepth, dtex, fallback, mix(0.001, 0.01, 1.0 - specval)); Spec = vec4(outColor.rgb, 1.0); +#endif } From b787c097051cacfdea1519b669ffd8698a5bcc0a Mon Sep 17 00:00:00 2001 From: samuncle Date: Fri, 31 May 2019 09:17:16 +0200 Subject: [PATCH 14/18] Remove useless code --- data/shaders/IBL.frag | 44 ++++--------------------------------------- 1 file changed, 4 insertions(+), 40 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 9281ee09a..bd21f900a 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -29,23 +29,6 @@ float makeLinear(float f, float n, float z) return (2 * n) / (f + n - z * (f - n)); } -vec3 fastBlur(vec2 uv, float spread) -{ - vec4 sum = vec4(0.0); - float X = uv.x; - float Y = uv.y; - vec2 pixel = vec2(spread); - float biais = 1.0; - - sum += textureLod(albedo, vec2(X - 3.0 * pixel.x, Y), biais) * 0.03125; - sum += textureLod(albedo, vec2(X - 1.3333 * pixel.x, Y), biais) * 0.328125; - sum += textureLod(albedo, vec2(X, Y), biais) * 0.273438; - sum += textureLod(albedo, vec2(X + 1.3333 * pixel.x, Y), biais) * 0.328125; - sum += textureLod(albedo, vec2(X + 3.0 * pixel.x, Y), biais) * 0.03125; - - return sum.rgb; -} - vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) { // Combine UV & depth into XY & Z (NDC) @@ -62,12 +45,6 @@ vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) return ViewPosition.xyz / ViewPosition.w; } -float rand(vec2 co) -{ - return fract(sin(dot(co.xy,vec2(12.9898,78.233))) * 43758.5453); -} - - vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback, float spread) { dir *= 0.25f; @@ -86,10 +63,8 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth { if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) { - // Mix with fallback (black area should be dark anyway) - vec3 finalColor = textureLod(albedo, projectedCoord.xy, 1.0).rgb; - // FIXME, this is heavy, needs to be an option in the settings - //vec3 finalColor = fastBlur(projectedCoord.xy, spread); + // FIXME We need to generate mipmap to take into account the gloss map + vec3 finalColor = textureLod(albedo, projectedCoord.xy, spread).rgb; if ((finalColor.r + finalColor.g + finalColor.b) > 0.) { vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; @@ -112,11 +87,6 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth return fallback; } -float rand2(vec2 co) -{ - return fract(sin(dot(co.xy,vec2(45.454545,5631.4))) * 43758.5453); -} - // Main =================================================================== void main(void) @@ -153,21 +123,15 @@ void main(void) View_Pos /= View_Pos.w; // Reflection vector - vec3 eyedir2 = eyedir; - eyedir2.y *= clamp(rand(uv), 0.01, 2.7); - vec3 normal2 = normal; - normal2.y = 0.5; //clamp(rand2(uv), 0.01, 2.7); vec3 reflected = normalize(reflect(eyedir, normal)); - vec3 reflected2 = normalize(reflect(eyedir2, normal2)); // Ray cast vec3 hitPos = View_Pos.xyz; float dDepth; float minRayStep = 100.0f; - vec3 outColor = RayCast(mix(reflected, reflected2, 1.0 - specval) * max(minRayStep, -View_Pos.z), - hitPos, dDepth, dtex, fallback, mix(0.001, 0.01, 1.0 - specval)); - + vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), + hitPos, dDepth, dtex, fallback, 0.0); Spec = vec4(outColor.rgb, 1.0); #endif From 187623b6a8132665ba7469fa07075112b60288fd Mon Sep 17 00:00:00 2001 From: samuncle Date: Fri, 31 May 2019 10:49:18 +0200 Subject: [PATCH 15/18] Update raytracer reflection to mitigate surface which are too glossy --- data/shaders/IBL.frag | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index bd21f900a..86b45c60d 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -132,6 +132,10 @@ void main(void) vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), hitPos, dDepth, dtex, fallback, 0.0); + + // TODO temporary measure the lack of mipmaping for RTT albedo + // Implement it in proper way + outColor = mix(fallback, outColor, specval); Spec = vec4(outColor.rgb, 1.0); #endif From 10eb549012f40cf5fb7fd00b23bd92540d80335c Mon Sep 17 00:00:00 2001 From: samuncle Date: Fri, 31 May 2019 12:25:18 +0200 Subject: [PATCH 16/18] soften the degraded ibl to make preview less dark --- data/shaders/degraded_ibl.frag | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/shaders/degraded_ibl.frag b/data/shaders/degraded_ibl.frag index b9d7a39c4..ec049f242 100644 --- a/data/shaders/degraded_ibl.frag +++ b/data/shaders/degraded_ibl.frag @@ -19,5 +19,5 @@ void main(void) vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.)); Diff = vec4(0.25 * DiffuseIBL(normal), 1.); - Spec = vec4(0., 0., 0., 1.); + Spec = vec4(0.031, 0.106, 0.173, 1.); } From 26a387ea36b6ccba7d96218ffb84315f018db51e Mon Sep 17 00:00:00 2001 From: samuncle Date: Mon, 24 Jun 2019 23:31:51 +0200 Subject: [PATCH 17/18] Update the reflection --- data/shaders/IBL.frag | 49 ++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 86b45c60d..b782b3c3d 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -32,7 +32,7 @@ float makeLinear(float f, float n, float z) vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) { // Combine UV & depth into XY & Z (NDC) - float z = makeLinear(1000.0, 0.01, textureLod(DepthMap, TexCoord, 0.).x); + float z = makeLinear(1000.0, 1.0, textureLod(DepthMap, TexCoord, 0.).x); vec3 rawPosition = vec3(TexCoord, z); // Convert from (0, 1) range to (-1, 1) @@ -45,6 +45,13 @@ vec3 CalcViewPositionFromDepth(in vec2 TexCoord, in sampler2D DepthMap) return ViewPosition.xyz / ViewPosition.w; } +float GetVignette(float factor) +{ + vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; + float vignette = 1. - dot(inside, inside) * 5; + return clamp(pow(vignette, factor), 0., 1.0); +} + vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D DepthMap, in vec3 fallback, float spread) { dir *= 0.25f; @@ -61,21 +68,25 @@ vec3 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth, in sampler2D Depth if(dDepth < 0.0) { - if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0)) + // Texture wrapping to extand artifcially the range of the lookup texture + // FIXME can be improved to lessen the distortion + projectedCoord.y = min(.99, projectedCoord.y); + projectedCoord.x = min(.99, projectedCoord.x); + projectedCoord.x = max(.01, projectedCoord.x); + + // We want only reflection on nearly horizontal surfaces + float cutout = dot(dir, vec3(0., 0., -1.)); + + if ((projectedCoord.x > 0.0 && projectedCoord.x < 1.0) + && (projectedCoord.y > 0.0 && projectedCoord.y < 1.0) + && (cutout > 10) + ) { // FIXME We need to generate mipmap to take into account the gloss map vec3 finalColor = textureLod(albedo, projectedCoord.xy, spread).rgb; - if ((finalColor.r + finalColor.g + finalColor.b) > 0.) - { - vec2 inside = (gl_FragCoord.xy / u_screen) - 0.5; - float vignette = 1. - dot(inside, inside) * 5; - vignette = clamp(pow(vignette, 10.0), 0., 1.0); - return mix(fallback, finalColor, vignette); - } - else - { - return fallback; - } + //return finalColor; + return mix(fallback, finalColor, GetVignette(4.)); + } else { @@ -106,18 +117,14 @@ void main(void) #ifdef GL_ES Spec = vec4(.25 * SpecularIBL(normal, eyedir, specval), 1.); #else - // Compute Space Screen Reflection ========================================================= + // :::::::: Compute Space Screen Reflection :::::::::::::::::::::::::::::::::::: float lineardepth = textureLod(dtex, uv, 0.).x; - int x = int(gl_FragCoord.x), y = int(gl_FragCoord.y); - vec3 FragPos = getXcYcZc(x, y, lineardepth); - // Fallback + // Fallback (if the ray can't find an intersection we display the sky) vec3 fallback = .25 * SpecularIBL(normal, eyedir, specval); - // Better implementation: ::::::::::::::::::::::::::::::::::: - - float View_Depth = makeLinear(1000.0, 0.001, lineardepth); + float View_Depth = makeLinear(1000.0, 1.0, lineardepth); vec3 ScreenPos = xpos.xyz; vec4 View_Pos = u_inverse_projection_matrix * vec4(ScreenPos, 1.0f); View_Pos /= View_Pos.w; @@ -130,7 +137,7 @@ void main(void) float dDepth; float minRayStep = 100.0f; - vec3 outColor = RayCast(reflected * max(minRayStep, -View_Pos.z), + vec3 outColor = RayCast(reflected * max(minRayStep, -xpos.z), hitPos, dDepth, dtex, fallback, 0.0); // TODO temporary measure the lack of mipmaping for RTT albedo From dfed11c6a8e59b791f18f3b228f4527451b751a6 Mon Sep 17 00:00:00 2001 From: samuncle Date: Tue, 25 Jun 2019 20:42:29 +0200 Subject: [PATCH 18/18] Bring back the legacy gloss computation (to disable once a proper fallback is done) --- data/shaders/combine_diffuse_color.frag | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/data/shaders/combine_diffuse_color.frag b/data/shaders/combine_diffuse_color.frag index 1d60e0792..a29e66fc5 100644 --- a/data/shaders/combine_diffuse_color.frag +++ b/data/shaders/combine_diffuse_color.frag @@ -29,7 +29,12 @@ void main() vec3 SpecularComponent = texture(specular_map, tc).xyz; vec3 diffuse_color_for_mix = diffuseMatColor.xyz * 4.0; - vec3 metallicMatColor = mix(vec3(specMapValue), diffuse_color_for_mix, metallicMapValue); + + // FIXME enable this once the fallback shader is properly done!!! + //vec3 metallicMatColor = mix(vec3(specMapValue), diffuse_color_for_mix, metallicMapValue); + vec3 metallicMatColor = mix(vec3(0.04), diffuse_color_for_mix, metallicMapValue); + // END FIXME + vec3 tmp = DiffuseComponent * mix(diffuseMatColor.xyz, vec3(0.0), metallicMapValue) + (metallicMatColor * SpecularComponent); vec3 emitCol = diffuseMatColor.xyz + (diffuseMatColor.xyz * diffuseMatColor.xyz * emitMapValue * emitMapValue * 10.0);