From 476b73375a0541c08d1443c2c6e1df030114ef33 Mon Sep 17 00:00:00 2001 From: samuncle Date: Sun, 10 Mar 2019 23:25:49 +0100 Subject: [PATCH] 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();